Re: DDL deparse

From: Nikhil Sontakke <nikhil(at)planetscale(dot)com>
To: Masahiko Sawada <sawada(dot)mshk(at)gmail(dot)com>
Cc: Bharath Rupireddy <bharath(dot)rupireddyforpostgres(at)gmail(dot)com>, PostgreSQL-development <pgsql-hackers(at)postgresql(dot)org>, Álvaro Herrera <alvherre(at)kurilemu(dot)de>, shveta malik <shveta(dot)malik(at)gmail(dot)com>, Amit Kapila <amit(dot)kapila16(at)gmail(dot)com>
Subject: Re: DDL deparse
Date: 2026-12-04 10:03:38
Message-ID: CA+UBoq3fkQS+KeoS1_JFxQ4Y6_JwMxDkx7x6WBRF18+0TCJcMA@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Hi Sawada-san,

On Sat, Nov 14, 2026 at 7:09 AM Masahiko Sawada <sawada(dot)mshk(at)gmail(dot)com>
wrote:

> On Wed, Nov 11, 2026 at 4:17 PM Masahiko Sawada <sawada(dot)mshk(at)gmail(dot)com>
> wrote:
> >
> > Hi,
> >
> > Sorry for the late reply.
> >
> > On Wed, Oct 14, 2026 at 12:48 AM Nikhil Sontakke <nikhil(at)planetscale(dot)com>
> wrote:
> > >
> > >>
> > >> I've rebased and updated the patch.
> > >>
> > >
> > > I reviewed and tested the v2 series. And found some issues that I
> attempted to fix via
> > > two follow-up patches, to apply in order on top of your four-patch
> series.
> > >
> > > The first addresses three correctness cases:
> > >
> > > * Repeated identity clauses on the same column currently match the
> first
> > > raw subcommand more than once. The patch matches successive collected
> > > identity subcommands to successive raw occurrences.
> > >
> > > * ADD COLUMN b int DEFAULT 1 followed by ALTER COLUMN b SET DEFAULT 2
> > > currently reads the final catalog default for both clauses. The patch
> > > captures each relevant subcommand's cooked default when it is
> collected,
> > > preserving the distinct effects on existing rows and future inserts.
> > >
> > > * Table-level IF EXISTS is preserved for ordinary ALTER TABLE, column
> and
> > > constraint renames, and SET SCHEMA. Table rename already handles it.
> > >
> > > The second patch adds SQL regression coverage for nested DDL. I'm
> happy to
> > > report that these cases worked with the existing deparsing logic. The
> harness changes
> > > use the existing settings without adding another GUC.
> >
> > Thank you for the report and the patch!
> >
> > I've reviewed and fixed them. I'll post the updated patch soon.
>
> I've attached the updated patches.
>
> While updating the patches, I realized that an event trigger function
> had no reliable way to get the raw parse tree of a DDL together with
> the commands collected for it. If a DDL is executed while another DDL
> is being executed (e.g., by a function called in the USING expression
> of ALTER TABLE ... ALTER COLUMN TYPE), the inner DDL is a separate
> complete query and fires event triggers with its own collected
> commands. Previously, the test module saved the raw parse tree in its
> ProcessUtility hook, but nothing tied it to the collected commands, so
> it could be paired with the inner DDL's commands. I think the event
> trigger state is the right place to keep it. So the 0001 patch makes
> the event trigger code keep a copy of the raw parse tree in the
> per-complete-query state, and adds EventTriggerGetParsetree() to get
> it. The parse tree and the collected commands then always come from
> the same query.
>
> I've also implemented DROP TABLE support (see 0006 patch). Other
> changes are mostly bug fixes and cleanups I found while reviewing the
> patches.
>
> Please note that these patches are still at the PoC phase. Feedback is
> very welcome.
>
>

Thanks for v3. Keeping the parse tree in the event trigger state is a
good fix for the nested-DDL pairing problem.

Further, I'm sending two additions to the test module. I'd like to propose
them for 0003, because they find problems the current tests can't see.
Below is what they find in v3.

The attachments are named nocfbot-* so that cfbot doesn't take them as a
new version of the series. They apply on top of v3 0001-0006.

== The test additions ==

The existing tests replay each deparsed command in the same session and
database as the original, inside a subtransaction that is rolled back.
They find a wrong deparse only when it changes the regression output.
That leaves two gaps:

- Anything that depends on the server or the session can't be seen.
- Most catalog properties are never printed. Most objects are dropped
before their test ends, so a wrong result never reaches the output
either.

nocfbot-0001 adds t/002_deparse_replay.pl, which replays on a separate
server:

- Each case runs its DDL on a source node with
print_deparsed_ddl = text, and collects the deparsed commands from the
NOTICEs.
- It replays them on a second, plain node, under search_path = '' and in
a single transaction, as an apply worker would.
- It then compares pg_dump --schema-only output from the two nodes.
- It also checks that at least a minimum number of commands was actually
deparsed, so the test can't pass when nothing is deparsed.
- Cases for known problems are marked TODO, so the test passes today and
each TODO can be removed when its fix lands.

nocfbot-0002 adds a catalog check to each replayed command in
001_deparse_regress.pl:

- At the end of the trial run, before the rollback, it fingerprints each
relation the command created, altered or dropped, plus inheritance
children. The fingerprint is text keyed by name: kind, access
method, options, columns, constraints, indexes, inheritance,
non-internal triggers, rules and owned sequences.
- After the deparsed command runs, it fingerprints the same relations
again and reports the first line that differs.
- A new GUC, test_ddl_deparse.check_catalog (off/log/warning), controls
the report level.
- 001 sets it to log, scans the server log, and fails on any relation
that isn't on a short known list.
- Its runtime is unchanged, about 18s for me here.

Do you see any issues in the logic of both changes?

Over the whole core regression suite, the check reports only one
relation. nulltest has CHECK (col5 IN ('c', 'd')) on a varchar domain,
and ruleutils prints the ANY array in a form that parses back to an
equivalent but different tree. I've put it on the known list.

With both applied, the regression, 001 and 002 tests all pass.

== What they find in v3 ==

1. Derived names are not stable across servers (0002, and 0004 for ADD
CONSTRAINT / ADD COLUMN). A constraint, index or sequence name is
emitted only if the user wrote it. The name chosen on replay depends
on what else exists in the namespace:

-- the source already has t_a_check and t_b_key in schema s
CREATE TABLE s.t (a int CHECK (a > 0), b int UNIQUE);
ALTER TABLE s.t DROP CONSTRAINT t_a_check1;
-- replay: ERROR: constraint "t_a_check1" of relation "t" does not
exist

A later command can also act on the wrong object when names are
reused. This means we should emit the catalog name every time. A
collision on the
target then fails loudly instead of silently picking a different
name.

2. Literals depend on DateStyle (0002). With 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');
-- deparsed: ... FROM ('02/01/2024') TO ('01/02/2024')

The replay under ISO, MDY fails with an empty range. The DEFAULT is
worse: it comes out as '02/01/2024'::date and is silently read as
1 February. Pinning DateStyle = ISO, IntervalStyle = postgres and
extra_float_digits = 3 during deparse (as pg_dump does), and in the
USING rendering in 0004, should be enough.

3. Session defaults are dropped (0002). After
SET default_table_access_method = heap2, CREATE TABLE is deparsed
without USING heap2, and the target creates a heap table.
default_tablespace and default_toast_compression are dropped the same
way. The access method has to be emitted. For tablespace and
compression we should at least decide explicitly.

4. CLUSTER ON and REPLICA IDENTITY are paired with the wrong subcommand
(0004). When ALTER COLUMN TYPE rebuilds an index,
ATPostAlterTypeCleanup() re-applies CLUSTER ON and REPLICA IDENTITY
internally. find_raw_index_marking() then pairs that internal
subcommand with the user's own raw subcommand:

CREATE TABLE s.cl (a int, b int);
CREATE INDEX cl_a ON s.cl (a);
ALTER TABLE s.cl CLUSTER ON cl_a;
ALTER TABLE s.cl ALTER COLUMN a TYPE bigint, SET WITHOUT CLUSTER,
CLUSTER ON cl_a;
-- deparsed: ... CLUSTER ON cl_a, SET WITHOUT CLUSTER
-- original: cl_a clustered; replay: not clustered

REPLICA IDENTITY DEFAULT, REPLICA IDENTITY USING INDEX ri_a goes wrong
the same way. The catalog check reports both at the statement:

WARNING: test_ddl_deparse: deparsed DDL left relation public.cl
different from the original
DETAIL: original: idx CREATE INDEX cl_a ON public.cl USING btree (a)
clustered=t ...
deparsed: idx CREATE INDEX cl_a ON public.cl USING btree (a)
clustered=f ...

Internal re-applications need to be told apart from user subcommands
when they are collected, rather than matched afterwards. There's no
test for this in 0005 today; the replay test has both cases.

5. The identity sequence's type isn't emitted (0002). The identity
options leave out AS <type>, so the replayed sequence takes the
column's type. The catalog check found this in the module's own
tests, on t_like_ident (nocfbot-0002 adds the WARNING to the expected
output). Its sequence is bigint in the original and integer after
replay, because CREATE TABLE ... LIKE ... INCLUDING IDENTITY on an int
identity column creates a bigint sequence. That part is a core bug,
and I have reported it separately here
https://www.postgresql.org/message-id/CA%2BUBoq1wdW6-h2NyH3Ms1x9qTaTS8XWym5SfLBafNPLfoMN3Qw%40mail.gmail.com
.
Either way, the deparser should emit the sequence's type from
pg_sequence.

== Where each finding shows up ==

derived_names replay fails, schemas differ (TODO)
datestyle replay fails with an empty range (TODO)
table_am target table uses heap (TODO)
cluster_on cl_a not clustered on the target (TODO)
replica_identity_reset replica identity reset on the target (TODO)
replica_identity_full passes
basic passes
catalog check (001) items 4 and 5 above, plus nulltest (known)

If these look useful, could you fold them into 0003? Then each fix can
remove its TODO, and new commands get the same coverage as they're added.
I'm also happy to send patches for items 1-3 and 5, and to prototype
item 4 once you've decided how the collector should mark internal
subcommands.

Regards,
---
Nikhil Sontakke
PlanetScale

Attachment Content-Type Size
nocfbot-0002-Check-that-each-replayed-DDL-command-leaves-the-cata.patch application/octet-stream 17.2 KB
nocfbot-0001-Add-a-TAP-test-that-replays-deparsed-DDL-on-a-separa.patch application/octet-stream 10.5 KB

In response to

Browse pgsql-hackers by date

  From Date Subject
Previous Message John Naylor 2026-12-04 09:56:06 Re: Improving scalability of Parallel Bitmap Heap/Index Scan