Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1eMdPH-0005HC-B1 for pgsql-hackers@arkaria.postgresql.org; Wed, 06 Dec 2017 17:22:51 +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 1eMdPG-00061k-F8 for pgsql-hackers@arkaria.postgresql.org; Wed, 06 Dec 2017 17:22:50 +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 1eMdPG-00061a-4N for pgsql-hackers@lists.postgresql.org; Wed, 06 Dec 2017 17:22:50 +0000 Received: from mail-wr0-x235.google.com ([2a00:1450:400c:c0c::235]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1eMdPB-00038v-WC for pgsql-hackers@postgresql.org; Wed, 06 Dec 2017 17:22:49 +0000 Received: by mail-wr0-x235.google.com with SMTP id v105so4720319wrc.3 for ; Wed, 06 Dec 2017 09:22:45 -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=JEuWgG5NOQ8d2nxPeAku99QBGuN+iSHGB3a83Zzvkks=; b=C1ZQCbI/B7HTbXdzidpW/sEbVmAW4GVb5RSCHQPdk9GFzMMAXo3XMk04sIZVlHDsuk g8/3ldXRxwtK/NGNNTyiNbgp+1NmBt7ThmCkNn9xAofiJQBzv0J44pmKLzgFVrtaIcwg bPsuy+G74OhCQioYQFiBBGPTR96pf6TPBwABMuhpcd+KwSvPxghHe+TKJbeNqaigrqFh ZwnVWUNS5r/KoO4ZDchGfeDTsa3IX5mL047VBkVcQsR4+tXtrO8mG2jFoJvCUaNHCGOj FugMhf1Om3ml/fJmACo31347jRNrjHoYGnYBZ6/qHQv+WNIcAGu+qnVJOPWlJLcHf3bF wDvQ== 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=JEuWgG5NOQ8d2nxPeAku99QBGuN+iSHGB3a83Zzvkks=; b=YBJgeok/ulGMFurEj6OUEwQ7PYNy0e3CAlhT0d5EFEPDIpPu75TeNsMXlXaIJfaQyv ArXcMk5F+OmD5tYQaIjjmvJcP/29if01qUrRaWtlccMuH3keJgn9c302UJ7PqFplnDdM meCrNfi9aRxgEMoe2csHoXfX7ayIrvdCUM216ML2TPucgyf7PBSCehlqJRp3JHA6oFB+ ptD4zAFlrfgLAfpMvnfDL/1qPhoMnvqQ5TJMf87qqHoYvK46VQfdgjMLuC8N4pTEgnAH 89Z7LWiMrNXz/5It53+ZafY7TkJY8gw+sKeTgIi87NYBCV+TS8RrWK8Vm3Sa3FJcX5xp YumA== X-Gm-Message-State: AJaThX6CTbjTxyuHUPUY1e5KuYVLXUvb74Gb5nGazfsJavXdR6iKWBSS PCPC2yaLuvUWCzhek7jzt9hPqA80wV1wAR1M2o6f08au3Cko1srRW440/7z5/W3pDeAn2AL4mUW cmkr65gVmp5QmA+JcI/86B86Vfr+yvHmfftCQVsfJh4FHLWqHSwBWBAdnAUYuBjfpQCEFAIGcLi /E7qW1ympG3Ug= X-Google-Smtp-Source: AGs4zMZq8dpfEpnxCUYVmgHAfK/35D2vRdkLeEtM+1e6Nf2X7crKQKkb+6TV/z5iNM0UMaP5weKYqA== X-Received: by 10.223.186.81 with SMTP id t17mr19768400wrg.275.1512580964471; Wed, 06 Dec 2017 09:22:44 -0800 (PST) Received: from [192.168.0.106] (150.123.broadband11.iol.cz. [90.178.123.150]) by smtp.gmail.com with ESMTPSA id c2sm3706002wrg.57.2017.12.06.09.22.43 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 06 Dec 2017 09:22:43 -0800 (PST) Subject: Re: [HACKERS] Walsender timeouts and large transactions To: Robert Haas , Craig Ringer Cc: Kyotaro HORIGUCHI , Yura Sokolov , PostgreSQL Hackers 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> <86acff25-ca5d-1dc0-2da7-006344021f06@2ndquadrant.com> From: Petr Jelinek Message-ID: <6f44fc5e-3083-3fe0-4078-fa6e94133d3f@2ndquadrant.com> Date: Wed, 6 Dec 2017 18:22:42 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.5.0 MIME-Version: 1.0 In-Reply-To: Content-Type: multipart/mixed; boundary="------------DC466F9A4B015D5D01AFFB27" 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. --------------DC466F9A4B015D5D01AFFB27 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit On 05/12/17 21:07, Robert Haas wrote: > On Mon, Dec 4, 2017 at 10:59 PM, Craig Ringer wrote: >> To me it looks like it's time to get this committed, marking as such. > > This version has noticeably more code rearrangement than before, and > I'm not sure that is actually buying us anything. Why not keep the > changes minimal? > Yeah we moved things around in the end, the main reason would be that it actually works closer to how it was originally designed to work. Meaning that the slow path is not so slow when !pq_is_send_pending() which seems to have been the reasoning for original coding. It's not completely necessary to do it for fixing the bug, but why make things slower than they need to be. > Also, TBH, this doesn't seem to have been carefully reviewed for style: > > - 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; > + } > > Generally we write if (a && b) { ... } not if (a) { if (b) .. } > It's rather ugly with && because one of the conditions is two line, but okay here you go. I am keeping the brackets even if normally don't for one-liners because it's completely unreadable without them IMHO. > - } > + }; > Oops. -- Petr Jelinek http://www.2ndQuadrant.com/ PostgreSQL Development, 24x7 Support, Training & Services --------------DC466F9A4B015D5D01AFFB27 Content-Type: text/x-patch; name="v6-0001-Fix-walsender-timeouts-when-decoding-large-transacti.patch" Content-Transfer-Encoding: quoted-printable Content-Disposition: attachment; filename*0="v6-0001-Fix-walsender-timeouts-when-decoding-large-transacti"; filename*1=".patch" =46rom 61a23cad58a0016876e3d6c829f6353ee491b4c7 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 | 66 +++++++++++++++++++++----------= ------ 1 file changed, 37 insertions(+), 29 deletions(-) diff --git a/src/backend/replication/walsender.c b/src/backend/replicatio= n/walsender.c index fa1db748b5..e015870a4e 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()) + /* Try taking fast path unless we get too close to walsender timeout. *= / + if (now < TimestampTzPlusMilliseconds(last_reply_timestamp, + wal_sender_timeout / 2) && + 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,34 +1231,9 @@ WalSndWriteData(LogicalDecodingContext *ctx, XLogR= ecPtr 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 */ --=20 2.14.1 --------------DC466F9A4B015D5D01AFFB27--