1
0
mirror of https://github.com/postgres/postgres.git synced 2025-11-07 19:06:32 +03:00

Support data-modifying commands (INSERT/UPDATE/DELETE) in WITH.

This patch implements data-modifying WITH queries according to the
semantics that the updates all happen with the same command counter value,
and in an unspecified order.  Therefore one WITH clause can't see the
effects of another, nor can the outer query see the effects other than
through the RETURNING values.  And attempts to do conflicting updates will
have unpredictable results.  We'll need to document all that.

This commit just fixes the code; documentation updates are waiting on
author.

Marko Tiikkaja and Hitoshi Harada
This commit is contained in:
Tom Lane
2011-02-25 18:56:23 -05:00
parent 0056066d06
commit 389af95155
38 changed files with 1323 additions and 161 deletions

View File

@@ -118,6 +118,7 @@ typedef struct Query
bool hasSubLinks; /* has subquery SubLink */
bool hasDistinctOn; /* distinctClause is from DISTINCT ON */
bool hasRecursive; /* WITH RECURSIVE was specified */
bool hasModifyingCTE; /* has INSERT/UPDATE/DELETE in WITH */
bool hasForUpdate; /* FOR UPDATE or FOR SHARE was specified */
List *cteList; /* WITH list (of CommonTableExpr's) */
@@ -884,7 +885,8 @@ typedef struct CommonTableExpr
NodeTag type;
char *ctename; /* query name (never qualified) */
List *aliascolnames; /* optional list of column names */
Node *ctequery; /* subquery (SelectStmt or Query) */
/* SelectStmt/InsertStmt/etc before parse analysis, Query afterwards: */
Node *ctequery; /* the CTE's subquery */
int location; /* token location, or -1 if unknown */
/* These fields are set during parse analysis: */
bool cterecursive; /* is this CTE actually recursive? */
@@ -896,6 +898,14 @@ typedef struct CommonTableExpr
List *ctecolcollations; /* OID list of column collation OIDs */
} CommonTableExpr;
/* Convenience macro to get the output tlist of a CTE's query */
#define GetCTETargetList(cte) \
(AssertMacro(IsA((cte)->ctequery, Query)), \
((Query *) (cte)->ctequery)->commandType == CMD_SELECT ? \
((Query *) (cte)->ctequery)->targetList : \
((Query *) (cte)->ctequery)->returningList)
/*****************************************************************************
* Optimizable Statements
*****************************************************************************/