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 1ol68U-0000wZ-Jr for pgsql-general@arkaria.postgresql.org; Wed, 19 Oct 2022 10:17:18 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1ol68T-0000dt-Dp for pgsql-general@arkaria.postgresql.org; Wed, 19 Oct 2022 10:17:17 +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 1ol68S-0000dk-UI for pgsql-general@lists.postgresql.org; Wed, 19 Oct 2022 10:17:17 +0000 Received: from outbound.visena.net ([46.226.12.34]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1ol68O-00053g-7l for pgsql-general@lists.postgresql.org; Wed, 19 Oct 2022 10:17:16 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=visena.com; s=20141101.wh; h=Content-Type:MIME-Version:Subject:References:In-Reply-To: Message-ID:Cc:To:From:Date:Sender:Reply-To:Content-Transfer-Encoding: Content-ID:Content-Description:Resent-Date:Resent-From:Resent-Sender: Resent-To:Resent-Cc:Resent-Message-ID:List-Id:List-Help:List-Unsubscribe: List-Subscribe:List-Post:List-Owner:List-Archive; bh=DBnmYc7mWbEds1hSPtuPUPzOxIJKEauOAWpE2EYwPlc=; b=IYToMP+z0tdnXZdfr8P977PixM fY0v9ZhFqVWk+N3Qt9xu4wL8EsH2k2i8n6DwiDSJoi1zMn0pufM/fMmVDwuOWsn8YrwBsN8D08TQA M6l/GebZ5Je/2ycDVnGDkXm/gBClyr7T9dtqroGbxntilSvElz0qOFnP0BgyxlzaXyvU=; Received: from batch01.services.internal.visena.net ([10.3.0.103]) by outbound.visena.net with utf8esmtp (Exim 4.93) (envelope-from ) id 1ol68N-00B50R-0P; Wed, 19 Oct 2022 12:17:11 +0200 Date: Wed, 19 Oct 2022 12:17:10 +0200 (CEST) From: Andreas Joseph Krogh To: Dominique Devienne Cc: pgsql-general@lists.postgresql.org Message-ID: In-Reply-To: References: Subject: Sv: How to store "blobs" efficiently for small and large sizes, with random access MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="----=_Part_50028_1302679665.1666174630751" X-Mailer: Visena Mail 3.2.163 List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk ------=_Part_50028_1302679665.1666174630751 Content-Type: multipart/related; boundary="----=_Part_50029_746778231.1666174630751" ------=_Part_50029_746778231.1666174630751 Content-Type: multipart/alternative; boundary="----=_Part_50030_407585278.1666174630783" ------=_Part_50030_407585278.1666174630783 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable First advice, don't do it. We started off storing blobs in DB for =E2=80=9C= TX safety=E2=80=9D,=20 but backup/restore quickly became too cumbersome so we ended up moving all= =20 blobs out and only store reference in DB. This required us to make a =E2=80= =9Cvacuum=20 system=E2=80=9D that cleans up the blob-storage regularly as ROLLBACK/crash= can make it=20 out of sync. We chose storing as LO because with it, streaming large blobs (not using mu= ch=20 memory) actually worked, with JDBC at least. P=C3=A5 onsdag 19. oktober 2022 kl. 11:47:59, skrev Dominique Devienne < ddevienne@gmail.com >: Hi. I'd like some advice storing blobs (millions of them), ranging from very small, to large > 1GB. I know about bytea versus lo, and I have probably read most of what's out there about them :) Upfront, I have to state that I'm not keen on lo, because of security considerations. We store blobs in many different schemas, and users can access some schemas, and not others. So the fact the lo table is unique for the whole database would allow users to see blobs from any schema, as I understand it. Right? OTOH, lo has random access, which I also need... (I'm also not a fan of lo needing triggers for lifetime management) (nor of the requirement to have an explicit transaction to use lo). Here are the main requirement I need to fulfil: 1) store literally millions of rows, 1 "blob" per row. (scientific data). 2) most "blobs" are smallish, below a few KBs. bytea's perfect for that. 3) yet many blobs are on the dozens of MBs. bytea's still OK. 4) but some blobs exceed the 1GB byte limit. Found at least a dozen just in our test data, so clients will have them. 5) for accessing larger blobs, the API I must implement accesses contiguous chunks of the blobs. Thus I need random access. I'm porting the backend of that API from Oracle to PostgreSQL. In Oracle we used RAW for smaller values, and SecureFile blobs for larger o= nes. Oracle LOBs are similar to lo I guess, providing random access, except higher performance for large sizes, but slower than bytea for smaller one. But a PITA to deal with, with locators, breaking pre-fetching, etc... PostgreSQL bytea is much better and simpler, except limited to 1GB... Bytea also has no direct random access, except via substr[ing], but how efficient and "random access" is that? For example, SQLite stores large values in overflow pages, and has to traverse all those pages to pointer-chase the chain of those for large blobs, so the substr() "random access" is NOT O(1) and involves way too much IO (i.e. O(N)). Do TOAST'ed large values (text or bytea) fare better in term of substr() efficiency, close to O(logN) and only the necessary IO for what's actually requested by the substr() (modulo the extrema 2K chunks of TOAST)? Some posts on the internet also peg lo to be faster above 20MB compared to bytea, for example. Is that even true? One post I read (from Daniel Verite I think) kinda hinted the lo table is not that different from TOAST ones, with 2K chunks, thus I don't see why lo would be any faster than a TOAST'ed bytea for example. Any insights from someone with knowledge of the internals can share on this= ? At this point, I have not yet redone the extensive benchmarking we did a few years ago between Oracle and PostgreSQL, which went into lo versus bytea then. And benchmark are hard to do, not having easy access to different kind of servers with different storage backends, or cloud-hosted PG. It's too easy to get biais from a local setup, leading to a design that'd not perform optimally in a different one. That's why I'm asking a more open-ended question to experts on this list. Because of #4 above, I need to either use lo (but see above, notably the security concern), or roll-up my own TOAST^2 (squared), as I call it, where I manually "shard" / chunk large blobs in an auxiliary table, itself TOAST'ed of course, with some threshold for chunks (e.g. 1 or 4 or 16 MB perhaps). The latter keeps the blobs in the schema (good for security), lifetime is managed by FKs (as usual), and the sharding limits the ill-effects of "emulating" random-access with substr() if necessary. I've already done things like this in SQLite land (also has the 1GB limit for its text and blob types). So is this a terrible idea? What alternatives do I have? I'd really appreciate some expert advice on the above, before I go too far down the rabbit hole. Thanks, --DD PS: Another drawback of lo is that because it's a single table, it still subject to the 32 TB limit on a relation. The 4TB limit per lo is way more than we need, but the 32 TB limit may actually be more of an issue for our larger clients, which have thousands of projects, each with upwards of a few millions of those blobs. bytea values being stored in different schemas (per-project, a design constraint), puts that limit per-project which will be more than enough. For the sum of all projects, maybe not... I.e. with real client-case of 3K projects, that puts an average of only 10GB of lo's per-project (i.e. schema), which could very well be problematic... -- Andreas Joseph Krogh CTO / Partner - Visena AS Mobile: +47 909 56 963 andreas@visena.com www.visena.com ------=_Part_50030_407585278.1666174630783 Content-Type: text/html;charset=UTF-8 Content-Transfer-Encoding: quoted-printable
First a= dvice, don't do it. We started off storing blobs in DB for =E2=80=9CTX safe= ty=E2=80=9D, but backup/restore quickly became too cumbersome so we ended u= p moving all blobs out and only store reference in DB. This required us to = make a =E2=80=9Cvacuum system=E2=80=9D that cleans up the blob-storage regu= larly as ROLLBACK/crash can make it out of sync.
=C2=A0
We chose storing as LO because with it, streaming large blobs (not using m= uch memory) actually worked, with JDBC at least.
=C2=A0
P=C3=A5 onsdag 19. oktober 2022 kl. 11:47:59, skrev Dominique Devienne <= ;ddevienne@gmail.com>:<= blockquote>
Hi. I'd like some advice storing blobs (millions of them), ranging
f= rom very small, to large > 1GB.

I know about bytea versus lo, and= I have probably read most of what's
out there about them :)

