Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1rCmbL-00HE2y-H6 for pgsql-hackers@arkaria.postgresql.org; Mon, 11 Dec 2023 20:10:03 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1rCmbK-00Gepz-4H for pgsql-hackers@arkaria.postgresql.org; Mon, 11 Dec 2023 20:10:02 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1rCmbJ-00Gepq-PR for pgsql-hackers@lists.postgresql.org; Mon, 11 Dec 2023 20:10:01 +0000 Received: from mail1.dalibo.net ([51.159.93.128] helo=mail.dalibo.com) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1rCmbC-00BRj0-OR for pgsql-hackers@postgresql.org; Mon, 11 Dec 2023 20:10:00 +0000 Received: from karst (82-65-23-130.subs.proxad.net [82.65.23.130]) by mail.dalibo.com (Postfix) with ESMTPSA id AC29A1F8EB for ; Mon, 11 Dec 2023 21:09:52 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=dalibo.com; s=a; t=1702325392; bh=tQiqzxg6gIYduytVBTTL2Cqjy6MC2CrB8+DawqVCBAg=; h=Date:From:To:Subject:From; b=DmXIlJFbjMACcOOr7B0iq29g9El8mgUxSQiH1rECD4BPVV6dn0jKwJFoz+fZzLyxP PtPrJEgAPdSrRbqXQgkwu/9r0lLgvRKqa5Lqyr+u3X1otE/2FxOAw2MUMYKgk07rSp 4Pxig5UvItYViCekVQbMjVjZk1QabRz75tCulEgA= Date: Mon, 11 Dec 2023 21:09:51 +0100 From: Jehan-Guillaume de Rorthais To: "pgsql-hackers@postgresql.org" Subject: Sorting regression of text function result since commit 586b98fdf1aae Message-ID: <20231211210951.5020a5d6@karst> Organization: Dalibo MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Hi, A customer found what looks like a sort regression while testing his code f= rom v11 on a higher version. We hunt this regression down to commit 586b98fdf1a= ae, introduced in v12. Consider the following test case: createdb -l fr_FR.utf8 -T template0 reg psql reg <<<" BEGIN; CREATE TABLE IF NOT EXISTS reg ( id bigint NOT NULL, reg bytea NOT NULL ); =20 INSERT INTO reg VALUES (1, convert_to( 'aaa', 'UTF8')), (2, convert_to( 'aa}', 'UTF8')); =20 SELECT id FROM reg ORDER BY convert_from(reg, 'UTF8');" In parent commit 68f6f2b7395fe, it results: id=20 =E2=94=80=E2=94=80=E2=94=80=E2=94=80 2 1 And in 586b98fdf1aae: id=20 =E2=94=80=E2=94=80=E2=94=80=E2=94=80 1 2 Looking at the plan, the sort node are different: * 68f6f2b7395fe: Sort Key: (convert_from(reg, 'UTF8'::name)) * 586b98fdf1aae: Sort Key: (convert_from(reg, 'UTF8'::name)) COLLATE "C" It looks like since 586b98fdf1aae, the result type collation of "convert_fr= om" is forced to "C", like the patch does for type "name", instead of the "defa= ult" collation for type "text". Looking at hints in the header comment of function "exprCollation", I poked around and found that the result collation wrongly follow the input collati= on in this case. With 586b98fdf1aae: -- 2nd parameter type resolved as "name" so collation forced to "C" SELECT id FROM reg ORDER BY convert_from(reg, 'UTF8'); -- 1 -- 2 -- Collation of 2nd parameter is forced to something else SELECT id FROM reg ORDER BY convert_from(reg, 'UTF8' COLLATE \"default\"); -- 2 -- 1 -- Sort -- Sort Key: (convert_from(reg, 'UTF8'::name COLLATE "default")) -- -> Seq Scan on reg It seems because the second parameter type is "name", the result collation become "C" instead of being the collation associated with "text" type: "default". I couldn't find anything explaining this behavior in the changelog. It looks like a regression to me, but if this is actually expected, maybe this deser= ve some documentation patch? Regards,