Skip to content

Conversation

davecramer
Copy link
Member

No description provided.

@davecramer
Copy link
Member Author

@vlsi I'd like to merge this. Any issues ?

@@ -218,6 +218,8 @@ private enum ReadOnlyBehavior {
private final @Nullable String xmlFactoryFactoryClass;
private @Nullable PGXmlFactoryFactory xmlFactoryFactory;
private final LazyCleaner.Cleanable<IOException> cleanable;
/* this is actually the database we are connected to */
private String catalog;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
private String catalog;
private @Nullable String catalog;

I guess we have @SuppressWarning for PgConnection constructor, so it results in silencing warning for the newly added field.
However, the field is nullable, so let's specify nullable type.

…ase in the connection URL or connection properties as this could be different if connected through a pooler or proxy

Update pgjdbc/src/main/java/org/postgresql/jdbc/PgConnection.java

Co-authored-by: Vladimir Sitnikov <sitnikov.vladimir@gmail.com>

fix checkstyle

fix checkstyle
@davecramer davecramer merged commit 156d724 into pgjdbc:master Apr 1, 2025
13 of 15 checks passed
@sehrope
Copy link
Member

sehrope commented Apr 2, 2025

Let me preface this by saying I'm not opposed to this PR ... though thinking about this a couple questions came to mind.

What happens if the DB changes? The whole point of this PR is to skip the fixed value in connection properties and pass through to the actual database, potentially bypassing any intermediate connection pool(s). If the pooler reroutes the connection somewhere else then it'd be wrong as we would have cached the previous value. I suppose that type of pooling is less common, but it does come up for things like live migrations and upgrades.

There's also a slight change in behavior here. Previously the method was a pass through to QueryExecutor.getDatabase() which returned a cached String value. Now that we always execute the command on the first call, this means you cannot call this method while the connection is otherwise in use. I'm worried this may lead to some weird edge case where a user runs a query or COPY operation, and then tries to use this data while processing the result.

We could resolve this by caching the value at connection startup. But I don't think we want to add more mandatory startup round trips.

@davecramer
Copy link
Member Author

Actually one point of the PR is to make sure we get the right database. If you use pg_bouncer then you connect to some database in pg_bouncer which redirects you to the actual database.

Q: How can the database change ?

@sehrope
Copy link
Member

sehrope commented Apr 2, 2025

The situation I'm describing is where the pooler swaps out the physical connection. There's no guarantee I get the same catalog when it opens a new connection.

Interestingly I found this pgbouncer fork (replacement?) which seems to do exactly what I'm describing: https://github.com/awslabs/pgbouncer-fast-switchover

Imagine configuring that with multiple targets that have different database names. Could be the same actual database, just the return value of SELECT catalog_name is different.

@davecramer
Copy link
Member Author

Well our options are to query the database every time or cache it.
99.99999% would be OK with caching it.

@sehrope
Copy link
Member

sehrope commented Apr 2, 2025

Or return the original value and have people who are doing that pooling stuff explicitly query for the live one themselves.

@vlsi
Copy link
Member

vlsi commented Apr 2, 2025

It would be great if catalog_name was GUC_REPORT.
The bouncer could emit notifications if the catalog_name ever changed.

@davecramer
Copy link
Member Author

It would be great if catalog_name was GUC_REPORT. The bouncer could emit notifications if the catalog_name ever changed.

That is really a pg_bouncer problem. In reality the server will never change the catalog once the connection is established. I think we could get pg_bouncer to emit database however.

@vlsi
Copy link
Member

vlsi commented Apr 2, 2025

My feeling exactly. However, if we expect pg_bouncer to emit database, we should expect the DB to emit it as well.
However, hope the current PR is good enough.

@landryb
Copy link

landryb commented May 31, 2025

thanks for this PR, 42.7.6 fixes a regression that was introduced with #3390.

Using 42.7.3 against a database cnx handled by pgbouncer having a different name/alias than the real database, geotools was able to list attributes/fields in a layer/table without issues, while it started failing after an update to 42.7.5.

turning log_statement on, i saw that the query done had AND current_database() = 'pgbouncer_alias' which wasn't the real table name, thus no attributes were returned and geotools failed accessing the layer geometry.

replacing 42.7.5 jar by 42.7.6 jar, everything works as before, so thanks @davecramer !

ianturton pushed a commit to geotools/geotools that referenced this pull request Jun 2, 2025
fixes access to a database behind a pgbouncer alias which doesn't
match the real database name, cf pgjdbc/pgjdbc#3565

more analysis in GEOS-11849
geoserver-bot pushed a commit to geotools/geotools that referenced this pull request Jun 2, 2025
fixes access to a database behind a pgbouncer alias which doesn't
match the real database name, cf pgjdbc/pgjdbc#3565

more analysis in GEOS-11849
geoserver-bot pushed a commit to geotools/geotools that referenced this pull request Jun 2, 2025
fixes access to a database behind a pgbouncer alias which doesn't
match the real database name, cf pgjdbc/pgjdbc#3565

more analysis in GEOS-11849
mprins pushed a commit to geotools/geotools that referenced this pull request Jun 3, 2025
fixes access to a database behind a pgbouncer alias which doesn't
match the real database name, cf pgjdbc/pgjdbc#3565

more analysis in GEOS-11849
mprins pushed a commit to geotools/geotools that referenced this pull request Jun 3, 2025
fixes access to a database behind a pgbouncer alias which doesn't
match the real database name, cf pgjdbc/pgjdbc#3565

more analysis in GEOS-11849
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants