Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1Wpj6e-0001T6-5Q for pgsql-bugs@arkaria.postgresql.org; Wed, 28 May 2014 19:01:44 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1Wpj6d-0001dB-L2 for pgsql-bugs@arkaria.postgresql.org; Wed, 28 May 2014 19:01:43 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:DHE_RSA_AES_256_CBC_SHA256:256) (Exim 4.80) (envelope-from ) id 1Wpj6c-0001d4-L7 for pgsql-bugs@postgresql.org; Wed, 28 May 2014 19:01:42 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by magus.postgresql.org with esmtps (TLS1.2:DHE_RSA_AES_256_CBC_SHA256:256) (Exim 4.80) (envelope-from ) id 1Wpj6V-00060h-6r for pgsql-bugs@postgresql.org; Wed, 28 May 2014 19:01:41 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.4/8.14.4) with ESMTP id s4SJ1WTj009370; Wed, 28 May 2014 15:01:32 -0400 From: Tom Lane To: Artiom Makarov cc: pgsql-bugs Subject: Re: pl/pgsql incorrect syntax checking on select .... into ... ? In-reply-to: References: Comments: In-reply-to Artiom Makarov message dated "Wed, 28 May 2014 16:04:32 +0400" Date: Wed, 28 May 2014 15:01:32 -0400 Message-ID: <9369.1401303692@sss.pgh.pa.us> 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 Artiom Makarov writes: > Missed comma in select ... into construction don't raise ERROR both on > compiling and run time. Unfortunately, that's perfectly legal syntax :-(. The first problem is that long ago, somebody failed to make up their mind about where the INTO clause could go in a plpgsql SELECT INTO, and thought it would be cute to allow it anywhere. What this means is that the plpgsql parser gobbles as much as looks like a syntactically valid variable list after INTO, and removes that from the statement, and then sees if the main SQL parser likes what's left. So starting with > -- ---- missed comma \/ ---- > select i1,i2, i3, i4 into var_i1,var_i2 var_i3, var_i4 from temp_table; we remove "into var_i1,var_i2": select i1,i2, i3, i4 var_i3, var_i4 from temp_table; The second problem is that that's valid SQL syntax, because even longer ago, somebody thought it'd be a good idea to allow AS to be omitted before SELECT output aliases. So "var_i3" is a column alias to be applied to i4, and then var_i4 is just a constant reference so far as this SELECT is concerned. The third problem is that SELECT INTO doesn't complain about extra output columns; otherwise we might hope to at least get a complaint about that. According to the comments in the source, this is necessary because exec_move_row is sometimes applied to tuples from inheritance child tables that might have extra physical columns. I'm not really sure that's still true, but even if it's not, people might not appreciate it if we started throwing an error for code that used to be accepted. In short: we could only make this throw an error if we were willing to break some subset of existing valid (more or less) queries. Personally I've always thought that "INTO anywhere" was a pretty darn bad idea, but it's probably much too late to change that. 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