October 22, 2024
Chicago 12, Melborne City, USA
java

Postgres drop type XX000 “cache lookup failed for type”


I’m using Quarkus and Hibernate to work with PostgreSQL/PostGIS. I have an entity Geofence that contains a Polygon field. The issue occurs when running tests with Cucumber.

The problem:
When running the tests, I get the following error on the second and subsequent examples within the test scenarios:

javax.persistence.PersistenceException: org.hibernate.exception.GenericJDBCException: could not execute statement ... Caused by: org.postgresql.util.PSQLException: ERROR: cache lookup failed for type 18049
The error occurs during the execution of the persistAndRefresh method:

public void persistAndRefresh(@NonNull Entity<?> object) {
persist(object);  // Error happens at this line on the second test run
entityManager.flush();
entityManager.refresh(object);
}

Important details:
Tests: I’m using Cucumber to write the tests. Each test has several examples (Examples).

Testcontainers: We spin up a single PostgreSQL/PostGIS container for all the tests, and for each test, we dump and restore the database using Testcontainers. The container uses the PostGIS image:

private static final DockerImageName POSTGIS_IMAGE = DockerImageName
.parse("postgis/postgis:16-3.4")
.asCompatibleSubstituteFor("postgres");

Issue behavior:

The first example in the scenario runs successfully.
On the second example in the same test, the error occurs when calling persist(object) (at the beginning of persistAndRefresh).

Workaround: If we call a method that retrieves all records from the table (for example, getAll()) before persisting the object, the error does not occur. Here is an example of how we are working around the issue:

geofenceClient.getAll(token).as(List.class);  // No error if this is called before persisting
persist(object);  // The error disappears after calling getAll()

Polygon field: The Polygon field in the Geofence entity is mapped to a geometry(Polygon, 4326) type in the database.

Clearing context after each test: After each test, we clear the test context and restore the database dump using the following code:

@After
public void cleanUp() {
    // Clear test contexts
    testContexts.stream().forEach(testContexts::destroy);

// Restore the database from the dump
PostgresResource.restoreDump();

// Clear the time context
DateTimeProvider.clearContext();

}

What could be causing the cache lookup failed for type 18049 error on the second example in the test, and how can it be fixed? Why does calling a method that fetches all records from the table prevent the error?



You need to sign in to view this answers

Leave feedback about this

  • Quality
  • Price
  • Service

PROS

+
Add Field

CONS

+
Add Field
Choose Image
Choose Video