| 1 |
# Test for vacuum's reduced processing of heap pages (used for any heap page |
| 2 |
# where a cleanup lock isn't immediately available) |
| 3 |
# |
| 4 |
# Debugging tip: Change VACUUM to VACUUM VERBOSE to get feedback on what's |
| 5 |
# really going on |
| 6 |
|
| 7 |
# Use name type here to avoid TOAST table: |
| 8 |
setup |
| 9 |
{ |
| 10 |
CREATE TABLE smalltbl AS SELECT i AS id, 't'::name AS t FROM generate_series(1,20) i; |
| 11 |
ALTER TABLE smalltbl SET (autovacuum_enabled = off); |
| 12 |
ALTER TABLE smalltbl ADD PRIMARY KEY (id); |
| 13 |
} |
| 14 |
setup |
| 15 |
{ |
| 16 |
VACUUM ANALYZE smalltbl; |
| 17 |
} |
| 18 |
|
| 19 |
teardown |
| 20 |
{ |
| 21 |
DROP TABLE smalltbl; |
| 22 |
} |
| 23 |
|
| 24 |
# This session holds a pin on smalltbl's only heap page: |
| 25 |
session pinholder |
| 26 |
step pinholder_cursor |
| 27 |
{ |
| 28 |
BEGIN; |
| 29 |
DECLARE c1 CURSOR FOR SELECT 1 AS dummy FROM smalltbl; |
| 30 |
FETCH NEXT FROM c1; |
| 31 |
} |
| 32 |
step pinholder_commit |
| 33 |
{ |
| 34 |
COMMIT; |
| 35 |
} |
| 36 |
|
| 37 |
# This session inserts and deletes tuples, potentially affecting reltuples: |
| 38 |
session dml |
| 39 |
step dml_insert |
| 40 |
{ |
| 41 |
INSERT INTO smalltbl SELECT max(id) + 1 FROM smalltbl; |
| 42 |
} |
| 43 |
step dml_delete |
| 44 |
{ |
| 45 |
DELETE FROM smalltbl WHERE id = (SELECT min(id) FROM smalltbl); |
| 46 |
} |
| 47 |
step dml_begin { BEGIN; } |
| 48 |
step dml_key_share { SELECT id FROM smalltbl WHERE id = 3 FOR KEY SHARE; } |
| 49 |
step dml_commit { COMMIT; } |
| 50 |
|
| 51 |
# Needed for Multixact test: |
| 52 |
session dml_other |
| 53 |
step dml_other_begin { BEGIN; } |
| 54 |
step dml_other_key_share { SELECT id FROM smalltbl WHERE id = 3 FOR KEY SHARE; } |
| 55 |
step dml_other_update { UPDATE smalltbl SET t = 'u' WHERE id = 3; } |
| 56 |
step dml_other_commit { COMMIT; } |
| 57 |
|
| 58 |
# This session runs non-aggressive VACUUM, but with maximally aggressive |
| 59 |
# cutoffs for tuple freezing (e.g., FreezeLimit == OldestXmin): |
| 60 |
session vacuumer |
| 61 |
setup |
| 62 |
{ |
| 63 |
SET vacuum_freeze_min_age = 0; |
| 64 |
SET vacuum_multixact_freeze_min_age = 0; |
| 65 |
} |
| 66 |
step vacuumer_nonaggressive_vacuum |
| 67 |
{ |
| 68 |
VACUUM smalltbl; |
| 69 |
} |
| 70 |
step vacuumer_pg_class_stats |
| 71 |
{ |
| 72 |
SELECT relpages, reltuples FROM pg_class WHERE oid = 'smalltbl'::regclass; |
| 73 |
} |
| 74 |
|
| 75 |
# Test VACUUM's reltuples counting mechanism. |
| 76 |
# |
| 77 |
# Final pg_class.reltuples should never be affected by VACUUM's inability to |
| 78 |
# get a cleanup lock on any page, except to the extent that any cleanup lock |
| 79 |
# contention changes the number of tuples that remain ("missed dead" tuples |
| 80 |
# are counted in reltuples, much like "recently dead" tuples). |
| 81 |
|
| 82 |
# Easy case: |
| 83 |
permutation |
| 84 |
vacuumer_pg_class_stats # Start with 20 tuples |
| 85 |
dml_insert |
| 86 |
vacuumer_nonaggressive_vacuum |
| 87 |
vacuumer_pg_class_stats # End with 21 tuples |
| 88 |
|
| 89 |
# Harder case -- count 21 tuples at the end (like last time), but with cleanup |
| 90 |
# lock contention this time: |
| 91 |
permutation |
| 92 |
vacuumer_pg_class_stats # Start with 20 tuples |
| 93 |
dml_insert |
| 94 |
pinholder_cursor |
| 95 |
vacuumer_nonaggressive_vacuum |
| 96 |
vacuumer_pg_class_stats # End with 21 tuples |
| 97 |
pinholder_commit # order doesn't matter |
| 98 |
|
| 99 |
# Same as "harder case", but vary the order, and delete an inserted row: |
| 100 |
permutation |
| 101 |
vacuumer_pg_class_stats # Start with 20 tuples |
| 102 |
pinholder_cursor |
| 103 |
dml_insert |
| 104 |
dml_delete |
| 105 |
dml_insert |
| 106 |
vacuumer_nonaggressive_vacuum |
| 107 |
# reltuples is 21 here again -- "recently dead" tuple won't be included in |
| 108 |
# count here: |
| 109 |
vacuumer_pg_class_stats |
| 110 |
pinholder_commit # order doesn't matter |
| 111 |
|
| 112 |
# Same as "harder case", but initial insert and delete before cursor: |
| 113 |
permutation |
| 114 |
vacuumer_pg_class_stats # Start with 20 tuples |
| 115 |
dml_insert |
| 116 |
dml_delete |
| 117 |
pinholder_cursor |
| 118 |
dml_insert |
| 119 |
vacuumer_nonaggressive_vacuum |
| 120 |
# reltuples is 21 here again -- "missed dead" tuple ("recently dead" when |
| 121 |
# concurrent activity held back VACUUM's OldestXmin) won't be included in |
| 122 |
# count here: |
| 123 |
vacuumer_pg_class_stats |
| 124 |
pinholder_commit # order doesn't matter |
| 125 |
|
| 126 |
# Test VACUUM's mechanism for skipping MultiXact freezing. |
| 127 |
# |
| 128 |
# This provides test coverage for code paths that are only hit when we need to |
| 129 |
# freeze, but inability to acquire a cleanup lock on a heap page makes |
| 130 |
# freezing some XIDs/MXIDs < FreezeLimit/MultiXactCutoff impossible (without |
| 131 |
# waiting for a cleanup lock, which non-aggressive VACUUM is unwilling to do). |
| 132 |
permutation |
| 133 |
dml_begin |
| 134 |
dml_other_begin |
| 135 |
dml_key_share |
| 136 |
dml_other_key_share |
| 137 |
# Will get cleanup lock, can't advance relminmxid yet: |
| 138 |
# (though will usually advance relfrozenxid by ~2 XIDs) |
| 139 |
vacuumer_nonaggressive_vacuum |
| 140 |
pinholder_cursor |
| 141 |
dml_other_update |
| 142 |
dml_commit |
| 143 |
dml_other_commit |
| 144 |
# Can't cleanup lock, so still can't advance relminmxid here: |
| 145 |
# (relfrozenxid held back by XIDs in MultiXact too) |
| 146 |
vacuumer_nonaggressive_vacuum |
| 147 |
pinholder_commit |
| 148 |
# Pin was dropped, so will advance relminmxid, at long last: |
| 149 |
# (ditto for relfrozenxid advancement) |
| 150 |
vacuumer_nonaggressive_vacuum |