Re: BUG #17803: Rule "ALSO INSERT ... SELECT ..." fails to substitute default values

Tom Lane <tgl@sss.pgh.pa.us>

From: Tom Lane <tgl@sss.pgh.pa.us>
To: Dean Rasheed <dean.a.rasheed@gmail.com>
Cc: exclusion@gmail.com, pgsql-bugs@lists.postgresql.org
Date: 2023-02-21T15:05:50Z
Lists: pgsql-bugs
Dean Rasheed <dean.a.rasheed@gmail.com> writes:
> It looks like we need something like the attached, to deal with
> product queries that are INSERT ... SELECT queries. In that case the
> VALUES RTE will be at the same index, but in the SELECT part of the
> product query, not the top-level product query itself.

It seems like this bit:

+                    rtr = (RangeTblRef *) linitial(pt->jointree->fromlist);
+                    selectrte = rt_fetch(rtr->rtindex, pt->rtable);
+                    selectquery = selectrte->subquery;

is missing several essential checks.  Is the node extracted from
jointree->fromlist actually a RangeTblRef?  Seems like it could
be a JoinExpr or FromExpr instead; even if it can't be that today,
an IsA check is cheap future-proofing.  Likewise, once you've
got your hands on the RTE, you should check rtekind == RTE_SUBQUERY
rather than assuming it's safe to touch the subquery field.

(I see that getInsertSelectQuery isn't much better about this,
but we should fix that while we're at it.)

			regards, tom lane



Commits

  1. Fix mishandling of OLD/NEW references in subqueries in rule actions.

  2. Fix multi-row DEFAULT handling for INSERT ... SELECT rules.

  3. Fix DEFAULT-handling in multi-row VALUES lists for updatable views.