Skip to content

Commit 57c03f7

Browse files
fila43Commitfest Bot
authored andcommitted
Add configurable receive buffer size
This commit introduces a new GUC parameter 'pq_recv_buffer_size' to allow users to configure the size of the network receive buffer for each backend connection. The receive buffer is now dynamically allocated instead of using a fixed compile-time size. Key changes: - Make PqRecvBuffer dynamically allocated based on pq_recv_buffer_size GUC - Add pq_recv_buffer_size GUC parameter (default 8KB, min 8KB, max configurable) has enough space Benefits: - Configurable buffer size allows tuning for workloads with large messages - Maintains backward compatibility with 8KB default
1 parent b4cbc10 commit 57c03f7

File tree

5 files changed

+24
-5
lines changed

5 files changed

+24
-5
lines changed

src/backend/libpq/pqcomm.c

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -112,19 +112,21 @@ static List *sock_paths = NIL;
112112
/*
113113
* Buffers for low-level I/O.
114114
*
115-
* The receive buffer is fixed size. Send buffer is usually 8k, but can be
116-
* enlarged by pq_putmessage_noblock() if the message doesn't fit otherwise.
115+
* Both send and receive buffers are dynamically allocated. Send buffer is
116+
* usually 8k, but can be enlarged by pq_putmessage_noblock() if the message
117+
* doesn't fit otherwise. Receive buffer size is configurable via the
118+
* pq_recv_buffer_size GUC parameter.
117119
*/
118120

119121
#define PQ_SEND_BUFFER_SIZE 8192
120-
#define PQ_RECV_BUFFER_SIZE 8192
121122

122123
static char *PqSendBuffer;
123124
static int PqSendBufferSize; /* Size send buffer */
124125
static size_t PqSendPointer; /* Next index to store a byte in PqSendBuffer */
125126
static size_t PqSendStart; /* Next index to send a byte in PqSendBuffer */
126127

127-
static char PqRecvBuffer[PQ_RECV_BUFFER_SIZE];
128+
static char *PqRecvBuffer; /* Dynamically allocated receive buffer */
129+
static int PqRecvBufferSize; /* Size of receive buffer */
128130
static int PqRecvPointer; /* Next index to read a byte from PqRecvBuffer */
129131
static int PqRecvLength; /* End of data available in PqRecvBuffer */
130132

@@ -278,6 +280,8 @@ pq_init(ClientSocket *client_sock)
278280
/* initialize state variables */
279281
PqSendBufferSize = PQ_SEND_BUFFER_SIZE;
280282
PqSendBuffer = MemoryContextAlloc(TopMemoryContext, PqSendBufferSize);
283+
PqRecvBufferSize = pq_recv_buffer_size * 1024; /* GUC is in KB */
284+
PqRecvBuffer = MemoryContextAlloc(TopMemoryContext, PqRecvBufferSize);
281285
PqSendPointer = PqSendStart = PqRecvPointer = PqRecvLength = 0;
282286
PqCommBusy = false;
283287
PqCommReadingMsg = false;
@@ -921,7 +925,7 @@ pq_recvbuf(void)
921925
errno = 0;
922926

923927
r = secure_read(MyProcPort, PqRecvBuffer + PqRecvLength,
924-
PQ_RECV_BUFFER_SIZE - PqRecvLength);
928+
PqRecvBufferSize - PqRecvLength);
925929

926930
if (r < 0)
927931
{

src/backend/utils/init/globals.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,3 +165,6 @@ int notify_buffers = 16;
165165
int serializable_buffers = 32;
166166
int subtransaction_buffers = 0;
167167
int transaction_buffers = 0;
168+
169+
/* network buffer sizes */
170+
int pq_recv_buffer_size = 8;

src/backend/utils/misc/guc_parameters.dat

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2266,6 +2266,16 @@
22662266
max => 'INT_MAX / 1000000',
22672267
},
22682268

2269+
{ name => 'pq_recv_buffer_size', type => 'int', context => 'PGC_BACKEND', group => 'RESOURCES_MEM',
2270+
short_desc => 'Sets the size of the network receive buffer for each backend.',
2271+
long_desc => 'Larger values can improve performance when receiving large messages, but consume more memory per connection.',
2272+
flags => 'GUC_UNIT_KB',
2273+
variable => 'pq_recv_buffer_size',
2274+
boot_val => '8',
2275+
min => '8',
2276+
max => 'MAX_KILOBYTES',
2277+
},
2278+
22692279
# Not for general use
22702280
{ name => 'pre_auth_delay', type => 'int', context => 'PGC_SIGHUP', group => 'DEVELOPER_OPTIONS',
22712281
short_desc => 'Sets the amount of time to wait before authentication on connection startup.',

src/backend/utils/misc/postgresql.conf.sample

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@
136136
#huge_page_size = 0 # zero for system default
137137
# (change requires restart)
138138
#temp_buffers = 8MB # min 800kB
139+
#pq_recv_buffer_size = 8kB # min 8kB
139140
#max_prepared_transactions = 0 # zero disables the feature
140141
# (change requires restart)
141142
# Caution: it is not advisable to set max_prepared_transactions nonzero unless

src/include/miscadmin.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,7 @@ extern PGDLLIMPORT int notify_buffers;
186186
extern PGDLLIMPORT int serializable_buffers;
187187
extern PGDLLIMPORT int subtransaction_buffers;
188188
extern PGDLLIMPORT int transaction_buffers;
189+
extern PGDLLIMPORT int pq_recv_buffer_size;
189190

190191
extern PGDLLIMPORT int MyProcPid;
191192
extern PGDLLIMPORT pg_time_t MyStartTime;

0 commit comments

Comments
 (0)