Received: from makus.postgresql.org (makus.postgresql.org [98.129.198.125]) by mail.postgresql.org (Postfix) with ESMTP id 7AA832934B4 for ; Sun, 8 Jan 2012 17:05:41 -0400 (AST) Received: from static-ip-217-118-26-141.inaddr.ip-pool.com ([217.118.26.141] helo=mail.softperience.eu) by makus.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1RjzCN-0005Q2-UQ for pgsql-general@postgresql.org; Sun, 08 Jan 2012 20:18:41 +0000 Received: from mail.softperience.eu (loft2296 [217.118.26.141]) by mail.softperience.eu (Postfix) with ESMTPA id EA3C6C00032; Sun, 8 Jan 2012 21:18:21 +0100 (CET) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Date: Sun, 08 Jan 2012 21:18:21 +0100 From: =?UTF-8?Q?Rados=C5=82aw_Smogura?= To: Stefan Keller Cc: pgsql-general List Subject: Re: Binary Large Objects (LOB/BLOB) in Hibernate and JDBC: Unresolved issues In-Reply-To: References: Message-ID: <54a09cf5e11d80002f3d412c0be94d59@mail.softperience.eu> X-Sender: rsmogura@softperience.eu User-Agent: Roundcube Webmail/0.5.4 Content-Transfer-Encoding: quoted-printable X-Pg-Spam-Score: -1.9 (-) X-Archive-Number: 201201/161 X-Sequence-Number: 183814 On Sun, 8 Jan 2012 18:08:09 +0100, Stefan Keller wrote: > I'd like to backup my statement below regarding in JDBC driver from > PostgreSQL: > > When storing fields of type BLOB it inserts the binary string in > system table pg_largeobject (via user table). But when rows in user > table get updated or deleted it does not update nor delete > corresponding rows in table pg_largeobject. > > That's really a bug! > > Fortunately there's a solution indicated in the official docs > (http://www.postgresql.org/docs/9.1/static/lo.html) > To me, something like this should be implemented before hand in JDBC=20 > driver. > And in any case there should be a bold note about this in the JDBC > docs (http://jdbc.postgresql.org/documentation/head/binary-data.html=20 > ) > > Yours, Stefan > > > > 2012/1/6 Stefan Keller : >> Hi, >> >> I run into a nasty behavior of current PostgreSQL JDBC. >> >> I maintain images (from Webcams). In the Java and Hibernate (JPA)=20 >> code >> I specified a @Lob annotation on class MyData and a attribte/data=20 >> type >> "byte[] mydata;". Hibernate then generates two tables in PostgreSQL, >> one called MyData with a column mydata of type oid and an internal=20 >> one >> called pg_largobjects (which contain foreign keys to the oid).=20 >> That's >> also explained in the JDBC docs [1], saying "PostgreSQL provides two >> distinct ways to store binary data. Binary data can be stored in a >> table using the data type bytea or by using the Large Object feature >> which stores the binary data in a separate table in a special format >> and refers to that table by storing a value of type oid in your >> table." >> >> Now, when replacing the images (few hundred) with new ones using=20 >> Java, >> pg_largeobjects grew constantly until the file system run out of >> space. So old image data did'nt get released! This is to me a bug >> because the user/programmer must (and should) assume that there is a >> strict 1:1 relationship between generated table MyData and its LOB >> column data (stored in pg_largeobjects). >> =3D> I finally found the supplied module 'lo' [2] which releases >> detached records. Is this the recommended way to resolve this=20 >> problem? >> >> Searching for explanations I found a ticket HHH2244 [3] which was >> closed by the Hibernate team without action referring to the JDBC >> Spec. which says: "An SQL BLOB is a built-in type that stores a=20 >> Binary >> Large Object as a column value in a row of a database table". >> =3D> In other words: The PostgreSQL JDBC team should take action on=20 >> this >> but didn't until now, right? >> >> There is another issue about "PostgreSQL and BLOBs" [4]. First it >> cites PostgreSQL JDBC docs [1]. The thread [4] ends somehow too in a >> 'deadlock' concluding =C2=A0"...the PostgreSQLDialect (as of 3.5.5) ne= eds >> to change not to use MaterializedBlobType until the Postgres (JDBC) >> team changes their driver (which does not seem to have happened in=20 >> the >> last 6 years)." >> =3D> Any solutions or comments form insiders on this? >> >> Yours, Stefan >> >> [1] http://jdbc.postgresql.org/documentation/head/binary-data.html >> [2] http://www.postgresql.org/docs/current/interactive/lo.html >> [3] https://hibernate.onjira.com/browse/HHH-2244 >> [4] http://relation.to/Bloggers/PostgreSQLAndBLOBs This is common approach for PostgreSQL and some proxy of real life=20 Blobs. One time I submitted bug about this to Hibernate. But step by=20 step. 1. BLOBs are... Large Objects, they are stored as reference because=20 those objects are large, if you will store this objects as bytea then=20 select * will return all large data. It may not be comfortable not only=20 to download few GB of data, but to keep this on stack too. From your=20 perspective it doesn't matters because you put it in byte[]. But if You=20 will keep e.g. CD-ROM images then it's much more better to use streaming=20 approach then bytea[]. More over due to some security JDBC driver will=20 at least double memory consumed by bytea. 2. Specifying hibernate data type as bytea do not resolve problems=20 because it will still use LOB approach. 3. pg_largeobjects is system table, hibernate do not creates it. 4. Trigger based approach is good for this, but You need to do this=20 mannualy 5. If you want to use bytea use=20 @Type(type=3D"org.hibernate.type.PrimitiveByteArrayBlobType") (I think yo= u=20 should remove @Lob too) on your field. Regards, Rados=C5=82aw Smogura