From 6cb724a745209986f1ab98ce266fb0403070c2ba Mon Sep 17 00:00:00 2001 From: Nikhil Sontakke Date: Thu, 3 Dec 2026 17:54:49 +0530 Subject: [PATCH 1/2] Add a TAP test that replays deparsed DDL on a separate server 001_deparse_regress.pl replays each deparsed command in the session and database that ran the original, so it cannot detect output that depends on the server or the session: names derived to avoid collisions with other objects, and session settings that the deparsed text silently depends on or omits. The new test runs each case's DDL on a source node, collects the deparsed commands from test_ddl_deparse.print_deparsed_ddl, executes them on a separate target node under search_path = '', and compares schema-only dumps of the two. It also checks that commands were actually deparsed, so the test can't pass vacuously. Cases for known deparser issues are marked TODO: derived constraint and index names, DateStyle-dependent literals, default_table_access_method, and the CLUSTER ON / REPLICA IDENTITY that ALTER COLUMN TYPE re-applies being taken for the user's own subcommand. --- src/test/modules/test_ddl_deparse/meson.build | 1 + .../test_ddl_deparse/t/002_deparse_replay.pl | 280 ++++++++++++++++++ 2 files changed, 281 insertions(+) create mode 100644 src/test/modules/test_ddl_deparse/t/002_deparse_replay.pl diff --git a/src/test/modules/test_ddl_deparse/meson.build b/src/test/modules/test_ddl_deparse/meson.build index 27f508d8199..1b42ae58bb9 100644 --- a/src/test/modules/test_ddl_deparse/meson.build +++ b/src/test/modules/test_ddl_deparse/meson.build @@ -57,6 +57,7 @@ tests += { 'tap': { 'tests': [ 't/001_deparse_regress.pl', + 't/002_deparse_replay.pl', ], 'test_kwargs': {'priority': 50} }, diff --git a/src/test/modules/test_ddl_deparse/t/002_deparse_replay.pl b/src/test/modules/test_ddl_deparse/t/002_deparse_replay.pl new file mode 100644 index 00000000000..9802153e6e0 --- /dev/null +++ b/src/test/modules/test_ddl_deparse/t/002_deparse_replay.pl @@ -0,0 +1,280 @@ +# Copyright (c) 2026, PostgreSQL Global Development Group + +# Replay deparsed DDL on a different server and compare the results. +# +# 001_deparse_regress.pl replays each deparsed command in the same session +# and database that ran the original, so anything that depends on the +# server or the session goes unnoticed there: names derived to avoid +# collisions with other objects, and session settings that the deparsed +# text silently depends on or drops. +# +# Here each case runs its DDL on a source node in a single session, with +# test_ddl_deparse.print_deparsed_ddl = text, and collects the deparsed +# commands from the NOTICEs. Those commands are executed on a separate +# target node, under search_path = '' and with the target's own defaults, +# as a logical replication apply worker would. The schema-only dumps of +# the two nodes must then be identical. +# +# Cases that fail because of known deparser issues are marked TODO. +use strict; +use warnings FATAL => 'all'; + +use PostgreSQL::Test::Cluster; +use PostgreSQL::Test::Utils; +use Test::More; + +my $source = PostgreSQL::Test::Cluster->new('source'); +$source->init; +$source->append_conf('postgresql.conf', <start; + +my $target = PostgreSQL::Test::Cluster->new('target'); +$target->init; +$target->start; + +# Each case gets its own database on both nodes. +# +# name - case name, also used as the database name +# both - setup run on both nodes, not deparsed +# source_only - setup run only on the source, not deparsed (objects that +# exist only on the source, such as unpublished tables) +# ddl - run on the source in one session; each supported command +# is deparsed and replayed on the target +# dump - pg_dump object selection options +# min_cmds - the least number of commands that must be deparsed +# todo_replay - reason, if replay is expected to fail today +# todo_dump - reason, if the dumps are expected to differ today +my @cases = ( + { + name => 'basic', + both => 'CREATE SCHEMA s;', + ddl => q{ +CREATE TABLE s.t (a int CONSTRAINT t_a_pos CHECK (a > 0), + b text NOT NULL DEFAULT 'x', + CONSTRAINT t_pk PRIMARY KEY (a)); +ALTER TABLE s.t ADD COLUMN c numeric(10,2) DEFAULT 1.5, + ALTER COLUMN b SET DEFAULT 'y'; +ALTER TABLE s.t RENAME COLUMN c TO c2; +ALTER TABLE s.t ALTER COLUMN c2 TYPE numeric(12,2); +CREATE TABLE s.u (x int CONSTRAINT u_x_fk REFERENCES s.t (a)); +ALTER TABLE s.u RENAME TO u2; +CREATE TABLE s.gone (z int); +DROP TABLE s.gone; +}, + dump => '-n s', + min_cmds => 8, + }, + { + # The source already has objects using the names that CREATE TABLE + # would derive, so it picks t_a_check1 and t_b_key1. The target + # doesn't, and picks t_a_check and t_b_key. The DROP CONSTRAINT + # that follows then fails on the target. + name => 'derived_names', + both => 'CREATE SCHEMA s;', + source_only => q{ +CREATE TABLE s.other (x int CONSTRAINT t_a_check CHECK (x > 0)); +CREATE TABLE s.t_b_key (x int); +}, + ddl => q{ +CREATE TABLE s.t (a int CHECK (a > 0), b int UNIQUE); +ALTER TABLE s.t DROP CONSTRAINT t_a_check1; +}, + dump => '-t s.t', + min_cmds => 2, + todo_replay => 'derived constraint and index names are not emitted', + todo_dump => 'derived constraint and index names are not emitted', + }, + { + # DateStyle on the source changes how date literals are written. + name => 'datestyle', + both => 'CREATE SCHEMA s;', + ddl => q{ +SET DateStyle = 'SQL, DMY'; +CREATE TABLE s.p (d date DEFAULT '2024-01-02') PARTITION BY RANGE (d); +CREATE TABLE s.p1 PARTITION OF s.p + FOR VALUES FROM ('2024-01-02') TO ('2024-02-01'); +}, + dump => '-n s', + min_cmds => 2, + todo_replay => 'deparse output depends on DateStyle', + todo_dump => 'deparse output depends on DateStyle', + }, + { + # A session default that changes what CREATE TABLE creates. + name => 'table_am', + both => q{ +CREATE SCHEMA s; +CREATE ACCESS METHOD heap2 TYPE TABLE HANDLER heap_tableam_handler; +}, + ddl => q{ +SET default_table_access_method = heap2; +CREATE TABLE s.t (a int); +}, + dump => '-n s', + min_cmds => 1, + todo_dump => 'default_table_access_method is not emitted', + }, + { + # ALTER COLUMN TYPE rebuilds cl_a and re-applies CLUSTER ON to it. + # The user's SET WITHOUT CLUSTER, CLUSTER ON cl_a leaves it clustered. + name => 'cluster_on', + both => q{ +CREATE SCHEMA s; +CREATE TABLE s.cl (a int, b int); +CREATE INDEX cl_a ON s.cl (a); +ALTER TABLE s.cl CLUSTER ON cl_a; +}, + ddl => q{ +ALTER TABLE s.cl ALTER COLUMN a TYPE bigint, SET WITHOUT CLUSTER, + CLUSTER ON cl_a; +}, + dump => '-n s', + min_cmds => 1, + todo_dump => + "CLUSTER ON re-applied by ALTER TYPE is taken for the user's", + }, + { + # The user's REPLICA IDENTITY FULL replaces the one ALTER COLUMN TYPE + # re-applies. + name => 'replica_identity_full', + both => q{ +CREATE SCHEMA s; +CREATE TABLE s.ri (a int NOT NULL, b int); +CREATE UNIQUE INDEX ri_a ON s.ri (a); +ALTER TABLE s.ri REPLICA IDENTITY USING INDEX ri_a; +}, + ddl => q{ +ALTER TABLE s.ri ALTER COLUMN a TYPE bigint, REPLICA IDENTITY FULL; +}, + dump => '-n s', + min_cmds => 1, + }, + { + # As for CLUSTER ON: ALTER COLUMN TYPE re-applies REPLICA IDENTITY + # USING INDEX ri_a, and the user's DEFAULT followed by USING INDEX + # ri_a must leave it set. + name => 'replica_identity_reset', + both => q{ +CREATE SCHEMA s; +CREATE TABLE s.ri (a int NOT NULL, b int); +CREATE UNIQUE INDEX ri_a ON s.ri (a); +ALTER TABLE s.ri REPLICA IDENTITY USING INDEX ri_a; +}, + ddl => q{ +ALTER TABLE s.ri ALTER COLUMN a TYPE bigint, REPLICA IDENTITY DEFAULT, + REPLICA IDENTITY USING INDEX ri_a; +}, + dump => '-n s', + min_cmds => 1, + todo_dump => + "REPLICA IDENTITY re-applied by ALTER TYPE is taken for the user's", + },); + +# Run the DDL of a case on the source and return the deparsed commands. +sub capture_deparsed +{ + my ($db, $ddl) = @_; + my ($stdout, $stderr); + + my $ret = $source->psql( + $db, + "SET test_ddl_deparse.print_deparsed_ddl = text;\n$ddl", + stdout => \$stdout, + stderr => \$stderr, + on_error_stop => 1); + die "DDL failed on source: $stderr" if $ret != 0; + + # A NOTICE may span several lines; it ends where the next message + # starts. + my @cmds; + my $cur; + for my $line (split /\n/, $stderr) + { + if ($line =~ /^(?:psql:[^:]*:\d+: )?NOTICE: deparsed DDL: (.*)$/) + { + push @cmds, $cur if defined $cur; + $cur = $1; + } + elsif ($line =~ /^(?:psql:[^:]*:\d+: )?[A-Z]+: /) + { + push @cmds, $cur if defined $cur; + undef $cur; + } + elsif (defined $cur) + { + $cur .= "\n$line"; + } + } + push @cmds, $cur if defined $cur; + return @cmds; +} + +sub dump_schema +{ + my ($node, $db, $opts) = @_; + my ($stdout, $stderr); + # A failed dump is returned as its error, so that it shows up as a + # mismatch. The target may lack the objects if the replay failed. + IPC::Run::run( + [ + 'pg_dump', '--schema-only', + '--no-owner', '--restrict-key=deparse', + split(/ /, $opts), '-d', + $node->connstr($db) + ], + '>' => \$stdout, + '2>' => \$stderr) or return "pg_dump failed: $stderr"; + + # Drop comments and blank lines, which name the dump's source. + return join("\n", grep { !/^--/ && !/^\s*$/ } split(/\n/, $stdout)); +} + +for my $case (@cases) +{ + my $db = $case->{name}; + + for my $node ($source, $target) + { + $node->safe_psql('postgres', "CREATE DATABASE $db"); + $node->safe_psql($db, $case->{both}) if $case->{both}; + } + $source->safe_psql($db, 'CREATE EXTENSION test_ddl_deparse'); + $source->safe_psql($db, $case->{source_only}) if $case->{source_only}; + + my @cmds = capture_deparsed($db, $case->{ddl}); + + cmp_ok(scalar(@cmds), '>=', $case->{min_cmds}, + "$db: enough commands were deparsed"); + + # Replay on the target as a single transaction, as an apply worker + # would, under an empty search_path. + my $script = "SET search_path = '';\n" . join('', map { "$_;\n" } @cmds); + my ($stdout, $stderr); + my $ret = $target->psql( + $db, $script, + stdout => \$stdout, + stderr => \$stderr, + on_error_stop => 1, + extra_params => ['--single-transaction']); + + TODO: + { + local $TODO = $case->{todo_replay}; + + is($ret, 0, "$db: deparsed commands replay on the target") + or diag("replay script:\n$script\nerror:\n$stderr"); + } + + TODO: + { + local $TODO = $case->{todo_dump}; + + my $src_dump = dump_schema($source, $db, $case->{dump}); + my $tgt_dump = dump_schema($target, $db, $case->{dump}); + is($tgt_dump, $src_dump, "$db: target schema matches source"); + } +} + +done_testing();