1
0
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:
Tom Lane
2009-10-26 02:26:45 +00:00
parent 76d8883c8e
commit 9f2ee8f287
50 changed files with 1547 additions and 1018 deletions

View File

@@ -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;
}