5

I need to have a generic key-value data-store in Postgres and Sqlite3. I originally planned to store this as a JSONB type but the SQL statements I execute needs to be compatible with both Postgres and Sqlite3. Since sqlite doesn't support jsonb, I planned to use BYTEA instead. So something like this:

Originally:

CREATE TABLE foo (
    id INT PRIMARY KEY,
    data JSONB
)

Now

CREATE TABLE foo (
    id INT PRIMARY KEY,
    data BYTEA
)

This is not the best but it's still fine since I thought I could just cast BYTEA to JSON during query. That is, I thought I could do something like this:

SELECT data::json ->> 'name' AS name from foo where id = $1;

But looks like I cannot cast BYTEA to JSON type (ERROR: cannot cast type bytea to json). This post describes how to convert BYTEA to JSON. But I'm worried that the decode() and convert_from() functions are going to be expensive.

So now I'm considering storing this data as VARCHAR instead of BYTEA, which eliminates the need for the decode() and convert_from() functions.

I'm wondering what the best option is here and if there's any better methods given the constraints I described.

13
  • 2
    Does SQLite really support the same syntax on JSON function as Postgres? If not you are going to have different SQL queries anyway. Having different CREATE TABLE statements seems the much smaller problem to solve. I would go for a jsonb column in Postgres and a text column in SQLite
    – user1822
    Commented Dec 18, 2019 at 22:56
  • 1
    The database initialization happens through a C++ program, I'm not in control of the underlying interface that interacts with the databases. My understanding is that Postgres and SQLite support mostly the same syntax except for a few caveats. jsonb is one of them. Essentially, this program run unit tests with SQLite and runs in production with Postgres.
    – bli00
    Commented Dec 18, 2019 at 23:18
  • 3
    why not 'text' type?
    – jjanes
    Commented Dec 18, 2019 at 23:45
  • 1
    Sure, text type. It think it'd still be the same thing as varchar though.
    – bli00
    Commented Dec 18, 2019 at 23:57
  • 1
    in postgresql text and varchar are different, in sqlite varchar(255) is converted to text, sqlite.org/datatype3.html postgresql.org/docs/9.3/datatype-character.html I'm with the jjanes use text type, it has the least issues trying to maintain db portability
    – zsheep
    Commented Dec 19, 2019 at 2:11

1 Answer 1

1

I have same setup and successfully extracted data with

SELECT encode(data, 'escape')::jsonb->'name'
FROM   foo 
1
  • I guess that could work so long as you don;t have any values containing 0x22 " eg: {"name": "Jesse \"The Body\" Ventura"}
    – Jasen
    Commented Sep 11, 2022 at 8:04

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.