1996-07-09 08:22:35 +02:00
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
*
|
1999-02-14 00:22:53 +01:00
|
|
|
* rewriteHandler.c
|
2001-06-13 20:56:30 +02:00
|
|
|
* Primary module of query rewriter.
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
2009-01-01 18:24:05 +01:00
|
|
|
* Portions Copyright (c) 1996-2009, PostgreSQL Global Development Group
|
2000-01-26 06:58:53 +01:00
|
|
|
* Portions Copyright (c) 1994, Regents of the University of California
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
|
|
|
* IDENTIFICATION
|
2009-10-27 18:11:18 +01:00
|
|
|
* $PostgreSQL: pgsql/src/backend/rewrite/rewriteHandler.c,v 1.189 2009/10/27 17:11:18 tgl Exp $
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
#include "postgres.h"
|
|
|
|
|
1999-07-16 07:00:38 +02:00
|
|
|
#include "access/heapam.h"
|
1998-10-02 18:28:04 +02:00
|
|
|
#include "catalog/pg_type.h"
|
1999-08-26 01:21:43 +02:00
|
|
|
#include "nodes/makefuncs.h"
|
2008-08-26 00:42:34 +02:00
|
|
|
#include "nodes/nodeFuncs.h"
|
1999-07-16 07:00:38 +02:00
|
|
|
#include "parser/analyze.h"
|
2002-04-05 07:47:05 +02:00
|
|
|
#include "parser/parse_coerce.h"
|
|
|
|
#include "parser/parsetree.h"
|
2007-03-20 00:38:32 +01:00
|
|
|
#include "rewrite/rewriteDefine.h"
|
2001-06-13 20:56:30 +02:00
|
|
|
#include "rewrite/rewriteHandler.h"
|
1999-07-16 07:00:38 +02:00
|
|
|
#include "rewrite/rewriteManip.h"
|
2002-04-05 07:47:05 +02:00
|
|
|
#include "utils/builtins.h"
|
1999-07-16 07:00:38 +02:00
|
|
|
#include "utils/lsyscache.h"
|
2007-03-20 00:38:32 +01:00
|
|
|
#include "commands/trigger.h"
|
1998-10-02 18:28:04 +02:00
|
|
|
|
|
|
|
|
2003-02-26 00:47:43 +01:00
|
|
|
/* We use a list of these to detect recursion in RewriteQuery */
|
2003-08-04 02:43:34 +02:00
|
|
|
typedef struct rewrite_event
|
|
|
|
{
|
2003-02-26 00:47:43 +01:00
|
|
|
Oid relation; /* OID of relation having rules */
|
|
|
|
CmdType event; /* type of rule being fired */
|
2003-08-08 23:42:59 +02:00
|
|
|
} rewrite_event;
|
2003-02-26 00:47:43 +01:00
|
|
|
|
2005-06-04 01:05:30 +02:00
|
|
|
static bool acquireLocksOnSubLinks(Node *node, void *context);
|
2001-06-13 20:56:30 +02:00
|
|
|
static Query *rewriteRuleAction(Query *parsetree,
|
2001-10-25 07:50:21 +02:00
|
|
|
Query *rule_action,
|
|
|
|
Node *rule_qual,
|
|
|
|
int rt_index,
|
2006-09-02 19:06:52 +02:00
|
|
|
CmdType event,
|
|
|
|
bool *returning_flag);
|
2001-01-27 05:40:59 +01:00
|
|
|
static List *adjustJoinTreeList(Query *parsetree, bool removert, int rt_index);
|
2006-08-02 03:59:48 +02:00
|
|
|
static void rewriteTargetList(Query *parsetree, Relation target_relation,
|
2006-10-04 02:30:14 +02:00
|
|
|
List **attrno_list);
|
2002-04-05 07:47:05 +02:00
|
|
|
static TargetEntry *process_matched_tle(TargetEntry *src_tle,
|
2004-08-29 07:07:03 +02:00
|
|
|
TargetEntry *prior_tle,
|
|
|
|
const char *attrName);
|
2004-06-09 21:08:20 +02:00
|
|
|
static Node *get_assignment_input(Node *node);
|
2006-08-02 03:59:48 +02:00
|
|
|
static void rewriteValuesRTE(RangeTblEntry *rte, Relation target_relation,
|
2006-10-04 02:30:14 +02:00
|
|
|
List *attrnos);
|
2007-03-01 19:50:28 +01:00
|
|
|
static void markQueryForLocking(Query *qry, Node *jtnode,
|
2007-11-16 00:23:44 +01:00
|
|
|
bool forUpdate, bool noWait);
|
2000-09-29 20:21:41 +02:00
|
|
|
static List *matchLocks(CmdType event, RuleLock *rulelocks,
|
2001-03-22 05:01:46 +01:00
|
|
|
int varno, Query *parsetree);
|
2003-02-26 00:47:43 +01:00
|
|
|
static Query *fireRIRrules(Query *parsetree, List *activeRIRs);
|
2000-10-05 21:11:39 +02:00
|
|
|
|
1996-07-09 08:22:35 +02:00
|
|
|
|
2005-06-04 01:05:30 +02:00
|
|
|
/*
|
|
|
|
* AcquireRewriteLocks -
|
|
|
|
* Acquire suitable locks on all the relations mentioned in the Query.
|
|
|
|
* These locks will ensure that the relation schemas don't change under us
|
|
|
|
* while we are rewriting and planning the query.
|
|
|
|
*
|
|
|
|
* A secondary purpose of this routine is to fix up JOIN RTE references to
|
|
|
|
* dropped columns (see details below). Because the RTEs are modified in
|
|
|
|
* place, it is generally appropriate for the caller of this routine to have
|
|
|
|
* first done a copyObject() to make a writable copy of the querytree in the
|
|
|
|
* current memory context.
|
|
|
|
*
|
|
|
|
* This processing can, and for efficiency's sake should, be skipped when the
|
|
|
|
* querytree has just been built by the parser: parse analysis already got
|
|
|
|
* all the same locks we'd get here, and the parser will have omitted dropped
|
|
|
|
* columns from JOINs to begin with. But we must do this whenever we are
|
|
|
|
* dealing with a querytree produced earlier than the current command.
|
|
|
|
*
|
|
|
|
* About JOINs and dropped columns: although the parser never includes an
|
|
|
|
* already-dropped column in a JOIN RTE's alias var list, it is possible for
|
|
|
|
* such a list in a stored rule to include references to dropped columns.
|
|
|
|
* (If the column is not explicitly referenced anywhere else in the query,
|
|
|
|
* the dependency mechanism won't consider it used by the rule and so won't
|
|
|
|
* prevent the column drop.) To support get_rte_attribute_is_dropped(),
|
|
|
|
* we replace join alias vars that reference dropped columns with NULL Const
|
|
|
|
* nodes.
|
|
|
|
*
|
|
|
|
* (In PostgreSQL 8.0, we did not do this processing but instead had
|
|
|
|
* get_rte_attribute_is_dropped() recurse to detect dropped columns in joins.
|
|
|
|
* That approach had horrible performance unfortunately; in particular
|
|
|
|
* construction of a nested join was O(N^2) in the nesting depth.)
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
AcquireRewriteLocks(Query *parsetree)
|
|
|
|
{
|
|
|
|
ListCell *l;
|
|
|
|
int rt_index;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* First, process RTEs of the current query level.
|
|
|
|
*/
|
|
|
|
rt_index = 0;
|
|
|
|
foreach(l, parsetree->rtable)
|
|
|
|
{
|
|
|
|
RangeTblEntry *rte = (RangeTblEntry *) lfirst(l);
|
|
|
|
Relation rel;
|
|
|
|
LOCKMODE lockmode;
|
|
|
|
List *newaliasvars;
|
2005-06-04 21:19:42 +02:00
|
|
|
Index curinputvarno;
|
|
|
|
RangeTblEntry *curinputrte;
|
2005-06-04 01:05:30 +02:00
|
|
|
ListCell *ll;
|
|
|
|
|
|
|
|
++rt_index;
|
|
|
|
switch (rte->rtekind)
|
|
|
|
{
|
|
|
|
case RTE_RELATION:
|
2005-10-15 04:49:52 +02:00
|
|
|
|
2005-06-04 01:05:30 +02:00
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* Grab the appropriate lock type for the relation, and do not
|
|
|
|
* release it until end of transaction. This protects the
|
|
|
|
* rewriter and planner against schema changes mid-query.
|
2005-06-04 01:05:30 +02:00
|
|
|
*
|
2005-11-22 19:17:34 +01:00
|
|
|
* If the relation is the query's result relation, then we
|
|
|
|
* need RowExclusiveLock. Otherwise, check to see if the
|
|
|
|
* relation is accessed FOR UPDATE/SHARE or not. We can't
|
|
|
|
* just grab AccessShareLock because then the executor would
|
|
|
|
* be trying to upgrade the lock, leading to possible
|
|
|
|
* deadlocks.
|
2005-06-04 01:05:30 +02:00
|
|
|
*/
|
|
|
|
if (rt_index == parsetree->resultRelation)
|
|
|
|
lockmode = RowExclusiveLock;
|
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.
2009-10-26 03:26:45 +01:00
|
|
|
else if (get_parse_rowmark(parsetree, rt_index) != NULL)
|
2005-06-04 01:05:30 +02:00
|
|
|
lockmode = RowShareLock;
|
|
|
|
else
|
|
|
|
lockmode = AccessShareLock;
|
|
|
|
|
|
|
|
rel = heap_open(rte->relid, lockmode);
|
|
|
|
heap_close(rel, NoLock);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case RTE_JOIN:
|
2005-10-15 04:49:52 +02:00
|
|
|
|
2005-06-04 01:05:30 +02:00
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* Scan the join's alias var list to see if any columns have
|
|
|
|
* been dropped, and if so replace those Vars with NULL
|
|
|
|
* Consts.
|
2005-06-04 21:19:42 +02:00
|
|
|
*
|
2005-10-15 04:49:52 +02:00
|
|
|
* Since a join has only two inputs, we can expect to see
|
|
|
|
* multiple references to the same input RTE; optimize away
|
|
|
|
* multiple fetches.
|
2005-06-04 01:05:30 +02:00
|
|
|
*/
|
|
|
|
newaliasvars = NIL;
|
2005-06-04 21:19:42 +02:00
|
|
|
curinputvarno = 0;
|
|
|
|
curinputrte = NULL;
|
2005-06-04 01:05:30 +02:00
|
|
|
foreach(ll, rte->joinaliasvars)
|
|
|
|
{
|
|
|
|
Var *aliasvar = (Var *) lfirst(ll);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If the list item isn't a simple Var, then it must
|
|
|
|
* represent a merged column, ie a USING column, and so it
|
|
|
|
* couldn't possibly be dropped, since it's referenced in
|
2005-10-15 04:49:52 +02:00
|
|
|
* the join clause. (Conceivably it could also be a NULL
|
|
|
|
* constant already? But that's OK too.)
|
2005-06-04 01:05:30 +02:00
|
|
|
*/
|
|
|
|
if (IsA(aliasvar, Var))
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* The elements of an alias list have to refer to
|
2005-10-15 04:49:52 +02:00
|
|
|
* earlier RTEs of the same rtable, because that's the
|
|
|
|
* order the planner builds things in. So we already
|
|
|
|
* processed the referenced RTE, and so it's safe to
|
|
|
|
* use get_rte_attribute_is_dropped on it. (This might
|
|
|
|
* not hold after rewriting or planning, but it's OK
|
|
|
|
* to assume here.)
|
2005-06-04 01:05:30 +02:00
|
|
|
*/
|
|
|
|
Assert(aliasvar->varlevelsup == 0);
|
2005-06-04 21:19:42 +02:00
|
|
|
if (aliasvar->varno != curinputvarno)
|
|
|
|
{
|
|
|
|
curinputvarno = aliasvar->varno;
|
|
|
|
if (curinputvarno >= rt_index)
|
|
|
|
elog(ERROR, "unexpected varno %d in JOIN RTE %d",
|
|
|
|
curinputvarno, rt_index);
|
|
|
|
curinputrte = rt_fetch(curinputvarno,
|
|
|
|
parsetree->rtable);
|
|
|
|
}
|
|
|
|
if (get_rte_attribute_is_dropped(curinputrte,
|
|
|
|
aliasvar->varattno))
|
2005-06-04 01:05:30 +02:00
|
|
|
{
|
|
|
|
/*
|
|
|
|
* can't use vartype here, since that might be a
|
|
|
|
* now-dropped type OID, but it doesn't really
|
|
|
|
* matter what type the Const claims to be.
|
|
|
|
*/
|
2007-09-06 19:31:58 +02:00
|
|
|
aliasvar = (Var *) makeNullConst(INT4OID, -1);
|
2005-06-04 01:05:30 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
newaliasvars = lappend(newaliasvars, aliasvar);
|
|
|
|
}
|
|
|
|
rte->joinaliasvars = newaliasvars;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case RTE_SUBQUERY:
|
2005-10-15 04:49:52 +02:00
|
|
|
|
2005-06-04 01:05:30 +02:00
|
|
|
/*
|
|
|
|
* The subquery RTE itself is all right, but we have to
|
|
|
|
* recurse to process the represented subquery.
|
|
|
|
*/
|
|
|
|
AcquireRewriteLocks(rte->subquery);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
/* ignore other types of RTEs */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-10-04 23:56:55 +02:00
|
|
|
/* Recurse into subqueries in WITH */
|
|
|
|
foreach(l, parsetree->cteList)
|
|
|
|
{
|
|
|
|
CommonTableExpr *cte = (CommonTableExpr *) lfirst(l);
|
|
|
|
|
|
|
|
AcquireRewriteLocks((Query *) cte->ctequery);
|
|
|
|
}
|
|
|
|
|
2005-06-04 01:05:30 +02:00
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* Recurse into sublink subqueries, too. But we already did the ones in
|
2008-10-04 23:56:55 +02:00
|
|
|
* the rtable and cteList.
|
2005-06-04 01:05:30 +02:00
|
|
|
*/
|
|
|
|
if (parsetree->hasSubLinks)
|
|
|
|
query_tree_walker(parsetree, acquireLocksOnSubLinks, NULL,
|
2008-10-04 23:56:55 +02:00
|
|
|
QTW_IGNORE_RC_SUBQUERIES);
|
2005-06-04 01:05:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Walker to find sublink subqueries for AcquireRewriteLocks
|
|
|
|
*/
|
|
|
|
static bool
|
|
|
|
acquireLocksOnSubLinks(Node *node, void *context)
|
|
|
|
{
|
|
|
|
if (node == NULL)
|
|
|
|
return false;
|
|
|
|
if (IsA(node, SubLink))
|
|
|
|
{
|
|
|
|
SubLink *sub = (SubLink *) node;
|
|
|
|
|
|
|
|
/* Do what we came for */
|
|
|
|
AcquireRewriteLocks((Query *) sub->subselect);
|
|
|
|
/* Fall through to process lefthand args of SubLink */
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Do NOT recurse into Query nodes, because AcquireRewriteLocks already
|
|
|
|
* processed subselects of subselects for us.
|
|
|
|
*/
|
|
|
|
return expression_tree_walker(node, acquireLocksOnSubLinks, context);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1996-07-09 08:22:35 +02:00
|
|
|
/*
|
2001-06-13 20:56:30 +02:00
|
|
|
* rewriteRuleAction -
|
|
|
|
* Rewrite the rule action with appropriate qualifiers (taken from
|
|
|
|
* the triggering query).
|
2006-09-02 19:06:52 +02:00
|
|
|
*
|
|
|
|
* Input arguments:
|
|
|
|
* parsetree - original query
|
|
|
|
* rule_action - one action (query) of a rule
|
|
|
|
* rule_qual - WHERE condition of rule, or NULL if unconditional
|
|
|
|
* rt_index - RT index of result relation in original query
|
|
|
|
* event - type of rule event
|
|
|
|
* Output arguments:
|
|
|
|
* *returning_flag - set TRUE if we rewrite RETURNING clause in rule_action
|
|
|
|
* (must be initialized to FALSE)
|
|
|
|
* Return value:
|
|
|
|
* rewritten form of rule_action
|
1996-07-09 08:22:35 +02:00
|
|
|
*/
|
2001-06-13 20:56:30 +02:00
|
|
|
static Query *
|
|
|
|
rewriteRuleAction(Query *parsetree,
|
1997-09-08 23:56:23 +02:00
|
|
|
Query *rule_action,
|
|
|
|
Node *rule_qual,
|
1997-09-07 07:04:48 +02:00
|
|
|
int rt_index,
|
2006-09-02 19:06:52 +02:00
|
|
|
CmdType event,
|
|
|
|
bool *returning_flag)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
2001-06-13 20:56:30 +02:00
|
|
|
int current_varno,
|
|
|
|
new_varno;
|
|
|
|
int rt_length;
|
2000-12-05 20:15:10 +01:00
|
|
|
Query *sub_action;
|
|
|
|
Query **sub_action_ptr;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2001-06-13 20:56:30 +02:00
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* Make modifiable copies of rule action and qual (what we're passed are
|
|
|
|
* the stored versions in the relcache; don't touch 'em!).
|
2001-06-13 20:56:30 +02:00
|
|
|
*/
|
|
|
|
rule_action = (Query *) copyObject(rule_action);
|
|
|
|
rule_qual = (Node *) copyObject(rule_qual);
|
|
|
|
|
2005-06-04 01:05:30 +02:00
|
|
|
/*
|
|
|
|
* Acquire necessary locks and fix any deleted JOIN RTE entries.
|
|
|
|
*/
|
|
|
|
AcquireRewriteLocks(rule_action);
|
|
|
|
(void) acquireLocksOnSubLinks(rule_qual, NULL);
|
|
|
|
|
2001-06-13 20:56:30 +02:00
|
|
|
current_varno = rt_index;
|
2004-05-31 01:40:41 +02:00
|
|
|
rt_length = list_length(parsetree->rtable);
|
2001-06-13 20:56:30 +02:00
|
|
|
new_varno = PRS2_NEW_VARNO + rt_length;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2000-12-05 20:15:10 +01:00
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* Adjust rule action and qual to offset its varnos, so that we can merge
|
|
|
|
* its rtable with the main parsetree's rtable.
|
2000-12-05 20:15:10 +01:00
|
|
|
*
|
2005-11-22 19:17:34 +01:00
|
|
|
* If the rule action is an INSERT...SELECT, the OLD/NEW rtable entries
|
|
|
|
* will be in the SELECT part, and we have to modify that rather than the
|
2005-10-15 04:49:52 +02:00
|
|
|
* top-level INSERT (kluge!).
|
2000-12-05 20:15:10 +01:00
|
|
|
*/
|
2001-06-13 20:56:30 +02:00
|
|
|
sub_action = getInsertSelectQuery(rule_action, &sub_action_ptr);
|
2000-09-12 23:07:18 +02:00
|
|
|
|
2000-12-05 20:15:10 +01:00
|
|
|
OffsetVarNodes((Node *) sub_action, rt_length, 0);
|
2001-06-13 20:56:30 +02:00
|
|
|
OffsetVarNodes(rule_qual, rt_length, 0);
|
2000-12-05 20:15:10 +01:00
|
|
|
/* but references to *OLD* should point at original rt_index */
|
|
|
|
ChangeVarNodes((Node *) sub_action,
|
|
|
|
PRS2_OLD_VARNO + rt_length, rt_index, 0);
|
2001-06-13 20:56:30 +02:00
|
|
|
ChangeVarNodes(rule_qual,
|
2000-12-05 20:15:10 +01:00
|
|
|
PRS2_OLD_VARNO + rt_length, rt_index, 0);
|
|
|
|
|
|
|
|
/*
|
2001-10-25 07:50:21 +02:00
|
|
|
* Generate expanded rtable consisting of main parsetree's rtable plus
|
|
|
|
* rule action's rtable; this becomes the complete rtable for the rule
|
2005-10-15 04:49:52 +02:00
|
|
|
* action. Some of the entries may be unused after we finish rewriting,
|
|
|
|
* but we leave them all in place for two reasons:
|
2004-01-14 04:39:22 +01:00
|
|
|
*
|
2005-11-22 19:17:34 +01:00
|
|
|
* We'd have a much harder job to adjust the query's varnos if we
|
|
|
|
* selectively removed RT entries.
|
2004-01-14 04:39:22 +01:00
|
|
|
*
|
2005-11-22 19:17:34 +01:00
|
|
|
* If the rule is INSTEAD, then the original query won't be executed at
|
|
|
|
* all, and so its rtable must be preserved so that the executor will do
|
|
|
|
* the correct permissions checks on it.
|
2004-01-14 04:39:22 +01:00
|
|
|
*
|
|
|
|
* RT entries that are not referenced in the completed jointree will be
|
2005-10-15 04:49:52 +02:00
|
|
|
* ignored by the planner, so they do not affect query semantics. But any
|
|
|
|
* permissions checks specified in them will be applied during executor
|
|
|
|
* startup (see ExecCheckRTEPerms()). This allows us to check that the
|
|
|
|
* caller has, say, insert-permission on a view, when the view is not
|
|
|
|
* semantically referenced at all in the resulting query.
|
2004-01-14 04:39:22 +01:00
|
|
|
*
|
2005-11-22 19:17:34 +01:00
|
|
|
* When a rule is not INSTEAD, the permissions checks done on its copied
|
|
|
|
* RT entries will be redundant with those done during execution of the
|
2005-10-15 04:49:52 +02:00
|
|
|
* original query, but we don't bother to treat that case differently.
|
2000-12-05 20:15:10 +01:00
|
|
|
*
|
2005-11-22 19:17:34 +01:00
|
|
|
* NOTE: because planner will destructively alter rtable, we must ensure
|
|
|
|
* that rule action's rtable is separate and shares no substructure with
|
|
|
|
* the main rtable. Hence do a deep copy here.
|
2000-12-05 20:15:10 +01:00
|
|
|
*/
|
2004-05-31 01:40:41 +02:00
|
|
|
sub_action->rtable = list_concat((List *) copyObject(parsetree->rtable),
|
2004-08-29 07:07:03 +02:00
|
|
|
sub_action->rtable);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2008-09-24 18:52:46 +02:00
|
|
|
/*
|
|
|
|
* There could have been some SubLinks in parsetree's rtable, in which
|
|
|
|
* case we'd better mark the sub_action correctly.
|
|
|
|
*/
|
|
|
|
if (parsetree->hasSubLinks && !sub_action->hasSubLinks)
|
|
|
|
{
|
|
|
|
ListCell *lc;
|
|
|
|
|
|
|
|
foreach(lc, parsetree->rtable)
|
|
|
|
{
|
|
|
|
RangeTblEntry *rte = (RangeTblEntry *) lfirst(lc);
|
|
|
|
|
|
|
|
switch (rte->rtekind)
|
|
|
|
{
|
|
|
|
case RTE_FUNCTION:
|
|
|
|
sub_action->hasSubLinks =
|
|
|
|
checkExprHasSubLink(rte->funcexpr);
|
|
|
|
break;
|
|
|
|
case RTE_VALUES:
|
|
|
|
sub_action->hasSubLinks =
|
|
|
|
checkExprHasSubLink((Node *) rte->values_lists);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
/* other RTE types don't contain bare expressions */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (sub_action->hasSubLinks)
|
2009-06-11 16:49:15 +02:00
|
|
|
break; /* no need to keep scanning rtable */
|
2008-09-24 18:52:46 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-12-05 20:15:10 +01:00
|
|
|
/*
|
|
|
|
* Each rule action's jointree should be the main parsetree's jointree
|
2005-10-15 04:49:52 +02:00
|
|
|
* plus that rule's jointree, but usually *without* the original rtindex
|
|
|
|
* that we're replacing (if present, which it won't be for INSERT). Note
|
|
|
|
* that if the rule action refers to OLD, its jointree will add a
|
|
|
|
* reference to rt_index. If the rule action doesn't refer to OLD, but
|
|
|
|
* either the rule_qual or the user query quals do, then we need to keep
|
|
|
|
* the original rtindex in the jointree to provide data for the quals. We
|
|
|
|
* don't want the original rtindex to be joined twice, however, so avoid
|
|
|
|
* keeping it if the rule action mentions it.
|
2001-06-12 20:54:22 +02:00
|
|
|
*
|
2005-11-22 19:17:34 +01:00
|
|
|
* As above, the action's jointree must not share substructure with the
|
|
|
|
* main parsetree's.
|
2000-12-05 20:15:10 +01:00
|
|
|
*/
|
2003-07-16 19:25:48 +02:00
|
|
|
if (sub_action->commandType != CMD_UTILITY)
|
2000-12-05 20:15:10 +01:00
|
|
|
{
|
2001-03-22 05:01:46 +01:00
|
|
|
bool keeporig;
|
|
|
|
List *newjointree;
|
2001-01-27 05:40:59 +01:00
|
|
|
|
2003-07-16 19:25:48 +02:00
|
|
|
Assert(sub_action->jointree != NULL);
|
2001-03-22 05:01:46 +01:00
|
|
|
keeporig = (!rangeTableEntry_used((Node *) sub_action->jointree,
|
|
|
|
rt_index, 0)) &&
|
2001-06-13 20:56:30 +02:00
|
|
|
(rangeTableEntry_used(rule_qual, rt_index, 0) ||
|
2005-10-15 04:49:52 +02:00
|
|
|
rangeTableEntry_used(parsetree->jointree->quals, rt_index, 0));
|
2001-01-27 05:40:59 +01:00
|
|
|
newjointree = adjustJoinTreeList(parsetree, !keeporig, rt_index);
|
2003-07-16 19:25:48 +02:00
|
|
|
if (newjointree != NIL)
|
|
|
|
{
|
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* If sub_action is a setop, manipulating its jointree will do no
|
|
|
|
* good at all, because the jointree is dummy. (Perhaps someday
|
|
|
|
* we could push the joining and quals down to the member
|
|
|
|
* statements of the setop?)
|
2003-07-16 19:25:48 +02:00
|
|
|
*/
|
|
|
|
if (sub_action->setOperations != NULL)
|
2003-07-25 02:01:09 +02:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
|
|
|
|
errmsg("conditional UNION/INTERSECT/EXCEPT statements are not implemented")));
|
2003-07-16 19:25:48 +02:00
|
|
|
|
|
|
|
sub_action->jointree->fromlist =
|
2004-05-31 01:40:41 +02:00
|
|
|
list_concat(newjointree, sub_action->jointree->fromlist);
|
2005-11-23 18:21:04 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* There could have been some SubLinks in newjointree, in which
|
|
|
|
* case we'd better mark the sub_action correctly.
|
|
|
|
*/
|
|
|
|
if (parsetree->hasSubLinks && !sub_action->hasSubLinks)
|
|
|
|
sub_action->hasSubLinks =
|
|
|
|
checkExprHasSubLink((Node *) newjointree);
|
2003-07-16 19:25:48 +02:00
|
|
|
}
|
2000-12-05 20:15:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* Event Qualification forces copying of parsetree and splitting into two
|
|
|
|
* queries one w/rule_qual, one w/NOT rule_qual. Also add user query qual
|
|
|
|
* onto rule action
|
2000-12-05 20:15:10 +01:00
|
|
|
*/
|
2001-06-13 20:56:30 +02:00
|
|
|
AddQual(sub_action, rule_qual);
|
2000-12-05 20:15:10 +01:00
|
|
|
|
|
|
|
AddQual(sub_action, parsetree->jointree->quals);
|
|
|
|
|
|
|
|
/*
|
2001-03-22 05:01:46 +01:00
|
|
|
* Rewrite new.attribute w/ right hand side of target-list entry for
|
|
|
|
* appropriate field name in insert/update.
|
2000-12-05 20:15:10 +01:00
|
|
|
*
|
2005-11-22 19:17:34 +01:00
|
|
|
* KLUGE ALERT: since ResolveNew returns a mutated copy, we can't just
|
|
|
|
* apply it to sub_action; we have to remember to update the sublink
|
|
|
|
* inside rule_action, too.
|
2000-12-05 20:15:10 +01:00
|
|
|
*/
|
2004-08-07 19:40:49 +02:00
|
|
|
if ((event == CMD_INSERT || event == CMD_UPDATE) &&
|
|
|
|
sub_action->commandType != CMD_UTILITY)
|
2000-12-05 20:15:10 +01:00
|
|
|
{
|
|
|
|
sub_action = (Query *) ResolveNew((Node *) sub_action,
|
2001-06-13 20:56:30 +02:00
|
|
|
new_varno,
|
2000-12-05 20:15:10 +01:00
|
|
|
0,
|
2005-06-04 21:19:42 +02:00
|
|
|
rt_fetch(new_varno,
|
|
|
|
sub_action->rtable),
|
2000-12-05 20:15:10 +01:00
|
|
|
parsetree->targetList,
|
2001-06-13 20:56:30 +02:00
|
|
|
event,
|
Fix subquery pullup to wrap a PlaceHolderVar around the entire RowExpr
that's generated for a whole-row Var referencing the subquery, when the
subquery is in the nullable side of an outer join. The previous coding
instead put PlaceHolderVars around the elements of the RowExpr. The effect
was that when the outer join made the subquery outputs go to null, the
whole-row Var produced ROW(NULL,NULL,...) rather than just NULL. There
are arguments afoot about whether those things ought to be semantically
indistinguishable, but for the moment they are not entirely so, and the
planner needs to take care that its machinations preserve the difference.
Per bug #5025.
Making this feasible required refactoring ResolveNew() to allow more caller
control over what is substituted for a Var. I chose to make ResolveNew()
a wrapper around a new general-purpose function replace_rte_variables().
I also fixed the ancient bogosity that ResolveNew might fail to set
a query's hasSubLinks field after inserting a SubLink in it. Although
all current callers make sure that happens anyway, we've had bugs of that
sort before, and it seemed like a good time to install a proper solution.
Back-patch to 8.4. The problem can be demonstrated clear back to 8.0,
but the fix would be too invasive in earlier branches; not to mention
that people may be depending on the subtly-incorrect behavior. The
8.4 series is new enough that fixing this probably won't cause complaints,
but it might in older branches. Also, 8.4 shows the incorrect behavior
in more cases than older branches do, because it is able to flatten
subqueries in more cases.
2009-09-02 19:52:24 +02:00
|
|
|
current_varno,
|
|
|
|
NULL);
|
2000-12-05 20:15:10 +01:00
|
|
|
if (sub_action_ptr)
|
|
|
|
*sub_action_ptr = sub_action;
|
|
|
|
else
|
2001-06-13 20:56:30 +02:00
|
|
|
rule_action = sub_action;
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|
2000-12-05 20:15:10 +01:00
|
|
|
|
2006-09-02 19:06:52 +02:00
|
|
|
/*
|
2006-10-04 02:30:14 +02:00
|
|
|
* If rule_action has a RETURNING clause, then either throw it away if the
|
|
|
|
* triggering query has no RETURNING clause, or rewrite it to emit what
|
|
|
|
* the triggering query's RETURNING clause asks for. Throw an error if
|
|
|
|
* more than one rule has a RETURNING clause.
|
2006-09-02 19:06:52 +02:00
|
|
|
*/
|
|
|
|
if (!parsetree->returningList)
|
|
|
|
rule_action->returningList = NIL;
|
|
|
|
else if (rule_action->returningList)
|
|
|
|
{
|
|
|
|
if (*returning_flag)
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
|
2006-10-04 02:30:14 +02:00
|
|
|
errmsg("cannot have RETURNING lists in multiple rules")));
|
2006-09-02 19:06:52 +02:00
|
|
|
*returning_flag = true;
|
|
|
|
rule_action->returningList = (List *)
|
|
|
|
ResolveNew((Node *) parsetree->returningList,
|
|
|
|
parsetree->resultRelation,
|
|
|
|
0,
|
|
|
|
rt_fetch(parsetree->resultRelation,
|
|
|
|
parsetree->rtable),
|
|
|
|
rule_action->returningList,
|
|
|
|
CMD_SELECT,
|
Fix subquery pullup to wrap a PlaceHolderVar around the entire RowExpr
that's generated for a whole-row Var referencing the subquery, when the
subquery is in the nullable side of an outer join. The previous coding
instead put PlaceHolderVars around the elements of the RowExpr. The effect
was that when the outer join made the subquery outputs go to null, the
whole-row Var produced ROW(NULL,NULL,...) rather than just NULL. There
are arguments afoot about whether those things ought to be semantically
indistinguishable, but for the moment they are not entirely so, and the
planner needs to take care that its machinations preserve the difference.
Per bug #5025.
Making this feasible required refactoring ResolveNew() to allow more caller
control over what is substituted for a Var. I chose to make ResolveNew()
a wrapper around a new general-purpose function replace_rte_variables().
I also fixed the ancient bogosity that ResolveNew might fail to set
a query's hasSubLinks field after inserting a SubLink in it. Although
all current callers make sure that happens anyway, we've had bugs of that
sort before, and it seemed like a good time to install a proper solution.
Back-patch to 8.4. The problem can be demonstrated clear back to 8.0,
but the fix would be too invasive in earlier branches; not to mention
that people may be depending on the subtly-incorrect behavior. The
8.4 series is new enough that fixing this probably won't cause complaints,
but it might in older branches. Also, 8.4 shows the incorrect behavior
in more cases than older branches do, because it is able to flatten
subqueries in more cases.
2009-09-02 19:52:24 +02:00
|
|
|
0,
|
|
|
|
&rule_action->hasSubLinks);
|
2008-09-24 18:52:46 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* There could have been some SubLinks in parsetree's returningList,
|
|
|
|
* in which case we'd better mark the rule_action correctly.
|
|
|
|
*/
|
|
|
|
if (parsetree->hasSubLinks && !rule_action->hasSubLinks)
|
|
|
|
rule_action->hasSubLinks =
|
2009-06-11 16:49:15 +02:00
|
|
|
checkExprHasSubLink((Node *) rule_action->returningList);
|
2006-09-02 19:06:52 +02:00
|
|
|
}
|
|
|
|
|
2001-06-13 20:56:30 +02:00
|
|
|
return rule_action;
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
1998-10-02 18:28:04 +02:00
|
|
|
/*
|
2001-01-27 05:40:59 +01:00
|
|
|
* Copy the query's jointree list, and optionally attempt to remove any
|
|
|
|
* occurrence of the given rt_index as a top-level join item (we do not look
|
|
|
|
* for it within join items; this is OK because we are only expecting to find
|
|
|
|
* it as an UPDATE or DELETE target relation, which will be at the top level
|
2001-06-12 20:54:22 +02:00
|
|
|
* of the join). Returns modified jointree list --- this is a separate copy
|
|
|
|
* sharing no nodes with the original.
|
1996-07-09 08:22:35 +02:00
|
|
|
*/
|
2000-09-12 23:07:18 +02:00
|
|
|
static List *
|
2001-01-27 05:40:59 +01:00
|
|
|
adjustJoinTreeList(Query *parsetree, bool removert, int rt_index)
|
2000-04-12 19:17:23 +02:00
|
|
|
{
|
2001-06-12 20:54:22 +02:00
|
|
|
List *newjointree = copyObject(parsetree->jointree->fromlist);
|
2004-05-26 06:41:50 +02:00
|
|
|
ListCell *l;
|
1999-10-01 06:08:24 +02:00
|
|
|
|
2001-01-27 05:40:59 +01:00
|
|
|
if (removert)
|
1999-05-25 18:15:34 +02:00
|
|
|
{
|
2004-05-26 06:41:50 +02:00
|
|
|
foreach(l, newjointree)
|
2000-09-12 23:07:18 +02:00
|
|
|
{
|
2004-05-26 06:41:50 +02:00
|
|
|
RangeTblRef *rtr = lfirst(l);
|
2001-01-27 05:40:59 +01:00
|
|
|
|
2002-12-17 02:18:35 +01:00
|
|
|
if (IsA(rtr, RangeTblRef) &&
|
|
|
|
rtr->rtindex == rt_index)
|
2001-01-27 05:40:59 +01:00
|
|
|
{
|
2004-05-31 01:40:41 +02:00
|
|
|
newjointree = list_delete_ptr(newjointree, rtr);
|
2004-08-29 07:07:03 +02:00
|
|
|
|
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* foreach is safe because we exit loop after list_delete...
|
2004-08-29 07:07:03 +02:00
|
|
|
*/
|
2001-01-27 05:40:59 +01:00
|
|
|
break;
|
|
|
|
}
|
2000-09-12 23:07:18 +02:00
|
|
|
}
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
2000-09-12 23:07:18 +02:00
|
|
|
return newjointree;
|
1998-10-02 18:28:04 +02:00
|
|
|
}
|
1998-08-24 03:38:11 +02:00
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2002-04-05 07:47:05 +02:00
|
|
|
/*
|
|
|
|
* rewriteTargetList - rewrite INSERT/UPDATE targetlist into standard form
|
|
|
|
*
|
|
|
|
* This has the following responsibilities:
|
|
|
|
*
|
|
|
|
* 1. For an INSERT, add tlist entries to compute default values for any
|
|
|
|
* attributes that have defaults and are not assigned to in the given tlist.
|
|
|
|
* (We do not insert anything for default-less attributes, however. The
|
|
|
|
* planner will later insert NULLs for them, but there's no reason to slow
|
2003-07-03 18:34:26 +02:00
|
|
|
* down rewriter processing with extra tlist nodes.) Also, for both INSERT
|
|
|
|
* and UPDATE, replace explicit DEFAULT specifications with column default
|
|
|
|
* expressions.
|
2002-04-05 07:47:05 +02:00
|
|
|
*
|
|
|
|
* 2. Merge multiple entries for the same target attribute, or declare error
|
2004-06-09 21:08:20 +02:00
|
|
|
* if we can't. Multiple entries are only allowed for INSERT/UPDATE of
|
|
|
|
* portions of an array or record field, for example
|
|
|
|
* UPDATE table SET foo[2] = 42, foo[4] = 43;
|
2002-04-05 07:47:05 +02:00
|
|
|
* We can merge such operations into a single assignment op. Essentially,
|
|
|
|
* the expression we want to produce in this case is like
|
|
|
|
* foo = array_set(array_set(foo, 2, 42), 4, 43)
|
|
|
|
*
|
|
|
|
* 3. Sort the tlist into standard order: non-junk fields in order by resno,
|
|
|
|
* then junk fields (these in no particular order).
|
|
|
|
*
|
|
|
|
* We must do items 1 and 2 before firing rewrite rules, else rewritten
|
2002-09-04 22:31:48 +02:00
|
|
|
* references to NEW.foo will produce wrong or incomplete results. Item 3
|
2002-04-05 07:47:05 +02:00
|
|
|
* is not needed for rewriting, but will be needed by the planner, and we
|
|
|
|
* can do it essentially for free while handling items 1 and 2.
|
2006-08-02 03:59:48 +02:00
|
|
|
*
|
|
|
|
* If attrno_list isn't NULL, we return an additional output besides the
|
|
|
|
* rewritten targetlist: an integer list of the assigned-to attnums, in
|
|
|
|
* order of the original tlist's non-junk entries. This is needed for
|
|
|
|
* processing VALUES RTEs.
|
2002-04-05 07:47:05 +02:00
|
|
|
*/
|
|
|
|
static void
|
2006-08-02 03:59:48 +02:00
|
|
|
rewriteTargetList(Query *parsetree, Relation target_relation,
|
|
|
|
List **attrno_list)
|
2002-04-05 07:47:05 +02:00
|
|
|
{
|
|
|
|
CmdType commandType = parsetree->commandType;
|
2005-03-26 06:53:01 +01:00
|
|
|
TargetEntry **new_tles;
|
2002-04-05 07:47:05 +02:00
|
|
|
List *new_tlist = NIL;
|
2005-03-26 06:53:01 +01:00
|
|
|
List *junk_tlist = NIL;
|
|
|
|
Form_pg_attribute att_tup;
|
2002-04-05 07:47:05 +02:00
|
|
|
int attrno,
|
2005-03-26 06:53:01 +01:00
|
|
|
next_junk_attrno,
|
2002-04-05 07:47:05 +02:00
|
|
|
numattrs;
|
2004-05-26 06:41:50 +02:00
|
|
|
ListCell *temp;
|
2002-04-05 07:47:05 +02:00
|
|
|
|
2006-08-02 03:59:48 +02:00
|
|
|
if (attrno_list) /* initialize optional result list */
|
|
|
|
*attrno_list = NIL;
|
|
|
|
|
2002-04-05 07:47:05 +02:00
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* We process the normal (non-junk) attributes by scanning the input tlist
|
|
|
|
* once and transferring TLEs into an array, then scanning the array to
|
|
|
|
* build an output tlist. This avoids O(N^2) behavior for large numbers
|
|
|
|
* of attributes.
|
2005-03-26 06:53:01 +01:00
|
|
|
*
|
2005-10-15 04:49:52 +02:00
|
|
|
* Junk attributes are tossed into a separate list during the same tlist
|
|
|
|
* scan, then appended to the reconstructed tlist.
|
2002-04-05 07:47:05 +02:00
|
|
|
*/
|
|
|
|
numattrs = RelationGetNumberOfAttributes(target_relation);
|
2005-03-26 06:53:01 +01:00
|
|
|
new_tles = (TargetEntry **) palloc0(numattrs * sizeof(TargetEntry *));
|
|
|
|
next_junk_attrno = numattrs + 1;
|
2002-04-05 07:47:05 +02:00
|
|
|
|
2005-03-26 06:53:01 +01:00
|
|
|
foreach(temp, parsetree->targetList)
|
2002-04-05 07:47:05 +02:00
|
|
|
{
|
2005-03-26 06:53:01 +01:00
|
|
|
TargetEntry *old_tle = (TargetEntry *) lfirst(temp);
|
2002-08-02 20:15:10 +02:00
|
|
|
|
2005-04-06 18:34:07 +02:00
|
|
|
if (!old_tle->resjunk)
|
2005-03-26 06:53:01 +01:00
|
|
|
{
|
|
|
|
/* Normal attr: stash it into new_tles[] */
|
2005-04-06 18:34:07 +02:00
|
|
|
attrno = old_tle->resno;
|
2005-03-26 06:53:01 +01:00
|
|
|
if (attrno < 1 || attrno > numattrs)
|
|
|
|
elog(ERROR, "bogus resno %d in targetlist", attrno);
|
|
|
|
att_tup = target_relation->rd_att->attrs[attrno - 1];
|
|
|
|
|
2006-08-02 03:59:48 +02:00
|
|
|
/* put attrno into attrno_list even if it's dropped */
|
|
|
|
if (attrno_list)
|
|
|
|
*attrno_list = lappend_int(*attrno_list, attrno);
|
|
|
|
|
2005-03-26 06:53:01 +01:00
|
|
|
/* We can (and must) ignore deleted attributes */
|
|
|
|
if (att_tup->attisdropped)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/* Merge with any prior assignment to same attribute */
|
|
|
|
new_tles[attrno - 1] =
|
|
|
|
process_matched_tle(old_tle,
|
|
|
|
new_tles[attrno - 1],
|
|
|
|
NameStr(att_tup->attname));
|
|
|
|
}
|
|
|
|
else
|
2002-04-05 07:47:05 +02:00
|
|
|
{
|
2005-03-26 06:53:01 +01:00
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* Copy all resjunk tlist entries to junk_tlist, and assign them
|
|
|
|
* resnos above the last real resno.
|
2005-03-26 06:53:01 +01:00
|
|
|
*
|
2005-11-22 19:17:34 +01:00
|
|
|
* Typical junk entries include ORDER BY or GROUP BY expressions
|
|
|
|
* (are these actually possible in an INSERT or UPDATE?), system
|
2005-03-26 06:53:01 +01:00
|
|
|
* attribute references, etc.
|
|
|
|
*/
|
2002-04-05 07:47:05 +02:00
|
|
|
|
2005-03-26 06:53:01 +01:00
|
|
|
/* Get the resno right, but don't copy unnecessarily */
|
2005-04-06 18:34:07 +02:00
|
|
|
if (old_tle->resno != next_junk_attrno)
|
2002-04-05 07:47:05 +02:00
|
|
|
{
|
2005-04-06 18:34:07 +02:00
|
|
|
old_tle = flatCopyTargetEntry(old_tle);
|
|
|
|
old_tle->resno = next_junk_attrno;
|
2002-04-05 07:47:05 +02:00
|
|
|
}
|
2005-03-26 06:53:01 +01:00
|
|
|
junk_tlist = lappend(junk_tlist, old_tle);
|
|
|
|
next_junk_attrno++;
|
2002-04-05 07:47:05 +02:00
|
|
|
}
|
2005-03-26 06:53:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
for (attrno = 1; attrno <= numattrs; attrno++)
|
|
|
|
{
|
|
|
|
TargetEntry *new_tle = new_tles[attrno - 1];
|
|
|
|
|
|
|
|
att_tup = target_relation->rd_att->attrs[attrno - 1];
|
|
|
|
|
|
|
|
/* We can (and must) ignore deleted attributes */
|
|
|
|
if (att_tup->attisdropped)
|
|
|
|
continue;
|
2002-04-05 07:47:05 +02:00
|
|
|
|
2003-07-03 18:34:26 +02:00
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* Handle the two cases where we need to insert a default expression:
|
|
|
|
* it's an INSERT and there's no tlist entry for the column, or the
|
|
|
|
* tlist entry is a DEFAULT placeholder node.
|
2003-07-03 18:34:26 +02:00
|
|
|
*/
|
|
|
|
if ((new_tle == NULL && commandType == CMD_INSERT) ||
|
2005-03-26 06:53:01 +01:00
|
|
|
(new_tle && new_tle->expr && IsA(new_tle->expr, SetToDefault)))
|
2002-04-05 07:47:05 +02:00
|
|
|
{
|
|
|
|
Node *new_expr;
|
|
|
|
|
|
|
|
new_expr = build_column_default(target_relation, attrno);
|
|
|
|
|
2003-07-03 18:34:26 +02:00
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* If there is no default (ie, default is effectively NULL), we
|
|
|
|
* can omit the tlist entry in the INSERT case, since the planner
|
|
|
|
* can insert a NULL for itself, and there's no point in spending
|
|
|
|
* any more rewriter cycles on the entry. But in the UPDATE case
|
|
|
|
* we've got to explicitly set the column to NULL.
|
2003-07-03 18:34:26 +02:00
|
|
|
*/
|
|
|
|
if (!new_expr)
|
|
|
|
{
|
|
|
|
if (commandType == CMD_INSERT)
|
|
|
|
new_tle = NULL;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
new_expr = (Node *) makeConst(att_tup->atttypid,
|
2007-03-17 01:11:05 +01:00
|
|
|
-1,
|
2003-07-03 18:34:26 +02:00
|
|
|
att_tup->attlen,
|
|
|
|
(Datum) 0,
|
|
|
|
true, /* isnull */
|
|
|
|
att_tup->attbyval);
|
|
|
|
/* this is to catch a NOT NULL domain constraint */
|
|
|
|
new_expr = coerce_to_domain(new_expr,
|
2006-04-06 00:11:58 +02:00
|
|
|
InvalidOid, -1,
|
2003-07-03 18:34:26 +02:00
|
|
|
att_tup->atttypid,
|
2004-06-16 03:27:00 +02:00
|
|
|
COERCE_IMPLICIT_CAST,
|
2008-08-29 01:09:48 +02:00
|
|
|
-1,
|
2004-11-06 18:46:38 +01:00
|
|
|
false,
|
2004-06-16 03:27:00 +02:00
|
|
|
false);
|
2003-07-03 18:34:26 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-04-05 07:47:05 +02:00
|
|
|
if (new_expr)
|
2005-04-06 18:34:07 +02:00
|
|
|
new_tle = makeTargetEntry((Expr *) new_expr,
|
|
|
|
attrno,
|
|
|
|
pstrdup(NameStr(att_tup->attname)),
|
|
|
|
false);
|
2002-04-05 07:47:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (new_tle)
|
|
|
|
new_tlist = lappend(new_tlist, new_tle);
|
|
|
|
}
|
|
|
|
|
2005-03-26 06:53:01 +01:00
|
|
|
pfree(new_tles);
|
2002-04-05 07:47:05 +02:00
|
|
|
|
2005-03-26 06:53:01 +01:00
|
|
|
parsetree->targetList = list_concat(new_tlist, junk_tlist);
|
2002-04-05 07:47:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Convert a matched TLE from the original tlist into a correct new TLE.
|
|
|
|
*
|
|
|
|
* This routine detects and handles multiple assignments to the same target
|
2003-08-12 01:04:50 +02:00
|
|
|
* attribute. (The attribute name is needed only for error messages.)
|
2002-04-05 07:47:05 +02:00
|
|
|
*/
|
|
|
|
static TargetEntry *
|
|
|
|
process_matched_tle(TargetEntry *src_tle,
|
2003-08-12 01:04:50 +02:00
|
|
|
TargetEntry *prior_tle,
|
|
|
|
const char *attrName)
|
2002-04-05 07:47:05 +02:00
|
|
|
{
|
2005-04-06 18:34:07 +02:00
|
|
|
TargetEntry *result;
|
2004-06-09 21:08:20 +02:00
|
|
|
Node *src_expr;
|
|
|
|
Node *prior_expr;
|
|
|
|
Node *src_input;
|
|
|
|
Node *prior_input;
|
2002-04-05 07:47:05 +02:00
|
|
|
Node *priorbottom;
|
2004-06-09 21:08:20 +02:00
|
|
|
Node *newexpr;
|
2002-04-05 07:47:05 +02:00
|
|
|
|
|
|
|
if (prior_tle == NULL)
|
|
|
|
{
|
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* Normal case where this is the first assignment to the attribute.
|
2002-04-05 07:47:05 +02:00
|
|
|
*/
|
|
|
|
return src_tle;
|
|
|
|
}
|
|
|
|
|
2004-06-09 21:08:20 +02:00
|
|
|
/*----------
|
2002-04-05 07:47:05 +02:00
|
|
|
* Multiple assignments to same attribute. Allow only if all are
|
2004-06-09 21:08:20 +02:00
|
|
|
* FieldStore or ArrayRef assignment operations. This is a bit
|
|
|
|
* tricky because what we may actually be looking at is a nest of
|
|
|
|
* such nodes; consider
|
|
|
|
* UPDATE tab SET col.fld1.subfld1 = x, col.fld2.subfld2 = y
|
|
|
|
* The two expressions produced by the parser will look like
|
|
|
|
* FieldStore(col, fld1, FieldStore(placeholder, subfld1, x))
|
|
|
|
* FieldStore(col, fld2, FieldStore(placeholder, subfld2, x))
|
|
|
|
* However, we can ignore the substructure and just consider the top
|
|
|
|
* FieldStore or ArrayRef from each assignment, because it works to
|
|
|
|
* combine these as
|
|
|
|
* FieldStore(FieldStore(col, fld1,
|
|
|
|
* FieldStore(placeholder, subfld1, x)),
|
|
|
|
* fld2, FieldStore(placeholder, subfld2, x))
|
|
|
|
* Note the leftmost expression goes on the inside so that the
|
|
|
|
* assignments appear to occur left-to-right.
|
|
|
|
*
|
|
|
|
* For FieldStore, instead of nesting we can generate a single
|
2004-08-29 07:07:03 +02:00
|
|
|
* FieldStore with multiple target fields. We must nest when
|
2004-06-09 21:08:20 +02:00
|
|
|
* ArrayRefs are involved though.
|
|
|
|
*----------
|
2002-04-05 07:47:05 +02:00
|
|
|
*/
|
2004-06-09 21:08:20 +02:00
|
|
|
src_expr = (Node *) src_tle->expr;
|
|
|
|
prior_expr = (Node *) prior_tle->expr;
|
|
|
|
src_input = get_assignment_input(src_expr);
|
|
|
|
prior_input = get_assignment_input(prior_expr);
|
|
|
|
if (src_input == NULL ||
|
|
|
|
prior_input == NULL ||
|
|
|
|
exprType(src_expr) != exprType(prior_expr))
|
2003-07-25 02:01:09 +02:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_SYNTAX_ERROR),
|
2003-09-25 08:58:07 +02:00
|
|
|
errmsg("multiple assignments to same column \"%s\"",
|
2003-08-12 01:04:50 +02:00
|
|
|
attrName)));
|
2002-04-05 07:47:05 +02:00
|
|
|
|
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* Prior TLE could be a nest of assignments if we do this more than once.
|
2002-04-05 07:47:05 +02:00
|
|
|
*/
|
2004-06-09 21:08:20 +02:00
|
|
|
priorbottom = prior_input;
|
|
|
|
for (;;)
|
|
|
|
{
|
2004-08-29 07:07:03 +02:00
|
|
|
Node *newbottom = get_assignment_input(priorbottom);
|
2004-06-09 21:08:20 +02:00
|
|
|
|
|
|
|
if (newbottom == NULL)
|
|
|
|
break; /* found the original Var reference */
|
|
|
|
priorbottom = newbottom;
|
|
|
|
}
|
|
|
|
if (!equal(priorbottom, src_input))
|
2003-07-25 02:01:09 +02:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_SYNTAX_ERROR),
|
2003-09-25 08:58:07 +02:00
|
|
|
errmsg("multiple assignments to same column \"%s\"",
|
2003-08-12 01:04:50 +02:00
|
|
|
attrName)));
|
2002-04-05 07:47:05 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Looks OK to nest 'em.
|
|
|
|
*/
|
2004-06-09 21:08:20 +02:00
|
|
|
if (IsA(src_expr, FieldStore))
|
|
|
|
{
|
2004-08-29 07:07:03 +02:00
|
|
|
FieldStore *fstore = makeNode(FieldStore);
|
2004-06-09 21:08:20 +02:00
|
|
|
|
|
|
|
if (IsA(prior_expr, FieldStore))
|
|
|
|
{
|
|
|
|
/* combine the two */
|
|
|
|
memcpy(fstore, prior_expr, sizeof(FieldStore));
|
|
|
|
fstore->newvals =
|
|
|
|
list_concat(list_copy(((FieldStore *) prior_expr)->newvals),
|
2005-10-15 04:49:52 +02:00
|
|
|
list_copy(((FieldStore *) src_expr)->newvals));
|
2004-06-09 21:08:20 +02:00
|
|
|
fstore->fieldnums =
|
|
|
|
list_concat(list_copy(((FieldStore *) prior_expr)->fieldnums),
|
2005-10-15 04:49:52 +02:00
|
|
|
list_copy(((FieldStore *) src_expr)->fieldnums));
|
2004-06-09 21:08:20 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* general case, just nest 'em */
|
|
|
|
memcpy(fstore, src_expr, sizeof(FieldStore));
|
|
|
|
fstore->arg = (Expr *) prior_expr;
|
|
|
|
}
|
|
|
|
newexpr = (Node *) fstore;
|
|
|
|
}
|
|
|
|
else if (IsA(src_expr, ArrayRef))
|
|
|
|
{
|
|
|
|
ArrayRef *aref = makeNode(ArrayRef);
|
|
|
|
|
|
|
|
memcpy(aref, src_expr, sizeof(ArrayRef));
|
|
|
|
aref->refexpr = (Expr *) prior_expr;
|
|
|
|
newexpr = (Node *) aref;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
Wording cleanup for error messages. Also change can't -> cannot.
Standard English uses "may", "can", and "might" in different ways:
may - permission, "You may borrow my rake."
can - ability, "I can lift that log."
might - possibility, "It might rain today."
Unfortunately, in conversational English, their use is often mixed, as
in, "You may use this variable to do X", when in fact, "can" is a better
choice. Similarly, "It may crash" is better stated, "It might crash".
2007-02-01 20:10:30 +01:00
|
|
|
elog(ERROR, "cannot happen");
|
2004-06-09 21:08:20 +02:00
|
|
|
newexpr = NULL;
|
|
|
|
}
|
2002-04-05 07:47:05 +02:00
|
|
|
|
2005-04-06 18:34:07 +02:00
|
|
|
result = flatCopyTargetEntry(src_tle);
|
|
|
|
result->expr = (Expr *) newexpr;
|
|
|
|
return result;
|
2002-04-05 07:47:05 +02:00
|
|
|
}
|
|
|
|
|
2004-06-09 21:08:20 +02:00
|
|
|
/*
|
|
|
|
* If node is an assignment node, return its input; else return NULL
|
|
|
|
*/
|
|
|
|
static Node *
|
|
|
|
get_assignment_input(Node *node)
|
|
|
|
{
|
|
|
|
if (node == NULL)
|
|
|
|
return NULL;
|
|
|
|
if (IsA(node, FieldStore))
|
|
|
|
{
|
|
|
|
FieldStore *fstore = (FieldStore *) node;
|
|
|
|
|
|
|
|
return (Node *) fstore->arg;
|
|
|
|
}
|
|
|
|
else if (IsA(node, ArrayRef))
|
|
|
|
{
|
|
|
|
ArrayRef *aref = (ArrayRef *) node;
|
|
|
|
|
|
|
|
if (aref->refassgnexpr == NULL)
|
|
|
|
return NULL;
|
|
|
|
return (Node *) aref->refexpr;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
2002-04-05 07:47:05 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Make an expression tree for the default value for a column.
|
|
|
|
*
|
|
|
|
* If there is no default, return a NULL instead.
|
|
|
|
*/
|
2002-07-18 06:43:51 +02:00
|
|
|
Node *
|
2002-04-05 07:47:05 +02:00
|
|
|
build_column_default(Relation rel, int attrno)
|
|
|
|
{
|
|
|
|
TupleDesc rd_att = rel->rd_att;
|
|
|
|
Form_pg_attribute att_tup = rd_att->attrs[attrno - 1];
|
|
|
|
Oid atttype = att_tup->atttypid;
|
|
|
|
int32 atttypmod = att_tup->atttypmod;
|
|
|
|
Node *expr = NULL;
|
|
|
|
Oid exprtype;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Scan to see if relation has a default for this column.
|
|
|
|
*/
|
|
|
|
if (rd_att->constr && rd_att->constr->num_defval > 0)
|
|
|
|
{
|
|
|
|
AttrDefault *defval = rd_att->constr->defval;
|
|
|
|
int ndef = rd_att->constr->num_defval;
|
|
|
|
|
|
|
|
while (--ndef >= 0)
|
|
|
|
{
|
|
|
|
if (attrno == defval[ndef].adnum)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Found it, convert string representation to node tree.
|
|
|
|
*/
|
|
|
|
expr = stringToNode(defval[ndef].adbin);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (expr == NULL)
|
|
|
|
{
|
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* No per-column default, so look for a default for the type itself.
|
2002-04-05 07:47:05 +02:00
|
|
|
*/
|
2004-04-01 23:28:47 +02:00
|
|
|
expr = get_typdefault(atttype);
|
2002-04-05 07:47:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (expr == NULL)
|
|
|
|
return NULL; /* No default anywhere */
|
|
|
|
|
|
|
|
/*
|
2003-07-29 19:21:27 +02:00
|
|
|
* Make sure the value is coerced to the target column type; this will
|
|
|
|
* generally be true already, but there seem to be some corner cases
|
2005-10-15 04:49:52 +02:00
|
|
|
* involving domain defaults where it might not be true. This should match
|
|
|
|
* the parser's processing of non-defaulted expressions --- see
|
2006-08-02 03:59:48 +02:00
|
|
|
* transformAssignedExpr().
|
2002-04-05 07:47:05 +02:00
|
|
|
*/
|
|
|
|
exprtype = exprType(expr);
|
|
|
|
|
2003-08-04 02:43:34 +02:00
|
|
|
expr = coerce_to_target_type(NULL, /* no UNKNOWN params here */
|
2003-04-30 00:13:11 +02:00
|
|
|
expr, exprtype,
|
Extend pg_cast castimplicit column to a three-way value; this allows us
to be flexible about assignment casts without introducing ambiguity in
operator/function resolution. Introduce a well-defined promotion hierarchy
for numeric datatypes (int2->int4->int8->numeric->float4->float8).
Change make_const to initially label numeric literals as int4, int8, or
numeric (never float8 anymore).
Explicitly mark Func and RelabelType nodes to indicate whether they came
from a function call, explicit cast, or implicit cast; use this to do
reverse-listing more accurately and without so many heuristics.
Explicit casts to char, varchar, bit, varbit will truncate or pad without
raising an error (the pre-7.2 behavior), while assigning to a column without
any explicit cast will still raise an error for wrong-length data like 7.3.
This more nearly follows the SQL spec than 7.2 behavior (we should be
reporting a 'completion condition' in the explicit-cast cases, but we have
no mechanism for that, so just do silent truncation).
Fix some problems with enforcement of typmod for array elements;
it didn't work at all in 'UPDATE ... SET array[n] = foo', for example.
Provide a generalized array_length_coerce() function to replace the
specialized per-array-type functions that used to be needed (and were
missing for NUMERIC as well as all the datetime types).
Add missing conversions int8<->float4, text<->numeric, oid<->int8.
initdb forced.
2002-09-18 23:35:25 +02:00
|
|
|
atttype, atttypmod,
|
|
|
|
COERCION_ASSIGNMENT,
|
2008-08-29 01:09:48 +02:00
|
|
|
COERCE_IMPLICIT_CAST,
|
|
|
|
-1);
|
Extend pg_cast castimplicit column to a three-way value; this allows us
to be flexible about assignment casts without introducing ambiguity in
operator/function resolution. Introduce a well-defined promotion hierarchy
for numeric datatypes (int2->int4->int8->numeric->float4->float8).
Change make_const to initially label numeric literals as int4, int8, or
numeric (never float8 anymore).
Explicitly mark Func and RelabelType nodes to indicate whether they came
from a function call, explicit cast, or implicit cast; use this to do
reverse-listing more accurately and without so many heuristics.
Explicit casts to char, varchar, bit, varbit will truncate or pad without
raising an error (the pre-7.2 behavior), while assigning to a column without
any explicit cast will still raise an error for wrong-length data like 7.3.
This more nearly follows the SQL spec than 7.2 behavior (we should be
reporting a 'completion condition' in the explicit-cast cases, but we have
no mechanism for that, so just do silent truncation).
Fix some problems with enforcement of typmod for array elements;
it didn't work at all in 'UPDATE ... SET array[n] = foo', for example.
Provide a generalized array_length_coerce() function to replace the
specialized per-array-type functions that used to be needed (and were
missing for NUMERIC as well as all the datetime types).
Add missing conversions int8<->float4, text<->numeric, oid<->int8.
initdb forced.
2002-09-18 23:35:25 +02:00
|
|
|
if (expr == NULL)
|
2003-07-25 02:01:09 +02:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_DATATYPE_MISMATCH),
|
|
|
|
errmsg("column \"%s\" is of type %s"
|
|
|
|
" but default expression is of type %s",
|
|
|
|
NameStr(att_tup->attname),
|
|
|
|
format_type_be(atttype),
|
|
|
|
format_type_be(exprtype)),
|
2005-10-15 04:49:52 +02:00
|
|
|
errhint("You will need to rewrite or cast the expression.")));
|
2002-04-05 07:47:05 +02:00
|
|
|
|
|
|
|
return expr;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-08-02 03:59:48 +02:00
|
|
|
/* Does VALUES RTE contain any SetToDefault items? */
|
|
|
|
static bool
|
|
|
|
searchForDefault(RangeTblEntry *rte)
|
|
|
|
{
|
|
|
|
ListCell *lc;
|
|
|
|
|
|
|
|
foreach(lc, rte->values_lists)
|
|
|
|
{
|
2006-10-04 02:30:14 +02:00
|
|
|
List *sublist = (List *) lfirst(lc);
|
|
|
|
ListCell *lc2;
|
2006-08-02 03:59:48 +02:00
|
|
|
|
|
|
|
foreach(lc2, sublist)
|
|
|
|
{
|
2006-10-04 02:30:14 +02:00
|
|
|
Node *col = (Node *) lfirst(lc2);
|
2006-08-02 03:59:48 +02:00
|
|
|
|
|
|
|
if (IsA(col, SetToDefault))
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* When processing INSERT ... VALUES with a VALUES RTE (ie, multiple VALUES
|
|
|
|
* lists), we have to replace any DEFAULT items in the VALUES lists with
|
|
|
|
* the appropriate default expressions. The other aspects of rewriteTargetList
|
|
|
|
* need be applied only to the query's targetlist proper.
|
|
|
|
*
|
|
|
|
* Note that we currently can't support subscripted or field assignment
|
|
|
|
* in the multi-VALUES case. The targetlist will contain simple Vars
|
|
|
|
* referencing the VALUES RTE, and therefore process_matched_tle() will
|
|
|
|
* reject any such attempt with "multiple assignments to same column".
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
rewriteValuesRTE(RangeTblEntry *rte, Relation target_relation, List *attrnos)
|
|
|
|
{
|
|
|
|
List *newValues;
|
|
|
|
ListCell *lc;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Rebuilding all the lists is a pretty expensive proposition in a big
|
|
|
|
* VALUES list, and it's a waste of time if there aren't any DEFAULT
|
|
|
|
* placeholders. So first scan to see if there are any.
|
|
|
|
*/
|
|
|
|
if (!searchForDefault(rte))
|
|
|
|
return; /* nothing to do */
|
|
|
|
|
|
|
|
/* Check list lengths (we can assume all the VALUES sublists are alike) */
|
|
|
|
Assert(list_length(attrnos) == list_length(linitial(rte->values_lists)));
|
|
|
|
|
|
|
|
newValues = NIL;
|
|
|
|
foreach(lc, rte->values_lists)
|
|
|
|
{
|
2006-10-04 02:30:14 +02:00
|
|
|
List *sublist = (List *) lfirst(lc);
|
|
|
|
List *newList = NIL;
|
|
|
|
ListCell *lc2;
|
|
|
|
ListCell *lc3;
|
2006-08-02 03:59:48 +02:00
|
|
|
|
|
|
|
forboth(lc2, sublist, lc3, attrnos)
|
|
|
|
{
|
2006-10-04 02:30:14 +02:00
|
|
|
Node *col = (Node *) lfirst(lc2);
|
|
|
|
int attrno = lfirst_int(lc3);
|
2006-08-02 03:59:48 +02:00
|
|
|
|
|
|
|
if (IsA(col, SetToDefault))
|
|
|
|
{
|
|
|
|
Form_pg_attribute att_tup;
|
|
|
|
Node *new_expr;
|
|
|
|
|
|
|
|
att_tup = target_relation->rd_att->attrs[attrno - 1];
|
|
|
|
|
|
|
|
if (!att_tup->attisdropped)
|
|
|
|
new_expr = build_column_default(target_relation, attrno);
|
|
|
|
else
|
2006-10-04 02:30:14 +02:00
|
|
|
new_expr = NULL; /* force a NULL if dropped */
|
2006-08-02 03:59:48 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* If there is no default (ie, default is effectively NULL),
|
|
|
|
* we've got to explicitly set the column to NULL.
|
|
|
|
*/
|
|
|
|
if (!new_expr)
|
|
|
|
{
|
|
|
|
new_expr = (Node *) makeConst(att_tup->atttypid,
|
2007-03-17 01:11:05 +01:00
|
|
|
-1,
|
2006-08-02 03:59:48 +02:00
|
|
|
att_tup->attlen,
|
|
|
|
(Datum) 0,
|
|
|
|
true, /* isnull */
|
|
|
|
att_tup->attbyval);
|
|
|
|
/* this is to catch a NOT NULL domain constraint */
|
|
|
|
new_expr = coerce_to_domain(new_expr,
|
|
|
|
InvalidOid, -1,
|
|
|
|
att_tup->atttypid,
|
|
|
|
COERCE_IMPLICIT_CAST,
|
2008-08-29 01:09:48 +02:00
|
|
|
-1,
|
2006-08-02 03:59:48 +02:00
|
|
|
false,
|
|
|
|
false);
|
|
|
|
}
|
|
|
|
newList = lappend(newList, new_expr);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
newList = lappend(newList, col);
|
|
|
|
}
|
|
|
|
newValues = lappend(newValues, newList);
|
|
|
|
}
|
|
|
|
rte->values_lists = newValues;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1998-10-02 18:28:04 +02:00
|
|
|
/*
|
2000-09-29 20:21:41 +02:00
|
|
|
* matchLocks -
|
|
|
|
* match the list of locks and returns the matching rules
|
1998-10-02 18:28:04 +02:00
|
|
|
*/
|
2000-09-29 20:21:41 +02:00
|
|
|
static List *
|
|
|
|
matchLocks(CmdType event,
|
|
|
|
RuleLock *rulelocks,
|
|
|
|
int varno,
|
|
|
|
Query *parsetree)
|
2000-04-12 19:17:23 +02:00
|
|
|
{
|
2002-10-19 21:00:47 +02:00
|
|
|
List *matching_locks = NIL;
|
2000-09-29 20:21:41 +02:00
|
|
|
int nlocks;
|
|
|
|
int i;
|
1998-10-02 18:28:04 +02:00
|
|
|
|
2003-02-26 00:47:43 +01:00
|
|
|
if (rulelocks == NULL)
|
|
|
|
return NIL;
|
1998-10-02 18:28:04 +02:00
|
|
|
|
2000-09-29 20:21:41 +02:00
|
|
|
if (parsetree->commandType != CMD_SELECT)
|
1999-10-01 06:08:24 +02:00
|
|
|
{
|
2000-09-29 20:21:41 +02:00
|
|
|
if (parsetree->resultRelation != varno)
|
|
|
|
return NIL;
|
1999-10-01 06:08:24 +02:00
|
|
|
}
|
1998-10-02 18:28:04 +02:00
|
|
|
|
2000-09-29 20:21:41 +02:00
|
|
|
nlocks = rulelocks->numLocks;
|
1998-10-02 18:28:04 +02:00
|
|
|
|
2000-09-29 20:21:41 +02:00
|
|
|
for (i = 0; i < nlocks; i++)
|
1999-05-25 18:15:34 +02:00
|
|
|
{
|
2000-09-29 20:21:41 +02:00
|
|
|
RewriteRule *oneLock = rulelocks->rules[i];
|
1998-10-02 18:28:04 +02:00
|
|
|
|
2007-03-20 00:38:32 +01:00
|
|
|
/*
|
2007-11-16 00:23:44 +01:00
|
|
|
* Suppress ON INSERT/UPDATE/DELETE rules that are disabled or
|
|
|
|
* configured to not fire during the current sessions replication
|
|
|
|
* role. ON SELECT rules will always be applied in order to keep views
|
|
|
|
* working even in LOCAL or REPLICA role.
|
2007-03-20 00:38:32 +01:00
|
|
|
*/
|
|
|
|
if (oneLock->event != CMD_SELECT)
|
|
|
|
{
|
|
|
|
if (SessionReplicationRole == SESSION_REPLICATION_ROLE_REPLICA)
|
|
|
|
{
|
|
|
|
if (oneLock->enabled == RULE_FIRES_ON_ORIGIN ||
|
|
|
|
oneLock->enabled == RULE_DISABLED)
|
|
|
|
continue;
|
|
|
|
}
|
2007-11-16 02:51:22 +01:00
|
|
|
else /* ORIGIN or LOCAL ROLE */
|
2007-03-20 00:38:32 +01:00
|
|
|
{
|
|
|
|
if (oneLock->enabled == RULE_FIRES_ON_REPLICA ||
|
|
|
|
oneLock->enabled == RULE_DISABLED)
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-09-29 20:21:41 +02:00
|
|
|
if (oneLock->event == event)
|
1999-11-15 03:00:15 +01:00
|
|
|
{
|
2000-09-29 20:21:41 +02:00
|
|
|
if (parsetree->commandType != CMD_SELECT ||
|
|
|
|
(oneLock->attrno == -1 ?
|
|
|
|
rangeTableEntry_used((Node *) parsetree, varno, 0) :
|
|
|
|
attribute_used((Node *) parsetree,
|
|
|
|
varno, oneLock->attrno, 0)))
|
2002-10-19 21:00:47 +02:00
|
|
|
matching_locks = lappend(matching_locks, oneLock);
|
1999-11-15 03:00:15 +01:00
|
|
|
}
|
1998-10-02 18:28:04 +02:00
|
|
|
}
|
2000-09-29 20:21:41 +02:00
|
|
|
|
2002-10-19 21:00:47 +02:00
|
|
|
return matching_locks;
|
1998-10-02 18:28:04 +02:00
|
|
|
}
|
|
|
|
|
2000-04-12 19:17:23 +02:00
|
|
|
|
2005-06-04 01:05:30 +02:00
|
|
|
/*
|
|
|
|
* ApplyRetrieveRule - expand an ON SELECT rule
|
|
|
|
*/
|
2000-09-29 20:21:41 +02:00
|
|
|
static Query *
|
|
|
|
ApplyRetrieveRule(Query *parsetree,
|
|
|
|
RewriteRule *rule,
|
|
|
|
int rt_index,
|
|
|
|
bool relation_level,
|
|
|
|
Relation relation,
|
2003-02-26 00:47:43 +01:00
|
|
|
List *activeRIRs)
|
2000-09-29 20:21:41 +02:00
|
|
|
{
|
|
|
|
Query *rule_action;
|
|
|
|
RangeTblEntry *rte,
|
|
|
|
*subrte;
|
2006-04-30 20:30:40 +02:00
|
|
|
RowMarkClause *rc;
|
2000-04-12 19:17:23 +02:00
|
|
|
|
2004-05-31 01:40:41 +02:00
|
|
|
if (list_length(rule->actions) != 1)
|
2003-07-25 02:01:09 +02:00
|
|
|
elog(ERROR, "expected just one rule action");
|
2000-09-29 20:21:41 +02:00
|
|
|
if (rule->qual != NULL)
|
2003-07-25 02:01:09 +02:00
|
|
|
elog(ERROR, "cannot handle qualified ON SELECT rule");
|
2001-03-22 05:01:46 +01:00
|
|
|
if (!relation_level)
|
2003-07-25 02:01:09 +02:00
|
|
|
elog(ERROR, "cannot handle per-attribute ON SELECT rule");
|
2000-04-12 19:17:23 +02:00
|
|
|
|
1999-10-02 06:42:04 +02:00
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* Make a modifiable copy of the view query, and acquire needed locks on
|
|
|
|
* the relations it mentions.
|
1999-10-02 06:42:04 +02:00
|
|
|
*/
|
2004-05-26 06:41:50 +02:00
|
|
|
rule_action = copyObject(linitial(rule->actions));
|
1998-10-02 18:28:04 +02:00
|
|
|
|
2005-06-04 01:05:30 +02:00
|
|
|
AcquireRewriteLocks(rule_action);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Recursively expand any view references inside the view.
|
|
|
|
*/
|
2003-02-26 00:47:43 +01:00
|
|
|
rule_action = fireRIRrules(rule_action, activeRIRs);
|
1999-11-15 03:00:15 +01:00
|
|
|
|
2000-04-12 19:17:23 +02:00
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* VIEWs are really easy --- just plug the view query in as a subselect,
|
|
|
|
* replacing the relation's original RTE.
|
1999-10-01 06:08:24 +02:00
|
|
|
*/
|
2000-09-29 20:21:41 +02:00
|
|
|
rte = rt_fetch(rt_index, parsetree->rtable);
|
1998-10-02 18:28:04 +02:00
|
|
|
|
2002-03-12 01:52:10 +01:00
|
|
|
rte->rtekind = RTE_SUBQUERY;
|
2000-09-29 20:21:41 +02:00
|
|
|
rte->relid = InvalidOid;
|
|
|
|
rte->subquery = rule_action;
|
|
|
|
rte->inh = false; /* must not be set for a subquery */
|
2000-04-12 19:17:23 +02:00
|
|
|
|
1999-11-15 03:00:15 +01:00
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* We move the view's permission check data down to its rangetable. The
|
|
|
|
* checks will actually be done against the *OLD* entry therein.
|
1999-10-01 06:08:24 +02:00
|
|
|
*/
|
2000-09-29 20:21:41 +02:00
|
|
|
subrte = rt_fetch(PRS2_OLD_VARNO, rule_action->rtable);
|
|
|
|
Assert(subrte->relid == relation->rd_id);
|
2004-01-15 00:01:55 +01:00
|
|
|
subrte->requiredPerms = rte->requiredPerms;
|
2001-05-03 19:47:49 +02:00
|
|
|
subrte->checkAsUser = rte->checkAsUser;
|
2009-01-22 21:16:10 +01:00
|
|
|
subrte->selectedCols = rte->selectedCols;
|
|
|
|
subrte->modifiedCols = rte->modifiedCols;
|
1998-10-02 18:28:04 +02:00
|
|
|
|
2004-01-15 00:01:55 +01:00
|
|
|
rte->requiredPerms = 0; /* no permission check on subquery itself */
|
2005-06-28 07:09:14 +02:00
|
|
|
rte->checkAsUser = InvalidOid;
|
2009-01-22 21:16:10 +01:00
|
|
|
rte->selectedCols = NULL;
|
|
|
|
rte->modifiedCols = NULL;
|
2000-04-12 19:17:23 +02:00
|
|
|
|
1999-10-01 06:08:24 +02:00
|
|
|
/*
|
2005-04-28 23:47:18 +02:00
|
|
|
* FOR UPDATE/SHARE of view?
|
1999-10-01 06:08:24 +02: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.
2009-10-26 03:26:45 +01:00
|
|
|
if ((rc = get_parse_rowmark(parsetree, rt_index)) != NULL)
|
1999-10-01 06:08:24 +02:00
|
|
|
{
|
2000-09-12 23:07:18 +02:00
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* Remove the view from the list of rels that will actually be marked
|
2006-04-30 20:30:40 +02:00
|
|
|
* FOR UPDATE/SHARE by the executor. It will still be access-checked
|
2005-10-15 04:49:52 +02:00
|
|
|
* for write access, though.
|
2000-09-12 23:07:18 +02:00
|
|
|
*/
|
2006-04-30 20:30:40 +02:00
|
|
|
parsetree->rowMarks = list_delete_ptr(parsetree->rowMarks, rc);
|
1999-05-25 18:15:34 +02:00
|
|
|
|
|
|
|
/*
|
2005-04-28 23:47:18 +02:00
|
|
|
* Set up the view's referenced tables as if FOR UPDATE/SHARE.
|
1999-01-21 17:08:55 +01:00
|
|
|
*/
|
2007-03-01 19:50:28 +01:00
|
|
|
markQueryForLocking(rule_action, (Node *) rule_action->jointree,
|
|
|
|
rc->forUpdate, rc->noWait);
|
1999-01-21 17:08:55 +01:00
|
|
|
}
|
|
|
|
|
1999-10-01 06:08:24 +02:00
|
|
|
return parsetree;
|
1998-10-02 18:28:04 +02:00
|
|
|
}
|
|
|
|
|
2000-12-07 02:22:25 +01:00
|
|
|
/*
|
2005-04-28 23:47:18 +02:00
|
|
|
* Recursively mark all relations used by a view as FOR UPDATE/SHARE.
|
2000-12-07 02:22:25 +01:00
|
|
|
*
|
|
|
|
* This may generate an invalid query, eg if some sub-query uses an
|
|
|
|
* aggregate. We leave it to the planner to detect that.
|
|
|
|
*
|
2007-03-01 19:50:28 +01:00
|
|
|
* NB: this must agree with the parser's transformLockingClause() routine.
|
|
|
|
* However, unlike the parser we have to be careful not to mark a view's
|
|
|
|
* OLD and NEW rels for updating. The best way to handle that seems to be
|
|
|
|
* to scan the jointree to determine which rels are used.
|
2000-12-07 02:22:25 +01:00
|
|
|
*/
|
|
|
|
static void
|
2007-03-01 19:50:28 +01:00
|
|
|
markQueryForLocking(Query *qry, Node *jtnode, bool forUpdate, bool noWait)
|
2000-12-07 02:22:25 +01:00
|
|
|
{
|
2007-03-01 19:50:28 +01:00
|
|
|
if (jtnode == NULL)
|
|
|
|
return;
|
|
|
|
if (IsA(jtnode, RangeTblRef))
|
2000-12-07 02:22:25 +01:00
|
|
|
{
|
2007-03-01 19:50:28 +01:00
|
|
|
int rti = ((RangeTblRef *) jtnode)->rtindex;
|
|
|
|
RangeTblEntry *rte = rt_fetch(rti, qry->rtable);
|
2000-12-07 02:22:25 +01:00
|
|
|
|
2002-03-12 01:52:10 +01:00
|
|
|
if (rte->rtekind == RTE_RELATION)
|
2000-12-07 02:22:25 +01:00
|
|
|
{
|
2006-04-30 20:30:40 +02:00
|
|
|
applyLockingClause(qry, rti, forUpdate, noWait);
|
2004-01-15 00:01:55 +01:00
|
|
|
rte->requiredPerms |= ACL_SELECT_FOR_UPDATE;
|
2000-12-07 02:22:25 +01:00
|
|
|
}
|
2002-03-12 01:52:10 +01:00
|
|
|
else if (rte->rtekind == RTE_SUBQUERY)
|
|
|
|
{
|
2005-04-28 23:47:18 +02:00
|
|
|
/* FOR UPDATE/SHARE of subquery is propagated to subquery's rels */
|
2007-03-01 19:50:28 +01:00
|
|
|
markQueryForLocking(rte->subquery, (Node *) rte->subquery->jointree,
|
|
|
|
forUpdate, noWait);
|
2002-03-12 01:52:10 +01:00
|
|
|
}
|
2009-10-27 18:11:18 +01:00
|
|
|
/* other RTE types are unaffected by FOR UPDATE */
|
2000-12-07 02:22:25 +01:00
|
|
|
}
|
2007-03-01 19:50:28 +01:00
|
|
|
else if (IsA(jtnode, FromExpr))
|
|
|
|
{
|
|
|
|
FromExpr *f = (FromExpr *) jtnode;
|
|
|
|
ListCell *l;
|
|
|
|
|
|
|
|
foreach(l, f->fromlist)
|
|
|
|
markQueryForLocking(qry, lfirst(l), forUpdate, noWait);
|
|
|
|
}
|
|
|
|
else if (IsA(jtnode, JoinExpr))
|
|
|
|
{
|
|
|
|
JoinExpr *j = (JoinExpr *) jtnode;
|
|
|
|
|
|
|
|
markQueryForLocking(qry, j->larg, forUpdate, noWait);
|
|
|
|
markQueryForLocking(qry, j->rarg, forUpdate, noWait);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
elog(ERROR, "unrecognized node type: %d",
|
|
|
|
(int) nodeTag(jtnode));
|
2000-12-07 02:22:25 +01:00
|
|
|
}
|
|
|
|
|
1998-10-02 18:28:04 +02:00
|
|
|
|
1999-10-01 06:08:24 +02:00
|
|
|
/*
|
2000-09-29 20:21:41 +02:00
|
|
|
* fireRIRonSubLink -
|
|
|
|
* Apply fireRIRrules() to each SubLink (subselect in expression) found
|
|
|
|
* in the given tree.
|
1999-10-01 06:08:24 +02:00
|
|
|
*
|
|
|
|
* NOTE: although this has the form of a walker, we cheat and modify the
|
2000-04-12 19:17:23 +02:00
|
|
|
* SubLink nodes in-place. It is caller's responsibility to ensure that
|
1999-10-01 06:08:24 +02:00
|
|
|
* no unwanted side-effects occur!
|
2000-09-12 23:07:18 +02:00
|
|
|
*
|
|
|
|
* This is unlike most of the other routines that recurse into subselects,
|
|
|
|
* because we must take control at the SubLink node in order to replace
|
|
|
|
* the SubLink's subselect link with the possibly-rewritten subquery.
|
1999-10-01 06:08:24 +02:00
|
|
|
*/
|
|
|
|
static bool
|
2003-02-26 00:47:43 +01:00
|
|
|
fireRIRonSubLink(Node *node, List *activeRIRs)
|
1998-10-02 18:28:04 +02:00
|
|
|
{
|
|
|
|
if (node == NULL)
|
1999-10-01 06:08:24 +02:00
|
|
|
return false;
|
|
|
|
if (IsA(node, SubLink))
|
1999-05-25 18:15:34 +02:00
|
|
|
{
|
1999-10-01 06:08:24 +02:00
|
|
|
SubLink *sub = (SubLink *) node;
|
|
|
|
|
|
|
|
/* Do what we came for */
|
2003-02-26 00:47:43 +01:00
|
|
|
sub->subselect = (Node *) fireRIRrules((Query *) sub->subselect,
|
|
|
|
activeRIRs);
|
2000-09-12 23:07:18 +02:00
|
|
|
/* Fall through to process lefthand args of SubLink */
|
1998-10-02 18:28:04 +02:00
|
|
|
}
|
2001-03-22 05:01:46 +01:00
|
|
|
|
2000-09-12 23:07:18 +02:00
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* Do NOT recurse into Query nodes, because fireRIRrules already processed
|
|
|
|
* subselects of subselects for us.
|
2000-09-12 23:07:18 +02:00
|
|
|
*/
|
2000-09-29 20:21:41 +02:00
|
|
|
return expression_tree_walker(node, fireRIRonSubLink,
|
2003-02-26 00:47:43 +01:00
|
|
|
(void *) activeRIRs);
|
1998-10-02 18:28:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* fireRIRrules -
|
|
|
|
* Apply all RIR rules on each rangetable entry in a query
|
|
|
|
*/
|
|
|
|
static Query *
|
2003-02-26 00:47:43 +01:00
|
|
|
fireRIRrules(Query *parsetree, List *activeRIRs)
|
1998-10-02 18:28:04 +02:00
|
|
|
{
|
1999-05-25 18:15:34 +02:00
|
|
|
int rt_index;
|
2008-10-04 23:56:55 +02:00
|
|
|
ListCell *lc;
|
1998-10-02 18:28:04 +02:00
|
|
|
|
2000-04-12 19:17:23 +02:00
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* don't try to convert this into a foreach loop, because rtable list can
|
|
|
|
* get changed each time through...
|
1999-10-07 06:23:24 +02:00
|
|
|
*/
|
1998-10-02 18:28:04 +02:00
|
|
|
rt_index = 0;
|
2004-05-31 01:40:41 +02:00
|
|
|
while (rt_index < list_length(parsetree->rtable))
|
1999-05-25 18:15:34 +02:00
|
|
|
{
|
2000-11-08 23:10:03 +01:00
|
|
|
RangeTblEntry *rte;
|
|
|
|
Relation rel;
|
|
|
|
List *locks;
|
|
|
|
RuleLock *rules;
|
|
|
|
RewriteRule *rule;
|
|
|
|
int i;
|
|
|
|
|
1998-10-02 18:28:04 +02:00
|
|
|
++rt_index;
|
|
|
|
|
1999-11-01 06:18:31 +01:00
|
|
|
rte = rt_fetch(rt_index, parsetree->rtable);
|
1999-05-25 15:16:10 +02:00
|
|
|
|
2000-09-29 20:21:41 +02:00
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* A subquery RTE can't have associated rules, so there's nothing to
|
|
|
|
* do to this level of the query, but we must recurse into the
|
2000-09-29 20:21:41 +02:00
|
|
|
* subquery to expand any rule references in it.
|
|
|
|
*/
|
2002-03-12 01:52:10 +01:00
|
|
|
if (rte->rtekind == RTE_SUBQUERY)
|
2000-09-29 20:21:41 +02:00
|
|
|
{
|
2003-02-26 00:47:43 +01:00
|
|
|
rte->subquery = fireRIRrules(rte->subquery, activeRIRs);
|
2000-09-29 20:21:41 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2002-03-12 01:52:10 +01:00
|
|
|
/*
|
|
|
|
* Joins and other non-relation RTEs can be ignored completely.
|
|
|
|
*/
|
|
|
|
if (rte->rtekind != RTE_RELATION)
|
|
|
|
continue;
|
|
|
|
|
1999-10-07 06:23:24 +02:00
|
|
|
/*
|
2000-04-20 02:31:49 +02:00
|
|
|
* If the table is not referenced in the query, then we ignore it.
|
|
|
|
* This prevents infinite expansion loop due to new rtable entries
|
|
|
|
* inserted by expansion of a rule. A table is referenced if it is
|
2005-10-15 04:49:52 +02:00
|
|
|
* part of the join set (a source table), or is referenced by any Var
|
|
|
|
* nodes, or is the result table.
|
1999-10-07 06:23:24 +02:00
|
|
|
*/
|
2005-05-29 20:34:57 +02:00
|
|
|
if (rt_index != parsetree->resultRelation &&
|
|
|
|
!rangeTableEntry_used((Node *) parsetree, rt_index, 0))
|
1998-10-02 18:28:04 +02:00
|
|
|
continue;
|
1999-05-25 18:15:34 +02:00
|
|
|
|
2000-11-08 23:10:03 +01:00
|
|
|
/*
|
2005-06-04 01:05:30 +02:00
|
|
|
* We can use NoLock here since either the parser or
|
|
|
|
* AcquireRewriteLocks should have locked the rel already.
|
2000-11-08 23:10:03 +01:00
|
|
|
*/
|
2005-06-04 01:05:30 +02:00
|
|
|
rel = heap_open(rte->relid, NoLock);
|
2001-04-17 02:32:58 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Collect the RIR rules that we must apply
|
|
|
|
*/
|
1999-10-07 06:23:24 +02:00
|
|
|
rules = rel->rd_rules;
|
|
|
|
if (rules == NULL)
|
1999-05-25 18:15:34 +02:00
|
|
|
{
|
2000-11-08 23:10:03 +01:00
|
|
|
heap_close(rel, NoLock);
|
1998-10-02 18:28:04 +02:00
|
|
|
continue;
|
|
|
|
}
|
1999-10-07 06:23:24 +02:00
|
|
|
locks = NIL;
|
1999-05-25 18:15:34 +02:00
|
|
|
for (i = 0; i < rules->numLocks; i++)
|
|
|
|
{
|
1998-10-02 18:28:04 +02:00
|
|
|
rule = rules->rules[i];
|
|
|
|
if (rule->event != CMD_SELECT)
|
|
|
|
continue;
|
1999-05-25 18:15:34 +02:00
|
|
|
|
1999-10-07 06:23:24 +02:00
|
|
|
if (rule->attrno > 0)
|
|
|
|
{
|
|
|
|
/* per-attr rule; do we need it? */
|
2000-09-12 23:07:18 +02:00
|
|
|
if (!attribute_used((Node *) parsetree, rt_index,
|
2000-04-12 19:17:23 +02:00
|
|
|
rule->attrno, 0))
|
1999-10-07 06:23:24 +02:00
|
|
|
continue;
|
|
|
|
}
|
1998-10-02 18:28:04 +02:00
|
|
|
|
|
|
|
locks = lappend(locks, rule);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2003-02-26 00:47:43 +01:00
|
|
|
* If we found any, apply them --- but first check for recursion!
|
1998-10-02 18:28:04 +02:00
|
|
|
*/
|
2003-02-26 00:47:43 +01:00
|
|
|
if (locks != NIL)
|
1999-05-25 18:15:34 +02:00
|
|
|
{
|
2004-05-26 06:41:50 +02:00
|
|
|
ListCell *l;
|
2003-02-26 00:47:43 +01:00
|
|
|
|
2004-05-31 01:40:41 +02:00
|
|
|
if (list_member_oid(activeRIRs, RelationGetRelid(rel)))
|
2003-07-25 02:01:09 +02:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
|
|
|
|
errmsg("infinite recursion detected in rules for relation \"%s\"",
|
|
|
|
RelationGetRelationName(rel))));
|
2004-05-31 01:40:41 +02:00
|
|
|
activeRIRs = lcons_oid(RelationGetRelid(rel), activeRIRs);
|
2003-02-26 00:47:43 +01:00
|
|
|
|
|
|
|
foreach(l, locks)
|
|
|
|
{
|
|
|
|
rule = lfirst(l);
|
|
|
|
|
|
|
|
parsetree = ApplyRetrieveRule(parsetree,
|
|
|
|
rule,
|
|
|
|
rt_index,
|
|
|
|
rule->attrno == -1,
|
|
|
|
rel,
|
2004-05-29 07:55:13 +02:00
|
|
|
activeRIRs);
|
2003-02-26 00:47:43 +01:00
|
|
|
}
|
2004-05-29 07:55:13 +02:00
|
|
|
|
|
|
|
activeRIRs = list_delete_first(activeRIRs);
|
1998-10-02 18:28:04 +02:00
|
|
|
}
|
|
|
|
|
2000-11-08 23:10:03 +01:00
|
|
|
heap_close(rel, NoLock);
|
1998-10-02 18:28:04 +02:00
|
|
|
}
|
|
|
|
|
2008-10-04 23:56:55 +02:00
|
|
|
/* Recurse into subqueries in WITH */
|
|
|
|
foreach(lc, parsetree->cteList)
|
|
|
|
{
|
|
|
|
CommonTableExpr *cte = (CommonTableExpr *) lfirst(lc);
|
|
|
|
|
|
|
|
cte->ctequery = (Node *)
|
|
|
|
fireRIRrules((Query *) cte->ctequery, activeRIRs);
|
|
|
|
}
|
|
|
|
|
2000-09-29 20:21:41 +02:00
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* Recurse into sublink subqueries, too. But we already did the ones in
|
2008-10-04 23:56:55 +02:00
|
|
|
* the rtable and cteList.
|
2000-09-29 20:21:41 +02:00
|
|
|
*/
|
|
|
|
if (parsetree->hasSubLinks)
|
2003-02-26 00:47:43 +01:00
|
|
|
query_tree_walker(parsetree, fireRIRonSubLink, (void *) activeRIRs,
|
2008-10-04 23:56:55 +02:00
|
|
|
QTW_IGNORE_RC_SUBQUERIES);
|
1998-10-02 18:28:04 +02:00
|
|
|
|
|
|
|
return parsetree;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-12-05 20:15:10 +01:00
|
|
|
/*
|
2002-10-20 02:58:55 +02:00
|
|
|
* Modify the given query by adding 'AND rule_qual IS NOT TRUE' to its
|
|
|
|
* qualification. This is used to generate suitable "else clauses" for
|
|
|
|
* conditional INSTEAD rules. (Unfortunately we must use "x IS NOT TRUE",
|
|
|
|
* not just "NOT x" which the planner is much smarter about, else we will
|
|
|
|
* do the wrong thing when the qual evaluates to NULL.)
|
2000-12-05 20:15:10 +01:00
|
|
|
*
|
2001-03-22 05:01:46 +01:00
|
|
|
* The rule_qual may contain references to OLD or NEW. OLD references are
|
2000-12-05 20:15:10 +01:00
|
|
|
* replaced by references to the specified rt_index (the relation that the
|
|
|
|
* rule applies to). NEW references are only possible for INSERT and UPDATE
|
|
|
|
* queries on the relation itself, and so they should be replaced by copies
|
|
|
|
* of the related entries in the query's own targetlist.
|
|
|
|
*/
|
1997-09-08 04:41:22 +02:00
|
|
|
static Query *
|
2002-10-20 02:58:55 +02:00
|
|
|
CopyAndAddInvertedQual(Query *parsetree,
|
|
|
|
Node *rule_qual,
|
|
|
|
int rt_index,
|
|
|
|
CmdType event)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
2005-06-04 01:05:30 +02:00
|
|
|
/* Don't scribble on the passed qual (it's in the relcache!) */
|
2000-12-05 20:15:10 +01:00
|
|
|
Node *new_qual = (Node *) copyObject(rule_qual);
|
|
|
|
|
2005-06-04 01:05:30 +02:00
|
|
|
/*
|
|
|
|
* In case there are subqueries in the qual, acquire necessary locks and
|
|
|
|
* fix any deleted JOIN RTE entries. (This is somewhat redundant with
|
2005-10-15 04:49:52 +02:00
|
|
|
* rewriteRuleAction, but not entirely ... consider restructuring so that
|
|
|
|
* we only need to process the qual this way once.)
|
2005-06-04 01:05:30 +02:00
|
|
|
*/
|
|
|
|
(void) acquireLocksOnSubLinks(new_qual, NULL);
|
|
|
|
|
2000-12-05 20:15:10 +01:00
|
|
|
/* Fix references to OLD */
|
|
|
|
ChangeVarNodes(new_qual, PRS2_OLD_VARNO, rt_index, 0);
|
|
|
|
/* Fix references to NEW */
|
|
|
|
if (event == CMD_INSERT || event == CMD_UPDATE)
|
|
|
|
new_qual = ResolveNew(new_qual,
|
|
|
|
PRS2_NEW_VARNO,
|
|
|
|
0,
|
2005-06-04 21:19:42 +02:00
|
|
|
rt_fetch(rt_index, parsetree->rtable),
|
2000-12-05 20:15:10 +01:00
|
|
|
parsetree->targetList,
|
|
|
|
event,
|
Fix subquery pullup to wrap a PlaceHolderVar around the entire RowExpr
that's generated for a whole-row Var referencing the subquery, when the
subquery is in the nullable side of an outer join. The previous coding
instead put PlaceHolderVars around the elements of the RowExpr. The effect
was that when the outer join made the subquery outputs go to null, the
whole-row Var produced ROW(NULL,NULL,...) rather than just NULL. There
are arguments afoot about whether those things ought to be semantically
indistinguishable, but for the moment they are not entirely so, and the
planner needs to take care that its machinations preserve the difference.
Per bug #5025.
Making this feasible required refactoring ResolveNew() to allow more caller
control over what is substituted for a Var. I chose to make ResolveNew()
a wrapper around a new general-purpose function replace_rte_variables().
I also fixed the ancient bogosity that ResolveNew might fail to set
a query's hasSubLinks field after inserting a SubLink in it. Although
all current callers make sure that happens anyway, we've had bugs of that
sort before, and it seemed like a good time to install a proper solution.
Back-patch to 8.4. The problem can be demonstrated clear back to 8.0,
but the fix would be too invasive in earlier branches; not to mention
that people may be depending on the subtly-incorrect behavior. The
8.4 series is new enough that fixing this probably won't cause complaints,
but it might in older branches. Also, 8.4 shows the incorrect behavior
in more cases than older branches do, because it is able to flatten
subqueries in more cases.
2009-09-02 19:52:24 +02:00
|
|
|
rt_index,
|
|
|
|
&parsetree->hasSubLinks);
|
2000-12-05 20:15:10 +01:00
|
|
|
/* And attach the fixed qual */
|
2005-06-04 01:05:30 +02:00
|
|
|
AddInvertedQual(parsetree, new_qual);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2005-06-04 01:05:30 +02:00
|
|
|
return parsetree;
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
1997-09-07 07:04:48 +02:00
|
|
|
* fireRules -
|
1998-08-18 02:49:04 +02:00
|
|
|
* Iterate through rule locks applying rules.
|
1998-09-01 06:40:42 +02:00
|
|
|
*
|
2002-10-19 21:00:47 +02:00
|
|
|
* Input arguments:
|
|
|
|
* parsetree - original query
|
|
|
|
* rt_index - RT index of result relation in original query
|
|
|
|
* event - type of rule event
|
|
|
|
* locks - list of rules to fire
|
|
|
|
* Output arguments:
|
|
|
|
* *instead_flag - set TRUE if any unqualified INSTEAD rule is found
|
|
|
|
* (must be initialized to FALSE)
|
2006-09-02 19:06:52 +02:00
|
|
|
* *returning_flag - set TRUE if we rewrite RETURNING clause in any rule
|
|
|
|
* (must be initialized to FALSE)
|
2002-10-19 21:00:47 +02:00
|
|
|
* *qual_product - filled with modified original query if any qualified
|
|
|
|
* INSTEAD rule is found (must be initialized to NULL)
|
|
|
|
* Return value:
|
|
|
|
* list of rule actions adjusted for use with this query
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
2002-10-19 21:00:47 +02:00
|
|
|
* Qualified INSTEAD rules generate their action with the qualification
|
|
|
|
* condition added. They also generate a modified version of the original
|
|
|
|
* query with the negated qualification added, so that it will run only for
|
|
|
|
* rows that the qualified action doesn't act on. (If there are multiple
|
|
|
|
* qualified INSTEAD rules, we AND all the negated quals onto a single
|
|
|
|
* modified original query.) We won't execute the original, unmodified
|
2003-08-04 02:43:34 +02:00
|
|
|
* query if we find either qualified or unqualified INSTEAD rules. If
|
2002-10-19 21:00:47 +02:00
|
|
|
* we find both, the modified original query is discarded too.
|
1996-07-09 08:22:35 +02:00
|
|
|
*/
|
1997-09-08 04:41:22 +02:00
|
|
|
static List *
|
1997-09-08 23:56:23 +02:00
|
|
|
fireRules(Query *parsetree,
|
1997-09-07 07:04:48 +02:00
|
|
|
int rt_index,
|
|
|
|
CmdType event,
|
1997-09-08 23:56:23 +02:00
|
|
|
List *locks,
|
2002-10-19 21:00:47 +02:00
|
|
|
bool *instead_flag,
|
2006-09-02 19:06:52 +02:00
|
|
|
bool *returning_flag,
|
2002-10-19 21:00:47 +02:00
|
|
|
Query **qual_product)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
List *results = NIL;
|
2004-05-26 06:41:50 +02:00
|
|
|
ListCell *l;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2004-05-26 06:41:50 +02:00
|
|
|
foreach(l, locks)
|
1997-09-07 07:04:48 +02:00
|
|
|
{
|
2004-05-26 06:41:50 +02:00
|
|
|
RewriteRule *rule_lock = (RewriteRule *) lfirst(l);
|
2002-10-19 21:00:47 +02:00
|
|
|
Node *event_qual = rule_lock->qual;
|
|
|
|
List *actions = rule_lock->actions;
|
2003-08-04 02:43:34 +02:00
|
|
|
QuerySource qsrc;
|
2004-05-26 06:41:50 +02:00
|
|
|
ListCell *r;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2002-10-15 00:14:35 +02:00
|
|
|
/* Determine correct QuerySource value for actions */
|
|
|
|
if (rule_lock->isInstead)
|
|
|
|
{
|
|
|
|
if (event_qual != NULL)
|
|
|
|
qsrc = QSRC_QUAL_INSTEAD_RULE;
|
|
|
|
else
|
2002-10-19 21:00:47 +02:00
|
|
|
{
|
2002-10-15 00:14:35 +02:00
|
|
|
qsrc = QSRC_INSTEAD_RULE;
|
2003-08-04 02:43:34 +02:00
|
|
|
*instead_flag = true; /* report unqualified INSTEAD */
|
2002-10-19 21:00:47 +02:00
|
|
|
}
|
2002-10-15 00:14:35 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
qsrc = QSRC_NON_INSTEAD_RULE;
|
|
|
|
|
|
|
|
if (qsrc == QSRC_QUAL_INSTEAD_RULE)
|
1998-09-01 06:40:42 +02:00
|
|
|
{
|
2001-03-22 07:16:21 +01:00
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* If there are INSTEAD rules with qualifications, the original
|
|
|
|
* query is still performed. But all the negated rule
|
|
|
|
* qualifications of the INSTEAD rules are added so it does its
|
|
|
|
* actions only in cases where the rule quals of all INSTEAD rules
|
|
|
|
* are false. Think of it as the default action in a case. We save
|
|
|
|
* this in *qual_product so RewriteQuery() can add it to the query
|
|
|
|
* list after we mangled it up enough.
|
2002-10-19 21:00:47 +02:00
|
|
|
*
|
2003-08-04 02:43:34 +02:00
|
|
|
* If we have already found an unqualified INSTEAD rule, then
|
|
|
|
* *qual_product won't be used, so don't bother building it.
|
1998-08-18 02:49:04 +02:00
|
|
|
*/
|
2003-08-04 02:43:34 +02:00
|
|
|
if (!*instead_flag)
|
2002-10-19 21:00:47 +02:00
|
|
|
{
|
|
|
|
if (*qual_product == NULL)
|
2005-06-04 01:05:30 +02:00
|
|
|
*qual_product = copyObject(parsetree);
|
2002-10-20 02:58:55 +02:00
|
|
|
*qual_product = CopyAndAddInvertedQual(*qual_product,
|
|
|
|
event_qual,
|
|
|
|
rt_index,
|
|
|
|
event);
|
2002-10-19 21:00:47 +02:00
|
|
|
}
|
1998-08-18 02:49:04 +02:00
|
|
|
}
|
|
|
|
|
2002-10-15 00:14:35 +02:00
|
|
|
/* Now process the rule's actions and add them to the result list */
|
1997-09-07 07:04:48 +02:00
|
|
|
foreach(r, actions)
|
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
Query *rule_action = lfirst(r);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
1998-08-18 02:49:04 +02:00
|
|
|
if (rule_action->commandType == CMD_NOTHING)
|
|
|
|
continue;
|
|
|
|
|
2001-06-13 20:56:30 +02:00
|
|
|
rule_action = rewriteRuleAction(parsetree, rule_action,
|
2006-09-02 19:06:52 +02:00
|
|
|
event_qual, rt_index, event,
|
|
|
|
returning_flag);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2002-10-15 00:14:35 +02:00
|
|
|
rule_action->querySource = qsrc;
|
2003-05-02 22:54:36 +02:00
|
|
|
rule_action->canSetTag = false; /* might change later */
|
2002-10-15 00:14:35 +02:00
|
|
|
|
2001-06-13 20:56:30 +02:00
|
|
|
results = lappend(results, rule_action);
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|
|
|
|
}
|
2002-10-15 00:14:35 +02:00
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
return results;
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
1998-08-18 02:49:04 +02:00
|
|
|
|
2002-10-19 21:00:47 +02:00
|
|
|
/*
|
2003-02-26 00:47:43 +01:00
|
|
|
* RewriteQuery -
|
|
|
|
* rewrites the query and apply the rules again on the queries rewritten
|
2002-10-19 21:00:47 +02:00
|
|
|
*
|
2003-02-26 00:47:43 +01:00
|
|
|
* rewrite_events is a list of open query-rewrite actions, so we can detect
|
|
|
|
* infinite recursion.
|
2002-10-19 21:00:47 +02:00
|
|
|
*/
|
1997-09-08 04:41:22 +02:00
|
|
|
static List *
|
2003-02-26 00:47:43 +01:00
|
|
|
RewriteQuery(Query *parsetree, List *rewrite_events)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
2003-02-26 00:47:43 +01:00
|
|
|
CmdType event = parsetree->commandType;
|
|
|
|
bool instead = false;
|
2006-09-02 19:06:52 +02:00
|
|
|
bool returning = false;
|
2003-02-26 00:47:43 +01:00
|
|
|
Query *qual_product = NULL;
|
|
|
|
List *rewritten = NIL;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
1998-10-02 18:28:04 +02:00
|
|
|
/*
|
2003-02-26 00:47:43 +01:00
|
|
|
* If the statement is an update, insert or delete - fire rules on it.
|
|
|
|
*
|
2005-10-15 04:49:52 +02:00
|
|
|
* SELECT rules are handled later when we have all the queries that should
|
|
|
|
* get executed. Also, utilities aren't rewritten at all (do we still
|
|
|
|
* need that check?)
|
2002-04-05 07:47:05 +02:00
|
|
|
*/
|
2003-02-26 00:47:43 +01:00
|
|
|
if (event != CMD_SELECT && event != CMD_UTILITY)
|
|
|
|
{
|
|
|
|
int result_relation;
|
|
|
|
RangeTblEntry *rt_entry;
|
|
|
|
Relation rt_entry_relation;
|
|
|
|
List *locks;
|
2002-04-05 07:47:05 +02:00
|
|
|
|
2003-02-26 00:47:43 +01:00
|
|
|
result_relation = parsetree->resultRelation;
|
|
|
|
Assert(result_relation != 0);
|
|
|
|
rt_entry = rt_fetch(result_relation, parsetree->rtable);
|
|
|
|
Assert(rt_entry->rtekind == RTE_RELATION);
|
1998-08-18 02:49:04 +02:00
|
|
|
|
2003-02-26 00:47:43 +01:00
|
|
|
/*
|
2005-06-04 01:05:30 +02:00
|
|
|
* We can use NoLock here since either the parser or
|
|
|
|
* AcquireRewriteLocks should have locked the rel already.
|
2003-02-26 00:47:43 +01:00
|
|
|
*/
|
2005-06-04 01:05:30 +02:00
|
|
|
rt_entry_relation = heap_open(rt_entry->relid, NoLock);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2003-02-26 00:47:43 +01:00
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* If it's an INSERT or UPDATE, rewrite the targetlist into standard
|
|
|
|
* form. This will be needed by the planner anyway, and doing it now
|
|
|
|
* ensures that any references to NEW.field will behave sanely.
|
2003-02-26 00:47:43 +01:00
|
|
|
*/
|
2006-08-02 03:59:48 +02:00
|
|
|
if (event == CMD_UPDATE)
|
|
|
|
rewriteTargetList(parsetree, rt_entry_relation, NULL);
|
|
|
|
else if (event == CMD_INSERT)
|
|
|
|
{
|
|
|
|
RangeTblEntry *values_rte = NULL;
|
|
|
|
|
|
|
|
/*
|
2006-10-04 02:30:14 +02:00
|
|
|
* If it's an INSERT ... VALUES (...), (...), ... there will be a
|
|
|
|
* single RTE for the VALUES targetlists.
|
2006-08-02 03:59:48 +02:00
|
|
|
*/
|
|
|
|
if (list_length(parsetree->jointree->fromlist) == 1)
|
|
|
|
{
|
|
|
|
RangeTblRef *rtr = (RangeTblRef *) linitial(parsetree->jointree->fromlist);
|
|
|
|
|
|
|
|
if (IsA(rtr, RangeTblRef))
|
|
|
|
{
|
|
|
|
RangeTblEntry *rte = rt_fetch(rtr->rtindex,
|
|
|
|
parsetree->rtable);
|
|
|
|
|
|
|
|
if (rte->rtekind == RTE_VALUES)
|
|
|
|
values_rte = rte;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (values_rte)
|
|
|
|
{
|
2006-10-04 02:30:14 +02:00
|
|
|
List *attrnos;
|
2006-08-02 03:59:48 +02:00
|
|
|
|
|
|
|
/* Process the main targetlist ... */
|
|
|
|
rewriteTargetList(parsetree, rt_entry_relation, &attrnos);
|
|
|
|
/* ... and the VALUES expression lists */
|
|
|
|
rewriteValuesRTE(values_rte, rt_entry_relation, attrnos);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Process just the main targetlist */
|
|
|
|
rewriteTargetList(parsetree, rt_entry_relation, NULL);
|
|
|
|
}
|
|
|
|
}
|
2000-06-30 09:04:23 +02:00
|
|
|
|
2003-02-26 00:47:43 +01:00
|
|
|
/*
|
|
|
|
* Collect and apply the appropriate rules.
|
|
|
|
*/
|
|
|
|
locks = matchLocks(event, rt_entry_relation->rd_rules,
|
|
|
|
result_relation, parsetree);
|
1996-07-09 08:22:35 +02:00
|
|
|
|
2003-02-26 00:47:43 +01:00
|
|
|
if (locks != NIL)
|
|
|
|
{
|
|
|
|
List *product_queries;
|
1998-10-02 18:28:04 +02:00
|
|
|
|
2003-02-26 00:47:43 +01:00
|
|
|
product_queries = fireRules(parsetree,
|
|
|
|
result_relation,
|
|
|
|
event,
|
|
|
|
locks,
|
|
|
|
&instead,
|
2006-09-02 19:06:52 +02:00
|
|
|
&returning,
|
2003-02-26 00:47:43 +01:00
|
|
|
&qual_product);
|
1996-07-09 08:22:35 +02:00
|
|
|
|
2003-02-26 00:47:43 +01:00
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* If we got any product queries, recursively rewrite them --- but
|
|
|
|
* first check for recursion!
|
2003-02-26 00:47:43 +01:00
|
|
|
*/
|
|
|
|
if (product_queries != NIL)
|
|
|
|
{
|
2004-08-29 07:07:03 +02:00
|
|
|
ListCell *n;
|
|
|
|
rewrite_event *rev;
|
1996-07-09 08:22:35 +02:00
|
|
|
|
2003-02-26 00:47:43 +01:00
|
|
|
foreach(n, rewrite_events)
|
|
|
|
{
|
|
|
|
rev = (rewrite_event *) lfirst(n);
|
|
|
|
if (rev->relation == RelationGetRelid(rt_entry_relation) &&
|
|
|
|
rev->event == event)
|
2003-07-25 02:01:09 +02:00
|
|
|
ereport(ERROR,
|
2005-10-15 04:49:52 +02:00
|
|
|
(errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
|
|
|
|
errmsg("infinite recursion detected in rules for relation \"%s\"",
|
|
|
|
RelationGetRelationName(rt_entry_relation))));
|
2003-02-26 00:47:43 +01:00
|
|
|
}
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2003-02-26 00:47:43 +01:00
|
|
|
rev = (rewrite_event *) palloc(sizeof(rewrite_event));
|
|
|
|
rev->relation = RelationGetRelid(rt_entry_relation);
|
|
|
|
rev->event = event;
|
|
|
|
rewrite_events = lcons(rev, rewrite_events);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2003-02-26 00:47:43 +01:00
|
|
|
foreach(n, product_queries)
|
|
|
|
{
|
|
|
|
Query *pt = (Query *) lfirst(n);
|
|
|
|
List *newstuff;
|
1996-07-09 08:22:35 +02:00
|
|
|
|
2003-02-26 00:47:43 +01:00
|
|
|
newstuff = RewriteQuery(pt, rewrite_events);
|
2004-05-31 01:40:41 +02:00
|
|
|
rewritten = list_concat(rewritten, newstuff);
|
2003-02-26 00:47:43 +01:00
|
|
|
}
|
2004-11-20 18:59:31 +01:00
|
|
|
|
|
|
|
rewrite_events = list_delete_first(rewrite_events);
|
2003-02-26 00:47:43 +01:00
|
|
|
}
|
|
|
|
}
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2006-09-02 19:06:52 +02:00
|
|
|
/*
|
2006-10-04 02:30:14 +02:00
|
|
|
* If there is an INSTEAD, and the original query has a RETURNING, we
|
|
|
|
* have to have found a RETURNING in the rule(s), else fail. (Because
|
|
|
|
* DefineQueryRewrite only allows RETURNING in unconditional INSTEAD
|
|
|
|
* rules, there's no need to worry whether the substituted RETURNING
|
|
|
|
* will actually be executed --- it must be.)
|
2006-09-02 19:06:52 +02:00
|
|
|
*/
|
|
|
|
if ((instead || qual_product != NULL) &&
|
|
|
|
parsetree->returningList &&
|
|
|
|
!returning)
|
|
|
|
{
|
|
|
|
switch (event)
|
|
|
|
{
|
|
|
|
case CMD_INSERT:
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
|
2007-11-16 00:23:44 +01:00
|
|
|
errmsg("cannot perform INSERT RETURNING on relation \"%s\"",
|
|
|
|
RelationGetRelationName(rt_entry_relation)),
|
2006-09-02 19:06:52 +02:00
|
|
|
errhint("You need an unconditional ON INSERT DO INSTEAD rule with a RETURNING clause.")));
|
|
|
|
break;
|
|
|
|
case CMD_UPDATE:
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
|
2007-11-16 00:23:44 +01:00
|
|
|
errmsg("cannot perform UPDATE RETURNING on relation \"%s\"",
|
|
|
|
RelationGetRelationName(rt_entry_relation)),
|
2006-09-02 19:06:52 +02:00
|
|
|
errhint("You need an unconditional ON UPDATE DO INSTEAD rule with a RETURNING clause.")));
|
|
|
|
break;
|
|
|
|
case CMD_DELETE:
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
|
2007-11-16 00:23:44 +01:00
|
|
|
errmsg("cannot perform DELETE RETURNING on relation \"%s\"",
|
|
|
|
RelationGetRelationName(rt_entry_relation)),
|
2006-09-02 19:06:52 +02:00
|
|
|
errhint("You need an unconditional ON DELETE DO INSTEAD rule with a RETURNING clause.")));
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
elog(ERROR, "unrecognized commandType: %d",
|
|
|
|
(int) event);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-06-04 01:05:30 +02:00
|
|
|
heap_close(rt_entry_relation, NoLock);
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|
1998-08-18 02:49:04 +02:00
|
|
|
|
2001-03-22 07:16:21 +01:00
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* For INSERTs, the original query is done first; for UPDATE/DELETE, it is
|
|
|
|
* done last. This is needed because update and delete rule actions might
|
|
|
|
* not do anything if they are invoked after the update or delete is
|
|
|
|
* performed. The command counter increment between the query executions
|
|
|
|
* makes the deleted (and maybe the updated) tuples disappear so the scans
|
|
|
|
* for them in the rule actions cannot find them.
|
2002-10-19 21:00:47 +02:00
|
|
|
*
|
2003-08-04 02:43:34 +02:00
|
|
|
* If we found any unqualified INSTEAD, the original query is not done at
|
|
|
|
* all, in any form. Otherwise, we add the modified form if qualified
|
|
|
|
* INSTEADs were found, else the unmodified form.
|
1998-08-18 02:49:04 +02:00
|
|
|
*/
|
2002-10-19 21:00:47 +02:00
|
|
|
if (!instead)
|
2001-07-10 01:50:32 +02:00
|
|
|
{
|
2002-10-19 21:00:47 +02:00
|
|
|
if (parsetree->commandType == CMD_INSERT)
|
|
|
|
{
|
|
|
|
if (qual_product != NULL)
|
|
|
|
rewritten = lcons(qual_product, rewritten);
|
|
|
|
else
|
|
|
|
rewritten = lcons(parsetree, rewritten);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (qual_product != NULL)
|
|
|
|
rewritten = lappend(rewritten, qual_product);
|
|
|
|
else
|
|
|
|
rewritten = lappend(rewritten, parsetree);
|
|
|
|
}
|
2001-07-10 01:50:32 +02:00
|
|
|
}
|
1998-08-18 02:49:04 +02:00
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
return rewritten;
|
|
|
|
}
|
1998-10-02 18:28:04 +02:00
|
|
|
|
|
|
|
|
|
|
|
/*
|
2000-10-05 21:11:39 +02:00
|
|
|
* QueryRewrite -
|
|
|
|
* Primary entry point to the query rewriter.
|
|
|
|
* Rewrite one query via query rewrite system, possibly returning 0
|
|
|
|
* or many queries.
|
|
|
|
*
|
2005-06-04 01:05:30 +02:00
|
|
|
* NOTE: the parsetree must either have come straight from the parser,
|
|
|
|
* or have been scanned by AcquireRewriteLocks to acquire suitable locks.
|
1998-10-02 18:28:04 +02:00
|
|
|
*/
|
2000-10-05 21:11:39 +02:00
|
|
|
List *
|
|
|
|
QueryRewrite(Query *parsetree)
|
1998-10-02 18:28:04 +02:00
|
|
|
{
|
1999-05-25 18:15:34 +02:00
|
|
|
List *querylist;
|
|
|
|
List *results = NIL;
|
2004-05-26 06:41:50 +02:00
|
|
|
ListCell *l;
|
2003-05-02 22:54:36 +02:00
|
|
|
CmdType origCmdType;
|
|
|
|
bool foundOriginalQuery;
|
|
|
|
Query *lastInstead;
|
1998-10-02 18:28:04 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Step 1
|
|
|
|
*
|
|
|
|
* Apply all non-SELECT rules possibly getting 0 or many queries
|
|
|
|
*/
|
2003-02-26 00:47:43 +01:00
|
|
|
querylist = RewriteQuery(parsetree, NIL);
|
1998-10-02 18:28:04 +02:00
|
|
|
|
|
|
|
/*
|
1999-11-01 06:18:31 +01:00
|
|
|
* Step 2
|
1998-10-02 18:28:04 +02:00
|
|
|
*
|
|
|
|
* Apply all the RIR rules on each query
|
|
|
|
*/
|
1999-05-25 18:15:34 +02:00
|
|
|
foreach(l, querylist)
|
|
|
|
{
|
2001-03-22 05:01:46 +01:00
|
|
|
Query *query = (Query *) lfirst(l);
|
1999-05-25 18:15:34 +02:00
|
|
|
|
2003-02-26 00:47:43 +01:00
|
|
|
query = fireRIRrules(query, NIL);
|
2000-04-12 19:17:23 +02:00
|
|
|
|
1999-05-25 18:15:34 +02:00
|
|
|
/*
|
2000-10-05 21:11:39 +02:00
|
|
|
* If the query target was rewritten as a view, complain.
|
1999-05-25 18:15:34 +02:00
|
|
|
*/
|
2000-10-05 21:11:39 +02:00
|
|
|
if (query->resultRelation)
|
1999-05-25 18:15:34 +02:00
|
|
|
{
|
2000-10-05 21:11:39 +02:00
|
|
|
RangeTblEntry *rte = rt_fetch(query->resultRelation,
|
|
|
|
query->rtable);
|
1999-05-25 18:15:34 +02:00
|
|
|
|
2002-03-12 01:52:10 +01:00
|
|
|
if (rte->rtekind == RTE_SUBQUERY)
|
1999-05-25 18:15:34 +02:00
|
|
|
{
|
2000-10-05 21:11:39 +02:00
|
|
|
switch (query->commandType)
|
|
|
|
{
|
|
|
|
case CMD_INSERT:
|
2003-07-25 02:01:09 +02:00
|
|
|
ereport(ERROR,
|
2009-01-27 13:40:15 +01:00
|
|
|
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
|
|
|
|
errmsg("cannot insert into a view"),
|
2003-07-25 02:01:09 +02:00
|
|
|
errhint("You need an unconditional ON INSERT DO INSTEAD rule.")));
|
2000-10-05 21:11:39 +02:00
|
|
|
break;
|
|
|
|
case CMD_UPDATE:
|
2003-07-25 02:01:09 +02:00
|
|
|
ereport(ERROR,
|
2009-01-27 13:40:15 +01:00
|
|
|
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
|
|
|
|
errmsg("cannot update a view"),
|
2003-07-25 02:01:09 +02:00
|
|
|
errhint("You need an unconditional ON UPDATE DO INSTEAD rule.")));
|
2000-10-05 21:11:39 +02:00
|
|
|
break;
|
|
|
|
case CMD_DELETE:
|
2003-07-25 02:01:09 +02:00
|
|
|
ereport(ERROR,
|
2009-01-27 13:40:15 +01:00
|
|
|
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
|
|
|
|
errmsg("cannot delete from a view"),
|
2003-07-25 02:01:09 +02:00
|
|
|
errhint("You need an unconditional ON DELETE DO INSTEAD rule.")));
|
2000-10-05 21:11:39 +02:00
|
|
|
break;
|
|
|
|
default:
|
2003-07-25 02:01:09 +02:00
|
|
|
elog(ERROR, "unrecognized commandType: %d",
|
2000-10-05 21:11:39 +02:00
|
|
|
(int) query->commandType);
|
|
|
|
break;
|
|
|
|
}
|
1999-05-25 18:15:34 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-10-05 21:11:39 +02:00
|
|
|
results = lappend(results, query);
|
1999-05-25 18:15:34 +02:00
|
|
|
}
|
1999-05-13 09:29:22 +02:00
|
|
|
|
2003-05-02 22:54:36 +02:00
|
|
|
/*
|
|
|
|
* Step 3
|
|
|
|
*
|
2005-11-22 19:17:34 +01:00
|
|
|
* Determine which, if any, of the resulting queries is supposed to set
|
|
|
|
* the command-result tag; and update the canSetTag fields accordingly.
|
2003-05-02 22:54:36 +02:00
|
|
|
*
|
|
|
|
* If the original query is still in the list, it sets the command tag.
|
2005-10-15 04:49:52 +02:00
|
|
|
* Otherwise, the last INSTEAD query of the same kind as the original is
|
|
|
|
* allowed to set the tag. (Note these rules can leave us with no query
|
|
|
|
* setting the tag. The tcop code has to cope with this by setting up a
|
|
|
|
* default tag based on the original un-rewritten query.)
|
2003-05-02 22:54:36 +02:00
|
|
|
*
|
|
|
|
* The Asserts verify that at most one query in the result list is marked
|
2005-10-15 04:49:52 +02:00
|
|
|
* canSetTag. If we aren't checking asserts, we can fall out of the loop
|
|
|
|
* as soon as we find the original query.
|
2003-05-02 22:54:36 +02:00
|
|
|
*/
|
|
|
|
origCmdType = parsetree->commandType;
|
|
|
|
foundOriginalQuery = false;
|
|
|
|
lastInstead = NULL;
|
|
|
|
|
|
|
|
foreach(l, results)
|
|
|
|
{
|
|
|
|
Query *query = (Query *) lfirst(l);
|
|
|
|
|
|
|
|
if (query->querySource == QSRC_ORIGINAL)
|
|
|
|
{
|
|
|
|
Assert(query->canSetTag);
|
|
|
|
Assert(!foundOriginalQuery);
|
|
|
|
foundOriginalQuery = true;
|
|
|
|
#ifndef USE_ASSERT_CHECKING
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Assert(!query->canSetTag);
|
|
|
|
if (query->commandType == origCmdType &&
|
|
|
|
(query->querySource == QSRC_INSTEAD_RULE ||
|
|
|
|
query->querySource == QSRC_QUAL_INSTEAD_RULE))
|
|
|
|
lastInstead = query;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!foundOriginalQuery && lastInstead != NULL)
|
|
|
|
lastInstead->canSetTag = true;
|
|
|
|
|
2000-10-05 21:11:39 +02:00
|
|
|
return results;
|
Hi!
INTERSECT and EXCEPT is available for postgresql-v6.4!
The patch against v6.4 is included at the end of the current text
(in uuencoded form!)
I also included the text of my Master's Thesis. (a postscript
version). I hope that you find something of it useful and would be
happy if parts of it find their way into the PostgreSQL documentation
project (If so, tell me, then I send the sources of the document!)
The contents of the document are:
-) The first chapter might be of less interest as it gives only an
overview on SQL.
-) The second chapter gives a description on much of PostgreSQL's
features (like user defined types etc. and how to use these features)
-) The third chapter starts with an overview of PostgreSQL's internal
structure with focus on the stages a query has to pass (i.e. parser,
planner/optimizer, executor). Then a detailed description of the
implementation of the Having clause and the Intersect/Except logic is
given.
Originally I worked on v6.3.2 but never found time enough to prepare
and post a patch. Now I applied the changes to v6.4 to get Intersect
and Except working with the new version. Chapter 3 of my documentation
deals with the changes against v6.3.2, so keep that in mind when
comparing the parts of the code printed there with the patched sources
of v6.4.
Here are some remarks on the patch. There are some things that have
still to be done but at the moment I don't have time to do them
myself. (I'm doing my military service at the moment) Sorry for that
:-(
-) I used a rewrite technique for the implementation of the Except/Intersect
logic which rewrites the query to a semantically equivalent query before
it is handed to the rewrite system (for views, rules etc.), planner,
executor etc.
-) In v6.3.2 the types of the attributes of two select statements
connected by the UNION keyword had to match 100%. In v6.4 the types
only need to be familiar (i.e. int and float can be mixed). Since this
feature did not exist when I worked on Intersect/Except it
does not work correctly for Except/Intersect queries WHEN USED IN
COMBINATION WITH UNIONS! (i.e. sometimes the wrong type is used for the
resulting table. This is because until now the types of the attributes of
the first select statement have been used for the resulting table.
When Intersects and/or Excepts are used in combination with Unions it
might happen, that the first select statement of the original query
appears at another position in the query which will be executed. The reason
for this is the technique used for the implementation of
Except/Intersect which does a query rewrite!)
NOTE: It is NOT broken for pure UNION queries and pure INTERSECT/EXCEPT
queries!!!
-) I had to add the field intersect_clause to some data structures
but did not find time to implement printfuncs for the new field.
This does NOT break the debug modes but when an Except/Intersect
is used the query debug output will be the already rewritten query.
-) Massive changes to the grammar rules for SELECT and INSERT statements
have been necessary (see comments in gram.y and documentation for
deatails) in order to be able to use mixed queries like
(SELECT ... UNION (SELECT ... EXCEPT SELECT)) INTERSECT SELECT...;
-) When using UNION/EXCEPT/INTERSECT you will get:
NOTICE: equal: "Don't know if nodes of type xxx are equal".
I did not have time to add comparsion support for all the needed nodes,
but the default behaviour of the function equal met my requirements.
I did not dare to supress this message!
That's the reason why the regression test for union will fail: These
messages are also included in the union.out file!
-) Somebody of you changed the union_planner() function for v6.4
(I copied the targetlist to new_tlist and that was removed and
replaced by a cleanup of the original targetlist). These chnages
violated some having queries executed against views so I changed
it back again. I did not have time to examine the differences between the
two versions but now it works :-)
If you want to find out, try the file queries/view_having.sql on
both versions and compare the results . Two queries won't produce a
correct result with your version.
regards
Stefan
1999-01-18 01:10:17 +01:00
|
|
|
}
|