|
| 1 | +setup |
| 2 | +{ |
| 3 | + CREATE EXTENSION injection_points; |
| 4 | + CREATE EXTENSION pg_rewrite; |
| 5 | + |
| 6 | + CREATE TABLE tbl_src(i int primary key, t text); |
| 7 | + |
| 8 | + INSERT INTO tbl_src(i, t) |
| 9 | + SELECT x, string_agg(random()::text, '') |
| 10 | + FROM generate_series(1, 2) g(x), generate_series(1, 200) h(y) |
| 11 | + GROUP BY x; |
| 12 | + |
| 13 | + CREATE TABLE tbl_dst(i int primary key, t text); |
| 14 | +} |
| 15 | + |
| 16 | +teardown |
| 17 | +{ |
| 18 | + DROP EXTENSION injection_points; |
| 19 | + DROP EXTENSION pg_rewrite; |
| 20 | + DROP TABLE tbl_src; |
| 21 | + DROP TABLE tbl_src_old; |
| 22 | +} |
| 23 | + |
| 24 | +session s1 |
| 25 | +setup |
| 26 | +{ |
| 27 | + SELECT injection_points_attach('pg_rewrite-before-lock', 'wait'); |
| 28 | + SELECT injection_points_attach('pg_rewrite-after-commit', 'wait'); |
| 29 | +} |
| 30 | +# Perform the initial load and wait for s2 to do some data changes. |
| 31 | +# |
| 32 | +# Since pg_rewrite uses background worker, the isolation tester does not |
| 33 | +# recognize that the session waits on an injection point (because the worker |
| 34 | +# is who waits). Therefore use rewrite_table_nowait(), which only launches the |
| 35 | +# worker and goes on. The 'wait_for_s1_sleep' step below then checks until the |
| 36 | +# waiting started. |
| 37 | +step do_rewrite |
| 38 | +{ |
| 39 | + SELECT rewrite_table_nowait('tbl_src', 'tbl_dst', 'tbl_src_old'); |
| 40 | +} |
| 41 | +# Check the data. |
| 42 | +step do_check |
| 43 | +{ |
| 44 | + TABLE pg_rewrite_progress; |
| 45 | + |
| 46 | + -- Each row should contain TOASTed value. |
| 47 | + SELECT count(*) FROM tbl_src WHERE pg_column_toast_chunk_id(t) ISNULL; |
| 48 | + |
| 49 | + -- The contents of the new table should be identical to that of the old |
| 50 | + -- one. |
| 51 | + SELECT count(*) |
| 52 | + FROM tbl_src t1 JOIN tbl_src_old t2 ON t1.i = t2.i |
| 53 | + WHERE t1.t <> t2.t; |
| 54 | +} |
| 55 | + |
| 56 | +session s2 |
| 57 | +# Since s1 uses background worker, the backend executing 'wait_before_lock' |
| 58 | +# does not appear to be waiting on the injection point. Instead we need to |
| 59 | +# check explicitly if the waiting on the injection point is in progress, and |
| 60 | +# wait if it's not. |
| 61 | +step wait_for_before_lock_ip |
| 62 | +{ |
| 63 | +DO $$ |
| 64 | +BEGIN |
| 65 | + LOOP |
| 66 | + PERFORM pg_stat_clear_snapshot(); |
| 67 | + |
| 68 | + PERFORM |
| 69 | + FROM pg_stat_activity |
| 70 | + WHERE (wait_event_type, wait_event)=('InjectionPoint', 'pg_rewrite-before-lock'); |
| 71 | + |
| 72 | + IF FOUND THEN |
| 73 | + EXIT; |
| 74 | + END IF; |
| 75 | + |
| 76 | + PERFORM pg_sleep(.1); |
| 77 | + END LOOP; |
| 78 | +END; |
| 79 | +$$; |
| 80 | +} |
| 81 | +step do_changes |
| 82 | +{ |
| 83 | + INSERT INTO tbl_src(i, t) |
| 84 | + SELECT 5, string_agg(random()::text, '') |
| 85 | + FROM generate_series(1, 200) h(y); |
| 86 | + |
| 87 | + UPDATE tbl_src SET t = t || 'x' WHERE i = 1; |
| 88 | +} |
| 89 | +step wakeup_before_lock_ip |
| 90 | +{ |
| 91 | + SELECT injection_points_wakeup('pg_rewrite-before-lock'); |
| 92 | +} |
| 93 | +# Wait until the concurrent changes have been committed by the pg_rewrite |
| 94 | +# worker. |
| 95 | +step wait_for_after_commit_ip |
| 96 | +{ |
| 97 | +DO $$ |
| 98 | +BEGIN |
| 99 | + LOOP |
| 100 | + PERFORM pg_stat_clear_snapshot(); |
| 101 | + |
| 102 | + PERFORM |
| 103 | + FROM pg_stat_activity |
| 104 | + WHERE (wait_event_type, wait_event)=('InjectionPoint', 'pg_rewrite-after-commit'); |
| 105 | + |
| 106 | + IF FOUND THEN |
| 107 | + EXIT; |
| 108 | + END IF; |
| 109 | + |
| 110 | + PERFORM pg_sleep(.1); |
| 111 | + END LOOP; |
| 112 | +END; |
| 113 | +$$; |
| 114 | +} |
| 115 | +# Like wakeup_before_lock_ip above. |
| 116 | +step wakeup_after_commit_ip |
| 117 | +{ |
| 118 | + SELECT injection_points_wakeup('pg_rewrite-after-commit'); |
| 119 | +} |
| 120 | +teardown |
| 121 | +{ |
| 122 | + SELECT injection_points_detach('pg_rewrite-before-lock'); |
| 123 | + SELECT injection_points_detach('pg_rewrite-after-commit'); |
| 124 | +} |
| 125 | + |
| 126 | +permutation |
| 127 | + do_rewrite |
| 128 | + wait_for_before_lock_ip |
| 129 | + do_changes |
| 130 | + wakeup_before_lock_ip |
| 131 | + wait_for_after_commit_ip |
| 132 | + do_check |
| 133 | + wakeup_after_commit_ip |
0 commit comments