ALTER TABLE stock_logs
ADD COLUMN stock NUMERIC(15, 3) NOT NULL DEFAULT 0;

WITH calc AS (
  SELECT
    id,
    SUM(COALESCE(increase, 0) - COALESCE(decrease, 0))
      OVER (
        PARTITION BY product_warehouse_id
        ORDER BY id
        ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
      ) AS running_stock
  FROM stock_logs
)
UPDATE stock_logs t
SET stock = c.running_stock
FROM calc c
WHERE t.id = c.id;

