Guest User

Untitled

a guest
Sep 22nd, 2026
202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.05 KB | None | 0 0
  1.  
  2.  
  3.  
  4.  
  5.  
  6. DO
  7. $$
  8. DECLARE
  9. c INT;
  10. conn_str TEXT DEFAULT
  11. 'host=localhost port=5418 user=merlin.moncure dbname=postgres sslmode=disable';
  12. conn_count INT DEFAULT 50;
  13.  
  14. remaining INT;
  15. running BOOL[];
  16.  
  17. conn TEXT;
  18.  
  19. active INT DEFAULT 0;
  20.  
  21. timer TIMESTAMPTZ;
  22. inner_timer TIMESTAMPTZ;
  23.  
  24. returned TEXT[];
  25. _found BOOL;
  26.  
  27. num_queries INT DEFAULT 20000;
  28. queries INT DEFAULT num_queries;
  29.  
  30. v BOOL;
  31. r RECORD;
  32.  
  33.  
  34. _query TEXT DEFAULT 'SELECT 0';
  35.  
  36. BEGIN
  37. CREATE EXTENSION IF NOT EXISTS dblink;
  38.  
  39. PERFORM dblink_connect(
  40. 'test' || s,
  41. conn_str)
  42. FROM generate_series(1,conn_count) s;
  43.  
  44. RAISE NOTICE 'classic';
  45. timer := clock_timestamp();
  46.  
  47. LOOP
  48. IF queries = 0 AND active = 0
  49. THEN
  50. EXIT;
  51. END IF;
  52.  
  53. _found := false;
  54.  
  55. FOR c IN 1..conn_count
  56. LOOP
  57. conn := 'test' || c;
  58.  
  59. IF running[c] IS TRUE
  60. THEN
  61. IF NOT dblink_is_busy(conn) = 1
  62. THEN
  63. _found := true;
  64.  
  65. PERFORM * FROM dblink_get_result(conn) AS R(V TEXT);
  66. PERFORM * FROM dblink_get_result(conn) AS R(V TEXT);
  67.  
  68. running[c] := false;
  69. active := active - 1;
  70. END IF;
  71. END IF;
  72.  
  73. IF queries > 0
  74. THEN
  75. PERFORM dblink_send_query('test' || c, _query);
  76. running[c] := true;
  77. active := active + 1;
  78. queries := queries - 1;
  79.  
  80. IF queries % 1000 = 0
  81. THEN
  82. RAISE NOTICE '% queries to go!', queries;
  83. END IF;
  84. END IF;
  85. END LOOP;
  86.  
  87. IF NOT _found
  88. THEN
  89. PERFORM pg_sleep(.001);
  90. END IF;
  91. END LOOP;
  92.  
  93. queries := num_queries;
  94. running := array_agg(false) FROM generate_series(1, conn_count);
  95.  
  96. raise notice '%', clock_timestamp() - timer;
  97. timer := clock_timestamp();
  98.  
  99.  
  100. RAISE NOTICE 'modified';
  101. LOOP
  102. IF queries = 0 AND active = 0
  103. THEN
  104. EXIT;
  105. END IF;
  106.  
  107. IF active > 0
  108. THEN
  109. returned := dblink_wait_for_query(1.0, false);
  110.  
  111. FOREACH conn IN ARRAY returned
  112. LOOP
  113. PERFORM count(*) FROM dblink_get_result(conn) AS R(V TEXT);
  114. PERFORM * FROM dblink_get_result(conn) AS R(V TEXT);
  115.  
  116. c := substring(conn, 5)::INT;
  117.  
  118. active := active - 1;
  119. running[c] := false;
  120. END LOOP;
  121. END IF;
  122.  
  123. IF queries > 0
  124. THEN
  125. FOR r IN
  126. SELECT * FROM
  127. (
  128. SELECT * FROM unnest(running) WITH ORDINALITY AS R(x, n)
  129. )
  130. WHERE x IS FALSE
  131. LOOP
  132. PERFORM dblink_send_query('test' || r.n, _query);
  133.  
  134. running[r.n] := true;
  135. active := active + 1;
  136. queries := queries - 1;
  137.  
  138. IF queries % 1000 = 0
  139. THEN
  140. RAISE NOTICE '% queries to go! % active', queries, active;
  141. END IF;
  142.  
  143. IF queries = 0 THEN
  144. EXIT;
  145. END IF;
  146. END LOOP;
  147. END IF;
  148. END LOOP;
  149.  
  150. raise notice '%', clock_timestamp() - timer;
  151.  
  152. PERFORM dblink_disconnect('test' || s) FROM generate_series(1,conn_count) s;
  153.  
  154. END;
  155. $$ LANGUAGE PLPGSQL;
  156.  
  157.  
Advertisement
Add Comment
Please, Sign In to add comment