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 1lX497-0002HT-Lw for pgsql-general@arkaria.postgresql.org; Thu, 15 Apr 2021 15:43:09 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1lX496-0006eu-E4 for pgsql-general@arkaria.postgresql.org; Thu, 15 Apr 2021 15:43:08 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from <9erthalion6@gmail.com>) id 1lX496-0006em-31 for pgsql-general@lists.postgresql.org; Thu, 15 Apr 2021 15:43:08 +0000 Received: from mail-ej1-x62c.google.com ([2a00:1450:4864:20::62c]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from <9erthalion6@gmail.com>) id 1lX48z-00032C-Fq for pgsql-general@postgresql.org; Thu, 15 Apr 2021 15:43:07 +0000 Received: by mail-ej1-x62c.google.com with SMTP id mh2so15999784ejb.8 for ; Thu, 15 Apr 2021 08:43:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to; bh=lwGIKmt/RInfR3hZxSQavAdLARM0WWXsws+dwFz/eQo=; b=eCyrm5NNIpQbOHRaSUHeVtiHYPr0FXiFlz9FUOy+5gC9oowxHoVF9AyQiGbm/He7ML WF8NQSHF3nejjFaxNKM6GYzjXTnUrZzZKWNfMcAQzsXkhjG30zMR2prtcgSe9l9g71gz NQ8gv57Ndd3RQU+vjsUr5in74tquUk2fTlBKDKU2KezD+5yTEvfMEmus5gXq4OkaAzCf QfZUKbBb7lMFnq3sDvmnlJo+6gqlvTykItXShZNZSLy4phdta5uuwWiESIHfK6qRH9hc FKOWAL3pp4nEwf2YB/K29ihbEU5IDHRzcHZ9MJtc4M3gEggII/6DcCByRpoIf4i+CbB+ lJeA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=lwGIKmt/RInfR3hZxSQavAdLARM0WWXsws+dwFz/eQo=; b=QbyxeDqMx89cjvL6njqLZQZwOmhQ3vy0eeCWkkM8Mzk1B+tRzh3Otm4vyLPMKlA50L VeD3Cfd2PMb/fwNhu4bri7bYiE+Mr/FhQjJsNLEwXvwp3gizxrXFkQJho41iU5nTIxvW KkgxEu4eze61biwBX7NQ3115hqnZVetPB1molAwwzAQlRbJklHxCOI/vo1Lu0SPIMdoq PzAjP4AqHc2edA5FjyiKg/dMrAm0FdrjCe/zc22ACMBg83GHFt2D0iGdOa403PPYbSLZ h7RXPxsNwRVdN3CvEQ36kY92RflMPBUwLKpQ14socbEiI0Pot2iLV2TLaOfHwcAmRuPQ 1BxA== X-Gm-Message-State: AOAM532z+LhGwNDBdoBKfoeg2AMpJtA0gCblKv++s8Y/3CTQUBUsqI5S oWtHSlhAFa+7JCV4sol5zW0= X-Google-Smtp-Source: ABdhPJzq5V2zhOZc98TgZTfu4loH9Y2EFUw9A9y+Ur2F8lqgQXlz6q4uePFXJXpi1fl6IoUqNRVFAQ== X-Received: by 2002:a17:906:b1d4:: with SMTP id bv20mr4143831ejb.46.1618501380148; Thu, 15 Apr 2021 08:43:00 -0700 (PDT) Received: from localhost (dslb-178-012-047-165.178.012.pools.vodafone-ip.de. [178.12.47.165]) by smtp.gmail.com with ESMTPSA id bh14sm2155394ejb.104.2021.04.15.08.42.58 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 15 Apr 2021 08:42:59 -0700 (PDT) Date: Thu, 15 Apr 2021 17:43:11 +0200 From: Dmitry Dolgov <9erthalion6@gmail.com> To: Tom Lane Cc: Mitar , pgsql-general@postgresql.org Subject: Re: Why is writing JSONB faster than just JSON? Message-ID: <20210415154311.t3meozbednmsfhxx@localhost> References: <20210414084845.rkbe525twb4jr4fw@localhost> <3488585.1618410383@sss.pgh.pa.us> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <3488585.1618410383@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk > On Wed, Apr 14, 2021 at 10:26:23AM -0400, Tom Lane wrote: > 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 large > > json objects, making other factors less visible and reducing difference in > > size (jsonb also should be a bit bigger, that's why I would think it would be > > slower). > > * At the same time perf shows that json compression spends a bit more time in > > pglz_find_match (mostly around accessing and checking history entries), which > > is compression optimization via history table. So probably due to differences > > in data layout this optimization works slightly worse for plain json than 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. Oh, of course I've missed that the input I was using was indeed formatted, without formatting both cases perform equally well and I can't reproduce the issue. Although if I understand correctly the original code in question doesn't actually do any formatting.