Re: Add SPLIT PARTITION/MERGE PARTITIONS commands

From: jian he <jian(dot)universality(at)gmail(dot)com>
To: Dmitry Koval <d(dot)koval(at)postgrespro(dot)ru>
Cc: pgsql-hackers(at)lists(dot)postgresql(dot)org
Subject: Re: Add SPLIT PARTITION/MERGE PARTITIONS commands
Date: 2025-06-16 05:52:45
Message-ID: CACJufxHakPGgUHfe4uThY2u_Hzq3YOP9m4B3gjZUTUm2mbVryA@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

hi.

static void checkPartition(Relation rel, Oid partRelOid)
function name checkPartition is not ideal, maybe we can change it to
CheckPartitionForMerge or MergePartitionCheck.
(attached v45-002 is error message refactoring for checkPartition,
I didn't change the name though.)

For the command:
ALTER TABLE pk MERGE PARTITIONS (pk_1, pk_2) INTO pk_1;
Acquiring AccessExclusiveLock on the partitions to be merged
(pk_1, pk_2) during transformPartitionCmdForMerge should be fine, IMHO.
Here’s why:

* The merged partitions (pk_1, pk_2) will be dropped in the end, so acquiring
AccessExclusiveLock is unavoidable for ALTER TABLE MERGE PARTITIONS.
* Taking an AccessShareLock first, then later acquiring AccessExclusiveLock
in ATExecMergePartitions unnecessarily wastes resources.
(acquire two locks, one stronger should be enough)
* Acquiring AccessExclusiveLock first helps avoid potential anomalies
caused by concurrent operations.

The attached patch refactors transformPartitionCmdForMerge and
ATExecMergePartitions based on the idea of acquiring AccessExclusiveLock on the
to be merged partitions during transformPartitionCmdForMerge

+ * Callback allows caller to check permissions or acquire additional locks
+ * prior to grabbing the relation lock.
Please see the above comments in RangeVarGetRelidExtended.

+ /*
+ * Search DEFAULT partition in the list. Lock partitions before
+ * calculating the boundary for resulting partition.
+ */
+ partOid = RangeVarGetRelid(name, AccessShareLock, false);
so the above transformPartitionCmdForMerge does not check if the
currently user have permission
or not, directly take a lock on RangeVar, name, which is a bug, we should
first do permission check then acquire a lock.

Attachment Content-Type Size
v45-0001-refactor-ATExecMergePartitions-transformPartitionCmdF.no-cfbot application/octet-stream 9.3 KB
v45-0002-error-message-refactoring.no-cfbot application/octet-stream 7.2 KB

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Michael Paquier 2025-06-16 06:49:18 relrewrite not documented at the top of heap_create_with_catalog()
Previous Message Oleg Tselebrovskiy 2025-06-16 04:23:20 Re: Cluster.pm psql() undefined $$stderr