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

Commit c3b011d

Browse files
committed
Support amcheck of sequences
Sequences were left out of the list of relation kinds that verify_heapam knew how to check, though it is fairly trivial to allow them. Doing that, and while at it, updating pg_amcheck to include sequences in relations matched by table and relation patterns. Author: Mark Dilger <mark.dilger@enterprisedb.com> Discussion: https://www.postgresql.org/message-id/flat/81ad4757-92c1-4aa3-7bee-f609544837e3%40enterprisedb.com
1 parent 7d1aa6b commit c3b011d

File tree

6 files changed

+95
-18
lines changed

6 files changed

+95
-18
lines changed

contrib/amcheck/expected/check_heap.out

+4-2
Original file line numberDiff line numberDiff line change
@@ -180,8 +180,10 @@ CREATE SEQUENCE test_sequence;
180180
SELECT * FROM verify_heapam('test_sequence',
181181
startblock := NULL,
182182
endblock := NULL);
183-
ERROR: cannot check relation "test_sequence"
184-
DETAIL: This operation is not supported for sequences.
183+
blkno | offnum | attnum | msg
184+
-------+--------+--------+-----
185+
(0 rows)
186+
185187
-- Check that foreign tables are rejected
186188
CREATE FOREIGN DATA WRAPPER dummy;
187189
CREATE SERVER dummy_server FOREIGN DATA WRAPPER dummy;

contrib/amcheck/t/001_verify_heapam.pl

+67-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
use TestLib;
99

1010
use Fcntl qw(:seek);
11-
use Test::More tests => 80;
11+
use Test::More tests => 272;
1212

1313
my ($node, $result);
1414

@@ -60,6 +60,22 @@
6060
"verify_heapam('test', skip := 'all-frozen')",
6161
"all-frozen corrupted table skipping all-frozen");
6262

63+
#
64+
# Check a sequence with no corruption. The current implementation of sequences
65+
# doesn't require its own test setup, since sequences are really just heap
66+
# tables under-the-hood. To guard against future implementation changes made
67+
# without remembering to update verify_heapam, we create and exercise a
68+
# sequence, checking along the way that it passes corruption checks.
69+
#
70+
fresh_test_sequence('test_seq');
71+
check_all_options_uncorrupted('test_seq', 'plain');
72+
advance_test_sequence('test_seq');
73+
check_all_options_uncorrupted('test_seq', 'plain');
74+
set_test_sequence('test_seq');
75+
check_all_options_uncorrupted('test_seq', 'plain');
76+
reset_test_sequence('test_seq');
77+
check_all_options_uncorrupted('test_seq', 'plain');
78+
6379
# Returns the filesystem path for the named relation.
6480
sub relation_filepath
6581
{
@@ -110,6 +126,56 @@ sub fresh_test_table
110126
));
111127
}
112128

129+
# Create a test sequence of the given name.
130+
sub fresh_test_sequence
131+
{
132+
my ($seqname) = @_;
133+
134+
return $node->safe_psql(
135+
'postgres', qq(
136+
DROP SEQUENCE IF EXISTS $seqname CASCADE;
137+
CREATE SEQUENCE $seqname
138+
INCREMENT BY 13
139+
MINVALUE 17
140+
START WITH 23;
141+
SELECT nextval('$seqname');
142+
SELECT setval('$seqname', currval('$seqname') + nextval('$seqname'));
143+
));
144+
}
145+
146+
# Call SQL functions to increment the sequence
147+
sub advance_test_sequence
148+
{
149+
my ($seqname) = @_;
150+
151+
return $node->safe_psql(
152+
'postgres', qq(
153+
SELECT nextval('$seqname');
154+
));
155+
}
156+
157+
# Call SQL functions to set the sequence
158+
sub set_test_sequence
159+
{
160+
my ($seqname) = @_;
161+
162+
return $node->safe_psql(
163+
'postgres', qq(
164+
SELECT setval('$seqname', 102);
165+
));
166+
}
167+
168+
# Call SQL functions to reset the sequence
169+
sub reset_test_sequence
170+
{
171+
my ($seqname) = @_;
172+
173+
return $node->safe_psql(
174+
'postgres', qq(
175+
ALTER SEQUENCE $seqname RESTART WITH 51
176+
));
177+
}
178+
113179
# Stops the test node, corrupts the first page of the named relation, and
114180
# restarts the node.
115181
sub corrupt_first_page

contrib/amcheck/verify_heapam.c

+8-2
Original file line numberDiff line numberDiff line change
@@ -305,14 +305,20 @@ verify_heapam(PG_FUNCTION_ARGS)
305305
*/
306306
if (ctx.rel->rd_rel->relkind != RELKIND_RELATION &&
307307
ctx.rel->rd_rel->relkind != RELKIND_MATVIEW &&
308-
ctx.rel->rd_rel->relkind != RELKIND_TOASTVALUE)
308+
ctx.rel->rd_rel->relkind != RELKIND_TOASTVALUE &&
309+
ctx.rel->rd_rel->relkind != RELKIND_SEQUENCE)
309310
ereport(ERROR,
310311
(errcode(ERRCODE_WRONG_OBJECT_TYPE),
311312
errmsg("cannot check relation \"%s\"",
312313
RelationGetRelationName(ctx.rel)),
313314
errdetail_relkind_not_supported(ctx.rel->rd_rel->relkind)));
314315

