pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
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: Wed, 22 Jan 2014 02:19:34 +0100
Message-ID: <52DF1CA6.8020800@joh.to> (raw)
In-Reply-To: <52DB2139.20604@joh.to>
References: <20140118232509.26700.59523@wrigleys.postgresql.org>
	<25794.1390089578@sss.pgh.pa.us>
	<52DB2139.20604@joh.to>
List-Unsubscribe: <mailto:majordomo@postgresql.org?body=unsub%20pgsql-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) {

view thread (11+ messages)  latest in thread

Message-ID: <52DF1CA6.8020800@joh.to>
Permalink:  ../52DF1CA6.8020800@joh.to/
Also on:    postgresql.org/message-id/52DF1CA6.8020800@joh.to

 ·  · 

reply

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: <52DF1CA6.8020800@joh.to>
  References: <20140118232509.26700.59523@wrigleys.postgresql.org> <25794.1390089578@sss.pgh.pa.us> <52DB2139.20604@joh.to> <52DF1CA6.8020800@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