cockroach icon indicating copy to clipboard operation
cockroach copied to clipboard

not able to delete sequence

Open kswathihs opened this issue 7 years ago • 7 comments

The sequence relation shows "true" for relhasindex but doesn't show up in the table view for the index that was created

We are trying to drop a sequence created: order_sequence but is getting error

drop sequence order_sequence;
pq: cannot drop sequence order_sequence because other objects depend on it

and then

select * from pg_class where relkind='S';
     oid     |    relname     | relnamespace | reltype | relowner | relam | relfilenode | reltablespace | relpages | reltuples | relallvisible | reltoastrelid | relhasindex | relisshared | relpersistence | relistemp | relkind | relnatts | relchecks | relhasoids | relhaspkey | relhasrules | relhastriggers | relhassubclass | relfrozenxid | relacl | reloptions
+------------+----------------+--------------+---------+----------+-------+-------------+---------------+----------+-----------+---------------+---------------+-------------+-------------+----------------+-----------+---------+----------+-----------+------------+------------+-------------+----------------+----------------+--------------+--------+------------+
  1219136245 | order_sequence |   2532413544 |       0 | NULL     | NULL  |           0 |             0 |     NULL | NULL      |             0 |             0 |    true     |    false    | p              |   false   | S       |        1 |         0 |   false    |    true    |    false    |     false      |     false      |            0 | NULL   | NULL
(1 row)

but not listed in the view below:

root@:26257/starscream> show indexes from orders;
  table_name |        index_name         | non_unique | seq_in_index | column_name  | direction | storing | implicit
+------------+---------------------------+------------+--------------+--------------+-----------+---------+----------+
  orders     | orders_pkey               |   false    |            1 | id           | ASC       |  false  |  false
  orders     | orders_id_key             |   false    |            1 | id           | ASC       |  false  |  false
  orders     | orders_order_number_index |    true    |            1 | order_number | ASC       |  false  |  false
  orders     | orders_order_number_index |    true    |            2 | id           | ASC       |  false  |   true
(4 rows)

To Reproduce

following queries were run for Table Orders: CREATE SEQUENCE order_sequence; ALTER TABLE orders ADD COLUMN sequence_number BIGINT; ALTER TABLE orders ALTER COLUMN sequence_number SET DEFAULT nextval('order_sequence'); CREATE INDEX sequence_number_index ON orders (sequence_number);

But index are not showing up in Table view. Not able to delete Sequence as well. neither column nor index exists as per below message

drop index orders@sequence_number_index;
pq: index "sequence_number_index" does not exist
pq: column "sequence_number" does not exist

why is the sequence relation shows "true" for relhasindex ? if exists why not it is shown in table view or why not we are able to delete?

Expected behavior Should be able to delete index if exist

If applicable, add screenshots to help explain your problem.

Environment:

  • CockroachDB version v2.1.4
  • Server OS: Centos7

Additional context What was the impact?

Add any other context about the problem here.

kswathihs avatar Apr 09 '19 06:04 kswathihs

Is there any way to delete sequence, how to get rid of unknown dependencies that is holding sequence

kswathihs avatar Apr 09 '19 06:04 kswathihs

@jordanlewis for triage

knz avatar Apr 09 '19 13:04 knz

Hi @kswathihs, thanks for the report. You should be able to drop the column from the table, after which the sequence will be droppable as well.

Here is a log to prove it:

[email protected]:52053/defaultdb> create table orders (a int primary key);
CREATE TABLE

Time: 3.096ms

[email protected]:52053/defaultdb> ALTER TABLE orders ADD COLUMN sequence_number BIGINT;
ALTER TABLE

Time: 46.85ms
erw
[email protected]:52053/defaultdb> CREATE SEQUENCE order_sequence;
CREATE SEQUENCE

Time: 3.414ms

[email protected]:52053/defaultdb> ALTER TABLE orders ALTER COLUMN sequence_number SET DEFAULT nextval('order_sequence');
ALTER TABLE

Time: 9.072ms

[email protected]:52053/defaultdb> CREATE INDEX sequence_number_index ON orders (sequence_number);
CREATE INDEX

