Module: PartitionGardener::Connection

Defined in:
lib/partition_gardener/connection.rb

Defined Under Namespace

Classes: AttachedPartition

Class Method Summary collapse

Class Method Details

.attached_partitions(table_name) ⇒ Object



90
91
92
# File 'lib/partition_gardener/connection.rb', line 90

def attached_partitions(table_name)
  attached_partitions_cache[table_name] ||= fetch_attached_partitions(table_name)
end

.attached_partitions_cacheObject



118
119
120
# File 'lib/partition_gardener/connection.rb', line 118

def attached_partitions_cache
  Thread.current[:partition_gardener_attached_partitions] ||= {}
end

.clear_attached_partitions_cache!Object



94
95
96
# File 'lib/partition_gardener/connection.rb', line 94

def clear_attached_partitions_cache!
  Thread.current[:partition_gardener_attached_partitions] = nil
end

.connectionObject



5
6
7
# File 'lib/partition_gardener/connection.rb', line 5

def connection
  PartitionGardener.configuration.connection
end

.count_rows_in_partition(partition_name, where_condition) ⇒ Object



81
82
83
84
85
86
87
88
# File 'lib/partition_gardener/connection.rb', line 81

def count_rows_in_partition(partition_name, where_condition)
  sql = "    SELECT COUNT(*) AS count\n    FROM \#{quoted_table(partition_name)}\n    WHERE \#{where_condition}\n  SQL\n  connection.execute(sql).first[\"count\"].to_i\nend\n"

.count_rows_in_partition_table(partition_name) ⇒ Object



73
74
75
76
77
78
79
# File 'lib/partition_gardener/connection.rb', line 73

def count_rows_in_partition_table(partition_name)
  sql = "    SELECT COUNT(*) AS count\n    FROM \#{quoted_table(partition_name)}\n  SQL\n  connection.execute(sql).first[\"count\"].to_i\nend\n"

.current_partition_lower_bound(table_name, partition_name) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/partition_gardener/connection.rb', line 52

def current_partition_lower_bound(table_name, partition_name)
  sql = "    SELECT pg_get_expr(child.relpartbound, child.oid) AS bound_expression\n    FROM pg_catalog.pg_inherits inheritance\n    JOIN pg_class parent ON parent.oid = inheritance.inhparent\n    JOIN pg_namespace parent_namespace ON parent_namespace.oid = parent.relnamespace\n    JOIN pg_class child ON child.oid = inheritance.inhrelid\n    JOIN pg_namespace child_namespace ON child_namespace.oid = child.relnamespace\n    WHERE parent_namespace.nspname = \#{connection.quote(schema_name)}\n      AND child_namespace.nspname = \#{connection.quote(schema_name)}\n      AND parent.relname = \#{connection.quote(table_name)}\n      AND child.relname = \#{connection.quote(partition_name)}\n  SQL\n\n  bound_expression = connection.execute(sql).first&.fetch(\"bound_expression\", nil)\n  return if Blank.blank?(bound_expression)\n\n  match = bound_expression.match(/FROM \\('([^']+)'\\)/)\n  match ? Date.parse(match[1]) : nil\nend\n"

.fetch_attached_partitions(table_name) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/partition_gardener/connection.rb', line 98

def fetch_attached_partitions(table_name)
  sql = "    SELECT child.relname AS partition_name,\n           pg_get_expr(child.relpartbound, child.oid) AS bound_expression\n    FROM pg_catalog.pg_inherits inheritance\n    JOIN pg_class parent ON parent.oid = inheritance.inhparent\n    JOIN pg_namespace parent_namespace ON parent_namespace.oid = parent.relnamespace\n    JOIN pg_class child ON child.oid = inheritance.inhrelid\n    JOIN pg_namespace child_namespace ON child_namespace.oid = child.relnamespace\n    WHERE parent_namespace.nspname = \#{connection.quote(schema_name)}\n      AND child_namespace.nspname = \#{connection.quote(schema_name)}\n      AND parent.relname = \#{connection.quote(table_name)}\n    ORDER BY child.relname\n  SQL\n\n  connection.execute(sql).filter_map do |row|\n    parse_attached_partition(row[\"partition_name\"], row[\"bound_expression\"])\n  end\nend\n"

