Re: Upsert performance considerations (~1 mil/hour)

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Wed, Sep 4, 2019 at 1:30 PM Fredrik Blomqvist <fredrik.blomqvist.95@xxxxxxxxx> wrote:
Hi,

I have tried doing some research for quite a while on the performance implications of the built-in upsert (INSERT ... ON CONFLICT UPDATE...) when a lot of upserts are made. The scale is something like 1 million records/hour, that is split up in groups of around 300 records each.

How is that done?  300 single-valued insert statements, grouped into on transaction?  one 300-valued insert statement?
 
So far, one hypothesis is that this project seems to be suffering from the large amount of writes that happen constantly since even if the upsert results in no inserts/updates, the "failed" inserts from the upsert will still get written somewhere (according to our knowledge).

You can suppress redundant updates with a trigger, as described https://www.postgresql.org/docs/current/functions-trigger.html.  This works even for updates that are the result of insert..on conflict..update.  There is still some writing, as each tuple does get locked, but it is much less (at least from a WAL perspective).   You can also put  a WHERE clause on the DO UPDATE so it only updates is a field has changed, but you have to list each field connected with OR.
 
Therefore, the idea is to utilize old-fashioned upserts (writeable CTEs) and do more granular operations that can make sure to only insert data that doesn't already exist, and only update data that has actually changed. Naturally, however, this will put more read-load on the DB and increase query complexity.

It shouldn't put a meaningful additional read load on the database, as the ON CONFLICT code still needs to do the read as well.  Yes, it makes the code slightly more complex.

Cheers,

Jeff

[Postgresql General]     [Postgresql PHP]     [PHP Users]     [PHP Home]     [PHP on Windows]     [Kernel Newbies]     [PHP Classes]     [PHP Books]     [PHP Databases]     [Yosemite]

  Powered by Linux