Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1UnDj1-0005rH-TQ for pgsql-bugs@arkaria.postgresql.org; Thu, 13 Jun 2013 20:02:28 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.72) (envelope-from ) id 1UnDj1-0003ph-0w for pgsql-bugs@arkaria.postgresql.org; Thu, 13 Jun 2013 20:02:27 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1UnDj0-0003pW-49 for pgsql-bugs@postgresql.org; Thu, 13 Jun 2013 20:02:26 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by magus.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1UnDiy-00082t-5O for pgsql-bugs@postgresql.org; Thu, 13 Jun 2013 20:02:25 +0000 Received: from sss2.sss.pgh.pa.us (tgl@localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.5/8.14.5) with ESMTP id r5DK2MXj016578; Thu, 13 Jun 2013 16:02:22 -0400 (EDT) From: Tom Lane To: david.g.johnston@gmail.com cc: pgsql-bugs@postgresql.org Subject: Re: BUG #8228: Unexpected "set-valued function" with varchar(n) but not varchar In-reply-to: References: Comments: In-reply-to david.g.johnston@gmail.com message dated "Thu, 13 Jun 2013 19:10:50 -0000" Date: Thu, 13 Jun 2013 16:02:22 -0400 Message-ID: <16577.1371153742@sss.pgh.pa.us> X-Pg-Spam-Score: -2.3 (--) 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 david.g.johnston@gmail.com writes: > The following query results in "SQL Error: ERROR: set-valued function called > in context that cannot accept a set" > SELECT *, CASE WHEN id = 2 THEN > (regexp_matches(input_string,'^0*([1-9]\d+)$'))[1] ELSE input_string > END::varchar(30) AS o_l2_a > FROM ( > VALUES (1,''), (2,'0000000049404'),(3,'FROM 10000000876') > ) l0_src (id, input_string) Hm, interesting example. What seems to be happening is that during evaluation of the SELECT list for the first VALUES row, the CASE expression doesn't call regexp_matches() but just returns the ELSE expression. The ExecMakeFunctionResult() call for the cast function then decides that the function's argument expression doesn't return a set, so it changes the node execution pointer so that subsequent executions go through the much simpler ExecMakeFunctionResultNoSets() execution function. And then that spits up when on the next row, the argument expression *does* return a set :-( You could work around that using the trick documented in the regexp_matches documentation to force it to return exactly one row, ie interpose a sub-SELECT: regression=# SELECT *, CASE WHEN id = 2 THEN (select (regexp_matches(input_string,'^0*([1-9]\d+)$'))[1]) ELSE input_string END::varchar(30) AS o_l2_a FROM ( VALUES (1,''), (2,'0000000049404'),(3,'FROM 10000000876') ) l0_src (id, input_string); id | input_string | o_l2_a ----+------------------+------------------ 1 | | 2 | 0000000049404 | 49404 3 | FROM 10000000876 | FROM 10000000876 (3 rows) Not sure about non-hack fixes. I guess we need to analyze can-it-return-a-set statically instead of believing the first execution result, but that might add an unpleasant amount of startup overhead. 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