This setup works fine over here (version 8.4 and 9.0):
CREATE TABLE customer
(
id bigserial NOT NULL,
name text,
datecreated timestamp with time zone DEFAULT now(),
CONSTRAINT customer_pkey PRIMARY KEY (id) USING INDEX TABLESPACE pg_default
);
CREATE OR REPLACE FUNCTION test_trigger()
RETURNS trigger AS
$BODY$
declare
query text;
begin
query := 'INSERT INTO customer_' || substring(NEW.name,1,1) || ' VALUES(' || NEW.id || ','''|| NEW.name||''')';
EXECUTE query;
RETURN NULL;
EXCEPTION
WHEN undefined_table THEN
query := 'CREATE TABLE "customer_' || substring(NEW.name,1,1) ||
'"( CONSTRAINT "customer_' || substring(NEW.name,1,1) ||'_name_check" CHECK (lower(substring(name, 1, 1)) = ''' || substring(NEW.name,1,1) || '''::text)
) INHERITS (customer); CREATE INDEX "i_customer_' || substring(NEW.name,1,1) ||'_name" ON customer_' || substring(NEW.name,1,1) ||' USING btree(name);';
EXECUTE query;
query := 'INSERT INTO customer_' || substring(NEW.name,1,1) || ' VALUES(' || NEW.id || ','''|| NEW.name||''')';
EXECUTE query;
query := 'ANALYZE "customer_' || substring(NEW.name,1,1) || '"';
EXECUTE query;
RETURN NULL;
end;
$BODY$
LANGUAGE plpgsql VOLATILE STRICT
COST 100;
CREATE TRIGGER t_customer
BEFORE INSERT OR UPDATE OR DELETE
ON customer
FOR EACH ROW
EXECUTE PROCEDURE test_trigger();
CREATE TABLE customer
(
id bigserial NOT NULL,
name text,
datecreated timestamp with time zone DEFAULT now(),
CONSTRAINT customer_pkey PRIMARY KEY (id) USING INDEX TABLESPACE pg_default
);
CREATE OR REPLACE FUNCTION test_trigger()
RETURNS trigger AS
$BODY$
declare
query text;
begin
query := 'INSERT INTO customer_' || substring(NEW.name,1,1) || ' VALUES(' || NEW.id || ','''|| NEW.name||''')';
EXECUTE query;
RETURN NULL;
EXCEPTION
WHEN undefined_table THEN
query := 'CREATE TABLE "customer_' || substring(NEW.name,1,1) ||
'"( CONSTRAINT "customer_' || substring(NEW.name,1,1) ||'_name_check" CHECK (lower(substring(name, 1, 1)) = ''' || substring(NEW.name,1,1) || '''::text)
) INHERITS (customer); CREATE INDEX "i_customer_' || substring(NEW.name,1,1) ||'_name" ON customer_' || substring(NEW.name,1,1) ||' USING btree(name);';
EXECUTE query;
query := 'INSERT INTO customer_' || substring(NEW.name,1,1) || ' VALUES(' || NEW.id || ','''|| NEW.name||''')';
EXECUTE query;
query := 'ANALYZE "customer_' || substring(NEW.name,1,1) || '"';
EXECUTE query;
RETURN NULL;
end;
$BODY$
LANGUAGE plpgsql VOLATILE STRICT
COST 100;
CREATE TRIGGER t_customer
BEFORE INSERT OR UPDATE OR DELETE
ON customer
FOR EACH ROW
EXECUTE PROCEDURE test_trigger();
INSERT INTO customer(name) VALUES ('john'), ('peter');