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

From Dirschel, Steve
Subject Using ALTER TABLE DETACH PARTITION CONCURRENTLY inside a procedure
Date
Msg-id BL0PR03MB40015668F50A4DDA59390AE5FAE32@BL0PR03MB4001.namprd03.prod.outlook.com
Whole thread Raw
Responses Re: Using ALTER TABLE DETACH PARTITION CONCURRENTLY inside a procedure
List pgsql-general

We have a custom procedure to add/drop partitions in Postgres.  It has a main FOR LOOP to find tables needing to be processed.  Inside that FOR LOOP there is a BEGIN so if a single table gets an error we catch the error in an exception.  At the end of the END for the FOR LOOP it issues a commit.  So if there are 20 tables to process and the 15th table gets an error it will capture that error, write a message to a log file, and continue processing the remaining tables.

 

We have ran into some locking issues when trying to DETACH a partition where if there is a long running query against the partitioned table it will block the DETACH PARTITION command.  Then what happens app sessions trying to insert into the table get blocked by the session trying to detach the partition and the app gets into a bad state.  In testing I found if I use the CONCURRENTLY clause with DETACH PARTITION the detach partition command can still get blocked by a long running query but that does not block app sessions from inserting into the table.  So this is great.

 

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 DETAIL:  HINT:  CONTEXT: SQL statement "alter table t2.test1 detach partition t2.test1_gentime_20240511 concurrently"

PL/pgSQL function part.partition_maintenance() line 323 at EXECUTE

 

The documentation states:

 

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

 

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

 

Thanks in advance.

 

This e-mail is for the sole use of the intended recipient and contains information that may be privileged and/or confidential. If you are not an intended recipient, please notify the sender by return e-mail and delete this e-mail and any attachments. Certain required legal entity disclosures can be accessed on our website: https://www.thomsonreuters.com/en/resources/disclosures.html

pgsql-general by date:

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