pg.ddx.io pgsql-bugs@postgresql.org mailing list archive
help / color / mirror / Atom feedBUG #8870: PL/PgSQL, SELECT .. INTO and the number of result columns
11+ messages / 5 participants
[nested] [flat]
* BUG #8870: PL/PgSQL, SELECT .. INTO and the number of result columns
@ 2014-01-18 23:25 marko@joh.to
2014-01-18 23:37 ` Re: BUG #8870: PL/PgSQL, SELECT .. INTO and the number of result columns Stephen Frost <sfrost@snowman.net>
2014-01-18 23:59 ` Re: BUG #8870: PL/PgSQL, SELECT .. INTO and the number of result columns Tom Lane <tgl@sss.pgh.pa.us>
0 siblings, 2 replies; 11+ messages in thread
From: marko@joh.to @ 2014-01-18 23:25 UTC (permalink / raw)
To: pgsql-bugs
The following bug has been logged on the website:
Bug reference: 8870
Logged by: Marko Tiikkaja
Email address: marko@joh.to
PostgreSQL version: 9.3.2
Operating system: OS X
Description:
I looked more closely into a gripe from Stephen Frost, which lead me into
discovering this behaviour:
=# create function f() returns void as
-# $$declare
$# f1 int;
$# begin
$# select 1, 2 into f1;
$# end
$# $$ language plpgsql;
CREATE FUNCTION
=# select f();
f
---
(1 row)
Which directly contradicts this statement in the docs
(http://www.postgresql.org/docs/9.3/static/plpgsql-statements.html#PLPGSQL-STATEMENTS-SQL-ONEROW):
"If a row or a variable list is used as target, the query's result columns
must exactly match the structure of the target as to number and data types,
or else a run-time error occurs."
This seems to have been broken by commit
8bb3c8fe5413103c30ba8d1bcb3a1f8e46bddf3d. I strongly believe the
documentation is right in this case, and the behaviour ought to change.
--
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs
^ permalink raw reply [nested|flat] 11+ messages in thread
* Re: BUG #8870: PL/PgSQL, SELECT .. INTO and the number of result columns
2014-01-18 23:25 BUG #8870: PL/PgSQL, SELECT .. INTO and the number of result columns marko@joh.to
@ 2014-01-18 23:37 ` Stephen Frost <sfrost@snowman.net>
1 sibling, 0 replies; 11+ messages in thread
From: Stephen Frost @ 2014-01-18 23:37 UTC (permalink / raw)
To: marko@joh.to; +Cc: pgsql-bugs
* marko@joh.to (marko@joh.to) wrote:
> I looked more closely into a gripe from Stephen Frost, which lead me into
> discovering this behaviour:
Well, as I got mentioned here..
The specific gripe I was bitching about on IRC goes thusly:
=*# do $$
declare f1 int;
declare f2 int;
declare f3 int;
begin
select
1,
2,
3
into
f1,
f2
f3;
end
$$ language plpgsql;
DO
The issue above being that when you have a long list of columns being
selected into variables, it can be easy to forget a comma and then
debugging can be very painful.
> Which directly contradicts this statement in the docs
> (http://www.postgresql.org/docs/9.3/static/plpgsql-statements.html#PLPGSQL-STATEMENTS-SQL-ONEROW):
>
> "If a row or a variable list is used as target, the query's result columns
> must exactly match the structure of the target as to number and data types,
> or else a run-time error occurs."
Agreed.
> This seems to have been broken by commit
> 8bb3c8fe5413103c30ba8d1bcb3a1f8e46bddf3d. I strongly believe the
> documentation is right in this case, and the behaviour ought to change.
Yeah, I don't entirely follow the logic in that commit. It's strongly
discouraged to use 'SELECT * INTO', for pretty obvious reasons. We
certainly have no sympathy for users using 'SELECT *' from other
languages when the columns get rearranged underneath them (due to a DROP
COLUMN or similar).
Thanks,
Stephen
Attachments:
signature.asc (application/pgp-signature, 835B) download
^ permalink raw reply [nested|flat] 11+ messages in thread
* Re: BUG #8870: PL/PgSQL, SELECT .. INTO and the number of result columns
2014-01-18 23:25 BUG #8870: PL/PgSQL, SELECT .. INTO and the number of result columns marko@joh.to
@ 2014-01-18 23:59 ` Tom Lane <tgl@sss.pgh.pa.us>
2014-01-19 00:08 ` Re: BUG #8870: PL/PgSQL, SELECT .. INTO and the number of result columns Stephen Frost <sfrost@snowman.net>
2014-01-19 00:50 ` Re: BUG #8870: PL/PgSQL, SELECT .. INTO and the number of result columns Marko Tiikkaja <marko@joh.to>
1 sibling, 2 replies; 11+ messages in thread
From: Tom Lane @ 2014-01-18 23:59 UTC (permalink / raw)
To: marko@joh.to; +Cc: pgsql-bugs
marko@joh.to writes:
> =# create function f() returns void as
> -# $$declare
> $# f1 int;
> $# begin
> $# select 1, 2 into f1;
> $# end
> $# $$ language plpgsql;
> CREATE FUNCTION
> =# select f();
> f
> ---
> (1 row)
> This seems to have been broken by commit
> 8bb3c8fe5413103c30ba8d1bcb3a1f8e46bddf3d.
Did you really test against a pre-2001 plpgsql?
The comments in that commit are correct as far as they go: we *must* be
willing to accept tuples with more or fewer attributes than the row has
fields, or unpleasant things will happen with code that is correct on
its face, if the table it's fetching from has inheritance children
and/or has been subject to ALTER TABLE.
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.
regards, tom lane
--
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs
^ permalink raw reply [nested|flat] 11+ messages in thread
* Re: BUG #8870: PL/PgSQL, SELECT .. INTO and the number of result columns
2014-01-18 23:25 BUG #8870: PL/PgSQL, SELECT .. INTO and the number of result columns marko@joh.to
2014-01-18 23:59 ` Re: BUG #8870: PL/PgSQL, SELECT .. INTO and the number of result columns Tom Lane <tgl@sss.pgh.pa.us>
@ 2014-01-19 00:08 ` Stephen Frost <sfrost@snowman.net>
1 sibling, 0 replies; 11+ messages in thread
From: Stephen Frost @ 2014-01-19 00:08 UTC (permalink / raw)
To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: marko@joh.to; pgsql-bugs
* Tom Lane (tgl@sss.pgh.pa.us) wrote:
> 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.
For my 2c- a compile-time check would be even better than a run-time one
in this case as it would provide pl/pgsql authors notice that they've
goof'd something up. Regardless, I don't see how we can claim to be
respecting what the documentation calls for here and it's causing pain
for our users. I'm on the fence about if we can feel comfortable
back-patching such a change, but I do feel we should figure out a way to
fix it for 9.4.
Thanks,
Stephen
Attachments:
signature.asc (application/pgp-signature, 835B) download
^ permalink raw reply [nested|flat] 11+ messages in thread
* Re: BUG #8870: PL/PgSQL, SELECT .. INTO and the number of result columns
2014-01-18 23:25 BUG #8870: PL/PgSQL, SELECT .. INTO and the number of result columns marko@joh.to
2014-01-18 23:59 ` Re: BUG #8870: PL/PgSQL, SELECT .. INTO and the number of result columns Tom Lane <tgl@sss.pgh.pa.us>
@ 2014-01-19 00:50 ` Marko Tiikkaja <marko@joh.to>
2014-01-22 01:19 ` Re: BUG #8870: PL/PgSQL, SELECT .. INTO and the number of result columns Marko Tiikkaja <marko@joh.to>
1 sibling, 1 reply; 11+ messages in thread
From: Marko Tiikkaja @ 2014-01-19 00:50 UTC (permalink / raw)
To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: pgsql-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-bugs
Attachments:
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);
}
}
}
^ permalink raw reply [nested|flat] 11+ messages in thread
* Re: BUG #8870: PL/PgSQL, SELECT .. INTO and the number of result columns
2014-01-18 23:25 BUG #8870: PL/PgSQL, SELECT .. INTO and the number of result columns marko@joh.to
2014-01-18 23:59 ` Re: BUG #8870: PL/PgSQL, SELECT .. INTO and the number of result columns Tom Lane <tgl@sss.pgh.pa.us>
2014-01-19 00:50 ` Re: BUG #8870: PL/PgSQL, SELECT .. INTO and the number of result columns Marko Tiikkaja <marko@joh.to>
@ 2014-01-22 01:19 ` Marko Tiikkaja <marko@joh.to>
2014-01-22 02:48 ` Re: BUG #8870: PL/PgSQL, SELECT .. INTO and the number of result columns Tom Lane <tgl@sss.pgh.pa.us>
0 siblings, 1 reply; 11+ messages in thread
From: Marko Tiikkaja @ 2014-01-22 01:19 UTC (permalink / raw)
To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: pgsql-bugs
On 1/19/14, 1:50 AM, I wrote:
> But making this check at run time doesn't seem too hard. How broken
> does the attached look?
A bit broken, now that I look at it in detail. In particular, I could
see someone doing something like:
CREATE TABLE lotsofcolumns(f1 int, f2 int, f3 int, ..);
DECLARE
f1 int;
f2 int;
BEGIN
SELECT * INTO f1, f2 FROM lotsofcolumns;
I can't say I think this is a good idea, but not sure breaking this case
is worth it either.
In bug #8893 there was some discussion which I interpreted to mean that
we could improve this a bit by checking the number of returned columns
during compile time if there's no * in the target list. Attached is a
crude patch attempting to do that, which appears to be working. Any
thoughts?
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-bugs
Attachments:
bug_8870_v2.patch (text/plain, 6.7K) download
*** a/src/pl/plpgsql/src/pl_gram.y
--- b/src/pl/plpgsql/src/pl_gram.y
***************
*** 22,27 ****
--- 22,28 ----
#include "parser/scanner.h"
#include "parser/scansup.h"
#include "utils/builtins.h"
+ #include "nodes/nodefuncs.h"
/* Location tracking support --- simpler than bison's default */
***************
*** 97,103 **** static PLpgSQL_row *make_scalar_list1(char *initial_name,
PLpgSQL_datum *initial_datum,
int lineno, int location);
static void check_sql_expr(const char *stmt, int location,
! int leaderlen);
static void plpgsql_sql_error_callback(void *arg);
static PLpgSQL_type *parse_datatype(const char *string, int location);
static void check_labels(const char *start_label,
--- 98,104 ----
PLpgSQL_datum *initial_datum,
int lineno, int location);
static void check_sql_expr(const char *stmt, int location,
! int leaderlen, PLpgSQL_row *check_row);
static void plpgsql_sql_error_callback(void *arg);
static PLpgSQL_type *parse_datatype(const char *string, int location);
static void check_labels(const char *start_label,
***************
*** 1408,1414 **** for_control : for_variable K_IN
PLpgSQL_stmt_fori *new;
/* Check first expression is well-formed */
! check_sql_expr(expr1->query, expr1loc, 7);
/* Read and check the second one */
expr2 = read_sql_expression2(K_LOOP, K_BY,
--- 1409,1415 ----
PLpgSQL_stmt_fori *new;
/* Check first expression is well-formed */
! check_sql_expr(expr1->query, expr1loc, 7, NULL);
/* Read and check the second one */
expr2 = read_sql_expression2(K_LOOP, K_BY,
***************
*** 1470,1476 **** for_control : for_variable K_IN
pfree(expr1->query);
expr1->query = tmp_query;
! check_sql_expr(expr1->query, expr1loc, 0);
new = palloc0(sizeof(PLpgSQL_stmt_fors));
new->cmd_type = PLPGSQL_STMT_FORS;
--- 1471,1477 ----
pfree(expr1->query);
expr1->query = tmp_query;
! check_sql_expr(expr1->query, expr1loc, 0, NULL);
new = palloc0(sizeof(PLpgSQL_stmt_fors));
new->cmd_type = PLPGSQL_STMT_FORS;
***************
*** 2562,2568 **** read_sql_construct(int until,
pfree(ds.data);
if (valid_sql)
! check_sql_expr(expr->query, startlocation, strlen(sqlstart));
return expr;
}
--- 2563,2569 ----
pfree(ds.data);
if (valid_sql)
! check_sql_expr(expr->query, startlocation, strlen(sqlstart), NULL);
return expr;
}
***************
*** 2785,2791 **** make_execsql_stmt(int firsttoken, int location)
expr->ns = plpgsql_ns_top();
pfree(ds.data);
! check_sql_expr(expr->query, location, 0);
execsql = palloc(sizeof(PLpgSQL_stmt_execsql));
execsql->cmd_type = PLPGSQL_STMT_EXECSQL;
--- 2786,2792 ----
expr->ns = plpgsql_ns_top();
pfree(ds.data);
! check_sql_expr(expr->query, location, 0, have_into ? row : NULL);
execsql = palloc(sizeof(PLpgSQL_stmt_execsql));
execsql->cmd_type = PLPGSQL_STMT_EXECSQL;
***************
*** 3357,3362 **** make_scalar_list1(char *initial_name,
--- 3358,3366 ----
return row;
}
+ bool
+ targetlist_magic_count(Node *stmt, int *count);
+
/*
* When the PL/pgSQL parser expects to see a SQL statement, it is very
* liberal in what it accepts; for example, we often assume an
***************
*** 3381,3391 **** make_scalar_list1(char *initial_name,
* If no error cursor is provided, we'll just point at "location".
*/
static void
! check_sql_expr(const char *stmt, int location, int leaderlen)
{
sql_error_callback_arg cbarg;
ErrorContextCallback syntax_errcontext;
MemoryContext oldCxt;
if (!plpgsql_check_syntax)
return;
--- 3385,3396 ----
* If no error cursor is provided, we'll just point at "location".
*/
static void
! check_sql_expr(const char *stmt, int location, int leaderlen, PLpgSQL_row *check_row)
{
sql_error_callback_arg cbarg;
ErrorContextCallback syntax_errcontext;
MemoryContext oldCxt;
+ List *raw_parsetree_list;
if (!plpgsql_check_syntax)
return;
***************
*** 3399,3411 **** check_sql_expr(const char *stmt, int location, int leaderlen)
error_context_stack = &syntax_errcontext;
oldCxt = MemoryContextSwitchTo(compile_tmp_cxt);
! (void) raw_parser(stmt);
MemoryContextSwitchTo(oldCxt);
/* Restore former ereport callback */
error_context_stack = syntax_errcontext.previous;
}
static void
plpgsql_sql_error_callback(void *arg)
{
--- 3404,3487 ----
error_context_stack = &syntax_errcontext;
oldCxt = MemoryContextSwitchTo(compile_tmp_cxt);
! raw_parsetree_list = raw_parser(stmt);
! if (check_row != NULL)
! {
! Node *raw_parse_tree;
! int ncols;
! int fnum;
! int expected_ncols = 0;
!
! for (fnum = 0; fnum < check_row->nfields; fnum++)
! {
! if (check_row->varnos[fnum] < 0)
! continue;
! expected_ncols++;
! }
!
! raw_parse_tree = linitial(raw_parsetree_list);
! if (targetlist_magic_count(raw_parse_tree, &ncols) &&
! ncols != expected_ncols)
! elog(ERROR, "expected %d, got %d", expected_ncols, ncols);
! }
MemoryContextSwitchTo(oldCxt);
/* Restore former ereport callback */
error_context_stack = syntax_errcontext.previous;
}
+ static bool
+ find_a_star_walker(Node *node, void *context)
+ {
+ if (node == NULL)
+ return false;
+ if (IsA(node, A_Star))
+ return true;
+ if (IsA(node, ColumnRef))
+ {
+ ColumnRef *ref = (ColumnRef *) node;
+ /* A_Star can only be the last element */
+ if (IsA(llast(ref->fields), A_Star))
+ return true;
+ }
+ return raw_expression_tree_walker((Node *) node,
+ find_a_star_walker,
+ context);
+ }
+
+ /*
+ * Find the number of columns in a raw statement's targetList (if SELECT) or
+ * returningList (if INSERT, UPDATE or DELETE). Returns false if the number of
+ * columns could not be determined because of an A_Star.
+ */
+ bool
+ targetlist_magic_count(Node *stmt, int *count)
+ {
+ List *tlist;
+
+ if (IsA(stmt, SelectStmt))
+ tlist = ((SelectStmt *) stmt)->targetList;
+ else if (IsA(stmt, InsertStmt))
+ tlist = ((InsertStmt *) stmt)->returningList;
+ else if (IsA(stmt, UpdateStmt))
+ tlist = ((UpdateStmt *) stmt)->returningList;
+ else if (IsA(stmt, DeleteStmt))
+ tlist = ((DeleteStmt *) stmt)->returningList;
+ else
+ elog(ERROR, "unknown nodeTag %d", nodeTag(stmt));
+
+ if (!tlist)
+ {
+ *count = 0;
+ return true;
+ }
+
+ if (raw_expression_tree_walker((Node *) tlist, find_a_star_walker, NULL))
+ return false;
+ *count = list_length(tlist);
+ return true;
+ }
+
static void
plpgsql_sql_error_callback(void *arg)
{
^ permalink raw reply [nested|flat] 11+ messages in thread
* Re: BUG #8870: PL/PgSQL, SELECT .. INTO and the number of result columns
2014-01-18 23:25 BUG #8870: PL/PgSQL, SELECT .. INTO and the number of result columns marko@joh.to
2014-01-18 23:59 ` Re: BUG #8870: PL/PgSQL, SELECT .. INTO and the number of result columns Tom Lane <tgl@sss.pgh.pa.us>
2014-01-19 00:50 ` Re: BUG #8870: PL/PgSQL, SELECT .. INTO and the number of result columns Marko Tiikkaja <marko@joh.to>
2014-01-22 01:19 ` Re: BUG #8870: PL/PgSQL, SELECT .. INTO and the number of result columns Marko Tiikkaja <marko@joh.to>
@ 2014-01-22 02:48 ` Tom Lane <tgl@sss.pgh.pa.us>
2014-01-22 09:56 ` Re: BUG #8870: PL/PgSQL, SELECT .. INTO and the number of result columns Marko Tiikkaja <marko@joh.to>
2014-02-02 16:48 ` Re: BUG #8870: PL/PgSQL, SELECT .. INTO and the number of result columns Marko Tiikkaja <marko@joh.to>
0 siblings, 2 replies; 11+ messages in thread
From: Tom Lane @ 2014-01-22 02:48 UTC (permalink / raw)
To: Marko Tiikkaja <marko@joh.to>; +Cc: pgsql-bugs
Marko Tiikkaja <marko@joh.to> writes:
> On 1/19/14, 1:50 AM, I wrote:
>> But making this check at run time doesn't seem too hard. How broken
>> does the attached look?
> A bit broken, now that I look at it in detail. In particular, I could
> see someone doing something like:
> CREATE TABLE lotsofcolumns(f1 int, f2 int, f3 int, ..);
> DECLARE
> f1 int;
> f2 int;
> BEGIN
> SELECT * INTO f1, f2 FROM lotsofcolumns;
> I can't say I think this is a good idea, but not sure breaking this case
> is worth it either.
Um, I thought the whole point was to complain about that. If this isn't a
mistake, how can you consistently maintain the other one is?
> In bug #8893 there was some discussion which I interpreted to mean that
> we could improve this a bit by checking the number of returned columns
> during compile time if there's no * in the target list. Attached is a
> crude patch attempting to do that, which appears to be working. Any
> thoughts?
Ick. I thought you wanted to do this at first execution, anyway, not in
pl_gram.y.
regards, tom lane
--
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs
^ permalink raw reply [nested|flat] 11+ messages in thread
* Re: BUG #8870: PL/PgSQL, SELECT .. INTO and the number of result columns
2014-01-18 23:25 BUG #8870: PL/PgSQL, SELECT .. INTO and the number of result columns marko@joh.to
2014-01-18 23:59 ` Re: BUG #8870: PL/PgSQL, SELECT .. INTO and the number of result columns Tom Lane <tgl@sss.pgh.pa.us>
2014-01-19 00:50 ` Re: BUG #8870: PL/PgSQL, SELECT .. INTO and the number of result columns Marko Tiikkaja <marko@joh.to>
2014-01-22 01:19 ` Re: BUG #8870: PL/PgSQL, SELECT .. INTO and the number of result columns Marko Tiikkaja <marko@joh.to>
2014-01-22 02:48 ` Re: BUG #8870: PL/PgSQL, SELECT .. INTO and the number of result columns Tom Lane <tgl@sss.pgh.pa.us>
@ 2014-01-22 09:56 ` Marko Tiikkaja <marko@joh.to>
2014-01-22 10:12 ` Re: BUG #8870: PL/PgSQL, SELECT .. INTO and the number of result columns Pavel Stehule <pavel.stehule@gmail.com>
1 sibling, 1 reply; 11+ messages in thread
From: Marko Tiikkaja @ 2014-01-22 09:56 UTC (permalink / raw)
To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: pgsql-bugs
On 1/22/14, 3:48 AM, Tom Lane wrote:
> Marko Tiikkaja <marko@joh.to> writes:
>> SELECT * INTO f1, f2 FROM lotsofcolumns;
>
>> I can't say I think this is a good idea, but not sure breaking this case
>> is worth it either.
>
> Um, I thought the whole point was to complain about that. If this isn't a
> mistake, how can you consistently maintain the other one is?
I'm sure you can see a difference between explicitly listing the wrong
number of columns and using * to ignore trailing columns. That said,
this *should* be an error according to the documentation, so it's
probably not the end of the world if we break it.
>> In bug #8893 there was some discussion which I interpreted to mean that
>> we could improve this a bit by checking the number of returned columns
>> during compile time if there's no * in the target list. Attached is a
>> crude patch attempting to do that, which appears to be working. Any
>> thoughts?
>
> Ick. I thought you wanted to do this at first execution, anyway, not in
> pl_gram.y.
Catching these errors at compile time would be ideal. If we also want
to catch the * cases we could also check the result structure before the
first execution.
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-bugs
^ permalink raw reply [nested|flat] 11+ messages in thread
* Re: BUG #8870: PL/PgSQL, SELECT .. INTO and the number of result columns
2014-01-18 23:25 BUG #8870: PL/PgSQL, SELECT .. INTO and the number of result columns marko@joh.to
2014-01-18 23:59 ` Re: BUG #8870: PL/PgSQL, SELECT .. INTO and the number of result columns Tom Lane <tgl@sss.pgh.pa.us>
2014-01-19 00:50 ` Re: BUG #8870: PL/PgSQL, SELECT .. INTO and the number of result columns Marko Tiikkaja <marko@joh.to>
2014-01-22 01:19 ` Re: BUG #8870: PL/PgSQL, SELECT .. INTO and the number of result columns Marko Tiikkaja <marko@joh.to>
2014-01-22 02:48 ` Re: BUG #8870: PL/PgSQL, SELECT .. INTO and the number of result columns Tom Lane <tgl@sss.pgh.pa.us>
2014-01-22 09:56 ` Re: BUG #8870: PL/PgSQL, SELECT .. INTO and the number of result columns Marko Tiikkaja <marko@joh.to>
@ 2014-01-22 10:12 ` Pavel Stehule <pavel.stehule@gmail.com>
0 siblings, 0 replies; 11+ messages in thread
From: Pavel Stehule @ 2014-01-22 10:12 UTC (permalink / raw)
To: Marko Tiikkaja <marko@joh.to>; +Cc: Tom Lane <tgl@sss.pgh.pa.us>; pgsql-bugs
2014/1/22 Marko Tiikkaja <marko@joh.to>
> On 1/22/14, 3:48 AM, Tom Lane wrote:
>
> Marko Tiikkaja <marko@joh.to> writes:
>>
>>> SELECT * INTO f1, f2 FROM lotsofcolumns;
>>>
>>
>> I can't say I think this is a good idea, but not sure breaking this case
>>> is worth it either.
>>>
>>
>> Um, I thought the whole point was to complain about that. If this isn't a
>> mistake, how can you consistently maintain the other one is?
>>
>
> I'm sure you can see a difference between explicitly listing the wrong
> number of columns and using * to ignore trailing columns. That said, this
> *should* be an error according to the documentation, so it's probably not
> the end of the world if we break it.
>
>
> In bug #8893 there was some discussion which I interpreted to mean that
>>> we could improve this a bit by checking the number of returned columns
>>> during compile time if there's no * in the target list. Attached is a
>>> crude patch attempting to do that, which appears to be working. Any
>>> thoughts?
>>>
>>
>> Ick. I thought you wanted to do this at first execution, anyway, not in
>> pl_gram.y.
>>
>
> Catching these errors at compile time would be ideal. If we also want to
> catch the * cases we could also check the result structure before the first
> execution.
>
It is exactly it what is done by plpgsql_check_function - without
introduction new dependencies and without possible performance impacts.
Regards
Pavel
>
>
> 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-bugs
>
^ permalink raw reply [nested|flat] 11+ messages in thread
* Re: BUG #8870: PL/PgSQL, SELECT .. INTO and the number of result columns
2014-01-18 23:25 BUG #8870: PL/PgSQL, SELECT .. INTO and the number of result columns marko@joh.to
2014-01-18 23:59 ` Re: BUG #8870: PL/PgSQL, SELECT .. INTO and the number of result columns Tom Lane <tgl@sss.pgh.pa.us>
2014-01-19 00:50 ` Re: BUG #8870: PL/PgSQL, SELECT .. INTO and the number of result columns Marko Tiikkaja <marko@joh.to>
2014-01-22 01:19 ` Re: BUG #8870: PL/PgSQL, SELECT .. INTO and the number of result columns Marko Tiikkaja <marko@joh.to>
2014-01-22 02:48 ` Re: BUG #8870: PL/PgSQL, SELECT .. INTO and the number of result columns Tom Lane <tgl@sss.pgh.pa.us>
@ 2014-02-02 16:48 ` Marko Tiikkaja <marko@joh.to>
2014-03-20 08:08 ` Re: BUG #8870: PL/PgSQL, SELECT .. INTO and the number of result columns Marko Tiikkaja <marko@joh.to>
1 sibling, 1 reply; 11+ messages in thread
From: Marko Tiikkaja @ 2014-02-02 16:48 UTC (permalink / raw)
To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: pgsql-bugs
So, should I clean up either (or both?) of these patches? Personally I
think we ought to both check for the easy cases during compilation and
for the harder cases during run-time.
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-bugs
^ permalink raw reply [nested|flat] 11+ messages in thread
* Re: BUG #8870: PL/PgSQL, SELECT .. INTO and the number of result columns
2014-01-18 23:25 BUG #8870: PL/PgSQL, SELECT .. INTO and the number of result columns marko@joh.to
2014-01-18 23:59 ` Re: BUG #8870: PL/PgSQL, SELECT .. INTO and the number of result columns Tom Lane <tgl@sss.pgh.pa.us>
2014-01-19 00:50 ` Re: BUG #8870: PL/PgSQL, SELECT .. INTO and the number of result columns Marko Tiikkaja <marko@joh.to>
2014-01-22 01:19 ` Re: BUG #8870: PL/PgSQL, SELECT .. INTO and the number of result columns Marko Tiikkaja <marko@joh.to>
2014-01-22 02:48 ` Re: BUG #8870: PL/PgSQL, SELECT .. INTO and the number of result columns Tom Lane <tgl@sss.pgh.pa.us>
2014-02-02 16:48 ` Re: BUG #8870: PL/PgSQL, SELECT .. INTO and the number of result columns Marko Tiikkaja <marko@joh.to>
@ 2014-03-20 08:08 ` Marko Tiikkaja <marko@joh.to>
0 siblings, 0 replies; 11+ messages in thread
From: Marko Tiikkaja @ 2014-03-20 08:08 UTC (permalink / raw)
To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: pgsql-bugs
On 2/2/14, 5:48 PM, I wrote:
> So, should I clean up either (or both?) of these patches?
Well, I went ahead and did that anyway. Attached is the cleaned up
version of the one that looks at the raw parse trees and rejects
obviously buggy code. I'll start working on the run-time version
soonish, unless someone wants to shoot me down before that.
This might be too late for 9.4 (though one could argue that this is a
bug fix), but that's all right.
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-bugs
Attachments:
bug_8870_v3.patch (text/plain, 10.8K) download
*** a/src/pl/plpgsql/src/pl_gram.y
--- b/src/pl/plpgsql/src/pl_gram.y
***************
*** 22,27 ****
--- 22,28 ----
#include "parser/scanner.h"
#include "parser/scansup.h"
#include "utils/builtins.h"
+ #include "nodes/nodefuncs.h"
/* Location tracking support --- simpler than bison's default */
***************
*** 97,103 **** static PLpgSQL_row *make_scalar_list1(char *initial_name,
PLpgSQL_datum *initial_datum,
int lineno, int location);
static void check_sql_expr(const char *stmt, int location,
! int leaderlen);
static void plpgsql_sql_error_callback(void *arg);
static PLpgSQL_type *parse_datatype(const char *string, int location);
static void check_labels(const char *start_label,
--- 98,104 ----
PLpgSQL_datum *initial_datum,
int lineno, int location);
static void check_sql_expr(const char *stmt, int location,
! int leaderlen, PLpgSQL_row *check_row);
static void plpgsql_sql_error_callback(void *arg);
static PLpgSQL_type *parse_datatype(const char *string, int location);
static void check_labels(const char *start_label,
***************
*** 106,111 **** static void check_labels(const char *start_label,
--- 107,115 ----
static PLpgSQL_expr *read_cursor_args(PLpgSQL_var *cursor,
int until, const char *expected);
static List *read_raise_options(void);
+ static bool find_a_star_walker(Node *node, void *context);
+ static int tlist_result_column_count(Node *stmt);
+
%}
***************
*** 1408,1414 **** for_control : for_variable K_IN
PLpgSQL_stmt_fori *new;
/* Check first expression is well-formed */
! check_sql_expr(expr1->query, expr1loc, 7);
/* Read and check the second one */
expr2 = read_sql_expression2(K_LOOP, K_BY,
--- 1412,1418 ----
PLpgSQL_stmt_fori *new;
/* Check first expression is well-formed */
! check_sql_expr(expr1->query, expr1loc, 7, NULL);
/* Read and check the second one */
expr2 = read_sql_expression2(K_LOOP, K_BY,
***************
*** 1470,1476 **** for_control : for_variable K_IN
pfree(expr1->query);
expr1->query = tmp_query;
! check_sql_expr(expr1->query, expr1loc, 0);
new = palloc0(sizeof(PLpgSQL_stmt_fors));
new->cmd_type = PLPGSQL_STMT_FORS;
--- 1474,1480 ----
pfree(expr1->query);
expr1->query = tmp_query;
! check_sql_expr(expr1->query, expr1loc, 0, NULL);
new = palloc0(sizeof(PLpgSQL_stmt_fors));
new->cmd_type = PLPGSQL_STMT_FORS;
***************
*** 2562,2568 **** read_sql_construct(int until,
pfree(ds.data);
if (valid_sql)
! check_sql_expr(expr->query, startlocation, strlen(sqlstart));
return expr;
}
--- 2566,2572 ----
pfree(ds.data);
if (valid_sql)
! check_sql_expr(expr->query, startlocation, strlen(sqlstart), NULL);
return expr;
}
***************
*** 2785,2791 **** make_execsql_stmt(int firsttoken, int location)
expr->ns = plpgsql_ns_top();
pfree(ds.data);
! check_sql_expr(expr->query, location, 0);
execsql = palloc(sizeof(PLpgSQL_stmt_execsql));
execsql->cmd_type = PLPGSQL_STMT_EXECSQL;
--- 2789,2795 ----
expr->ns = plpgsql_ns_top();
pfree(ds.data);
! check_sql_expr(expr->query, location, 0, have_into ? row : NULL);
execsql = palloc(sizeof(PLpgSQL_stmt_execsql));
execsql->cmd_type = PLPGSQL_STMT_EXECSQL;
***************
*** 3379,3391 **** make_scalar_list1(char *initial_name,
* (typically "SELECT ") prefixed to the source text. We use this assumption
* to transpose any error cursor position back to the function source text.
* If no error cursor is provided, we'll just point at "location".
*/
static void
! check_sql_expr(const char *stmt, int location, int leaderlen)
{
sql_error_callback_arg cbarg;
ErrorContextCallback syntax_errcontext;
MemoryContext oldCxt;
if (!plpgsql_check_syntax)
return;
--- 3383,3400 ----
* (typically "SELECT ") prefixed to the source text. We use this assumption
* to transpose any error cursor position back to the function source text.
* If no error cursor is provided, we'll just point at "location".
+ *
+ * If check_row is specified, the statement's result column count is compared
+ * against it when the exact number of columns is known. If the columns counts
+ * don't match, a syntax error is raised.
*/
static void
! check_sql_expr(const char *stmt, int location, int leaderlen, PLpgSQL_row *check_row)
{
sql_error_callback_arg cbarg;
ErrorContextCallback syntax_errcontext;
MemoryContext oldCxt;
+ List *raw_parsetree_list;
if (!plpgsql_check_syntax)
return;
***************
*** 3399,3411 **** check_sql_expr(const char *stmt, int location, int leaderlen)
error_context_stack = &syntax_errcontext;
oldCxt = MemoryContextSwitchTo(compile_tmp_cxt);
! (void) raw_parser(stmt);
MemoryContextSwitchTo(oldCxt);
/* Restore former ereport callback */
error_context_stack = syntax_errcontext.previous;
}
static void
plpgsql_sql_error_callback(void *arg)
{
--- 3408,3497 ----
error_context_stack = &syntax_errcontext;
oldCxt = MemoryContextSwitchTo(compile_tmp_cxt);
! raw_parsetree_list = raw_parser(stmt);
! if (check_row != NULL)
! {
! Node *raw_parse_tree;
! int ncols;
! int fnum;
! int expected_ncols = 0;
!
! for (fnum = 0; fnum < check_row->nfields; fnum++)
! {
! if (check_row->varnos[fnum] < 0)
! continue;
! expected_ncols++;
! }
!
! raw_parse_tree = linitial(raw_parsetree_list);
! ncols = tlist_result_column_count(raw_parse_tree);
! if (ncols >= 0 && ncols > expected_ncols)
! ereport(ERROR,
! (errcode(ERRCODE_SYNTAX_ERROR),
! errmsg("query has more expressions than expected by the INTO target")));
! if (ncols >= 0 && ncols < expected_ncols)
! ereport(ERROR,
! (errcode(ERRCODE_SYNTAX_ERROR),
! errmsg("INTO target expects more expressions than the query has")));
! }
MemoryContextSwitchTo(oldCxt);
/* Restore former ereport callback */
error_context_stack = syntax_errcontext.previous;
}
+ /*
+ * Expression tree walker for tlist_result_column_count. Returns true if the
+ * expression tree contains an A_Star node, false otherwise.
+ */
+ static bool
+ find_a_star_walker(Node *node, void *context)
+ {
+ if (node == NULL)
+ return false;
+ if (IsA(node, A_Star))
+ return true;
+ if (IsA(node, ColumnRef))
+ {
+ ColumnRef *ref = (ColumnRef *) node;
+ /* A_Star can only be the last element */
+ if (IsA(llast(ref->fields), A_Star))
+ return true;
+ }
+ return raw_expression_tree_walker((Node *) node,
+ find_a_star_walker,
+ context);
+ }
+
+ /*
+ * Find the number of columns in a raw statement's targetList (if SELECT) or
+ * returningList (if INSERT, UPDATE or DELETE). Returns -1 if the number of
+ * columns could not be determined because of an A_Star.
+ */
+ static int
+ tlist_result_column_count(Node *stmt)
+ {
+ List *tlist;
+
+ if (IsA(stmt, SelectStmt))
+ tlist = ((SelectStmt *) stmt)->targetList;
+ else if (IsA(stmt, InsertStmt))
+ tlist = ((InsertStmt *) stmt)->returningList;
+ else if (IsA(stmt, UpdateStmt))
+ tlist = ((UpdateStmt *) stmt)->returningList;
+ else if (IsA(stmt, DeleteStmt))
+ tlist = ((DeleteStmt *) stmt)->returningList;
+ else
+ elog(ERROR, "unknown nodeTag %d", nodeTag(stmt));
+
+ if (tlist == NIL)
+ return 0;
+
+ if (raw_expression_tree_walker((Node *) tlist, find_a_star_walker, NULL))
+ return -1;
+ return list_length(tlist);
+ }
+
static void
plpgsql_sql_error_callback(void *arg)
{
*** a/src/test/regress/expected/plpgsql.out
--- b/src/test/regress/expected/plpgsql.out
***************
*** 5218,5220 **** NOTICE: outer_func() done
--- 5218,5280 ----
drop function outer_outer_func(int);
drop function outer_func(int);
drop function inner_func(int);
+ create temporary table somecols(a int, b int);
+ -- INTO column counts
+ create or replace function into_counts()
+ returns void as $$
+ declare
+ myresult int;
+ myrec record;
+ begin
+ -- not an error
+ select 1 into myresult;
+ -- we don't know the column count without parse analysis, not an error
+ select * into myresult from somecols;
+ -- records are OK
+ select 1, 2 into myrec;
+ -- error
+ select a, b into myresult from somecols;
+ end;
+ $$ language plpgsql;
+ ERROR: query has more expressions than expected by the INTO target
+ LINE 14: select a, b into myresult from somecols;
+ ^
+ create or replace function into_counts()
+ returns void as $$
+ declare
+ myresult int;
+ begin
+ -- error
+ select into myresult from somecols;
+ end;
+ $$ language plpgsql;
+ ERROR: INTO target expects more expressions than the query has
+ LINE 7: select into myresult from somecols;
+ ^
+ create or replace function into_counts()
+ returns void as $$
+ declare
+ myresult somecols;
+ begin
+ -- not error
+ select 1, 2 into myresult;
+ -- error
+ select 1 into myresult;
+ end;
+ $$ language plpgsql;
+ ERROR: INTO target expects more expressions than the query has
+ LINE 9: select 1 into myresult;
+ ^
+ create or replace function into_counts()
+ returns void as $$
+ declare
+ myresult somecols;
+ begin
+ -- error
+ select 1, 2, 3 into myresult;
+ end;
+ $$ language plpgsql;
+ ERROR: query has more expressions than expected by the INTO target
+ LINE 7: select 1, 2, 3 into myresult;
+ ^
+ drop table somecols;
*** a/src/test/regress/sql/plpgsql.sql
--- b/src/test/regress/sql/plpgsql.sql
***************
*** 4104,4106 **** select outer_outer_func(20);
--- 4104,4160 ----
drop function outer_outer_func(int);
drop function outer_func(int);
drop function inner_func(int);
+
+ create temporary table somecols(a int, b int);
+
+ -- INTO column counts
+ create or replace function into_counts()
+ returns void as $$
+ declare
+ myresult int;
+ myrec record;
+ begin
+ -- not an error
+ select 1 into myresult;
+ -- we don't know the column count without parse analysis, not an error
+ select * into myresult from somecols;
+ -- records are OK
+ select 1, 2 into myrec;
+ -- error
+ select a, b into myresult from somecols;
+ end;
+ $$ language plpgsql;
+
+ create or replace function into_counts()
+ returns void as $$
+ declare
+ myresult int;
+ begin
+ -- error
+ select into myresult from somecols;
+ end;
+ $$ language plpgsql;
+
+ create or replace function into_counts()
+ returns void as $$
+ declare
+ myresult somecols;
+ begin
+ -- not error
+ select 1, 2 into myresult;
+ -- error
+ select 1 into myresult;
+ end;
+ $$ language plpgsql;
+
+ create or replace function into_counts()
+ returns void as $$
+ declare
+ myresult somecols;
+ begin
+ -- error
+ select 1, 2, 3 into myresult;
+ end;
+ $$ language plpgsql;
+
+ drop table somecols;
^ permalink raw reply [nested|flat] 11+ messages in thread
end of thread, other threads:[~2014-03-20 08:08 UTC | newest]
Thread overview: 11+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2014-01-18 23:25 BUG #8870: PL/PgSQL, SELECT .. INTO and the number of result columns marko@joh.to
2014-01-18 23:37 ` Stephen Frost <sfrost@snowman.net>
2014-01-18 23:59 ` Tom Lane <tgl@sss.pgh.pa.us>
2014-01-19 00:08 ` Stephen Frost <sfrost@snowman.net>
2014-01-19 00:50 ` Marko Tiikkaja <marko@joh.to>
2014-01-22 01:19 ` Marko Tiikkaja <marko@joh.to>
2014-01-22 02:48 ` Tom Lane <tgl@sss.pgh.pa.us>
2014-01-22 09:56 ` Marko Tiikkaja <marko@joh.to>
2014-01-22 10:12 ` Pavel Stehule <pavel.stehule@gmail.com>
2014-02-02 16:48 ` Marko Tiikkaja <marko@joh.to>
2014-03-20 08:08 ` Marko Tiikkaja <marko@joh.to>
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