sql: cannot add a new sequence-populated column (or pg-compatible SERIAL) to an existing table, + bug
Two problems in this issue
- a known limitation: cannot add such columns
- a bug: the error is only detected at run-time, should be detected at compile-time.
Because of the bug part, the severity is higher than if this was just a missing feature.
The limitation
Today the column backfiller is unable to execute sequence operations such as evaluating the nextval() function.
This makes it impossible to add a column that uses a sequence-based DEFAULT to an existing table.
For example:
> create table t(x int); insert into t(x) values (1), (2), (3);
> create sequence s;
> alter table t add column y int default nextval('s');
pq: nextval(): cannot backfill such sequence operation
Or:
> set experimental_serial_normalization=sql_sequence; -- for pg compat
> alter table t add column y serial;
pq: nextval(): cannot backfill such sequence operation
The bug
A large problem with this limitation is that the error is only detected at runtime, not at compile-time. This means that an attempt to add such a column inside an explicit txn will violate atomicity with the dreaded XXA00 error (see #42061)
> begin;
insert into t(x) values (4);
alter table t add column y int default nextval('d');
commit;
pq: transaction committed but schema change aborted with error: (42P15): nextval(): cannot backfill such sequence operation
HINT: Some of the non-DDL statements may have committed successfully, but some of the DDL statement(s) failed.
Manual inspection may be required to determine the actual state of the database.
--
See: https://github.com/cockroachdb/cockroach/issues/42061
> select * from t;
x
+---+
1
2
3
4
(4 rows)
(the INSERT was committed, but the ALTER failed: atomicity anomaly)
Jira issue: CRDB-5351
Epic CRDB-60873
cc @jordanlewis @RaduBerinde @lucy-zhang for triage.
Also I'm thinking about this a bit and I really don't see why the backfiller cannot evaluate sequence operations. They are non-transactional, so it should really be possible. @jordanlewis what do you think?
Would it be possible to detect sequence-populated columns at compile time, and backport a fix using that approach? As I understand it, #42509 continues to detect the problem only at runtime (though of course it would still be an improvement).
I agree with your proposal.
That said, the main change in #42509 is removing the error flattening, to ensure that the more specific error codes bubble up no matter what. I think this should be merged first before the improvement you sugggest.
This known limitation was documented some time ago: https://www.cockroachlabs.com/docs/stable/known-limitations.html#adding-a-column-with-certain-default-values. I'll add a link to this issue. Need to confirm if the language is still correct and sufficient.
Faced the same issue today. Accidentally discovered, that it can be done, but in multiple steps. Furthermore, with
update mytable set myseq = nextval('seq') where myseq is null;
existing fields get filled. It would be nice to have that in docs.
@jordanlewis For example, the described bug leads to this.
Implementing ADD COLUMN in the declarative schema changer will help here, as the column backfiller will no longer be used.
The index backfiller is not any better equipped to interact with sequences.
We do at least clean up the back-references properly in the declarative schema changer
any updates on this ?
wait too