execute 'create table tbl_user'||i||' (like tbl_user including constraints including defaults including indexes) inherits (tbl_user);';
execute 'alter table tbl_user'||i||' add constraint ck_user'||i||' check (mod(id,16)='||i||');';
CREATE OR REPLACE FUNCTION tbl_userinsert_trigger()
RETURNS TRIGGER AS $$
BEGIN
IF ( mod(NEW.id,16) = 0 ) THEN
INSERT INTO tbl_user0 VALUES (NEW.*);
ELSIF ( mod(NEW.id,16) = 1 ) THEN
INSERT INTO tbl_user1 VALUES (NEW.*);
ELSIF ( mod(NEW.id,16) = 2 ) THEN
INSERT INTO tbl_user2 VALUES (NEW.*);
ELSIF ( mod(NEW.id,16) = 3 ) THEN
INSERT INTO tbl_user3 VALUES (NEW.*);
ELSIF ( mod(NEW.id,16) = 4 ) THEN
INSERT INTO tbl_user4 VALUES (NEW.*);
ELSIF ( mod(NEW.id,16) = 5 ) THEN
INSERT INTO tbl_user5 VALUES (NEW.*);
ELSIF ( mod(NEW.id,16) = 6 ) THEN
INSERT INTO tbl_user6 VALUES (NEW.*);
ELSIF ( mod(NEW.id,16) = 7 ) THEN
INSERT INTO tbl_user7 VALUES (NEW.*);
ELSIF ( mod(NEW.id,16) = 8 ) THEN
INSERT INTO tbl_user8 VALUES (NEW.*);
ELSIF ( mod(NEW.id,16) = 9 ) THEN
INSERT INTO tbl_user9 VALUES (NEW.*);
ELSIF ( mod(NEW.id,16) = 10 ) THEN
INSERT INTO tbl_user10 VALUES (NEW.*);
ELSIF ( mod(NEW.id,16) = 11 ) THEN
INSERT INTO tbl_user11 VALUES (NEW.*);
ELSIF ( mod(NEW.id,16) = 12 ) THEN
INSERT INTO tbl_user12 VALUES (NEW.*);
ELSIF ( mod(NEW.id,16) = 13 ) THEN
INSERT INTO tbl_user13 VALUES (NEW.*);
ELSIF ( mod(NEW.id,16) = 14 ) THEN
INSERT INTO tbl_user14 VALUES (NEW.*);
ELSIF ( mod(NEW.id,16) = 15 ) THEN
INSERT INTO tbl_user15 VALUES (NEW.*);
ELSE
RAISE EXCEPTION 'ID out of range. Please fix the tbl_userinsert_trigger() function!';
END IF;
RETURN NULL;
END;
$$
LANGUAGE plpgsql;
CREATE OR REPLACE FUNCTION tbl_userdelete_trigger()
RETURNS TRIGGER AS $$
BEGIN
IF ( mod(OLD.id,16) = 0 ) THEN
DELETE FROM tbl_user0 where id=OLD.id;
ELSIF ( mod(OLD.id,16) = 1 ) THEN
DELETE FROM tbl_user1 where id=OLD.id;
ELSIF ( mod(OLD.id,16) = 2 ) THEN
DELETE FROM tbl_user2 where id=OLD.id;
ELSIF ( mod(OLD.id,16) = 3 ) THEN
DELETE FROM tbl_user3 where id=OLD.id;
ELSIF ( mod(OLD.id,16) = 4 ) THEN
DELETE FROM tbl_user4 where id=OLD.id;
ELSIF ( mod(OLD.id,16) = 5 ) THEN
DELETE FROM tbl_user5 where id=OLD.id;
ELSIF ( mod(OLD.id,16) = 6 ) THEN
DELETE FROM tbl_user6 where id=OLD.id;
ELSIF ( mod(OLD.id,16) = 7 ) THEN
DELETE FROM tbl_user7 where id=OLD.id;
ELSIF ( mod(OLD.id,16) = 8 ) THEN
DELETE FROM tbl_user8 where id=OLD.id;
ELSIF ( mod(OLD.id,16) = 9 ) THEN
DELETE FROM tbl_user9 where id=OLD.id;
ELSIF ( mod(OLD.id,16) = 10 ) THEN
DELETE FROM tbl_user10 where id=OLD.id;
ELSIF ( mod(OLD.id,16) = 11 ) THEN
DELETE FROM tbl_user11 where id=OLD.id;
ELSIF ( mod(OLD.id,16) = 12 ) THEN
DELETE FROM tbl_user12 where id=OLD.id;
ELSIF ( mod(OLD.id,16) = 13 ) THEN
DELETE FROM tbl_user13 where id=OLD.id;
ELSIF ( mod(OLD.id,16) = 14 ) THEN
DELETE FROM tbl_user14 where id=OLD.id;
ELSIF ( mod(OLD.id,16) = 15 ) THEN
DELETE FROM tbl_user15 where id=OLD.id;
ELSE
RAISE EXCEPTION 'ID out of range. Please fix the tbl_userdelete_trigger() function!';
END IF;
RETURN NULL;
END;
$$
LANGUAGE plpgsql;