CREATE TABLE .. LIKE copies comments to an unrelated table

Поиск
Список
Период
Сортировка
Искать

CREATE TABLE .. LIKE copies comments to an unrelated table

От:
Jim Jones <jim.jones@uni-muenster.de>
Дата:
Hi

While working on another patch I found that CREATE TABLE ... LIKE
INCLUDING COMMENTS copies the comments to the wrong table when the
target is a temp table and a table with the same name exists in a schema
listed before pg_temp in search_path.

Example:

psql (18.3 (Debian 18.3-1.pgdg13+1))
Type "help" for help.

db=> CREATE TABLE src (a int);
CREATE TABLE
db=> COMMENT ON COLUMN src.a IS 'col comment';
COMMENT
db=> CREATE TABLE foo (a int); -- unrelated table
CREATE TABLE
db=> COMMENT ON COLUMN foo.a IS 'important comment';
COMMENT
db=> SET search_path = public, pg_temp;
SET
db=> CREATE TEMP TABLE foo (LIKE src INCLUDING COMMENTS);
CREATE TABLE
db=> 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     | col comment
 pg_temp_24 | foo     |
(2 rows)

The unrelated public.foo got the column comment and the created
temporary table got none -- public.foo.a comment was overwritten. Is
this the expected behaviour?

Setting the schema to pg_temp in case of RELPERSISTENCE_TEMP in
transformCreateStmt seems to do the trick (but I didn't dive too deep in
the code just yet):

-       if (stmt->relation->schemaname == NULL
-               && stmt->relation->relpersistence != RELPERSISTENCE_TEMP)
-               stmt->relation->schemaname =
get_namespace_name(namespaceid);
+       if (stmt->relation->schemaname == NULL)
+       {
+               if (stmt->relation->relpersistence == RELPERSISTENCE_TEMP)
+                       stmt->relation->schemaname = pstrdup("pg_temp");
+               else
+                       stmt->relation->schemaname =
get_namespace_name(namespaceid);
+       }

With my changes in HEAD:

db=> CREATE TABLE src (a int);
CREATE TABLE
db=> COMMENT ON COLUMN src.a IS 'col comment';
COMMENT
db=> CREATE TABLE foo (a int); -- unrelated table
CREATE TABLE
db=> COMMENT ON COLUMN foo.a IS 'important comment';
COMMENT
db=> SET search_path = public, pg_temp;
SET
db=> CREATE TEMP TABLE foo (LIKE src INCLUDING COMMENTS);
CREATE TABLE
db=> 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_92 | foo     | col comment
(2 rows)


WDYT?

Best, Jim



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

От:
Junwang Zhao <zhjwpku@gmail.com>
Дата:
On Thu, Oct 1, 2026 at 10:35 PM Jim Jones  wrote:
>
> Hi
>
> While working on another patch I found that CREATE TABLE ... LIKE
> INCLUDING COMMENTS copies the comments to the wrong table when the
> target is a temp table and a table with the same name exists in a schema
> listed before pg_temp in search_path.
>
> Example:
>
> psql (18.3 (Debian 18.3-1.pgdg13+1))
> Type "help" for help.
>
> db=> CREATE TABLE src (a int);
> CREATE TABLE
> db=> COMMENT ON COLUMN src.a IS 'col comment';
> COMMENT
> db=> CREATE TABLE foo (a int); -- unrelated table
> CREATE TABLE
> db=> COMMENT ON COLUMN foo.a IS 'important comment';
> COMMENT
> db=> SET search_path = public, pg_temp;
> SET
> db=> CREATE TEMP TABLE foo (LIKE src INCLUDING COMMENTS);
> CREATE TABLE
> db=> 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     | col comment
>  pg_temp_24 | foo     |
> (2 rows)
>
> The unrelated public.foo got the column comment and the created
> temporary table got none -- public.foo.a comment was overwritten. Is
> this the expected behaviour?

I think this is a bug, and I can reproduce it on master.

>
> Setting the schema to pg_temp in case of RELPERSISTENCE_TEMP in
> transformCreateStmt seems to do the trick (but I didn't dive too deep in
> the code just yet):
>
> -       if (stmt->relation->schemaname == NULL
> -               && stmt->relation->relpersistence != RELPERSISTENCE_TEMP)
> -               stmt->relation->schemaname =
> get_namespace_name(namespaceid);
> +       if (stmt->relation->schemaname == NULL)
> +       {
> +               if (stmt->relation->relpersistence == RELPERSISTENCE_TEMP)
> +                       stmt->relation->schemaname = pstrdup("pg_temp");
> +               else
> +                       stmt->relation->schemaname =
> get_namespace_name(namespaceid);
> +       }

The preceding RangeVarGetAndCheckCreationNamespace has already
initialized the temporary namespace and returned its OID, so simplified it
to:

