0

I'm quite new to Postgres and PgBouncer as well, not a complete newbie, but still lacking experience.

I'm using Postgres + PgBouncer, spring and hibernate as tech stack.

We have multiple microservices working under different schemas, sometimes even shared ones. All these microservices are connecting to db via PgBouncer.

Sometimes I'm receiving the following exception when running queries:

"Caused by: org.postgresql.util.PSQLException: ERROR: relation "XXX" does not exist"

For queries defined in Repositories it's quite easy to fix it by applying {h-schema} before table names like "SELECT * FROM {h-schema}XXX"

but I also have ~100 queries stored in db which I create using Query query = EntityManager.createNativeQuery(..); return query.getResultList(); and I would like to avoid updating the statements of these queries.

Queries are readonly but they are supposed to be invoked sometimes sequentially in a single transaction (isolation level = Repeatable Read), so it's wrapped in spring @Transactional service.

From what I understand PgBouncer sometimes returns connections from pool which are pointing to wrong schema / search_path. How can I enforce my queries to use the correct schema without modifying them?

Shall it be a simple append:

entityManager.createNativeQuery("set search_path to 'schema';" + queryStr, Foo.class)

Since @Transactional implies every query is run in the same connection (obtained once from PgBouncer) it will guarantee Repeatable Read, am I correct?

I have tried multiple things but since the error is not easily reproducible, I have doubts and want to learn the correct way.

2
  • It should be sufficient to set the search path as the first action in the transaction. There's no need to set it before each individual query. Commented Mar 1 at 18:17
  • @JohnH thanks for a suggestion, seems like it will work for my usecase (transactional mode for PgBouncer). Commented Mar 3 at 17:59

1 Answer 1

0

As @JohnH suggested, seems like having SET SEARCH_PATH TO XXX at the beginning of each transaction is enough for PgBouncer operating in transactional mode.

Sign up to request clarification or add additional context in comments.

1 Comment

Please accept your own answer if it solves the problem

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.