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:
Is there an option to call that CONCURRENTLY inside a procedure as I describe?
Thanks in advance. |