I need to add a required field for newly added rows. However, it is undesirable to set the default value for old rows due to the large size of the table. I need to provide an automated script that will do this. I tried this, but it does not work:
do $$ declare max_id int8; begin select max(id) into max_id from transactions; alter table transactions add constraint check_process_is_assigned check (id <= max_id or process_id is not null); end $$;
idis always less than or equal to themax_id(assuming it is notnull). What is the point of the logic?CHECKconstraint that enforcesprocess_id IS NOT NULLonly for future rows (bigger IDs than the current maximum).