From f4b4a46f01eca60f4e7a2827cf13ef8bbff5f1f8 Mon Sep 17 00:00:00 2001
From: Tom Lane <tgl@sss.pgh.pa.us>
Date: Thu, 19 Aug 2010 18:10:48 +0000
Subject: [PATCH] Keep exec_simple_check_plan() from thinking "SELECT foo INTO
 bar" is simple.

It's not clear if this situation can occur in plpgsql other than via the
EXECUTE USING case Heikki illustrated, which I will shortly close off.
However, ignoring the intoClause if it's there is surely wrong, so let's
patch it for safety.

Backpatch to 8.3, which is as far back as this code has a PlannedStmt
to deal with.  There might be another way to make an equivalent test
before that, but since this is just preventing hypothetical bugs,
I'm not going to obsess about it.
---
 src/pl/plpgsql/src/pl_exec.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/src/pl/plpgsql/src/pl_exec.c b/src/pl/plpgsql/src/pl_exec.c
index 223eaf9db55..2ae80a32f14 100644
--- a/src/pl/plpgsql/src/pl_exec.c
+++ b/src/pl/plpgsql/src/pl_exec.c
@@ -8,7 +8,7 @@
  *
  *
  * IDENTIFICATION
- *	  $PostgreSQL: pgsql/src/pl/plpgsql/src/pl_exec.c,v 1.265 2010/08/19 17:31:43 tgl Exp $
+ *	  $PostgreSQL: pgsql/src/pl/plpgsql/src/pl_exec.c,v 1.266 2010/08/19 18:10:48 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -5280,6 +5280,8 @@ exec_simple_check_plan(PLpgSQL_expr *expr)
 	 */
 	if (!IsA(stmt, PlannedStmt))
 		return;
+	if (stmt->commandType != CMD_SELECT || stmt->intoClause)
+		return;
 	plan = stmt->planTree;
 	if (!IsA(plan, Result))
 		return;
-- 
GitLab