mirror of
https://github.com/postgres/postgres.git
synced 2025-07-05 07:21:24 +03:00
Re-implement EvalPlanQual processing to improve its performance and eliminate
a lot of strange behaviors that occurred in join cases. We now identify the "current" row for every joined relation in UPDATE, DELETE, and SELECT FOR UPDATE/SHARE queries. If an EvalPlanQual recheck is necessary, we jam the appropriate row into each scan node in the rechecking plan, forcing it to emit only that one row. The former behavior could rescan the whole of each joined relation for each recheck, which was terrible for performance, and what's much worse could result in duplicated output tuples. Also, the original implementation of EvalPlanQual could not re-use the recheck execution tree --- it had to go through a full executor init and shutdown for every row to be tested. To avoid this overhead, I've associated a special runtime Param with each LockRows or ModifyTable plan node, and arranged to make every scan node below such a node depend on that Param. Thus, by signaling a change in that Param, the EPQ machinery can just rescan the already-built test plan. This patch also adds a prohibition on set-returning functions in the targetlist of SELECT FOR UPDATE/SHARE. This is needed to avoid the duplicate-output-tuple problem. It seems fairly reasonable since the other restrictions on SELECT FOR UPDATE are meant to ensure that there is a unique correspondence between source tuples and result tuples, which an output SRF destroys as much as anything else does.
This commit is contained in:
29
src/backend/utils/cache/plancache.c
vendored
29
src/backend/utils/cache/plancache.c
vendored
@ -35,7 +35,7 @@
|
||||
* Portions Copyright (c) 1994, Regents of the University of California
|
||||
*
|
||||
* IDENTIFICATION
|
||||
* $PostgreSQL: pgsql/src/backend/utils/cache/plancache.c,v 1.29 2009/10/10 01:43:50 tgl Exp $
|
||||
* $PostgreSQL: pgsql/src/backend/utils/cache/plancache.c,v 1.30 2009/10/26 02:26:41 tgl Exp $
|
||||
*
|
||||
*-------------------------------------------------------------------------
|
||||
*/
|
||||
@ -48,6 +48,8 @@
|
||||
#include "executor/spi.h"
|
||||
#include "nodes/nodeFuncs.h"
|
||||
#include "optimizer/planmain.h"
|
||||
#include "optimizer/prep.h"
|
||||
#include "parser/parsetree.h"
|
||||
#include "storage/lmgr.h"
|
||||
#include "tcop/pquery.h"
|
||||
#include "tcop/tcopprot.h"
|
||||
@ -67,7 +69,6 @@ static void AcquireExecutorLocks(List *stmt_list, bool acquire);
|
||||
static void AcquirePlannerLocks(List *stmt_list, bool acquire);
|
||||
static void ScanQueryForLocks(Query *parsetree, bool acquire);
|
||||
static bool ScanQueryWalker(Node *node, bool *acquire);
|
||||
static bool rowmark_member(List *rowMarks, int rt_index);
|
||||
static bool plan_list_is_transient(List *stmt_list);
|
||||
static void PlanCacheRelCallback(Datum arg, Oid relid);
|
||||
static void PlanCacheFuncCallback(Datum arg, int cacheid, ItemPointer tuplePtr);
|
||||
@ -658,6 +659,7 @@ AcquireExecutorLocks(List *stmt_list, bool acquire)
|
||||
{
|
||||
RangeTblEntry *rte = (RangeTblEntry *) lfirst(lc2);
|
||||
LOCKMODE lockmode;
|
||||
PlanRowMark *rc;
|
||||
|
||||
rt_index++;
|
||||
|
||||
@ -672,7 +674,8 @@ AcquireExecutorLocks(List *stmt_list, bool acquire)
|
||||
*/
|
||||
if (list_member_int(plannedstmt->resultRelations, rt_index))
|
||||
lockmode = RowExclusiveLock;
|
||||
else if (rowmark_member(plannedstmt->rowMarks, rt_index))
|
||||
else if ((rc = get_plan_rowmark(plannedstmt->rowMarks, rt_index)) != NULL &&
|
||||
RowMarkRequiresRowShareLock(rc->markType))
|
||||
lockmode = RowShareLock;
|
||||
else
|
||||
lockmode = AccessShareLock;
|
||||
@ -732,7 +735,7 @@ ScanQueryForLocks(Query *parsetree, bool acquire)
|
||||
/* Acquire or release the appropriate type of lock */
|
||||
if (rt_index == parsetree->resultRelation)
|
||||
lockmode = RowExclusiveLock;
|
||||
else if (rowmark_member(parsetree->rowMarks, rt_index))
|
||||
else if (get_parse_rowmark(parsetree, rt_index) != NULL)
|
||||
lockmode = RowShareLock;
|
||||
else
|
||||
lockmode = AccessShareLock;
|
||||
@ -798,24 +801,6 @@ ScanQueryWalker(Node *node, bool *acquire)
|
||||
(void *) acquire);
|
||||
}
|
||||
|
||||
/*
|
||||
* rowmark_member: check whether an RT index appears in a RowMarkClause list.
|
||||
*/
|
||||
static bool
|
||||
rowmark_member(List *rowMarks, int rt_index)
|
||||
{
|
||||
ListCell *l;
|
||||
|
||||
foreach(l, rowMarks)
|
||||
{
|
||||
RowMarkClause *rc = (RowMarkClause *) lfirst(l);
|
||||
|
||||
if (rc->rti == rt_index)
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/*
|
||||
* plan_list_is_transient: check if any of the plans in the list are transient.
|
||||
*/
|
||||
|
Reference in New Issue
Block a user