Segfault in backend CTE code - Mailing list pgsql-bugs

From Phil Sorber
Subject Segfault in backend CTE code
Date
Msg-id CADAkt-iWfSvovEMd4-sT15OQ+YK4FC_YbZDefxwUK5EwrWe4bA@mail.gmail.com
Whole thread Raw
Responses Re: Segfault in backend CTE code  (Tom Lane <tgl@sss.pgh.pa.us>)
List pgsql-bugs
Running Postgres 9.1.2.

I've attached a backtrace. Looking at the backtrace it looks like
ExecGetResultType() gets called with a NULL planstate and causes the
segmentation fault:

https://github.com/postgres/postgres/blob/master/src/backend/executor/execUtils.c#L470

Following the stack I see that an optimization for writeable CTE's
inserts a NULL subplanstate:

https://github.com/postgres/postgres/blob/master/src/backend/executor/execMain.c#L2344

ExecInitCteScan() is what eventually passes it to ExecGetResultType():

https://github.com/postgres/postgres/blob/master/src/backend/executor/nodeCtescan.c#L255

I've also attached a proposed fix. In this optimized case it says that
we won't ever use the subplan anyway, so I figured that not setting
the scan tuple type won't matter. I also added an Assert() to
ExecGetResultType(). I modified the declaration of 'slot' to remove a
compiler warning. This patch is against master but should backport to
9.1 cleanly. It also passed all regression tests. If you end up using
this patch please also credit Rick Pufky who helped me with this.

Attachment

pgsql-bugs by date:

Previous
From: Bridget Frey
Date:
Subject: Re: BUG #6200: standby bad memory allocations on SELECT
Next
From: Tom Lane
Date:
Subject: Re: Segfault in backend CTE code