cockroach icon indicating copy to clipboard operation
cockroach copied to clipboard

sql: cannot add a new sequence-populated column (or pg-compatible SERIAL) to an existing table, + bug

Open knz opened this issue 7 years ago • 12 comments

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

knz avatar Nov 15 '19 09:11 knz

cc @jordanlewis @RaduBerinde @lucy-zhang for triage.

knz avatar Nov 15 '19 09:11 knz

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?

knz avatar Nov 15 '19 09:11 knz

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

thoszhang avatar Nov 17 '19 18:11 thoszhang

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.

knz avatar Nov 17 '19 18:11 knz

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.

jseldess avatar Apr 16 '20 17:04 jseldess

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.

Bessonov avatar Aug 13 '20 17:08 Bessonov

@jordanlewis For example, the described bug leads to this.

akss1 avatar Sep 09 '21 12:09 akss1

Implementing ADD COLUMN in the declarative schema changer will help here, as the column backfiller will no longer be used.

postamar avatar Mar 08 '22 21:03 postamar

The index backfiller is not any better equipped to interact with sequences.

ajwerner avatar Jul 19 '22 21:07 ajwerner

We do at least clean up the back-references properly in the declarative schema changer

ajwerner avatar Jul 19 '22 21:07 ajwerner

any updates on this ?

a-eid avatar Nov 10 '23 12:11 a-eid

wait too

z0mb1ek avatar Dec 18 '23 21:12 z0mb1ek