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 1lw6qd-0007oi-UH for pgsql-general@arkaria.postgresql.org; Wed, 23 Jun 2021 17:39:35 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1lw6qc-0007Xc-Uh for pgsql-general@arkaria.postgresql.org; Wed, 23 Jun 2021 17:39:34 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1lw6qc-0007XT-M9 for pgsql-general@lists.postgresql.org; Wed, 23 Jun 2021 17:39:34 +0000 Received: from mail.hjp.at ([212.17.106.138] helo=rorschach.hjp.at) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1lw6qW-0000iw-A2 for pgsql-general@lists.postgresql.org; Wed, 23 Jun 2021 17:39:32 +0000 Received: by rorschach.hjp.at (Postfix, from userid 1000) id 5C4EE4EF6; Wed, 23 Jun 2021 19:39:25 +0200 (CEST) Date: Wed, 23 Jun 2021 19:39:25 +0200 From: "Peter J. Holzer" To: pgsql-general@lists.postgresql.org Subject: Re: How to hash a large amount of data within Postgres? Message-ID: <20210623173925.GA27765@hjp.at> Mail-Followup-To: pgsql-general@lists.postgresql.org References: <77309168.20210621155309@am-soft.de> MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha512; protocol="application/pgp-signature"; boundary="WIyZ46R2i8wDzkSu" Content-Disposition: inline In-Reply-To: <77309168.20210621155309@am-soft.de> User-Agent: Mutt/1.10.1 (2018-07-13) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --WIyZ46R2i8wDzkSu Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On 2021-06-21 15:53:09 +0200, Thorsten Sch=F6ning wrote: > Some years ago I implemented some SQL to read all files, build a table > of SHA256 hashes and tell me how much data is redundant. The goal was > to have a look at which files share the same hash with different LOIDs > and optionally change that, so that all those files are only stored > once on the end. >=20 > While the approach was pretty naive, because it simply read all files > into memory to calculate the hashes, I'm somewhat sure it worked in > the past with Postgres 9.6. The executing server had enough free RAM > available as well to process the at most ~4 GiB large files one after > another. >=20 > I tried that SQL today with Postgres 11 on UB 18.04 and it failed: >=20 > > [Code: 0, SQL State: XX000] FEHLER: invalid memory alloc request size = 1898107949 > > Wobei: PL/pgSQL-Funktion loid_hash_calc(oid,text)[...] [...] > I searched regaridng that issue and only found two relevant results: > Corrupted rows for some reason and simply size restrictions when > allocating memory. The latter is more likely than the former in my > case, as the restrictions seems to be 1 GiB and I do have larger > files. 1 GB is the maximum size of quite a few data structures in PostgreSQL. I don't use PL/pgSQL, but I wouldn't be surprised if it was the maximum size of whatever loread() returns (a bytea?). I would be surprised if this limit was higher in version 9.6 than it is in version 11, however. > I'm doing the following simply currently, because I didn't find any > interfaces allowing to forward blocks of data, LOIDs, file descriptors > or anything like that working with smaller buffers or alike. >=20 > > fd :=3D lo_open( loid, INV_READ); > > size :=3D lo_lseek(fd, 0, SEEK_END); > > PERFORM lo_lseek(fd, 0, SEEK_SET); >=20 > > hashBin :=3D digest(loread(fd, size), algorithm); > > hashHex :=3D encode(hashBin, 'hex'); >=20 > So, is there any way to work around the problem I have currently? Normally, hash libararies have a way to feed chunks of data into a hash computations to avoid having to keep the whole thing in RAM. The pgcrypto extension seems to be lacking such functionality, however. I would build something similar to a Merkle tree: Choose a convenient chunk size (a few MB is probably ok), read the large object in chunks of this size, computing the hash for each. Concatenate all the hashes and compute the hash of that. Add intermediate levels if the the concatenated hashes are still too large to fit in memory. hp --=20 _ | Peter J. Holzer | Story must make more sense than reality. |_|_) | | | | | hjp@hjp.at | -- Charles Stross, "Creative writing __/ | http://www.hjp.at/ | challenge!" --WIyZ46R2i8wDzkSu Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iQIzBAABCgAdFiEETtJbRjyPwVTYGJ5k8g5IURL+KF0FAmDTccgACgkQ8g5IURL+ KF2FKQ//d4GmNqscsNTeRAtNraEXBriNAdbTSkcHcKjIHgTy0S7saI7/WCaC3O8m dQ5nqVLKuaiSY2oiTfHunOem8SbQQ1ZMwCovfmryBwvgu72IJkvFcNKHA+4f21a2 Qks5f53sVR57mhUfmv3i1uaooIEmKlsk1OgFlhHMqNqY34aPuwwgGBeOIlHHX+iE XA2jKffdXOCwnPQrZ0i12YfxLbIDwaQAb5953uxvFju2MiIGIY05jixq5dpJRPQ+ fP5QgtNcGhJgfL6B1PcQUJ68ip7A7kxyiKUgYAXckVl7V/sNiJ1P/Vcw49AyHRqR uLWBMkZOxpL5T2ygeEc92xrYDJG/gmaFWdrMO9REqCPwjtPNz2ZzxXAMZSFtsPBI 9xoeaenSQnFpFtExgno9fVUGRLuoLnn2Vjbv6IfONhmnAcviJyz7D6l/AZcE7aou n6dcvf/bE84whI0a4lfwpeDO3uxaf/fQii9voC3UhGP9wK3HExfLmcmwBAPGwB7U yQ03Za5TnGneISTlYDPHrd8C20WdbxPs6qk/uawBiuH5oUHT8ZLYSFsGSBoOaDvN fV+wilBMcgV/yBQ9rthH132A2yHKY3xJ1JL5tHdkM+VafFUMXYF+Au+qbdhu8BaQ sNGfodTW+ftg0ZnhHEcM07opPr+6krfdp69iyMMOU8BhY+5Lg9I= =xmrw -----END PGP SIGNATURE----- --WIyZ46R2i8wDzkSu--