Re: Using ALTER TABLE DETACH PARTITION CONCURRENTLY inside a procedure - Mailing list pgsql-general

From Alvaro Herrera
Subject Re: Using ALTER TABLE DETACH PARTITION CONCURRENTLY inside a procedure
Date
Msg-id 202405142045.avyazkmi6kq2@alvherre.pgsql
Whole thread Raw
In response to Using ALTER TABLE DETACH PARTITION CONCURRENTLY inside a procedure  ("Dirschel, Steve" <steve.dirschel@thomsonreuters.com>)
List pgsql-general
On 2024-May-14, Dirschel, Steve wrote:

> But when I try and run the command inside the procedure it throws this error:
> 
> STATE: 25001
> MESSAGE: ALTER TABLE ... DETACH CONCURRENTLY cannot run inside a transaction block
> CONTEXT: SQL statement "alter table t2.test1 detach partition t2.test1_gentime_20240511 concurrently"
> PL/pgSQL function part.partition_maintenance() line 323 at EXECUTE

Yeah, ouch.

> The documentation states:
> 
> CONCURRENTLY cannot be run in a transaction block and is not allowed if the partitioned table contains a default
partition.

Right.

> Is there an option to call that CONCURRENTLY inside a procedure as I describe?

Not at the moment.  The issue is that CONCURRENTLY needs to commit a
transaction internally and start a new one, and to ensure that works
correctly we check that it's being executed as a "top-level command",
which rules out procedures.  It may be possible to relax that
restriction when run inside procedures, given that procedures need
transaction control of their own anyway so we could arrange for the
right things to happen; but this is hypothetical and I don't know that
anybody has attempted to implement that.

-- 
Álvaro Herrera               48°01'N 7°57'E  —  https://www.EnterpriseDB.com/



pgsql-general by date:

Previous
From: Dimitrios Apostolou
Date:
Subject: pg_stat_io clarifications: background worker, writes and reads
Next
From: sud
Date:
Subject: Re: Adding constraints faster