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.