mirror of
https://github.com/postgres/postgres.git
synced 2025-11-21 00:42:43 +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:
@@ -16,7 +16,7 @@
|
||||
*
|
||||
*
|
||||
* IDENTIFICATION
|
||||
* $PostgreSQL: pgsql/src/backend/optimizer/prep/prepjointree.c,v 1.67 2009/09/02 17:52:24 tgl Exp $
|
||||
* $PostgreSQL: pgsql/src/backend/optimizer/prep/prepjointree.c,v 1.68 2009/10/26 02:26:35 tgl Exp $
|
||||
*
|
||||
*-------------------------------------------------------------------------
|
||||
*/
|
||||
@@ -628,6 +628,7 @@ pull_up_simple_subquery(PlannerInfo *root, Node *jtnode, RangeTblEntry *rte,
|
||||
subroot->cte_plan_ids = NIL;
|
||||
subroot->eq_classes = NIL;
|
||||
subroot->append_rel_list = NIL;
|
||||
subroot->rowMarks = NIL;
|
||||
subroot->hasRecursion = false;
|
||||
subroot->wt_param_id = -1;
|
||||
subroot->non_recursive_plan = NULL;
|
||||
|
||||
@@ -9,14 +9,15 @@
|
||||
* relation in the correct order. For both UPDATE and DELETE queries,
|
||||
* we need a junk targetlist entry holding the CTID attribute --- the
|
||||
* executor relies on this to find the tuple to be replaced/deleted.
|
||||
* We may also need junk tlist entries for Vars used in the RETURNING list.
|
||||
* We may also need junk tlist entries for Vars used in the RETURNING list
|
||||
* and row ID information needed for EvalPlanQual checking.
|
||||
*
|
||||
*
|
||||
* Portions Copyright (c) 1996-2009, PostgreSQL Global Development Group
|
||||
* Portions Copyright (c) 1994, Regents of the University of California
|
||||
*
|
||||
* IDENTIFICATION
|
||||
* $PostgreSQL: pgsql/src/backend/optimizer/prep/preptlist.c,v 1.97 2009/10/12 18:10:48 tgl Exp $
|
||||
* $PostgreSQL: pgsql/src/backend/optimizer/prep/preptlist.c,v 1.98 2009/10/26 02:26:35 tgl Exp $
|
||||
*
|
||||
*-------------------------------------------------------------------------
|
||||
*/
|
||||
@@ -31,7 +32,6 @@
|
||||
#include "optimizer/subselect.h"
|
||||
#include "optimizer/tlist.h"
|
||||
#include "optimizer/var.h"
|
||||
#include "parser/analyze.h"
|
||||
#include "parser/parsetree.h"
|
||||
#include "parser/parse_coerce.h"
|
||||
#include "utils/rel.h"
|
||||
@@ -54,6 +54,7 @@ preprocess_targetlist(PlannerInfo *root, List *tlist)
|
||||
int result_relation = parse->resultRelation;
|
||||
List *range_table = parse->rtable;
|
||||
CmdType command_type = parse->commandType;
|
||||
ListCell *lc;
|
||||
|
||||
/*
|
||||
* Sanity check: if there is a result relation, it'd better be a real
|
||||
@@ -108,51 +109,47 @@ preprocess_targetlist(PlannerInfo *root, List *tlist)
|
||||
}
|
||||
|
||||
/*
|
||||
* Add TID targets for rels selected FOR UPDATE/SHARE. The executor uses
|
||||
* the TID to know which rows to lock, much as for UPDATE or DELETE.
|
||||
* Add necessary junk columns for rowmarked rels. These values are
|
||||
* needed for locking of rels selected FOR UPDATE/SHARE, and to do
|
||||
* EvalPlanQual rechecking. While we are at it, store these junk attnos
|
||||
* in the PlanRowMark list so that we don't have to redetermine them
|
||||
* at runtime.
|
||||
*/
|
||||
if (parse->rowMarks)
|
||||
foreach(lc, root->rowMarks)
|
||||
{
|
||||
ListCell *l;
|
||||
PlanRowMark *rc = (PlanRowMark *) lfirst(lc);
|
||||
Var *var;
|
||||
char resname[32];
|
||||
TargetEntry *tle;
|
||||
|
||||
/*
|
||||
* We've got trouble if the FOR UPDATE/SHARE appears inside grouping,
|
||||
* since grouping renders a reference to individual tuple CTIDs
|
||||
* invalid. This is also checked at parse time, but that's
|
||||
* insufficient because of rule substitution, query pullup, etc.
|
||||
*/
|
||||
CheckSelectLocking(parse);
|
||||
|
||||
foreach(l, parse->rowMarks)
|
||||
/* child rels should just use the same junk attrs as their parents */
|
||||
if (rc->rti != rc->prti)
|
||||
{
|
||||
RowMarkClause *rc = (RowMarkClause *) lfirst(l);
|
||||
Var *var;
|
||||
char resname[32];
|
||||
TargetEntry *tle;
|
||||
PlanRowMark *prc = get_plan_rowmark(root->rowMarks, rc->prti);
|
||||
|
||||
/* ignore child rels */
|
||||
if (rc->rti != rc->prti)
|
||||
continue;
|
||||
/* parent should have appeared earlier in list */
|
||||
if (prc == NULL || prc->toidAttNo == InvalidAttrNumber)
|
||||
elog(ERROR, "parent PlanRowMark not processed yet");
|
||||
rc->ctidAttNo = prc->ctidAttNo;
|
||||
rc->toidAttNo = prc->toidAttNo;
|
||||
continue;
|
||||
}
|
||||
|
||||
/* we should have an ID for the RowMarkClause */
|
||||
Assert(rc->rowmarkId != 0);
|
||||
|
||||
/* always need the ctid */
|
||||
if (rc->markType != ROW_MARK_COPY)
|
||||
{
|
||||
/* It's a regular table, so fetch its TID */
|
||||
var = makeVar(rc->rti,
|
||||
SelfItemPointerAttributeNumber,
|
||||
TIDOID,
|
||||
-1,
|
||||
0);
|
||||
|
||||
snprintf(resname, sizeof(resname),
|
||||
"ctid%u", rc->rowmarkId);
|
||||
|
||||
snprintf(resname, sizeof(resname), "ctid%u", rc->rti);
|
||||
tle = makeTargetEntry((Expr *) var,
|
||||
list_length(tlist) + 1,
|
||||
pstrdup(resname),
|
||||
true);
|
||||
|
||||
tlist = lappend(tlist, tle);
|
||||
rc->ctidAttNo = tle->resno;
|
||||
|
||||
/* if parent of inheritance tree, need the tableoid too */
|
||||
if (rc->isParent)
|
||||
@@ -162,18 +159,31 @@ preprocess_targetlist(PlannerInfo *root, List *tlist)
|
||||
OIDOID,
|
||||
-1,
|
||||
0);
|
||||
|
||||
snprintf(resname, sizeof(resname),
|
||||
"tableoid%u", rc->rowmarkId);
|
||||
|
||||
snprintf(resname, sizeof(resname), "tableoid%u", rc->rti);
|
||||
tle = makeTargetEntry((Expr *) var,
|
||||
list_length(tlist) + 1,
|
||||
pstrdup(resname),
|
||||
true);
|
||||
|
||||
tlist = lappend(tlist, tle);
|
||||
rc->toidAttNo = tle->resno;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
/* Not a table, so we need the whole row as a junk var */
|
||||
var = makeVar(rc->rti,
|
||||
InvalidAttrNumber,
|
||||
RECORDOID,
|
||||
-1,
|
||||
0);
|
||||
snprintf(resname, sizeof(resname), "wholerow%u", rc->rti);
|
||||
tle = makeTargetEntry((Expr *) var,
|
||||
list_length(tlist) + 1,
|
||||
pstrdup(resname),
|
||||
true);
|
||||
tlist = lappend(tlist, tle);
|
||||
rc->wholeAttNo = tle->resno;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -394,3 +404,24 @@ expand_targetlist(List *tlist, int command_type,
|
||||
|
||||
return new_tlist;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* Locate PlanRowMark for given RT index, or return NULL if none
|
||||
*
|
||||
* This probably ought to be elsewhere, but there's no very good place
|
||||
*/
|
||||
PlanRowMark *
|
||||
get_plan_rowmark(List *rowmarks, Index rtindex)
|
||||
{
|
||||
ListCell *l;
|
||||
|
||||
foreach(l, rowmarks)
|
||||
{
|
||||
PlanRowMark *rc = (PlanRowMark *) lfirst(l);
|
||||
|
||||
if (rc->rti == rtindex)
|
||||
return rc;
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
@@ -22,7 +22,7 @@
|
||||
*
|
||||
*
|
||||
* IDENTIFICATION
|
||||
* $PostgreSQL: pgsql/src/backend/optimizer/prep/prepunion.c,v 1.177 2009/10/23 05:24:52 petere Exp $
|
||||
* $PostgreSQL: pgsql/src/backend/optimizer/prep/prepunion.c,v 1.178 2009/10/26 02:26:35 tgl Exp $
|
||||
*
|
||||
*-------------------------------------------------------------------------
|
||||
*/
|
||||
@@ -248,7 +248,7 @@ recurse_set_operations(Node *setOp, PlannerInfo *root,
|
||||
rtr->rtindex,
|
||||
subplan,
|
||||
subroot->parse->rtable,
|
||||
subroot->parse->rowMarks);
|
||||
subroot->rowMarks);
|
||||
|
||||
/*
|
||||
* We don't bother to determine the subquery's output ordering since
|
||||
@@ -1133,7 +1133,7 @@ expand_inherited_rtentry(PlannerInfo *root, RangeTblEntry *rte, Index rti)
|
||||
{
|
||||
Query *parse = root->parse;
|
||||
Oid parentOID;
|
||||
RowMarkClause *oldrc;
|
||||
PlanRowMark *oldrc;
|
||||
Relation oldrelation;
|
||||
LOCKMODE lockmode;
|
||||
List *inhOIDs;
|
||||
@@ -1171,10 +1171,10 @@ expand_inherited_rtentry(PlannerInfo *root, RangeTblEntry *rte, Index rti)
|
||||
* the lock, leading to possible deadlocks. (This code should match the
|
||||
* parser and rewriter.)
|
||||
*/
|
||||
oldrc = get_rowmark(parse, rti);
|
||||
oldrc = get_plan_rowmark(root->rowMarks, rti);
|
||||
if (rti == parse->resultRelation)
|
||||
lockmode = RowExclusiveLock;
|
||||
else if (oldrc)
|
||||
else if (oldrc && RowMarkRequiresRowShareLock(oldrc->markType))
|
||||
lockmode = RowShareLock;
|
||||
else
|
||||
lockmode = AccessShareLock;
|
||||
@@ -1196,7 +1196,7 @@ expand_inherited_rtentry(PlannerInfo *root, RangeTblEntry *rte, Index rti)
|
||||
|
||||
/*
|
||||
* If parent relation is selected FOR UPDATE/SHARE, we need to mark its
|
||||
* RowMarkClause as isParent = true, and generate a new RowMarkClause for
|
||||
* PlanRowMark as isParent = true, and generate a new PlanRowMark for
|
||||
* each child.
|
||||
*/
|
||||
if (oldrc)
|
||||
@@ -1275,21 +1275,23 @@ expand_inherited_rtentry(PlannerInfo *root, RangeTblEntry *rte, Index rti)
|
||||
}
|
||||
|
||||
/*
|
||||
* Build a RowMarkClause if parent is marked FOR UPDATE/SHARE.
|
||||
* Build a PlanRowMark if parent is marked FOR UPDATE/SHARE.
|
||||
*/
|
||||
if (oldrc)
|
||||
{
|
||||
RowMarkClause *newrc = makeNode(RowMarkClause);
|
||||
PlanRowMark *newrc = makeNode(PlanRowMark);
|
||||
|
||||
newrc->rti = childRTindex;
|
||||
newrc->prti = rti;
|
||||
/* children use the same rowmarkId as their parent */
|
||||
newrc->rowmarkId = oldrc->rowmarkId;
|
||||
newrc->forUpdate = oldrc->forUpdate;
|
||||
newrc->markType = oldrc->markType;
|
||||
newrc->noWait = oldrc->noWait;
|
||||
newrc->isParent = false;
|
||||
/* junk attrs for children are not identified yet */
|
||||
newrc->ctidAttNo = InvalidAttrNumber;
|
||||
newrc->toidAttNo = InvalidAttrNumber;
|
||||
newrc->wholeAttNo = InvalidAttrNumber;
|
||||
|
||||
parse->rowMarks = lappend(parse->rowMarks, newrc);
|
||||
root->rowMarks = lappend(root->rowMarks, newrc);
|
||||
}
|
||||
|
||||
/* Close child relations, but keep locks */
|
||||
|
||||
Reference in New Issue
Block a user