Debian Codegraph @

src/test/modules/test_pg_dump/t/001_base.pl

raw
1
2 # Copyright (c) 2021-2025, PostgreSQL Global Development Group
3
4 use strict;
5 use warnings FATAL => 'all';
6
7 use PostgreSQL::Test::Cluster;
8 use PostgreSQL::Test::Utils;
9 use Test::More;
10
11 my $tempdir = PostgreSQL::Test::Utils::tempdir;
12
13 ###############################################################
14 # This structure is based off of the src/bin/pg_dump/t test
15 # suite.
16 ###############################################################
17 # Definition of the pg_dump runs to make.
18 #
19 # Each of these runs are named and those names are used below
20 # to define how each test should (or shouldn't) treat a result
21 # from a given run.
22 #
23 # compile_option indicates if the commands run depend on a compilation
24 # option, if any. This can be used to control if tests should be
25 # skipped when a build dependency is not satisfied.
26 #
27 # test_key indicates that a given run should simply use the same
28 # set of like/unlike tests as another run, and which run that is.
29 #
30 # dump_cmd is the pg_dump command to run, which is an array of
31 # the full command and arguments to run. Note that this is run
32 # using $node->command_ok(), so the port does not need to be
33 # specified and is pulled from $PGPORT, which is set by the
34 # PostgreSQL::Test::Cluster system.
35 #
36 # restore_cmd is the pg_restore command to run, if any. Note
37 # that this should generally be used when the pg_dump goes to
38 # a non-text file and that the restore can then be used to
39 # generate a text file to run through the tests from the
40 # non-text file generated by pg_dump.
41 #
42 # TODO: Have pg_restore actually restore to an independent
43 # database and then pg_dump *that* database (or something along
44 # those lines) to validate that part of the process.
45
46 my %pgdump_runs = (
47 binary_upgrade => {
48 dump_cmd => [
49 'pg_dump', '--no-sync',
50 '--file' => "$tempdir/binary_upgrade.sql",
51 '--schema-only', '--sequence-data', '--binary-upgrade',
52 '--dbname' => 'postgres',
53 ],
54 },
55 clean => {
56 dump_cmd => [
57 'pg_dump', '--no-sync',
58 '--file' => "$tempdir/clean.sql",
59 '--clean',
60 '--dbname' => 'postgres',
61 ],
62 },
63 clean_if_exists => {
64 dump_cmd => [
65 'pg_dump', '--no-sync',
66 '--file' => "$tempdir/clean_if_exists.sql",
67 '--clean',
68 '--if-exists',
69 '--encoding' => 'UTF8', # no-op, just tests that it is accepted
70 'postgres',
71 ],
72 },
73 createdb => {
74 dump_cmd => [
75 'pg_dump', '--no-sync',
76 '--file' => "$tempdir/createdb.sql",
77 '--create',
78 '--no-reconnect', # no-op, just for testing
79 'postgres',
80 ],
81 },
82 data_only => {
83 dump_cmd => [
84 'pg_dump', '--no-sync',
85 '--file' => "$tempdir/data_only.sql",
86 '--data-only',
87 '--verbose', # no-op, just make sure it works
88 'postgres',
89 ],
90 },
91 defaults => {
92 dump_cmd => [
93 'pg_dump',
94 '--file' => "$tempdir/defaults.sql",
95 'postgres',
96 ],
97 },
98 defaults_custom_format => {
99 test_key => 'defaults',
100 compile_option => 'gzip',
101 dump_cmd => [
102 'pg_dump', '--no-sync',
103 '--format' => 'custom',
104 '--compress' => 6,
105 '--file' => "$tempdir/defaults_custom_format.dump",
106 'postgres',
107 ],
108 restore_cmd => [
109 'pg_restore',
110 '--file' => "$tempdir/defaults_custom_format.sql",
111 "$tempdir/defaults_custom_format.dump",
112 ],
113 },
114 defaults_dir_format => {
115 test_key => 'defaults',
116 dump_cmd => [
117 'pg_dump', '--no-sync',
118 '--format' => 'directory',
119 '--file' => "$tempdir/defaults_dir_format",
120 'postgres',
121 ],
122 restore_cmd => [
123 'pg_restore',
124 '--file' => "$tempdir/defaults_dir_format.sql",
125 "$tempdir/defaults_dir_format",
126 ],
127 },
128 defaults_parallel => {
129 test_key => 'defaults',
130 dump_cmd => [
131 'pg_dump', '--no-sync',
132 '--format' => 'directory',
133 '--jobs' => 2,
134 '--file' => "$tempdir/defaults_parallel",
135 'postgres',
136 ],
137 restore_cmd => [
138 'pg_restore',
139 '--file' => "$tempdir/defaults_parallel.sql",
140 "$tempdir/defaults_parallel",
141 ],
142 },
143 defaults_tar_format => {
144 test_key => 'defaults',
145 dump_cmd => [
146 'pg_dump', '--no-sync',
147 '--format' => 'tar',
148 '--file' => "$tempdir/defaults_tar_format.tar",
149 'postgres',
150 ],
151 restore_cmd => [
152 'pg_restore',
153 '--file' => "$tempdir/defaults_tar_format.sql",
154 "$tempdir/defaults_tar_format.tar",
155 ],
156 },
157 exclude_table => {
158 dump_cmd => [
159 'pg_dump',
160 '--exclude-table' => 'regress_table_dumpable',
161 '--file' => "$tempdir/exclude_table.sql",
162 'postgres',
163 ],
164 },
165 extension_schema => {
166 dump_cmd => [
167 'pg_dump',
168 '--schema' => 'public',
169 '--file' => "$tempdir/extension_schema.sql",
170 'postgres',
171 ],
172 },
173 pg_dumpall_globals => {
174 dump_cmd => [
175 'pg_dumpall', '--no-sync',
176 '--file' => "$tempdir/pg_dumpall_globals.sql",
177 '--globals-only',
178 ],
179 },
180 no_privs => {
181 dump_cmd => [
182 'pg_dump', '--no-sync',
183 '--file' => "$tempdir/no_privs.sql",
184 '--no-privileges',
185 'postgres',
186 ],
187 },
188 no_owner => {
189 dump_cmd => [
190 'pg_dump', '--no-sync',
191 '--file' => "$tempdir/no_owner.sql",
192 '--no-owner',
193 'postgres',
194 ],
195 },
196
197 # regress_dump_login_role shouldn't need SELECT rights on internal
198 # (undumped) extension tables
199 privileged_internals => {
200 dump_cmd => [
201 'pg_dump', '--no-sync',
202 '--file' => "$tempdir/privileged_internals.sql",
203 # these two tables are irrelevant to the test case
204 '--exclude-table' => 'regress_pg_dump_schema.external_tab',
205 '--exclude-table' => 'regress_pg_dump_schema.extdependtab',
206 '--username' => 'regress_dump_login_role',
207 'postgres',
208 ],
209 },
210
211 schema_only => {
212 dump_cmd => [
213 'pg_dump', '--no-sync',
214 '--file' => "$tempdir/schema_only.sql",
215 '--schema-only', 'postgres',
216 ],
217 },
218 section_pre_data => {
219 dump_cmd => [
220 'pg_dump', '--no-sync',
221 '--file' => "$tempdir/section_pre_data.sql",
222 '--section' => 'pre-data',
223 'postgres',
224 ],
225 },
226 section_data => {
227 dump_cmd => [
228 'pg_dump', '--no-sync',
229 '--file' => "$tempdir/section_data.sql",
230 '--section' => 'data',
231 'postgres',
232 ],
233 },
234 section_post_data => {
235 dump_cmd => [
236 'pg_dump', '--no-sync',
237 '--file' => "$tempdir/section_post_data.sql",
238 '--section' => 'post-data',
239 'postgres',
240 ],
241 },
242 with_extension => {
243 dump_cmd => [
244 'pg_dump', '--no-sync',
245 '--file' => "$tempdir/with_extension.sql",
246 '--extension' => 'test_pg_dump',
247 'postgres',
248 ],
249 },
250 exclude_extension => {
251 dump_cmd => [
252 'pg_dump', '--no-sync',
253 '--file' => "$tempdir/exclude_extension.sql",
254 '--exclude-extension' => 'test_pg_dump',
255 'postgres',
256 ],
257 },
258 exclude_extension_filter => {
259 dump_cmd => [
260 'pg_dump',
261 '--no-sync',
262 '--file' => "$tempdir/exclude_extension_filter.sql",
263 '--filter' => "$tempdir/exclude_extension_filter.txt",
264 'postgres',
265 ],
266 },
267
268 # plpgsql in the list blocks the dump of extension test_pg_dump
269 without_extension => {
270 dump_cmd => [
271 'pg_dump', '--no-sync',
272 '--file' => "$tempdir/without_extension.sql",
273 '--extension' => 'plpgsql',
274 'postgres',
275 ],
276 },
277
278 # plpgsql in the list of extensions blocks the dump of extension
279 # test_pg_dump. "public" is the schema used by the extension
280 # test_pg_dump, but none of its objects should be dumped.
281 without_extension_explicit_schema => {
282 dump_cmd => [
283 'pg_dump',
284 '--no-sync',
285 '--file' => "$tempdir/without_extension_explicit_schema.sql",
286 '--extension' => 'plpgsql',
287 '--schema' => 'public',
288 'postgres',
289 ],
290 },
291
292 # plpgsql in the list of extensions blocks the dump of extension
293 # test_pg_dump, but not the dump of objects not dependent on the
294 # extension located on a schema maintained by the extension.
295 without_extension_internal_schema => {
296 dump_cmd => [
297 'pg_dump',
298 '--no-sync',
299 '--file' => "$tempdir/without_extension_internal_schema.sql",
300 '--extension' => 'plpgsql',
301 '--schema' => 'regress_pg_dump_schema',
302 'postgres',
303 ],
304 },);
305
306 ###############################################################
307 # Definition of the tests to run.
308 #
309 # Each test is defined using the log message that will be used.
310 #
311 # A regexp should be defined for each test which provides the
312 # basis for the test. That regexp will be run against the output
313 # file of each of the runs which the test is to be run against
314 # and the success of the result will depend on if the regexp
315 # result matches the expected 'like' or 'unlike' case.
316 # The runs listed as 'like' will be checked if they match the
317 # regexp and, if so, the test passes. All runs which are not
318 # listed as 'like' will be checked to ensure they don't match
319 # the regexp; if they do, the test will fail.
320 #
321 # The below hashes provide convenience sets of runs. Individual
322 # runs can be excluded from a general hash by placing that run
323 # into the 'unlike' section.
324 #
325 # There can then be a 'create_sql' and 'create_order' for a
326 # given test. The 'create_sql' commands are collected up in
327 # 'create_order' and then run against the database prior to any
328 # of the pg_dump runs happening. This is what "seeds" the
329 # system with objects to be dumped out.
330 #
331 # Building of this hash takes a bit of time as all of the regexps
332 # included in it are compiled. This greatly improves performance
333 # as the regexps are used for each run the test applies to.
334
335 # Tests which are considered 'full' dumps by pg_dump, but there
336 # are flags used to exclude specific items (ACLs, LOs, etc).
337 my %full_runs = (
338 binary_upgrade => 1,
339 clean => 1,
340 clean_if_exists => 1,
341 createdb => 1,
342 defaults => 1,
343 exclude_table => 1,
344 no_privs => 1,
345 no_owner => 1,
346 privileged_internals => 1,
347 with_extension => 1,
348 exclude_extension => 1,
349 exclude_extension_filter => 1,
350 without_extension => 1);
351
352 my %tests = (
353 'ALTER EXTENSION test_pg_dump' => {
354 create_order => 9,
355 create_sql =>
356 'ALTER EXTENSION test_pg_dump ADD TABLE regress_pg_dump_table_added;',
357 regexp => qr/^
358 \QCREATE TABLE public.regress_pg_dump_table_added (\E
359 \n\s+\Qcol1 integer NOT NULL,\E
360 \n\s+\Qcol2 integer\E
361 \n\);\n/xm,
362 like => { binary_upgrade => 1, },
363 },
364
365 'CREATE EXTENSION test_pg_dump' => {
366 create_order => 2,
367 create_sql => 'CREATE EXTENSION test_pg_dump;',
368 regexp => qr/^
369 \QCREATE EXTENSION IF NOT EXISTS test_pg_dump WITH SCHEMA public;\E
370 \n/xm,
371 like => {
372 %full_runs,
373 schema_only => 1,
374 section_pre_data => 1,
375 },
376 unlike => {
377 binary_upgrade => 1,
378 exclude_extension => 1,
379 exclude_extension_filter => 1,
380 without_extension => 1
381 },
382 },
383
384 'CREATE ROLE regress_dump_test_role' => {
385 create_order => 1,
386 create_sql => 'CREATE ROLE regress_dump_test_role;',
387 regexp => qr/^CREATE ROLE regress_dump_test_role;\n/m,
388 like => { pg_dumpall_globals => 1, },
389 },
390
391 'CREATE ROLE regress_dump_login_role' => {
392 create_order => 1,
393 create_sql => 'CREATE ROLE regress_dump_login_role LOGIN;',
394 regexp => qr/^
395 \QCREATE ROLE regress_dump_login_role;\E
396 \n\QALTER ROLE regress_dump_login_role WITH \E.*\Q LOGIN \E.*;
397 \n/xm,
398 like => { pg_dumpall_globals => 1, },
399 },
400
401 'GRANT ALTER SYSTEM ON PARAMETER full_page_writes TO regress_dump_test_role'
402 => {
403 create_order => 2,
404 create_sql =>
405 'GRANT ALTER SYSTEM ON PARAMETER full_page_writes TO regress_dump_test_role;',
406 regexp =>
407
408 qr/^GRANT ALTER SYSTEM ON PARAMETER full_page_writes TO regress_dump_test_role;/m,
409 like => { pg_dumpall_globals => 1, },
410 },
411
412 'GRANT ALL ON PARAMETER Custom.Knob TO regress_dump_test_role WITH GRANT OPTION'
413 => {
414 create_order => 2,
415 create_sql =>
416 'GRANT SET, ALTER SYSTEM ON PARAMETER Custom.Knob TO regress_dump_test_role WITH GRANT OPTION;',
417 regexp =>
418 # "set" plus "alter system" is "all" privileges on parameters
419 qr/^GRANT ALL ON PARAMETER "custom.knob" TO regress_dump_test_role WITH GRANT OPTION;/m,
420 like => { pg_dumpall_globals => 1, },
421 },
422
423 'GRANT ALL ON PARAMETER DateStyle TO regress_dump_test_role' => {
424 create_order => 2,
425 create_sql =>
426 'GRANT ALL ON PARAMETER "DateStyle" TO regress_dump_test_role WITH GRANT OPTION; REVOKE GRANT OPTION FOR ALL ON PARAMETER DateStyle FROM regress_dump_test_role;',
427 regexp =>
428 # The revoke simplifies the ultimate grant so as to not include "with grant option"
429 qr/^GRANT ALL ON PARAMETER datestyle TO regress_dump_test_role;/m,
430 like => { pg_dumpall_globals => 1, },
431 },
432
433 'CREATE SCHEMA public' => {
434 regexp => qr/^CREATE SCHEMA public;/m,
435 like => {
436 extension_schema => 1,
437 without_extension_explicit_schema => 1,
438 },
439 },
440
441 'CREATE SEQUENCE regress_pg_dump_table_col1_seq' => {
442 regexp => qr/^
443 \QCREATE SEQUENCE public.regress_pg_dump_table_col1_seq\E
444 \n\s+\QAS integer\E
445 \n\s+\QSTART WITH 1\E
446 \n\s+\QINCREMENT BY 1\E
447 \n\s+\QNO MINVALUE\E
448 \n\s+\QNO MAXVALUE\E
449 \n\s+\QCACHE 1;\E
450 \n/xm,
451 like => { binary_upgrade => 1, },
452 },
453
454 'CREATE TABLE regress_pg_dump_table_added' => {
455 create_order => 7,
456 create_sql =>
457 'CREATE TABLE regress_pg_dump_table_added (col1 int not null, col2 int);',
458 regexp => qr/^
459 \QCREATE TABLE public.regress_pg_dump_table_added (\E
460 \n\s+\Qcol1 integer NOT NULL,\E
461 \n\s+\Qcol2 integer\E
462 \n\);\n/xm,
463 like => { binary_upgrade => 1, },
464 },
465
466 'CREATE SEQUENCE regress_pg_dump_seq' => {
467 regexp => qr/^
468 \QCREATE SEQUENCE public.regress_pg_dump_seq\E
469 \n\s+\QSTART WITH 1\E
470 \n\s+\QINCREMENT BY 1\E
471 \n\s+\QNO MINVALUE\E
472 \n\s+\QNO MAXVALUE\E
473 \n\s+\QCACHE 1;\E
474 \n/xm,
475 like => { binary_upgrade => 1, },
476 },
477
478 'SETVAL SEQUENCE regress_seq_dumpable' => {
479 create_order => 6,
480 create_sql => qq{SELECT nextval('regress_seq_dumpable');},
481 regexp => qr/^
482 \QSELECT pg_catalog.setval('public.regress_seq_dumpable', 1, true);\E
483 \n/xm,
484 like => {
485 %full_runs,
486 data_only => 1,
487 section_data => 1,
488 extension_schema => 1,
489 },
490 unlike => {
491 exclude_extension => 1,
492 exclude_extension_filter => 1,
493 without_extension => 1,
494 },
495 },
496
497 'CREATE TABLE regress_pg_dump_table' => {
498 regexp => qr/^
499 \QCREATE TABLE public.regress_pg_dump_table (\E
500 \n\s+\Qcol1 integer NOT NULL,\E
501 \n\s+\Qcol2 integer,\E
502 \n\s+\QCONSTRAINT regress_pg_dump_table_col2_check CHECK ((col2 > 0))\E
503 \n\);\n/xm,
504 like => { binary_upgrade => 1, },
505 },
506
507 'COPY public.regress_table_dumpable (col1)' => {
508 regexp => qr/^
509 \QCOPY public.regress_table_dumpable (col1) FROM stdin;\E
510 \n/xm,
511 like => {
512 %full_runs,
513 data_only => 1,
514 section_data => 1,
515 extension_schema => 1,
516 },
517 unlike => {
518 binary_upgrade => 1,
519 exclude_table => 1,
520 exclude_extension => 1,
521 exclude_extension_filter => 1,
522 without_extension => 1,
523 },
524 },
525
526 'REVOKE ALL ON FUNCTION wgo_then_no_access' => {
527 create_order => 3,
528 create_sql => q{
529 DO $$BEGIN EXECUTE format(
530 'REVOKE ALL ON FUNCTION wgo_then_no_access()
531 FROM pg_signal_backend, public, %I',
532 (SELECT usename
533 FROM pg_user JOIN pg_proc ON proowner = usesysid
534 WHERE proname = 'wgo_then_no_access')); END$$;},
535 regexp => qr/^
536 \QREVOKE ALL ON FUNCTION public.wgo_then_no_access() FROM PUBLIC;\E
537 \n\QREVOKE ALL ON FUNCTION public.wgo_then_no_access() FROM \E.*;
538 \n\QREVOKE ALL ON FUNCTION public.wgo_then_no_access() FROM pg_signal_backend;\E
539 /xm,
540 like => {
541 %full_runs,
542 schema_only => 1,
543 section_pre_data => 1,
544 },
545 unlike => {
546 no_privs => 1,
547 exclude_extension => 1,
548 exclude_extension_filter => 1,
549 without_extension => 1,
550 },
551 },
552
553 'REVOKE GRANT OPTION FOR UPDATE ON SEQUENCE wgo_then_regular' => {
554 create_order => 3,
555 create_sql => 'REVOKE GRANT OPTION FOR UPDATE ON SEQUENCE
556 wgo_then_regular FROM pg_signal_backend;',
557 regexp => qr/^
558 \QREVOKE ALL ON SEQUENCE public.wgo_then_regular FROM pg_signal_backend;\E
559 \n\QGRANT SELECT,UPDATE ON SEQUENCE public.wgo_then_regular TO pg_signal_backend;\E
560 \n\QGRANT USAGE ON SEQUENCE public.wgo_then_regular TO pg_signal_backend WITH GRANT OPTION;\E
561 /xm,
562 like => {
563 %full_runs,
564 schema_only => 1,
565 section_pre_data => 1,
566 },
567 unlike => {
568 no_privs => 1,
569 exclude_extension => 1,
570 exclude_extension_filter => 1,
571 without_extension => 1,
572 },
573 },
574
575 'CREATE ACCESS METHOD regress_test_am' => {
576 regexp => qr/^
577 \QCREATE ACCESS METHOD regress_test_am TYPE INDEX HANDLER bthandler;\E
578 \n/xm,
579 like => { binary_upgrade => 1, },
580 },
581
582 'COMMENT ON EXTENSION test_pg_dump' => {
583 regexp => qr/^
584 \QCOMMENT ON EXTENSION test_pg_dump \E
585 \QIS 'Test pg_dump with an extension';\E
586 \n/xm,
587 like => {
588 %full_runs,
589 schema_only => 1,
590 section_pre_data => 1,
591 },
592 unlike => {
593 exclude_extension => 1,
594 exclude_extension_filter => 1,
595 without_extension => 1,
596 },
597 },
598
599 'GRANT SELECT regress_pg_dump_table_added pre-ALTER EXTENSION' => {
600 create_order => 8,
601 create_sql =>
602 'GRANT SELECT ON regress_pg_dump_table_added TO regress_dump_test_role;',
603 regexp => qr/^
604 \QGRANT SELECT ON TABLE public.regress_pg_dump_table_added TO regress_dump_test_role;\E
605 \n/xm,
606 like => { binary_upgrade => 1, },
607 },
608
609 'REVOKE SELECT regress_pg_dump_table_added post-ALTER EXTENSION' => {
610 create_order => 10,
611 create_sql =>
612 'REVOKE SELECT ON regress_pg_dump_table_added FROM regress_dump_test_role;',
613 regexp => qr/^
614 \QREVOKE SELECT ON TABLE public.regress_pg_dump_table_added FROM regress_dump_test_role;\E
615 \n/xm,
616 like => {
617 %full_runs,
618 schema_only => 1,
619 section_pre_data => 1,
620 },
621 unlike => {
622 no_privs => 1,
623 exclude_extension => 1,
624 exclude_extension_filter => 1,
625 without_extension => 1,
626 },
627 },
628
629 'GRANT SELECT ON TABLE regress_pg_dump_table' => {
630 regexp => qr/^
631 \QSELECT pg_catalog.binary_upgrade_set_record_init_privs(true);\E\n
632 \QGRANT SELECT ON TABLE public.regress_pg_dump_table TO regress_dump_test_role;\E\n
633 \QSELECT pg_catalog.binary_upgrade_set_record_init_privs(false);\E
634 \n/xms,
635 like => { binary_upgrade => 1, },
636 },
637
638 'GRANT SELECT(col1) ON regress_pg_dump_table' => {
639 regexp => qr/^
640 \QSELECT pg_catalog.binary_upgrade_set_record_init_privs(true);\E\n
641 \QGRANT SELECT(col1) ON TABLE public.regress_pg_dump_table TO PUBLIC;\E\n
642 \QSELECT pg_catalog.binary_upgrade_set_record_init_privs(false);\E
643 \n/xms,
644 like => { binary_upgrade => 1, },
645 },
646
647 'GRANT SELECT(col2) ON regress_pg_dump_table TO regress_dump_test_role'
648 => {
649 create_order => 4,
650 create_sql => 'GRANT SELECT(col2) ON regress_pg_dump_table
651 TO regress_dump_test_role;',
652 regexp => qr/^
653 \QGRANT SELECT(col2) ON TABLE public.regress_pg_dump_table TO regress_dump_test_role;\E
654 \n/xm,
655 like => {
656 %full_runs,
657 schema_only => 1,
658 section_pre_data => 1,
659 },
660 unlike => {
661 no_privs => 1,
662 exclude_extension => 1,
663 exclude_extension_filter => 1,
664 without_extension => 1
665 },
666 },
667
668 'GRANT USAGE ON regress_pg_dump_table_col1_seq TO regress_dump_test_role'
669 => {
670 create_order => 5,
671 create_sql => 'GRANT USAGE ON SEQUENCE regress_pg_dump_table_col1_seq
672 TO regress_dump_test_role;',
673 regexp => qr/^
674 \QGRANT USAGE ON SEQUENCE public.regress_pg_dump_table_col1_seq TO regress_dump_test_role;\E
675 \n/xm,
676 like => {
677 %full_runs,
678 schema_only => 1,
679 section_pre_data => 1,
680 },
681 unlike => {
682 no_privs => 1,
683 exclude_extension => 1,
684 exclude_extension_filter => 1,
685 without_extension => 1,
686 },
687 },
688
689 'GRANT USAGE ON regress_pg_dump_seq TO regress_dump_test_role' => {
690 regexp => qr/^
691 \QGRANT USAGE ON SEQUENCE public.regress_pg_dump_seq TO regress_dump_test_role;\E
692 \n/xm,
693 like => { binary_upgrade => 1, },
694 },
695
696 'REVOKE SELECT(col1) ON regress_pg_dump_table' => {
697 create_order => 3,
698 create_sql => 'REVOKE SELECT(col1) ON regress_pg_dump_table
699 FROM PUBLIC;',
700 regexp => qr/^
701 \QREVOKE SELECT(col1) ON TABLE public.regress_pg_dump_table FROM PUBLIC;\E
702 \n/xm,
703 like => {
704 %full_runs,
705 schema_only => 1,
706 section_pre_data => 1,
707 },
708 unlike => {
709 no_privs => 1,
710 exclude_extension => 1,
711 exclude_extension_filter => 1,
712 without_extension => 1,
713 },
714 },
715
716 # Objects included in extension part of a schema created by this extension */
717 'CREATE TABLE regress_pg_dump_schema.test_table' => {
718 regexp => qr/^
719 \QCREATE TABLE regress_pg_dump_schema.test_table (\E
720 \n\s+\Qcol1 integer,\E
721 \n\s+\Qcol2 integer,\E
722 \n\s+\QCONSTRAINT test_table_col2_check CHECK ((col2 > 0))\E
723 \n\);\n/xm,
724 like => { binary_upgrade => 1, },
725 },
726
727 'GRANT SELECT ON regress_pg_dump_schema.test_table' => {
728 regexp => qr/^
729 \QSELECT pg_catalog.binary_upgrade_set_record_init_privs(true);\E\n
730 \QGRANT SELECT ON TABLE regress_pg_dump_schema.test_table TO regress_dump_test_role;\E\n
731 \QSELECT pg_catalog.binary_upgrade_set_record_init_privs(false);\E
732 \n/xms,
733 like => { binary_upgrade => 1, },
734 },
735
736 'CREATE SEQUENCE regress_pg_dump_schema.test_seq' => {
737 regexp => qr/^
738 \QCREATE SEQUENCE regress_pg_dump_schema.test_seq\E
739 \n\s+\QSTART WITH 1\E
740 \n\s+\QINCREMENT BY 1\E
741 \n\s+\QNO MINVALUE\E
742 \n\s+\QNO MAXVALUE\E
743 \n\s+\QCACHE 1;\E
744 \n/xm,
745 like => { binary_upgrade => 1, },
746 },
747
748 'GRANT USAGE ON regress_pg_dump_schema.test_seq' => {
749 regexp => qr/^
750 \QSELECT pg_catalog.binary_upgrade_set_record_init_privs(true);\E\n
751 \QGRANT USAGE ON SEQUENCE regress_pg_dump_schema.test_seq TO regress_dump_test_role;\E\n
752 \QSELECT pg_catalog.binary_upgrade_set_record_init_privs(false);\E
753 \n/xms,
754 like => { binary_upgrade => 1, },
755 },
756
757 'CREATE TYPE regress_pg_dump_schema.test_type' => {
758 regexp => qr/^
759 \QCREATE TYPE regress_pg_dump_schema.test_type AS (\E
760 \n\s+\Qcol1 integer\E
761 \n\);\n/xm,
762 like => { binary_upgrade => 1, },
763 },
764
765 'GRANT USAGE ON regress_pg_dump_schema.test_type' => {
766 regexp => qr/^
767 \QSELECT pg_catalog.binary_upgrade_set_record_init_privs(true);\E\n
768 \QGRANT ALL ON TYPE regress_pg_dump_schema.test_type TO regress_dump_test_role;\E\n
769 \QSELECT pg_catalog.binary_upgrade_set_record_init_privs(false);\E
770 \n/xms,
771 like => { binary_upgrade => 1, },
772 },
773
774 'CREATE FUNCTION regress_pg_dump_schema.test_func' => {
775 regexp => qr/^
776 \QCREATE FUNCTION regress_pg_dump_schema.test_func() RETURNS integer\E
777 \n\s+\QLANGUAGE sql\E
778 \n/xm,
779 like => { binary_upgrade => 1, },
780 },
781
782 'GRANT ALL ON regress_pg_dump_schema.test_func' => {
783 regexp => qr/^
784 \QSELECT pg_catalog.binary_upgrade_set_record_init_privs(true);\E\n
785 \QGRANT ALL ON FUNCTION regress_pg_dump_schema.test_func() TO regress_dump_test_role;\E\n
786 \QSELECT pg_catalog.binary_upgrade_set_record_init_privs(false);\E
787 \n/xms,
788 like => { binary_upgrade => 1, },
789 },
790
791 'CREATE AGGREGATE regress_pg_dump_schema.test_agg' => {
792 regexp => qr/^
793 \QCREATE AGGREGATE regress_pg_dump_schema.test_agg(smallint) (\E
794 \n\s+\QSFUNC = int2_sum,\E
795 \n\s+\QSTYPE = bigint\E
796 \n\);\n/xm,
797 like => { binary_upgrade => 1, },
798 },
799
800 'GRANT ALL ON regress_pg_dump_schema.test_agg' => {
801 regexp => qr/^
802 \QSELECT pg_catalog.binary_upgrade_set_record_init_privs(true);\E\n
803 \QGRANT ALL ON FUNCTION regress_pg_dump_schema.test_agg(smallint) TO regress_dump_test_role;\E\n
804 \QSELECT pg_catalog.binary_upgrade_set_record_init_privs(false);\E
805 \n/xms,
806 like => { binary_upgrade => 1, },
807 },
808
809 'ALTER INDEX pkey DEPENDS ON extension' => {
810 create_order => 11,
811 create_sql =>
812 'CREATE TABLE regress_pg_dump_schema.extdependtab (col1 integer primary key, col2 int);
813 CREATE INDEX ON regress_pg_dump_schema.extdependtab (col2);
814 ALTER INDEX regress_pg_dump_schema.extdependtab_col2_idx DEPENDS ON EXTENSION test_pg_dump;
815 ALTER INDEX regress_pg_dump_schema.extdependtab_pkey DEPENDS ON EXTENSION test_pg_dump;',
816 regexp => qr/^
817 \QALTER INDEX regress_pg_dump_schema.extdependtab_pkey DEPENDS ON EXTENSION test_pg_dump;\E\n
818 /xms,
819 like => {%pgdump_runs},
820 unlike => {
821 data_only => 1,
822 extension_schema => 1,
823 pg_dumpall_globals => 1,
824 privileged_internals => 1,
825 section_data => 1,
826 section_pre_data => 1,
827 # Excludes this schema as extension is not listed.
828 without_extension_explicit_schema => 1,
829 },
830 },
831
832 'ALTER INDEX idx DEPENDS ON extension' => {
833 regexp => qr/^
834 \QALTER INDEX regress_pg_dump_schema.extdependtab_col2_idx DEPENDS ON EXTENSION test_pg_dump;\E\n
835 /xms,
836 like => {%pgdump_runs},
837 unlike => {
838 data_only => 1,
839 extension_schema => 1,
840 pg_dumpall_globals => 1,
841 privileged_internals => 1,
842 section_data => 1,
843 section_pre_data => 1,
844 # Excludes this schema as extension is not listed.
845 without_extension_explicit_schema => 1,
846 },
847 },
848
849 # Objects not included in extension, part of schema created by extension
850 'CREATE TABLE regress_pg_dump_schema.external_tab' => {
851 create_order => 4,
852 create_sql => 'CREATE TABLE regress_pg_dump_schema.external_tab
853 (col1 int);',
854 regexp => qr/^
855 \QCREATE TABLE regress_pg_dump_schema.external_tab (\E
856 \n\s+\Qcol1 integer\E
857 \n\);\n/xm,
858 like => {
859 %full_runs,
860 schema_only => 1,
861 section_pre_data => 1,
862 # Excludes the extension and keeps the schema's data.
863 without_extension_internal_schema => 1,
864 },
865 unlike => { privileged_internals => 1 },
866 },);
867
868 #########################################
869 # Create a PG instance to test actually dumping from
870
871 my $node = PostgreSQL::Test::Cluster->new('main');
872 $node->init(auth_extra => [ '--create-role' => 'regress_dump_login_role' ]);
873 $node->start;
874
875 my $port = $node->port;
876
877 my $supports_gzip = check_pg_config("#define HAVE_LIBZ 1");
878
879 #########################################
880 # Set up schemas, tables, etc, to be dumped.
881
882 # Build up the create statements
883 my $create_sql = '';
884
885 foreach my $test (
886 sort {
887 if ($tests{$a}->{create_order} and $tests{$b}->{create_order})
888 {
889 $tests{$a}->{create_order} <=> $tests{$b}->{create_order};
890 }
891 elsif ($tests{$a}->{create_order})
892 {
893 -1;
894 }
895 elsif ($tests{$b}->{create_order})
896 {
897 1;
898 }
899 else
900 {
901 0;
902 }
903 } keys %tests)
904 {
905 if ($tests{$test}->{create_sql})
906 {
907 $create_sql .= $tests{$test}->{create_sql};
908 }
909 }
910
911 # Send the combined set of commands to psql
912 $node->safe_psql('postgres', $create_sql);
913
914 #########################################
915 # Create filter file for exclude_extension_filter test
916
917 my $filterfile;
918
919 open $filterfile, '>', "$tempdir/exclude_extension_filter.txt"
920 or die "unable to open filter file for writing";
921 print $filterfile "exclude extension test_pg_dump\n";
922 close $filterfile;
923
924 #########################################
925 # Run all runs
926
927 foreach my $run (sort keys %pgdump_runs)
928 {
929
930 my $test_key = $run;
931
932 # Skip command-level tests for gzip if there is no support for it.
933 if ( defined($pgdump_runs{$run}->{compile_option})
934 && $pgdump_runs{$run}->{compile_option} eq 'gzip'
935 && !$supports_gzip)
936 {
937 note "$run: skipped due to no gzip support";
938 next;
939 }
940
941 $node->command_ok(\@{ $pgdump_runs{$run}->{dump_cmd} },
942 "$run: pg_dump runs");
943
944 if ($pgdump_runs{$run}->{restore_cmd})
945 {
946 $node->command_ok(\@{ $pgdump_runs{$run}->{restore_cmd} },
947 "$run: pg_restore runs");
948 }
949
950 if ($pgdump_runs{$run}->{test_key})
951 {
952 $test_key = $pgdump_runs{$run}->{test_key};
953 }
954
955 my $output_file = slurp_file("$tempdir/${run}.sql");
956
957 #########################################
958 # Run all tests where this run is included
959 # as either a 'like' or 'unlike' test.
960
961 foreach my $test (sort keys %tests)
962 {
963 # Check for proper test definitions
964 #
965 # There should be a "like" list, even if it is empty. (This
966 # makes the test more self-documenting.)
967 if (!defined($tests{$test}->{like}))
968 {
969 die "missing \"like\" in test \"$test\"";
970 }
971 # Check for useless entries in "unlike" list. Runs that are
972 # not listed in "like" don't need to be excluded in "unlike".
973 if ($tests{$test}->{unlike}->{$test_key}
974 && !defined($tests{$test}->{like}->{$test_key}))
975 {
976 die "useless \"unlike\" entry \"$test_key\" in test \"$test\"";
977 }
978
979 # Run the test listed as a like, unless it is specifically noted
980 # as an unlike (generally due to an explicit exclusion or similar).
981 if ($tests{$test}->{like}->{$test_key}
982 && !defined($tests{$test}->{unlike}->{$test_key}))
983 {
984 if (!ok($output_file =~ $tests{$test}->{regexp},
985 "$run: should dump $test"))
986 {
987 diag("Review $run results in $tempdir");
988 }
989 }
990 else
991 {
992 if (!ok($output_file !~ $tests{$test}->{regexp},
993 "$run: should not dump $test"))
994 {
995 diag("Review $run results in $tempdir");
996 }
997 }
998 }
999 }
1000
1001 #########################################
1002 # Stop the database instance, which will be removed at the end of the tests.
1003
1004 $node->stop('fast');
1005
1006 done_testing();