if (stmt->relation->schemaname == NULL)
    stmt->relation->schemaname = get_namespace_name(namespaceid);

should work, I did a quick test to verify this.

>
> With my changes in HEAD:
>
> db=> CREATE TABLE src (a int);
> CREATE TABLE
> db=> COMMENT ON COLUMN src.a IS 'col comment';
> COMMENT
> db=> CREATE TABLE foo (a int); -- unrelated table
> CREATE TABLE
> db=> COMMENT ON COLUMN foo.a IS 'important comment';
> COMMENT
> db=> SET search_path = public, pg_temp;
> SET
> db=> CREATE TEMP TABLE foo (LIKE src INCLUDING COMMENTS);
> CREATE TABLE
> db=> 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_92 | foo     | col comment
> (2 rows)
>
>
> WDYT?
>
> Best, Jim
>
>
>


-- 
Regards
Junwang Zhao


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

От:
Jim Jones <jim.jones@uni-muenster.de>
Дата:
Hi Junwang

On 01/10/2026 17:31, Junwang Zhao wrote:
> The preceding RangeVarGetAndCheckCreationNamespace has already
> initialized the temporary namespace and returned its OID, so simplified it
> to:
> 
> if (stmt->relation->schemaname == NULL)
>     stmt->relation->schemaname = get_namespace_name(namespaceid);
> 
> should work, I did a quick test to verify this.

+1

Thanks! I will write some tests and prepare a patch.

Best, Jim



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

От:
Jim Jones <jim.jones@uni-muenster.de>
Дата:
Вложения:
On 01/10/2026 18:02, Jim Jones wrote:
> Thanks! I will write some tests and prepare a patch.

PFA this fix and some tests in create_table_like.sql.

Best, Jim

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

От:
"Koshi Shibagaki (Fujitsu)" <shibagaki.koshi@fujitsu.com>
Дата:
Hi,

While reviewing v1, I noticed a behavior change when a temporary schema
is renamed concurrently. Would it make sense to qualify temporary table
names with pg_temp rather than the actual schema name?

The following sequence reproduces it with two connections to the same
database:

    -- A
    CREATE TABLE public.src (a int);
    CREATE TEMP TABLE sentinel (a int);
    SELECT nspname FROM pg_namespace WHERE oid = pg_my_temp_schema();

    -- B
    BEGIN;
    LOCK TABLE public.src IN ACCESS EXCLUSIVE MODE;

    -- A: wait until this statement is blocked before continuing in B.
    CREATE TEMP TABLE dst (LIKE public.src);

    -- B: replace pg_temp_N with the name returned by A's SELECT.
    ALTER SCHEMA pg_temp_N RENAME TO renamed_temp_for_review;
    COMMIT;

With v1, A fails with "schema \"pg_temp_N\" does not exist", because the
schema name saved by transformCreateStmt() becomes stale while waiting
for the lock. The same sequence succeeds without the patch.

This requires a superuser, and permanent tables can already encounter 
the same error when their destination schema is renamed concurrently. 
Therefore, if the intention is to match the name-based behavior of permanent 
tables, I think this is reasonable.

Or would it be preferable to use pg_temp for temporary tables, so that 
the generated COMMENT commands are schema-qualified while preserving 
the previous behavior (i.e., the behavior of current master) in this case?

If we choose pg_temp, the additional change on top of v1 would be:
Permanent tables would still be qualified with the actual schema name.

diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c
--- a/src/backend/parser/parse_utilcmd.c
+++ b/src/backend/parser/parse_utilcmd.c
@@ -229,7 +229,9 @@
  * COMMENTS.
  */
  if (stmt->relation->schemaname == NULL)
- stmt->relation->schemaname = get_namespace_name(namespaceid);
+ stmt->relation->schemaname =
+ (stmt->relation->relpersistence == RELPERSISTENCE_TEMP) ?
+ pstrdup("pg_temp") : get_namespace_name(namespaceid);
 
  /* Set up CreateStmtContext */


Regards,

Koshi Shibagaki
FUJITSU LIMITED
https://www.fujitsu.com/

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

От:
Jim Jones <jim.jones@uni-muenster.de>
Дата:
Hi Koshi

On 13/10/2026 13:57, Koshi Shibagaki (Fujitsu) wrote:
> This requires a superuser, and permanent tables can already encounter 
> the same error when their destination schema is renamed concurrently. 
> Therefore, if the intention is to match the name-based behavior of permanent 
> tables, I think this is reasonable.

I see your point there. However, since this requires a superuser, I
don't think it needs to be fixed -- a superuser can do a lot of nasty
things. Trying to anticipate a superuser renaming a temp schema
mid-session is IMHO not something we should worry about. Please let me
know if I'm missing something here.

Thanks for the review. Much appreciated!

Best, Jim



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

От:
"Koshi Shibagaki (Fujitsu)" <shibagaki.koshi@fujitsu.com>
Дата:
Hi Jim,

> I see your point there. However, since this requires a superuser, I
> don't think it needs to be fixed -- a superuser can do a lot of nasty
> things. Trying to anticipate a superuser renaming a temp schema
> mid-session is IMHO not something we should worry about. Please let me
> know if I'm missing something here.

That makes sense. I agree that we don't need to handle
a superuser renaming a temporary schema mid-session.

Thanks for the clarification!

Best,
Koshi Shibagaki
FUJITSU LIMITED
https://www.fujitsu.com/


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

От:
Salma Elsayed <salmasayed182003@gmail.com>
Дата:
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


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

От:
Jim Jones <jim.jones@uni-muenster.de>
Дата:
Вложения:
On 29/10/2026 16:58, Salma Elsayed wrote:
> The test covers the column comment case. Would it be worth also
> covering the constraint case from the commit message?

Done.
v2 adds the missing tests.

Thanks for the review. Much appreciated!

Best, Jim

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

От:
João Rito <joaovictor.rito@gmail.com>
Дата:
Hi Jim,

I tested v2 (CF entry 7379). Thank you for the patch. Here are my results.

## Setup

- I used the commitfest bot branch cf/7379, commit
b4fc3ecd05530dac85f0bb832e4386efa07f2696 ("[CF 7379] v2"). It contains
v2-0001 only.
- I built it on Linux under WSL with: --enable-cassert --enable-debug
--enable-tap-tests --without-icu.
- The build gave 0 compiler warnings.
- For comparison I used an unpatched master build (PostgreSQL
20devel). I ran the same SQL script on a fresh temporary cluster for
each build.

## Tests that passed

- make check: "All 243 tests passed", and create_table_like was ok.
- Your case from the first mail (column comment, search_path = public,
pg_temp): on master, public.foo got "col comment" and the temp foo got
none. With v2, public.foo keeps "important comment" and the temp foo
gets "col comment".
- Salma's NOT NULL constraint case: on master, CREATE TEMP TABLE foo2
(LIKE src2 INCLUDING COMMENTS) fails with: constraint
"src2_a_not_null" for table "foo2" does not exist. With v2 it
succeeds, and the column comment and the constraint comment ("nn
comment") are on the temp table. The permanent foo2 keeps its own
comment.
- LIKE ... INCLUDING ALL with a same-named permanent table (it has its
own primary key index comment and table comment): on master it fails
with: constraint "src3_b_chk" for table "foo3" does not exist. With v2
it succeeds. The temp table gets the copied index comment ("src pk
index") and the CHECK constraint comment ("src check"). The permanent
foo3 keeps "unrelated table" and "unrelated index".
- Default search_path (pg_temp implicitly first, no same-named table):
the copied CHECK constraint comment is the same on master and with v2.
- Permanent tables: I created a permanent dup5 in schema s5
(search_path = s5, public, pg_temp) while public.dup5 and pg_temp.dup5
also existed. The column comment was copied to s5.dup5 (I did not
check the constraint comment there), and public.dup5 kept its own
comment. The result was the same on master and with v2.
- A temp source table with LIKE ... INCLUDING COMMENTS and CREATE TEMP
TABLE IF NOT EXISTS ... ON COMMIT DROP, run twice in one transaction:
the first run copied the column and constraint comments, and the
second run only gave the "already exists, skipping" notice. The result
was the same on master and with v2.
- A temp table with INHERITS, and a temp partitioned table with a
partition, were created without errors with v2.
- A grep of the patched cluster's server log for ERROR, WARNING, TRAP
and PANIC found nothing. The master log had only the two errors
described above.

## Manual tests

The SQL script and the complete psql output for master and v2 are in
the log I kept (I can send it if useful). The cases are the ones
listed above.

## Not tested

- The concurrent rename of a temp schema discussed with Koshi (two
sessions, superuser). I did not run it. I agree with Jim's reasoning
that it does not need handling, but I did not verify the behaviour
myself.
- The TAP tests (only make check was run), other platforms, Windows,
and builds with ICU.
- Other LIKE options not listed above (for example INCLUDING
STATISTICS, GENERATED, IDENTITY), and temp tables created inside
functions or by other sessions.
- I did not measure performance, and I did not check other callers of
transformCreateStmt().

Overall, v2 behaves as described in the commit message in all the
cases I tried, and I found no problems.

Best regards,
João Rito


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

От:
Jim Jones <jim.jones@uni-muenster.de>
Дата:
Hi Joao

On 12/11/2026 03:34, João Rito wrote:
> Overall, v2 behaves as described in the commit message in all the
> cases I tried, and I found no problems.

Thanks for the thorough review!

Best, Jim



FAQ