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

Building PK's Concurrently for Partitions #65

Merged
merged 5 commits into from
Aug 31, 2023
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
163 changes: 158 additions & 5 deletions internal/migration_acceptance_tests/partitioned_index_cases_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,6 @@ var partitionedIndexAcceptanceTestCases = []acceptanceTestCase{
},
expectedHazardTypes: []diff.MigrationHazardType{
diff.MigrationHazardTypeAcquiresAccessExclusiveLock,
diff.MigrationHazardTypeAcquiresShareLock,
diff.MigrationHazardTypeIndexBuild,
},
},
Expand Down Expand Up @@ -252,7 +251,6 @@ var partitionedIndexAcceptanceTestCases = []acceptanceTestCase{
expectedHazardTypes: []diff.MigrationHazardType{
diff.MigrationHazardTypeAcquiresAccessExclusiveLock,
diff.MigrationHazardTypeIndexBuild,
diff.MigrationHazardTypeAcquiresShareLock,
},
},
{
Expand All @@ -279,7 +277,6 @@ var partitionedIndexAcceptanceTestCases = []acceptanceTestCase{
},
expectedHazardTypes: []diff.MigrationHazardType{
diff.MigrationHazardTypeAcquiresAccessExclusiveLock,
diff.MigrationHazardTypeAcquiresShareLock,
diff.MigrationHazardTypeIndexDropped,
diff.MigrationHazardTypeIndexBuild,
},
Expand Down Expand Up @@ -309,7 +306,6 @@ var partitionedIndexAcceptanceTestCases = []acceptanceTestCase{
},
expectedHazardTypes: []diff.MigrationHazardType{
diff.MigrationHazardTypeAcquiresAccessExclusiveLock,
diff.MigrationHazardTypeAcquiresShareLock,
diff.MigrationHazardTypeIndexBuild,
},
},
Expand Down Expand Up @@ -569,7 +565,6 @@ var partitionedIndexAcceptanceTestCases = []acceptanceTestCase{
`,
},
expectedHazardTypes: []diff.MigrationHazardType{
diff.MigrationHazardTypeAcquiresShareLock,
diff.MigrationHazardTypeAcquiresAccessExclusiveLock,
diff.MigrationHazardTypeIndexDropped,
diff.MigrationHazardTypeIndexBuild,
Expand Down Expand Up @@ -623,6 +618,164 @@ var partitionedIndexAcceptanceTestCases = []acceptanceTestCase{
`,
},
},
{
name: "Add primary key constraint with existing matching base-table index (matching child index does not exist)",
oldSchemaDDL: []string{
`
CREATE TABLE foobar(
id INT NOT NULL,
foo VARCHAR(255) NOT NULL
Comment on lines +626 to +627
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this indent for foo correct?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like the entire file is messed up. It renders okay in my editor...I'm guessing this is using tabs instead of spaces, which happens to align okay in my editor. I'll fix in a subsequent PR

) PARTITION BY LIST (foo);
CREATE TABLE foobar_1 PARTITION OF foobar FOR VALUES IN ('foo_1');

-- A unique index for foobar_1 exists, but it does not have the default name as the primary key constraint (foobar_1_pkey),
-- so the primary key index effectively does not already exist when migrating
CREATE UNIQUE INDEX foobar_pkey ON foobar(foo, id);
`,
},
newSchemaDDL: []string{
`
CREATE TABLE foobar(
id INT NOT NULL,
foo VARCHAR(255) NOT NULL
) PARTITION BY LIST (foo);
CREATE TABLE foobar_1 PARTITION OF foobar FOR VALUES IN ('foo_1');

ALTER TABLE foobar ADD CONSTRAINT foobar_pkey PRIMARY KEY (foo, id);
`,
},
expectedHazardTypes: []diff.MigrationHazardType{
// The parent table index is dropped and rebuilt
diff.MigrationHazardTypeIndexDropped,
diff.MigrationHazardTypeIndexBuild,
diff.MigrationHazardTypeAcquiresAccessExclusiveLock,
},
},
{
name: "Add primary key constraint with existing matching base-table index (local matching child index exists)",
oldSchemaDDL: []string{
`
CREATE TABLE foobar(
id INT NOT NULL,
foo VARCHAR(255) NOT NULL
) PARTITION BY LIST (foo);
CREATE TABLE foobar_1 PARTITION OF foobar FOR VALUES IN ('foo_1');

CREATE UNIQUE INDEX foobar_pkey ON ONLY foobar(foo, id);
CREATE UNIQUE INDEX foobar_1_pkey ON foobar_1(foo, id);
ALTER INDEX foobar_pkey ATTACH PARTITION foobar_1_pkey;
`,
},
newSchemaDDL: []string{
`
CREATE TABLE foobar(
id INT NOT NULL,
foo VARCHAR(255) NOT NULL
) PARTITION BY LIST (foo);
CREATE TABLE foobar_1 PARTITION OF foobar FOR VALUES IN ('foo_1');

ALTER TABLE foobar ADD CONSTRAINT foobar_pkey PRIMARY KEY (foo, id);
`,
},
expectedHazardTypes: []diff.MigrationHazardType{
// The parent table index is dropped and rebuilt
diff.MigrationHazardTypeIndexDropped,
diff.MigrationHazardTypeIndexBuild,
diff.MigrationHazardTypeAcquiresAccessExclusiveLock,
},
},
{
name: "Add primary key constraint with existing matching base-table index (matching non-local index exists that backs local matching PK)",
oldSchemaDDL: []string{
`
CREATE TABLE foobar(
id INT NOT NULL,
foo VARCHAR(255) NOT NULL
) PARTITION BY LIST (foo);
CREATE TABLE foobar_1 PARTITION OF foobar FOR VALUES IN ('foo_1');

CREATE UNIQUE INDEX foobar_pkey ON foobar(foo, id);
ALTER TABLE foobar_1 ADD CONSTRAINT foobar_1_pkey PRIMARY KEY USING INDEX foobar_1_foo_id_idx;
`,
},
newSchemaDDL: []string{
`
CREATE TABLE foobar(
id INT NOT NULL,
foo VARCHAR(255) NOT NULL
) PARTITION BY LIST (foo);
CREATE TABLE foobar_1 PARTITION OF foobar FOR VALUES IN ('foo_1');

ALTER TABLE foobar ADD CONSTRAINT foobar_pkey PRIMARY KEY (foo, id);
`,
},
vanillaExpectations: expectations{
planErrorIs: diff.ErrNotImplemented,
},
dataPackingExpectations: expectations{
planErrorIs: diff.ErrNotImplemented,
},
},
{
name: "Add primary key constraint with existing matching base-table index (matching local PK already exists backed by local index)",
oldSchemaDDL: []string{
`
CREATE TABLE foobar(
id INT NOT NULL,
foo VARCHAR(255) NOT NULL
) PARTITION BY LIST (foo);
CREATE TABLE foobar_1 PARTITION OF foobar FOR VALUES IN ('foo_1');

CREATE UNIQUE INDEX foobar_pkey ON ONLY foobar(foo, id);
ALTER TABLE foobar_1 ADD CONSTRAINT foobar_1_pkey PRIMARY KEY(foo, id);
`,
},
newSchemaDDL: []string{
`
CREATE TABLE foobar(
id INT NOT NULL,
foo VARCHAR(255) NOT NULL
) PARTITION BY LIST (foo);
CREATE TABLE foobar_1 PARTITION OF foobar FOR VALUES IN ('foo_1');

ALTER TABLE foobar ADD CONSTRAINT foobar_pkey PRIMARY KEY (foo, id);
`,
},
expectedHazardTypes: []diff.MigrationHazardType{
// Base table index is dropped
diff.MigrationHazardTypeIndexDropped,
diff.MigrationHazardTypeAcquiresAccessExclusiveLock,
},
},
{
name: "Add key constraint to partition using existing index",
oldSchemaDDL: []string{
`
CREATE TABLE foobar(
id INT NOT NULL,
foo VARCHAR(255) NOT NULL
) PARTITION BY LIST (foo);
CREATE TABLE foobar_1 PARTITION OF foobar FOR VALUES IN ('foo_1');

ALTER TABLE ONLY foobar ADD CONSTRAINT some_pkey PRIMARY KEY (foo, id);
CREATE UNIQUE INDEX foobar_1_pkey ON foobar_1(foo, id);
`,
},
newSchemaDDL: []string{
`
CREATE TABLE foobar(
id INT NOT NULL,
foo VARCHAR(255) NOT NULL
) PARTITION BY LIST (foo);
CREATE TABLE foobar_1 PARTITION OF foobar FOR VALUES IN ('foo_1');

ALTER TABLE ONLY foobar ADD CONSTRAINT some_pkey PRIMARY KEY (foo, id);
CREATE UNIQUE INDEX foobar_1_pkey ON foobar_1(foo, id);
ALTER TABLE foobar_1 ADD CONSTRAINT foobar_1_pkey PRIMARY KEY USING INDEX foobar_1_pkey;
ALTER INDEX some_pkey ATTACH PARTITION foobar_1_pkey;
`,
},
},
}

func (suite *acceptanceTestSuite) TestPartitionedIndexAcceptanceTestCases() {
Expand Down
14 changes: 8 additions & 6 deletions internal/queries/queries.sql
Original file line number Diff line number Diff line change
Expand Up @@ -60,17 +60,22 @@ SELECT
table_c.relname::TEXT AS table_name,
pg_catalog.pg_get_indexdef(c.oid)::TEXT AS def_stmt,
COALESCE(con.conname, '')::TEXT AS constraint_name,
COALESCE(con.contype, '')::TEXT AS constraint_type,
COALESCE(
pg_catalog.pg_get_constraintdef(con.oid), ''
)::TEXT AS constraint_def,
i.indisvalid AS index_is_valid,
i.indisprimary AS index_is_pk,
i.indisunique AS index_is_unique,
COALESCE(parent_c.relname, '')::TEXT AS parent_index_name,
COALESCE(parent_namespace.nspname, '')::TEXT AS parent_index_schema_name
COALESCE(parent_namespace.nspname, '')::TEXT AS parent_index_schema_name,
COALESCE(con.conislocal, false) AS constraint_is_local
FROM pg_catalog.pg_class AS c
INNER JOIN pg_catalog.pg_index AS i ON (i.indexrelid = c.oid)
INNER JOIN pg_catalog.pg_class AS table_c ON (table_c.oid = i.indrelid)
LEFT JOIN
pg_catalog.pg_constraint AS con
ON (con.conindid = c.oid AND con.contype IN ('p', 'u', NULL))
ON (con.conindid = c.oid AND con.contype IN ('p', 'u', null))
LEFT JOIN
pg_catalog.pg_inherits AS idx_inherits
ON (c.oid = idx_inherits.inhrelid)
Expand Down Expand Up @@ -132,9 +137,6 @@ INNER JOIN
INNER JOIN pg_catalog.pg_namespace AS foreign_table_namespace
ON
foreign_table_c.relnamespace = foreign_table_namespace.oid
INNER JOIN
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This join isn't used for anything

pg_catalog.pg_class AS index_c
ON pg_constraint.conindid = index_c.oid
WHERE
constraint_namespace.nspname = 'public'
AND pg_constraint.contype = 'f'
Expand Down Expand Up @@ -229,7 +231,7 @@ SELECT
pg_seq.seqmin AS min_value,
pg_seq.seqcache AS cache_size,
pg_seq.seqcycle AS is_cycle,
FORMAT_TYPE(pg_seq.seqtypid, NULL) AS data_type
FORMAT_TYPE(pg_seq.seqtypid, null) AS data_type
FROM pg_catalog.pg_sequence AS pg_seq
INNER JOIN pg_catalog.pg_class AS seq_c ON pg_seq.seqrelid = seq_c.oid
INNER JOIN pg_catalog.pg_namespace AS seq_ns ON seq_c.relnamespace = seq_ns.oid
Expand Down
20 changes: 14 additions & 6 deletions internal/queries/queries.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading