<para>
    The actual number of rows for which the (last) command was executed
-   is returned in the global variable <varname>SPI_processed</varname>
-   (unless the return value of the function is
-   <symbol>SPI_OK_UTILITY</symbol>).  If the return value of the
-   function is <symbol>SPI_OK_SELECT</symbol> then you may use the
+   is returned in the global variable <varname>SPI_processed</varname>.
+   If the return value of the function is <symbol>SPI_OK_SELECT</symbol>,
+   <symbol>SPI_OK_INSERT_RETURNING</symbol>,
+   <symbol>SPI_OK_DELETE_RETURNING</symbol>, or
+   <symbol>SPI_OK_UPDATE_RETURNING</symbol>,
+   then you may use the
    global pointer <literal>SPITupleTable *SPI_tuptable</literal> to
-   access the result rows.
+   access the result rows.  Some utility commands (such as
+   <command>EXPLAIN</>) also return rowsets, and <literal>SPI_tuptable</> 
+   will contain the result in these cases too.
   </para>
 
   <para>
     </varlistentry>
 
     <varlistentry>
-     <term><symbol>SPI_OK_DELETE</symbol></term>
+     <term><symbol>SPI_OK_INSERT</symbol></term>
      <listitem>
       <para>
-       if a <command>DELETE</command> was executed
+       if an <command>INSERT</command> was executed
       </para>
      </listitem>
     </varlistentry>
 
     <varlistentry>
-     <term><symbol>SPI_OK_INSERT</symbol></term>
+     <term><symbol>SPI_OK_DELETE</symbol></term>
      <listitem>
       <para>
-       if an <command>INSERT</command> was executed
+       if a <command>DELETE</command> was executed
       </para>
      </listitem>
     </varlistentry>
      </listitem>
     </varlistentry>
 
+    <varlistentry>
+     <term><symbol>SPI_OK_INSERT_RETURNING</symbol></term>
+     <listitem>
+      <para>
+       if an <command>INSERT RETURNING</command> was executed
+      </para>
+     </listitem>
+    </varlistentry>
+
+    <varlistentry>
+     <term><symbol>SPI_OK_DELETE_RETURNING</symbol></term>
+     <listitem>
+      <para>
+       if a <command>DELETE RETURNING</command> was executed
+      </para>
+     </listitem>
+    </varlistentry>
+
+    <varlistentry>
+     <term><symbol>SPI_OK_UPDATE_RETURNING</symbol></term>
+     <listitem>
+      <para>
+       if an <command>UPDATE RETURNING</command> was executed
+      </para>
+     </listitem>
+    </varlistentry>
+
     <varlistentry>
      <term><symbol>SPI_OK_UTILITY</symbol></term>
      <listitem>
     
     proc = SPI_processed;
     /*
-     * If this is a SELECT and some rows were fetched,
-     * then the rows are printed via elog(INFO).
+     * If some rows were fetched, print them via elog(INFO).
      */
-    if (ret == SPI_OK_SELECT && SPI_processed > 0)
+    if (ret > 0 && SPI_tuptable != NULL)
     {
         TupleDesc tupdesc = SPI_tuptable->tupdesc;
         SPITupleTable *tuptable = SPI_tuptable;
                 snprintf(buf + strlen (buf), sizeof(buf) - strlen(buf), " %s%s",
                         SPI_getvalue(tuple, tupdesc, i),
                         (i == tupdesc->natts) ? " " : " |");
-            elog (INFO, "EXECQ: %s", buf);
+            elog(INFO, "EXECQ: %s", buf);
         }
     }
 
 
                        return "SPI_OK_UPDATE";
                case SPI_OK_CURSOR:
                        return "SPI_OK_CURSOR";
+               case SPI_OK_INSERT_RETURNING:
+                       return "SPI_OK_INSERT_RETURNING";
+               case SPI_OK_DELETE_RETURNING:
+                       return "SPI_OK_DELETE_RETURNING";
+               case SPI_OK_UPDATE_RETURNING:
+                       return "SPI_OK_UPDATE_RETURNING";
        }
        /* Unrecognized code ... return something useful ... */
        sprintf(buf, "Unrecognized SPI code %d", code);
                                {
                                        ProcessUtility(queryTree->utilityStmt, paramLI,
                                                                   dest, NULL);
+                                       /* Update "processed" if stmt returned tuples */
+                                       if (_SPI_current->tuptable)
+                                               _SPI_current->processed = _SPI_current->tuptable->alloced - _SPI_current->tuptable->free;
                                        res = SPI_OK_UTILITY;
                                }
                                else
                                res = SPI_OK_SELECT;
                        break;
                case CMD_INSERT:
-                       res = SPI_OK_INSERT;
+                       if (queryDesc->parsetree->returningList)
+                               res = SPI_OK_INSERT_RETURNING;
+                       else
+                               res = SPI_OK_INSERT;
                        break;
                case CMD_DELETE:
