Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1wf3Ij-005EWQ-0x for pgsql-hackers@arkaria.postgresql.org; Wed, 01 Jul 2026 22:21:01 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1wf3Hi-00Fvzp-33 for pgsql-hackers@arkaria.postgresql.org; Wed, 01 Jul 2026 22:19:58 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1wf3Hi-00Fvzg-1y for pgsql-hackers@lists.postgresql.org; Wed, 01 Jul 2026 22:19:58 +0000 Received: from mail-yw1-x112f.google.com ([2607:f8b0:4864:20::112f]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.98.2) (envelope-from ) id 1wf3Hf-00000001HHf-3DAg for pgsql-hackers@lists.postgresql.org; Wed, 01 Jul 2026 22:19:57 +0000 Received: by mail-yw1-x112f.google.com with SMTP id 00721157ae682-80d33d13a23so14461187b3.1 for ; Wed, 01 Jul 2026 15:19:55 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1782944394; cv=none; d=google.com; s=arc-20260327; b=s1iCK5MkXVPRHCx1sS6r72/rh6cHYCwoEpHCqYBeXnWOFXZ5IATaSSmqZgA9mY2xQJ Bpc/wBZ2u7U0T3riqeEJUwV5Pb9vGuKwilf4IyYL/Lg17IdiBsbfuqBEY4wbaYylLeUu CLRBa5Vo6OMqrx2UkjWSxYX332inH8OVNzGap6Z1xwKPh2uH/31biocMmAyqHLCTAnc0 3VMQh20RnG0WcNzfVsqNBTMranDqk+Xq+csIQOhsqrgKlQ8QGUqMMzyriqxCRhf5uwoK N0iewlxF8/RmXUy1tc//VvBi0tnvrY/zUpI9fjMArsu69yzL9tTYfDUtM0sFkglTFtYN duDg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20260327; h=to:subject:message-id:date:mime-version:references:in-reply-to:from :dkim-signature; bh=yfI7v0Jjv5sEUsFldCAaErzy4YARHT4YwM2wLpgXl5U=; fh=nwNxTtLLPTU0ewfLM7SSbrjMajMl+wwnFkCY/fi90vE=; b=IadyxYRQ9BnZTVtBCWPCRmws6Hu9qbv9lySwsSF0XOy53wcsyNFXHxNWrLA1PbauCr CZzZBXTQcCcvotOq51WGJanLPjqVdjA2g52XkUOmVClubJEmiYsdZ3EQxw7UUiio18j+ HsVq/Qcele6GxnuP76itQk6s2cJKdCoS4/Dt+xxFxfTccZymucoDBPJG1unRHL2kCcEB r0rgG7SBHyQpWFLYL4tZrwo0Wz+bOrNU2eqwdOx4RZXis8/8825HMjbGvx6rUqXU1XvL F4vlglYSo01uKaRtO9RWv2ZXTf6XLl/M4MFn0vde2hBJDE69GuuzjM7tDhuMnTqTlzoT yODg==; darn=lists.postgresql.org ARC-Authentication-Results: i=1; mx.google.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=percona.com; s=google; t=1782944394; x=1783549194; darn=lists.postgresql.org; h=to:subject:message-id:date:mime-version:references:in-reply-to:from :from:to:cc:subject:date:message-id:reply-to; bh=yfI7v0Jjv5sEUsFldCAaErzy4YARHT4YwM2wLpgXl5U=; b=srt2cwkxnE529BVrqMqKH62jqqXw/XbptXngG3NMBVAgo4K19JMu0vbeQ+y1L5ZF2D AmUHrXCzJsa1KhRgLdDXG5JywZg6nKPgvbdoVCUQthB3v2nH1B8ZRcoDli8xbcBwYWNh cvyMV5XGUH8p+gaOehnYGXZbmG2Qa7Vh0jkSpJJHITD/PKznNLitJm7F1trsq9vrkun1 pgyP3hX+bIFIqDM+G9czpF69sLjXqR8NjcW8zX/HZcwDh+bYE8rFdOO6vXlep/2cSrLK EMBUwY9T2a62BPJUsRSPAkuzui8dwYZ6k5mLT8iAxpywre5aZWITdm+CUdzT/d8Di0zO 9kMg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1782944394; x=1783549194; h=to:subject:message-id:date:mime-version:references:in-reply-to:from :x-gm-gg:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=yfI7v0Jjv5sEUsFldCAaErzy4YARHT4YwM2wLpgXl5U=; b=kGbgd7PorxuoS0bxX2V9ozCf214fKeiZcmBdB9ZsgH9oEJ35iWxn/76SLugXDSAdRs k1BzTEsfrbxNHcctHVYX8Fttz+qqReafVqWxi296PLgFWjx+K06DNMyEVRMi3FbLHsbx eYrXZr/TRzoAFV7vVawC614M2dGUg4GiAmAXqiiAX0qHqIDCuTTzqXzG/bHMJGvt67Za uQHrqmmOp0aiaeBa7KeBwn84SrZSwE+iDq7UBH0YNnTZoef12LDGvv7pbR/4TI2o3P+n helD+WdqE2TX+sERZhv53l2pg5xgL0Kv6Pj0UxDfAeXlnL/exFZ0roIM2kN462hleXka QEuw== X-Gm-Message-State: AOJu0Yz04EzPqKT2FSDP5ot6t+NlXgedL5oumbCwX4nNCIz5L32RrCKf rSZnSj4fAFNHQOZskW1V+h6t+kPib03fZOe/PxrPTey0SGxztRyiwFTs1zUX5QDOXfJimvuUvQG CgBibudyYEXX160TReHeWzO3eHat6IQfs45/NjGrt1EyXvhbj2MoYhySEdQIScxUsZ4F4bsin8B PxoVq5RirXKiUD1zZ/fwmWjEzuFrZmpgwYHX8TU7nr51Xt0OJO1nQ6CHgBYqqiZJuNfF4RWW1Nk ymXFrz1aWQsHbSywtudYfF0AtJWQKN7VDmffDAczs9bLYim/x3/JDBOl9L/IuVZkdxdCaJbRT3v cw== X-Gm-Gg: AfdE7clsl4thAauO9r8i1gzDQOEfb7/+WN2VzgnVOdMcDiBEpbliFs56JK4Buajbd8y ItyeJox/FIdNgEMFiDTnhpu505V1/Ml8zNRKqzv+v/cuy+vhOqKzGbKd+XB2zhxgtzVU8oKH0WI ftHmsCiILYOrmenWy8fDlgs9rPpfQEpJY95CJJoCZMcgRwT2MbRqVUXAaMbq/7JcCiWHVvMXHbJ B4yWhK94e979pqbIFScRu1hRZthTratmwgptwA9gg/yOwG+hrJ30gdiu/dLdZgL1Y6d4eOxFZkr ahfmT1kCvLBj2yPHIUu+lpGdxQK+v2hENiDBEeFbxW414Kzb2A79tIvFJA== X-Received: by 2002:a05:690c:6f12:b0:7f4:e55a:5e2a with SMTP id 00721157ae682-812eae812d8mr40265547b3.34.1782944393568; Wed, 01 Jul 2026 15:19:53 -0700 (PDT) Received: from 298783833264 named unknown by gmailapi.google.com with HTTPREST; Wed, 1 Jul 2026 15:19:51 -0700 Received: from 298783833264 named unknown by gmailapi.google.com with HTTPREST; Wed, 1 Jul 2026 15:19:51 -0700 From: Zsolt Parragi In-Reply-To: References: <983C7B03-D9C3-4F15-8913-7616EC5E6414@gmail.com> <20260623.162104.775158751399277188.horikyota.ntt@gmail.com> MIME-Version: 1.0 Date: Wed, 1 Jul 2026 15:19:51 -0700 X-Gm-Features: AVVi8CeKR-LazRs8G20hlYGwsJWwHYt9t3lTjCoBf7Lux-gV_uS4EVuhhxm8V9I Message-ID: Subject: Re: [PATCH] Add pg_get_table_ddl() to reconstruct CREATE TABLE statements To: pgsql-hackers@lists.postgresql.org Content-Type: text/plain; charset="UTF-8" X-CLOUD-SEC-AV-Sent: true X-CLOUD-SEC-AV-Info: percona,google_mail,monitor X-Gm-Spam: 0 X-Gm-Phishy: 0 List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk 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?