Skip to content

Commit 09991e5

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 422952e commit 09991e5

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
@@ -1366,6 +1366,15 @@ _readRangeTblEntry(void)
13661366
break;
13671367
case RTE_TABLEFUNC:
13681368
READ_NODE_FIELD(tablefunc);
1369+
/* The RTE must have a copy of the column type info, if any */
1370+
if (local_node->tablefunc)
1371+
{
1372+
TableFunc *tf = local_node->tablefunc;
1373+
1374+
local_node->coltypes = tf->coltypes;
1375+
local_node->coltypmods = tf->coltypmods;
1376+
local_node->colcollations = tf->colcollations;
1377+
}
13691378
break;
13701379
case RTE_VALUES:
13711380
READ_NODE_FIELD(values_lists);
@@ -1909,6 +1918,21 @@ _readCteScan(void)
19091918
READ_DONE();
19101919
}
19111920

1921+
/*
1922+
* _readNamedTuplestoreScan
1923+
*/
1924+
static NamedTuplestoreScan *
1925+
_readNamedTuplestoreScan(void)
1926+
{
1927+
READ_LOCALS(NamedTuplestoreScan);
1928+
1929+
ReadCommonScan(&local_node->scan);
1930+
1931+
READ_STRING_FIELD(enrname);
1932+
1933+
READ_DONE();
1934+
}
1935+
19121936
/*
19131937
* _readWorkTableScan
19141938
*/
@@ -2693,6 +2717,8 @@ parseNodeString(void)
26932717
return_value = _readTableFuncScan();
26942718
else if (MATCH("CTESCAN", 7))
26952719
return_value = _readCteScan();
2720+
else if (MATCH("NAMEDTUPLESTORESCAN", 19))
2721+
return_value = _readNamedTuplestoreScan();
26962722
else if (MATCH("WORKTABLESCAN", 13))
26972723
return_value = _readWorkTableScan();
26982724
else if (MATCH("FOREIGNSCAN", 11))

src/include/nodes/parsenodes.h

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

10361036
/*
1037-
* Fields valid for table functions, values, CTE and ENR RTEs (else NIL):
1037+
* Fields valid for CTE, VALUES, ENR, and TableFunc RTEs (else NIL):
10381038
*
10391039
* We need these for CTE RTEs so that the types of self-referential
10401040
* columns are well-defined. For VALUES RTEs, storing these explicitly
10411041
* saves having to re-determine the info by scanning the values_lists. For
10421042
* ENRs, we store the types explicitly here (we could get the information
10431043
* from the catalogs if 'relid' was supplied, but we'd still need these
10441044
* for TupleDesc-based ENRs, so we might as well always store the type
1045-
* info here).
1045+
* info here). For TableFuncs, these fields are redundant with data in
1046+
* the TableFunc node, but keeping them here allows some code sharing with
1047+
* the other cases.
10461048
*
10471049
* For ENRs only, we have to consider the possibility of dropped columns.
10481050
* A dropped column is included in these lists, but it will have zeroes in

0 commit comments

Comments
 (0)