| From: | Grzegorz Jaśkiewicz <gryzman(at)gmail(dot)com> | 
|---|---|
| To: | Merlin Moncure <mmoncure(at)gmail(dot)com> | 
| Cc: | Dimitri Fontaine <dfontaine(at)hi-media(dot)com>, pgsql-general(at)postgresql(dot)org | 
| Subject: | Re: dynamic insert in plpgsql | 
| Date: | 2010-01-10 01:04:49 | 
| Message-ID: | 2f4958ff1001091704ra796d0ch92d415180a3a4fbb@mail.gmail.com | 
| Views: | Whole Thread | Raw Message | Download mbox | Resend email | 
| Thread: | |
| Lists: | pgsql-general | 
This is what I hacked quickly last night, what you guys think?
CREATE OR REPLACE FUNCTION something.ziew_partition_insert() RETURNS TRIGGER AS
$_$
DECLARE
  partition_table_name varchar;
  old_partition_table_name varchar;
BEGIN
  SELECT 'something_partitions.ziew_'||to_char(NEW.logtime, 'IYYY_MM')
INTO partition_table_name;
BEGIN
   EXECUTE 'INSERT INTO '||partition_table_name||'  (SELECT
(something.ziew '||quote_literal(NEW)||').*)';
    EXCEPTION
      WHEN undefined_table THEN
        BEGIN
          SET client_min_messages = error;
          EXECUTE 'CREATE TABLE '||partition_table_name||'() INHERITS
(something.ziew)';
          EXECUTE 'ALTER TABLE  '||partition_table_name||' ADD PRIMARY
KEY (id)';
          EXECUTE 'CREATE INDEX something_time'|| to_char(NEW.logtime,
'IYYY_MM')||' ON '||partition_table_name||'(logtime)';
          SELECT 'something_partitions.ziew_'||to_char(NEW.logtime-'2
months'::interval, 'IYYY_MM') INTO old_partition_table_name;
          -- don't care if it fails
          BEGIN
            EXECUTE 'DROP TABLE '||old_partition_table_name;
            EXCEPTION
              WHEN others THEN
                --- in place for NOP
                old_partition_table_name := '';
          END;
          EXECUTE 'INSERT INTO '||partition_table_name||'  (SELECT
(something.ziew '||quote_literal(NEW)||').*)';
        EXCEPTION
          WHEN others THEN
            RAISE EXCEPTION 'somethings wrong %',SQLERRM;
            RETURN NULL;
        END;
    END;
  RETURN NULL;
END;
$_$ LANGUAGE 'plpgsql';
| From | Date | Subject | |
|---|---|---|---|
| Next Message | Ivan Sergio Borgonovo | 2010-01-10 01:30:15 | aggregate over tables in different schema | 
| Previous Message | Greg Smith | 2010-01-10 00:50:10 | Re: Gentoo, 8,2 ---> 8.4, and /var/run/postgresql in mode 770 |