Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 5815696

Browse files
committed
Make parser rely more heavily on the ParseNamespaceItem data structure.
When I added the ParseNamespaceItem data structure (in commit 5ebaaa4), it wasn't very tightly integrated into the parser's APIs. In the wake of adding p_rtindex to that struct (commit b541e9a), there is a good reason to make more use of it: by passing around ParseNamespaceItem pointers instead of bare RTE pointers, we can get rid of various messy methods for passing back or deducing the rangetable index of an RTE during parsing. Hence, refactor the addRangeTableEntryXXX functions to build and return a ParseNamespaceItem struct, not just the RTE proper; and replace addRTEtoQuery with addNSItemToQuery, which is passed a ParseNamespaceItem rather than building one internally. Also, add per-column data (a ParseNamespaceColumn array) to each ParseNamespaceItem. These arrays are built during addRangeTableEntryXXX, where we have column type data at hand so that it's nearly free to fill the data structure. Later, when we need to build Vars referencing RTEs, we can use the ParseNamespaceColumn info to avoid the rather expensive operations done in get_rte_attribute_type() or expandRTE(). get_rte_attribute_type() is indeed dead code now, so I've removed it. This makes for a useful improvement in parse analysis speed, around 20% in one moderately-complex test query. The ParseNamespaceColumn structs also include Var identity information (varno/varattno). That info isn't actually being used in this patch, except that p_varno == 0 is a handy test for a dropped column. A follow-on patch will make more use of it. Discussion: https://postgr.es/m/2461.1577764221@sss.pgh.pa.us
1 parent 198c715 commit 5815696

File tree

20 files changed

+927
-794
lines changed

20 files changed

+927
-794
lines changed

src/backend/catalog/heap.c

