Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ALTER TABLE will refresh previous snapshot values for absent cells #17121

Open
BugenZhao opened this issue Jun 5, 2024 · 0 comments
Open

ALTER TABLE will refresh previous snapshot values for absent cells #17121

BugenZhao opened this issue Jun 5, 2024 · 0 comments
Labels
type/bug Something isn't working
Milestone

Comments

@BugenZhao
Copy link
Member

There's a field named snapshot_value in DefaultColumnDesc, which is the evaluation result of the default expression at the time when it's created or added with ALTER TABLE. This is introduced in #12678 to support impure default expressions like DEFAULT NOW().

If we then call ALTER TABLE again, the table will be replanned and the snapshot values will be evaluated once more. The new values could be different from the previous one if the expression is impure, leading to inconsistent select results for pre-existing rows prior to the very first ADD COLUMN .. DEFAULT NOW().

To reproduce:

dev=> create table t(v int);
CREATE_TABLE
dev=> insert into t values (1);
INSERT 0 1
dev=> alter table t add column now1 timestamptz default now();
ALTER_TABLE
dev=> select * from t;
 v |             now1              
---+-------------------------------
 1 | 2024-06-05 05:50:41.723+00:00  <- snapshot value
(1 row)

dev=> insert into t values (2);
INSERT 0 1
dev=> select * from t;
 v |             now1              
---+-------------------------------
 1 | 2024-06-05 05:50:41.723+00:00. <- snapshot value
 2 | 2024-06-05 05:50:56.326+00:00. <- real-time value
(2 rows)

dev=> alter table t add column v2 varchar;
ALTER_TABLE
dev=> select * from t order by v;
 v |             now1              | v2 
---+-------------------------------+----
 1 | 2024-06-05 05:51:43.370+00:00 |     <- snapshot value got updated unexpectedly!
 2 | 2024-06-05 05:50:56.326+00:00 | 
(2 rows)

In Postgres, since the table will be rewritten to fill the default values for pre-existing rows, there's no such issue. We should be consistent with that behavior.

@BugenZhao BugenZhao added the type/bug Something isn't working label Jun 5, 2024
@github-actions github-actions bot added this to the release-1.10 milestone Jun 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type/bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant