Re: Second RewriteQuery complains about first RewriteQuery in edge case

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Dean Rasheed <dean(dot)a(dot)rasheed(at)gmail(dot)com>
Cc: Kirill Reshke <reshkekirill(at)gmail(dot)com>, Bernice Southey <bernice(dot)southey(at)gmail(dot)com>, PostgreSQL Hackers <pgsql-hackers(at)lists(dot)postgresql(dot)org>
Subject: Re: Second RewriteQuery complains about first RewriteQuery in edge case
Date: 2025-11-26 16:49:56
Message-ID: 1603561.1764175796@sss.pgh.pa.us
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Dean Rasheed <dean(dot)a(dot)rasheed(at)gmail(dot)com> writes:
> The majority of RewriteQuery() is safe if it's called a second time on
> the same query, because it fully expands all non-SELECT rules and
> auto-updatable target views, so the second time round, it would do
> nothing of that kind. However, evidently it's not safe to call
> rewriteTargetListIU() twice. Of course, we could attempt to make
> rewriteTargetListIU() idempotent, but that might be more effort, and
> in any case, processing parts of the query multiple times is a waste
> of effort even if it doesn't fail, so I think the approach taken by
> the patch is the right one.

Not sure. As you say, RewriteQuery() was idempotent up until the
addition of generated columns. Who knows what other places are
relying on that property, that haven't yet been tested against
cases including generated columns? I think there's a good case for
trying to fix it by restoring that idempotency.

Of course, that might prove impractical. Bernice's fix does have
the attraction of being short and sweet.

On the third hand ... if we need to rewrite the query twice in
the first place, is it really true that "Data-modifying WITH clauses
need only be rewritten once per query"? How do we know that whatever
prompted the repeat rewrite didn't change the WITH clauses?

I've not dug into the code yet, just reacting to what I see in this
thread.

regards, tom lane

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message David Geier 2025-11-26 17:22:50 Re: Performance issues with parallelism and LIMIT
Previous Message Fujii Masao 2025-11-26 16:33:25 Re: Allow GUC settings in CREATE SUBSCRIPTION CONNECTION to take effect