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 1iepQQ-00041k-Kd for pgsql-general@arkaria.postgresql.org; Wed, 11 Dec 2019 00:00:18 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1iepQN-0003tU-34 for pgsql-general@arkaria.postgresql.org; Wed, 11 Dec 2019 00:00:15 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1iepQM-0003tN-MY for pgsql-general@lists.postgresql.org; Wed, 11 Dec 2019 00:00:14 +0000 Received: from mail-oi1-x241.google.com ([2607:f8b0:4864:20::241]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1iepQF-000475-BU for pgsql-general@lists.postgresql.org; Wed, 11 Dec 2019 00:00:14 +0000 Received: by mail-oi1-x241.google.com with SMTP id i1so11587667oie.8 for ; Tue, 10 Dec 2019 16:00:06 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=subject:to:references:from:message-id:date:user-agent:mime-version :in-reply-to:content-transfer-encoding:content-language; bh=Svgj5hIY3l87602RmfKxUTYhGul4hxHIyxmnsqmfKUc=; b=shUQXT5ORwJ8rWmPeY3mtYv0KpeTeKBaKBK86MX8PpkfirMIHpFJURCIQd0Z6B16H0 i0sQwuKiLNsH75bS3IExRiUR96hs2HGrHoEXm+McOgJF2A5GgZbg+KmKV6YpfqJg5f/V pX1ORvcONf9RqI9z7C5Zv4p6spWyNQfc7CFQgcyZ3jF4i9XgyX/BkGcWJWtLaVkYOiRT zbXZ+KQn7Kqt2d1/Sm5KALbW9pu2RFMiI6sj7pZsDI36NXXO302QT/AQ3HApo2uhMj3q QUCMMcxY6EMALOpy+F/T2kLNH8D/VFjo1ov3UT0V2YkcNEnloYAiyrkAjaPtZwawpu4Q dPcQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-transfer-encoding :content-language; bh=Svgj5hIY3l87602RmfKxUTYhGul4hxHIyxmnsqmfKUc=; b=RAYpWCAzT1HYBTiwBJ5yu7GKfz5LAIY4/mwL26OgRTr1LoDE9ymV7ZvTp7SjoMygMf JTLoXHup44nZSJpqDVNOIYHGmCdhnCz2FVKKqYg/qWAeFdZJEyxJc8vivHyHpsRC9KCG wTREevUYd+rnwZXlCCvJN8+wgvO20oFj/gYarSl2/ylCsPSyIPT5mOtC3xbrTNFNGckJ VxKhhOVGehlH+g67cfUbX5294rnSfcfmjBWJTSEIUsa/fxDQ9A0LT8FXi7d+KKSkwjPq ZBzwRD+zyjpPpzUNYltm0fef7L6J5E50RteOH3TBpLuyovkhbEMINrewHjHWa6dATGdp p2ZQ== X-Gm-Message-State: APjAAAXHycnbwSYytGvxFvBOvli+86PITmM7BAhgRQS6RyTKw/1vht9Z 1uFdcpXGhe3TCQQoNymEB+DEDBevCPI= X-Google-Smtp-Source: APXvYqyBRtLa1R+XIIB79m/QEbQV3LeaBcVIQySG3c87sX7h2ylN8EqNw7ebL3ZZ0pYHnXiixh4lqg== X-Received: by 2002:a05:6808:6c5:: with SMTP id m5mr529642oih.143.1576022404269; Tue, 10 Dec 2019 16:00:04 -0800 (PST) Received: from [192.168.1.10] (ip70-186-126-197.no.no.cox.net. [70.186.126.197]) by smtp.googlemail.com with ESMTPSA id b3sm111745oie.25.2019.12.10.16.00.03 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 10 Dec 2019 16:00:03 -0800 (PST) Subject: Re: Fast, stable, portable hash function producing 4-byte or 8-byte values? To: pgsql-general@lists.postgresql.org References: From: Ron Message-ID: Date: Tue, 10 Dec 2019 18:00:02 -0600 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.9.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit Content-Language: en-US List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On 12/10/19 3:11 PM, Erwin Brandstetter wrote: > I am looking for stable hash functions producing 8-byte or 4-byte hashes > from long text values in Postgres 10 or later. > > There is md5(), the result of which can be cast to uuid. This reliably > produces practically unique, stable 16-byte values. I have usecases where > an 8-byte or even 4-byte hash would be good enough to make collisions > reasonably unlikely. (I can recheck on the full string) - and expression > indexes substantially smaller. I could truncate md5 and cast back and > forth, but that seems like a lot of wasted computation. Are there > suggestions for text hash functions that are > - fast > - keep collisions to a minimum > - stable across major Postgres versions (so expression indexes don't break) > - croptographic aspect is not needed (acceptable, but no benefit) What about a CRC32 function?  It's fast, and an SSE4 instruction has been in Intel CPUs for about 10 years. > > There is an old post from 2012 by Tom Lane suggesting that hashtext() and > friends are not for users: > > https://www.postgresql.org/message-id/24463.1329854466%40sss.pgh.pa.us > > Postgres 11 added hashtextextended() and friends to generate bigint > hashes. In a more recent post from 3 months ago, Tom suggests to use it in > user-land - if portability is not needed: > > https://www.postgresql.org/message-id/9434.1568839177%40sss.pgh.pa.us > > Is pghashlib by Marko Kreen my best option? > > https://github.com/markokr/pghashlib > > Or the "version-independent hash functions for PostgreSQL" from Peter > Eisentraut: > > https://github.com/petere/pgvihash > > Neither received updates for a couple of years. Unmaintained? Or obsolete? > And neither is available on most hosted services like RDS or Heroku (which > would be required in come cases). > > So what are my best options? > > Regards > Erwin -- Angular momentum makes the world go 'round.