Debian Codegraph @

src/test/perl/PostgreSQL/Test/AdjustUpgrade.pm

raw
1
2 # Copyright (c) 2023-2025, PostgreSQL Global Development Group
3
4 =pod
5
6 =head1 NAME
7
8 PostgreSQL::Test::AdjustUpgrade - helper module for cross-version upgrade tests
9
10 =head1 SYNOPSIS
11
12 use PostgreSQL::Test::AdjustUpgrade;
13
14 # Build commands to adjust contents of old-version database before dumping
15 $statements = adjust_database_contents($old_version, %dbnames);
16
17 # Adjust contents of old pg_dumpall output file to match newer version
18 $dump = adjust_old_dumpfile($old_version, $dump);
19
20 # Adjust contents of new pg_dumpall output file to match older version
21 $dump = adjust_new_dumpfile($old_version, $dump);
22
23 =head1 DESCRIPTION
24
25 C<PostgreSQL::Test::AdjustUpgrade> encapsulates various hacks needed to
26 compare the results of cross-version upgrade tests.
27
28 =cut
29
30 package PostgreSQL::Test::AdjustUpgrade;
31
32 use strict;
33 use warnings FATAL => 'all';
34
35 use Exporter 'import';
36 use PostgreSQL::Version;
37
38 our @EXPORT = qw(
39 adjust_database_contents
40 adjust_old_dumpfile
41 adjust_new_dumpfile
42 );
43
44 =pod
45
46 =head1 ROUTINES
47
48 =over
49
50 =item $statements = adjust_database_contents($old_version, %dbnames)
51
52 Generate SQL commands to perform any changes to an old-version installation
53 that are needed before we can pg_upgrade it into the current PostgreSQL
54 version.
55
56 Typically this involves dropping or adjusting no-longer-supported objects.
57
58 Arguments:
59
60 =over
61
62 =item C<old_version>: Branch we are upgrading from, represented as a
63 PostgreSQL::Version object.
64
65 =item C<dbnames>: Hash of database names present in the old installation.
66
67 =back
68
69 Returns a reference to a hash, wherein the keys are database names and the
70 values are arrayrefs to lists of statements to be run in those databases.
71
72 =cut
73
74 sub adjust_database_contents
75 {
76 my ($old_version, %dbnames) = @_;
77 my $result = {};
78
79 die "wrong type for \$old_version\n"
80 unless $old_version->isa("PostgreSQL::Version");
81
82 # The version tests can be sensitive if fixups have been applied in a
83 # recent version and pg_upgrade is run with a beta version, or such.
84 # Therefore, use a modified version object that only contains the major.
85 $old_version = PostgreSQL::Version->new($old_version->major);
86
87 # remove dbs of modules known to cause pg_upgrade to fail
88 # anything not builtin and incompatible should clean up its own db
89 foreach my $bad_module ('adminpack', 'test_ddl_deparse', 'tsearch2')
90 {
91 if ($dbnames{"contrib_regression_$bad_module"})
92 {
93 _add_st($result, 'postgres',
94 "drop database contrib_regression_$bad_module");
95 delete($dbnames{"contrib_regression_$bad_module"});
96 }
97 if ($dbnames{"regression_$bad_module"})
98 {
99 _add_st($result, 'postgres',
100 "drop database regression_$bad_module");
101 delete($dbnames{"regression_$bad_module"});
102 }
103 }
104
105 # avoid no-path-to-downgrade-extension-version issues
106 if ($dbnames{contrib_regression_test_extensions})
107 {
108 _add_st(
109 $result,
110 'contrib_regression_test_extensions',
111 'drop extension if exists test_ext_cine',
112 'drop extension if exists test_ext7');
113 }
114
115 # we removed these test-support functions in v18
116 if ($old_version < 18)
117 {
118 _add_st($result, 'regression', 'drop function ttdummy()');
119 _add_st($result, 'regression', 'drop function set_ttdummy(integer)');
120 _add_st($result, 'regression', 'drop function autoinc()');
121 _add_st($result, 'regression', 'drop function check_foreign_key()');
122 _add_st($result, 'regression', 'drop function check_primary_key()');
123 }
124
125 # we removed this test-support function in v17
126 if ($old_version >= 15 && $old_version < 17)
127 {
128 _add_st($result, 'regression',
129 'drop function get_columns_length(oid[])');
130 }
131
132 # stuff not supported from release 16
133 if ($old_version >= 12 && $old_version < 16)
134 {
135 # Can't upgrade aclitem in user tables from pre 16 to 16+.
136 _add_st($result, 'regression',
137 'alter table public.tab_core_types drop column aclitem');
138 # Can't handle child tables with locally-generated columns.
139 _add_st(
140 $result, 'regression',
141 'drop table public.gtest_normal_child',
142 'drop table public.gtest_normal_child2');
143 }
144
145 # stuff not supported from release 14
146 if ($old_version < 14)
147 {
148 # postfix operators (some don't exist in very old versions)
149 _add_st(
150 $result,
151 'regression',
152 'drop operator #@# (bigint,NONE)',
153 'drop operator #%# (bigint,NONE)',
154 'drop operator if exists !=- (bigint,NONE)',
155 'drop operator if exists #@%# (bigint,NONE)');
156
157 # get rid of dblink's dependencies on regress.so
158 my $regrdb =
159 $old_version le '9.4'
160 ? 'contrib_regression'
161 : 'contrib_regression_dblink';
162
163 if ($dbnames{$regrdb})
164 {
165 _add_st(
166 $result, $regrdb,
167 'drop function if exists public.putenv(text)',
168 'drop function if exists public.wait_pid(integer)');
169 }
170
171 # delete seg row that pre-14 was printed incorrectly but would now
172 # be printed correctly
173 if ($dbnames{contrib_regression_seg})
174 {
175 _add_st($result, 'contrib_regression_seg',
176 "delete from test_seg where s = '4.6 .. ~7.0'");
177 }
178 }
179
180 # user table OIDs are gone from release 12 on
181 if ($old_version < 12)
182 {
183 my $nooid_stmt = q{
184 DO $stmt$
185 DECLARE
186 rec text;
187 BEGIN
188 FOR rec in
189 select oid::regclass::text
190 from pg_class
191 where relname !~ '^pg_'
192 and relhasoids
193 and relkind in ('r','m')
194 order by 1
195 LOOP
196 execute 'ALTER TABLE ' || rec || ' SET WITHOUT OIDS';
197 RAISE NOTICE 'removing oids from table %', rec;
198 END LOOP;
199 END; $stmt$;
200 };
201
202 foreach my $oiddb ('regression', 'contrib_regression_btree_gist')
203 {
204 next unless $dbnames{$oiddb};
205 _add_st($result, $oiddb, $nooid_stmt);
206 }
207
208 # this table had OIDs too, but we'll just drop it
209 if ($old_version >= 10 && $dbnames{'contrib_regression_postgres_fdw'})
210 {
211 _add_st(
212 $result,
213 'contrib_regression_postgres_fdw',
214 'drop foreign table ft_pg_type');
215 }
216 }
217
218 # abstime+friends are gone from release 12 on; but these tables
219 # might or might not be present depending on regression test vintage
220 if ($old_version < 12)
221 {
222 _add_st($result, 'regression',
223 'drop table if exists abstime_tbl, reltime_tbl, tinterval_tbl');
224 }
225
226 # some regression functions gone from release 11 on
227 if ($old_version < 11)
228 {
229 _add_st(
230 $result, 'regression',
231 'drop function if exists public.boxarea(box)',
232 'drop function if exists public.funny_dup17()');
233 }
234
235 # version-0 C functions are no longer supported
236 if ($old_version < 10)
237 {
238 _add_st($result, 'regression',
239 'drop function oldstyle_length(integer, text)');
240 }
241
242 if ($old_version lt '9.5')
243 {
244 # cope with changes of underlying functions
245 _add_st(
246 $result,
247 'regression',
248 'drop operator @#@ (NONE, bigint)',
249 'CREATE OPERATOR @#@ ('
250 . 'PROCEDURE = factorial, RIGHTARG = bigint )',
251 'drop aggregate public.array_cat_accum(anyarray)',
252 'CREATE AGGREGATE array_larger_accum (anyarray) ' . ' ( '
253 . ' sfunc = array_larger, '
254 . ' stype = anyarray, '
255 . ' initcond = $${}$$ ' . ' ) ');
256
257 # "=>" is no longer valid as an operator name
258 _add_st($result, 'regression',
259 'drop operator if exists public.=> (bigint, NONE)');
260 }
261
262 return $result;
263 }
264
265 # Internal subroutine to add statement(s) to the list for the given db.
266 sub _add_st
267 {
268 my ($result, $db, @st) = @_;
269
270 $result->{$db} ||= [];
271 push(@{ $result->{$db} }, @st);
272 }
273
274 =pod
275
276 =item adjust_old_dumpfile($old_version, $dump)
277
278 Edit a dump output file, taken from the adjusted old-version installation
279 by current-version C<pg_dumpall -s>, so that it will match the results of
280 C<pg_dumpall -s> on the pg_upgrade'd installation.
281
282 Typically this involves coping with cosmetic differences in the output
283 of backend subroutines used by pg_dump.
284
285 Arguments:
286
287 =over
288
289 =item C<old_version>: Branch we are upgrading from, represented as a
290 PostgreSQL::Version object.
291
292 =item C<dump>: Contents of dump file
293
294 =back
295
296 Returns the modified dump text.
297
298 =cut
299
300 sub adjust_old_dumpfile
301 {
302 my ($old_version, $dump) = @_;
303
304 die "wrong type for \$old_version\n"
305 unless $old_version->isa("PostgreSQL::Version");
306 # See adjust_database_contents about this
307 $old_version = PostgreSQL::Version->new($old_version->major);
308
309 # use Unix newlines
310 $dump =~ s/\r\n/\n/g;
311
312 # Version comments will certainly not match.
313 $dump =~ s/^-- Dumped from database version.*\n//mg;
314
315 # Same with version argument to pg_restore_relation_stats() or
316 # pg_restore_attribute_stats().
317 $dump =~ s {\n(\s+'version',) '\d+'::integer,$}
318 {$1 '000000'::integer,}mg;
319
320 if ($old_version < 16)
321 {
322 # Fix up some view queries that no longer require table-qualification.
323 $dump = _mash_view_qualifiers($dump);
324 }
325
326 if ($old_version >= 14 && $old_version < 17)
327 {
328 # Fix up some privilege-set discrepancies.
329 $dump =~
330 s {^REVOKE SELECT,INSERT,REFERENCES,DELETE,TRIGGER,TRUNCATE,UPDATE ON TABLE}
331 {REVOKE ALL ON TABLE}mg;
332 $dump =~
333 s {^(GRANT SELECT,INSERT,REFERENCES,TRIGGER,TRUNCATE),UPDATE ON TABLE}
334 {$1,MAINTAIN,UPDATE ON TABLE}mg;
335 }
336
337 if ($old_version < 14)
338 {
339 # Remove mentions of extended hash functions.
340 $dump =~ s {^(\s+OPERATOR\s1\s=\(integer,integer\))\s,\n
341 \s+FUNCTION\s2\s\(integer,\sinteger\)\spublic\.part_hashint4_noop\(integer,bigint\);}
342 {$1;}mxg;
343 $dump =~ s {^(\s+OPERATOR\s1\s=\(text,text\))\s,\n
344 \s+FUNCTION\s2\s\(text,\stext\)\spublic\.part_hashtext_length\(text,bigint\);}
345 {$1;}mxg;
346 }
347
348 # Change trigger definitions to say ... EXECUTE FUNCTION ...
349 if ($old_version < 12)
350 {
351 # would like to use lookbehind here but perl complains
352 # so do it this way
353 $dump =~ s/
354 (^CREATE\sTRIGGER\s.*?)
355 \sEXECUTE\sPROCEDURE
356 /$1 EXECUTE FUNCTION/mgx;
357 }
358
359 # During pg_upgrade, we reindex hash indexes if the source is pre-v10.
360 # This may change their tables' relallvisible values, so don't compare
361 # those.
362 if ($old_version < 10)
363 {
364 $dump =~ s/
365 (^SELECT\s\*\sFROM\spg_catalog\.pg_restore_relation_stats\(
366 [^;]*'relation',\s'public\.hash_[a-z0-9]*_heap'::regclass,
367 [^;]*'relallvisible',)\s'\d+'::integer
368 /$1 ''::integer/mgx;
369 }
370
371 if ($old_version lt '9.6')
372 {
373 # adjust some places where we don't print so many parens anymore
374
375 my $prefix =
376 "'New York'\tnew & york | big & apple | nyc\t'new' & 'york'\t";
377 my $orig = "( 'new' & 'york' | 'big' & 'appl' ) | 'nyc'";
378 my $repl = "'new' & 'york' | 'big' & 'appl' | 'nyc'";
379 $dump =~ s/(?<=^\Q$prefix\E)\Q$orig\E/$repl/mg;
380
381 $prefix =
382 "'Sanct Peter'\tPeterburg | peter | 'Sanct Peterburg'\t'sanct' & 'peter'\t";
383 $orig = "( 'peterburg' | 'peter' ) | 'sanct' & 'peterburg'";
384 $repl = "'peterburg' | 'peter' | 'sanct' & 'peterburg'";
385 $dump =~ s/(?<=^\Q$prefix\E)\Q$orig\E/$repl/mg;
386 }
387
388 if ($old_version lt '9.5')
389 {
390 # adjust some places where we don't print so many parens anymore
391
392 my $prefix = "CONSTRAINT (?:sequence|copy)_con CHECK [(][(]";
393 my $orig = "((x > 3) AND (y <> 'check failed'::text))";
394 my $repl = "(x > 3) AND (y <> 'check failed'::text)";
395 $dump =~ s/($prefix)\Q$orig\E/$1$repl/mg;
396
397 $prefix = "CONSTRAINT insert_con CHECK [(][(]";
398 $orig = "((x >= 3) AND (y <> 'check failed'::text))";
399 $repl = "(x >= 3) AND (y <> 'check failed'::text)";
400 $dump =~ s/($prefix)\Q$orig\E/$1$repl/mg;
401
402 $orig = "DEFAULT ((-1) * currval('public.insert_seq'::regclass))";
403 $repl =
404 "DEFAULT ('-1'::integer * currval('public.insert_seq'::regclass))";
405 $dump =~ s/\Q$orig\E/$repl/mg;
406
407 my $expr =
408 "(rsl.sl_color = rsh.slcolor) AND (rsl.sl_len_cm >= rsh.slminlen_cm)";
409 $dump =~ s/WHERE \(\(\Q$expr\E\)/WHERE ($expr/g;
410
411 $expr =
412 "(rule_and_refint_t3.id3a = new.id3a) AND (rule_and_refint_t3.id3b = new.id3b)";
413 $dump =~ s/WHERE \(\(\Q$expr\E\)/WHERE ($expr/g;
414
415 $expr =
416 "(rule_and_refint_t3_1.id3a = new.id3a) AND (rule_and_refint_t3_1.id3b = new.id3b)";
417 $dump =~ s/WHERE \(\(\Q$expr\E\)/WHERE ($expr/g;
418 }
419
420 if ($old_version lt '9.3')
421 {
422 # CREATE VIEW/RULE statements were not pretty-printed before 9.3.
423 # To cope, reduce all whitespace sequences within them to one space.
424 # This must be done on both old and new dumps.
425 $dump = _mash_view_whitespace($dump);
426
427 # _mash_view_whitespace doesn't handle multi-command rules;
428 # rather than trying to fix that, just hack the exceptions manually.
429
430 my $prefix =
431 "CREATE RULE rtest_sys_del AS ON DELETE TO public.rtest_system DO (DELETE FROM public.rtest_interface WHERE (rtest_interface.sysname = old.sysname);";
432 my $line2 = " DELETE FROM public.rtest_admin";
433 my $line3 = " WHERE (rtest_admin.sysname = old.sysname);";
434 $dump =~
435 s/(?<=\Q$prefix\E)\Q$line2$line3\E \);/\n$line2\n $line3\n);/mg;
436
437 $prefix =
438 "CREATE RULE rtest_sys_upd AS ON UPDATE TO public.rtest_system DO (UPDATE public.rtest_interface SET sysname = new.sysname WHERE (rtest_interface.sysname = old.sysname);";
439 $line2 = " UPDATE public.rtest_admin SET sysname = new.sysname";
440 $line3 = " WHERE (rtest_admin.sysname = old.sysname);";
441 $dump =~
442 s/(?<=\Q$prefix\E)\Q$line2$line3\E \);/\n$line2\n $line3\n);/mg;
443
444 # and there's one place where pre-9.3 uses a different table alias
445 $dump =~ s {^(CREATE\sRULE\srule_and_refint_t3_ins\sAS\s
446 ON\sINSERT\sTO\spublic\.rule_and_refint_t3\s
447 WHERE\s\(EXISTS\s\(SELECT\s1\sFROM\spublic\.rule_and_refint_t3)\s
448 (WHERE\s\(\(rule_and_refint_t3)
449 (\.id3a\s=\snew\.id3a\)\sAND\s\(rule_and_refint_t3)
450 (\.id3b\s=\snew\.id3b\)\sAND\s\(rule_and_refint_t3)}
451 {$1 rule_and_refint_t3_1 $2_1$3_1$4_1}mx;
452
453 # Also fix old use of NATURAL JOIN syntax
454 $dump =~ s {NATURAL JOIN public\.credit_card r}
455 {JOIN public.credit_card r USING (cid)}mg;
456 $dump =~ s {NATURAL JOIN public\.credit_usage r}
457 {JOIN public.credit_usage r USING (cid)}mg;
458 }
459
460 # Suppress blank lines, as some places in pg_dump emit more or fewer.
461 $dump =~ s/\n\n+/\n/g;
462
463 return $dump;
464 }
465
466
467 # Data for _mash_view_qualifiers
468 my @_unused_view_qualifiers = (
469 # Present at least since 9.2
470 { obj => 'VIEW public.trigger_test_view', qual => 'trigger_test' },
471 { obj => 'VIEW public.domview', qual => 'domtab' },
472 { obj => 'VIEW public.my_property_normal', qual => 'customer' },
473 { obj => 'VIEW public.my_property_secure', qual => 'customer' },
474 { obj => 'VIEW public.pfield_v1', qual => 'pf' },
475 { obj => 'VIEW public.rtest_v1', qual => 'rtest_t1' },
476 { obj => 'VIEW public.rtest_vview1', qual => 'x' },
477 { obj => 'VIEW public.rtest_vview2', qual => 'rtest_view1' },
478 { obj => 'VIEW public.rtest_vview3', qual => 'x' },
479 { obj => 'VIEW public.rtest_vview5', qual => 'rtest_view1' },
480 { obj => 'VIEW public.shoelace_obsolete', qual => 'shoelace' },
481 { obj => 'VIEW public.shoelace_candelete', qual => 'shoelace_obsolete' },
482 { obj => 'VIEW public.toyemp', qual => 'emp' },
483 { obj => 'VIEW public.xmlview4', qual => 'emp' },
484 # Since 9.3 (some of these were removed in 9.6)
485 { obj => 'VIEW public.tv', qual => 't' },
486 { obj => 'MATERIALIZED VIEW mvschema.tvm', qual => 'tv' },
487 { obj => 'VIEW public.tvv', qual => 'tv' },
488 { obj => 'MATERIALIZED VIEW public.tvvm', qual => 'tvv' },
489 { obj => 'VIEW public.tvvmv', qual => 'tvvm' },
490 { obj => 'MATERIALIZED VIEW public.bb', qual => 'tvvmv' },
491 { obj => 'VIEW public.nums', qual => 'nums' },
492 { obj => 'VIEW public.sums_1_100', qual => 't' },
493 { obj => 'MATERIALIZED VIEW public.tm', qual => 't' },
494 { obj => 'MATERIALIZED VIEW public.tmm', qual => 'tm' },
495 { obj => 'MATERIALIZED VIEW public.tvmm', qual => 'tvm' },
496 # Since 9.4
497 {
498 obj => 'MATERIALIZED VIEW public.citext_matview',
499 qual => 'citext_table'
500 },
501 {
502 obj => 'OR REPLACE VIEW public.key_dependent_view',
503 qual => 'view_base_table'
504 },
505 {
506 obj => 'OR REPLACE VIEW public.key_dependent_view_no_cols',
507 qual => 'view_base_table'
508 },
509 # Since 9.5
510 {
511 obj => 'VIEW public.dummy_seclabel_view1',
512 qual => 'dummy_seclabel_tbl2'
513 },
514 { obj => 'VIEW public.vv', qual => 'test_tablesample' },
515 { obj => 'VIEW public.test_tablesample_v1', qual => 'test_tablesample' },
516 { obj => 'VIEW public.test_tablesample_v2', qual => 'test_tablesample' },
517 # Since 9.6
518 {
519 obj => 'MATERIALIZED VIEW public.test_pg_dump_mv1',
520 qual => 'test_pg_dump_t1'
521 },
522 { obj => 'VIEW public.test_pg_dump_v1', qual => 'test_pg_dump_t1' },
523 { obj => 'VIEW public.mvtest_tv', qual => 'mvtest_t' },
524 {
525 obj => 'MATERIALIZED VIEW mvtest_mvschema.mvtest_tvm',
526 qual => 'mvtest_tv'
527 },
528 { obj => 'VIEW public.mvtest_tvv', qual => 'mvtest_tv' },
529 { obj => 'MATERIALIZED VIEW public.mvtest_tvvm', qual => 'mvtest_tvv' },
530 { obj => 'VIEW public.mvtest_tvvmv', qual => 'mvtest_tvvm' },
531 { obj => 'MATERIALIZED VIEW public.mvtest_bb', qual => 'mvtest_tvvmv' },
532 { obj => 'MATERIALIZED VIEW public.mvtest_tm', qual => 'mvtest_t' },
533 { obj => 'MATERIALIZED VIEW public.mvtest_tmm', qual => 'mvtest_tm' },
534 { obj => 'MATERIALIZED VIEW public.mvtest_tvmm', qual => 'mvtest_tvm' },
535 # Since 10 (some removed in 12)
536 { obj => 'VIEW public.itestv10', qual => 'itest10' },
537 { obj => 'VIEW public.itestv11', qual => 'itest11' },
538 { obj => 'VIEW public.xmltableview2', qual => '"xmltable"' },
539 # Since 12
540 {
541 obj => 'MATERIALIZED VIEW public.tableam_tblmv_heap2',
542 qual => 'tableam_tbl_heap2'
543 },
544 # Since 13
545 { obj => 'VIEW public.limit_thousand_v_1', qual => 'onek' },
546 { obj => 'VIEW public.limit_thousand_v_2', qual => 'onek' },
547 { obj => 'VIEW public.limit_thousand_v_3', qual => 'onek' },
548 { obj => 'VIEW public.limit_thousand_v_4', qual => 'onek' },
549 { obj => 'VIEW public.limit_thousand_v_5', qual => 'onek' },
550 # Since 14
551 { obj => 'MATERIALIZED VIEW public.compressmv', qual => 'cmdata1' });
552
553 # Internal subroutine to remove no-longer-used table qualifiers from
554 # CREATE [MATERIALIZED] VIEW commands. See list of targeted views above.
555 sub _mash_view_qualifiers
556 {
557 my ($dump) = @_;
558
559 for my $uvq (@_unused_view_qualifiers)
560 {
561 my $leader = "CREATE $uvq->{obj} ";
562 my $qualifier = $uvq->{qual};
563 # Note: we loop because there are presently some cases where the same
564 # view name appears in multiple databases. Fortunately, the same
565 # qualifier removal applies or is harmless for each instance ... but
566 # we might want to rename some things to avoid assuming that.
567 my @splitchunks = split $leader, $dump;
568 $dump = shift(@splitchunks);
569 foreach my $chunk (@splitchunks)
570 {
571 my @thischunks = split /;/, $chunk, 2;
572 my $stmt = shift(@thischunks);
573
574 # now $stmt is just the body of the CREATE [MATERIALIZED] VIEW
575 $stmt =~ s/$qualifier\.//g;
576
577 $dump .= $leader . $stmt . ';' . $thischunks[0];
578 }
579 }
580
581 # Further hack a few cases where not all occurrences of the qualifier
582 # should be removed.
583 $dump =~ s {^(CREATE VIEW public\.rtest_vview1 .*?)(a\)\)\);)}
584 {$1x.$2}ms;
585 $dump =~ s {^(CREATE VIEW public\.rtest_vview3 .*?)(a\)\)\);)}
586 {$1x.$2}ms;
587 $dump =~
588 s {^(CREATE VIEW public\.shoelace_obsolete .*?)(sl_color\)\)\)\);)}
589 {$1shoelace.$2}ms;
590
591 return $dump;
592 }
593
594
595 # Internal subroutine to mangle whitespace within view/rule commands.
596 # Any consecutive sequence of whitespace is reduced to one space.
597 sub _mash_view_whitespace
598 {
599 my ($dump) = @_;
600
601 foreach my $leader ('CREATE VIEW', 'CREATE RULE')
602 {
603 my @splitchunks = split $leader, $dump;
604
605 $dump = shift(@splitchunks);
606 foreach my $chunk (@splitchunks)
607 {
608 my @thischunks = split /;/, $chunk, 2;
609 my $stmt = shift(@thischunks);
610
611 # now $stmt is just the body of the CREATE VIEW/RULE
612 $stmt =~ s/\s+/ /sg;
613 # we also need to smash these forms for sub-selects and rules
614 $stmt =~ s/\( SELECT/(SELECT/g;
615 $stmt =~ s/\( INSERT/(INSERT/g;
616 $stmt =~ s/\( UPDATE/(UPDATE/g;
617 $stmt =~ s/\( DELETE/(DELETE/g;
618
619 $dump .= $leader . $stmt . ';' . $thischunks[0];
620 }
621 }
622 return $dump;
623 }
624
625 =pod
626
627 =item adjust_new_dumpfile($old_version, $dump)
628
629 Edit a dump output file, taken from the pg_upgrade'd installation
630 by current-version C<pg_dumpall -s>, so that it will match the old
631 dump output file as adjusted by C<adjust_old_dumpfile>.
632
633 Typically this involves deleting data not present in the old installation.
634
635 Arguments:
636
637 =over
638
639 =item C<old_version>: Branch we are upgrading from, represented as a
640 PostgreSQL::Version object.
641
642 =item C<dump>: Contents of dump file
643
644 =back
645
646 Returns the modified dump text.
647
648 =cut
649
650 sub adjust_new_dumpfile
651 {
652 my ($old_version, $dump) = @_;
653
654 die "wrong type for \$old_version\n"
655 unless $old_version->isa("PostgreSQL::Version");
656 # See adjust_database_contents about this
657 $old_version = PostgreSQL::Version->new($old_version->major);
658
659 # use Unix newlines
660 $dump =~ s/\r\n/\n/g;
661
662 # Version comments will certainly not match.
663 $dump =~ s/^-- Dumped from database version.*\n//mg;
664
665 # Same with version argument to pg_restore_relation_stats() or
666 # pg_restore_attribute_stats().
667 $dump =~ s {\n(\s+'version',) '\d+'::integer,$}
668 {$1 '000000'::integer,}mg;
669
670 if ($old_version < 18)
671 {
672 $dump =~ s {,\n(\s+'relallfrozen',) '-?\d+'::integer$}{}mg;
673 }
674
675 # pre-v16 dumps do not know about XMLSERIALIZE(NO INDENT).
676 if ($old_version < 16)
677 {
678 $dump =~ s/XMLSERIALIZE\((.*)? NO INDENT\)/XMLSERIALIZE\($1\)/mg;
679 }
680
681 if ($old_version < 14)
682 {
683 # Suppress noise-word uses of IN in CREATE/ALTER PROCEDURE.
684 $dump =~ s/^(CREATE PROCEDURE .*?)\(IN /$1(/mg;
685 $dump =~ s/^(ALTER PROCEDURE .*?)\(IN /$1(/mg;
686 $dump =~ s/^(CREATE PROCEDURE .*?), IN /$1, /mg;
687 $dump =~ s/^(ALTER PROCEDURE .*?), IN /$1, /mg;
688 $dump =~ s/^(CREATE PROCEDURE .*?), IN /$1, /mg;
689 $dump =~ s/^(ALTER PROCEDURE .*?), IN /$1, /mg;
690
691 # Remove SUBSCRIPT clauses in CREATE TYPE.
692 $dump =~ s/^\s+SUBSCRIPT = raw_array_subscript_handler,\n//mg;
693
694 # Remove multirange_type_name clauses in CREATE TYPE AS RANGE.
695 $dump =~ s {,\n\s+multirange_type_name = .*?(,?)$} {$1}mg;
696
697 # Remove mentions of extended hash functions.
698 $dump =~
699 s {^ALTER\sOPERATOR\sFAMILY\spublic\.part_test_int4_ops\sUSING\shash\sADD\n
700 \s+FUNCTION\s2\s\(integer,\sinteger\)\spublic\.part_hashint4_noop\(integer,bigint\);} {}mxg;
701 $dump =~
702 s {^ALTER\sOPERATOR\sFAMILY\spublic\.part_test_text_ops\sUSING\shash\sADD\n
703 \s+FUNCTION\s2\s\(text,\stext\)\spublic\.part_hashtext_length\(text,bigint\);} {}mxg;
704 }
705
706 # pre-v12 dumps will not say anything about default_table_access_method.
707 if ($old_version < 12)
708 {
709 $dump =~ s/^SET default_table_access_method = heap;\n//mg;
710 }
711
712 # During pg_upgrade, we reindex hash indexes if the source is pre-v10.
713 # This may change their tables' relallvisible values, so don't compare
714 # those.
715 if ($old_version < 10)
716 {
717 $dump =~ s/
718 (^SELECT\s\*\sFROM\spg_catalog\.pg_restore_relation_stats\(
719 [^;]*'relation',\s'public\.hash_[a-z0-9]*_heap'::regclass,
720 [^;]*'relallvisible',)\s'\d+'::integer
721 /$1 ''::integer/mgx;
722 }
723
724 # dumps from pre-9.6 dblink may include redundant ACL settings
725 if ($old_version lt '9.6')
726 {
727 my $comment =
728 "-- Name: FUNCTION dblink_connect_u\(.*?\); Type: ACL; Schema: public; Owner: .*";
729 my $sql =
730 "REVOKE ALL ON FUNCTION public\.dblink_connect_u\(.*?\) FROM PUBLIC;";
731 $dump =~ s/^--\n$comment\n--\n+$sql\n+//mg;
732 }
733
734 if ($old_version lt '9.3')
735 {
736 # CREATE VIEW/RULE statements were not pretty-printed before 9.3.
737 # To cope, reduce all whitespace sequences within them to one space.
738 # This must be done on both old and new dumps.
739 $dump = _mash_view_whitespace($dump);
740 }
741
742 # Suppress blank lines, as some places in pg_dump emit more or fewer.
743 $dump =~ s/\n\n+/\n/g;
744
745 return $dump;
746 }
747
748 =pod
749
750 =back
751
752 =cut
753
754 1;