From 56b01dc9ff900a944cd970ea8d65fd3de2361441 Mon Sep 17 00:00:00 2001
From: Tom Lane <tgl@sss.pgh.pa.us>
Date: Thu, 9 Jun 2005 21:25:22 +0000
Subject: [PATCH] Make SPI set SPI_processed for CREATE TABLE AS / SELECT INTO
 commands; this in turn causes CREATE TABLE AS in plpgsql to set ROW_COUNT.
 This is how it behaved before 7.4; I had unintentionally changed the behavior
 in a bit of sloppy micro-optimization.

---
 src/backend/executor/spi.c | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)

diff --git a/src/backend/executor/spi.c b/src/backend/executor/spi.c
index 22619930b2f..f00a2c3c411 100644
--- a/src/backend/executor/spi.c
+++ b/src/backend/executor/spi.c
@@ -8,7 +8,7 @@
  *
  *
  * IDENTIFICATION
- *	  $PostgreSQL: pgsql/src/backend/executor/spi.c,v 1.140 2005/05/06 17:24:54 tgl Exp $
+ *	  $PostgreSQL: pgsql/src/backend/executor/spi.c,v 1.141 2005/06/09 21:25:22 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -1497,6 +1497,7 @@ static int
 _SPI_pquery(QueryDesc *queryDesc, long tcount)
 {
 	int			operation = queryDesc->operation;
+	CommandDest	origDest = queryDesc->dest->mydest;
 	int			res;
 	Oid			save_lastoid;
 
@@ -1504,10 +1505,10 @@ _SPI_pquery(QueryDesc *queryDesc, long tcount)
 	{
 		case CMD_SELECT:
 			res = SPI_OK_SELECT;
-			if (queryDesc->parsetree->into != NULL)		/* select into table */
+			if (queryDesc->parsetree->into)			/* select into table? */
 			{
 				res = SPI_OK_SELINTO;
-				queryDesc->dest = None_Receiver;		/* don't output results */
+				queryDesc->dest = None_Receiver;	/* don't output results */
 			}
 			break;
 		case CMD_INSERT:
@@ -1548,7 +1549,8 @@ _SPI_pquery(QueryDesc *queryDesc, long tcount)
 
 	ExecutorEnd(queryDesc);
 
-	if (queryDesc->dest->mydest == SPI)
+	/* Test origDest here so that SPI_processed gets set in SELINTO case */
+	if (origDest == SPI)
 	{
 		SPI_processed = _SPI_current->processed;
 		SPI_lastoid = save_lastoid;
-- 
GitLab