Skip to content

Commit

Permalink
Validate one constraint at a time (#124)
Browse files Browse the repository at this point in the history
Just an optimization change to ensure a single transaction opened for too long when dealing
with large tables
  • Loading branch information
shayonj authored Oct 15, 2023
1 parent 60b7db9 commit 8f10359
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 25 deletions.
21 changes: 14 additions & 7 deletions lib/pg_online_schema_change/orchestrate.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,10 @@ def setup_store
Store.set(:trigger_time_column, "trigger_time_#{pgosc_identifier}")
Store.set(:audit_table_pk, "at_#{pgosc_identifier}_id")
Store.set(:shadow_table, "pgosc_st_#{client.table.downcase}_#{pgosc_identifier}")
Store.set(:primary_table_storage_parameters, Query.storage_parameters_for(client, client.table_name, true) || "")
Store.set(
:primary_table_storage_parameters,
Query.storage_parameters_for(client, client.table_name, true) || "",
)

Store.set(
:referential_foreign_key_statements,
Expand Down Expand Up @@ -109,7 +112,10 @@ def setup_audit_table!

Query.run(client.connection, sql)

Store.set(:audit_table_pk_sequence, Query.get_sequence_name(client, audit_table, audit_table_pk))
Store.set(
:audit_table_pk_sequence,
Query.get_sequence_name(client, audit_table, audit_table_pk),
)
end

def setup_trigger!
Expand Down Expand Up @@ -270,11 +276,12 @@ def run_analyze!
end

def validate_constraints!
logger.info("Validating constraints!")

validate_statements = Query.get_foreign_keys_to_validate(client, client.table_name)

Query.run(client.connection, validate_statements)
Query
.get_foreign_keys_to_validate(client, client.table_name)
.each do |statement|
logger.info("Validating constraints!", statement: statement)
Query.run(client.connection, statement)
end
end

def replace_views!
Expand Down
17 changes: 7 additions & 10 deletions lib/pg_online_schema_change/query.rb
Original file line number Diff line number Diff line change
Expand Up @@ -140,10 +140,9 @@ def get_sequence_name(client, table, column)
SELECT pg_get_serial_sequence('#{table}', '#{column}');
SQL

run(client.connection, query) { |result| result.map { |row|
row["pg_get_serial_sequence"]
}
}.first
run(client.connection, query) do |result|
result.map { |row| row["pg_get_serial_sequence"] }
end.first
end

def get_triggers_for(client, table)
Expand Down Expand Up @@ -238,11 +237,9 @@ def get_foreign_keys_to_validate(client, table)
self_foreign_keys =
constraints.select { |row| row["table_on"] == table && row["constraint_type"] == "f" }

[referential_foreign_keys, self_foreign_keys].flatten
.map do |row|
"ALTER TABLE #{row["table_on"]} VALIDATE CONSTRAINT #{row["constraint_name"]};"
end
.join
[referential_foreign_keys, self_foreign_keys].flatten.map do |row|
"ALTER TABLE #{row["table_on"]} VALIDATE CONSTRAINT #{row["constraint_name"]};"
end
end

def dropped_columns(client)
Expand Down Expand Up @@ -327,7 +324,7 @@ def view_definitions_for(client, table)

definitions = []
run(client.connection, query) do |result|
definitions = result.map { |row| {row["view_name"] => row["view_definition"].strip} }
definitions = result.map { |row| { row["view_name"] => row["view_definition"].strip } }
end

definitions
Expand Down
23 changes: 15 additions & 8 deletions spec/lib/query_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -457,9 +457,13 @@
before { setup_tables(client) }

it "returns drop and add statements" do
result =
"ALTER TABLE book_audits VALIDATE CONSTRAINT book_audits_book_id_fkey;ALTER TABLE chapters VALIDATE CONSTRAINT chapters_book_id_fkey;ALTER TABLE books VALIDATE CONSTRAINT books_seller_id_fkey;"
expect(described_class.get_foreign_keys_to_validate(client, "books")).to eq(result)
expect(described_class.get_foreign_keys_to_validate(client, "books")).to eq(
[
"ALTER TABLE book_audits VALIDATE CONSTRAINT book_audits_book_id_fkey;",
"ALTER TABLE chapters VALIDATE CONSTRAINT chapters_book_id_fkey;",
"ALTER TABLE books VALIDATE CONSTRAINT books_seller_id_fkey;",
],
)
end
end

Expand Down Expand Up @@ -693,11 +697,14 @@
ingest_dummy_data_into_dummy_table(client)
described_class.run(client.connection, "reset search_path")
result = described_class.view_definitions_for(client, "books")
expect(result).to eq([
{
"books_view" =>"SELECT books.user_id,\n books.username,\n books.seller_id,\n books.password,\n books.email,\n books.\"createdOn\",\n books.last_login\n FROM test_schema.books\n WHERE (books.seller_id = 1);",
}
])
expect(result).to eq(
[
{
"books_view" =>
"SELECT books.user_id,\n books.username,\n books.seller_id,\n books.password,\n books.email,\n books.\"createdOn\",\n books.last_login\n FROM test_schema.books\n WHERE (books.seller_id = 1);",
},
],
)
end
end

Expand Down

0 comments on commit 8f10359

Please sign in to comment.