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 *forPortionOfClause, 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 3012 of file analyze.c.

3014{
3015 List *colnames;
3016 int numattrs;
3019
3020 /* copy per-column data from the target relation */
3021 colnames = pstate->p_target_nsitem->p_rte->eref->colnames;
3022 numattrs = list_length(colnames);
3023
3025
3027 numattrs * sizeof(ParseNamespaceColumn));
3028
3029 /* mark all columns as returning OLD/NEW */
3030 for (int i = 0; i < numattrs; i++)
3031 nscolumns[i].p_varreturningtype = returning_type;
3032
3033 /* build the nsitem, copying most fields from the target relation */
3035 nsitem->p_names = makeAlias(aliasname, colnames);
3036 nsitem->p_rte = pstate->p_target_nsitem->p_rte;
3037 nsitem->p_rtindex = pstate->p_target_nsitem->p_rtindex;
3038 nsitem->p_perminfo = pstate->p_target_nsitem->p_perminfo;
3039 nsitem->p_nscolumns = nscolumns;
3040 nsitem->p_returning_type = returning_type;
3041
3042 /* add it to the query namespace as a table-only item */
3043 addNSItemToQuery(pstate, nsitem, false, true, false);
3044}
memcpy(sums, checksumBaseOffsets, sizeof(checksumBaseOffsets))
#define palloc_object(type)
Definition fe_memutils.h:74
#define palloc_array(type, count)
Definition fe_memutils.h:76
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 513 of file analyze.c.

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

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 4020 of file analyze.c.

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

1622{
1623 List *result = NIL;
1624 int attno;
1625 Var *var;
1626 TargetEntry *te;
1627
1628 /*
1629 * Note that resnos of the tlist must correspond to attnos of the
1630 * underlying relation, hence we need entries for dropped columns too.
1631 */
1632 for (attno = 0; attno < RelationGetNumberOfAttributes(targetrel); attno++)
1633 {
1634 Form_pg_attribute attr = TupleDescAttr(targetrel->rd_att, attno);
1635 char *name;
1636
1637 if (attr->attisdropped)
1638 {
1639 /*
1640 * can't use atttypid here, but it doesn't really matter what type
1641 * the Const claims to be.
1642 */
1643 var = (Var *) makeNullConst(INT4OID, -1, InvalidOid);
1644 name = NULL;
1645 }
1646 else
1647 {
1648 var = makeVar(exclRelIndex, attno + 1,
1649 attr->atttypid, attr->atttypmod,
1650 attr->attcollation,
1651 0);
1652 name = pstrdup(NameStr(attr->attname));
1653 }
1654
1655 te = makeTargetEntry((Expr *) var,
1656 attno + 1,
1657 name,
1658 false);
1659
1660 result = lappend(result, te);
1661 }
1662
1663 /*
1664 * Add a whole-row-Var entry to support references to "EXCLUDED.*". Like
1665 * the other entries in the EXCLUDED tlist, its resno must match the Var's
1666 * varattno, else the wrong things happen while resolving references in
1667 * setrefs.c. This is against normal conventions for targetlists, but
1668 * it's okay since we don't use this as a real tlist.
1669 */
1670 var = makeVar(exclRelIndex, InvalidAttrNumber,
1671 targetrel->rd_rel->reltype,
1672 -1, InvalidOid, 0);
1673 te = makeTargetEntry((Expr *) var, InvalidAttrNumber, NULL, true);
1674 result = lappend(result, te);
1675
1676 return result;
1677}
#define InvalidAttrNumber
Definition attnum.h:23
#define NameStr(name)
Definition c.h:835
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:1781
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 3729 of file analyze.c.

3730{
3731 Assert(strength != LCS_NONE); /* else caller error */
3732
3733 if (qry->setOperations)
3734 ereport(ERROR,
3736 /*------
3737 translator: %s is a SQL row locking clause such as FOR UPDATE */
3738 errmsg("%s is not allowed with UNION/INTERSECT/EXCEPT",
3739 LCS_asString(strength))));
3740 if (qry->distinctClause != NIL)
3741 ereport(ERROR,
3743 /*------
3744 translator: %s is a SQL row locking clause such as FOR UPDATE */
3745 errmsg("%s is not allowed with DISTINCT clause",
3746 LCS_asString(strength))));
3747 if (qry->groupClause != NIL || qry->groupingSets != NIL)
3748 ereport(ERROR,
3750 /*------
3751 translator: %s is a SQL row locking clause such as FOR UPDATE */
3752 errmsg("%s is not allowed with GROUP BY clause",
3753 LCS_asString(strength))));
3754 if (qry->havingQual != NULL)
3755 ereport(ERROR,
3757 /*------
3758 translator: %s is a SQL row locking clause such as FOR UPDATE */
3759 errmsg("%s is not allowed with HAVING clause",
3760 LCS_asString(strength))));
3761 if (qry->hasAggs)
3762 ereport(ERROR,
3764 /*------
3765 translator: %s is a SQL row locking clause such as FOR UPDATE */
3766 errmsg("%s is not allowed with aggregate functions",
3767 LCS_asString(strength))));
3768 if (qry->hasWindowFuncs)
3769 ereport(ERROR,
3771 /*------
3772 translator: %s is a SQL row locking clause such as FOR UPDATE */
3773 errmsg("%s is not allowed with window functions",
3774 LCS_asString(strength))));
3775 if (qry->hasTargetSRFs)
3776 ereport(ERROR,
3778 /*------
3779 translator: %s is a SQL row locking clause such as FOR UPDATE */
3780 errmsg("%s is not allowed with set-returning functions in the target list",
3781 LCS_asString(strength))));
3782}
int errcode(int sqlerrcode)
Definition elog.c:874
#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:3704
Node * setOperations
Definition parsenodes.h:239
List * groupClause
Definition parsenodes.h:219
Node * havingQual
Definition parsenodes.h:225
List * groupingSets
Definition parsenodes.h:223
List * distinctClause
Definition parsenodes.h:229

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 2601 of file analyze.c.

