Re: Add CREATE SCHEMA ... LIKE support

From: Jim Jones <jim(dot)jones(at)uni-muenster(dot)de>
To: Matheus Alcantara <matheusssilv97(at)gmail(dot)com>, Marcos Pegoraro <marcos(at)f10(dot)com(dot)br>
Cc: "David G(dot) Johnston" <david(dot)g(dot)johnston(at)gmail(dot)com>, "pgsql-hackers(at)lists(dot)postgresql(dot)org" <pgsql-hackers(at)lists(dot)postgresql(dot)org>
Subject: Re: Add CREATE SCHEMA ... LIKE support
Date: 2026-02-10 10:09:33
Message-ID: c8af4c1c-056a-4653-bd1b-77f0374d1729@uni-muenster.de
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On 09/02/2026 23:39, Matheus Alcantara wrote:
> So here is V2 with some documentation changes and also with the index
> name not being preserved issue that Marcos have mentioned earlier fixed.

Thanks for the patch!

One observation:

In a scenario where a parent table and the partitioned tables live in
different schemas, creating a schema based on the schema that contains
only the partitions arguably generates useless tables.

postgres=# CREATE SCHEMA s1;
CREATE TABLE s1.m (
city_id int NOT NULL,
logdate date NOT NULL
) PARTITION BY RANGE (logdate);
CREATE SCHEMA
CREATE TABLE

Partitions are in s2:

postgres=# CREATE SCHEMA s2;
CREATE TABLE s2.p1 PARTITION OF s1.m
FOR VALUES FROM ('2006-02-01') TO ('2006-03-01');
CREATE TABLE s2.p2 PARTITION OF s1.m
FOR VALUES FROM ('2006-03-01') TO ('2006-04-01');
CREATE SCHEMA
CREATE TABLE
CREATE TABLE

postgres=# CREATE SCHEMA s3 LIKE s2 INCLUDING ALL;
CREATE SCHEMA
postgres=# \d s3.p1
Table "s3.p1"
Column | Type | Collation | Nullable | Default
---------+---------+-----------+----------+---------
city_id | integer | | not null |
logdate | date | | not null |

postgres=# \d s2.p1
Table "s2.p1"
Column | Type | Collation | Nullable | Default
---------+---------+-----------+----------+---------
city_id | integer | | not null |
logdate | date | | not null |
Partition of: s1.m FOR VALUES FROM ('2006-02-01') TO ('2006-03-01')

I'm not saying it's wrong, but perhaps you should consider ERROR/WARN if
trying to copy a schema with "orphan" partitions

The same applies for creating schema that contains only the parent table

postgres=# CREATE SCHEMA s4 LIKE s1 INCLUDING ALL;
CREATE SCHEMA
postgres=# \d s4.m
Table "s4.m"
Column | Type | Collation | Nullable | Default
---------+---------+-----------+----------+---------
city_id | integer | | not null |
logdate | date | | not null |

postgres=# \d s1.m
Partitioned table "s1.m"
Column | Type | Collation | Nullable | Default
---------+---------+-----------+----------+---------
city_id | integer | | not null |
logdate | date | | not null |
Partition key: RANGE (logdate)
Number of partitions: 2 (Use \d+ to list them.)

Even if parent and children live in the same schema, they become
detached in the new copy -- I'd argue that this one is a bug.

postgres=# CREATE SCHEMA s1;
CREATE TABLE s1.m (
city_id int NOT NULL,
logdate date NOT NULL
) PARTITION BY RANGE (logdate);
CREATE SCHEMA
CREATE TABLE

postgres=# CREATE TABLE s1.p1 PARTITION OF s1.m
FOR VALUES FROM ('2006-02-01') TO ('2006-03-01');
CREATE TABLE s1.p2 PARTITION OF s1.m
FOR VALUES FROM ('2006-03-01') TO ('2006-04-01');
CREATE TABLE
CREATE TABLE

postgres=# CREATE SCHEMA s2 LIKE s1 INCLUDING ALL;
CREATE SCHEMA

postgres=# \d s1.m
Partitioned table "s1.m"
Column | Type | Collation | Nullable | Default
---------+---------+-----------+----------+---------
city_id | integer | | not null |
logdate | date | | not null |
Partition key: RANGE (logdate)
Number of partitions: 2 (Use \d+ to list them.)

postgres=# \d s2.m
Table "s2.m"
Column | Type | Collation | Nullable | Default
---------+---------+-----------+----------+---------
city_id | integer | | not null |
logdate | date | | not null |

postgres=# \d s1.p1
Table "s1.p1"
Column | Type | Collation | Nullable | Default
---------+---------+-----------+----------+---------
city_id | integer | | not null |
logdate | date | | not null |
Partition of: s1.m FOR VALUES FROM ('2006-02-01') TO ('2006-03-01')

postgres=# \d s2.p1
Table "s2.p1"
Column | Type | Collation | Nullable | Default
---------+---------+-----------+----------+---------
city_id | integer | | not null |
logdate | date | | not null |

Comments are also being ignored, but I guess it was already mentioned
upthread:

postgres=# \dt+ s2.t
List of tables
-[ RECORD 1 ]-+----------
Schema | s2
Name | t
Type | table
Owner | jim
Persistence | permanent
Access method | heap
Size | 0 bytes
Description |

postgres=# \dt+ s1.t
List of tables
-[ RECORD 1 ]-+----------
Schema | s1
Name | t
Type | table
Owner | jim
Persistence | permanent
Access method | heap
Size | 0 bytes
Description | foo

Thanks!

Best, Jim

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Álvaro Herrera 2026-02-10 10:26:55 Re: Miscellaneous message fixes
Previous Message Álvaro Herrera 2026-02-10 10:07:48 Re: Remove "struct" markers from varlena, varatt_external and varatt_indirect