Month based partitioning

From PostgreSQL wiki

Revision as of 01:46, 5 July 2010 by Ttt (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

As of now PostgreSQL 8.4, table partitioning is not provided out of the box. This an example of how to get partitioned tables.

The purpose is to have monthly table partitioning using PostgreSQL.

First we need a master table, then we can create partitions inheriting from the master table. A function is used to create the partitions for a given time range.

A timestamp will be used as a partitioning variable, and in this case it is assumed that it will never be updated.

A trigger must be created to intercept the insertions on the master table and redirect the data rows to the correct partition table. A trigger function is used to perform this.

In this specific example, it is assumed that the partitioning variable is a creation timestamp which will never change. This only leads to the need of redirecting insert operations from the master table to the appropriate partition. If the partitioning variable may be updated, then extra trigger and trigger functions would be needed for update operations. Updating the partitioning variable may imply that the updated row must be moved to another partition.

Performance Snippets

Month based partitioning

Works with PostgreSQL

8.4

Written in

pgplsql

Depends on

Nothing


Contents

Create the master table

-- drop table test cascade;
CREATE TABLE test (
	id 	bigserial PRIMARY KEY,
	time	timestamp
);

Load language module

CREATE LANGUAGE plpgsql;

Create a function to create the partitions and indexes

-- drop function test_partition_creation();
CREATE OR REPLACE FUNCTION test_partition_creation( DATE, DATE )
returns void AS $$
DECLARE
	create_query text;
	index_query text;
BEGIN
	FOR create_query, index_query IN SELECT
			'create table test_'
			|| TO_CHAR( d, 'YYYY_MM' )
			|| ' ( check( time >= date '''
			|| TO_CHAR( d, 'YYYY-MM-DD' )
			|| ''' and time < date '''
			|| TO_CHAR( d + INTERVAL '1 month', 'YYYY-MM-DD' )
			|| ''' ) ) inherits ( test );',
			'create index test_'
			|| TO_CHAR( d, 'YYYY_MM' )
			|| '_time on test_' 
			|| TO_CHAR( d, 'YYYY_MM' )
			|| ' ( time );'
		FROM generate_series( $1, $2, '1 month' ) AS d
	LOOP
		EXECUTE create_query;
		EXECUTE index_query;
	END LOOP;
END;
$$
language plpgsql;

Partition creation for a given time period

SELECT test_partition_creation( '2010-01-01', '2012-01-01' ) ;

Trigger function creation

-- drop function test_partition_function();
CREATE OR REPLACE FUNCTION test_partition_function()
returns TRIGGER AS $$
begin
	execute 'insert into test_'
		|| to_char( NEW.time, 'YYYY_MM' )
		|| ' values ( $1, $2 )' USING NEW.id, NEW.time ;
	RETURN NULL;
end;
$$
LANGUAGE plpgsql;

Trigger activation

-- drop trigger test_partition_trigger;
CREATE TRIGGER test_partition_trigger
	before INSERT
	ON test
	FOR each row
	execute procedure test_partition_function() ;
Personal tools