POSTGRESQL Partition + Hibernate Insert

Source: Internet
Author: User
Tags postgresql volatile

Unlike Oracle, PostgreSQL requires manual control of partition rule triggers.

Step one: Create a partition

CREATE table table_partition_1 (CHECK partition_column criteria) inhenrits (table)

Step Two: Create a PK and index for the partition table, use btree here
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);

Step three: Create a trigger manually
CREATE OR REPLACE FUNCTION before_insert_table ()  RETURNS trigger as$body$ DECLARE  beginif criteria  Thenexecute ' INSERT into appropriate table ... '  SELECT ($  ). * '  USING new;end if;return null; END; $BODY $  LANGUAGE plpgsql VOLATILE cost  100; ALTER FUNCTION before_insert_table ()  OWNER to DB;

Rule is more appropriate for bulk operations, but is expensive for individual operations.
CREATE RULE table_partition_1 _insert as (criteria ...) Do INSTEAD     INSERT into table_partition_1 VALUES (new.*)

Step four: Trigger mode when hibernate inserts data into a partition, the resulting result count is 0, which causes the failure of the judgment to be rolled back.The workaround is to use rule, or declare the partition to be inserted without a result check.
@SQLInsert (sql = "INSERT into" + "table (column,...)" + "VALUES (?,...)", check = Resultcheckstyle.none)

in a Java project, consider that partition creation is automatically created by job, and can be created by using the function.
CREATE OR REPLACE FUNCTION "public". " Function "()  RETURNS void as $BODY $  DECLARE    _tablename text;   Quarter Integer;record1 record;  Beginselect function (now ()) as quarter into  record1;  Quarter: = record1.quarter;_tablename: = ' partition_name '; PERFORM 1FROM   pg_catalog.pg_class cwhere  c.relname = _tablename;if FOUND <> TRUE thenexecute ' 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| | ' _indexon ' | |  _tablename | | ' USING btree (column) ';   END IF; end$body$  LANGUAGE ' plpgsql ' VOLATILE cost 100; ALTER FUNCTION "public". " Function "() OWNER to" DB ";



Resources:Entity-hibspec-customsql.html
postgresql-table-partitioning-hibernate.html


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.