question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

reWriteBatchedInserts does not work with PostgreSQL

See original GitHub issue

I’m trying to use reWriteBatchedInserts with a PostgreSQL database. According to pgjdbc the rewrite is possible only if no return values are requested.

In InsertStatement.kt:

    override fun prepared(transaction: Transaction, sql: String): PreparedStatementApi = when {
        // https://github.com/pgjdbc/pgjdbc/issues/1168
        // Column names always escaped/quoted in RETURNING clause
        autoIncColumns.isNotEmpty() && currentDialect is PostgreSQLDialect ->
            transaction.connection.prepareStatement(sql, true)

        autoIncColumns.isNotEmpty() ->
            // http://viralpatel.net/blogs/oracle-java-jdbc-get-primary-key-insert-sql/
            transaction.connection.prepareStatement(sql, autoIncColumns.map { it.name.inProperCase() }.toTypedArray())

        else ->
            transaction.connection.prepareStatement(sql, true)
    }

So the prepare statement is always call with true.

In JdbcConnectionImpl:

    override fun prepareStatement(sql: String, returnKeys: Boolean) : PreparedStatementApi {
        val generated = if (returnKeys)
            PreparedStatement.RETURN_GENERATED_KEYS
        else
            PreparedStatement.NO_GENERATED_KEYS
        return JdbcPreparedStatementImpl(connection.prepareStatement(sql, generated), returnKeys)
    }

So every insert requests set returnKeys to PreparedStatement.RETURN_GENERATED_KEYS.

The consequence is that the optimization implies by reWriteBatchedInserts is not done.

Am I missing something?

ps: I’m using batchInsert and I set reWriteBatchedInserts to true correctly (I checked it in debug). I also tested some inserts using directly the jdbc api and the rewrite worked (I saw it in postgresql logs).

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
ekanscommented, May 26, 2020

@Tapac for your information I just tested version 0.25.1 and it works great! Thanks.

1reaction
Tapaccommented, May 18, 2020

Today or tomorrow.

Read more comments on GitHub >

github_iconTop Results From Across the Web

reWriteBatchedInserts=true not working with ON CONFLICT ...
When using the new reWriteBatchedInserts=true feature, this fails when using ON CONFLICT DO UPDATE ... Assume the following table:.
Read more >
PostgreSQL reWriteBatchedInserts configuration property
Learn how the PostgreSQL reWriteBatchedInserts JDBC configuration property works and how it generates multi-VALUE INSERT statements.
Read more >
How to Write into a Database Using reWriteBatchedInserts ...
In this article, the author explains how to perform batch insert in PostgreSQL to write data into a database quickly.
Read more >
Hibernate/Postgresql batch insert no longer working after ...
After the update, the INSERTs are no longer batched (I can tell because the execution time is much faster before the update.) I...
Read more >
Vlad Mihalcea on Twitter: "5. Enable the ...
Learn how the PostgreSQL SERIAL column type works and why you don't want to ... Enable the reWriteBatchedInserts JDBC setting to speed up...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found