Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1cscT2-0005IE-B2 for pgsql-jdbc@arkaria.postgresql.org; Mon, 27 Mar 2017 21:46:24 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84_2) (envelope-from ) id 1cscT1-0000fN-42 for pgsql-jdbc@arkaria.postgresql.org; Mon, 27 Mar 2017 21:46:23 +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_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1cscT0-0000fD-Ji for pgsql-jdbc@postgresql.org; Mon, 27 Mar 2017 21:46:22 +0000 Received: from mail-qk0-x231.google.com ([2607:f8b0:400d:c09::231]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.84_2) (envelope-from ) id 1cscSo-0003UA-9l for pgsql-jdbc@postgresql.org; Mon, 27 Mar 2017 21:46:21 +0000 Received: by mail-qk0-x231.google.com with SMTP id f11so50966095qkb.0 for ; Mon, 27 Mar 2017 14:46:09 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=8kdata-com.20150623.gappssmtp.com; s=20150623; h=subject:to:references:from:message-id:date:user-agent:mime-version :in-reply-to:content-transfer-encoding; bh=uFoqMCQmXQnkdGTIUBWgZARljLxNHdPt6ioRCYJZpjE=; b=S+DWKUGe42XUPlaZ9H3a7/aqCvbEAZ0fDZN/ExSMWx1vtDiAGl9trw1Qc5SbovEWyF MaLHzzwW1qyDl++1kOz9/PivNcXnUxkkVNmzJHNIwPuOyqncYB6pJaDZ9wCC4Zbc/02K UgEkYBqekc8dgCmAQGP/AazgOIZtqlmKSj3VWoy8osjTUfcrAR2fjG/tqDkceacGWb+N yL/hR0GVNkPYNEAn/rLUTAIm9ER2A+EifMAghHaLavKMbBkWgZ0Qaf39bhdjGoDR8qoQ Tk1droTc3Qm1rHsWlbwFxyulrNV0rsxugkojwX0aG7fwMAKIi81FSOmSdVXKT5b0VodJ Ut4w== 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; bh=uFoqMCQmXQnkdGTIUBWgZARljLxNHdPt6ioRCYJZpjE=; b=j3rt6Ok0/75PkdDMuprGUIhe2s8X6ajCPXRARJrPxlyKS7nS+SdDGOap9oUPWiI2ic E3RVuTgwCjJTOac7sw6ky45z9lUkARCP3sZ4T878CAAV39TRi+U/z/cT8ejHCpZ9ly1L y6xM820ImD75vadtPD++RiJDNeXgQad56sV6ME7ec1r5vxbMiubGBvrS5kCQwDUarKpd tRSYORwJfNnLJgk/1nXU8tlcqFufSENtbDxOUojgw6jrFFyJf8Q/xF7abiHq1glcjCeu 9BIvP4DLx2L9bjhMF2xg8IWCJoJ01ZjOwLsEYjtfVLju4uYY0IH4sR9YW42L+T6y75jh S/2w== X-Gm-Message-State: AFeK/H2ZU01NsUv/0J1gpiUxqizXutnfqfGqx0fZxHvRtfJHIxeMbtIPP/GfkpN36OkPLA== X-Received: by 10.55.137.193 with SMTP id l184mr2341485qkd.122.1490651168174; Mon, 27 Mar 2017 14:46:08 -0700 (PDT) Received: from [10.51.99.149] ([157.130.19.110]) by smtp.gmail.com with ESMTPSA id 94sm1256424qte.37.2017.03.27.14.46.07 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 27 Mar 2017 14:46:07 -0700 (PDT) Subject: Re: Interest in allowing caller to push binary data rather than having it pulled? To: Tom Dunstan , pgsql-jdbc@postgresql.org References: From: =?UTF-8?Q?=c3=81lvaro_Hern=c3=a1ndez_Tortosa?= Message-ID: Date: Mon, 27 Mar 2017 17:46:07 -0400 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Thunderbird/45.8.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit X-Pg-Spam-Score: -1.8 (-) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-jdbc Precedence: bulk Sender: pgsql-jdbc-owner@postgresql.org On 23/03/17 04:24, Tom Dunstan wrote: > Hi all > > I hit an interesting case today. It’s a bit of a limitation in the JDBC interface, so any support would have to be a proprietary interface. > > Basically I have one or more byte buffers that I’d like to stream into a BYTEA at the server (using a plain INSERT statement). In my case I’ve got Netty ByteBuf objects, but it could be anything. > > What are my current options? JDBC basically gives me PreparedStatement.setBytes() and PreparedStatement.setBinaryStream(). > > PreparedStatement.setBytes() involves copying all the data, potentially multiple large buffers, into a large buffer of exactly the correct size. The reason to use ByteBufs in the first place was to pool our use of large buffers so that we don’t blow out our heap - this completely kills any hope of that. > > PreparedStatement.setBinaryStream() is more flexible, but under the hood we’re just pulling stuff into an intermediary 8k buffer and then writing it out to the socket. This is OK from a heap management perspective, but still has some unnecessary copying. > > What I’d really like to do would be to provide an object that the driver could interrogate for a length and then provide an OutputStream to write to. The interface would look something like: > > interface ByteStreamWriter { > int getLength(); > void writeTo(OutputStream stream); > } > > The provided output stream would be a very thin wrapper around the socket output stream just ensuring that we don’t write too many bytes out. > > Usage would look thusly: > > myPreparedStatement.setObject(n, new MyByteStreamWriter(myByteBuf), Types.VARBINARY); > > And the user could write whatever adapter they wanted around their data. > > There’s an existing StreamWrapper class in the codebase, but it just provides an InputStream when asked. It could be adjusted to use the above interface for consistency though. > > Thoughts? I’d be happy to code up a PR if there’s interest. > > Cheers > > Tom > > > > Hi Tom. I think this is quite a good approach. I've seen significant overheads in heap object creation in the process of serialization/deserialization. Some of them were documented as part of the slides of this talk: https://www.slideshare.net/8kdata/java-and-postgresql-performance-features-and-the-future (starting slide #19). So having an adapter to write the data to the socket without rewriting the bytes is a very desirable goal, in my opinion. Cheers, Álvaro -- Álvaro Hernández Tortosa ----------- <8K>data -- Sent via pgsql-jdbc mailing list (pgsql-jdbc@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-jdbc