# Copyright (c) 2026, PostgreSQL Global Development Group # RuDB: section selection must precede comment/AQ data conversion, and # restoring AQ properties must preserve user data and reject non-seed state. use strict; use warnings FATAL => 'all'; use PostgreSQL::Test::Cluster; use PostgreSQL::Test::Utils; use Test::More; my $node = PostgreSQL::Test::Cluster->new('rudb_aq_sections'); $node->init(extra => ['--encoding=UTF8', '--locale=C']); $node->start; my $tmp = PostgreSQL::Test::Utils::tempdir; my $src = 'postgres'; my $dst = 'aq_restore'; my $aq = '"SYS"."AQ$_SYSTEM_PROPS"'; my $aq_rows = qq{SELECT "NAME", "VALUE" FROM $aq ORDER BY "NAME" COLLATE "C"}; my @dump = ('pg_dump', '-d', $src, '--encoding=SJIS'); my $bad_encoding = qr/has no equivalent|cannot be converted/; $node->safe_psql($src, q{ CREATE SCHEMA sec; CREATE TABLE sec.t (id int, payload text); INSERT INTO sec.t VALUES (1, 'marker'); CREATE INDEX t_idx ON sec.t(id); CREATE TYPE sec.comp AS (a int, b text); COMMENT ON TABLE sec.t IS U&'\+01F600'; COMMENT ON COLUMN sec.comp.b IS U&'\+01F600'; }); $node->command_like([@dump, '-n', 'sec', '--section=data'], qr/COPY sec\.t/, 'data section excludes pre-data comments'); $node->command_like([@dump, '-n', 'sec', '--section=post-data'], qr/CREATE INDEX t_idx/, 'post-data section excludes pre-data comments'); $node->command_fails_like([@dump, '-t', 'sec.t', '--section=pre-data'], $bad_encoding, 'selected table comment must be representable'); $node->command_fails_like([@dump, '-n', 'sec', '--section=pre-data'], $bad_encoding, 'selected pre-data comments must be representable'); $node->safe_psql($src, q{COMMENT ON TABLE sec.t IS U&'\65E5\672C'; COMMENT ON COLUMN sec.comp.b IS 'composite column'; COMMENT ON INDEX sec.t_idx IS U&'\+01F600';}); $node->command_like([@dump, '-n', 'sec', '--section=pre-data'], qr/CREATE TABLE sec\.t/, 'pre-data section excludes post-data comment'); $node->command_like([@dump, '-n', 'sec', '--section=data'], qr/COPY sec\.t/, 'data section excludes post-data comment'); $node->command_fails_like([@dump, '-n', 'sec', '--section=post-data'], $bad_encoding, 'selected index comment must be representable'); $node->command_like([@dump, '-n', 'sec', '--no-comments'], qr/CREATE INDEX t_idx/, 'no-comments skips unrepresentable text'); $node->safe_psql($src, q{COMMENT ON INDEX sec.t_idx IS 'marker index'; COMMENT ON COLUMN sec.comp.b IS U&'\+01F600';}); $node->command_fails_like([@dump, '-n', 'sec', '--section=pre-data'], $bad_encoding, 'selected composite column comment must be representable'); $node->safe_psql($src, q{COMMENT ON COLUMN sec.comp.b IS 'composite column';}); # An unrepresentable AQ key must matter only when AQ data is selected. $node->safe_psql($src, qq{INSERT INTO $aq VALUES (U&'AQ_\\+01F600', 7)}); $node->command_like([@dump, '--section=pre-data'], qr/CREATE TABLE sec\.t/, 'pre-data excludes AQ data conversion'); $node->command_like([@dump, '--section=post-data'], qr/CREATE INDEX t_idx/, 'post-data excludes AQ data conversion'); $node->command_like([@dump, '-n', 'sec', '--section=data'], qr/COPY sec\.t/, 'schema selection excludes AQ data'); $node->command_fails_like([@dump, '--section=data'], $bad_encoding, 'selected AQ data must be representable'); $node->safe_psql($src, qq{DELETE FROM $aq WHERE "NAME" = U&'AQ_\\+01F600'; UPDATE $aq SET "VALUE" = CASE "NAME" WHEN 'WATERMARK' THEN 47 WHEN 'MAX_STREAMS_POOL' THEN 13 ELSE 2 END; INSERT INTO $aq VALUES (U&'AQ_\\65E5\\672C', 123.45);}); my $expected = $node->safe_psql($src, $aq_rows); my $expected_comment = $node->safe_psql($src, q{SELECT obj_description('sec.t'::regclass)}); my $seed_rows = "MAX_STREAMS_POOL|0\nMIN_STREAMS_POOL|0\nWATERMARK|10"; sub fresh_target { $node->safe_psql('postgres', "DROP DATABASE IF EXISTS $dst"); $node->safe_psql('postgres', "CREATE DATABASE $dst TEMPLATE template0"); } for my $format ('plain', 'custom', 'tar', 'directory') { my $file = "$tmp/aq_$format"; my @parallel = $format eq 'directory' ? ('-j2') : (); fresh_target(); is($node->safe_psql($dst, $aq_rows), $seed_rows, "$format: target has exact initdb seeds"); $node->command_ok([@dump, '-F', $format, '-f', $file, @parallel], "$format: dump AQ and ordinary objects in SJIS"); if ($format eq 'plain') { $node->command_ok(['psql', '-X', '-d', $dst, '-v', 'ON_ERROR_STOP=1', '-f', $file], "$format: restore"); } else { $node->command_ok(['pg_restore', '-d', $dst, '--exit-on-error', @parallel, $file], "$format: restore"); } is($node->safe_psql($dst, $aq_rows), $expected, "$format: changed seeds and custom AQ key/value preserved"); is($node->safe_psql($dst, q{SELECT * FROM sec.t}), '1|marker', "$format: ordinary table restored"); is($node->safe_psql($dst, q{SELECT obj_description('sec.t'::regclass)}), $expected_comment, "$format: selected comment round trips"); } my $selected = "$tmp/aq_selected.dump"; $node->command_ok([@dump, '-Fc', '--data-only', '-t', $aq, '-f', $selected], 'selected AQ data-only archive'); fresh_target(); $node->safe_psql($dst, "DELETE FROM $aq"); $node->command_ok(['pg_restore', '-d', $dst, '--exit-on-error', $selected], 'restore into empty AQ table'); is($node->safe_psql($dst, $aq_rows), $expected, 'empty target restored'); fresh_target(); $node->safe_psql($dst, qq{UPDATE $aq SET "VALUE" = 99 WHERE "NAME" = 'WATERMARK'}); my $before = $node->safe_psql($dst, $aq_rows); $node->command_fails_like(['pg_restore', '-d', $dst, '--exit-on-error', $selected], qr/unexpected AQ system property seeds/, 'reject non-seed target'); is($node->safe_psql($dst, $aq_rows), $before, 'failed restore leaves AQ unchanged'); $node->stop; done_testing();