Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1iItHZ-0000jb-4N for pgsql-general@arkaria.postgresql.org; Fri, 11 Oct 2019 11:40:29 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1iItHX-0002y5-Vq for pgsql-general@arkaria.postgresql.org; Fri, 11 Oct 2019 11:40:27 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1iItHX-0002xo-Lz for pgsql-general@lists.postgresql.org; Fri, 11 Oct 2019 11:40:27 +0000 Received: from mout.kundenserver.de ([217.72.192.74]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1iItHT-0003l9-JS for pgsql-general@lists.postgresql.org; Fri, 11 Oct 2019 11:40:25 +0000 Received: from [10.8.0.75] ([185.40.248.10]) by mrelayeu.kundenserver.de (mreue106 [212.227.15.145]) with ESMTPSA (Nemesis) id 1MfI21-1hrGN53CAN-00gllP for ; Fri, 11 Oct 2019 13:40:20 +0200 X-Mozilla-News-Host: news://news.gmane.org:119 To: pgsql-general@lists.postgresql.org From: Thomas Kellerer Subject: JSON vs. JSONB storage size Message-ID: <2bf3fe6a-394b-73bb-12a4-64a12f5c246f@gmx.net> Date: Fri, 11 Oct 2019 13:40:18 +0200 User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.23) Gecko/20090812 Thunderbird/2.0.0.23 Mnenhy/0.7.6.666 MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Language: en-GB Content-Transfer-Encoding: 7bit X-Provags-ID: V03:K1:2cwdnleTK6oEaRW8Mfax4g+kAk60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w+wD8tMiWhennF+q2aZ8YFFHqiUWXdzG7rJ6fvjEyWvWZdpoKJ4GhUq8RfUYIVkik5VZNBz M3DD2U8V+CA7gj/KG0NYVRDy5R3nJvIPzMF5h0zB0= List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk I recently stumbled over the presentation "How to Use JSON in MySQL Wrong" by Bill Karwin[1] While most of the indexing part simply doesn't apply to Postgres, I was curious about the statement that the data type of a json value inside the json matters as well (Slide 56) Apparently in MySQL storing {"a": 123456789} takes less space than {"a": '123456789'} So I tested that with Postgres both using json and jsonb - my expectation was, that this would be similar in Postgres as well. However, it turned out that for a json column there was no difference at all (both versions would show up the same with pg_total_relation_size()) The table size with jsonb was bigger in general, but the one with the "integer" value was even bigger than the one with the "string" storage. The following little test script: create table json_length_test1 (id serial primary key, d json); insert into json_length_test1 select i, jsonb_build_object('a', 1234567890) from generate_series(1,1e6) t(i); create table json_length_test2 (id serial primary key, d json); insert into json_length_test2 select i, jsonb_build_object('a', '1234567890') from generate_series(1,1e6) t(i); create table jsonb_length_test1 (id serial primary key, d jsonb); insert into jsonb_length_test1 select i, jsonb_build_object('a', 1234567890) from generate_series(1,1e6) t(i); create table jsonb_length_test2 (id serial primary key, d jsonb); insert into jsonb_length_test2 select i, jsonb_build_object('a', '1234567890') from generate_series(1,1e6) t(i); select 'json', pg_size_pretty(pg_total_relation_size('json_length_test1')) as json_int_size, pg_size_pretty(pg_total_relation_size('json_length_test2')) as json_text_size union all select 'jsonb', pg_size_pretty(pg_total_relation_size('jsonb_length_test1')) as json_int_size, pg_size_pretty(pg_total_relation_size('jsonb_length_test2')) as json_text_size Returns (Postgres 12, Windows 10) ?column? | json_int_size | json_text_size ---------+---------------+--------------- json | 71 MB | 71 MB jsonb | 87 MB | 79 MB I am a bit surprised by this (not because the jsonb sizes are generally bigger, but that the string value takes less space) Is this caused by the fact that a string value compresses better internally? Thomas [1] https://www.slideshare.net/billkarwin/how-to-use-json-in-mysql-wrong