Re: BUG #18546: Attempt to insert default into a non-updatable column of a view fails with a dubious error
Tom Lane <tgl@sss.pgh.pa.us>
From: Tom Lane <tgl@sss.pgh.pa.us>
To: exclusion@gmail.com
Cc: pgsql-bugs@lists.postgresql.org, Dean Rasheed <dean.a.rasheed@gmail.com>
Date: 2024-07-20T16:26:29Z
Lists: pgsql-bugs
Attachments
- fix-bug-18546.patch (text/x-diff) patch
PG Bug reporting form <noreply@postgresql.org> writes: > The following query: > CREATE TABLE t (a int); > CREATE VIEW v AS SELECT a, a + 0 AS a0 FROM t; > INSERT INTO v values (default, default); > raises > ERROR: XX000: attribute number 2 not found in view targetlist > LOCATION: adjust_view_column_set, rewriteHandler.c:3045 > Whilst > INSERT INTO v values (default, 1); > fails with clearer > ERROR: 0A000: cannot insert into column "a0" of view "v" > DETAIL: View columns that are not columns of their base relation are not > updatable. Interesting. I thought this was a wrong-order-of-checks problem, but it's more subtle than that. The "cannot insert into column "a0"" message is produced when view_cols_are_auto_updatable recognizes that we can't assign to that particular column. But view_cols_are_auto_updatable doesn't test the a0 column, because it's told to check the columns that are targeted by the query targetlist after rewriteTargetListIU ... and rewriteTargetListIU has thrown away the DEFAULT markers, on the grounds that the column defaults are null and we don't need to represent that explicitly. The existing code/comments (dating AFAICS to Dean's cab5dc5da) already point out that rewriteTargetListIU can add targetlist items, but we missed the fact that it can delete them too. So it seems like what we need to do is union the original set of target columns with what's listed in the targetlist, as attached. I suppose we could also rethink the decision to throw away null defaults, but that seems much more invasive. Thanks for the report! regards, tom lane
Commits
-
Correctly check updatability of columns targeted by INSERT...DEFAULT.
- feca6c688cd9 12.20 landed
- fd958bbbdf20 16.4 landed
- 96953052a12c 15.8 landed
- 461f47948553 13.16 landed
- 220003b9b937 18.0 landed
- 0d712ec12a90 14.13 landed
- 041a00c4803b 17.0 landed