PostgreSQL Source Code git master
Loading...
Searching...
No Matches
analyze.c File Reference
#include "postgres.h"
#include "access/stratnum.h"
#include "access/sysattr.h"
#include "catalog/dependency.h"
#include "catalog/pg_am.h"
#include "catalog/pg_operator.h"
#include "catalog/pg_proc.h"
#include "catalog/pg_type.h"
#include "commands/defrem.h"
#include "miscadmin.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
#include "nodes/queryjumble.h"
#include "optimizer/optimizer.h"
#include "parser/analyze.h"
#include "parser/parse_agg.h"
#include "parser/parse_clause.h"
#include "parser/parse_coerce.h"
#include "parser/parse_collate.h"
#include "parser/parse_cte.h"
#include "parser/parse_expr.h"
#include "parser/parse_func.h"
#include "parser/parse_merge.h"
#include "parser/parse_oper.h"
#include "parser/parse_param.h"
#include "parser/parse_relation.h"
#include "parser/parse_target.h"
#include "parser/parse_type.h"
#include "parser/parsetree.h"
#include "utils/backend_status.h"
#include "utils/builtins.h"
#include "utils/fmgroids.h"
#include "utils/guc.h"
#include "utils/lsyscache.h"
#include "utils/rangetypes.h"
#include "utils/rel.h"
#include "utils/syscache.h"
Include dependency graph for analyze.c:

Go to the source code of this file.

Data Structures

struct  SelectStmtPassthrough
 

Typedefs

typedef struct SelectStmtPassthrough SelectStmtPassthrough
 

Functions

static QuerytransformOptionalSelectInto (ParseState *pstate, Node *parseTree)
 
static QuerytransformDeleteStmt (ParseState *pstate, DeleteStmt *stmt)
 
static QuerytransformInsertStmt (ParseState *pstate, InsertStmt *stmt)
 
static OnConflictExprtransformOnConflictClause (ParseState *pstate, OnConflictClause *onConflictClause)
 
static ForPortionOfExprtransformForPortionOfClause (ParseState *pstate, int rtindex, const ForPortionOfClause *forPortionOf, const Node *whereClause, bool isUpdate)
 
static int count_rowexpr_columns (ParseState *pstate, Node *expr)
 
static QuerytransformSelectStmt (ParseState *pstate, SelectStmt *stmt, SelectStmtPassthrough *passthru)
 
static QuerytransformValuesClause (ParseState *pstate, SelectStmt *stmt)
 
static QuerytransformSetOperationStmt (ParseState *pstate, SelectStmt *stmt)
 
static NodetransformSetOperationTree (ParseState *pstate, SelectStmt *stmt, bool isTopLevel, List **targetlist)
 
static void determineRecursiveColTypes (ParseState *pstate, Node *larg, List *nrtargetlist)
 
static QuerytransformReturnStmt (ParseState *pstate, ReturnStmt *stmt)
 
static QuerytransformUpdateStmt (ParseState *pstate, UpdateStmt *stmt)
 
static QuerytransformPLAssignStmt (ParseState *pstate, PLAssignStmt *stmt)
 
static ListtransformPLAssignStmtTarget (ParseState *pstate, List *tlist, SelectStmtPassthrough *passthru)
 
static QuerytransformDeclareCursorStmt (ParseState *pstate, DeclareCursorStmt *stmt)
 
static QuerytransformExplainStmt (ParseState *pstate, ExplainStmt *stmt)
 
static QuerytransformCreateTableAsStmt (ParseState *pstate, CreateTableAsStmt *stmt)
 
static QuerytransformCallStmt (ParseState *pstate, CallStmt *stmt)
 
static void transformLockingClause (ParseState *pstate, Query *qry, LockingClause *lc, bool pushedDown)
 
Queryparse_analyze_fixedparams (RawStmt *parseTree, const char *sourceText, const Oid *paramTypes, int numParams, QueryEnvironment *queryEnv)
 
Queryparse_analyze_varparams (RawStmt *parseTree, const char *sourceText, Oid **paramTypes, int *numParams, QueryEnvironment *queryEnv)
 
Queryparse_analyze_withcb (RawStmt *parseTree, const char *sourceText, ParserSetupHook parserSetup, void *parserSetupArg, QueryEnvironment *queryEnv)
 
Queryparse_sub_analyze (Node *parseTree, ParseState *parentParseState, CommonTableExpr *parentCTE, bool locked_from_parent, bool resolve_unknowns)
 
QuerytransformTopLevelStmt (ParseState *pstate, RawStmt *parseTree)
 
QuerytransformStmt (ParseState *pstate, Node *parseTree)
 
bool stmt_requires_parse_analysis (RawStmt *parseTree)
 
bool analyze_requires_snapshot (RawStmt *parseTree)
 
bool query_requires_rewrite_plan (Query *query)
 
ListtransformInsertRow (ParseState *pstate, List *exprlist, List *stmtcols, List *icolumns, List *attrnos, bool strip_indirection)
 
ListBuildOnConflictExcludedTargetlist (Relation targetrel, Index exclRelIndex)
 
SortGroupClausemakeSortGroupClauseForSetOp (Oid rescoltype, bool require_hash)
 
void constructSetOpTargetlist (ParseState *pstate, SetOperationStmt *op, const List *ltargetlist, const List *rtargetlist, List **targetlist, const char *context, bool recursive)
 
ListtransformUpdateTargetList (ParseState *pstate, List *origTlist, ForPortionOfExpr *forPortionOf)
 
static void addNSItemForReturning (ParseState *pstate, const char *aliasname, VarReturningType returning_type)
 
void transformReturningClause (ParseState *pstate, Query *qry, ReturningClause *returningClause, ParseExprKind exprKind)
 
const charLCS_asString (LockClauseStrength strength)
 
void CheckSelectLocking (Query *qry, LockClauseStrength strength)
 
void applyLockingClause (Query *qry, Index rtindex, LockClauseStrength strength, LockWaitPolicy waitPolicy, bool pushedDown)
 

Variables

post_parse_analyze_hook_type post_parse_analyze_hook = NULL
 

Typedef Documentation

◆ SelectStmtPassthrough

Function Documentation

◆ addNSItemForReturning()

static void addNSItemForReturning ( ParseState pstate,
const char aliasname,
VarReturningType  returning_type 
)
static

Definition at line 3025 of file analyze.c.

3027{
3028 List *colnames;
3029 int numattrs;
3032
3033 /* copy per-column data from the target relation */
3034 colnames = pstate->p_target_nsitem->p_rte->eref->colnames;
3035 numattrs = list_length(colnames);
3036
3038
3040 numattrs * sizeof(ParseNamespaceColumn));
3041
3042 /* mark all columns as returning OLD/NEW */
3043 for (int i = 0; i < numattrs; i++)
3044 nscolumns[i].p_varreturningtype = returning_type;
3045
3046 /* build the nsitem, copying most fields from the target relation */
3048 nsitem->p_names = makeAlias(aliasname, colnames);
3049 nsitem->p_rte = pstate->p_target_nsitem->p_rte;
3050 nsitem->p_rtindex = pstate->p_target_nsitem->p_rtindex;
3051 nsitem->p_perminfo = pstate->p_target_nsitem->p_perminfo;
3052 nsitem->p_nscolumns = nscolumns;
3053 nsitem->p_returning_type = returning_type;
3054
3055 /* add it to the query namespace as a table-only item */
3056 addNSItemToQuery(pstate, nsitem, false, true, false);
3057}
memcpy(sums, checksumBaseOffsets, sizeof(checksumBaseOffsets))
#define palloc_object(type)
Definition fe_memutils.h:89
#define palloc_array(type, count)
Definition fe_memutils.h:91
int i
Definition isn.c:77
Alias * makeAlias(const char *aliasname, List *colnames)
Definition makefuncs.c:438
void addNSItemToQuery(ParseState *pstate, ParseNamespaceItem *nsitem, bool addToJoinList, bool addToRelNameSpace, bool addToVarNameSpace)
static int list_length(const List *l)
Definition pg_list.h:152
static int fb(int x)
Definition pg_list.h:54
RangeTblEntry * p_rte
Definition parse_node.h:315
ParseNamespaceColumn * p_nscolumns
Definition parse_node.h:319
RTEPermissionInfo * p_perminfo
Definition parse_node.h:317
ParseNamespaceItem * p_target_nsitem
Definition parse_node.h:229

References addNSItemToQuery(), fb(), i, list_length(), makeAlias(), memcpy(), ParseNamespaceItem::p_nscolumns, ParseNamespaceItem::p_perminfo, ParseNamespaceItem::p_rte, ParseNamespaceItem::p_rtindex, ParseState::p_target_nsitem, palloc_array, and palloc_object.

Referenced by transformReturningClause().

◆ analyze_requires_snapshot()

bool analyze_requires_snapshot ( RawStmt parseTree)

Definition at line 514 of file analyze.c.

515{
516 /*
517 * Currently, this should return true in exactly the same cases that
518 * stmt_requires_parse_analysis() does, so we just invoke that function
519 * rather than duplicating it. We keep the two entry points separate for
520 * clarity of callers, since from the callers' standpoint these are
521 * different conditions.
522 *
523 * While there may someday be a statement type for which transformStmt()
524 * does something nontrivial and yet no snapshot is needed for that
525 * processing, it seems likely that making such a choice would be fragile.
526 * If you want to install an exception, document the reasoning for it in a
527 * comment.
528 */
530}
bool stmt_requires_parse_analysis(RawStmt *parseTree)
Definition analyze.c:470

References fb(), and stmt_requires_parse_analysis().

Referenced by BuildingPlanRequiresSnapshot(), exec_bind_message(), exec_parse_message(), and exec_simple_query().

◆ applyLockingClause()

void applyLockingClause ( Query qry,
Index  rtindex,
LockClauseStrength  strength,
LockWaitPolicy  waitPolicy,
bool  pushedDown 
)

Definition at line 4033 of file analyze.c.

4036{
4037 RowMarkClause *rc;
4038
4039 Assert(strength != LCS_NONE); /* else caller error */
4040
4041 /* If it's an explicit clause, make sure hasForUpdate gets set */
4042 if (!pushedDown)
4043 qry->hasForUpdate = true;
4044
4045 /* Check for pre-existing entry for same rtindex */
4046 if ((rc = get_parse_rowmark(qry, rtindex)) != NULL)
4047 {
4048 /*
4049 * If the same RTE is specified with more than one locking strength,
4050 * use the strongest. (Reasonable, since you can't take both a shared
4051 * and exclusive lock at the same time; it'll end up being exclusive
4052 * anyway.)
4053 *
4054 * Similarly, if the same RTE is specified with more than one lock
4055 * wait policy, consider that NOWAIT wins over SKIP LOCKED, which in
4056 * turn wins over waiting for the lock (the default). This is a bit
4057 * more debatable but raising an error doesn't seem helpful. (Consider
4058 * for instance SELECT FOR UPDATE NOWAIT from a view that internally
4059 * contains a plain FOR UPDATE spec.) Having NOWAIT win over SKIP
4060 * LOCKED is reasonable since the former throws an error in case of
4061 * coming across a locked tuple, which may be undesirable in some
4062 * cases but it seems better than silently returning inconsistent
4063 * results.
4064 *
4065 * And of course pushedDown becomes false if any clause is explicit.
4066 */
4067 rc->strength = Max(rc->strength, strength);
4068 rc->waitPolicy = Max(rc->waitPolicy, waitPolicy);
4069 rc->pushedDown &= pushedDown;
4070 return;
4071 }
4072
4073 /* Make a new RowMarkClause */
4074 rc = makeNode(RowMarkClause);
4075 rc->rti = rtindex;
4076 rc->strength = strength;
4077 rc->waitPolicy = waitPolicy;
4078 rc->pushedDown = pushedDown;
4079 qry->rowMarks = lappend(qry->rowMarks, rc);
4080}
#define Max(x, y)
Definition c.h:1141
#define Assert(condition)
Definition c.h:999
List * lappend(List *list, void *datum)
Definition list.c:339
@ LCS_NONE
Definition lockoptions.h:23
#define makeNode(_type_)
Definition nodes.h:159
RowMarkClause * get_parse_rowmark(Query *qry, Index rtindex)
List * rowMarks
Definition parsenodes.h:239
LockClauseStrength strength
LockWaitPolicy waitPolicy

References Assert, fb(), get_parse_rowmark(), lappend(), LCS_NONE, makeNode, Max, RowMarkClause::pushedDown, Query::rowMarks, RowMarkClause::rti, RowMarkClause::strength, and RowMarkClause::waitPolicy.

Referenced by markQueryForLocking(), and transformLockingClause().

◆ BuildOnConflictExcludedTargetlist()

List * BuildOnConflictExcludedTargetlist ( Relation  targetrel,
Index  exclRelIndex 
)

Definition at line 1624 of file analyze.c.

1626{
1627 List *result = NIL;
1628 int attno;
1629 Var *var;
1630 TargetEntry *te;
1631
1632 /*
1633 * Note that resnos of the tlist must correspond to attnos of the
1634 * underlying relation, hence we need entries for dropped columns too.
1635 */
1636 for (attno = 0; attno < RelationGetNumberOfAttributes(targetrel); attno++)
1637 {
1638 Form_pg_attribute attr = TupleDescAttr(targetrel->rd_att, attno);
1639 char *name;
1640
1641 if (attr->attisdropped)
1642 {
1643 /*
1644 * can't use atttypid here, but it doesn't really matter what type
1645 * the Const claims to be.
1646 */
1647 var = (Var *) makeNullConst(INT4OID, -1, InvalidOid);
1648 name = NULL;
1649 }
1650 else
1651 {
1652 var = makeVar(exclRelIndex, attno + 1,
1653 attr->atttypid, attr->atttypmod,
1654 attr->attcollation,
1655 0);
1656 name = pstrdup(NameStr(attr->attname));
1657 }
1658
1659 te = makeTargetEntry((Expr *) var,
1660 attno + 1,
1661 name,
1662 false);
1663
1664 result = lappend(result, te);
1665 }
1666
1667 /*
1668 * Add a whole-row-Var entry to support references to "EXCLUDED.*". Like
1669 * the other entries in the EXCLUDED tlist, its resno must match the Var's
1670 * varattno, else the wrong things happen while resolving references in
1671 * setrefs.c. This is against normal conventions for targetlists, but
1672 * it's okay since we don't use this as a real tlist.
1673 */
1674 var = makeVar(exclRelIndex, InvalidAttrNumber,
1675 targetrel->rd_rel->reltype,
1676 -1, InvalidOid, 0);
1677 te = makeTargetEntry((Expr *) var, InvalidAttrNumber, NULL, true);
1678 result = lappend(result, te);
1679
1680 return result;
1681}
#define InvalidAttrNumber
Definition attnum.h:23
#define NameStr(name)
Definition c.h:891
uint32 result
Var * makeVar(int varno, AttrNumber varattno, Oid vartype, int32 vartypmod, Oid varcollid, Index varlevelsup)
Definition makefuncs.c:66
Const * makeNullConst(Oid consttype, int32 consttypmod, Oid constcollid)
Definition makefuncs.c:388
TargetEntry * makeTargetEntry(Expr *expr, AttrNumber resno, char *resname, bool resjunk)
Definition makefuncs.c:289
char * pstrdup(const char *in)
Definition mcxt.c:1910
FormData_pg_attribute * Form_pg_attribute
#define NIL
Definition pg_list.h:68
#define InvalidOid
#define RelationGetNumberOfAttributes(relation)
Definition rel.h:522
static FormData_pg_attribute * TupleDescAttr(TupleDesc tupdesc, int i)
Definition tupdesc.h:178
const char * name

References fb(), InvalidAttrNumber, InvalidOid, lappend(), makeNullConst(), makeTargetEntry(), makeVar(), name, NameStr, NIL, pstrdup(), RelationGetNumberOfAttributes, result, and TupleDescAttr().

Referenced by rewriteTargetView(), and transformOnConflictClause().

◆ CheckSelectLocking()

void CheckSelectLocking ( Query qry,
LockClauseStrength  strength 
)

Definition at line 3742 of file analyze.c.

3743{
3744 Assert(strength != LCS_NONE); /* else caller error */
3745
3746 if (qry->setOperations)
3747 ereport(ERROR,
3749 /*------
3750 translator: %s is a SQL row locking clause such as FOR UPDATE */
3751 errmsg("%s is not allowed with UNION/INTERSECT/EXCEPT",
3752 LCS_asString(strength))));
3753 if (qry->distinctClause != NIL)
3754 ereport(ERROR,
3756 /*------
3757 translator: %s is a SQL row locking clause such as FOR UPDATE */
3758 errmsg("%s is not allowed with DISTINCT clause",
3759 LCS_asString(strength))));
3760 if (qry->groupClause != NIL || qry->groupingSets != NIL)
3761 ereport(ERROR,
3763 /*------
3764 translator: %s is a SQL row locking clause such as FOR UPDATE */
3765 errmsg("%s is not allowed with GROUP BY clause",
3766 LCS_asString(strength))));
3767 if (qry->havingQual != NULL)
3768 ereport(ERROR,
3770 /*------
3771 translator: %s is a SQL row locking clause such as FOR UPDATE */
3772 errmsg("%s is not allowed with HAVING clause",
3773 LCS_asString(strength))));
3774 if (qry->hasAggs)
3775 ereport(ERROR,
3777 /*------
3778 translator: %s is a SQL row locking clause such as FOR UPDATE */
3779 errmsg("%s is not allowed with aggregate functions",
3780 LCS_asString(strength))));
3781 if (qry->hasWindowFuncs)
3782 ereport(ERROR,
3784 /*------
3785 translator: %s is a SQL row locking clause such as FOR UPDATE */
3786 errmsg("%s is not allowed with window functions",
3787 LCS_asString(strength))));
3788 if (qry->hasTargetSRFs)
3789 ereport(ERROR,
3791 /*------
3792 translator: %s is a SQL row locking clause such as FOR UPDATE */
3793 errmsg("%s is not allowed with set-returning functions in the target list",
3794 LCS_asString(strength))));
3795}
int errcode(int sqlerrcode)
Definition elog.c:875
#define ERROR
Definition elog.h:40
#define ereport(elevel,...)
Definition elog.h:152
static char * errmsg
const char * LCS_asString(LockClauseStrength strength)
Definition analyze.c:3717
Node * setOperations
Definition parsenodes.h:241
List * groupClause
Definition parsenodes.h:221
Node * havingQual
Definition parsenodes.h:227
List * groupingSets
Definition parsenodes.h:225
List * distinctClause
Definition parsenodes.h:231

References Assert, Query::distinctClause, ereport, errcode(), errmsg, ERROR, fb(), Query::groupClause, Query::groupingSets, Query::havingQual, LCS_asString(), LCS_NONE, NIL, and Query::setOperations.

Referenced by preprocess_rowmarks(), and transformLockingClause().

◆ constructSetOpTargetlist()

void constructSetOpTargetlist ( ParseState pstate,
SetOperationStmt op,
const List ltargetlist,
const List rtargetlist,
List **  targetlist,
const char context,
bool  recursive 
)

Definition at line 2605 of file analyze.c.

