Pg_jsonschema – JSON Schema Support for Postgres

by awaliason 3/22/23, 8:32 AMwith 44 comments
by onderkalacion 3/22/23, 10:54 AM

PG community had a similar patch, which got reverted from PG 15 on the last minute: https://www.depesz.com/2022/04/06/waiting-for-postgresql-15-...

by julian37on 3/22/23, 1:29 PM

I've found that this PL/pgSQL implementation also works well when you don't need maximum performance:

https://github.com/gavinwahl/postgres-json-schema

by boomskatson 3/22/23, 9:48 AM

This is long overdue and elegantly done. Great work!

On a personal note, it's great to finally see Neon & Supabase playing with each other. Much more interesting to me than Hasura.

by Pxtlon 3/22/23, 3:00 PM

I've tried to use JSON Schema and the big gap I couldn't figure was how to handle polymorphism.

That is, if I have a case where object

    {
        "Foo": {
            "Type":"Fred"
            "Bar":1
            "Baz":2
            "Quux":2
        }
    }
and the object

    {
        "Foo": {
            "Type":"Waldo"
            "Bar":1
            "Corge":2
            "Xyzzy":7
        }
    }
are both valid, without just allowing any object members or allowing the union of their members.

I did a hack by multiplexing the types into a child-object, but that was ugly and clumsy.

In XSD or any statically-typed programming language I could handle this trivially using types and polymorphism, because "Fred" and "Waldo" would be different types.

But I can't figure out how to do that in Json Schema.

by mehdixon 3/22/23, 10:03 AM

The article is also a joy to read. Well done.

by hhthrowaway1230on 3/22/23, 9:40 AM

Ah yeah super nice always wanted schema support in progress love to see them hammering away on top of the solid foundation of postgres

by Kydlawon 3/22/23, 10:04 AM

> 2022-08-19

Has anyone tried it? Any feedback on it?

by korijnon 3/22/23, 11:29 AM

So how do you apply this in the real world when dealing with schema versions and database migrations?

by anonuon 3/22/23, 7:55 PM

well written article and looks like a great extension. However, my only issue with JSON Schema is that it becomes unsupportable once your JSON objects get too big: to many keys or too many items in an array for example. If you are looking to find "where" the issue is in your JSON object, most schema validators don't provide enough guidance, they just say "something is wrong" as it appears this one does.

by marwison 3/22/23, 9:08 PM

The detail error from example is pretty awful and missing any useful detail. Is it the limitation of the jsonschema library?

by naedishon 3/22/23, 10:01 AM

This is interesting. Would be curious to see if this can replace pydantic for specific cases.

by rapfariaon 3/22/23, 2:03 PM

Wouldn't you wanna save the json even if the structure is incorrect?

by raydiatianon 3/22/23, 2:43 PM

This looks rad.

by roenxion 3/22/23, 12:13 PM

I can see some technical advantages to supporting JSON schema directly; but I suspect most people will be using this extension because SQL is really ugly and they don't want to use it to set up their schemas in pg proper.

It says a lot about how weak the SQL syntax is. An extension to replace CREATE TABLE with a JSON schema construct would be wildly popular.