Hello, according to my mentor's suggestion, I send first PoC patch of "RETURNING AFTER/BEFORE" statement. Some info: - it is early version - more hack PoC than RC patch - AFTER in this version works as decribed before but it returns row after update but before post-update before (to be fixed or switch with current "*" behaviour - I don't know yet) - patch passes all regression tests - the code is uncommented already, to be fixed I'm not sure what may fail so you use it on your risk ;) Regards, Karol
diff --git a/src/backend/nodes/outfuncs.c b/src/backend/nodes/outfuncs.c index b2183f4..2698535 100644 --- a/src/backend/nodes/outfuncs.c +++ b/src/backend/nodes/outfuncs.c @@ -2351,6 +2351,7 @@ _outRangeTblEntry(StringInfo str, const RangeTblEntry *node) switch (node->rtekind) { case RTE_RELATION: + case RTE_BEFORE: WRITE_OID_FIELD(relid); WRITE_CHAR_FIELD(relkind); break; diff --git a/src/backend/nodes/readfuncs.c b/src/backend/nodes/readfuncs.c index 3a16e9d..04931ee 100644 --- a/src/backend/nodes/readfuncs.c +++ b/src/backend/nodes/readfuncs.c @@ -1189,6 +1189,7 @@ _readRangeTblEntry(void) switch (local_node->rtekind) { case RTE_RELATION: + case RTE_BEFORE: READ_OID_FIELD(relid); READ_CHAR_FIELD(relkind); break; diff --git a/src/backend/optimizer/plan/initsplan.c b/src/backend/optimizer/plan/initsplan.c index 839ed9d..7fc6ea1 100644 --- a/src/backend/optimizer/plan/initsplan.c +++ b/src/backend/optimizer/plan/initsplan.c @@ -174,9 +174,16 @@ add_vars_to_targetlist(PlannerInfo *root, List *vars, if (IsA(node, Var)) { Var *var = (Var *) node; - RelOptInfo *rel = find_base_rel(root, var->varno); + RelOptInfo *rel; int attno = var->varattno; + RangeTblEntry *rte; + if (root->parse->commandType == CMD_UPDATE){ + rte = ((RangeTblEntry *) list_nth(root->parse->rtable, (var->varno)-1)); + if(rte->rtekind == RTE_BEFORE) + continue; + } + rel = find_base_rel(root, var->varno); Assert(attno >= rel->min_attr && attno <= rel->max_attr); attno -= rel->min_attr; if (rel->attr_needed[attno] == NULL) diff --git a/src/backend/optimizer/plan/planner.c b/src/backend/optimizer/plan/planner.c index d80c264..77b0c38 100644 --- a/src/backend/optimizer/plan/planner.c +++ b/src/backend/optimizer/plan/planner.c @@ -1989,6 +1989,9 @@ preprocess_rowmarks(PlannerInfo *root) if (rte->relkind == RELKIND_FOREIGN_TABLE) continue; + if (rte->relkind == RELKIND_BEFORE) + continue; + rels = bms_del_member(rels, rc->rti); newrc = makeNode(PlanRowMark); @@ -2028,6 +2031,9 @@ preprocess_rowmarks(PlannerInfo *root) if (!bms_is_member(i, rels)) continue; + if (rte->relkind == RELKIND_BEFORE) + continue; + newrc = makeNode(PlanRowMark); newrc->rti = newrc->prti = i; newrc->rowmarkId = ++(root->glob->lastRowMarkId); diff --git a/src/backend/optimizer/plan/setrefs.c b/src/backend/optimizer/plan/setrefs.c index b78d727..6828a7d 100644 --- a/src/backend/optimizer/plan/setrefs.c +++ b/src/backend/optimizer/plan/setrefs.c @@ -1691,6 +1691,12 @@ fix_join_expr_mutator(Node *node, fix_join_expr_context *context) if (IsA(node, Var)) { Var *var = (Var *) node; + if (var->varno<=list_length(context->root->parse->rtable) && var->varno>1 && context->root->parse->commandType == CMD_UPDATE){ + RangeTblEntry *rte_a,*rte_b; + rte_a = (RangeTblEntry *)list_nth(context->root->parse->rtable,var->varno-1); + rte_b = (RangeTblEntry *)list_nth(context->root->parse->rtable,var->varno-2); + if (rte_a->rtekind == RTE_BEFORE && rte_b->rtekind == RTE_BEFORE) var->varno-=1; + } /* First look for the var in the input tlists */ newvar = search_indexed_tlist_for_var(var, @@ -1892,6 +1898,37 @@ fix_upper_expr_mutator(Node *node, fix_upper_expr_context *context) * * Note: resultRelation is not yet adjusted by rtoffset. */ + +void fix_varno_varattno(List *rlist, int begin, int bef, int aft){ + ListCell *temp; + ListCell *temp2; + Var *var; + foreach(temp, rlist){ + TargetEntry *tle = (TargetEntry *)lfirst(temp); + + if(IsA(tle, TargetEntry)){ + var = (Var*)tle->expr; + }else if(IsA(tle, Var)) var=(Var*)tle; + if( IsA(var, Var) ){ + if(var->varnoold == bef){ + var->varno = OUTER_VAR; + var->varattno = var->varoattno + begin; + } + else if(var->varnoold == aft) + { + var->varno = OUTER_VAR; + var->varattno = var->varoattno; + } + } + else if( IsA(var, OpExpr )){ + fix_varno_varattno(((OpExpr*)var)->args, begin, bef, aft); + } + else if( IsA(var, FuncExpr )){ + fix_varno_varattno(((FuncExpr*)var)->args, begin, bef, aft); + } + } +} + static List * set_returning_clause_references(PlannerInfo *root, List *rlist, @@ -1900,7 +1937,48 @@ set_returning_clause_references(PlannerInfo *root, int rtoffset) { indexed_tlist *itlist; + int before_index=0, after_index=0, var_index=0; + Query *parse = root->parse; + + ListCell *rt; + RangeTblEntry *bef; + TargetEntry *tr; + + int index_rel=1; + int index_var=0; + + + foreach(rt,parse->rtable) + { + bef = (RangeTblEntry *)lfirst(rt); + if(strcmp(bef->eref->aliasname,"before") == 0 && bef->rtekind == RTE_BEFORE ) + { + before_index = index_rel; + } + else if(strcmp(bef->eref->aliasname,"after") == 0 && bef->rtekind == RTE_BEFORE ) + { + after_index = index_rel; + } + index_rel++; + } + + foreach(rt, topplan->targetlist) + { + Var *var; + tr = (TargetEntry*)lfirst(rt); + var = tr->expr; + if(IsA(var,Var)) + { + if(var->varnoold == before_index && var->varattno == 1) + { + var_index = index_var; + break; + } + } + + index_var++; + } /* * We can perform the desired Var fixup by abusing the fix_join_expr * machinery that formerly handled inner indexscan fixup. We search the @@ -1924,6 +2002,9 @@ set_returning_clause_references(PlannerInfo *root, resultRelation, rtoffset); + + + fix_varno_varattno(rlist, var_index, before_index, after_index); pfree(itlist); return rlist; diff --git a/src/backend/optimizer/prep/prepjointree.c b/src/backend/optimizer/prep/prepjointree.c index 5284293..5e3f06d 100644 --- a/src/backend/optimizer/prep/prepjointree.c +++ b/src/backend/optimizer/prep/prepjointree.c @@ -648,6 +648,9 @@ pull_up_subqueries_recurse(PlannerInfo *root, Node *jtnode, int varno = ((RangeTblRef *) jtnode)->rtindex; RangeTblEntry *rte = rt_fetch(varno, root->parse->rtable); + if (rte->rtekind == RTE_BEFORE) + return NULL; + /* * Is this a subquery RTE, and if so, is the subquery simple enough to * pull up? @@ -753,6 +756,35 @@ pull_up_subqueries_recurse(PlannerInfo *root, Node *jtnode, return jtnode; } +void prepare_returning_before(PlannerInfo *root, List *ret, int varno) +{ + ListCell *v; + Var *var; + List *rtable = root->parse->rtable; + RangeTblEntry *rte; + TargetEntry *target; + foreach(v,ret) + { + target = (TargetEntry*)lfirst(v); + if(IsA(target,TargetEntry)) + { + var = (Var*)target->expr; + if(IsA(var,Var)) + { + if (var->varno <= list_length(rtable)) + { + rte = (RangeTblEntry*)list_nth(rtable,var->varno-1); + if(rte->rtekind == RTE_BEFORE) + { + var->varno=varno; + } + } + + } + } + } +} + /* * pull_up_simple_subquery * Attempt to pull up a single simple subquery. @@ -912,6 +944,8 @@ pull_up_simple_subquery(PlannerInfo *root, Node *jtnode, RangeTblEntry *rte, */ parse->targetList = (List *) pullup_replace_vars((Node *) parse->targetList, &rvcontext); + + prepare_returning_before(root,parse->returningList,varno); parse->returningList = (List *) pullup_replace_vars((Node *) parse->returningList, &rvcontext); replace_vars_in_jointree((Node *) parse->jointree, &rvcontext, diff --git a/src/backend/optimizer/util/relnode.c b/src/backend/optimizer/util/relnode.c index 8ee5671..28c5ff7 100644 --- a/src/backend/optimizer/util/relnode.c +++ b/src/backend/optimizer/util/relnode.c @@ -135,6 +135,8 @@ build_simple_rel(PlannerInfo *root, int relid, RelOptKind reloptkind) /* Table --- retrieve statistics from the system catalogs */ get_relation_info(root, rte->relid, rte->inh, rel); break; + case RTE_BEFORE: + break; case RTE_SUBQUERY: case RTE_FUNCTION: case RTE_VALUES: diff --git a/src/backend/parser/analyze.c b/src/backend/parser/analyze.c index 16ff234..fd51a0e 100644 --- a/src/backend/parser/analyze.c +++ b/src/backend/parser/analyze.c @@ -2003,6 +2003,9 @@ transformReturningList(ParseState *pstate, List *returningList) save_next_resno = pstate->p_next_resno; pstate->p_next_resno = 1; + if (pstate->p_is_update) + addAliases(pstate); + /* transform RETURNING identically to a SELECT targetlist */ rlist = transformTargetList(pstate, returningList, EXPR_KIND_RETURNING); diff --git a/src/backend/parser/parse_clause.c b/src/backend/parser/parse_clause.c index cbfb431..bcc3f91 100644 --- a/src/backend/parser/parse_clause.c +++ b/src/backend/parser/parse_clause.c @@ -85,7 +85,54 @@ static WindowClause *findWindowClause(List *wclist, const char *name); static Node *transformFrameOffset(ParseState *pstate, int frameOptions, Node *clause); +extern void addAliases(ParseState *pstate); +void addAliases(ParseState *pstate){ + const int noal = 2; + char *aliases[] = {"before","after"}; + int i; + ListCell *l; + ParseNamespaceItem *nsitem; + RangeTblEntry *rte = NULL; + + foreach(l, pstate->p_namespace) + { + nsitem = (ParseNamespaceItem *) lfirst(l); + rte = nsitem->p_rte; + + /* Ignore columns-only items */ + if (!nsitem->p_rel_visible) + continue; + /* If not inside LATERAL, ignore lateral-only items */ + if (nsitem->p_lateral_only && !pstate->p_lateral_active) + continue; + + for(i=0 ; i<noal; i++){ + if (aliases[i]) + if (strcmp(rte->eref->aliasname, aliases[i]) == 0) + { + aliases[i] = NULL; + } + } + } + + l = pstate->p_namespace->head; + nsitem = (ParseNamespaceItem *) lfirst(l); + + for(i=0 ; i<noal; i++){ + if (aliases[i]) + { + rte = makeNode(RangeTblEntry); + rte->eref = makeAlias(aliases[i], nsitem->p_rte->eref->colnames); + rte->inh = INH_NO; + rte->rtekind = RTE_BEFORE; + rte->relkind = RELKIND_BEFORE; + rte->relid = nsitem->p_rte->relid; + pstate->p_rtable = lappend(pstate->p_rtable, rte); + addRTEtoQuery(pstate, rte, true, true, false); + } + } +} /* * transformFromClause - * Process the FROM clause and add items to the query's range table, diff --git a/src/backend/parser/parse_relation.c b/src/backend/parser/parse_relation.c index a9254c8..e57fccf 100644 --- a/src/backend/parser/parse_relation.c +++ b/src/backend/parser/parse_relation.c @@ -1658,6 +1658,7 @@ expandRTE(RangeTblEntry *rte, int rtindex, int sublevels_up, switch (rte->rtekind) { case RTE_RELATION: + case RTE_BEFORE: /* Ordinary relation RTE */ expandRelation(rte->relid, rte->eref, rtindex, sublevels_up, location, @@ -2113,6 +2114,7 @@ get_rte_attribute_type(RangeTblEntry *rte, AttrNumber attnum, switch (rte->rtekind) { case RTE_RELATION: + case RTE_BEFORE: { /* Plain relation RTE --- get the attribute's type info */ HeapTuple tp; @@ -2273,6 +2275,7 @@ get_rte_attribute_is_dropped(RangeTblEntry *rte, AttrNumber attnum) switch (rte->rtekind) { case RTE_RELATION: + case RTE_BEFORE: { /* * Plain relation RTE --- get the attribute's catalog entry diff --git a/src/include/catalog/pg_class.h b/src/include/catalog/pg_class.h index 49c4f6f..1b09994 100644 --- a/src/include/catalog/pg_class.h +++ b/src/include/catalog/pg_class.h @@ -154,6 +154,7 @@ DESCR(""); #define RELKIND_COMPOSITE_TYPE 'c' /* composite type */ #define RELKIND_FOREIGN_TABLE 'f' /* foreign table */ #define RELKIND_MATVIEW 'm' /* materialized view */ +#define RELKIND_BEFORE 'b' /* virtual table for before/after statements */ #define RELPERSISTENCE_PERMANENT 'p' /* regular table */ #define RELPERSISTENCE_UNLOGGED 'u' /* unlogged permanent table */ diff --git a/src/include/nodes/parsenodes.h b/src/include/nodes/parsenodes.h index de22dff..fa5083c 100644 --- a/src/include/nodes/parsenodes.h +++ b/src/include/nodes/parsenodes.h @@ -698,7 +698,8 @@ typedef enum RTEKind RTE_JOIN, /* join */ RTE_FUNCTION, /* function in FROM */ RTE_VALUES, /* VALUES (<exprlist>), (<exprlist>), ... */ - RTE_CTE /* common table expr (WITH list element) */ + RTE_CTE, /* common table expr (WITH list element) */ + RTE_BEFORE /* for before/after statements */ } RTEKind; typedef struct RangeTblEntry diff --git a/src/include/parser/parse_clause.h b/src/include/parser/parse_clause.h index 9bdb033..67cbbb2 100644 --- a/src/include/parser/parse_clause.h +++ b/src/include/parser/parse_clause.h @@ -44,5 +44,6 @@ extern List *transformDistinctOnClause(ParseState *pstate, List *distinctlist, extern Index assignSortGroupRef(TargetEntry *tle, List *tlist); extern bool targetIsInSortList(TargetEntry *tle, Oid sortop, List *sortList); +extern void addAliases(ParseState *pstate); #endif /* PARSE_CLAUSE_H */
-- Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-hackers