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


Reply via email to