pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Andrew Gierth <andrew@tao11.riddles.org.uk>
To: pgsql-general@lists.postgresql.org
Subject: Re: JSON vs. JSONB storage size
Date: Fri, 11 Oct 2019 15:32:26 +0100
Message-ID: <87v9svxtzi.fsf@news-spur.riddles.org.uk> (raw)
In-Reply-To: <2bf3fe6a-394b-73bb-12a4-64a12f5c246f@gmx.net>
References: <2bf3fe6a-394b-73bb-12a4-64a12f5c246f@gmx.net>

>>>>> "Thomas" == Thomas Kellerer <spam_eater@gmx.net> writes:

 Thomas> The table size with jsonb was bigger in general, but the one
 Thomas> with the "integer" value was even bigger than the one with the
 Thomas> "string" storage.

jsonb stores numeric values as "numeric", not as integers or floats, so
the storage needed will depend on the number of decimal digits.

The size results you're seeing are mainly the consequence of the fact
that jsonb stores the whole Numeric datum, varlena header included (and
without packing the header), so there's an extra 4 bytes you might not
have accounted for: 1234567890 is three numeric "digits" (2 bytes each)
plus a 2 byte numeric header (for weight/scale/sign) plus the 4 byte
varlena header, for 12 bytes total, whereas "1234567890" takes only 10
(since the length is encoded in the jsonb value offsets). Furthermore,
there may be up to 3 padding bytes before the numeric value.

I think in your test, the extra 3 bytes is pushing the size of a single
row up to the next multiple of MAXALIGN, so you're getting slightly
fewer rows per page. I don't know what Windows is doing, but on my
system (freebsd amd64) I get 136 rows/page vs. 120 rows/page, which
would make a million rows take 57MB or 65MB. (Your use of
pg_total_relation_size is including the pkey index, which confuses the
results a bit.)

-- 
Andrew (irc:RhodiumToad)





view thread (4+ messages)

Message-ID: <87v9svxtzi.fsf@news-spur.riddles.org.uk>
Permalink:  ../87v9svxtzi.fsf@news-spur.riddles.org.uk/
Also on:    postgresql.org/message-id/87v9svxtzi.fsf@news-spur.riddles.org.uk

 · 

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: andrew@tao11.riddles.org.uk
  Subject: Re: JSON vs. JSONB storage size
  In-Reply-To: <87v9svxtzi.fsf@news-spur.riddles.org.uk>
  References: <2bf3fe6a-394b-73bb-12a4-64a12f5c246f@gmx.net> <87v9svxtzi.fsf@news-spur.riddles.org.uk>

* 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