Upfr= ont, I have to state that I'm not keen on lo, because of security
consid= erations. We store
blobs in many different schemas, and users can access= some schemas,
and not others. So
the fact the lo table is unique for= the whole database would allow
users to see blobs from any
schema, a= s I understand it. Right? OTOH, lo has random access, which I
also need.= ..
(I'm also not a fan of lo needing triggers for lifetime management)(nor of the requirement to have an explicit transaction to use lo).
Here are the main requirement I need to fulfil:
1) store literally mil= lions of rows, 1 "blob" per row. (scientific data).
2) most &q= uot;blobs" are smallish, below a few KBs. bytea's perfect for that.3) yet many blobs are on the dozens of MBs. bytea's still OK.
4) but so= me blobs exceed the 1GB byte limit. Found at least a dozen
just in our t= est data, so clients will have them.
5) for accessing larger blobs, the = API I must implement accesses
contiguous chunks of the blobs. Thus I nee= d random access.

I'm porting the backend of that API from Oracle to = PostgreSQL.
In Oracle we used RAW for smaller values, and SecureFile blo= bs for larger ones.
Oracle LOBs are similar to lo I guess, providing ran= dom access, except
higher performance for large sizes,
but slower tha= n bytea for smaller one. But a PITA to deal with, with
locators, breakin= g pre-fetching, etc...

PostgreSQL bytea is much better and simpler, = except limited to 1GB...
Bytea also has no direct random access, except = via substr[ing], but
how efficient and "random access" is that= ?
For example, SQLite stores large values in overflow pages, and has to<= br>traverse all those pages to pointer-chase
the chain of those for larg= e blobs, so the substr() "random access" is
NOT O(1) and invol= ves way too much IO (i.e. O(N)).
Do TOAST'ed large values (text or bytea= ) fare better in term of
substr() efficiency, close to O(logN) and only<= br>the necessary IO for what's actually requested by the substr() (modulothe extrema 2K chunks of TOAST)?

Some posts on the internet also p= eg lo to be faster above 20MB
compared to bytea, for example. Is that ev= en true?
One post I read (from Daniel Verite I think) kinda hinted the l= o table
is not that different from TOAST ones,
with 2K chunks, thus I= don't see why lo would be any faster than a
TOAST'ed bytea for example.=
Any insights from someone with knowledge of the internals can share on = this?

At this point, I have not yet redone the extensive benchmarkin= g we did
a few years ago between
Oracle and PostgreSQL, which went in= to lo versus bytea then. And
benchmark are hard to do, not
having eas= y access to different kind of servers with different storage
backends, o= r cloud-hosted PG.
It's too easy to get biais from a local setup, leadin= g to a design
that'd not perform optimally in a different one.
That's= why I'm asking a more open-ended question to experts on this list.

= Because of #4 above, I need to either use lo (but see above, notably
the= security concern),
or roll-up my own TOAST^2 (squared), as I call it, w= here I manually
"shard" / chunk large blobs
in an auxiliary= table, itself TOAST'ed of course, with some threshold
for chunks (e.g. = 1 or 4 or 16 MB perhaps).
The latter keeps the blobs in the schema (good= for security), lifetime
is managed by FKs (as usual),
and the shardi= ng limits the ill-effects of "emulating" random-access
with su= bstr() if necessary.
I've already done things like this in SQLite land (= also has the 1GB
limit for its text and blob types).

So is this a= terrible idea? What alternatives do I have?
I'd really appreciate some = expert advice on the above, before I go too
far down the rabbit hole.
Thanks, --DD

PS: Another drawback of lo is that because it's a = single table, it
still subject to the 32 TB limit on a relation.
=C2= =A0 The 4TB limit per lo is way more than we need, but the 32 TB limit
m= ay actually be more of an issue for
=C2=A0 our larger clients, which hav= e thousands of projects, each with
upwards of a few millions of those bl= obs.
=C2=A0 bytea values being stored in different schemas (per-project,= a
design constraint), puts that limit per-project
=C2=A0 which will = be more than enough. For the sum of all projects, maybe
not... I.e. with= real client-case of 3K projects,
=C2=A0 that puts an average of only 10= GB of lo's per-project (i.e. schema),
which could very well be problemat= ic...



=C2=A0

=C2= =A0

=C2=A0

--
Andreas Joseph Krogh
CTO / Partner - Visena AS
Mobile: +47 909 56 963
3D""
=C2=A0
------=_Part_50030_407585278.1666174630783-- ------=_Part_50029_746778231.1666174630751 Content-Type: image/png Content-Transfer-Encoding: base64 Content-Disposition: inline Content-ID: iVBORw0KGgoAAAANSUhEUgAAAIUAAAAYCAYAAADUIj6hAAAABHNCSVQICAgIfAhkiAAABzBJREFU aEPtmNFxHDcMhmVP3i1VECpvnjzkVIHWFfhcgVcVRKrAUgWRK/C6Al8H3lTgy0PGbzFdQc4VJP/H ADs43q6kROeJNbOYgQACIAgCWJKng4MZ5gxUGXh0U0b+SIuF9G+EWXj2Q15vbrE/NPtk9uub7Gfd t5mBx1NhqSEo8HshjbEUtlO2yIM9tsx5dZP9rPt2MzDZFAqZE4LGcFjdsg3saQaHt7fY70X949On aS+OZidDBkavD33157L4xaw2os+Mp/DAC10l2XhOCeStj0W5ajrJL8WfCi80Xgf9vVlrhg9yRONe /f7xI2vNsIcM7JwU9o6oGyJrLT8JOA1aX9sKP4wl94bAniukEbo/n7YPmuTETzIab4Y9ZWCrKexd 8M58lxPCvvD6aihfvexbEQoPYO8NQRO0JnddGO6FJYaVsBde7cXj7KRk4LsqDxQzCYeGsJNgaXZe +JXkyGgWINq3Gp+bHNILz+wEYs5q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