| 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-30 20:34:46 |
| Message-ID: | DN8TRN0MHCI8.3LJW474HJBMOT@gmail.com |
| Views: | Whole Thread | Raw Message | Download mbox | Resend email |
| Thread: | |
| Lists: | pgsql-hackers |
Hi Zsolt,
> +/*
> + * Preparation phase for
> + *
> + * ALTER COLUMN col ADD GENERATED USING CONSTRAINT name STORED
> + *
> + * In an inheritance hierarchy, it is only valid to alter the type of the
> + * whole hierarchy at once.
> + */
> +static void
>
> Is this comment a copy-past mistake?
No copy-paste involved, but it does look incomplete. I was trying to
explain what the main concern of this function is (whole-hierarchy
consistency in the case of recursion). I've changed it to
/*
* ALTER COLUMN col ADD GENERATED USING CONSTRAINT name STORED
*
* In phase 2, we need to enforce that the generated state of a column is
* changed at once for the whole inheritance hierarchy.
*/
static void
ATPrepAddGenStored(Relation rel, AlterTableCmd *cmd, bool recurse, bool recursing)
> 1. adding an overload can change the generated expression:
>
> CREATE FUNCTION val(anyelement) RETURNS int LANGUAGE SQL IMMUTABLE AS
> 'SELECT 1';
> CREATE TABLE probe(a int, b int, CONSTRAINT k CHECK
> (pg_datum_image_equal(b, val(a))));
> INSERT INTO probe VALUES (10,1);
> CREATE FUNCTION val(int) RETURNS int LANGUAGE SQL IMMUTABLE AS 'SELECT 2';
> INSERT INTO probe VALUES (20,1);
> ALTER TABLE probe ALTER b ADD GENERATED USING CONSTRAINT k STORED;
> INSERT INTO probe(a) VALUES (30);
> -- ERROR: new row for relation "probe" violates check constraint "k"
> -- DETAIL: Failing row contains (30, 2).
>
> This setup seems broken from multiple angles, so maybe the best
> approach is to reject the alter?
Mh, interesting. My first hunch was that we cannot protect the user from
this. It seemed equivalent to changing the function body. The function's
OID would stay the same, and we wouldn't have a way to detect that it
has changed.
However, we can compare the generator expression we're adding to the
column with the one used in the constraint, and bail if they're not
equal(). That doesn't seem to hurt any legitimate use case (the tests
still pass), and it does detect that the "val()" in the expression now
resolves to a different OID. This makes the whole thing a tiny bit more
robust, as now we explicitly check any situation which could lead to a
different expression tree at conversion time. Thanks for catching this!
This case now fails with:
ERROR: cannot convert column "b" to generated
DETAIL: The constraint expression has changed since the constraint "chk_gen" was created.
HINT: Check that the types used in the expression are still the same, and no overloads were added.
> 2. Should self-referencing foreign-keys work?
>
> CREATE TABLE tree(a int, id int UNIQUE, parent_id int REFERENCES
> tree(id) ON UPDATE CASCADE);
> ALTER TABLE tree ADD CONSTRAINT k CHECK (pg_datum_image_equal(id, a));
> ALTER TABLE tree ALTER id ADD GENERATED USING CONSTRAINT k STORED;
> -- ERROR: cannot convert column "id" to generated
> -- DETAIL: Column "id" is part of a foreign key with a restricted ON
> DELETE / ON UPDATE action.
My mistake, I was checking only conrelid, but of course I should check
conkey as well. Fixed :)
> 3. the trigger check now also refuses triggers on other tables
>
> CREATE TABLE coords(x int, y int);
> CREATE TABLE samples(n int, pt coords);
> CREATE TRIGGER inspect_pt BEFORE UPDATE ON samples FOR EACH ROW
> WHEN ((NEW.pt).y IS NULL) EXECUTE FUNCTION
> suppress_redundant_updates_trigger();
> ALTER TABLE coords ADD CONSTRAINT k CHECK (pg_datum_image_equal(y, x+1));
> ALTER TABLE coords ALTER y ADD GENERATED USING CONSTRAINT k STORED;
> -- ERROR: cannot convert column "y" to generated
> -- DETAIL: Column NEW."y" is referenced by the WHEN clause of trigger
> "inspect_pt".
Another mistake similar to the above, it seems I have some trouble
navigating the dependencies network. Thanks for checking this :)
> 4. DEFAULT is reported as generated column
>
> CREATE TABLE d(x int, y int, r int);
> ALTER TABLE d ALTER r SET DEFAULT ((NULL::d).y);
> ALTER TABLE d ADD CONSTRAINT k CHECK (pg_datum_image_equal(y, x+1));
> ALTER TABLE d ALTER y ADD GENERATED USING CONSTRAINT k STORED;
> -- ERROR: cannot convert column "y" to generated
> -- DETAIL: Column "y" is referenced by generated column "r".
>
> Also, similarly to the above example, changing the order of operations
> and adding the default after the ADD GENERATED works.
Yes this case should probably be allowed (the column isn't actually used
in the expression, it's evaluated and stored as const NULL in the
default). I added an explicit check that the referencing column is
indeed generated.
Thanks for another round of testing, and looking forward to your
thoughts.
Regards,
Alberto
--
Alberto Piai
Sensational AG
Zürich, Switzerland
| Attachment | Content-Type | Size |
|---|---|---|
| v15-0001-Add-SQL-level-datum-equality-tests.patch | text/plain | 8.8 KB |
| v15-0002-Support-changing-a-column-into-a-stored-generate.patch | text/plain | 97.1 KB |
| From | Date | Subject | |
|---|---|---|---|
| Previous Message | Lukas Fittl | 2026-11-30 20:03:29 | Re: Stack-based tracking of per-node WAL/buffer usage |