Received: from localhost (unknown [200.46.204.183]) by postgresql.org (Postfix) with ESMTP id CEA4D2E0051 for ; Thu, 20 Mar 2008 19:28:24 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.183]) (amavisd-maia, port 10024) with ESMTP id 16453-08 for ; Thu, 20 Mar 2008 19:28:22 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey-1.7.5 Received: from smtp166.iad.emailsrvr.com (smtp166.iad.emailsrvr.com [207.97.245.166]) by postgresql.org (Postfix) with ESMTP id 182B12E003A for ; Thu, 20 Mar 2008 19:28:22 -0300 (ADT) Received: from relay6.relay.iad.emailsrvr.com (localhost [127.0.0.1]) by relay6.relay.iad.emailsrvr.com (SMTP Server) with ESMTP id BCB6072A8DD for ; Thu, 20 Mar 2008 18:28:20 -0400 (EDT) Received: by relay6.relay.iad.emailsrvr.com (Authenticated sender: erik-AT-myemma.com) with ESMTP id 8D87372B484 for ; Thu, 20 Mar 2008 18:28:20 -0400 (EDT) Message-Id: From: Erik Jones To: SQL Postgresql List Content-Type: text/plain; charset=ISO-8859-1; format=flowed; delsp=yes Content-Transfer-Encoding: quoted-printable Mime-Version: 1.0 (Apple Message framework v919.2) Subject: Dynamic sql and variable record types Date: Thu, 20 Mar 2008 17:28:18 -0500 X-Mailer: Apple Mail (2.919.2) X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200803/161 X-Sequence-Number: 30489 Hi, I've been working on a generic date partitioning system and I =20 think I've reached something that can't be done, but I thought I'd =20 post a question to the masses in the hope that I'm missing something. =20= The basic idea of what I'm doing is some userland scripts that will =20 accept a table name argument along with the name of a date/timestamp =20 attribute to partition on and create partitions for that table along =20 with the appropriate trigger and trigger function. The part I'm =20 having trouble with is the trigger function. What I'm done for that is to create a template file that my scripts =20 read in and substitute the table column names wherever necessary, then =20= run the results through the db to create the functions. The problem =20 is that for the function to be generic it needs to be able to work =20 with different record/row types. Here's the template for function =20 (not working, which I'll discuss below): CREATE OR REPLACE FUNCTION %s_ins_func(op text, rec %s) RETURNS boolean AS $$ DECLARE partition varchar; name_parts varchar[]; upper_dim integer; BEGIN FOR partition IN SELECT relname FROM pg_class WHERE relname ~ ('^%s_[0-9]{8}_[0-9]{8}$') LOOP name_parts :=3D string_to_array(partition, '_'); upper_dim :=3D array_upper(name_parts, 1); IF rec.%s >=3D name_parts[upper_dim-1]::timestamp AND rec.%s < =20= name_parts[upper_dim] THEN IF op =3D 'INSERT' THEN EXECUTE 'INSERT INTO %s_' || name_parts[upper_dim-1] =20= || '_' || name_parts[upper_dim] || ' VALUES ' || =20 rec || ';'; -- the problem is here with rec RETURN TRUE; END IF; END IF; END LOOP; RETURN FALSE; END; $$ language plpgsql; The userland scripts substitute the table and column names for the %s =20= escapes where appropriate. What the function actually does is to us =20 the parent table's name to find all of the child partitions which are =20= name like some_table_20080101_20080201, split out the dates from those =20= to determine which table the insert needs to be redirected to. That =20 works fine. The problem is that since I have to dynamically generate =20= the destination table name I have to use EXECUTE for the INSERT =20 statement. But, I can't see how to use a record in query passed to =20 EXECUTE. Am I right in thinking (now) that this can't be done? Erik Jones DBA | Emma=AE erik@myemma.com 800.595.4401 or 615.292.5888 615.292.0777 (fax) Emma helps organizations everywhere communicate & market in style. Visit us online at http://www.myemma.com