Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Fix some probably-minor oversights in readfuncs.c.
authorTom Lane <tgl@sss.pgh.pa.us>
Tue, 18 Sep 2018 17:02:27 +0000 (13:02 -0400)
committerTom Lane <tgl@sss.pgh.pa.us>
Tue, 18 Sep 2018 17:02:27 +0000 (13:02 -0400)
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

src/backend/nodes/readfuncs.c
src/include/nodes/parsenodes.h

index 67b9e19d29b4d5c0da9bea2c9ecac72ec8d647a2..cd426638c51daa55078e1883fcdfd99f8ae18eb2 100644 (file)
@@ -1355,6 +1355,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);
@@ -1889,6 +1898,21 @@ _readCteScan(void)
    READ_DONE();
 }
 
+/*
+ * _readNamedTuplestoreScan
+ */
+static NamedTuplestoreScan *
+_readNamedTuplestoreScan(void)
+{
+   READ_LOCALS(NamedTuplestoreScan);
+
+   ReadCommonScan(&local_node->scan);
+
+   READ_STRING_FIELD(enrname);
+
+   READ_DONE();
+}
+
 /*
  * _readWorkTableScan
  */
@@ -2605,6 +2629,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))
index aa0b9b0ce48e7d21098d4900ad66fa102c67e7ec..1f80dfaa85d104b09b16662c4f2d14c48f288bbf 100644 (file)
@@ -1019,7 +1019,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
@@ -1027,7 +1027,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