pg.ddx.io  pgsql-jdbc@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Bernd Helmle <mailings@oopsware.de>
To: pgsql-jdbc@postgresql.org
Subject: Configurable Send/Receive Buffer Sizes
Date: Thu, 21 Apr 2011 16:42:07 +0200
Message-ID: <88D436FBC188677633204A9D@apophis.local> (raw)

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 {

view thread (5+ messages)  latest in thread

Message-ID: <88D436FBC188677633204A9D@apophis.local>
Permalink:  ../88D436FBC188677633204A9D@apophis.local/
Also on:    postgresql.org/message-id/88D436FBC188677633204A9D@apophis.local

 ·  · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-jdbc@postgresql.org
  Cc: mailings@oopsware.de
  Subject: Re: Configurable Send/Receive Buffer Sizes
  In-Reply-To: <88D436FBC188677633204A9D@apophis.local>
  References: <88D436FBC188677633204A9D@apophis.local>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

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