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 1javNI-0003Cl-B9 for pgsql-general@arkaria.postgresql.org; Tue, 19 May 2020 06:05:12 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1javNG-0007PX-CK for pgsql-general@arkaria.postgresql.org; Tue, 19 May 2020 06:05:10 +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 1javNG-0007PQ-49 for pgsql-general@lists.postgresql.org; Tue, 19 May 2020 06:05:10 +0000 Received: from mail-wr1-x442.google.com ([2a00:1450:4864:20::442]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1javN9-0000bt-9n for pgsql-general@lists.postgresql.org; Tue, 19 May 2020 06:05:09 +0000 Received: by mail-wr1-x442.google.com with SMTP id l18so14415455wrn.6 for ; Mon, 18 May 2020 23:05:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cybertec-at.20150623.gappssmtp.com; s=20150623; h=message-id:subject:from:to:date:in-reply-to:references:user-agent :mime-version:content-transfer-encoding; bh=bXfgFOmzmTGAHIYtNSjs15a1rpTCW+U6v9bQJvnazU8=; b=OGGPoJVPi/OvRc0lyq3eZny8PKCpoKemiu9mlQj24t5inGQYUTrXY/0adkV+4JQk3u WhQZRMvFlYiwAVIzlW97dU5Qm716v8pHDh+YIK5q3RjK1noh3PVlA5VA9864boU6R8VB s9WOSSXZHYK90GaFvnxR0EuDEGNmLnPJ/QdPEUSEUJOE4X8kEvCfJtSmWRJ/11AqXjOc JBv9yC4Yh/Tg9s7o2V/HBg/mpNlENnvDlziUHGjBIN7LbyJPhaC5w5jCVKJF6Bz5ZPRq 0Ou0IR6VxdvWkwgK341Uuf/sktiW8dyxUdAXsS/SuSW6FAI1MmYi0aFjyG8TyzCA6uen +bRw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:message-id:subject:from:to:date:in-reply-to :references:user-agent:mime-version:content-transfer-encoding; bh=bXfgFOmzmTGAHIYtNSjs15a1rpTCW+U6v9bQJvnazU8=; b=q/ogFDUEs8IoZooS6HKBYu5wccd51yfomg+1hloRNFbF6Xj5oStpiCHixP3e4hE0qv Q0U1hoTVAoVndg27i/z57ubRKNBt1GGKLcZMaNj1rqseeAg+loMYrwhuQ3xMBNJCdFOA UJZ+t2YMiQlvaBJSlWWD0RBapW+OXgNGKNWipZa2QLtvb9Pxaba+qPa8VM5Wjl4YMDn6 uVQSHPWIwEdcxi5xtNsB9PtJt+/YfyNo4EpZTZsuItUQ/F2z9veTc9JbLMOYP4DXFHj1 n84YVBksmS9Gjb+VC0xY+EuX2a8CF+JpvK4aHJQEKOW0x3NOEBHFiuLusUb193dxssf8 P50A== X-Gm-Message-State: AOAM531qL0/Vy/sm2i8u9l01coHSTLGth3mMflFu6tibv3wws+v4xZjn 79Fpz8VIdTi2OeP39vgUy0ysIg== X-Google-Smtp-Source: ABdhPJwEyCpPX0g/DraBQ12XQFIkK4MUPNn4Im0HEZmjWc9NgvZw6SntobAZrYxDXHHmWR7FuLkmBA== X-Received: by 2002:a5d:6b83:: with SMTP id n3mr25537353wrx.213.1589868302098; Mon, 18 May 2020 23:05:02 -0700 (PDT) Received: from localhost.localdomain (217-149-174-138.nat.highway.telekom.at. [217.149.174.138]) by smtp.gmail.com with ESMTPSA id h137sm3181333wme.0.2020.05.18.23.05.01 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 18 May 2020 23:05:01 -0700 (PDT) Message-ID: <3dde4e83382ccd371216003f5d486ef49bc3d08c.camel@cybertec.at> Subject: Re: Unique index on hash of jsonb value - correct solution? From: Laurenz Albe To: Albrecht =?ISO-8859-1?Q?Dre=DF?= , pgsql-general@lists.postgresql.org Date: Tue, 19 May 2020 08:05:00 +0200 In-Reply-To: References: Content-Type: text/plain; charset="UTF-8" User-Agent: Evolution 3.32.5 (3.32.5-1.fc30) MIME-Version: 1.0 Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On Mon, 2020-05-18 at 18:43 +0200, Albrecht Dreß wrote: > in a database I have a table with a text, a jsonb and a bytea column, which > together shall be unique, like: > > > Column | Type | Collation | Nullable | Default > ---------+--------+-----------+----------+------------------------------------- > id | bigint | | not null | nextval('demotab_id_seq'::regclass) > textval | text | | not null | > jsonval | jsonb | | not null | > blobval | bytea | | not null | > Indexes: > "demo_idx" UNIQUE, btree (textval, jsonval, blobval) > > > This seems to work just fine in most cases, but adding a bigger json value (the text > and the bytea columns are always short) results in a “index row size 2840 exceeds > maximum 2712 for index "…"” error. Following the hint in the error message, > I replaced the index by > > > Indexes: > "demo_idx" UNIQUE, btree (textval, md5(jsonval::text), blobval) > > > which seems to solve the issue. > > My question: is this approach (a) correct and (b) still safe if the items 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}' actually does > produce the same hash (i.e. the 2nd insert attempt is rejected due to the index), > but is this guaranteed by design for every case? Or is there a better solution > for this use case? "jsonb" uses an internal binary representation that reorders the attributes in a deterministic fashin, so yes, that is guaranteed. I would use an "md5" hash for the "bytea" column as well to keep the index smaller. There can be collisions with an md5 hash, so it is possible for duplicates to creep in. Besides, if you have an auto-generated "bigint" column, you should make that the primary key. Perhaps then the uniqueness condition is no longer necessary. Yours, Laurenz Albe -- Cybertec | https://www.cybertec-postgresql.com