Create partitions in postgresql

Source: Internet
Author: User
Create a partition in postgresql 1. Create a primary table createtablemeasurement (city_idintnotNULL, logdatedatenotNULL, peaktempint, unitsalesint); 2 create a partition table createtablemeasurement_201303 (CHECK (logdateDATE2013-03-01andlogdateDATE2013-04-01) INH

Create partitions in postgresql 1. create table measurement (city_id int not NULL, logdate date not NULL, peaktemp int, unitsales int ); 2 create partition table create table measurement_201303 (CHECK (logdate = DATE2013-03-01 and logdate DATE2013-04-01) INH

Create partitions in postgresql

1. Create a master table

Create table measurement (

City_id int not NULL,

Logdate date not NULL,

Peaktemp int,

Unitsales int

);

2. Create a partition table

Create table measurement_201303 (

CHECK (logdate> = DATE '2017-03-01 'and logdate <DATE '2017-04-01 ')

) INHERITS (measurement );

Create table measurement_201304 (

CHECK (logdate> = DATE '2014-04-01 'and logdate <DATE '2014-05-01 ')

) INHERITS (measurement );

Create table measurement_201305 (

CHECK (logdate> = DATE '2014-05-01 'and logdate <DATE '2014-06-01 ')

) INHERITS (measurement );

3. You can create an index on the corresponding partition table.

Create index measurement_201303_logdate on measurement_201303 (logdate );

Create index measurement_201304_logdate on measurement_201304 (logdate );

Create index measurement_201305_logdate on measurement_201305 (logdate );

4. Create a stored procedure

Create or replace function measurement_insert_trigger ()

Returns trigger as $

Begin

If (NEW. logdate> = date '2017-03-01 'and NEW. logdate

Insert into measurement_201303 VALUES (NEW .*);

ELSEIF (NEW. logdate> = date '2017-04-01 'and NEW. logdate

Insert into measurement_201304 VALUES (NEW .*);

ELSEIF (NEW. logdate> = date '2017-05-01 'and NEW. logdate

Insert into measurement_201305 VALUES (NEW .*);

ELSE

Raise EXCEPTION 'date out of range. Fix the measurment_insert_trigger () function! ';

End if;

RETURN null;

End;

$

LANGUAGE plpgsql;

5. Create a trigger

Create trigger insert_measurement_trigger

Before insert on measurement

For each row execute procedure measurement_insert_trigger ();

6. insert data

Insert into measurement (city_id, logdate, peaktemp, unitsales) VALUES (1, '2017-03-02 ', 2013 );

Insert into measurement (city_id, logdate, peaktemp, unitsales) VALUES (2, '2017-04-02 ', 2013 );

Insert into measurement (city_id, logdate, peaktemp, unitsales) VALUES (3, '2017-05-02 ', 3,3 );

7. query data.

Select * from measurement

Select * from measurement_201303;

Select * from measurement_201304;

Select * from measurement_201305;

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.