First, hstore is a contrib module, which only allows you to store key => value pairs, where keys and values can only be texts (however values can be sql NULLs too).
Both json & jsonb allows you to store a valid JSON value (defined in its spec).
F.ex. these are valid JSON representations: null, true, [1,false,"string",{"foo":"bar"}], {"foo":"bar","baz":[null]} - hstore is just a little subset compared to what JSON is capable (but if you only need this subset, it's fine).
The only difference between json & jsonb is their storage:
jsonis stored in its plain text format, whilejsonbis stored in some binary representation
There are 3 major consequences of this:
jsonbusually takes more disk space to store thanjson(sometimes not)jsonbtakes more time to build from its input representation thanjsonjsonoperations take significantly more time thanjsonb(& parsing also needs to be done each time you do some operation at ajsontyped value)
When jsonb will be available with a stable release, there will be two major use cases, when you can easily select between them:
- If you only work with the JSON representation in your application, PostgreSQL is only used to store & retrieve this representation, you should use
json. - If you do a lot of operations on the JSON value in PostgreSQL, or use indexing on some JSON field, you should use
jsonb.
When do JSON columns make sense in Postgres?
When to use JSONB vs. separate columns
How does PostgreSQL keeps long data such as JSONB.
Single Database Table with a JSONB column
First, hstore is a contrib module, which only allows you to store key => value pairs, where keys and values can only be texts (however values can be sql NULLs too).
Both json & jsonb allows you to store a valid JSON value (defined in its spec).
F.ex. these are valid JSON representations: null, true, [1,false,"string",{"foo":"bar"}], {"foo":"bar","baz":[null]} - hstore is just a little subset compared to what JSON is capable (but if you only need this subset, it's fine).
The only difference between json & jsonb is their storage:
jsonis stored in its plain text format, whilejsonbis stored in some binary representation
There are 3 major consequences of this:
jsonbusually takes more disk space to store thanjson(sometimes not)jsonbtakes more time to build from its input representation thanjsonjsonoperations take significantly more time thanjsonb(& parsing also needs to be done each time you do some operation at ajsontyped value)
When jsonb will be available with a stable release, there will be two major use cases, when you can easily select between them:
- If you only work with the JSON representation in your application, PostgreSQL is only used to store & retrieve this representation, you should use
json. - If you do a lot of operations on the JSON value in PostgreSQL, or use indexing on some JSON field, you should use
jsonb.
Peeyush:
The short answer is:
- If you are doing a lot of JSON manipulation inside PostgreSQL, such as sorting, slicing, splicing, etc., you should use JSONB for speed reasons.
- If you need indexed lookups for arbitrary key searches on JSON, then you should use JSONB.
- If you are doing neither of the above, you should probably use JSON.
- If you need to preserve key ordering, whitespace, and duplicate keys, you should use JSON.
For a longer answer, you'll need to wait for me to do a full "HowTo" writeup closer to the 9.4 release.
I'm considering using JSON for fields that I never use in WHERE clauses and which are only used for a relatively small subset of rows in the table like attributes for certain customers.
I'm building a social media-like application and trying to model the tables.
I realise Postgres now supports JSONB which is exciting, but I'm unsure when to use it.
I have a few examples where I think it could be used:
A user signs up and they specifiy their address. Should the address be a JSONB or separate fields. E.g.,
JSONB: { address1: "", address2: "", zipCode: "" } Columns: address1 (Varchar), address2 (varchar)...
Also things like posts, comments, upvotes etc. They feel like they could be JSONB as well? I'm really not sure!
Post info: { id: uuid, description: "", upvotes: 4 } Columns: id (uuid), description (varchar(255)), upvotes: int
Thanks all.