Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- DO
- $$
- DECLARE
- c INT;
- conn_str TEXT DEFAULT
- 'host=localhost port=5418 user=merlin.moncure dbname=postgres sslmode=disable';
- conn_count INT DEFAULT 50;
- remaining INT;
- running BOOL[];
- conn TEXT;
- active INT DEFAULT 0;
- timer TIMESTAMPTZ;
- inner_timer TIMESTAMPTZ;
- returned TEXT[];
- _found BOOL;
- num_queries INT DEFAULT 20000;
- queries INT DEFAULT num_queries;
- v BOOL;
- r RECORD;
- _query TEXT DEFAULT 'SELECT 0';
- BEGIN
- CREATE EXTENSION IF NOT EXISTS dblink;
- PERFORM dblink_connect(
- 'test' || s,
- conn_str)
- FROM generate_series(1,conn_count) s;
- RAISE NOTICE 'classic';
- timer := clock_timestamp();
- LOOP
- IF queries = 0 AND active = 0
- THEN
- EXIT;
- END IF;
- _found := false;
- FOR c IN 1..conn_count
- LOOP
- conn := 'test' || c;
- IF running[c] IS TRUE
- THEN
- IF NOT dblink_is_busy(conn) = 1
- THEN
- _found := true;
- PERFORM * FROM dblink_get_result(conn) AS R(V TEXT);
- PERFORM * FROM dblink_get_result(conn) AS R(V TEXT);
- running[c] := false;
- active := active - 1;
- END IF;
- END IF;
- IF queries > 0
- THEN
- PERFORM dblink_send_query('test' || c, _query);
- running[c] := true;
- active := active + 1;
- queries := queries - 1;
- IF queries % 1000 = 0
- THEN
- RAISE NOTICE '% queries to go!', queries;
- END IF;
- END IF;
- END LOOP;
- IF NOT _found
- THEN
- PERFORM pg_sleep(.001);
- END IF;
- END LOOP;
- queries := num_queries;
- running := array_agg(false) FROM generate_series(1, conn_count);
- raise notice '%', clock_timestamp() - timer;
- timer := clock_timestamp();
- RAISE NOTICE 'modified';
- LOOP
- IF queries = 0 AND active = 0
- THEN
- EXIT;
- END IF;
- IF active > 0
- THEN
- returned := dblink_wait_for_query(1.0, false);
- FOREACH conn IN ARRAY returned
- LOOP
- PERFORM count(*) FROM dblink_get_result(conn) AS R(V TEXT);
- PERFORM * FROM dblink_get_result(conn) AS R(V TEXT);
- c := substring(conn, 5)::INT;
- active := active - 1;
- running[c] := false;
- END LOOP;
- END IF;
- IF queries > 0
- THEN
- FOR r IN
- SELECT * FROM
- (
- SELECT * FROM unnest(running) WITH ORDINALITY AS R(x, n)
- )
- WHERE x IS FALSE
- LOOP
- PERFORM dblink_send_query('test' || r.n, _query);
- running[r.n] := true;
- active := active + 1;
- queries := queries - 1;
- IF queries % 1000 = 0
- THEN
- RAISE NOTICE '% queries to go! % active', queries, active;
- END IF;
- IF queries = 0 THEN
- EXIT;
- END IF;
- END LOOP;
- END IF;
- END LOOP;
- raise notice '%', clock_timestamp() - timer;
- PERFORM dblink_disconnect('test' || s) FROM generate_series(1,conn_count) s;
- END;
- $$ LANGUAGE PLPGSQL;
Advertisement
Add Comment
Please, Sign In to add comment