pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Adrian Klaver <adrian.klaver@aklaver.com>
To: David Gauthier <davegauthierpg@gmail.com>
To: Postgres General <pgsql-general@postgresql.org>
Subject: Re: How is JSON stored in PG ?
Date: Tue, 16 Nov 2021 08:26:21 -0800
Message-ID: <d179aa08-0c6f-7890-0a8e-610169bbbef1@aklaver.com> (raw)
In-Reply-To: <CAMBRECD14zp50Ex3E8L2MtZPHGAsSVX3zDo57=e_ov7fM8ONSA@mail.gmail.com>
References: <CAMBRECD14zp50Ex3E8L2MtZPHGAsSVX3zDo57=e_ov7fM8ONSA@mail.gmail.com>

On 11/16/21 07:54, David Gauthier wrote:
> Hi:
> 
> psql (11.5, server 11.3) on linux
> 
> I'm considering using JSON as a datatype for something I'm working on.  
> The reasons are...
> 
> 1) the 'metadata' (if you want to call it that) in JSON is very 
> flexible.  Doesn't require an alter table or anything like that to change.
> 2) The customers for this data is probably going to be python code.  
> IOW, they can sort things out in their code after reading the whole JSON 
> file/record.
> 3) Nice array of built-in functions for this datatype.
> 4) There does appear to be the ability to formulate query predicates on 
> the JSON content (just in case)
> 
> But #4 has me a bit worried.  I'm wondering how PG stores this data 
> given that its content can be specified in a query predicate.  Does PG 
> just store the content in traditional PG tables ?  If so, I can do that 
> myself.  If not, is there a penalty to be paid at query time if PG needs 
> to get the JSON data, then dismantle into a temp table (or something 
> like that) to query.

1) Are you talking json or jsonb?

2) If you are going to store JSON in a non-JSON field then you will need 
to cast the field value to json/jsonb to use the JSON capabilities in 
Postgres. Not sure that is a good idea.

3) json(b) is stored in a field in a table per 
https://www.postgresql.org/docs/current/datatype-json.html:

"The json and jsonb data types accept almost identical sets of values as 
input. The major practical difference is one of efficiency. The json 
data type stores an exact copy of the input text, which processing 
functions must reparse on each execution; while jsonb data is stored in 
a decomposed binary format that makes it slightly slower to input due to 
added conversion overhead, but significantly faster to process, since no 
reparsing is needed. jsonb also supports indexing, which can be a 
significant advantage."


> 
> Pros and Cons for using JSON ?

Pros

You get the warm fuzzy feeling of using NoSQL in a SQL database

Cons

You get the task of basically unfolding a host of embedded databases.

> 
> Thanks for any help !
> 


-- 
Adrian Klaver
adrian.klaver@aklaver.com





view thread (4+ messages)  latest in thread

Message-ID: <d179aa08-0c6f-7890-0a8e-610169bbbef1@aklaver.com>
Permalink:  ../d179aa08-0c6f-7890-0a8e-610169bbbef1@aklaver.com/
Also on:    postgresql.org/message-id/d179aa08-0c6f-7890-0a8e-610169bbbef1@aklaver.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-general@postgresql.org
  Cc: adrian.klaver@aklaver.com, davegauthierpg@gmail.com
  Subject: Re: How is JSON stored in PG ?
  In-Reply-To: <d179aa08-0c6f-7890-0a8e-610169bbbef1@aklaver.com>
  References: <CAMBRECD14zp50Ex3E8L2MtZPHGAsSVX3zDo57=e_ov7fM8ONSA@mail.gmail.com> <d179aa08-0c6f-7890-0a8e-610169bbbef1@aklaver.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox