]> granicus.if.org Git - postgresql/commitdiff
Explicitly support the case that a plancache's raw_parse_tree is NULL.
authorTom Lane <tgl@sss.pgh.pa.us>
Wed, 12 Nov 2014 20:58:37 +0000 (15:58 -0500)
committerTom Lane <tgl@sss.pgh.pa.us>
Wed, 12 Nov 2014 20:59:01 +0000 (15:59 -0500)
This only happens if a client issues a Parse message with an empty query
string, which is a bit odd; but since it is explicitly called out as legal
by our FE/BE protocol spec, we'd probably better continue to allow it.

Fix by adding tests everywhere that the raw_parse_tree field is passed to
functions that don't or shouldn't accept NULL.  Also make it clear in the
relevant comments that NULL is an expected case.

This reverts commits a73c9dbab0165b3395dfe8a44a7dfd16166963c4 and
2e9650cbcff8c8fb0d9ef807c73a44f241822eee, which fixed specific crash
symptoms by hacking things at what now seems to be the wrong end, ie the
callee functions.  Making the callees allow NULL is superficially more
robust, but it's not always true that there is a defensible thing for the
callee to do in such cases.  The caller has more context and is better
able to decide what the empty-query case ought to do.

Per followup discussion of bug #11335.  Back-patch to 9.2.  The code
before that is sufficiently different that it would require development
of a separate patch, which doesn't seem worthwhile for what is believed
to be an essentially cosmetic change.

src/backend/executor/spi.c
src/backend/parser/analyze.c
src/backend/tcop/postgres.c
src/backend/tcop/utility.c
src/backend/utils/cache/plancache.c
src/include/utils/plancache.h

index 7ba1fd90663381e2d1d3acf5a8ce610adc23d0a9..cfa4a24686feac2884cd2ccf2d33ca733c8c838e 100644 (file)
@@ -2037,7 +2037,9 @@ _SPI_execute_plan(SPIPlanPtr plan, ParamListInfo paramLI,
                         * Parameter datatypes are driven by parserSetup hook if provided,
                         * otherwise we use the fixed parameter list.
                         */
-                       if (plan->parserSetup != NULL)
+                       if (parsetree == NULL)
+                               stmt_list = NIL;
+                       else if (plan->parserSetup != NULL)
                        {
                                Assert(plan->nargs == 0);
                                stmt_list = pg_analyze_and_rewrite_params(parsetree,
index bd78e94985301e8b4fcbf28752e4ec1e951f9bcd..cc569eddd562730867e8f96cde59be81859ae120 100644 (file)
@@ -288,17 +288,13 @@ transformStmt(ParseState *pstate, Node *parseTree)
  *             Returns true if a snapshot must be set before doing parse analysis
  *             on the given raw parse tree.
  *
- * Classification here should match transformStmt(); but we also have to
- * allow a NULL input (for Parse/Bind of an empty query string).
+ * Classification here should match transformStmt().
  */
 bool
 analyze_requires_snapshot(Node *parseTree)
 {
        bool            result;
 
-       if (parseTree == NULL)
-               return false;
-
        switch (nodeTag(parseTree))
        {
                        /*
index 61f17bf1e9b2468d14f87398203c1cb1d8dcc52a..cc62b2cfe8b73a17ed975d25167eb28bcfe9e6f1 100644 (file)
@@ -1546,7 +1546,9 @@ exec_bind_message(StringInfo input_message)
         * snapshot active till we're done, so that plancache.c doesn't have to
         * take new ones.
         */
-       if (numParams > 0 || analyze_requires_snapshot(psrc->raw_parse_tree))
+       if (numParams > 0 ||
+               (psrc->raw_parse_tree &&
+                analyze_requires_snapshot(psrc->raw_parse_tree)))
        {
                PushActiveSnapshot(GetTransactionSnapshot());
                snapshot_set = true;
index 422911cde03238d084a3fcd3f64c270c2461cbb7..58f78ce50d54aa627c803a496591c8a87ff7a8ed 100644 (file)
@@ -2485,9 +2485,6 @@ GetCommandLogLevel(Node *parsetree)
 {
        LogStmtLevel lev;
 
-       if (parsetree == NULL)
-               return LOGSTMT_ALL;
-
        switch (nodeTag(parsetree))
        {
                        /* raw plannable queries */
@@ -2592,7 +2589,7 @@ GetCommandLogLevel(Node *parsetree)
 
                                /* Look through an EXECUTE to the referenced stmt */
                                ps = FetchPreparedStatement(stmt->name, false);
-                               if (ps)
+                               if (ps && ps->plansource->raw_parse_tree)
                                        lev = GetCommandLogLevel(ps->plansource->raw_parse_tree);
                                else
                                        lev = LOGSTMT_ALL;
index 26ef2fa6ff16fab552424f0d4f623ff81479fc5e..d85ea1bbd3ae8fa693807e289a547bfb1e7efeef 100644 (file)
@@ -141,7 +141,7 @@ InitPlanCache(void)
  * Once constructed, the cached plan can be made longer-lived, if needed,
  * by calling SaveCachedPlan.
  *
- * raw_parse_tree: output of raw_parser()
+ * raw_parse_tree: output of raw_parser(), or NULL if empty query
  * query_string: original query text
  * commandTag: compile-time-constant tag for query, or NULL if empty query
  */
@@ -232,7 +232,7 @@ CreateCachedPlan(Node *raw_parse_tree,
  * invalidation, so plan use must be completed in the current transaction,
  * and DDL that might invalidate the querytree_list must be avoided as well.
  *
- * raw_parse_tree: output of raw_parser()
+ * raw_parse_tree: output of raw_parser(), or NULL if empty query
  * query_string: original query text
  * commandTag: compile-time-constant tag for query, or NULL if empty query
  */
@@ -699,7 +699,9 @@ RevalidateCachedQuery(CachedPlanSource *plansource)
         * the cache.
         */
        rawtree = copyObject(plansource->raw_parse_tree);
-       if (plansource->parserSetup != NULL)
+       if (rawtree == NULL)
+               tlist = NIL;
+       else if (plansource->parserSetup != NULL)
                tlist = pg_analyze_and_rewrite_params(rawtree,
                                                                                          plansource->query_string,
                                                                                          plansource->parserSetup,
@@ -928,6 +930,7 @@ BuildCachedPlan(CachedPlanSource *plansource, List *qlist,
         */
        snapshot_set = false;
        if (!ActiveSnapshotSet() &&
+               plansource->raw_parse_tree &&
                analyze_requires_snapshot(plansource->raw_parse_tree))
        {
                PushActiveSnapshot(GetTransactionSnapshot());
index 56bf4bb7afc69de065a71d40ce7e6009794300ef..2622ceb54bc1908c4146d4332c631a530a048c54 100644 (file)
@@ -76,7 +76,7 @@
 typedef struct CachedPlanSource
 {
        int                     magic;                  /* should equal CACHEDPLANSOURCE_MAGIC */
-       Node       *raw_parse_tree; /* output of raw_parser() */
+       Node       *raw_parse_tree; /* output of raw_parser(), or NULL */
        const char *query_string;       /* source text of query */
        const char *commandTag;         /* command tag (a constant!), or NULL */
        Oid                *param_types;        /* array of parameter type OIDs, or NULL */