pg.ddx.io pgsql-hackers@postgresql.org mailing list archive
help / color / mirror / Atom feedCOMMENTS are not being copied in CREATE TABLE LIKE
37+ messages / 11 participants
[nested] [flat]
* COMMENTS are not being copied in CREATE TABLE LIKE
@ 2026-02-12 10:11 Jim Jones <jim.jones@uni-muenster.de>
2026-02-12 14:36 ` Re: COMMENTS are not being copied in CREATE TABLE LIKE Matheus Alcantara <matheusssilv97@gmail.com>
2026-07-18 14:03 ` Re: COMMENTS are not being copied in CREATE TABLE LIKE ZizhuanLiu X-MAN <44973863@qq.com>
0 siblings, 2 replies; 37+ messages in thread
From: Jim Jones @ 2026-02-12 10:11 UTC (permalink / raw)
To: pgsql-hackers@lists.postgresql.org <pgsql-hackers@lists.postgresql.org>; +Cc: Matheus Alcantara <matheusssilv97@gmail.com>
Hi,
While reviewing another patch[1] I saw that COMMENTS on tables are being
ignored in CREATE TABLE LIKE:
psql (18.1 (Debian 18.1-1.pgdg13+2))
Type "help" for help.
postgres=# \pset null '(null)'
Null display is "(null)".
postgres=# CREATE TABLE t1 (id int, name text);
COMMENT ON TABLE t1 IS 'table comment';
CREATE TABLE t2 (LIKE t1 INCLUDING ALL);
CREATE TABLE t3 (LIKE t1 INCLUDING COMMENTS);
SELECT
obj_description('t1'::regclass, 'pg_class') AS t1_comment,
obj_description('t2'::regclass, 'pg_class') AS t2_comment,
obj_description('t3'::regclass, 'pg_class') AS t3_comment;
CREATE TABLE
COMMENT
CREATE TABLE
CREATE TABLE
t1_comment | t2_comment | t3_comment
---------------+------------+------------
table comment | (null) | (null)
(1 row)
v1 attached attempts to fix it by expanding expandTableLikeClause() to
retrieve and copy the table-level comment when the INCLUDING COMMENTS
[ALL] option is specified:
psql (19devel)
Type "help" for help.
postgres=# CREATE TABLE t1 (id int, name text);
COMMENT ON TABLE t1 IS 'table comment';
CREATE TABLE t2 (LIKE t1 INCLUDING ALL);
CREATE TABLE t3 (LIKE t1 INCLUDING COMMENTS);
SELECT
obj_description('t1'::regclass, 'pg_class') AS t1_comment,
obj_description('t2'::regclass, 'pg_class') AS t2_comment,
obj_description('t3'::regclass, 'pg_class') AS t3_comment;
CREATE TABLE
COMMENT
CREATE TABLE
CREATE TABLE
t1_comment | t2_comment | t3_comment
---------------+---------------+---------------
table comment | table comment | table comment
(1 row)
Thoughts?
Best, Jim
1 -
https://www.postgresql.org/message-id/flat/DG7Y34A6VBEG.76L7K1OML5DI%40gmail.com
Attachments:
v1-0001-Fix-missing-table-level-comment-copying-in-CREATE.patch (text/x-patch, 5.2K) download
From 5afdb72e13092a94d01d40dcdc78a96c2632b423 Mon Sep 17 00:00:00 2001
From: Jim Jones <jim.jones@uni-muenster.de>
Date: Thu, 12 Feb 2026 10:39:25 +0100
Subject: [PATCH v1] Fix missing table-level comment copying in CREATE TABLE
LIKE
When using CREATE TABLE ... LIKE ... INCLUDING COMMENTS (or INCLUDING
ALL), table-level comments were not being copied to the new table, even
though column comments, constraint comments, index comments, and
statistics comments were properly copied.
---
src/backend/parser/parse_utilcmd.c | 36 +++++++++++++++----
.../regress/expected/create_table_like.out | 7 ++++
src/test/regress/sql/create_table_like.sql | 2 ++
3 files changed, 39 insertions(+), 6 deletions(-)
diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c
index b5f4c72459..bc6c9175a3 100644
--- a/src/backend/parser/parse_utilcmd.c
+++ b/src/backend/parser/parse_utilcmd.c
@@ -1307,17 +1307,18 @@ transformTableLikeClause(CreateStmtContext *cxt, TableLikeClause *table_like_cla
}
/*
- * We cannot yet deal with defaults, CHECK constraints, indexes, or
- * statistics, since we don't yet know what column numbers the copied
- * columns will have in the finished table. If any of those options are
- * specified, add the LIKE clause to cxt->likeclauses so that
- * expandTableLikeClause will be called after we do know that.
+ * We cannot yet deal with defaults, CHECK constraints, indexes,
+ * statistics, or table comments, since we don't yet know what column
+ * numbers the copied columns will have in the finished table. If any of
+ * those options are specified, add the LIKE clause to cxt->likeclauses
+ * so that expandTableLikeClause will be called after we do know that.
*
* In order for this to work, we remember the relation OID so that
* expandTableLikeClause is certain to open the same table.
*/
if (table_like_clause->options &
- (CREATE_TABLE_LIKE_DEFAULTS |
+ (CREATE_TABLE_LIKE_COMMENTS |
+ CREATE_TABLE_LIKE_DEFAULTS |
CREATE_TABLE_LIKE_GENERATED |
CREATE_TABLE_LIKE_CONSTRAINTS |
CREATE_TABLE_LIKE_INDEXES |
@@ -1625,6 +1626,29 @@ expandTableLikeClause(RangeVar *heapRel, TableLikeClause *table_like_clause)
list_free(parent_extstats);
}
+ /*
+ * Copy comment on the relation itself, if requested.
+ */
+ if (table_like_clause->options & CREATE_TABLE_LIKE_COMMENTS)
+ {
+ comment = GetComment(RelationGetRelid(relation), RelationRelationId, 0);
+
+ if (comment != NULL)
+ {
+ CommentStmt *stmt = makeNode(CommentStmt);
+
+ stmt->objtype = OBJECT_TABLE;
+ if (heapRel->schemaname)
+ stmt->object = (Node *)list_make2(makeString(heapRel->schemaname),
+ makeString(heapRel->relname));
+ else
+ stmt->object = (Node *)list_make1(makeString(heapRel->relname));
+ stmt->comment = comment;
+
+ result = lappend(result, stmt);
+ }
+ }
+
/* Done with child rel */
table_close(childrel, NoLock);
diff --git a/src/test/regress/expected/create_table_like.out b/src/test/regress/expected/create_table_like.out
index d3c35c1484..765155912f 100644
--- a/src/test/regress/expected/create_table_like.out
+++ b/src/test/regress/expected/create_table_like.out
@@ -341,6 +341,7 @@ CREATE TABLE ctlt3 (a text CHECK (length(a) < 5), c text CHECK (length(c) < 7));
ALTER TABLE ctlt3 ALTER COLUMN c SET STORAGE EXTERNAL;
ALTER TABLE ctlt3 ALTER COLUMN a SET STORAGE MAIN;
CREATE INDEX ctlt3_fnidx ON ctlt3 ((a || c));
+COMMENT ON TABLE ctlt3 IS 'ctlt3 table comment';
COMMENT ON COLUMN ctlt3.a IS 'A3';
COMMENT ON COLUMN ctlt3.c IS 'C';
COMMENT ON CONSTRAINT ctlt3_a_check ON ctlt3 IS 't3_a_check';
@@ -449,6 +450,12 @@ SELECT description FROM pg_description, pg_constraint c WHERE classoid = 'pg_con
t3_a_check
(1 row)
+SELECT obj_description('ctlt13_like'::regclass, 'pg_class') AS table_comment;
+ table_comment
+---------------------
+ ctlt3 table comment
+(1 row)
+
CREATE TABLE ctlt_all (LIKE ctlt1 INCLUDING ALL);
\d+ ctlt_all
Table "public.ctlt_all"
diff --git a/src/test/regress/sql/create_table_like.sql b/src/test/regress/sql/create_table_like.sql
index 93389b57db..0390834186 100644
--- a/src/test/regress/sql/create_table_like.sql
+++ b/src/test/regress/sql/create_table_like.sql
@@ -153,6 +153,7 @@ CREATE TABLE ctlt3 (a text CHECK (length(a) < 5), c text CHECK (length(c) < 7));
ALTER TABLE ctlt3 ALTER COLUMN c SET STORAGE EXTERNAL;
ALTER TABLE ctlt3 ALTER COLUMN a SET STORAGE MAIN;
CREATE INDEX ctlt3_fnidx ON ctlt3 ((a || c));
+COMMENT ON TABLE ctlt3 IS 'ctlt3 table comment';
COMMENT ON COLUMN ctlt3.a IS 'A3';
COMMENT ON COLUMN ctlt3.c IS 'C';
COMMENT ON CONSTRAINT ctlt3_a_check ON ctlt3 IS 't3_a_check';
@@ -173,6 +174,7 @@ CREATE TABLE ctlt13_inh () INHERITS (ctlt1, ctlt3);
CREATE TABLE ctlt13_like (LIKE ctlt3 INCLUDING CONSTRAINTS INCLUDING INDEXES INCLUDING COMMENTS INCLUDING STORAGE) INHERITS (ctlt1);
\d+ ctlt13_like
SELECT description FROM pg_description, pg_constraint c WHERE classoid = 'pg_constraint'::regclass AND objoid = c.oid AND c.conrelid = 'ctlt13_like'::regclass;
+SELECT obj_description('ctlt13_like'::regclass, 'pg_class') AS table_comment;
CREATE TABLE ctlt_all (LIKE ctlt1 INCLUDING ALL);
\d+ ctlt_all
--
2.43.0
^ permalink raw reply [nested|flat] 37+ messages in thread
* Re: COMMENTS are not being copied in CREATE TABLE LIKE
2026-02-12 10:11 COMMENTS are not being copied in CREATE TABLE LIKE Jim Jones <jim.jones@uni-muenster.de>
@ 2026-02-12 14:36 ` Matheus Alcantara <matheusssilv97@gmail.com>
2026-02-12 15:08 ` Re: COMMENTS are not being copied in CREATE TABLE LIKE Fujii Masao <masao.fujii@gmail.com>
1 sibling, 1 reply; 37+ messages in thread
From: Matheus Alcantara @ 2026-02-12 14:36 UTC (permalink / raw)
To: Jim Jones <jim.jones@uni-muenster.de>; pgsql-hackers@lists.postgresql.org <pgsql-hackers@lists.postgresql.org>; +Cc: Matheus Alcantara <matheusssilv97@gmail.com>
Hi, thanks for checking this.
On Thu Feb 12, 2026 at 7:11 AM -03, Jim Jones wrote:
> Hi,
>
> While reviewing another patch[1] I saw that COMMENTS on tables are being
> ignored in CREATE TABLE LIKE:
>
> psql (18.1 (Debian 18.1-1.pgdg13+2))
> Type "help" for help.
>
> postgres=# \pset null '(null)'
> Null display is "(null)".
> postgres=# CREATE TABLE t1 (id int, name text);
> COMMENT ON TABLE t1 IS 'table comment';
> CREATE TABLE t2 (LIKE t1 INCLUDING ALL);
> CREATE TABLE t3 (LIKE t1 INCLUDING COMMENTS);
>
> SELECT
> obj_description('t1'::regclass, 'pg_class') AS t1_comment,
> obj_description('t2'::regclass, 'pg_class') AS t2_comment,
> obj_description('t3'::regclass, 'pg_class') AS t3_comment;
> CREATE TABLE
> COMMENT
> CREATE TABLE
> CREATE TABLE
> t1_comment | t2_comment | t3_comment
> ---------------+------------+------------
> table comment | (null) | (null)
> (1 row)
>
>
> v1 attached attempts to fix it by expanding expandTableLikeClause() to
> retrieve and copy the table-level comment when the INCLUDING COMMENTS
> [ALL] option is specified:
>
The patch fix the issue and it seems correct to me.
This bug seems to also happen on 14.20:
postgres=# select version();
version
------------------------------------------------------------------------------------------------------------------------------
PostgreSQL 14.20 (Homebrew) on aarch64-apple-darwin24.6.0, compiled by Apple clang version 17.0.0 (clang-1700.4.4.1), 64-bit
(1 row)
postgres=# CREATE TABLE t(a int, b text);
CREATE TABLE
postgres=# COMMENT ON TABLE t IS 'foo';
COMMENT
postgres=# CREATE TABLE t2(LIKE t INCLUDING ALL);
CREATE TABLE
postgres=# SELECT obj_description('t'::regclass, 'pg_class') as t_comment, obj_description('t2'::regclass, 'pg_class') AS t2_comment;
t_comment | t2_comment
-----------+------------
foo |
(1 row)
So I think that we need to backport.
--
Matheus Alcantara
EDB: https://www.enterprisedb.com
^ permalink raw reply [nested|flat] 37+ messages in thread
* Re: COMMENTS are not being copied in CREATE TABLE LIKE
2026-02-12 10:11 COMMENTS are not being copied in CREATE TABLE LIKE Jim Jones <jim.jones@uni-muenster.de>
2026-02-12 14:36 ` Re: COMMENTS are not being copied in CREATE TABLE LIKE Matheus Alcantara <matheusssilv97@gmail.com>
@ 2026-02-12 15:08 ` Fujii Masao <masao.fujii@gmail.com>
2026-02-12 15:33 ` Re: COMMENTS are not being copied in CREATE TABLE LIKE Jim Jones <jim.jones@uni-muenster.de>
0 siblings, 1 reply; 37+ messages in thread
From: Fujii Masao @ 2026-02-12 15:08 UTC (permalink / raw)
To: Matheus Alcantara <matheusssilv97@gmail.com>; +Cc: Jim Jones <jim.jones@uni-muenster.de>; pgsql-hackers@lists.postgresql.org <pgsql-hackers@lists.postgresql.org>
On Thu, Feb 12, 2026 at 11:36 PM Matheus Alcantara
<matheusssilv97@gmail.com> wrote:
>
> Hi, thanks for checking this.
>
> On Thu Feb 12, 2026 at 7:11 AM -03, Jim Jones wrote:
> > Hi,
> >
> > While reviewing another patch[1] I saw that COMMENTS on tables are being
> > ignored in CREATE TABLE LIKE:
> >
> > psql (18.1 (Debian 18.1-1.pgdg13+2))
> > Type "help" for help.
> >
> > postgres=# \pset null '(null)'
> > Null display is "(null)".
> > postgres=# CREATE TABLE t1 (id int, name text);
> > COMMENT ON TABLE t1 IS 'table comment';
> > CREATE TABLE t2 (LIKE t1 INCLUDING ALL);
> > CREATE TABLE t3 (LIKE t1 INCLUDING COMMENTS);
> >
> > SELECT
> > obj_description('t1'::regclass, 'pg_class') AS t1_comment,
> > obj_description('t2'::regclass, 'pg_class') AS t2_comment,
> > obj_description('t3'::regclass, 'pg_class') AS t3_comment;
> > CREATE TABLE
> > COMMENT
> > CREATE TABLE
> > CREATE TABLE
> > t1_comment | t2_comment | t3_comment
> > ---------------+------------+------------
> > table comment | (null) | (null)
> > (1 row)
> >
> >
> > v1 attached attempts to fix it by expanding expandTableLikeClause() to
> > retrieve and copy the table-level comment when the INCLUDING COMMENTS
> > [ALL] option is specified:
> >
>
> The patch fix the issue and it seems correct to me.
>
> This bug seems to also happen on 14.20:
> postgres=# select version();
> version
> ------------------------------------------------------------------------------------------------------------------------------
> PostgreSQL 14.20 (Homebrew) on aarch64-apple-darwin24.6.0, compiled by Apple clang version 17.0.0 (clang-1700.4.4.1), 64-bit
> (1 row)
>
> postgres=# CREATE TABLE t(a int, b text);
> CREATE TABLE
> postgres=# COMMENT ON TABLE t IS 'foo';
> COMMENT
> postgres=# CREATE TABLE t2(LIKE t INCLUDING ALL);
> CREATE TABLE
> postgres=# SELECT obj_description('t'::regclass, 'pg_class') as t_comment, obj_description('t2'::regclass, 'pg_class') AS t2_comment;
> t_comment | t2_comment
> -----------+------------
> foo |
> (1 row)
>
> So I think that we need to backport.
The documentation [1] states that INCLUDING COMMENTS copies comments for
the copied columns, constraints, and indexes. It does not mention copying
comments on the table itself. Therefore, not copying table comments with
INCLUDING COMMENTS does not appear to be a bug. That is, the proposed patch
seems more like an improvement than a bug fix.
Regards,
[1]
https://www.postgresql.org/docs/devel/sql-createtable.html#SQL-CREATETABLE-PARMS-LIKE-OPT-COMMENTS
--
Fujii Masao
^ permalink raw reply [nested|flat] 37+ messages in thread
* Re: COMMENTS are not being copied in CREATE TABLE LIKE
2026-02-12 10:11 COMMENTS are not being copied in CREATE TABLE LIKE Jim Jones <jim.jones@uni-muenster.de>
2026-02-12 14:36 ` Re: COMMENTS are not being copied in CREATE TABLE LIKE Matheus Alcantara <matheusssilv97@gmail.com>
2026-02-12 15:08 ` Re: COMMENTS are not being copied in CREATE TABLE LIKE Fujii Masao <masao.fujii@gmail.com>
@ 2026-02-12 15:33 ` Jim Jones <jim.jones@uni-muenster.de>
2026-02-12 16:25 ` Re: COMMENTS are not being copied in CREATE TABLE LIKE Matheus Alcantara <matheusssilv97@gmail.com>
2026-02-13 01:29 ` Re: COMMENTS are not being copied in CREATE TABLE LIKE Chao Li <li.evan.chao@gmail.com>
2026-02-13 04:13 ` Re: COMMENTS are not being copied in CREATE TABLE LIKE Fujii Masao <masao.fujii@gmail.com>
0 siblings, 3 replies; 37+ messages in thread
From: Jim Jones @ 2026-02-12 15:33 UTC (permalink / raw)
To: Fujii Masao <masao.fujii@gmail.com>; Matheus Alcantara <matheusssilv97@gmail.com>; +Cc: pgsql-hackers@lists.postgresql.org <pgsql-hackers@lists.postgresql.org>
On 12/02/2026 16:08, Fujii Masao wrote:
> On Thu, Feb 12, 2026 at 11:36 PM Matheus Alcantara
> <matheusssilv97@gmail.com> wrote:
>> The patch fix the issue and it seems correct to me.
Thanks for the review!
> The documentation [1] states that INCLUDING COMMENTS copies comments for
> the copied columns, constraints, and indexes. It does not mention copying
> comments on the table itself. Therefore, not copying table comments with
> INCLUDING COMMENTS does not appear to be a bug. That is, the proposed patch
> seems more like an improvement than a bug fix.
Hmm, it seemed so obvious to me that I didn’t look more closely at the
documentation :) Although I struggle to see the rationale for not
copying a table’s comment in a CREATE TABLE LIKE ... INCLUDING COMMENTS
statement, I have to agree that the documentation is quite clear about
this limitation, and therefore it cannot really be considered a bug.
That said, it may well be worth considering for a future release.
What are your thoughts?
Thanks!
Best, Jim
^ permalink raw reply [nested|flat] 37+ messages in thread
* Re: COMMENTS are not being copied in CREATE TABLE LIKE
2026-02-12 10:11 COMMENTS are not being copied in CREATE TABLE LIKE Jim Jones <jim.jones@uni-muenster.de>
2026-02-12 14:36 ` Re: COMMENTS are not being copied in CREATE TABLE LIKE Matheus Alcantara <matheusssilv97@gmail.com>
2026-02-12 15:08 ` Re: COMMENTS are not being copied in CREATE TABLE LIKE Fujii Masao <masao.fujii@gmail.com>
2026-02-12 15:33 ` Re: COMMENTS are not being copied in CREATE TABLE LIKE Jim Jones <jim.jones@uni-muenster.de>
@ 2026-02-12 16:25 ` Matheus Alcantara <matheusssilv97@gmail.com>
2 siblings, 0 replies; 37+ messages in thread
From: Matheus Alcantara @ 2026-02-12 16:25 UTC (permalink / raw)
To: Jim Jones <jim.jones@uni-muenster.de>; Fujii Masao <masao.fujii@gmail.com>; +Cc: pgsql-hackers@lists.postgresql.org <pgsql-hackers@lists.postgresql.org>
On Thu Feb 12, 2026 at 12:33 PM -03, Jim Jones wrote:
>
>
> On 12/02/2026 16:08, Fujii Masao wrote:
>> On Thu, Feb 12, 2026 at 11:36 PM Matheus Alcantara
>> <matheusssilv97@gmail.com> wrote:
>>> The patch fix the issue and it seems correct to me.
>
> Thanks for the review!
>
>> The documentation [1] states that INCLUDING COMMENTS copies comments for
>> the copied columns, constraints, and indexes. It does not mention copying
>> comments on the table itself. Therefore, not copying table comments with
>> INCLUDING COMMENTS does not appear to be a bug. That is, the proposed patch
>> seems more like an improvement than a bug fix.
>
> Hmm, it seemed so obvious to me that I didn’t look more closely at the
> documentation :)
>
Yeah, me too. I even checked if column comments was being created, but I
still assume that table comments should also be copied.
> Although I struggle to see the rationale for not
> copying a table’s comment in a CREATE TABLE LIKE ... INCLUDING COMMENTS
> statement, I have to agree that the documentation is quite clear about
> this limitation, and therefore it cannot really be considered a bug.
> That said, it may well be worth considering for a future release.
>
> What are your thoughts?
>
+1 for having this as a feature on a future release.
--
Matheus Alcantara
EDB: https://www.enterprisedb.com
^ permalink raw reply [nested|flat] 37+ messages in thread
* Re: COMMENTS are not being copied in CREATE TABLE LIKE
2026-02-12 10:11 COMMENTS are not being copied in CREATE TABLE LIKE Jim Jones <jim.jones@uni-muenster.de>
2026-02-12 14:36 ` Re: COMMENTS are not being copied in CREATE TABLE LIKE Matheus Alcantara <matheusssilv97@gmail.com>
2026-02-12 15:08 ` Re: COMMENTS are not being copied in CREATE TABLE LIKE Fujii Masao <masao.fujii@gmail.com>
2026-02-12 15:33 ` Re: COMMENTS are not being copied in CREATE TABLE LIKE Jim Jones <jim.jones@uni-muenster.de>
@ 2026-02-13 01:29 ` Chao Li <li.evan.chao@gmail.com>
2026-02-13 04:30 ` Re: COMMENTS are not being copied in CREATE TABLE LIKE Tom Lane <tgl@sss.pgh.pa.us>
2 siblings, 1 reply; 37+ messages in thread
From: Chao Li @ 2026-02-13 01:29 UTC (permalink / raw)
To: Jim Jones <jim.jones@uni-muenster.de>; +Cc: Fujii Masao <masao.fujii@gmail.com>; Matheus Alcantara <matheusssilv97@gmail.com>; pgsql-hackers@lists.postgresql.org <pgsql-hackers@lists.postgresql.org>
> On Feb 12, 2026, at 23:33, Jim Jones <jim.jones@uni-muenster.de> wrote:
>
>
>
> On 12/02/2026 16:08, Fujii Masao wrote:
>> On Thu, Feb 12, 2026 at 11:36 PM Matheus Alcantara
>> <matheusssilv97@gmail.com> wrote:
>>> The patch fix the issue and it seems correct to me.
>
> Thanks for the review!
>
>> The documentation [1] states that INCLUDING COMMENTS copies comments for
>> the copied columns, constraints, and indexes. It does not mention copying
>> comments on the table itself. Therefore, not copying table comments with
>> INCLUDING COMMENTS does not appear to be a bug. That is, the proposed patch
>> seems more like an improvement than a bug fix.
>
> Hmm, it seemed so obvious to me that I didn’t look more closely at the
> documentation :) Although I struggle to see the rationale for not
> copying a table’s comment in a CREATE TABLE LIKE ... INCLUDING COMMENTS
> statement, I have to agree that the documentation is quite clear about
> this limitation, and therefore it cannot really be considered a bug.
> That said, it may well be worth considering for a future release.
>
> What are your thoughts?
>
> Thanks!
>
> Best, Jim
>
>
I feel the current behavior is proper. When you create a table using LIKE, you are cloning the structure, not the identity. Just as you have to pick a new name for the table, you should provide a new comment that fits its specific purpose. If a comment is needed, the COMMENT ON command is already the straightforward way to handle that.
Best regards,
--
Chao Li (Evan)
HighGo Software Co., Ltd.
https://www.highgo.com/
^ permalink raw reply [nested|flat] 37+ messages in thread
* Re: COMMENTS are not being copied in CREATE TABLE LIKE
2026-02-12 10:11 COMMENTS are not being copied in CREATE TABLE LIKE Jim Jones <jim.jones@uni-muenster.de>
2026-02-12 14:36 ` Re: COMMENTS are not being copied in CREATE TABLE LIKE Matheus Alcantara <matheusssilv97@gmail.com>
2026-02-12 15:08 ` Re: COMMENTS are not being copied in CREATE TABLE LIKE Fujii Masao <masao.fujii@gmail.com>
2026-02-12 15:33 ` Re: COMMENTS are not being copied in CREATE TABLE LIKE Jim Jones <jim.jones@uni-muenster.de>
2026-02-13 01:29 ` Re: COMMENTS are not being copied in CREATE TABLE LIKE Chao Li <li.evan.chao@gmail.com>
@ 2026-02-13 04:30 ` Tom Lane <tgl@sss.pgh.pa.us>
2026-02-13 09:19 ` Re: COMMENTS are not being copied in CREATE TABLE LIKE Jim Jones <jim.jones@uni-muenster.de>
0 siblings, 1 reply; 37+ messages in thread
From: Tom Lane @ 2026-02-13 04:30 UTC (permalink / raw)
To: Chao Li <li.evan.chao@gmail.com>; +Cc: Jim Jones <jim.jones@uni-muenster.de>; Fujii Masao <masao.fujii@gmail.com>; Matheus Alcantara <matheusssilv97@gmail.com>; pgsql-hackers@lists.postgresql.org <pgsql-hackers@lists.postgresql.org>
Chao Li <li.evan.chao@gmail.com> writes:
> On Feb 12, 2026, at 23:33, Jim Jones <jim.jones@uni-muenster.de> wrote:
>> On 12/02/2026 16:08, Fujii Masao wrote:
>>> The documentation [1] states that INCLUDING COMMENTS copies comments for
>>> the copied columns, constraints, and indexes. It does not mention copying
>>> comments on the table itself. Therefore, not copying table comments with
>>> INCLUDING COMMENTS does not appear to be a bug. That is, the proposed patch
>>> seems more like an improvement than a bug fix.
>> Hmm, it seemed so obvious to me that I didn’t look more closely at the
>> documentation :) Although I struggle to see the rationale for not
>> copying a table’s comment in a CREATE TABLE LIKE ... INCLUDING COMMENTS
>> statement, I have to agree that the documentation is quite clear about
>> this limitation, and therefore it cannot really be considered a bug.
>> That said, it may well be worth considering for a future release.
> I feel the current behavior is proper. When you create a table using
> LIKE, you are cloning the structure, not the identity.
Yeah, I was about to make a similar comment. We do not for example
clone the ownership or permissions of the source table. Maybe there
is an argument for cloning the table-level comment but it's by no
means open-and-shut. So I think the current behavior is intentional
not an oversight. Might be good to go find the thread in which the
INCLUDING COMMENTS functionality was developed and see if there was
discussion.
(Speaking of which, I'm feeling very dubious about the nearby
proposal to invent LIKE INCLUDING POLICIES. It's hard to credit
that it makes sense to clone RLS policies while not cloning
table ownership and permissions. But I suppose I should raise
that point in that thread.)
regards, tom lane
^ permalink raw reply [nested|flat] 37+ messages in thread
* Re: COMMENTS are not being copied in CREATE TABLE LIKE
2026-02-12 10:11 COMMENTS are not being copied in CREATE TABLE LIKE Jim Jones <jim.jones@uni-muenster.de>
2026-02-12 14:36 ` Re: COMMENTS are not being copied in CREATE TABLE LIKE Matheus Alcantara <matheusssilv97@gmail.com>
2026-02-12 15:08 ` Re: COMMENTS are not being copied in CREATE TABLE LIKE Fujii Masao <masao.fujii@gmail.com>
2026-02-12 15:33 ` Re: COMMENTS are not being copied in CREATE TABLE LIKE Jim Jones <jim.jones@uni-muenster.de>
2026-02-13 01:29 ` Re: COMMENTS are not being copied in CREATE TABLE LIKE Chao Li <li.evan.chao@gmail.com>
2026-02-13 04:30 ` Re: COMMENTS are not being copied in CREATE TABLE LIKE Tom Lane <tgl@sss.pgh.pa.us>
@ 2026-02-13 09:19 ` Jim Jones <jim.jones@uni-muenster.de>
2026-02-22 08:05 ` Re: COMMENTS are not being copied in CREATE TABLE LIKE Hüseyin Demir <huseyin.d3r@gmail.com>
2026-02-26 13:39 ` Re: COMMENTS are not being copied in CREATE TABLE LIKE Jim Jones <jim.jones@uni-muenster.de>
0 siblings, 2 replies; 37+ messages in thread
From: Jim Jones @ 2026-02-13 09:19 UTC (permalink / raw)
To: Tom Lane <tgl@sss.pgh.pa.us>; Chao Li <li.evan.chao@gmail.com>; +Cc: Fujii Masao <masao.fujii@gmail.com>; Matheus Alcantara <matheusssilv97@gmail.com>; pgsql-hackers@lists.postgresql.org <pgsql-hackers@lists.postgresql.org>; David G. Johnston <david.g.johnston@gmail.com>
Thanks everyone for the comments!
On 13/02/2026 05:30, Tom Lane wrote:
> Chao Li <li.evan.chao@gmail.com> writes:
>> I feel the current behavior is proper. When you create a table using
>> LIKE, you are cloning the structure, not the identity.
The concern regarding identity is certainly a valid one, but in this
case I do not see how it applies. Copying the comment would not, IMHO,
transfer the identity of the source table (in a semantic sense), but
would instead merely indicate its provenance.
> Yeah, I was about to make a similar comment. We do not for example
> clone the ownership or permissions of the source table. Maybe there
> is an argument for cloning the table-level comment but it's by no
> means open-and-shut. So I think the current behavior is intentional
> not an oversight. Might be good to go find the thread in which the
> INCLUDING COMMENTS functionality was developed and see if there was
> discussion.
I did a bit of digging in the mailing list and found this old thread[1]
where INCLUDING COMMENTS was introduced. I couldn't see anything related
to table-level comments there. Perhaps it was discussed elsewhere?
On 13/02/2026 05:13, Fujii Masao wrote:
> For example, if two source tables each have a table comment and both are
> specified in LIKE, which comment should be applied to the new table?
On 13/02/2026 05:22, David G. Johnston wrote:
> Both, with a new line between them.
I supposed we could, as David mentioned, simply concatenate them. How it
should be done can be discussed, but a \n (or two) would IMO work just fine.
Example:
CREATE TABLE t1 (a int);
COMMENT ON TABLE t1 IS 'comment from table 1';
CREATE TABLE t2 (b int);
COMMENT ON TABLE t2 IS 'comment from table 2';
CREATE TABLE t3 (c int);
COMMENT ON TABLE t3 IS 'comment from table 3';
CREATE TABLE tm (
LIKE t1 INCLUDING COMMENTS,
LIKE t3 INCLUDING COMMENTS,
LIKE t2 INCLUDING COMMENTS
);
SELECT obj_description('tm'::regclass, 'pg_class') AS table_comment;
table_comment
----------------------
comment from table 1+
comment from table 3+
comment from table 2
(1 row)
Any thoughts on that?
Best, Jim
1 -
https://www.postgresql.org/message-id/flat/20090907114058.C855.52131E4D%40oss.ntt.co.jp
Attachments:
v2-0001-Add-table-comments-in-CREATE-TABLE-LIKE-INCLUDING.patch (text/x-patch, 9.5K) download
From f49b512337b261c2824a4d495af68d39231c367e Mon Sep 17 00:00:00 2001
From: Jim Jones <jim.jones@uni-muenster.de>
Date: Fri, 13 Feb 2026 09:30:31 +0100
Subject: [PATCH v2] Add table comments in CREATE TABLE LIKE INCLUDING COMMENTS
When using CREATE TABLE ... LIKE ... INCLUDING COMMENTS (or INCLUDING
ALL), table-level comments were not being copied to the new table, even
though column comments, constraint comments, index comments, and
statistics comments were properly copied. This patch extends the
feature to also copy the table's own comment to the target table.
When multiple LIKE clauses specify INCLUDING COMMENTS and the source
tables have table-level comments, the comments are now concatenated in
the target table, separated by newlines, in the order that the LIKE
clauses appear. This allows users to preserve comment information from
all source tables when creating tables that combine properties from
multiple sources.
---
doc/src/sgml/ref/create_table.sgml | 15 +++--
src/backend/parser/parse_utilcmd.c | 59 +++++++++++++++++--
.../regress/expected/create_table_like.out | 30 ++++++++++
src/test/regress/sql/create_table_like.sql | 21 +++++++
4 files changed, 115 insertions(+), 10 deletions(-)
diff --git a/doc/src/sgml/ref/create_table.sgml b/doc/src/sgml/ref/create_table.sgml
index 77c5a763d4..223f1eac5c 100644
--- a/doc/src/sgml/ref/create_table.sgml
+++ b/doc/src/sgml/ref/create_table.sgml
@@ -672,10 +672,17 @@ WITH ( MODULUS <replaceable class="parameter">numeric_literal</replaceable>, REM
<term><literal>INCLUDING COMMENTS</literal></term>
<listitem>
<para>
- Comments for the copied columns, constraints, and indexes will be
- copied. The default behavior is to exclude comments, resulting in
- the copied columns and constraints in the new table having no
- comments.
+ Comments for the table itself and for the copied columns,
+ constraints, and indexes will be copied. The default behavior is to
+ exclude comments, resulting in the new table and its copied columns
+ and constraints having no comments.
+ </para>
+ <para>
+ If multiple <literal>LIKE</literal> clauses specify
+ <literal>INCLUDING COMMENTS</literal> and the source tables have
+ table-level comments, the comments will be concatenated in the new
+ table, separated by newlines, in the order that the
+ <literal>LIKE</literal> clauses appear.
</para>
</listitem>
</varlistentry>
diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c
index b5f4c72459..2c247312b9 100644
--- a/src/backend/parser/parse_utilcmd.c
+++ b/src/backend/parser/parse_utilcmd.c
@@ -43,6 +43,7 @@
#include "commands/comment.h"
#include "commands/defrem.h"
#include "commands/sequence.h"
+#include "lib/stringinfo.h"
#include "commands/tablecmds.h"
#include "commands/tablespace.h"
#include "miscadmin.h"
@@ -1307,17 +1308,18 @@ transformTableLikeClause(CreateStmtContext *cxt, TableLikeClause *table_like_cla
}
/*
- * We cannot yet deal with defaults, CHECK constraints, indexes, or
- * statistics, since we don't yet know what column numbers the copied
- * columns will have in the finished table. If any of those options are
- * specified, add the LIKE clause to cxt->likeclauses so that
- * expandTableLikeClause will be called after we do know that.
+ * We cannot yet deal with defaults, CHECK constraints, indexes,
+ * statistics, or table comments, since we don't yet know what column
+ * numbers the copied columns will have in the finished table. If any of
+ * those options are specified, add the LIKE clause to cxt->likeclauses
+ * so that expandTableLikeClause will be called after we do know that.
*
* In order for this to work, we remember the relation OID so that
* expandTableLikeClause is certain to open the same table.
*/
if (table_like_clause->options &
- (CREATE_TABLE_LIKE_DEFAULTS |
+ (CREATE_TABLE_LIKE_COMMENTS |
+ CREATE_TABLE_LIKE_DEFAULTS |
CREATE_TABLE_LIKE_GENERATED |
CREATE_TABLE_LIKE_CONSTRAINTS |
CREATE_TABLE_LIKE_INDEXES |
@@ -1625,6 +1627,51 @@ expandTableLikeClause(RangeVar *heapRel, TableLikeClause *table_like_clause)
list_free(parent_extstats);
}
+ /*
+ * Copy comment on the relation itself, if requested.
+ */
+ if (table_like_clause->options & CREATE_TABLE_LIKE_COMMENTS)
+ {
+ comment = GetComment(RelationGetRelid(relation), RelationRelationId, 0);
+
+ if (comment != NULL)
+ {
+ CommentStmt *stmt;
+ char *existing_comment;
+
+ /*
+ * Check if the target table already has a comment from a previous
+ * LIKE clause. If so, append this comment to it with a newline
+ * separator.
+ */
+ existing_comment = GetComment(RelationGetRelid(childrel), RelationRelationId, 0);
+
+ stmt = makeNode(CommentStmt);
+ stmt->objtype = OBJECT_TABLE;
+ if (heapRel->schemaname)
+ stmt->object = (Node *)list_make2(makeString(heapRel->schemaname),
+ makeString(heapRel->relname));
+ else
+ stmt->object = (Node *)list_make1(makeString(heapRel->relname));
+
+ /* Combine comments if there was a previous one */
+ if (existing_comment != NULL)
+ {
+ StringInfoData buf;
+
+ initStringInfo(&buf);
+ appendStringInfoString(&buf, existing_comment);
+ appendStringInfoChar(&buf, '\n');
+ appendStringInfoString(&buf, comment);
+ stmt->comment = buf.data;
+ }
+ else
+ stmt->comment = comment;
+
+ result = lappend(result, stmt);
+ }
+ }
+
/* Done with child rel */
table_close(childrel, NoLock);
diff --git a/src/test/regress/expected/create_table_like.out b/src/test/regress/expected/create_table_like.out
index d3c35c1484..e62ccd1ad5 100644
--- a/src/test/regress/expected/create_table_like.out
+++ b/src/test/regress/expected/create_table_like.out
@@ -341,6 +341,7 @@ CREATE TABLE ctlt3 (a text CHECK (length(a) < 5), c text CHECK (length(c) < 7));
ALTER TABLE ctlt3 ALTER COLUMN c SET STORAGE EXTERNAL;
ALTER TABLE ctlt3 ALTER COLUMN a SET STORAGE MAIN;
CREATE INDEX ctlt3_fnidx ON ctlt3 ((a || c));
+COMMENT ON TABLE ctlt3 IS 'ctlt3 table comment';
COMMENT ON COLUMN ctlt3.a IS 'A3';
COMMENT ON COLUMN ctlt3.c IS 'C';
COMMENT ON CONSTRAINT ctlt3_a_check ON ctlt3 IS 't3_a_check';
@@ -449,6 +450,35 @@ SELECT description FROM pg_description, pg_constraint c WHERE classoid = 'pg_con
t3_a_check
(1 row)
+SELECT obj_description('ctlt13_like'::regclass, 'pg_class') AS table_comment;
+ table_comment
+---------------------
+ ctlt3 table comment
+(1 row)
+
+-- Test multiple LIKE clauses with table comments
+CREATE TABLE ctlt_comment1 (a int);
+COMMENT ON TABLE ctlt_comment1 IS 'table 1 comment';
+CREATE TABLE ctlt_comment2 (b int);
+COMMENT ON TABLE ctlt_comment2 IS 'table 2 comment';
+CREATE TABLE ctlt_comment3 (c int);
+COMMENT ON TABLE ctlt_comment3 IS 'table 3 comment';
+-- Multiple LIKE clauses should concatenate table comments
+CREATE TABLE ctlt_multi_comments (
+ LIKE ctlt_comment1 INCLUDING COMMENTS,
+ LIKE ctlt_comment3 INCLUDING COMMENTS,
+ LIKE ctlt_comment2 INCLUDING COMMENTS
+);
+-- The order of comments should be the same as the order of LIKE clauses
+SELECT obj_description('ctlt_multi_comments'::regclass, 'pg_class') AS table_comment;
+ table_comment
+-----------------
+ table 1 comment+
+ table 3 comment+
+ table 2 comment
+(1 row)
+
+DROP TABLE ctlt_comment1, ctlt_comment2, ctlt_comment3, ctlt_multi_comments;
CREATE TABLE ctlt_all (LIKE ctlt1 INCLUDING ALL);
\d+ ctlt_all
Table "public.ctlt_all"
diff --git a/src/test/regress/sql/create_table_like.sql b/src/test/regress/sql/create_table_like.sql
index 93389b57db..903c51d062 100644
--- a/src/test/regress/sql/create_table_like.sql
+++ b/src/test/regress/sql/create_table_like.sql
@@ -153,6 +153,7 @@ CREATE TABLE ctlt3 (a text CHECK (length(a) < 5), c text CHECK (length(c) < 7));
ALTER TABLE ctlt3 ALTER COLUMN c SET STORAGE EXTERNAL;
ALTER TABLE ctlt3 ALTER COLUMN a SET STORAGE MAIN;
CREATE INDEX ctlt3_fnidx ON ctlt3 ((a || c));
+COMMENT ON TABLE ctlt3 IS 'ctlt3 table comment';
COMMENT ON COLUMN ctlt3.a IS 'A3';
COMMENT ON COLUMN ctlt3.c IS 'C';
COMMENT ON CONSTRAINT ctlt3_a_check ON ctlt3 IS 't3_a_check';
@@ -173,6 +174,26 @@ CREATE TABLE ctlt13_inh () INHERITS (ctlt1, ctlt3);
CREATE TABLE ctlt13_like (LIKE ctlt3 INCLUDING CONSTRAINTS INCLUDING INDEXES INCLUDING COMMENTS INCLUDING STORAGE) INHERITS (ctlt1);
\d+ ctlt13_like
SELECT description FROM pg_description, pg_constraint c WHERE classoid = 'pg_constraint'::regclass AND objoid = c.oid AND c.conrelid = 'ctlt13_like'::regclass;
+SELECT obj_description('ctlt13_like'::regclass, 'pg_class') AS table_comment;
+
+-- Test multiple LIKE clauses with table comments
+CREATE TABLE ctlt_comment1 (a int);
+COMMENT ON TABLE ctlt_comment1 IS 'table 1 comment';
+CREATE TABLE ctlt_comment2 (b int);
+COMMENT ON TABLE ctlt_comment2 IS 'table 2 comment';
+CREATE TABLE ctlt_comment3 (c int);
+COMMENT ON TABLE ctlt_comment3 IS 'table 3 comment';
+
+
+-- Multiple LIKE clauses should concatenate table comments
+CREATE TABLE ctlt_multi_comments (
+ LIKE ctlt_comment1 INCLUDING COMMENTS,
+ LIKE ctlt_comment3 INCLUDING COMMENTS,
+ LIKE ctlt_comment2 INCLUDING COMMENTS
+);
+-- The order of comments should be the same as the order of LIKE clauses
+SELECT obj_description('ctlt_multi_comments'::regclass, 'pg_class') AS table_comment;
+DROP TABLE ctlt_comment1, ctlt_comment2, ctlt_comment3, ctlt_multi_comments;
CREATE TABLE ctlt_all (LIKE ctlt1 INCLUDING ALL);
\d+ ctlt_all
--
2.43.0
^ permalink raw reply [nested|flat] 37+ messages in thread
* Re: COMMENTS are not being copied in CREATE TABLE LIKE
2026-02-12 10:11 COMMENTS are not being copied in CREATE TABLE LIKE Jim Jones <jim.jones@uni-muenster.de>
2026-02-12 14:36 ` Re: COMMENTS are not being copied in CREATE TABLE LIKE Matheus Alcantara <matheusssilv97@gmail.com>
2026-02-12 15:08 ` Re: COMMENTS are not being copied in CREATE TABLE LIKE Fujii Masao <masao.fujii@gmail.com>
2026-02-12 15:33 ` Re: COMMENTS are not being copied in CREATE TABLE LIKE Jim Jones <jim.jones@uni-muenster.de>
2026-02-13 01:29 ` Re: COMMENTS are not being copied in CREATE TABLE LIKE Chao Li <li.evan.chao@gmail.com>
2026-02-13 04:30 ` Re: COMMENTS are not being copied in CREATE TABLE LIKE Tom Lane <tgl@sss.pgh.pa.us>
2026-02-13 09:19 ` Re: COMMENTS are not being copied in CREATE TABLE LIKE Jim Jones <jim.jones@uni-muenster.de>
@ 2026-02-22 08:05 ` Hüseyin Demir <huseyin.d3r@gmail.com>
2026-02-22 12:34 ` Re: COMMENTS are not being copied in CREATE TABLE LIKE Jim Jones <jim.jones@uni-muenster.de>
1 sibling, 1 reply; 37+ messages in thread
From: Hüseyin Demir @ 2026-02-22 08:05 UTC (permalink / raw)
To: pgsql-hackers@lists.postgresql.org; +Cc: Jim Jones <jim.jones@uni-muenster.de>
Hi Jim,
I have a couple of concerns about the patch and I wanted to highlight the following cases.
1. Table comments tend to describe the purpose or context of a specific table (e.g. "staging table for pipeline X"), unlike column or constraint comments which describe the schema structure. Copying them by default may be wrong more often than it's right, since the new table almost certainly serves a different purpose than the source.
2. This changes the behavior of INCLUDING ALL, which many users rely on without thinking too carefully about what it pulls in. Silently copying a source table's comment (which might say something like "template — do not use directly") into every derived table could cause confusion in practice.
Neither of these is necessarily a blocker, but I think they're worth discussing before we commit to this as the default behavior under INCLUDING COMMENTS.
Before reviewing the patch for code quality and repo standards, I think we need to decide whether this behavior change is the right approach at all. My preference would be to keep table comments managed separately, given the situations above.
^ permalink raw reply [nested|flat] 37+ messages in thread
* Re: COMMENTS are not being copied in CREATE TABLE LIKE
2026-02-12 10:11 COMMENTS are not being copied in CREATE TABLE LIKE Jim Jones <jim.jones@uni-muenster.de>
2026-02-12 14:36 ` Re: COMMENTS are not being copied in CREATE TABLE LIKE Matheus Alcantara <matheusssilv97@gmail.com>
2026-02-12 15:08 ` Re: COMMENTS are not being copied in CREATE TABLE LIKE Fujii Masao <masao.fujii@gmail.com>
2026-02-12 15:33 ` Re: COMMENTS are not being copied in CREATE TABLE LIKE Jim Jones <jim.jones@uni-muenster.de>
2026-02-13 01:29 ` Re: COMMENTS are not being copied in CREATE TABLE LIKE Chao Li <li.evan.chao@gmail.com>
2026-02-13 04:30 ` Re: COMMENTS are not being copied in CREATE TABLE LIKE Tom Lane <tgl@sss.pgh.pa.us>
2026-02-13 09:19 ` Re: COMMENTS are not being copied in CREATE TABLE LIKE Jim Jones <jim.jones@uni-muenster.de>
2026-02-22 08:05 ` Re: COMMENTS are not being copied in CREATE TABLE LIKE Hüseyin Demir <huseyin.d3r@gmail.com>
@ 2026-02-22 12:34 ` Jim Jones <jim.jones@uni-muenster.de>
2026-02-26 07:46 ` Re: COMMENTS are not being copied in CREATE TABLE LIKE Hüseyin Demir <huseyin.d3r@gmail.com>
0 siblings, 1 reply; 37+ messages in thread
From: Jim Jones @ 2026-02-22 12:34 UTC (permalink / raw)
To: Hüseyin Demir <huseyin.d3r@gmail.com>; pgsql-hackers@lists.postgresql.org
Hi Hüseyin
On 22/02/2026 09:05, Hüseyin Demir wrote:
> 1. Table comments tend to describe the purpose or context of a specific table (e.g. "staging table for pipeline X"), unlike column or constraint comments which describe the schema structure. Copying them by default may be wrong more often than it's right, since the new table almost certainly serves a different purpose than the source.
Good point. Comments may well lose their semantic value when placed in a
different context, but I'm not sure how it differs from column comments.
A column comment can also refer to a context (original table) that is no
longer applicable after cloning, specially when the CREATE TABLE LIKE
includes multiple tables.
> 2. This changes the behavior of INCLUDING ALL, which many users rely on without thinking too carefully about what it pulls in. Silently copying a source table's comment (which might say something like "template — do not use directly") into every derived table could cause confusion in practice.
It's also a valid concern - although I see it slightly differently. I we
take this line of reasoning too seriously, we might never be able to
expand CREATE TABLE LIKE, since the ALL keyword would be directly
affected (expanded) in the process. There are also other patches that
aim to expand CREATE TABLE LIKE, e.g. INCLUDING TRIGGERS[1]
> Before reviewing the patch for code quality and repo standards, I think we need to decide whether this behavior change is the right approach at all. My preference would be to keep table comments managed separately, given the situations above.
Are you suggesting we should simply keep ignoring the table comments? Or
should we manage them differently?
Thanks for the comments. Much appreciated!
Best, Jim
1 - https://commitfest.postgresql.org/patch/6087/
^ permalink raw reply [nested|flat] 37+ messages in thread
* Re: COMMENTS are not being copied in CREATE TABLE LIKE
2026-02-12 10:11 COMMENTS are not being copied in CREATE TABLE LIKE Jim Jones <jim.jones@uni-muenster.de>
2026-02-12 14:36 ` Re: COMMENTS are not being copied in CREATE TABLE LIKE Matheus Alcantara <matheusssilv97@gmail.com>
2026-02-12 15:08 ` Re: COMMENTS are not being copied in CREATE TABLE LIKE Fujii Masao <masao.fujii@gmail.com>
2026-02-12 15:33 ` Re: COMMENTS are not being copied in CREATE TABLE LIKE Jim Jones <jim.jones@uni-muenster.de>
2026-02-13 01:29 ` Re: COMMENTS are not being copied in CREATE TABLE LIKE Chao Li <li.evan.chao@gmail.com>
2026-02-13 04:30 ` Re: COMMENTS are not being copied in CREATE TABLE LIKE Tom Lane <tgl@sss.pgh.pa.us>
2026-02-13 09:19 ` Re: COMMENTS are not being copied in CREATE TABLE LIKE Jim Jones <jim.jones@uni-muenster.de>
2026-02-22 08:05 ` Re: COMMENTS are not being copied in CREATE TABLE LIKE Hüseyin Demir <huseyin.d3r@gmail.com>
2026-02-22 12:34 ` Re: COMMENTS are not being copied in CREATE TABLE LIKE Jim Jones <jim.jones@uni-muenster.de>
@ 2026-02-26 07:46 ` Hüseyin Demir <huseyin.d3r@gmail.com>
2026-02-26 10:07 ` Re: COMMENTS are not being copied in CREATE TABLE LIKE Jim Jones <jim.jones@uni-muenster.de>
0 siblings, 1 reply; 37+ messages in thread
From: Hüseyin Demir @ 2026-02-26 07:46 UTC (permalink / raw)
To: Jim Jones <jim.jones@uni-muenster.de>; +Cc: pgsql-hackers@lists.postgresql.org
Hi Jim,
> Are you suggesting we should simply keep ignoring the table comments? Or
> should we manage them differently?
Yes, because changing the behavior will lead to misinterpretations while
creating tables and tables derived from template ones will be presented
differently with respect to comments they have.
Such changes can cause unexpected different objects inside a database.
For example, any existing script using INCLUDING COMMENTS or INCLUDING ALL
will now copy the table comment where it didn't before.
If we need this behavioral change my idea is to introduce a new sub-option
or keep table comments excluded unless explicitly opted in. Because after
changing the current behavior there is no way to implement old behavior.
Regards.
Jim Jones <jim.jones@uni-muenster.de>, 22 Şub 2026 Paz, 13:34 tarihinde
şunu yazdı:
> Hi Hüseyin
>
> On 22/02/2026 09:05, Hüseyin Demir wrote:
> > 1. Table comments tend to describe the purpose or context of a specific
> table (e.g. "staging table for pipeline X"), unlike column or constraint
> comments which describe the schema structure. Copying them by default may
> be wrong more often than it's right, since the new table almost certainly
> serves a different purpose than the source.
>
> Good point. Comments may well lose their semantic value when placed in a
> different context, but I'm not sure how it differs from column comments.
> A column comment can also refer to a context (original table) that is no
> longer applicable after cloning, specially when the CREATE TABLE LIKE
> includes multiple tables.
>
>
> > 2. This changes the behavior of INCLUDING ALL, which many users rely on
> without thinking too carefully about what it pulls in. Silently copying a
> source table's comment (which might say something like "template — do not
> use directly") into every derived table could cause confusion in practice.
>
>
> It's also a valid concern - although I see it slightly differently. I we
> take this line of reasoning too seriously, we might never be able to
> expand CREATE TABLE LIKE, since the ALL keyword would be directly
> affected (expanded) in the process. There are also other patches that
> aim to expand CREATE TABLE LIKE, e.g. INCLUDING TRIGGERS[1]
>
>
> > Before reviewing the patch for code quality and repo standards, I think
> we need to decide whether this behavior change is the right approach at
> all. My preference would be to keep table comments managed separately,
> given the situations above.
>
>
> Are you suggesting we should simply keep ignoring the table comments? Or
> should we manage them differently?
>
> Thanks for the comments. Much appreciated!
>
> Best, Jim
>
> 1 - https://commitfest.postgresql.org/patch/6087/
>
^ permalink raw reply [nested|flat] 37+ messages in thread
* Re: COMMENTS are not being copied in CREATE TABLE LIKE
2026-02-12 10:11 COMMENTS are not being copied in CREATE TABLE LIKE Jim Jones <jim.jones@uni-muenster.de>
2026-02-12 14:36 ` Re: COMMENTS are not being copied in CREATE TABLE LIKE Matheus Alcantara <matheusssilv97@gmail.com>
2026-02-12 15:08 ` Re: COMMENTS are not being copied in CREATE TABLE LIKE Fujii Masao <masao.fujii@gmail.com>
2026-02-12 15:33 ` Re: COMMENTS are not being copied in CREATE TABLE LIKE Jim Jones <jim.jones@uni-muenster.de>
2026-02-13 01:29 ` Re: COMMENTS are not being copied in CREATE TABLE LIKE Chao Li <li.evan.chao@gmail.com>
2026-02-13 04:30 ` Re: COMMENTS are not being copied in CREATE TABLE LIKE Tom Lane <tgl@sss.pgh.pa.us>
2026-02-13 09:19 ` Re: COMMENTS are not being copied in CREATE TABLE LIKE Jim Jones <jim.jones@uni-muenster.de>
2026-02-22 08:05 ` Re: COMMENTS are not being copied in CREATE TABLE LIKE Hüseyin Demir <huseyin.d3r@gmail.com>
2026-02-22 12:34 ` Re: COMMENTS are not being copied in CREATE TABLE LIKE Jim Jones <jim.jones@uni-muenster.de>
2026-02-26 07:46 ` Re: COMMENTS are not being copied in CREATE TABLE LIKE Hüseyin Demir <huseyin.d3r@gmail.com>
@ 2026-02-26 10:07 ` Jim Jones <jim.jones@uni-muenster.de>
0 siblings, 0 replies; 37+ messages in thread
From: Jim Jones @ 2026-02-26 10:07 UTC (permalink / raw)
To: Hüseyin Demir <huseyin.d3r@gmail.com>; +Cc: pgsql-hackers@lists.postgresql.org
On 26/02/2026 08:46, Hüseyin Demir wrote:
> Yes, because changing the behavior will lead to misinterpretations while
> creating tables and tables derived from template ones will be presented
> differently with respect to comments they have.
But isn't that the case to any feature that we add to CREATE TABLE LIKE?
For instance, if we add INCLUDING TRIGGERS, it will inevitably change
the behaviour of INCLUDING ALL.
> Such changes can cause unexpected different objects inside a database.
> For example, any existing script using INCLUDING COMMENTS or INCLUDING
> ALL will now copy the table comment where it didn't before.
That's true, the newly created table would then get a comment it didn't
have before. I just fail to see how this would create an unexpected
different object.
Of course, if this script relies on the table not having a comment to
work, it could case some problems.
> If we need this behavioral change my idea is to introduce a new sub-
> option or keep table comments excluded unless explicitly opted in.
> Because after changing the current behavior there is no way to implement
> old behavior.
The user can always use EXCLUDING COMMENTS for that
CREATE TABLE t2 (
LIKE t1
INCLUDING ALL
EXCLUDING COMMENTS
);
Thanks, Hüseyin!
Best, Jim
^ permalink raw reply [nested|flat] 37+ messages in thread
* Re: COMMENTS are not being copied in CREATE TABLE LIKE
2026-02-12 10:11 COMMENTS are not being copied in CREATE TABLE LIKE Jim Jones <jim.jones@uni-muenster.de>
2026-02-12 14:36 ` Re: COMMENTS are not being copied in CREATE TABLE LIKE Matheus Alcantara <matheusssilv97@gmail.com>
2026-02-12 15:08 ` Re: COMMENTS are not being copied in CREATE TABLE LIKE Fujii Masao <masao.fujii@gmail.com>
2026-02-12 15:33 ` Re: COMMENTS are not being copied in CREATE TABLE LIKE Jim Jones <jim.jones@uni-muenster.de>
2026-02-13 01:29 ` Re: COMMENTS are not being copied in CREATE TABLE LIKE Chao Li <li.evan.chao@gmail.com>
2026-02-13 04:30 ` Re: COMMENTS are not being copied in CREATE TABLE LIKE Tom Lane <tgl@sss.pgh.pa.us>
2026-02-13 09:19 ` Re: COMMENTS are not being copied in CREATE TABLE LIKE Jim Jones <jim.jones@uni-muenster.de>
@ 2026-02-26 13:39 ` Jim Jones <jim.jones@uni-muenster.de>
2026-03-24 18:32 ` Re: COMMENTS are not being copied in CREATE TABLE LIKE Carlos Alves <carlos@alves.inf.br>
1 sibling, 1 reply; 37+ messages in thread
From: Jim Jones @ 2026-02-26 13:39 UTC (permalink / raw)
To: Tom Lane <tgl@sss.pgh.pa.us>; Chao Li <li.evan.chao@gmail.com>; +Cc: Fujii Masao <masao.fujii@gmail.com>; Matheus Alcantara <matheusssilv97@gmail.com>; pgsql-hackers@lists.postgresql.org <pgsql-hackers@lists.postgresql.org>; David G. Johnston <david.g.johnston@gmail.com>
On 13/02/2026 10:19, Jim Jones wrote:
> ...
> Example:
>
> CREATE TABLE t1 (a int);
> COMMENT ON TABLE t1 IS 'comment from table 1';
> CREATE TABLE t2 (b int);
> COMMENT ON TABLE t2 IS 'comment from table 2';
> CREATE TABLE t3 (c int);
> COMMENT ON TABLE t3 IS 'comment from table 3';
>
> CREATE TABLE tm (
> LIKE t1 INCLUDING COMMENTS,
> LIKE t3 INCLUDING COMMENTS,
> LIKE t2 INCLUDING COMMENTS
> );
>
> SELECT obj_description('tm'::regclass, 'pg_class') AS table_comment;
> table_comment
> ----------------------
> comment from table 1+
> comment from table 3+
> comment from table 2
> (1 row)
>
>
> Any thoughts on that?
Rebase
Best, Jim
Attachments:
v3-0001-Add-table-comments-in-CREATE-TABLE-LIKE-INCLUDING.patch (text/x-patch, 9.6K) download
From d61ded2485c69293b21c9b2372363e82b7adbe88 Mon Sep 17 00:00:00 2001
From: Jim Jones <jim.jones@uni-muenster.de>
Date: Thu, 26 Feb 2026 14:11:43 +0100
Subject: [PATCH v3] Add table comments in CREATE TABLE LIKE INCLUDING COMMENTS
When using CREATE TABLE ... LIKE ... INCLUDING COMMENTS (or INCLUDING
ALL), table-level comments were not being copied to the new table, even
though column comments, constraint comments, index comments, and
statistics comments were properly copied. This patch extends the
feature to also copy the table's own comment to the target table.
When multiple LIKE clauses specify INCLUDING COMMENTS and the source
tables have table-level comments, the comments are now concatenated in
the target table, separated by newlines, in the order that the LIKE
clauses appear. This allows users to preserve comment information from
all source tables when creating tables that combine properties from
multiple sources.
---
doc/src/sgml/ref/create_table.sgml | 17 ++++--
src/backend/parser/parse_utilcmd.c | 59 +++++++++++++++++--
.../regress/expected/create_table_like.out | 30 ++++++++++
src/test/regress/sql/create_table_like.sql | 21 +++++++
4 files changed, 116 insertions(+), 11 deletions(-)
diff --git a/doc/src/sgml/ref/create_table.sgml b/doc/src/sgml/ref/create_table.sgml
index 982532fe72..483a1b4736 100644
--- a/doc/src/sgml/ref/create_table.sgml
+++ b/doc/src/sgml/ref/create_table.sgml
@@ -672,11 +672,18 @@ WITH ( MODULUS <replaceable class="parameter">numeric_literal</replaceable>, REM
<term><literal>INCLUDING COMMENTS</literal></term>
<listitem>
<para>
- Comments for the copied columns, check constraints,
- not-null constraints, indexes, and extended statistics will be
- copied. The default behavior is to exclude comments, resulting in
- the corresponding objects in the new table having no
- comments.
+ Comments on the copied columns, check constraints,
+ not-null constraints, indexes, extended statistics, and on the
+ table itself will be copied. The default behavior is to exclude
+ comments, resulting in the corresponding objects in the new table
+ having no comments.
+ </para>
+ <para>
+ If multiple <literal>LIKE</literal> clauses specify
+ <literal>INCLUDING COMMENTS</literal> and the source tables have
+ table-level comments, these comments will be concatenated in the new
+ table, separated by newlines, in the order that the
+ <literal>LIKE</literal> clauses appear.
</para>
</listitem>
</varlistentry>
diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c
index cc244c49e9..6c21dd093f 100644
--- a/src/backend/parser/parse_utilcmd.c
+++ b/src/backend/parser/parse_utilcmd.c
@@ -43,6 +43,7 @@
#include "commands/comment.h"
#include "commands/defrem.h"
#include "commands/sequence.h"
+#include "lib/stringinfo.h"
#include "commands/tablecmds.h"
#include "commands/tablespace.h"
#include "miscadmin.h"
@@ -1307,17 +1308,18 @@ transformTableLikeClause(CreateStmtContext *cxt, TableLikeClause *table_like_cla
}
/*
- * We cannot yet deal with defaults, CHECK constraints, indexes, or
- * statistics, since we don't yet know what column numbers the copied
- * columns will have in the finished table. If any of those options are
- * specified, add the LIKE clause to cxt->likeclauses so that
- * expandTableLikeClause will be called after we do know that.
+ * We cannot yet deal with defaults, CHECK constraints, indexes,
+ * statistics, or table comments, since we don't yet know what column
+ * numbers the copied columns will have in the finished table. If any of
+ * those options are specified, add the LIKE clause to cxt->likeclauses
+ * so that expandTableLikeClause will be called after we do know that.
*
* In order for this to work, we remember the relation OID so that
* expandTableLikeClause is certain to open the same table.
*/
if (table_like_clause->options &
- (CREATE_TABLE_LIKE_DEFAULTS |
+ (CREATE_TABLE_LIKE_COMMENTS |
+ CREATE_TABLE_LIKE_DEFAULTS |
CREATE_TABLE_LIKE_GENERATED |
CREATE_TABLE_LIKE_CONSTRAINTS |
CREATE_TABLE_LIKE_INDEXES |
@@ -1625,6 +1627,51 @@ expandTableLikeClause(RangeVar *heapRel, TableLikeClause *table_like_clause)
list_free(parent_extstats);
}
+ /*
+ * Copy comment on the relation itself, if requested.
+ */
+ if (table_like_clause->options & CREATE_TABLE_LIKE_COMMENTS)
+ {
+ comment = GetComment(RelationGetRelid(relation), RelationRelationId, 0);
+
+ if (comment != NULL)
+ {
+ CommentStmt *stmt;
+ char *existing_comment;
+
+ /*
+ * Check if the target table already has a comment from a previous
+ * LIKE clause. If so, append this comment to it with a newline
+ * separator.
+ */
+ existing_comment = GetComment(RelationGetRelid(childrel), RelationRelationId, 0);
+
+ stmt = makeNode(CommentStmt);
+ stmt->objtype = OBJECT_TABLE;
+ if (heapRel->schemaname)
+ stmt->object = (Node *)list_make2(makeString(heapRel->schemaname),
+ makeString(heapRel->relname));
+ else
+ stmt->object = (Node *)list_make1(makeString(heapRel->relname));
+
+ /* Combine comments if there was a previous one */
+ if (existing_comment != NULL)
+ {
+ StringInfoData buf;
+
+ initStringInfo(&buf);
+ appendStringInfoString(&buf, existing_comment);
+ appendStringInfoChar(&buf, '\n');
+ appendStringInfoString(&buf, comment);
+ stmt->comment = buf.data;
+ }
+ else
+ stmt->comment = comment;
+
+ result = lappend(result, stmt);
+ }
+ }
+
/* Done with child rel */
table_close(childrel, NoLock);
diff --git a/src/test/regress/expected/create_table_like.out b/src/test/regress/expected/create_table_like.out
index d3c35c1484..e62ccd1ad5 100644
--- a/src/test/regress/expected/create_table_like.out
+++ b/src/test/regress/expected/create_table_like.out
@@ -341,6 +341,7 @@ CREATE TABLE ctlt3 (a text CHECK (length(a) < 5), c text CHECK (length(c) < 7));
ALTER TABLE ctlt3 ALTER COLUMN c SET STORAGE EXTERNAL;
ALTER TABLE ctlt3 ALTER COLUMN a SET STORAGE MAIN;
CREATE INDEX ctlt3_fnidx ON ctlt3 ((a || c));
+COMMENT ON TABLE ctlt3 IS 'ctlt3 table comment';
COMMENT ON COLUMN ctlt3.a IS 'A3';
COMMENT ON COLUMN ctlt3.c IS 'C';
COMMENT ON CONSTRAINT ctlt3_a_check ON ctlt3 IS 't3_a_check';
@@ -449,6 +450,35 @@ SELECT description FROM pg_description, pg_constraint c WHERE classoid = 'pg_con
t3_a_check
(1 row)
+SELECT obj_description('ctlt13_like'::regclass, 'pg_class') AS table_comment;
+ table_comment
+---------------------
+ ctlt3 table comment
+(1 row)
+
+-- Test multiple LIKE clauses with table comments
+CREATE TABLE ctlt_comment1 (a int);
+COMMENT ON TABLE ctlt_comment1 IS 'table 1 comment';
+CREATE TABLE ctlt_comment2 (b int);
+COMMENT ON TABLE ctlt_comment2 IS 'table 2 comment';
+CREATE TABLE ctlt_comment3 (c int);
+COMMENT ON TABLE ctlt_comment3 IS 'table 3 comment';
+-- Multiple LIKE clauses should concatenate table comments
+CREATE TABLE ctlt_multi_comments (
+ LIKE ctlt_comment1 INCLUDING COMMENTS,
+ LIKE ctlt_comment3 INCLUDING COMMENTS,
+ LIKE ctlt_comment2 INCLUDING COMMENTS
+);
+-- The order of comments should be the same as the order of LIKE clauses
+SELECT obj_description('ctlt_multi_comments'::regclass, 'pg_class') AS table_comment;
+ table_comment
+-----------------
+ table 1 comment+
+ table 3 comment+
+ table 2 comment
+(1 row)
+
+DROP TABLE ctlt_comment1, ctlt_comment2, ctlt_comment3, ctlt_multi_comments;
CREATE TABLE ctlt_all (LIKE ctlt1 INCLUDING ALL);
\d+ ctlt_all
Table "public.ctlt_all"
diff --git a/src/test/regress/sql/create_table_like.sql b/src/test/regress/sql/create_table_like.sql
index 93389b57db..903c51d062 100644
--- a/src/test/regress/sql/create_table_like.sql
+++ b/src/test/regress/sql/create_table_like.sql
@@ -153,6 +153,7 @@ CREATE TABLE ctlt3 (a text CHECK (length(a) < 5), c text CHECK (length(c) < 7));
ALTER TABLE ctlt3 ALTER COLUMN c SET STORAGE EXTERNAL;
ALTER TABLE ctlt3 ALTER COLUMN a SET STORAGE MAIN;
CREATE INDEX ctlt3_fnidx ON ctlt3 ((a || c));
+COMMENT ON TABLE ctlt3 IS 'ctlt3 table comment';
COMMENT ON COLUMN ctlt3.a IS 'A3';
COMMENT ON COLUMN ctlt3.c IS 'C';
COMMENT ON CONSTRAINT ctlt3_a_check ON ctlt3 IS 't3_a_check';
@@ -173,6 +174,26 @@ CREATE TABLE ctlt13_inh () INHERITS (ctlt1, ctlt3);
CREATE TABLE ctlt13_like (LIKE ctlt3 INCLUDING CONSTRAINTS INCLUDING INDEXES INCLUDING COMMENTS INCLUDING STORAGE) INHERITS (ctlt1);
\d+ ctlt13_like
SELECT description FROM pg_description, pg_constraint c WHERE classoid = 'pg_constraint'::regclass AND objoid = c.oid AND c.conrelid = 'ctlt13_like'::regclass;
+SELECT obj_description('ctlt13_like'::regclass, 'pg_class') AS table_comment;
+
+-- Test multiple LIKE clauses with table comments
+CREATE TABLE ctlt_comment1 (a int);
+COMMENT ON TABLE ctlt_comment1 IS 'table 1 comment';
+CREATE TABLE ctlt_comment2 (b int);
+COMMENT ON TABLE ctlt_comment2 IS 'table 2 comment';
+CREATE TABLE ctlt_comment3 (c int);
+COMMENT ON TABLE ctlt_comment3 IS 'table 3 comment';
+
+
+-- Multiple LIKE clauses should concatenate table comments
+CREATE TABLE ctlt_multi_comments (
+ LIKE ctlt_comment1 INCLUDING COMMENTS,
+ LIKE ctlt_comment3 INCLUDING COMMENTS,
+ LIKE ctlt_comment2 INCLUDING COMMENTS
+);
+-- The order of comments should be the same as the order of LIKE clauses
+SELECT obj_description('ctlt_multi_comments'::regclass, 'pg_class') AS table_comment;
+DROP TABLE ctlt_comment1, ctlt_comment2, ctlt_comment3, ctlt_multi_comments;
CREATE TABLE ctlt_all (LIKE ctlt1 INCLUDING ALL);
\d+ ctlt_all
--
2.43.0
^ permalink raw reply [nested|flat] 37+ messages in thread
* Re: COMMENTS are not being copied in CREATE TABLE LIKE
2026-02-12 10:11 COMMENTS are not being copied in CREATE TABLE LIKE Jim Jones <jim.jones@uni-muenster.de>
2026-02-12 14:36 ` Re: COMMENTS are not being copied in CREATE TABLE LIKE Matheus Alcantara <matheusssilv97@gmail.com>
2026-02-12 15:08 ` Re: COMMENTS are not being copied in CREATE TABLE LIKE Fujii Masao <masao.fujii@gmail.com>
2026-02-12 15:33 ` Re: COMMENTS are not being copied in CREATE TABLE LIKE Jim Jones <jim.jones@uni-muenster.de>
2026-02-13 01:29 ` Re: COMMENTS are not being copied in CREATE TABLE LIKE Chao Li <li.evan.chao@gmail.com>
2026-02-13 04:30 ` Re: COMMENTS are not being copied in CREATE TABLE LIKE Tom Lane <tgl@sss.pgh.pa.us>
2026-02-13 09:19 ` Re: COMMENTS are not being copied in CREATE TABLE LIKE Jim Jones <jim.jones@uni-muenster.de>
2026-02-26 13:39 ` Re: COMMENTS are not being copied in CREATE TABLE LIKE Jim Jones <jim.jones@uni-muenster.de>
@ 2026-03-24 18:32 ` Carlos Alves <carlos@alves.inf.br>
2026-03-25 09:21 ` Re: COMMENTS are not being copied in CREATE TABLE LIKE Jim Jones <jim.jones@uni-muenster.de>
0 siblings, 1 reply; 37+ messages in thread
From: Carlos Alves @ 2026-03-24 18:32 UTC (permalink / raw)
To: Jim Jones <jim.jones@uni-muenster.de>; +Cc: Tom Lane <tgl@sss.pgh.pa.us>; Chao Li <li.evan.chao@gmail.com>; Fujii Masao <masao.fujii@gmail.com>; Matheus Alcantara <matheusssilv97@gmail.com>; pgsql-hackers@lists.postgresql.org <pgsql-hackers@lists.postgresql.org>; David G. Johnston <david.g.johnston@gmail.com>
Em seg., 23 de mar. de 2026 às 16:40, Jim Jones <jim.jones@uni-muenster.de>
escreveu:
>
>
> On 13/02/2026 10:19, Jim Jones wrote:
> > ...
> > Example:
> >
> > CREATE TABLE t1 (a int);
> > COMMENT ON TABLE t1 IS 'comment from table 1';
> > CREATE TABLE t2 (b int);
> > COMMENT ON TABLE t2 IS 'comment from table 2';
> > CREATE TABLE t3 (c int);
> > COMMENT ON TABLE t3 IS 'comment from table 3';
> >
> > CREATE TABLE tm (
> > LIKE t1 INCLUDING COMMENTS,
> > LIKE t3 INCLUDING COMMENTS,
> > LIKE t2 INCLUDING COMMENTS
> > );
> >
> > SELECT obj_description('tm'::regclass, 'pg_class') AS table_comment;
> > table_comment
> > ----------------------
> > comment from table 1+
> > comment from table 3+
> > comment from table 2
> > (1 row)
> >
> >
> > Any thoughts on that?
>
> Rebase
>
> Best, Jim
Hi Jim!
I ran some tests after applying your patch and initially didn't find any
problems. I tried to cover the main scenarios in a focused way, observing
the generated results.
Regarding the possibility of including comments in the table itself using
the LIKE clause, it seems sensible since other elements like columns,
indexes, and constraints have copied comments, as already discussed in the
thread.
Just one point I'd like to address is the documentation. Following the idea
of the other "includings" options wouldn't it be interesting to present a
more concise text? As a suggestion, it could be something like:
"Comments on columns, not null and check constraints, indexes, extended
statistics and the table itself from a source table will be copied. If the
command references multiple source tables with including clauses, any
existing table-level comments will be merged into a single entry, separated
by newlines in the order they were specified."
I've attached some preliminary tests I performed.
Thank you in advance for your attention.
Attachments:
tests.sql (application/octet-stream, 8.2K) download
^ permalink raw reply [nested|flat] 37+ messages in thread
* Re: COMMENTS are not being copied in CREATE TABLE LIKE
2026-02-12 10:11 COMMENTS are not being copied in CREATE TABLE LIKE Jim Jones <jim.jones@uni-muenster.de>
2026-02-12 14:36 ` Re: COMMENTS are not being copied in CREATE TABLE LIKE Matheus Alcantara <matheusssilv97@gmail.com>
2026-02-12 15:08 ` Re: COMMENTS are not being copied in CREATE TABLE LIKE Fujii Masao <masao.fujii@gmail.com>
2026-02-12 15:33 ` Re: COMMENTS are not being copied in CREATE TABLE LIKE Jim Jones <jim.jones@uni-muenster.de>
2026-02-13 01:29 ` Re: COMMENTS are not being copied in CREATE TABLE LIKE Chao Li <li.evan.chao@gmail.com>
2026-02-13 04:30 ` Re: COMMENTS are not being copied in CREATE TABLE LIKE Tom Lane <tgl@sss.pgh.pa.us>
2026-02-13 09:19 ` Re: COMMENTS are not being copied in CREATE TABLE LIKE Jim Jones <jim.jones@uni-muenster.de>
2026-02-26 13:39 ` Re: COMMENTS are not being copied in CREATE TABLE LIKE Jim Jones <jim.jones@uni-muenster.de>
2026-03-24 18:32 ` Re: COMMENTS are not being copied in CREATE TABLE LIKE Carlos Alves <carlos@alves.inf.br>
@ 2026-03-25 09:21 ` Jim Jones <jim.jones@uni-muenster.de>
2026-03-25 13:59 ` Re: COMMENTS are not being copied in CREATE TABLE LIKE Carlos Alves <carlos@alves.inf.br>
0 siblings, 1 reply; 37+ messages in thread
From: Jim Jones @ 2026-03-25 09:21 UTC (permalink / raw)
To: Carlos Alves <carlos@alves.inf.br>; +Cc: Tom Lane <tgl@sss.pgh.pa.us>; Chao Li <li.evan.chao@gmail.com>; Fujii Masao <masao.fujii@gmail.com>; Matheus Alcantara <matheusssilv97@gmail.com>; pgsql-hackers@lists.postgresql.org <pgsql-hackers@lists.postgresql.org>; David G. Johnston <david.g.johnston@gmail.com>
Hi Carlos
On 24/03/2026 19:32, Carlos Alves wrote:
> Just one point I'd like to address is the documentation. Following the
> idea of the other "includings" options wouldn't it be interesting to
> present a more concise text? As a suggestion, it could be something like:
>
> "Comments on columns, not null and check constraints, indexes, extended
> statistics and the table itself from a source table will be copied. If
> the command references multiple source tables with including clauses,
> any existing table-level comments will be merged into a single entry,
> separated by newlines in the order they were specified."
In the second paragraph of the INCLUDING COMMENTS docs I wrote:
<para>
If multiple <literal>LIKE</literal> clauses specify
<literal>INCLUDING COMMENTS</literal> and the source tables have
table-level comments, these comments will be concatenated in the new
table, separated by newlines, in the order that the
<literal>LIKE</literal> clauses appear.
</para>
Which pretty much states the same? Please let me know if I am missing
your point here.
> I've attached some preliminary tests I performed.
Thanks for the thorough tests. Much appreciated!
Best, Jim
^ permalink raw reply [nested|flat] 37+ messages in thread
* Re: COMMENTS are not being copied in CREATE TABLE LIKE
2026-02-12 10:11 COMMENTS are not being copied in CREATE TABLE LIKE Jim Jones <jim.jones@uni-muenster.de>
2026-02-12 14:36 ` Re: COMMENTS are not being copied in CREATE TABLE LIKE Matheus Alcantara <matheusssilv97@gmail.com>
2026-02-12 15:08 ` Re: COMMENTS are not being copied in CREATE TABLE LIKE Fujii Masao <masao.fujii@gmail.com>
2026-02-12 15:33 ` Re: COMMENTS are not being copied in CREATE TABLE LIKE Jim Jones <jim.jones@uni-muenster.de>
2026-02-13 01:29 ` Re: COMMENTS are not being copied in CREATE TABLE LIKE Chao Li <li.evan.chao@gmail.com>
2026-02-13 04:30 ` Re: COMMENTS are not being copied in CREATE TABLE LIKE Tom Lane <tgl@sss.pgh.pa.us>
2026-02-13 09:19 ` Re: COMMENTS are not being copied in CREATE TABLE LIKE Jim Jones <jim.jones@uni-muenster.de>
2026-02-26 13:39 ` Re: COMMENTS are not being copied in CREATE TABLE LIKE Jim Jones <jim.jones@uni-muenster.de>
2026-03-24 18:32 ` Re: COMMENTS are not being copied in CREATE TABLE LIKE Carlos Alves <carlos@alves.inf.br>
2026-03-25 09:21 ` Re: COMMENTS are not being copied in CREATE TABLE LIKE Jim Jones <jim.jones@uni-muenster.de>
@ 2026-03-25 13:59 ` Carlos Alves <carlos@alves.inf.br>
2026-03-26 17:59 ` Re: COMMENTS are not being copied in CREATE TABLE LIKE Carlos Alves <carlos@alves.inf.br>
0 siblings, 1 reply; 37+ messages in thread
From: Carlos Alves @ 2026-03-25 13:59 UTC (permalink / raw)
To: Jim Jones <jim.jones@uni-muenster.de>; +Cc: Tom Lane <tgl@sss.pgh.pa.us>; Chao Li <li.evan.chao@gmail.com>; Fujii Masao <masao.fujii@gmail.com>; Matheus Alcantara <matheusssilv97@gmail.com>; pgsql-hackers@lists.postgresql.org <pgsql-hackers@lists.postgresql.org>; David G. Johnston <david.g.johnston@gmail.com>
Em qua., 25 de mar. de 2026 às 06:23, Jim Jones <jim.jones@uni-muenster.de>
escreveu:
> Hi Carlos
>
> On 24/03/2026 19:32, Carlos Alves wrote:
> > Just one point I'd like to address is the documentation. Following the
> > idea of the other "includings" options wouldn't it be interesting to
> > present a more concise text? As a suggestion, it could be something like:
> >
> > "Comments on columns, not null and check constraints, indexes, extended
> > statistics and the table itself from a source table will be copied. If
> > the command references multiple source tables with including clauses,
> > any existing table-level comments will be merged into a single entry,
> > separated by newlines in the order they were specified."
>
> In the second paragraph of the INCLUDING COMMENTS docs I wrote:
>
> <para>
> If multiple <literal>LIKE</literal> clauses specify
> <literal>INCLUDING COMMENTS</literal> and the source tables have
> table-level comments, these comments will be concatenated in the new
> table, separated by newlines, in the order that the
> <literal>LIKE</literal> clauses appear.
> </para>
>
> Which pretty much states the same? Please let me know if I am missing
> your point here.
>
>
> > I've attached some preliminary tests I performed.
>
> Thanks for the thorough tests. Much appreciated!
>
> Best, Jim
>
>
>
Hi Jim!
I saw the second paragraph that you wrote and the explanations are perfect!
My suggestion was instead of describing it in two paragraphs, try
describing the parameter in just one (and the text I sent was just a
suggestion).
But your document is perfect for me! It was just to simplify, as in the
description of the other parameters.
Sincerely, Carlos
^ permalink raw reply [nested|flat] 37+ messages in thread
* Re: COMMENTS are not being copied in CREATE TABLE LIKE
2026-02-12 10:11 COMMENTS are not being copied in CREATE TABLE LIKE Jim Jones <jim.jones@uni-muenster.de>
2026-02-12 14:36 ` Re: COMMENTS are not being copied in CREATE TABLE LIKE Matheus Alcantara <matheusssilv97@gmail.com>
2026-02-12 15:08 ` Re: COMMENTS are not being copied in CREATE TABLE LIKE Fujii Masao <masao.fujii@gmail.com>
2026-02-12 15:33 ` Re: COMMENTS are not being copied in CREATE TABLE LIKE Jim Jones <jim.jones@uni-muenster.de>
2026-02-13 01:29 ` Re: COMMENTS are not being copied in CREATE TABLE LIKE Chao Li <li.evan.chao@gmail.com>
2026-02-13 04:30 ` Re: COMMENTS are not being copied in CREATE TABLE LIKE Tom Lane <tgl@sss.pgh.pa.us>
2026-02-13 09:19 ` Re: COMMENTS are not being copied in CREATE TABLE LIKE Jim Jones <jim.jones@uni-muenster.de>
2026-02-26 13:39 ` Re: COMMENTS are not being copied in CREATE TABLE LIKE Jim Jones <jim.jones@uni-muenster.de>
2026-03-24 18:32 ` Re: COMMENTS are not being copied in CREATE TABLE LIKE Carlos Alves <carlos@alves.inf.br>
2026-03-25 09:21 ` Re: COMMENTS are not being copied in CREATE TABLE LIKE Jim Jones <jim.jones@uni-muenster.de>
2026-03-25 13:59 ` Re: COMMENTS are not being copied in CREATE TABLE LIKE Carlos Alves <carlos@alves.inf.br>
@ 2026-03-26 17:59 ` Carlos Alves <carlos@alves.inf.br>
2026-03-26 18:12 ` Re: COMMENTS are not being copied in CREATE TABLE LIKE Jim Jones <jim.jones@uni-muenster.de>
0 siblings, 1 reply; 37+ messages in thread
From: Carlos Alves @ 2026-03-26 17:59 UTC (permalink / raw)
To: pgsql-hackers@lists.postgresql.org; +Cc: Jim Jones <jim.jones@uni-muenster.de>
The following review has been posted through the commitfest application:
make installcheck-world: tested, passed
Implements feature: tested, passed
Spec compliant: not tested
Documentation: tested, passed
Hi Jim!
The tests performed were as follows:
1. Basic: creating a table from another table containing comments and without comments;
2. Multiple tables: containing comments, without comments, changing the order of the tables;
3. Overriding inclusion and exclusion directives;
4. Comments with line breaks and special characters;
5. Creation of a table involving hundreds of original tables (stress test);
6. Comments in other structures: fields, indexes, constraints;
7. Output generated by pg_dump;
8. Documentation
No errors or failures were detected during the tests.
The only suggestion is to keep the parameter description a single paragraph, following the same style as the description of the other inclusion parameters. But the current description is coherent and okay.
After the tests, the patch is working as expected:
- Table-level comments are copied when the `including comments` parameter is used.
- When there is more than one source table, comments, if any, are kept in the order in which they appear in the command, separated by a new line.
- The performance of command execution was not affected.
I think only the code review is missing, but I don't have much experience to perform this step.
Best regards,
Carlos
^ permalink raw reply [nested|flat] 37+ messages in thread
* Re: COMMENTS are not being copied in CREATE TABLE LIKE
2026-02-12 10:11 COMMENTS are not being copied in CREATE TABLE LIKE Jim Jones <jim.jones@uni-muenster.de>
2026-02-12 14:36 ` Re: COMMENTS are not being copied in CREATE TABLE LIKE Matheus Alcantara <matheusssilv97@gmail.com>
2026-02-12 15:08 ` Re: COMMENTS are not being copied in CREATE TABLE LIKE Fujii Masao <masao.fujii@gmail.com>
2026-02-12 15:33 ` Re: COMMENTS are not being copied in CREATE TABLE LIKE Jim Jones <jim.jones@uni-muenster.de>
2026-02-13 01:29 ` Re: COMMENTS are not being copied in CREATE TABLE LIKE Chao Li <li.evan.chao@gmail.com>
2026-02-13 04:30 ` Re: COMMENTS are not being copied in CREATE TABLE LIKE Tom Lane <tgl@sss.pgh.pa.us>
2026-02-13 09:19 ` Re: COMMENTS are not being copied in CREATE TABLE LIKE Jim Jones <jim.jones@uni-muenster.de>
2026-02-26 13:39 ` Re: COMMENTS are not being copied in CREATE TABLE LIKE Jim Jones <jim.jones@uni-muenster.de>
2026-03-24 18:32 ` Re: COMMENTS are not being copied in CREATE TABLE LIKE Carlos Alves <carlos@alves.inf.br>
2026-03-25 09:21 ` Re: COMMENTS are not being copied in CREATE TABLE LIKE Jim Jones <jim.jones@uni-muenster.de>
2026-03-25 13:59 ` Re: COMMENTS are not being copied in CREATE TABLE LIKE Carlos Alves <carlos@alves.inf.br>
2026-03-26 17:59 ` Re: COMMENTS are not being copied in CREATE TABLE LIKE Carlos Alves <carlos@alves.inf.br>
@ 2026-03-26 18:12 ` Jim Jones <jim.jones@uni-muenster.de>
0 siblings, 0 replies; 37+ messages in thread
From: Jim Jones @ 2026-03-26 18:12 UTC (permalink / raw)
To: Carlos Alves <carlos@alves.inf.br>; pgsql-hackers@lists.postgresql.org
Hi Carlos
On 26/03/2026 18:59, Carlos Alves wrote:
> No errors or failures were detected during the tests.
>
> The only suggestion is to keep the parameter description a single paragraph, following the same style as the description of the other inclusion parameters. But the current description is coherent and okay.
>
> After the tests, the patch is working as expected:
> - Table-level comments are copied when the `including comments` parameter is used.
> - When there is more than one source table, comments, if any, are kept in the order in which they appear in the command, separated by a new line.
> - The performance of command execution was not affected.
>
> I think only the code review is missing, but I don't have much experience to perform this step.
Thanks for this thorough review. This helps a lot!
Let's see what the other reviewers have to say about the code and your
docs suggestion.
Best, Jim
^ permalink raw reply [nested|flat] 37+ messages in thread
* Re: COMMENTS are not being copied in CREATE TABLE LIKE
2026-02-12 10:11 COMMENTS are not being copied in CREATE TABLE LIKE Jim Jones <jim.jones@uni-muenster.de>
2026-02-12 14:36 ` Re: COMMENTS are not being copied in CREATE TABLE LIKE Matheus Alcantara <matheusssilv97@gmail.com>
2026-02-12 15:08 ` Re: COMMENTS are not being copied in CREATE TABLE LIKE Fujii Masao <masao.fujii@gmail.com>
2026-02-12 15:33 ` Re: COMMENTS are not being copied in CREATE TABLE LIKE Jim Jones <jim.jones@uni-muenster.de>
@ 2026-02-13 04:13 ` Fujii Masao <masao.fujii@gmail.com>
2026-02-13 04:22 ` Re: COMMENTS are not being copied in CREATE TABLE LIKE David G. Johnston <david.g.johnston@gmail.com>
2 siblings, 1 reply; 37+ messages in thread
From: Fujii Masao @ 2026-02-13 04:13 UTC (permalink / raw)
To: Jim Jones <jim.jones@uni-muenster.de>; +Cc: Matheus Alcantara <matheusssilv97@gmail.com>; pgsql-hackers@lists.postgresql.org <pgsql-hackers@lists.postgresql.org>
On Fri, Feb 13, 2026 at 12:34 AM Jim Jones <jim.jones@uni-muenster.de> wrote:
>
>
>
> On 12/02/2026 16:08, Fujii Masao wrote:
> > On Thu, Feb 12, 2026 at 11:36 PM Matheus Alcantara
> > <matheusssilv97@gmail.com> wrote:
> >> The patch fix the issue and it seems correct to me.
>
> Thanks for the review!
>
> > The documentation [1] states that INCLUDING COMMENTS copies comments for
> > the copied columns, constraints, and indexes. It does not mention copying
> > comments on the table itself. Therefore, not copying table comments with
> > INCLUDING COMMENTS does not appear to be a bug. That is, the proposed patch
> > seems more like an improvement than a bug fix.
>
> Hmm, it seemed so obvious to me that I didn’t look more closely at the
> documentation :) Although I struggle to see the rationale for not
> copying a table’s comment in a CREATE TABLE LIKE ... INCLUDING COMMENTS
> statement, I have to agree that the documentation is quite clear about
> this limitation, and therefore it cannot really be considered a bug.
> That said, it may well be worth considering for a future release.
>
> What are your thoughts?
At first, I felt it would be more intuitive to copy the table comment as well.
However, on second thought, since LIKE can reference multiple tables,
copying table-level properties such as comments may not be well-defined.
For example, if two source tables each have a table comment and both are
specified in LIKE, which comment should be applied to the new table?
Regards,
--
Fujii Masao
^ permalink raw reply [nested|flat] 37+ messages in thread
* Re: COMMENTS are not being copied in CREATE TABLE LIKE
2026-02-12 10:11 COMMENTS are not being copied in CREATE TABLE LIKE Jim Jones <jim.jones@uni-muenster.de>
2026-02-12 14:36 ` Re: COMMENTS are not being copied in CREATE TABLE LIKE Matheus Alcantara <matheusssilv97@gmail.com>
2026-02-12 15:08 ` Re: COMMENTS are not being copied in CREATE TABLE LIKE Fujii Masao <masao.fujii@gmail.com>
2026-02-12 15:33 ` Re: COMMENTS are not being copied in CREATE TABLE LIKE Jim Jones <jim.jones@uni-muenster.de>
2026-02-13 04:13 ` Re: COMMENTS are not being copied in CREATE TABLE LIKE Fujii Masao <masao.fujii@gmail.com>
@ 2026-02-13 04:22 ` David G. Johnston <david.g.johnston@gmail.com>
0 siblings, 0 replies; 37+ messages in thread
From: David G. Johnston @ 2026-02-13 04:22 UTC (permalink / raw)
To: Fujii Masao <masao.fujii@gmail.com>; +Cc: Jim Jones <jim.jones@uni-muenster.de>; Matheus Alcantara <matheusssilv97@gmail.com>; pgsql-hackers@lists.postgresql.org <pgsql-hackers@lists.postgresql.org>
On Thursday, February 12, 2026, Fujii Masao <masao.fujii@gmail.com> wrote:
>
>
> For example, if two source tables each have a table comment and both are
> specified in LIKE, which comment should be applied to the new table?
>
Both, with a new line between them.
David J.
^ permalink raw reply [nested|flat] 37+ messages in thread
* Re: COMMENTS are not being copied in CREATE TABLE LIKE
2026-02-12 10:11 COMMENTS are not being copied in CREATE TABLE LIKE Jim Jones <jim.jones@uni-muenster.de>
@ 2026-07-18 14:03 ` ZizhuanLiu X-MAN <44973863@qq.com>
2026-07-20 11:52 ` Re: COMMENTS are not being copied in CREATE TABLE LIKE Jim Jones <jim.jones@uni-muenster.de>
1 sibling, 1 reply; 37+ messages in thread
From: ZizhuanLiu X-MAN @ 2026-07-18 14:03 UTC (permalink / raw)
To: Jim Jones <jim.jones@uni-muenster.de>; matheusssilv97 <matheusssilv97@gmail.com>; Chao Li <li.evan.chao@gmail.com>; masao.fujii <masao.fujii@gmail.com>; david.g.johnston <david.g.johnston@gmail.com>; tgl <tgl@sss.pgh.pa.us>; huseyin.d3r <huseyin.d3r@gmail.com>; +Cc: Matheus Alcantara <matheusssilv97@gmail.com>; pgsql-hackers <pgsql-hackers@lists.postgresql.org>; 我自己的邮箱 <44973863@qq.com>
>Hi,
>
>While reviewing another patch[1] I saw that COMMENTS on tables are being
>ignored in CREATE TABLE LIKE:
>.....
>Thoughts?
>
>Best, Jim
>
>1 -
>https://www.postgresql.org/message-id/flat/DG7Y34A6VBEG.76L7K1OML5DI%40gmail.com
Hi everyone,
I would like to share my understanding of common use cases for `CREATE TABLE LIKE`
alongside some related thoughts on copying table comments.
1. When cloning a table together with its associated objects and comments for an
entirely separate business purpose:
1.1 If the user has no special requirements for the comment on the newly created table,
whether the table-level comment is copied or not will make no practical difference to them.
1.2 If the user does need a customized comment for the new table, they will manually run a
`COMMENT ON TABLE` statement to rewrite the description even under the current behavior
that does not copy table comments. Therefore the final outcome remains unaffected regardless
of whether table comments are inherited via `CREATE TABLE LIKE`.
2. If the cloned table is intended to replace the original source table, enabling automatic copy of
the table comment will bring obvious convenience and benefits.
3. In cases where multiple source tables are referenced in one `LIKE` clause, I suggest skipping table
comment copying entirely. Concatenating multiple disparate table comments would result in bloated
and confusing text that fails to accurately describe the intended purpose of the combined new table.
4. As for table ownership and access privileges, these attributes should follow the default rules applied
during standard table creation rather than being copied one-to-one from the source table. We can
draw an analogy to the `cp` command in operating systems: file and directory ownership, group
assignments and access permissions are determined by the system umask environment variable instead
of an exact duplicate of the source file’s permission settings.
5. This aligns with an earlier suggestion from the mailing list thread linked below:
https://www.postgresql.org/message-id/flat/20090907114058.C855.52131E4D@oss.ntt.co.jp
> I realize that might involve a bit
> more code, but I think I'd feel happier if we cloned all the comments we
> reasonably could from the outset.”
From my perspective, adding support to copy table-level comments carries no obvious negative side effects.
Thanks for your time and discussion.
regards,
--
ZizhuanLiu (X-MAN)
44973863@qq.com
^ permalink raw reply [nested|flat] 37+ messages in thread
* Re: COMMENTS are not being copied in CREATE TABLE LIKE
2026-02-12 10:11 COMMENTS are not being copied in CREATE TABLE LIKE Jim Jones <jim.jones@uni-muenster.de>
2026-07-18 14:03 ` Re: COMMENTS are not being copied in CREATE TABLE LIKE ZizhuanLiu X-MAN <44973863@qq.com>
@ 2026-07-20 11:52 ` Jim Jones <jim.jones@uni-muenster.de>
2026-07-20 13:24 ` Re: COMMENTS are not being copied in CREATE TABLE LIKE ZizhuanLiu X-MAN <44973863@qq.com>
0 siblings, 1 reply; 37+ messages in thread
From: Jim Jones @ 2026-07-20 11:52 UTC (permalink / raw)
To: ZizhuanLiu X-MAN <44973863@qq.com>; matheusssilv97 <matheusssilv97@gmail.com>; Chao Li <li.evan.chao@gmail.com>; masao.fujii <masao.fujii@gmail.com>; david.g.johnston <david.g.johnston@gmail.com>; tgl <tgl@sss.pgh.pa.us>; huseyin.d3r <huseyin.d3r@gmail.com>; +Cc: pgsql-hackers <pgsql-hackers@lists.postgresql.org>
Hi ZizhuanLiu
Thanks for the detailed review!
On 18.07.26 16:03, ZizhuanLiu X-MAN wrote:
> 3. In cases where multiple source tables are referenced in one `LIKE` clause, I suggest skipping table
> comment copying entirely. Concatenating multiple disparate table comments would result in bloated
> and confusing text that fails to accurately describe the intended purpose of the combined new table.
I slightly disagree. I don't have a strong opinion about it, but I find
this a rather unnecessary limitation. I do understand the concern of a
bloated comment, but doing so we sort of imply that *all* table comments
are not fit for this feature, which is arguably not accurate. IMHO, if
the user explicitly wants the comments to be copied, we should not judge
if the they make sense or not -- of course, given that the documentation
describes the concatenation.
What do you think?
Thanks for the comments. Much appreciated!
Best, Jim
^ permalink raw reply [nested|flat] 37+ messages in thread
* Re: COMMENTS are not being copied in CREATE TABLE LIKE
2026-02-12 10:11 COMMENTS are not being copied in CREATE TABLE LIKE Jim Jones <jim.jones@uni-muenster.de>
2026-07-18 14:03 ` Re: COMMENTS are not being copied in CREATE TABLE LIKE ZizhuanLiu X-MAN <44973863@qq.com>
2026-07-20 11:52 ` Re: COMMENTS are not being copied in CREATE TABLE LIKE Jim Jones <jim.jones@uni-muenster.de>
@ 2026-07-20 13:24 ` ZizhuanLiu X-MAN <44973863@qq.com>
2026-07-23 11:51 ` Re: COMMENTS are not being copied in CREATE TABLE LIKE Jim Jones <jim.jones@uni-muenster.de>
0 siblings, 1 reply; 37+ messages in thread
From: ZizhuanLiu X-MAN @ 2026-07-20 13:24 UTC (permalink / raw)
To: Jim Jones <jim.jones@uni-muenster.de>; matheusssilv97 <matheusssilv97@gmail.com>; Chao Li <li.evan.chao@gmail.com>; masao.fujii <masao.fujii@gmail.com>; david.g.johnston <david.g.johnston@gmail.com>; tgl <tgl@sss.pgh.pa.us>; huseyin.d3r <huseyin.d3r@gmail.com>; +Cc: pgsql-hackers <pgsql-hackers@lists.postgresql.org>; 我自己的邮箱 <44973863@qq.com>
>Hi ZizhuanLiu
>
>Thanks for the detailed review!
>
>On 18.07.26 16:03, ZizhuanLiu X-MAN wrote:
>> 3. In cases where multiple source tables are referenced in one `LIKE` clause, I suggest skipping table
>> comment copying entirely. Concatenating multiple disparate table comments would result in bloated
>> and confusing text that fails to accurately describe the intended purpose of the combined new table.
>
>I slightly disagree. I don't have a strong opinion about it, but I find
>this a rather unnecessary limitation. I do understand the concern of a
>bloated comment, but doing so we sort of imply that *all* table comments
>are not fit for this feature, which is arguably not accurate. IMHO, if
>the user explicitly wants the comments to be copied, we should not judge
>if the they make sense or not -- of course, given that the documentation
>describes the concatenation.
>
>What do you think?
>
>Thanks for the comments. Much appreciated!
>
>Best, Jim
Hi Jim,everyone
Thanks a lot for your feedback.
I’ve been thinking about the scenario where multiple source tables are used, and here are my concerns:
1. Even putting aside the problem of bloated combined text, we need to consider whether
concatenating all source table comments delivers what developers, DBAs and business users
actually expect. Can the merged description accurately reflect the intended purpose of the
newly created table?
2. If we do implement comment concatenation, what merging rules should we adopt?
Should we cap the number of source tables whose comments get merged, or impose
a maximum character limit on the final combined text? Whichever restriction we pick
will likely require truncating parts of the original comments. After truncation, can the
resulting text still match what users intend to document?
3. In both cases outlined above, I believe the merged result will fail to satisfy users in
most real-world scenarios. Even if we build this merging logic, users will still end up
having to manually rewrite the table comment afterwards to fix it(Unless he does not
care about it). Given that extra manual cleanup work would be unavoidable, it may
be better to skip copying table-level comments entirely for multi-table cases.
4. It is indeed tricky to strike a balance between consistent feature behavior and intuitive,
reasonable usability, which is exactly why we’re having this discussion. Our ultimate goal is
to deliver a smooth experience for end users, so I think it makes sense to shape the design
around the needs of the vast majority of users in their common use cases.
Looking forward to hearing more thoughts from everyone.
regards,
--
ZizhuanLiu (X-MAN)
44973863@qq.com
^ permalink raw reply [nested|flat] 37+ messages in thread
* Re: COMMENTS are not being copied in CREATE TABLE LIKE
2026-02-12 10:11 COMMENTS are not being copied in CREATE TABLE LIKE Jim Jones <jim.jones@uni-muenster.de>
2026-07-18 14:03 ` Re: COMMENTS are not being copied in CREATE TABLE LIKE ZizhuanLiu X-MAN <44973863@qq.com>
2026-07-20 11:52 ` Re: COMMENTS are not being copied in CREATE TABLE LIKE Jim Jones <jim.jones@uni-muenster.de>
2026-07-20 13:24 ` Re: COMMENTS are not being copied in CREATE TABLE LIKE ZizhuanLiu X-MAN <44973863@qq.com>
@ 2026-07-23 11:51 ` Jim Jones <jim.jones@uni-muenster.de>
2026-07-24 02:01 ` Re: COMMENTS are not being copied in CREATE TABLE LIKE ZizhuanLiu X-MAN <44973863@qq.com>
0 siblings, 1 reply; 37+ messages in thread
From: Jim Jones @ 2026-07-23 11:51 UTC (permalink / raw)
To: ZizhuanLiu X-MAN <44973863@qq.com>; matheusssilv97 <matheusssilv97@gmail.com>; Chao Li <li.evan.chao@gmail.com>; masao.fujii <masao.fujii@gmail.com>; david.g.johnston <david.g.johnston@gmail.com>; tgl <tgl@sss.pgh.pa.us>; huseyin.d3r <huseyin.d3r@gmail.com>; +Cc: pgsql-hackers <pgsql-hackers@lists.postgresql.org>
On 20/07/2026 15:24, ZizhuanLiu X-MAN wrote:
> I’ve been thinking about the scenario where multiple source tables are used, and here are my concerns:
> 1. Even putting aside the problem of bloated combined text, we need to consider whether
> concatenating all source table comments delivers what developers, DBAs and business users
> actually expect. Can the merged description accurately reflect the intended purpose of the
> newly created table?
>
> 2. If we do implement comment concatenation, what merging rules should we adopt?
> Should we cap the number of source tables whose comments get merged, or impose
> a maximum character limit on the final combined text? Whichever restriction we pick
> will likely require truncating parts of the original comments. After truncation, can the
> resulting text still match what users intend to document?
>
> 3. In both cases outlined above, I believe the merged result will fail to satisfy users in
> most real-world scenarios. Even if we build this merging logic, users will still end up
> having to manually rewrite the table comment afterwards to fix it(Unless he does not
> care about it). Given that extra manual cleanup work would be unavoidable, it may
> be better to skip copying table-level comments entirely for multi-table cases.
>
> 4. It is indeed tricky to strike a balance between consistent feature behavior and intuitive,
> reasonable usability, which is exactly why we’re having this discussion. Our ultimate goal is
> to deliver a smooth experience for end users, so I think it makes sense to shape the design
> around the needs of the vast majority of users in their common use cases.
These are good points, but I'd argue that excluding multiple tables from
this feature would be hard to justify. Some of these points can be made
to column comments as well -- we cannot guarantee their semantics in the
new table, and yet we allow them to be copied.
postgres=# CREATE TABLE t1 (a int);
COMMENT ON COLUMN t1.a IS 'foo';
CREATE TABLE t2 (b int);
COMMENT ON COLUMN t2.b IS 'bar';
CREATE TABLE tm (
LIKE t1 INCLUDING COMMENTS,
LIKE t2 INCLUDING COMMENTS
);
CREATE TABLE
COMMENT
CREATE TABLE
COMMENT
CREATE TABLE
postgres=# SELECT
col_description('tm'::regclass::oid, 1),
col_description('tm'::regclass::oid, 2);
col_description | col_description
-----------------+-----------------
foo | bar
(1 row)
In this case is up to user to either make sure the column comments are
"transferable" or to correct them in the target table -- which is also
my case on including the table comments.
I'm not totally against dropping comments for multiple tables, but I
want to make sure our arguments against having them make sense. If we
conclude that it's impractical to concatenate table comments we should
also consider the option of withdrawing the patch -- it's not like I'm
terribly in love with it :)
Thanks for the input!
Best, Jim
^ permalink raw reply [nested|flat] 37+ messages in thread
* Re: COMMENTS are not being copied in CREATE TABLE LIKE
2026-02-12 10:11 COMMENTS are not being copied in CREATE TABLE LIKE Jim Jones <jim.jones@uni-muenster.de>
2026-07-18 14:03 ` Re: COMMENTS are not being copied in CREATE TABLE LIKE ZizhuanLiu X-MAN <44973863@qq.com>
2026-07-20 11:52 ` Re: COMMENTS are not being copied in CREATE TABLE LIKE Jim Jones <jim.jones@uni-muenster.de>
2026-07-20 13:24 ` Re: COMMENTS are not being copied in CREATE TABLE LIKE ZizhuanLiu X-MAN <44973863@qq.com>
2026-07-23 11:51 ` Re: COMMENTS are not being copied in CREATE TABLE LIKE Jim Jones <jim.jones@uni-muenster.de>
@ 2026-07-24 02:01 ` ZizhuanLiu X-MAN <44973863@qq.com>
2026-07-24 07:07 ` Re: COMMENTS are not being copied in CREATE TABLE LIKE ZizhuanLiu X-MAN <44973863@qq.com>
2026-09-20 20:28 ` Re: COMMENTS are not being copied in CREATE TABLE LIKE Alex Liapychev <coder.sam@gmail.com>
0 siblings, 2 replies; 37+ messages in thread
From: ZizhuanLiu X-MAN @ 2026-07-24 02:01 UTC (permalink / raw)
To: Jim Jones <jim.jones@uni-muenster.de>; matheusssilv97 <matheusssilv97@gmail.com>; Chao Li <li.evan.chao@gmail.com>; masao.fujii <masao.fujii@gmail.com>; david.g.johnston <david.g.johnston@gmail.com>; tgl <tgl@sss.pgh.pa.us>; huseyin.d3r <huseyin.d3r@gmail.com>; +Cc: pgsql-hackers <pgsql-hackers@lists.postgresql.org>; 我自己的邮箱 <44973863@qq.com>
>Original
>From: Jim Jones <jim.jones@uni-muenster.de>
>Date: 2026-07-23 19:51
>To: ZizhuanLiu X-MAN <44973863@qq.com>, matheusssilv97 <matheusssilv97@gmail.com>, Chao Li <li.evan.chao@gmail.com>, masao.fujii <masao.fujii@gmail.com>, david.g.johnston <david.g.johnston@gmail.com>, tgl <tgl@sss.pgh.pa.us>, huseyin.d3r <huseyin.d3r@gmail.com>
>Cc: pgsql-hackers <pgsql-hackers@lists.postgresql.org>
>Subject: Re: COMMENTS are not being copied in CREATE TABLE LIKE
>These are good points, but I'd argue that excluding multiple tables from
>this feature would be hard to justify. Some of these points can be made
>to column comments as well -- we cannot guarantee their semantics in the
>new table, and yet we allow them to be copied.
>
>postgres=# CREATE TABLE t1 (a int);
>COMMENT ON COLUMN t1.a IS 'foo';
>CREATE TABLE t2 (b int);
>COMMENT ON COLUMN t2.b IS 'bar';
>
>CREATE TABLE tm (
> LIKE t1 INCLUDING COMMENTS,
> LIKE t2 INCLUDING COMMENTS
>);
>CREATE TABLE
>COMMENT
>CREATE TABLE
>COMMENT
>CREATE TABLE
>postgres=# SELECT
> col_description('tm'::regclass::oid, 1),
> col_description('tm'::regclass::oid, 2);
> col_description | col_description
>-----------------+-----------------
> foo | bar
>(1 row)
>
>In this case is up to user to either make sure the column comments are
>"transferable" or to correct them in the target table -- which is also
>my case on including the table comments.
>
>I'm not totally against dropping comments for multiple tables, but I
>want to make sure our arguments against having them make sense. If we
>conclude that it's impractical to concatenate table comments we should
>also consider the option of withdrawing the patch -- it's not like I'm
>terribly in love with it :)
>
>Thanks for the input!
>
>Best, Jim
Hi, Jim, everyone
Thanks for all your feedback, discussion and reminders.
After further consideration, I would like to clarify that I am only sharing my
personal perspective here, with no intention of pushing for any specific
implementation or insisting on my preference.
I have reviewed the relevant syntax and parsing rules:
```SQL
{ INCLUDING | EXCLUDING }
{ COMMENTS | COMPRESSION | CONSTRAINTS | DEFAULTS | GENERATED | IDENTITY | INDEXES | STATISTICS | STORAGE | ALL }
```SQL
With the existing implementation, any object enabled via these clauses is
copied strictly one-to-one alongside the source objects. The command never
performs additional logic such as merging content or pruning items automatically.
This leaves three general user workflows for any single attribute type XXX:
1. All instances of XXX fit the use case perfectly: use `INCLUDING XXX` with no post-copy modifications required.
2. Most items of XXX are suitable: copy via `INCLUDING XXX`, then apply minor manual adjustments afterward.
3. Only a small subset of XXX items are needed: skip copying entirely with `EXCLUDING XXX`, with optional follow-up tweaks.
Extending this logic to the full set of attributes covered by `ALL`:
1. Every attribute works as expected: use `INCLUDING ALL` with zero follow-up changes.
2. Most attributes are applicable: either copy everything with `INCLUDING ALL` and tweak afterward,
or use `INCLUDING ALL` paired with `EXCLUDING XXX/YYY/ZZZ...` to exclude unwanted categories.
3. Only a minority of attributes are required: opt for `EXCLUDING ALL`, then explicitly list needed items
via `INCLUDING XXX/YYY/ZZZ...`.
The current syntax design covering `INCLUDING`, `EXCLUDING`, and `ALL` already offers adequate
flexibility for users to make top-level copy decisions.
One-to-one copying works reliably when exactly one source table is used.
Multi-table scenarios constitute the only exceptional case where the command’s behavior diverges
from single-table usage. If we can resolve the merging logic properly, I believe merging comments
would be a reasonable approach. I do not think introducing brand-new dedicated syntax options is
necessary for this scenario. At this stage, I have not come up with a more elegant alternative solution.
regards,
--
ZizhuanLiu (X-MAN)
44973863@qq.com
^ permalink raw reply [nested|flat] 37+ messages in thread
* Re: COMMENTS are not being copied in CREATE TABLE LIKE
2026-02-12 10:11 COMMENTS are not being copied in CREATE TABLE LIKE Jim Jones <jim.jones@uni-muenster.de>
2026-07-18 14:03 ` Re: COMMENTS are not being copied in CREATE TABLE LIKE ZizhuanLiu X-MAN <44973863@qq.com>
2026-07-20 11:52 ` Re: COMMENTS are not being copied in CREATE TABLE LIKE Jim Jones <jim.jones@uni-muenster.de>
2026-07-20 13:24 ` Re: COMMENTS are not being copied in CREATE TABLE LIKE ZizhuanLiu X-MAN <44973863@qq.com>
2026-07-23 11:51 ` Re: COMMENTS are not being copied in CREATE TABLE LIKE Jim Jones <jim.jones@uni-muenster.de>
2026-07-24 02:01 ` Re: COMMENTS are not being copied in CREATE TABLE LIKE ZizhuanLiu X-MAN <44973863@qq.com>
@ 2026-07-24 07:07 ` ZizhuanLiu X-MAN <44973863@qq.com>
1 sibling, 0 replies; 37+ messages in thread
From: ZizhuanLiu X-MAN @ 2026-07-24 07:07 UTC (permalink / raw)
To: Jim Jones <jim.jones@uni-muenster.de>; matheusssilv97 <matheusssilv97@gmail.com>; Chao Li <li.evan.chao@gmail.com>; masao.fujii <masao.fujii@gmail.com>; david.g.johnston <david.g.johnston@gmail.com>; tgl <tgl@sss.pgh.pa.us>; huseyin.d3r <huseyin.d3r@gmail.com>; +Cc: pgsql-hackers <pgsql-hackers@lists.postgresql.org>; 我自己的邮箱 <44973863@qq.com>
>Original
>From: Jim Jones <jim.jones@uni-muenster.de>
>Date: 2026-07-23 19:51
>To: ZizhuanLiu X-MAN <44973863@qq.com>, matheusssilv97 <matheusssilv97@gmail.com>, Chao Li <li.evan.chao@gmail.com>, masao.fujii <masao.fujii@gmail.com>, david.g.johnston <david.g.johnston@gmail.com>, tgl <tgl@sss.pgh.pa.us>, huseyin.d3r <huseyin.d3r@gmail.com>
>Cc: pgsql-hackers <pgsql-hackers@lists.postgresql.org>
>Subject: Re: COMMENTS are not being copied in CREATE TABLE LIKE
>These are good points, but I'd argue that excluding multiple tables from
>this feature would be hard to justify. Some of these points can be made
>to column comments as well -- we cannot guarantee their semantics in the
>new table, and yet we allow them to be copied.
>
>postgres=# CREATE TABLE t1 (a int);
>COMMENT ON COLUMN t1.a IS 'foo';
>CREATE TABLE t2 (b int);
>COMMENT ON COLUMN t2.b IS 'bar';
>
>CREATE TABLE tm (
> LIKE t1 INCLUDING COMMENTS,
> LIKE t2 INCLUDING COMMENTS
>);
>CREATE TABLE
>COMMENT
>CREATE TABLE
>COMMENT
>CREATE TABLE
>postgres=# SELECT
> col_description('tm'::regclass::oid, 1),
> col_description('tm'::regclass::oid, 2);
> col_description | col_description
>-----------------+-----------------
> foo | bar
>(1 row)
>
>In this case is up to user to either make sure the column comments are
>"transferable" or to correct them in the target table -- which is also
>my case on including the table comments.
>
>I'm not totally against dropping comments for multiple tables, but I
>want to make sure our arguments against having them make sense. If we
>conclude that it's impractical to concatenate table comments we should
>also consider the option of withdrawing the patch -- it's not like I'm
>terribly in love with it :)
>
>Thanks for the input!
>
>Best, Jim
Hi, Jim, everyone
I have checked Db2’s syntax design. Db2’s LIKE syntax is intended to copy columns
only from a single source object, which may be a table, view or nickname. This matches
our discussion perfectly: single-table usage works smoothly, but it is difficult to implement
elegant comment copying when multiple source tables are involved.
```Db2
>>-CREATE TABLE--table-name------------------------------------->
>--+-| element-list |----------------------------+--●----------->
+-OF--type-name1--+-------------------------+-+
| '-| typed-table-options |-' |
+-LIKE--+-table-name1-+--+------------------+-+
| +-view-name---+ '-| copy-options |-' |
| '-nickname-----' |
+-| as-result-table |--+------------------+---+
| '-| copy-options |-' |
+-| materialized-query-definition |-----------+
'-| staging-table-definition |----------------'
......
......
copy-options
|--●--+---------------------------------------------+--●---------------->
| .-COLUMN-. |
'-+-INCLUDING-+--+-----------+--DEFAULTS-'
'-EXCLUDING-'
.-COLUMN ATTRIBUTES-.
.-EXCLUDING IDENTITY--+-------------------------+-.
>--+-------------------------------------------------------+--●-------------|
| .-COLUMN ATTRIBUTES-. |
'-INCLUDING IDENTITY--+-------------------------+--'
```Db2
I suspect PostgreSQL’s CREATE TABLE LIKE was also originally intended to copy metadata
from just one source table, though I found no archived mailing list records to verify this
original design purpose. Even so, PostgreSQL’s syntax and implementation allow users to
specify multiple source tables.
I think multi-table support was added mainly to aggregate columns from different tables,
since most options under `INCLUDING`/`EXCLUDING` govern column-level attributes.
This leads me to wonder why table comment copying was never implemented.
It is probable that developers chose to omit multi-table table comment copying to
enable merging columns from several source tables. Even so, copying table comments
when there is only one source table would be a valuable improvement.
Overall, PostgreSQL supports many more copyable objects and attributes than Db2.
regards,
--
ZizhuanLiu (X-MAN)
44973863@qq.com
^ permalink raw reply [nested|flat] 37+ messages in thread
* Re: COMMENTS are not being copied in CREATE TABLE LIKE
2026-02-12 10:11 COMMENTS are not being copied in CREATE TABLE LIKE Jim Jones <jim.jones@uni-muenster.de>
2026-07-18 14:03 ` Re: COMMENTS are not being copied in CREATE TABLE LIKE ZizhuanLiu X-MAN <44973863@qq.com>
2026-07-20 11:52 ` Re: COMMENTS are not being copied in CREATE TABLE LIKE Jim Jones <jim.jones@uni-muenster.de>
2026-07-20 13:24 ` Re: COMMENTS are not being copied in CREATE TABLE LIKE ZizhuanLiu X-MAN <44973863@qq.com>
2026-07-23 11:51 ` Re: COMMENTS are not being copied in CREATE TABLE LIKE Jim Jones <jim.jones@uni-muenster.de>
2026-07-24 02:01 ` Re: COMMENTS are not being copied in CREATE TABLE LIKE ZizhuanLiu X-MAN <44973863@qq.com>
@ 2026-09-20 20:28 ` Alex Liapychev <coder.sam@gmail.com>
2026-09-21 13:58 ` Re: COMMENTS are not being copied in CREATE TABLE LIKE Tomas Vondra <tomas@vondra.me>
1 sibling, 1 reply; 37+ messages in thread
From: Alex Liapychev @ 2026-09-20 20:28 UTC (permalink / raw)
To: jim.jones@uni-muenster.de; +Cc: matheusssilv97 <matheusssilv97@gmail.com>; Chao Li <li.evan.chao@gmail.com>; masao.fujii <masao.fujii@gmail.com>; david.g.johnston <david.g.johnston@gmail.com>; tgl <tgl@sss.pgh.pa.us>; huseyin.d3r <huseyin.d3r@gmail.com>; pgsql-hackers <pgsql-hackers@lists.postgresql.org>; chochoforwork@gmail.com; 44973863@qq.com
Hello Jim,
Hello everyone,
Thank you Jim and all the reviewers for working on this patch.
We reviewed the patch version v3 on September 20, 2026.
The patch aims to fill the gap in CREATE TABLE LIKE functionality, where INCLUDING COMMENTS or INCLUDING ALL does not copy a comment of a Table itself.
While there is still a discussion going on, if the table's own comment is worth copying at all, we concentrated on reviewing implementation itself.
Patch set structure is good: description of what was done and why, documentation and tests included.
We compiled the patch from branch cf/6482 (commit: 451748bf) against master (commit: 9e17d25e79d) on MacOS (version 26.6.2) on Intel hardware.
Run standard tests(meson tests) and executed manual tests in psql. Tests in both master and cf/6482 completed successfully.
Below are our findings:
1. Corner case: concatenation of big comments, whose total size exceeds MaxAllocSize, fails with an error.
Here is the test case to prove that:
```
CREATE TABLE comment_1gb_test (id BIGINT);
-- set comment 1 GiB in size
UPDATE pg_catalog.pg_description
SET description = pg_catalog.repeat('x', 1000000000)
WHERE classoid = 'pg_catalog.pg_class'::regclass
AND objoid = 'public.comment_1gb_test'::regclass
AND objsubid = 0;
SELECT octet_length(description) as comment_size_bytes FROM pg_catalog.pg_description WHERE classoid = 'pg_catalog.pg_class'::regclass AND objoid = 'public.comment_1gb_test'::regclass AND objsubid = 0;
comment_size_bytes
--------------------
1000000000
(1 row)
CREATE TABLE xxl1 (LIKE comment_1gb_test INCLUDING ALL);
CREATE TABLE xxl2 (LIKE comment_1gb_test INCLUDING ALL);
ALTER TABLE xxl2 RENAME COLUMN id TO id2;
CREATE TABLE merge_xxl (LIKE xxl1 INCLUDING ALL, LIKE xxl2 INCLUDING ALL);
ERROR: string buffer exceeds maximum allowed length (1073741823 bytes)
DETAIL: Cannot enlarge string buffer containing 1000000001 bytes by 1000000000 more bytes.
```
Some form of truncation should be applied, cap the total size is easiest: first table's comment takes an advantage, others - as fit.
2. Code review notes:
2.1. nitpick: parse_utilcmd.c:46: order of includes would be better if added include ("lib/stringinfo.h") was placed either before "miscadmin.h" (alphabetical order) or before "utils/..." (functional order).
2.2. nitpick: parse_utilcmd.c:1652: it would match style of surrounding code better if local variable `CommentStmt *stmt` would be named `comment_stmt`; see code above in the same function: `stats_stmt` (line 1618), `index_stmt` (line 1577), etc.
2.3. nitpick: create_table_like.out:486 & create_table_like.sql:198: Since behaviour of INCLUDING ALL has also been changed by this patch. It would be better to update the tests to cover it.
Thank you very much for your attention.
Kind regards,
Eddie Cho and Alex Liapychev
P.S.
This is our first review, it was done together within a Postgres Patch Review Workshop: Sept 2026.
Thank you, Paul A. Jungwirth, for your efforts to welcome newcomers and involve them in meaningful work on PostgreSQL.
^ permalink raw reply [nested|flat] 37+ messages in thread
* Re: COMMENTS are not being copied in CREATE TABLE LIKE
2026-02-12 10:11 COMMENTS are not being copied in CREATE TABLE LIKE Jim Jones <jim.jones@uni-muenster.de>
2026-07-18 14:03 ` Re: COMMENTS are not being copied in CREATE TABLE LIKE ZizhuanLiu X-MAN <44973863@qq.com>
2026-07-20 11:52 ` Re: COMMENTS are not being copied in CREATE TABLE LIKE Jim Jones <jim.jones@uni-muenster.de>
2026-07-20 13:24 ` Re: COMMENTS are not being copied in CREATE TABLE LIKE ZizhuanLiu X-MAN <44973863@qq.com>
2026-07-23 11:51 ` Re: COMMENTS are not being copied in CREATE TABLE LIKE Jim Jones <jim.jones@uni-muenster.de>
2026-07-24 02:01 ` Re: COMMENTS are not being copied in CREATE TABLE LIKE ZizhuanLiu X-MAN <44973863@qq.com>
2026-09-20 20:28 ` Re: COMMENTS are not being copied in CREATE TABLE LIKE Alex Liapychev <coder.sam@gmail.com>
@ 2026-09-21 13:58 ` Tomas Vondra <tomas@vondra.me>
2026-09-22 19:49 ` Re: COMMENTS are not being copied in CREATE TABLE LIKE Jim Jones <jim.jones@uni-muenster.de>
0 siblings, 1 reply; 37+ messages in thread
From: Tomas Vondra @ 2026-09-21 13:58 UTC (permalink / raw)
To: Alex Liapychev <coder.sam@gmail.com>; jim.jones@uni-muenster.de; +Cc: matheusssilv97 <matheusssilv97@gmail.com>; Chao Li <li.evan.chao@gmail.com>; masao.fujii <masao.fujii@gmail.com>; david.g.johnston <david.g.johnston@gmail.com>; tgl <tgl@sss.pgh.pa.us>; huseyin.d3r <huseyin.d3r@gmail.com>; pgsql-hackers <pgsql-hackers@lists.postgresql.org>; chochoforwork@gmail.com; 44973863@qq.com
On 9/20/26 22:28, Alex Liapychev wrote:
> Hello Jim,
> Hello everyone,
>
> Thank you Jim and all the reviewers for working on this patch.
>
> We reviewed the patch version v3 on September 20, 2026.
>
> The patch aims to fill the gap in CREATE TABLE LIKE functionality, where INCLUDING COMMENTS or INCLUDING ALL does not copy a comment of a Table itself.
> While there is still a discussion going on, if the table's own comment is worth copying at all, we concentrated on reviewing implementation itself.
>
> Patch set structure is good: description of what was done and why, documentation and tests included.
> We compiled the patch from branch cf/6482 (commit: 451748bf) against master (commit: 9e17d25e79d) on MacOS (version 26.6.2) on Intel hardware.
> Run standard tests(meson tests) and executed manual tests in psql. Tests in both master and cf/6482 completed successfully.
>
> Below are our findings:
>
> 1. Corner case: concatenation of big comments, whose total size exceeds MaxAllocSize, fails with an error.
> Here is the test case to prove that:
> ```
> CREATE TABLE comment_1gb_test (id BIGINT);
> -- set comment 1 GiB in size
> UPDATE pg_catalog.pg_description
> SET description = pg_catalog.repeat('x', 1000000000)
> WHERE classoid = 'pg_catalog.pg_class'::regclass
> AND objoid = 'public.comment_1gb_test'::regclass
> AND objsubid = 0;
> SELECT octet_length(description) as comment_size_bytes FROM pg_catalog.pg_description WHERE classoid = 'pg_catalog.pg_class'::regclass AND objoid = 'public.comment_1gb_test'::regclass AND objsubid = 0;
> comment_size_bytes
> --------------------
> 1000000000
> (1 row)
> CREATE TABLE xxl1 (LIKE comment_1gb_test INCLUDING ALL);
> CREATE TABLE xxl2 (LIKE comment_1gb_test INCLUDING ALL);
> ALTER TABLE xxl2 RENAME COLUMN id TO id2;
>
> CREATE TABLE merge_xxl (LIKE xxl1 INCLUDING ALL, LIKE xxl2 INCLUDING ALL);
> ERROR: string buffer exceeds maximum allowed length (1073741823 bytes)
> DETAIL: Cannot enlarge string buffer containing 1000000001 bytes by 1000000000 more bytes.
> ```
> Some form of truncation should be applied, cap the total size is easiest: first table's comment takes an advantage, others - as fit.
>
I'd just reject such cases, with an ERROR that says the comment would be
too long. It's cleaner than just silently start discarding user
information. The number of people hitting this is about 0 anyway. Who
would even have comments of this size?
That being said, I'm not convinced we actually want to concatenate
comments like this. It feels a bit weird, and it can probably lead to
weird stuff like "duplicate" comments, etc. Do we have any precedent for
this behavior? Are we concatenating comments (or other stuff) anywhere?
I couldn't find such place, but maybe I missed something.
FWIW if we really are worried about very long comments, then maybe
expandTableLikeClause should be more careful about freeing comments
during the concatenation? Right now it keeps all the comments and then
also the intermediate strings during concatenation. (I think.)
> 2. Code review notes:
> 2.1. nitpick: parse_utilcmd.c:46: order of includes would be better if added include ("lib/stringinfo.h") was placed either before "miscadmin.h" (alphabetical order) or before "utils/..." (functional order).
Before miscadmin.h, please. We keep includes in alphabetical order.
> 2.2. nitpick: parse_utilcmd.c:1652: it would match style of surrounding code better if local variable `CommentStmt *stmt` would be named `comment_stmt`; see code above in the same function: `stats_stmt` (line 1618), `index_stmt` (line 1577), etc.
Seems very cosmetic, and there's also a lot of places using 'stmt'.
> 2.3. nitpick: create_table_like.out:486 & create_table_like.sql:198: Since behaviour of INCLUDING ALL has also been changed by this patch. It would be better to update the tests to cover it.
>
Yes, that's a fair point. It'd be good to test INCLUDING ALL copies
comments too. The existing INCLUDING ALL test does not check that.
Aside from that, I don't understand why this patch needs to add
CREATE_TABLE_LIKE_COMMENTS to the last block in transformTableLikeClause
intended to deal with options that need column numbers. I mean, this
deals with a comment on the table itself, no? Or does it need to wait
for some other reason, and the comment is misleading?
regards
--
Tomas Vondra
^ permalink raw reply [nested|flat] 37+ messages in thread
* Re: COMMENTS are not being copied in CREATE TABLE LIKE
2026-02-12 10:11 COMMENTS are not being copied in CREATE TABLE LIKE Jim Jones <jim.jones@uni-muenster.de>
2026-07-18 14:03 ` Re: COMMENTS are not being copied in CREATE TABLE LIKE ZizhuanLiu X-MAN <44973863@qq.com>
2026-07-20 11:52 ` Re: COMMENTS are not being copied in CREATE TABLE LIKE Jim Jones <jim.jones@uni-muenster.de>
2026-07-20 13:24 ` Re: COMMENTS are not being copied in CREATE TABLE LIKE ZizhuanLiu X-MAN <44973863@qq.com>
2026-07-23 11:51 ` Re: COMMENTS are not being copied in CREATE TABLE LIKE Jim Jones <jim.jones@uni-muenster.de>
2026-07-24 02:01 ` Re: COMMENTS are not being copied in CREATE TABLE LIKE ZizhuanLiu X-MAN <44973863@qq.com>
2026-09-20 20:28 ` Re: COMMENTS are not being copied in CREATE TABLE LIKE Alex Liapychev <coder.sam@gmail.com>
2026-09-21 13:58 ` Re: COMMENTS are not being copied in CREATE TABLE LIKE Tomas Vondra <tomas@vondra.me>
@ 2026-09-22 19:49 ` Jim Jones <jim.jones@uni-muenster.de>
2026-09-27 00:23 ` Re: COMMENTS are not being copied in CREATE TABLE LIKE Alex Liapychev <coder.sam@gmail.com>
0 siblings, 1 reply; 37+ messages in thread
From: Jim Jones @ 2026-09-22 19:49 UTC (permalink / raw)
To: Tomas Vondra <tomas@vondra.me>; Alex Liapychev <coder.sam@gmail.com>; +Cc: matheusssilv97 <matheusssilv97@gmail.com>; Chao Li <li.evan.chao@gmail.com>; masao.fujii <masao.fujii@gmail.com>; david.g.johnston <david.g.johnston@gmail.com>; tgl <tgl@sss.pgh.pa.us>; huseyin.d3r <huseyin.d3r@gmail.com>; pgsql-hackers <pgsql-hackers@lists.postgresql.org>; chochoforwork@gmail.com; 44973863@qq.com
Hi Alex, Eddie, and Tomas,
Thanks for the thorough review!
On 21/09/2026 15:58, Tomas Vondra wrote:
>
> On 9/20/26 22:28, Alex Liapychev wrote:
>> 1. Corner case: concatenation of big comments, whose total size exceeds MaxAllocSize, fails with an error.
>> Here is the test case to prove that:
>> ```
>> CREATE TABLE comment_1gb_test (id BIGINT);
>> -- set comment 1 GiB in size
>> UPDATE pg_catalog.pg_description
>> SET description = pg_catalog.repeat('x', 1000000000)
>> WHERE classoid = 'pg_catalog.pg_class'::regclass
>> AND objoid = 'public.comment_1gb_test'::regclass
>> AND objsubid = 0;
>> SELECT octet_length(description) as comment_size_bytes FROM pg_catalog.pg_description WHERE classoid = 'pg_catalog.pg_class'::regclass AND objoid = 'public.comment_1gb_test'::regclass AND objsubid = 0;
>> comment_size_bytes
>> --------------------
>> 1000000000
>> (1 row)
>> CREATE TABLE xxl1 (LIKE comment_1gb_test INCLUDING ALL);
>> CREATE TABLE xxl2 (LIKE comment_1gb_test INCLUDING ALL);
>> ALTER TABLE xxl2 RENAME COLUMN id TO id2;
>>
>> CREATE TABLE merge_xxl (LIKE xxl1 INCLUDING ALL, LIKE xxl2 INCLUDING ALL);
>> ERROR: string buffer exceeds maximum allowed length (1073741823 bytes)
>> DETAIL: Cannot enlarge string buffer containing 1000000001 bytes by 1000000000 more bytes.
>> ```
>> Some form of truncation should be applied, cap the total size is easiest: first table's comment takes an advantage, others - as fit.
>>
>
> I'd just reject such cases, with an ERROR that says the comment would be
> too long. It's cleaner than just silently start discarding user
> information. The number of people hitting this is about 0 anyway. Who
> would even have comments of this size?
I also doubt that anyone has a use case that justifies such a large
comment. The needed size for the merged comment is now checked and an
error is raised if it exceeds MaxAllocSize.
psql (20devel)
Type "help" for help.
postgres=# CREATE TABLE xxl1 (a int);
CREATE TABLE xxl2 (b int);
SET allow_system_table_mods = on;
INSERT INTO pg_description VALUES ('xxl1'::regclass,
'pg_class'::regclass, 0, repeat('x',600000000));
INSERT INTO pg_description VALUES ('xxl2'::regclass,
'pg_class'::regclass, 0, repeat('y',600000000));
CREATE TABLE
CREATE TABLE
SET
INSERT 0 1
INSERT 0 1
postgres=# CREATE TABLE merge_xxl (LIKE xxl1 INCLUDING COMMENTS, LIKE
xxl2 INCLUDING COMMENTS);
ERROR: comment for relation "merge_xxl" is too long
DETAIL: The comment is the concatenation of comments copied from
multiple relations named in LIKE clauses, and the result exceeds the
maximum size allowed for a comment.
> That being said, I'm not convinced we actually want to concatenate
> comments like this. It feels a bit weird, and it can probably lead to
> weird stuff like "duplicate" comments, etc. Do we have any precedent for
> this behavior? Are we concatenating comments (or other stuff) anywhere?
> I couldn't find such place, but maybe I missed something.
Neither am I. I'm just not sure that ignoring the comments when using
multiple tables is a better alternative. I can live with it, but so far
I didn't find enough arguments to remove it. The alternatives I see are:
1) concatenate with a (\n) separator (current behaviour)
2) concatenate without a separator
3) first one wins
4) last one wins
5) ignore it altogether when multiple comments are detected (my least
favourite)
6) your idea? :)
WDYT?
>> 2. Code review notes:
>> 2.1. nitpick: parse_utilcmd.c:46: order of includes would be better if added include ("lib/stringinfo.h") was placed either before "miscadmin.h" (alphabetical order) or before "utils/..." (functional order).
>
> Before miscadmin.h, please. We keep includes in alphabetical order.
Fixed.
>> 2.2. nitpick: parse_utilcmd.c:1652: it would match style of surrounding code better if local variable `CommentStmt *stmt` would be named `comment_stmt`; see code above in the same function: `stats_stmt` (line 1618), `index_stmt` (line 1577), etc.
>
> Seems very cosmetic, and there's also a lot of places using 'stmt'.
>
>> 2.3. nitpick: create_table_like.out:486 & create_table_like.sql:198: Since behaviour of INCLUDING ALL has also been changed by this patch. It would be better to update the tests to cover it.
>>
>
> Yes, that's a fair point. It'd be good to test INCLUDING ALL copies
> comments too. The existing INCLUDING ALL test does not check that.
Fixed. I also included a few tests with views, custom types, foreign
tables, and temporary tables.
>
> Aside from that, I don't understand why this patch needs to add
> CREATE_TABLE_LIKE_COMMENTS to the last block in transformTableLikeClause
> intended to deal with options that need column numbers. I mean, this
> deals with a comment on the table itself, no? Or does it need to wait
> for some other reason, and the comment is misleading?
While revisiting the code I found a much larger problem: v3 hard codes
OBJECT_TABLE in obj_type, which breaks the feature when the target is a
foreign table:
psql (20devel)
Type "help" for help.
postgres=# CREATE TABLE t1 (a int);
COMMENT ON TABLE t1 IS 't1 comment';
CREATE SERVER s FOREIGN DATA WRAPPER dummy;
CREATE FOREIGN TABLE ft (LIKE t1 INCLUDING ALL) SERVER s;
CREATE TABLE
COMMENT
CREATE SERVER
ERROR: "ft" is not a table
-- PG18
psql (18.4 (Debian 18.4-1.pgdg13+1))
Type "help" for help.
postgres=# CREATE TABLE t1 (a int);
COMMENT ON TABLE t1 IS 't1 comment';
CREATE SERVER s FOREIGN DATA WRAPPER dummy;
CREATE FOREIGN TABLE ft (LIKE t1 INCLUDING ALL) SERVER s;
CREATE TABLE
COMMENT
CREATE SERVER
CREATE FOREIGN TABLE
postgres=# \d ft
Foreign table "public.ft"
Column | Type | Collation | Nullable | Default | FDW options
--------+---------+-----------+----------+---------+-------------
a | integer | | | |
Server: s
I moved the logic to transformCreateStmt, where I can use cxt.isforeign
to feed cstmt->objtype with OBJECT_FOREIGN_TABLE or OBJECT_TABLE.
Another open question: should we also copy the comments from custom
types when they're used in the LIKE clause?
Example (copied from the regression tests):
CREATE TYPE ctlty_comment6 AS (f int);
COMMENT ON TYPE ctlty_comment6 IS 'comment6';
CREATE TABLE ctlt_type_comment (LIKE ctlty_comment6 INCLUDING COMMENTS);
SELECT obj_description('ctlt_type_comment'::regclass, 'pg_class') AS
table_comment;
table_comment
---------------
comment6
(1 row)
I'd be inclined to leave it out, since types and tables aren't
semantically very close, but I'd like to hear your thoughts first --
removing it would be quick.
PFA v4.
WDYT?
Best, Jim
Attachments:
v4-0001-Add-table-comments-in-CREATE-TABLE-LIKE-INCLUDING.patch (text/x-patch, 15.0K) download
From 0edc783e6ed1e50a54d005f280e2e372bd6fab35 Mon Sep 17 00:00:00 2001
From: Jim Jones <jim.jones@uni-muenster.de>
Date: Tue, 22 Sep 2026 21:18:52 +0200
Subject: [PATCH v4] Add table comments in CREATE TABLE LIKE INCLUDING COMMENTS
When using CREATE TABLE ... LIKE ... INCLUDING COMMENTS (or INCLUDING
ALL), table-level comments were not being copied to the new table, even
though column comments, constraint comments, index comments, and
statistics comments were properly copied. This patch extends the
feature to also copy the table's own comment to the target table.
When multiple LIKE clauses specify INCLUDING COMMENTS and the source
tables have table-level comments, the comments are now concatenated in
the target table, separated by newlines, in the order that the LIKE
clauses appear. This allows users to preserve comment information from
all source tables when creating tables that combine properties from
multiple sources.
---
doc/src/sgml/ref/create_table.sgml | 17 +++-
src/backend/parser/parse_utilcmd.c | 70 ++++++++++++++
.../regress/expected/create_table_like.out | 96 +++++++++++++++++++
src/test/regress/sql/create_table_like.sql | 63 ++++++++++++
4 files changed, 241 insertions(+), 5 deletions(-)
diff --git a/doc/src/sgml/ref/create_table.sgml b/doc/src/sgml/ref/create_table.sgml
index fef24d8f3a2..aff7ae4dc84 100644
--- a/doc/src/sgml/ref/create_table.sgml
+++ b/doc/src/sgml/ref/create_table.sgml
@@ -692,11 +692,18 @@ WITH ( MODULUS <replaceable class="parameter">numeric_literal</replaceable>, REM
<term><literal>INCLUDING COMMENTS</literal></term>
<listitem>
<para>
- Comments for the copied columns, check constraints,
- not-null constraints, indexes, and extended statistics will be
- copied. The default behavior is to exclude comments, resulting in
- the corresponding objects in the new table having no
- comments.
+ Comments for the copied columns, check constraints, not-null
+ constraints, indexes, and extended statistics will be copied, as
+ will the comment on the source relation itself. The default
+ behavior is to exclude comments, resulting in the corresponding
+ objects in the new table having no comments.
+ </para>
+ <para>
+ If multiple <literal>LIKE</literal> clauses specify
+ <literal>INCLUDING COMMENTS</literal> and the source relations have
+ table-level comments, these comments will be concatenated in the new
+ table, separated by newlines, in the order that the
+ <literal>LIKE</literal> clauses appear.
</para>
</listitem>
</varlistentry>
diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c
index f838311090b..10b35d80407 100644
--- a/src/backend/parser/parse_utilcmd.c
+++ b/src/backend/parser/parse_utilcmd.c
@@ -45,6 +45,7 @@
#include "commands/sequence.h"
#include "commands/tablecmds.h"
#include "commands/tablespace.h"
+#include "lib/stringinfo.h"
#include "miscadmin.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
@@ -63,6 +64,7 @@
#include "utils/acl.h"
#include "utils/builtins.h"
#include "utils/lsyscache.h"
+#include "utils/memutils.h"
#include "utils/partcache.h"
#include "utils/rel.h"
#include "utils/ruleutils.h"
@@ -86,6 +88,7 @@ typedef struct
List *fkconstraints; /* FOREIGN KEY constraints */
List *ixconstraints; /* index-creating constraints */
List *likeclauses; /* LIKE clauses that need post-processing */
+ StringInfo tablecomment; /* comment collected from LIKE clauses */
List *blist; /* "before list" of things to do before
* creating the table */
List *alist; /* "after list" of things to do after creating
@@ -251,6 +254,7 @@ transformCreateStmt(CreateStmt *stmt, const char *queryString)
cxt.fkconstraints = NIL;
cxt.ixconstraints = NIL;
cxt.likeclauses = NIL;
+ cxt.tablecomment = NULL;
cxt.blist = NIL;
cxt.alist = NIL;
cxt.pkey = NULL;
@@ -300,6 +304,25 @@ transformCreateStmt(CreateStmt *stmt, const char *queryString)
}
}
+ /*
+ * If any LIKE clause collected a comment on its source table, emit a
+ * single command applying the accumulated text to the new table.
+ */
+ if (cxt.tablecomment != NULL)
+ {
+ CommentStmt *cstmt = makeNode(CommentStmt);
+
+ cstmt->objtype = cxt.isforeign ? OBJECT_FOREIGN_TABLE : OBJECT_TABLE;
+ if (cxt.relation->schemaname)
+ cstmt->object = (Node *) list_make2(makeString(cxt.relation->schemaname),
+ makeString(cxt.relation->relname));
+ else
+ cstmt->object = (Node *) list_make1(makeString(cxt.relation->relname));
+ cstmt->comment = cxt.tablecomment->data;
+
+ cxt.alist = lappend(cxt.alist, cstmt);
+ }
+
/*
* Transfer anything we already have in cxt.alist into save_alist, to keep
* it separate from the output of transformIndexConstraints. (This may
@@ -1309,6 +1332,52 @@ transformTableLikeClause(CreateStmtContext *cxt, TableLikeClause *table_like_cla
}
}
+ /*
+ * Copy the comment on the source relation itself, if requested. Several
+ * LIKE clauses may each supply one; accumulate them in clause order and
+ * let transformCreateStmt emit a single COMMENT command for the result.
+ */
+ if (table_like_clause->options & CREATE_TABLE_LIKE_COMMENTS)
+ {
+ char *tblcomment;
+
+ /* A composite type's comment is attached to its pg_type entry */
+ if (relation->rd_rel->relkind == RELKIND_COMPOSITE_TYPE)
+ tblcomment = GetComment(relation->rd_rel->reltype,
+ TypeRelationId,
+ 0);
+ else
+ tblcomment = GetComment(RelationGetRelid(relation),
+ RelationRelationId,
+ 0);
+
+ if (tblcomment != NULL)
+ {
+ size_t needed = strlen(tblcomment);
+
+ if (cxt->tablecomment == NULL)
+ cxt->tablecomment = makeStringInfo();
+ else
+ needed++; /* the newline separator */
+
+ /*
+ * Reject an over-long result explicitly, rather than letting the
+ * user hit enlargeStringInfo()'s much less informative complaint.
+ */
+ if ((size_t) cxt->tablecomment->len + needed >= MaxAllocSize)
+ ereport(ERROR,
+ (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
+ errmsg("comment for relation \"%s\" is too long",
+ cxt->relation->relname),
+ errdetail("The comment is the concatenation of comments copied from multiple relations named in LIKE clauses, and the result exceeds the maximum size allowed for a comment.")));
+
+ if (cxt->tablecomment->len > 0)
+ appendStringInfoChar(cxt->tablecomment, '\n');
+ appendStringInfoString(cxt->tablecomment, tblcomment);
+ pfree(tblcomment);
+ }
+ }
+
/*
* We cannot yet deal with defaults, CHECK constraints, indexes, or
* statistics, since we don't yet know what column numbers the copied
@@ -3622,6 +3691,7 @@ transformAlterTableStmt(Oid relid, AlterTableStmt *stmt,
cxt.fkconstraints = NIL;
cxt.ixconstraints = NIL;
cxt.likeclauses = NIL;
+ cxt.tablecomment = NULL;
cxt.blist = NIL;
cxt.alist = NIL;
cxt.pkey = NULL;
diff --git a/src/test/regress/expected/create_table_like.out b/src/test/regress/expected/create_table_like.out
index a23735b5fb4..58db2bab784 100644
--- a/src/test/regress/expected/create_table_like.out
+++ b/src/test/regress/expected/create_table_like.out
@@ -698,6 +698,102 @@ SELECT attname, attcompression FROM pg_attribute
e |
(5 rows)
+-- LIKE ... INCLUDING COMMENTS
+-- Test multiple LIKE clauses with table comments
+CREATE TABLE ctlt_comment1 (a int);
+COMMENT ON TABLE ctlt_comment1 IS 'comment1';
+CREATE FOREIGN TABLE ctlft_comment2 (b int) SERVER ctl_s0;
+COMMENT ON FOREIGN TABLE ctlft_comment2 IS 'comment2';
+CREATE VIEW ctlv_comment3 AS SELECT 42 AS c;
+COMMENT ON VIEW ctlv_comment3 IS 'comment3';
+CREATE TEMPORARY TABLE ctltt_comment4 (d int);
+COMMENT ON TABLE ctltt_comment4 IS 'comment4';
+-- Single LIKE clause should copy table comment when INCLUDING COMMENTS is specified.
+CREATE TABLE ctlt_single_comment (LIKE ctlt_comment1 INCLUDING COMMENTS);
+SELECT obj_description('ctlt_single_comment'::regclass, 'pg_class') AS table_comment;
+ table_comment
+---------------
+ comment1
+(1 row)
+
+-- Single LIKE clause should copy table comment when INCLUDING ALL is specified.
+CREATE TABLE ctlt_single_comment_all (LIKE ctlt_comment1 INCLUDING ALL);
+SELECT obj_description('ctlt_single_comment_all'::regclass, 'pg_class') AS table_comment;
+ table_comment
+---------------
+ comment1
+(1 row)
+
+-- Multiple LIKE clauses should concatenate table comments when INCLUDING COMMENTS is specified.
+-- INCLUDING ALL implies INCLUDING COMMENTS, and no comment is copied when neither is specified.
+-- The order of comments should be the same as the order of LIKE clauses
+CREATE TABLE ctlt_multi_comments (
+ LIKE ctlt_comment1 INCLUDING ALL,
+ LIKE ctlv_comment3 INCLUDING COMMENTS,
+ LIKE ctlft_comment2,
+ LIKE ctltt_comment4 INCLUDING COMMENTS
+);
+SELECT obj_description('ctlt_multi_comments'::regclass, 'pg_class') AS table_comment;
+ table_comment
+---------------
+ comment1 +
+ comment3 +
+ comment4
+(1 row)
+
+-- a comment-less source in the middle must not leave a stray separator
+CREATE TABLE ctlt_nocomment (e int);
+CREATE TABLE ctlt_gap_comments (
+ LIKE ctlt_comment1 INCLUDING COMMENTS,
+ LIKE ctlt_nocomment INCLUDING COMMENTS,
+ LIKE ctltt_comment4 INCLUDING COMMENTS
+);
+SELECT obj_description('ctlt_gap_comments'::regclass, 'pg_class') AS table_comment;
+ table_comment
+---------------
+ comment1 +
+ comment4
+(1 row)
+
+-- Test that INCLUDING COMMENTS works for target foreign tables
+CREATE FOREIGN TABLE ctlft_comment4 (LIKE ctlt_comment1 INCLUDING COMMENTS) SERVER ctl_s0;
+SELECT obj_description('ctlft_comment4'::regclass, 'pg_class') AS table_comment;
+ table_comment
+---------------
+ comment1
+(1 row)
+
+-- Test that INCLUDING COMMENTS works for target temporary tables
+CREATE TEMPORARY TABLE ctltt_comment5 (LIKE ctlt_comment1 INCLUDING COMMENTS);
+SELECT obj_description('ctltt_comment5'::regclass, 'pg_class') AS table_comment;
+ table_comment
+---------------
+ comment1
+(1 row)
+
+-- INCLUDING ALL EXCLUDING COMMENTS must not copy the comment
+CREATE TABLE ctlt_no_comment (LIKE ctlt_comment1 INCLUDING ALL EXCLUDING COMMENTS);
+SELECT obj_description('ctlt_no_comment'::regclass, 'pg_class') IS NULL AS no_comment;
+ no_comment
+------------
+ t
+(1 row)
+
+-- A composite type's comment is stored on its pg_type entry, not on pg_class,
+-- but it should be copied just the same
+CREATE TYPE ctlty_comment6 AS (f int);
+COMMENT ON TYPE ctlty_comment6 IS 'comment6';
+CREATE TABLE ctlt_type_comment (LIKE ctlty_comment6 INCLUDING COMMENTS);
+SELECT obj_description('ctlt_type_comment'::regclass, 'pg_class') AS table_comment;
+ table_comment
+---------------
+ comment6
+(1 row)
+
+DROP TABLE ctlt_comment1, ctlt_multi_comments, ctltt_comment4, ctltt_comment5, ctlt_single_comment, ctlt_nocomment, ctlt_gap_comments, ctlt_no_comment, ctlt_single_comment_all, ctlt_type_comment;
+DROP FOREIGN TABLE ctlft_comment2, ctlft_comment4;
+DROP VIEW ctlv_comment3;
+DROP TYPE ctlty_comment6;
-- LIKE ... INCLUDING STATISTICS with dropped columns in the parent,
-- so stxkeys attnums are not contiguous.
CREATE TABLE ctl_stats3_parent (a int, b int, c int);
diff --git a/src/test/regress/sql/create_table_like.sql b/src/test/regress/sql/create_table_like.sql
index d52a93ef131..d2b9c634d53 100644
--- a/src/test/regress/sql/create_table_like.sql
+++ b/src/test/regress/sql/create_table_like.sql
@@ -276,6 +276,69 @@ CREATE FOREIGN TABLE ctl_foreign_table2(LIKE ctl_table INCLUDING ALL) SERVER ctl
SELECT attname, attcompression FROM pg_attribute
WHERE attrelid = 'ctl_foreign_table2'::regclass and attnum > 0 ORDER BY attnum;
+-- LIKE ... INCLUDING COMMENTS
+-- Test multiple LIKE clauses with table comments
+CREATE TABLE ctlt_comment1 (a int);
+COMMENT ON TABLE ctlt_comment1 IS 'comment1';
+CREATE FOREIGN TABLE ctlft_comment2 (b int) SERVER ctl_s0;
+COMMENT ON FOREIGN TABLE ctlft_comment2 IS 'comment2';
+CREATE VIEW ctlv_comment3 AS SELECT 42 AS c;
+COMMENT ON VIEW ctlv_comment3 IS 'comment3';
+CREATE TEMPORARY TABLE ctltt_comment4 (d int);
+COMMENT ON TABLE ctltt_comment4 IS 'comment4';
+
+-- Single LIKE clause should copy table comment when INCLUDING COMMENTS is specified.
+CREATE TABLE ctlt_single_comment (LIKE ctlt_comment1 INCLUDING COMMENTS);
+SELECT obj_description('ctlt_single_comment'::regclass, 'pg_class') AS table_comment;
+
+-- Single LIKE clause should copy table comment when INCLUDING ALL is specified.
+CREATE TABLE ctlt_single_comment_all (LIKE ctlt_comment1 INCLUDING ALL);
+SELECT obj_description('ctlt_single_comment_all'::regclass, 'pg_class') AS table_comment;
+
+-- Multiple LIKE clauses should concatenate table comments when INCLUDING COMMENTS is specified.
+-- INCLUDING ALL implies INCLUDING COMMENTS, and no comment is copied when neither is specified.
+-- The order of comments should be the same as the order of LIKE clauses
+CREATE TABLE ctlt_multi_comments (
+ LIKE ctlt_comment1 INCLUDING ALL,
+ LIKE ctlv_comment3 INCLUDING COMMENTS,
+ LIKE ctlft_comment2,
+ LIKE ctltt_comment4 INCLUDING COMMENTS
+);
+SELECT obj_description('ctlt_multi_comments'::regclass, 'pg_class') AS table_comment;
+
+-- a comment-less source in the middle must not leave a stray separator
+CREATE TABLE ctlt_nocomment (e int);
+CREATE TABLE ctlt_gap_comments (
+ LIKE ctlt_comment1 INCLUDING COMMENTS,
+ LIKE ctlt_nocomment INCLUDING COMMENTS,
+ LIKE ctltt_comment4 INCLUDING COMMENTS
+);
+SELECT obj_description('ctlt_gap_comments'::regclass, 'pg_class') AS table_comment;
+
+-- Test that INCLUDING COMMENTS works for target foreign tables
+CREATE FOREIGN TABLE ctlft_comment4 (LIKE ctlt_comment1 INCLUDING COMMENTS) SERVER ctl_s0;
+SELECT obj_description('ctlft_comment4'::regclass, 'pg_class') AS table_comment;
+
+-- Test that INCLUDING COMMENTS works for target temporary tables
+CREATE TEMPORARY TABLE ctltt_comment5 (LIKE ctlt_comment1 INCLUDING COMMENTS);
+SELECT obj_description('ctltt_comment5'::regclass, 'pg_class') AS table_comment;
+
+-- INCLUDING ALL EXCLUDING COMMENTS must not copy the comment
+CREATE TABLE ctlt_no_comment (LIKE ctlt_comment1 INCLUDING ALL EXCLUDING COMMENTS);
+SELECT obj_description('ctlt_no_comment'::regclass, 'pg_class') IS NULL AS no_comment;
+
+-- A composite type's comment is stored on its pg_type entry, not on pg_class,
+-- but it should be copied just the same
+CREATE TYPE ctlty_comment6 AS (f int);
+COMMENT ON TYPE ctlty_comment6 IS 'comment6';
+CREATE TABLE ctlt_type_comment (LIKE ctlty_comment6 INCLUDING COMMENTS);
+SELECT obj_description('ctlt_type_comment'::regclass, 'pg_class') AS table_comment;
+
+DROP TABLE ctlt_comment1, ctlt_multi_comments, ctltt_comment4, ctltt_comment5, ctlt_single_comment, ctlt_nocomment, ctlt_gap_comments, ctlt_no_comment, ctlt_single_comment_all, ctlt_type_comment;
+DROP FOREIGN TABLE ctlft_comment2, ctlft_comment4;
+DROP VIEW ctlv_comment3;
+DROP TYPE ctlty_comment6;
+
-- LIKE ... INCLUDING STATISTICS with dropped columns in the parent,
-- so stxkeys attnums are not contiguous.
CREATE TABLE ctl_stats3_parent (a int, b int, c int);
--
2.55.0
^ permalink raw reply [nested|flat] 37+ messages in thread
* Re: COMMENTS are not being copied in CREATE TABLE LIKE
2026-02-12 10:11 COMMENTS are not being copied in CREATE TABLE LIKE Jim Jones <jim.jones@uni-muenster.de>
2026-07-18 14:03 ` Re: COMMENTS are not being copied in CREATE TABLE LIKE ZizhuanLiu X-MAN <44973863@qq.com>
2026-07-20 11:52 ` Re: COMMENTS are not being copied in CREATE TABLE LIKE Jim Jones <jim.jones@uni-muenster.de>
2026-07-20 13:24 ` Re: COMMENTS are not being copied in CREATE TABLE LIKE ZizhuanLiu X-MAN <44973863@qq.com>
2026-07-23 11:51 ` Re: COMMENTS are not being copied in CREATE TABLE LIKE Jim Jones <jim.jones@uni-muenster.de>
2026-07-24 02:01 ` Re: COMMENTS are not being copied in CREATE TABLE LIKE ZizhuanLiu X-MAN <44973863@qq.com>
2026-09-20 20:28 ` Re: COMMENTS are not being copied in CREATE TABLE LIKE Alex Liapychev <coder.sam@gmail.com>
2026-09-21 13:58 ` Re: COMMENTS are not being copied in CREATE TABLE LIKE Tomas Vondra <tomas@vondra.me>
2026-09-22 19:49 ` Re: COMMENTS are not being copied in CREATE TABLE LIKE Jim Jones <jim.jones@uni-muenster.de>
@ 2026-09-27 00:23 ` Alex Liapychev <coder.sam@gmail.com>
2026-09-30 23:14 ` Re: COMMENTS are not being copied in CREATE TABLE LIKE Alex Liapychev <coder.sam@gmail.com>
0 siblings, 1 reply; 37+ messages in thread
From: Alex Liapychev @ 2026-09-27 00:23 UTC (permalink / raw)
To: Jim Jones <jim.jones@uni-muenster.de>; tomas@vondra.me; +Cc: matheusssilv97 <matheusssilv97@gmail.com>; Chao Li <li.evan.chao@gmail.com>; masao.fujii <masao.fujii@gmail.com>; david.g.johnston <david.g.johnston@gmail.com>; tgl <tgl@sss.pgh.pa.us>; huseyin.d3r <huseyin.d3r@gmail.com>; pgsql-hackers <pgsql-hackers@lists.postgresql.org>; chochoforwork@gmail.com; 44973863@qq.com
Hi Jim and Tomas, everyone,
Thank you Tomas for stepping in and putting things into perspective.
Thank you Jim for making the changes so quickly.
We did a recheck of the new version v4 of the patch on September 26, 2026.
Branch cf/6482 (commit: 47ed4119) rebased over master (commit: f25c50fd).
Tests on both master alone and with cf/6482 on top completed successfully.
V4 is a complete rewrite.
Author fixed all issues pointed by Tomas and by us (see 3 previous messages).
I will list notable changes briefly:
1. Docs: create_table.sgml:695 - replaced “table” for “source relation”, because it could be other types of relations.
2. Tests: added INCLUDING ALL test case; test cases for concatenation corner cases; foreign and temporary table as copy target cases; type as a source case.
3. Code review of a file parse_utilcmd.c:
3.1. Includes are now sorted in alphabetical order.
3.2. Case when concatenated comment exceeds MaxAllocSize:
>>> 1. Corner case: concatenation of big comments, whose total size exceeds MaxAllocSize, fails with an error.
>> I'd just reject such cases, with an ERROR that says the comment would be
>> too long. It's cleaner than just silently start discarding user
>> information. The number of people hitting this is about 0 anyway. Who
>> would even have comments of this size?
Implemented on L:1369-1374, error ERRCODE_PROGRAM_LIMIT_EXCEEDED is returned with clear message.
3.3. Memory allocation and freeing:
>> FWIW if we really are worried about very long comments, then maybe
>> expandTableLikeClause should be more careful about freeing comments
>> during the concatenation? Right now it keeps all the comments and then
>> also the intermediate strings during concatenation. (I think.)
I tried to trace memory allocations, there are 2 scopes where allocations happen:
3.3.a. The local scope, where comment of each LIKE relation is read:
- allocated: in GetComment (L:1348 or 1352);
- freed: pfree(tblcomment) at L:1379;
Seems good.
3.3.b. Scope of transformCreateStmt function:
- defined: Variable cxt (CreateStmtContext) is created on stack (at L:176), inside is new field tablecomment (StringInfo).
- - It is NULLed before first use at L:257.
- allocated: Field cxt->tablecomment is initialised at L:1361 via makeStringInfo(), which does 2 palloc’s internally.
- read: cxt.tablecomment->data is stored into cstmt->comment at L:321.
- freed: NEVER.
destroyStringInfo is not called from parse_utilcmd.c.
Which seems correct, because we pass data to outside of a function.
But probably we should call pfree for cxt.tablecomment (StringInfo struct itself) to balance 1st palloc from makeStringInfo.
3.3.c. (bonus) Scope where de-allocation also might happen:
Comment produced in transformCreateStmt function then reaches code from file comment.c, where it is stored to target relation.
I could not find any pfree for stmt->comment inside the file comment.c, where it is consumed.
Which should exist to balance 2nd palloc from makeStringInfo.
I don’t know if this palloc'd memory is freed somehow automagically at some later point.
But to my understanding it may be a memory leak, which spans past this specific patch scope.
4. The question if we should concat comments at all:
>> That being said, I'm not convinced we actually want to concatenate
>> comments like this. It feels a bit weird, and it can probably lead to
>> weird stuff like "duplicate" comments, etc. Do we have any precedent for
>> this behavior? Are we concatenating comments (or other stuff) anywhere?
>> I couldn't find such place, but maybe I missed something.
>
> Neither am I. I'm just not sure that ignoring the comments when using
> multiple tables is a better alternative. I can live with it, but so far
> I didn't find enough arguments to remove it. The alternatives I see are:
>
> 1) concatenate with a (\n) separator (current behaviour)
> 2) concatenate without a separator
> 3) first one wins
> 4) last one wins
> 5) ignore it altogether when multiple comments are detected (my least
> favourite)
> 6) your idea? :)
>
> WDYT?
I think from perspective of semantics of INCLUDING COMMENTS/ALL it seems right to copy comments of a table.
But for such complicated scenario, where there are few source relations - not sure.
Maybe "last one wins" would be the easiest to implement and least counter-intuitive for users?
This concatenation code works, but what consequences it may have to support it over years?
Kind regards,
Alex Liapychev
^ permalink raw reply [nested|flat] 37+ messages in thread
* Re: COMMENTS are not being copied in CREATE TABLE LIKE
2026-02-12 10:11 COMMENTS are not being copied in CREATE TABLE LIKE Jim Jones <jim.jones@uni-muenster.de>
2026-07-18 14:03 ` Re: COMMENTS are not being copied in CREATE TABLE LIKE ZizhuanLiu X-MAN <44973863@qq.com>
2026-07-20 11:52 ` Re: COMMENTS are not being copied in CREATE TABLE LIKE Jim Jones <jim.jones@uni-muenster.de>
2026-07-20 13:24 ` Re: COMMENTS are not being copied in CREATE TABLE LIKE ZizhuanLiu X-MAN <44973863@qq.com>
2026-07-23 11:51 ` Re: COMMENTS are not being copied in CREATE TABLE LIKE Jim Jones <jim.jones@uni-muenster.de>
2026-07-24 02:01 ` Re: COMMENTS are not being copied in CREATE TABLE LIKE ZizhuanLiu X-MAN <44973863@qq.com>
2026-09-20 20:28 ` Re: COMMENTS are not being copied in CREATE TABLE LIKE Alex Liapychev <coder.sam@gmail.com>
2026-09-21 13:58 ` Re: COMMENTS are not being copied in CREATE TABLE LIKE Tomas Vondra <tomas@vondra.me>
2026-09-22 19:49 ` Re: COMMENTS are not being copied in CREATE TABLE LIKE Jim Jones <jim.jones@uni-muenster.de>
2026-09-27 00:23 ` Re: COMMENTS are not being copied in CREATE TABLE LIKE Alex Liapychev <coder.sam@gmail.com>
@ 2026-09-30 23:14 ` Alex Liapychev <coder.sam@gmail.com>
2026-10-01 16:49 ` Re: COMMENTS are not being copied in CREATE TABLE LIKE Jim Jones <jim.jones@uni-muenster.de>
0 siblings, 1 reply; 37+ messages in thread
From: Alex Liapychev @ 2026-09-30 23:14 UTC (permalink / raw)
To: Jim Jones <jim.jones@uni-muenster.de>; tomas@vondra.me; +Cc: matheusssilv97 <matheusssilv97@gmail.com>; Chao Li <li.evan.chao@gmail.com>; masao.fujii <masao.fujii@gmail.com>; david.g.johnston <david.g.johnston@gmail.com>; tgl <tgl@sss.pgh.pa.us>; huseyin.d3r <huseyin.d3r@gmail.com>; pgsql-hackers <pgsql-hackers@lists.postgresql.org>; chochoforwork@gmail.com; 44973863@qq.com
Hi Jim and Tomas, everyone,
I dig the mailing list to find if there was anything related to copying the table comments in the past.
I have found thread starting on 2009-09-07, where “COMMENTS” was added to “INCLUDING …” clause for the first time:
https://www.postgresql.org/message-id/flat/20090907114058.C855.52131E4D%40oss.ntt.co.jp
Part of the discussion in this thread regarding COMMENTS goes around columns, indexes and constraints.
I could not find any mention of table’s own comment.
My personal view is that adding this functionality carries more risks than leaving it out. Although the code is relatively small and appears to work correctly, I would not merge it into the codebase.
An example of how this functionality could be used maliciously:
A workflow creates a new table from several template tables in response to an event.
An adversarial user with sufficient database access adds one comment to each of two template tables. The combined size of these comments exceeds MaxAllocSize, causing the automation to fail unexpectedly.
Kind regards,
Alex Liapychev
> On 27 Sep 2026, at 02:23, Alex Liapychev <coder.sam@gmail.com> wrote:
>
> Hi Jim and Tomas, everyone,
>
> Thank you Tomas for stepping in and putting things into perspective.
> Thank you Jim for making the changes so quickly.
>
> We did a recheck of the new version v4 of the patch on September 26, 2026.
> Branch cf/6482 (commit: 47ed4119) rebased over master (commit: f25c50fd).
> Tests on both master alone and with cf/6482 on top completed successfully.
>
> V4 is a complete rewrite.
>
> Author fixed all issues pointed by Tomas and by us (see 3 previous messages).
>
> I will list notable changes briefly:
> 1. Docs: create_table.sgml:695 - replaced “table” for “source relation”, because it could be other types of relations.
> 2. Tests: added INCLUDING ALL test case; test cases for concatenation corner cases; foreign and temporary table as copy target cases; type as a source case.
>
> 3. Code review of a file parse_utilcmd.c:
> 3.1. Includes are now sorted in alphabetical order.
> 3.2. Case when concatenated comment exceeds MaxAllocSize:
>>>> 1. Corner case: concatenation of big comments, whose total size exceeds MaxAllocSize, fails with an error.
>
>>> I'd just reject such cases, with an ERROR that says the comment would be
>>> too long. It's cleaner than just silently start discarding user
>>> information. The number of people hitting this is about 0 anyway. Who
>>> would even have comments of this size?
>
> Implemented on L:1369-1374, error ERRCODE_PROGRAM_LIMIT_EXCEEDED is returned with clear message.
>
> 3.3. Memory allocation and freeing:
>>> FWIW if we really are worried about very long comments, then maybe
>>> expandTableLikeClause should be more careful about freeing comments
>>> during the concatenation? Right now it keeps all the comments and then
>>> also the intermediate strings during concatenation. (I think.)
>
> I tried to trace memory allocations, there are 2 scopes where allocations happen:
>
> 3.3.a. The local scope, where comment of each LIKE relation is read:
> - allocated: in GetComment (L:1348 or 1352);
> - freed: pfree(tblcomment) at L:1379;
> Seems good.
>
> 3.3.b. Scope of transformCreateStmt function:
>
> - defined: Variable cxt (CreateStmtContext) is created on stack (at L:176), inside is new field tablecomment (StringInfo).
> - - It is NULLed before first use at L:257.
>
> - allocated: Field cxt->tablecomment is initialised at L:1361 via makeStringInfo(), which does 2 palloc’s internally.
> - read: cxt.tablecomment->data is stored into cstmt->comment at L:321.
> - freed: NEVER.
>
> destroyStringInfo is not called from parse_utilcmd.c.
> Which seems correct, because we pass data to outside of a function.
> But probably we should call pfree for cxt.tablecomment (StringInfo struct itself) to balance 1st palloc from makeStringInfo.
>
> 3.3.c. (bonus) Scope where de-allocation also might happen:
>
> Comment produced in transformCreateStmt function then reaches code from file comment.c, where it is stored to target relation.
>
> I could not find any pfree for stmt->comment inside the file comment.c, where it is consumed.
> Which should exist to balance 2nd palloc from makeStringInfo.
>
> I don’t know if this palloc'd memory is freed somehow automagically at some later point.
> But to my understanding it may be a memory leak, which spans past this specific patch scope.
>
>
> 4. The question if we should concat comments at all:
>>> That being said, I'm not convinced we actually want to concatenate
>>> comments like this. It feels a bit weird, and it can probably lead to
>>> weird stuff like "duplicate" comments, etc. Do we have any precedent for
>>> this behavior? Are we concatenating comments (or other stuff) anywhere?
>>> I couldn't find such place, but maybe I missed something.
>>
>> Neither am I. I'm just not sure that ignoring the comments when using
>> multiple tables is a better alternative. I can live with it, but so far
>> I didn't find enough arguments to remove it. The alternatives I see are:
>>
>> 1) concatenate with a (\n) separator (current behaviour)
>> 2) concatenate without a separator
>> 3) first one wins
>> 4) last one wins
>> 5) ignore it altogether when multiple comments are detected (my least
>> favourite)
>> 6) your idea? :)
>>
>> WDYT?
>
> I think from perspective of semantics of INCLUDING COMMENTS/ALL it seems right to copy comments of a table.
> But for such complicated scenario, where there are few source relations - not sure.
>
> Maybe "last one wins" would be the easiest to implement and least counter-intuitive for users?
>
> This concatenation code works, but what consequences it may have to support it over years?
>
> Kind regards,
> Alex Liapychev
>
>
^ permalink raw reply [nested|flat] 37+ messages in thread
* Re: COMMENTS are not being copied in CREATE TABLE LIKE
2026-02-12 10:11 COMMENTS are not being copied in CREATE TABLE LIKE Jim Jones <jim.jones@uni-muenster.de>
2026-07-18 14:03 ` Re: COMMENTS are not being copied in CREATE TABLE LIKE ZizhuanLiu X-MAN <44973863@qq.com>
2026-07-20 11:52 ` Re: COMMENTS are not being copied in CREATE TABLE LIKE Jim Jones <jim.jones@uni-muenster.de>
2026-07-20 13:24 ` Re: COMMENTS are not being copied in CREATE TABLE LIKE ZizhuanLiu X-MAN <44973863@qq.com>
2026-07-23 11:51 ` Re: COMMENTS are not being copied in CREATE TABLE LIKE Jim Jones <jim.jones@uni-muenster.de>
2026-07-24 02:01 ` Re: COMMENTS are not being copied in CREATE TABLE LIKE ZizhuanLiu X-MAN <44973863@qq.com>
2026-09-20 20:28 ` Re: COMMENTS are not being copied in CREATE TABLE LIKE Alex Liapychev <coder.sam@gmail.com>
2026-09-21 13:58 ` Re: COMMENTS are not being copied in CREATE TABLE LIKE Tomas Vondra <tomas@vondra.me>
2026-09-22 19:49 ` Re: COMMENTS are not being copied in CREATE TABLE LIKE Jim Jones <jim.jones@uni-muenster.de>
2026-09-27 00:23 ` Re: COMMENTS are not being copied in CREATE TABLE LIKE Alex Liapychev <coder.sam@gmail.com>
2026-09-30 23:14 ` Re: COMMENTS are not being copied in CREATE TABLE LIKE Alex Liapychev <coder.sam@gmail.com>
@ 2026-10-01 16:49 ` Jim Jones <jim.jones@uni-muenster.de>
2026-10-01 17:25 ` Re: COMMENTS are not being copied in CREATE TABLE LIKE Tomas Vondra <tomas@vondra.me>
2026-10-01 21:13 ` Re: COMMENTS are not being copied in CREATE TABLE LIKE Alex Liapychev <coder.sam@gmail.com>
0 siblings, 2 replies; 37+ messages in thread
From: Jim Jones @ 2026-10-01 16:49 UTC (permalink / raw)
To: Alex Liapychev <coder.sam@gmail.com>; tomas@vondra.me; +Cc: matheusssilv97 <matheusssilv97@gmail.com>; Chao Li <li.evan.chao@gmail.com>; masao.fujii <masao.fujii@gmail.com>; david.g.johnston <david.g.johnston@gmail.com>; tgl <tgl@sss.pgh.pa.us>; huseyin.d3r <huseyin.d3r@gmail.com>; pgsql-hackers <pgsql-hackers@lists.postgresql.org>; chochoforwork@gmail.com; 44973863@qq.com
On 01/10/2026 01:14, Alex Liapychev wrote:
>
> My personal view is that adding this functionality carries more risks
> than leaving it out. Although the code is relatively small and appears
> to work correctly, I would not merge it into the codebase.
You mean the multiple tables scenario or the whole patch?
> An example of how this functionality could be used maliciously:
>
> * A workflow creates a new table from several template tables in
> response to an event.
Can you elaborate more on this scenario? I'm afraid I didn't get your
point here. Thanks!
> * An adversarial user with sufficient database access adds one comment
> to each of two template tables. The combined size of these comments
> exceeds |MaxAllocSize|, causing the automation to fail unexpectedly.
An "adversarial" user with enough privileges can do many things break
it, like renaming a column causing a conflict. I see this large comment
scenario as purely theoretical -- at least I fail to see any practical
use case ever exhausting this limit.
Thanks!
Best, Jim
^ permalink raw reply [nested|flat] 37+ messages in thread
* Re: COMMENTS are not being copied in CREATE TABLE LIKE
2026-02-12 10:11 COMMENTS are not being copied in CREATE TABLE LIKE Jim Jones <jim.jones@uni-muenster.de>
2026-07-18 14:03 ` Re: COMMENTS are not being copied in CREATE TABLE LIKE ZizhuanLiu X-MAN <44973863@qq.com>
2026-07-20 11:52 ` Re: COMMENTS are not being copied in CREATE TABLE LIKE Jim Jones <jim.jones@uni-muenster.de>
2026-07-20 13:24 ` Re: COMMENTS are not being copied in CREATE TABLE LIKE ZizhuanLiu X-MAN <44973863@qq.com>
2026-07-23 11:51 ` Re: COMMENTS are not being copied in CREATE TABLE LIKE Jim Jones <jim.jones@uni-muenster.de>
2026-07-24 02:01 ` Re: COMMENTS are not being copied in CREATE TABLE LIKE ZizhuanLiu X-MAN <44973863@qq.com>
2026-09-20 20:28 ` Re: COMMENTS are not being copied in CREATE TABLE LIKE Alex Liapychev <coder.sam@gmail.com>
2026-09-21 13:58 ` Re: COMMENTS are not being copied in CREATE TABLE LIKE Tomas Vondra <tomas@vondra.me>
2026-09-22 19:49 ` Re: COMMENTS are not being copied in CREATE TABLE LIKE Jim Jones <jim.jones@uni-muenster.de>
2026-09-27 00:23 ` Re: COMMENTS are not being copied in CREATE TABLE LIKE Alex Liapychev <coder.sam@gmail.com>
2026-09-30 23:14 ` Re: COMMENTS are not being copied in CREATE TABLE LIKE Alex Liapychev <coder.sam@gmail.com>
2026-10-01 16:49 ` Re: COMMENTS are not being copied in CREATE TABLE LIKE Jim Jones <jim.jones@uni-muenster.de>
@ 2026-10-01 17:25 ` Tomas Vondra <tomas@vondra.me>
2026-10-01 17:45 ` Re: COMMENTS are not being copied in CREATE TABLE LIKE Tom Lane <tgl@sss.pgh.pa.us>
2026-10-01 21:30 ` Re: COMMENTS are not being copied in CREATE TABLE LIKE Jim Jones <jim.jones@uni-muenster.de>
1 sibling, 2 replies; 37+ messages in thread
From: Tomas Vondra @ 2026-10-01 17:25 UTC (permalink / raw)
To: Jim Jones <jim.jones@uni-muenster.de>; Alex Liapychev <coder.sam@gmail.com>; +Cc: matheusssilv97 <matheusssilv97@gmail.com>; Chao Li <li.evan.chao@gmail.com>; masao.fujii <masao.fujii@gmail.com>; david.g.johnston <david.g.johnston@gmail.com>; tgl <tgl@sss.pgh.pa.us>; huseyin.d3r <huseyin.d3r@gmail.com>; pgsql-hackers <pgsql-hackers@lists.postgresql.org>; chochoforwork@gmail.com; 44973863@qq.com
On 10/1/26 18:49, Jim Jones wrote:
> On 01/10/2026 01:14, Alex Liapychev wrote:
>>
>> My personal view is that adding this functionality carries more risks
>> than leaving it out. Although the code is relatively small and appears
>> to work correctly, I would not merge it into the codebase.
>
> You mean the multiple tables scenario or the whole patch?
>
Multiple tables scenario, I think. I suggested asking for a "real" use
case on Discord, because (a) I still don't know if anyone actually needs
this feature, and (b) if there's such use case, it'd probably give us
insights what to do about duplicate comments.
Yes, I can construct a made-up example, but I struggle to create an
example where copying table comments would be meaningful.
I think this lack of use case is partially due to the origin of the
patch. If it was written because of genuine need for the feature, we'd
have the use case by definition. But it was written "because it's
missing" and so no use case.
I was wondering if maybe this was discussed when the INCLUDING COMMENTS
was added, so I did a bit of digging in the archives, and I found this
thread from 2009:
https://www.postgresql.org/message-id/flat/20090907114058.C855.52131E4D%40oss.ntt.co.jp
Unfortunately, there's no discussion about (not) copying table comments.
>> An example of how this functionality could be used maliciously:
>>
>> * A workflow creates a new table from several template tables in
>> response to an event.
>
> Can you elaborate more on this scenario? I'm afraid I didn't get your
> point here. Thanks!
>
>> * An adversarial user with sufficient database access adds one comment
>> to each of two template tables. The combined size of these comments
>> exceeds |MaxAllocSize|, causing the automation to fail unexpectedly.
>
> An "adversarial" user with enough privileges can do many things break
> it, like renaming a column causing a conflict. I see this large comment
> scenario as purely theoretical -- at least I fail to see any practical
> use case ever exhausting this limit.
>
Right. To add comments on an object you have to be an owner, at which
point you can do all kinds of dangerous stuff.
I was more worried about "accidental breakage" where suddenly copying
comments breaks something else. But I failed to construct a practical
example, so maybe it's fine.
regards
--
Tomas Vondra
^ permalink raw reply [nested|flat] 37+ messages in thread
* Re: COMMENTS are not being copied in CREATE TABLE LIKE
2026-02-12 10:11 COMMENTS are not being copied in CREATE TABLE LIKE Jim Jones <jim.jones@uni-muenster.de>
2026-07-18 14:03 ` Re: COMMENTS are not being copied in CREATE TABLE LIKE ZizhuanLiu X-MAN <44973863@qq.com>
2026-07-20 11:52 ` Re: COMMENTS are not being copied in CREATE TABLE LIKE Jim Jones <jim.jones@uni-muenster.de>
2026-07-20 13:24 ` Re: COMMENTS are not being copied in CREATE TABLE LIKE ZizhuanLiu X-MAN <44973863@qq.com>
2026-07-23 11:51 ` Re: COMMENTS are not being copied in CREATE TABLE LIKE Jim Jones <jim.jones@uni-muenster.de>
2026-07-24 02:01 ` Re: COMMENTS are not being copied in CREATE TABLE LIKE ZizhuanLiu X-MAN <44973863@qq.com>
2026-09-20 20:28 ` Re: COMMENTS are not being copied in CREATE TABLE LIKE Alex Liapychev <coder.sam@gmail.com>
2026-09-21 13:58 ` Re: COMMENTS are not being copied in CREATE TABLE LIKE Tomas Vondra <tomas@vondra.me>
2026-09-22 19:49 ` Re: COMMENTS are not being copied in CREATE TABLE LIKE Jim Jones <jim.jones@uni-muenster.de>
2026-09-27 00:23 ` Re: COMMENTS are not being copied in CREATE TABLE LIKE Alex Liapychev <coder.sam@gmail.com>
2026-09-30 23:14 ` Re: COMMENTS are not being copied in CREATE TABLE LIKE Alex Liapychev <coder.sam@gmail.com>
2026-10-01 16:49 ` Re: COMMENTS are not being copied in CREATE TABLE LIKE Jim Jones <jim.jones@uni-muenster.de>
2026-10-01 17:25 ` Re: COMMENTS are not being copied in CREATE TABLE LIKE Tomas Vondra <tomas@vondra.me>
@ 2026-10-01 17:45 ` Tom Lane <tgl@sss.pgh.pa.us>
1 sibling, 0 replies; 37+ messages in thread
From: Tom Lane @ 2026-10-01 17:45 UTC (permalink / raw)
To: Tomas Vondra <tomas@vondra.me>; +Cc: Jim Jones <jim.jones@uni-muenster.de>; Alex Liapychev <coder.sam@gmail.com>; matheusssilv97 <matheusssilv97@gmail.com>; Chao Li <li.evan.chao@gmail.com>; masao.fujii <masao.fujii@gmail.com>; david.g.johnston <david.g.johnston@gmail.com>; huseyin.d3r <huseyin.d3r@gmail.com>; pgsql-hackers <pgsql-hackers@lists.postgresql.org>; chochoforwork@gmail.com; 44973863@qq.com
Tomas Vondra <tomas@vondra.me> writes:
> Yes, I can construct a made-up example, but I struggle to create an
> example where copying table comments would be meaningful.
> I think this lack of use case is partially due to the origin of the
> patch. If it was written because of genuine need for the feature, we'd
> have the use case by definition. But it was written "because it's
> missing" and so no use case.
Yeah, this argument is what's bugging me. We are struggling to figure
out what the patch should do with multiple comments because we don't
have a clear use-case in mind. Any behavior we choose might be the
wrong one for whatever usage is out there. So I'd just as soon pass
on implementing this functionality until we have some field requests
and we can quiz those people about their needs.
regards, tom lane
^ permalink raw reply [nested|flat] 37+ messages in thread
* Re: COMMENTS are not being copied in CREATE TABLE LIKE
2026-02-12 10:11 COMMENTS are not being copied in CREATE TABLE LIKE Jim Jones <jim.jones@uni-muenster.de>
2026-07-18 14:03 ` Re: COMMENTS are not being copied in CREATE TABLE LIKE ZizhuanLiu X-MAN <44973863@qq.com>
2026-07-20 11:52 ` Re: COMMENTS are not being copied in CREATE TABLE LIKE Jim Jones <jim.jones@uni-muenster.de>
2026-07-20 13:24 ` Re: COMMENTS are not being copied in CREATE TABLE LIKE ZizhuanLiu X-MAN <44973863@qq.com>
2026-07-23 11:51 ` Re: COMMENTS are not being copied in CREATE TABLE LIKE Jim Jones <jim.jones@uni-muenster.de>
2026-07-24 02:01 ` Re: COMMENTS are not being copied in CREATE TABLE LIKE ZizhuanLiu X-MAN <44973863@qq.com>
2026-09-20 20:28 ` Re: COMMENTS are not being copied in CREATE TABLE LIKE Alex Liapychev <coder.sam@gmail.com>
2026-09-21 13:58 ` Re: COMMENTS are not being copied in CREATE TABLE LIKE Tomas Vondra <tomas@vondra.me>
2026-09-22 19:49 ` Re: COMMENTS are not being copied in CREATE TABLE LIKE Jim Jones <jim.jones@uni-muenster.de>
2026-09-27 00:23 ` Re: COMMENTS are not being copied in CREATE TABLE LIKE Alex Liapychev <coder.sam@gmail.com>
2026-09-30 23:14 ` Re: COMMENTS are not being copied in CREATE TABLE LIKE Alex Liapychev <coder.sam@gmail.com>
2026-10-01 16:49 ` Re: COMMENTS are not being copied in CREATE TABLE LIKE Jim Jones <jim.jones@uni-muenster.de>
2026-10-01 17:25 ` Re: COMMENTS are not being copied in CREATE TABLE LIKE Tomas Vondra <tomas@vondra.me>
@ 2026-10-01 21:30 ` Jim Jones <jim.jones@uni-muenster.de>
1 sibling, 0 replies; 37+ messages in thread
From: Jim Jones @ 2026-10-01 21:30 UTC (permalink / raw)
To: Tomas Vondra <tomas@vondra.me>; Alex Liapychev <coder.sam@gmail.com>; +Cc: matheusssilv97 <matheusssilv97@gmail.com>; Chao Li <li.evan.chao@gmail.com>; masao.fujii <masao.fujii@gmail.com>; david.g.johnston <david.g.johnston@gmail.com>; tgl <tgl@sss.pgh.pa.us>; huseyin.d3r <huseyin.d3r@gmail.com>; pgsql-hackers <pgsql-hackers@lists.postgresql.org>; chochoforwork@gmail.com; 44973863@qq.com
On 01/10/2026 19:25, Tomas Vondra wrote:
> Multiple tables scenario, I think. I suggested asking for a "real" use
> case on Discord, because (a) I still don't know if anyone actually needs
> this feature, and (b) if there's such use case, it'd probably give us
> insights what to do about duplicate comments.
Oh, I totally missed this discussion on discord.
> Yes, I can construct a made-up example, but I struggle to create an
> example where copying table comments would be meaningful.
>
> I think this lack of use case is partially due to the origin of the
> patch. If it was written because of genuine need for the feature, we'd
> have the use case by definition. But it was written "because it's
> missing" and so no use case.
Yeah, this is the root cause of this issue. I had to deal with this
multiple tables scenario, which I didn't anticipated when proposing the
patch, and couldn't find any good argument just to ignore it. But the
lack of practical use cases is indeed argument enough to leave it alone
for now.
The support for multiple LIKE clauses is now removed from the patch. I
added a variable in CreateStmtContext to count the numbers of LIKE
clauses in transformTableLikeClause(), so that we can skip the comments
in transformCreateStmt() if it's different than 1. Docs and tests were
updated accordingly.
Thanks!
Best, Jim
Attachments:
v5-0001-Add-table-comments-in-CREATE-TABLE-LIKE-INCLUDING.patch (text/x-patch, 15.3K) download
From 62f3f4cb2121abd91c9d91d99e2bd9b973bd72b1 Mon Sep 17 00:00:00 2001
From: Jim Jones <jim.jones@uni-muenster.de>
Date: Thu, 1 Oct 2026 23:04:24 +0200
Subject: [PATCH v5] Add table comments in CREATE TABLE LIKE INCLUDING COMMENTS
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
CREATE TABLE ... (LIKE ... INCLUDING COMMENTS), and therefore INCLUDING
ALL, copies the comments on the source relation's columns, constraints,
indexes, and extended statistics, but not the comment on the source
relation itself. Copy that one too, so that INCLUDING COMMENTS covers
every comment attached to the objects that LIKE copies.
The table comment is copied only when the command contains a single
LIKE clause. A table built from several source relations is not
described by any one of their comments, and there is no obvious way to
choose between or combine them, so in that case no table comment is
copied. Comments on the copied columns, constraints, indexes, and
statistics are unaffected by this.
Author: Jim Jones <jim.jones@uni-muenster.de>
Reviewed-by: Alex Liapychev <coder.sam@gmail.com>
Reviewed-by: Eddie Cho <chochoforwork@gmail.com>
Reviewed-by: Tomas Vondra <tomas@vondra.me>
Reviewed-by: ZizhuanLiu X-MAN <44973863@qq.com>
Reviewed-by: Carlos Alves <carlos@alves.inf.br>
Reviewed-by: Hüseyin Demir <huseyin.d3r@gmail.com>
Reviewed-by: Tom Lane <tgl@sss.pgh.pa.us>
Reviewed-by: David. G. Johnston <david.g.johnston@gmail.com>
Reviewed-by: Chao Li <li.evan.chao@gmail.com>
Reviewed-by: Matheus Alcantara <matheusssilv97@gmail.com>
Reviewed-by: Fujii Masao <masao.fujii@gmail.com>
Discussion: https://www.postgresql.org/message-id/flat/e08cb97f-0364-4002-9cda-3c16b42e4136%40uni-muenster.de
---
doc/src/sgml/ref/create_table.sgml | 17 ++-
src/backend/parser/parse_utilcmd.c | 48 ++++++++
.../regress/expected/create_table_like.out | 109 ++++++++++++++++++
src/test/regress/sql/create_table_like.sql | 70 +++++++++++
4 files changed, 239 insertions(+), 5 deletions(-)
diff --git a/doc/src/sgml/ref/create_table.sgml b/doc/src/sgml/ref/create_table.sgml
index fef24d8f3a2..4e36b121150 100644
--- a/doc/src/sgml/ref/create_table.sgml
+++ b/doc/src/sgml/ref/create_table.sgml
@@ -692,11 +692,18 @@ WITH ( MODULUS <replaceable class="parameter">numeric_literal</replaceable>, REM
<term><literal>INCLUDING COMMENTS</literal></term>
<listitem>
<para>
- Comments for the copied columns, check constraints,
- not-null constraints, indexes, and extended statistics will be
- copied. The default behavior is to exclude comments, resulting in
- the corresponding objects in the new table having no
- comments.
+ Comments for the copied columns, check constraints, not-null
+ constraints, indexes, and extended statistics will be copied, as
+ will the comment on the source relation itself. The default
+ behavior is to exclude comments, resulting in the corresponding
+ objects in the new table having no comments.
+ </para>
+ <para>
+ The comment on the source relation is copied only if the command
+ contains a single <literal>LIKE</literal> clause. If there are
+ several, no source relation's comment is copied to the new table,
+ though the comments on their columns, constraints, indexes, and
+ extended statistics still are.
</para>
</listitem>
</varlistentry>
diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c
index f0c7755b8fb..92d05956a92 100644
--- a/src/backend/parser/parse_utilcmd.c
+++ b/src/backend/parser/parse_utilcmd.c
@@ -86,6 +86,8 @@ typedef struct
List *fkconstraints; /* FOREIGN KEY constraints */
List *ixconstraints; /* index-creating constraints */
List *likeclauses; /* LIKE clauses that need post-processing */
+ char *tablecomment; /* comment on the LIKE source relation */
+ int nlikeclauses; /* # of LIKE clauses seen so far */
List *blist; /* "before list" of things to do before
* creating the table */
List *alist; /* "after list" of things to do after creating
@@ -251,6 +253,8 @@ transformCreateStmt(CreateStmt *stmt, const char *queryString)
cxt.fkconstraints = NIL;
cxt.ixconstraints = NIL;
cxt.likeclauses = NIL;
+ cxt.tablecomment = NULL;
+ cxt.nlikeclauses = 0;
cxt.blist = NIL;
cxt.alist = NIL;
cxt.pkey = NULL;
@@ -300,6 +304,27 @@ transformCreateStmt(CreateStmt *stmt, const char *queryString)
}
}
+ /*
+ * Copy the comment on the LIKE source relation to the new table, but only
+ * if there is exactly one LIKE clause. A table built from several source
+ * relations is not described by any one of their comments, and there is
+ * no obvious way to combine them, so we copy none.
+ */
+ if (cxt.nlikeclauses == 1 && cxt.tablecomment != NULL)
+ {
+ CommentStmt *cstmt = makeNode(CommentStmt);
+
+ cstmt->objtype = cxt.isforeign ? OBJECT_FOREIGN_TABLE : OBJECT_TABLE;
+ if (cxt.relation->schemaname)
+ cstmt->object = (Node *) list_make2(makeString(cxt.relation->schemaname),
+ makeString(cxt.relation->relname));
+ else
+ cstmt->object = (Node *) list_make1(makeString(cxt.relation->relname));
+ cstmt->comment = cxt.tablecomment;
+
+ cxt.alist = lappend(cxt.alist, cstmt);
+ }
+
/*
* Transfer anything we already have in cxt.alist into save_alist, to keep
* it separate from the output of transformIndexConstraints. (This may
@@ -1311,6 +1336,27 @@ transformTableLikeClause(CreateStmtContext *cxt, TableLikeClause *table_like_cla
}
}
+ /*
+ * Remember the comment on the source relation itself, if requested.
+ * transformCreateStmt decides whether to copy it, since that depends on
+ * how many LIKE clauses there are. There's no need to look up the
+ * comment for any clause after the first.
+ */
+ cxt->nlikeclauses++;
+ if (cxt->nlikeclauses == 1 &&
+ (table_like_clause->options & CREATE_TABLE_LIKE_COMMENTS))
+ {
+ /* A composite type's comment is attached to its pg_type entry */
+ if (relation->rd_rel->relkind == RELKIND_COMPOSITE_TYPE)
+ cxt->tablecomment = GetComment(relation->rd_rel->reltype,
+ TypeRelationId,
+ 0);
+ else
+ cxt->tablecomment = GetComment(RelationGetRelid(relation),
+ RelationRelationId,
+ 0);
+ }
+
/*
* We cannot yet deal with defaults, CHECK constraints, indexes, or
* statistics, since we don't yet know what column numbers the copied
@@ -3624,6 +3670,8 @@ transformAlterTableStmt(Oid relid, AlterTableStmt *stmt,
cxt.fkconstraints = NIL;
cxt.ixconstraints = NIL;
cxt.likeclauses = NIL;
+ cxt.tablecomment = NULL;
+ cxt.nlikeclauses = 0;
cxt.blist = NIL;
cxt.alist = NIL;
cxt.pkey = NULL;
diff --git a/src/test/regress/expected/create_table_like.out b/src/test/regress/expected/create_table_like.out
index a23735b5fb4..8a491f338bc 100644
--- a/src/test/regress/expected/create_table_like.out
+++ b/src/test/regress/expected/create_table_like.out
@@ -698,6 +698,115 @@ SELECT attname, attcompression FROM pg_attribute
e |
(5 rows)
+-- LIKE ... INCLUDING COMMENTS
+-- Test copying the comment on the source relation itself
+CREATE TABLE ctlt_comment1 (a int);
+COMMENT ON TABLE ctlt_comment1 IS 'comment1';
+COMMENT ON COLUMN ctlt_comment1.a IS 'column a';
+CREATE FOREIGN TABLE ctlft_comment2 (b int) SERVER ctl_s0;
+COMMENT ON FOREIGN TABLE ctlft_comment2 IS 'comment2';
+CREATE VIEW ctlv_comment3 AS SELECT 42 AS c;
+COMMENT ON VIEW ctlv_comment3 IS 'comment3';
+CREATE TEMPORARY TABLE ctltt_comment4 (d int);
+COMMENT ON TABLE ctltt_comment4 IS 'comment4';
+-- Single LIKE clause should copy table comment when INCLUDING COMMENTS is specified.
+CREATE TABLE ctlt_single_comment (LIKE ctlt_comment1 INCLUDING COMMENTS);
+SELECT obj_description('ctlt_single_comment'::regclass, 'pg_class') AS table_comment;
+ table_comment
+---------------
+ comment1
+(1 row)
+
+-- Single LIKE clause should copy table comment when INCLUDING ALL is specified.
+CREATE TABLE ctlt_single_comment_all (LIKE ctlt_comment1 INCLUDING ALL);
+SELECT obj_description('ctlt_single_comment_all'::regclass, 'pg_class') AS table_comment;
+ table_comment
+---------------
+ comment1
+(1 row)
+
+-- With several LIKE clauses no table comment is copied, even if only one of
+-- them specifies INCLUDING COMMENTS
+CREATE TABLE ctlt_one_of_many (
+ LIKE ctlft_comment2,
+ LIKE ctlv_comment3 INCLUDING COMMENTS,
+ LIKE ctltt_comment4 INCLUDING ALL EXCLUDING COMMENTS
+);
+SELECT obj_description('ctlt_one_of_many'::regclass, 'pg_class') IS NULL AS no_comment;
+ no_comment
+------------
+ t
+(1 row)
+
+-- Likewise if several of them specify INCLUDING COMMENTS (INCLUDING ALL implies
+-- it); column comments are still copied
+CREATE TABLE ctlt_multi_comments (
+ LIKE ctlt_comment1 INCLUDING ALL,
+ LIKE ctlv_comment3 INCLUDING COMMENTS
+);
+SELECT obj_description('ctlt_multi_comments'::regclass, 'pg_class') IS NULL AS no_comment;
+ no_comment
+------------
+ t
+(1 row)
+
+SELECT col_description('ctlt_multi_comments'::regclass, 1) AS column_comment;
+ column_comment
+----------------
+ column a
+(1 row)
+
+-- Likewise if only one of the sources actually has a comment
+CREATE TABLE ctlt_nocomment (e int);
+CREATE TABLE ctlt_gap_comments (
+ LIKE ctlt_nocomment INCLUDING COMMENTS,
+ LIKE ctlt_comment1 INCLUDING COMMENTS
+);
+SELECT obj_description('ctlt_gap_comments'::regclass, 'pg_class') IS NULL AS no_comment;
+ no_comment
+------------
+ t
+(1 row)
+
+-- Test that INCLUDING COMMENTS works for target foreign tables
+CREATE FOREIGN TABLE ctlft_comment4 (LIKE ctlt_comment1 INCLUDING COMMENTS) SERVER ctl_s0;
+SELECT obj_description('ctlft_comment4'::regclass, 'pg_class') AS table_comment;
+ table_comment
+---------------
+ comment1
+(1 row)
+
+-- Test that INCLUDING COMMENTS works for target temporary tables
+CREATE TEMPORARY TABLE ctltt_comment5 (LIKE ctlt_comment1 INCLUDING COMMENTS);
+SELECT obj_description('ctltt_comment5'::regclass, 'pg_class') AS table_comment;
+ table_comment
+---------------
+ comment1
+(1 row)
+
+-- INCLUDING ALL EXCLUDING COMMENTS must not copy the comment
+CREATE TABLE ctlt_no_comment (LIKE ctlt_comment1 INCLUDING ALL EXCLUDING COMMENTS);
+SELECT obj_description('ctlt_no_comment'::regclass, 'pg_class') IS NULL AS no_comment;
+ no_comment
+------------
+ t
+(1 row)
+
+-- A composite type's comment is stored on its pg_type entry, not on pg_class,
+-- but it should be copied just the same
+CREATE TYPE ctlty_comment6 AS (f int);
+COMMENT ON TYPE ctlty_comment6 IS 'comment6';
+CREATE TABLE ctlt_type_comment (LIKE ctlty_comment6 INCLUDING COMMENTS);
+SELECT obj_description('ctlt_type_comment'::regclass, 'pg_class') AS table_comment;
+ table_comment
+---------------
+ comment6
+(1 row)
+
+DROP TABLE ctlt_comment1, ctlt_one_of_many, ctlt_multi_comments, ctltt_comment4, ctltt_comment5, ctlt_single_comment, ctlt_nocomment, ctlt_gap_comments, ctlt_no_comment, ctlt_single_comment_all, ctlt_type_comment;
+DROP FOREIGN TABLE ctlft_comment2, ctlft_comment4;
+DROP VIEW ctlv_comment3;
+DROP TYPE ctlty_comment6;
-- LIKE ... INCLUDING STATISTICS with dropped columns in the parent,
-- so stxkeys attnums are not contiguous.
CREATE TABLE ctl_stats3_parent (a int, b int, c int);
diff --git a/src/test/regress/sql/create_table_like.sql b/src/test/regress/sql/create_table_like.sql
index d52a93ef131..1023e8c5a5c 100644
--- a/src/test/regress/sql/create_table_like.sql
+++ b/src/test/regress/sql/create_table_like.sql
@@ -276,6 +276,76 @@ CREATE FOREIGN TABLE ctl_foreign_table2(LIKE ctl_table INCLUDING ALL) SERVER ctl
SELECT attname, attcompression FROM pg_attribute
WHERE attrelid = 'ctl_foreign_table2'::regclass and attnum > 0 ORDER BY attnum;
+-- LIKE ... INCLUDING COMMENTS
+-- Test copying the comment on the source relation itself
+CREATE TABLE ctlt_comment1 (a int);
+COMMENT ON TABLE ctlt_comment1 IS 'comment1';
+COMMENT ON COLUMN ctlt_comment1.a IS 'column a';
+CREATE FOREIGN TABLE ctlft_comment2 (b int) SERVER ctl_s0;
+COMMENT ON FOREIGN TABLE ctlft_comment2 IS 'comment2';
+CREATE VIEW ctlv_comment3 AS SELECT 42 AS c;
+COMMENT ON VIEW ctlv_comment3 IS 'comment3';
+CREATE TEMPORARY TABLE ctltt_comment4 (d int);
+COMMENT ON TABLE ctltt_comment4 IS 'comment4';
+
+-- Single LIKE clause should copy table comment when INCLUDING COMMENTS is specified.
+CREATE TABLE ctlt_single_comment (LIKE ctlt_comment1 INCLUDING COMMENTS);
+SELECT obj_description('ctlt_single_comment'::regclass, 'pg_class') AS table_comment;
+
+-- Single LIKE clause should copy table comment when INCLUDING ALL is specified.
+CREATE TABLE ctlt_single_comment_all (LIKE ctlt_comment1 INCLUDING ALL);
+SELECT obj_description('ctlt_single_comment_all'::regclass, 'pg_class') AS table_comment;
+
+-- With several LIKE clauses no table comment is copied, even if only one of
+-- them specifies INCLUDING COMMENTS
+CREATE TABLE ctlt_one_of_many (
+ LIKE ctlft_comment2,
+ LIKE ctlv_comment3 INCLUDING COMMENTS,
+ LIKE ctltt_comment4 INCLUDING ALL EXCLUDING COMMENTS
+);
+SELECT obj_description('ctlt_one_of_many'::regclass, 'pg_class') IS NULL AS no_comment;
+
+-- Likewise if several of them specify INCLUDING COMMENTS (INCLUDING ALL implies
+-- it); column comments are still copied
+CREATE TABLE ctlt_multi_comments (
+ LIKE ctlt_comment1 INCLUDING ALL,
+ LIKE ctlv_comment3 INCLUDING COMMENTS
+);
+SELECT obj_description('ctlt_multi_comments'::regclass, 'pg_class') IS NULL AS no_comment;
+SELECT col_description('ctlt_multi_comments'::regclass, 1) AS column_comment;
+
+-- Likewise if only one of the sources actually has a comment
+CREATE TABLE ctlt_nocomment (e int);
+CREATE TABLE ctlt_gap_comments (
+ LIKE ctlt_nocomment INCLUDING COMMENTS,
+ LIKE ctlt_comment1 INCLUDING COMMENTS
+);
+SELECT obj_description('ctlt_gap_comments'::regclass, 'pg_class') IS NULL AS no_comment;
+
+-- Test that INCLUDING COMMENTS works for target foreign tables
+CREATE FOREIGN TABLE ctlft_comment4 (LIKE ctlt_comment1 INCLUDING COMMENTS) SERVER ctl_s0;
+SELECT obj_description('ctlft_comment4'::regclass, 'pg_class') AS table_comment;
+
+-- Test that INCLUDING COMMENTS works for target temporary tables
+CREATE TEMPORARY TABLE ctltt_comment5 (LIKE ctlt_comment1 INCLUDING COMMENTS);
+SELECT obj_description('ctltt_comment5'::regclass, 'pg_class') AS table_comment;
+
+-- INCLUDING ALL EXCLUDING COMMENTS must not copy the comment
+CREATE TABLE ctlt_no_comment (LIKE ctlt_comment1 INCLUDING ALL EXCLUDING COMMENTS);
+SELECT obj_description('ctlt_no_comment'::regclass, 'pg_class') IS NULL AS no_comment;
+
+-- A composite type's comment is stored on its pg_type entry, not on pg_class,
+-- but it should be copied just the same
+CREATE TYPE ctlty_comment6 AS (f int);
+COMMENT ON TYPE ctlty_comment6 IS 'comment6';
+CREATE TABLE ctlt_type_comment (LIKE ctlty_comment6 INCLUDING COMMENTS);
+SELECT obj_description('ctlt_type_comment'::regclass, 'pg_class') AS table_comment;
+
+DROP TABLE ctlt_comment1, ctlt_one_of_many, ctlt_multi_comments, ctltt_comment4, ctltt_comment5, ctlt_single_comment, ctlt_nocomment, ctlt_gap_comments, ctlt_no_comment, ctlt_single_comment_all, ctlt_type_comment;
+DROP FOREIGN TABLE ctlft_comment2, ctlft_comment4;
+DROP VIEW ctlv_comment3;
+DROP TYPE ctlty_comment6;
+
-- LIKE ... INCLUDING STATISTICS with dropped columns in the parent,
-- so stxkeys attnums are not contiguous.
CREATE TABLE ctl_stats3_parent (a int, b int, c int);
--
2.55.0
^ permalink raw reply [nested|flat] 37+ messages in thread
* Re: COMMENTS are not being copied in CREATE TABLE LIKE
2026-02-12 10:11 COMMENTS are not being copied in CREATE TABLE LIKE Jim Jones <jim.jones@uni-muenster.de>
2026-07-18 14:03 ` Re: COMMENTS are not being copied in CREATE TABLE LIKE ZizhuanLiu X-MAN <44973863@qq.com>
2026-07-20 11:52 ` Re: COMMENTS are not being copied in CREATE TABLE LIKE Jim Jones <jim.jones@uni-muenster.de>
2026-07-20 13:24 ` Re: COMMENTS are not being copied in CREATE TABLE LIKE ZizhuanLiu X-MAN <44973863@qq.com>
2026-07-23 11:51 ` Re: COMMENTS are not being copied in CREATE TABLE LIKE Jim Jones <jim.jones@uni-muenster.de>
2026-07-24 02:01 ` Re: COMMENTS are not being copied in CREATE TABLE LIKE ZizhuanLiu X-MAN <44973863@qq.com>
2026-09-20 20:28 ` Re: COMMENTS are not being copied in CREATE TABLE LIKE Alex Liapychev <coder.sam@gmail.com>
2026-09-21 13:58 ` Re: COMMENTS are not being copied in CREATE TABLE LIKE Tomas Vondra <tomas@vondra.me>
2026-09-22 19:49 ` Re: COMMENTS are not being copied in CREATE TABLE LIKE Jim Jones <jim.jones@uni-muenster.de>
2026-09-27 00:23 ` Re: COMMENTS are not being copied in CREATE TABLE LIKE Alex Liapychev <coder.sam@gmail.com>
2026-09-30 23:14 ` Re: COMMENTS are not being copied in CREATE TABLE LIKE Alex Liapychev <coder.sam@gmail.com>
2026-10-01 16:49 ` Re: COMMENTS are not being copied in CREATE TABLE LIKE Jim Jones <jim.jones@uni-muenster.de>
@ 2026-10-01 21:13 ` Alex Liapychev <coder.sam@gmail.com>
2026-10-01 21:36 ` Re: COMMENTS are not being copied in CREATE TABLE LIKE Jim Jones <jim.jones@uni-muenster.de>
1 sibling, 1 reply; 37+ messages in thread
From: Alex Liapychev @ 2026-10-01 21:13 UTC (permalink / raw)
To: Jim Jones <jim.jones@uni-muenster.de>; +Cc: tomas@vondra.me, tgl@sss.pgh.pa.us, matheusssilv97 <matheusssilv97@gmail.com>; Chao Li <li.evan.chao@gmail.com>; masao.fujii <masao.fujii@gmail.com>; david.g.johnston <david.g.johnston@gmail.com>; tgl <tgl@sss.pgh.pa.us>; huseyin.d3r <huseyin.d3r@gmail.com>; pgsql-hackers <pgsql-hackers@lists.postgresql.org>; chochoforwork@gmail.com; 44973863@qq.com
Hi Jim,
Hi Tomas and Tom,
First, let me address the specific questions.
> On 1 Oct 2026, at 18:49, Jim Jones <jim.jones@uni-muenster.de> wrote:
>
> On 01/10/2026 01:14, Alex Liapychev wrote:
>>
>> My personal view is that adding this functionality carries more risks
>> than leaving it out. Although the code is relatively small and appears
>> to work correctly, I would not merge it into the codebase.
>
> You mean the multiple tables scenario or the whole patch?
Yes, I meant the scenario involving multiple tables - specifically, the concatenation of their comments.
Sorry I wasn’t clear about that here.
>> An example of how this functionality could be used maliciously:
>>
>> * A workflow creates a new table from several template tables in
>> response to an event.
>
> Can you elaborate more on this scenario? I'm afraid I didn't get your
> point here. Thanks!
>
I meant that CREATE TABLE is executed within some automatic flow, without direct human supervision.
For example, by a cron job or application’s code that executes CREATE TABLE in response to some event.
>> * An adversarial user with sufficient database access adds one comment
>> to each of two template tables. The combined size of these comments
>> exceeds |MaxAllocSize|, causing the automation to fail unexpectedly.
>
> An "adversarial" user with enough privileges can do many things break
> it, like renaming a column causing a conflict. I see this large comment
> scenario as purely theoretical -- at least I fail to see any practical
> use case ever exhausting this limit.
Yes, this is a narrow and theoretical scenario. But introducing a way to break the application still creates a real vulnerability, even if it requires very specific conditions. The delay between the adversary’s action (adding long comments) and the resulting failure (when CREATE TABLE actually runs) makes the cause harder to identify and can help the adversary remain undetected longer.
Second, let me explain my overall assessment.
First of all, the code itself in v4 looks good. (The question of freeing memory that I had, is answered by the Memory Contexts.)
My concern is less about the implementation itself than about whether the benefit justifies the long-term maintenance burden and the potential security risk, however theoretical.
As Tomas and Tom’s comments have highlighted, the underlying issue is the lack of a clear use case.
A concrete, practical use case would help establish whether those costs and risks are justified at all, and which alternative to concatenation to pick.
That said, this is just my assessment as a reviewer.
Thank you for your work on this patch and for taking the time to discuss these concerns.
Kind regards,
Alex Liapychev
^ permalink raw reply [nested|flat] 37+ messages in thread
* Re: COMMENTS are not being copied in CREATE TABLE LIKE
2026-02-12 10:11 COMMENTS are not being copied in CREATE TABLE LIKE Jim Jones <jim.jones@uni-muenster.de>
2026-07-18 14:03 ` Re: COMMENTS are not being copied in CREATE TABLE LIKE ZizhuanLiu X-MAN <44973863@qq.com>
2026-07-20 11:52 ` Re: COMMENTS are not being copied in CREATE TABLE LIKE Jim Jones <jim.jones@uni-muenster.de>
2026-07-20 13:24 ` Re: COMMENTS are not being copied in CREATE TABLE LIKE ZizhuanLiu X-MAN <44973863@qq.com>
2026-07-23 11:51 ` Re: COMMENTS are not being copied in CREATE TABLE LIKE Jim Jones <jim.jones@uni-muenster.de>
2026-07-24 02:01 ` Re: COMMENTS are not being copied in CREATE TABLE LIKE ZizhuanLiu X-MAN <44973863@qq.com>
2026-09-20 20:28 ` Re: COMMENTS are not being copied in CREATE TABLE LIKE Alex Liapychev <coder.sam@gmail.com>
2026-09-21 13:58 ` Re: COMMENTS are not being copied in CREATE TABLE LIKE Tomas Vondra <tomas@vondra.me>
2026-09-22 19:49 ` Re: COMMENTS are not being copied in CREATE TABLE LIKE Jim Jones <jim.jones@uni-muenster.de>
2026-09-27 00:23 ` Re: COMMENTS are not being copied in CREATE TABLE LIKE Alex Liapychev <coder.sam@gmail.com>
2026-09-30 23:14 ` Re: COMMENTS are not being copied in CREATE TABLE LIKE Alex Liapychev <coder.sam@gmail.com>
2026-10-01 16:49 ` Re: COMMENTS are not being copied in CREATE TABLE LIKE Jim Jones <jim.jones@uni-muenster.de>
2026-10-01 21:13 ` Re: COMMENTS are not being copied in CREATE TABLE LIKE Alex Liapychev <coder.sam@gmail.com>
@ 2026-10-01 21:36 ` Jim Jones <jim.jones@uni-muenster.de>
0 siblings, 0 replies; 37+ messages in thread
From: Jim Jones @ 2026-10-01 21:36 UTC (permalink / raw)
To: Alex Liapychev <coder.sam@gmail.com>; +Cc: tomas@vondra.me, tgl@sss.pgh.pa.us, matheusssilv97 <matheusssilv97@gmail.com>; Chao Li <li.evan.chao@gmail.com>; masao.fujii <masao.fujii@gmail.com>; david.g.johnston <david.g.johnston@gmail.com>; huseyin.d3r <huseyin.d3r@gmail.com>; pgsql-hackers <pgsql-hackers@lists.postgresql.org>; chochoforwork@gmail.com; 44973863@qq.com
Hi Alex
On 01/10/2026 23:13, Alex Liapychev wrote:
> As Tomas and Tom’s comments have highlighted, the underlying issue is the lack of a clear use case.
> A concrete, practical use case would help establish whether those costs and risks are justified at all, and which alternative to concatenation to pick.
Looking from this perspective I agree with you. I was so busy trying to
find out "how to concatenate the comments" that I forgot to ask myself
"why should we concatenate them".
Thanks for the thorough review.
Best, Jim
^ permalink raw reply [nested|flat] 37+ messages in thread
end of thread, other threads:[~2026-10-01 21:36 UTC | newest]
Thread overview: 37+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2026-02-12 10:11 COMMENTS are not being copied in CREATE TABLE LIKE Jim Jones <jim.jones@uni-muenster.de>
2026-02-12 14:36 ` Matheus Alcantara <matheusssilv97@gmail.com>
2026-02-12 15:08 ` Fujii Masao <masao.fujii@gmail.com>
2026-02-12 15:33 ` Jim Jones <jim.jones@uni-muenster.de>
2026-02-12 16:25 ` Matheus Alcantara <matheusssilv97@gmail.com>
2026-02-13 01:29 ` Chao Li <li.evan.chao@gmail.com>
2026-02-13 04:30 ` Tom Lane <tgl@sss.pgh.pa.us>
2026-02-13 09:19 ` Jim Jones <jim.jones@uni-muenster.de>
2026-02-22 08:05 ` Hüseyin Demir <huseyin.d3r@gmail.com>
2026-02-22 12:34 ` Jim Jones <jim.jones@uni-muenster.de>
2026-02-26 07:46 ` Hüseyin Demir <huseyin.d3r@gmail.com>
2026-02-26 10:07 ` Jim Jones <jim.jones@uni-muenster.de>
2026-02-26 13:39 ` Jim Jones <jim.jones@uni-muenster.de>
2026-03-24 18:32 ` Carlos Alves <carlos@alves.inf.br>
2026-03-25 09:21 ` Jim Jones <jim.jones@uni-muenster.de>
2026-03-25 13:59 ` Carlos Alves <carlos@alves.inf.br>
2026-03-26 17:59 ` Carlos Alves <carlos@alves.inf.br>
2026-03-26 18:12 ` Jim Jones <jim.jones@uni-muenster.de>
2026-02-13 04:13 ` Fujii Masao <masao.fujii@gmail.com>
2026-02-13 04:22 ` David G. Johnston <david.g.johnston@gmail.com>
2026-07-18 14:03 ` ZizhuanLiu X-MAN <44973863@qq.com>
2026-07-20 11:52 ` Jim Jones <jim.jones@uni-muenster.de>
2026-07-20 13:24 ` ZizhuanLiu X-MAN <44973863@qq.com>
2026-07-23 11:51 ` Jim Jones <jim.jones@uni-muenster.de>
2026-07-24 02:01 ` ZizhuanLiu X-MAN <44973863@qq.com>
2026-07-24 07:07 ` ZizhuanLiu X-MAN <44973863@qq.com>
2026-09-20 20:28 ` Alex Liapychev <coder.sam@gmail.com>
2026-09-21 13:58 ` Tomas Vondra <tomas@vondra.me>
2026-09-22 19:49 ` Jim Jones <jim.jones@uni-muenster.de>
2026-09-27 00:23 ` Alex Liapychev <coder.sam@gmail.com>
2026-09-30 23:14 ` Alex Liapychev <coder.sam@gmail.com>
2026-10-01 16:49 ` Jim Jones <jim.jones@uni-muenster.de>
2026-10-01 17:25 ` Tomas Vondra <tomas@vondra.me>
2026-10-01 17:45 ` Tom Lane <tgl@sss.pgh.pa.us>
2026-10-01 21:30 ` Jim Jones <jim.jones@uni-muenster.de>
2026-10-01 21:13 ` Alex Liapychev <coder.sam@gmail.com>
2026-10-01 21:36 ` Jim Jones <jim.jones@uni-muenster.de>
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox