Our loader API has multiple options to throttle insertions through jOOQ API: https://www.jooq.org/doc/latest/manual/sql-execution/importing/importing-options/importing-option-throttling/
But the loader API doesn't support the RETURNING clause. I'm assuming you're using an IDENTITY (or serial) column, so one workaround could be to pre-fetch all the IDs that you're going to need. jOOQ has a convenient nextvals() synthetic table valued function to do this: https://blog.jooq.org/how-to-fetch-sequence-values-with-jooq/ And then indeed, using the COPY command heavily outperforms anything SQL based (if you're not using any other jOOQ feature for this insertion). If you must use jOOQ for some abstraction reason, then pre-fetching IDs and using the Loader API would be my go-to option here, or alternatively, chunk your data set into smaller sets manually. I hope this helps, Lukas On Tue, May 28, 2024 at 8:39 PM Giovanni Zotta < [email protected]> wrote: > Hello there, > > This is not necessarily a jOOQ issue, but I wonder how you would best > solve it when using jOOQ. If it's too off-topic, feel free to direct me > somewhere else. > > We have been using jOOQ happily for a while in production; every day we > have a couple of big bulk insert queries (inserting >200k records at once) > that were taking ~2 minutes every time. Some days ago, we configured a > parameter in our Postgres server (idle_in_transaction_session_timeout > <https://www.postgresql.org/docs/current/runtime-config-client.html#GUC-IDLE-IN-TRANSACTION-SESSION-TIMEOUT>) > that kills all sessions that are in a transaction, but haven't executed any > query in 30s. After implementing this configuration, these inserts started > to fail. > > On closer inspection of a past trace where the timeout is not implemented, > these are the logs on our services: > > 1. 23:31:53: Application starts the insert query (with returning > clause) > 2. 23:31:56: SQL is rendered by jOOQ and logged (3s) > 3. 23:33:53: Postgres says query took 8s (after 2 minutes) > 4. 23:33:54: Application fetched all results, and transaction is > committed in the same second > > So, Postgres says the query took ~8s, but the application has been waiting > for 2 minutes. We think this is because the SQL we render is very large > (it's a query of the form `insert into table (column1, column2) values (1, > 2), (3,4)`) and Postgres spends a lot of time parsing it, even though > honestly ~2 minutes seems like a lot of time. > > This is not a jOOQ issue per-se, but we speculated that splitting the > query up into smaller batches would solve the issue. I'm curious what > options we have to do such an insert, where we also need to return the IDs > of the generated records. > > Regards, > Giovanni > > -- > You received this message because you are subscribed to the Google Groups > "jOOQ User Group" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to [email protected]. > To view this discussion on the web visit > https://groups.google.com/d/msgid/jooq-user/0266d773-652a-4b46-80d0-4b5d86fbbff9n%40googlegroups.com > <https://groups.google.com/d/msgid/jooq-user/0266d773-652a-4b46-80d0-4b5d86fbbff9n%40googlegroups.com?utm_medium=email&utm_source=footer> > . > -- You received this message because you are subscribed to the Google Groups "jOOQ User Group" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To view this discussion on the web visit https://groups.google.com/d/msgid/jooq-user/CAB4ELO7sWEuUtH51oN8EudEv2%3Dt_i-or30vKFbfJG6fy-umpXA%40mail.gmail.com.
