Skip to content

Instantly share code, notes, and snippets.

@maxjustus
Created March 30, 2012 23:02
Show Gist options
  • Save maxjustus/2257305 to your computer and use it in GitHub Desktop.
Save maxjustus/2257305 to your computer and use it in GitHub Desktop.
dynamic insertion into partition based on value in insert for Postgres
create or replace function insert_herp_to_derp()
returns trigger as $$
begin
execute 'INSERT INTO ' || quote_ident('derp_' || NEW.derp_id || '_herps') || ' SELECT ($1).*' using NEW;
return null;
end;
$$ LANGUAGE plpgsql;
CREATE TRIGGER insert_herps_trigger BEFORE INSERT ON herps FOR EACH ROW EXECUTE PROCEDURE insert_herp_to_derp();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment