I'm trying to update an entry in my database (postgresql). I'm having a problem with updating the query:
INSERT INTO prices (price, product_id)
SELECT 
product_price, commn_id
FROM products_temp as prod
WHERE NOT EXISTS 
(SELECT *
   FROM   prices od
   WHERE  prod.commn_id = od.product_id)
OR prod.product_price != 
( SELECT price
  FROM prices as p
  WHERE p.product_id = prod.commn_id
  order by p.end
  desc LIMIT 1 )
Query works fine when i delete:
OR prod.product_price != 
( SELECT price
  FROM prices as p
  WHERE p.product_id = prod.commn_id
  order by p.end
  desc LIMIT 1 )
So it seems to me that it's looping through this operation. My question is, how can I fix it?
 
     
    