Apart from concurrency issues, it is possible that you
have sequence generation problems. Depending on how
you inserted the original rows into the 'purchases' table, it is possible
that the nextval number has not kept-up and is lagging behind.
You need to ensure that 'purchases_purchase_id_seq' is pointing
to the correct next value! That is, if it is current nextval
is number 100, but you already have 110 rows on the table (without gaps),
it is no wonder you will receive primary key violations for the
next 10 inserts but will work fine afterwards. ( Assuming, of course,
you are the only one receiving values from the sequence.)
Thanks
Ioannis Tambouras