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 1is8a1-0006u0-IK for pgsql-general@arkaria.postgresql.org; Thu, 16 Jan 2020 17:05:13 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1is8a0-0005qn-Cl for pgsql-general@arkaria.postgresql.org; Thu, 16 Jan 2020 17:05:12 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1is8a0-0005ps-2h for pgsql-general@lists.postgresql.org; Thu, 16 Jan 2020 17:05:12 +0000 Received: from out4-smtp.messagingengine.com ([66.111.4.28]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1is8Zx-0006Y2-6L for pgsql-general@lists.postgresql.org; Thu, 16 Jan 2020 17:05:11 +0000 Received: from compute6.internal (compute6.nyi.internal [10.202.2.46]) by mailout.nyi.internal (Postfix) with ESMTP id DE68C21FDF; Thu, 16 Jan 2020 12:05:07 -0500 (EST) Received: from mailfrontend1 ([10.202.2.162]) by compute6.internal (MEProxy); Thu, 16 Jan 2020 12:05:07 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=aklaver.com; h= subject:to:references:from:message-id:date:mime-version :in-reply-to:content-type:content-transfer-encoding; s=fm2; bh=o TAMX11NTiy4AsTTtVeVxWRK8ajw2RHNj/ImChDzG/A=; b=TTnZDh49wDG06tqMP 5SK5MQ5/fX7crA0IALsUTnbyLbZ1+EsjUBFNgWNDV+JjSZ0dFA9ELTQr/4KY7Dm3 aQLDOtaXMGPxbvVnKlJ+Fvd448bege4w22ZK0Kz/ADtHx5qsb3Fz4uTeekez+p7D ML4jgo2X05zWMPpTaOFpUi84EPm9TpKJuB8gC1JqQnQulBWXMql3RyOe0QRbXGrW CT7r6oUkBg7rRc0gELZ5zeKmt0fyL31IIcDop29mzR8UA20Pfje2LHx3d8ub2Qiq MG5qLQdEgLTvqbXBZ9GOEDqbPPR5Obl6syCZTPFp9gYjH9+RRR3pSjmDRC0gTuSX RZwLA== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=content-transfer-encoding:content-type :date:from:in-reply-to:message-id:mime-version:references :subject:to:x-me-proxy:x-me-proxy:x-me-sender:x-me-sender :x-sasl-enc; s=fm1; bh=oTAMX11NTiy4AsTTtVeVxWRK8ajw2RHNj/ImChDzG /A=; b=lIc7M33GQ41wmCxWks6R+JGWPA1Iy37hkz64b7t4PmUXKqdNkSI+oIiRW XPTUY/iSrgz7SjB8NJGlwcqDzfMR/vOpMAOzRrYOh5kUI3yJ1ep+N1tDlfx/LAoC VSufbbmz4dxlFQ6uUU3YuLG23GYo3QsDPhzKpcefVRGeLX6l0eghq03mMWzK4K/U uNsK8Xqs2On/Xv0WF8B8Qk0uqFLf/7g+gl/ncEH7Sq/CtGM5/D+/Zz1Pe1jRxkXe cPYNi/vg1FrVY1yZqktAgnDcUL0iELfV5qssz6vE06elpvrlQp8WLHGtknpv1the o7Qmqc42fphURTnCOtQbtxLDES/ag== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedugedrtdehgdelgecutefuodetggdotefrodftvf curfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfghnecu uegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmdenuc fjughrpefuvfhfhffkffgfgggjtgfgsehtkeertddtfeejnecuhfhrohhmpeetughrihgr nhcumfhlrghvvghruceorggurhhirghnrdhklhgrvhgvrhesrghklhgrvhgvrhdrtghomh eqnecuffhomhgrihhnpehgihhthhhusgdrtghomhdpuggrrhhkfhgrshgvlhdrnhgvthdp mhgrthhrihigrdhorhhgnecukfhppeejuddrvdduvddruddvledrudelfeenucfrrghrrg hmpehmrghilhhfrhhomheprggurhhirghnrdhklhgrvhgvrhesrghklhgrvhgvrhdrtgho mhenucevlhhushhtvghrufhiiigvpedt X-ME-Proxy: Received: from [192.168.1.10] (unknown [71.212.129.193]) by mail.messagingengine.com (Postfix) with ESMTPA id 3D1A380060; Thu, 16 Jan 2020 12:05:07 -0500 (EST) Subject: Re: Inexplicable duplicate rows with unique constraint To: Richard van der Hoff , pgsql-general@lists.postgresql.org References: <288488b7-d3b6-905b-3706-43f7a3c7bc52@matrix.org> From: Adrian Klaver Message-ID: <16bd51ab-69ef-04f9-d47a-3e692b1ff595@aklaver.com> Date: Thu, 16 Jan 2020 09:05:06 -0800 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.3.1 MIME-Version: 1.0 In-Reply-To: <288488b7-d3b6-905b-3706-43f7a3c7bc52@matrix.org> Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On 1/16/20 8:50 AM, Richard van der Hoff wrote: > I'm trying to track down the cause of some duplicate rows in a table > which I would expect to be impossible due to a unique constraint. I'm > hoping that somebody here will be able to suggest something I might have > missed. > > The problem relates to a bug filed against our application > (https://github.com/matrix-org/synapse/issues/6696). At first I put this > down to random data corruption on a single user's postgres instance, but > I've now seen three separate reports in as many days and am wondering if > there is more to it. > > We have a table whose schema is as follows: > > synapse=# \d current_state_events > Table "public.current_state_events" >    Column   | Type | Modifiers > ------------+------+----------- >  event_id   | text | not null >  room_id    | text | not null >  type       | text | not null >  state_key  | text | not null >  membership | text | > Indexes: >     "current_state_events_event_id_key" UNIQUE CONSTRAINT, btree > (event_id) >     "current_state_events_room_id_type_state_key_key" UNIQUE > CONSTRAINT, btree (room_id, type, state_key) >     "current_state_events_member_index" btree (state_key) WHERE type = > 'm.room.member'::text > > Despite the presence of the > current_state_events_room_id_type_state_key_key constraint, several > users have reported seeing errors which suggest that their tables have > duplicate rows for the same (room_id, type, state_key) triplet and > indeed querying confirms that to be the case: > > synapse=> select count(*), room_id, type, state_key from > current_state_events group by 2,3,4 order by count(*) DESC LIMIT 2; >  count |              room_id              |     type      |   state_key > -------+-----------------------------------+---------------+------------------------------------- > >      3 | !ueLfVrSWYGMnFnoCbT:darkfasel.net | m.room.member | > @irc_ebi_:darkfasel.net >      3 | !HwocBmCtBcHQhILtYQ:matrix.org    | m.room.member | > @freenode_AlmarShenwan_:matrix.org > (2 rows) > I'm assuming the above are obfuscated? > > Further investigation suggests that these are genuinely separate rows > rather than duplicate entries in an index. If you use length() on the values are they the same? > > The index appears to consider itself valid: > > synapse=> select i.* from pg_class c join pg_index i on > i.indexrelid=c.oid  where > relname='current_state_events_room_id_type_state_key_key'; >  indexrelid | indrelid | indnatts | indisunique | indisprimary | > indisexclusion | indimmediate | indisclustered | indisvalid | > indcheckxmin | indisready | indislive | indisreplident | indkey | > indcollation |    indclass    | indoption | indexprs | indpred > ------------+----------+----------+-------------+--------------+----------------+--------------+----------------+------------+--------------+------------+-----------+----------------+--------+--------------+----------------+-----------+----------+--------- > >       17023 |    16456 |        3 | t           | f            | f >         | t            | f              | t          | f            | t >          | t         | f              | 2 3 4  | 100 100 100  | 3126 > 3126 3126 | 0 0 0     |          | > (1 row) > > So, question: what could we be doing wrong to get ourselves into this > situation? > > Some other datapoints which may be relevant: >  * this has been reported by one user on postgres 9.6.15 and one on > 10.10, though it's hard to be certain of the version that was running > when the duplication occurred >  * the constraint is added when the table is first created (before any > data is added) >  * At least one user reports that he has recently migrated his database > from one server to another via a `pg_dump -C` and later piping into psql. > > -- Adrian Klaver adrian.klaver@aklaver.com