Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1lWgTN-0006tm-Sr for pgsql-general@arkaria.postgresql.org; Wed, 14 Apr 2021 14:26:29 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1lWgTM-0002CG-2E for pgsql-general@arkaria.postgresql.org; Wed, 14 Apr 2021 14:26:28 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1lWgTL-0002C8-Qr for pgsql-general@lists.postgresql.org; Wed, 14 Apr 2021 14:26:27 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1lWgTJ-0007O5-Qu for pgsql-general@postgresql.org; Wed, 14 Apr 2021 14:26:27 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.15.2/8.15.2) with ESMTP id 13EEQNWK3488586; Wed, 14 Apr 2021 10:26:23 -0400 From: Tom Lane To: Dmitry Dolgov <9erthalion6@gmail.com> cc: Mitar , pgsql-general@postgresql.org Subject: Re: Why is writing JSONB faster than just JSON? In-reply-to: <20210414084845.rkbe525twb4jr4fw@localhost> References: <20210414084845.rkbe525twb4jr4fw@localhost> Comments: In-reply-to Dmitry Dolgov <9erthalion6@gmail.com> message dated "Wed, 14 Apr 2021 10:48:45 +0200" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <3488584.1618410383.1@sss.pgh.pa.us> Content-Transfer-Encoding: quoted-printable Date: Wed, 14 Apr 2021 10:26:23 -0400 Message-ID: <3488585.1618410383@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Dmitry Dolgov <9erthalion6@gmail.com> writes: >> On Tue, Apr 13, 2021 at 11:38:04PM -0700, Mitar wrote: >> ... Namely, it looks like writing into a jsonb typed >> column is 30% faster than writing into a json typed column. Why is >> that? Does not jsonb require parsing of JSON and conversion? That >> should be slower than just storing a blob as-is? > * Parsing is happening in both cases, for json it's done for validation > purposes. Right. > * Compression of json data is actually dominates the runtime load for la= rge > json objects, making other factors less visible and reducing differenc= e in > size (jsonb also should be a bit bigger, that's why I would think it w= ould be > slower). > * At the same time perf shows that json compression spends a bit more ti= me in > pglz_find_match (mostly around accessing and checking history entries)= , which > is compression optimization via history table. So probably due to diff= erences > in data layout this optimization works slightly worse for plain json t= han for > jsonb? Interesting. I recall that we made some last-minute changes in the JSONB physical representation after finding that the original choices resulted in sucky compression behavior. But I think we were only looking at the resultant compressed size, not time-to-compress. My own guess about this, without having tried to reproduce it, is that JSONB might end up physically smaller than JSON, resulting in less work to push the toasted datum out to disk. This'd depend a lot on your formatting habits for JSON, of course. But in any case, it'd be worth comparing pg_column_size() results to see what's up with that. regards, tom lane