add batch remove batch split batch comment selection show hidden batches hide batch highlight batch
db<>fiddle
donate feedback about
By using db<>fiddle, you agree to license everything you submit by Creative Commons CC0.
Help with an interesting Postgres question: Why isn't an Index Only Scan used on a partition accessed via the parent table?.
create table t (id int, pid int , name name , dt date) partition by range(dt);

create table t1 partition of t for values from ('2020-01-01') to ('2020-02-01');
alter table t1 add constraint uk1 unique (id,pid);
create table t2 partition of t for values from ('2020-02-01') to ('2020-03-01');
alter table t2 add constraint uk2 unique (id,pid);
create table t4 partition of t for values from ('2020-03-01') to ('2020-04-01');
alter table t4 add constraint uk3 unique (id,pid);
create table t3 partition of t for values from ('2020-04-01') to ('2020-05-01');
alter table t3 add constraint uk4 unique (id,pid);





--SELECT conname FROM pg_constraint WHERE conrelid = m_table1::regclass::oid and contype = 'u' into conn_name;


CREATE or replace FUNCTION insert_trigger()
RETURNS trigger
LANGUAGE 'plpgsql'
COST 100
VOLATILE NOT LEAKPROOF
AS $BODY$
DECLARE
conn_name text;
c_table TEXT;
t_schema text;
c_table1 text;
m_table1 text;
BEGIN
c_table1 := TG_TABLE_NAME;
t_schema := TG_TABLE_SCHEMA;
m_table1 := t_schema||'.'||TG_TABLE_NAME;
SELECT conname FROM pg_constraint WHERE conrelid = TG_TABLE_NAME ::regclass::oid and contype = 'u' into conn_name;
execute 'insert into '|| m_table1 || ' values ' || new.* || ' on conflict on constraint ' || conn_name || ' do nothing -- or somthing';
RETURN null;
end;
$BODY$;


CREATE TRIGGER insert
BEFORE INSERT
ON t4
FOR EACH ROW
WHEN (pg_trigger_depth() < 1)
EXECUTE FUNCTION insert_trigger();
CREATE TRIGGER insert
BEFORE INSERT
ON t3
FOR EACH ROW
WHEN (pg_trigger_depth() < 1)
EXECUTE FUNCTION insert_trigger();
CREATE TRIGGER insert
BEFORE INSERT
ON t2
FOR EACH ROW
WHEN (pg_trigger_depth() < 1)
EXECUTE FUNCTION insert_trigger();
CREATE TRIGGER insert
BEFORE INSERT
ON t1
FOR EACH ROW
WHEN (pg_trigger_depth() < 1)
EXECUTE FUNCTION insert_trigger();
insert into t values (1,2,'raj','2020-01-01');

insert into t values (1,2,'raj','2020-01-01');
ERROR:  column "raj" does not exist
LINE 1: insert into public.t1 values (1,2,raj,2020-01-01) on conflic...
                                          ^
QUERY:  insert into public.t1 values (1,2,raj,2020-01-01) on conflict on constraint uk1 do nothing -- or somthing
CONTEXT:  PL/pgSQL function insert_trigger() line 17 at EXECUTE

select * from t;