Re: Attribute of type record has wrong type error with MERGE ... WHEN NOT MATCHED BY SOURCE THEN DELETE

Dean Rasheed <dean.a.rasheed@gmail.com>

From: Dean Rasheed <dean.a.rasheed@gmail.com>
To: Tom Lane <tgl@sss.pgh.pa.us>
Cc: Tender Wang <tndrwang@gmail.com>, Duncan Sands <duncan.sands@deepbluecap.com>, pgsql-bugs@lists.postgresql.org
Date: 2025-03-10T20:03:15Z
Lists: pgsql-bugs
On Mon, 10 Mar 2025 at 19:11, Tom Lane <tgl@sss.pgh.pa.us> wrote:
>
> I wrote:
> > Yeah, I think we can likely get away with that.  We cannot back-patch
> > the changes that added relid to the outfuncs/readfuncs representation,
> > which means that the RTE's relid won't propagate to parallel workers,
> > but I don't see why they'd need it.  We only need that info to get
> > as far as planning.  I've not tried though.
>
> OK, the attached patch for v15 passes check-world, with or without
> force_parallel_mode.  I'm inclined to commit the rewriteHandler.c
> and parsenodes.h bits in a separate patch for commit log visibility.
>

That looks good to me, on a quick read-through.

However, that's not quite the end of it -- preprocess_function_rtes()
/ inline_set_returning_function() can turn a function RTE into a
subquery RTE, leading to a similar problem:

create table foo (a int, b int);
insert into foo values (1,2);
create or replace function f() returns setof foo as
  $$ select * from foo offset 0 $$ language sql stable;
update foo set b = f.b from f() as f(a,b) where f.a = foo.a returning f;

ERROR:  attribute 3 of type record has wrong type
DETAIL:  Table has type record, but query expects foo.

I tried looking for other places that change an RTE's rtekind, and
haven't managed to find any other problems, but may have missed
something.

Regards,
Dean



Commits

Same data as JSON: GET /api/v1/messages/:b64id/commits the thread's linked commits as JSON, with link sources. API reference →
  1. Repair commits 317aba70e et al for -DWRITE_READ_PARSE_PLAN_TREES.

  2. Build whole-row Vars the same way during parsing and planning.

  3. Preserve RangeTblEntry.relid when expanding a view RTE.

  4. Fix incorrect non-strict join recheck in MERGE WHEN NOT MATCHED BY SOURCE.