I have the following table:
create table foobar
(
account_id bigint not null,
timestamp timestamp not null,
balance_one numeric not null,
balance_two numeric not null,
primary key (timestamp, account_id),
);
Currently, I'm inserting thousands of records into this table at once using the following statement:
insert into foobar(account_id, timestamp, balance_one, balance_two)
VALUES (?, ?, ?, ?), (?, ?, ?, ?), ETC...
on conflict DO NOTHING;
I want to add a constraint to the insert statement, stopping new records being inserted if new.balance_one == old.balance_one AND new.balance_two == old.balance_two
where old = the most recent (timestamp) record where account_id = new.account_id.
How do I do this? Insert performance is also a concern.