-
Notifications
You must be signed in to change notification settings - Fork 140
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add background index creation jobs (#723)
- Loading branch information
Showing
5 changed files
with
145 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
import sql from "./utils/db"; | ||
|
||
export async function createIndexes() { | ||
await sql`select pg_advisory_lock(123456789)`; | ||
|
||
try { | ||
const migrations = await sql` | ||
select | ||
id, | ||
name, | ||
operation, | ||
statement | ||
from | ||
_db_migration_index | ||
where | ||
status in ('pending', 'failed') | ||
order | ||
by id; | ||
`; | ||
|
||
for (const migration of migrations) { | ||
const { id, name, operation, statement } = migration; | ||
|
||
try { | ||
await sql` | ||
update | ||
_db_migration_index | ||
set | ||
status = 'in-progress' | ||
where | ||
id = ${id} | ||
`; | ||
|
||
await sql.unsafe(statement); | ||
|
||
if (operation === "create") { | ||
const [validCheck] = await sql` | ||
select | ||
c.relname, | ||
i.indisvalid | ||
from | ||
pg_class c | ||
join pg_index i on c.oid = i.indexrelid | ||
where | ||
c.relname = ${name} | ||
and i.indisvalid = true | ||
`; | ||
|
||
if (validCheck) { | ||
await sql` | ||
update | ||
_db_migration_index | ||
set | ||
status = 'done' | ||
where | ||
id = ${id} | ||
`; | ||
console.log(`Index migration "${name}" completed successfully.`); | ||
} else { | ||
await sql.unsafe(`drop index if exists ${name}`); | ||
await sql` | ||
update | ||
_db_migration_index | ||
set | ||
status = 'failed' | ||
where | ||
id = ${id} | ||
`; | ||
console.warn( | ||
`Index migration "${name}" failed; dropped partial index.`, | ||
); | ||
} | ||
} else if (operation === "drop") { | ||
const [stillExists] = await sql` | ||
select | ||
c.relname | ||
from | ||
pg_class c | ||
join pg_index i on c.oid = i.indexrelid | ||
where c.relname = ${name} | ||
`; | ||
|
||
if (!stillExists) { | ||
await sql` | ||
update | ||
_db_migration_index | ||
set | ||
status = 'done' | ||
where | ||
id = ${id} | ||
`; | ||
console.log(`Index drop "${name}" completed successfully.`); | ||
} else { | ||
await sql` | ||
update | ||
_db_migration_index | ||
set | ||
status = 'failed' | ||
where | ||
id = ${id} | ||
`; | ||
console.warn(`Index drop "${name}" failed; index still exists.`); | ||
} | ||
} | ||
} catch (err) { | ||
console.error(`Index migration "${name}" errored:`, err); | ||
await sql.unsafe(`drop index if exists ${name}`); | ||
await sql` | ||
update | ||
_db_migration_index | ||
set | ||
status = 'failed' | ||
where | ||
id = ${id} | ||
`; | ||
} | ||
} | ||
} finally { | ||
await sql`select pg_advisory_unlock(123456789)`; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,10 @@ | ||
drop index if exists run_input_idx; | ||
drop index if exists run_output_idx; | ||
drop index if exists run_error_idx; | ||
drop table if exists log; | ||
create extension if not exists btree_gin; | ||
create index run_project_id_input_idx on run using gin (project_id, ((input)::text) gin_trgm_ops); | ||
create index run_project_id_output_idx on run using gin (project_id, ((output)::text) gin_trgm_ops); | ||
|
||
-- !!!WARNING!!! statement below moved to the new index creation system in 0059.sql, but it's kept as a reference for users who have already run this migration | ||
|
||
-- drop index if exists run_input_idx; | ||
-- drop index if exists run_output_idx; | ||
-- drop index if exists run_error_idx; | ||
-- create index run_project_id_input_idx on run using gin (project_id, ((input)::text) gin_trgm_ops); | ||
-- create index run_project_id_output_idx on run using gin (project_id, ((output)::text) gin_trgm_ops); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
create table _db_migration_index ( | ||
id serial primary key, | ||
name text not null, | ||
statement text not null, | ||
operation text not null default 'create', | ||
status text not null default 'pending' -- "pending", "in-progress", "done", "failed" | ||
); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
insert into _db_migration_index (name, operation, statement) values | ||
('run_input_idx', 'drop', 'drop index concurrently if exists run_input_idx'), | ||
('run_output_idx', 'drop', 'drop index concurrently if exists run_output_idx'), | ||
('run_error_idx', 'drop', 'drop index concurrently if exists run_error_idx'), | ||
('run_project_id_input_idx', 'create', 'create index concurrently if not exists run_project_id_input_idx on run using gin (project_id, ((input)::text) gin_trgm_ops)'), | ||
('run_project_id_output_idx', 'create', 'create index concurrently if not exists run_project_id_output_idx on run using gin (project_id, ((output)::text) gin_trgm_ops)'); |