2608{
2609 ListCell *ltl;
2610 ListCell *rtl;
2611
2612 /*
2613 * Verify that the two children have the same number of non-junk columns,
2614 * and determine the types of the merged output columns.
2615 */
2617 ereport(ERROR,
2619 errmsg("each %s query must have the same number of columns",
2620 context),
2621 parser_errposition(pstate,
2622 exprLocation((const Node *) rtargetlist))));
2623
2624 if (targetlist)
2625 *targetlist = NIL;
2626 op->colTypes = NIL;
2627 op->colTypmods = NIL;
2628 op->colCollations = NIL;
2629 op->groupClauses = NIL;
2630
2632 {
2635 Node *lcolnode = (Node *) ltle->expr;
2636 Node *rcolnode = (Node *) rtle->expr;
2639 Node *bestexpr;
2640 int bestlocation;
2644
2645 /* select common type, same as CASE et al */
2648 context,
2649 &bestexpr);
2651
2652 /*
2653 * Verify the coercions are actually possible. If not, we'd fail
2654 * later anyway, but we want to fail now while we have sufficient
2655 * context to produce an error cursor position.
2656 *
2657 * For all non-UNKNOWN-type cases, we verify coercibility but we don't
2658 * modify the child's expression, for fear of changing the child
2659 * query's semantics.
2660 *
2661 * If a child expression is an UNKNOWN-type Const or Param, we want to
2662 * replace it with the coerced expression. This can only happen when
2663 * the child is a leaf set-op node. It's safe to replace the
2664 * expression because if the child query's semantics depended on the
2665 * type of this output column, it'd have already coerced the UNKNOWN
2666 * to something else. We want to do this because (a) we want to
2667 * verify that a Const is valid for the target type, or resolve the
2668 * actual type of an UNKNOWN Param, and (b) we want to avoid
2669 * unnecessary discrepancies between the output type of the child
2670 * query and the resolved target type. Such a discrepancy would
2671 * disable optimization in the planner.
2672 *
2673 * If it's some other UNKNOWN-type node, eg a Var, we do nothing
2674 * (knowing that coerce_to_common_type would fail). The planner is
2675 * sometimes able to fold an UNKNOWN Var to a constant before it has
2676 * to coerce the type, so failing now would just break cases that
2677 * might work.
2678 */
2679 if (lcoltype != UNKNOWNOID)
2681 rescoltype, context);
2682 else if (IsA(lcolnode, Const) ||
2683 IsA(lcolnode, Param))
2684 {
2686 rescoltype, context);
2687 ltle->expr = (Expr *) lcolnode;
2688 }
2689
2690 if (rcoltype != UNKNOWNOID)
2692 rescoltype, context);
2693 else if (IsA(rcolnode, Const) ||
2694 IsA(rcolnode, Param))
2695 {
2697 rescoltype, context);
2698 rtle->expr = (Expr *) rcolnode;
2699 }
2700
2703 rescoltype);
2704
2705 /*
2706 * Select common collation. A common collation is required for all
2707 * set operators except UNION ALL; see SQL:2008 7.13 <query
2708 * expression> Syntax Rule 15c. (If we fail to identify a common
2709 * collation for a UNION ALL column, the colCollations element will be
2710 * set to InvalidOid, which may result in a runtime error if something
2711 * at a higher query level wants to use the column's collation.)
2712 */
2715 (op->op == SETOP_UNION && op->all));
2716
2717 /* emit results */
2718 op->colTypes = lappend_oid(op->colTypes, rescoltype);
2719 op->colTypmods = lappend_int(op->colTypmods, rescoltypmod);
2720 op->colCollations = lappend_oid(op->colCollations, rescolcoll);
2721
2722 /*
2723 * For all cases except UNION ALL, identify the grouping operators
2724 * (and, if available, sorting operators) that will be used to
2725 * eliminate duplicates.
2726 */
2727 if (op->op != SETOP_UNION || !op->all)
2728 {
2730
2732 bestlocation);
2733
2734 /* If it's a recursive union, we need to require hashing support. */
2735 op->groupClauses = lappend(op->groupClauses,
2737
2739 }
2740
2741 /*
2742 * Construct a dummy tlist entry to return. We use a SetToDefault
2743 * node for the expression, since it carries exactly the fields
2744 * needed, but any other expression node type would do as well.
2745 */
2746 if (targetlist)
2747 {
2750
2751 rescolnode->typeId = rescoltype;
2752 rescolnode->typeMod = rescoltypmod;
2753 rescolnode->collation = rescolcoll;
2754 rescolnode->location = bestlocation;
2756 0, /* no need to set resno */
2757 NULL,
2758 false);
2759 *targetlist = lappend(*targetlist, restle);
2760 }
2761 }
2762}
int32_t int32
Definition c.h:676
List * lappend_int(List *list, int datum)
Definition list.c:357
List * lappend_oid(List *list, Oid datum)
Definition list.c:375
Oid exprType(const Node *expr)
Definition nodeFuncs.c:42
int exprLocation(const Node *expr)
Definition nodeFuncs.c:1403
#define IsA(nodeptr, _type_)
Definition nodes.h:162
Node * coerce_to_common_type(ParseState *pstate, Node *node, Oid targetTypeId, const char *context)
int32 select_common_typmod(ParseState *pstate, List *exprs, Oid common_type)
Oid select_common_type(ParseState *pstate, List *exprs, const char *context, Node **which_expr)
Oid select_common_collation(ParseState *pstate, List *exprs, bool none_ok)
void cancel_parser_errposition_callback(ParseCallbackState *pcbstate)
Definition parse_node.c:156
int parser_errposition(ParseState *pstate, int location)
Definition parse_node.c:106
void setup_parser_errposition_callback(ParseCallbackState *pcbstate, ParseState *pstate, int location)
Definition parse_node.c:140
@ SETOP_UNION
SortGroupClause * makeSortGroupClauseForSetOp(Oid rescoltype, bool require_hash)
Definition analyze.c:2373
#define lfirst(lc)
Definition pg_list.h:172
#define forboth(cell1, list1, cell2, list2)
Definition pg_list.h:550
#define list_make2(x1, x2)
Definition pg_list.h:246
unsigned int Oid
Definition nodes.h:133
SetOperation op

References SetOperationStmt::all, cancel_parser_errposition_callback(), coerce_to_common_type(), ereport, errcode(), errmsg, ERROR, exprLocation(), exprType(), fb(), forboth, IsA, lappend(), lappend_int(), lappend_oid(), lfirst, list_length(), list_make2, makeNode, makeSortGroupClauseForSetOp(), makeTargetEntry(), NIL, SetOperationStmt::op, parser_errposition(), select_common_collation(), select_common_type(), select_common_typmod(), SETOP_UNION, and setup_parser_errposition_callback().

Referenced by generate_setop_from_pathqueries(), and transformSetOperationTree().

◆ count_rowexpr_columns()

static int count_rowexpr_columns ( ParseState pstate,
Node expr 
)
static

Definition at line 1694 of file analyze.c.

1695{
1696 if (expr == NULL)
1697 return -1;
1698 if (IsA(expr, RowExpr))
1699 return list_length(((RowExpr *) expr)->args);
1700 if (IsA(expr, Var))
1701 {
1702 Var *var = (Var *) expr;
1703 AttrNumber attnum = var->varattno;
1704
1705 if (attnum > 0 && var->vartype == RECORDOID)
1706 {
1708
1709 rte = GetRTEByRangeTablePosn(pstate, var->varno, var->varlevelsup);
1710 if (rte->rtekind == RTE_SUBQUERY)
1711 {
1712 /* Subselect-in-FROM: examine sub-select's output expr */
1713 TargetEntry *ste = get_tle_by_resno(rte->subquery->targetList,
1714 attnum);
1715
1716 if (ste == NULL || ste->resjunk)
1717 return -1;
1718 expr = (Node *) ste->expr;
1719 if (IsA(expr, RowExpr))
1720 return list_length(((RowExpr *) expr)->args);
1721 }
1722 }
1723 }
1724 return -1;
1725}
int16 AttrNumber
Definition attnum.h:21
TargetEntry * get_tle_by_resno(List *tlist, AttrNumber resno)
RangeTblEntry * GetRTEByRangeTablePosn(ParseState *pstate, int varno, int sublevels_up)
@ RTE_SUBQUERY
int16 attnum
AttrNumber varattno
Definition primnodes.h:275
int varno
Definition primnodes.h:270
Index varlevelsup
Definition primnodes.h:295

References attnum, fb(), get_tle_by_resno(), GetRTEByRangeTablePosn(), IsA, list_length(), RTE_SUBQUERY, Var::varattno, Var::varlevelsup, and Var::varno.

Referenced by transformInsertRow().

◆ determineRecursiveColTypes()

static void determineRecursiveColTypes ( ParseState pstate,
Node larg,
List nrtargetlist 
)
static

Definition at line 2769 of file analyze.c.

2770{
2771 Node *node;
2772 int leftmostRTI;
2774 List *targetList;
2776 ListCell *nrtl;
2777 int next_resno;
2778
2779 /*
2780 * Find leftmost leaf SELECT
2781 */
2782 node = larg;
2783 while (node && IsA(node, SetOperationStmt))
2784 node = ((SetOperationStmt *) node)->larg;
2785 Assert(node && IsA(node, RangeTblRef));
2786 leftmostRTI = ((RangeTblRef *) node)->rtindex;
2787 leftmostQuery = rt_fetch(leftmostRTI, pstate->p_rtable)->subquery;
2789
2790 /*
2791 * Generate dummy targetlist using column names of leftmost select and
2792 * dummy result expressions of the non-recursive term.
2793 */
2794 targetList = NIL;
2795 next_resno = 1;
2796
2798 {
2801 char *colName;
2803
2804 Assert(!lefttle->resjunk);
2805 colName = pstrdup(lefttle->resname);
2806 tle = makeTargetEntry(nrtle->expr,
2807 next_resno++,
2808 colName,
2809 false);
2810 targetList = lappend(targetList, tle);
2811 }
2812
2813 /* Now build CTE's output column info using dummy targetlist */
2814 analyzeCTETargetList(pstate, pstate->p_parent_cte, targetList);
2815}
void analyzeCTETargetList(ParseState *pstate, CommonTableExpr *cte, List *tlist)
Definition parse_cte.c:571
#define rt_fetch(rangetable_index, rangetable)
Definition parsetree.h:31
CommonTableExpr * p_parent_cte
Definition parse_node.h:227
List * p_rtable
Definition parse_node.h:215

References analyzeCTETargetList(), Assert, fb(), forboth, IsA, lappend(), lfirst, makeTargetEntry(), NIL, ParseState::p_parent_cte, ParseState::p_rtable, pstrdup(), and rt_fetch.

Referenced by transformSetOperationTree().

◆ LCS_asString()

const char * LCS_asString ( LockClauseStrength  strength)

Definition at line 3717 of file analyze.c.

3718{
3719 switch (strength)
3720 {
3721 case LCS_NONE:
3722 Assert(false);
3723 break;
3724 case LCS_FORKEYSHARE:
3725 return "FOR KEY SHARE";
3726 case LCS_FORSHARE:
3727 return "FOR SHARE";
3728 case LCS_FORNOKEYUPDATE:
3729 return "FOR NO KEY UPDATE";
3730 case LCS_FORUPDATE:
3731 return "FOR UPDATE";
3732 }
3733 return "FOR some"; /* shouldn't happen */
3734}
@ LCS_FORUPDATE
Definition lockoptions.h:28
@ LCS_FORSHARE
Definition lockoptions.h:26
@ LCS_FORKEYSHARE
Definition lockoptions.h:25
@ LCS_FORNOKEYUPDATE
Definition lockoptions.h:27

References Assert, LCS_FORKEYSHARE, LCS_FORNOKEYUPDATE, LCS_FORSHARE, LCS_FORUPDATE, and LCS_NONE.

Referenced by CheckSelectLocking(), grouping_planner(), make_outerjoininfo(), transformDeclareCursorStmt(), transformLockingClause(), transformSetOperationStmt(), transformSetOperationTree(), and transformValuesClause().

◆ makeSortGroupClauseForSetOp()

SortGroupClause * makeSortGroupClauseForSetOp ( Oid  rescoltype,
bool  require_hash 
)

Definition at line 2373 of file analyze.c.

2374{
2376 Oid sortop;
2377 Oid eqop;
2378 bool hashable;
2379
2380 /* determine the eqop and optional sortop */
2382 false, true, false,
2383 &sortop, &eqop, NULL,
2384 &hashable);
2385
2386 /*
2387 * The type cache doesn't believe that record is hashable (see
2388 * cache_record_field_properties()), but if the caller really needs hash
2389 * support, we can assume it does. Worst case, if any components of the
2390 * record don't support hashing, we will fail at execution.
2391 */
2393 hashable = true;
2394
2395 /* we don't have a tlist yet, so can't assign sortgrouprefs */
2396 grpcl->tleSortGroupRef = 0;
2397 grpcl->eqop = eqop;
2398 grpcl->sortop = sortop;
2399 grpcl->reverse_sort = false; /* Sort-op is "less than", or InvalidOid */
2400 grpcl->nulls_first = false; /* OK with or without sortop */
2401 grpcl->hashable = hashable;
2402
2403 return grpcl;
2404}
void get_sort_group_operators(Oid argtype, bool needLT, bool needEQ, bool needGT, Oid *ltOpr, Oid *eqOpr, Oid *gtOpr, bool *isHashable)
Definition parse_oper.c:183

References fb(), get_sort_group_operators(), and makeNode.

Referenced by constructSetOpTargetlist(), and rewriteSearchAndCycle().

◆ parse_analyze_fixedparams()

Query * parse_analyze_fixedparams ( RawStmt parseTree,
const char sourceText,
const Oid paramTypes,
int  numParams,
QueryEnvironment queryEnv 
)

Definition at line 128 of file analyze.c.

131{
133 Query *query;
135
136 Assert(sourceText != NULL); /* required as of 8.4 */
137
138 pstate->p_sourcetext = sourceText;
139
140 if (numParams > 0)
141 setup_parse_fixed_parameters(pstate, paramTypes, numParams);
142
143 pstate->p_queryEnv = queryEnv;
144
145 query = transformTopLevelStmt(pstate, parseTree);
146
147 if (IsQueryIdEnabled())
148 jstate = JumbleQuery(query);
149
151 (*post_parse_analyze_hook) (pstate, query, jstate);
152
153 free_parsestate(pstate);
154
155 pgstat_report_query_id(query->queryId, false);
156
157 return query;
158}
void pgstat_report_query_id(int64 query_id, bool force)
void free_parsestate(ParseState *pstate)
Definition parse_node.c:72
ParseState * make_parsestate(ParseState *parentParseState)
Definition parse_node.c:39
void setup_parse_fixed_parameters(ParseState *pstate, const Oid *paramTypes, int numParams)
Definition parse_param.c:68
post_parse_analyze_hook_type post_parse_analyze_hook
Definition analyze.c:74
Query * transformTopLevelStmt(ParseState *pstate, RawStmt *parseTree)
Definition analyze.c:272
static bool IsQueryIdEnabled(void)
JumbleState * JumbleQuery(Query *query)
QueryEnvironment * p_queryEnv
Definition parse_node.h:241
const char * p_sourcetext
Definition parse_node.h:214

References Assert, fb(), free_parsestate(), IsQueryIdEnabled(), JumbleQuery(), make_parsestate(), ParseState::p_queryEnv, ParseState::p_sourcetext, pgstat_report_query_id(), post_parse_analyze_hook, setup_parse_fixed_parameters(), and transformTopLevelStmt().

Referenced by DefineView(), and pg_analyze_and_rewrite_fixedparams().

◆ parse_analyze_varparams()

Query * parse_analyze_varparams ( RawStmt parseTree,
const char sourceText,
Oid **  paramTypes,
int numParams,
QueryEnvironment queryEnv 
)

Definition at line 168 of file analyze.c.

171{
173 Query *query;
175
176 Assert(sourceText != NULL); /* required as of 8.4 */
177
178 pstate->p_sourcetext = sourceText;
179
180 setup_parse_variable_parameters(pstate, paramTypes, numParams);
181
182 pstate->p_queryEnv = queryEnv;
183
184 query = transformTopLevelStmt(pstate, parseTree);
185
186 /* make sure all is well with parameter types */
187 check_variable_parameters(pstate, query);
188
189 if (IsQueryIdEnabled())
190 jstate = JumbleQuery(query);
191
193 (*post_parse_analyze_hook) (pstate, query, jstate);
194
195 free_parsestate(pstate);
196
197 pgstat_report_query_id(query->queryId, false);
198
199 return query;
200}
void check_variable_parameters(ParseState *pstate, Query *query)
void setup_parse_variable_parameters(ParseState *pstate, Oid **paramTypes, int *numParams)
Definition parse_param.c:84

References Assert, check_variable_parameters(), fb(), free_parsestate(), IsQueryIdEnabled(), JumbleQuery(), make_parsestate(), ParseState::p_queryEnv, ParseState::p_sourcetext, pgstat_report_query_id(), post_parse_analyze_hook, setup_parse_variable_parameters(), and transformTopLevelStmt().

Referenced by pg_analyze_and_rewrite_varparams().

◆ parse_analyze_withcb()

Query * parse_analyze_withcb ( RawStmt parseTree,
const char sourceText,
ParserSetupHook  parserSetup,
void parserSetupArg,
QueryEnvironment queryEnv 
)

Definition at line 209 of file analyze.c.

213{
215 Query *query;
217
218 Assert(sourceText != NULL); /* required as of 8.4 */
219
220 pstate->p_sourcetext = sourceText;
221 pstate->p_queryEnv = queryEnv;
222 (*parserSetup) (pstate, parserSetupArg);
223
224 query = transformTopLevelStmt(pstate, parseTree);
225
226 if (IsQueryIdEnabled())
227 jstate = JumbleQuery(query);
228
230 (*post_parse_analyze_hook) (pstate, query, jstate);
231
232 free_parsestate(pstate);
233
234 pgstat_report_query_id(query->queryId, false);
235
236 return query;
237}

References Assert, fb(), free_parsestate(), IsQueryIdEnabled(), JumbleQuery(), make_parsestate(), ParseState::p_queryEnv, ParseState::p_sourcetext, pgstat_report_query_id(), post_parse_analyze_hook, and transformTopLevelStmt().

Referenced by pg_analyze_and_rewrite_withcb().

◆ parse_sub_analyze()

Query * parse_sub_analyze ( Node parseTree,
ParseState parentParseState,
CommonTableExpr parentCTE,
bool  locked_from_parent,
bool  resolve_unknowns 
)

Definition at line 245 of file analyze.c.

249{
250 ParseState *pstate = make_parsestate(parentParseState);
251 Query *query;
252
253 pstate->p_parent_cte = parentCTE;
256
257 query = transformStmt(pstate, parseTree);
258
259 free_parsestate(pstate);
260
261 return query;
262}
Query * transformStmt(ParseState *pstate, Node *parseTree)
Definition analyze.c:335
bool p_locked_from_parent
Definition parse_node.h:236
bool p_resolve_unknowns
Definition parse_node.h:238

References fb(), free_parsestate(), make_parsestate(), ParseState::p_locked_from_parent, ParseState::p_parent_cte, ParseState::p_resolve_unknowns, and transformStmt().

Referenced by analyzeCTE(), transformRangeSubselect(), transformSetOperationTree(), and transformSubLink().

◆ query_requires_rewrite_plan()

bool query_requires_rewrite_plan ( Query query)

Definition at line 543 of file analyze.c.

544{
545 bool result;
546
547 if (query->commandType != CMD_UTILITY)
548 {
549 /* All optimizable statements require rewriting/planning */
550 result = true;
551 }
552 else
553 {
554 /* This list should match stmt_requires_parse_analysis() */
555 switch (nodeTag(query->utilityStmt))
556 {
558 case T_ExplainStmt:
560 case T_CallStmt:
561 result = true;
562 break;
563 default:
564 result = false;
565 break;
566 }
567 }
568 return result;
569}
#define nodeTag(nodeptr)
Definition nodes.h:137
@ CMD_UTILITY
Definition nodes.h:278
CmdType commandType
Definition parsenodes.h:124
Node * utilityStmt
Definition parsenodes.h:144

References CMD_UTILITY, Query::commandType, fb(), nodeTag, result, and Query::utilityStmt.

Referenced by BuildingPlanRequiresSnapshot(), and StmtPlanRequiresRevalidation().

◆ stmt_requires_parse_analysis()

bool stmt_requires_parse_analysis ( RawStmt parseTree)

Definition at line 470 of file analyze.c.

471{
472 bool result;
473
474 switch (nodeTag(parseTree->stmt))
475 {
476 /*
477 * Optimizable statements
478 */
479 case T_InsertStmt:
480 case T_DeleteStmt:
481 case T_UpdateStmt:
482 case T_MergeStmt:
483 case T_SelectStmt:
484 case T_ReturnStmt:
485 case T_PLAssignStmt:
486 result = true;
487 break;
488
489 /*
490 * Special cases
491 */
493 case T_ExplainStmt:
495 case T_CallStmt:
496 result = true;
497 break;
498
499 default:
500 /* all other statements just get wrapped in a CMD_UTILITY Query */
501 result = false;
502 break;
503 }
504
505 return result;
506}

References fb(), nodeTag, and result.

Referenced by analyze_requires_snapshot(), and StmtPlanRequiresRevalidation().

◆ transformCallStmt()

static Query * transformCallStmt ( ParseState pstate,
CallStmt stmt 
)
static

Definition at line 3592 of file analyze.c.

3593{
3594 List *targs;
3595 ListCell *lc;
3596 Node *node;
3597 FuncExpr *fexpr;
3600 bool isNull;
3601 List *outargs = NIL;
3602 Query *result;
3603
3604 /*
3605 * First, do standard parse analysis on the procedure call and its
3606 * arguments, allowing us to identify the called procedure.
3607 */
3608 targs = NIL;
3609 foreach(lc, stmt->funccall->args)
3610 {
3611 targs = lappend(targs, transformExpr(pstate,
3612 (Node *) lfirst(lc),
3614 }
3615
3616 node = ParseFuncOrColumn(pstate,
3617 stmt->funccall->funcname,
3618 targs,
3619 pstate->p_last_srf,
3620 stmt->funccall,
3621 true,
3622 stmt->funccall->location);
3623
3624 assign_expr_collations(pstate, node);
3625
3626 fexpr = castNode(FuncExpr, node);
3627
3630 elog(ERROR, "cache lookup failed for function %u", fexpr->funcid);
3631
3632 /*
3633 * Expand the argument list to deal with named-argument notation and
3634 * default arguments. For ordinary FuncExprs this'd be done during
3635 * planning, but a CallStmt doesn't go through planning, and there seems
3636 * no good reason not to do it here.
3637 */
3639 true,
3640 fexpr->funcresulttype,
3641 proctup);
3642
3643 /* Fetch proargmodes; if it's null, there are no output args */
3646 &isNull);
3647 if (!isNull)
3648 {
3649 /*
3650 * Split the list into input arguments in fexpr->args and output
3651 * arguments in stmt->outargs. INOUT arguments appear in both lists.
3652 */
3653 ArrayType *arr;
3654 int numargs;
3655 char *argmodes;
3656 List *inargs;
3657 int i;
3658
3659 arr = DatumGetArrayTypeP(proargmodes); /* ensure not toasted */
3660 numargs = list_length(fexpr->args);
3661 if (ARR_NDIM(arr) != 1 ||
3662 ARR_DIMS(arr)[0] != numargs ||
3663 ARR_HASNULL(arr) ||
3664 ARR_ELEMTYPE(arr) != CHAROID)
3665 elog(ERROR, "proargmodes is not a 1-D char array of length %d or it contains nulls",
3666 numargs);
3667 argmodes = (char *) ARR_DATA_PTR(arr);
3668
3669 inargs = NIL;
3670 i = 0;
3671 foreach(lc, fexpr->args)
3672 {
3673 Node *n = lfirst(lc);
3674
3675 switch (argmodes[i])
3676 {
3677 case PROARGMODE_IN:
3679 inargs = lappend(inargs, n);
3680 break;
3681 case PROARGMODE_OUT:
3682 outargs = lappend(outargs, n);
3683 break;
3684 case PROARGMODE_INOUT:
3685 inargs = lappend(inargs, n);
3686 outargs = lappend(outargs, copyObject(n));
3687 break;
3688 default:
3689 /* note we don't support PROARGMODE_TABLE */
3690 elog(ERROR, "invalid argmode %c for procedure",
3691 argmodes[i]);
3692 break;
3693 }
3694 i++;
3695 }
3696 fexpr->args = inargs;
3697 }
3698
3699 stmt->funcexpr = fexpr;
3700 stmt->outargs = outargs;
3701
3703
3704 /* represent the command as a utility Query */
3706 result->commandType = CMD_UTILITY;
3707 result->utilityStmt = (Node *) stmt;
3708
3709 return result;
3710}
#define ARR_NDIM(a)
Definition array.h:290
#define ARR_DATA_PTR(a)
Definition array.h:322
#define DatumGetArrayTypeP(X)
Definition array.h:261
#define ARR_ELEMTYPE(a)
Definition array.h:292
#define ARR_DIMS(a)
Definition array.h:294
#define ARR_HASNULL(a)
Definition array.h:291
List * expand_function_arguments(List *args, bool include_out_arguments, Oid result_type, HeapTuple func_tuple)
Definition clauses.c:4930
#define elog(elevel,...)
Definition elog.h:228
#define HeapTupleIsValid(tuple)
Definition htup.h:78
#define stmt
#define copyObject(obj)
Definition nodes.h:230
#define castNode(_type_, nodeptr)
Definition nodes.h:180
void assign_expr_collations(ParseState *pstate, Node *expr)
Node * transformExpr(ParseState *pstate, Node *expr, ParseExprKind exprKind)
Definition parse_expr.c:121
Node * ParseFuncOrColumn(ParseState *pstate, List *funcname, List *fargs, Node *last_srf, FuncCall *fn, bool proc_call, int location)
Definition parse_func.c:92
@ EXPR_KIND_CALL_ARGUMENT
Definition parse_node.h:82
static Datum ObjectIdGetDatum(Oid X)
Definition postgres.h:252
uint64_t Datum
Definition postgres.h:70
Node * p_last_srf
Definition parse_node.h:252
void ReleaseSysCache(HeapTuple tuple)
Definition syscache.c:265
HeapTuple SearchSysCache1(SysCacheIdentifier cacheId, Datum key1)
Definition syscache.c:221
Datum SysCacheGetAttr(SysCacheIdentifier cacheId, HeapTuple tup, AttrNumber attributeNumber, bool *isNull)
Definition syscache.c:596

