Delay lock acquisition for partitions until we route a tuple to them.

Instead of locking all partitions to which we might route a tuple at
executor startup, just lock them as we use them.  In some cases such a
partition might get locked at executor startup anyway because it
appears in the query's range table for some other reason, but in other
cases this is a bit savings.

This changes the order in which partitions are locked in some cases,
which might conceivably create deadlock hazards that don't exist
today, but per discussion, it seems like such cases should be rare
enough that we can neglect them in favor of improving performance.

David Rowley, reviewed and tested by Tomas Vondra, Sho Kato, John
Naylor, Tom Lane, and me.

Discussion: http://postgr.es/m/CAKJS1f-=FnMqmQP6qitkD+xEddxw22ySLP-0xFk3JAqUX2yfMw@mail.gmail.com
This commit is contained in:
Robert Haas 2019-02-21 11:24:40 -05:00
parent 7aa00d2464
commit 9eefba181f
1 changed files with 16 additions and 19 deletions

View File

@ -191,9 +191,6 @@ static void find_matching_subplans_recurse(PartitionPruningData *prunedata,
* tuple routing for partitioned tables, encapsulates it in
* PartitionTupleRouting, and returns it.
*
* Note that all the relations in the partition tree are locked using the
* RowExclusiveLock mode upon return from this function.
*
* Callers must use the returned PartitionTupleRouting during calls to
* ExecFindPartition(). The actual ResultRelInfo for a partition is only
* allocated when the partition is found for the first time.
@ -208,9 +205,6 @@ ExecSetupPartitionTupleRouting(ModifyTableState *mtstate, Relation rel)
PartitionTupleRouting *proute;
ModifyTable *node = mtstate ? (ModifyTable *) mtstate->ps.plan : NULL;
/* Lock all the partitions. */
(void) find_all_inheritors(RelationGetRelid(rel), RowExclusiveLock, NULL);
/*
* Here we attempt to expend as little effort as possible in setting up
* the PartitionTupleRouting. Each partition's ResultRelInfo is built on
@ -487,8 +481,9 @@ ExecHashSubPlanResultRelsByOid(ModifyTableState *mtstate,
/*
* ExecInitPartitionInfo
* Initialize ResultRelInfo and other information for a partition
* and store it in the next empty slot in the proute->partitions array.
* Lock the partition and initialize ResultRelInfo. Also setup other
* information for the partition and store it in the next empty slot in
* the proute->partitions array.
*
* Returns the ResultRelInfo
*/
@ -510,11 +505,7 @@ ExecInitPartitionInfo(ModifyTableState *mtstate, EState *estate,
oldcxt = MemoryContextSwitchTo(proute->memcxt);
/*
* We locked all the partitions in ExecSetupPartitionTupleRouting
* including the leaf partitions.
*/
partrel = table_open(dispatch->partdesc->oids[partidx], NoLock);
partrel = table_open(dispatch->partdesc->oids[partidx], RowExclusiveLock);
leaf_part_rri = makeNode(ResultRelInfo);
InitResultRelInfo(leaf_part_rri,
@ -964,11 +955,12 @@ ExecInitRoutingInfo(ModifyTableState *mtstate,
/*
* ExecInitPartitionDispatchInfo
* Initialize PartitionDispatch for a partitioned table and store it in
* the next available slot in the proute->partition_dispatch_info array.
* Also, record the index into this array in the parent_pd->indexes[]
* array in the partidx element so that we can properly retrieve the
* newly created PartitionDispatch later.
* Lock the partitioned table (if not locked already) and initialize
* PartitionDispatch for a partitioned table and store it in the next
* available slot in the proute->partition_dispatch_info array. Also,
* record the index into this array in the parent_pd->indexes[] array in
* the partidx element so that we can properly retrieve the newly created
* PartitionDispatch later.
*/
static PartitionDispatch
ExecInitPartitionDispatchInfo(PartitionTupleRouting *proute, Oid partoid,
@ -982,8 +974,13 @@ ExecInitPartitionDispatchInfo(PartitionTupleRouting *proute, Oid partoid,
oldcxt = MemoryContextSwitchTo(proute->memcxt);
/*
* Only sub-partitioned tables need to be locked here. The root
* partitioned table will already have been locked as it's referenced in
* the query's rtable.
*/
if (partoid != RelationGetRelid(proute->partition_root))
rel = table_open(partoid, NoLock);
rel = table_open(partoid, RowExclusiveLock);
else
rel = proute->partition_root;
partdesc = RelationGetPartitionDesc(rel);