Received: from maia.hub.org (maia-3.hub.org [200.46.204.243]) by mail.postgresql.org (Postfix) with ESMTP id DC59B1337B79 for ; Thu, 27 Jan 2011 13:56:52 -0400 (AST) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.243]) (amavisd-maia, port 10024) with ESMTP id 50176-07 for ; Thu, 27 Jan 2011 17:56:45 +0000 (UTC) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.6 Received: from mail-ew0-f46.google.com (mail-ew0-f46.google.com [209.85.215.46]) by mail.postgresql.org (Postfix) with ESMTP id EC07A1337B4E for ; Thu, 27 Jan 2011 13:56:44 -0400 (AST) Received: by ewy5 with SMTP id 5so1076628ewy.19 for ; Thu, 27 Jan 2011 09:56:43 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:from:date:message-id:subject:to :content-type; bh=25H4NpGnxLR8+Lwe113eUDTpuG+cp+heqkFZIiarh0M=; b=IdUzvgZmopQF1Q1Js0lf9vb8YwDZrz99TOShmohIE/pNR84M/CFEY2pTigQtI3Rnur A32e46WgG8OVy1/dXkRO2lceqQziicgebU+q7l4lAs6bgWUxG2oPvXoYVEQ/tt6ZFn3F x9s4AVOqWIbWjhK9qVXadYKIjJwhugfzC8CG4= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:from:date:message-id:subject:to:content-type; b=GOhrTFK89yq7K+HNEadaNesat9gY+/uiaOfHPQiA4pYKmYJFnnNcRlB6v08VlQH5SK hqqw+LmpMmWG+hLCrvx57Deb0H479jDNzMc5LDCnkhvjLHCmwr633raYHBAecfHAHunS wcWsblO1UusVnskNWNUkkCWZkFuWD8qAWVcJE= Received: by 10.213.21.206 with SMTP id k14mr3429846ebb.42.1296151003661; Thu, 27 Jan 2011 09:56:43 -0800 (PST) MIME-Version: 1.0 Received: by 10.213.14.82 with HTTP; Thu, 27 Jan 2011 09:50:31 -0800 (PST) From: Amitabh Kant Date: Thu, 27 Jan 2011 23:20:31 +0530 Message-ID: Subject: Automating PostgreSql table partition using triggers To: pgsql-sql@postgresql.org Content-Type: multipart/alternative; boundary=0015174feb5e260583049ad7acb6 X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-2.588 tagged_above=-5 required=5 tests=BAYES_00=-1.9, FREEMAIL_FROM=0.001, HTML_MESSAGE=0.001, RCVD_IN_DNSWL_LOW=-0.7, T_TO_NO_BRKTS_FREEMAIL=0.01 X-Spam-Level: X-Archive-Number: 201101/108 X-Sequence-Number: 35055 --0015174feb5e260583049ad7acb6 Content-Type: text/plain; charset=ISO-8859-1 Hi I am trying to write a function which is being called from a trigger used for partitioning a large table. The partitioning is to happen based on an integer field (testing_id). A simplified structure of what I am trying to do is written below. Create Table tbltesting( testing_id int not null, testing_name character varying(255)); Create table tbltesting1(check(testing_id = 1)) inherits(tbltesting); Create table tbltesting2(check(testing_id = 2)) inherits(tbltesting); CREATE OR REPLACE FUNCTION partition_insert_trigger() RETURNS TRIGGER AS $$ DECLARE id integer ; BEGIN id := NEW.testing_id; INSERT INTO tbltesting'||id||' VALUES (NEW.*); //Problem line, not sure what syntax to use here RETURN NULL; END; $$ LANGUAGE plpgsql; CREATE TRIGGER partition_trigger BEFORE INSERT ON tbltesting FOR EACH ROW EXECUTE PROCEDURE partition_insert_trigger(); Creating tables or child tables is not a problem and the trigger works fine if the function has static definitions. What I am trying to achieve is use the new testing_id to create a table name for use in the insert statement. If I am able to use the variable in the table name, I would not have to re-declare the function each time with modified conditions for each separate testing_id. With regards Amitabh --0015174feb5e260583049ad7acb6 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Hi

I am trying to write a function which is being called from a trig= ger used for partitioning a large table. The partitioning is to happen base= d on an integer field (testing_id). A simplified structure of what I am try= ing to do is written below.

Create Table tbltesting(
=A0testing_id int not null,
=A0testing_n= ame character varying(255));

Create table tbltesting1(check(testing_= id =3D 1)) inherits(tbltesting);
Create table tbltesting2(check(testing_= id =3D 2)) inherits(tbltesting);

CREATE OR REPLACE FUNCTION partition_insert_trigger()
RETURNS TRIGGE= R AS $$
DECLARE id integer ;
BEGIN
=A0=A0=A0 id :=3D NEW.testing_i= d;
=A0=A0=A0
=A0=A0=A0 INSERT INTO tbltesting'||id||' VALUE= S (NEW.*);=A0=A0 //Problem line, not sure what syntax to use here

=A0=A0=A0 RETURN NULL;
END;
$$
LANGUAGE plpgsql;


CR= EATE TRIGGER partition_trigger
=A0=A0=A0 BEFORE INSERT ON tbltesting
= =A0=A0=A0 FOR EACH ROW EXECUTE PROCEDURE partition_insert_trigger();
Creating tables or child tables is not a problem and the trigger works fin= e if the function has static definitions. What I am trying to achieve is us= e the new testing_id to create a table name for use in the insert statement= . If I am able to use the variable in the table name, I would not have to r= e-declare the function each time with modified conditions for each separate= testing_id.


With regards

Amitabh

--0015174feb5e260583049ad7acb6--