In your case, you need to set the default value for c2 to the current value of the sequence associated with c1, ie:
ALTER TABLE t1 ALTER COLUMN c2 SET DEFAULT CURRVAL(PG_GET_SERIAL_SEQUENCE('t1', 'c1'));
Of course, this will not work if you specify some explicit value for c1. If there are such cases, then you should make a BEFORE trigger to be sure that c2 will always be the same as c1 in the inset.
CREATE FUNCTION sync_c2() RETURNS trigger AS $$ BEGIN IF NEW.c2 IS NULL THEN NEW.c2 := NEW.c1; END IF; RETURN NEW; END; $$ LANGUAGE plpgsql; CREATE TRIGGER sync_c2 BEFORE INSERT ON t1 FOR EACH ROW EXECUTE PROCEDURE sync_c2();
source share