Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1dP5tj-00082i-B3 for pgsql-hackers@arkaria.postgresql.org; Sun, 25 Jun 2017 11:40:11 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84_2) (envelope-from ) id 1dP5ti-0001II-Dd for pgsql-hackers@arkaria.postgresql.org; Sun, 25 Jun 2017 11:40:10 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1dP5s9-0006rS-8J for pgsql-hackers@postgresql.org; Sun, 25 Jun 2017 11:38:33 +0000 Received: from mail-wr0-x22d.google.com ([2a00:1450:400c:c0c::22d]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.84_2) (envelope-from ) id 1dP5s1-00074t-BB for pgsql-hackers@postgresql.org; Sun, 25 Jun 2017 11:38:32 +0000 Received: by mail-wr0-x22d.google.com with SMTP id 77so118343296wrb.1 for ; Sun, 25 Jun 2017 04:38:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=2ndquadrant-com.20150623.gappssmtp.com; s=20150623; h=subject:from:to:cc:references:message-id:date:user-agent :mime-version:in-reply-to:content-language; bh=svTpd2Cfp9xQAO5emQ5q5ceUXO7dhCu46pu4Y26eJWo=; b=wuWPTfBWY0mhl2chDhT94+URO+A3R0WzRopZulSafaAooym1PqvpiWDM/HFWFbsOiN BXEmfAoLVXe5Npze08h11p2fCwcm2TornpdLEYql4G0reoYX9k6Rjx9bUr2aXQdBSFFd 437Zq6Opj6S+uquQLHkw8Ch3DLA6RWqCroaiQIpZJPDG7z7S15/PKUYoWkuPMfv5Di/c 41H8W9VxwGmgJ4ZQs4ijIPg/Triu96Fj4rAyEPfREUfzxx1IhCu+Gbz54g/u1WYYcqIQ OMPs5H+/FU9WBuCnIirYg2UHLEME+uxGzjX6ftcu+vHK9W0olawFzykp+wWUdgFlk+VT yqiA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:from:to:cc:references:message-id:date :user-agent:mime-version:in-reply-to:content-language; bh=svTpd2Cfp9xQAO5emQ5q5ceUXO7dhCu46pu4Y26eJWo=; b=n32r47zpsfvs8EzFo+R8yg19dA5BxJS1EW3PFG0lv/4Sw/u0RX2rIVmKGcUDEVkAf1 6YWGFjKEzDo9JplWyYG4lDILTXpycqf7LkLcNSK4PbQii6G9Z7xpx6Z5GXfPWhx2bc+P tCW5DjcSjIzv3dmswhTa6Qdx4JGK8MO9Ywa50Sm//RAoHYd+miaVuwxcFa6qfwjPNjoP H6F7gU+4ASNwEWLgf53ZOig5ur3HSLUNgaFSSihpEPx0ZMPf38YaIN9D01BdS5ZNY59W y+mpY8UPsAIvkpS4i8VFg7ZNdA/OaQ78aDxDu9h/g6eOMZcdSYBEqw9Hfjegz2VW21IX 4cAw== X-Gm-Message-State: AKS2vOzpBc2KDIMl1jv6W21gXUF6hJxc4OLUCoYVqg4FHXHuTiS7voeo 8Yw4nPWaKYS7npoz X-Received: by 10.223.160.178 with SMTP id m47mr11630877wrm.194.1498390703712; Sun, 25 Jun 2017 04:38:23 -0700 (PDT) Received: from [192.168.0.105] (150.123.broadband11.iol.cz. [90.178.123.150]) by smtp.gmail.com with ESMTPSA id 24sm16390201wrw.0.2017.06.25.04.38.21 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sun, 25 Jun 2017 04:38:21 -0700 (PDT) Subject: Re: Walsender timeouts and large transactions From: Petr Jelinek To: Kyotaro HORIGUCHI Cc: pgsql-hackers@postgresql.org, andres@anarazel.de References: <20170530.180219.188282269.horiguchi.kyotaro@lab.ntt.co.jp> <97e5e70a-87d2-dee1-0482-7e3609822095@2ndquadrant.com> Message-ID: Date: Sun, 25 Jun 2017 13:38:20 +0200 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.1.1 MIME-Version: 1.0 In-Reply-To: <97e5e70a-87d2-dee1-0482-7e3609822095@2ndquadrant.com> Content-Type: multipart/mixed; boundary="------------9EF4F486D202F5D9D71FE67F" Content-Language: en-US List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-hackers Precedence: bulk Sender: pgsql-hackers-owner@postgresql.org This is a multi-part message in MIME format. --------------9EF4F486D202F5D9D71FE67F Content-Type: text/plain; charset=windows-1252 Content-Transfer-Encoding: 7bit On 30/05/17 15:44, Petr Jelinek wrote: > On 30/05/17 11:02, Kyotaro HORIGUCHI wrote: >> >> + TimestampTz now = GetCurrentTimestamp(); >> >> I think it is not recommended to read the current time too >> frequently, especially within a loop that hates slowness. (I >> suppose that a loop that can fill up a send queue falls into that > > Yeah that was my main worry for the patch as well, although given that > the loop does tuple processing it might not be very noticeable. > I realized we actually call GetCurrentTimestamp() there anyway (for the pq_sendint64). So I just modified the patch to use the now variable there instead which means there are no additional GetCurrentTimestamp() calls compared to state before patch now. -- Petr Jelinek http://www.2ndQuadrant.com/ PostgreSQL Development, 24x7 Support, Training & Services --------------9EF4F486D202F5D9D71FE67F Content-Type: text/x-patch; name="v2-0001-Fix-walsender-timeouts-when-decoding-large-transacti.patch" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename*0="v2-0001-Fix-walsender-timeouts-when-decoding-large-transacti"; filename*1=".patch" From c6e9cdb81311371662c580db42a165ab0575d951 Mon Sep 17 00:00:00 2001 From: Petr Jelinek Date: Thu, 25 May 2017 17:35:24 +0200 Subject: [PATCH] Fix walsender timeouts when decoding large transaction The logical slots have fast code path for sending data in order to not impose too high per message overhead. The fast path skips checks for interrupts and timeouts. However the fast path failed to consider the fact that transaction with large number of changes may take very long to be processed and sent to the client. This causes walsender to ignore interrupts for potentially long time but more importantly it will cause walsender being killed due to timeout at the end of such transaction. This commit changes the fast path to also check for interrupts and only allows calling the fast path when last keeplaive check happened less than half of walsender timeout ago, otherwise the slower code path will be taken. --- src/backend/replication/walsender.c | 36 ++++++++++++++++++++++-------------- 1 file changed, 22 insertions(+), 14 deletions(-) diff --git a/src/backend/replication/walsender.c b/src/backend/replication/walsender.c index f845180..278f882 100644 --- a/src/backend/replication/walsender.c +++ b/src/backend/replication/walsender.c @@ -1160,6 +1160,8 @@ static void WalSndWriteData(LogicalDecodingContext *ctx, XLogRecPtr lsn, TransactionId xid, bool last_write) { + TimestampTz now = GetCurrentTimestamp(); + /* output previously gathered data in a CopyData packet */ pq_putmessage_noblock('d', ctx->out->data, ctx->out->len); @@ -1169,23 +1171,28 @@ WalSndWriteData(LogicalDecodingContext *ctx, XLogRecPtr lsn, TransactionId xid, * several releases by streaming physical replication. */ resetStringInfo(&tmpbuf); - pq_sendint64(&tmpbuf, GetCurrentTimestamp()); + pq_sendint64(&tmpbuf, now); memcpy(&ctx->out->data[1 + sizeof(int64) + sizeof(int64)], tmpbuf.data, sizeof(int64)); - /* fast path */ - /* Try to flush pending output to the client */ - if (pq_flush_if_writable() != 0) - WalSndShutdown(); + /* Try taking fast path unless we get too close to walsender timeout. */ + if (now < TimestampTzPlusMilliseconds(last_reply_timestamp, + wal_sender_timeout / 2)) + { + CHECK_FOR_INTERRUPTS(); - if (!pq_is_send_pending()) - return; + /* Try to flush pending output to the client */ + if (pq_flush_if_writable() != 0) + WalSndShutdown(); + + if (!pq_is_send_pending()) + return; + } for (;;) { int wakeEvents; long sleeptime; - TimestampTz now; /* * Emergency bailout if postmaster has died. This is to avoid the @@ -1214,18 +1221,16 @@ WalSndWriteData(LogicalDecodingContext *ctx, XLogRecPtr lsn, TransactionId xid, if (pq_flush_if_writable() != 0) WalSndShutdown(); - /* If we finished clearing the buffered data, we're done here. */ - if (!pq_is_send_pending()) - break; - - now = GetCurrentTimestamp(); - /* die if timeout was reached */ WalSndCheckTimeOut(now); /* Send keepalive if the time has come */ WalSndKeepaliveIfNecessary(now); + /* If we finished clearing the buffered data, we're done here. */ + if (!pq_is_send_pending()) + break; + sleeptime = WalSndComputeSleeptime(now); wakeEvents = WL_LATCH_SET | WL_POSTMASTER_DEATH | @@ -1235,6 +1240,9 @@ WalSndWriteData(LogicalDecodingContext *ctx, XLogRecPtr lsn, TransactionId xid, WaitLatchOrSocket(MyLatch, wakeEvents, MyProcPort->sock, sleeptime, WAIT_EVENT_WAL_SENDER_WRITE_DATA); + + /* Update our idea of current time for the next cycle. */ + now = GetCurrentTimestamp(); } /* reactivate latch so WalSndLoop knows to continue */ -- 2.7.4 --------------9EF4F486D202F5D9D71FE67F Content-Type: text/plain Content-Disposition: inline Content-Transfer-Encoding: 8bit MIME-Version: 1.0 -- Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-hackers --------------9EF4F486D202F5D9D71FE67F--