Skip to content

Commit cdbdf85

Browse files
committed
Fix some probably-minor oversights in readfuncs.c.
The system expects TABLEFUNC RTEs to have coltypes, coltypmods, and colcollations lists, but outfuncs doesn't dump them and readfuncs doesn't restore them. This doesn't cause obvious failures, because the only things that look at those fields are expandRTE() and get_rte_attribute_type(), which are mostly used during parse analysis, before anything would've passed the parsetree through outfuncs/readfuncs. But expandRTE() is used in build_physical_tlist(), which means that that function will return a wrong answer for a TABLEFUNC RTE that came from a view. Very accidentally, this doesn't cause serious problems, because what it will return is NIL which callers will interpret as "couldn't build a physical tlist because of dropped columns". So you still get a plan that works, though it's marginally less efficient than it could be. There are also some other expandRTE() calls associated with transformation of whole-row Vars in the planner. I have been unable to exhibit misbehavior from that, and it may be unreachable in any case that anyone would care about ... but I'm not entirely convinced, so this seems like something we should back- patch a fix for. Fortunately, we can fix it without forcing a change of stored rules and a catversion bump, because we can just copy these lists from the subsidiary TableFunc object. readfuncs.c was also missing support for NamedTuplestoreScan plan nodes. This accidentally fails to break parallel query because a query using a named tuplestore would never be considered parallel-safe anyway. However, project policy since parallel query came in is that all plan node types should have outfuncs/readfuncs support, so this is clearly an oversight that should be repaired. Noted while fooling around with a patch to test outfuncs/readfuncs more thoroughly. That exposed some other issues too, but these are the only ones that seem worth back-patching. Back-patch to v10 where both of these features came in. Discussion: https://postgr.es/m/17114.1537138992@sss.pgh.pa.us
1 parent 7167fa8 commit cdbdf85

File tree

2 files changed

+30
-2
lines changed

2 files changed

+30
-2
lines changed

src/backend/nodes/readfuncs.c

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1355,6 +1355,15 @@ _readRangeTblEntry(void)
13551355
break;
13561356
case RTE_TABLEFUNC:
13571357
READ_NODE_FIELD(tablefunc);
1358+
/* The RTE must have a copy of the column type info, if any */
1359+
if (local_node->tablefunc)
1360+
{
1361+
TableFunc *tf = local_node->tablefunc;
1362+
1363+
local_node->coltypes = tf->coltypes;
1364+
local_node->coltypmods = tf->coltypmods;
1365+
local_node->colcollations = tf->colcollations;
1366+
}
13581367
break;
13591368
case RTE_VALUES:
13601369
READ_NODE_FIELD(values_lists);
@@ -1889,6 +1898,21 @@ _readCteScan(void)
18891898
READ_DONE();
18901899
}
18911900

1901+
/*
1902+
* _readNamedTuplestoreScan
1903+
*/
1904+
static NamedTuplestoreScan *
1905+
_readNamedTuplestoreScan(void)
1906+
{
1907+
READ_LOCALS(NamedTuplestoreScan);
1908+
1909+
ReadCommonScan(&local_node->scan);
1910+
1911+
READ_STRING_FIELD(enrname);
1912+
1913+
READ_DONE();
1914+
}
1915+
18921916
/*
18931917
* _readWorkTableScan
18941918
*/
@@ -2605,6 +2629,8 @@ parseNodeString(void)
26052629
return_value = _readTableFuncScan();
26062630
else if (MATCH("CTESCAN", 7))
26072631
return_value = _readCteScan();
2632+
else if (MATCH("NAMEDTUPLESTORESCAN", 19))
2633+
return_value = _readNamedTuplestoreScan();
26082634
else if (MATCH("WORKTABLESCAN", 13))
26092635
return_value = _readWorkTableScan();
26102636
else if (MATCH("FOREIGNSCAN", 11))

src/include/nodes/parsenodes.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1019,15 +1019,17 @@ typedef struct RangeTblEntry
10191019
bool self_reference; /* is this a recursive self-reference? */
10201020

10211021
/*
1022-
* Fields valid for table functions, values, CTE and ENR RTEs (else NIL):
1022+
* Fields valid for CTE, VALUES, ENR, and TableFunc RTEs (else NIL):
10231023
*
10241024
* We need these for CTE RTEs so that the types of self-referential
10251025
* columns are well-defined. For VALUES RTEs, storing these explicitly
10261026
* saves having to re-determine the info by scanning the values_lists. For
10271027
* ENRs, we store the types explicitly here (we could get the information
10281028
* from the catalogs if 'relid' was supplied, but we'd still need these
10291029
* for TupleDesc-based ENRs, so we might as well always store the type
1030-
* info here).
1030+
* info here). For TableFuncs, these fields are redundant with data in
1031+
* the TableFunc node, but keeping them here allows some code sharing with
1032+
* the other cases.
10311033
*
10321034
* For ENRs only, we have to consider the possibility of dropped columns.
10331035
* A dropped column is included in these lists, but it will have zeroes in

0 commit comments

Comments
 (0)