Module: PartitionGardener::Connection
- Defined in:
- lib/partition_gardener/connection.rb
Defined Under Namespace
Classes: AttachedPartition
Class Method Summary collapse
- .attached_partitions(table_name) ⇒ Object
- .attached_partitions_cache ⇒ Object
- .clear_attached_partitions_cache! ⇒ Object
- .connection ⇒ Object
- .count_rows_in_partition(partition_name, where_condition) ⇒ Object
- .count_rows_in_partition_table(partition_name) ⇒ Object
- .current_partition_lower_bound(table_name, partition_name) ⇒ Object
- .fetch_attached_partitions(table_name) ⇒ Object
- .get_distinct_partition_identifiers(partition_name, partition_key_column, extract_partition_identifier) ⇒ Object
- .parse_attached_partition(partition_name, bound_expression) ⇒ Object
- .parse_bound_value(value) ⇒ Object
- .parse_list_value(value) ⇒ Object
- .partition_attached?(table_name, partition_name) ⇒ Boolean
- .partition_exists?(partition_name) ⇒ Boolean
- .partman_parent_configured?(table_name) ⇒ Boolean
- .quoted_table(name) ⇒ Object
- .schema_name ⇒ Object
- .table_is_partitioned?(table_name) ⇒ Boolean
- .unique_index_column_sets(table_name) ⇒ Object
- .unique_index_covers?(table_name, conflict_key) ⇒ Boolean
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_cache ⇒ Object
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 |
.connection ⇒ Object
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 = <<~SQL SELECT COUNT(*) AS count FROM #{quoted_table(partition_name)} WHERE #{where_condition} SQL connection.execute(sql).first["count"].to_i end |
.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 = <<~SQL SELECT COUNT(*) AS count FROM #{quoted_table(partition_name)} SQL connection.execute(sql).first["count"].to_i end |
.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 = <<~SQL SELECT pg_get_expr(child.relpartbound, child.oid) AS bound_expression FROM pg_catalog.pg_inherits inheritance JOIN pg_class parent ON parent.oid = inheritance.inhparent JOIN pg_namespace parent_namespace ON parent_namespace.oid = parent.relnamespace JOIN pg_class child ON child.oid = inheritance.inhrelid JOIN pg_namespace child_namespace ON child_namespace.oid = child.relnamespace WHERE parent_namespace.nspname = #{connection.quote(schema_name)} AND child_namespace.nspname = #{connection.quote(schema_name)} AND parent.relname = #{connection.quote(table_name)} AND child.relname = #{connection.quote(partition_name)} SQL bound_expression = connection.execute(sql).first&.fetch("bound_expression", nil) return if Blank.blank?(bound_expression) match = bound_expression.match(/FROM \('([^']+)'\)/) match ? Date.parse(match[1]) : nil end |
.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 = <<~SQL SELECT child.relname AS partition_name, pg_get_expr(child.relpartbound, child.oid) AS bound_expression FROM pg_catalog.pg_inherits inheritance JOIN pg_class parent ON parent.oid = inheritance.inhparent JOIN pg_namespace parent_namespace ON parent_namespace.oid = parent.relnamespace JOIN pg_class child ON child.oid = inheritance.inhrelid JOIN pg_namespace child_namespace ON child_namespace.oid = child.relnamespace WHERE parent_namespace.nspname = #{connection.quote(schema_name)} AND child_namespace.nspname = #{connection.quote(schema_name)} AND parent.relname = #{connection.quote(table_name)} ORDER BY child.relname SQL connection.execute(sql).filter_map do |row| parse_attached_partition(row["partition_name"], row["bound_expression"]) end end |
.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 = <<~SQL SELECT DISTINCT #{partition_key_column} AS partition_key_value FROM #{quoted_table(partition_name)} WHERE #{connection.quote_column_name(base_column)} IS NOT NULL ORDER BY #{partition_key_column} SQL else sql = <<~SQL SELECT DISTINCT #{connection.quote_column_name(partition_key_column)} AS partition_key_value FROM #{quoted_table(partition_name)} WHERE #{connection.quote_column_name(partition_key_column)} IS NOT NULL ORDER BY #{connection.quote_column_name(partition_key_column)} SQL end connection.execute(sql).to_a.filter_map do |row| value = row["partition_key_value"] extract_partition_identifier.call(value) if Blank.present?(value) end.uniq end |
.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
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 = <<~SQL SELECT COUNT(*) AS count FROM pg_catalog.pg_inherits i JOIN pg_class parent ON parent.oid = i.inhparent JOIN pg_namespace parent_ns ON parent_ns.oid = parent.relnamespace JOIN pg_class child ON child.oid = i.inhrelid JOIN pg_namespace child_ns ON child_ns.oid = child.relnamespace WHERE parent_ns.nspname = #{connection.quote(schema_name)} AND child_ns.nspname = #{connection.quote(schema_name)} AND parent.relname = #{connection.quote(table_name)} AND child.relname = #{connection.quote(partition_name)} SQL connection.execute(sql).first["count"].to_i.positive? end |
.partition_exists?(partition_name) ⇒ 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
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 = <<~SQL SELECT 1 FROM partman.part_config WHERE parent_table = #{connection.quote(qualified_name)} LIMIT 1 SQL connection.execute(sql).any? end |
.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_name ⇒ Object
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
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 = <<~SQL SELECT COUNT(*) AS count FROM pg_partitioned_table pt JOIN pg_class c ON c.oid = pt.partrelid JOIN pg_namespace n ON n.oid = c.relnamespace WHERE n.nspname = #{connection.quote(schema_name)} AND c.relname = #{connection.quote(table_name)} SQL connection.execute(sql).first["count"].to_i.positive? end |
.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 = <<~SQL SELECT i.indisprimary AS is_primary, array_agg(a.attname ORDER BY key_ord.ordinality) AS column_names FROM pg_index i JOIN pg_class table_class ON table_class.oid = i.indrelid JOIN pg_namespace table_namespace ON table_namespace.oid = table_class.relnamespace JOIN LATERAL unnest(i.indkey) WITH ORDINALITY AS key_ord(attnum, ordinality) ON true JOIN pg_attribute a ON a.attrelid = table_class.oid AND a.attnum = key_ord.attnum WHERE table_namespace.nspname = #{connection.quote(schema_name)} AND table_class.relname = #{connection.quote(table_name)} AND (i.indisunique OR i.indisprimary) GROUP BY i.indexrelid, i.indisprimary SQL connection.execute(sql).map do |row| columns = row["column_names"] columns = columns.gsub(/[{}]/, "").split(",") if columns.is_a?(String) columns end end |
.unique_index_covers?(table_name, conflict_key) ⇒ 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 |