pg.ddx.io pgsql-jdbc@postgresql.org mailing list archive
help / color / mirror / Atom feedWorkarounds for getBinaryStream returning ByteArrayInputStream on bytea
7+ messages / 3 participants
[nested] [flat]
* Workarounds for getBinaryStream returning ByteArrayInputStream on bytea
@ 2010-11-24 20:50 Александър Шопов <lists@kambanaria.org>
2010-11-24 22:04 ` Re: Workarounds for getBinaryStream returning ByteArrayInputStream on bytea Radosław Smogura <rsmogura@softperience.eu>
0 siblings, 1 reply; 7+ messages in thread
From: Александър Шопов @ 2010-11-24 20:50 UTC (permalink / raw)
To: pgsql-jdbc
Hi everyone,
I have a table containing file contents in bytea columns.
The functionality I am trying to achieve is having a result set
containing such columns, iterating over them and streaming them while
zipping them.
The problem is that I get ByteArrayInputStream from
ResultSet.getBinaryStream.
Thus iterating over many rows, each containing more than 10MB of data
smashes the heap. In peak times I will have several such processes.
I am using postgresql-8.4-702.jdbc3.jar against a PG 8.4.5 installation.
I looked at the current source of driver.
Jdbc3ResultSet extends AbstractJdbc3ResultSet extends
AbstractJdbc2ResultSet which is the place that provides implementation
for getBinaryStream which returns ByteArrayInputStream on bytea
columns, and BlobInputStream on blob columns. On skimming it seems that
BlobInputStream does indeed stream the bytes instead of reading them in
memory (chunks for reads are 4k).
So what am I options? Refactor the DB schema to use blobs rather than
bytea? Is it impossible to have bytea read in chunks?
Kind regards:
al_shopov
^ permalink raw reply [nested|flat] 7+ messages in thread
* Re: Workarounds for getBinaryStream returning ByteArrayInputStream on bytea
2010-11-24 20:50 Workarounds for getBinaryStream returning ByteArrayInputStream on bytea Александър Шопов <lists@kambanaria.org>
@ 2010-11-24 22:04 ` Radosław Smogura <rsmogura@softperience.eu>
2010-11-24 22:53 ` Re: Workarounds for getBinaryStream returning ByteArrayInputStream on bytea Александър Шопов <lists@kambanaria.org>
0 siblings, 1 reply; 7+ messages in thread
From: Radosław Smogura @ 2010-11-24 22:04 UTC (permalink / raw)
To: Александър Шопов <lists@kambanaria.org>; +Cc: pgsql-jdbc
I see only two possibilities
1. Decrease fetch size, e.g. to 1.
2. Refactor schema.
Kind regards,
Radek
On Wed, 24 Nov 2010 22:50:46 +0200, Александър Шопов
<lists@kambanaria.org>
wrote:
> Hi everyone,
> I have a table containing file contents in bytea columns.
> The functionality I am trying to achieve is having a result set
> containing such columns, iterating over them and streaming them while
> zipping them.
[...]
> So what am I options? Refactor the DB schema to use blobs rather than
> bytea? Is it impossible to have bytea read in chunks?
> Kind regards:
> al_shopov
--
----------
Radosław Smogura
http://www.softperience.eu
^ permalink raw reply [nested|flat] 7+ messages in thread
* Re: Workarounds for getBinaryStream returning ByteArrayInputStream on bytea
2010-11-24 20:50 Workarounds for getBinaryStream returning ByteArrayInputStream on bytea Александър Шопов <lists@kambanaria.org>
2010-11-24 22:04 ` Re: Workarounds for getBinaryStream returning ByteArrayInputStream on bytea Radosław Smogura <rsmogura@softperience.eu>
@ 2010-11-24 22:53 ` Александър Шопов <lists@kambanaria.org>
2010-11-25 09:43 ` Re: Workarounds for getBinaryStream returning ByteArrayInputStream on bytea Radosław Smogura <rsmogura@softperience.eu>
2010-11-26 12:05 ` Re: Workarounds for getBinaryStream returning ByteArrayInputStream on bytea Radosław Smogura <rsmogura@softperience.eu>
0 siblings, 2 replies; 7+ messages in thread
From: Александър Шопов @ 2010-11-24 22:53 UTC (permalink / raw)
To: pgsql-jdbc
В 16:04 -0600 на 24.11.2010 (ср), Radosław Smogura написа:
> I see only two possibilities
> 1. Decrease fetch size, e.g. to 1.
Even if I do, bytea is potentially 1GB. Plus peaks in usage can still
smash the heap.
So refactoring to BLOBs is perhaps the only way out.
Will the JDBC driver always present bytea InputStream as
ByteArrayInputStream? No plans to change that? (even if there are, I
will still have to refactor meanwhile).
Perhaps this behaviour should be better communicated to DB schema
designers.
It seems to me from the Npgsql2.0.11 readme.txt that reading in chunks
is provided for .Net.
Is there need to perhaps make patches for this in the jdbc driver?
Kind regards:
al_shopov
^ permalink raw reply [nested|flat] 7+ messages in thread
* Re: Workarounds for getBinaryStream returning ByteArrayInputStream on bytea
2010-11-24 20:50 Workarounds for getBinaryStream returning ByteArrayInputStream on bytea Александър Шопов <lists@kambanaria.org>
2010-11-24 22:04 ` Re: Workarounds for getBinaryStream returning ByteArrayInputStream on bytea Radosław Smogura <rsmogura@softperience.eu>
2010-11-24 22:53 ` Re: Workarounds for getBinaryStream returning ByteArrayInputStream on bytea Александър Шопов <lists@kambanaria.org>
@ 2010-11-25 09:43 ` Radosław Smogura <rsmogura@softperience.eu>
1 sibling, 0 replies; 7+ messages in thread
From: Radosław Smogura @ 2010-11-25 09:43 UTC (permalink / raw)
To: Александър Шопов <lists@kambanaria.org>; +Cc: pgsql-jdbc
On Thu, 25 Nov 2010 00:53:31 +0200, Александър Шопов
<lists@kambanaria.org>
wrote:
> В 16:04 -0600 на 24.11.2010 (ср), Radosław Smogura написа:
>> I see only two possibilities
>> 1. Decrease fetch size, e.g. to 1.
> Even if I do, bytea is potentially 1GB. Plus peaks in usage can still
> smash the heap.
bytea is like varchar, and it's transmitted to client at all, even if you
don't want to read it, it is somewhere on heap.
> So refactoring to BLOBs is perhaps the only way out.
Here is other solution
http://archives.postgresql.org/pgsql-jdbc/2007-08/msg00078.php
You can write simple stream to do such reads on demand, select everything
without bytea column..., but probably blobs will be better.
> Will the JDBC driver always present bytea InputStream as
> ByteArrayInputStream? No plans to change that? (even if there are, I
> will still have to refactor meanwhile).
As above, the content is on heap, much more when you read you transform
this content and you creates new array (so heap 2x), maybe some chunked on
demand transformation will be better, but this is driver specific... or you
need to wait when I end binary JDBC and this will be in main release...
> Perhaps this behaviour should be better communicated to DB schema
> designers.
> It seems to me from the Npgsql2.0.11 readme.txt that reading in chunks
> is provided for .Net.
At a glance it looks that chunk reading means reading all network response
at all not bytea.
> Is there need to perhaps make patches for this in the jdbc driver?
> Kind regards:
> al_shopov
----------
Radosław Smogura
http://www.softperience.eu
^ permalink raw reply [nested|flat] 7+ messages in thread
* Re: Workarounds for getBinaryStream returning ByteArrayInputStream on bytea
2010-11-24 20:50 Workarounds for getBinaryStream returning ByteArrayInputStream on bytea Александър Шопов <lists@kambanaria.org>
2010-11-24 22:04 ` Re: Workarounds for getBinaryStream returning ByteArrayInputStream on bytea Radosław Smogura <rsmogura@softperience.eu>
2010-11-24 22:53 ` Re: Workarounds for getBinaryStream returning ByteArrayInputStream on bytea Александър Шопов <lists@kambanaria.org>
@ 2010-11-26 12:05 ` Radosław Smogura <rsmogura@softperience.eu>
2010-11-26 15:25 ` Re: Workarounds for getBinaryStream returning ByteArrayInputStream on bytea Kris Jurka <books@ejurka.com>
1 sibling, 1 reply; 7+ messages in thread
From: Radosław Smogura @ 2010-11-26 12:05 UTC (permalink / raw)
To: Александър Шопов <lists@kambanaria.org>; +Cc: pgsql-jdbc
Hi,
I would like to send few files for getBinaryStream(). So this will work
much like stream and will don't eat so much heap. I don't copy source
this_row[i] array, so I don't know how this will do with concur updates,
(original method doesn't make this when column is not bytea, too). I left
few comments if we should throw exception on broken streams in 8.4, or just
silence notify EOF.
One thing in the below code is to change to PSQLException.
Below is AbstractJdbc2ResultSet.getBinaryStream
public InputStream getBinaryStream(int columnIndex) throws SQLException
{
checkResultSet( columnIndex );
if (wasNullFlag)
return null;
if (connection.haveMinimumCompatibleVersion("7.2")) {
//Version 7.2 supports BinaryStream for all PG bytea type
//As the spec/javadoc for this method indicate this is to be
used for
//large binary values (i.e. LONGVARBINARY) PG doesn't have a
separate
//long binary datatype, but with toast the bytea datatype is
capable of
//handling very large values. Thus the implementation ends up
calling
//getBytes() since there is no current way to stream the value
from the server
//Copy of some logic from getBytes
//Version 7.2 supports the bytea datatype for byte arrays
if (fields[columnIndex - 1].getOID() == Oid.BYTEA) {
//TODO: Move to datacast in future
final byte[] bytes = this_row[columnIndex - 1];
// Starting with PG 9.0, a new hex format is supported
// that starts with "\x". Figure out which format we're
// dealing with here.
//
if (bytes.length < 2 || bytes[0] != '\\' || bytes[1] !=
'x') {
return new PGByteaTextInputStream_8_4(bytes,
(maxFieldSize > 0 &&
isColumnTrimmable(columnIndex)) ? maxFieldSize : Long.MAX_VALUE);
}else {
if (bytes.length % 2 == 1)
getExceptionFactory().createException(
GT.tr("Unexpected bytea result size, should be
even."),
PSQLState.DATA_ERROR);
return new PGByteaTextInputStream_9_0_1(bytes,
(maxFieldSize > 0 &&
isColumnTrimmable(columnIndex)) ? maxFieldSize : Long.MAX_VALUE);
}
}else {
return new ByteArrayInputStream(getBytes(columnIndex));
}
} else {
// In 7.1 Handle as BLOBS so return the LargeObject input
stream
if ( fields[columnIndex - 1].getOID() == Oid.OID)
{
LargeObjectManager lom = connection.getLargeObjectAPI();
LargeObject lob = lom.open(getLong(columnIndex));
return lob.getInputStream();
}
}
return null;
}
On Thu, 25 Nov 2010 00:53:31 +0200, Александър Шопов
<lists@kambanaria.org>
wrote:
> В 16:04 -0600 на 24.11.2010 (ср), Radosław Smogura написа:
>> I see only two possibilities
>> 1. Decrease fetch size, e.g. to 1.
> Even if I do, bytea is potentially 1GB. Plus peaks in usage can still
> smash the heap.
> So refactoring to BLOBs is perhaps the only way out.
> Will the JDBC driver always present bytea InputStream as
> ByteArrayInputStream? No plans to change that? (even if there are, I
> will still have to refactor meanwhile).
> Perhaps this behaviour should be better communicated to DB schema
> designers.
> It seems to me from the Npgsql2.0.11 readme.txt that reading in chunks
> is provided for .Net.
> Is there need to perhaps make patches for this in the jdbc driver?
> Kind regards:
> al_shopov
--
----------
Radosław Smogura
http://www.softperience.eu
Attachments:
pgjdbc-patch-binary-stream.tgz (application/octet-stream, 1.6K) download
^ permalink raw reply [nested|flat] 7+ messages in thread
* Re: Workarounds for getBinaryStream returning ByteArrayInputStream on bytea
2010-11-24 20:50 Workarounds for getBinaryStream returning ByteArrayInputStream on bytea Александър Шопов <lists@kambanaria.org>
2010-11-24 22:04 ` Re: Workarounds for getBinaryStream returning ByteArrayInputStream on bytea Radosław Smogura <rsmogura@softperience.eu>
2010-11-24 22:53 ` Re: Workarounds for getBinaryStream returning ByteArrayInputStream on bytea Александър Шопов <lists@kambanaria.org>
2010-11-26 12:05 ` Re: Workarounds for getBinaryStream returning ByteArrayInputStream on bytea Radosław Smogura <rsmogura@softperience.eu>
@ 2010-11-26 15:25 ` Kris Jurka <books@ejurka.com>
2010-11-26 16:20 ` Re: Workarounds for getBinaryStream returning ByteArrayInputStream on bytea Radosław Smogura <rsmogura@softperience.eu>
0 siblings, 1 reply; 7+ messages in thread
From: Kris Jurka @ 2010-11-26 15:25 UTC (permalink / raw)
To: Radosław Smogura <rsmogura@softperience.eu>; +Cc: Александър Шопов <lists@kambanaria.org>; pgsql-jdbc
On Fri, 26 Nov 2010, Rados?aw Smogura wrote:
> I would like to send few files for getBinaryStream(). So this will work
> much like stream and will don't eat so much heap. I don't copy source
> this_row[i] array, so I don't know how this will do with concur updates,
> (original method doesn't make this when column is not bytea, too). I left
> few comments if we should throw exception on broken streams in 8.4, or just
> silence notify EOF.
The problem is that the whole bytea is still in this_row[i]. The value
isn't being streamed from the server. So yes, you are saving a copy of
the value which does save heap space, but that won't really help the
described problem where many large bytea values are fetched because the
driver will have read and stored them all prior to getBinaryStream being
called.
Kris Jurka
^ permalink raw reply [nested|flat] 7+ messages in thread
* Re: Workarounds for getBinaryStream returning ByteArrayInputStream on bytea
2010-11-24 20:50 Workarounds for getBinaryStream returning ByteArrayInputStream on bytea Александър Шопов <lists@kambanaria.org>
2010-11-24 22:04 ` Re: Workarounds for getBinaryStream returning ByteArrayInputStream on bytea Radosław Smogura <rsmogura@softperience.eu>
2010-11-24 22:53 ` Re: Workarounds for getBinaryStream returning ByteArrayInputStream on bytea Александър Шопов <lists@kambanaria.org>
2010-11-26 12:05 ` Re: Workarounds for getBinaryStream returning ByteArrayInputStream on bytea Radosław Smogura <rsmogura@softperience.eu>
2010-11-26 15:25 ` Re: Workarounds for getBinaryStream returning ByteArrayInputStream on bytea Kris Jurka <books@ejurka.com>
@ 2010-11-26 16:20 ` Radosław Smogura <rsmogura@softperience.eu>
0 siblings, 0 replies; 7+ messages in thread
From: Radosław Smogura @ 2010-11-26 16:20 UTC (permalink / raw)
To: Kris Jurka <books@ejurka.com>; +Cc: Александър Шопов <lists@kambanaria.org>; pgsql-jdbc
On Fri, 26 Nov 2010 10:25:01 -0500 (EST), Kris Jurka <books@ejurka.com>
wrote:
> On Fri, 26 Nov 2010, Rados?aw Smogura wrote:
>
>> I would like to send few files for getBinaryStream(). So this will work
>> much like stream and will don't eat so much heap. I don't copy source
>> this_row[i] array, so I don't know how this will do with concur
updates,
>> (original method doesn't make this when column is not bytea, too). I
left
>> few comments if we should throw exception on broken streams in 8.4, or
>> just
>> silence notify EOF.
>
> The problem is that the whole bytea is still in this_row[i]. The value
> isn't being streamed from the server. So yes, you are saving a copy of
> the value which does save heap space, but that won't really help the
> described problem where many large bytea values are fetched because the
> driver will have read and stored them all prior to getBinaryStream being
> called.
>
> Kris Jurka
Yes indeed it will don't give you "big" heap save, but driver calls in
getBinaryStream() getBytes(), then PGBytea... method. This method
transforms source, text based, array into pure binary array, so it creates
some kind of copy of source, generally smeller (this copy will not be
smaller then source divided by 4). So, when Aleksander compress 1GB files,
I assume he use stream compression, he allocates in addition about
500-800MB on heap for this transformed array, but he doesn't needs it so
big at one time, as compression block isn't larger then 1MB.
It is the way why submitted streams performs "on-line" conversion.
--
----------
Radosław Smogura
http://www.softperience.eu
^ permalink raw reply [nested|flat] 7+ messages in thread
end of thread, other threads:[~2010-11-26 16:20 UTC | newest]
Thread overview: 7+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2010-11-24 20:50 Workarounds for getBinaryStream returning ByteArrayInputStream on bytea Александър Шопов <lists@kambanaria.org>
2010-11-24 22:04 ` Radosław Smogura <rsmogura@softperience.eu>
2010-11-24 22:53 ` Александър Шопов <lists@kambanaria.org>
2010-11-25 09:43 ` Radosław Smogura <rsmogura@softperience.eu>
2010-11-26 12:05 ` Radosław Smogura <rsmogura@softperience.eu>
2010-11-26 15:25 ` Kris Jurka <books@ejurka.com>
2010-11-26 16:20 ` Radosław Smogura <rsmogura@softperience.eu>
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox