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 1pb4lU-00021b-LT for pgsql-admin@arkaria.postgresql.org; Sat, 11 Mar 2023 19:20:24 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1pb4lS-0005Zf-KT for pgsql-admin@arkaria.postgresql.org; Sat, 11 Mar 2023 19:20:22 +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 1pb4lS-0005ZW-9M for pgsql-admin@lists.postgresql.org; Sat, 11 Mar 2023 19:20:22 +0000 Received: from mout-u-204.mailbox.org ([2001:67c:2050:101:465::204]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1pb4lP-00089K-D4 for pgsql-admin@lists.postgresql.org; Sat, 11 Mar 2023 19:20:21 +0000 Received: from smtp2.mailbox.org (smtp2.mailbox.org [IPv6:2001:67c:2050:b231:465::2]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-384) server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by mout-u-204.mailbox.org (Postfix) with ESMTPS id 4PYt6P388Lz9sWT; Sat, 11 Mar 2023 20:20:13 +0100 (CET) Date: Sat, 11 Mar 2023 20:20:12 +0100 (CET) From: Erik Wienhold To: Alexey Murz Korepov , pgsql-admin@lists.postgresql.org Message-ID: <1378811615.328761.1678562412852@office.mailbox.org> In-Reply-To: References: Subject: Re: Ways to change a database collation with removing duplicates MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable X-Priority: 3 Importance: Normal X-Rspamd-Queue-Id: 4PYt6P388Lz9sWT List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk > On 11/03/2023 19:23 CET Alexey Murz Korepov wrote: > > Could anyone suggest to me the ways to change a database collation with > removing all the duplicates, caused by this change? Collations can only affect uniqueness if they are nondeterministic or if yo= u have functional indexes, e.g. using lower(text) for a case-insensitive uniq= ue index. Otherwise the collations only affect text ordering. You need to find all duplicates in the original database (or a copy) by usi= ng the target collation for comparisons[0]. Delete all duplicates, pg_dump th= at database and restore in database with new collation. > I have a pretty large database (around 500 Gb) that was created with > `en_US.UTF-8` collation, but the new version of the application=C2=A0requ= ires > that the collation should be strictly `C`. Both collations should be deterministic[1] which means they consider byte sequences when comparing strings for equality. What does pg_collation say? =09select * from pg_collation where collname in ('C', 'en_US.utf8') > I can successfully create a dump of the old database using `pgdump`. > > But when I'm importing the dump to the new database with `COLLATE=3DC`, I= see > a lot of errors on ALTER TABLE when creating primary keys, and the same - > for `CREATE INDEX` commands: Restore the dump in a new database with same collation en_US.UTF-8 to rule = out that the duplicates come from collation C and do not already exist in the original database. > ALTER TABLE > ERROR: could not create unique index "access_tokens_pkey" > DETAIL: Key (id)=3D(16734) is duplicated. Is column "id" an integer? If so then this looks like a corrupt index in t= he original database because collations should not affect indexes on non-text columns. > ERROR: could not create unique index "access_tokens_token_key" > DETAIL: Key (token)=3D(XXX) is duplicated. > CONTEXT: parallel worker > ERROR: could not create unique index "account_data_uniqueness" > DETAIL: Key (user_id, account_data_type)=3D(@username:XXX, im.vector.set= ting.breadcrumbs) is duplicated. > CREATE INDEX > ERROR: could not create unique index "e2e_cross_signing_keys_idx" > DETAIL: Key (user_id, keytype, stream_id)=3D(@-----------------:matrix.o= rg (http://matrix.org), master, 1606172) is duplicated. > ERROR: could not create unique index "e2e_cross_signing_keys_stream_idx" > DETAIL: Key (stream_id)=3D(1779009) is duplicated. > > So, could anyone give some advice on how to perform the collation change > with cleaning out all the duplicates? Thanks! [0] https://www.postgresql.org/docs/current/indexes-collations.html [1] https://www.postgresql.org/docs/current/collation.html#COLLATION-NONDET= ERMINISTIC -- Erik