1
0
mirror of https://github.com/postgres/postgres.git synced 2025-07-02 09:02:37 +03:00

Implement SQL-standard WITH clauses, including WITH RECURSIVE.

There are some unimplemented aspects: recursive queries must use UNION ALL
(should allow UNION too), and we don't have SEARCH or CYCLE clauses.
These might or might not get done for 8.4, but even without them it's a
pretty useful feature.

There are also a couple of small loose ends and definitional quibbles,
which I'll send a memo about to pgsql-hackers shortly.  But let's land
the patch now so we can get on with other development.

Yoshiyuki Asaba, with lots of help from Tatsuo Ishii and Tom Lane
This commit is contained in:
Tom Lane
2008-10-04 21:56:55 +00:00
parent 607b2be7bb
commit 44d5be0e53
77 changed files with 5893 additions and 313 deletions

View File

@ -7,7 +7,7 @@
* Portions Copyright (c) 1994, Regents of the University of California
*
* IDENTIFICATION
* $PostgreSQL: pgsql/src/backend/rewrite/rewriteHandler.c,v 1.180 2008/09/24 16:52:46 tgl Exp $
* $PostgreSQL: pgsql/src/backend/rewrite/rewriteHandler.c,v 1.181 2008/10/04 21:56:54 tgl Exp $
*
*-------------------------------------------------------------------------
*/
@ -215,13 +215,21 @@ AcquireRewriteLocks(Query *parsetree)
}
}
/* Recurse into subqueries in WITH */
foreach(l, parsetree->cteList)
{
CommonTableExpr *cte = (CommonTableExpr *) lfirst(l);
AcquireRewriteLocks((Query *) cte->ctequery);
}
/*
* Recurse into sublink subqueries, too. But we already did the ones in
* the rtable.
* the rtable and cteList.
*/
if (parsetree->hasSubLinks)
query_tree_walker(parsetree, acquireLocksOnSubLinks, NULL,
QTW_IGNORE_RT_SUBQUERIES);
QTW_IGNORE_RC_SUBQUERIES);
}
/*
@ -1228,6 +1236,35 @@ markQueryForLocking(Query *qry, Node *jtnode, bool forUpdate, bool noWait)
markQueryForLocking(rte->subquery, (Node *) rte->subquery->jointree,
forUpdate, noWait);
}
else if (rte->rtekind == RTE_CTE)
{
/*
* We allow FOR UPDATE/SHARE of a WITH query to be propagated into
* the WITH, but it doesn't seem very sane to allow this for a
* reference to an outer-level WITH (compare
* transformLockingClause). Which simplifies life here.
*/
CommonTableExpr *cte = NULL;
ListCell *lc;
if (rte->ctelevelsup > 0 || rte->self_reference)
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
errmsg("SELECT FOR UPDATE/SHARE cannot be applied to an outer-level WITH query")));
foreach(lc, qry->cteList)
{
cte = (CommonTableExpr *) lfirst(lc);
if (strcmp(cte->ctename, rte->ctename) == 0)
break;
}
if (lc == NULL) /* shouldn't happen */
elog(ERROR, "could not find CTE \"%s\"", rte->ctename);
/* should be analyzed by now */
Assert(IsA(cte->ctequery, Query));
markQueryForLocking((Query *) cte->ctequery,
(Node *) ((Query *) cte->ctequery)->jointree,
forUpdate, noWait);
}
}
else if (IsA(jtnode, FromExpr))
{
@ -1295,6 +1332,7 @@ static Query *
fireRIRrules(Query *parsetree, List *activeRIRs)
{
int rt_index;
ListCell *lc;
/*
* don't try to convert this into a foreach loop, because rtable list can
@ -1407,13 +1445,22 @@ fireRIRrules(Query *parsetree, List *activeRIRs)
heap_close(rel, NoLock);
}
/* Recurse into subqueries in WITH */
foreach(lc, parsetree->cteList)
{
CommonTableExpr *cte = (CommonTableExpr *) lfirst(lc);
cte->ctequery = (Node *)
fireRIRrules((Query *) cte->ctequery, activeRIRs);
}
/*
* Recurse into sublink subqueries, too. But we already did the ones in
* the rtable.
* the rtable and cteList.
*/
if (parsetree->hasSubLinks)
query_tree_walker(parsetree, fireRIRonSubLink, (void *) activeRIRs,
QTW_IGNORE_RT_SUBQUERIES);
QTW_IGNORE_RC_SUBQUERIES);
return parsetree;
}