\timing on

DROP TABLE prices;

CREATE TABLE prices (
  pid int NOT NULL, -- REFERENCES products,
  validTil timestamp(0) NULL,
  price int NOT NULL,
  UNIQUE (pid, validTil)
);

CREATE UNIQUE INDEX current_prices
ON prices (
  pid
)
WHERE validTil IS NULL;

INSERT INTO prices
SELECT generate_series,
CASE MOD(generate_series,10)
WHEN 0 THEN NULL
ELSE current_timestamp - MOD(generate_series,10) * interval '1' day
END,
123
FROM generate_series(1,1000000);

explain select price from prices where pid = 12344 and validTil is null;
