| From: | Dean Rasheed <dean(dot)a(dot)rasheed(at)gmail(dot)com> |
|---|---|
| To: | Kirill Reshke <reshkekirill(at)gmail(dot)com> |
| Cc: | 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 15:04:20 |
| Message-ID: | CAEZATCUSR8Lf2FY6vcwnaSYeqfLTztdkfjBh+k-DCV5jM45xbw@mail.gmail.com |
| Views: | Whole Thread | Raw Message | Download mbox | Resend email |
| Thread: | |
| Lists: | pgsql-hackers |
On Wed, 26 Nov 2025 at 12:13, Kirill Reshke <reshkekirill(at)gmail(dot)com> wrote:
>
> On Wed, 26 Nov 2025 at 13:34, Bernice Southey <bernice(dot)southey(at)gmail(dot)com> wrote:
>
> > I get an odd error if a CTE inserts a GENERATED ALWAYS AS IDENTITY
> > column, and then tries to modify an automatically updatable view.
> >
> > create table t(i int generated always as identity);
> > create table base(j int);
> > create view v as select * from base;
> >
> > with cte as (insert into t default values returning i)
> > delete from v using cte where j = i;
> > ERROR: cannot insert a non-DEFAULT value into column "i" Column "i" is
> > an identity column defined as GENERATED ALWAYS.
>
> Nice catch!
Indeed.
> I think we have a bug worth fixing here. However, I am not sure about
> the approach. For me, a safer option would be
> to disallow queries which try to rewrite something for a second time...
No, I disagree, there's no reason a query like this should be
disallowed. The fact that the rewriter is attempting to rewrite parts
of the query multiple times is the bug, albeit one that was probably
harmless before the introduction of generated columns.
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.
Regards,
Dean
| From | Date | Subject | |
|---|---|---|---|
| Next Message | Maxim Orlov | 2025-11-26 15:23:18 | Re: POC: make mxidoff 64 bits |
| Previous Message | David G. Johnston | 2025-11-26 15:02:31 | Re: Additional info for CREATE ROLE with REPLICATION |