Skip to content

Commit bdca82f

Browse files
committed
Add a relkind field to RangeTblEntry to avoid some syscache lookups.
The recent additions for FDW support required checking foreign-table-ness in several places in the parse/plan chain. While it's not clear whether that would really result in a noticeable slowdown, it seems best to avoid any performance risk by keeping a copy of the relation's relkind in RangeTblEntry. That might have some other uses later, anyway. Per discussion.
1 parent 1c51c7d commit bdca82f

File tree

19 files changed

+85
-74
lines changed

19 files changed

+85
-74
lines changed

src/backend/catalog/dependency.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1284,6 +1284,7 @@ recordDependencyOnSingleRelExpr(const ObjectAddress *depender,
12841284
rte.type = T_RangeTblEntry;
12851285
rte.rtekind = RTE_RELATION;
12861286
rte.relid = relId;
1287+
rte.relkind = RELKIND_RELATION; /* no need for exactness here */
12871288

12881289
context.rtables = list_make1(list_make1(&rte));
12891290

src/backend/commands/copy.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -763,6 +763,7 @@ DoCopy(const CopyStmt *stmt, const char *queryString)
763763
rte = makeNode(RangeTblEntry);
764764
rte->rtekind = RTE_RELATION;
765765
rte->relid = RelationGetRelid(rel);
766+
rte->relkind = rel->rd_rel->relkind;
766767
rte->requiredPerms = required_access;
767768

768769
tupDesc = RelationGetDescr(rel);

src/backend/nodes/copyfuncs.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1927,6 +1927,7 @@ _copyRangeTblEntry(RangeTblEntry *from)
19271927

19281928
COPY_SCALAR_FIELD(rtekind);
19291929
COPY_SCALAR_FIELD(relid);
1930+
COPY_SCALAR_FIELD(relkind);
19301931
COPY_NODE_FIELD(subquery);
19311932
COPY_SCALAR_FIELD(jointype);
19321933
COPY_NODE_FIELD(joinaliasvars);

src/backend/nodes/equalfuncs.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2286,6 +2286,7 @@ _equalRangeTblEntry(RangeTblEntry *a, RangeTblEntry *b)
22862286
{
22872287
COMPARE_SCALAR_FIELD(rtekind);
22882288
COMPARE_SCALAR_FIELD(relid);
2289+
COMPARE_SCALAR_FIELD(relkind);
22892290
COMPARE_NODE_FIELD(subquery);
22902291
COMPARE_SCALAR_FIELD(jointype);
22912292
COMPARE_NODE_FIELD(joinaliasvars);

src/backend/nodes/nodeFuncs.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1671,7 +1671,6 @@ range_table_walker(List *rtable,
16711671
switch (rte->rtekind)
16721672
{
16731673
case RTE_RELATION:
1674-
case RTE_SPECIAL:
16751674
case RTE_CTE:
16761675
/* nothing to do */
16771676
break;
@@ -2374,7 +2373,6 @@ range_table_mutator(List *rtable,
23742373
switch (rte->rtekind)
23752374
{
23762375
case RTE_RELATION:
2377-
case RTE_SPECIAL:
23782376
case RTE_CTE:
23792377
/* we don't bother to copy eref, aliases, etc; OK? */
23802378
break;

src/backend/nodes/outfuncs.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2275,8 +2275,8 @@ _outRangeTblEntry(StringInfo str, RangeTblEntry *node)
22752275
switch (node->rtekind)
22762276
{
22772277
case RTE_RELATION:
2278-
case RTE_SPECIAL:
22792278
WRITE_OID_FIELD(relid);
2279+
WRITE_CHAR_FIELD(relkind);
22802280
break;
22812281
case RTE_SUBQUERY:
22822282
WRITE_NODE_FIELD(subquery);

src/backend/nodes/print.c

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -265,8 +265,8 @@ print_rt(List *rtable)
265265
switch (rte->rtekind)
266266
{
267267
case RTE_RELATION:
268-
printf("%d\t%s\t%u",
269-
i, rte->eref->aliasname, rte->relid);
268+
printf("%d\t%s\t%u\t%c",
269+
i, rte->eref->aliasname, rte->relid, rte->relkind);
270270
break;
271271
case RTE_SUBQUERY:
272272
printf("%d\t%s\t[subquery]",
@@ -276,10 +276,6 @@ print_rt(List *rtable)
276276
printf("%d\t%s\t[join]",
277277
i, rte->eref->aliasname);
278278
break;
279-
case RTE_SPECIAL:
280-
printf("%d\t%s\t[special]",
281-
i, rte->eref->aliasname);
282-
break;
283279
case RTE_FUNCTION:
284280
printf("%d\t%s\t[rangefunction]",
285281
i, rte->eref->aliasname);

src/backend/nodes/readfuncs.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1171,8 +1171,8 @@ _readRangeTblEntry(void)
11711171
switch (local_node->rtekind)
11721172
{
11731173
case RTE_RELATION:
1174-
case RTE_SPECIAL:
11751174
READ_OID_FIELD(relid);
1175+
READ_CHAR_FIELD(relkind);
11761176
break;
11771177
case RTE_SUBQUERY:
11781178
READ_NODE_FIELD(subquery);

src/backend/optimizer/path/allpaths.c

Lines changed: 35 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -176,41 +176,44 @@ set_rel_pathlist(PlannerInfo *root, RelOptInfo *rel,
176176
/* It's an "append relation", process accordingly */
177177
set_append_rel_pathlist(root, rel, rti, rte);
178178
}
179-
else if (rel->rtekind == RTE_SUBQUERY)
180-
{
181-
/* Subquery --- generate a separate plan for it */
182-
set_subquery_pathlist(root, rel, rti, rte);
183-
}
184-
else if (rel->rtekind == RTE_FUNCTION)
185-
{
186-
/* RangeFunction --- generate a suitable path for it */
187-
set_function_pathlist(root, rel, rte);
188-
}
189-
else if (rel->rtekind == RTE_VALUES)
190-
{
191-
/* Values list --- generate a suitable path for it */
192-
set_values_pathlist(root, rel, rte);
193-
}
194-
else if (rel->rtekind == RTE_CTE)
195-
{
196-
/* CTE reference --- generate a suitable path for it */
197-
if (rte->self_reference)
198-
set_worktable_pathlist(root, rel, rte);
199-
else
200-
set_cte_pathlist(root, rel, rte);
201-
}
202179
else
203180
{
204-
Assert(rel->rtekind == RTE_RELATION);
205-
if (get_rel_relkind(rte->relid) == RELKIND_FOREIGN_TABLE)
206-
{
207-
/* Foreign table */
208-
set_foreign_pathlist(root, rel, rte);
209-
}
210-
else
181+
switch (rel->rtekind)
211182
{
212-
/* Plain relation */
213-
set_plain_rel_pathlist(root, rel, rte);
183+
case RTE_RELATION:
184+
if (rte->relkind == RELKIND_FOREIGN_TABLE)
185+
{
186+
/* Foreign table */
187+
set_foreign_pathlist(root, rel, rte);
188+
}
189+
else
190+
{
191+
/* Plain relation */
192+
set_plain_rel_pathlist(root, rel, rte);
193+
}
194+
break;
195+
case RTE_SUBQUERY:
196+
/* Subquery --- generate a separate plan for it */
197+
set_subquery_pathlist(root, rel, rti, rte);
198+
break;
199+
case RTE_FUNCTION:
200+
/* RangeFunction --- generate a suitable path for it */
201+
set_function_pathlist(root, rel, rte);
202+
break;
203+
case RTE_VALUES:
204+
/* Values list --- generate a suitable path for it */
205+
set_values_pathlist(root, rel, rte);
206+
break;
207+
case RTE_CTE:
208+
/* CTE reference --- generate a suitable path for it */
209+
if (rte->self_reference)
210+
set_worktable_pathlist(root, rel, rte);
211+
else
212+
set_cte_pathlist(root, rel, rte);
213+
break;
214+
default:
215+
elog(ERROR, "unexpected rtekind: %d", (int) rel->rtekind);
216+
break;
214217
}
215218
}
216219

src/backend/optimizer/plan/planner.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1915,7 +1915,7 @@ preprocess_rowmarks(PlannerInfo *root)
19151915
newrc->rowmarkId = ++(root->glob->lastRowMarkId);
19161916
/* real tables support REFERENCE, anything else needs COPY */
19171917
if (rte->rtekind == RTE_RELATION &&
1918-
get_rel_relkind(rte->relid) != RELKIND_FOREIGN_TABLE)
1918+
rte->relkind != RELKIND_FOREIGN_TABLE)
19191919
newrc->markType = ROW_MARK_REFERENCE;
19201920
else
19211921
newrc->markType = ROW_MARK_COPY;
@@ -3078,6 +3078,7 @@ plan_cluster_use_sort(Oid tableOid, Oid indexOid)
30783078
rte = makeNode(RangeTblEntry);
30793079
rte->rtekind = RTE_RELATION;
30803080
rte->relid = tableOid;
3081+
rte->relkind = RELKIND_RELATION;
30813082
rte->inh = false;
30823083
rte->inFromCl = true;
30833084
query->rtable = list_make1(rte);

0 commit comments

Comments
 (0)