Upsert PostgreSQL

An upsert is a database operation that will update an existing row if a specified value already exists in a table, and insert a new row if the specified value doesn’t already exist.

Authors: Sergio del Amo

Micronaut Version: 5.2.3

1. Getting Started

In this guide, we will create a Micronaut application written in Groovy.

An upsert is a native database operation that inserts a row when no matching row exists, or updates the existing row when a match is found. Doing this in a single statement, instead of a "find, then insert or update" sequence, avoids a race condition: two concurrent requests cannot both decide the row is missing and both attempt an insert.

In this guide, you will declare an upsert method in a Micronaut Data JDBC repository and use it from an HTTP controller backed by PostgreSQL.

2. What you will need

To complete this guide, you will need the following:

3. Solution

We recommend that you follow the instructions in the next sections and create the application step by step. However, you can go right to the completed example.

4. Writing the Application

Create an application using the Micronaut Command Line Interface or with Micronaut Launch.

mn create-app example.micronaut.micronautguide \
    --features=data-jdbc,liquibase,postgres,serialization-jackson,testcontainers \
    --build=maven \
    --lang=groovy \
    --test=spock
If you don’t specify the --build argument, Gradle with the Kotlin DSL is used as the build tool.
If you don’t specify the --lang argument, Java is used as the language.
If you don’t specify the --test argument, JUnit is used for Java and Kotlin, and Spock is used for Groovy.

The previous command creates a Micronaut application with the default package example.micronaut in a directory named micronautguide.

If you use Micronaut Launch, select Micronaut Application as application type and add data-jdbc, liquibase, postgres, serialization-jackson, and testcontainers features.

If you have an existing Micronaut application and want to add the functionality described here, you can view the dependency and configuration changes from the specified features, and apply those changes to your application.

4.1. Micronaut Data JDBC

Add Micronaut Data JDBC dependencies to the project:

pom.xml
<dependency>
    <groupId>io.micronaut.data</groupId>
    <artifactId>micronaut-data-processor</artifactId>
    <scope>compile</scope>
</dependency>
<dependency>
    <groupId>io.micronaut.data</groupId>
    <artifactId>micronaut-data-jdbc</artifactId>
    <scope>compile</scope>
</dependency>
<dependency>
    <groupId>io.micronaut.sql</groupId>
    <artifactId>micronaut-jdbc-hikari</artifactId>
    <scope>compile</scope>
</dependency>

4.2. PostgreSQL Driver

Add also the PostgreSQL Driver

pom.xml
<dependency>
    <groupId>org.postgresql</groupId>
    <artifactId>postgresql</artifactId>
    <scope>runtime</scope>
</dependency>

4.3. Data Source configuration

Define the datasource in src/main/resources/application.properties.

src/main/resources/application.properties
(1)
datasources.default.driver-class-name=org.postgresql.Driver
(2)
datasources.default.db-type=postgres
(3)
datasources.default.dialect=POSTGRES
1 Use the PostgreSQL driver.
2 Set the database type.
3 Configure the PostgreSQL dialect.

4.4. Database Migration with Liquibase

We need a way to create the database schema. For that, we use Micronaut integration with Liquibase.

Add the following snippet to include the necessary dependencies:

pom.xml
<dependency>
    <groupId>io.micronaut.liquibase</groupId>
    <artifactId>micronaut-liquibase</artifactId>
    <scope>compile</scope>
</dependency>

Configure the database migrations directory for Liquibase in application.properties.

src/main/resources/application.properties
liquibase.datasources.default.change-log=classpath\:db/liquibase-changelog.xml

Create the following files with the database schema creation:

src/main/resources/db/liquibase-changelog.xml
<?xml version="1.0" encoding="UTF-8"?>
<databaseChangeLog
  xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog
         http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.1.xsd">
  <include file="changelog/01-schema.xml" relativeToChangelogFile="true"/>
</databaseChangeLog>
src/main/resources/db/changelog/01-schema.xml
<?xml version="1.0" encoding="UTF-8"?>

