Re: BUG #16801: Invalid memory access on WITH RECURSIVE with nested WITHs

Michael Paquier <michael@paquier.xyz>

From: Michael Paquier <michael@paquier.xyz>
To: Tom Lane <tgl@sss.pgh.pa.us>
Cc: Alexander Lakhin <exclusion@gmail.com>, pgsql-bugs@lists.postgresql.org
Date: 2021-02-25T01:30:41Z
Lists: pgsql-bugs
On Wed, Feb 24, 2021 at 07:36:26PM -0500, Tom Lane wrote:
> Here's a patch that I think fixes it correctly, including a test
> case that doesn't work with your patch.

Yes, thanks.  While looking at that this morning, I have been able to
get a crash with my previous patch once I used more nesting in those
CTEs and your test is much simpler.  I also got a test case able to
break things the same way in checkWellFormedRecursionWalker():
WITH RECURSIVE outermost(x) AS (
  SELECT 1
  UNION (WITH innermost as (WITH innermost2 AS (SELECT 2) SELECT * FROM innermost2)
         SELECT * FROM outermost
         UNION SELECT * FROM innermost)
)
SELECT * FROM outermost ORDER BY 1;

Your patch does not have a test for that, but it fixes the list
handling.  With more nested levels and some UNIONs, the patch I sent
previously would equally break, though I am not sure we need more than
what I am sending here.  What do you think about this extra test?
--
Michael

Commits

  1. Fix list-manipulation bug in WITH RECURSIVE processing.