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 1jfiaM-0001or-1T for pgsql-general@arkaria.postgresql.org; Mon, 01 Jun 2020 11:26:30 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1jfiaJ-0002jC-NM for pgsql-general@arkaria.postgresql.org; Mon, 01 Jun 2020 11:26:27 +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 1jfiXB-0001QP-Dq for pgsql-general@lists.postgresql.org; Mon, 01 Jun 2020 11:23:13 +0000 Received: from hosted.mailcow.de ([2a00:f820:417::202]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1jfiX8-0004K8-Dn for pgsql-general@postgresql.org; Mon, 01 Jun 2020 11:23:12 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=beroun.io; s=default; t=1591010587; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to; bh=XH/Evs+J7R+omQDNesz80+pawuobedEUsD3YWfxrouQ=; b=jaSXauQlw96SWffzah8zvflofs6EC9jPTh1EgkU7G/S78D9DHr43tm/ctDAo0qJrn+/7GO 1b1QyxLUR+kBt5O7AbDU/TH2uBp9yft54xQQLL6q4cqMRbePd03JCYcrul82IV30P8Rwf8 EJ9XJL2yobMolTxFDunZS+T+EJx+MBA= Received: from [127.0.0.1] (localhost [127.0.0.1]) by localhost (Mailerdaemon) with ESMTPA id A5F625C00C8; Mon, 1 Jun 2020 13:23:07 +0200 (CEST) From: "Bernhard Beroun" In-Reply-To: Content-Type: multipart/alternative; boundary="----=_=-_OpenGroupware_org_NGMime-26739-1591010587.505553-0------" Date: Mon, 01 Jun 2020 13:23:07 +0200 Cc: "pgsql-general" To: "Julien Rouhaud" MIME-Version: 1.0 Message-ID: <6873-5ed4e500-1-1946ff60@82537409> Subject: =?utf-8?q?Re=3A?= Vanishing unique constraint List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk ------=_=-_OpenGroupware_org_NGMime-26739-1591010587.505553-0------ Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Content-Length: 2900 Hello, Am Montag, Juni 01, 2020 12:56 CEST, schrieb Julien Rouhaud : =C2=A0Hello, On Mon, Jun 1, 2020 at 11:15 AM Bernhard Beroun wr= ote: > > Hello, > > I am experiencing a strange thing on my production database server, w= hich I can't explain. > > On my production database server, there is a table called "label=5Fsu= ggestion" which has a unique constraint on the "name" column. If I do a= "\d+ label=5Fsuggestion" I can see, that the unique constraint shows u= p with > > "label=5Fsuggestion=5Fname=5Funique" UNIQUE CONSTRAINT, btree (name) > > in the output. But when I execute the following query > > SELECT name FROM label=5Fsuggestion GROUP BY name HAVING COUNT(name) = > 1 > > I can see that there are actually entries with a duplicate name in th= e database, which makes me believe that the unique constraint isn't the= re at all. (or at least it's not enforced) It looks like the underlying index is corrupted. Did you have any issue on that server? If the datatype is collatable, another possibility would be that the underlying glibc version (or the equivalent on your system) was upgraded to a version with different ordering for your collation. Recently, glibc 2.28 is a quite likely scenario, see for instance https://postgresql.verite.pro/blog/2018/08/27/glibc-upgrade.html, but it could also be some bug, possibly in earlier version if you updated postgres since the initial install. > Next, I dumped the complete production database (via pg=5Fdumpall) an= d applied the dump on my local machine. I did again a "\d+ label=5Fsugg= estion" and now the unique constraint doesn't show up anymore. There's probably an error displayed during the restore. The constraint are restored after the data, so there's no way that the constraint can be restored if you have duplicated values. You should definitely find out how you ended up in this situation and fix the root cause before trying to manually clean up the data. you are spot on with your failure analysis. I initially had PostgreSQL = 9.6 running on Debian Stretch (glibc < 2.28) and then migrated to Debia= n Buster (glibc 2.28) a few months ago. There's probably an error displayed during the restore. The constraint are restored after the data, so there's no way that the constraint can be restored if you have duplicated values.You are absolutely right, the= re is indeed an error - completely missed that! Many thanks for your help, Julien - you really helped me with this one.= Very much appreciated! Is there something special I need to consider when cleaning up the dupl= icated values? My current plan would be to run the migrate script that = I've written (which cleans up the mess and get rids of the duplicate va= lues) and then reindex with "REINDEX database ". Is that right or am I missing something here? Thanks, Bernhard =C2=A0 ------=_=-_OpenGroupware_org_NGMime-26739-1591010587.505553-0------ Content-Type: text/html; charset=utf-8 Content-Transfer-Encoding: quoted-printable Content-Length: 3472 Hello,

Am Montag, Juni 01, 2020 12:56 CEST, schrieb Ju= lien Rouhaud <rjuju123@gmail.com>:
 
Hello,

On Mon, Jun 1, 2020 at 11:15 AM Ber= nhard Beroun <bernhard@beroun.io> wrote:
>
> Hell= o,
>
> I am experiencing a strange thing on my producti= on database server, which I can't explain.
>
> On my pr= oduction database server, there is a table called "label=5Fsuggestion" = which has a unique constraint on the "name" column. If I do a "\d+ labe= l=5Fsuggestion" I can see, that the unique constraint shows up with
>
> "label=5Fsuggestion=5Fname=5Funique" UNIQUE CONSTRAINT= , btree (name)
>
> in the output. But when I execute th= e following query
>
> SELECT name FROM label=5Fsuggesti= on GROUP BY name HAVING COUNT(name) > 1
>
> I can se= e that there are actually entries with a duplicate name in the database= , which makes me believe that the unique constraint isn't there at all.= (or at least it's not enforced)

It looks like the underlyin= g index is corrupted. Did you have any
issue on that server? If th= e datatype is collatable, another
possibility would be that the un= derlying glibc version (or the
equivalent on your system) was upgr= aded to a version with different
ordering for your collation. Rece= ntly, glibc 2.28 is a quite likely
scenario, see for instance
https://postgresql.verite.pro/blog/2018/08/27/glibc-upgrade.html, but<= br />it could also be some bug, possibly in earlier version if you upda= ted
postgres since the initial install.

> Next, I du= mped the complete production database (via pg=5Fdumpall) and applied th= e dump on my local machine. I did again a "\d+ label=5Fsuggestion" and = now the unique constraint doesn't show up anymore.

There's p= robably an error displayed during the restore. The constraint
are = restored after the data, so there's no way that the constraint can
be restored if you have duplicated values.

You should defin= itely find out how you ended up in this situation and
fix the root= cause before trying to manually clean up the data.

y= ou are spot on with your failure analysis. I initially had PostgreSQL 9= .6 running on Debian Stretch (glibc < 2.28) and then migrated to Deb= ian Buster (glibc 2.28) a few months ago.

There's probabl= y an error displayed during the restore. The constraint
are restor= ed after the data, so there's no way that the constraint can
be re= stored if you have duplicated values.

You are absolutel= y right, there is indeed an error - completely missed that!

= Many thanks for your help, Julien - you really helped me with this one.= Very much appreciated!

Is there something special I need to= consider when cleaning up the duplicated values? My current plan would= be to run the migrate script that I've written (which cleans up the me= ss and get rids of the duplicate values) and then reindex with "REINDEX= database <mydatabase>".

Is that right or am I missing= something here?

Thanks,
Bernhard
  ------=_=-_OpenGroupware_org_NGMime-26739-1591010587.505553-0--------