SPI_exec shouldn't return SPI_OK_SELECT if it hasn't actually returned
authorTom Lane <tgl@sss.pgh.pa.us>
Wed, 29 Jan 2003 15:24:46 +0000 (15:24 +0000)
committerTom Lane <tgl@sss.pgh.pa.us>
Wed, 29 Jan 2003 15:24:46 +0000 (15:24 +0000)
a tuple table.  Fixes core dump in pltcl (and probably other PLs) when
executing a query rewritten by a rule.  Per bug report from Wolfgang Walter.

src/backend/executor/spi.c

index 3df2426473a084004c7127e461cc32e78f7b5bdd..193bd08b4d7238d1c55742e0130401c7594f6624 100644 (file)
@@ -8,7 +8,7 @@
  *
  *
  * IDENTIFICATION
- *   $Header: /cvsroot/pgsql/src/backend/executor/spi.c,v 1.84 2003/01/21 22:06:12 tgl Exp $
+ *   $Header: /cvsroot/pgsql/src/backend/executor/spi.c,v 1.85 2003/01/29 15:24:46 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -124,6 +124,14 @@ SPI_finish(void)
    MemoryContextDelete(_SPI_current->execCxt);
    MemoryContextDelete(_SPI_current->procCxt);
 
+   /*
+    * Reset result variables, especially SPI_tuptable which is probably
+    * pointing at a just-deleted tuptable
+    */
+   SPI_processed = 0;
+   SPI_lastoid = InvalidOid;
+   SPI_tuptable = NULL;
+
    /*
     * After _SPI_begin_call _SPI_connected == _SPI_curid. Now we are
     * closing connection to SPI and returning to upper Executor and so
@@ -1314,6 +1322,11 @@ _SPI_pquery(QueryDesc *queryDesc, bool runit, int tcount)
        SPI_lastoid = save_lastoid;
        SPI_tuptable = _SPI_current->tuptable;
    }
+   else if (res == SPI_OK_SELECT)
+   {
+       /* Don't return SPI_OK_SELECT if we discarded the result */
+       res = SPI_OK_UTILITY;
+   }
 
    ExecutorEnd(queryDesc);