2016-12-19 4:21 GMT+05:00 David Fetter <da...@fetter.org>: > Couldn't it sleep in increments smaller than a second? Like maybe > milliseconds? Also, it's probably cleaner (or at least more > comprehensible) to write something using format() and dollar quoting > than the line with the double 's.
Right. Here's version which waits for half a second. I do not see how to path doubles via dollar sign, pg_background_launch() gets a string as a parameter, it's not EXCEUTE USING. Best regards, Andrey Borodin.
diff --git a/contrib/pg_background/Makefile b/contrib/pg_background/Makefile index c4e717d..085fbff 100644 --- a/contrib/pg_background/Makefile +++ b/contrib/pg_background/Makefile @@ -6,6 +6,8 @@ OBJS = pg_background.o EXTENSION = pg_background DATA = pg_background--1.0.sql +REGRESS = pg_background + ifdef USE_PGXS PG_CONFIG = pg_config PGXS := $(shell $(PG_CONFIG) --pgxs) diff --git a/contrib/pg_background/expected/pg_background.out b/contrib/pg_background/expected/pg_background.out new file mode 100644 index 0000000..a6613ce --- /dev/null +++ b/contrib/pg_background/expected/pg_background.out @@ -0,0 +1,31 @@ +CREATE EXTENSION pg_background; +--run 6 workers which wait .0, .1, .2, .3, .4, .5 seconds respectively +CREATE TABLE input as SELECT x FROM generate_series(0,.5,0.1) x ORDER BY x DESC; +CREATE TABLE output(place int,value float); +--sequence for indication of who's finished on which place +CREATE sequence s start 1; +CREATE TABLE handles as SELECT pg_background_launch(format('select pg_sleep(%s); insert into output values (nextval(''s''),%s);',x,x)) h FROM input; +--wait until everyone finishes +SELECT (SELECT * FROM pg_background_result(h) as (x text) limit 1) FROM handles; + x +---------- + SELECT 1 + SELECT 1 + SELECT 1 + SELECT 1 + SELECT 1 + SELECT 1 +(6 rows) + +--output results +SELECT * FROM output ORDER BY place; + place | value +-------+------- + 1 | 0 + 2 | 0.1 + 3 | 0.2 + 4 | 0.3 + 5 | 0.4 + 6 | 0.5 +(6 rows) + diff --git a/contrib/pg_background/sql/pg_background.sql b/contrib/pg_background/sql/pg_background.sql new file mode 100644 index 0000000..770f0b8 --- /dev/null +++ b/contrib/pg_background/sql/pg_background.sql @@ -0,0 +1,12 @@ +CREATE EXTENSION pg_background; + +--run 6 workers which wait .0, .1, .2, .3, .4, .5 seconds respectively +CREATE TABLE input as SELECT x FROM generate_series(0,.5,0.1) x ORDER BY x DESC; +CREATE TABLE output(place int,value float); +--sequence for indication of who's finished on which place +CREATE sequence s start 1; +CREATE TABLE handles as SELECT pg_background_launch(format('select pg_sleep(%s); insert into output values (nextval(''s''),%s);',x,x)) h FROM input; +--wait until everyone finishes +SELECT (SELECT * FROM pg_background_result(h) as (x text) limit 1) FROM handles; +--output results +SELECT * FROM output ORDER BY place;
-- Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-hackers