标签:postgresql partition hiberna
与Oracle不同,PostgreSQL需要手动控制分区规则触发器。
步骤一:创建分区
CREATE TABLE table_partition_1( CHECK partition_column criteria) INHENRITS (table)
ALTER TABLE ONLY table_partition_1 ADD CONSTRAINT table_partition_1_pkey PRIMARY KEY (key_column);
CREATE INDEX index_table_partition_1 ON table_partition_1 USING btree(column);
CREATE OR REPLACE FUNCTION before_insert_table() RETURNS trigger AS $BODY$ DECLARE BEGIN if criteria then EXECUTE 'insert into appropriate table ...' SELECT ($1).* ' USING NEW; end if; return null; END; $BODY$ LANGUAGE plpgsql VOLATILE COST 100; ALTER FUNCTION before_insert_table() OWNER TO db;
CREATE RULE table_partition_1 _insert AS (criteria ...) DO INSTEAD INSERT INTO table_partition_1 VALUES (NEW.*)
@SQLInsert(sql = "INSERT INTO " + "table(column,...)" + " VALUES(?,...)", check = ResultCheckStyle.NONE)
CREATE OR REPLACE FUNCTION "public"."function"() RETURNS void AS $BODY$ DECLARE _tablename text ; quarter integer; record1 record; BEGIN select function(now()) as quarter into record1; quarter := record1.quarter; _tablename := 'partition_name'; PERFORM 1 FROM pg_catalog.pg_class c WHERE c.relname = _tablename; IF FOUND <> TRUE THEN EXECUTE 'CREATE TABLE ' || _tablename || ' ( CHECK ( criteria) ) INHERITS (table)'; EXECUTE 'ALTER TABLE ' || _tablename || ' OWNER TO db '; EXECUTE ' alter table ' || _tablename || ' add CONSTRAINT ' || _tablename||'_pkey PRIMARY key (column) ' ; EXECUTE ' CREATE INDEX ' || _tablename|| '_index ON '|| _tablename ||' USING btree (column)'; END IF; END $BODY$ LANGUAGE 'plpgsql' VOLATILE COST 100; ALTER FUNCTION "public"."function"() OWNER TO "db";
PostgreSql Partition + Hibernate Insert,布布扣,bubuko.com
PostgreSql Partition + Hibernate Insert
标签:postgresql partition hiberna
原文地址:http://blog.csdn.net/paristao/article/details/37595015