I managed to get this to work by dynamically compiling a function that takes a new line as a parameter:
EXECUTE 'create or replace function partition_insert(r ' || TG_TABLE_NAME || ') RETURNS void AS $FUNC$' || 'BEGIN ' || 'insert into ' || TG_TABLE_NAME || ' SELECT r.*; ' || 'END $FUNC$ LANGUAGE plpgsql VOLATILE'; PERFORM partition_insert(NEW);
Since Postgres functions are polymorphic, this will create a different function for each table that uses this trigger.
Although this is an ugly shred, it seems to do the job.
Although it looks like I can define every polymorphic variation in the front when I build the system, due to caching, I have to recompile this function whenever I create or delete a child table so that the function uses the latest version of RULE. p>
EDIT: Extra wrinkles
It worked a little with this technique: if this EXECUTE / PERFORM action was canceled on the first try due to another error (for example, in my case, the CHECK restriction failed), then the function containing this code seems to cache the rollback reference for partition_insert () created using EXECUTE and subsequent calls fail because there is no cached object.
I solved this by first creating stub versions of the function for each required table type parameter when I define the database.
Adrian Pronk Jan 04 '10 at 5:52 2010-01-04 05:52
source share