PostgreSQL Create a Delete partition table

Source: Internet
Author: User
Tags postgresql

Often add partitioned tables, and clean up partitioned tables, very simple 2 functions--Add partition table CREATE OR REPLACE function create_table (table_name character Varying,table_num integer ) RETURNS void LANGUAGE Plpgsql as $function $ declare

V_date char (8); V_tablename varchar (64);

Begin for I in 0..table_num loop

V_date: =to_char (current_date + i, ' YYYYMMDD '); V_tablename: = table_name | | ' _'|| V_date;

Execute ' CREATE TABLE ' | | V_tablename | | ' (LIKE ' | | table_name| | including all) inherits (' | | table_name| | ') '; Execute ' Grant Select on ' | | V_tablename | | ' To Dwetl ';

End Loop; End $function $;

--Delete partition table CREATE OR REPLACE FUNCTION drop_table (table_name character varying,table_num integer) RETURNS void LANGUAGE L as $function $ declare

V_date char (8); V_tablename varchar (64);

Begin for I in 0..table_num loop

V_date: =to_char (current_date + i, ' YYYYMMDD '); V_tablename: = table_name | | ' _'|| V_date;

Execute ' drop table ' | | V_tablename;

End Loop; End $function $;

--Executive Function Select Create_table (' table_name ', table_num); Select Drop_table (' table_name ', table_num); --Delete Functions drop function create_table (table_name character varying,table_num integer); #必须写上输入参数 Drop FUNCTION drop_table (table_name character varying,table_num integer);

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.