Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1e7qGy-0003WA-6M for pgsql-bugs@arkaria.postgresql.org; Thu, 26 Oct 2017 22:05:08 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84_2) (envelope-from ) id 1e7qGw-0005h6-HW for pgsql-bugs@arkaria.postgresql.org; Thu, 26 Oct 2017 22:05:06 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1e7qGw-0005gu-1D for pgsql-bugs@postgresql.org; Thu, 26 Oct 2017 22:05:06 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1e7qGs-0002lQ-RB for pgsql-bugs@postgresql.org; Thu, 26 Oct 2017 22:05:05 +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 v9QM4xaN031442; Thu, 26 Oct 2017 18:04:59 -0400 From: Tom Lane To: Michael Paquier cc: samuel.horwitz@gmail.com, PostgreSQL mailing lists Subject: Re: BUG #14876: Segmentation fault with JSONB column used in store proc that gets used by view and later altered In-reply-to: <26672.1509050556@sss.pgh.pa.us> References: <20171026184035.1471.82810@wrigleys.postgresql.org> <26672.1509050556@sss.pgh.pa.us> Comments: In-reply-to Tom Lane message dated "Thu, 26 Oct 2017 16:42:36 -0400" MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="----- =_aaaaaaaaaa0" Content-ID: <31429.1509055479.0@sss.pgh.pa.us> Date: Thu, 26 Oct 2017 18:04:59 -0400 Message-ID: <31441.1509055499@sss.pgh.pa.us> 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 ------- =_aaaaaaaaaa0 Content-Type: text/plain; charset="us-ascii" Content-ID: <31429.1509055479.1@sss.pgh.pa.us> I wrote: > Thanks for posting the reproducer. The attached seems to fix it, but > now that I've seen this, I wonder if there are other similar cases. After some not-very-exhaustive looking around, the only thing I've found that seems closely related is expandRTE's behavior for a function returning composite in an RTE_FUNCTION RTE. It's clearly possible for somebody to have added columns to the composite type since the calling query was parsed, so there is a comparable hazard in that case as well. But what that code path does is to ignore any columns beyond what it saw originally (which it's memorialized in funccolcount; see the comment for struct RangeTblFunction). To be consistent with that, it seems like what the RTE_SUBQUERY case ought to do is ignore columns beyond the length of eref->colnames. This is probably less useful than what I posted first --- it means you don't get to see any added columns in the result of "subqueryname.*". But I doubt we want different behaviors in the two cases. regards, tom lane ------- =_aaaaaaaaaa0 Content-Type: text/x-diff; name="make-expandRTE-ignore-new-columns.patch"; charset="us-ascii" Content-ID: <31429.1509055479.2@sss.pgh.pa.us> Content-Description: make-expandRTE-ignore-new-columns.patch Content-Transfer-Encoding: quoted-printable diff --git a/src/backend/parser/parse_relation.c b/src/backend/parser/parse= _relation.c index ca32a37..e89bebf 100644 *** a/src/backend/parser/parse_relation.c --- b/src/backend/parser/parse_relation.c *************** expandRTE(RangeTblEntry *rte, int rtinde *** 2205,2213 **** varattno++; Assert(varattno =3D=3D te->resno); =20=20 if (colnames) { - /* Assume there is one alias per target item */ char *label =3D strVal(lfirst(aliasp_item)); =20=20 *colnames =3D lappend(*colnames, makeString(pstrdup(label))); --- 2205,2223 ---- varattno++; Assert(varattno =3D=3D te->resno); =20=20 + /* + * In scenarios where columns have been added to a view + * since the outer query was originally parsed, there can + * be more items in the subquery tlist than the outer + * query expects. We should ignore such extra column(s) + * --- compare the behavior for composite-returning + * functions, in the RTE_FUNCTION case below. + */ + if (!aliasp_item) + break; +=20 if (colnames) { char *label =3D strVal(lfirst(aliasp_item)); =20=20 *colnames =3D lappend(*colnames, makeString(pstrdup(label))); ------- =_aaaaaaaaaa0 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 ------- =_aaaaaaaaaa0--