References ARR_DATA_PTR, ARR_DIMS, ARR_ELEMTYPE, ARR_HASNULL, ARR_NDIM, assign_expr_collations(), castNode, CMD_UTILITY, copyObject, DatumGetArrayTypeP, elog, ERROR, expand_function_arguments(), EXPR_KIND_CALL_ARGUMENT, fb(), HeapTupleIsValid, i, lappend(), lfirst, list_length(), makeNode, NIL, ObjectIdGetDatum(), ParseState::p_last_srf, ParseFuncOrColumn(), ReleaseSysCache(), result, SearchSysCache1(), stmt, SysCacheGetAttr(), and transformExpr().

Referenced by transformStmt().

◆ transformCreateTableAsStmt()

static Query * transformCreateTableAsStmt ( ParseState pstate,
CreateTableAsStmt stmt 
)
static

Definition at line 3513 of file analyze.c.

3514{
3515 Query *result;
3516 Query *query;
3517
3518 /* transform contained query, not allowing SELECT INTO */
3519 query = transformStmt(pstate, stmt->query);
3520 stmt->query = (Node *) query;
3521
3522 /* additional work needed for CREATE MATERIALIZED VIEW */
3523 if (stmt->objtype == OBJECT_MATVIEW)
3524 {
3526
3527 /*
3528 * Prohibit a data-modifying CTE in the query used to create a
3529 * materialized view. It's not sufficiently clear what the user would
3530 * want to happen if the MV is refreshed or incrementally maintained.
3531 */
3532 if (query->hasModifyingCTE)
3533 ereport(ERROR,
3535 errmsg("materialized views must not use data-modifying statements in WITH")));
3536
3537 /*
3538 * Check whether any temporary database objects are used in the
3539 * creation query. It would be hard to refresh data or incrementally
3540 * maintain it if a source disappeared.
3541 */
3543 ereport(ERROR,
3545 errmsg("materialized views must not use temporary objects"),
3546 errdetail("This view depends on temporary %s.",
3548
3549 /*
3550 * A materialized view would either need to save parameters for use in
3551 * maintaining/loading the data or prohibit them entirely. The latter
3552 * seems safer and more sane.
3553 */
3555 ereport(ERROR,
3557 errmsg("materialized views may not be defined using bound parameters")));
3558
3559 /*
3560 * For now, we disallow unlogged materialized views, because it seems
3561 * like a bad idea for them to just go to empty after a crash. (If we
3562 * could mark them as unpopulated, that would be better, but that
3563 * requires catalog changes which crash recovery can't presently
3564 * handle.)
3565 */
3566 if (stmt->into->rel->relpersistence == RELPERSISTENCE_UNLOGGED)
3567 ereport(ERROR,
3569 errmsg("materialized views cannot be unlogged")));
3570
3571 /*
3572 * At runtime, we'll need a copy of the parsed-but-not-rewritten Query
3573 * for purposes of creating the view's ON SELECT rule. We stash that
3574 * in the IntoClause because that's where intorel_startup() can
3575 * conveniently get it from.
3576 */
3577 stmt->into->viewQuery = copyObject(query);
3578 }
3579
3580 /* represent the command as a utility Query */
3582 result->commandType = CMD_UTILITY;
3583 result->utilityStmt = (Node *) stmt;
3584
3585 return result;
3586}
bool query_uses_temp_object(Query *query, ObjectAddress *temp_object)
int errdetail(const char *fmt,...) pg_attribute_printf(1
char * getObjectDescription(const ObjectAddress *object, bool missing_ok)
bool query_contains_extern_params(Query *query)
@ OBJECT_MATVIEW

References CMD_UTILITY, copyObject, ereport, errcode(), errdetail(), errmsg, ERROR, fb(), getObjectDescription(), makeNode, OBJECT_MATVIEW, query_contains_extern_params(), query_uses_temp_object(), result, stmt, and transformStmt().

Referenced by transformStmt().

◆ transformDeclareCursorStmt()

static Query * transformDeclareCursorStmt ( ParseState pstate,
DeclareCursorStmt stmt 
)
static

Definition at line 3368 of file analyze.c.

3369{
3370 Query *result;
3371 Query *query;
3372
3373 if ((stmt->options & CURSOR_OPT_SCROLL) &&
3374 (stmt->options & CURSOR_OPT_NO_SCROLL))
3375 ereport(ERROR,
3377 /* translator: %s is a SQL keyword */
3378 errmsg("cannot specify both %s and %s",
3379 "SCROLL", "NO SCROLL")));
3380
3381 if ((stmt->options & CURSOR_OPT_ASENSITIVE) &&
3382 (stmt->options & CURSOR_OPT_INSENSITIVE))
3383 ereport(ERROR,
3385 /* translator: %s is a SQL keyword */
3386 errmsg("cannot specify both %s and %s",
3387 "ASENSITIVE", "INSENSITIVE")));
3388
3389 /* Transform contained query, not allowing SELECT INTO */
3390 query = transformStmt(pstate, stmt->query);
3391 stmt->query = (Node *) query;
3392
3393 /* Grammar should not have allowed anything but SELECT */
3394 if (!IsA(query, Query) ||
3395 query->commandType != CMD_SELECT)
3396 elog(ERROR, "unexpected non-SELECT command in DECLARE CURSOR");
3397
3398 /*
3399 * We also disallow data-modifying WITH in a cursor. (This could be
3400 * allowed, but the semantics of when the updates occur might be
3401 * surprising.)
3402 */
3403 if (query->hasModifyingCTE)
3404 ereport(ERROR,
3406 errmsg("DECLARE CURSOR must not contain data-modifying statements in WITH")));
3407
3408 /* FOR UPDATE and WITH HOLD are not compatible */
3409 if (query->rowMarks != NIL && (stmt->options & CURSOR_OPT_HOLD))
3410 ereport(ERROR,
3412 /*------
3413 translator: %s is a SQL row locking clause such as FOR UPDATE */
3414 errmsg("DECLARE CURSOR WITH HOLD ... %s is not supported",
3416 linitial(query->rowMarks))->strength)),
3417 errdetail("Holdable cursors must be READ ONLY.")));
3418
3419 /* FOR UPDATE and SCROLL are not compatible */
3420 if (query->rowMarks != NIL && (stmt->options & CURSOR_OPT_SCROLL))
3421 ereport(ERROR,
3423 /*------
3424 translator: %s is a SQL row locking clause such as FOR UPDATE */
3425 errmsg("DECLARE SCROLL CURSOR ... %s is not supported",
3427 linitial(query->rowMarks))->strength)),
3428 errdetail("Scrollable cursors must be READ ONLY.")));
3429
3430 /* FOR UPDATE and INSENSITIVE are not compatible */
3431 if (query->rowMarks != NIL && (stmt->options & CURSOR_OPT_INSENSITIVE))
3432 ereport(ERROR,
3434 /*------
3435 translator: %s is a SQL row locking clause such as FOR UPDATE */
3436 errmsg("DECLARE INSENSITIVE CURSOR ... %s is not valid",
3438 linitial(query->rowMarks))->strength)),
3439 errdetail("Insensitive cursors must be READ ONLY.")));
3440
3441 /* represent the command as a utility Query */
3443 result->commandType = CMD_UTILITY;
3444 result->utilityStmt = (Node *) stmt;
3445
3446 return result;
3447}
@ CMD_SELECT
Definition nodes.h:273
#define CURSOR_OPT_INSENSITIVE
#define CURSOR_OPT_SCROLL
#define CURSOR_OPT_HOLD
#define CURSOR_OPT_ASENSITIVE
#define CURSOR_OPT_NO_SCROLL
#define linitial(l)
Definition pg_list.h:178

References CMD_SELECT, CMD_UTILITY, Query::commandType, CURSOR_OPT_ASENSITIVE, CURSOR_OPT_HOLD, CURSOR_OPT_INSENSITIVE, CURSOR_OPT_NO_SCROLL, CURSOR_OPT_SCROLL, elog, ereport, errcode(), errdetail(), errmsg, ERROR, fb(), IsA, LCS_asString(), linitial, makeNode, NIL, result, Query::rowMarks, stmt, and transformStmt().

Referenced by transformStmt().

◆ transformDeleteStmt()

static Query * transformDeleteStmt ( ParseState pstate,
DeleteStmt stmt 
)
static

Definition at line 576 of file analyze.c.

577{
578 Query *qry = makeNode(Query);
580 Node *qual;
581
582 qry->commandType = CMD_DELETE;
583
584 /* process the WITH clause independently of all else */
585 if (stmt->withClause)
586 {
587 qry->hasRecursive = stmt->withClause->recursive;
588 qry->cteList = transformWithClause(pstate, stmt->withClause);
589 qry->hasModifyingCTE = pstate->p_hasModifyingCTE;
590 }
591
592 /* set up range table with just the result rel */
593 qry->resultRelation = setTargetTable(pstate, stmt->relation,
594 stmt->relation->inh,
595 true,
596 ACL_DELETE);
597 nsitem = pstate->p_target_nsitem;
598
599 /* disallow DELETE ... WHERE CURRENT OF on a view */
600 if (stmt->whereClause &&
601 IsA(stmt->whereClause, CurrentOfExpr) &&
602 pstate->p_target_relation->rd_rel->relkind == RELKIND_VIEW)
605 errmsg("WHERE CURRENT OF on a view is not implemented"));
606
607 /* there's no DISTINCT in DELETE */
608 qry->distinctClause = NIL;
609
610 /* subqueries in USING cannot access the result relation */
611 nsitem->p_lateral_only = true;
612 nsitem->p_lateral_ok = false;
613
614 /*
615 * The USING clause is non-standard SQL syntax, and is equivalent in
616 * functionality to the FROM list that can be specified for UPDATE. The
617 * USING keyword is used rather than FROM because FROM is already a
618 * keyword in the DELETE syntax.
619 */
620 transformFromClause(pstate, stmt->usingClause);
621
622 /* remaining clauses can reference the result relation normally */
623 nsitem->p_lateral_only = false;
624 nsitem->p_lateral_ok = true;
625
626 if (stmt->forPortionOf)
628 qry->resultRelation,
630 stmt->whereClause,
631 false);
632
633 qual = transformWhereClause(pstate, stmt->whereClause,
634 EXPR_KIND_WHERE, "WHERE");
635
636 transformReturningClause(pstate, qry, stmt->returningClause,
638
639 /* done building the range table and jointree */
640 qry->rtable = pstate->p_rtable;
641 qry->rteperminfos = pstate->p_rteperminfos;
642 qry->jointree = makeFromExpr(pstate->p_joinlist, qual);
643
644 qry->hasSubLinks = pstate->p_hasSubLinks;
645 qry->hasWindowFuncs = pstate->p_hasWindowFuncs;
646 qry->hasTargetSRFs = pstate->p_hasTargetSRFs;
647 qry->hasAggs = pstate->p_hasAggs;
648
649 assign_query_collations(pstate, qry);
650
651 /* this must be done after collations, for reliable comparison of exprs */
652 if (pstate->p_hasAggs)
653 parseCheckAggregates(pstate, qry);
654
655 return qry;
656}
FromExpr * makeFromExpr(List *fromlist, Node *quals)
Definition makefuncs.c:336
@ CMD_DELETE
Definition nodes.h:276
void parseCheckAggregates(ParseState *pstate, Query *qry)
Definition parse_agg.c:1160
Node * transformWhereClause(ParseState *pstate, Node *clause, ParseExprKind exprKind, const char *constructName)
void transformFromClause(ParseState *pstate, List *frmList)
int setTargetTable(ParseState *pstate, RangeVar *relation, bool inh, bool alsoSource, AclMode requiredPerms)
void assign_query_collations(ParseState *pstate, Query *query)
List * transformWithClause(ParseState *pstate, WithClause *withClause)
Definition parse_cte.c:110
@ EXPR_KIND_WHERE
Definition parse_node.h:46
@ EXPR_KIND_RETURNING
Definition parse_node.h:65
#define ACL_DELETE
Definition parsenodes.h:79
static ForPortionOfExpr * transformForPortionOfClause(ParseState *pstate, int rtindex, const ForPortionOfClause *forPortionOf, const Node *whereClause, bool isUpdate)
Definition analyze.c:1321
void transformReturningClause(ParseState *pstate, Query *qry, ReturningClause *returningClause, ParseExprKind exprKind)
Definition analyze.c:3064
bool p_hasTargetSRFs
Definition parse_node.h:248
bool p_hasWindowFuncs
Definition parse_node.h:247
bool p_hasModifyingCTE
Definition parse_node.h:250
List * p_rteperminfos
Definition parse_node.h:216
Relation p_target_relation
Definition parse_node.h:228
bool p_hasSubLinks
Definition parse_node.h:249
List * p_joinlist
Definition parse_node.h:220
bool p_hasAggs
Definition parse_node.h:246
FromExpr * jointree
Definition parsenodes.h:187
List * cteList
Definition parsenodes.h:178
ForPortionOfExpr * forPortionOf
Definition parsenodes.h:153
List * rtable
Definition parsenodes.h:180
Form_pg_class rd_rel
Definition rel.h:111

References ACL_DELETE, assign_query_collations(), CMD_DELETE, Query::commandType, Query::cteList, Query::distinctClause, ereport, errcode(), errmsg, ERROR, EXPR_KIND_RETURNING, EXPR_KIND_WHERE, fb(), Query::forPortionOf, IsA, Query::jointree, makeFromExpr(), makeNode, NIL, ParseState::p_hasAggs, ParseState::p_hasModifyingCTE, ParseState::p_hasSubLinks, ParseState::p_hasTargetSRFs, ParseState::p_hasWindowFuncs, ParseState::p_joinlist, ParseState::p_rtable, ParseState::p_rteperminfos, ParseState::p_target_nsitem, ParseState::p_target_relation, parseCheckAggregates(), RelationData::rd_rel, Query::rtable, setTargetTable(), stmt, transformForPortionOfClause(), transformFromClause(), transformReturningClause(), transformWhereClause(), and transformWithClause().

Referenced by transformStmt().

◆ transformExplainStmt()

static Query * transformExplainStmt ( ParseState pstate,
ExplainStmt stmt 
)
static

Definition at line 3461 of file analyze.c.

3462{
3463 Query *result;
3464 bool generic_plan = false;
3465 Oid *paramTypes = NULL;
3466 int numParams = 0;
3467
3468 /*
3469 * If we have no external source of parameter definitions, and the
3470 * GENERIC_PLAN option is specified, then accept variable parameter
3471 * definitions (similarly to PREPARE, for example).
3472 */
3473 if (pstate->p_paramref_hook == NULL)
3474 {
3475 ListCell *lc;
3476
3477 foreach(lc, stmt->options)
3478 {
3479 DefElem *opt = (DefElem *) lfirst(lc);
3480
3481 if (strcmp(opt->defname, "generic_plan") == 0)
3483 /* don't "break", as we want the last value */
3484 }
3485 if (generic_plan)
3486 setup_parse_variable_parameters(pstate, &paramTypes, &numParams);
3487 }
3488
3489 /* transform contained query, allowing SELECT INTO */
3490 stmt->query = (Node *) transformOptionalSelectInto(pstate, stmt->query);
3491
3492 /* make sure all is well with parameter types */
3493 if (generic_plan)
3494 check_variable_parameters(pstate, (Query *) stmt->query);
3495
3496 /* represent the command as a utility Query */
3498 result->commandType = CMD_UTILITY;
3499 result->utilityStmt = (Node *) stmt;
3500
3501 return result;
3502}
bool defGetBoolean(DefElem *def)
Definition define.c:93
static Query * transformOptionalSelectInto(ParseState *pstate, Node *parseTree)
Definition analyze.c:296
char * defname
Definition parsenodes.h:862
ParseParamRefHook p_paramref_hook
Definition parse_node.h:260

References check_variable_parameters(), CMD_UTILITY, defGetBoolean(), DefElem::defname, fb(), lfirst, makeNode, ParseState::p_paramref_hook, result, setup_parse_variable_parameters(), stmt, and transformOptionalSelectInto().

Referenced by transformStmt().

◆ transformForPortionOfClause()

static ForPortionOfExpr * transformForPortionOfClause ( ParseState pstate,
int  rtindex,
const ForPortionOfClause forPortionOf,
const Node whereClause,
bool  isUpdate 
)
static

Definition at line 1321 of file analyze.c.