<databaseChangeLog
  xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog
         http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.1.xsd">
  <changeSet id="01" author="username">
    <createTable tableName="book">
      <column name="id" type="BIGINT">
        <constraints primaryKey="true" nullable="false"/>
      </column>
      <column name="title" type="VARCHAR(255)">
        <constraints nullable="false"/>
      </column>
      <column name="pages" type="INT">
        <constraints nullable="true"/>
      </column>
    </createTable>
  </changeSet>
</databaseChangeLog>

The id column is the table’s primary key. PostgreSQL requires a primary key or unique constraint that matches the conflict target of an upsert.

4.5. Entity

Create a Book entity:

src/main/groovy/example/micronaut/Book.groovy
package example.micronaut

import groovy.transform.CompileStatic
import io.micronaut.data.annotation.Id
import io.micronaut.data.annotation.MappedEntity
import io.micronaut.serde.annotation.Serdeable
import org.jspecify.annotations.Nullable

@CompileStatic
@Serdeable (1)
@MappedEntity (2)
class Book {

    @Id (3)
    Long id

    String title

    @Nullable
    Integer pages
}
1 Declare the @Serdeable annotation at the type level in your source code to allow the type to be serialized or deserialized.
2 Annotate the class with @MappedEntity to map the class to the table defined in the schema.
3 Specifies the ID of an entity
The id is not annotated with @GeneratedValue. By default, Micronaut Data uses the entity identity as the upsert conflict target, so the caller must supply the identity value. Upserts that rely on the default identity conflict target reject entities with generated identities.

4.6. Repository

Create a repository and declare an upsert method:

src/main/groovy/example/micronaut/BookRepository.groovy
package example.micronaut

import io.micronaut.data.jdbc.annotation.JdbcRepository
import io.micronaut.data.model.query.builder.sql.Dialect
import io.micronaut.data.repository.CrudRepository
import org.jspecify.annotations.Nullable

@JdbcRepository(dialect = Dialect.POSTGRES) (1)
interface BookRepository extends CrudRepository<Book, Long> { (2)

    @Nullable
    Book findByTitle(String title)

    void upsert(Book book) (3)
}
1 @JdbcRepository with a specific dialect.
2 By extending CrudRepository you enable automatic generation of CRUD (Create, Read, Update, Delete) operations.
3 Upsert methods are opt-in; they are not inherited from CrudRepository. Micronaut Data implements a method named exactly upsert (or upsertAll for an Iterable of entities) as a native upsert.

For the POSTGRES dialect, Micronaut Data generates an INSERT …​ ON CONFLICT …​ DO UPDATE statement at compilation time. For the Book entity, the generated SQL is similar to:

INSERT INTO "book" ("id","title","pages") VALUES (?,?,?)
ON CONFLICT ("id") DO UPDATE SET "title"=EXCLUDED."title","pages"=EXCLUDED."pages"

If no row with the given id exists, PostgreSQL inserts a new row. Otherwise, it updates the title and pages columns of the existing row with the values proposed for insertion, which PostgreSQL exposes through the special EXCLUDED table.

Use the @Upsert annotation to give an upsert method a custom name, or its conflictsOn member to use another unique property, such as an ISBN, as the conflict target instead of the identity.

4.7. Controller

Create a controller which uses the upsert method both to create and to update a book:

src/main/groovy/example/micronaut/BookController.groovy
package example.micronaut

import groovy.transform.CompileStatic
import io.micronaut.http.HttpStatus
import io.micronaut.http.annotation.Body
import io.micronaut.http.annotation.Controller
import io.micronaut.http.annotation.Post
import io.micronaut.http.annotation.Put
import io.micronaut.http.annotation.Status

@CompileStatic
@Controller('/books') (1)
class BookController {

    private final BookRepository bookRepository

    BookController(BookRepository bookRepository) { (2)
        this.bookRepository = bookRepository
    }

    @Post (3)
    @Status(HttpStatus.CREATED) (4)
    void save(@Body Book book) { (5)
        bookRepository.upsert(book) (6)
    }

