Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 5fb5b6c

Browse files
committed
Add more tests for REINDEX DATABASE/SYSTEM with relfilenode changes
Adding such commands in the main regression test suite is not a good approach performance-wise as it impacts all the objects in the regression database, so this additional coverage is added in the TAP tests of reindexdb where we already run a few REINDEX commands with SYSTEM and DATABASE so there is no runtime difference for the test. This additional coverage checks which relations are rewritten with relfilenode changes, as of: - a toast index in user table. - a toast index in catalog table. - a catalog index. - a user index. This test suite is something I have implemented for a separate patch that reworks a bit the way we handle these two REINDEX behaviors, but it has enough value in itself to be in a separate commit. This also makes easier to follow what actually changes once the REINDEX logic is reworked (currently, DABATASE rewrites both catalog and user tables, and SYSTEM works only on catalogs). Discussion: https://postgr.es/m/YtOqA7ldcJQADEE8@paquier.xyz
1 parent 950e64f commit 5fb5b6c

File tree

1 file changed

+68
-9
lines changed

1 file changed

+68
-9
lines changed

src/bin/scripts/t/090_reindexdb.pl

+68-9
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,6 @@
2525
$node->safe_psql('postgres',
2626
"CREATE TABLESPACE $tbspace_name LOCATION '$tbspace_path';");
2727

28-
$node->issues_sql_like(
29-
[ 'reindexdb', 'postgres' ],
30-
qr/statement: REINDEX DATABASE postgres;/,
31-
'SQL REINDEX run');
32-
3328
# Use text as data type to get a toast table.
3429
$node->safe_psql('postgres',
3530
'CREATE TABLE test1 (a text); CREATE INDEX test1x ON test1 (a);');
@@ -41,6 +36,74 @@
4136
"SELECT indexrelid::regclass FROM pg_index WHERE indrelid = '$toast_table'::regclass;"
4237
);
4338

39+
# Set of SQL queries to cross-check the state of relfilenodes across
40+
# REINDEX operations. A set of relfilenodes is saved from the catalogs
41+
# and then compared with pg_class.
42+
$node->safe_psql('postgres',
43+
'CREATE TABLE toast_relfilenodes (parent regclass, indname regclass, relfilenode oid);'
44+
);
45+
# Save the relfilenode of a set of toast indexes, one from the catalog
46+
# pg_constraint and one from the test table.
47+
my $fetch_toast_relfilenodes =
48+
qq{SELECT b.oid::regclass, c.oid::regclass, c.relfilenode
49+
FROM pg_class a
50+
JOIN pg_class b ON (a.oid = b.reltoastrelid)
51+
JOIN pg_index i on (a.oid = i.indrelid)
52+
JOIN pg_class c on (i.indexrelid = c.oid)
53+
WHERE b.oid IN ('pg_constraint'::regclass, 'test1'::regclass)};
54+
# Same for relfilenodes of normal indexes. This saves the relfilenode
55+
# from an index of pg_constraint, and from the index of the test table.
56+
my $fetch_index_relfilenodes = qq{SELECT i.indrelid, a.oid, a.relfilenode
57+
FROM pg_class a
58+
JOIN pg_index i ON (i.indexrelid = a.oid)
59+
WHERE a.relname IN ('pg_constraint_oid_index', 'test1x')};
60+
my $save_relfilenodes =
61+
"INSERT INTO toast_relfilenodes $fetch_toast_relfilenodes;"
62+
. "INSERT INTO toast_relfilenodes $fetch_index_relfilenodes;";
63+
64+
# Query to compare a set of relfilenodes saved with the contents of pg_class.
65+
# Note that this does not join using OIDs, as CONCURRENTLY would change them
66+
# when reindexing. A filter is applied on the toast index names, even if this
67+
# does not make a difference between the catalog and normal ones. The ordering
68+
# based on the name is enough to ensure a fixed output, where the name of the
69+
# parent table is included to provide more context.
70+
my $compare_relfilenodes = qq(SELECT b.parent::regclass,
71+
regexp_replace(b.indname::text, '(pg_toast.pg_toast_)\\d{4,5}(_index)', '\\1<oid>\\2'),
72+
CASE WHEN a.relfilenode = b.relfilenode THEN 'relfilenode is unchanged'
73+
ELSE 'relfilenode has changed' END
74+
FROM toast_relfilenodes b
75+
JOIN pg_class a ON b.indname::text = a.oid::regclass::text
76+
ORDER BY b.parent::text, b.indname::text);
77+
78+
# Save the set of relfilenodes and compare them.
79+
$node->safe_psql('postgres', $save_relfilenodes);
80+
$node->issues_sql_like(
81+
[ 'reindexdb', 'postgres' ],
82+
qr/statement: REINDEX DATABASE postgres;/,
83+
'SQL REINDEX run');
84+
my $relnode_info = $node->safe_psql('postgres', $compare_relfilenodes);
85+
is( $relnode_info,
86+
qq(pg_constraint|pg_constraint_oid_index|relfilenode has changed
87+
pg_constraint|pg_toast.pg_toast_<oid>_index|relfilenode has changed
88+
test1|pg_toast.pg_toast_<oid>_index|relfilenode has changed
89+
test1|test1x|relfilenode has changed),
90+
'relfilenode change after REINDEX DATABASE');
91+
92+
# Re-save and run the second one.
93+
$node->safe_psql('postgres',
94+
"TRUNCATE toast_relfilenodes; $save_relfilenodes");
95+
$node->issues_sql_like(
96+
[ 'reindexdb', '-s', 'postgres' ],
97+
qr/statement: REINDEX SYSTEM postgres;/,
98+
'reindex system tables');
99+
$relnode_info = $node->safe_psql('postgres', $compare_relfilenodes);
100+
is( $relnode_info,
101+
qq(pg_constraint|pg_constraint_oid_index|relfilenode has changed
102+
pg_constraint|pg_toast.pg_toast_<oid>_index|relfilenode has changed
103+
test1|pg_toast.pg_toast_<oid>_index|relfilenode is unchanged
104+
test1|test1x|relfilenode is unchanged),
105+
'relfilenode change after REINDEX SYSTEM');
106+
44107
$node->issues_sql_like(
45108
[ 'reindexdb', '-t', 'test1', 'postgres' ],
46109
qr/statement: REINDEX TABLE public\.test1;/,
@@ -57,10 +120,6 @@
57120
[ 'reindexdb', '-S', 'pg_catalog', 'postgres' ],
58121
qr/statement: REINDEX SCHEMA pg_catalog;/,
59122
'reindex specific schema');
60-
$node->issues_sql_like(
61-
[ 'reindexdb', '-s', 'postgres' ],
62-
qr/statement: REINDEX SYSTEM postgres;/,
63-
'reindex system tables');
64123
$node->issues_sql_like(
65124
[ 'reindexdb', '-v', '-t', 'test1', 'postgres' ],
66125
qr/statement: REINDEX \(VERBOSE\) TABLE public\.test1;/,

0 commit comments

Comments
 (0)