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

From: Akshay Joshi <akshay(dot)joshi(at)enterprisedb(dot)com>
To: Zsolt Parragi <zsolt(dot)parragi(at)percona(dot)com>
Cc: pgsql-hackers(at)lists(dot)postgresql(dot)org
Subject: Re: [PATCH] Add pg_get_table_ddl() to reconstruct CREATE TABLE statements
Date: 2026-07-02 10:39:50
Message-ID: CANxoLDduU1sPUG+RBmPooKXBgFgFuctu4MH_mFuNoQeYy-F=wQ@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Thanks for the review, I have fixed the mentioned issue.
The v15 patch is ready for review.

On Thu, Jul 2, 2026 at 3:50 AM Zsolt Parragi <zsolt(dot)parragi(at)percona(dot)com>
wrote:

> I did some more testing, I noticed one more issue with self
> referencing foreign keys:
>
> CREATE TABLE t (id int PRIMARY KEY, parent_id int REFERENCES t(id));
> SELECT pg_get_table_ddl('t'::regclass);
> -- CREATE TABLE public.t (id integer NOT NULL, parent_id integer);
> -- ALTER TABLE public.t OWNER TO postgres;
> -- ALTER TABLE public.t ADD CONSTRAINT t_parent_id_fkey FOREIGN KEY
> (parent_id) REFERENCES public.t(id);
> -- ALTER TABLE public.t ADD CONSTRAINT t_pkey PRIMARY KEY (id);
>
> It tries to add the foreign key before the primary, and fails with
> `ERROR: there is no unique constraint matching given keys for
> referenced table "t"`
>
> There's also another issue in schema_qualified false, with partitions
> in different schemas:
>
> CREATE SCHEMA s;
> CREATE SCHEMA other;
> CREATE TABLE s.pt (id int, val int) PARTITION BY RANGE (id);
> CREATE TABLE other.pt_c PARTITION OF s.pt FOR VALUES FROM (0) TO (100);
> SELECT pg_get_table_ddl('s.pt'::regclass, schema_qualified => false);
> -- CREATE TABLE pt (id integer, val integer) PARTITION BY RANGE (id);
> -- ALTER TABLE pt OWNER TO postgres;
> -- CREATE TABLE pt_c PARTITION OF s.pt FOR VALUES FROM (0) TO (100);
> -- ALTER TABLE pt_c OWNER TO postgres;
>
> The second create table statement references pt as s.pt, which seems
> incorrect.
> It is also missing its own schema qualification, which I'm unsure if
> it is wrong or not. If I interpret the documentation strictly, it
> isn't the target table, so it should appear with its schema
> qualification?
>
>
>

Attachment Content-Type Size
v15-0001-Add-pg_get_table_ddl-to-reconstruct-CREATE-TABLE.patch application/octet-stream 199.1 KB

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Andrei Lepikhov 2026-07-02 11:05:57 Re: RFC: Logging plan of the running query
Previous Message Álvaro Herrera 2026-07-02 10:20:52 Re: DROP INVALID INDEXES command