我使用PostgreSQL 9.5,我尝试插入使用BULK INSERT每天数百万行:PostgreSQL的日志到另一个表与冲突
INSERT INTO "tours" as cst ("adults","country_id", "price")
VALUES (3, 129, 80), (2, 119,120)
on conflict (adults, country_id) do
update set price = EXCLUDED.price, updated_at = now()
where excluded.price < cst.price
RETURNING ID;
如果价格较低,我会更新一行。我想为未更新的日志价格添加else
条件。事情是这样的:
INSERT INTO "tours" as cst ("adults","country_id", "price")
VALUES (3, 129, 80), (2, 119,120)
on conflict (adults, country_id)
case when excluded.price < cst.price
then
do update set price = EXCLUDED.price, updated_at = now()
else
INSERT INTO "tours_price_logging" (price, created_at)
values (EXCLUDED.price, now()) end;
但我有错误这种情况:
ERROR: syntax error at or near "case"
我想从插入取出条件where excluded.price < cst.price
,并将这个逻辑trigger
,但后来我会更新行的价格与价值从tours_price_logging
和价格可能更多然后当前。
所以,我想使用upsert和批量插入日志记录。
我注意到,有时我需要更新的价格定价,更多的则电流(从另一个查询)。价格已过期,当它过期时,我从'tours_price_logging'获得新价格,而且价格可能会更高。所以,这个触发器并不适用于我所有的情况。 –
或者我可以跳过查询中的触发器执行? –
您可以使用['when(condition)'](https://www.postgresql.org/docs/current/static/sql-createtrigger.html)子句创建* conditional * trigger。您应该找到一个列,您可以在其中设置条件以跳过触发器执行。 – klin