    @Put (7)
    @Status(HttpStatus.OK)
    void update(@Body Book book) {
        bookRepository.upsert(book) (6)
    }
}
1 The class is defined as a controller with the @Controller annotation mapped to the path /books.
2 Use constructor injection to inject a bean of type BookRepository.
3 The @Post annotation maps the save method to an HTTP POST request on /books.
4 You can return void in your controller’s method and specify the HTTP status code via the @Status annotation.
5 The @Body annotation indicates that the request body should be parsed by Micronaut Framework and bound to the annotated parameter.
6 The same repository method inserts or updates the row, depending on whether a book with the supplied id already exists.
7 The @Put annotation maps the update method to an HTTP PUT request on /books.

4.8. Tests

Create an abstract test class that starts a PostgreSQL container with Testcontainers and points the default datasource to it:

src/test/groovy/example/micronaut/AbstractSpec.groovy
package example.micronaut

import io.micronaut.core.annotation.NonNull
import io.micronaut.test.support.TestPropertyProvider
import org.testcontainers.containers.PostgreSQLContainer
import spock.lang.Specification

abstract class AbstractSpec extends Specification implements TestPropertyProvider { (1)

    static PostgreSQLContainer postgres = new PostgreSQLContainer('postgres:15.3-alpine') (2)

    @Override
    @NonNull
    Map<String, String> getProperties() {
        if (!postgres.running) {
            postgres.start() (3)
        }
        [
                'datasources.default.driver-class-name': 'org.postgresql.Driver', (4)
                'datasources.default.url'              : postgres.jdbcUrl,
                'datasources.default.username'         : postgres.username,
                'datasources.default.password'         : postgres.password,
        ]
    }
}
1 When you need dynamic properties definition, implement the TestPropertyProvider interface. Override the method .getProperties() and return the properties you want to expose to the application.
2 Declare the container once, so every test which extends the abstract class shares it.
3 Start the container if it is not running yet.
4 Use the PostgreSQL JDBC driver and the container’s JDBC URL and credentials.

Create a test which sends a POST request to create a book and then a PUT request with the same id to update it:

src/test/groovy/example/micronaut/BookControllerSpec.groovy
package example.micronaut

import io.micronaut.http.HttpRequest
import io.micronaut.http.HttpResponse
import io.micronaut.http.HttpStatus
import io.micronaut.http.client.BlockingHttpClient
import io.micronaut.http.client.HttpClient
import io.micronaut.http.client.annotation.Client
import io.micronaut.test.extensions.spock.annotation.MicronautTest
import jakarta.inject.Inject

@MicronautTest (1)
class BookControllerSpec extends AbstractSpec { (2)

    @Inject
    BookRepository repository (3)

    @Inject
    @Client('/')
    HttpClient httpClient (4)

    void 'POST and PUT upsert a book'() {
        given:
        BlockingHttpClient client = httpClient.toBlocking()
        String path = '/books'
        String title = 'Netty in Action'

        expect:
        !repository.findByTitle(title) (5)

        when:
        HttpResponse<?> postResponse = client.exchange(HttpRequest.POST(path, [id: 123L, title: title]))

        then:
        postResponse.status == HttpStatus.CREATED (6)
        repository.findByTitle(title)
        repository.findByTitle(title).pages == null

        when:
        HttpResponse<?> putResponse = client.exchange(HttpRequest.PUT(path, [id: 123L, title: title, pages: 265]))

        then:
        putResponse.status == HttpStatus.OK (7)
        repository.findByTitle(title).pages == 265 (8)
    }
}
1 Annotate the class with @MicronautTest so the Micronaut framework will initialize the application context and the embedded server. More info.
2 Extend AbstractSpec to run the test against a database started by Testcontainers.
3 Inject the BookRepository bean to verify the database state.
4 Inject the HttpClient bean and point it to the embedded server.
5 Initially, the book does not exist.
6 The first upsert inserts the book without pages.
7 The second upsert, with the same id, updates the existing row instead of failing with a duplicate key error.
8 The book now has pages.

5. Testing the Application

To run the tests:

./mvnw test

6. Next Steps

Explore more features with Micronaut Guides.

Read more about Upserts in the Micronaut Data documentation, including conflict targets, batch upserts, supported dialects, and lifecycle event behavior.

7. License

All guides are released with an Apache License 2.0 for the code and a Creative Commons Attribution 4.0 license for the writing and media (images).