Time: 74.011ms

[email protected]:52053/defaultdb> drop sequence order_sequence;                                                                                                                                                                                                                                                                                                               pq: cannot drop sequence order_sequence because other objects depend on it
[email protected]:52053/defaultdb> alter table orders drop column sequence_number;                                                                                                                                                                                                                                                                                             pq: rejected: ALTER TABLE DROP COLUMN will remove all data in that column (sql_safe_updates = true)
[email protected]:52053/defaultdb> set sql_safe_updates=false;
SET

Time: 307µs

[email protected]:52053/defaultdb> alter table orders drop column sequence_number;
ALTER TABLE

Time: 108.519ms

[email protected]:52053/defaultdb> drop sequence order_sequence;
DROP SEQUENCE

Time: 8.218ms

Sounds like there's a bug in our metadata tables, but you should be able to delete the sequence fine.

jordanlewis avatar Apr 09 '19 14:04 jordanlewis

@jordanlewis Thanks. unfortunately it is not getting deleted even after deleting column in our case. May be because of a different data model or we actually created sequence, column , index for the table with already existing data of about 1 million records

or Could you try to replicate this table, have some data and then reproduce the same

CREATE TABLE IF NOT EXISTS orders (id UUID NOT NULL UNIQUE, order_json STRING NOT NULL, order_number STRING NOT NULL, guest_id STRING NOT NULL, failed_publish BOOL, CONSTRAINT orders_pkey PRIMARY KEY (id))

kswathihs avatar Apr 10 '19 05:04 kswathihs

@kswathihs please provide a log of your deletion attempts so I can understand better what's going on.

jordanlewis avatar May 01 '19 05:05 jordanlewis

@kswathihs I still can't reproduce even with data in the table. Closing this for now. Please re-open if you can provide a reproduction case.

Thank you!

jordanlewis avatar Jun 16 '19 21:06 jordanlewis

@jordanlewis hello. Here's my example. The bug appears on line 18 (ALTER TABLE test_table ADD COLUMN id int default nextval('test_table_seq');) related to issue 42508.

BEGIN;
    CREATE TABLE test_table(
        guid uuid not null constraint test_table_pkey primary key default gen_random_uuid(),
        col1 TEXT,
        col2 INT NOT NULL
    );

    INSERT INTO test_table(col1, col2)
    VALUES ('row1', 1),
           ('row2', 2),
           ('row3', 3),
           ('row4', 4),
           ('row5', 5);
COMMIT;

BEGIN;
    CREATE SEQUENCE test_table_seq;
    ALTER TABLE test_table ADD COLUMN id int default nextval('test_table_seq');
COMMIT;

BEGIN;
    UPDATE test_table SET id = nextval('test_table_seq') WHERE id IS NULL;
    ALTER TABLE test_table ALTER COLUMN id SET NOT NULL;
    CREATE UNIQUE INDEX ON test_table(id);
COMMIT;

At this line, the transaction will be aborted with errors:

[2021-09-09 15:41:23] [XXA00] ERROR: transaction committed but schema change aborted with error: (0A000): nextval(): unimplemented: cannot evaluate scalar expressions containing sequence operations in this context
[2021-09-09 15:41:23] Подсказка: You have attempted to use a feature that is not yet implemented.
[2021-09-09 15:41:23] See: https://go.crdb.dev/issue-v/42508/v20.2
[2021-09-09 15:41:23] --
[2021-09-09 15:41:23] Some of the non-DDL statements may have committed successfully, but some of the DDL statement(s) failed.
[2021-09-09 15:41:23] Manual inspection may be required to determine the actual state of the database.
[2021-09-09 15:41:23] --
[2021-09-09 15:41:23] See: https://github.com/cockroachdb/cockroach/issues/42061

But the sequence will have time to be created. After that, this sequence cannot be deleted.

When trying to drop, it prints: [2021-09-09 15:41:45] [2BP01] ERROR: cannot drop sequence test_table_seq because other objects depend on it

CockroachDB version v20.2.8 Server OS: Centos7

akss1 avatar Sep 09 '21 12:09 akss1