1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
|
# Copyright (c) 2021-2022, PostgreSQL Global Development Group
use strict;
use warnings;
use PostgreSQL::Test::Cluster;
use PostgreSQL::Test::Utils;
use Test::More;
# Test set-up
my ($node, $port);
$node = PostgreSQL::Test::Cluster->new('test');
$node->init;
$node->start;
$port = $node->port;
# Load the amcheck extension, upon which pg_amcheck depends
$node->safe_psql('postgres', q(CREATE EXTENSION amcheck));
#########################################
# Test non-existent databases
# Failing to connect to the initial database is an error.
$node->command_checks_all(
[ 'pg_amcheck', 'qqq' ],
1, [qr/^$/],
[qr/FATAL: database "qqq" does not exist/],
'checking a non-existent database');
# Failing to resolve a database pattern is an error by default.
$node->command_checks_all(
[ 'pg_amcheck', '-d', 'qqq', '-d', 'postgres' ],
1,
[qr/^$/],
[qr/pg_amcheck: error: no connectable databases to check matching "qqq"/],
'checking an unresolvable database pattern');
# But only a warning under --no-strict-names
$node->command_checks_all(
[ 'pg_amcheck', '--no-strict-names', '-d', 'qqq', '-d', 'postgres' ],
0,
[qr/^$/],
[
qr/pg_amcheck: warning: no connectable databases to check matching "qqq"/
],
'checking an unresolvable database pattern under --no-strict-names');
# Check that a substring of an existent database name does not get interpreted
# as a matching pattern.
$node->command_checks_all(
[ 'pg_amcheck', '-d', 'post', '-d', 'postgres' ],
1,
[qr/^$/],
[
qr/pg_amcheck: error: no connectable databases to check matching "post"/
],
'checking an unresolvable database pattern (substring of existent database)'
);
# Check that a superstring of an existent database name does not get interpreted
# as a matching pattern.
$node->command_checks_all(
[ 'pg_amcheck', '-d', 'postgresql', '-d', 'postgres' ],
1,
[qr/^$/],
[
qr/pg_amcheck: error: no connectable databases to check matching "postgresql"/
],
'checking an unresolvable database pattern (superstring of existent database)'
);
#########################################
# Test connecting with a non-existent user
# Failing to connect to the initial database due to bad username is an error.
$node->command_checks_all([ 'pg_amcheck', '-U', 'no_such_user', 'postgres' ],
1, [qr/^$/], [], 'checking with a non-existent user');
#########################################
# Test checking databases without amcheck installed
# Attempting to check a database by name where amcheck is not installed should
# raise a warning. If all databases are skipped, having no relations to check
# raises an error.
$node->command_checks_all(
[ 'pg_amcheck', 'template1' ],
1,
[qr/^$/],
[
qr/pg_amcheck: warning: skipping database "template1": amcheck is not installed/,
qr/pg_amcheck: error: no relations to check/
],
'checking a database by name without amcheck installed, no other databases'
);
# Again, but this time with another database to check, so no error is raised.
$node->command_checks_all(
[ 'pg_amcheck', '-d', 'template1', '-d', 'postgres' ],
0,
[qr/^$/],
[
qr/pg_amcheck: warning: skipping database "template1": amcheck is not installed/
],
'checking a database by name without amcheck installed, with other databases'
);
# Again, but by way of checking all databases
$node->command_checks_all(
[ 'pg_amcheck', '--all' ],
0,
[qr/^$/],
[
qr/pg_amcheck: warning: skipping database "template1": amcheck is not installed/
],
'checking a database by pattern without amcheck installed, with other databases'
);
#########################################
# Test unreasonable patterns
# Check three-part unreasonable pattern that has zero-length names
$node->command_checks_all(
[ 'pg_amcheck', '-d', 'postgres', '-t', '..' ],
1,
[qr/^$/],
[
qr/pg_amcheck: error: no connectable databases to check matching "\.\."/
],
'checking table pattern ".."');
# Again, but with non-trivial schema and relation parts
$node->command_checks_all(
[ 'pg_amcheck', '-d', 'postgres', '-t', '.foo.bar' ],
1,
[qr/^$/],
[
qr/pg_amcheck: error: no connectable databases to check matching "\.foo\.bar"/
],
'checking table pattern ".foo.bar"');
# Check two-part unreasonable pattern that has zero-length names
$node->command_checks_all(
[ 'pg_amcheck', '-d', 'postgres', '-t', '.' ],
1,
[qr/^$/],
[qr/pg_amcheck: error: no heap tables to check matching "\."/],
'checking table pattern "."');
# Check that a multipart database name is rejected
$node->command_checks_all(
[ 'pg_amcheck', '-d', 'localhost.postgres' ],
2,
[qr/^$/],
[
qr/pg_amcheck: error: improper qualified name \(too many dotted names\): localhost\.postgres/
],
'multipart database patterns are rejected');
# Check that a three-part schema name is rejected
$node->command_checks_all(
[ 'pg_amcheck', '-s', 'localhost.postgres.pg_catalog' ],
2,
[qr/^$/],
[
qr/pg_amcheck: error: improper qualified name \(too many dotted names\): localhost\.postgres\.pg_catalog/
],
'three part schema patterns are rejected');
# Check that a four-part table name is rejected
$node->command_checks_all(
[ 'pg_amcheck', '-t', 'localhost.postgres.pg_catalog.pg_class' ],
2,
[qr/^$/],
[
qr/pg_amcheck: error: improper relation name \(too many dotted names\): localhost\.postgres\.pg_catalog\.pg_class/
],
'four part table patterns are rejected');
# Check that too many dotted names still draws an error under --no-strict-names
# That flag means that it is ok for the object to be missing, not that it is ok
# for the object name to be ungrammatical
$node->command_checks_all(
[
'pg_amcheck', '--no-strict-names',
'-t', 'this.is.a.really.long.dotted.string'
],
2,
[qr/^$/],
[
qr/pg_amcheck: error: improper relation name \(too many dotted names\): this\.is\.a\.really\.long\.dotted\.string/
],
'ungrammatical table names still draw errors under --no-strict-names');
$node->command_checks_all(
[
'pg_amcheck', '--no-strict-names', '-s',
'postgres.long.dotted.string'
],
2,
[qr/^$/],
[
qr/pg_amcheck: error: improper qualified name \(too many dotted names\): postgres\.long\.dotted\.string/
],
'ungrammatical schema names still draw errors under --no-strict-names');
$node->command_checks_all(
[
'pg_amcheck', '--no-strict-names', '-d',
'postgres.long.dotted.string'
],
2,
[qr/^$/],
[
qr/pg_amcheck: error: improper qualified name \(too many dotted names\): postgres\.long\.dotted\.string/
],
'ungrammatical database names still draw errors under --no-strict-names');
# Likewise for exclusion patterns
$node->command_checks_all(
[ 'pg_amcheck', '--no-strict-names', '-T', 'a.b.c.d' ],
2,
[qr/^$/],
[
qr/pg_amcheck: error: improper relation name \(too many dotted names\): a\.b\.c\.d/
],
'ungrammatical table exclusions still draw errors under --no-strict-names'
);
$node->command_checks_all(
[ 'pg_amcheck', '--no-strict-names', '-S', 'a.b.c' ],
2,
[qr/^$/],
[
qr/pg_amcheck: error: improper qualified name \(too many dotted names\): a\.b\.c/
],
'ungrammatical schema exclusions still draw errors under --no-strict-names'
);
$node->command_checks_all(
[ 'pg_amcheck', '--no-strict-names', '-D', 'a.b' ],
2,
[qr/^$/],
[
qr/pg_amcheck: error: improper qualified name \(too many dotted names\): a\.b/
],
'ungrammatical database exclusions still draw errors under --no-strict-names'
);
#########################################
# Test checking non-existent databases, schemas, tables, and indexes
# Use --no-strict-names and a single existent table so we only get warnings
# about the failed pattern matches
$node->command_checks_all(
[
'pg_amcheck', '--no-strict-names',
'-t', 'no_such_table',
'-t', 'no*such*table',
'-i', 'no_such_index',
'-i', 'no*such*index',
'-r', 'no_such_relation',
'-r', 'no*such*relation',
'-d', 'no_such_database',
'-d', 'no*such*database',
'-r', 'none.none',
'-r', 'none.none.none',
'-r', 'postgres.none.none',
'-r', 'postgres.pg_catalog.none',
'-r', 'postgres.none.pg_class',
'-t', 'postgres.pg_catalog.pg_class', # This exists
],
0,
[qr/^$/],
[
qr/pg_amcheck: warning: no heap tables to check matching "no_such_table"/,
qr/pg_amcheck: warning: no heap tables to check matching "no\*such\*table"/,
qr/pg_amcheck: warning: no btree indexes to check matching "no_such_index"/,
qr/pg_amcheck: warning: no btree indexes to check matching "no\*such\*index"/,
qr/pg_amcheck: warning: no relations to check matching "no_such_relation"/,
qr/pg_amcheck: warning: no relations to check matching "no\*such\*relation"/,
qr/pg_amcheck: warning: no heap tables to check matching "no\*such\*table"/,
qr/pg_amcheck: warning: no connectable databases to check matching "no_such_database"/,
qr/pg_amcheck: warning: no connectable databases to check matching "no\*such\*database"/,
qr/pg_amcheck: warning: no relations to check matching "none\.none"/,
qr/pg_amcheck: warning: no connectable databases to check matching "none\.none\.none"/,
qr/pg_amcheck: warning: no relations to check matching "postgres\.none\.none"/,
qr/pg_amcheck: warning: no relations to check matching "postgres\.pg_catalog\.none"/,
qr/pg_amcheck: warning: no relations to check matching "postgres\.none\.pg_class"/,
qr/pg_amcheck: warning: no connectable databases to check matching "no_such_database"/,
qr/pg_amcheck: warning: no connectable databases to check matching "no\*such\*database"/,
qr/pg_amcheck: warning: no connectable databases to check matching "none\.none\.none"/,
],
'many unmatched patterns and one matched pattern under --no-strict-names'
);
#########################################
# Test that an invalid / partially dropped database won't be targeted
$node->safe_psql(
'postgres', q(
CREATE DATABASE regression_invalid;
UPDATE pg_database SET datconnlimit = -2 WHERE datname = 'regression_invalid';
));
$node->command_checks_all(
[
'pg_amcheck', '-d', 'regression_invalid'
],
1,
[qr/^$/],
[
qr/pg_amcheck: error: no connectable databases to check matching "regression_invalid"/,
],
'checking handling of invalid database');
$node->command_checks_all(
[
'pg_amcheck', '-d', 'postgres',
'-t', 'regression_invalid.public.foo',
],
1,
[qr/^$/],
[
qr/pg_amcheck: error: no connectable databases to check matching "regression_invalid.public.foo"/,
],
'checking handling of object in invalid database');
#########################################
# Test checking otherwise existent objects but in databases where they do not exist
$node->safe_psql(
'postgres', q(
CREATE TABLE public.foo (f integer);
CREATE INDEX foo_idx ON foo(f);
));
$node->safe_psql('postgres', q(CREATE DATABASE another_db));
$node->command_checks_all(
[
'pg_amcheck', '-d',
'postgres', '--no-strict-names',
'-t', 'template1.public.foo',
'-t', 'another_db.public.foo',
'-t', 'no_such_database.public.foo',
'-i', 'template1.public.foo_idx',
'-i', 'another_db.public.foo_idx',
'-i', 'no_such_database.public.foo_idx',
],
1,
[qr/^$/],
[
qr/pg_amcheck: warning: skipping database "template1": amcheck is not installed/,
qr/pg_amcheck: warning: no heap tables to check matching "template1\.public\.foo"/,
qr/pg_amcheck: warning: no heap tables to check matching "another_db\.public\.foo"/,
qr/pg_amcheck: warning: no connectable databases to check matching "no_such_database\.public\.foo"/,
qr/pg_amcheck: warning: no btree indexes to check matching "template1\.public\.foo_idx"/,
qr/pg_amcheck: warning: no btree indexes to check matching "another_db\.public\.foo_idx"/,
qr/pg_amcheck: warning: no connectable databases to check matching "no_such_database\.public\.foo_idx"/,
qr/pg_amcheck: error: no relations to check/,
],
'checking otherwise existent objets in the wrong databases');
#########################################
# Test schema exclusion patterns
# Check with only schema exclusion patterns
$node->command_checks_all(
[
'pg_amcheck', '--all', '--no-strict-names', '-S',
'public', '-S', 'pg_catalog', '-S',
'pg_toast', '-S', 'information_schema',
],
1,
[qr/^$/],
[
qr/pg_amcheck: warning: skipping database "template1": amcheck is not installed/,
qr/pg_amcheck: error: no relations to check/
],
'schema exclusion patterns exclude all relations');
# Check with schema exclusion patterns overriding relation and schema inclusion patterns
$node->command_checks_all(
[
'pg_amcheck', '--all', '--no-strict-names', '-s',
'public', '-s', 'pg_catalog', '-s',
'pg_toast', '-s', 'information_schema', '-t',
'pg_catalog.pg_class', '-S*'
],
1,
[qr/^$/],
[
qr/pg_amcheck: warning: skipping database "template1": amcheck is not installed/,
qr/pg_amcheck: error: no relations to check/
],
'schema exclusion pattern overrides all inclusion patterns');
done_testing();
|