question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Support PostgreSQL JSONB data type in JdbcEventStorageEngine

See original GitHub issue

JdbcEventStorageEngine doesn’t easily support using nonstandard data types such as PostgreSQL’s JSONB for the payload and metadata columns.

The problem is that the queries pass the output of the serializer to the JDBC driver with PreparedStatement.setObject(), but there’s no way to communicate type information.

In the case of PostgreSQL’s JDBC driver, you can insert into a JSONB column in either of two ways. First, you can add an explicit cast to the prepared statement, e.g.,

PreparedStatement ps = conn.prepareStatement("INSERT INTO foo (bar) VALUES (?::JSONB)");
ps.setObject(1, jsonFromSerializer);

Or you can pass the value as a PGobject with type information.

PreparedStatement ps = connection.prepareStatement("INSERT INTO foo (bar) VALUES (?)");
PGobject pgObject = new PGobject();
pgObject.setType("JSONB");
pgObject.setValue(jsonFromSerializer);
ps.setObject(1, pgObject);

Either approach could be exposed as a public API by JdbcEventStorageEngine. Maybe something along the lines of one of

storageEngineBuilder
    .payloadBindString("?::JSONB")
    .metaDataBindString("?::JSONB")

or

private PGobject wrapInPgObject(Object data) {
    PGobject pgObject = new PGobject();
    pgObject.setType("JSONB");
    pgObject.setValue(data);
    return pgObject;
}

...
storageEngineBuilder
    .payloadConverter(this::wrapInPgObject)
    .metaDataConverter(this::wrapInPgObject);

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
smcvbcommented, Aug 1, 2019

Sure thing @Raido0812! This blog by my former colleague Patrick was a consequence of getting PostgreSQL’s JSONB stuff to work.

1reaction
sgrimm-sgcommented, May 8, 2019

That suggestion seems like it might do the trick, yes. Obviously we’ll need to try it to make sure, but it sounds reasonable.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Documentation: 15: 8.14. JSON Types - PostgreSQL
The jsonb data type supports array-style subscripting expressions to extract and modify elements. Nested values can be indicated by chaining subscripting ...
Read more >
No-spring JDBC data store, using jsonb postgresql datatype
Desire to use jsonb for payload column in JdbcEventStorageEngine, non-spring setup. I've setup the Jackson serializer okay, ...
Read more >
How to use PostgreSQL's JSONB data type with Hibernate
PostgreSQL offers proprietary datatypes to store JSON documents which are not supported by Hibernate. But you can change that with a UserType.
Read more >
Processing PostgreSQL JSON & JSONB data in Java - EDB
Starting v9.2, PostgreSQL is providing native data type support for JSON objects. Subsequent releases introduced JSONB (binary formatted ...
Read more >
Querying JSON (JSONB) data types in PostgreSQL - SILOTA
Querying JSON (JSONB) data types in PostgreSQL. One of the unusual features of the PostgreSQL database is the ability to store and process...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found