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 1l0Q6a-00068D-5T for pgsql-hackers@arkaria.postgresql.org; Fri, 15 Jan 2021 14:29:36 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1l0Q6Y-0005rX-LF for pgsql-hackers@arkaria.postgresql.org; Fri, 15 Jan 2021 14:29:34 +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 1l0Q6Y-0005rQ-DB for pgsql-hackers@lists.postgresql.org; Fri, 15 Jan 2021 14:29:34 +0000 Received: from out4-smtp.messagingengine.com ([66.111.4.28]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1l0Q6W-0001on-2b for pgsql-hackers@lists.postgresql.org; Fri, 15 Jan 2021 14:29:34 +0000 Received: from compute1.internal (compute1.nyi.internal [10.202.2.41]) by mailout.nyi.internal (Postfix) with ESMTP id EFCAB5C0189; Fri, 15 Jan 2021 09:29:29 -0500 (EST) Received: from mailfrontend2 ([10.202.2.163]) by compute1.internal (MEProxy); Fri, 15 Jan 2021 09:29:29 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-transfer-encoding:content-type :date:from:in-reply-to:message-id:mime-version:subject:to :x-me-proxy:x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s= fm1; bh=UJs1OkPczdd7rtp9zh8fT5BHF9I4az6W/gcNU2elbIk=; b=Wl3Yhkxv 3ZrW0RgssKhGqzL8g/6cxawyfOv14kH1x1+Uv4lgTEj6lJwHKdHjx7TpgQbttXsV 8hyGTYz4sAAi1knKKiJK7axGdQ/NFyacOSHgKI3dBoqEY7sLKaWmUY60Ueg0+VDh wHuGR2V9xjL3u8EGqFXpeexS2xBnfHoob4/jSPI3T2nk1H3h21pECEPAPyDSFda9 pbOPhJ2SJWFl0WSx0Mp3xe6+HV45MbjFH+uebTPoPXHRRdOaUGaY8bO9pl0k8hUp y+iqPsG8tdoSUSJHvmE78N93m3KqTfZ/QuPuFZJ0FMkgRnf7aG2GW686EEAWnmNC CSyJJToJuKUQmQ== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeduledrtddvgdegudcutefuodetggdotefrodftvf curfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfghnecu uegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmdenuc fjughrpeffhffvuffkgggtugfgjggfsehmkeerredtredunecuhfhrohhmpemllhhvrghr ohcujfgvrhhrvghrrgcuoegrlhhvhhgvrhhrvgesrghlvhhhrdhnohdqihhprdhorhhgqe enucggtffrrghtthgvrhhnpeehkedtkeehfeeghfffleefleetleelueeftdevvdffudej hfetveegtddtieehueenucfkphepudeltddrleehrdduledrudejjeenucevlhhushhtvg hrufhiiigvpedtnecurfgrrhgrmhepmhgrihhlfhhrohhmpegrlhhvhhgvrhhrvgesrghl vhhhrdhnohdqihhprdhorhhg X-ME-Proxy: Received: from perhan.alvh.no-ip.org (unknown [190.95.19.177]) by mail.messagingengine.com (Postfix) with ESMTPA id 4F5F61080057; Fri, 15 Jan 2021 09:29:29 -0500 (EST) Received: by perhan.alvh.no-ip.org (Postfix, from userid 1000) id B32CB2A0FB5; Fri, 15 Jan 2021 11:29:26 -0300 (-03) Date: Fri, 15 Jan 2021 11:29:26 -0300 From: =?iso-8859-1?Q?=C1lvaro?= Herrera To: Masahiko Sawada Cc: Hamid Akhtar , PostgreSQL Hackers Subject: Re: {CREATE INDEX, REINDEX} CONCURRENTLY improvements Message-ID: <20210115142926.GA19300@alvherre.pgsql> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="EVF5PPMfhYS0aIcm" Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <20210115133858.GA18931@alvherre.pgsql> User-Agent: Mutt/1.10.1 (2018-07-13) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk --EVF5PPMfhYS0aIcm Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline Content-Transfer-Encoding: 8bit So one last remaining improvement was to have VACUUM ignore processes doing CIC and RC to compute the Xid horizon of tuples to remove. I think we can do something simple like the attached patch. -- Álvaro Herrera Valdivia, Chile "Aprender sin pensar es inútil; pensar sin aprender, peligroso" (Confucio) --EVF5PPMfhYS0aIcm Content-Type: text/x-diff; charset=us-ascii Content-Disposition: attachment; filename="vacuum-cic.patch" diff --git a/src/backend/storage/ipc/procarray.c b/src/backend/storage/ipc/procarray.c index cf12eda504..f584230b79 100644 --- a/src/backend/storage/ipc/procarray.c +++ b/src/backend/storage/ipc/procarray.c @@ -1601,7 +1601,7 @@ TransactionIdIsActive(TransactionId xid) * well as "internally" by GlobalVisUpdate() (see comment above struct * GlobalVisState). * - * See the definition of ComputedXidHorizonsResult for the various computed + * See the definition of ComputeXidHorizonsResult for the various computed * horizons. * * For VACUUM separate horizons (used to decide which deleted tuples must @@ -1610,7 +1610,12 @@ TransactionIdIsActive(TransactionId xid) * relations that's not required, since only backends in my own database could * ever see the tuples in them. Also, we can ignore concurrently running lazy * VACUUMs because (a) they must be working on other tables, and (b) they - * don't need to do snapshot-based lookups. + * don't need to do snapshot-based lookups. Also, for the non-catalog + * horizon, we can ignore CREATE INDEX CONCURRENTLY and REINDEX CONCURRENTLY + * when they are working on non-partial, non-expressional indexes, for the + * same reasons and because they can't run in transaction blocks. (They are + * not possible to ignore for catalogs, because CIC and RC do some catalog + * operations.) * * This also computes a horizon used to truncate pg_subtrans. For that * backends in all databases have to be considered, and concurrently running @@ -1660,9 +1665,6 @@ ComputeXidHorizons(ComputeXidHorizonsResult *h) bool in_recovery = RecoveryInProgress(); TransactionId *other_xids = ProcGlobal->xids; - /* inferred after ProcArrayLock is released */ - h->catalog_oldest_nonremovable = InvalidTransactionId; - LWLockAcquire(ProcArrayLock, LW_SHARED); h->latest_completed = ShmemVariableCache->latestCompletedXid; @@ -1682,6 +1684,7 @@ ComputeXidHorizons(ComputeXidHorizonsResult *h) h->oldest_considered_running = initial; h->shared_oldest_nonremovable = initial; + h->catalog_oldest_nonremovable = initial; h->data_oldest_nonremovable = initial; /* @@ -1752,7 +1755,7 @@ ComputeXidHorizons(ComputeXidHorizonsResult *h) if (statusFlags & (PROC_IN_VACUUM | PROC_IN_LOGICAL_DECODING)) continue; - /* shared tables need to take backends in all database into account */ + /* shared tables need to take backends in all databases into account */ h->shared_oldest_nonremovable = TransactionIdOlder(h->shared_oldest_nonremovable, xmin); @@ -1768,16 +1771,29 @@ ComputeXidHorizons(ComputeXidHorizonsResult *h) * to prune still needed data away). If the current backend never * connects to a database that is harmless, because * data_oldest_nonremovable will never be utilized. + * + * Additionally, processes doing CREATE INDEX CONCURRENTLY and REINDEX + * CONCURRENTLY on "safe" indexes can be ignored for non-catalog + * horizon. (But not for catalogs: some transactions in CIC/RC do + * catalog updates.) */ if (in_recovery || MyDatabaseId == InvalidOid || proc->databaseId == MyDatabaseId || proc->databaseId == 0) /* always include WalSender */ { - h->data_oldest_nonremovable = - TransactionIdOlder(h->data_oldest_nonremovable, xmin); + if (statusFlags & PROC_IN_SAFE_IC) + h->catalog_oldest_nonremovable = + TransactionIdOlder(h->catalog_oldest_nonremovable, xmin); + else + h->data_oldest_nonremovable = h->catalog_oldest_nonremovable = + TransactionIdOlder(h->data_oldest_nonremovable, xmin); } } + /* catalog horizon should never be later than data */ + Assert(TransactionIdPrecedesOrEquals(h->catalog_oldest_nonremovable, + h->data_oldest_nonremovable)); + /* * If in recovery fetch oldest xid in KnownAssignedXids, will be applied * after lock is released. @@ -1799,6 +1815,8 @@ ComputeXidHorizons(ComputeXidHorizonsResult *h) TransactionIdOlder(h->shared_oldest_nonremovable, kaxmin); h->data_oldest_nonremovable = TransactionIdOlder(h->data_oldest_nonremovable, kaxmin); + h->catalog_oldest_nonremovable = + TransactionIdOlder(h->catalog_oldest_nonremovable, kaxmin); /* temp relations cannot be accessed in recovery */ } else @@ -1825,6 +1843,9 @@ ComputeXidHorizons(ComputeXidHorizonsResult *h) h->data_oldest_nonremovable = TransactionIdRetreatedBy(h->data_oldest_nonremovable, vacuum_defer_cleanup_age); + h->catalog_oldest_nonremovable = + TransactionIdRetreatedBy(h->catalog_oldest_nonremovable, + vacuum_defer_cleanup_age); /* defer doesn't apply to temp relations */ } @@ -1847,7 +1868,6 @@ ComputeXidHorizons(ComputeXidHorizonsResult *h) h->shared_oldest_nonremovable = TransactionIdOlder(h->shared_oldest_nonremovable, h->slot_catalog_xmin); - h->catalog_oldest_nonremovable = h->data_oldest_nonremovable; h->catalog_oldest_nonremovable = TransactionIdOlder(h->catalog_oldest_nonremovable, h->slot_catalog_xmin); --EVF5PPMfhYS0aIcm--