2604{
2605 ListCell *ltl;
2606 ListCell *rtl;
2607
2608 /*
2609 * Verify that the two children have the same number of non-junk columns,
2610 * and determine the types of the merged output columns.
2611 */
2613 ereport(ERROR,
2615 errmsg("each %s query must have the same number of columns",
2616 context),
2617 parser_errposition(pstate,
2618 exprLocation((const Node *) rtargetlist))));
2619
2620 if (targetlist)
2621 *targetlist = NIL;
2622 op->colTypes = NIL;
2623 op->colTypmods = NIL;
2624 op->colCollations = NIL;
2625 op->groupClauses = NIL;
2626
2628 {
2631 Node *lcolnode = (Node *) ltle->expr;
2632 Node *rcolnode = (Node *) rtle->expr;
2635 Node *bestexpr;
2636 int bestlocation;
2640
2641 /* select common type, same as CASE et al */
2644 context,
2645 &bestexpr);
2647
2648 /*
2649 * Verify the coercions are actually possible. If not, we'd fail
2650 * later anyway, but we want to fail now while we have sufficient
2651 * context to produce an error cursor position.
2652 *
2653 * For all non-UNKNOWN-type cases, we verify coercibility but we don't
2654 * modify the child's expression, for fear of changing the child
2655 * query's semantics.
2656 *
2657 * If a child expression is an UNKNOWN-type Const or Param, we want to
2658 * replace it with the coerced expression. This can only happen when
2659 * the child is a leaf set-op node. It's safe to replace the
2660 * expression because if the child query's semantics depended on the
2661 * type of this output column, it'd have already coerced the UNKNOWN
2662 * to something else. We want to do this because (a) we want to
2663 * verify that a Const is valid for the target type, or resolve the
2664 * actual type of an UNKNOWN Param, and (b) we want to avoid
2665 * unnecessary discrepancies between the output type of the child
2666 * query and the resolved target type. Such a discrepancy would
2667 * disable optimization in the planner.
2668 *
2669 * If it's some other UNKNOWN-type node, eg a Var, we do nothing
2670 * (knowing that coerce_to_common_type would fail). The planner is
2671 * sometimes able to fold an UNKNOWN Var to a constant before it has
2672 * to coerce the type, so failing now would just break cases that
2673 * might work.
2674 */
2675 if (lcoltype != UNKNOWNOID)
2677 rescoltype, context);
2678 else if (IsA(lcolnode, Const) ||
2679 IsA(lcolnode, Param))
2680 {
2682 rescoltype, context);
2683 ltle->expr = (Expr *) lcolnode;
2684 }
2685
2686 if (rcoltype != UNKNOWNOID)
2688 rescoltype, context);
2689 else if (IsA(rcolnode, Const) ||
2690 IsA(rcolnode, Param))
2691 {
2693 rescoltype, context);
2694 rtle->expr = (Expr *) rcolnode;
2695 }
2696
2699 rescoltype);
2700
2701 /*
2702 * Select common collation. A common collation is required for all
2703 * set operators except UNION ALL; see SQL:2008 7.13 <query
2704 * expression> Syntax Rule 15c. (If we fail to identify a common
2705 * collation for a UNION ALL column, the colCollations element will be
2706 * set to InvalidOid, which may result in a runtime error if something
2707 * at a higher query level wants to use the column's collation.)
2708 */
2711 (op->op == SETOP_UNION && op->all));
2712
2713 /* emit results */
2714 op->colTypes = lappend_oid(op->colTypes, rescoltype);
2715 op->colTypmods = lappend_int(op->colTypmods, rescoltypmod);
2716 op->colCollations = lappend_oid(op->colCollations, rescolcoll);
2717
2718 /*
2719 * For all cases except UNION ALL, identify the grouping operators
2720 * (and, if available, sorting operators) that will be used to
2721 * eliminate duplicates.
2722 */
2723 if (op->op != SETOP_UNION || !op->all)
2724 {
2726
2728 bestlocation);
2729
2730 /* If it's a recursive union, we need to require hashing support. */
2731 op->groupClauses = lappend(op->groupClauses,
2733
2735 }
2736
2737 /*
2738 * Construct a dummy tlist entry to return. We use a SetToDefault
2739 * node for the expression, since it carries exactly the fields
2740 * needed, but any other expression node type would do as well.
2741 */
2742 if (targetlist)
2743 {
2746
2747 rescolnode->typeId = rescoltype;
2748 rescolnode->typeMod = rescoltypmod;
2749 rescolnode->collation = rescolcoll;
2750 rescolnode->location = bestlocation;
2752 0, /* no need to set resno */
2753 NULL,
2754 false);
2755 *targetlist = lappend(*targetlist, restle);
2756 }
2757 }
2758}
int32_t int32
Definition c.h:620
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:1392
#define IsA(nodeptr, _type_)
Definition nodes.h:164
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:2369
#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:135
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 1690 of file analyze.c.

1691{
1692 if (expr == NULL)
1693 return -1;
1694 if (IsA(expr, RowExpr))
1695 return list_length(((RowExpr *) expr)->args);
1696 if (IsA(expr, Var))
1697 {
1698 Var *var = (Var *) expr;
1699 AttrNumber attnum = var->varattno;
1700
1701 if (attnum > 0 && var->vartype == RECORDOID)
1702 {
1704
1705 rte = GetRTEByRangeTablePosn(pstate, var->varno, var->varlevelsup);
1706 if (rte->rtekind == RTE_SUBQUERY)
1707 {
1708 /* Subselect-in-FROM: examine sub-select's output expr */
1709 TargetEntry *ste = get_tle_by_resno(rte->subquery->targetList,
1710 attnum);
1711
1712 if (ste == NULL || ste->resjunk)
1713 return -1;
1714 expr = (Node *) ste->expr;
1715 if (IsA(expr, RowExpr))
1716 return list_length(((RowExpr *) expr)->args);
1717 }
1718 }
1719 }
1720 return -1;
1721}
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 2765 of file analyze.c.

2766{
2767 Node *node;
2768 int leftmostRTI;
2770 List *targetList;
2772 ListCell *nrtl;
2773 int next_resno;
2774
2775 /*
2776 * Find leftmost leaf SELECT
2777 */
2778 node = larg;
2779 while (node && IsA(node, SetOperationStmt))
2780 node = ((SetOperationStmt *) node)->larg;
2781 Assert(node && IsA(node, RangeTblRef));
2782 leftmostRTI = ((RangeTblRef *) node)->rtindex;
2783 leftmostQuery = rt_fetch(leftmostRTI, pstate->p_rtable)->subquery;
2785
2786 /*
2787 * Generate dummy targetlist using column names of leftmost select and
2788 * dummy result expressions of the non-recursive term.
2789 */
2790 targetList = NIL;
2791 next_resno = 1;
2792
2794 {
2797 char *colName;
2799
2800 Assert(!lefttle->resjunk);
2801 colName = pstrdup(lefttle->resname);
2802 tle = makeTargetEntry(nrtle->expr,
2803 next_resno++,
2804 colName,
2805 false);
2806 targetList = lappend(targetList, tle);
2807 }
2808
2809 /* Now build CTE's output column info using dummy targetlist */
2810 analyzeCTETargetList(pstate, pstate->p_parent_cte, targetList);
2811}
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 3704 of file analyze.c.

3705{
3706 switch (strength)
3707 {
3708 case LCS_NONE:
3709 Assert(false);
3710 break;
3711 case LCS_FORKEYSHARE:
3712 return "FOR KEY SHARE";
3713 case LCS_FORSHARE:
3714 return "FOR SHARE";
3715 case LCS_FORNOKEYUPDATE:
3716 return "FOR NO KEY UPDATE";
3717 case LCS_FORUPDATE:
3718 return "FOR UPDATE";
3719 }
3720 return "FOR some"; /* shouldn't happen */
3721}
@ 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 2369 of file analyze.c.

2370{
2372 Oid sortop;
2373 Oid eqop;
2374 bool hashable;
2375
2376 /* determine the eqop and optional sortop */
2378 false, true, false,
2379 &sortop, &eqop, NULL,
2380 &hashable);
2381
2382 /*
2383 * The type cache doesn't believe that record is hashable (see
2384 * cache_record_field_properties()), but if the caller really needs hash
2385 * support, we can assume it does. Worst case, if any components of the
2386 * record don't support hashing, we will fail at execution.
2387 */
2389 hashable = true;
2390
2391 /* we don't have a tlist yet, so can't assign sortgrouprefs */
2392 grpcl->tleSortGroupRef = 0;
2393 grpcl->eqop = eqop;
2394 grpcl->sortop = sortop;
2395 grpcl->reverse_sort = false; /* Sort-op is "less than", or InvalidOid */
2396 grpcl->nulls_first = false; /* OK with or without sortop */
2397 grpcl->hashable = hashable;
2398
2399 return grpcl;
2400}
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 127 of file analyze.c.

130{
132 Query *query;
134
135 Assert(sourceText != NULL); /* required as of 8.4 */
136
137 pstate->p_sourcetext = sourceText;
138
139 if (numParams > 0)
140 setup_parse_fixed_parameters(pstate, paramTypes, numParams);
141
142 pstate->p_queryEnv = queryEnv;
143
144 query = transformTopLevelStmt(pstate, parseTree);
145
146 if (IsQueryIdEnabled())
147 jstate = JumbleQuery(query);
148
150 (*post_parse_analyze_hook) (pstate, query, jstate);
151
152 free_parsestate(pstate);
153
154 pgstat_report_query_id(query->queryId, false);
155
156 return query;
157}
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:271
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 167 of file analyze.c.

170{
172 Query *query;
174
175 Assert(sourceText != NULL); /* required as of 8.4 */
176
177 pstate->p_sourcetext = sourceText;
178
179 setup_parse_variable_parameters(pstate, paramTypes, numParams);
180
181 pstate->p_queryEnv = queryEnv;
182
183 query = transformTopLevelStmt(pstate, parseTree);
184
185 /* make sure all is well with parameter types */
186 check_variable_parameters(pstate, query);
187
188 if (IsQueryIdEnabled())
189 jstate = JumbleQuery(query);
190
192 (*post_parse_analyze_hook) (pstate, query, jstate);
193
194 free_parsestate(pstate);
195
196 pgstat_report_query_id(query->queryId, false);
197
198 return query;
199}
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 208 of file analyze.c.

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

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 244 of file analyze.c.

