Re: CREATE TABLE LIKE INCLUDING POLICIES

From: Zsolt Parragi <zsolt(dot)parragi(at)percona(dot)com>
To: jian he <jian(dot)universality(at)gmail(dot)com>
Cc: PostgreSQL-development <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: CREATE TABLE LIKE INCLUDING POLICIES
Date: 2026-01-22 06:23:20
Message-ID: CAN4CZFOLJ5hvMbHiNPY3J9Emv19h1FV27LoeLZzWxFYMudh96w@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

> Actually, this extra parentheses is optional.

Thanks for the links, I thought this is a mandatory convention in PG.

> I noticed that the above RelationGetIndexList does not list_free
> parent_indexes as well.
> It should be ok not not call list_free polrels too, because as the memory
> allocation here is in portal context which should be reset by end of the current
> statement execution.

I mainly suggested that because here PolicyGetRelations is inside a
loop, so it allocates multiple lists, not just one per statement. It
might be still a low amount of memory, so maybe its good as is.

> how about ...

that sounds good to me.

> do you mean
> https://www.postgresql.org/docs/current/sql-createtableas.html
> to also copy RLS policies.

No, sorry, I meant to write CREATE TABLE LIKE there. What I meant is
that it might be better to enable the policies if they are enabled on
the parent table, or at least to have an option to do this, as this
seems easy to overlook (to assume that if it INCLUDING POLICIES, that
includes enabling them).

Especially because in most RLS examples/introduction, you first run
ALTER TABLE ENABLE ROW LEVEL SECURITY, and then add policies with
CREATE POLICY after that.

> if so, seems not doable, for example,
>
> create table x as select from tenk1, tenk2 limit 2;
>
> Should it copy all RLS policies from tenk1 and tenk2 to the new tables?

Now that I think about it, it still would be a nice option for simple
cases where people just want to duplicate a table

create table including policies table_copy as select * from table;

But that's definitely a different topic.

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Zsolt Parragi 2026-01-22 06:32:20 Re: Use correct collation in pg_trgm
Previous Message Peter Smith 2026-01-22 06:14:54 Re: DOCS - "\d mytable" also shows any publications that publish mytable