Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1e9uKh-00076n-MN for pgsql-hackers@arkaria.postgresql.org; Wed, 01 Nov 2017 14:49:31 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84_2) (envelope-from ) id 1e9uKh-00061g-90 for pgsql-hackers@arkaria.postgresql.org; Wed, 01 Nov 2017 14:49:31 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1e9uKe-0005yc-U9 for pgsql-hackers@postgresql.org; Wed, 01 Nov 2017 14:49:29 +0000 Received: from mail-lf0-x243.google.com ([2a00:1450:4010:c07::243]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.84_2) (envelope-from ) id 1e9uKX-0004A7-3h for pgsql-hackers@postgresql.org; Wed, 01 Nov 2017 14:49:27 +0000 Received: by mail-lf0-x243.google.com with SMTP id g70so2827754lfl.3 for ; Wed, 01 Nov 2017 07:49:20 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=2ndquadrant-com.20150623.gappssmtp.com; s=20150623; h=subject:to:cc:references:from:message-id:date:user-agent :mime-version:in-reply-to:content-language; bh=x9REmLjWs1Yl57KSIOEEnAa/ONZU4+eQlpw+Wa0lYn8=; b=PQhVxFD1dxuVr+gqzbDx9lHVIWKUjteOnJmPW9ITaNzqrWPaRZcw3TokeBdW2MxYRH R+WGAceBLYKpdU3q2UbMMXoDmg7wXUSczApPNQT1skS58yFcJXBym2exh06pUQVxg27E mtGvsShFaEfOtu0Cfh9IwjuOAvssGGX8npWTuoav86wefDEb33jz5ybdJmDTjFpiWLR/ Wt6lEGbm6Q+ei46+k9yS+UeXOGneP1m3qq6tja/scPCAOG0R0f9el1e1fgILYuPT5KS5 5Ipqi6kSpD+OOkcWUN0VRkChABh1dEYSPngNv7Uv6Qh5LTV2c0N8MipMmi4IpMKXeIP4 RVvg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:cc:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-language; bh=x9REmLjWs1Yl57KSIOEEnAa/ONZU4+eQlpw+Wa0lYn8=; b=uhr92cVDxYuuvTRZ18EPD/ych4hku/EkzRWdKCSBVxGAR0trQaKrYeCNkGBFwIaN7h TuKIEV4NZ1JkLnRcQSgiUQvVhsuYF72xgMDN44SB7PA4N7icMC7RGquAgptts2yekNP0 TpH4cekDZG9pm+6n23iRV4rgDcwPtmy0jKB4jSsGwcZ+CJzm4nC6Qd2Vand5WLwLa9lA N2oWs3QsoxayNlZ+sBDI+5vEB9DmC5fXh10wcrTHvDGTM5LOAy0c2Fh2TS9p4SiQW1wO K+3ErEX3QzyO/mayrUXdJ4ezOOBZan8radzuOOxronf68CVG3zmS/GDnGcwBDl1nU+U4 LC8w== X-Gm-Message-State: AMCzsaV0pRz1HeSbmLFZq8xMnb6/76frgduc5rIcPb6q47mQxcCgmjS4 W74QQVpDRNtlj2ENPFQ2YueO+A== X-Google-Smtp-Source: ABhQp+S24RbVkSRB6QsL47pRi/+DCrPb9qolkDfGgMB2snit63g719l2WEw0ErqdQnRFU+okyWR6KQ== X-Received: by 10.46.43.205 with SMTP id r74mr29437ljr.159.1509547759025; Wed, 01 Nov 2017 07:49:19 -0700 (PDT) Received: from [10.23.73.24] ([212.24.157.174]) by smtp.gmail.com with ESMTPSA id l194sm134837lfl.11.2017.11.01.07.49.17 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 01 Nov 2017 07:49:17 -0700 (PDT) Subject: Re: Walsender timeouts and large transactions To: Kyotaro HORIGUCHI , funny.falcon@postgrespro.ru Cc: pgsql-hackers@postgresql.org, pjmodos@pjmodos.net References: <90bb67da7131e6186b50897c4b0f0ec3@postgrespro.ru> <20170929.172635.133003609.horiguchi.kyotaro@lab.ntt.co.jp> <20171002.124422.224472998.horiguchi.kyotaro@lab.ntt.co.jp> From: Petr Jelinek Message-ID: <9f192946-aa3a-dbb2-768b-ee17ebc8c7eb@2ndquadrant.com> Date: Wed, 1 Nov 2017 15:49:17 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.4.0 MIME-Version: 1.0 In-Reply-To: <20171002.124422.224472998.horiguchi.kyotaro@lab.ntt.co.jp> Content-Type: multipart/mixed; boundary="------------71D32E10CDDF256DC353ABE9" 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. --------------71D32E10CDDF256DC353ABE9 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit Hi, sorry for the delay but I didn't have much time in past weeks to follow this thread. On 02/10/17 05:44, Kyotaro HORIGUCHI wrote: > Hello Sokolov. > > At Fri, 29 Sep 2017 15:19:23 +0300, Sokolov Yura wrote in >> I don't want to make test to lasts so long and generate so many data. >> That is why I used such small timeouts for tests. > > I understand your point, but still *I* think such a short timeout > is out of expectation by design. (But it can be set.) > > Does anyone have opinions on this? Yes, it's not practically useful to have such small wal_sender_timeout given that the main purpose of that is to detect network issues. > >> Test is failing if there is "short quit" after >> `!pq_is_send_pending()`, >> so I doubt your patch will pass the test. > > It is because I think that the test "should" fail since the > timeout is out of expected range. I (and perhaps also Petr) is > thinking that the problem is just that a large transaction causes > a timeout with an ordinary timeout. My test case is based on the > assumption. > > Your test is for a timeout during replication-startup with > extremely short timeout. This may be a different problem to > discuss, but perhaps better to be solved together. > > I'd like to have opnions from others on this point. I think it's different problem and because of what I wrote above it does not seem to me like something we should spend out time on trying to fix. > > Uggh! I misunderstood there. It wais for writing socket so the > sleep is wrong and WaitLatchOrSocket is right. > > After all, I put +1 for Petr's latest patch. Sorry for my > carelessness. > Great, I attached version 3 which is just rebased on current master and also does not move the GetCurrentTimestamp() call because the concern about skewing the timestamp during config reload (and also network flush as I realized later) is valid. It's rather hard to test all the scenarios that this patch fixes in automated fashion without generating a lot of wal or adding sleeps to the processing. That's why I didn't produce usable TAP test. Since it seems like some of my reasoning is unclear I will try to describe it once more. The main problem we have is that unless we call the ProcessRepliesIfAny() before the wal_sender_timeout expires we'll die because of timeout eventually. The current coding will skip that call if there is a long transaction being processed (if network is fast enough). This is what the first part (first 2 hunks) of the patch solves. There is also issue that while this is happening the walsender ignores signals so it's impossible to stop it which is why I added the CHECK_FOR_INTERRUPTS to the fast path. The second problem is that if we solved just the first one, then if downstream (and again network) is fast enough, the ProcessRepliesIfAny() will not do anything useful because downstream is not going to send any response while the network buffer contains any data. This is caused by the fact that we normally code the receiver side to receive until there is something and only send reply when there is a "pause" in the stream. To get around this problem we also need to make sure that we send WalSndKeepaliveIfNecessary() periodically and that will not happen on fast network unless we do the second part of the patch (3rd hunk), ie, move the pq_is_send_pending() after the keepalive handling. This code is specific to logical decoding walsender interface so it only happens for logical decoding/replication (which means it should be backported all the way to 9.4). The physical one These two issues happen quite normally in the wild as all we need is big data load in single transaction, or update of large part of an table or something similar for this to happen with default settings. -- Petr Jelinek http://www.2ndQuadrant.com/ PostgreSQL Development, 24x7 Support, Training & Services --------------71D32E10CDDF256DC353ABE9 Content-Type: text/x-patch; name="v3-0001-Fix-walsender-timeouts-when-decoding-large-transacti.patch" Content-Transfer-Encoding: quoted-printable Content-Disposition: attachment; filename*0="v3-0001-Fix-walsender-timeouts-when-decoding-large-transacti"; filename*1=".patch" =46rom df2d5b09a74cb31537e2bb74895a8e31febce5f8 Mon Sep 17 00:00:00 2001 From: Petr Jelinek Date: Tue, 31 Oct 2017 14:00:37 +0100 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 existing coding 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 and 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 | 31 +++++++++++++++++++------------ 1 file changed, 19 insertions(+), 12 deletions(-) diff --git a/src/backend/replication/walsender.c b/src/backend/replication/= walsender.c index fa1db748b5..79c5153ac7 100644 --- a/src/backend/replication/walsender.c +++ b/src/backend/replication/walsender.c @@ -1151,6 +1151,8 @@ static void WalSndWriteData(LogicalDecodingContext *ctx, XLogRecPtr lsn, TransactionId= xid, bool last_write) { + TimestampTz now =3D GetCurrentTimestamp(); + /* output previously gathered data in a CopyData packet */ pq_putmessage_noblock('d', ctx->out->data, ctx->out->len); =20 @@ -1160,23 +1162,28 @@ WalSndWriteData(LogicalDecodingContext *ctx, XLogRe= cPtr 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)); =20 - /* fast path */ - /* Try to flush pending output to the client */ - if (pq_flush_if_writable() !=3D 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(); =20 - if (!pq_is_send_pending()) - return; + /* Try to flush pending output to the client */ + if (pq_flush_if_writable() !=3D 0) + WalSndShutdown(); + + if (!pq_is_send_pending()) + return; + } =20 for (;;) { int wakeEvents; long sleeptime; - TimestampTz now; =20 /* * Emergency bailout if postmaster has died. This is to avoid the @@ -1205,10 +1212,6 @@ WalSndWriteData(LogicalDecodingContext *ctx, XLogRec= Ptr lsn, TransactionId xid, if (pq_flush_if_writable() !=3D 0) WalSndShutdown(); =20 - /* If we finished clearing the buffered data, we're done here. */ - if (!pq_is_send_pending()) - break; - now =3D GetCurrentTimestamp(); =20 /* die if timeout was reached */ @@ -1217,6 +1220,10 @@ WalSndWriteData(LogicalDecodingContext *ctx, XLogRec= Ptr lsn, TransactionId xid, /* Send keepalive if the time has come */ WalSndKeepaliveIfNecessary(now); =20 + /* If we finished clearing the buffered data, we're done here. */ + if (!pq_is_send_pending()) + break; + sleeptime =3D WalSndComputeSleeptime(now); =20 wakeEvents =3D WL_LATCH_SET | WL_POSTMASTER_DEATH | --=20 2.11.0 --------------71D32E10CDDF256DC353ABE9 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 --------------71D32E10CDDF256DC353ABE9--