Re: Adding a stored generated column without long-lived locks

From: Zsolt Parragi <zsolt(dot)parragi(at)percona(dot)com>
To: 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 23:31:57
Message-ID: CAN4CZFPuJGL=QG7VTrUmLpXHg=of-137x0mW-y_zpvV7dFm9FA@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

> 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?

Looks better!

+/*
+ * 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?

And some additional issues:

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?

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.

As I am able to create the matching table with CREATE TABLE

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".

If we move the CREATE TRIGGER after the ALTER it works

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.

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Tomas Vondra 2026-11-25 23:37:56 Re: Unify parallel worker handling for index builds and instrumentation
Previous Message Chao Li 2026-11-25 23:27:31 Re: Bitwise OR instead of logical one in test_aio.c