| From: | Alberto Piai <alberto(dot)piai(at)gmail(dot)com> |
|---|---|
| To: | Zsolt Parragi <zsolt(dot)parragi(at)percona(dot)com>, Alberto Piai <alberto(dot)piai(at)gmail(dot)com> |
| Cc: | pgsql-hackers(at)lists(dot)postgresql(dot)org, Xiaoxuan Li <xiaoxuan(dot)li(dot)dev(at)gmail(dot)com>, Laurenz Albe <laurenz(dot)albe(at)cybertec(dot)at>, Matthias van de Meent <boekewurm+postgres(at)gmail(dot)com>, Álvaro Herrera <alvherre(at)kurilemu(dot)de> |
| Subject: | Re: Adding a stored generated column without long-lived locks |
| Date: | 2026-11-25 18:00:55 |
| Message-ID: | DN4HILE90P4W.G94H1S28J6J3@gmail.com |
| Views: | Whole Thread | Raw Message | Download mbox | Resend email |
| Thread: | |
| Lists: | pgsql-hackers |
Hi Zsolt,
thank you so much for testing this!
On Wed Nov 18, 2026 at 11:40 PM CET, Zsolt Parragi wrote:
>
> 1. The generation expression can change when it is reparsed based on
> session GUCs
>
> CREATE TABLE g (a float8, b float8);
> INSERT INTO g VALUES (1, 1 + '3.141592653589793'::float8);
> ALTER TABLE g ADD CONSTRAINT c CHECK (pg_datum_image_equal(b, a +
> '3.141592653589793'::float8));
> SET extra_float_digits = 0;
> ALTER TABLE g ALTER b ADD GENERATED USING CONSTRAINT c STORED;
> RESET extra_float_digits;
> \d g
> b | double precision | generated always as (a +
> '3.14159265358979'::double precision) stored
> UPDATE g SET a = a;
> -- ERROR: new row for relation "g" violates check constraint "c"
> -- DETAIL: Failing row contains (1, 4.14159265358979).
>
> float4/timestamptz is also affected.
Nice catch, I fixed this by temporarily setting extra_float_digits=3 and
datestyle=ISO when reparsing the expression. This seems to be a common
workaround, used in a few other places too so I think it shouldn't be
problematic here. (Ideally we could instead tell the deparse
infrastructure that we need output which is round-trip safe, and not
merely meant for display... but the final output is anyway produced by
the type's output function...)
In a sibling thread about logical replication I saw that they are
discussing the same problem, and also need to pin
intervalstyle=postgres. Pg_dump too explicitly says "avoid using
sql_standard intervalstyle".
For the purpose of this command though I couldn't come up with an
interval value that actually is problematic when deparsed/reparsed with
the same setting like we need to do, so I left that out. Happy to add a
test if you can see something I didn't :)
> 2. BEFORE triggers with WHEN on NEW.b survive the conversion
>
> CREATE TABLE t (a int, b int); INSERT INTO t VALUES (1, 2);
> ALTER TABLE t ADD CONSTRAINT c CHECK (pg_datum_image_equal(b, a*2));
> CREATE TRIGGER w_null BEFORE INSERT OR UPDATE ON t FOR EACH ROW WHEN
> (NEW.b IS NULL) EXECUTE FUNCTION trg();
> CREATE TRIGGER w_100 BEFORE INSERT OR UPDATE ON t FOR EACH ROW WHEN
> (NEW.b = 100) EXECUTE FUNCTION trg();
> ALTER TABLE t ALTER b ADD GENERATED USING CONSTRAINT c STORED; -- succeeds
> INSERT INTO t (a) VALUES (50); -- fires w_null, not w_100, stored b = 100
> -- pg_dump | psql:
> -- ERROR: BEFORE trigger's WHEN condition cannot reference NEW
> generated columns
>
> CREATE TRIGGER refuses this combination (trigger.c, "BEFORE trigger's
> WHEN condition cannot reference NEW generated columns"), because the
> generated value isn't computed yet when WHEN runs.
> Note that this is partially preexisting, the whole-row variant (WHEN
> (NEW IS DISTINCT FROM OLD)) is already reachable on master with ADD
> COLUMN ... GENERATED, but the NEW.b one is new.
I could detect this for both a direct reference to the column and a
whole-row reference. The command now bails with (respectively):
ERROR: cannot convert column "b" to generated
DETAIL: Column NEW."b" is referenced by the WHEN clause of trigger "t_trigger".
and
ERROR: cannot convert column "b" to generated
DETAIL: The WHEN clause of trigger "t_trigger" references a row which encompasses NEW."b".
I added tests for both.
> 3. Multiple inheritance
>
> CREATE TABLE p1 (a int, b int); CREATE TABLE p2 (a int, b int);
> CREATE TABLE c () INHERITS (p1, p2);
> ALTER TABLE p1 ADD CONSTRAINT k CHECK (pg_datum_image_equal(b, a*2));
> INSERT INTO c VALUES (1, 2);
> ALTER TABLE p1 ALTER b ADD GENERATED USING CONSTRAINT k STORED; -- succeeds
> ALTER TABLE p1 DROP CONSTRAINT k;
> UPDATE p2 SET b = 999;
> SELECT a, b FROM c; -- 1 | 999 (expression gives 2)
> -- pg_dump | psql:
> -- ERROR: inherited column "b" has a generation conflict
> -- ERROR: relation "public.c" does not exist
>
> c.b becomes generated while p2.b stays a regular column. This seems to
> come from DROP EXPRESSION: it has the same problem in the other
> direction on master (drop the expression on one of two generated
> parents), which probably deserves a separate fix.
This was pretty interesting to look into. I don't see a way for the user
to transform the whole hierarchy when multiple inheritance adds parent
nodes which aren't reachable by the root node, but I can detect that
case and bail. So this shape is allowed when running the command on "A":
A
/ \
B C
\ /
D
while this is rejected:
A A'
/ \ /
B C
\ /
D
The check is for now inlined in the ATPrep*() function for this command,
but ideally it would be done in such a way that it can be reused by DROP
EXPRESSION. I'd keep this separated from this patch though and start a
new thread. At a first glance, I'd like if this was checked (optionally
of course) by ATSimpleRecursion(), because that already calls
find_all_inheritors() (the function that can count the n. of parent
nodes we've visited for each node, allowing us to detect extra parents).
Doing it there would save us an additional call to find_all_inheritors()
which I guess can be quite a lot of work when called on big inheritance
hierarchies.
Anyway what I've done seems good enough for this patch, we can have a
look at the above separately. I've added tests for both shapes mentioned
above.
> 4. Typed tables are converted although CREATE TABLE OF refuses generated columns
>
> CREATE TYPE tt AS (a int, b int);
> CREATE TABLE s6 OF tt;
> INSERT INTO s6 VALUES (1, 2);
> ALTER TABLE s6 ADD CONSTRAINT c CHECK (pg_datum_image_equal(b, a + 1));
> ALTER TABLE s6 ALTER b ADD GENERATED USING CONSTRAINT c STORED; -- succeeds
> -- pg_dump | psql:
> -- ERROR: generated columns are not supported on typed tables
> -- ERROR: relation "public.s6" does not exist
I did not think of this at all. Thanks! It's now detected and rejected,
with a regression test.
> 5. FK check also rejects the referenced side
>
> CREATE TABLE pk (a int, b int UNIQUE);
> CREATE TABLE fk (x int REFERENCES pk(b) ON UPDATE CASCADE);
> ALTER TABLE pk ADD CONSTRAINT k CHECK (pg_datum_image_equal(b, a*2));
> ALTER TABLE pk ALTER b ADD GENERATED USING CONSTRAINT k STORED;
> -- ERROR: cannot convert column "b" to generated
> -- DETAIL: Column "b" is part of a foreign key with an ON DELETE / ON
> UPDATE action.
>
> Master only restricts the actions when the referencing columns are
> generated, a generated UNIQUE column can be referenced with ON UPDATE
> CASCADE.
> The detail is also a bit misleading, ON DELETE CASCADE is accepted.
I forgot to check that we're on the referencing side (conrelid). Fixed,
and added a test. I also changed the error message, hopefully it's
clearer now:
ERROR: cannot convert column "b" to generated
-DETAIL: Column "b" is part of a foreign key with an ON DELETE / ON UPDATE action.
+DETAIL: Column "b" is part of a foreign key with a restricted ON DELETE / ON UPDATE action.
What do you think?
Thanks again for your testing effort,
Alberto
--
Alberto Piai
Sensational AG
Zürich, Switzerland
| Attachment | Content-Type | Size |
|---|---|---|
| v14-0001-Add-SQL-level-datum-equality-tests.patch | text/plain | 8.8 KB |
| v14-0002-Support-changing-a-column-into-a-stored-generate.patch | text/plain | 90.4 KB |
| From | Date | Subject | |
|---|---|---|---|
| Next Message | Peter Geoghegan | 2026-11-25 18:08:30 | Re: GiST multirange index scans can fail to return rows |
| Previous Message | Kirill Reshke | 2026-11-25 17:58:40 | Re: OOM kill in xmlelement() with large arrays |