Materializza l'array JSON in un array PostgreSQL con una funzione di supporto, poi usa quella funzione in un vincolo CHECK.
create function data_product_ids(JSON) returns integer[] immutable as $$ select array_agg((a->>'product_id')::integer) from json_array_elements($1->'products') as a $$ language sql; alter table orders add check (1 <= ALL(data_product_ids(data)));