Skip to content

Commit 6f95ea3

Browse files
committed
feat(pg: use concurrent index builds
1 parent 6e3ae5b commit 6f95ea3

22 files changed

+95
-95
lines changed

migrations/db/init-scripts/00000000000001-auth-schema.sql

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ CREATE TABLE auth.users (
2828
updated_at timestamptz NULL,
2929
CONSTRAINT users_pkey PRIMARY KEY (id)
3030
);
31-
CREATE INDEX users_instance_id_email_idx ON auth.users USING btree (instance_id, email);
32-
CREATE INDEX users_instance_id_idx ON auth.users USING btree (instance_id);
31+
CREATE INDEX CONCURRENTLY IF NOT EXISTS users_instance_id_email_idx ON auth.users USING btree (instance_id, email);
32+
CREATE INDEX CONCURRENTLY IF NOT EXISTS users_instance_id_idx ON auth.users USING btree (instance_id);
3333
comment on table auth.users is 'Auth: Stores user login data within a secure schema.';
3434

3535
-- auth.refresh_tokens definition
@@ -44,9 +44,9 @@ CREATE TABLE auth.refresh_tokens (
4444
updated_at timestamptz NULL,
4545
CONSTRAINT refresh_tokens_pkey PRIMARY KEY (id)
4646
);
47-
CREATE INDEX refresh_tokens_instance_id_idx ON auth.refresh_tokens USING btree (instance_id);
48-
CREATE INDEX refresh_tokens_instance_id_user_id_idx ON auth.refresh_tokens USING btree (instance_id, user_id);
49-
CREATE INDEX refresh_tokens_token_idx ON auth.refresh_tokens USING btree (token);
47+
CREATE INDEX CONCURRENTLY IF NOT EXISTS refresh_tokens_instance_id_idx ON auth.refresh_tokens USING btree (instance_id);
48+
CREATE INDEX CONCURRENTLY IF NOT EXISTS refresh_tokens_instance_id_user_id_idx ON auth.refresh_tokens USING btree (instance_id, user_id);
49+
CREATE INDEX CONCURRENTLY IF NOT EXISTS refresh_tokens_token_idx ON auth.refresh_tokens USING btree (token);
5050
comment on table auth.refresh_tokens is 'Auth: Store of tokens used to refresh JWT tokens once they expire.';
5151

5252
-- auth.instances definition
@@ -70,7 +70,7 @@ CREATE TABLE auth.audit_log_entries (
7070
created_at timestamptz NULL,
7171
CONSTRAINT audit_log_entries_pkey PRIMARY KEY (id)
7272
);
73-
CREATE INDEX audit_logs_instance_id_idx ON auth.audit_log_entries USING btree (instance_id);
73+
CREATE INDEX CONCURRENTLY IF NOT EXISTS audit_logs_instance_id_idx ON auth.audit_log_entries USING btree (instance_id);
7474
comment on table auth.audit_log_entries is 'Auth: Audit trail for user actions.';
7575

7676
-- auth.schema_migrations definition

migrations/db/init-scripts/00000000000002-storage-schema.sql

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ CREATE TABLE "storage"."buckets" (
1616
CONSTRAINT "buckets_owner_fkey" FOREIGN KEY ("owner") REFERENCES "auth"."users"("id"),
1717
PRIMARY KEY ("id")
1818
);
19-
CREATE UNIQUE INDEX "bname" ON "storage"."buckets" USING BTREE ("name");
19+
CREATE UNIQUE INDEX CONCURRENTLY IF NOT EXISTS "bname" ON "storage"."buckets" USING BTREE ("name");
2020

2121
CREATE TABLE "storage"."objects" (
2222
"id" uuid NOT NULL DEFAULT extensions.uuid_generate_v4(),
@@ -31,8 +31,8 @@ CREATE TABLE "storage"."objects" (
3131
CONSTRAINT "objects_owner_fkey" FOREIGN KEY ("owner") REFERENCES "auth"."users"("id"),
3232
PRIMARY KEY ("id")
3333
);
34-
CREATE UNIQUE INDEX "bucketid_objname" ON "storage"."objects" USING BTREE ("bucket_id","name");
35-
CREATE INDEX name_prefix_search ON storage.objects(name text_pattern_ops);
34+
CREATE UNIQUE INDEX CONCURRENTLY IF NOT EXISTS "bucketid_objname" ON "storage"."objects" USING BTREE ("bucket_id","name");
35+
CREATE INDEX CONCURRENTLY IF NOT EXISTS name_prefix_search ON storage.objects(name text_pattern_ops);
3636

3737
ALTER TABLE storage.objects ENABLE ROW LEVEL SECURITY;
3838

migrations/schema-15.sql

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -847,63 +847,63 @@ ALTER TABLE ONLY storage.objects
847847
-- Name: audit_logs_instance_id_idx; Type: INDEX; Schema: auth; Owner: -
848848
--
849849

850-
CREATE INDEX audit_logs_instance_id_idx ON auth.audit_log_entries USING btree (instance_id);
850+
CREATE INDEX CONCURRENTLY IF NOT EXISTS audit_logs_instance_id_idx ON auth.audit_log_entries USING btree (instance_id);
851851

852852

853853
--
854854
-- Name: refresh_tokens_instance_id_idx; Type: INDEX; Schema: auth; Owner: -
855855
--
856856

857-
CREATE INDEX refresh_tokens_instance_id_idx ON auth.refresh_tokens USING btree (instance_id);
857+
CREATE INDEX CONCURRENTLY IF NOT EXISTS refresh_tokens_instance_id_idx ON auth.refresh_tokens USING btree (instance_id);
858858

859859

860860
--
861861
-- Name: refresh_tokens_instance_id_user_id_idx; Type: INDEX; Schema: auth; Owner: -
862862
--
863863

864-
CREATE INDEX refresh_tokens_instance_id_user_id_idx ON auth.refresh_tokens USING btree (instance_id, user_id);
864+
CREATE INDEX CONCURRENTLY IF NOT EXISTS refresh_tokens_instance_id_user_id_idx ON auth.refresh_tokens USING btree (instance_id, user_id);
865865

866866

867867
--
868868
-- Name: refresh_tokens_token_idx; Type: INDEX; Schema: auth; Owner: -
869869
--
870870

871-
CREATE INDEX refresh_tokens_token_idx ON auth.refresh_tokens USING btree (token);
871+
CREATE INDEX CONCURRENTLY IF NOT EXISTS refresh_tokens_token_idx ON auth.refresh_tokens USING btree (token);
872872

873873

874874
--
875875
-- Name: users_instance_id_email_idx; Type: INDEX; Schema: auth; Owner: -
876876
--
877877

878-
CREATE INDEX users_instance_id_email_idx ON auth.users USING btree (instance_id, email);
878+
CREATE INDEX CONCURRENTLY IF NOT EXISTS users_instance_id_email_idx ON auth.users USING btree (instance_id, email);
879879

880880

881881
--
882882
-- Name: users_instance_id_idx; Type: INDEX; Schema: auth; Owner: -
883883
--
884884

885-
CREATE INDEX users_instance_id_idx ON auth.users USING btree (instance_id);
885+
CREATE INDEX CONCURRENTLY IF NOT EXISTS users_instance_id_idx ON auth.users USING btree (instance_id);
886886

887887

888888
--
889889
-- Name: bname; Type: INDEX; Schema: storage; Owner: -
890890
--
891891

892-
CREATE UNIQUE INDEX bname ON storage.buckets USING btree (name);
892+
CREATE UNIQUE INDEX CONCURRENTLY IF NOT EXISTS bname ON storage.buckets USING btree (name);
893893

894894

895895
--
896896
-- Name: bucketid_objname; Type: INDEX; Schema: storage; Owner: -
897897
--
898898

899-
CREATE UNIQUE INDEX bucketid_objname ON storage.objects USING btree (bucket_id, name);
899+
CREATE UNIQUE INDEX CONCURRENTLY IF NOT EXISTS bucketid_objname ON storage.objects USING btree (bucket_id, name);
900900

901901

902902
--
903903
-- Name: name_prefix_search; Type: INDEX; Schema: storage; Owner: -
904904
--
905905

906-
CREATE INDEX name_prefix_search ON storage.objects USING btree (name text_pattern_ops);
906+
CREATE INDEX CONCURRENTLY IF NOT EXISTS name_prefix_search ON storage.objects USING btree (name text_pattern_ops);
907907

908908

909909
--

migrations/schema-17.sql

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -848,63 +848,63 @@ ALTER TABLE ONLY storage.objects
848848
-- Name: audit_logs_instance_id_idx; Type: INDEX; Schema: auth; Owner: -
849849
--
850850

851-
CREATE INDEX audit_logs_instance_id_idx ON auth.audit_log_entries USING btree (instance_id);
851+
CREATE INDEX CONCURRENTLY IF NOT EXISTS audit_logs_instance_id_idx ON auth.audit_log_entries USING btree (instance_id);
852852

853853

854854
--
855855
-- Name: refresh_tokens_instance_id_idx; Type: INDEX; Schema: auth; Owner: -
856856
--
857857

858-
CREATE INDEX refresh_tokens_instance_id_idx ON auth.refresh_tokens USING btree (instance_id);
858+
CREATE INDEX CONCURRENTLY IF NOT EXISTS refresh_tokens_instance_id_idx ON auth.refresh_tokens USING btree (instance_id);
859859

860860

861861
--
862862
-- Name: refresh_tokens_instance_id_user_id_idx; Type: INDEX; Schema: auth; Owner: -
863863
--
864864

865-
CREATE INDEX refresh_tokens_instance_id_user_id_idx ON auth.refresh_tokens USING btree (instance_id, user_id);
865+
CREATE INDEX CONCURRENTLY IF NOT EXISTINDEX CONCURRENTLY IF NOT EXISTS refresh_tokens_instance_id_user_id_idx ON auth.refresh_tokens USING btree (instance_id, user_id);
866866

867867

868868
--
869869
-- Name: refresh_tokens_token_idx; Type: INDEX; Schema: auth; Owner: -
870870
--
871871

872-
CREATE INDEX refresh_tokens_token_idx ON auth.refresh_tokens USING btree (token);
872+
CREATE INDEX CONCURRENTLY IF NOT EXISTS refresh_tokens_token_idx ON auth.refresh_tokens USING btree (token);
873873

874874

875875
--
876876
-- Name: users_instance_id_email_idx; Type: INDEX; Schema: auth; Owner: -
877877
--
878878

879-
CREATE INDEX users_instance_id_email_idx ON auth.users USING btree (instance_id, email);
879+
CREATE INDEX CONCURRENTLY IF NOT EXISTS users_instance_id_email_idx ON auth.users USING btree (instance_id, email);
880880

881881

882882
--
883883
-- Name: users_instance_id_idx; Type: INDEX; Schema: auth; Owner: -
884884
--
885885

886-
CREATE INDEX users_instance_id_idx ON auth.users USING btree (instance_id);
886+
CREATE INDEX CONCURRENTLY IF NOT EXISTS users_instance_id_idx ON auth.users USING btree (instance_id);
887887

888888

889889
--
890890
-- Name: bname; Type: INDEX; Schema: storage; Owner: -
891891
--
892892

893-
CREATE UNIQUE INDEX bname ON storage.buckets USING btree (name);
893+
CREATE UNIQUE INDEX CONCURRENTLY IF NOT EXISTS bname ON storage.buckets USING btree (name);
894894

895895

896896
--
897897
-- Name: bucketid_objname; Type: INDEX; Schema: storage; Owner: -
898898
--
899899

900-
CREATE UNIQUE INDEX bucketid_objname ON storage.objects USING btree (bucket_id, name);
900+
CREATE UNIQUE INDEX CONCURRENTLY IF NOT EXISTS bucketid_objname ON storage.objects USING btree (bucket_id, name);
901901

902902

903903
--
904904
-- Name: name_prefix_search; Type: INDEX; Schema: storage; Owner: -
905905
--
906906

907-
CREATE INDEX name_prefix_search ON storage.objects USING btree (name text_pattern_ops);
907+
CREATE INDEX CONCURRENTLY IF NOT EXISTS name_prefix_search ON storage.objects USING btree (name text_pattern_ops);
908908

909909

910910
--

migrations/schema-orioledb-17.sql

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -860,56 +860,56 @@ ALTER TABLE ONLY storage.objects
860860
-- Name: audit_logs_instance_id_idx; Type: INDEX; Schema: auth; Owner: -
861861
--
862862

863-
CREATE INDEX audit_logs_instance_id_idx ON auth.audit_log_entries USING btree (instance_id);
863+
CREATE INDEX CONCURRENTLY IF NOT EXISTS audit_logs_instance_id_idx ON auth.audit_log_entries USING btree (instance_id);
864864

865865

866866
--
867867
-- Name: refresh_tokens_instance_id_idx; Type: INDEX; Schema: auth; Owner: -
868868
--
869869

870-
CREATE INDEX refresh_tokens_instance_id_idx ON auth.refresh_tokens USING btree (instance_id);
870+
CREATE INDEX CONCURRENTLY IF NOT EXISTS refresh_tokens_instance_id_idx ON auth.refresh_tokens USING btree (instance_id);
871871

872872

873873
--
874874
-- Name: refresh_tokens_instance_id_user_id_idx; Type: INDEX; Schema: auth; Owner: -
875875
--
876876

877-
CREATE INDEX refresh_tokens_instance_id_user_id_idx ON auth.refresh_tokens USING btree (instance_id, user_id);
877+
CREATE INDEX CONCURRENTLY IF NOT EXISTS refresh_tokens_instance_id_user_id_idx ON auth.refresh_tokens USING btree (instance_id, user_id);
878878

879879

880880
--
881881
-- Name: refresh_tokens_token_idx; Type: INDEX; Schema: auth; Owner: -
882882
--
883883

884-
CREATE INDEX refresh_tokens_token_idx ON auth.refresh_tokens USING btree (token);
884+
CREATE INDEX CONCURRENTLY IF NOT EXISTS refresh_tokens_token_idx ON auth.refresh_tokens USING btree (token);
885885

886886

887887
--
888888
-- Name: users_instance_id_email_idx; Type: INDEX; Schema: auth; Owner: -
889889
--
890890

891-
CREATE INDEX users_instance_id_email_idx ON auth.users USING btree (instance_id, email);
891+
CREATE INDEX CONCURRENTLY IF NOT EXISTS users_instance_id_email_idx ON auth.users USING btree (instance_id, email);
892892

893893

894894
--
895895
-- Name: users_instance_id_idx; Type: INDEX; Schema: auth; Owner: -
896896
--
897897

898-
CREATE INDEX users_instance_id_idx ON auth.users USING btree (instance_id);
898+
CREATE INDEX CONCURRENTLY IF NOT EXISTS users_instance_id_idx ON auth.users USING btree (instance_id);
899899

900900

901901
--
902902
-- Name: bname; Type: INDEX; Schema: storage; Owner: -
903903
--
904904

905-
CREATE UNIQUE INDEX bname ON storage.buckets USING btree (name);
905+
CREATE UNIQUE INDEX CONCURRENTLY IF NOT EXISTS bname ON storage.buckets USING btree (name);
906906

907907

908908
--
909909
-- Name: bucketid_objname; Type: INDEX; Schema: storage; Owner: -
910910
--
911911

912-
CREATE UNIQUE INDEX bucketid_objname ON storage.objects USING btree (bucket_id, name);
912+
CREATE UNIQUE INDEX CONCURRENTLY IF NOT EXISTS bucketid_objname ON storage.objects USING btree (bucket_id, name);
913913

914914

915915
--

migrations/schema.sql

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -838,63 +838,63 @@ ALTER TABLE ONLY storage.objects
838838
-- Name: audit_logs_instance_id_idx; Type: INDEX; Schema: auth; Owner: -
839839
--
840840

841-
CREATE INDEX audit_logs_instance_id_idx ON auth.audit_log_entries USING btree (instance_id);
841+
CREATE INDEX CONCURRENTLY IF NOT EXISTS audit_logs_instance_id_idx ON auth.audit_log_entries USING btree (instance_id);
842842

843843

844844
--
845845
-- Name: refresh_tokens_instance_id_idx; Type: INDEX; Schema: auth; Owner: -
846846
--
847847

848-
CREATE INDEX refresh_tokens_instance_id_idx ON auth.refresh_tokens USING btree (instance_id);
848+
CREATE INDEX CONCURRENTLY IF NOT EXISTS refresh_tokens_instance_id_idx ON auth.refresh_tokens USING btree (instance_id);
849849

850850

851851
--
852852
-- Name: refresh_tokens_instance_id_user_id_idx; Type: INDEX; Schema: auth; Owner: -
853853
--
854854

855-
CREATE INDEX refresh_tokens_instance_id_user_id_idx ON auth.refresh_tokens USING btree (instance_id, user_id);
855+
CREATE INDEX CONCURRENTLY IF NOT EXISTS refresh_tokens_instance_id_user_id_idx ON auth.refresh_tokens USING btree (instance_id, user_id);
856856

857857

858858
--
859859
-- Name: refresh_tokens_token_idx; Type: INDEX; Schema: auth; Owner: -
860860
--
861861

862-
CREATE INDEX refresh_tokens_token_idx ON auth.refresh_tokens USING btree (token);
862+
CREATE INDEX CONCURRENTLY IF NOT EXISTS refresh_tokens_token_idx ON auth.refresh_tokens USING btree (token);
863863

864864

865865
--
866866
-- Name: users_instance_id_email_idx; Type: INDEX; Schema: auth; Owner: -
867867
--
868868

869-
CREATE INDEX users_instance_id_email_idx ON auth.users USING btree (instance_id, email);
869+
CREATE INDEX CONCURRENTLY IF NOT EXISTS users_instance_id_email_idx ON auth.users USING btree (instance_id, email);
870870

871871

872872
--
873873
-- Name: users_instance_id_idx; Type: INDEX; Schema: auth; Owner: -
874874
--
875875

876-
CREATE INDEX users_instance_id_idx ON auth.users USING btree (instance_id);
876+
CREATE INDEX CONCURRENTLY IF NOT EXISTS users_instance_id_idx ON auth.users USING btree (instance_id);
877877

878878

879879
--
880880
-- Name: bname; Type: INDEX; Schema: storage; Owner: -
881881
--
882882

883-
CREATE UNIQUE INDEX bname ON storage.buckets USING btree (name);
883+
CREATE UNIQUE INDEX CONCURRENTLY IF NOT EXISTS bname ON storage.buckets USING btree (name);
884884

885885

886886
--
887887
-- Name: bucketid_objname; Type: INDEX; Schema: storage; Owner: -
888888
--
889889

890-
CREATE UNIQUE INDEX bucketid_objname ON storage.objects USING btree (bucket_id, name);
890+
CREATE UNIQUE INDEX CONCURRENTLY IF NOT EXISTS bucketid_objname ON storage.objects USING btree (bucket_id, name);
891891

892892

893893
--
894894
-- Name: name_prefix_search; Type: INDEX; Schema: storage; Owner: -
895895
--
896896

897-
CREATE INDEX name_prefix_search ON storage.objects USING btree (name text_pattern_ops);
897+
CREATE INDEX CONCURRENTLY IF NOT EXISTS name_prefix_search ON storage.objects USING btree (name text_pattern_ops);
898898

899899

900900
--

nix/tests/expected/docs-full-text-search.out

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ alter table
148148
books
149149
add column
150150
fts tsvector generated always as (to_tsvector('english', description || ' ' || title)) stored;
151-
create index books_fts on books using gin (fts);
151+
create index concurrently if not exists books_fts on books using gin (fts);
152152
select id, fts
153153
from books;
154154
id | fts

nix/tests/expected/docs-indexes.out

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ select name from persons where age = 32;
1414
John Doe
1515
(1 row)
1616

17-
create index idx_persons_age on persons (age);
18-
create index idx_living_persons_age on persons (age) where deceased is false;
19-
create index idx_persons_age_desc on persons (age desc nulls last);
17+
create index concurrently if not exists idx_persons_age on persons (age);
18+
create index concurrently if not exists idx_living_persons_age on persons (age) where deceased is false;
19+
create index concurrently if not exists idx_persons_age_desc on persons (age desc nulls last);
2020
reindex index concurrently idx_persons_age;
2121
reindex table concurrently persons;
2222
drop table persons cascade;

nix/tests/expected/hypopg.out

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ create table v.samp(
33
id int
44
);
55
select 1 from hypopg_create_index($$
6-
create index on v.samp(id)
6+
create index concurrently if not exists on v.samp(id)
77
$$);
88
?column?
99
----------

nix/tests/expected/index_advisor.out

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ from
99
index_advisor('select id from v.book where title = $1');
1010
index_statements | errors
1111
------------------------------------------------+--------
12-
{"CREATE INDEX ON v.book USING btree (title)"} | {}
12+
{"CREATE INDEX CONCURRENTLY IF NOT EXISTS ON v.book USING btree (title)"} | {}
1313
(1 row)
1414

1515
drop schema v cascade;

0 commit comments

Comments
 (0)