|
25 | 25 | $node->safe_psql('postgres',
|
26 | 26 | "CREATE TABLESPACE $tbspace_name LOCATION '$tbspace_path';");
|
27 | 27 |
|
28 |
| -$node->issues_sql_like( |
29 |
| - [ 'reindexdb', 'postgres' ], |
30 |
| - qr/statement: REINDEX DATABASE postgres;/, |
31 |
| - 'SQL REINDEX run'); |
32 |
| - |
33 | 28 | # Use text as data type to get a toast table.
|
34 | 29 | $node->safe_psql('postgres',
|
35 | 30 | 'CREATE TABLE test1 (a text); CREATE INDEX test1x ON test1 (a);');
|
|
41 | 36 | "SELECT indexrelid::regclass FROM pg_index WHERE indrelid = '$toast_table'::regclass;"
|
42 | 37 | );
|
43 | 38 |
|
| 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 | + |
44 | 107 | $node->issues_sql_like(
|
45 | 108 | [ 'reindexdb', '-t', 'test1', 'postgres' ],
|
46 | 109 | qr/statement: REINDEX TABLE public\.test1;/,
|
|
57 | 120 | [ 'reindexdb', '-S', 'pg_catalog', 'postgres' ],
|
58 | 121 | qr/statement: REINDEX SCHEMA pg_catalog;/,
|
59 | 122 | 'reindex specific schema');
|
60 |
| -$node->issues_sql_like( |
61 |
| - [ 'reindexdb', '-s', 'postgres' ], |
62 |
| - qr/statement: REINDEX SYSTEM postgres;/, |
63 |
| - 'reindex system tables'); |
64 | 123 | $node->issues_sql_like(
|
65 | 124 | [ 'reindexdb', '-v', '-t', 'test1', 'postgres' ],
|
66 | 125 | qr/statement: REINDEX \(VERBOSE\) TABLE public\.test1;/,
|
|
0 commit comments