| 1 |
# Test that pruning and vacuuming pay attention to concurrent sessions |
| 2 |
# in the right way. For normal relations that means that rows cannot |
| 3 |
# be pruned away if there's an older snapshot, in contrast to that |
| 4 |
# temporary tables should nearly always be prunable. |
| 5 |
# |
| 6 |
# NB: Think hard before adding a test showing that rows in permanent |
| 7 |
# tables get pruned - it's quite likely that it'd be racy, e.g. due to |
| 8 |
# an autovacuum worker holding a snapshot. |
| 9 |
|
| 10 |
setup { |
| 11 |
CREATE OR REPLACE FUNCTION explain_json(p_query text) |
| 12 |
RETURNS json |
| 13 |
LANGUAGE plpgsql AS $$ |
| 14 |
DECLARE |
| 15 |
v_ret json; |
| 16 |
BEGIN |
| 17 |
EXECUTE p_query INTO STRICT v_ret; |
| 18 |
RETURN v_ret; |
| 19 |
END;$$; |
| 20 |
} |
| 21 |
|
| 22 |
teardown { |
| 23 |
DROP FUNCTION explain_json(text); |
| 24 |
} |
| 25 |
|
| 26 |
session lifeline |
| 27 |
|
| 28 |
# Start a transaction, force a snapshot to be held |
| 29 |
step ll_start |
| 30 |
{ |
| 31 |
BEGIN TRANSACTION ISOLATION LEVEL REPEATABLE READ; |
| 32 |
SELECT 1; |
| 33 |
} |
| 34 |
|
| 35 |
step ll_commit { COMMIT; } |
| 36 |
|
| 37 |
|
| 38 |
session pruner |
| 39 |
|
| 40 |
setup |
| 41 |
{ |
| 42 |
SET enable_seqscan = false; |
| 43 |
SET enable_bitmapscan = false; |
| 44 |
} |
| 45 |
|
| 46 |
step pruner_create_temp |
| 47 |
{ |
| 48 |
CREATE TEMPORARY TABLE horizons_tst (data int unique) WITH (autovacuum_enabled = off); |
| 49 |
INSERT INTO horizons_tst(data) VALUES(1),(2); |
| 50 |
} |
| 51 |
|
| 52 |
step pruner_create_perm |
| 53 |
{ |
| 54 |
CREATE TABLE horizons_tst (data int unique) WITH (autovacuum_enabled = off); |
| 55 |
INSERT INTO horizons_tst(data) VALUES(1),(2); |
| 56 |
} |
| 57 |
|
| 58 |
# Temp tables cannot be dropped in the teardown, so just always do so |
| 59 |
# as part of the permutation |
| 60 |
step pruner_drop |
| 61 |
{ |
| 62 |
DROP TABLE horizons_tst; |
| 63 |
} |
| 64 |
|
| 65 |
step pruner_delete |
| 66 |
{ |
| 67 |
DELETE FROM horizons_tst; |
| 68 |
} |
| 69 |
|
| 70 |
step pruner_begin { BEGIN; } |
| 71 |
step pruner_commit { COMMIT; } |
| 72 |
|
| 73 |
step pruner_vacuum |
| 74 |
{ |
| 75 |
VACUUM horizons_tst; |
| 76 |
} |
| 77 |
|
| 78 |
# Show the heap fetches of an ordered index-only-scan (other plans |
| 79 |
# have been forbidden above) - that tells us how many non-killed leaf |
| 80 |
# entries there are. |
| 81 |
step pruner_query |
| 82 |
{ |
| 83 |
SELECT explain_json($$ |
| 84 |
EXPLAIN (FORMAT json, BUFFERS, ANALYZE) |
| 85 |
SELECT * FROM horizons_tst ORDER BY data;$$)->0->'Plan'->'Heap Fetches'; |
| 86 |
} |
| 87 |
|
| 88 |
# Verify that the query plan still is an IOS |
| 89 |
step pruner_query_plan |
| 90 |
{ |
| 91 |
EXPLAIN (COSTS OFF) SELECT * FROM horizons_tst ORDER BY data; |
| 92 |
} |
| 93 |
|
| 94 |
|
| 95 |
# Show that with a permanent relation deleted rows cannot be pruned |
| 96 |
# away if there's a concurrent session still seeing the rows. |
| 97 |
permutation |
| 98 |
pruner_create_perm |
| 99 |
ll_start |
| 100 |
pruner_query_plan |
| 101 |
# Run query that could do pruning twice, first has chance to prune, |
| 102 |
# second would not perform heap fetches if first query did. |
| 103 |
pruner_query |
| 104 |
pruner_query |
| 105 |
pruner_delete |
| 106 |
pruner_query |
| 107 |
pruner_query |
| 108 |
ll_commit |
| 109 |
pruner_drop |
| 110 |
|
| 111 |
# Show that with a temporary relation deleted rows can be pruned away, |
| 112 |
# even if there's a concurrent session with a snapshot from before the |
| 113 |
# deletion. That's safe because the session with the older snapshot |
| 114 |
# cannot access the temporary table. |
| 115 |
permutation |
| 116 |
pruner_create_temp |
| 117 |
ll_start |
| 118 |
pruner_query_plan |
| 119 |
pruner_query |
| 120 |
pruner_query |
| 121 |
pruner_delete |
| 122 |
pruner_query |
| 123 |
pruner_query |
| 124 |
ll_commit |
| 125 |
pruner_drop |
| 126 |
|
| 127 |
# Verify that pruning in temporary relations doesn't remove rows still |
| 128 |
# visible in the current session |
| 129 |
permutation |
| 130 |
pruner_create_temp |
| 131 |
ll_start |
| 132 |
pruner_query |
| 133 |
pruner_query |
| 134 |
pruner_begin |
| 135 |
pruner_delete |
| 136 |
pruner_query |
| 137 |
pruner_query |
| 138 |
ll_commit |
| 139 |
pruner_commit |
| 140 |
pruner_drop |
| 141 |
|
| 142 |
# Show that vacuum cannot remove deleted rows still visible to another |
| 143 |
# session's snapshot, when accessing a permanent table. |
| 144 |
permutation |
| 145 |
pruner_create_perm |
| 146 |
ll_start |
| 147 |
pruner_query |
| 148 |
pruner_query |
| 149 |
pruner_delete |
| 150 |
pruner_vacuum |
| 151 |
pruner_query |
| 152 |
pruner_query |
| 153 |
ll_commit |
| 154 |
pruner_drop |
| 155 |
|
| 156 |
# Show that vacuum can remove deleted rows still visible to another |
| 157 |
# session's snapshot, when accessing a temporary table. |
| 158 |
permutation |
| 159 |
pruner_create_temp |
| 160 |
ll_start |
| 161 |
pruner_query |
| 162 |
pruner_query |
| 163 |
pruner_delete |
| 164 |
pruner_vacuum |
| 165 |
pruner_query |
| 166 |
pruner_query |
| 167 |
ll_commit |
| 168 |
pruner_drop |