pg.ddx.io  pgsql-jdbc@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Configurable Send/Receive Buffer Sizes
5+ messages / 4 participants
[nested] [flat]

* Configurable Send/Receive Buffer Sizes
@ 2011-04-21 14:42 Bernd Helmle <mailings@oopsware.de>
  2011-04-21 14:49 ` Re: Configurable Send/Receive Buffer Sizes Kevin Grittner <Kevin.Grittner@wicourts.gov>
  2011-08-10 17:29 ` Re: Configurable Send/Receive Buffer Sizes Dave Cramer <pg@fastcrypt.com>
  0 siblings, 2 replies; 5+ messages in thread

From: Bernd Helmle @ 2011-04-21 14:42 UTC (permalink / raw)
  To: pgsql-jdbc

Folks,

please find attached a patch which allows to specify the size of TCP 
send/receive buffers (SO_SNDBUF/SO_RECVBUF socket options) in the JDBC driver.

The patch adds two connection parameters to the JDBC driver:

send_buffer_size: Specifes the size of the TCP write buffer used with the 
connection

receive_buffer_size: Specifies the size of the TCP read buffer used with the 
connection

For example:

Connection conn = 
DriverManager.getConnection("jdbc:postgresql://localhost:5432/bernd?loglevel=2&send_buffer_size=262144&receive_buffer_size=1048576", 
"bernd", "");

It occured to me several times now where this functionality became useful, 
especially when working on migration projects in the past, where the 
application couldn't be changed immediately when doing large batch processing. 
An interesting example is the small test program from

<http://archives.postgresql.org/pgsql-jdbc/2008-10/msg00045.php;

which could be make working with the appropiate buffer settings.

Maybe this is worth for discussion or even to investigate it further for 
inclusion.

Note that on Linux you need to adjust the kernel maximum buffer sizes in 
/proc/sys/net/core/wmem_max and /proc/sys/net/core/rmem_max to become any 
settings effective. I have reports that this isn't necessary on Windows, 
however.  This certainly needs adequate documentation.

I didn't invest any time on benchmarks, but maybe there's also some value in it 
performance-wise.

-- 
Thanks

	Bernd

Attachments:

