Module: PgEasyReplicate::IndexManager

Extended by:
Helper
Defined in:
lib/pg_easy_replicate/index_manager.rb

Class Method Summary collapse

Methods included from Helper

abort_with, connection_info, convert_to_array, db_name, db_user, determine_tables, internal_schema_name, internal_user_name, list_all_tables, logger, publication_name, quote_ident, restore_connections_on_source_db, secondary_source_db_url, source_db_url, subscription_name, target_db_url, test_env?, underscore, validate_table_lists

Class Method Details

.drop_indices(source_conn_string:, target_conn_string:, tables:, schema:) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/pg_easy_replicate/index_manager.rb', line 7

def self.drop_indices(
  source_conn_string:,
  target_conn_string:,
  tables:,
  schema:
)
  logger.info("Dropping indices from target database")

  fetch_indices(
    conn_string: source_conn_string,
    tables: tables,
    schema: schema,
  ).each do |index|
    drop_sql = "DROP INDEX CONCURRENTLY #{schema}.#{index[:index_name]};"

    Query.run(
      query: drop_sql,
      connection_url: target_conn_string,
      schema: schema,
      transaction: false,
    )
  end
end

.fetch_indices(conn_string:, tables:, schema:) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/pg_easy_replicate/index_manager.rb', line 58

def self.fetch_indices(conn_string:, tables:, schema:)
  return [] if tables.empty?
  table_list = tables.map { |table| "'#{table}'" }.join(",")

  sql = "    SELECT\n        t.relname AS table_name,\n        i.relname AS index_name,\n        pg_get_indexdef(i.oid) AS index_definition\n    FROM\n        pg_class t,\n        pg_class i,\n        pg_index ix,\n        pg_namespace n\n    WHERE\n        t.oid = ix.indrelid\n        AND i.oid = ix.indexrelid\n        AND n.oid = t.relnamespace\n        AND t.relkind = 'r'  -- only find indexes of tables\n        AND ix.indisprimary = FALSE  -- exclude primary keys\n        AND ix.indisunique = FALSE  -- exclude unique indexes\n        AND n.nspname = '\#{schema}'\n        AND t.relname IN (\#{table_list})\n    ORDER BY\n        t.relname,\n        i.relname;\n  SQL\n  Query.run(query: sql, connection_url: conn_string, schema: schema)\nend\n"

.recreate_indices(source_conn_string:, target_conn_string:, tables:, schema:) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/pg_easy_replicate/index_manager.rb', line 31

def self.recreate_indices(
  source_conn_string:,
  target_conn_string:,
  tables:,
  schema:
)
  logger.info("Recreating indices on target database")

  indices =
    fetch_indices(
      conn_string: source_conn_string,
      tables: tables,
      schema: schema,
    )
  indices.each do |index|
    create_sql =
      "#{index[:index_definition].gsub("CREATE INDEX", "CREATE INDEX CONCURRENTLY IF NOT EXISTS")};"

    Query.run(
      query: create_sql,
      connection_url: target_conn_string,
      schema: schema,
      transaction: false,
    )
  end
end

.wait_for_replication_completion(group_name:) ⇒ Object



88
89
90
91
92
93
# File 'lib/pg_easy_replicate/index_manager.rb', line 88

def self.wait_for_replication_completion(group_name:)
  loop do
    break if Stats.all_tables_replicating?(group_name)
    sleep(5)
  end
end