Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1YgGM5-0007Cg-Su for pgsql-bugs@arkaria.postgresql.org; Thu, 09 Apr 2015 17:35:06 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1YgGM4-0005qz-DJ for pgsql-bugs@arkaria.postgresql.org; Thu, 09 Apr 2015 17:35:04 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:DHE_RSA_AES_256_CBC_SHA256:256) (Exim 4.80) (envelope-from ) id 1YgGM3-0005qt-O7 for pgsql-bugs@postgresql.org; Thu, 09 Apr 2015 17:35:03 +0000 Received: from mail.anarazel.de ([217.115.131.40]) by magus.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1YgGLz-00054d-S2 for pgsql-bugs@postgresql.org; Thu, 09 Apr 2015 17:35:02 +0000 Received: by mail.anarazel.de (Postfix, from userid 108) id 24ABA860001; Thu, 9 Apr 2015 19:34:59 +0200 (CEST) X-Spam-Checker-Version: SpamAssassin 3.2.5 (2008-06-10) on mail X-Spam-Level: X-Spam-Status: No, score=-6.3 required=5.0 tests=ALL_TRUSTED,AWL,BAYES_00 autolearn=ham version=3.2.5 Received: from anarazel.de (p5DDC6862.dip0.t-ipconnect.de [93.220.104.98]) (Authenticated sender: andres@anarazel.de) by mail.anarazel.de (Postfix) with ESMTPSA id 13E8D460001; Thu, 9 Apr 2015 19:34:56 +0200 (CEST) Received: by anarazel.de (Postfix, from userid 1000) id 6D696100343; Thu, 9 Apr 2015 19:34:55 +0200 (CEST) Date: Thu, 9 Apr 2015 19:34:55 +0200 From: Andres Freund To: Peter Slavov Cc: pgsql-bugs@postgresql.org Subject: Re: BUG #12910: Memory leak with logical decoding Message-ID: <20150409173455.GE9764@awork2.anarazel.de> References: <20150327094757.2488.58808@wrigleys.postgresql.org> <20150331144048.GL4878@alap3.anarazel.de> <55227D87.9080007@gmail.com> <20150406135026.GJ17586@awork2.anarazel.de> <5526969F.4050704@gmail.com> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="W/nzBZO5zC0uMSeA" Content-Disposition: inline In-Reply-To: <5526969F.4050704@gmail.com> X-Pg-Spam-Score: -4.2 (----) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-bugs Precedence: bulk Sender: pgsql-bugs-owner@postgresql.org --W/nzBZO5zC0uMSeA Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Hi, On 2015-04-09 18:11:27 +0300, Peter Slavov wrote: > I prepared a test case, that can reproduce the problem. Yup. I can reproduce it... I did not (yet) have the time to run the test to completion, but i believe the attached patch should fix the problem (and also improve performance a bit...). Using the SQL interface for such large transactions isn't going to be fun as all of the data, due to the nature of the set returning function implementation in postgres, will be additionally written into a tuplestore. The streaming interface doesn't have that behaviour. Additionally it's probably not a good idea to stream such a large resultset via SELECT using psql - IIRC it'll try to store all that data in memory :). Try something like \copy (select * from pg_logical_slot_peek_changes('testing', null, 1)) TO /tmp/f or such. Greetings, Andres Freund --W/nzBZO5zC0uMSeA Content-Type: text/x-diff; charset=us-ascii Content-Disposition: attachment; filename="logical-sql-output-leak.diff" diff --git a/src/backend/replication/logical/logicalfuncs.c b/src/backend/replication/logical/logicalfuncs.c index 3be5263..039bcb1 100644 --- a/src/backend/replication/logical/logicalfuncs.c +++ b/src/backend/replication/logical/logicalfuncs.c @@ -72,6 +72,7 @@ LogicalOutputWrite(LogicalDecodingContext *ctx, XLogRecPtr lsn, TransactionId xi Datum values[3]; bool nulls[3]; DecodingOutputState *p; + text *output; /* SQL Datums can only be of a limited length... */ if (ctx->out->len > MaxAllocSize - VARHDRSZ) @@ -93,10 +94,11 @@ LogicalOutputWrite(LogicalDecodingContext *ctx, XLogRecPtr lsn, TransactionId xi false)); /* ick, but cstring_to_text_with_len works for bytea perfectly fine */ - values[2] = PointerGetDatum( - cstring_to_text_with_len(ctx->out->data, ctx->out->len)); + output = cstring_to_text_with_len(ctx->out->data, ctx->out->len); + values[2] = PointerGetDatum(output); tuplestore_putvalues(p->tupstore, p->tupdesc, values, nulls); + pfree(output); p->returned_rows++; } diff --git a/src/backend/replication/logical/reorderbuffer.c b/src/backend/replication/logical/reorderbuffer.c index 20bb3b7..a004041 100644 --- a/src/backend/replication/logical/reorderbuffer.c +++ b/src/backend/replication/logical/reorderbuffer.c @@ -1936,7 +1936,7 @@ ReorderBufferSerializeTXN(ReorderBuffer *rb, ReorderBufferTXN *txn) * store in segment in which it belongs by start lsn, don't split over * multiple segments tho */ - if (fd == -1 || XLByteInSeg(change->lsn, curOpenSegNo)) + if (fd == -1 || !XLByteInSeg(change->lsn, curOpenSegNo)) { XLogRecPtr recptr; --W/nzBZO5zC0uMSeA Content-Type: text/plain Content-Disposition: inline Content-Transfer-Encoding: 8bit MIME-Version: 1.0 -- Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-bugs --W/nzBZO5zC0uMSeA--