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
This commit is contained in:
Tom Lane 2018-09-18 13:02:27 -04:00
parent 422952ee78
commit 09991e5a47
2 changed files with 30 additions and 2 deletions

View File

@ -1366,6 +1366,15 @@ _readRangeTblEntry(void)
break;
case RTE_TABLEFUNC:
READ_NODE_FIELD(tablefunc);
/* The RTE must have a copy of the column type info, if any */
if (local_node->tablefunc)
{
TableFunc *tf = local_node->tablefunc;
local_node->coltypes = tf->coltypes;
local_node->coltypmods = tf->coltypmods;
local_node->colcollations = tf->colcollations;
}
break;
case RTE_VALUES:
READ_NODE_FIELD(values_lists);
@ -1909,6 +1918,21 @@ _readCteScan(void)
READ_DONE();
}
/*
* _readNamedTuplestoreScan
*/
static NamedTuplestoreScan *
_readNamedTuplestoreScan(void)
{
READ_LOCALS(NamedTuplestoreScan);
ReadCommonScan(&local_node->scan);
READ_STRING_FIELD(enrname);
READ_DONE();
}
/*
* _readWorkTableScan
*/
@ -2693,6 +2717,8 @@ parseNodeString(void)
return_value = _readTableFuncScan();
else if (MATCH("CTESCAN", 7))
return_value = _readCteScan();
else if (MATCH("NAMEDTUPLESTORESCAN", 19))
return_value = _readNamedTuplestoreScan();
else if (MATCH("WORKTABLESCAN", 13))
return_value = _readWorkTableScan();
else if (MATCH("FOREIGNSCAN", 11))

View File

@ -1034,7 +1034,7 @@ typedef struct RangeTblEntry
bool self_reference; /* is this a recursive self-reference? */
/*
* Fields valid for table functions, values, CTE and ENR RTEs (else NIL):
* Fields valid for CTE, VALUES, ENR, and TableFunc RTEs (else NIL):
*
* We need these for CTE RTEs so that the types of self-referential
* columns are well-defined. For VALUES RTEs, storing these explicitly
@ -1042,7 +1042,9 @@ typedef struct RangeTblEntry
* ENRs, we store the types explicitly here (we could get the information
* from the catalogs if 'relid' was supplied, but we'd still need these
* for TupleDesc-based ENRs, so we might as well always store the type
* info here).
* info here). For TableFuncs, these fields are redundant with data in
* the TableFunc node, but keeping them here allows some code sharing with
* the other cases.
*
* For ENRs only, we have to consider the possibility of dropped columns.
* A dropped column is included in these lists, but it will have zeroes in