-                       res = SPI_OK_DELETE;
+                       if (queryDesc->parsetree->returningList)
+                               res = SPI_OK_DELETE_RETURNING;
+                       else
+                               res = SPI_OK_DELETE;
                        break;
                case CMD_UPDATE:
-                       res = SPI_OK_UPDATE;
+                       if (queryDesc->parsetree->returningList)
+                               res = SPI_OK_UPDATE_RETURNING;
+                       else
+                               res = SPI_OK_UPDATE;
                        break;
                default:
                        return SPI_ERROR_OPUNKNOWN;
        _SPI_current->processed = queryDesc->estate->es_processed;
        _SPI_current->lastoid = queryDesc->estate->es_lastoid;
 
-       if (operation == CMD_SELECT && queryDesc->dest->mydest == DestSPI)
+       if ((res == SPI_OK_SELECT || queryDesc->parsetree->returningList) &&
+               queryDesc->dest->mydest == DestSPI)
        {
                if (_SPI_checktuples())
                        elog(ERROR, "consistency check on SPI tuple count failed");
 
 #define SPI_OK_DELETE                  8
 #define SPI_OK_UPDATE                  9
 #define SPI_OK_CURSOR                  10
+#define SPI_OK_INSERT_RETURNING        11
+#define SPI_OK_DELETE_RETURNING        12
+#define SPI_OK_UPDATE_RETURNING        13
 
 extern DLLIMPORT uint32 SPI_processed;
 extern DLLIMPORT Oid SPI_lastoid;
 
        hv_store(result, "processed", strlen("processed"),
                         newSViv(processed), 0);
 
-       if (status == SPI_OK_SELECT)
+       if (status > 0 && tuptable)
        {
                AV                 *rows;
                SV                 *row;
 
                case SPI_OK_INSERT:
                case SPI_OK_UPDATE:
                case SPI_OK_DELETE:
+               case SPI_OK_INSERT_RETURNING:
+               case SPI_OK_UPDATE_RETURNING:
+               case SPI_OK_DELETE_RETURNING:
                        Assert(stmt->mod_stmt);
                        exec_set_found(estate, (SPI_processed != 0));
                        break;
 
                case SPI_OK_SELINTO:
-                       Assert(!stmt->mod_stmt);
-                       break;
-
                case SPI_OK_UTILITY:
                        Assert(!stmt->mod_stmt);
-                       /*
-                        * spi.c currently does not update SPI_processed for utility
-                        * commands.  Not clear if this should be considered a bug;
-                        * for the moment, work around it here.
-                        */
-                       if (SPI_tuptable)
-                               SPI_processed = (SPI_tuptable->alloced - SPI_tuptable->free);
                        break;
 
                default:
                case SPI_OK_INSERT:
                case SPI_OK_UPDATE:
                case SPI_OK_DELETE:
-                       break;
-
+               case SPI_OK_INSERT_RETURNING:
+               case SPI_OK_UPDATE_RETURNING:
+               case SPI_OK_DELETE_RETURNING:
                case SPI_OK_UTILITY:
-                       /*
-                        * spi.c currently does not update SPI_processed for utility
-                        * commands.  Not clear if this should be considered a bug;
-                        * for the moment, work around it here.
-                        */
-                       if (SPI_tuptable)
-                               SPI_processed = (SPI_tuptable->alloced - SPI_tuptable->free);
                        break;
 
                case 0:
 
        Py_DECREF(result->status);
        result->status = PyInt_FromLong(status);
 
-       if (status == SPI_OK_UTILITY)
-       {
-               Py_DECREF(result->nrows);
-               result->nrows = PyInt_FromLong(0);
-       }
-       else if (status != SPI_OK_SELECT)
+       if (status > 0 && tuptable == NULL)
        {
                Py_DECREF(result->nrows);
                result->nrows = PyInt_FromLong(rows);
        }
-       else
+       else if (status > 0 && tuptable != NULL)
        {
                PLyTypeInfo args;
                int                     i;
 
-               PLy_typeinfo_init(&args);
                Py_DECREF(result->nrows);
                result->nrows = PyInt_FromLong(rows);
+               PLy_typeinfo_init(&args);
 
                oldcontext = CurrentMemoryContext;
                PG_TRY();
 
 
        switch (spi_rc)
        {
-               case SPI_OK_UTILITY:
-                       Tcl_SetResult(interp, "0", TCL_VOLATILE);
-                       break;
-
                case SPI_OK_SELINTO:
                case SPI_OK_INSERT:
                case SPI_OK_DELETE:
                        Tcl_SetResult(interp, buf, TCL_VOLATILE);
                        break;
 
+               case SPI_OK_UTILITY:
+                       if (tuptable == NULL)
+                       {
+                               Tcl_SetResult(interp, "0", TCL_VOLATILE);
+                               break;
+                       }
+                       /* FALL THRU for utility returning tuples */
+
                case SPI_OK_SELECT:
+               case SPI_OK_INSERT_RETURNING:
+               case SPI_OK_DELETE_RETURNING:
+               case SPI_OK_UPDATE_RETURNING:
 
                        /*
                         * Process the tuples we got