Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions postgresql_proxy/proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ def _create_pg_connection(self, address, context):
redirect_config = self.instance_config.redirect

pg_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
pg_sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have you measured the impact on larger queries? Will we lose significant performance?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I ran a small benchmark with TCP_NODELAY=1 vs Nagle across payload sizes from 1B to 10MB:

(mean end-to-end latency for a single SELECT query)

Payload TCP_NODELAY=1 Nagle
1 B 0.22ms 0.24ms
100 KB 0.69ms 0.69ms
1 MB 6.75ms 6.68ms
10 MB 634ms 628ms

No meaningful difference for larger queries.
The benchmark did surface a pre-existing bug where large payloads could cause connection hangs. I will open a follow-up PR for this.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PR for the large payload hang #16

pg_sock.connect((redirect_config.host, redirect_config.port))
pg_sock.setblocking(False)

Expand Down Expand Up @@ -130,6 +131,7 @@ def accept_wrapper(self, sock: socket.socket):

# Accept the raw connection
clientsocket, address = sock.accept()
clientsocket.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Question: Would there be a point to enable only for ssl?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think so. Nagle's delay applies to any small-packet exchange, not just SSL. SSL makes the improvement more obvious because of the extra SSLRequest round trip, but auth and ready-for-query messages are small on every connection.

# On macOS, accepted sockets inherit O_NONBLOCK from the listening socket.
# SSL negotiation uses blocking recv, so we must set blocking explicitly here.
clientsocket.setblocking(True)
Expand Down
Loading