pgjdbc_buffer_sizes.patch (application/octet-stream, 6.0K) download Index: org/postgresql/core/v3/ConnectionFactoryImpl.java =================================================================== RCS file: /cvsroot/jdbc/pgjdbc/org/postgresql/core/v3/ConnectionFactoryImpl.java,v retrieving revision 1.25 diff -c -r1.25 ConnectionFactoryImpl.java *** org/postgresql/core/v3/ConnectionFactoryImpl.java 19 Apr 2011 01:15:21 -0000 1.25 --- org/postgresql/core/v3/ConnectionFactoryImpl.java 21 Apr 2011 14:03:29 -0000 *************** *** 93,98 **** --- 93,137 ---- // Enable TCP keep-alive probe if required. newStream.getSocket().setKeepAlive(requireTCPKeepAlive); + // Try to set SO_SNDBUF and SO_RECVBUF socket options, if requested. + // If receive_buffer_size and send_buffer_size are set to a value greater + // than 0, adjust. -1 means use the system default, 0 is ignored since not + // supported. + + // Set SO_RECVBUF read buffer size + String receiveBufferSizeProperty = info.getProperty("receive_buffer_size", "-1"); + try { + int receiveBufferSize = Integer.parseInt(receiveBufferSizeProperty); + if (receiveBufferSize > -1) { + // value of 0 not a valid buffer size value + if (receiveBufferSize > 0) { + newStream.getSocket().setReceiveBufferSize(receiveBufferSize); + } else { + logger.info("Ignore invalid value for receive_buffer_size: " + receiveBufferSize); + } + } + } catch (NumberFormatException nfe) { + logger.info("Couldn't parse receive_buffer_size value: " + receiveBufferSizeProperty); + } + + // Set SO_SNDBUF write buffer size + String sendBufferSizeProperty = info.getProperty("send_buffer_size", "-1"); + try { + int sendBufferSize = Integer.parseInt(sendBufferSizeProperty); + if (sendBufferSize > -1) { + if (sendBufferSize > 0) { + newStream.getSocket().setSendBufferSize(sendBufferSize); + } else { + logger.info("Ignore invalid value for send_buffer_size: " + sendBufferSize); + } + } + } catch (NumberFormatException nfe) { + logger.info("Couldn't parse send_buffer_size value: " + sendBufferSizeProperty); + } + + logger.debug("Receive Buffer Size is " + newStream.getSocket().getReceiveBufferSize()); + logger.debug("Send Buffer Size is " + newStream.getSocket().getSendBufferSize()); + // Construct and send a startup packet. String[][] params = { { "user", user }, Index: org/postgresql/ds/common/BaseDataSource.java =================================================================== RCS file: /cvsroot/jdbc/pgjdbc/org/postgresql/ds/common/BaseDataSource.java,v retrieving revision 1.22 diff -c -r1.22 BaseDataSource.java *** org/postgresql/ds/common/BaseDataSource.java 25 Dec 2010 22:57:23 -0000 1.22 --- org/postgresql/ds/common/BaseDataSource.java 21 Apr 2011 14:03:30 -0000 *************** *** 53,58 **** --- 53,60 ---- private int unknownLength = Integer.MAX_VALUE; private int loginTimeout = 0; // in seconds private int socketTimeout = 0; // in seconds + private int receive_buffer_size = -1; // off (-1), not in use + private int send_buffer_size = -1; // off (-1), not in use private boolean ssl = false; private String sslfactory; private boolean tcpKeepAlive = false; *************** *** 292,297 **** --- 294,315 ---- } /** + * Sets the write buffer size of TCP/IP socket. + */ + public void setReceiveBufferSize(int nbytes) + { + this.receive_buffer_size = nbytes; + } + + /** + * Sets the send buffer size of TCP/IP socket. + */ + public void setSendBufferSize(int nbytes) + { + this.send_buffer_size = nbytes; + } + + /** * Gets the default threshold for enabling server-side prepare. * * @see #setPrepareThreshold(int) *************** *** 424,429 **** --- 442,450 ---- sb.append(applicationName); } + sb.append("&receive_buffer_size=").append(receive_buffer_size); + sb.append("&send_buffer_size=").append(send_buffer_size); + return sb.toString(); } *************** *** 473,478 **** --- 494,502 ---- ref.add(new StringRefAddr("protocolVersion", Integer.toString(protocolVersion))); ref.add(new StringRefAddr("ApplicationName", applicationName)); + ref.add(new StringRefAddr("receive_buffer_size", Integer.toString(receive_buffer_size))); + ref.add(new StringRefAddr("send_buffer_size", Integer.toString(send_buffer_size))); + return ref; } *************** *** 494,499 **** --- 518,525 ---- out.writeInt(logLevel); out.writeInt(protocolVersion); out.writeObject(applicationName); + out.writeInt(receive_buffer_size); + out.writeInt(send_buffer_size); } protected void readBaseObject(ObjectInputStream in) throws IOException, ClassNotFoundException *************** *** 513,519 **** compatible = (String)in.readObject(); logLevel = in.readInt(); protocolVersion = in.readInt(); ! applicationName = (String)in.readObject(); } public void initializeFrom(BaseDataSource source) throws IOException, ClassNotFoundException { --- 539,547 ---- compatible = (String)in.readObject(); logLevel = in.readInt(); protocolVersion = in.readInt(); ! applicationName = (String)in.readObject(); ! receive_buffer_size = in.readInt(); ! send_buffer_size = in.readInt(); } public void initializeFrom(BaseDataSource source) throws IOException, ClassNotFoundException {
^ permalink raw reply [nested|flat] 5+ messages in thread

* Re: Configurable Send/Receive Buffer Sizes
  2011-04-21 14:42 Configurable Send/Receive Buffer Sizes Bernd Helmle <mailings@oopsware.de>
@ 2011-04-21 14:49 ` Kevin Grittner <Kevin.Grittner@wicourts.gov>
  2011-04-21 15:16   ` Re: Configurable Send/Receive Buffer Sizes Bernd Helmle <mailings@oopsware.de>
  1 sibling, 1 reply; 5+ messages in thread

From: Kevin Grittner @ 2011-04-21 14:49 UTC (permalink / raw)
  To: Bernd Helmle <mailings@oopsware.de>; pgsql-jdbc

Bernd Helmle <mailings@oopsware.de> wrote:
 
> An interesting example is the small test program from
> 
> <http://archives.postgresql.org/pgsql-jdbc/2008-10/msg00045.php;
> 
> which could be make working with the appropiate buffer settings.
 
> I didn't invest any time on benchmarks, but maybe there's also
> some value in it performance-wise.
 
So the primary point is to avoid deadlocks on full network buffers?
 
-Kevin



^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* Re: Configurable Send/Receive Buffer Sizes
  2011-04-21 14:42 Configurable Send/Receive Buffer Sizes Bernd Helmle <mailings@oopsware.de>
  2011-04-21 14:49 ` Re: Configurable Send/Receive Buffer Sizes Kevin Grittner <Kevin.Grittner@wicourts.gov>
@ 2011-04-21 15:16   ` Bernd Helmle <mailings@oopsware.de>
  2011-04-27 12:02     ` Re: Configurable Send/Receive Buffer Sizes Daniel Migowski <dmigowski@ikoffice.de>
  0 siblings, 1 reply; 5+ messages in thread

From: Bernd Helmle @ 2011-04-21 15:16 UTC (permalink / raw)
  To: Kevin Grittner <Kevin.Grittner@wicourts.gov>; pgsql-jdbc



--On 21. April 2011 09:49:54 -0500 Kevin Grittner <Kevin.Grittner@wicourts.gov> 
wrote:

> Bernd Helmle <mailings@oopsware.de> wrote:
>
>> An interesting example is the small test program from
>>
>> <http://archives.postgresql.org/pgsql-jdbc/2008-10/msg00045.php;
>>
>> which could be make working with the appropiate buffer settings.
>
>> I didn't invest any time on benchmarks, but maybe there's also
>> some value in it performance-wise.
>
> So the primary point is to avoid deadlocks on full network buffers?
>

Yeah, that was the primary idea...or, well, to workaround them. You could argue 
that its still possible to run into those issue, but less likely. With the 
proper settings it didn't occur to me again, though.


-- 
Thanks

	Bernd





^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* Re: Configurable Send/Receive Buffer Sizes
  2011-04-21 14:42 Configurable Send/Receive Buffer Sizes Bernd Helmle <mailings@oopsware.de>
  2011-04-21 14:49 ` Re: Configurable Send/Receive Buffer Sizes Kevin Grittner <Kevin.Grittner@wicourts.gov>
  2011-04-21 15:16   ` Re: Configurable Send/Receive Buffer Sizes Bernd Helmle <mailings@oopsware.de>
@ 2011-04-27 12:02     ` Daniel Migowski <dmigowski@ikoffice.de>
  0 siblings, 0 replies; 5+ messages in thread

From: Daniel Migowski @ 2011-04-27 12:02 UTC (permalink / raw)
  To: pgsql-jdbc

I fully support this request! I currently have a overridden PGStream in my Classpath to be able to set it manually to 128k. Else our update scripts hang for the same reason. Making this configurable would be of great help.

Regards,
Daniel Migowski


________________________________________
Von: pgsql-jdbc-owner@postgresql.org [pgsql-jdbc-owner@postgresql.org]&quot; im Auftrag von &quot;Bernd Helmle [mailings@oopsware.de]
Gesendet: Donnerstag, 21. April 2011 17:16
An: Kevin Grittner; pgsql-jdbc@postgresql.org
Betreff: Re: [JDBC] Configurable Send/Receive Buffer Sizes

--On 21. April 2011 09:49:54 -0500 Kevin Grittner <Kevin.Grittner@wicourts.gov>
wrote:

> Bernd Helmle <mailings@oopsware.de> wrote:
>
>> An interesting example is the small test program from
>>
>> <http://archives.postgresql.org/pgsql-jdbc/2008-10/msg00045.php;
>>
>> which could be make working with the appropiate buffer settings.
>
>> I didn't invest any time on benchmarks, but maybe there's also
>> some value in it performance-wise.
>
> So the primary point is to avoid deadlocks on full network buffers?
>

Yeah, that was the primary idea...or, well, to workaround them. You could argue
that its still possible to run into those issue, but less likely. With the
proper settings it didn't occur to me again, though.


--
Thanks

        Bernd



--
Sent via pgsql-jdbc mailing list (pgsql-jdbc@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-jdbc



^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* Re: Configurable Send/Receive Buffer Sizes
  2011-04-21 14:42 Configurable Send/Receive Buffer Sizes Bernd Helmle <mailings@oopsware.de>
@ 2011-08-10 17:29 ` Dave Cramer <pg@fastcrypt.com>
  1 sibling, 0 replies; 5+ messages in thread

From: Dave Cramer @ 2011-08-10 17:29 UTC (permalink / raw)
  To: Bernd Helmle <mailings@oopsware.de>; +Cc: pgsql-jdbc

Is there any objection to this patch ?

Otherwise I will look at committing it .

Dave Cramer

dave.cramer(at)credativ(dot)ca
http://www.credativ.ca




On Thu, Apr 21, 2011 at 10:42 AM, Bernd Helmle <mailings@oopsware.de> wrote:
> Folks,
>
> please find attached a patch which allows to specify the size of TCP
> send/receive buffers (SO_SNDBUF/SO_RECVBUF socket options) in the JDBC
> driver.
>
> The patch adds two connection parameters to the JDBC driver:
>
> send_buffer_size: Specifes the size of the TCP write buffer used with the
> connection
>
> receive_buffer_size: Specifies the size of the TCP read buffer used with the
> connection
>
> For example:
>
> Connection conn =
> DriverManager.getConnection("jdbc:postgresql://localhost:5432/bernd?loglevel=2&send_buffer_size=262144&receive_buffer_size=1048576",
> "bernd", "");
>
> It occured to me several times now where this functionality became useful,
> especially when working on migration projects in the past, where the
> application couldn't be changed immediately when doing large batch
> processing. An interesting example is the small test program from
>
> <http://archives.postgresql.org/pgsql-jdbc/2008-10/msg00045.php;
>
> which could be make working with the appropiate buffer settings.
>
> Maybe this is worth for discussion or even to investigate it further for
> inclusion.
>
> Note that on Linux you need to adjust the kernel maximum buffer sizes in
> /proc/sys/net/core/wmem_max and /proc/sys/net/core/rmem_max to become any
> settings effective. I have reports that this isn't necessary on Windows,
> however.  This certainly needs adequate documentation.
>
> I didn't invest any time on benchmarks, but maybe there's also some value in
> it performance-wise.
>
> --
> Thanks
>
>        Bernd
>
> --
> Sent via pgsql-jdbc mailing list (pgsql-jdbc@postgresql.org)
> To make changes to your subscription:
> http://www.postgresql.org/mailpref/pgsql-jdbc
>
>



^ permalink  raw  reply  [nested|flat] 5+ messages in thread


end of thread, other threads:[~2011-08-10 17:29 UTC | newest]

Thread overview: 5+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2011-04-21 14:42 Configurable Send/Receive Buffer Sizes Bernd Helmle <mailings@oopsware.de>
2011-04-21 14:49 ` Kevin Grittner <Kevin.Grittner@wicourts.gov>
2011-04-21 15:16   ` Bernd Helmle <mailings@oopsware.de>
2011-04-27 12:02     ` Daniel Migowski <dmigowski@ikoffice.de>
2011-08-10 17:29 ` Dave Cramer <pg@fastcrypt.com>

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