Defend against crash while processing Describe Statement or Describe Portal
authorTom Lane <[email protected]>
Wed, 14 Dec 2005 17:06:51 +0000 (17:06 +0000)
committerTom Lane <[email protected]>
Wed, 14 Dec 2005 17:06:51 +0000 (17:06 +0000)
messages, when client attempts to execute these outside a transaction (start
one) or in a failed transaction (reject message, except for COMMIT/ROLLBACK
statements which we can handle).  Per report from Francisco Figueiredo Jr.

src/backend/commands/prepare.c
src/backend/tcop/postgres.c
src/include/commands/prepare.h

index 41e214168b4020dd7706543faac5e4ba614978b7..addef84418c26269b32fc30cab5eac8c2b52fd0a 100644 (file)
@@ -441,6 +441,30 @@ FetchPreparedStatementResultDesc(PreparedStatement *stmt)
        return NULL;
 }
 
+/*
+ * Given a prepared statement, determine whether it will return tuples.
+ *
+ * Note: this is used rather than just testing the result of
+ * FetchPreparedStatementResultDesc() because that routine can fail if
+ * invoked in an aborted transaction.  This one is safe to use in any
+ * context.  Be sure to keep the two routines in sync!
+ */
+bool
+PreparedStatementReturnsTuples(PreparedStatement *stmt)
+{
+       switch (ChoosePortalStrategy(stmt->query_list))
+       {
+               case PORTAL_ONE_SELECT:
+               case PORTAL_UTIL_SELECT:
+                       return true;
+
+               case PORTAL_MULTI_QUERY:
+                       /* will not return tuples */
+                       break;
+       }
+       return false;
+}
+
 /*
  * Implements the 'DEALLOCATE' utility statement: deletes the
  * specified plan from storage.
index 5750c9f6fb8b39a5bbb6bf007ca9c9f68a7606d2..8558085a9220642dcbaa24de7a8c842cd4435eb2 100644 (file)
@@ -1732,6 +1732,15 @@ exec_describe_statement_message(const char *stmt_name)
        ListCell   *l;
        StringInfoData buf;
 
+       /*
+        * Start up a transaction command. (Note that this will normally change
+        * current memory context.) Nothing happens if we are already in one.
+        */
+       start_xact_command();
+
+       /* Switch back to message context */
+       MemoryContextSwitchTo(MessageContext);
+
        /* Find prepared statement */
        if (stmt_name[0] != '\0')
                pstmt = FetchPreparedStatement(stmt_name, true);
@@ -1745,6 +1754,22 @@ exec_describe_statement_message(const char *stmt_name)
                                   errmsg("unnamed prepared statement does not exist")));
        }
 
+       /*
+        * If we are in aborted transaction state, we can't safely create a result
+        * tupledesc, because that needs catalog accesses.  Hence, refuse to
+        * Describe statements that return data.  (We shouldn't just refuse all
+        * Describes, since that might break the ability of some clients to issue
+        * COMMIT or ROLLBACK commands, if they use code that blindly Describes
+        * whatever it does.)  We can Describe parameters without doing anything
+        * dangerous, so we don't restrict that.
+        */
+       if (IsAbortedTransactionBlockState() &&
+               PreparedStatementReturnsTuples(pstmt))
+               ereport(ERROR,
+                               (errcode(ERRCODE_IN_FAILED_SQL_TRANSACTION),
+                                errmsg("current transaction is aborted, "
+                                               "commands ignored until end of transaction block")));
+
        if (whereToSendOutput != Remote)
                return;                                 /* can't actually do anything... */
 
@@ -1791,12 +1816,36 @@ exec_describe_portal_message(const char *portal_name)
 {
        Portal          portal;
 
+       /*
+        * Start up a transaction command. (Note that this will normally change
+        * current memory context.) Nothing happens if we are already in one.
+        */
+       start_xact_command();
+
+       /* Switch back to message context */
+       MemoryContextSwitchTo(MessageContext);
+
        portal = GetPortalByName(portal_name);
        if (!PortalIsValid(portal))
                ereport(ERROR,
                                (errcode(ERRCODE_UNDEFINED_CURSOR),
                                 errmsg("portal \"%s\" does not exist", portal_name)));
 
+       /*
+        * If we are in aborted transaction state, we can't run
+        * SendRowDescriptionMessage(), because that needs catalog accesses.
+        * Hence, refuse to Describe portals that return data.  (We shouldn't just
+        * refuse all Describes, since that might break the ability of some
+        * clients to issue COMMIT or ROLLBACK commands, if they use code that
+        * blindly Describes whatever it does.)
+        */
+       if (IsAbortedTransactionBlockState() &&
+               portal->tupDesc)
+               ereport(ERROR,
+                               (errcode(ERRCODE_IN_FAILED_SQL_TRANSACTION),
+                                errmsg("current transaction is aborted, "
+                                               "commands ignored until end of transaction block")));
+
        if (whereToSendOutput != Remote)
                return;                                 /* can't actually do anything... */
 
index d4ccbd04db86ef4d4959303e68610f04f5debf57..eae603235d3a32574a362b7e190c6262a4a4510f 100644 (file)
@@ -59,5 +59,6 @@ extern PreparedStatement *FetchPreparedStatement(const char *stmt_name,
 extern void DropPreparedStatement(const char *stmt_name, bool showError);
 extern List *FetchPreparedStatementParams(const char *stmt_name);
 extern TupleDesc FetchPreparedStatementResultDesc(PreparedStatement *stmt);
+extern bool PreparedStatementReturnsTuples(PreparedStatement *stmt);
 
 #endif   /* PREPARE_H */