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 1iCSt3-0000nF-Kq for pgsql-general@arkaria.postgresql.org; Mon, 23 Sep 2019 18:16:37 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1iCSt2-0000Wa-Bu for pgsql-general@arkaria.postgresql.org; Mon, 23 Sep 2019 18:16:36 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1iCSt1-0000TK-Rt for pgsql-general@lists.postgresql.org; Mon, 23 Sep 2019 18:16:36 +0000 Received: from mail-wr1-x42d.google.com ([2a00:1450:4864:20::42d]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1iCSsx-0004lD-TQ for pgsql-general@postgresql.org; Mon, 23 Sep 2019 18:16:34 +0000 Received: by mail-wr1-x42d.google.com with SMTP id i1so15062699wro.4 for ; Mon, 23 Sep 2019 11:16:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cybertec-at.20150623.gappssmtp.com; s=20150623; h=message-id:subject:from:to:date:in-reply-to:references:user-agent :mime-version:content-transfer-encoding; bh=/3W5BJf+qxSNiwbsm4gauKA9N7bbYuZp7+YHLT6WbQU=; b=Jd1KDSQkI5GBfMMVx+yCxEg8QbxCR8oHcfKEdlz/zKpEOrzCuZMo8Rr59LwWTCNyye K27jCr9b0WFI2j7YpNTpJD+YjAQtUqTeFIzVrsUvHkJWCFtrEH4lBCxFqNKVTIt/+Y59 yqKHzU5wKZyYMUsNY3rKPAOgQnxGQZfHLBEki1yckIipcRVKnBLRPMMANCQh5i5TVSa3 t2hL5QB8G9S6PlhsMV+ZaZr7zis+V1Xw02gqo8FbYyrvcT0bluG62v86UC2Z8NopbtkN oBVDQLw3Wro5q4799DCC1GzbgD29zKNxSxkuSxM17I5czV6K2LUtO1SC11UxopQmVA/h 6oew== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:message-id:subject:from:to:date:in-reply-to :references:user-agent:mime-version:content-transfer-encoding; bh=/3W5BJf+qxSNiwbsm4gauKA9N7bbYuZp7+YHLT6WbQU=; b=iQj3oGNr39JAbd/le7IZTGCTcmxzEM0T6Nbhl3J93B1IucfPlS9W7hEA1hs5WMLL3P ZcuujhyyzCj8zZyWniB5euY6jRDEOHOCI+WiuVK2HYrvoZtqNxmGCrxQSuobNa4fXT1W rgoKgsQf/h2+6vFRZK4J2sSquFtdTPOSN/cT6QxwTLRVuebxt1e0KiJ30w7y8khOlYDL 61tNmGBg4Lycmf3OdzKAt/lkL1DFeH+xnPRK3DV4kRWzuUi5aPrn9qbGFZxgh2I37vFU +mL8Czxb+4VzDMu53v/Vs8uscfzSRZOu64LW9tvdbBHdUgQkzhrmmCtvOrmbYJlmSlvB 17Lg== X-Gm-Message-State: APjAAAX6V35MvcXBEEoIfm6TkTAUBVhfG+IExrP+KApzGqImtMx+GFTd QJqgQpxbebOAn6WKxe6kN8uvZA== X-Google-Smtp-Source: APXvYqxdbH9kZZE/S5rMKOOZbKK4lmTiargmxi2iHYHnUztTW04enubQTvi8AIijAc2HxWaYBuQKKQ== X-Received: by 2002:a5d:650d:: with SMTP id x13mr524479wru.37.1569262589245; Mon, 23 Sep 2019 11:16:29 -0700 (PDT) Received: from localhost.localdomain (p200300C14F461001B784325CF0D5DCC6.dip0.t-ipconnect.de. [2003:c1:4f46:1001:b784:325c:f0d5:dcc6]) by smtp.gmail.com with ESMTPSA id b62sm9763165wmc.13.2019.09.23.11.16.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 23 Sep 2019 11:16:28 -0700 (PDT) Message-ID: <5e0018a7e73aad15d08006b6ac158742e1561294.camel@cybertec.at> Subject: Re: Autovacuum lock conflict From: Laurenz Albe To: Christophe Escobar , pgsql-general@postgresql.org Date: Mon, 23 Sep 2019 20:16:28 +0200 In-Reply-To: References: Content-Type: text/plain; charset="UTF-8" User-Agent: Evolution 3.32.4 (3.32.4-1.fc30) MIME-Version: 1.0 Content-Transfer-Encoding: 7bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Christophe Escobar wrote: > We do try to have no maintenance when migrating, thus we generally > use CREATE INDEX CONCURRENTLY to avoid locking a table for reading > and writing. > > We went across an issue with our index creation being locked by an > auto vacuum, as both processes hold a Share Update Exclusive lock. > > Thankfully for us, we run our database migration with a small lock > timeout so it was not too dangerous, but we are left with some doubt > about our "smooth" process. > > In these cases, we want to have our migration run in priority as it > blocks our release and everything that goes along. > > I've read - but I cannot find it anywhere - that a vacuum process > "should" kill itself if it finds out that it is blocking any other > query; > but only if the notice "to prevent wraparound" is not present. (have > I > missed official documentation somewhere..?) > > We would like to have inputs on how to cleanly prevent these issues > in the future. > > 1) What's the actual answer about a vacuum killing itself if it > blocks > a query? Is that true? > How fast is it to kill itself? As we have a 2s lock timeout, if > it > takes 5s to check that it should kill itself, we won't benefit from > this behaviour. It must be an anti-wraparound VACUUM, because a normal autovacuum would terminate after about 1 second ("deadlock_timeout" parameter). > 2) What can we do to prevent any concurrent index creation from being > locked out by auto vacuums? > We have thought of a dirty solution consisting of: > - disabling auto vacuums before a migration > - kill current pids with a vacuum > - do our migration > - enable back auto vacuum. > We find this solution awful, error-prone and "complex" to have. > Are there any other way except having manuals vacuum being played > during the night? Yes, that is ugly and not a good idea. Unless done very carefully, this has the potential to harm your database. VACUUM is important. I would make sure that all tables are vacuumed in a timely fashion. - Avoid explicit table locks or other operations that would block autovacuum (and so prevent it, because it will terminate). - For tables that receive UPDATEs and DELETEs, make sure that "autovacuum_vacuum_cost_delay" is small enough so that autovacuum finishes quickly. - For tables that receive only INSERTs, schedule a regular VACUUM with "cron" or similar. Unfortunately, PostgreSQL isn't very smart about vacuuming insert-only tables. Yours, Laurenz Albe -- Cybertec | https://www.cybertec-postgresql.com