DDL Partitionion Inheritance -- improved trigger function - Mailing list pgsql-docs

From Kirk Parker
Subject DDL Partitionion Inheritance -- improved trigger function
Date
Msg-id CANwZ8rkXSFCBOXW8mZ37vYo_MUe-Q35AOTX8uEjEZ2sRwqRohw@mail.gmail.com
Whole thread Raw
Responses Re: DDL Partitionion Inheritance -- improved trigger function
List pgsql-docs
I'm a big fan of maintenance-free functions.  What would you think about adding the following as an alternative trigger function, or as a replacement for the current function, to 
https://www.postgresql.org/docs/current/ddl-partitioning.html#DDL-PARTITIONING-INHERITANCE-EXAMPLE , item #5?  

CREATE OR REPLACE FUNCTION measurement_insert_trigger()
RETURNS TRIGGER AS $$
BEGIN
    EXECUTE format('INSERT INTO measurement_%s VALUES (NEW.*)', to_char( NEW.logdate, 'YYYYMM'));
    RETURN NULL;
END;
$$
LANGUAGE plpgsql;

For the modest overhead of an extra call to to_char() and using EXECUTE rather than a literal INSERT, you get a trigger function that works forever. Given that the example anticipates one insert per city/day, it doesn't expect an extremely high rate of inserts where every microsecond counts.

And yes, bad things happen if the partition table does not exist, but that's true of the other trigger functions shown here, too.


pgsql-docs by date:

Previous
From: Dejan Spasic
Date:
Subject: Re: Confusion in section 8.7.3. Type Safety
Next
From: David Rowley
Date:
Subject: Re: DDL Partitionion Inheritance -- improved trigger function