Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1eK4lh-0006HE-QE for pgsql-hackers@arkaria.postgresql.org; Wed, 29 Nov 2017 15:59:25 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1eK4lh-0004Bo-DQ for pgsql-hackers@arkaria.postgresql.org; Wed, 29 Nov 2017 15:59:25 +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 1eK4lh-0004Be-76 for pgsql-hackers@lists.postgresql.org; Wed, 29 Nov 2017 15:59:25 +0000 Received: from mail-wm0-x244.google.com ([2a00:1450:400c:c09::244]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1eK4ld-0004pn-8t for pgsql-hackers@postgresql.org; Wed, 29 Nov 2017 15:59:24 +0000 Received: by mail-wm0-x244.google.com with SMTP id g130so36996931wme.0 for ; Wed, 29 Nov 2017 07:59:20 -0800 (PST) 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=k4wjeiEBHJgqhbyEcVTMz46KZ9uyq2wGyu/wg6K9yaI=; b=iAaEqaF57qOlCsEpyZmK26L6wBCNM/T0WWB4auRxtpdKgmrlc8r7s/lvWkXRgf0Ij1 dbg/zIznE2Bz89ezys1UXNV8eTtlW317I8mW5VhQ8vqz2yevcAuo8FEGfCHDFdrVz7bJ lQyntA7y/dW9b3mtBcnGja13KzTTIay9CV2EQ4WLNZTcumlFW9syiST6mLvwji4gvyiO rCRin8BVusJKVgOtHJdFESp3D2beyoOkoijZ+/51YfinETskEw4/4Lz1nxKsPJacJVtt p4fbdTJF5/6HuvvNwww6kiPOarDVacT088Tg9flW6Lm0ZWEJBnyUfHIYk8JS9MP/0Hin XhmA== 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=k4wjeiEBHJgqhbyEcVTMz46KZ9uyq2wGyu/wg6K9yaI=; b=A3qruzC3rfW2101IMEhbkB3FFvRoEskZUq1OhyOt1RD2RZ9BeSCos+18iGoxpVtSGh tWkMXs33zVpZbksAEC1v79N3KYENf/VvQnK3EQ5+xK5g2VPEo/00UkIG4eKtP3Af95Ik Ikk1wVepHlI60A9vvYn3YXz8bAIEjob5eI01mP446XiIt0N+Icd2/Gulu/USWnyxxktA fdPdkIbsJLefKD5gNdWXiM3oT5HUVvyj6KjshHAY7CipBUkzmcDssNqorbQ0Nam03qqi tLf87KgfqstlLHUdU7NAkQ9L/PHK0D9968WlSE114SN2DgqWoaXiXh0xPaeFLP9azvCt 0dhw== X-Gm-Message-State: AJaThX6mtpljVNBTBg3Tra9IwVB0noGgpuq4ZqkZ990Zso+9yVi5buQc yb5uX5R9kSpvHNUfQV1I9zpehw== X-Google-Smtp-Source: AGs4zMbCofaMMjdV7vcnUNDBe5d9jMRRuMRWCRi7K2fvWzytEXSjzzQ5tBStTWEfFsfP7J8oUcLDcQ== X-Received: by 10.80.176.38 with SMTP id i35mr8529333edd.156.1511971159913; Wed, 29 Nov 2017 07:59:19 -0800 (PST) Received: from [192.168.0.105] (150.123.broadband11.iol.cz. [90.178.123.150]) by smtp.gmail.com with ESMTPSA id b30sm2254135ede.53.2017.11.29.07.59.18 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 29 Nov 2017 07:59:18 -0800 (PST) Subject: Re: [HACKERS] Walsender timeouts and large transactions To: Kyotaro HORIGUCHI Cc: robertmhaas@gmail.com, funny.falcon@postgrespro.ru, pgsql-hackers@postgresql.org, pjmodos@pjmodos.net References: <20171117.132408.85564852.horiguchi.kyotaro@lab.ntt.co.jp> <20171117.133555.250037971.horiguchi.kyotaro@lab.ntt.co.jp> <20171117.163543.173137002.horiguchi.kyotaro@lab.ntt.co.jp> From: Petr Jelinek Message-ID: <86acff25-ca5d-1dc0-2da7-006344021f06@2ndquadrant.com> Date: Wed, 29 Nov 2017 16:59: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: <20171117.163543.173137002.horiguchi.kyotaro@lab.ntt.co.jp> Content-Type: multipart/mixed; boundary="------------E0C67CBC4E5F7A19A6532720" Content-Language: en-US List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk This is a multi-part message in MIME format. --------------E0C67CBC4E5F7A19A6532720 Content-Type: text/plain; charset=windows-1252 Content-Transfer-Encoding: 8bit Hi, On 17/11/17 08:35, Kyotaro HORIGUCHI wrote: > > Moving around the code allow us to place ps_is_send_pending() in > the while condition, which seems to be more proper place to do > that. I haven't added test for this particular case. > > I tested this that > > - cleanly applies on the current master HEAD and passes make > check and subscription test. > > - walsender properly chooses the slow-path even if > pq_is_send_pending() is always false. (happens on a fast enough > network) > > - walsender waits properly waits on socket and process-reply time > in WaitLatchOrSocket. > > - walsender exits by timeout on network stall. > > So, I think the patch is functionally perfect. > > I'm a reviewer of this patch but I think I'm not allowed to mark > this "Ready for Commiter" since the last change is made by me. > Thanks for working on this, but there are couple of problems with your modifications which mean that it does not actually fix the original issue anymore (transaction taking long time to decode while sending changes over network works fine will result in walsender timout). The firs one is that you put pq_is_send_pending() in the while so the while is again never executed if there is no network send pending which makes the if above meaningless. Also you missed ProcessRepliesIfAny() when moving code around. That's needed for timeout calculations to work correctly. So one more revision attached with those things fixed. This version fixes the original issue as well. -- Petr Jelinek http://www.2ndQuadrant.com/ PostgreSQL Development, 24x7 Support, Training & Services --------------E0C67CBC4E5F7A19A6532720 Content-Type: text/x-patch; name="v5-0001-Fix-walsender-timeouts-when-decoding-large-transacti.patch" Content-Transfer-Encoding: quoted-printable Content-Disposition: attachment; filename*0="v5-0001-Fix-walsender-timeouts-when-decoding-large-transacti"; filename*1=".patch" =46rom 46bbc20a3b4d6e2eed97ddc1b05d828399241c63 Mon Sep 17 00:00:00 2001 From: Petr Jelinek Date: Tue, 12 Sep 2017 17:31:28 +0900 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 | 70 +++++++++++++++++++++----------= ------ 1 file changed, 39 insertions(+), 31 deletions(-) diff --git a/src/backend/replication/walsender.c b/src/backend/replicatio= n/walsender.c index fa1db748b5..0c600ed2e3 100644 --- a/src/backend/replication/walsender.c +++ b/src/backend/replication/walsender.c @@ -1151,6 +1151,8 @@ static void WalSndWriteData(LogicalDecodingContext *ctx, XLogRecPtr lsn, Transaction= Id xid, bool last_write) { + TimestampTz now; + /* output previously gathered data in a CopyData packet */ pq_putmessage_noblock('d', ctx->out->data, ctx->out->len); =20 @@ -1160,23 +1162,54 @@ WalSndWriteData(LogicalDecodingContext *ctx, XLog= RecPtr lsn, TransactionId xid, * several releases by streaming physical replication. */ resetStringInfo(&tmpbuf); - pq_sendint64(&tmpbuf, GetCurrentTimestamp()); + now =3D GetCurrentTimestamp(); + pq_sendint64(&tmpbuf, now); memcpy(&ctx->out->data[1 + sizeof(int64) + sizeof(int64)], tmpbuf.data, sizeof(int64)); =20 - /* fast path */ + CHECK_FOR_INTERRUPTS(); + /* Try to flush pending output to the client */ if (pq_flush_if_writable() !=3D 0) WalSndShutdown(); =20 - if (!pq_is_send_pending()) - return; + /* Try taking fast path unless we get too close to walsender timeout. *= / + if (now < TimestampTzPlusMilliseconds(last_reply_timestamp, + wal_sender_timeout / 2)) + { + if (!pq_is_send_pending()) + return; + } =20 + /* If we have pending write here, go to slow path */ for (;;) { int wakeEvents; long sleeptime; - TimestampTz now; + + /* Check for input from the client */ + ProcessRepliesIfAny(); + + now =3D GetCurrentTimestamp(); + + /* die if timeout was reached */ + WalSndCheckTimeOut(now); + + /* Send keepalive if the time has come */ + WalSndKeepaliveIfNecessary(now); + + if (!pq_is_send_pending()) + break; + + sleeptime =3D WalSndComputeSleeptime(now); + + wakeEvents =3D WL_LATCH_SET | WL_POSTMASTER_DEATH | + WL_SOCKET_WRITEABLE | WL_SOCKET_READABLE | WL_TIMEOUT; + + /* Sleep until something happens or we time out */ + WaitLatchOrSocket(MyLatch, wakeEvents, + MyProcPort->sock, sleeptime, + WAIT_EVENT_WAL_SENDER_WRITE_DATA); =20 /* * Emergency bailout if postmaster has died. This is to avoid the @@ -1198,35 +1231,10 @@ WalSndWriteData(LogicalDecodingContext *ctx, XLog= RecPtr lsn, TransactionId xid, SyncRepInitConfig(); } =20 - /* Check for input from the client */ - ProcessRepliesIfAny(); - /* Try to flush pending output to the client */ if (pq_flush_if_writable() !=3D 0) WalSndShutdown(); - - /* If we finished clearing the buffered data, we're done here. */ - if (!pq_is_send_pending()) - break; - - now =3D GetCurrentTimestamp(); - - /* die if timeout was reached */ - WalSndCheckTimeOut(now); - - /* Send keepalive if the time has come */ - WalSndKeepaliveIfNecessary(now); - - sleeptime =3D WalSndComputeSleeptime(now); - - wakeEvents =3D WL_LATCH_SET | WL_POSTMASTER_DEATH | - WL_SOCKET_WRITEABLE | WL_SOCKET_READABLE | WL_TIMEOUT; - - /* Sleep until something happens or we time out */ - WaitLatchOrSocket(MyLatch, wakeEvents, - MyProcPort->sock, sleeptime, - WAIT_EVENT_WAL_SENDER_WRITE_DATA); - } + }; =20 /* reactivate latch so WalSndLoop knows to continue */ SetLatch(MyLatch); --=20 2.14.1 --------------E0C67CBC4E5F7A19A6532720--