.get_distinct_partition_identifiers(partition_name, partition_key_column, extract_partition_identifier) ⇒ Object



195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
# File 'lib/partition_gardener/connection.rb', line 195

def get_distinct_partition_identifiers(partition_name, partition_key_column, extract_partition_identifier)
  if partition_key_column.include?("::")
    base_column = partition_key_column.split("::").first.strip
    sql = "      SELECT DISTINCT \#{partition_key_column} AS partition_key_value\n      FROM \#{quoted_table(partition_name)}\n      WHERE \#{connection.quote_column_name(base_column)} IS NOT NULL\n      ORDER BY \#{partition_key_column}\n    SQL\n  else\n    sql = <<~SQL\n      SELECT DISTINCT \#{connection.quote_column_name(partition_key_column)} AS partition_key_value\n      FROM \#{quoted_table(partition_name)}\n      WHERE \#{connection.quote_column_name(partition_key_column)} IS NOT NULL\n      ORDER BY \#{connection.quote_column_name(partition_key_column)}\n    SQL\n  end\n\n  connection.execute(sql).to_a.filter_map do |row|\n    value = row[\"partition_key_value\"]\n    extract_partition_identifier.call(value) if Blank.present?(value)\n  end.uniq\nend\n"

.parse_attached_partition(partition_name, bound_expression) ⇒ Object



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/partition_gardener/connection.rb', line 124

def parse_attached_partition(partition_name, bound_expression)
  return if Blank.blank?(bound_expression)

  if bound_expression == "DEFAULT"
    return AttachedPartition.new(
      name: partition_name,
      range_start: nil,
      range_end: nil,
      default: true,
      list_values: nil
    )
  end

  hash_match = bound_expression.match(/modulus (\d+), remainder (\d+)/)
  if hash_match
    return AttachedPartition.new(
      name: partition_name,
      range_start: {modulus: hash_match[1].to_i, remainder: hash_match[2].to_i},
      range_end: nil,
      default: false,
      list_values: nil
    )
  end

  list_match = bound_expression.match(/^IN \((.+)\)$/)
  if list_match
    values = list_match[1].split(",").map { |value| parse_list_value(value.strip) }
    return AttachedPartition.new(
      name: partition_name,
      range_start: values.first,
      range_end: nil,
      default: false,
      list_values: values
    )
  end

  from_match = bound_expression.match(/FROM \('([^']+)'\)/) || bound_expression.match(/FROM \((\d+)\)/)
  to_match = bound_expression.match(/TO \('([^']+)'\)/) || bound_expression.match(/TO \((\d+)\)/)
  range_end = if bound_expression.include?("MAXVALUE")
    :max
  elsif to_match
    parse_bound_value(to_match[1])
  end

  AttachedPartition.new(
    name: partition_name,
    range_start: from_match ? parse_bound_value(from_match[1]) : nil,
    range_end: range_end,
    default: false,
    list_values: nil
  )
end

.parse_bound_value(value) ⇒ Object



183
184
185
186
187
# File 'lib/partition_gardener/connection.rb', line 183

def parse_bound_value(value)
  return Date.parse(value) if value.include?("-")

  value.to_i
end

.parse_list_value(value) ⇒ Object



177
178
179
180
181
# File 'lib/partition_gardener/connection.rb', line 177

def parse_list_value(value)
  return nil if value.upcase == "NULL"

  value.delete_prefix("'").delete_suffix("'")
end

.partition_attached?(table_name, partition_name) ⇒ Boolean

Returns:

  • (Boolean)


35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/partition_gardener/connection.rb', line 35