248{
249 ParseState *pstate = make_parsestate(parentParseState);
250 Query *query;
251
252 pstate->p_parent_cte = parentCTE;
255
256 query = transformStmt(pstate, parseTree);
257
258 free_parsestate(pstate);
259
260 return query;
261}
Query * transformStmt(ParseState *pstate, Node *parseTree)
Definition analyze.c:334
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 542 of file analyze.c.

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

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 469 of file analyze.c.

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

References fb(), nodeTag, and result.

Referenced by analyze_requires_snapshot(), and StmtPlanRequiresRevalidation().

◆ transformCallStmt()

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

Definition at line 3579 of file analyze.c.

3580{
3581 List *targs;
3582 ListCell *lc;
3583 Node *node;
3584 FuncExpr *fexpr;
3587 bool isNull;
3588 List *outargs = NIL;
3589 Query *result;
3590
3591 /*
3592 * First, do standard parse analysis on the procedure call and its
3593 * arguments, allowing us to identify the called procedure.
3594 */
3595 targs = NIL;
3596 foreach(lc, stmt->funccall->args)
3597 {
3598 targs = lappend(targs, transformExpr(pstate,
3599 (Node *) lfirst(lc),
3601 }
3602
3603 node = ParseFuncOrColumn(pstate,
3604 stmt->funccall->funcname,
3605 targs,
3606 pstate->p_last_srf,
3607 stmt->funccall,
3608 true,
3609 stmt->funccall->location);
3610
3611 assign_expr_collations(pstate, node);
3612
3613 fexpr = castNode(FuncExpr, node);
3614
3617 elog(ERROR, "cache lookup failed for function %u", fexpr->funcid);
3618
3619 /*
3620 * Expand the argument list to deal with named-argument notation and
3621 * default arguments. For ordinary FuncExprs this'd be done during
3622 * planning, but a CallStmt doesn't go through planning, and there seems
3623 * no good reason not to do it here.
3624 */
3626 true,
3627 fexpr->funcresulttype,
3628 proctup);
3629
3630 /* Fetch proargmodes; if it's null, there are no output args */
3633 &isNull);
3634 if (!isNull)
3635 {
3636 /*
3637 * Split the list into input arguments in fexpr->args and output
3638 * arguments in stmt->outargs. INOUT arguments appear in both lists.
3639 */
3640 ArrayType *arr;
3641 int numargs;
3642 char *argmodes;
3643 List *inargs;
3644 int i;
3645
3646 arr = DatumGetArrayTypeP(proargmodes); /* ensure not toasted */
3647 numargs = list_length(fexpr->args);
3648 if (ARR_NDIM(arr) != 1 ||
3649 ARR_DIMS(arr)[0] != numargs ||
3650 ARR_HASNULL(arr) ||
3651 ARR_ELEMTYPE(arr) != CHAROID)
3652 elog(ERROR, "proargmodes is not a 1-D char array of length %d or it contains nulls",
3653 numargs);
3654 argmodes = (char *) ARR_DATA_PTR(arr);
3655
3656 inargs = NIL;
3657 i = 0;
3658 foreach(lc, fexpr->args)
3659 {
3660 Node *n = lfirst(lc);
3661
3662 switch (argmodes[i])
3663 {
3664 case PROARGMODE_IN:
3666 inargs = lappend(inargs, n);
3667 break;
3668 case PROARGMODE_OUT:
3669 outargs = lappend(outargs, n);
3670 break;
3671 case PROARGMODE_INOUT:
3672 inargs = lappend(inargs, n);
3673 outargs = lappend(outargs, copyObject(n));
3674 break;
3675 default:
3676 /* note we don't support PROARGMODE_TABLE */
3677 elog(ERROR, "invalid argmode %c for procedure",
3678 argmodes[i]);
3679 break;
3680 }
3681 i++;
3682 }
3683 fexpr->args = inargs;
3684 }
3685
3686 stmt->funcexpr = fexpr;
3687 stmt->outargs = outargs;
3688
3690
3691 /* represent the command as a utility Query */
3693 result->commandType = CMD_UTILITY;
3694 result->utilityStmt = (Node *) stmt;
3695
3696 return result;
3697}
#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:4899
#define elog(elevel,...)
Definition elog.h:228
#define HeapTupleIsValid(tuple)
Definition htup.h:78
#define stmt
#define copyObject(obj)
Definition nodes.h:232
#define castNode(_type_, nodeptr)
Definition nodes.h:182
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 3500 of file analyze.c.

3501{
3502 Query *result;
3503 Query *query;
3504
3505 /* transform contained query, not allowing SELECT INTO */
3506 query = transformStmt(pstate, stmt->query);
3507 stmt->query = (Node *) query;
3508
3509 /* additional work needed for CREATE MATERIALIZED VIEW */
3510 if (stmt->objtype == OBJECT_MATVIEW)
3511 {
3513
3514 /*
3515 * Prohibit a data-modifying CTE in the query used to create a
3516 * materialized view. It's not sufficiently clear what the user would
3517 * want to happen if the MV is refreshed or incrementally maintained.
3518 */
3519 if (query->hasModifyingCTE)
3520 ereport(ERROR,
3522 errmsg("materialized views must not use data-modifying statements in WITH")));
3523
3524 /*
3525 * Check whether any temporary database objects are used in the
3526 * creation query. It would be hard to refresh data or incrementally
3527 * maintain it if a source disappeared.
3528 */
3530 ereport(ERROR,
3532 errmsg("materialized views must not use temporary objects"),
3533 errdetail("This view depends on temporary %s.",
3535
3536 /*
3537 * A materialized view would either need to save parameters for use in
3538 * maintaining/loading the data or prohibit them entirely. The latter
3539 * seems safer and more sane.
3540 */
3542 ereport(ERROR,
3544 errmsg("materialized views may not be defined using bound parameters")));
3545
3546 /*
3547 * For now, we disallow unlogged materialized views, because it seems
3548 * like a bad idea for them to just go to empty after a crash. (If we
3549 * could mark them as unpopulated, that would be better, but that
3550 * requires catalog changes which crash recovery can't presently
3551 * handle.)
3552 */
3553 if (stmt->into->rel->relpersistence == RELPERSISTENCE_UNLOGGED)
3554 ereport(ERROR,
3556 errmsg("materialized views cannot be unlogged")));
3557
3558 /*
3559 * At runtime, we'll need a copy of the parsed-but-not-rewritten Query
3560 * for purposes of creating the view's ON SELECT rule. We stash that
3561 * in the IntoClause because that's where intorel_startup() can
3562 * conveniently get it from.
3563 */
3564 stmt->into->viewQuery = copyObject(query);
3565 }
3566
3567 /* represent the command as a utility Query */
3569 result->commandType = CMD_UTILITY;
3570 result->utilityStmt = (Node *) stmt;
3571
3572 return result;
3573}
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 3355 of file analyze.c.

3356{
3357 Query *result;
3358 Query *query;
3359
3360 if ((stmt->options & CURSOR_OPT_SCROLL) &&
3361 (stmt->options & CURSOR_OPT_NO_SCROLL))
3362 ereport(ERROR,
3364 /* translator: %s is a SQL keyword */
3365 errmsg("cannot specify both %s and %s",
3366 "SCROLL", "NO SCROLL")));
3367
3368 if ((stmt->options & CURSOR_OPT_ASENSITIVE) &&
3369 (stmt->options & CURSOR_OPT_INSENSITIVE))
3370 ereport(ERROR,
3372 /* translator: %s is a SQL keyword */
3373 errmsg("cannot specify both %s and %s",
3374 "ASENSITIVE", "INSENSITIVE")));
3375
3376 /* Transform contained query, not allowing SELECT INTO */
3377 query = transformStmt(pstate, stmt->query);
3378 stmt->query = (Node *) query;
3379
3380 /* Grammar should not have allowed anything but SELECT */
3381 if (!IsA(query, Query) ||
3382 query->commandType != CMD_SELECT)
3383 elog(ERROR, "unexpected non-SELECT command in DECLARE CURSOR");
3384
3385 /*
3386 * We also disallow data-modifying WITH in a cursor. (This could be
3387 * allowed, but the semantics of when the updates occur might be
3388 * surprising.)
3389 */
3390 if (query->hasModifyingCTE)
3391 ereport(ERROR,
3393 errmsg("DECLARE CURSOR must not contain data-modifying statements in WITH")));
3394
3395 /* FOR UPDATE and WITH HOLD are not compatible */
3396 if (query->rowMarks != NIL && (stmt->options & CURSOR_OPT_HOLD))
3397 ereport(ERROR,
3399 /*------
3400 translator: %s is a SQL row locking clause such as FOR UPDATE */
3401 errmsg("DECLARE CURSOR WITH HOLD ... %s is not supported",
3403 linitial(query->rowMarks))->strength)),
3404 errdetail("Holdable cursors must be READ ONLY.")));
3405
3406 /* FOR UPDATE and SCROLL are not compatible */
3407 if (query->rowMarks != NIL && (stmt->options & CURSOR_OPT_SCROLL))
3408 ereport(ERROR,
3410 /*------
3411 translator: %s is a SQL row locking clause such as FOR UPDATE */
3412 errmsg("DECLARE SCROLL CURSOR ... %s is not supported",
3414 linitial(query->rowMarks))->strength)),
3415 errdetail("Scrollable cursors must be READ ONLY.")));
3416
3417 /* FOR UPDATE and INSENSITIVE are not compatible */
3418 if (query->rowMarks != NIL && (stmt->options & CURSOR_OPT_INSENSITIVE))
3419 ereport(ERROR,
3421 /*------
3422 translator: %s is a SQL row locking clause such as FOR UPDATE */
3423 errmsg("DECLARE INSENSITIVE CURSOR ... %s is not valid",
3425 linitial(query->rowMarks))->strength)),
3426 errdetail("Insensitive cursors must be READ ONLY.")));
3427
3428 /* represent the command as a utility Query */
3430 result->commandType = CMD_UTILITY;
3431 result->utilityStmt = (Node *) stmt;
3432
3433 return result;
3434}
@ CMD_SELECT
Definition nodes.h:275
#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 575 of file analyze.c.

576{
577 Query *qry = makeNode(Query);
579 Node *qual;
580
581 qry->commandType = CMD_DELETE;
582
583 /* process the WITH clause independently of all else */
584 if (stmt->withClause)
585 {
586 qry->hasRecursive = stmt->withClause->recursive;
587 qry->cteList = transformWithClause(pstate, stmt->withClause);
588 qry->hasModifyingCTE = pstate->p_hasModifyingCTE;
589 }
590
591 /* set up range table with just the result rel */
592 qry->resultRelation = setTargetTable(pstate, stmt->relation,
593 stmt->relation->inh,
594 true,
595 ACL_DELETE);
596 nsitem = pstate->p_target_nsitem;
597
598 /* there's no DISTINCT in DELETE */
599 qry->distinctClause = NIL;
600
601 /* subqueries in USING cannot access the result relation */
602 nsitem->p_lateral_only = true;
603 nsitem->p_lateral_ok = false;
604
605 /*
606 * The USING clause is non-standard SQL syntax, and is equivalent in
607 * functionality to the FROM list that can be specified for UPDATE. The
608 * USING keyword is used rather than FROM because FROM is already a
609 * keyword in the DELETE syntax.
610 */
611 transformFromClause(pstate, stmt->usingClause);
612
613 /* remaining clauses can reference the result relation normally */
614 nsitem->p_lateral_only = false;
615 nsitem->p_lateral_ok = true;
616
617 if (stmt->forPortionOf)
619 qry->resultRelation,
621 false);
622
623 qual = transformWhereClause(pstate, stmt->whereClause,
624 EXPR_KIND_WHERE, "WHERE");
625
626 transformReturningClause(pstate, qry, stmt->returningClause,
628
629 /* done building the range table and jointree */
630 qry->rtable = pstate->p_rtable;
631 qry->rteperminfos = pstate->p_rteperminfos;
632 qry->jointree = makeFromExpr(pstate->p_joinlist, qual);
633
634 qry->hasSubLinks = pstate->p_hasSubLinks;
635 qry->hasWindowFuncs = pstate->p_hasWindowFuncs;
636 qry->hasTargetSRFs = pstate->p_hasTargetSRFs;
637 qry->hasAggs = pstate->p_hasAggs;
638
639 assign_query_collations(pstate, qry);
640
641 /* this must be done after collations, for reliable comparison of exprs */
642 if (pstate->p_hasAggs)
643 parseCheckAggregates(pstate, qry);
644
645 return qry;
646}
FromExpr * makeFromExpr(List *fromlist, Node *quals)
Definition makefuncs.c:336
@ CMD_DELETE
Definition nodes.h:278
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
void transformReturningClause(ParseState *pstate, Query *qry, ReturningClause *returningClause, ParseExprKind exprKind)
Definition analyze.c:3051
static ForPortionOfExpr * transformForPortionOfClause(ParseState *pstate, int rtindex, const ForPortionOfClause *forPortionOfClause, bool isUpdate)
Definition analyze.c:1311
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
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:185
List * cteList
Definition parsenodes.h:176
ForPortionOfExpr * forPortionOf
Definition parsenodes.h:151
List * rtable
Definition parsenodes.h:178

References ACL_DELETE, assign_query_collations(), CMD_DELETE, Query::commandType, Query::cteList, Query::distinctClause, EXPR_KIND_RETURNING, EXPR_KIND_WHERE, fb(), Query::forPortionOf, 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, parseCheckAggregates(), 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 3448 of file analyze.c.

3449{
3450 Query *result;
3451 bool generic_plan = false;
3452 Oid *paramTypes = NULL;
3453 int numParams = 0;
3454
3455 /*
3456 * If we have no external source of parameter definitions, and the
3457 * GENERIC_PLAN option is specified, then accept variable parameter
3458 * definitions (similarly to PREPARE, for example).
3459 */
3460 if (pstate->p_paramref_hook == NULL)
3461 {
3462 ListCell *lc;
3463
3464 foreach(lc, stmt->options)
3465 {
3466 DefElem *opt = (DefElem *) lfirst(lc);
3467
3468 if (strcmp(opt->defname, "generic_plan") == 0)
3470 /* don't "break", as we want the last value */
3471 }
3472 if (generic_plan)
3473 setup_parse_variable_parameters(pstate, &paramTypes, &numParams);
3474 }
3475
3476 /* transform contained query, allowing SELECT INTO */
3477 stmt->query = (Node *) transformOptionalSelectInto(pstate, stmt->query);
3478
3479 /* make sure all is well with parameter types */
3480 if (generic_plan)
3481 check_variable_parameters(pstate, (Query *) stmt->query);
3482
3483 /* represent the command as a utility Query */
3485 result->commandType = CMD_UTILITY;
3486 result->utilityStmt = (Node *) stmt;
3487
3488 return result;
3489}
bool defGetBoolean(DefElem *def)
Definition define.c:93
static Query * transformOptionalSelectInto(ParseState *pstate, Node *parseTree)
Definition analyze.c:295
char * defname
Definition parsenodes.h:860
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 forPortionOfClause,
bool  isUpdate 
)
static

Definition at line 1311 of file analyze.c.

1315{
1318 Form_pg_attribute attr;
1320 Oid opclass;
1321 Oid opfamily;
1322 Oid opcintype;
1323 Oid funcid = InvalidOid;
1325 Oid opid;
1326 OpExpr *op;
1328 Var *rangeVar;
1329
1330 /* We don't support FOR PORTION OF FDW queries. */
1331 if (targetrel->rd_rel->relkind == RELKIND_FOREIGN_TABLE)
1332 ereport(ERROR,
1334 errmsg("foreign tables don't support FOR PORTION OF")));
1335
1337
1338 /* Look up the FOR PORTION OF name requested. */
1339 range_attno = attnameAttNum(targetrel, forPortionOf->range_name, false);
1341 ereport(ERROR,
1343 errmsg("column \"%s\" of relation \"%s\" does not exist",
1344 forPortionOf->range_name,
1346 parser_errposition(pstate, forPortionOf->location)));
1347 attr = TupleDescAttr(targetrel->rd_att, range_attno - 1);
1348
1349 attbasetype = getBaseType(attr->atttypid);
1350
1351 rangeVar = makeVar(rtindex,
1353 attr->atttypid,
1354 attr->atttypmod,
1355 attr->attcollation,
1356 0);
1357 rangeVar->location = forPortionOf->location;
1358 result->rangeVar = rangeVar;
1359
1360 /* Require SELECT privilege on the application-time column. */
1361 markVarForSelectPriv(pstate, rangeVar);
1362
1363 /*
1364 * Use the basetype for the target, which shouldn't be required to follow
1365 * domain rules. The table's column type is in the Var if we need it.
1366 */
1367 result->rangeType = attbasetype;
1368 result->isDomain = attbasetype != attr->atttypid;
1369
1370 if (forPortionOf->target)
1371 {
1374
1375 /*
1376 * We were already given an expression for the target, so we don't
1377 * have to build anything. We still have to make sure we got the right
1378 * type. NULL will be caught be the executor.
1379 */
1380
1381 result->targetRange = transformExpr(pstate,
1382 forPortionOf->target,
1384
1385 actual_target_type = exprType(result->targetRange);
1386
1388 ereport(ERROR,
1390 errmsg("could not coerce FOR PORTION OF target from %s to %s",
1393 parser_errposition(pstate, exprLocation(forPortionOf->target))));
1394
1395 result->targetRange = coerce_type(pstate,
1396 result->targetRange,
1399 -1,
1402 exprLocation(forPortionOf->target));
1403
1404 /*
1405 * XXX: For now we only support ranges and multiranges, so we fail on
1406 * anything else.
1407 */
1409 ereport(ERROR,
1411 errmsg("column \"%s\" of relation \"%s\" is not a range or multirange type",
1412 forPortionOf->range_name,
1414 parser_errposition(pstate, forPortionOf->location)));
1415
1416 }
1417 else
1418 {
1422 List *args;
1423
1424 /*
1425 * Make sure it's a range column. XXX: We could support this syntax on
1426 * multirange columns too, if we just built a one-range multirange
1427 * from the FROM/TO phrases.
1428 */
1430 ereport(ERROR,
1432 errmsg("column \"%s\" of relation \"%s\" is not a range type",
1433 forPortionOf->range_name,
1435 parser_errposition(pstate, forPortionOf->location)));
1436
1440
1441 /*
1442 * Build a range from the FROM ... TO ... bounds. This should give a
1443 * constant result, so we accept functions like NOW() but not column
1444 * references, subqueries, etc.
1445 */
1446 result->targetFrom = transformExpr(pstate,
1447 forPortionOf->target_start,
1449 result->targetTo = transformExpr(pstate,
1450 forPortionOf->target_end,
1452 actual_arg_types[0] = exprType(result->targetFrom);
1453 actual_arg_types[1] = exprType(result->targetTo);
1454 args = list_make2(copyObject(result->targetFrom),
1455 copyObject(result->targetTo));
1456
1457 /*
1458 * Check the bound types separately, for better error message and
1459 * location
1460 */
1462 ereport(ERROR,
1464 errmsg("could not coerce FOR PORTION OF %s bound from %s to %s",
1465 "FROM",
1468 parser_errposition(pstate, exprLocation(forPortionOf->target_start))));
1470 ereport(ERROR,
1472 errmsg("could not coerce FOR PORTION OF %s bound from %s to %s",
1473 "TO",
1476 parser_errposition(pstate, exprLocation(forPortionOf->target_end))));
1477
1481 args,
1483 }
1485 ereport(ERROR,
1486 (errmsg("FOR PORTION OF bounds cannot contain volatile functions")));
1487
1488 /*
1489 * Build overlapsExpr to use as an extra qual. This means we only hit rows
1490 * matching the FROM & TO bounds. We must look up the overlaps operator
1491 * (usually "&&").
1492 */
1493 opclass = GetDefaultOpClass(attr->atttypid, GIST_AM_OID);
1494 if (!OidIsValid(opclass))
1495 ereport(ERROR,
1497 errmsg("data type %s has no default operator class for access method \"%s\"",
1498 format_type_be(attr->atttypid), "gist"),
1499 errhint("You must define a default operator class for the data type.")));
1500
1501 /* Look up the operators and functions we need. */
1503 op = makeNode(OpExpr);
1504 op->opno = opid;
1505 op->opfuncid = get_opcode(opid);
1506 op->opresulttype = BOOLOID;
1507 op->args = list_make2(copyObject(rangeVar), copyObject(result->targetRange));
1508 result->overlapsExpr = (Node *) op;
1509
1510 /*
1511 * Look up the without_portion func. This computes the bounds of temporal
1512 * leftovers.
1513 *
1514 * XXX: Find a more extensible way to look up the function, permitting
1515 * user-defined types. An opclass support function doesn't make sense,
1516 * since there is no index involved. Perhaps a type support function.
1517 */
1518 if (get_opclass_opfamily_and_input_type(opclass, &opfamily, &opcintype))
1519 switch (opcintype)
1520 {
1521 case ANYRANGEOID:
1522 result->withoutPortionProc = F_RANGE_MINUS_MULTI;
1523 break;
1524 case ANYMULTIRANGEOID:
1525 result->withoutPortionProc = F_MULTIRANGE_MINUS_MULTI;
1526 break;
1527 default:
1528 elog(ERROR, "unexpected opcintype: %u", opcintype);
1529 }
1530 else
1531 elog(ERROR, "unexpected opclass: %u", opclass);
1532
1533 if (isUpdate)
1534 {
1535 /*
1536 * Now make sure we update the start/end time of the record. For a
1537 * range col (r) this is `r = r * targetRange` (where * is the
1538 * intersect operator).
1539 */
1541 List *funcArgs;
1544
1545 /*
1546 * Whatever operator is used for intersect by temporal foreign keys,
1547 * we can use its backing procedure for intersects in FOR PORTION OF.
1548 * XXX: Share code with FindFKPeriodOpers?
1549 */
1550 switch (opcintype)
1551 {
1552 case ANYRANGEOID:
1554 break;
1555 case ANYMULTIRANGEOID:
1557 break;
1558 default:
1559 elog(ERROR, "unexpected opcintype: %u", opcintype);
1560 }
1561 funcid = get_opcode(intersectoperoid);
1562 if (!OidIsValid(funcid))
1563 ereport(ERROR,
1565 errmsg("could not identify an intersect function for type %s",
1566 format_type_be(opcintype)));
1567
1568 funcArgs = list_make2(copyObject(rangeVar),
1569 copyObject(result->targetRange));
1573
1574 /*
1575 * Coerce to domain if necessary. If we skip this, we will allow
1576 * updating to forbidden values.
1577 */
1578 rangeTLEExpr = coerce_type(pstate,
1581 attr->atttypid,
1582 -1,
1585 exprLocation(forPortionOf->target));
1586
1587 /* Make a TLE to set the range column */
1588 result->rangeTargetList = NIL;
1590 forPortionOf->range_name, false);
1591 result->rangeTargetList = lappend(result->rangeTargetList, tle);
1592
1593 /*
1594 * The range column will change, but you don't need UPDATE permission
1595 * on it, so we don't add to updatedCols here. XXX: If
1596 * https://www.postgresql.org/message-id/CACJufxEtY1hdLcx%3DFhnqp-ERcV1PhbvELG5COy_CZjoEW76ZPQ%40mail.gmail.com
1597 * is merged (only validate CHECK constraints if they depend on one of
1598 * the columns being UPDATEd), we need to make sure that code knows
1599 * that we are updating the application-time column.
1600 */
1601 }
1602 else
1603 result->rangeTargetList = NIL;
1604
1605 result->range_name = forPortionOf->range_name;
1606 result->location = forPortionOf->location;
1607 result->targetLocation = forPortionOf->target_location;
1608
1609 return result;
1610}
#define OidIsValid(objectId)
Definition c.h:858
bool contain_volatile_functions_after_planning(Expr *expr)
Definition clauses.c:670
@ 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:3629
RegProcedure get_range_constructor2(Oid rangeOid)
Definition lsyscache.c:3680
bool type_is_range(Oid typid)
Definition lsyscache.c:2910
bool get_opclass_opfamily_and_input_type(Oid opclass, Oid *opfamily, Oid *opcintype)
Definition lsyscache.c:1407
RegProcedure get_opcode(Oid opno)
Definition lsyscache.c:1505
Oid getBaseType(Oid typid)
Definition lsyscache.c:2743
bool type_is_multirange(Oid typid)
Definition lsyscache.c:2920
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:769
@ COERCE_EXPLICIT_CALL
Definition primnodes.h:767
@ COERCION_IMPLICIT
Definition primnodes.h:747
#define RelationGetRelationName(relation)
Definition rel.h:550
uint16 StrategyNumber
Definition stratnum.h:22
Oid opno
Definition primnodes.h:851
List * args
Definition primnodes.h:869
Relation p_target_relation
Definition parse_node.h:228
ParseLoc location
Definition primnodes.h:311

References OpExpr::args, attnameAttNum(), can_coerce_type(), COERCE_EXPLICIT_CALL, COERCE_IMPLICIT_CAST, coerce_type(), COERCION_IMPLICIT, COMPARE_OVERLAP, contain_volatile_functions_after_planning(), copyObject, elog, ereport, errcode(), errhint(), errmsg, ERROR, EXPR_KIND_FOR_PORTION, exprLocation(), exprType(), fb(), format_type_be(), get_opclass_opfamily_and_input_type(), get_opcode(), get_range_constructor2(), get_range_subtype(), getBaseType(), GetDefaultOpClass(), GetOperatorFromCompareType(), InvalidAttrNumber, InvalidOid, lappend(), list_make2, ForPortionOfClause::location, Var::location, make_fn_arguments(), makeFuncExpr(), makeNode, makeTargetEntry(), makeVar(), markVarForSelectPriv(), NIL, OidIsValid, OpExpr::opno, 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 1094 of file analyze.c.

1097{
1098 List *result;
1099 ListCell *lc;
1100 ListCell *icols;
1102
1103 /*
1104 * Check length of expr list. It must not have more expressions than
1105 * there are target columns. We allow fewer, but only if no explicit
1106 * columns list was given (the remaining columns are implicitly
1107 * defaulted). Note we must check this *after* transformation because
1108 * that could expand '*' into multiple items.
1109 */
1111 ereport(ERROR,
1113 errmsg("INSERT has more expressions than target columns"),
1114 parser_errposition(pstate,
1116 list_length(icolumns))))));
1117 if (stmtcols != NIL &&
1119 {
1120 /*
1121 * We can get here for cases like INSERT ... SELECT (a,b,c) FROM ...
1122 * where the user accidentally created a RowExpr instead of separate
1123 * columns. Add a suitable hint if that seems to be the problem,
1124 * because the main error message is quite misleading for this case.
1125 * (If there's no stmtcols, you'll get something about data type
1126 * mismatch, which is less misleading so we don't worry about giving a
1127 * hint in that case.)
1128 */
1129 ereport(ERROR,
1131 errmsg("INSERT has more target columns than expressions"),
1132 ((list_length(exprlist) == 1 &&
1135 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),
1136 parser_errposition(pstate,
1138 list_length(exprlist))))));
1139 }
1140
1141 /*
1142 * Prepare columns for assignment to target table.
1143 */
1144 result = NIL;
1146 {
1147 Expr *expr = (Expr *) lfirst(lc);
1149 int attno = lfirst_int(attnos);
1150
1151 expr = transformAssignedExpr(pstate, expr,
1153 col->name,
1154 attno,
1155 col->indirection,
1156 col->location);
1157
1159 {
1160 /*
1161 * We need to remove top-level FieldStores and SubscriptingRefs,
1162 * as well as any CoerceToDomain appearing above one of those ---
1163 * but not a CoerceToDomain that isn't above one of those.
1164 */
1165 while (expr)
1166 {
1167 Expr *subexpr = expr;
1168
1169 while (IsA(subexpr, CoerceToDomain))
1170 {
1171 subexpr = ((CoerceToDomain *) subexpr)->arg;
1172 }
1173 if (IsA(subexpr, FieldStore))
1174 {
1175 FieldStore *fstore = (FieldStore *) subexpr;
1176
1177 expr = (Expr *) linitial(fstore->newvals);
1178 }
1179 else if (IsA(subexpr, SubscriptingRef))
1180 {
1181 SubscriptingRef *sbsref = (SubscriptingRef *) subexpr;
1182
1183 if (sbsref->refassgnexpr == NULL)
1184 break;
1185
1186 expr = sbsref->refassgnexpr;
1187 }
1188 else
1189 break;
1190 }
1191 }
1192
1193 result = lappend(result, expr);
1194 }
1195
1196 return result;
1197}
@ 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:1690
#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:1195
Expr * refassgnexpr
Definition primnodes.h:736

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 653 of file analyze.c.

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

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

1206{
1208 List *arbiterElems;
1209 Node *arbiterWhere;
1211 List *onConflictSet = NIL;
1212 Node *onConflictWhere = NULL;
1213 int exclRelIndex = 0;
1214 List *exclRelTlist = NIL;
1216
1217 /*
1218 * If this is ON CONFLICT DO SELECT/UPDATE, first create the range table
1219 * entry for the EXCLUDED pseudo relation, so that that will be present
1220 * while processing arbiter expressions. (You can't actually reference it
1221 * from there, but this provides a useful error message if you try.)
1222 */
1223 if (onConflictClause->action == ONCONFLICT_UPDATE ||
1224 onConflictClause->action == ONCONFLICT_SELECT)
1225 {
1228
1230 targetrel,
1232 makeAlias("excluded", NIL),
1233 false, false);
1234 exclRte = exclNSItem->p_rte;
1235 exclRelIndex = exclNSItem->p_rtindex;
1236
1237 /*
1238 * relkind is set to composite to signal that we're not dealing with
1239 * an actual relation, and no permission checks are required on it.
1240 * (We'll check the actual target relation, instead.)
1241 */
1243
1244 /* Create EXCLUDED rel's targetlist for use by EXPLAIN */
1246 exclRelIndex);
1247 }
1248
1249 /* Process the arbiter clause, ON CONFLICT ON (...) */
1250 transformOnConflictArbiter(pstate, onConflictClause, &arbiterElems,
1251 &arbiterWhere, &arbiterConstraint);
1252
1253 /* Process DO SELECT/UPDATE */
1254 if (onConflictClause->action == ONCONFLICT_UPDATE ||
1255 onConflictClause->action == ONCONFLICT_SELECT)
1256 {
1257 /*
1258 * Add the EXCLUDED pseudo relation to the query namespace, making it
1259 * available in SET and WHERE subexpressions.
1260 */
1261 addNSItemToQuery(pstate, exclNSItem, false, true, true);
1262
1263 /* Process the UPDATE SET clause */
1264 if (onConflictClause->action == ONCONFLICT_UPDATE)
1265 onConflictSet =
1266 transformUpdateTargetList(pstate, onConflictClause->targetList, NULL);
1267
1268 /* Process the SELECT/UPDATE WHERE clause */
1269 onConflictWhere = transformWhereClause(pstate,
1270 onConflictClause->whereClause,
1271 EXPR_KIND_WHERE, "WHERE");
1272
1273 /*
1274 * Remove the EXCLUDED pseudo relation from the query namespace, since
1275 * it's not supposed to be available in RETURNING. (Maybe someday we
1276 * could allow that, and drop this step.)
1277 */
1279 pstate->p_namespace = list_delete_last(pstate->p_namespace);
1280 }
1281
1282 /* Finally, build ON CONFLICT DO [NOTHING | SELECT | UPDATE] expression */
1284
1285 result->action = onConflictClause->action;
1286 result->arbiterElems = arbiterElems;
1287 result->arbiterWhere = arbiterWhere;
1288 result->constraint = arbiterConstraint;
1289 result->lockStrength = onConflictClause->lockStrength;
1290 result->onConflictSet = onConflictSet;
1291 result->onConflictWhere = onConflictWhere;
1292 result->exclRelIndex = exclRelIndex;
1293 result->exclRelTlist = exclRelTlist;
1294
1295 return result;
1296}
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:2923
List * BuildOnConflictExcludedTargetlist(Relation targetrel, Index exclRelIndex)
Definition analyze.c:1620
#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 295 of file analyze.c.

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

3174{
3175 Query *qry;
3177 List *indirection = stmt->indirection;
3178 int nnames = stmt->nnames;
3179 Node *target;
3182
3183 /*
3184 * First, construct a ColumnRef for the target variable. If the target
3185 * has more than one dotted name, we have to pull the extra names out of
3186 * the indirection list.
3187 */
3188 cref->fields = list_make1(makeString(stmt->name));
3189 cref->location = stmt->location;
3190 if (nnames > 1)
3191 {
3192 /* avoid munging the raw parsetree */
3193 indirection = list_copy(indirection);
3194 while (--nnames > 0 && indirection != NIL)
3195 {
3196 Node *ind = (Node *) linitial(indirection);
3197
3198 if (!IsA(ind, String))
3199 elog(ERROR, "invalid name count in PLAssignStmt");
3200 cref->fields = lappend(cref->fields, ind);
3201 indirection = list_delete_first(indirection);
3202 }
3203 }
3204
3205 /*
3206 * Transform the target reference. Typically we will get back a Param
3207 * node, but there's no reason to be too picky about its type. (Note that
3208 * we must do this before calling transformSelectStmt. It's tempting to
3209 * do it inside transformPLAssignStmtTarget, but we need to do it before
3210 * adding any FROM tables to the pstate's namespace, else we might wrongly
3211 * resolve the target as a table column.)
3212 */
3213 target = transformExpr(pstate, (Node *) cref,
3215
3216 /* Set up passthrough data for transformPLAssignStmtTarget */
3217 passthru.stmt = stmt;
3218 passthru.target = target;
3219 passthru.indirection = indirection;
3220
3221 /*
3222 * To avoid duplicating a lot of code, we use transformSelectStmt to do
3223 * almost all of the work. However, we need to do additional processing
3224 * on the SELECT's targetlist after it's been transformed, but before
3225 * possible addition of targetlist items for ORDER BY or GROUP BY.
3226 * transformSelectStmt knows it should call transformPLAssignStmtTarget if
3227 * it's passed a passthru argument.
3228 *
3229 * Also, disable resolution of unknown-type tlist items; PL/pgSQL wants to
3230 * deal with that itself.
3231 */
3233 pstate->p_resolve_unknowns = false;
3234 qry = transformSelectStmt(pstate, stmt->val, &passthru);
3236
3237 return qry;
3238}
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:1739
#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 3248 of file analyze.c.

3250{
3251 PLAssignStmt *stmt = passthru->stmt;
3252 Node *target = passthru->target;
3253 List *indirection = passthru->indirection;
3254 Oid targettype;
3255 int32 targettypmod;
3258 Oid type_id;
3259
3260 targettype = exprType(target);
3261 targettypmod = exprTypmod(target);
3263
3264 /* we should have exactly one targetlist item */
3265 if (list_length(tlist) != 1)
3266 ereport(ERROR,
3268 errmsg_plural("assignment source returned %d column",
3269 "assignment source returned %d columns",
3270 list_length(tlist),
3271 list_length(tlist))));
3272
3273 tle = linitial_node(TargetEntry, tlist);
3274
3275 /*
3276 * This next bit is similar to transformAssignedExpr; the key difference
3277 * is we use COERCION_PLPGSQL not COERCION_ASSIGNMENT.
3278 */
3279 type_id = exprType((Node *) tle->expr);
3280
3282
3283 if (indirection)
3284 {
3285 tle->expr = (Expr *)
3287 target,
3288 stmt->name,
3289 false,
3290 targettype,
3291 targettypmod,
3293 indirection,
3294 list_head(indirection),
3295 (Node *) tle->expr,
3297 exprLocation(target));
3298 }
3299 else if (targettype != type_id &&
3300 (targettype == RECORDOID || ISCOMPLEX(targettype)) &&
3301 (type_id == RECORDOID || ISCOMPLEX(type_id)))
3302 {
3303 /*
3304 * Hack: do not let coerce_to_target_type() deal with inconsistent
3305 * composite types. Just pass the expression result through as-is,
3306 * and let the PL/pgSQL executor do the conversion its way. This is
3307 * rather bogus, but it's needed for backwards compatibility.
3308 */
3309 }
3310 else
3311 {
3312 /*
3313 * For normal non-qualified target column, do type checking and
3314 * coercion.
3315 */
3316 Node *orig_expr = (Node *) tle->expr;
3317
3318 tle->expr = (Expr *)
3319 coerce_to_target_type(pstate,
3320 orig_expr, type_id,
3321 targettype, targettypmod,
3324 -1);
3325 /* With COERCION_PLPGSQL, this error is probably unreachable */
3326 if (tle->expr == NULL)
3327 ereport(ERROR,
3329 errmsg("variable \"%s\" is of type %s"
3330 " but expression is of type %s",
3331 stmt->name,
3332 format_type_be(targettype),
3333 format_type_be(type_id)),
3334 errhint("You will need to rewrite or cast the expression."),
3336 }
3337
3338 pstate->p_expr_kind = EXPR_KIND_NONE;
3339
3340 return list_make1(tle);
3341}
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:749
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 3051 of file analyze.c.

3054{
3055 int save_nslen = list_length(pstate->p_namespace);
3056 int save_next_resno;
3057
3058 if (returningClause == NULL)
3059 return; /* nothing to do */
3060
3061 /*
3062 * Scan RETURNING WITH(...) options for OLD/NEW alias names. Complain if
3063 * there is any conflict with existing relations.
3064 */
3065 foreach_node(ReturningOption, option, returningClause->options)
3066 {
3067 switch (option->option)
3068 {
3070 if (qry->returningOldAlias != NULL)
3071 ereport(ERROR,
3073 /* translator: %s is OLD or NEW */
3074 errmsg("%s cannot be specified multiple times", "OLD"),
3075 parser_errposition(pstate, option->location));
3076 qry->returningOldAlias = option->value;
3077 break;
3078
3080 if (qry->returningNewAlias != NULL)
3081 ereport(ERROR,
3083 /* translator: %s is OLD or NEW */
3084 errmsg("%s cannot be specified multiple times", "NEW"),
3085 parser_errposition(pstate, option->location));
3086 qry->returningNewAlias = option->value;
3087 break;
3088
3089 default:
3090 elog(ERROR, "unrecognized returning option: %d", option->option);
3091 }
3092
3093 if (refnameNamespaceItem(pstate, NULL, option->value, -1, NULL) != NULL)
3094 ereport(ERROR,
3096 errmsg("table name \"%s\" specified more than once",
3097 option->value),
3098 parser_errposition(pstate, option->location));
3099
3100 addNSItemForReturning(pstate, option->value,
3101 option->option == RETURNING_OPTION_OLD ?
3103 }
3104
3105 /*
3106 * If OLD/NEW alias names weren't explicitly specified, use "old"/"new"
3107 * unless masked by existing relations.
3108 */
3109 if (qry->returningOldAlias == NULL &&
3110 refnameNamespaceItem(pstate, NULL, "old", -1, NULL) == NULL)
3111 {
3112 qry->returningOldAlias = "old";
3114 }
3115 if (qry->returningNewAlias == NULL &&
3116 refnameNamespaceItem(pstate, NULL, "new", -1, NULL) == NULL)
3117 {
3118 qry->returningNewAlias = "new";
3120 }
3121
3122 /*
3123 * We need to assign resnos starting at one in the RETURNING list. Save
3124 * and restore the main tlist's value of p_next_resno, just in case
3125 * someone looks at it later (probably won't happen).
3126 */
3127 save_next_resno = pstate->p_next_resno;
3128 pstate->p_next_resno = 1;
3129
3130 /* transform RETURNING expressions identically to a SELECT targetlist */
3131 qry->returningList = transformTargetList(pstate,
3132 returningClause->exprs,
3133 exprKind);
3134
3135 /*
3136 * Complain if the nonempty tlist expanded to nothing (which is possible
3137 * if it contains only a star-expansion of a zero-column table). If we
3138 * allow this, the parsed Query will look like it didn't have RETURNING,
3139 * with results that would probably surprise the user.
3140 */
3141 if (qry->returningList == NIL)
3142 ereport(ERROR,
3144 errmsg("RETURNING must have at least one column"),
3145 parser_errposition(pstate,
3146 exprLocation(linitial(returningClause->exprs)))));
3147
3148 /* mark column origins */
3150
3151 /* resolve any still-unresolved output columns as being type text */
3152 if (pstate->p_resolve_unknowns)
3154
3155 /* restore state */
3156 pstate->p_namespace = list_truncate(pstate->p_namespace, save_nslen);
3157 pstate->p_next_resno = save_next_resno;
3158}
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:3012
#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:217

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 2819 of file analyze.c.

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

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 1739 of file analyze.c.

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

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 2112 of file analyze.c.

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

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

335{
336 Query *result;
337
338#ifdef DEBUG_NODE_TESTS_ENABLED
339
340 /*
341 * We apply debug_raw_expression_coverage_test testing to basic DML
342 * statements; we can't just run it on everything because
343 * raw_expression_tree_walker() doesn't claim to handle utility
344 * statements.
345 */
347 {
348 switch (nodeTag(parseTree))
349 {
350 case T_SelectStmt:
351 case T_InsertStmt:
352 case T_UpdateStmt:
353 case T_DeleteStmt:
354 case T_MergeStmt:
356 break;
357 default:
358 break;
359 }
360 }
361#endif /* DEBUG_NODE_TESTS_ENABLED */
362
363 /*
364 * Caution: when changing the set of statement types that have non-default
365 * processing here, see also stmt_requires_parse_analysis() and
366 * analyze_requires_snapshot().
367 */
368 switch (nodeTag(parseTree))
369 {
370 /*
371 * Optimizable statements
372 */
373 case T_InsertStmt:
375 break;
376
377 case T_DeleteStmt:
379 break;
380
381 case T_UpdateStmt:
383 break;
384
385 case T_MergeStmt:
387 break;
388
389 case T_SelectStmt:
390 {
392
393 if (n->valuesLists)
394 result = transformValuesClause(pstate, n);
395 else if (n->op == SETOP_NONE)
396 result = transformSelectStmt(pstate, n, NULL);
397 else
399 }
400 break;
401
402 case T_ReturnStmt:
404 break;
405
406 case T_PLAssignStmt:
409 break;
410
411 /*
412 * Special cases
413 */
417 break;
418
419 case T_ExplainStmt:
422 break;
423
427 break;
428
429 case T_CallStmt:
430 result = transformCallStmt(pstate,
431 (CallStmt *) parseTree);
432 break;
433
434 default:
435
436 /*
437 * other statements don't require any transformation; just return
438 * the original parsetree with a Query node plastered on top.
439 */
441 result->commandType = CMD_UTILITY;
442 result->utilityStmt = parseTree;
443 break;
444 }
445
446 /* Mark as original query until we learn differently */
447 result->querySource = QSRC_ORIGINAL;
448 result->canSetTag = true;
449
450 return result;
451}
Query * transformMergeStmt(ParseState *pstate, MergeStmt *stmt)
@ QSRC_ORIGINAL
Definition parsenodes.h:36
static Query * transformDeleteStmt(ParseState *pstate, DeleteStmt *stmt)
Definition analyze.c:575
static Query * transformReturnStmt(ParseState *pstate, ReturnStmt *stmt)
Definition analyze.c:2819
static Query * transformPLAssignStmt(ParseState *pstate, PLAssignStmt *stmt)
Definition analyze.c:3173
static Query * transformCreateTableAsStmt(ParseState *pstate, CreateTableAsStmt *stmt)
Definition analyze.c:3500
static Query * transformCallStmt(ParseState *pstate, CallStmt *stmt)
Definition analyze.c:3579
static Query * transformSetOperationStmt(ParseState *pstate, SelectStmt *stmt)
Definition analyze.c:2112
static Query * transformUpdateStmt(ParseState *pstate, UpdateStmt *stmt)
Definition analyze.c:2850
static Query * transformExplainStmt(ParseState *pstate, ExplainStmt *stmt)
Definition analyze.c:3448
static Query * transformDeclareCursorStmt(ParseState *pstate, DeclareCursorStmt *stmt)
Definition analyze.c:3355
static Query * transformInsertStmt(ParseState *pstate, InsertStmt *stmt)
Definition analyze.c:653
static Query * transformValuesClause(ParseState *pstate, SelectStmt *stmt)
Definition analyze.c:1893
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 271 of file analyze.c.

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

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 2850 of file analyze.c.

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

References ACL_UPDATE, assign_query_collations(), CMD_UPDATE, Query::commandType, Query::cteList, EXPR_KIND_RETURNING, EXPR_KIND_WHERE, fb(), Query::forPortionOf, 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, 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 2923 of file analyze.c.

2924{
2925 List *tlist = NIL;
2928 ListCell *tl;
2929
2930 tlist = transformTargetList(pstate, origTlist,
2932
2933 /* Prepare to assign non-conflicting resnos to resjunk attributes */
2936
2937 /* Prepare non-junk columns for assignment to target table */
2940
2941 foreach(tl, tlist)
2942 {
2945 int attrno;
2946
2947 if (tle->resjunk)
2948 {
2949 /*
2950 * Resjunk nodes need no additional processing, but be sure they
2951 * have resnos that do not match any target columns; else rewriter
2952 * or planner might get confused. They don't need a resname
2953 * either.
2954 */
2955 tle->resno = (AttrNumber) pstate->p_next_resno++;
2956 tle->resname = NULL;
2957 continue;
2958 }
2959 if (orig_tl == NULL)
2960 elog(ERROR, "UPDATE target count mismatch --- internal error");
2962
2964 origTarget->name, true);
2966 ereport(ERROR,
2968 errmsg("column \"%s\" of relation \"%s\" does not exist",
2969 origTarget->name,
2971 (origTarget->indirection != NIL &&
2972 strcmp(origTarget->name, pstate->p_target_nsitem->p_names->aliasname) == 0) ?
2973 errhint("SET target columns cannot be qualified with the relation name.") : 0,
2974 parser_errposition(pstate, origTarget->location)));
2975
2976 /*
2977 * If this is a FOR PORTION OF update, forbid directly setting the
2978 * range column, since that would conflict with the implicit updates.
2979 */
2980 if (forPortionOf != NULL)
2981 {
2982 if (attrno == forPortionOf->rangeVar->varattno)
2983 ereport(ERROR,
2985 errmsg("cannot update column \"%s\" because it is used in FOR PORTION OF",
2986 origTarget->name),
2987 parser_errposition(pstate, origTarget->location)));
2988 }
2989
2990 updateTargetListEntry(pstate, tle, origTarget->name,
2991 attrno,
2992 origTarget->indirection,
2993 origTarget->location);
2994
2995 /* Mark the target column as requiring update permissions */
2996 target_perminfo->updatedCols = bms_add_member(target_perminfo->updatedCols,
2998
3000 }
3001 if (orig_tl != NULL)
3002 elog(ERROR, "UPDATE target count mismatch --- internal error");
3003
3004 return tlist;
3005}
@ 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 1893 of file analyze.c.

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