+8-8
Original file line numberDiff line numberDiff line change
@@ -2531,7 +2531,7 @@ AddRelationNewConstraints(Relation rel,
25312531
TupleConstr *oldconstr;
25322532
int numoldchecks;
25332533
ParseState *pstate;
2534-
RangeTblEntry *rte;
2534+
ParseNamespaceItem *nsitem;
25352535
int numchecks;
25362536
List *checknames;
25372537
ListCell *cell;
@@ -2554,13 +2554,13 @@ AddRelationNewConstraints(Relation rel,
25542554
*/
25552555
pstate = make_parsestate(NULL);
25562556
pstate->p_sourcetext = queryString;
2557-
rte = addRangeTableEntryForRelation(pstate,
2558-
rel,
2559-
AccessShareLock,
2560-
NULL,
2561-
false,
2562-
true);
2563-
addRTEtoQuery(pstate, rte, true, true, true);
2557+
nsitem = addRangeTableEntryForRelation(pstate,
2558+
rel,
2559+
AccessShareLock,
2560+
NULL,
2561+
false,
2562+
true);
2563+
addNSItemToQuery(pstate, nsitem, true, true, true);
25642564

25652565
/*
25662566
* Process column default expressions.

src/backend/commands/copy.c

+6-4
Original file line numberDiff line numberDiff line change
@@ -882,6 +882,7 @@ DoCopy(ParseState *pstate, const CopyStmt *stmt,
882882
if (stmt->relation)
883883
{
884884
LOCKMODE lockmode = is_from ? RowExclusiveLock : AccessShareLock;
885+
ParseNamespaceItem *nsitem;
885886
RangeTblEntry *rte;
886887
TupleDesc tupDesc;
887888
List *attnums;
@@ -894,14 +895,15 @@ DoCopy(ParseState *pstate, const CopyStmt *stmt,
894895

895896
relid = RelationGetRelid(rel);
896897

897-
rte = addRangeTableEntryForRelation(pstate, rel, lockmode,
898-
NULL, false, false);
898+
nsitem = addRangeTableEntryForRelation(pstate, rel, lockmode,
899+
NULL, false, false);
900+
rte = nsitem->p_rte;
899901
rte->requiredPerms = (is_from ? ACL_INSERT : ACL_SELECT);
900902

901903
if (stmt->whereClause)
902904
{
903-
/* add rte to column namespace */
904-
addRTEtoQuery(pstate, rte, false, true, true);
905+
/* add nsitem to query namespace */
906+
addNSItemToQuery(pstate, nsitem, false, true, true);
905907

906908
/* Transform the raw expression tree */
907909
whereClause = transformExpr(pstate, stmt->whereClause, EXPR_KIND_COPY_WHERE);

src/backend/commands/policy.c

+32-31
Original file line numberDiff line numberDiff line change
@@ -568,9 +568,9 @@ RemoveRoleFromObjectPolicy(Oid roleid, Oid classid, Oid policy_id)
568568
qual_expr = stringToNode(qual_value);
569569

570570
/* Add this rel to the parsestate's rangetable, for dependencies */
571-
addRangeTableEntryForRelation(qual_pstate, rel,
572-
AccessShareLock,
573-
NULL, false, false);
571+
(void) addRangeTableEntryForRelation(qual_pstate, rel,
572+
AccessShareLock,
573+
NULL, false, false);
574574

575575
qual_parse_rtable = qual_pstate->p_rtable;
576576
free_parsestate(qual_pstate);
@@ -592,9 +592,9 @@ RemoveRoleFromObjectPolicy(Oid roleid, Oid classid, Oid policy_id)
592592
with_check_qual = stringToNode(with_check_value);
593593

594594
/* Add this rel to the parsestate's rangetable, for dependencies */
595-
addRangeTableEntryForRelation(with_check_pstate, rel,
596-
AccessShareLock,
597-
NULL, false, false);
595+
(void) addRangeTableEntryForRelation(with_check_pstate, rel,
596+
AccessShareLock,
597+
NULL, false, false);
598598

599599
with_check_parse_rtable = with_check_pstate->p_rtable;
600600
free_parsestate(with_check_pstate);
@@ -699,7 +699,7 @@ CreatePolicy(CreatePolicyStmt *stmt)
699699
ArrayType *role_ids;
700700
ParseState *qual_pstate;
701701
ParseState *with_check_pstate;
702-
RangeTblEntry *rte;
702+
ParseNamespaceItem *nsitem;
703703
Node *qual;
704704
Node *with_check_qual;
705705
ScanKeyData skey[2];
@@ -755,16 +755,16 @@ CreatePolicy(CreatePolicyStmt *stmt)
755755
target_table = relation_open(table_id, NoLock);
756756

757757
/* Add for the regular security quals */
758-
rte = addRangeTableEntryForRelation(qual_pstate, target_table,
759-
AccessShareLock,
760-
NULL, false, false);
761-
addRTEtoQuery(qual_pstate, rte, false, true, true);
758+
nsitem = addRangeTableEntryForRelation(qual_pstate, target_table,
759+
AccessShareLock,
760+
NULL, false, false);
761+
addNSItemToQuery(qual_pstate, nsitem, false, true, true);
762762

763763
/* Add for the with-check quals */
764-
rte = addRangeTableEntryForRelation(with_check_pstate, target_table,
765-
AccessShareLock,
766-
NULL, false, false);
767-
addRTEtoQuery(with_check_pstate, rte, false, true, true);
764+
nsitem = addRangeTableEntryForRelation(with_check_pstate, target_table,
765+
AccessShareLock,
766+
NULL, false, false);
767+
addNSItemToQuery(with_check_pstate, nsitem, false, true, true);
768768

769769
qual = transformWhereClause(qual_pstate,
770770
copyObject(stmt->qual),
@@ -933,14 +933,14 @@ AlterPolicy(AlterPolicyStmt *stmt)
933933
/* Parse the using policy clause */
934934
if (stmt->qual)
935935
{
936-
RangeTblEntry *rte;
936+
ParseNamespaceItem *nsitem;
937937
ParseState *qual_pstate = make_parsestate(NULL);
938938

939-
rte = addRangeTableEntryForRelation(qual_pstate, target_table,
940-
AccessShareLock,
941-
NULL, false, false);
939+
nsitem = addRangeTableEntryForRelation(qual_pstate, target_table,
940+
AccessShareLock,
941+
NULL, false, false);
942942

943-
addRTEtoQuery(qual_pstate, rte, false, true, true);
943+
addNSItemToQuery(qual_pstate, nsitem, false, true, true);
944944

945945
qual = transformWhereClause(qual_pstate, copyObject(stmt->qual),
946946
EXPR_KIND_POLICY,
@@ -956,14 +956,14 @@ AlterPolicy(AlterPolicyStmt *stmt)
956956
/* Parse the with-check policy clause */
957957
if (stmt->with_check)
958958
{
959-
RangeTblEntry *rte;
959+
ParseNamespaceItem *nsitem;
960960
ParseState *with_check_pstate = make_parsestate(NULL);
961961

962-
rte = addRangeTableEntryForRelation(with_check_pstate, target_table,
963-
AccessShareLock,
964-
NULL, false, false);
962+
nsitem = addRangeTableEntryForRelation(with_check_pstate, target_table,
963+
AccessShareLock,
964+
NULL, false, false);
965965

966-
addRTEtoQuery(with_check_pstate, rte, false, true, true);
966+
addNSItemToQuery(with_check_pstate, nsitem, false, true, true);
967967

968968
with_check_qual = transformWhereClause(with_check_pstate,
969969
copyObject(stmt->with_check),
@@ -1107,9 +1107,9 @@ AlterPolicy(AlterPolicyStmt *stmt)
11071107
qual = stringToNode(qual_value);
11081108

11091109
/* Add this rel to the parsestate's rangetable, for dependencies */
1110-
addRangeTableEntryForRelation(qual_pstate, target_table,
1111-
AccessShareLock,
1112-
NULL, false, false);
1110+
(void) addRangeTableEntryForRelation(qual_pstate, target_table,
1111+
AccessShareLock,
1112+
NULL, false, false);
11131113

11141114
qual_parse_rtable = qual_pstate->p_rtable;
11151115
free_parsestate(qual_pstate);
@@ -1149,9 +1149,10 @@ AlterPolicy(AlterPolicyStmt *stmt)
11491149
with_check_qual = stringToNode(with_check_value);
11501150

11511151
/* Add this rel to the parsestate's rangetable, for dependencies */
1152-
addRangeTableEntryForRelation(with_check_pstate, target_table,
1153-
AccessShareLock,
1154-
NULL, false, false);
1152+
(void) addRangeTableEntryForRelation(with_check_pstate,
1153+
target_table,
1154+
AccessShareLock,
1155+
NULL, false, false);
11551156

11561157
with_check_parse_rtable = with_check_pstate->p_rtable;
11571158
free_parsestate(with_check_pstate);

src/backend/commands/tablecmds.c

+12-11
Original file line numberDiff line numberDiff line change
@@ -918,7 +918,7 @@ DefineRelation(CreateStmt *stmt, char relkind, Oid ownerId,
918918
defaultPartOid;
919919
Relation parent,
920920
defaultRel = NULL;
921-
RangeTblEntry *rte;
921+
ParseNamespaceItem *nsitem;
922922

923923
/* Already have strong enough lock on the parent */
924924
parent = table_open(parentId, NoLock);
@@ -962,13 +962,14 @@ DefineRelation(CreateStmt *stmt, char relkind, Oid ownerId,
962962
pstate->p_sourcetext = queryString;
963963

964964
/*
965-
* Add an RTE containing this relation, so that transformExpr called
966-
* on partition bound expressions is able to report errors using a
967-
* proper context.
965+
* Add an nsitem containing this relation, so that transformExpr
966+
* called on partition bound expressions is able to report errors
967+
* using a proper context.
968968
*/
969-
rte = addRangeTableEntryForRelation(pstate, rel, AccessShareLock,
970-
NULL, false, false);
971-
addRTEtoQuery(pstate, rte, false, true, true);
969+
nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock,
970+
NULL, false, false);
971+
addNSItemToQuery(pstate, nsitem, false, true, true);
972+
972973
bound = transformPartitionBound(pstate, parent, stmt->partbound);
973974

974975
/*
@@ -14970,7 +14971,7 @@ transformPartitionSpec(Relation rel, PartitionSpec *partspec, char *strategy)
1497014971
{
1497114972
PartitionSpec *newspec;
1497214973
ParseState *pstate;
14973-
RangeTblEntry *rte;
14974+
ParseNamespaceItem *nsitem;
1497414975
ListCell *l;
1497514976

1497614977
newspec = makeNode(PartitionSpec);
@@ -15004,9 +15005,9 @@ transformPartitionSpec(Relation rel, PartitionSpec *partspec, char *strategy)
1500415005
* rangetable entry. We need a ParseState for transformExpr.
1500515006
*/
1500615007
pstate = make_parsestate(NULL);
15007-
rte = addRangeTableEntryForRelation(pstate, rel, AccessShareLock,
15008-
NULL, false, true);
15009-
addRTEtoQuery(pstate, rte, true, true, true);
15008+
nsitem = addRangeTableEntryForRelation(pstate, rel, AccessShareLock,
15009+
NULL, false, true);
15010+
addNSItemToQuery(pstate, nsitem, true, true, true);
1501015011

1501115012
/* take care of any partition expressions */
1501215013
foreach(l, partspec->partParams)

src/backend/commands/trigger.c

+12-12
Original file line numberDiff line numberDiff line change
@@ -565,7 +565,7 @@ CreateTrigger(CreateTrigStmt *stmt, const char *queryString,
565565
if (!whenClause && stmt->whenClause)
566566
{
567567
ParseState *pstate;
568-
RangeTblEntry *rte;
568+
ParseNamespaceItem *nsitem;
569569
List *varList;
570570
ListCell *lc;
571571

@@ -574,20 +574,20 @@ CreateTrigger(CreateTrigStmt *stmt, const char *queryString,
574574
pstate->p_sourcetext = queryString;
575575

576576
/*
577-
* Set up RTEs for OLD and NEW references.
577+
* Set up nsitems for OLD and NEW references.
578578
*
579579
* 'OLD' must always have varno equal to 1 and 'NEW' equal to 2.
580580
*/
581-
rte = addRangeTableEntryForRelation(pstate, rel,
582-
AccessShareLock,
583-
makeAlias("old", NIL),
584-
false, false);
585-
addRTEtoQuery(pstate, rte, false, true, true);
586-
rte = addRangeTableEntryForRelation(pstate, rel,
587-
AccessShareLock,
588-
makeAlias("new", NIL),
589-
false, false);
590-
addRTEtoQuery(pstate, rte, false, true, true);
581+
nsitem = addRangeTableEntryForRelation(pstate, rel,
582+
AccessShareLock,
583+
makeAlias("old", NIL),
584+
false, false);
585+
addNSItemToQuery(pstate, nsitem, false, true, true);
586+
nsitem = addRangeTableEntryForRelation(pstate, rel,
587+
AccessShareLock,
588+
makeAlias("new", NIL),
589+
false, false);
590+
addNSItemToQuery(pstate, nsitem, false, true, true);
591591

592592
/* Transform expression. Copy to be sure we don't modify original */
593593
whenClause = transformWhereClause(pstate,

src/backend/commands/view.c

+12-8
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,7 @@ UpdateRangeTableOfViewParse(Oid viewOid, Query *viewParse)
341341
{
342342
Relation viewRel;
343343
List *new_rt;
344+
ParseNamespaceItem *nsitem;
344345
RangeTblEntry *rt_entry1,
345346
*rt_entry2;
346347
ParseState *pstate;
@@ -365,14 +366,17 @@ UpdateRangeTableOfViewParse(Oid viewOid, Query *viewParse)
365366
* Create the 2 new range table entries and form the new range table...
366367
* OLD first, then NEW....
367368
*/
368-
rt_entry1 = addRangeTableEntryForRelation(pstate, viewRel,
369-
AccessShareLock,
370-
makeAlias("old", NIL),
371-
false, false);
372-
rt_entry2 = addRangeTableEntryForRelation(pstate, viewRel,
373-
AccessShareLock,
374-
makeAlias("new", NIL),
375-
false, false);
369+
nsitem = addRangeTableEntryForRelation(pstate, viewRel,
370+
AccessShareLock,
371+
makeAlias("old", NIL),
372+
false, false);
373+
rt_entry1 = nsitem->p_rte;
374+
nsitem = addRangeTableEntryForRelation(pstate, viewRel,
375+
AccessShareLock,
376+
makeAlias("new", NIL),
377+
false, false);
378+
rt_entry2 = nsitem->p_rte;
379+
376380
/* Must override addRangeTableEntry's default access-check flags */
377381
rt_entry1->requiredPerms = 0;
378382
rt_entry2->requiredPerms = 0;

src/backend/optimizer/plan/subselect.c

+7-5
Original file line numberDiff line numberDiff line change
@@ -1217,6 +1217,7 @@ convert_ANY_sublink_to_join(PlannerInfo *root, SubLink *sublink,
12171217
Query *subselect = (Query *) sublink->subselect;
12181218
Relids upper_varnos;
12191219
int rtindex;
1220+
ParseNamespaceItem *nsitem;
12201221
RangeTblEntry *rte;
12211222
RangeTblRef *rtr;
12221223
List *subquery_vars;
@@ -1264,11 +1265,12 @@ convert_ANY_sublink_to_join(PlannerInfo *root, SubLink *sublink,
12641265
* below). Therefore this is a lot easier than what pull_up_subqueries has
12651266
* to go through.
12661267
*/
1267-
rte = addRangeTableEntryForSubquery(pstate,
1268-
subselect,
1269-
makeAlias("ANY_subquery", NIL),
1270-
false,
1271-
false);
1268+
nsitem = addRangeTableEntryForSubquery(pstate,
1269+
subselect,
1270+
makeAlias("ANY_subquery", NIL),
1271+
false,
1272+
false);
1273+
rte = nsitem->p_rte;
12721274
parse->rtable = lappend(parse->rtable, rte);
12731275
rtindex = list_length(parse->rtable);
12741276

0 commit comments

Comments
 (0)