My table is defined as: (it is a simple metric-config tracking one)
CREATE TABLE IF NOT EXISTS strategy_registry (
name VARCHAR NOT NULL,
symbol VARCHAR NOT NULL,
predictor_id VARCHAR NOT NULL,
params jsonb NOT NULL,
metric FLOAT NOT NULL,
PRIMARY KEY(name, symbol, predictor_id)
);
And on insert conflict, I want to keep the maximum metric and the params column, of the version that got the maximum metric value. I am using TimescaleDB (postgres 12)
INSERT INTO strategy_registry (name, symbol, predictor_id, params, metric)
VALUES ({{ name }}, {{ symbol }}, {{ predictor_id }}, {{ params }}, {{ metric }})
ON CONFLICT (name, symbol, predictor_id)
DO UPDATE SET
(metric, params) = max(existing, excluded), params of greatest `metric` column value;