Skip to content

Instantly share code, notes, and snippets.

@emiel
Created August 1, 2017 18:32
Show Gist options
  • Save emiel/4d4fd8c5c5ba9173e3175a08bb055be4 to your computer and use it in GitHub Desktop.
Save emiel/4d4fd8c5c5ba9173e3175a08bb055be4 to your computer and use it in GitHub Desktop.
create table queue
(
id integer,
diff integer
);
create table cache
(
id integer primary key,
count integer not null,
last_update_at timestamptz
);
create or replace function process_queue()
returns bool language plpgsql
as $body$
begin
insert into cache (id, count, last_update_at)
select id, sum(diff), now()
from queue
group by id
on conflict (id) do update set
count = cache.count + EXCLUDED.count,
last_update_at = EXCLUDED.last_update_at
;
-- removing the truncate line resolves the issue
truncate queue;
return true;
end;
$body$;
-- Need to invoke the following a number of times to trigger the segfault in cstore_fdw.so
-- No data is required
select process_queue();
select process_queue();
select process_queue();
select process_queue();
select process_queue();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment