Skip to content

Commit

Permalink
Introduce optional global TypeInfoCache
Browse files Browse the repository at this point in the history
In the current implementation, `TypeInfoCache` is a per-connection
cache. This means, over time, that the same type information is
retrieved from the database copious times. For some DB systems, this
has a negative cost/performance impact.

Introduce an optional global `TypeInfoCache`. This is useful for
cases where a single DB system is used in Production for all
connections and there's no need for each connection to re-retrieve
type info.

- Extract the storage portion of `TypeInfoCache` into a new class,
`TypeInfoCacheStorage`
- Add new property, `GLOBAL_TYPE_INFO_CACHE` to enable a global cache
- When enabled, the constant `TypeInfoCacheStorage GLOBAL_INSTANCE` is
always used. Over the course of running a Production instance, this global
cache should not need to query the database for type info.
  • Loading branch information
Randgalt committed Apr 10, 2024
1 parent 699459e commit e4a9de8
Show file tree
Hide file tree
Showing 5 changed files with 260 additions and 132 deletions.
8 changes: 8 additions & 0 deletions pgjdbc/src/main/java/org/postgresql/PGProperty.java
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,14 @@ public enum PGProperty {
+ "the statements reaches the same connection that is being initialized."
),

GLOBAL_TYPE_INFO_CACHE(
"globalTypeInfoCache",
"false",
"Use a type info cache that is global to the driver and not to each connection. "
+ "This can improve overall performance over time by limiting queries to the database "
+ "to get type metadata."
),

GSS_ENC_MODE(
"gssEncMode",
"allow",
Expand Down
4 changes: 3 additions & 1 deletion pgjdbc/src/main/java/org/postgresql/jdbc/PgConnection.java
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,9 @@ public PgConnection(HostSpec[] hostSpecs,

// Initialize object handling
@SuppressWarnings("argument")
TypeInfo typeCache = createTypeInfo(this, unknownLength);
TypeInfo typeCache = PGProperty.GLOBAL_TYPE_INFO_CACHE.getBoolean(info)
? new TypeInfoCache(this, unknownLength, TypeInfoCacheStorage.GLOBAL_INSTANCE)
: createTypeInfo(this, unknownLength);
this.typeCache = typeCache;

Check failure on line 355 in pgjdbc/src/main/java/org/postgresql/jdbc/PgConnection.java

View workflow job for this annotation

GitHub Actions / CheckerFramework

[Task :postgresql:compileJava] [assignment] incompatible types in assignment. this.typeCache = typeCache; ^ found : @UnknownInitialization(org.postgresql.jdbc.TypeInfoCache.class) @nonnull TypeInfo
initObjectTypes(info);

Expand Down

0 comments on commit e4a9de8

Please sign in to comment.