Re: CREATE TABLE .. LIKE copies comments to an unrelated table

From: Salma Elsayed <salmasayed182003(at)gmail(dot)com>
To: Jim Jones <jim(dot)jones(at)uni-muenster(dot)de>
Cc: Junwang Zhao <zhjwpku(at)gmail(dot)com>, PostgreSQL Hackers <pgsql-hackers(at)postgresql(dot)org>, "Koshi Shibagaki (Fujitsu)" <shibagaki(dot)koshi(at)fujitsu(dot)com>
Subject: Re: CREATE TABLE .. LIKE copies comments to an unrelated table
Date: 2026-10-29 15:58:20
Message-ID: CANBEAPEv7+WCfenbcVW_5=XtkwbDuW_iVcAq-XAJBB-U-MzVZg@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Hi Jim,

I tested v1 on master. I could reproduce both cases from the commit
message: the column comment landing on the wrong table and the error
when the other table has no matching constraint.

Without the patch:

postgres=# CREATE TABLE src (a int NOT NULL);
CREATE TABLE
postgres=# COMMENT ON COLUMN src.a IS 'column comment';
COMMENT
postgres=# COMMENT ON CONSTRAINT src_a_not_null ON src IS 'nn comment';
COMMENT
postgres=# CREATE TABLE foo (a int);
CREATE TABLE
postgres=# SET search_path = public, pg_temp;
SET
postgres=# COMMENT ON COLUMN foo.a IS 'important comment';
COMMENT
postgres=# CREATE TEMP TABLE foo (LIKE src INCLUDING COMMENTS);
ERROR: constraint "src_a_not_null" for table "foo" does not exist

With the patch, same tables:

postgres=# SET search_path = public, pg_temp;
SET
postgres=# CREATE TEMP TABLE foo (LIKE src INCLUDING COMMENTS);
CREATE TABLE
postgres=# SELECT n.nspname, relname, col_description(c.oid, 1) AS colcomment
FROM pg_class c JOIN pg_namespace n ON n.oid = c.relnamespace
WHERE c.relname = 'foo';
nspname | relname | colcomment
-----------+---------+-------------------
public | foo | important comment
pg_temp_0 | foo | column comment
(2 rows)

postgres=# SELECT n.nspname, co.conname, obj_description(co.oid,
'pg_constraint') AS concomment
FROM pg_constraint co
JOIN pg_class c ON c.oid = co.conrelid
JOIN pg_namespace n ON n.oid = c.relnamespace
WHERE c.relname = 'foo';
nspname | conname | concomment
-----------+----------------+------------
pg_temp_0 | src_a_not_null | nn comment
(1 row)

public.foo keeps its own comment, and both the column comment and the
NOT NULL constraint comment end up on the temp table with no error.

The test covers the column comment case. Would it be worth also
covering the constraint case from the commit message?

Thanks,
Salma

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Roman Khapov 2026-10-29 16:10:42 Re: Check the fd number against FD_SETSIZE in PQsocketPoll
Previous Message Greg Burd 2026-10-29 15:40:15 Re: UNDO with FILEOPS, and the FLUX table AM