Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1i4a6C-0008Bu-Ec for pgsql-general@arkaria.postgresql.org; Mon, 02 Sep 2019 00:21:36 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1i4a69-0004ws-Gz for pgsql-general@arkaria.postgresql.org; Mon, 02 Sep 2019 00:21:33 +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_SHA1:256) (Exim 4.89) (envelope-from ) id 1i4a69-0004sM-2G for pgsql-general@lists.postgresql.org; Mon, 02 Sep 2019 00:21:33 +0000 Received: from mail-pf1-x42d.google.com ([2607:f8b0:4864:20::42d]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1i4a63-0006Mw-3Q for pgsql-general@postgresql.org; Mon, 02 Sep 2019 00:21:32 +0000 Received: by mail-pf1-x42d.google.com with SMTP id q21so2893317pfn.11 for ; Sun, 01 Sep 2019 17:21:26 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=sender:date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to; bh=DO7R4IQduA66c+aes3IYuYITmkbbMjPDsr1l0mKQ8e4=; b=AH5htTlNO4CQtwUiavvmcSGvGE9iAO0+tBVDcl0dYary8mBRAoDj+qYMmvNeQp8az8 goZZ8OovRaUGqcJyeMJCx7i77nIIKSABtguFA1NRQdOwn7qxgDCi3Q+3KE+un8TxcWOV NHMLPLVMyUSRDAgW7nrFPW5gaRUkTFx1H9LUBCtaM23VeaDZS19jEvJc+oq7AqPpKtxT OVUTUuGou++nGJh8XINYqF4xe+nPjmMCGcw7NNy9cYZG7c03Fchh7xkESbSCBQ0TLhy4 eNjnWMu8vkeK08Nwtl+Vg19snqJhke6brfALc37EQXJppQXAKLi0E1QoSSaLaSVmj2Ha FOXQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:date:from:to:cc:subject:message-id :references:mime-version:content-disposition:in-reply-to; bh=DO7R4IQduA66c+aes3IYuYITmkbbMjPDsr1l0mKQ8e4=; b=HBT+CkBtbn8h8tWJXQyJLY1fV+9WiQFglcNu4tAVQpqNVFEj6yJbdKpBp9uH3sMw2m Rz89oY3cLjCaNN46MR7yWXtixM7HQ4q1g9EONWFVNewY2+cOy88Rvsv6R0BV9fzWpgTc gj+RTeDpJyg7kNE89/Y4hUNL75EH6gnToOH59ZDd1msSo+sgK5kezpUJy4MAAEa3sP0c xj22qceLlQSyKR+pRxf5h6RjWFGDYNtQxB9Q/EJaVZ5V626Jr04uHHdRaYEyUi68yfaq qCP/A6thePTBqIQGhXL7GW4ssahZhwi4KSBq+ab3NFBzpHCG7Fn/P/705T7FQ7UfpoDs Am/Q== X-Gm-Message-State: APjAAAWt+eDqhTu1e4W6qDd6XWYLxTXtFB3+CR6BYQ+8wHxwa/JHI5SU Dq//Zrciu0Uv/61GBfG/8+Y= X-Google-Smtp-Source: APXvYqxk4IsBb98/STp7atMl/kxN06u18xBL6k/dz6CJPSnWxZRjxsdmisdj6wg5wU2p9DA8bakwoA== X-Received: by 2002:a62:55c1:: with SMTP id j184mr11544652pfb.104.1567383683607; Sun, 01 Sep 2019 17:21:23 -0700 (PDT) Received: from dev.null (d64-180-144-158.bchsia.telus.net. [64.180.144.158]) by smtp.gmail.com with ESMTPSA id bb7sm2500999pjb.2.2019.09.01.17.21.22 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Sun, 01 Sep 2019 17:21:22 -0700 (PDT) Sender: Will Storey Date: Sun, 1 Sep 2019 17:21:20 -0700 From: Will Storey To: Tom Lane Cc: Adrian Klaver , pgsql-general@postgresql.org Subject: Re: Unexpected "canceling statement due to user request" error Message-ID: <20190902002120.jp4xjlfuvkwwksr3@dev.null> References: <20190816210243.tohrx52bnbxx5zra@dev.null> <34a5ea6d-8dbb-e46a-0c08-fbbea4d4bcd7@aklaver.com> <20190817181335.wc7t3z3tfs7taiqo@dev.null> <11561.1567381579@sss.pgh.pa.us> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline In-Reply-To: <11561.1567381579@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On Sun 2019-09-01 19:46:19 -0400, Tom Lane wrote: > I poked at this for awhile and concluded that what you must be seeing is > that the statement timeout interrupt triggers, but no CHECK_FOR_INTERRUPTS > call happens thereafter, until we get to the disable_statement_timeout() > call in finish_xact_command(). That throws away the timeout-happened > indicator, but it does nothing to reset InterruptPending or > QueryCancelPending. Those flags will also be thrown away eventually, > after we've read a new client command: > > /* > * (4) disable async signal conditions again. > * > * Query cancel is supposed to be a no-op when there is no query in > * progress, so if a query cancel arrived while we were idle, just > * reset QueryCancelPending. ProcessInterrupts() has that effect when > * it's called when DoingCommandRead is set, so check for interrupts > * before resetting DoingCommandRead. > */ > CHECK_FOR_INTERRUPTS(); > DoingCommandRead = false; > > Ignoring the timeout if it happened too late is what we're supposed > to be doing, so that's fine so far. > > But: what happens if some other CHECK_FOR_INTERRUPTS occurs before we > reach the main input loop? Of course, exactly the symptom you saw: > a bogus "canceling statement due to user request" error report. > What's worse, that happens after we've completed the transaction, leaving > the client with a very misleading idea of whether the command committed > or not. > > So how could that happen? After experimenting, I found a pretty > obvious candidate: statement duration logging happens after we > do finish_xact_command, and there's a CHECK_FOR_INTERRUPTS in > errfinish(). So I hypothesize that you have log_duration or > log_min_duration_statement enabled. Yes, I have log_min_duration_statement set to 0! (log_duration is off) > I was able to replicate the problem by lobotomizing a couple of > CHECK_FOR_INTERRUPTS that would normally happen in a simple > "select pg_sleep()" query (see attached for-demonstration- > purposes-only patch). Then I get > > regression=# set statement_timeout TO '1s'; > SET > regression=# select pg_sleep(2.0); -- completes normally after 2 sec > pg_sleep > ---------- > > (1 row) > > regression=# set log_min_duration_statement TO '100ms'; > SET > regression=# select pg_sleep(2.0); -- sits for 2 sec, then: > ERROR: canceling statement due to user request > > So that's not very good and we ought to do something about it. > We can't have disable_statement_timeout() clear QueryCancelPending, > because that might lose state about some other cause for query > cancel. I'm inclined to think that we need to split QueryCancelPending > into more than one flag. > > A separate question is how come the particular query you're complaining > about has (seemingly) a fairly wide window where it never does any > CHECK_FOR_INTERRUPTS call before terminating. Perhaps there's someplace > we need to sprinkle a few more of those. Yeah, it is strange. There are many queries in this system with statement timeouts and this is the only one where I see this and it happens several times a day. Thank you so much for looking into this. An impressive analysis!