From: Marko Tiikkaja <marko@joh.to>
To: Tom Lane <tgl@sss.pgh.pa.us>
Cc: pgsql-bugs@postgresql.org
Subject: Re: BUG #8870: PL/PgSQL, SELECT .. INTO and the number of result columns
Date: Sun, 19 Jan 2014 01:50:01 +0100
Message-ID: <52DB2139.20604@joh.to> (raw)
In-Reply-To: <25794.1390089578@sss.pgh.pa.us>
References: <20140118232509.26700.59523@wrigleys.postgresql.org>
<25794.1390089578@sss.pgh.pa.us>
List-Unsubscribe: <mailto:majordomo@postgresql.org?body=unsub%20pgsql-bugs>
On 1/19/14, 12:59 AM, Tom Lane wrote:
>> This seems to have been broken by commit
>> 8bb3c8fe5413103c30ba8d1bcb3a1f8e46bddf3d.
>
> Did you really test against a pre-2001 plpgsql?
No, I can't compile it. It just *looks* like the behaviour was broken
by that commit. Sorry for the confusion.
> ISTM what you're really asking for is a compile time (not run-time) check
> that the number of INTO targets matches the nominal number of output
> columns of the query. Not sure how difficult that is, but it'd be more
> likely to be successful than messing with the run-time behavior in
> exec_move_row.
Please correct me if I'm wrong, but as far as I can tell we can't do
that check at compile time because we don't do parse analysis. And
making that happen every time CREATE FUNCTION is called.. I don't see
that happening.
But making this check at run time doesn't seem too hard. How broken
does the attached look? It passes all the existing regression tests, at
least..
Regards,
Marko Tiikkaja
--
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugsAttachments:bug_8870.patch (text/plain, 1.6K) download
*** a/src/pl/plpgsql/src/pl_exec.c
--- b/src/pl/plpgsql/src/pl_exec.c
***************
*** 3256,3261 **** exec_prepare_plan(PLpgSQL_execstate *estate,
--- 3256,3291 ----
exec_simple_check_plan(expr);
}
+ /*+ * exec_check_execsql_targetlist: Check that the target list of a query with+ * INTO matches the number of INTO targets.+ */+ static void+ exec_check_execsql_targetlist(PLpgSQL_stmt_execsql *stmt,+ List *targetList)+ {+ int fnum;+ int target_columns;+ + /* no need to check if it's a record */+ if (stmt->row == NULL)+ return;+ + target_columns = 0;+ for (fnum = 0; fnum < stmt->row->nfields; fnum++)+ {+ if (stmt->row->varnos[fnum] < 0)+ continue;+ target_columns++;+ }+ + if (list_length(targetList) != target_columns)+ ereport(ERROR,+ (errcode(ERRCODE_SYNTAX_ERROR),+ errmsg("the number of targets in INTO does not match the number of columns returned by the query"),+ errdetail("the query returns %d columns, but the target expects %d",+ list_length(targetList), target_columns)));+ }
/* ----------
* exec_stmt_execsql Execute an SQL statement (possibly with INTO).
***************
*** 3295,3301 **** exec_stmt_execsql(PLpgSQL_execstate *estate,
--- 3325,3336 ----
if (q->commandType == CMD_INSERT ||
q->commandType == CMD_UPDATE ||
q->commandType == CMD_DELETE)
+ {+ exec_check_execsql_targetlist(stmt, q->returningList);
stmt->mod_stmt = true;
+ }+ else+ exec_check_execsql_targetlist(stmt, q->targetList);
}
}
}
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-bugs@postgresql.org
Cc: marko@joh.to, tgl@sss.pgh.pa.us
Subject: Re: BUG #8870: PL/PgSQL, SELECT .. INTO and the number of result columns
In-Reply-To: <52DB2139.20604@joh.to>
References: <20140118232509.26700.59523@wrigleys.postgresql.org> <25794.1390089578@sss.pgh.pa.us> <52DB2139.20604@joh.to>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox