Module: PgPartitioner::SeparationType::Quater
- Defined in:
- lib/pg_partitioner/separation_type/quater.rb
Instance Method Summary collapse
- #create_current_quater_table ⇒ Object
- #create_next_quater_table ⇒ Object
- #create_partitioning_by_quater_triggers ⇒ Object
- #create_quater_table(date = Date.today) ⇒ Object
Instance Method Details
#create_current_quater_table ⇒ Object
4 5 6 |
# File 'lib/pg_partitioner/separation_type/quater.rb', line 4 def create_current_quater_table create_quater_table(Date.today) end |
#create_next_quater_table ⇒ Object
8 9 10 |
# File 'lib/pg_partitioner/separation_type/quater.rb', line 8 def create_next_quater_table create_quater_table(Date.today.next_quarter) end |
#create_partitioning_by_quater_triggers ⇒ Object
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 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/pg_partitioner/separation_type/quater.rb', line 32 def create_partitioning_by_quater_triggers sql = "CREATE OR REPLACE FUNCTION #{table_name}_insert_trigger() RETURNS trigger AS $$ DECLARE curY varchar(4); curQ varchar(1); tbl varchar(121); BEGIN select cast(DATE_PART('year', new.#{parting_column}) as varchar) into curY; select lpad(cast(DATE_PART('quarter', new.#{parting_column}) as varchar), 2, '0') into curQ; tbl := '#{table_name}_y' || curY || 'q' || curQ; EXECUTE format('INSERT into %I values ($1.*);', tbl) USING NEW; return NEW; END; $$ LANGUAGE plpgsql; CREATE TRIGGER #{table_name}_insert BEFORE INSERT ON #{table_name} FOR EACH ROW EXECUTE PROCEDURE #{table_name}_insert_trigger(); -- Trigger function to delete from the master table after the insert CREATE OR REPLACE FUNCTION #{table_name}_delete_trigger() RETURNS trigger AS $$ DECLARE r #{table_name}%rowtype; BEGIN DELETE FROM ONLY #{table_name} where id = new.id returning * into r; RETURN r; end; $$ LANGUAGE plpgsql; CREATE TRIGGER #{table_name}_after_insert AFTER INSERT ON #{table_name} FOR EACH ROW EXECUTE PROCEDURE #{table_name}_delete_trigger();" execute_sql(sql) end |
#create_quater_table(date = Date.today) ⇒ Object
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/pg_partitioner/separation_type/quater.rb', line 12 def create_quater_table(date = Date.today) date_start = date.at_beginning_of_quarter date_end = date.end_of_quarter.next_month.at_beginning_of_month partition_table_name = name_of_partition_table(date, type: :quater) return 'Already exists' if connection.table_exists? partition_table_name sql = "CREATE TABLE IF NOT EXISTS #{partition_table_name} ( CHECK ( #{parting_column} >= DATE('#{date_start}') AND #{parting_column} < DATE('#{date_end}') ) ) INHERITS (#{table_name});" execute_sql(sql) sql = "ALTER TABLE #{partition_table_name} ADD PRIMARY KEY (id);" execute_sql(sql) disable_autovacuum(partition_table_name) create_partition_indexes(partition_table_name) create_partition_named_indexes(partition_table_name) create_partition_unique_indexes(partition_table_name) create_partition_named_unique_indexes(partition_table_name) end |