ActiveRecord PostgreSQL 分区
ActiveRecord migrations and model helpers for creating and managing PostgreSQL 10+ partitions!
db/schema.rb — please use the :sql schema formatThis gem is tested against:
I plan to separate out the model functionality into a new gem and port the migration functionality into pg_ha_migrations (some of which has already been done). I will continue to maintain this gem (bugfixes / support for new versions of Rails) until that work is complete.
I originally planned to add a feature for automatic partition creation, but I think that functionality would be better served by pg_partman.
Add this line to your application's Gemfile:
gem 'pg_party'And then execute:
$ bundleOr install it yourself as:
$ gem install pg_partyNote that the gemspec does not require pg, as some model methods may work for other databases.
Migration methods will be unavailable unless pg is installed.
These values can be accessed and set via PgParty.config and PgParty.configure.
cachingtruecaching_ttl-1 (never expire cache entries)schema_exclude_partitionsrake db:structure:dumptruecreate_template_tablestemplate: option when creating partitioned tables to override this default.truecreate_with_primary_keyprimary_key: [:id, :created_at], partition_key: :created_atcreate_with_primary_key: option when creating partitioned tablesfalseinclude_subpartitions_in_partition_listYourModelClass.partiton_list mby default.
You can always pass the include_subpartitions: option to override this.false (for backward compatibility)Note that caching is done in-memory for each process of an application. Attaching / detaching partitions will clear the cache, but only for the process that initiated the request. For multi-process web servers, it is recommended to use a TTL or disable caching entirely.
# in a Rails initializer
PgParty.configure do |c|
c.caching_ttl = 60
c.schema_exclude_partitions = false
c.include_subpartitions_in_partition_list = true
# Postgres 11+ users starting fresh may consider the below options to rely on Postgres' native features instead of
# this gem's template tables feature.
c.create_template_tables = false
c.create_with_primary_key = true
endThese methods are available in migrations as well as ActiveRecord::Base#connection objects.
create_range_partitiontable_name, partitition_key:create_list_partitiontable_name, partition_key:create_hash_partition (Postgres 11+)table_name, partition_key:create_range_partition_oftable_name, start_range:, end_range:partition_type: of :range, :list, or :hash and a partition_key:create_list_partition_oftable_name, values:partition_type: of :range, :list, or :hash and a partition_key:create_hash_partition_of (Postgres 11+)table_name, modulus:, remainderpartition_type: of :range, :list, or :hash and a partition_key:create_default_partition_of (Postgres 11+)table_nameattach_range_partitionparent_table_name, child_table_name, start_range:, end_range:attach_list_partitionparent_table_name, child_table_name, values:attach_hash_partition (Postgres 11+)parent_table_name, child_table_name, modulus:, remainderattach_default_partition (Postgres 11+)parent_table_name, child_table_namedetach_partitionparent_table_name, child_table_namecreate_table_liketable_name, new_table_namepartitions_for_table_nametable_name, include_subpartitions: (true or false)parent_for_table_nametable_nametraverse: true to return the top-level table in the hierarchy (for subpartitions)nil if the table is not a partition / has no parenttable_partitioned?table_nameadd_index_on_all_partitionstable_name using Postgres's ADD INDEX CONCURRENTLY
algorithm which adds the index in a non-blocking manner.table_name, column_name (all add_index arguments are supported)in_threads: option to add indexes in parallel threads when there are many partitions. A value of 2 to 4
may be reasonable for tables with many large partitions and hosts with 4+ CPUs/cores.disable_ddl_transaction! in your migration to disable transactions when using this command with in_threads:
or algorithm: :concurrently.Create range partitioned table on created_at::date with two partitions:
class CreateSomeRangeRecord { "(created_at::date)" } do |t|
t.text :some_value
t.timestamps
end
# optional name argument is used to specify child table name
create_range_partition_of \
:some_range_records,
name: :some_range_records_a,
start_range: "2019-06-07",
end_range: "2019-06-08"
# optional name argument is used to specify child table name
create_range_partition_of \
:some_range_records,
name: :some_range_records_b,
start_range: "2019-06-08",
end_range: "2019-06-09"
end
endCreate list partitioned table on id with two partitions:
class CreateSomeListRecord "
create_list_partition_of \
:some_list_records,
values: 1..100
# without name argument, child partition created as "some_list_records_"
create_list_partition_of \
:some_list_records,
values: 101..200
# default partition support is available in Postgres 11 or higher
create_default_partition_of \
:some_list_records
end
endCreate hash partitioned table on account_id with two partitions (Postgres 11+ required):
modulus: should always equal the total number of partitions planned for the tableremainder: is an integer which should be in the range of 0 to modulus-1class CreateSomeHashRecord "
create_hash_partition_of \
:some_hash_records,
modulus: 2,
remainder: 0
# without name argument, child partition created as "some_list_records_"
create_hash_partition_of \
:some_hash_records,
modulus: 2,
remainder: 1
end
endAdvanced example with subpartitioning: Create list partitioned table on account_id subpartitioned by range on created_at
with default partitions. This example is for a table with no primary key... perhaps for some analytics use case.
…Unfortunately, PostgreSQL 10 doesn't support indexes on partitioned tables.
However, individual partitions can have indexes.
To avoid explicit index creation for every new partition, we've introduced the idea of template tables.
For every call to create_list_partition and create_range_partition, a clone _template is created.
Indexes, constraints, etc. created on the template table will propagate to new partitions in calls to create_list_partition_of and create_range_partition_of:
…Attach an existing table to a range partitioned table:
class AttachRangePartition < ActiveRecord::Migration[5.1]
def up
attach_range_partition \
:some_range_records,
:some_existing_table,
start_range: "2019-06-09",
end_range: "2019-06-10"
end
endAttach an existing table to a list partitioned table:
class AttachListPartition < ActiveRecord::Migration[5.1]
def up暂无开放 Issues,或尚未同步最近议题。