Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1nvKRC-0002G4-45 for pgsql-general@arkaria.postgresql.org; Sun, 29 May 2022 15:02:38 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1nvKRB-0006Ul-0V for pgsql-general@arkaria.postgresql.org; Sun, 29 May 2022 15:02:37 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1nvKRA-0006Uc-JU for pgsql-general@lists.postgresql.org; Sun, 29 May 2022 15:02:36 +0000 Received: from mail-ed1-x52e.google.com ([2a00:1450:4864:20::52e]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1nvKR6-0002yi-AA for pgsql-general@postgresql.org; Sun, 29 May 2022 15:02:36 +0000 Received: by mail-ed1-x52e.google.com with SMTP id b8so2687649edf.11 for ; Sun, 29 May 2022 08:02:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cybertec-at.20210112.gappssmtp.com; s=20210112; h=message-id:subject:from:to:date:in-reply-to:references:user-agent :mime-version:content-transfer-encoding; bh=+r0y7hxiojaqkGtlZ/6IVjnCY4qbsIsTYUUTorOBZh4=; b=iVbVHkFMilqar+qZeP2XNsm62BOg2Tn5W5TmIVYL4aZ2NZ3mQ8RxCwgYfe0fWXe/sG BwzTy8a4b2Svfvq4flvupqqkMO5KPYGRjxp9eU+SvzfjgC+dZR5XoeloJGNyn2stPAbZ wJRzvFvon2Qy2Q7KwPT1GVflxZneHZF9mrinD8qEUEXi57s8cvd9IFjK8DwLKY4h9iwT J0hg7zU9rxSyakXkcNBOBYDqZyP6YwWW1iN/S8VpelKTMJsGjDDWKMs1aX1vMZkd+Kmt 72rTJtiqJkOXrviysTAmOgzuHN/2VfMc5fW6B36LZV/J4RvdatnI5Az5pje3pH4ZTouT i8KA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:message-id:subject:from:to:date:in-reply-to :references:user-agent:mime-version:content-transfer-encoding; bh=+r0y7hxiojaqkGtlZ/6IVjnCY4qbsIsTYUUTorOBZh4=; b=TJaGAjDCZ8VdaE39ZqvOKWHqhtA27VgCBeSqSoVBWFMW+s7JMBp8TiS083aF7W4HXF yhvuNCCsOxfCCQLp7TDWvKeaojBFFQRycQJl+CV1/NgSGsPlae7s+9TsqOpdKBChRhnO ItJDHhKFOHAopzA59BJOPJuF7i8AVS2PuKPMZGIuvp8w5Id1oclbiwliLoMB6m+VWBJQ 7HfgdUW3tN6OE9BRD8YCrhhiTReV0rhWUXe0oMdHKT2hn7AEBNbV7+OJxLNbD8jlGex9 mFYTKPpjymqf8YCsw4SlvxQp2zzAT1X6kQfMxr4tOouV0Jcy+dFDq40inJ8/buejrxUr c7Qg== X-Gm-Message-State: AOAM5314C9HWK4CUyfrVDzGKWHPK8XR1R6yK2FgPYhGNeLyDhAJT7RLo iNj5DDPZJTYncjm0ikA5eE5v08sJ/p9EIA== X-Google-Smtp-Source: ABdhPJzogTCTw3yDCxBGpF+nzUkYAviTFOV1eME81zCeZXgfeWGBh2Yjn92lo9iKu7tTneohqd7PzA== X-Received: by 2002:a05:6402:50d2:b0:42b:2bf0:4dbe with SMTP id h18-20020a05640250d200b0042b2bf04dbemr47463863edb.309.1653836550042; Sun, 29 May 2022 08:02:30 -0700 (PDT) Received: from localhost.localdomain (178-190-176-173.adsl.highway.telekom.at. [178.190.176.173]) by smtp.gmail.com with ESMTPSA id x25-20020a50f199000000b0042dd109b212sm402105edl.3.2022.05.29.08.02.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 29 May 2022 08:02:29 -0700 (PDT) Message-ID: <0ffa96ab1ec3b22bb738b15a93612e69d1e5c11c.camel@cybertec.at> Subject: Re: autovacuum on primary blocking queries on replica? From: Laurenz Albe To: Don Seiler , Postgres General Date: Sun, 29 May 2022 17:02:28 +0200 In-Reply-To: References: Content-Type: text/plain; charset="UTF-8" User-Agent: Evolution 3.42.4 (3.42.4-2.fc35) MIME-Version: 1.0 Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Fri, 2022-05-27 at 14:00 -0500, Don Seiler wrote: >  * PostgreSQL 12.9 - PGDG Ubuntu 18.04 image >  * Streaming physical replication >  * hot_standby_feedback = on > We use a read replica to offload a lot of (what should be) quick queries. This morning we had an incident > where these queries were all blocking on AccessShareLock waits, written to the log as: > > 2022-05-27 15:23:53.476 UTC [8185] foo@foo_all - myapp LOG:  process 8185 still waiting for AccessShareLock on relation 16834 of database 16401 after 1000.228 ms at character 204 > 2022-05-27 15:23:53.476 UTC [8185] foo@foo_all - myapp DETAIL:  Process holding the lock: 10822. Wait queue: 32373, 8185, 13782, [...] > This went on for 30 seconds (the value of max_standby_streaming_delay) until PG killed the blocking process: > 2022-05-27 15:24:22.474 UTC [10822] foo@foo_all - anotherapp FATAL:  terminating connection due to conflict with recovery > 2022-05-27 15:24:22.474 UTC [10822] foo@foo_all - anotherapp DETAIL:  User was holding a relation lock for too long. > > I'm trying to find the root cause of why this started. We did see an UPDATE [...] An UPDATE cannot be the problem. > 15 seconds later we then see an aggressive autovacuum on this table: > > 2022-05-27 15:23:52.507 UTC [30513] LOG:  automatic aggressive vacuum of table "foo_all.public.industry": index scans: 1 >         pages: 252 removed, 323 remain, 0 skipped due to pins, 0 skipped frozen >         tuples: 8252 removed, 8252 remain, 0 are dead but not yet removable, oldest xmin: 1670999292 >         buffer usage: 12219 hits, 137 misses, 54 dirtied >         avg read rate: 2.372 MB/s, avg write rate: 0.935 MB/s >         system usage: CPU: user: 0.14 s, system: 0.00 s, elapsed: 0.45 s > > and less than a second after that is when we see the first AccessShareLock message on the replica. > > I've been reading tales of autovacuum taking an AccessExclusiveLock when truncating empty pages at the end of a table. > I'm imagining that updating every row of a table and then rolling back would leave all of those rows empty at the end > and qualify for truncation and lead to the scenario I saw this morning. > > I'm still not entirely satisfied since that table in question was so small (only 8252 rows) so I wouldn't imagine it would > hold things up as long as it did. Although the blocking session on the replica was an application session, > not any background/recovery process. I think you are on the right trail. VACUUM will truncate trailing pages if it can get a (very short) ACCESS EXCLUSIVE lock on the table. Now that lock and the truncation is replicated, and they can create a replication conflict just like you describe. Even if the lock is held for a very short time, replaying it will conflict with any query on that table on the standby. You can disable autovacuum truncation on the table with ALTER TABLE foo_all.public.industry SET (vacuum_truncate = off); if you know that you can do without autovacuum truncation for that query. Yours, Laurenz Albe -- Cybertec | https://www.cybertec-postgresql.com