1326{
1329 Form_pg_attribute attr;
1331 Oid opclass;
1332 Oid opfamily;
1333 Oid opcintype;
1334 Oid funcid = InvalidOid;
1336 Oid opid;
1337 OpExpr *op;
1339 Var *rangeVar;
1340
1341 /* disallow FOR PORTION OF ... WHERE CURRENT OF */
1342 if (whereClause && IsA(whereClause, CurrentOfExpr))
1343 ereport(ERROR,
1345 errmsg("WHERE CURRENT OF with FOR PORTION OF is not implemented"));
1346
1348
1349 /* Look up the FOR PORTION OF name requested. */
1350 range_attno = attnameAttNum(targetrel, forPortionOf->range_name, false);
1352 ereport(ERROR,
1354 errmsg("column \"%s\" of relation \"%s\" does not exist",
1355 forPortionOf->range_name,
1357 parser_errposition(pstate, forPortionOf->location)));
1358 attr = TupleDescAttr(targetrel->rd_att, range_attno - 1);
1359
1360 attbasetype = getBaseType(attr->atttypid);
1361
1362 rangeVar = makeVar(rtindex,
1364 attr->atttypid,
1365 attr->atttypmod,
1366 attr->attcollation,
1367 0);
1368 rangeVar->location = forPortionOf->location;
1369 result->rangeVar = rangeVar;
1370
1371 /* Require SELECT privilege on the application-time column. */
1372 markVarForSelectPriv(pstate, rangeVar);
1373
1374 /*
1375 * Use the basetype for the target, which shouldn't be required to follow
1376 * domain rules. The table's column type is in the Var if we need it.
1377 */
1378 result->rangeType = attbasetype;
1379 result->isDomain = attbasetype != attr->atttypid;
1380
1381 if (forPortionOf->target)
1382 {
1385
1386 /*
1387 * We were already given an expression for the target, so we don't
1388 * have to build anything. We still have to make sure we got the right
1389 * type. NULL will be caught be the executor.
1390 */
1391
1392 result->targetRange = transformExpr(pstate,
1393 forPortionOf->target,
1395
1396 actual_target_type = exprType(result->targetRange);
1397
1399 ereport(ERROR,
1401 errmsg("could not coerce FOR PORTION OF target from %s to %s",
1404 parser_errposition(pstate, exprLocation(forPortionOf->target))));
1405
1406 result->targetRange = coerce_type(pstate,
1407 result->targetRange,
1410 -1,
1413 exprLocation(forPortionOf->target));
1414
1415 /*
1416 * XXX: For now we only support ranges and multiranges, so we fail on
1417 * anything else.
1418 */
1420 ereport(ERROR,
1422 errmsg("column \"%s\" of relation \"%s\" is not a range or multirange type",
1423 forPortionOf->range_name,
1425 parser_errposition(pstate, forPortionOf->location)));
1426
1427 }
1428 else
1429 {
1433 List *args;
1434
1435 /*
1436 * Make sure it's a range column. XXX: We could support this syntax on
1437 * multirange columns too, if we just built a one-range multirange
1438 * from the FROM/TO phrases.
1439 */
1441 ereport(ERROR,
1443 errmsg("column \"%s\" of relation \"%s\" is not a range type",
1444 forPortionOf->range_name,
1446 parser_errposition(pstate, forPortionOf->location)));
1447
1451
1452 /*
1453 * Build a range from the FROM ... TO ... bounds. This should give a
1454 * constant result, so we accept functions like NOW() but not column
1455 * references, subqueries, etc.
1456 */
1457 result->targetFrom = transformExpr(pstate,
1458 forPortionOf->target_start,
1460 result->targetTo = transformExpr(pstate,
1461 forPortionOf->target_end,
1463 actual_arg_types[0] = exprType(result->targetFrom);
1464 actual_arg_types[1] = exprType(result->targetTo);
1465 args = list_make2(copyObject(result->targetFrom),
1466 copyObject(result->targetTo));
1467
1468 /*
1469 * Check the bound types separately, for better error message and
1470 * location
1471 */
1473 ereport(ERROR,
1475 errmsg("could not coerce FOR PORTION OF %s bound from %s to %s",
1476 "FROM",
1479 parser_errposition(pstate, exprLocation(forPortionOf->target_start))));
1481 ereport(ERROR,
1483 errmsg("could not coerce FOR PORTION OF %s bound from %s to %s",
1484 "TO",
1487 parser_errposition(pstate, exprLocation(forPortionOf->target_end))));
1488
1492 args,
1494 }
1495
1496 /*
1497 * Build overlapsExpr to use as an extra qual. This means we only hit rows
1498 * matching the FROM & TO bounds. We must look up the overlaps operator
1499 * (usually "&&").
1500 */
1501 opclass = GetDefaultOpClass(attr->atttypid, GIST_AM_OID);
1502 if (!OidIsValid(opclass))
1503 ereport(ERROR,
1505 errmsg("data type %s has no default operator class for access method \"%s\"",
1506 format_type_be(attr->atttypid), "gist"),
1507 errhint("You must define a default operator class for the data type.")));
1508
1509 /* Look up the operators and functions we need. */
1511 op = makeNode(OpExpr);
1512 op->opno = opid;
1513 op->opfuncid = get_opcode(opid);
1514 op->opresulttype = BOOLOID;
1515 op->args = list_make2(copyObject(rangeVar), copyObject(result->targetRange));
1516 result->overlapsExpr = (Node *) op;
1517
1518 /*
1519 * Look up the without_portion func. This computes the bounds of temporal
1520 * leftovers.
1521 *
1522 * XXX: Find a more extensible way to look up the function, permitting
1523 * user-defined types. An opclass support function doesn't make sense,
1524 * since there is no index involved. Perhaps a type support function.
1525 */
1526 if (get_opclass_opfamily_and_input_type(opclass, &opfamily, &opcintype))
1527 switch (opcintype)
1528 {
1529 case ANYRANGEOID:
1530 result->withoutPortionProc = F_RANGE_MINUS_MULTI;
1531 break;
1532 case ANYMULTIRANGEOID:
1533 result->withoutPortionProc = F_MULTIRANGE_MINUS_MULTI;
1534 break;
1535 default:
1536 elog(ERROR, "unexpected opcintype: %u", opcintype);
1537 }
1538 else
1539 elog(ERROR, "unexpected opclass: %u", opclass);
1540
1541 if (isUpdate)
1542 {
1543 /*
1544 * Now make sure we update the start/end time of the record. For a
1545 * range col (r) this is `r = r * targetRange` (where * is the
1546 * intersect operator).
1547 */
1549 List *funcArgs;
1553
1554 /*
1555 * Whatever operator is used for intersect by temporal foreign keys,
1556 * we can use its backing procedure for intersects in FOR PORTION OF.
1557 * XXX: Share code with FindFKPeriodOpers?
1558 */
1559 switch (opcintype)
1560 {
1561 case ANYRANGEOID:
1563 break;
1564 case ANYMULTIRANGEOID:
1566 break;
1567 default:
1568 elog(ERROR, "unexpected opcintype: %u", opcintype);
1569 }
1570 funcid = get_opcode(intersectoperoid);
1571 if (!OidIsValid(funcid))
1572 ereport(ERROR,
1574 errmsg("could not identify an intersect function for type %s",
1575 format_type_be(opcintype)));
1576
1577 funcArgs = list_make2(copyObject(rangeVar),
1578 copyObject(result->targetRange));
1582
1583 /*
1584 * Coerce to domain if necessary. If we skip this, we will allow
1585 * updating to forbidden values.
1586 */
1587 rangeTLEExpr = coerce_type(pstate,
1590 attr->atttypid,
1591 -1,
1594 exprLocation(forPortionOf->target));
1595
1596 /* Make a TLE to set the range column */
1597 result->rangeTargetList = NIL;
1599 forPortionOf->range_name, false);
1600 result->rangeTargetList = lappend(result->rangeTargetList, tle);
1601
1602 /* Mark the range column as requiring update permissions */
1603 target_perminfo->updatedCols = bms_add_member(target_perminfo->updatedCols,
1605 }
1606 else
1607 result->rangeTargetList = NIL;
1608
1609 result->range_name = forPortionOf->range_name;
1610 result->location = forPortionOf->location;
1611 result->targetLocation = forPortionOf->target_location;
1612
1613 return result;
1614}
Bitmapset * bms_add_member(Bitmapset *a, int x)
Definition bitmapset.c:799
#define OidIsValid(objectId)
Definition c.h:914
@ COMPARE_OVERLAP
Definition cmptype.h:40
int errhint(const char *fmt,...) pg_attribute_printf(1
char * format_type_be(Oid type_oid)
Oid GetDefaultOpClass(Oid type_id, Oid am_id)
Definition indexcmds.c:2371
void GetOperatorFromCompareType(Oid opclass, Oid rhstype, CompareType cmptype, Oid *opid, StrategyNumber *strat)
Definition indexcmds.c:2473
Oid get_range_subtype(Oid rangeOid)
Definition lsyscache.c:3735
RegProcedure get_range_constructor2(Oid rangeOid)
Definition lsyscache.c:3786
bool type_is_range(Oid typid)
Definition lsyscache.c:2996
bool get_opclass_opfamily_and_input_type(Oid opclass, Oid *opfamily, Oid *opcintype)
Definition lsyscache.c:1479
RegProcedure get_opcode(Oid opno)
Definition lsyscache.c:1577
Oid getBaseType(Oid typid)
Definition lsyscache.c:2829
bool type_is_multirange(Oid typid)
Definition lsyscache.c:3006
FuncExpr * makeFuncExpr(Oid funcid, Oid rettype, List *args, Oid funccollid, Oid inputcollid, CoercionForm fformat)
Definition makefuncs.c:594
Node * coerce_type(ParseState *pstate, Node *node, Oid inputTypeId, Oid targetTypeId, int32 targetTypeMod, CoercionContext ccontext, CoercionForm cformat, int location)
bool can_coerce_type(int nargs, const Oid *input_typeids, const Oid *target_typeids, CoercionContext ccontext)
void make_fn_arguments(ParseState *pstate, List *fargs, Oid *actual_arg_types, Oid *declared_arg_types)
@ EXPR_KIND_FOR_PORTION
Definition parse_node.h:59
void markVarForSelectPriv(ParseState *pstate, Var *var)
int attnameAttNum(Relation rd, const char *attname, bool sysColOK)
@ COERCE_IMPLICIT_CAST
Definition primnodes.h:759
@ COERCE_EXPLICIT_CALL
Definition primnodes.h:757
@ COERCION_IMPLICIT
Definition primnodes.h:737
#define RelationGetRelationName(relation)
Definition rel.h:550
uint16 StrategyNumber
Definition stratnum.h:22
ParseLoc target_location
Oid opno
Definition primnodes.h:835
List * args
Definition primnodes.h:853
ParseLoc location
Definition primnodes.h:311
#define FirstLowInvalidHeapAttributeNumber
Definition sysattr.h:27

References OpExpr::args, attnameAttNum(), bms_add_member(), can_coerce_type(), COERCE_EXPLICIT_CALL, COERCE_IMPLICIT_CAST, coerce_type(), COERCION_IMPLICIT, COMPARE_OVERLAP, copyObject, elog, ereport, errcode(), errhint(), errmsg, ERROR, EXPR_KIND_FOR_PORTION, exprLocation(), exprType(), fb(), FirstLowInvalidHeapAttributeNumber, format_type_be(), get_opclass_opfamily_and_input_type(), get_opcode(), get_range_constructor2(), get_range_subtype(), getBaseType(), GetDefaultOpClass(), GetOperatorFromCompareType(), InvalidAttrNumber, InvalidOid, IsA, lappend(), list_make2, ForPortionOfClause::location, Var::location, make_fn_arguments(), makeFuncExpr(), makeNode, makeTargetEntry(), makeVar(), markVarForSelectPriv(), NIL, OidIsValid, OpExpr::opno, ParseNamespaceItem::p_perminfo, ParseState::p_target_nsitem, ParseState::p_target_relation, parser_errposition(), ForPortionOfClause::range_name, RelationGetRelationName, result, ForPortionOfClause::target, ForPortionOfClause::target_end, ForPortionOfClause::target_location, ForPortionOfClause::target_start, transformExpr(), TupleDescAttr(), type_is_multirange(), and type_is_range().

Referenced by transformDeleteStmt(), and transformUpdateStmt().

◆ transformInsertRow()

List * transformInsertRow ( ParseState pstate,
List exprlist,
List stmtcols,
List icolumns,
List attrnos,
bool  strip_indirection 
)

Definition at line 1104 of file analyze.c.

1107{
1108 List *result;
1109 ListCell *lc;
1110 ListCell *icols;
1112
1113 /*
1114 * Check length of expr list. It must not have more expressions than
1115 * there are target columns. We allow fewer, but only if no explicit
1116 * columns list was given (the remaining columns are implicitly
1117 * defaulted). Note we must check this *after* transformation because
1118 * that could expand '*' into multiple items.
1119 */
1121 ereport(ERROR,
1123 errmsg("INSERT has more expressions than target columns"),
1124 parser_errposition(pstate,
1126 list_length(icolumns))))));
1127 if (stmtcols != NIL &&
1129 {
1130 /*
1131 * We can get here for cases like INSERT ... SELECT (a,b,c) FROM ...
1132 * where the user accidentally created a RowExpr instead of separate
1133 * columns. Add a suitable hint if that seems to be the problem,
1134 * because the main error message is quite misleading for this case.
1135 * (If there's no stmtcols, you'll get something about data type
1136 * mismatch, which is less misleading so we don't worry about giving a
1137 * hint in that case.)
1138 */
1139 ereport(ERROR,
1141 errmsg("INSERT has more target columns than expressions"),
1142 ((list_length(exprlist) == 1 &&
1145 errhint("The insertion source is a row expression containing the same number of columns expected by the INSERT. Did you accidentally use extra parentheses?") : 0),
1146 parser_errposition(pstate,
1148 list_length(exprlist))))));
1149 }
1150
1151 /*
1152 * Prepare columns for assignment to target table.
1153 */
1154 result = NIL;
1156 {
1157 Expr *expr = (Expr *) lfirst(lc);
1159 int attno = lfirst_int(attnos);
1160
1161 expr = transformAssignedExpr(pstate, expr,
1163 col->name,
1164 attno,
1165 col->indirection,
1166 col->location);
1167
1169 {
1170 /*
1171 * We need to remove top-level FieldStores and SubscriptingRefs,
1172 * as well as any CoerceToDomain appearing above one of those ---
1173 * but not a CoerceToDomain that isn't above one of those.
1174 */
1175 while (expr)
1176 {
1177 Expr *subexpr = expr;
1178
1179 while (IsA(subexpr, CoerceToDomain))
1180 {
1181 subexpr = ((CoerceToDomain *) subexpr)->arg;
1182 }
1183 if (IsA(subexpr, FieldStore))
1184 {
1185 FieldStore *fstore = (FieldStore *) subexpr;
1186
1187 expr = (Expr *) linitial(fstore->newvals);
1188 }
1189 else if (IsA(subexpr, SubscriptingRef))
1190 {
1191 SubscriptingRef *sbsref = (SubscriptingRef *) subexpr;
1192
1193 if (sbsref->refassgnexpr == NULL)
1194 break;
1195
1196 expr = sbsref->refassgnexpr;
1197 }
1198 else
1199 break;
1200 }
1201 }
1202
1203 result = lappend(result, expr);
1204 }
1205
1206 return result;
1207}
@ EXPR_KIND_INSERT_TARGET
Definition parse_node.h:55
Expr * transformAssignedExpr(ParseState *pstate, Expr *expr, ParseExprKind exprKind, const char *colname, int attrno, List *indirection, int location)
static int count_rowexpr_columns(ParseState *pstate, Node *expr)
Definition analyze.c:1694
#define lfirst_node(type, lc)
Definition pg_list.h:176
#define lfirst_int(lc)
Definition pg_list.h:173
#define forthree(cell1, list1, cell2, list2, cell3, list3)
Definition pg_list.h:595
static void * list_nth(const List *list, int n)
Definition pg_list.h:331
List * newvals
Definition primnodes.h:1176
Expr * refassgnexpr
Definition primnodes.h:726

References count_rowexpr_columns(), ereport, errcode(), errhint(), errmsg, ERROR, EXPR_KIND_INSERT_TARGET, exprLocation(), fb(), forthree, IsA, lappend(), lfirst, lfirst_int, lfirst_node, linitial, list_length(), list_nth(), FieldStore::newvals, NIL, parser_errposition(), SubscriptingRef::refassgnexpr, result, and transformAssignedExpr().

Referenced by transformInsertStmt(), and transformMergeStmt().

◆ transformInsertStmt()

static Query * transformInsertStmt ( ParseState pstate,
InsertStmt stmt 
)
static

Definition at line 663 of file analyze.c.

