Re: [PATCH] Add pg_get_table_ddl() to reconstruct CREATE TABLE statements

Akshay Joshi <akshay.joshi@enterprisedb.com>

From: Akshay Joshi <akshay.joshi@enterprisedb.com>
To: Zsolt Parragi <zsolt.parragi@percona.com>
Cc: pgsql-hackers@lists.postgresql.org
Date: 2026-06-24T09:16:03Z
Lists: pgsql-hackers

Attachments

On Wed, Jun 24, 2026 at 1:35 AM Zsolt Parragi <zsolt.parragi@percona.com>
wrote:

> The new design is definitely an improvement as a more generic
> interface. I'm unsure about the "include" wording, to me that suggest
> "also includes", while the actual behavior is "only includes". To me
> it would be a bit surprising pg_get_table_ddl including something not
> including the basic create table statement.
>
> I also found a few issues with include in v10.
>
> Include check doesn't seem to work:
>
> CREATE TABLE t (
>     id int PRIMARY KEY,
>     qty int CHECK (qty > 0),
>     CONSTRAINT t_id_pos CHECK (id > 0)
> );
>
> SELECT * FROM pg_get_table_ddl('t', 'include', 'check'); -- empty?
>
> The include partitions clause also doesn't work:
>
> CREATE TABLE p (id int, val text) PARTITION BY RANGE (id);
> CREATE TABLE p_a PARTITION OF p FOR VALUES FROM (0) TO (100);
> CREATE TABLE p_b PARTITION OF p FOR VALUES FROM (100) TO (200);
>
> SELECT * FROM pg_get_table_ddl('p','include','partitions'); -- empty
>
> There's also an issue with replica identity non primary key unique indexes:
>
> CREATE TABLE t2 (a int NOT NULL UNIQUE, b int);
> ALTER TABLE t2 REPLICA IDENTITY USING INDEX t2_a_key;
> SELECT * FROM pg_get_table_ddl('t2','exclude','unique');
> -- ALTER TABLE public.t2 REPLICA IDENTITY USING INDEX t2_a_key;
> -- but there's no such index
>
> Thanks for the review. In the attached patch, I’ve renamed
*include/exclude* to *only* and *except*. If you have any other suggestions
or a better name, please let me know.
Fixed all the above issues as well.

The semantics remain unchanged:
  1) only => 'kind1,kind2,...' — emits only the listed kinds.
  2) except => 'kind1,kind2,...' — emits everything except the listed kinds.
  3) Setting both raises an error; setting neither emits all kinds (the
default).

I also dropped the trailing "s" from the multi-item kind names so the
vocabulary reads naturally with only/except (e.g., index, foreign_key,
rule, trigger, policy, partition). statistics and rls retain their existing
spellings. The documentation, regression tests, and func-info.sgml have all
been updated accordingly.

The v11 patch is now ready for review and testing.