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

fix: faster migrations for metrics table #9060

Merged
merged 1 commit into from
Mar 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*
Rollback changing `partition_type` on `metrics` from ENUM type to TEXT.
Rollback changing `partition_type` on `metrics` from ENUM type to TEXT,
dropping NOT NULL on `total_batches`

Partition keys cannot be modified without dropping and recreating the table.
*/
Expand Down Expand Up @@ -52,7 +53,7 @@ CREATE TABLE metrics (
trial_id integer NOT NULL,
end_time timestamp with time zone,
metrics jsonb,
total_batches integer,
total_batches integer NOT NULL DEFAULT 0,
trial_run_id integer NOT NULL DEFAULT 0,
archived boolean NOT NULL DEFAULT false,
id integer NOT NULL DEFAULT nextval('metrics_id_seq'),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*
Change `partition_type` on `metrics` from ENUM type to TEXT.
Drop 'NOT NULL' on `metrics.total_batches`.
Change `partition_type` on `metrics` from ENUM type to TEXT.

Partition keys cannot be modified without dropping and recreating the table.
*/
Expand Down Expand Up @@ -40,7 +41,7 @@ SELECT setval(
true
);

-- Re-create table with `partition_type` TEXT.
-- Re-create table with `partition_type` TEXT and nullable `total_batches`
CREATE TABLE metrics (
trial_id integer NOT NULL,
end_time timestamp with time zone,
Expand All @@ -55,14 +56,18 @@ CREATE TABLE metrics (


-- Modify child partitions to have `partition_type` TEXT and set defaults.
ALTER TABLE raw_steps ALTER COLUMN partition_type TYPE text;
ALTER TABLE raw_steps ALTER COLUMN partition_type SET DEFAULT 'TRAINING';
-- Drop and recreate the columns, since it's faster and we already know the values.
ALTER TABLE raw_steps DROP COLUMN partition_type;
ALTER TABLE raw_steps ADD COLUMN partition_type TEXT DEFAULT 'TRAINING';
ALTER TABLE raw_steps ALTER COLUMN partition_type SET NOT NULL;

ALTER TABLE raw_validations ALTER COLUMN partition_type TYPE text;
ALTER TABLE raw_validations ALTER COLUMN partition_type SET DEFAULT 'VALIDATION';
ALTER TABLE raw_validations DROP COLUMN partition_type;
ALTER TABLE raw_validations ADD COLUMN partition_type TEXT DEFAULT 'VALIDATION';
ALTER TABLE raw_validations ALTER COLUMN partition_type SET NOT NULL;

ALTER TABLE generic_metrics ALTER COLUMN partition_type TYPE text;
ALTER TABLE generic_metrics ALTER COLUMN partition_type SET DEFAULT 'GENERIC';
ALTER TABLE generic_metrics DROP COLUMN partition_type;
ALTER TABLE generic_metrics ADD COLUMN partition_type TEXT DEFAULT 'GENERIC';
ALTER TABLE generic_metrics ALTER COLUMN partition_type SET NOT NULL;


-- Drop `metric_partition_type` enum.
Expand Down
Loading