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 1iIvy8-0006eq-Dv for pgsql-general@arkaria.postgresql.org; Fri, 11 Oct 2019 14:32:36 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1iIvy7-0007Cl-5v for pgsql-general@arkaria.postgresql.org; Fri, 11 Oct 2019 14:32:35 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1iIvy6-0007CW-Od for pgsql-general@lists.postgresql.org; Fri, 11 Oct 2019 14:32:34 +0000 Received: from lungold.riddles.org.uk ([82.68.208.19]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1iIvy3-0004ob-HO for pgsql-general@lists.postgresql.org; Fri, 11 Oct 2019 14:32:33 +0000 Received: from [192.168.127.1] (port=21878 helo=caithnard.riddles.org.uk) by lungold.riddles.org.uk with esmtp (Exim 4.92.3 (FreeBSD)) (envelope-from ) id 1iIvxz-0003mH-1o for pgsql-general@lists.postgresql.org; Fri, 11 Oct 2019 14:32:27 +0000 Received: from localhost ([127.0.0.1]:34181 helo=caithnard.riddles.org.uk) by caithnard.riddles.org.uk with esmtp (Exim 4.92.1 (FreeBSD)) (envelope-from ) id 1iIvxy-0008PH-Tg for pgsql-general@lists.postgresql.org; Fri, 11 Oct 2019 14:32:27 +0000 From: Andrew Gierth To: pgsql-general@lists.postgresql.org Subject: Re: JSON vs. JSONB storage size In-Reply-To: <2bf3fe6a-394b-73bb-12a4-64a12f5c246f@gmx.net> (Thomas Kellerer's message of "Fri, 11 Oct 2019 13:40:18 +0200") Message-ID: <87v9svxtzi.fsf@news-spur.riddles.org.uk> References: <2bf3fe6a-394b-73bb-12a4-64a12f5c246f@gmx.net> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/26.2 (berkeley-unix) Date: Fri, 11 Oct 2019 15:32:26 +0100 MIME-Version: 1.0 Content-Type: text/plain List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk >>>>> "Thomas" == Thomas Kellerer 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)