Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1W4gaY-0005hf-Ur for pgsql-bugs@arkaria.postgresql.org; Sun, 19 Jan 2014 00:50:11 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1W4gaY-0003Ap-BF for pgsql-bugs@arkaria.postgresql.org; Sun, 19 Jan 2014 00:50:10 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1W4gaX-0003Ac-BL for pgsql-bugs@postgresql.org; Sun, 19 Jan 2014 00:50:09 +0000 Received: from mail-lb0-f171.google.com ([209.85.217.171]) by magus.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1W4gaU-0000GQ-RO for pgsql-bugs@postgresql.org; Sun, 19 Jan 2014 00:50:09 +0000 Received: by mail-lb0-f171.google.com with SMTP id c11so3959934lbj.2 for ; Sat, 18 Jan 2014 16:50:05 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:message-id:date:from:user-agent:mime-version:to :cc:subject:references:in-reply-to:content-type; bh=ABvlFEw58EnUFh1NzfmWKytGOacOmHHLIFbye1u/ne0=; b=AtTzidZ5S7ZliWc5eTyn0rCkKfRv1udrYkfjYNWLZTvSj5o+N9vJaHp0ekyCOk6znE /hR9G/1eyLbbsJLZINbbE+bx9UioST8iUCXiVjvU8g9Wa+QgI67FEpBK61F9qrrijk/Q Qri8X/wtppua9rGSU70h14N+sxmef6vFHtkOSQmDUrU9KTDRnhwoJ8zYAey/ao4YsOBs 5y3OPSGufQcz7gCmyJozsN+FXZq5y6T6ZRUl/a+UY9ekl9vI+Mv2CDfMzGTiMqT87rOl KzIJwnDRZBWwDKnvsyhe/Xs/PmEhQnFJoEDtx8g/w6seQV/nYh01cRi0C3GxIGejQAw0 f4Jw== X-Gm-Message-State: ALoCoQmDGtZYOtdr5EAOdFZIBXsXdZivJiO8tIsmUEFHF+yhCVNN09NG8jIZq1L4h3yt2NBgTsCm X-Received: by 10.112.52.74 with SMTP id r10mr213986lbo.36.1390092605866; Sat, 18 Jan 2014 16:50:05 -0800 (PST) Received: from dalli.local ([94.234.170.161]) by mx.google.com with ESMTPSA id bo10sm10224229lbb.16.2014.01.18.16.50.03 for (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Sat, 18 Jan 2014 16:50:04 -0800 (PST) Message-ID: <52DB2139.20604@joh.to> Date: Sun, 19 Jan 2014 01:50:01 +0100 From: Marko Tiikkaja User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.9; rv:24.0) Gecko/20100101 Thunderbird/24.2.0 MIME-Version: 1.0 To: Tom Lane CC: pgsql-bugs@postgresql.org Subject: Re: BUG #8870: PL/PgSQL, SELECT .. INTO and the number of result columns References: <20140118232509.26700.59523@wrigleys.postgresql.org> <25794.1390089578@sss.pgh.pa.us> In-Reply-To: <25794.1390089578@sss.pgh.pa.us> Content-Type: multipart/mixed; boundary="------------010601010908060209000207" X-Pg-Spam-Score: -2.6 (--) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-bugs Precedence: bulk Sender: pgsql-bugs-owner@postgresql.org This is a multi-part message in MIME format. --------------010601010908060209000207 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit 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 --------------010601010908060209000207 Content-Type: text/plain; charset=UTF-8; x-mac-type="0"; x-mac-creator="0"; name="bug_8870.patch" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="bug_8870.patch" *** 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); } } } --------------010601010908060209000207 Content-Type: text/plain Content-Disposition: inline Content-Transfer-Encoding: 8bit MIME-Version: 1.0 -- Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-bugs --------------010601010908060209000207--