def partition_attached?(table_name, partition_name)
  sql = "    SELECT COUNT(*) AS count\n    FROM pg_catalog.pg_inherits i\n    JOIN pg_class parent ON parent.oid = i.inhparent\n    JOIN pg_namespace parent_ns ON parent_ns.oid = parent.relnamespace\n    JOIN pg_class child ON child.oid = i.inhrelid\n    JOIN pg_namespace child_ns ON child_ns.oid = child.relnamespace\n    WHERE parent_ns.nspname = \#{connection.quote(schema_name)}\n      AND child_ns.nspname = \#{connection.quote(schema_name)}\n      AND parent.relname = \#{connection.quote(table_name)}\n      AND child.relname = \#{connection.quote(partition_name)}\n  SQL\n\n  connection.execute(sql).first[\"count\"].to_i.positive?\nend\n"

.partition_exists?(partition_name) ⇒ Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/partition_gardener/connection.rb', line 31

def partition_exists?(partition_name)
  connection.table_exists?(partition_name)
end

.partman_parent_configured?(table_name) ⇒ Boolean

Returns:

  • (Boolean)


248
249
250
251
252
253
254
255
256
257
258
# File 'lib/partition_gardener/connection.rb', line 248

def partman_parent_configured?(table_name)
  qualified_name = "#{schema_name}.#{table_name}"
  sql = "    SELECT 1\n    FROM partman.part_config\n    WHERE parent_table = \#{connection.quote(qualified_name)}\n    LIMIT 1\n  SQL\n\n  connection.execute(sql).any?\nend\n"

.quoted_table(name) ⇒ Object



13
14
15
# File 'lib/partition_gardener/connection.rb', line 13

def quoted_table(name)
  connection.quote_table_name(name)
end

.schema_nameObject



9
10
11
# File 'lib/partition_gardener/connection.rb', line 9

def schema_name
  PartitionGardener.configuration.schema_name
end

.table_is_partitioned?(table_name) ⇒ Boolean

Returns:

  • (Boolean)


17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/partition_gardener/connection.rb', line 17

def table_is_partitioned?(table_name)
  return false unless connection.table_exists?(table_name)

  sql = "    SELECT COUNT(*) AS count\n    FROM pg_partitioned_table pt\n    JOIN pg_class c ON c.oid = pt.partrelid\n    JOIN pg_namespace n ON n.oid = c.relnamespace\n    WHERE n.nspname = \#{connection.quote(schema_name)}\n      AND c.relname = \#{connection.quote(table_name)}\n  SQL\n  connection.execute(sql).first[\"count\"].to_i.positive?\nend\n"

.unique_index_column_sets(table_name) ⇒ Object



219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
# File 'lib/partition_gardener/connection.rb', line 219

def unique_index_column_sets(table_name)
  sql = "    SELECT i.indisprimary AS is_primary,\n           array_agg(a.attname ORDER BY key_ord.ordinality) AS column_names\n    FROM pg_index i\n    JOIN pg_class table_class ON table_class.oid = i.indrelid\n    JOIN pg_namespace table_namespace ON table_namespace.oid = table_class.relnamespace\n    JOIN LATERAL unnest(i.indkey) WITH ORDINALITY AS key_ord(attnum, ordinality) ON true\n    JOIN pg_attribute a ON a.attrelid = table_class.oid AND a.attnum = key_ord.attnum\n    WHERE table_namespace.nspname = \#{connection.quote(schema_name)}\n      AND table_class.relname = \#{connection.quote(table_name)}\n      AND (i.indisunique OR i.indisprimary)\n    GROUP BY i.indexrelid, i.indisprimary\n  SQL\n\n  connection.execute(sql).map do |row|\n    columns = row[\"column_names\"]\n    columns = columns.gsub(/[{}]/, \"\").split(\",\") if columns.is_a?(String)\n    columns\n  end\nend\n"

.unique_index_covers?(table_name, conflict_key) ⇒ Boolean

Returns:

  • (Boolean)


241
242
243
244
245
246
# File 'lib/partition_gardener/connection.rb', line 241

def unique_index_covers?(table_name, conflict_key)
  conflict_columns = conflict_key.map(&:to_s)
  unique_index_column_sets(table_name).any? do |index_columns|
    index_columns.first(conflict_columns.length) == conflict_columns
  end
end