Skip to content

Commit 54e22bc

Browse files
committed
Do a blocking flush every 100 calls to put_copy_data
We had a blocking flush in pg-1.3.x at every call to put_copy_data. This made sure, that all data is sent until the next put_copy_data. In #462 (and pg-1.4.0 to .2) the behaviour was changed to rely on the non-blocking flushs libpq is doing internally. This makes a decent performance improvement especially on Windows. Unfortunately #473 proved that memory bloat can happen, when sending the data is slower than calls to put_copy_data happen. As a trade-off this proposes to do a blocking flush only every 100 calls. If libpq is running in blocking mode (PG::Connection.async_api = false) put_copy_data does a blocking flush every time new memory is allocated. Unfortunately we don't have this kind of information, since we don't have access to libpq's PGconn struct and the return codes don't give us an indication when this happens. So doing a flush at every fixed number of calls is a very simple heuristic. Fixes #473
1 parent 28b73d2 commit 54e22bc

File tree

2 files changed

+12
-0
lines changed

2 files changed

+12
-0
lines changed

ext/pg_connection.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,7 @@ pgconn_s_allocate( VALUE klass )
266266
this->encoder_for_put_copy_data = Qnil;
267267
this->decoder_for_get_copy_data = Qnil;
268268
this->trace_stream = Qnil;
269+
rb_ivar_set(self, rb_intern("@calls_to_put_copy_data"), INT2FIX(0));
269270

270271
return self;
271272
}

lib/pg/connection.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -408,7 +408,17 @@ def isnonblocking
408408
# See also #copy_data.
409409
#
410410
def put_copy_data(buffer, encoder=nil)
411+
# sync_put_copy_data does a non-blocking attept to flush data.
411412
until res=sync_put_copy_data(buffer, encoder)
413+
# It didn't flush immediately and allocation of more buffering memory failed.
414+
# Wait for all data sent by doing a blocking flush.
415+
res = flush
416+
end
417+
418+
# And do a blocking flush every 100 calls.
419+
# This is to avoid memory bloat, when sending the data is slower than calls to put_copy_data happen.
420+
if (@calls_to_put_copy_data += 1) > 100
421+
@calls_to_put_copy_data = 0
412422
res = flush
413423
end
414424
res
@@ -431,6 +441,7 @@ def put_copy_end(*args)
431441
until sync_put_copy_end(*args)
432442
flush
433443
end
444+
@calls_to_put_copy_data = 0
434445
flush
435446
end
436447
alias async_put_copy_end put_copy_end

0 commit comments

Comments
 (0)