Re: ALTER TABLE ... ADD COLUMN ... DEFAULT with volatile function loses DML - Mailing list pgsql-bugs

From Tom Lane
Subject Re: ALTER TABLE ... ADD COLUMN ... DEFAULT with volatile function loses DML
Date
Msg-id 650759.1762280552@sss.pgh.pa.us
Whole thread Raw
In response to ALTER TABLE ... ADD COLUMN ... DEFAULT with volatile function loses DML  (Jeff Davis <pgsql@j-davis.com>)
Responses Re: ALTER TABLE ... ADD COLUMN ... DEFAULT with volatile function loses DML
List pgsql-bugs
Jeff Davis <pgsql@j-davis.com> writes:
> The following SQL seems to lose the updates during the ALTER:

>   CREATE TABLE t(id INT);
>   INSERT INTO t VALUES (1), (2);

>   CREATE FUNCTION f() RETURNS INT VOLATILE AS $$
>     BEGIN
>       UPDATE t SET id = id + 10;
>       RETURN (SELECT MAX(id) FROM t);
>     END
>   $$ LANGUAGE plpgsql;

>   ALTER TABLE t ADD COLUMN c INT DEFAULT f();

>   SELECT * FROM t;

>    id | c  
>   ----+----
>     1 | 12
>     2 | 22
>   (2 rows)

Hmm ... ideally we'd throw an error for that.  CheckTableNotInUse()
intends to prevent some similar cases, but it misses this one because
ALTER TABLE is the outermost command and there's no check performed
at the inner UPDATE.  Not sure how hard we need to work at preventing
people from shooting themselves in the foot, though.

            regards, tom lane



pgsql-bugs by date:

Previous
From: Jeff Davis
Date:
Subject: ALTER TABLE ... ADD COLUMN ... DEFAULT with volatile function loses DML
Next
From: Jeff Davis
Date:
Subject: Re: ALTER TABLE ... ADD COLUMN ... DEFAULT with volatile function loses DML