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 1k38tk-00053T-2K for pgsql-hackers@arkaria.postgresql.org; Wed, 05 Aug 2020 02:11:20 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1k38ti-0001HQ-5A for pgsql-hackers@arkaria.postgresql.org; Wed, 05 Aug 2020 02:11:18 +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 1k38th-0001HJ-PS for pgsql-hackers@lists.postgresql.org; Wed, 05 Aug 2020 02:11:17 +0000 Received: from mail-qt1-x844.google.com ([2607:f8b0:4864:20::844]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1k38te-00064R-Qt for pgsql-hackers@postgresql.org; Wed, 05 Aug 2020 02:11:17 +0000 Received: by mail-qt1-x844.google.com with SMTP id e5so18571349qth.5 for ; Tue, 04 Aug 2020 19:11:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=2ndquadrant-com.20150623.gappssmtp.com; s=20150623; h=date:from:to:cc:subject:message-id:mime-version:content-disposition :content-transfer-encoding:in-reply-to:user-agent; bh=xRzLpDGdizd2iU1jztVOQwtOVT6q5CWmQdCVRcz47p4=; b=YHVpoIXK2ezaTBzPOFa7DNefqq9imKLxNgDXI47yqjP/oLozYyYm0FHOOjAfgC0nBV KYycaINYgrjySsyYjoq2jPJO0q/BUcEXko+LxLzMDZUjNtjGAZkJMFisuWoiDo6gq8jK WNeZQVtkfkn5PrsKfDhtfvTu+tOaTVLEHnpzGkt4pZxwQVd+p4xLq+ddY45Dd+ya9DeP PH8pkCBNxaqoPA8z7BfRk4/4mhlt65DRnsUtnKFQcp9AH+HjPiiOIwy5FBGgyExuFjTB SrIaHnVWsHirhuKH5E1wUPcJXdn+LyACED/yYLyv/gaIQPmPOwQRhtjnfERYxVGcTuQR Zi4g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:mime-version :content-disposition:content-transfer-encoding:in-reply-to :user-agent; bh=xRzLpDGdizd2iU1jztVOQwtOVT6q5CWmQdCVRcz47p4=; b=hdHphybg+ie4y/153CNBjOxFZqp/AuCZeN27APS3Dx3IW1Yim0fZJUHyMUIk8rOwua SXYOSxgqvAoezzcOKIP4pLIuAhWUFnwWTPkHf+VJPjl9InB3tWF9OpM4QhL1DuPk9k/h s5p3Nn0oFRJDSzCDSzGcSa3LPrKtT87cmMOLGgjBosNM0gCB7fz7JDXjQMnju/YjheiY CilxiqxPADzOIS1YYmesPAkZRpKEInzrHvxPqhrjYCSLl5sK/lOqK3GknLdJ3WFKN6Do wo32tTvBkqQa/53wfH2Mm8NaTtMVgQLkpedLShpmrV0ANAGVj7WWE7o816RJR2/lERNr hGaw== X-Gm-Message-State: AOAM530d649IBLzEhJeRlQwajq8Qa5caAtyXpIu/smha5m7TZgD+KvEB tKc/EJrrDKk3Ybxa/OksvZVIsg== X-Google-Smtp-Source: ABdhPJx6mcKMxyz3W4H57Vi0B0X3YD2+xxILjjodePHMin6vxrno+GfIgk0qS3G/NJiQqlikCM8tMA== X-Received: by 2002:ac8:b4c:: with SMTP id m12mr980010qti.321.1596593472316; Tue, 04 Aug 2020 19:11:12 -0700 (PDT) Received: from perhan.alvh.no-ip.org ([190.95.19.47]) by smtp.gmail.com with ESMTPSA id n85sm432579qkn.80.2020.08.04.19.11.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 04 Aug 2020 19:11:11 -0700 (PDT) Received: by perhan.alvh.no-ip.org (Postfix, from userid 1000) id A85FB2A032F; Tue, 4 Aug 2020 22:11:09 -0400 (-04) Date: Tue, 4 Aug 2020 22:11:09 -0400 From: Alvaro Herrera To: Andres Freund Cc: James Coleman , pgsql-hackers Subject: Re: [DOC] Document concurrent index builds waiting on each other Message-ID: <20200805021109.GA9079@alvherre.pgsql> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="VS++wcV0S1rZb1Fb" Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <20200325195841.gq4hpl25t6pxv3gl@alap3.anarazel.de> User-Agent: Mutt/1.10.1 (2018-07-13) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk --VS++wcV0S1rZb1Fb Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline Content-Transfer-Encoding: 8bit On 2020-Mar-25, Andres Freund wrote: > What I was thinking of was a new flag, with a distinct value from > PROC_IN_VACUUM. It'd currently just be specified in the > GetCurrentVirtualXIDs() calls in WaitForOlderSnapshots(). That'd avoid > needing to wait for other CICs on different relations. Since CIC is not > permitted on system tables, and CIC doesn't do DML on normal tables, it > seems fairly obviously correct to exclude other CICs. Hmm, that does work, and seems a pretty small patch -- attached. Of course, some more commentary is necessary, but the theory of operation is as Andres says. (It does not solve the vacuuming problem I was describing in the other thread, only the spurious waiting that James is complaining about in this thread.) I'm going to try and poke holes on this now ... (Expression indexes with falsely immutable functions?) -- Álvaro Herrera https://www.2ndQuadrant.com/ PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services --VS++wcV0S1rZb1Fb Content-Type: text/x-diff; charset=us-ascii Content-Disposition: attachment; filename="0001-Flag-CREATE-INDEX-CONCURRENTLY-to-avoid-spurious-wai.patch" From a691c48ddd5d9ff99e2f17b91777028bd5fbf36b Mon Sep 17 00:00:00 2001 From: Alvaro Herrera Date: Tue, 4 Aug 2020 22:04:57 -0400 Subject: [PATCH] Flag CREATE INDEX CONCURRENTLY to avoid spurious waiting --- src/backend/commands/indexcmds.c | 13 +++++++++++-- src/include/storage/proc.h | 2 ++ src/include/storage/procarray.h | 6 +++++- 3 files changed, 18 insertions(+), 3 deletions(-) diff --git a/src/backend/commands/indexcmds.c b/src/backend/commands/indexcmds.c index a3cb3cd47f..241c8a337e 100644 --- a/src/backend/commands/indexcmds.c +++ b/src/backend/commands/indexcmds.c @@ -393,7 +393,7 @@ WaitForOlderSnapshots(TransactionId limitXmin, bool progress) VirtualTransactionId *old_snapshots; old_snapshots = GetCurrentVirtualXIDs(limitXmin, true, false, - PROC_IS_AUTOVACUUM | PROC_IN_VACUUM, + PROC_IS_AUTOVACUUM | PROC_IN_VACUUM | PROC_IN_CIC, &n_old_snapshots); if (progress) pgstat_progress_update_param(PROGRESS_WAITFOR_TOTAL, n_old_snapshots); @@ -413,7 +413,7 @@ WaitForOlderSnapshots(TransactionId limitXmin, bool progress) newer_snapshots = GetCurrentVirtualXIDs(limitXmin, true, false, - PROC_IS_AUTOVACUUM | PROC_IN_VACUUM, + PROC_IS_AUTOVACUUM | PROC_IN_VACUUM | PROC_IN_CIC, &n_newer_snapshots); for (j = i; j < n_old_snapshots; j++) { @@ -1420,6 +1420,9 @@ DefineIndex(Oid relationId, CommitTransactionCommand(); StartTransactionCommand(); + /* Set flag for other concurrent index builds to ignore us */ + MyPgXact->vacuumFlags |= PROC_IN_CIC; + /* * The index is now visible, so we can report the OID. */ @@ -1482,6 +1485,9 @@ DefineIndex(Oid relationId, CommitTransactionCommand(); StartTransactionCommand(); + /* Set flag for other concurrent index builds to ignore us */ + MyPgXact->vacuumFlags |= PROC_IN_CIC; + /* * Phase 3 of concurrent index build * @@ -1541,6 +1547,9 @@ DefineIndex(Oid relationId, CommitTransactionCommand(); StartTransactionCommand(); + /* Set flag for other concurrent index builds to ignore us */ + MyPgXact->vacuumFlags |= PROC_IN_CIC; + /* We should now definitely not be advertising any xmin. */ Assert(MyPgXact->xmin == InvalidTransactionId); diff --git a/src/include/storage/proc.h b/src/include/storage/proc.h index b20e2ad4f6..43c8ea3e31 100644 --- a/src/include/storage/proc.h +++ b/src/include/storage/proc.h @@ -53,6 +53,8 @@ struct XidCache #define PROC_IS_AUTOVACUUM 0x01 /* is it an autovac worker? */ #define PROC_IN_VACUUM 0x02 /* currently running lazy vacuum */ #define PROC_IN_ANALYZE 0x04 /* currently running analyze */ +#define PROC_IN_CIC 0x40 /* currently running CREATE INDEX + CONCURRENTLY */ #define PROC_VACUUM_FOR_WRAPAROUND 0x08 /* set by autovac only */ #define PROC_IN_LOGICAL_DECODING 0x10 /* currently doing logical * decoding outside xact */ diff --git a/src/include/storage/procarray.h b/src/include/storage/procarray.h index a5c7d0c064..0255949307 100644 --- a/src/include/storage/procarray.h +++ b/src/include/storage/procarray.h @@ -36,6 +36,9 @@ #define PROCARRAY_SLOTS_XMIN 0x20 /* replication slot xmin, * catalog_xmin */ +#define PROCARRAY_CIC_FLAG 0x40 /* currently running CREATE INDEX + * CONCURRENTLY */ + /* * Only flags in PROCARRAY_PROC_FLAGS_MASK are considered when matching * PGXACT->vacuumFlags. Other flags are used for different purposes and @@ -43,7 +46,8 @@ */ #define PROCARRAY_PROC_FLAGS_MASK (PROCARRAY_VACUUM_FLAG | \ PROCARRAY_ANALYZE_FLAG | \ - PROCARRAY_LOGICAL_DECODING_FLAG) + PROCARRAY_LOGICAL_DECODING_FLAG | \ + PROCARRAY_CIC_FLAG) /* Use the following flags as an input "flags" to GetOldestXmin function */ /* Consider all backends except for logical decoding ones which manage xmin separately */ -- 2.20.1 --VS++wcV0S1rZb1Fb--