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 1is8Lr-0006Lv-HK for pgsql-general@arkaria.postgresql.org; Thu, 16 Jan 2020 16:50:35 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1is8Lq-0004Kq-95 for pgsql-general@arkaria.postgresql.org; Thu, 16 Jan 2020 16:50:34 +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 1is8Lp-0004Kg-Qc for pgsql-general@lists.postgresql.org; Thu, 16 Jan 2020 16:50:34 +0000 Received: from polemos.matrix.org ([94.237.46.156]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1is8Lk-0001d3-4S for pgsql-general@lists.postgresql.org; Thu, 16 Jan 2020 16:50:32 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=matrix.org; s=polemos; h=Content-Transfer-Encoding:Content-Type:MIME-Version:Date: Message-ID:To:Subject:From:Sender:Reply-To:Cc:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: In-Reply-To:References:List-Id:List-Help:List-Unsubscribe:List-Subscribe: List-Post:List-Owner:List-Archive; bh=5K8FwHNSIB5In+1sTYXuGUZivatN7RYrsAtEArmxrlI=; b=15SHfC2aXc1v5Z/gpAX2b6N4tj zS27K0P9tBXFTc/xgrj6d8Tdxg+Ayua1M6cyxkNLnXqFS6PmiiOS9ELeaXj9922uevHNFdLjPsUWB ocFdWIUf9IhJCl9mInUqsAZCvgtV9YrX9RQZxf/F7zr/PYJrBbHMaM5A5s+EAkHuFnqRM9rS1xvuf /qP/6VIgT2L/XXbiN9gvIIwJnNvba/c6nr9fzOWx/iN51AZLTUvlWcfyuY6T4uasJxqHtGLIgcwEH qTFgRI8/cy9Me0V+YaLVDtnxeko8rnrGx3Uxv8Z7aQ17joLNv8ckGZonO+T2vQNrQ8ZnNdkJqW23R goF+msA2AR1ODMjKNAPIIz5l/gX/iof0Nj8hAAwfpVwikv4VCOOXMAIQSfFnRp8CvW8zdIICrzrte RJHOh4pPHSIi6J7ztIvOtVflRLJUvvkGUKxOv+CnrlJqzbZkMgsbSXjK5cUZ0g4Y0lYlYrpaqj0P7 XiTgvh5Yyx9uk8PxcCi37XMTq2S2P2u7gHuAbngotf+q833IwwkGPHZsgj/x6e8lxT6uijZ1LHD96 41K25CT3G5ssUyjJQI+Tw233UCQ9zf97WFTH5U31Mxsk9n/gKTpNy7MCmhrqEJG9ho+FfYoV5KRCT mFcQmMgjKOnxb8oydnhuufsjrzULoGKGXz3nSUEyc=; Received: from [2001:8b0:ba7a:e98f:2d18:c4bf:423e:ed6f] by polemos.matrix.org with esmtpsa (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.89) (envelope-from ) id 1is8Li-0006xa-5h for pgsql-general@lists.postgresql.org; Thu, 16 Jan 2020 16:50:26 +0000 From: Richard van der Hoff Subject: Inexplicable duplicate rows with unique constraint To: pgsql-general@lists.postgresql.org Message-ID: <288488b7-d3b6-905b-3706-43f7a3c7bc52@matrix.org> Date: Thu, 16 Jan 2020 16:50:25 +0000 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.2.2 MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 7bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk 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) Further investigation suggests that these are genuinely separate rows rather than duplicate entries in an index. 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.