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

sql: add primary key on empty table very slow #47607

Closed
kocoten1992 opened this issue Apr 17, 2020 · 5 comments · Fixed by #48608
Closed

sql: add primary key on empty table very slow #47607

kocoten1992 opened this issue Apr 17, 2020 · 5 comments · Fixed by #48608
Labels
C-bug Code not up to spec/doc, specs & docs deemed correct. Solution expected to change code/behavior. O-community Originated from the community X-blathers-triaged blathers was able to find an owner

Comments

@kocoten1992
Copy link

Describe the problem

discover while trying reproduce #47512

To Reproduce

  1. start fresh cockroach start-single-node --insecure (empty data)
  2. create a file create_users_table.sql with content follow
drop table if exists "users" cascade;
create table "users" ("id" uuid not null, "name" varchar(255) not null, "email" varchar(255) not null, "password" varchar(255) not null, "remember_token" varchar(100) null, "created_at" timestamp(0) without time zone null, "updated_at" timestamp(0) without time zone null, "deleted_at" timestamp(0) without time zone null);
alter table "users" add primary key ("id");
alter table "users" add constraint "users_email_unique" unique ("email");
  1. run cockroach sql --insecure < create_users_table.sql

It take a whooping 30s to add primary key on empty table (╯°□°)╯︵ ┻━┻

NOTICE: primary key changes are finalized asynchronously; further schema changes on this table may be restricted until the job completes
ALTER TABLE

Time: 211.001799ms

ALTER TABLE

Time: 30.581253037s

Expected behavior
Should be much faster since the table is empty.

Environment:

  • CockroachDB version
Build Tag:    82294d1
Build Time:   2020/04/15 01:47:13
Distribution: CCL
Platform:     linux amd64 (x86_64-pc-linux-gnu)
Go Version:   go1.13.9
C Compiler:   4.2.1 Compatible Clang 3.8.0 (tags/RELEASE_380/final)
Build SHA-1:  82294d100ad1cb26b488f53e7241242d474d58d7
Build Type:   development
  • Server OS: ubuntu 20.04 dev
  • Client app cockroach sql (same version as server)
@blathers-crl
Copy link

blathers-crl bot commented Apr 17, 2020

Hello, I am Blathers. I am here to help you get the issue triaged.

Hoot - a bug! Though bugs are the bane of my existence, rest assured the wretched thing will get the best of care here.

I have CC'd a few people who may be able to assist you:

🦉 Hoot! I am a Blathers, a bot for CockroachDB. My owner is otan.

@blathers-crl blathers-crl bot added C-bug Code not up to spec/doc, specs & docs deemed correct. Solution expected to change code/behavior. O-community Originated from the community X-blathers-triaged blathers was able to find an owner labels Apr 17, 2020
@rohany
Copy link
Contributor

rohany commented Apr 17, 2020

As the notice says, there is asynchronous work after adding a primary key that is happening in the background, which is delaying the constraint add from completing. If you put the create + add pk + add constraint into a transaction it will complete immediately. i.e.

begin;
drop table if exists "users" cascade;
create table "users" ("id" uuid not null, "name" varchar(255) not null, "email" varchar(255) not null, "password" varchar(255) not null, "remember_token" varchar(100) null, "created_at" timestamp(0) without time zone null, "updated_at" timestamp(0) without time zone null, "deleted_at" timestamp(0) without time zone null);
alter table "users" add primary key ("id");
alter table "users" add constraint "users_email_unique" unique ("email");
commit;

@awoods187
Copy link
Contributor

we should look into this because i imagine this is common behavior in test suites in which a PK is a logical and not a physical grouping for Postgres and is not created implicitly.

@otan
Copy link
Contributor

otan commented Apr 17, 2020

i suspect #47624 may help

@knz knz changed the title add primary key on empty table very slow sql: add primary key on empty table very slow Apr 30, 2020
@knz
Copy link
Contributor

knz commented Apr 30, 2020

cc @ajwerner for triage. maybe you want to fold under #47790

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Code not up to spec/doc, specs & docs deemed correct. Solution expected to change code/behavior. O-community Originated from the community X-blathers-triaged blathers was able to find an owner
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants