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 1jb33m-0002hI-3P for pgsql-general@arkaria.postgresql.org; Tue, 19 May 2020 14:17:34 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1jb33F-0007LC-KL for pgsql-general@arkaria.postgresql.org; Tue, 19 May 2020 14:17:01 +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 1jb33F-0007L3-CZ for pgsql-general@lists.postgresql.org; Tue, 19 May 2020 14:17:01 +0000 Received: from tamriel.snowman.net ([96.255.250.162]) by magus.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1jb33A-00053b-AO for pgsql-general@lists.postgresql.org; Tue, 19 May 2020 14:17:00 +0000 Received: by tamriel.snowman.net (Postfix, from userid 1000) id 4EEF25F798; Tue, 19 May 2020 10:16:55 -0400 (EDT) Date: Tue, 19 May 2020 10:16:55 -0400 From: Stephen Frost To: Laurenz Albe Cc: Albrecht =?iso-8859-1?Q?Dre=DF?= , pgsql-general@lists.postgresql.org Subject: Re: Unique index on hash of jsonb value - correct solution? Message-ID: <20200519141655.GL13712@tamriel.snowman.net> References: <3dde4e83382ccd371216003f5d486ef49bc3d08c.camel@cybertec.at> MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha512; protocol="application/pgp-signature"; boundary="WHxjD7a0qZTXW2sV" Content-Disposition: inline In-Reply-To: <3dde4e83382ccd371216003f5d486ef49bc3d08c.camel@cybertec.at> User-Agent: Mutt/1.5.24 (2015-08-30) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk --WHxjD7a0qZTXW2sV Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: quoted-printable Greetings, * Laurenz Albe (laurenz.albe@cybertec.at) wrote: > On Mon, 2020-05-18 at 18:43 +0200, Albrecht Dre=C3=9F wrote: > > in a database I have a table with a text, a jsonb and a bytea column, w= hich > > together shall be unique, like: > >=20 > > > > Column | Type | Collation | Nullable | Default > > ---------+--------+-----------+----------+-----------------------------= -------- > > id | bigint | | not null | nextval('demotab_id_seq'::r= egclass) > > textval | text | | not null | > > jsonval | jsonb | | not null | > > blobval | bytea | | not null | > > Indexes: > > "demo_idx" UNIQUE, btree (textval, jsonval, blobval) > > > >=20 > > This seems to work just fine in most cases, but adding a bigger json va= lue (the text > > and the bytea columns are always short) results in a =E2=80=9Cindex row= size 2840 exceeds > > maximum 2712 for index "=E2=80=A6"=E2=80=9D error. Following the hint = in the error message, > > I replaced the index by > >=20 > > > > Indexes: > > "demo_idx" UNIQUE, btree (textval, md5(jsonval::text), blobval) > > > >=20 > > which seems to solve the issue. That's only going to work up to a certain size for that text and blob value too, of course.. This is looking like it might be some kind of KV store which is generally discouraged. > > My question: is this approach (a) correct and (b) still safe if the ite= ms in the > > jsonb (always a dict in my case) are re-ordered? I tested a few cases,= and trying > > to insert something like e.g. '{"a":1,"b":2}' and '{"b":2,"a":1}' actua= lly does > > produce the same hash (i.e. the 2nd insert attempt is rejected due to t= he index), > > but is this guaranteed by design for every case? Or is there a better = solution > > for this use case? >=20 > "jsonb" uses an internal binary representation that reorders the attribut= es in > a deterministic fashin, so yes, that is guaranteed. Sure- but the md5() isn't going to run on the jsonb data directly, it'll run on the text representation that's returned, and I'm not at all convinced that the project has agreed to make that text form always be canonical and identical forever, including across major version upgrades.. Further, there are some other cases to consider- such as: do you think that 0.00001230 is different from 0.0000123? Since we'll store numbers in jsonb as numeric, we preserve trailing franctional zeroes. eg: select md5(c1::text), md5(c2::text), c1, c2 from (select '{"reading": 1.230= e-5}'::jsonb, '{"reading": 1.23e-5}'::jsonb) as x(c1,c2); md5 | md5 | = c1 | c2 =20 ----------------------------------+----------------------------------+-----= --------------------+------------------------ d793380db9196092889ccdb4c84f2bd4 | 4fb5efd00c7a0f0e0e6f6375b8a834d5 | {"re= ading": 0.00001230} | {"reading": 0.0000123} (1 row) You might wonder what PG normally thinks returns when comparing those numbers, but that's easy to see: select '1.230e-5'::numeric =3D '1.23e-5'::numeric; ?column?=20 ---------- t (1 row) > I would use an "md5" hash for the "bytea" column as well to keep the inde= x smaller. This goes to the point above that you might want to include the other columns in the hash, or hash them independently if needed, to avoid hitting the max index row size. > There can be collisions with an md5 hash, so it is possible for duplicate= s to > creep in. Besides, if you have an auto-generated "bigint" column, you sh= ould > make that the primary key. Perhaps then the uniqueness condition is no l= onger > necessary. The issue isn't just with the md5 hash, as illustrated above. Having an alternative column that can be used as a primary key certainly does *not* remove the general need to ensure that these columns are unique, if that's what the data model calls for. Overall, if what you need is uniqueness here, I'd strongly reconsider the choice to use jsonb to store this data and instead see if you can break the data out into proper columns with a proper unique constraint across them (or across some hash of the combination of them that's done in a way that is clear and unambiguous). Thanks, Stephen --WHxjD7a0qZTXW2sV Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- Version: GnuPG v1 iQIcBAEBCgAGBQJew+pWAAoJEO1sijiDR2RVn6YP/j7n1E9tMg+TnByIGDg7i6PP CyMAdEeRV+MFTfnlUZBEuVuEKaJEwXUeH2/rs8DD1eiRUBTOh9P7VyvERG0JmYNn P/Dz531ejnAuIIUHglR/LXb3z17T8DK6JBa4dLUWcYz/coIm4OUpZA678axs8S4Y neynT/Rc9OW0PH35M37hSWWjceinFKNAj0y99w5TsD/yifOnCqWEkTdA+TZfW7H1 miT3yUkrl8LH4B9m90jv9Q7fQBG7iDAFZM3zwUX6JohH6ExwD5ZmvsONtcw8bacD 3TzRsZaRmE0GaOvGHhe/Kc0OVMS5vI9KkOmyP50F4A+eLWdFYu6zPX+SzWReUNl5 BqMKME7AXRbLY0LS+wGIs43K2pJ1KW+Z+7HO6keIn+ye0nMKNCVI5dGRu0I0hisG Uj77zv8Ge9u0N0wbySusZC6UPbTwRzwhVrimCbJLavfcYVwfehQDC3T0JSojXQ/a 61egUFhqeM+fPBL1rNF+1hVTw4X1KacCmHYH92HkVqUtV7OsznURBhjd7RwSH6YG SLTI6rYdHx8uTxp7sMPB7y2JOxbwEmoBf9LSlXT0XP5FhnwHEAH7VvyoeKDHBTj0 ZDyukqDCzkCXL2XuaFLb5vkA47DoUH2xYbQwgjbl1t9L18UHP/N8bu1Me0APZJ5Z obh6oc+XLD/jUOpavCfa =9uQM -----END PGP SIGNATURE----- --WHxjD7a0qZTXW2sV--