664{
665 Query *qry = makeNode(Query);
666 SelectStmt *selectStmt = (SelectStmt *) stmt->selectStmt;
667 List *exprList = NIL;
668 bool isGeneralSelect;
672 List *icolumns;
673 List *attrnos;
678 ListCell *lc;
681
682 /* There can't be any outer WITH to worry about */
683 Assert(pstate->p_ctenamespace == NIL);
684
685 qry->commandType = CMD_INSERT;
686
687 /* process the WITH clause independently of all else */
688 if (stmt->withClause)
689 {
690 qry->hasRecursive = stmt->withClause->recursive;
691 qry->cteList = transformWithClause(pstate, stmt->withClause);
692 qry->hasModifyingCTE = pstate->p_hasModifyingCTE;
693 }
694
695 qry->override = stmt->override;
696
697 /*
698 * ON CONFLICT DO UPDATE and ON CONFLICT DO SELECT FOR UPDATE/SHARE
699 * require UPDATE permission on the target relation.
700 */
701 requiresUpdatePerm = (stmt->onConflictClause &&
702 (stmt->onConflictClause->action == ONCONFLICT_UPDATE ||
703 (stmt->onConflictClause->action == ONCONFLICT_SELECT &&
704 stmt->onConflictClause->lockStrength != LCS_NONE)));
705
706 /*
707 * We have three cases to deal with: DEFAULT VALUES (selectStmt == NULL),
708 * VALUES list, or general SELECT input. We special-case VALUES, both for
709 * efficiency and so we can handle DEFAULT specifications.
710 *
711 * The grammar allows attaching ORDER BY, LIMIT, FOR UPDATE, or WITH to a
712 * VALUES clause. If we have any of those, treat it as a general SELECT;
713 * so it will work, but you can't use DEFAULT items together with those.
714 */
715 isGeneralSelect = (selectStmt && (selectStmt->valuesLists == NIL ||
716 selectStmt->sortClause != NIL ||
717 selectStmt->limitOffset != NULL ||
718 selectStmt->limitCount != NULL ||
719 selectStmt->lockingClause != NIL ||
720 selectStmt->withClause != NULL));
721
722 /*
723 * If a non-nil rangetable/namespace was passed in, and we are doing
724 * INSERT/SELECT, arrange to pass the rangetable/rteperminfos/namespace
725 * down to the SELECT. This can only happen if we are inside a CREATE
726 * RULE, and in that case we want the rule's OLD and NEW rtable entries to
727 * appear as part of the SELECT's rtable, not as outer references for it.
728 * (Kluge!) The SELECT's joinlist is not affected however. We must do
729 * this before adding the target table to the INSERT's rtable.
730 */
731 if (isGeneralSelect)
732 {
733 sub_rtable = pstate->p_rtable;
734 pstate->p_rtable = NIL;
736 pstate->p_rteperminfos = NIL;
737 sub_namespace = pstate->p_namespace;
738 pstate->p_namespace = NIL;
739 }
740 else
741 {
742 sub_rtable = NIL; /* not used, but keep compiler quiet */
745 }
746
747 /*
748 * Must get write lock on INSERT target table before scanning SELECT, else
749 * we will grab the wrong kind of initial lock if the target table is also
750 * mentioned in the SELECT part. Note that the target table is not added
751 * to the joinlist or namespace.
752 */
756 qry->resultRelation = setTargetTable(pstate, stmt->relation,
757 false, false, targetPerms);
758
759 /* Validate stmt->cols list, or build default list if no list given */
760 icolumns = checkInsertTargets(pstate, stmt->cols, &attrnos);
762
763 /*
764 * Determine which variant of INSERT we have.
765 */
766 if (selectStmt == NULL)
767 {
768 /*
769 * We have INSERT ... DEFAULT VALUES. We can handle this case by
770 * emitting an empty targetlist --- all columns will be defaulted when
771 * the planner expands the targetlist.
772 */
773 exprList = NIL;
774 }
775 else if (isGeneralSelect)
776 {
777 /*
778 * We make the sub-pstate a child of the outer pstate so that it can
779 * see any Param definitions supplied from above. Since the outer
780 * pstate's rtable and namespace are presently empty, there are no
781 * side-effects of exposing names the sub-SELECT shouldn't be able to
782 * see.
783 */
786
787 /*
788 * Process the source SELECT.
789 *
790 * It is important that this be handled just like a standalone SELECT;
791 * otherwise the behavior of SELECT within INSERT might be different
792 * from a stand-alone SELECT. (Indeed, Postgres up through 6.5 had
793 * bugs of just that nature...)
794 *
795 * The sole exception is that we prevent resolving unknown-type
796 * outputs as TEXT. This does not change the semantics since if the
797 * column type matters semantically, it would have been resolved to
798 * something else anyway. Doing this lets us resolve such outputs as
799 * the target column's type, which we handle below.
800 */
801 sub_pstate->p_rtable = sub_rtable;
802 sub_pstate->p_rteperminfos = sub_rteperminfos;
803 sub_pstate->p_joinexprs = NIL; /* sub_rtable has no joins */
804 sub_pstate->p_nullingrels = NIL;
805 sub_pstate->p_namespace = sub_namespace;
806 sub_pstate->p_resolve_unknowns = false;
807
809
811
812 /* The grammar should have produced a SELECT */
813 if (!IsA(selectQuery, Query) ||
814 selectQuery->commandType != CMD_SELECT)
815 elog(ERROR, "unexpected non-SELECT command in INSERT ... SELECT");
816
817 /*
818 * Make the source be a subquery in the INSERT's rangetable, and add
819 * it to the INSERT's joinlist (but not the namespace).
820 */
823 NULL,
824 false,
825 false);
826 addNSItemToQuery(pstate, nsitem, true, false, false);
827
828 /*----------
829 * Generate an expression list for the INSERT that selects all the
830 * non-resjunk columns from the subquery. (INSERT's tlist must be
831 * separate from the subquery's tlist because we may add columns,
832 * insert datatype coercions, etc.)
833 *
834 * HACK: unknown-type constants and params in the SELECT's targetlist
835 * are copied up as-is rather than being referenced as subquery
836 * outputs. This is to ensure that when we try to coerce them to
837 * the target column's datatype, the right things happen (see
838 * special cases in coerce_type). Otherwise, this fails:
839 * INSERT INTO foo SELECT 'bar', ... FROM baz
840 *----------
841 */
842 exprList = NIL;
843 foreach(lc, selectQuery->targetList)
844 {
846 Expr *expr;
847
848 if (tle->resjunk)
849 continue;
850 if (tle->expr &&
851 (IsA(tle->expr, Const) || IsA(tle->expr, Param)) &&
852 exprType((Node *) tle->expr) == UNKNOWNOID)
853 expr = tle->expr;
854 else
855 {
856 Var *var = makeVarFromTargetEntry(nsitem->p_rtindex, tle);
857
858 var->location = exprLocation((Node *) tle->expr);
859 expr = (Expr *) var;
860 }
861 exprList = lappend(exprList, expr);
862 }
863
864 /* Prepare row for assignment to target table */
866 stmt->cols,
868 false);
869 }
870 else if (list_length(selectStmt->valuesLists) > 1)
871 {
872 /*
873 * Process INSERT ... VALUES with multiple VALUES sublists. We
874 * generate a VALUES RTE holding the transformed expression lists, and
875 * build up a targetlist containing Vars that reference the VALUES
876 * RTE.
877 */
879 List *coltypes = NIL;
882 int sublist_length = -1;
883 bool lateral = false;
884
885 Assert(selectStmt->intoClause == NULL);
886
887 foreach(lc, selectStmt->valuesLists)
888 {
889 List *sublist = (List *) lfirst(lc);
890
891 /*
892 * Do basic expression transformation (same as a ROW() expr, but
893 * allow SetToDefault at top level)
894 */
896 EXPR_KIND_VALUES, true);
897
898 /*
899 * All the sublists must be the same length, *after*
900 * transformation (which might expand '*' into multiple items).
901 * The VALUES RTE can't handle anything different.
902 */
903 if (sublist_length < 0)
904 {
905 /* Remember post-transformation length of first sublist */
907 }
909 {
912 errmsg("VALUES lists must all be the same length"),
913 parser_errposition(pstate,
914 exprLocation((Node *) sublist))));
915 }
916
917 /*
918 * Prepare row for assignment to target table. We process any
919 * indirection on the target column specs normally but then strip
920 * off the resulting field/array assignment nodes, since we don't
921 * want the parsed statement to contain copies of those in each
922 * VALUES row. (It's annoying to have to transform the
923 * indirection specs over and over like this, but avoiding it
924 * would take some really messy refactoring of
925 * transformAssignmentIndirection.)
926 */
928 stmt->cols,
930 true);
931
932 /*
933 * We must assign collations now because assign_query_collations
934 * doesn't process rangetable entries. We just assign all the
935 * collations independently in each row, and don't worry about
936 * whether they are consistent vertically. The outer INSERT query
937 * isn't going to care about the collations of the VALUES columns,
938 * so it's not worth the effort to identify a common collation for
939 * each one here. (But note this does have one user-visible
940 * consequence: INSERT ... VALUES won't complain about conflicting
941 * explicit COLLATEs in a column, whereas the same VALUES
942 * construct in another context would complain.)
943 */
945
947 }
948
949 /*
950 * Construct column type/typmod/collation lists for the VALUES RTE.
951 * Every expression in each column has been coerced to the type/typmod
952 * of the corresponding target column or subfield, so it's sufficient
953 * to look at the exprType/exprTypmod of the first row. We don't care
954 * about the collation labeling, so just fill in InvalidOid for that.
955 */
956 foreach(lc, (List *) linitial(exprsLists))
957 {
958 Node *val = (Node *) lfirst(lc);
959
963 }
964
965 /*
966 * Ordinarily there can't be any current-level Vars in the expression
967 * lists, because the namespace was empty ... but if we're inside
968 * CREATE RULE, then NEW/OLD references might appear. In that case we
969 * have to mark the VALUES RTE as LATERAL.
970 */
971 if (list_length(pstate->p_rtable) != 1 &&
973 lateral = true;
974
975 /*
976 * Generate the VALUES RTE
977 */
980 NULL, lateral, true);
981 addNSItemToQuery(pstate, nsitem, true, false, false);
982
983 /*
984 * Generate list of Vars referencing the RTE
985 */
986 exprList = expandNSItemVars(pstate, nsitem, 0, -1, NULL);
987
988 /*
989 * Re-apply any indirection on the target column specs to the Vars
990 */
992 stmt->cols,
994 false);
995 }
996 else
997 {
998 /*
999 * Process INSERT ... VALUES with a single VALUES sublist. We treat
1000 * this case separately for efficiency. The sublist is just computed
1001 * directly as the Query's targetlist, with no VALUES RTE. So it
1002 * works just like a SELECT without any FROM.
1003 */
1004 List *valuesLists = selectStmt->valuesLists;
1005
1006 Assert(list_length(valuesLists) == 1);
1007 Assert(selectStmt->intoClause == NULL);
1008
1009 /*
1010 * Do basic expression transformation (same as a ROW() expr, but allow
1011 * SetToDefault at top level)
1012 */
1014 (List *) linitial(valuesLists),
1016 true);
1017
1018 /* Prepare row for assignment to target table */
1020 stmt->cols,
1022 false);
1023 }
1024
1025 /*
1026 * Generate query's target list using the computed list of expressions.
1027 * Also, mark all the target columns as needing insert permissions.
1028 */
1030 qry->targetList = NIL;
1033 {
1034 Expr *expr = (Expr *) lfirst(lc);
1038
1039 tle = makeTargetEntry(expr,
1040 attr_num,
1041 col->name,
1042 false);
1043 qry->targetList = lappend(qry->targetList, tle);
1044
1045 perminfo->insertedCols = bms_add_member(perminfo->insertedCols,
1047 }
1048
1049 /*
1050 * If we have any clauses yet to process, set the query namespace to
1051 * contain only the target relation, removing any entries added in a
1052 * sub-SELECT or VALUES list.
1053 */
1054 if (stmt->onConflictClause || stmt->returningClause)
1055 {
1056 pstate->p_namespace = NIL;
1057 addNSItemToQuery(pstate, pstate->p_target_nsitem,
1058 false, true, true);
1059 }
1060
1061 /* ON CONFLICT DO SELECT requires a RETURNING clause */
1062 if (stmt->onConflictClause &&
1063 stmt->onConflictClause->action == ONCONFLICT_SELECT &&
1064 !stmt->returningClause)
1065 ereport(ERROR,
1067 errmsg("ON CONFLICT DO SELECT requires a RETURNING clause"),
1068 parser_errposition(pstate, stmt->onConflictClause->location));
1069
1070 /* Process ON CONFLICT, if any. */
1071 if (stmt->onConflictClause)
1072 qry->onConflict = transformOnConflictClause(pstate,
1073 stmt->onConflictClause);
1074
1075 /* Process RETURNING, if any. */
1076 if (stmt->returningClause)
1077 transformReturningClause(pstate, qry, stmt->returningClause,
1079
1080 /* done building the range table and jointree */
1081 qry->rtable = pstate->p_rtable;
1082 qry->rteperminfos = pstate->p_rteperminfos;
1083 qry->jointree = makeFromExpr(pstate->p_joinlist, NULL);
1084
1085 qry->hasTargetSRFs = pstate->p_hasTargetSRFs;
1086 qry->hasSubLinks = pstate->p_hasSubLinks;
1087
1088 assign_query_collations(pstate, qry);
1089
1090 return qry;
1091}
long val
Definition informix.c:689
Var * makeVarFromTargetEntry(int varno, TargetEntry *tle)
Definition makefuncs.c:107
int32 exprTypmod(const Node *expr)
Definition nodeFuncs.c:304
@ ONCONFLICT_SELECT
Definition nodes.h:429
@ ONCONFLICT_UPDATE
Definition nodes.h:428
@ CMD_INSERT
Definition nodes.h:275
void assign_list_collations(ParseState *pstate, List *exprs)
@ EXPR_KIND_VALUES
Definition parse_node.h:67
@ EXPR_KIND_VALUES_SINGLE
Definition parse_node.h:68
List * expandNSItemVars(ParseState *pstate, ParseNamespaceItem *nsitem, int sublevels_up, int location, List **colnames)
ParseNamespaceItem * addRangeTableEntryForSubquery(ParseState *pstate, Query *subquery, Alias *alias, bool lateral, bool inFromCl)
ParseNamespaceItem * addRangeTableEntryForValues(ParseState *pstate, List *exprs, List *coltypes, List *coltypmods, List *colcollations, Alias *alias, bool lateral, bool inFromCl)
List * transformExpressionList(ParseState *pstate, List *exprlist, ParseExprKind exprKind, bool allowDefault)
List * checkInsertTargets(ParseState *pstate, List *cols, List **attrnos)
uint64 AclMode
Definition parsenodes.h:74
#define ACL_INSERT
Definition parsenodes.h:76
#define ACL_UPDATE
Definition parsenodes.h:78
static OnConflictExpr * transformOnConflictClause(ParseState *pstate, OnConflictClause *onConflictClause)
Definition analyze.c:1214
List * transformInsertRow(ParseState *pstate, List *exprlist, List *stmtcols, List *icolumns, List *attrnos, bool strip_indirection)
Definition analyze.c:1104
List * p_ctenamespace
Definition parse_node.h:225
List * p_namespace
Definition parse_node.h:222
bool contain_vars_of_level(Node *node, int levelsup)
Definition var.c:444

References ACL_INSERT, ACL_UPDATE, addNSItemToQuery(), addRangeTableEntryForSubquery(), addRangeTableEntryForValues(), Assert, assign_list_collations(), assign_query_collations(), bms_add_member(), checkInsertTargets(), CMD_INSERT, CMD_SELECT, Query::commandType, contain_vars_of_level(), Query::cteList, elog, ereport, errcode(), errmsg, ERROR, expandNSItemVars(), EXPR_KIND_RETURNING, EXPR_KIND_VALUES, EXPR_KIND_VALUES_SINGLE, exprLocation(), exprType(), exprTypmod(), fb(), FirstLowInvalidHeapAttributeNumber, forthree, free_parsestate(), SelectStmt::intoClause, InvalidOid, IsA, Query::jointree, lappend(), lappend_int(), lappend_oid(), LCS_NONE, lfirst, lfirst_int, lfirst_node, SelectStmt::limitCount, SelectStmt::limitOffset, linitial, list_length(), Var::location, SelectStmt::lockingClause, make_parsestate(), makeFromExpr(), makeNode, makeTargetEntry(), makeVarFromTargetEntry(), NIL, Query::onConflict, ONCONFLICT_SELECT, ONCONFLICT_UPDATE, ParseState::p_ctenamespace, ParseState::p_hasModifyingCTE, ParseState::p_hasSubLinks, ParseState::p_hasTargetSRFs, ParseState::p_joinlist, ParseState::p_namespace, ParseNamespaceItem::p_perminfo, ParseState::p_rtable, ParseState::p_rteperminfos, ParseState::p_target_nsitem, parser_errposition(), Query::rtable, setTargetTable(), SelectStmt::sortClause, stmt, Query::targetList, transformExpressionList(), transformInsertRow(), transformOnConflictClause(), transformReturningClause(), transformStmt(), transformWithClause(), val, SelectStmt::valuesLists, and SelectStmt::withClause.

Referenced by transformStmt().

◆ transformLockingClause()

static void transformLockingClause ( ParseState pstate,
Query qry,
LockingClause lc,
bool  pushedDown 
)
static

Definition at line 3806 of file analyze.c.

3808{
3809 List *lockedRels = lc->lockedRels;
3810 ListCell *l;
3811 ListCell *rt;
3812 Index i;
3814
3815 CheckSelectLocking(qry, lc->strength);
3816
3817 /* make a clause we can pass down to subqueries to select all rels */
3819 allrels->lockedRels = NIL; /* indicates all rels */
3820 allrels->strength = lc->strength;
3821 allrels->waitPolicy = lc->waitPolicy;
3822
3823 if (lockedRels == NIL)
3824 {
3825 /*
3826 * Lock all regular tables used in query and its subqueries. We
3827 * examine inFromCl to exclude auto-added RTEs, particularly NEW/OLD
3828 * in rules. This is a bit of an abuse of a mostly-obsolete flag, but
3829 * it's convenient. We can't rely on the namespace mechanism that has
3830 * largely replaced inFromCl, since for example we need to lock
3831 * base-relation RTEs even if they are masked by upper joins.
3832 */
3833 i = 0;
3834 foreach(rt, qry->rtable)
3835 {
3837
3838 ++i;
3839 if (!rte->inFromCl)
3840 continue;
3841 switch (rte->rtekind)
3842 {
3843 case RTE_RELATION:
3844 {
3846
3847 applyLockingClause(qry, i,
3848 lc->strength,
3849 lc->waitPolicy,
3850 pushedDown);
3851 perminfo = getRTEPermissionInfo(qry->rteperminfos, rte);
3852 perminfo->requiredPerms |= ACL_SELECT_FOR_UPDATE;
3853 }
3854 break;
3855 case RTE_SUBQUERY:
3856 applyLockingClause(qry, i, lc->strength, lc->waitPolicy,
3857 pushedDown);
3858
3859 /*
3860 * FOR UPDATE/SHARE of subquery is propagated to all of
3861 * subquery's rels, too. We could do this later (based on
3862 * the marking of the subquery RTE) but it is convenient
3863 * to have local knowledge in each query level about which
3864 * rels need to be opened with RowShareLock.
3865 */
3866 transformLockingClause(pstate, rte->subquery,
3867 allrels, true);
3868 break;
3869 default:
3870 /* ignore JOIN, SPECIAL, FUNCTION, VALUES, CTE RTEs */
3871 break;
3872 }
3873 }
3874 }
3875 else
3876 {
3877 /*
3878 * Lock just the named tables. As above, we allow locking any base
3879 * relation regardless of alias-visibility rules, so we need to
3880 * examine inFromCl to exclude OLD/NEW.
3881 */
3882 foreach(l, lockedRels)
3883 {
3884 RangeVar *thisrel = (RangeVar *) lfirst(l);
3885
3886 /* For simplicity we insist on unqualified alias names here */
3887 if (thisrel->catalogname || thisrel->schemaname)
3888 ereport(ERROR,
3890 /*------
3891 translator: %s is a SQL row locking clause such as FOR UPDATE */
3892 errmsg("%s must specify unqualified relation names",
3893 LCS_asString(lc->strength)),
3894 parser_errposition(pstate, thisrel->location)));
3895
3896 i = 0;
3897 foreach(rt, qry->rtable)
3898 {
3900 char *rtename = rte->eref->aliasname;
3901
3902 ++i;
3903 if (!rte->inFromCl)
3904 continue;
3905
3906 /*
3907 * A join RTE without an alias is not visible as a relation
3908 * name and needs to be skipped (otherwise it might hide a
3909 * base relation with the same name), except if it has a USING
3910 * alias, which *is* visible.
3911 *
3912 * Subquery and values RTEs without aliases are never visible
3913 * as relation names and must always be skipped.
3914 */
3915 if (rte->alias == NULL)
3916 {
3917 if (rte->rtekind == RTE_JOIN)
3918 {
3919 if (rte->join_using_alias == NULL)
3920 continue;
3921 rtename = rte->join_using_alias->aliasname;
3922 }
3923 else if (rte->rtekind == RTE_SUBQUERY ||
3924 rte->rtekind == RTE_VALUES)
3925 continue;
3926 }
3927
3928 if (strcmp(rtename, thisrel->relname) == 0)
3929 {
3930 switch (rte->rtekind)
3931 {
3932 case RTE_RELATION:
3933 {
3935
3936 applyLockingClause(qry, i,
3937 lc->strength,
3938 lc->waitPolicy,
3939 pushedDown);
3940 perminfo = getRTEPermissionInfo(qry->rteperminfos, rte);
3941 perminfo->requiredPerms |= ACL_SELECT_FOR_UPDATE;
3942 }
3943 break;
3944 case RTE_SUBQUERY:
3945 applyLockingClause(qry, i, lc->strength,
3946 lc->waitPolicy, pushedDown);
3947 /* see comment above */
3948 transformLockingClause(pstate, rte->subquery,
3949 allrels, true);
3950 break;
3951 case RTE_JOIN:
3952 ereport(ERROR,
3954 /*------
3955 translator: %s is a SQL row locking clause such as FOR UPDATE */
3956 errmsg("%s cannot be applied to a join",
3957 LCS_asString(lc->strength)),
3958 parser_errposition(pstate, thisrel->location)));
3959 break;
3960 case RTE_FUNCTION:
3961 ereport(ERROR,
3963 /*------
3964 translator: %s is a SQL row locking clause such as FOR UPDATE */
3965 errmsg("%s cannot be applied to a function",
3966 LCS_asString(lc->strength)),
3967 parser_errposition(pstate, thisrel->location)));
3968 break;
3969 case RTE_TABLEFUNC:
3970 ereport(ERROR,
3972 /*------
3973 translator: %s is a SQL row locking clause such as FOR UPDATE */
3974 errmsg("%s cannot be applied to a table function",
3975 LCS_asString(lc->strength)),
3976 parser_errposition(pstate, thisrel->location)));
3977 break;
3978 case RTE_VALUES:
3979 ereport(ERROR,
3981 /*------
3982 translator: %s is a SQL row locking clause such as FOR UPDATE */
3983 errmsg("%s cannot be applied to VALUES",
3984 LCS_asString(lc->strength)),
3985 parser_errposition(pstate, thisrel->location)));
3986 break;
3987 case RTE_CTE:
3988 ereport(ERROR,
3990 /*------
3991 translator: %s is a SQL row locking clause such as FOR UPDATE */
3992 errmsg("%s cannot be applied to a WITH query",
3993 LCS_asString(lc->strength)),
3994 parser_errposition(pstate, thisrel->location)));
3995 break;
3997 ereport(ERROR,
3999 /*------
4000 translator: %s is a SQL row locking clause such as FOR UPDATE */
4001 errmsg("%s cannot be applied to a named tuplestore",
4002 LCS_asString(lc->strength)),
4003 parser_errposition(pstate, thisrel->location)));
4004 break;
4005
4006 /* Shouldn't be possible to see RTE_RESULT here */
4007
4008 default:
4009 elog(ERROR, "unrecognized RTE type: %d",
4010 (int) rte->rtekind);
4011 break;
4012 }
4013 break; /* out of foreach loop */
4014 }
4015 }
4016 if (rt == NULL)
4017 ereport(ERROR,
4019 /*------
4020 translator: %s is a SQL row locking clause such as FOR UPDATE */
4021 errmsg("relation \"%s\" in %s clause not found in FROM clause",
4022 thisrel->relname,
4023 LCS_asString(lc->strength)),
4024 parser_errposition(pstate, thisrel->location)));
4025 }
4026 }
4027}
unsigned int Index
Definition c.h:754
RTEPermissionInfo * getRTEPermissionInfo(List *rteperminfos, RangeTblEntry *rte)
@ RTE_JOIN
@ RTE_CTE
@ RTE_NAMEDTUPLESTORE
@ RTE_VALUES
@ RTE_FUNCTION
@ RTE_TABLEFUNC
@ RTE_RELATION
#define ACL_SELECT_FOR_UPDATE
Definition parsenodes.h:94
static void transformLockingClause(ParseState *pstate, Query *qry, LockingClause *lc, bool pushedDown)
Definition analyze.c:3806
void CheckSelectLocking(Query *qry, LockClauseStrength strength)
Definition analyze.c:3742
void applyLockingClause(Query *qry, Index rtindex, LockClauseStrength strength, LockWaitPolicy waitPolicy, bool pushedDown)
Definition analyze.c:4033
#define ERRCODE_UNDEFINED_TABLE
Definition pgbench.c:79

References ACL_SELECT_FOR_UPDATE, applyLockingClause(), CheckSelectLocking(), elog, ereport, errcode(), ERRCODE_UNDEFINED_TABLE, errmsg, ERROR, fb(), getRTEPermissionInfo(), i, LCS_asString(), lfirst, makeNode, NIL, parser_errposition(), Query::rtable, RTE_CTE, RTE_FUNCTION, RTE_JOIN, RTE_NAMEDTUPLESTORE, RTE_RELATION, RTE_SUBQUERY, RTE_TABLEFUNC, RTE_VALUES, and transformLockingClause().

Referenced by transformLockingClause(), transformSelectStmt(), and transformSetOperationStmt().

◆ transformOnConflictClause()

static OnConflictExpr * transformOnConflictClause ( ParseState pstate,
OnConflictClause onConflictClause 
)
static

Definition at line 1214 of file analyze.c.

1216{
1218 List *arbiterElems;
1219 Node *arbiterWhere;
1221 List *onConflictSet = NIL;
1222 Node *onConflictWhere = NULL;
1223 int exclRelIndex = 0;
1224 List *exclRelTlist = NIL;
1226
1227 /*
1228 * If this is ON CONFLICT DO SELECT/UPDATE, first create the range table
1229 * entry for the EXCLUDED pseudo relation, so that that will be present
1230 * while processing arbiter expressions. (You can't actually reference it
1231 * from there, but this provides a useful error message if you try.)
1232 */
1233 if (onConflictClause->action == ONCONFLICT_UPDATE ||
1234 onConflictClause->action == ONCONFLICT_SELECT)
1235 {
1238
1240 targetrel,
1242 makeAlias("excluded", NIL),
1243 false, false);
1244 exclRte = exclNSItem->p_rte;
1245 exclRelIndex = exclNSItem->p_rtindex;
1246
1247 /*
1248 * relkind is set to composite to signal that we're not dealing with
1249 * an actual relation, and no permission checks are required on it.
1250 * (We'll check the actual target relation, instead.)
1251 */
1253
1254 /* Create EXCLUDED rel's targetlist for use by EXPLAIN */
1256 exclRelIndex);
1257 }
1258
1259 /* Process the arbiter clause, ON CONFLICT ON (...) */
1260 transformOnConflictArbiter(pstate, onConflictClause, &arbiterElems,
1261 &arbiterWhere, &arbiterConstraint);
1262
1263 /* Process DO SELECT/UPDATE */
1264 if (onConflictClause->action == ONCONFLICT_UPDATE ||
1265 onConflictClause->action == ONCONFLICT_SELECT)
1266 {
1267 /*
1268 * Add the EXCLUDED pseudo relation to the query namespace, making it
1269 * available in SET and WHERE subexpressions.
1270 */
1271 addNSItemToQuery(pstate, exclNSItem, false, true, true);
1272
1273 /* Process the UPDATE SET clause */
1274 if (onConflictClause->action == ONCONFLICT_UPDATE)
1275 onConflictSet =
1276 transformUpdateTargetList(pstate, onConflictClause->targetList, NULL);
1277
1278 /* Process the SELECT/UPDATE WHERE clause */
1279 onConflictWhere = transformWhereClause(pstate,
1280 onConflictClause->whereClause,
1281 EXPR_KIND_WHERE, "WHERE");
1282
1283 /*
1284 * Remove the EXCLUDED pseudo relation from the query namespace, since
1285 * it's not supposed to be available in RETURNING. (Maybe someday we
1286 * could allow that, and drop this step.)
1287 */
1289 pstate->p_namespace = list_delete_last(pstate->p_namespace);
1290 }
1291
1292 /* Finally, build ON CONFLICT DO [NOTHING | SELECT | UPDATE] expression */
1294
1295 result->action = onConflictClause->action;
1296 result->arbiterElems = arbiterElems;
1297 result->arbiterWhere = arbiterWhere;
1298 result->constraint = arbiterConstraint;
1299 result->lockStrength = onConflictClause->lockStrength;
1300 result->onConflictSet = onConflictSet;
1301 result->onConflictWhere = onConflictWhere;
1302 result->exclRelIndex = exclRelIndex;
1303 result->exclRelTlist = exclRelTlist;
1304
1305 return result;
1306}
List * list_delete_last(List *list)
Definition list.c:957
#define RowExclusiveLock
Definition lockdefs.h:38
void transformOnConflictArbiter(ParseState *pstate, OnConflictClause *onConflictClause, List **arbiterExpr, Node **arbiterWhere, Oid *constraint)
ParseNamespaceItem * addRangeTableEntryForRelation(ParseState *pstate, Relation rel, LOCKMODE lockmode, Alias *alias, bool inh, bool inFromCl)
List * transformUpdateTargetList(ParseState *pstate, List *origTlist, ForPortionOfExpr *forPortionOf)
Definition analyze.c:2936
List * BuildOnConflictExcludedTargetlist(Relation targetrel, Index exclRelIndex)
Definition analyze.c:1624
#define llast(l)
Definition pg_list.h:198
OnConflictAction action
LockClauseStrength lockStrength

References OnConflictClause::action, addNSItemToQuery(), addRangeTableEntryForRelation(), Assert, BuildOnConflictExcludedTargetlist(), EXPR_KIND_WHERE, fb(), list_delete_last(), llast, OnConflictClause::lockStrength, makeAlias(), makeNode, NIL, ONCONFLICT_SELECT, ONCONFLICT_UPDATE, ParseState::p_namespace, ParseState::p_target_relation, result, RowExclusiveLock, OnConflictClause::targetList, transformOnConflictArbiter(), transformUpdateTargetList(), transformWhereClause(), and OnConflictClause::whereClause.

Referenced by transformInsertStmt().

◆ transformOptionalSelectInto()

static Query * transformOptionalSelectInto ( ParseState pstate,
Node parseTree 
)
static

Definition at line 296 of file analyze.c.

297{
299 {
301
302 /* If it's a set-operation tree, drill down to leftmost SelectStmt */
303 while (stmt && stmt->op != SETOP_NONE)
304 stmt = stmt->larg;
305 Assert(stmt && IsA(stmt, SelectStmt) && stmt->larg == NULL);
306
307 if (stmt->intoClause)
308 {
310
311 ctas->query = parseTree;
312 ctas->into = stmt->intoClause;
313 ctas->objtype = OBJECT_TABLE;
314 ctas->is_select_into = true;
315
316 /*
317 * Remove the intoClause from the SelectStmt. This makes it safe
318 * for transformSelectStmt to complain if it finds intoClause set
319 * (implying that the INTO appeared in a disallowed place).
320 */
321 stmt->intoClause = NULL;
322
323 parseTree = (Node *) ctas;
324 }
325 }
326
327 return transformStmt(pstate, parseTree);
328}
@ SETOP_NONE
@ OBJECT_TABLE
struct SelectStmt * larg

References Assert, fb(), IsA, SelectStmt::larg, makeNode, OBJECT_TABLE, SETOP_NONE, stmt, and transformStmt().

Referenced by transformExplainStmt(), and transformTopLevelStmt().

◆ transformPLAssignStmt()

static Query * transformPLAssignStmt ( ParseState pstate,
PLAssignStmt stmt 
)
static

Definition at line 3186 of file analyze.c.

3187{
3188 Query *qry;
3190 List *indirection = stmt->indirection;
3191 int nnames = stmt->nnames;
3192 Node *target;
3195
3196 /*
3197 * First, construct a ColumnRef for the target variable. If the target
3198 * has more than one dotted name, we have to pull the extra names out of
3199 * the indirection list.
3200 */
3201 cref->fields = list_make1(makeString(stmt->name));
3202 cref->location = stmt->location;
3203 if (nnames > 1)
3204 {
3205 /* avoid munging the raw parsetree */
3206 indirection = list_copy(indirection);
3207 while (--nnames > 0 && indirection != NIL)
3208 {
3209 Node *ind = (Node *) linitial(indirection);
3210
3211 if (!IsA(ind, String))
3212 elog(ERROR, "invalid name count in PLAssignStmt");
3213 cref->fields = lappend(cref->fields, ind);
3214 indirection = list_delete_first(indirection);
3215 }
3216 }
3217
3218 /*
3219 * Transform the target reference. Typically we will get back a Param
3220 * node, but there's no reason to be too picky about its type. (Note that
3221 * we must do this before calling transformSelectStmt. It's tempting to
3222 * do it inside transformPLAssignStmtTarget, but we need to do it before
3223 * adding any FROM tables to the pstate's namespace, else we might wrongly
3224 * resolve the target as a table column.)
3225 */
3226 target = transformExpr(pstate, (Node *) cref,
3228
3229 /* Set up passthrough data for transformPLAssignStmtTarget */
3230 passthru.stmt = stmt;
3231 passthru.target = target;
3232 passthru.indirection = indirection;
3233
3234 /*
3235 * To avoid duplicating a lot of code, we use transformSelectStmt to do
3236 * almost all of the work. However, we need to do additional processing
3237 * on the SELECT's targetlist after it's been transformed, but before
3238 * possible addition of targetlist items for ORDER BY or GROUP BY.
3239 * transformSelectStmt knows it should call transformPLAssignStmtTarget if
3240 * it's passed a passthru argument.
3241 *
3242 * Also, disable resolution of unknown-type tlist items; PL/pgSQL wants to
3243 * deal with that itself.
3244 */
3246 pstate->p_resolve_unknowns = false;
3247 qry = transformSelectStmt(pstate, stmt->val, &passthru);
3249
3250 return qry;
3251}
List * list_delete_first(List *list)
Definition list.c:943
List * list_copy(const List *oldlist)
Definition list.c:1573
@ EXPR_KIND_UPDATE_TARGET
Definition parse_node.h:57
static Query * transformSelectStmt(ParseState *pstate, SelectStmt *stmt, SelectStmtPassthrough *passthru)
Definition analyze.c:1743
#define list_make1(x1)
Definition pg_list.h:244
Definition value.h:64
String * makeString(char *str)
Definition value.c:63

References elog, ERROR, EXPR_KIND_UPDATE_TARGET, fb(), IsA, lappend(), linitial, list_copy(), list_delete_first(), list_make1, makeNode, makeString(), NIL, ParseState::p_resolve_unknowns, stmt, transformExpr(), and transformSelectStmt().

Referenced by transformStmt().

◆ transformPLAssignStmtTarget()

static List * transformPLAssignStmtTarget ( ParseState pstate,
List tlist,
SelectStmtPassthrough passthru 
)
static

Definition at line 3261 of file analyze.c.

3263{
3264 PLAssignStmt *stmt = passthru->stmt;
3265 Node *target = passthru->target;
3266 List *indirection = passthru->indirection;
3267 Oid targettype;
3268 int32 targettypmod;
3271 Oid type_id;
3272
3273 targettype = exprType(target);
3274 targettypmod = exprTypmod(target);
3276
3277 /* we should have exactly one targetlist item */
3278 if (list_length(tlist) != 1)
3279 ereport(ERROR,
3281 errmsg_plural("assignment source returned %d column",
3282 "assignment source returned %d columns",
3283 list_length(tlist),
3284 list_length(tlist))));
3285
3286 tle = linitial_node(TargetEntry, tlist);
3287
3288 /*
3289 * This next bit is similar to transformAssignedExpr; the key difference
3290 * is we use COERCION_PLPGSQL not COERCION_ASSIGNMENT.
3291 */
3292 type_id = exprType((Node *) tle->expr);
3293
3295
3296 if (indirection)
3297 {
3298 tle->expr = (Expr *)
3300 target,
3301 stmt->name,
3302 false,
3303 targettype,
3304 targettypmod,
3306 indirection,
3307 list_head(indirection),
3308 (Node *) tle->expr,
3310 exprLocation(target));
3311 }
3312 else if (targettype != type_id &&
3313 (targettype == RECORDOID || ISCOMPLEX(targettype)) &&
3314 (type_id == RECORDOID || ISCOMPLEX(type_id)))
3315 {
3316 /*
3317 * Hack: do not let coerce_to_target_type() deal with inconsistent
3318 * composite types. Just pass the expression result through as-is,
3319 * and let the PL/pgSQL executor do the conversion its way. This is
3320 * rather bogus, but it's needed for backwards compatibility.
3321 */
3322 }
3323 else
3324 {
3325 /*
3326 * For normal non-qualified target column, do type checking and
3327 * coercion.
3328 */
3329 Node *orig_expr = (Node *) tle->expr;
3330
3331 tle->expr = (Expr *)
3332 coerce_to_target_type(pstate,
3333 orig_expr, type_id,
3334 targettype, targettypmod,
3337 -1);
3338 /* With COERCION_PLPGSQL, this error is probably unreachable */
3339 if (tle->expr == NULL)
3340 ereport(ERROR,
3342 errmsg("variable \"%s\" is of type %s"
3343 " but expression is of type %s",
3344 stmt->name,
3345 format_type_be(targettype),
3346 format_type_be(type_id)),
3347 errhint("You will need to rewrite or cast the expression."),
3349 }
3350
3351 pstate->p_expr_kind = EXPR_KIND_NONE;
3352
3353 return list_make1(tle);
3354}
int int int errmsg_plural(const char *fmt_singular, const char *fmt_plural, unsigned long n,...) pg_attribute_printf(1
Oid exprCollation(const Node *expr)
Definition nodeFuncs.c:826
Node * coerce_to_target_type(ParseState *pstate, Node *expr, Oid exprtype, Oid targettype, int32 targettypmod, CoercionContext ccontext, CoercionForm cformat, int location)
@ EXPR_KIND_NONE
Definition parse_node.h:40
Node * transformAssignmentIndirection(ParseState *pstate, Node *basenode, const char *targetName, bool targetIsSubscripting, Oid targetTypeId, int32 targetTypMod, Oid targetCollation, List *indirection, ListCell *indirection_cell, Node *rhs, CoercionContext ccontext, int location)
#define ISCOMPLEX(typeid)
Definition parse_type.h:59
#define linitial_node(type, l)
Definition pg_list.h:181
static ListCell * list_head(const List *l)
Definition pg_list.h:128
@ COERCION_PLPGSQL
Definition primnodes.h:739
ParseExprKind p_expr_kind
Definition parse_node.h:232

References COERCE_IMPLICIT_CAST, coerce_to_target_type(), COERCION_PLPGSQL, ereport, errcode(), errhint(), errmsg, errmsg_plural(), ERROR, EXPR_KIND_NONE, EXPR_KIND_UPDATE_TARGET, exprCollation(), exprLocation(), exprType(), exprTypmod(), fb(), format_type_be(), ISCOMPLEX, linitial_node, list_head(), list_length(), list_make1, ParseState::p_expr_kind, parser_errposition(), stmt, and transformAssignmentIndirection().

Referenced by transformSelectStmt().

◆ transformReturningClause()

void transformReturningClause ( ParseState pstate,
Query qry,
ReturningClause returningClause,
ParseExprKind  exprKind 
)

Definition at line 3064 of file analyze.c.

3067{
3068 int save_nslen = list_length(pstate->p_namespace);
3069 int save_next_resno;
3070
3071 if (returningClause == NULL)
3072 return; /* nothing to do */
3073
3074 /*
3075 * Scan RETURNING WITH(...) options for OLD/NEW alias names. Complain if
3076 * there is any conflict with existing relations.
3077 */
3078 foreach_node(ReturningOption, option, returningClause->options)
3079 {
3080 switch (option->option)
3081 {
3083 if (qry->returningOldAlias != NULL)
3084 ereport(ERROR,
3086 /* translator: %s is OLD or NEW */
3087 errmsg("%s cannot be specified multiple times", "OLD"),
3088 parser_errposition(pstate, option->location));
3089 qry->returningOldAlias = option->value;
3090 break;
3091
3093 if (qry->returningNewAlias != NULL)
3094 ereport(ERROR,
3096 /* translator: %s is OLD or NEW */
3097 errmsg("%s cannot be specified multiple times", "NEW"),
3098 parser_errposition(pstate, option->location));
3099 qry->returningNewAlias = option->value;
3100 break;
3101
3102 default:
3103 elog(ERROR, "unrecognized returning option: %d", option->option);
3104 }
3105
3106 if (refnameNamespaceItem(pstate, NULL, option->value, -1, NULL) != NULL)
3107 ereport(ERROR,
3109 errmsg("table name \"%s\" specified more than once",
3110 option->value),
3111 parser_errposition(pstate, option->location));
3112
3113 addNSItemForReturning(pstate, option->value,
3114 option->option == RETURNING_OPTION_OLD ?
3116 }
3117
3118 /*
3119 * If OLD/NEW alias names weren't explicitly specified, use "old"/"new"
3120 * unless masked by existing relations.
3121 */
3122 if (qry->returningOldAlias == NULL &&
3123 refnameNamespaceItem(pstate, NULL, "old", -1, NULL) == NULL)
3124 {
3125 qry->returningOldAlias = "old";
3127 }
3128 if (qry->returningNewAlias == NULL &&
3129 refnameNamespaceItem(pstate, NULL, "new", -1, NULL) == NULL)
3130 {
3131 qry->returningNewAlias = "new";
3133 }
3134
3135 /*
3136 * We need to assign resnos starting at one in the RETURNING list. Save
3137 * and restore the main tlist's value of p_next_resno, just in case
3138 * someone looks at it later (probably won't happen).
3139 */
3140 save_next_resno = pstate->p_next_resno;
3141 pstate->p_next_resno = 1;
3142
3143 /* transform RETURNING expressions identically to a SELECT targetlist */
3144 qry->returningList = transformTargetList(pstate,
3145 returningClause->exprs,
3146 exprKind);
3147
3148 /*
3149 * Complain if the nonempty tlist expanded to nothing (which is possible
3150 * if it contains only a star-expansion of a zero-column table). If we
3151 * allow this, the parsed Query will look like it didn't have RETURNING,
3152 * with results that would probably surprise the user.
3153 */
3154 if (qry->returningList == NIL)
3155 ereport(ERROR,
3157 errmsg("RETURNING must have at least one column"),
3158 parser_errposition(pstate,
3159 exprLocation(linitial(returningClause->exprs)))));
3160
3161 /* mark column origins */
3163
3164 /* resolve any still-unresolved output columns as being type text */
3165 if (pstate->p_resolve_unknowns)
3167
3168 /* restore state */
3169 pstate->p_namespace = list_truncate(pstate->p_namespace, save_nslen);
3170 pstate->p_next_resno = save_next_resno;
3171}
List * list_truncate(List *list, int new_size)
Definition list.c:631
ParseNamespaceItem * refnameNamespaceItem(ParseState *pstate, const char *schemaname, const char *refname, int location, int *sublevels_up)
List * transformTargetList(ParseState *pstate, List *targetlist, ParseExprKind exprKind)
void resolveTargetListUnknowns(ParseState *pstate, List *targetlist)
void markTargetListOrigins(ParseState *pstate, List *targetlist)
@ RETURNING_OPTION_NEW
@ RETURNING_OPTION_OLD
static void addNSItemForReturning(ParseState *pstate, const char *aliasname, VarReturningType returning_type)
Definition analyze.c:3025
#define foreach_node(type, var, lst)
Definition pg_list.h:528
@ VAR_RETURNING_OLD
Definition primnodes.h:258
@ VAR_RETURNING_NEW
Definition primnodes.h:259
int p_next_resno
Definition parse_node.h:233
List * returningList
Definition parsenodes.h:219

References addNSItemForReturning(), elog, ereport, errcode(), errmsg, ERROR, exprLocation(), ReturningClause::exprs, fb(), foreach_node, linitial, list_length(), list_truncate(), markTargetListOrigins(), NIL, ReturningClause::options, ParseState::p_namespace, ParseState::p_next_resno, ParseState::p_resolve_unknowns, parser_errposition(), refnameNamespaceItem(), resolveTargetListUnknowns(), RETURNING_OPTION_NEW, RETURNING_OPTION_OLD, Query::returningList, transformTargetList(), VAR_RETURNING_NEW, and VAR_RETURNING_OLD.

Referenced by transformDeleteStmt(), transformInsertStmt(), transformMergeStmt(), and transformUpdateStmt().

◆ transformReturnStmt()

static Query * transformReturnStmt ( ParseState pstate,
ReturnStmt stmt 
)
static

Definition at line 2823 of file analyze.c.

2824{
2825 Query *qry = makeNode(Query);
2826
2827 qry->commandType = CMD_SELECT;
2828 qry->isReturn = true;
2829
2831 1, NULL, false));
2832
2833 if (pstate->p_resolve_unknowns)
2835 qry->rtable = pstate->p_rtable;
2836 qry->rteperminfos = pstate->p_rteperminfos;
2837 qry->jointree = makeFromExpr(pstate->p_joinlist, NULL);
2838 qry->hasSubLinks = pstate->p_hasSubLinks;
2839 qry->hasWindowFuncs = pstate->p_hasWindowFuncs;
2840 qry->hasTargetSRFs = pstate->p_hasTargetSRFs;
2841 qry->hasAggs = pstate->p_hasAggs;
2842
2843 assign_query_collations(pstate, qry);
2844
2845 return qry;
2846}
@ EXPR_KIND_SELECT_TARGET
Definition parse_node.h:54
List * targetList
Definition parsenodes.h:203

References assign_query_collations(), CMD_SELECT, Query::commandType, EXPR_KIND_SELECT_TARGET, fb(), Query::jointree, list_make1, makeFromExpr(), makeNode, makeTargetEntry(), ParseState::p_hasAggs, ParseState::p_hasSubLinks, ParseState::p_hasTargetSRFs, ParseState::p_hasWindowFuncs, ParseState::p_joinlist, ParseState::p_resolve_unknowns, ParseState::p_rtable, ParseState::p_rteperminfos, resolveTargetListUnknowns(), Query::rtable, stmt, Query::targetList, and transformExpr().

Referenced by transformStmt().

◆ transformSelectStmt()

static Query * transformSelectStmt ( ParseState pstate,
SelectStmt stmt,
SelectStmtPassthrough passthru 
)
static

Definition at line 1743 of file analyze.c.

1745{
1746 Query *qry = makeNode(Query);
1747 Node *qual;
1748 ListCell *l;
1749
1750 qry->commandType = CMD_SELECT;
1751
1752 /* process the WITH clause independently of all else */
1753 if (stmt->withClause)
1754 {
1755 qry->hasRecursive = stmt->withClause->recursive;
1756 qry->cteList = transformWithClause(pstate, stmt->withClause);
1757 qry->hasModifyingCTE = pstate->p_hasModifyingCTE;
1758 }
1759
1760 /* Complain if we get called from someplace where INTO is not allowed */
1761 if (stmt->intoClause)
1762 ereport(ERROR,
1764 errmsg("SELECT ... INTO is not allowed here"),
1765 parser_errposition(pstate,
1766 exprLocation((Node *) stmt->intoClause))));
1767
1768 /* make FOR UPDATE/FOR SHARE info available to addRangeTableEntry */
1769 pstate->p_locking_clause = stmt->lockingClause;
1770
1771 /* make WINDOW info available for window functions, too */
1772 pstate->p_windowdefs = stmt->windowClause;
1773
1774 /* process the FROM clause */
1775 transformFromClause(pstate, stmt->fromClause);
1776
1777 /* transform targetlist */
1778 qry->targetList = transformTargetList(pstate, stmt->targetList,
1780
1781 /*
1782 * If we're within a PLAssignStmt, do further transformation of the
1783 * targetlist; that has to happen before we consider sorting or grouping.
1784 * Otherwise, mark column origins (which are useless in a PLAssignStmt).
1785 */
1786 if (passthru)
1788 passthru);
1789 else
1790 markTargetListOrigins(pstate, qry->targetList);
1791
1792 /* transform WHERE */
1793 qual = transformWhereClause(pstate, stmt->whereClause,
1794 EXPR_KIND_WHERE, "WHERE");
1795
1796 /* initial processing of HAVING clause is much like WHERE clause */
1797 qry->havingQual = transformWhereClause(pstate, stmt->havingClause,
1798 EXPR_KIND_HAVING, "HAVING");
1799
1800 /*
1801 * Transform sorting/grouping stuff. Do ORDER BY first because both
1802 * transformGroupClause and transformDistinctClause need the results. Note
1803 * that these functions can also change the targetList, so it's passed to
1804 * them by reference.
1805 */
1806 qry->sortClause = transformSortClause(pstate,
1807 stmt->sortClause,
1808 &qry->targetList,
1810 false /* allow SQL92 rules */ );
1811
1812 qry->groupClause = transformGroupClause(pstate,
1813 stmt->groupClause,
1814 stmt->groupByAll,
1815 &qry->groupingSets,
1816 &qry->targetList,
1817 qry->sortClause,
1819 false /* allow SQL92 rules */ );
1820 qry->groupDistinct = stmt->groupDistinct;
1821 qry->groupByAll = stmt->groupByAll;
1822
1823 if (stmt->distinctClause == NIL)
1824 {
1825 qry->distinctClause = NIL;
1826 qry->hasDistinctOn = false;
1827 }
1828 else if (linitial(stmt->distinctClause) == NULL)
1829 {
1830 /* We had SELECT DISTINCT */
1832 &qry->targetList,
1833 qry->sortClause,
1834 false);
1835 qry->hasDistinctOn = false;
1836 }
1837 else
1838 {
1839 /* We had SELECT DISTINCT ON */
1841 stmt->distinctClause,
1842 &qry->targetList,
1843 qry->sortClause);
1844 qry->hasDistinctOn = true;
1845 }
1846
1847 /* transform LIMIT */
1848 qry->limitOffset = transformLimitClause(pstate, stmt->limitOffset,
1849 EXPR_KIND_OFFSET, "OFFSET",
1850 stmt->limitOption);
1851 qry->limitCount = transformLimitClause(pstate, stmt->limitCount,
1852 EXPR_KIND_LIMIT, "LIMIT",
1853 stmt->limitOption);
1854 qry->limitOption = stmt->limitOption;
1855
1856 /* transform window clauses after we have seen all window functions */
1858 pstate->p_windowdefs,
1859 &qry->targetList);
1860
1861 /* resolve any still-unresolved output columns as being type text */
1862 if (pstate->p_resolve_unknowns)
1864
1865 qry->rtable = pstate->p_rtable;
1866 qry->rteperminfos = pstate->p_rteperminfos;
1867 qry->jointree = makeFromExpr(pstate->p_joinlist, qual);
1868
1869 qry->hasSubLinks = pstate->p_hasSubLinks;
1870 qry->hasWindowFuncs = pstate->p_hasWindowFuncs;
1871 qry->hasTargetSRFs = pstate->p_hasTargetSRFs;
1872 qry->hasAggs = pstate->p_hasAggs;
1873
1874 foreach(l, stmt->lockingClause)
1875 {
1876 transformLockingClause(pstate, qry,
1877 (LockingClause *) lfirst(l), false);
1878 }
1879
1880 assign_query_collations(pstate, qry);
1881
1882 /* this must be done after collations, for reliable comparison of exprs */
1883 if (pstate->p_hasAggs || qry->groupClause || qry->groupingSets || qry->havingQual)
1884 parseCheckAggregates(pstate, qry);
1885
1886 return qry;
1887}
List * transformGroupClause(ParseState *pstate, List *grouplist, bool groupByAll, List **groupingSets, List **targetlist, List *sortClause, ParseExprKind exprKind, bool useSQL99)
List * transformSortClause(ParseState *pstate, List *orderlist, List **targetlist, ParseExprKind exprKind, bool useSQL99)
List * transformDistinctOnClause(ParseState *pstate, List *distinctlist, List **targetlist, List *sortClause)
List * transformWindowDefinitions(ParseState *pstate, List *windowdefs, List **targetlist)
List * transformDistinctClause(ParseState *pstate, List **targetlist, List *sortClause, bool is_agg)
Node * transformLimitClause(ParseState *pstate, Node *clause, ParseExprKind exprKind, const char *constructName, LimitOption limitOption)
@ EXPR_KIND_ORDER_BY
Definition parse_node.h:61
@ EXPR_KIND_OFFSET
Definition parse_node.h:64
@ EXPR_KIND_HAVING
Definition parse_node.h:47
@ EXPR_KIND_LIMIT
Definition parse_node.h:63
@ EXPR_KIND_GROUP_BY
Definition parse_node.h:60
static List * transformPLAssignStmtTarget(ParseState *pstate, List *tlist, SelectStmtPassthrough *passthru)
Definition analyze.c:3261
List * p_windowdefs
Definition parse_node.h:231
List * p_locking_clause
Definition parse_node.h:235
bool groupDistinct
Definition parsenodes.h:222
Node * limitCount
Definition parsenodes.h:236
Node * limitOffset
Definition parsenodes.h:235
LimitOption limitOption
Definition parsenodes.h:237
List * windowClause
Definition parsenodes.h:229
bool groupByAll
Definition parsenodes.h:223
List * sortClause
Definition parsenodes.h:233

References assign_query_collations(), CMD_SELECT, Query::commandType, Query::cteList, Query::distinctClause, ereport, errcode(), errmsg, ERROR, EXPR_KIND_GROUP_BY, EXPR_KIND_HAVING, EXPR_KIND_LIMIT, EXPR_KIND_OFFSET, EXPR_KIND_ORDER_BY, EXPR_KIND_SELECT_TARGET, EXPR_KIND_WHERE, exprLocation(), fb(), Query::groupByAll, Query::groupClause, Query::groupDistinct, Query::groupingSets, Query::havingQual, Query::jointree, lfirst, Query::limitCount, Query::limitOffset, Query::limitOption, linitial, makeFromExpr(), makeNode, markTargetListOrigins(), NIL, ParseState::p_hasAggs, ParseState::p_hasModifyingCTE, ParseState::p_hasSubLinks, ParseState::p_hasTargetSRFs, ParseState::p_hasWindowFuncs, ParseState::p_joinlist, ParseState::p_locking_clause, ParseState::p_resolve_unknowns, ParseState::p_rtable, ParseState::p_rteperminfos, ParseState::p_windowdefs, parseCheckAggregates(), parser_errposition(), resolveTargetListUnknowns(), Query::rtable, Query::sortClause, stmt, Query::targetList, transformDistinctClause(), transformDistinctOnClause(), transformFromClause(), transformGroupClause(), transformLimitClause(), transformLockingClause(), transformPLAssignStmtTarget(), transformSortClause(), transformTargetList(), transformWhereClause(), transformWindowDefinitions(), transformWithClause(), and Query::windowClause.

Referenced by transformPLAssignStmt(), and transformStmt().

◆ transformSetOperationStmt()

static Query * transformSetOperationStmt ( ParseState pstate,
SelectStmt stmt 
)
static

Definition at line 2116 of file analyze.c.

2117{
2118 Query *qry = makeNode(Query);
2120 int leftmostRTI;
2123 List *sortClause;
2124 Node *limitOffset;
2125 Node *limitCount;
2126 List *lockingClause;
2127 WithClause *withClause;
2128 Node *node;
2130 *lct,
2131 *lcm,
2132 *lcc,
2133 *l;
2135 *targetnames,
2136 *sv_namespace;
2137 int sv_rtable_length;
2140 int sortcolindex;
2141 int tllen;
2142
2143 qry->commandType = CMD_SELECT;
2144
2145 /*
2146 * Find leftmost leaf SelectStmt. We currently only need to do this in
2147 * order to deliver a suitable error message if there's an INTO clause
2148 * there, implying the set-op tree is in a context that doesn't allow
2149 * INTO. (transformSetOperationTree would throw error anyway, but it
2150 * seems worth the trouble to throw a different error for non-leftmost
2151 * INTO, so we produce that error in transformSetOperationTree.)
2152 */
2153 leftmostSelect = stmt->larg;
2154 while (leftmostSelect && leftmostSelect->op != SETOP_NONE)
2157 leftmostSelect->larg == NULL);
2158 if (leftmostSelect->intoClause)
2159 ereport(ERROR,
2161 errmsg("SELECT ... INTO is not allowed here"),
2162 parser_errposition(pstate,
2163 exprLocation((Node *) leftmostSelect->intoClause))));
2164
2165 /*
2166 * We need to extract ORDER BY and other top-level clauses here and not
2167 * let transformSetOperationTree() see them --- else it'll just recurse
2168 * right back here!
2169 */
2170 sortClause = stmt->sortClause;
2171 limitOffset = stmt->limitOffset;
2172 limitCount = stmt->limitCount;
2173 lockingClause = stmt->lockingClause;
2174 withClause = stmt->withClause;
2175
2176 stmt->sortClause = NIL;
2177 stmt->limitOffset = NULL;
2178 stmt->limitCount = NULL;
2179 stmt->lockingClause = NIL;
2180 stmt->withClause = NULL;
2181
2182 /* We don't support FOR UPDATE/SHARE with set ops at the moment. */
2183 if (lockingClause)
2184 ereport(ERROR,
2186 /*------
2187 translator: %s is a SQL row locking clause such as FOR UPDATE */
2188 errmsg("%s is not allowed with UNION/INTERSECT/EXCEPT",
2190 linitial(lockingClause))->strength))));
2191
2192 /* Process the WITH clause independently of all else */
2193 if (withClause)
2194 {
2195 qry->hasRecursive = withClause->recursive;
2196 qry->cteList = transformWithClause(pstate, withClause);
2197 qry->hasModifyingCTE = pstate->p_hasModifyingCTE;
2198 }
2199
2200 /*
2201 * Recursively transform the components of the tree.
2202 */
2204 transformSetOperationTree(pstate, stmt, true, NULL));
2205 Assert(sostmt);
2206 qry->setOperations = (Node *) sostmt;
2207
2208 /*
2209 * Re-find leftmost SELECT (now it's a sub-query in rangetable)
2210 */
2211 node = sostmt->larg;
2212 while (node && IsA(node, SetOperationStmt))
2213 node = ((SetOperationStmt *) node)->larg;
2214 Assert(node && IsA(node, RangeTblRef));
2215 leftmostRTI = ((RangeTblRef *) node)->rtindex;
2216 leftmostQuery = rt_fetch(leftmostRTI, pstate->p_rtable)->subquery;
2218
2219 /*
2220 * Generate dummy targetlist for outer query using column names of
2221 * leftmost select and common datatypes/collations of topmost set
2222 * operation. Also make lists of the dummy vars and their names for use
2223 * in parsing ORDER BY.
2224 *
2225 * Note: we use leftmostRTI as the varno of the dummy variables. It
2226 * shouldn't matter too much which RT index they have, as long as they
2227 * have one that corresponds to a real RT entry; else funny things may
2228 * happen when the tree is mashed by rule rewriting.
2229 */
2230 qry->targetList = NIL;
2231 targetvars = NIL;
2232 targetnames = NIL;
2234 palloc0(list_length(sostmt->colTypes) * sizeof(ParseNamespaceColumn));
2235 sortcolindex = 0;
2236
2237 forfour(lct, sostmt->colTypes,
2238 lcm, sostmt->colTypmods,
2239 lcc, sostmt->colCollations,
2240 left_tlist, leftmostQuery->targetList)
2241 {
2246 char *colName;
2248 Var *var;
2249
2250 Assert(!lefttle->resjunk);
2251 colName = pstrdup(lefttle->resname);
2252 var = makeVar(leftmostRTI,
2253 lefttle->resno,
2254 colType,
2255 colTypmod,
2257 0);
2258 var->location = exprLocation((Node *) lefttle->expr);
2259 tle = makeTargetEntry((Expr *) var,
2260 (AttrNumber) pstate->p_next_resno++,
2261 colName,
2262 false);
2263 qry->targetList = lappend(qry->targetList, tle);
2267 sortnscolumns[sortcolindex].p_varattno = lefttle->resno;
2268 sortnscolumns[sortcolindex].p_vartype = colType;
2269 sortnscolumns[sortcolindex].p_vartypmod = colTypmod;
2272 sortnscolumns[sortcolindex].p_varattnosyn = lefttle->resno;
2273 sortcolindex++;
2274 }
2275
2276 /*
2277 * As a first step towards supporting sort clauses that are expressions
2278 * using the output columns, generate a namespace entry that makes the
2279 * output columns visible. A Join RTE node is handy for this, since we
2280 * can easily control the Vars generated upon matches.
2281 *
2282 * Note: we don't yet do anything useful with such cases, but at least
2283 * "ORDER BY upper(foo)" will draw the right error message rather than
2284 * "foo not found".
2285 */
2287
2291 JOIN_INNER,
2292 0,
2293 targetvars,
2294 NIL,
2295 NIL,
2296 NULL,
2297 NULL,
2298 false);
2299
2300 sv_namespace = pstate->p_namespace;
2301 pstate->p_namespace = NIL;
2302
2303 /* add jnsitem to column namespace only */
2304 addNSItemToQuery(pstate, jnsitem, false, false, true);
2305
2306 /*
2307 * For now, we don't support resjunk sort clauses on the output of a
2308 * setOperation tree --- you can only use the SQL92-spec options of
2309 * selecting an output column by name or number. Enforce by checking that
2310 * transformSortClause doesn't add any items to tlist. Note, if changing
2311 * this, add_setop_child_rel_equivalences() will need to be updated.
2312 */
2314
2315 qry->sortClause = transformSortClause(pstate,
2316 sortClause,
2317 &qry->targetList,
2319 false /* allow SQL92 rules */ );
2320
2321 /* restore namespace, remove join RTE from rtable */
2322 pstate->p_namespace = sv_namespace;
2323 pstate->p_rtable = list_truncate(pstate->p_rtable, sv_rtable_length);
2324
2325 if (tllen != list_length(qry->targetList))
2326 ereport(ERROR,
2328 errmsg("invalid UNION/INTERSECT/EXCEPT ORDER BY clause"),
2329 errdetail("Only result column names can be used, not expressions or functions."),
2330 errhint("Add the expression/function to every SELECT, or move the UNION into a FROM clause."),
2331 parser_errposition(pstate,
2333
2334 qry->limitOffset = transformLimitClause(pstate, limitOffset,
2335 EXPR_KIND_OFFSET, "OFFSET",
2336 stmt->limitOption);
2337 qry->limitCount = transformLimitClause(pstate, limitCount,
2338 EXPR_KIND_LIMIT, "LIMIT",
2339 stmt->limitOption);
2340 qry->limitOption = stmt->limitOption;
2341
2342 qry->rtable = pstate->p_rtable;
2343 qry->rteperminfos = pstate->p_rteperminfos;
2344 qry->jointree = makeFromExpr(pstate->p_joinlist, NULL);
2345
2346 qry->hasSubLinks = pstate->p_hasSubLinks;
2347 qry->hasWindowFuncs = pstate->p_hasWindowFuncs;
2348 qry->hasTargetSRFs = pstate->p_hasTargetSRFs;
2349 qry->hasAggs = pstate->p_hasAggs;
2350
2351 foreach(l, lockingClause)
2352 {
2353 transformLockingClause(pstate, qry,
2354 (LockingClause *) lfirst(l), false);
2355 }
2356
2357 assign_query_collations(pstate, qry);
2358
2359 /* this must be done after collations, for reliable comparison of exprs */
2360 if (pstate->p_hasAggs || qry->groupClause || qry->groupingSets || qry->havingQual)
2361 parseCheckAggregates(pstate, qry);
2362
2363 return qry;
2364}
void * palloc0(Size size)
Definition mcxt.c:1420
@ JOIN_INNER
Definition nodes.h:301
ParseNamespaceItem * addRangeTableEntryForJoin(ParseState *pstate, List *colnames, ParseNamespaceColumn *nscolumns, JoinType jointype, int nummergedcols, List *aliasvars, List *leftcols, List *rightcols, Alias *join_using_alias, Alias *alias, bool inFromCl)
static Node * transformSetOperationTree(ParseState *pstate, SelectStmt *stmt, bool isTopLevel, List **targetlist)
Definition analyze.c:2421
#define forfour(cell1, list1, cell2, list2, cell3, list3, cell4, list4)
Definition pg_list.h:607
#define lfirst_oid(lc)
Definition pg_list.h:174

References addNSItemToQuery(), addRangeTableEntryForJoin(), Assert, assign_query_collations(), castNode, CMD_SELECT, Query::commandType, Query::cteList, ereport, errcode(), errdetail(), errhint(), errmsg, ERROR, EXPR_KIND_LIMIT, EXPR_KIND_OFFSET, EXPR_KIND_ORDER_BY, exprLocation(), fb(), forfour, Query::groupClause, Query::groupingSets, Query::havingQual, IsA, JOIN_INNER, Query::jointree, lappend(), LCS_asString(), lfirst, lfirst_int, lfirst_oid, Query::limitCount, Query::limitOffset, Query::limitOption, linitial, list_length(), list_nth(), list_truncate(), Var::location, makeFromExpr(), makeNode, makeString(), makeTargetEntry(), makeVar(), NIL, ParseState::p_hasAggs, ParseState::p_hasModifyingCTE, ParseState::p_hasSubLinks, ParseState::p_hasTargetSRFs, ParseState::p_hasWindowFuncs, ParseState::p_joinlist, ParseState::p_namespace, ParseState::p_next_resno, ParseState::p_rtable, ParseState::p_rteperminfos, palloc0(), parseCheckAggregates(), parser_errposition(), pstrdup(), WithClause::recursive, rt_fetch, Query::rtable, SETOP_NONE, Query::setOperations, Query::sortClause, stmt, Query::targetList, transformLimitClause(), transformLockingClause(), transformSetOperationTree(), transformSortClause(), and transformWithClause().

Referenced by transformStmt().

◆ transformSetOperationTree()

static Node * transformSetOperationTree ( ParseState pstate,
SelectStmt stmt,
bool  isTopLevel,
List **  targetlist 
)
static

Definition at line 2421 of file analyze.c.

2423{
2424 bool isLeaf;
2425
2427
2428 /* Guard against stack overflow due to overly complex set-expressions */
2430
2431 /*
2432 * Validity-check both leaf and internal SELECTs for disallowed ops.
2433 */
2434 if (stmt->intoClause)
2435 ereport(ERROR,
2437 errmsg("INTO is only allowed on first SELECT of UNION/INTERSECT/EXCEPT"),
2438 parser_errposition(pstate,
2439 exprLocation((Node *) stmt->intoClause))));
2440
2441 /* We don't support FOR UPDATE/SHARE with set ops at the moment. */
2442 if (stmt->lockingClause)
2443 ereport(ERROR,
2445 /*------
2446 translator: %s is a SQL row locking clause such as FOR UPDATE */
2447 errmsg("%s is not allowed with UNION/INTERSECT/EXCEPT",
2449 linitial(stmt->lockingClause))->strength))));
2450
2451 /*
2452 * If an internal node of a set-op tree has ORDER BY, LIMIT, FOR UPDATE,
2453 * or WITH clauses attached, we need to treat it like a leaf node to
2454 * generate an independent sub-Query tree. Otherwise, it can be
2455 * represented by a SetOperationStmt node underneath the parent Query.
2456 */
2457 if (stmt->op == SETOP_NONE)
2458 {
2459 Assert(stmt->larg == NULL && stmt->rarg == NULL);
2460 isLeaf = true;
2461 }
2462 else
2463 {
2464 Assert(stmt->larg != NULL && stmt->rarg != NULL);
2465 if (stmt->sortClause || stmt->limitOffset || stmt->limitCount ||
2466 stmt->lockingClause || stmt->withClause)
2467 isLeaf = true;
2468 else
2469 isLeaf = false;
2470 }
2471
2472 if (isLeaf)
2473 {
2474 /* Process leaf SELECT */
2478
2479 /*
2480 * Transform SelectStmt into a Query.
2481 *
2482 * This works the same as SELECT transformation normally would, except
2483 * that we prevent resolving unknown-type outputs as TEXT. This does
2484 * not change the subquery's semantics since if the column type
2485 * matters semantically, it would have been resolved to something else
2486 * anyway. Doing this lets us resolve such outputs using
2487 * select_common_type(), below.
2488 *
2489 * Note: previously transformed sub-queries don't affect the parsing
2490 * of this sub-query, because they are not in the toplevel pstate's
2491 * namespace list.
2492 */
2493 selectQuery = parse_sub_analyze((Node *) stmt, pstate,
2494 NULL, false, false);
2495
2496 /*
2497 * Check for bogus references to Vars on the current query level (but
2498 * upper-level references are okay). Normally this can't happen
2499 * because the namespace will be empty, but it could happen if we are
2500 * inside a rule.
2501 */
2502 if (pstate->p_namespace)
2503 {
2505 ereport(ERROR,
2507 errmsg("UNION/INTERSECT/EXCEPT member statement cannot refer to other relations of same query level"),
2508 parser_errposition(pstate,
2510 }
2511
2512 /*
2513 * Extract a list of the non-junk TLEs for upper-level processing.
2514 */
2515 if (targetlist)
2516 {
2517 ListCell *tl;
2518
2519 *targetlist = NIL;
2520 foreach(tl, selectQuery->targetList)
2521 {
2523
2524 if (!tle->resjunk)
2525 *targetlist = lappend(*targetlist, tle);
2526 }
2527 }
2528
2529 /*
2530 * Make the leaf query be a subquery in the top-level rangetable.
2531 */
2534 NULL,
2535 false,
2536 false);
2537
2538 /*
2539 * Return a RangeTblRef to replace the SelectStmt in the set-op tree.
2540 */
2542 rtr->rtindex = nsitem->p_rtindex;
2543 return (Node *) rtr;
2544 }
2545 else
2546 {
2547 /* Process an internal node (set operation node) */
2551 const char *context;
2552 bool recursive = (pstate->p_parent_cte &&
2553 pstate->p_parent_cte->cterecursive);
2554
2555 context = (stmt->op == SETOP_UNION ? "UNION" :
2556 (stmt->op == SETOP_INTERSECT ? "INTERSECT" :
2557 "EXCEPT"));
2558
2559 op->op = stmt->op;
2560 op->all = stmt->all;
2561
2562 /*
2563 * Recursively transform the left child node.
2564 */
2565 op->larg = transformSetOperationTree(pstate, stmt->larg,
2566 false,
2567 &ltargetlist);
2568
2569 /*
2570 * If we are processing a recursive union query, now is the time to
2571 * examine the non-recursive term's output columns and mark the
2572 * containing CTE as having those result columns. We should do this
2573 * only at the topmost setop of the CTE, of course.
2574 */
2575 if (isTopLevel && recursive)
2577
2578 /*
2579 * Recursively transform the right child node.
2580 */
2581 op->rarg = transformSetOperationTree(pstate, stmt->rarg,
2582 false,
2583 &rtargetlist);
2584
2585 constructSetOpTargetlist(pstate, op, ltargetlist, rtargetlist, targetlist,
2586 context, recursive);
2587
2588 return (Node *) op;
2589 }
2590}
@ SETOP_INTERSECT
static void determineRecursiveColTypes(ParseState *pstate, Node *larg, List *nrtargetlist)
Definition analyze.c:2769
void constructSetOpTargetlist(ParseState *pstate, SetOperationStmt *op, const List *ltargetlist, const List *rtargetlist, List **targetlist, const char *context, bool recursive)
Definition analyze.c:2605
Query * parse_sub_analyze(Node *parseTree, ParseState *parentParseState, CommonTableExpr *parentCTE, bool locked_from_parent, bool resolve_unknowns)
Definition analyze.c:245
void check_stack_depth(void)
Definition stack_depth.c:96
int locate_var_of_level(Node *node, int levelsup)
Definition var.c:555

