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.96) (envelope-from ) id 1vsj0i-00Gb3j-34 for pgsql-general@arkaria.postgresql.org; Wed, 18 Feb 2026 14:58:41 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1vsj0i-00GDrJ-0p for pgsql-general@arkaria.postgresql.org; Wed, 18 Feb 2026 14:58:40 +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.96) (envelope-from ) id 1vsj0h-00GDr8-2m for pgsql-general@lists.postgresql.org; Wed, 18 Feb 2026 14:58:40 +0000 Received: from lana.depesz.com ([88.198.49.178] helo=depesz.com) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.98.2) (envelope-from ) id 1vsj0f-00000001NNR-3VUa for pgsql-general@postgresql.org; Wed, 18 Feb 2026 14:58:39 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=depesz.com; s=20170201; h=Content-Type:MIME-Version:Reply-To:Message-ID:Subject:To:Sender :From:Date:Cc:Content-Transfer-Encoding:Content-ID:Content-Description: In-Reply-To:References; bh=Bm0vRVLzQGiwqNwz34oFmcQ0vS3/wDbJQ0gNnf+hdvE=; b=NJ zaHVTYs2lSLMrf+Oq1XUB5vACTuuOe09KAFAEziJnW5kp+Ze31Yvi5khS/7iLJI6OrUXW1JBtOkpn 9QPD9RHBMMe6ylg4Mf9VNzQihD5q/mZu/leY8xB4udzrVfUHVyl6Q/unOlVRBFQGADkUtLGuvMjxo 7A4SyVZRQdIajC0=; Received: from depesz by depesz.com with local (Exim 4.96) (envelope-from ) id 1vsj0e-000UPX-0U for pgsql-general@postgresql.org; Wed, 18 Feb 2026 15:58:36 +0100 Date: Wed, 18 Feb 2026 15:58:36 +0100 From: hubert depesz lubaczewski Sender: depesz@depesz.com To: pgsql-general mailing list Subject: Can we get sha* function over text, that could be used in index? Message-ID: Reply-To: depesz@depesz.com MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Hi, So, we have various sha* functions. And I recently got asked about using them as a based for unique index over long texts. Normally one would do it with md5(text), but the person asking wanted to use sha(). and these functions work only on bytea. And apparently - we can't. 'text-value'::bytea won't work for some specific text values. convert_to() isn't immutable. I figured out that I can do something like: SELECT sha256( string_agg( ascii( t )::text, ',' ORDER BY idx )::bytea ) FROM regexp_split_to_table( 'INPUT_STRING', '' ) WITH ORDINALITY AS x ( t, idx ); But that's hardly sane solution. I've read bug report from 2008: https://www.postgresql.org/message-id/flat/48D20645.1090503%40gmx.net#ce27df4802c9854a9eb77066a5c7cb05 And while I kinda undestand, create-conversion, server-encoding, I don't really *grok* why we can't have immutable conversion to bytea. And/or versions of sha* functions that simply work on text. Is it doable? How does it work in md5()? Apparently it does also work in pgcrypto/digest(), so there should be a way to get it in core sha* functions? Best regards, depesz