315-
if (ctx.rel->rd_rel->relam != HEAP_TABLE_AM_OID)
316+
/*
317+
* Sequences always use heap AM, but they don't show that in the catalogs.
318+
* Other relkinds might be using a different AM, so check.
319+
*/
320+
if (ctx.rel->rd_rel->relkind != RELKIND_SEQUENCE &&
321+
ctx.rel->rd_rel->relam != HEAP_TABLE_AM_OID)
316322
ereport(ERROR,
317323
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
318324
errmsg("only heap AM is supported")));

doc/src/sgml/amcheck.sgml

+4-4
Original file line numberDiff line numberDiff line change
@@ -220,10 +220,10 @@ SET client_min_messages = DEBUG1;
220220
</term>
221221
<listitem>
222222
<para>
223-
Checks a table for structural corruption, where pages in the relation
224-
contain data that is invalidly formatted, and for logical corruption,
225-
where pages are structurally valid but inconsistent with the rest of the
226-
database cluster.
223+
Checks a table, sequence, or materialized view for structural corruption,
224+
where pages in the relation contain data that is invalidly formatted, and
225+
for logical corruption, where pages are structurally valid but
226+
inconsistent with the rest of the database cluster.
227227
</para>
228228
<para>
229229
The following optional arguments are recognized:

doc/src/sgml/ref/pg_amcheck.sgml

+9-6
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,9 @@ PostgreSQL documentation
4141
</para>
4242

4343
<para>
44-
Only table relations and btree indexes are currently supported. Other
45-
relation types are silently skipped.
44+
Only ordinary and toast table relations, materialized views, sequences, and
45+
btree indexes are currently supported. Other relation types are silently
46+
skipped.
4647
</para>
4748

4849
<para>
@@ -124,7 +125,7 @@ PostgreSQL documentation
124125
</para>
125126
<para>
126127
This is similar to the <option>--relation</option> option, except that
127-
it applies only to indexes, not tables.
128+
it applies only to indexes, not to other relation types.
128129
</para>
129130
</listitem>
130131
</varlistentry>
@@ -140,7 +141,7 @@ PostgreSQL documentation
140141
</para>
141142
<para>
142143
This is similar to the <option>--exclude-relation</option> option,
143-
except that it applies only to indexes, not tables.
144+
except that it applies only to indexes, not other relation types.
144145
</para>
145146
</listitem>
146147
</varlistentry>
@@ -236,7 +237,8 @@ PostgreSQL documentation
236237
</para>
237238
<para>
238239
This is similar to the <option>--relation</option> option, except that
239-
it applies only to tables, not indexes.
240+
it applies only to tables, materialized views, and sequences, not to
241+
indexes.
240242
</para>
241243
</listitem>
242244
</varlistentry>
@@ -252,7 +254,8 @@ PostgreSQL documentation
252254
</para>
253255
<para>
254256
This is similar to the <option>--exclude-relation</option> option,
255-
except that it applies only to tables, not indexes.
257+
except that it applies only to tables, materialized views, and
258+
sequences, not to indexes.
256259
</para>
257260
</listitem>
258261
</varlistentry>

src/bin/pg_amcheck/pg_amcheck.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -1910,14 +1910,14 @@ compile_relation_list_one_db(PGconn *conn, SimplePtrList *relations,
19101910
if (opts.allrel)
19111911
appendPQExpBuffer(&sql,
19121912
" AND c.relam = %u "
1913-
"AND c.relkind IN ('r', 'm', 't') "
1913+
"AND c.relkind IN ('r', 'S', 'm', 't') "
19141914
"AND c.relnamespace != %u",
19151915
HEAP_TABLE_AM_OID, PG_TOAST_NAMESPACE);
19161916
else
19171917
appendPQExpBuffer(&sql,
19181918
" AND c.relam IN (%u, %u)"
1919-
"AND c.relkind IN ('r', 'm', 't', 'i') "
1920-
"AND ((c.relam = %u AND c.relkind IN ('r', 'm', 't')) OR "
1919+
"AND c.relkind IN ('r', 'S', 'm', 't', 'i') "
1920+
"AND ((c.relam = %u AND c.relkind IN ('r', 'S', 'm', 't')) OR "
19211921
"(c.relam = %u AND c.relkind = 'i'))",
19221922
HEAP_TABLE_AM_OID, BTREE_AM_OID,
19231923
HEAP_TABLE_AM_OID, BTREE_AM_OID);

0 commit comments

Comments
 (0)