References addRangeTableEntryForSubquery(), SetOperationStmt::all, Assert, check_stack_depth(), constructSetOpTargetlist(), contain_vars_of_level(), determineRecursiveColTypes(), ereport, errcode(), errmsg, ERROR, exprLocation(), fb(), IsA, lappend(), SetOperationStmt::larg, LCS_asString(), lfirst, linitial, locate_var_of_level(), makeNode, NIL, SetOperationStmt::op, ParseState::p_namespace, ParseState::p_parent_cte, parse_sub_analyze(), parser_errposition(), SetOperationStmt::rarg, SETOP_INTERSECT, SETOP_NONE, SETOP_UNION, stmt, and transformSetOperationTree().

Referenced by transformSetOperationStmt(), and transformSetOperationTree().

◆ transformStmt()

Query * transformStmt ( ParseState pstate,
Node parseTree 
)

Definition at line 335 of file analyze.c.

336{
337 Query *result;
338
339#ifdef DEBUG_NODE_TESTS_ENABLED
340
341 /*
342 * We apply debug_raw_expression_coverage_test testing to basic DML
343 * statements; we can't just run it on everything because
344 * raw_expression_tree_walker() doesn't claim to handle utility
345 * statements.
346 */
348 {
349 switch (nodeTag(parseTree))
350 {
351 case T_SelectStmt:
352 case T_InsertStmt:
353 case T_UpdateStmt:
354 case T_DeleteStmt:
355 case T_MergeStmt:
357 break;
358 default:
359 break;
360 }
361 }
362#endif /* DEBUG_NODE_TESTS_ENABLED */
363
364 /*
365 * Caution: when changing the set of statement types that have non-default
366 * processing here, see also stmt_requires_parse_analysis() and
367 * analyze_requires_snapshot().
368 */
369 switch (nodeTag(parseTree))
370 {
371 /*
372 * Optimizable statements
373 */
374 case T_InsertStmt:
376 break;
377
378 case T_DeleteStmt:
380 break;
381
382 case T_UpdateStmt:
384 break;
385
386 case T_MergeStmt:
388 break;
389
390 case T_SelectStmt:
391 {
393
394 if (n->valuesLists)
395 result = transformValuesClause(pstate, n);
396 else if (n->op == SETOP_NONE)
397 result = transformSelectStmt(pstate, n, NULL);
398 else
400 }
401 break;
402
403 case T_ReturnStmt:
405 break;
406
407 case T_PLAssignStmt:
410 break;
411
412 /*
413 * Special cases
414 */
418 break;
419
420 case T_ExplainStmt:
423 break;
424
428 break;
429
430 case T_CallStmt:
431 result = transformCallStmt(pstate,
432 (CallStmt *) parseTree);
433 break;
434
435 default:
436
437 /*
438 * other statements don't require any transformation; just return
439 * the original parsetree with a Query node plastered on top.
440 */
442 result->commandType = CMD_UTILITY;
443 result->utilityStmt = parseTree;
444 break;
445 }
446
447 /* Mark as original query until we learn differently */
448 result->querySource = QSRC_ORIGINAL;
449 result->canSetTag = true;
450
451 return result;
452}
Query * transformMergeStmt(ParseState *pstate, MergeStmt *stmt)
@ QSRC_ORIGINAL
Definition parsenodes.h:36
static Query * transformDeleteStmt(ParseState *pstate, DeleteStmt *stmt)
Definition analyze.c:576
static Query * transformReturnStmt(ParseState *pstate, ReturnStmt *stmt)
Definition analyze.c:2823
static Query * transformPLAssignStmt(ParseState *pstate, PLAssignStmt *stmt)
Definition analyze.c:3186
static Query * transformCreateTableAsStmt(ParseState *pstate, CreateTableAsStmt *stmt)
Definition analyze.c:3513
static Query * transformCallStmt(ParseState *pstate, CallStmt *stmt)
Definition analyze.c:3592
static Query * transformSetOperationStmt(ParseState *pstate, SelectStmt *stmt)
Definition analyze.c:2116
static Query * transformUpdateStmt(ParseState *pstate, UpdateStmt *stmt)
Definition analyze.c:2854
static Query * transformExplainStmt(ParseState *pstate, ExplainStmt *stmt)
Definition analyze.c:3461
static Query * transformDeclareCursorStmt(ParseState *pstate, DeclareCursorStmt *stmt)
Definition analyze.c:3368
static Query * transformInsertStmt(ParseState *pstate, InsertStmt *stmt)
Definition analyze.c:663
static Query * transformValuesClause(ParseState *pstate, SelectStmt *stmt)
Definition analyze.c:1897
List * valuesLists
SetOperation op

References CMD_UTILITY, fb(), makeNode, nodeTag, SelectStmt::op, QSRC_ORIGINAL, result, SETOP_NONE, transformCallStmt(), transformCreateTableAsStmt(), transformDeclareCursorStmt(), transformDeleteStmt(), transformExplainStmt(), transformInsertStmt(), transformMergeStmt(), transformPLAssignStmt(), transformReturnStmt(), transformSelectStmt(), transformSetOperationStmt(), transformUpdateStmt(), transformValuesClause(), and SelectStmt::valuesLists.

Referenced by interpret_AS_clause(), parse_sub_analyze(), transformCreateTableAsStmt(), transformDeclareCursorStmt(), transformInsertStmt(), transformJsonArrayQueryConstructor(), transformOptionalSelectInto(), and transformRuleStmt().

◆ transformTopLevelStmt()

Query * transformTopLevelStmt ( ParseState pstate,
RawStmt parseTree 
)

Definition at line 272 of file analyze.c.

273{
274 Query *result;
275
276 /* We're at top level, so allow SELECT INTO */
278
279 result->stmt_location = parseTree->stmt_location;
280 result->stmt_len = parseTree->stmt_len;
281
282 return result;
283}

References fb(), result, and transformOptionalSelectInto().

Referenced by inline_function(), parse_analyze_fixedparams(), parse_analyze_varparams(), and parse_analyze_withcb().

◆ transformUpdateStmt()

static Query * transformUpdateStmt ( ParseState pstate,
UpdateStmt stmt 
)
static

Definition at line 2854 of file analyze.c.

2855{
2856 Query *qry = makeNode(Query);
2858 Node *qual;
2859
2860 qry->commandType = CMD_UPDATE;
2861
2862 /* process the WITH clause independently of all else */
2863 if (stmt->withClause)
2864 {
2865 qry->hasRecursive = stmt->withClause->recursive;
2866 qry->cteList = transformWithClause(pstate, stmt->withClause);
2867 qry->hasModifyingCTE = pstate->p_hasModifyingCTE;
2868 }
2869
2870 qry->resultRelation = setTargetTable(pstate, stmt->relation,
2871 stmt->relation->inh,
2872 true,
2873 ACL_UPDATE);
2874
2875 /* disallow UPDATE ... WHERE CURRENT OF on a view */
2876 if (stmt->whereClause &&
2877 IsA(stmt->whereClause, CurrentOfExpr) &&
2878 pstate->p_target_relation->rd_rel->relkind == RELKIND_VIEW)
2879 ereport(ERROR,
2881 errmsg("WHERE CURRENT OF on a view is not implemented"));
2882
2883 if (stmt->forPortionOf)
2885 qry->resultRelation,
2887 stmt->whereClause,
2888 true);
2889
2890 nsitem = pstate->p_target_nsitem;
2891
2892 /* subqueries in FROM cannot access the result relation */
2893 nsitem->p_lateral_only = true;
2894 nsitem->p_lateral_ok = false;
2895
2896 /*
2897 * the FROM clause is non-standard SQL syntax. We used to be able to do
2898 * this with REPLACE in POSTQUEL so we keep the feature.
2899 */
2900 transformFromClause(pstate, stmt->fromClause);
2901
2902 /* remaining clauses can reference the result relation normally */
2903 nsitem->p_lateral_only = false;
2904 nsitem->p_lateral_ok = true;
2905
2906 qual = transformWhereClause(pstate, stmt->whereClause,
2907 EXPR_KIND_WHERE, "WHERE");
2908
2909 transformReturningClause(pstate, qry, stmt->returningClause,
2911
2912 /*
2913 * Now we are done with SELECT-like processing, and can get on with
2914 * transforming the target list to match the UPDATE target columns.
2915 */
2916 qry->targetList = transformUpdateTargetList(pstate, stmt->targetList,
2917 qry->forPortionOf);
2918
2919 qry->rtable = pstate->p_rtable;
2920 qry->rteperminfos = pstate->p_rteperminfos;
2921 qry->jointree = makeFromExpr(pstate->p_joinlist, qual);
2922
2923 qry->hasTargetSRFs = pstate->p_hasTargetSRFs;
2924 qry->hasSubLinks = pstate->p_hasSubLinks;
2925
2926 assign_query_collations(pstate, qry);
2927
2928 return qry;
2929}
@ CMD_UPDATE
Definition nodes.h:274

References ACL_UPDATE, assign_query_collations(), CMD_UPDATE, Query::commandType, Query::cteList, ereport, errcode(), errmsg, ERROR, EXPR_KIND_RETURNING, EXPR_KIND_WHERE, fb(), Query::forPortionOf, IsA, Query::jointree, makeFromExpr(), makeNode, ParseState::p_hasModifyingCTE, ParseState::p_hasSubLinks, ParseState::p_hasTargetSRFs, ParseState::p_joinlist, ParseNamespaceItem::p_lateral_only, ParseState::p_rtable, ParseState::p_rteperminfos, ParseState::p_target_nsitem, ParseState::p_target_relation, RelationData::rd_rel, Query::rtable, setTargetTable(), stmt, Query::targetList, transformForPortionOfClause(), transformFromClause(), transformReturningClause(), transformUpdateTargetList(), transformWhereClause(), and transformWithClause().

Referenced by transformStmt().

◆ transformUpdateTargetList()

List * transformUpdateTargetList ( ParseState pstate,
List origTlist,
ForPortionOfExpr forPortionOf 
)

Definition at line 2936 of file analyze.c.

2937{
2938 List *tlist = NIL;
2941 ListCell *tl;
2942
2943 tlist = transformTargetList(pstate, origTlist,
2945
2946 /* Prepare to assign non-conflicting resnos to resjunk attributes */
2949
2950 /* Prepare non-junk columns for assignment to target table */
2953
2954 foreach(tl, tlist)
2955 {
2958 int attrno;
2959
2960 if (tle->resjunk)
2961 {
2962 /*
2963 * Resjunk nodes need no additional processing, but be sure they
2964 * have resnos that do not match any target columns; else rewriter
2965 * or planner might get confused. They don't need a resname
2966 * either.
2967 */
2968 tle->resno = (AttrNumber) pstate->p_next_resno++;
2969 tle->resname = NULL;
2970 continue;
2971 }
2972 if (orig_tl == NULL)
2973 elog(ERROR, "UPDATE target count mismatch --- internal error");
2975
2977 origTarget->name, true);
2979 ereport(ERROR,
2981 errmsg("column \"%s\" of relation \"%s\" does not exist",
2982 origTarget->name,
2984 (origTarget->indirection != NIL &&
2985 strcmp(origTarget->name, pstate->p_target_nsitem->p_names->aliasname) == 0) ?
2986 errhint("SET target columns cannot be qualified with the relation name.") : 0,
2987 parser_errposition(pstate, origTarget->location)));
2988
2989 /*
2990 * If this is a FOR PORTION OF update, forbid directly setting the
2991 * range column, since that would conflict with the implicit updates.
2992 */
2993 if (forPortionOf != NULL)
2994 {
2995 if (attrno == forPortionOf->rangeVar->varattno)
2996 ereport(ERROR,
2998 errmsg("cannot update column \"%s\" because it is used in FOR PORTION OF",
2999 origTarget->name),
3000 parser_errposition(pstate, origTarget->location)));
3001 }
3002
3003 updateTargetListEntry(pstate, tle, origTarget->name,
3004 attrno,
3005 origTarget->indirection,
3006 origTarget->location);
3007
3008 /* Mark the target column as requiring update permissions */
3009 target_perminfo->updatedCols = bms_add_member(target_perminfo->updatedCols,
3011
3013 }
3014 if (orig_tl != NULL)
3015 elog(ERROR, "UPDATE target count mismatch --- internal error");
3016
3017 return tlist;
3018}
@ EXPR_KIND_UPDATE_SOURCE
Definition parse_node.h:56
void updateTargetListEntry(ParseState *pstate, TargetEntry *tle, char *colname, int attrno, List *indirection, int location)
static ListCell * lnext(const List *l, const ListCell *c)
Definition pg_list.h:375
char * aliasname
Definition primnodes.h:52

References Alias::aliasname, attnameAttNum(), bms_add_member(), elog, ereport, errcode(), errhint(), errmsg, ERROR, EXPR_KIND_UPDATE_SOURCE, fb(), FirstLowInvalidHeapAttributeNumber, InvalidAttrNumber, lfirst, lfirst_node, list_head(), lnext(), NIL, ParseNamespaceItem::p_names, ParseState::p_next_resno, ParseNamespaceItem::p_perminfo, ParseState::p_target_nsitem, ParseState::p_target_relation, parser_errposition(), ForPortionOfExpr::rangeVar, RelationGetNumberOfAttributes, RelationGetRelationName, transformTargetList(), updateTargetListEntry(), and Var::varattno.

Referenced by transformMergeStmt(), transformOnConflictClause(), and transformUpdateStmt().

◆ transformValuesClause()

static Query * transformValuesClause ( ParseState pstate,
SelectStmt stmt 
)
static

Definition at line 1897 of file analyze.c.

1898{
1899 Query *qry = makeNode(Query);
1900 List *exprsLists = NIL;
1901 List *coltypes = NIL;
1902 List *coltypmods = NIL;
1904 List **colexprs = NULL;
1905 int sublist_length = -1;
1906 bool lateral = false;
1908 ListCell *lc;
1909 ListCell *lc2;
1910 int i;
1911
1912 qry->commandType = CMD_SELECT;
1913
1914 /* Most SELECT stuff doesn't apply in a VALUES clause */
1915 Assert(stmt->distinctClause == NIL);
1916 Assert(stmt->intoClause == NULL);
1917 Assert(stmt->targetList == NIL);
1918 Assert(stmt->fromClause == NIL);
1919 Assert(stmt->whereClause == NULL);
1920 Assert(stmt->groupClause == NIL);
1921 Assert(stmt->havingClause == NULL);
1922 Assert(stmt->windowClause == NIL);
1923 Assert(stmt->op == SETOP_NONE);
1924
1925 /* process the WITH clause independently of all else */
1926 if (stmt->withClause)
1927 {
1928 qry->hasRecursive = stmt->withClause->recursive;
1929 qry->cteList = transformWithClause(pstate, stmt->withClause);
1930 qry->hasModifyingCTE = pstate->p_hasModifyingCTE;
1931 }
1932
1933 /*
1934 * For each row of VALUES, transform the raw expressions.
1935 *
1936 * Note that the intermediate representation we build is column-organized
1937 * not row-organized. That simplifies the type and collation processing
1938 * below.
1939 */
1940 foreach(lc, stmt->valuesLists)
1941 {
1942 List *sublist = (List *) lfirst(lc);
1943
1944 /*
1945 * Do basic expression transformation (same as a ROW() expr, but here
1946 * we disallow SetToDefault)
1947 */
1949 EXPR_KIND_VALUES, false);
1950
1951 /*
1952 * All the sublists must be the same length, *after* transformation
1953 * (which might expand '*' into multiple items). The VALUES RTE can't
1954 * handle anything different.
1955 */
1956 if (sublist_length < 0)
1957 {
1958 /* Remember post-transformation length of first sublist */
1960 /* and allocate array for per-column lists */
1961 colexprs = (List **) palloc0(sublist_length * sizeof(List *));
1962 }
1963 else if (sublist_length != list_length(sublist))
1964 {
1965 ereport(ERROR,
1967 errmsg("VALUES lists must all be the same length"),
1968 parser_errposition(pstate,
1969 exprLocation((Node *) sublist))));
1970 }
1971
1972 /* Build per-column expression lists */
1973 i = 0;
1974 foreach(lc2, sublist)
1975 {
1976 Node *col = (Node *) lfirst(lc2);
1977
1978 colexprs[i] = lappend(colexprs[i], col);
1979 i++;
1980 }
1981
1982 /* Release sub-list's cells to save memory */
1984
1985 /* Prepare an exprsLists element for this row */
1987 }
1988
1989 /*
1990 * Now resolve the common types of the columns, and coerce everything to
1991 * those types. Then identify the common typmod and common collation, if
1992 * any, of each column.
1993 *
1994 * We must do collation processing now because (1) assign_query_collations
1995 * doesn't process rangetable entries, and (2) we need to label the VALUES
1996 * RTE with column collations for use in the outer query. We don't
1997 * consider conflict of implicit collations to be an error here; instead
1998 * the column will just show InvalidOid as its collation, and you'll get a
1999 * failure later if that results in failure to resolve a collation.
2000 *
2001 * Note we modify the per-column expression lists in-place.
2002 */
2003 for (i = 0; i < sublist_length; i++)
2004 {
2005 Oid coltype;
2007 Oid colcoll;
2008
2009 coltype = select_common_type(pstate, colexprs[i], "VALUES", NULL);
2010
2011 foreach(lc, colexprs[i])
2012 {
2013 Node *col = (Node *) lfirst(lc);
2014
2015 col = coerce_to_common_type(pstate, col, coltype, "VALUES");
2016 lfirst(lc) = col;
2017 }
2018
2019 coltypmod = select_common_typmod(pstate, colexprs[i], coltype);
2020 colcoll = select_common_collation(pstate, colexprs[i], true);
2021
2022 coltypes = lappend_oid(coltypes, coltype);
2025 }
2026
2027 /*
2028 * Finally, rearrange the coerced expressions into row-organized lists.
2029 */
2030 for (i = 0; i < sublist_length; i++)
2031 {
2032 forboth(lc, colexprs[i], lc2, exprsLists)
2033 {
2034 Node *col = (Node *) lfirst(lc);
2035 List *sublist = lfirst(lc2);
2036
2038 lfirst(lc2) = sublist;
2039 }
2040 list_free(colexprs[i]);
2041 }
2042
2043 /*
2044 * Ordinarily there can't be any current-level Vars in the expression
2045 * lists, because the namespace was empty ... but if we're inside CREATE
2046 * RULE, then NEW/OLD references might appear. In that case we have to
2047 * mark the VALUES RTE as LATERAL.
2048 */
2049 if (pstate->p_rtable != NIL &&
2051 lateral = true;
2052
2053 /*
2054 * Generate the VALUES RTE
2055 */
2058 NULL, lateral, true);
2059 addNSItemToQuery(pstate, nsitem, true, true, true);
2060
2061 /*
2062 * Generate a targetlist as though expanding "*"
2063 */
2064 Assert(pstate->p_next_resno == 1);
2065 qry->targetList = expandNSItemAttrs(pstate, nsitem, 0, true, -1);
2066
2067 /*
2068 * The grammar allows attaching ORDER BY, LIMIT, and FOR UPDATE to a
2069 * VALUES, so cope.
2070 */
2071 qry->sortClause = transformSortClause(pstate,
2072 stmt->sortClause,
2073 &qry->targetList,
2075 false /* allow SQL92 rules */ );
2076
2077 qry->limitOffset = transformLimitClause(pstate, stmt->limitOffset,
2078 EXPR_KIND_OFFSET, "OFFSET",
2079 stmt->limitOption);
2080 qry->limitCount = transformLimitClause(pstate, stmt->limitCount,
2081 EXPR_KIND_LIMIT, "LIMIT",
2082 stmt->limitOption);
2083 qry->limitOption = stmt->limitOption;
2084
2085 if (stmt->lockingClause)
2086 ereport(ERROR,
2088 /*------
2089 translator: %s is a SQL row locking clause such as FOR UPDATE */
2090 errmsg("%s cannot be applied to VALUES",
2092 linitial(stmt->lockingClause))->strength))));
2093
2094 qry->rtable = pstate->p_rtable;
2095 qry->rteperminfos = pstate->p_rteperminfos;
2096 qry->jointree = makeFromExpr(pstate->p_joinlist, NULL);
2097
2098 qry->hasSubLinks = pstate->p_hasSubLinks;
2099
2100 assign_query_collations(pstate, qry);
2101
2102 return qry;
2103}
void list_free(List *list)
Definition list.c:1546
List * expandNSItemAttrs(ParseState *pstate, ParseNamespaceItem *nsitem, int sublevels_up, bool require_col_privs, int location)

References addNSItemToQuery(), addRangeTableEntryForValues(), Assert, assign_query_collations(), CMD_SELECT, coerce_to_common_type(), Query::commandType, contain_vars_of_level(), Query::cteList, ereport, errcode(), errmsg, ERROR, expandNSItemAttrs(), EXPR_KIND_LIMIT, EXPR_KIND_OFFSET, EXPR_KIND_ORDER_BY, EXPR_KIND_VALUES, exprLocation(), fb(), forboth, i, Query::jointree, lappend(), lappend_int(), lappend_oid(), LCS_asString(), lfirst, Query::limitCount, Query::limitOffset, Query::limitOption, linitial, list_free(), list_length(), makeFromExpr(), makeNode, NIL, ParseState::p_hasModifyingCTE, ParseState::p_hasSubLinks, ParseState::p_joinlist, ParseState::p_next_resno, ParseState::p_rtable, ParseState::p_rteperminfos, palloc0(), parser_errposition(), Query::rtable, select_common_collation(), select_common_type(), select_common_typmod(), SETOP_NONE, Query::sortClause, stmt, Query::targetList, transformExpressionList(), transformLimitClause(), transformSortClause(), and transformWithClause().

Referenced by transformStmt().

Variable Documentation

◆ post_parse_analyze_hook