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 1k5HN7-0004wM-2H for pgsql-hackers@arkaria.postgresql.org; Mon, 10 Aug 2020 23:38:29 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1k5HN5-0008TG-Jm for pgsql-hackers@arkaria.postgresql.org; Mon, 10 Aug 2020 23:38:27 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1k5HN5-0008T3-6k for pgsql-hackers@lists.postgresql.org; Mon, 10 Aug 2020 23:38:27 +0000 Received: from mail-qk1-x729.google.com ([2607:f8b0:4864:20::729]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1k5HMy-0007w3-5M for pgsql-hackers@lists.postgresql.org; Mon, 10 Aug 2020 23:38:25 +0000 Received: by mail-qk1-x729.google.com with SMTP id j187so10094825qke.11 for ; Mon, 10 Aug 2020 16:38:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=2ndquadrant-com.20150623.gappssmtp.com; s=20150623; h=date:from:to:subject:message-id:mime-version:content-disposition :content-transfer-encoding:user-agent; bh=jA2hTPdbpyFH2+pT2xyYo5Jf/fqj5jG+3wdRl5wpRgI=; b=nXiipeGianhO77+8exJ2aAmst3vXdgfyHiVloU+H2oSuXr6rRL6VRJgkwMjsoJcfjd RWChpCSYHfhan/xIe3bHDYUeZJ7SX3E0/qp/oYmqQRS/vqAGEDJU94wtlCshkk79nI8n 6A3K08bHwr0qu8ex4+V+U7juhxbK5eAZyDBcRVlZYjX+fUzwL24cYs/NIF72YO4o+r7m N7ZGzosYuIfkGnzZQF6AgNREWpp1Dr2Q2A066D4fFjhNvZC6Biw1Jamr52leMZlukT+l 3OjfB72Cif3nVgD8JPTSx9S4zJXAb/1pb8sCFqjqb+bMdVCtgyMKud9EMgK6VwnI/LBe JGlg== 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:subject:message-id:mime-version :content-disposition:content-transfer-encoding:user-agent; bh=jA2hTPdbpyFH2+pT2xyYo5Jf/fqj5jG+3wdRl5wpRgI=; b=j5iBYBqTeqxQ6H+8T5ejUTSCPU0SPKli6CNEFYk+fBE54a/omGOo0VimCdRjUJkC5D AOyofQsKLddyDVh0L0jSmnk521rdxhrD4Jt8I9VzMySEsy0IXZ9oN4lryw+T75qBaLWa qiEXQINu1xsPpng5/pHUGw7X5denj4OeE9fdzm6egGL5/Aui04+wSjCDCPHQysNVQxFD s0Wqd7u1wITGk9vXUmAVV6Vn+2j7vNuqMYyEaVQFD+EOj7D0mY6bp6GMpVCgR4g6ySnO XJwJ9N5bzGuy9NI8xbQfLyYfXuqF94IDdGjdB0FHzzVyJ4uqg0cQe2NfqVLObhBeXcQ2 J4ig== X-Gm-Message-State: AOAM532qBFs/cUUtRYRPoeSMX7M2fl9jucNOltK5v35HwN1coBAt6v4e MwXsJg/UxeVyxVvo+k9/yD0v/f4/w2Asv7kzwxXLi8o9Qf3DqIPgKGAi+lXeGqon0ZA6IfoD0jy MJgdawj73S6yk0bcznp6qHI3H4nIVII2yh39h0I98nWbt9UYXnrWstrso/KvRjYfx1ig53BwBnW mZAT+qrC9lsFss+nhQ X-Google-Smtp-Source: ABdhPJyFYoKuQCe/7WwBuSJhpyfK/GBgtC2Ogk1aaljWrAg2uM3Rgb+nKeLofHUelLURll+zUgBl7w== X-Received: by 2002:a37:a80b:: with SMTP id r11mr25823910qke.119.1597102698776; Mon, 10 Aug 2020 16:38:18 -0700 (PDT) Received: from perhan.alvh.no-ip.org ([190.95.19.47]) by smtp.gmail.com with ESMTPSA id r48sm18576045qtr.17.2020.08.10.16.38.17 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 10 Aug 2020 16:38:17 -0700 (PDT) Received: by perhan.alvh.no-ip.org (Postfix, from userid 1000) id D46252A151C; Mon, 10 Aug 2020 19:38:15 -0400 (-04) Date: Mon, 10 Aug 2020 19:38:15 -0400 From: Alvaro Herrera To: Pg Hackers Subject: remove spurious CREATE INDEX CONCURRENTLY wait Message-ID: <20200810233815.GA18970@alvherre.pgsql> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="KsGdsel6WgEHnImy" Content-Disposition: inline Content-Transfer-Encoding: 8bit User-Agent: Mutt/1.10.1 (2018-07-13) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk --KsGdsel6WgEHnImy Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline Content-Transfer-Encoding: 8bit I previously[1] posted a patch to have multiple CREATE INDEX CONCURRENTLY not wait for the slowest of them. This is an update of that, with minor conflicts fixed and a fresh thread. To recap: currently, any CREATE INDEX CONCURRENTLY will wait for all other CICs running concurrently to finish, because they can't be distinguished amidst other old snapshots. We can change things by having CIC set a special flag in PGPROC (like PROC_IN_VACUUM) indicating that it's doing CIC; other CICs will see that flag and will know that they don't need to wait for those processes. With this, CIC on small tables don't have to wait for CIC on large tables to complete. [1] https://postgr.es/m/20200805021109.GA9079@alvherre.pgsql -- Álvaro Herrera http://www.linkedin.com/in/alvherre "Escucha y olvidarás; ve y recordarás; haz y entenderás" (Confucio) --KsGdsel6WgEHnImy Content-Type: text/x-diff; charset=us-ascii Content-Disposition: attachment; filename="0001-Flag-CREATE-INDEX-CONCURRENTLY-to-avoid-spurious-wai.patch" From 2596c3033aacceb021463f58b50e2c4eed8a5ab2 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 | 4 +++- src/include/storage/procarray.h | 4 ++++ 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 5ceb2494ba..b030dcde6c 100644 --- a/src/include/storage/proc.h +++ b/src/include/storage/proc.h @@ -52,6 +52,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_CIC 0x04 /* 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 */ @@ -59,7 +61,7 @@ struct XidCache /* flags reset at EOXact */ #define PROC_VACUUM_STATE_MASK \ - (PROC_IN_VACUUM | PROC_VACUUM_FOR_WRAPAROUND) + (PROC_IN_VACUUM | PROC_IN_CIC | PROC_VACUUM_FOR_WRAPAROUND) /* * We allow a small number of "weak" relation locks (AccessShareLock, diff --git a/src/include/storage/procarray.h b/src/include/storage/procarray.h index 01040d76e1..c6edeb36e0 100644 --- a/src/include/storage/procarray.h +++ b/src/include/storage/procarray.h @@ -29,17 +29,21 @@ */ #define PROCARRAY_VACUUM_FLAG 0x02 /* currently running lazy * vacuum */ +#define PROCARRAY_CIC_FLAG 0x04 /* currently running CREATE INDEX + * CONCURRENTLY */ #define PROCARRAY_LOGICAL_DECODING_FLAG 0x10 /* currently doing logical * decoding outside xact */ #define PROCARRAY_SLOTS_XMIN 0x20 /* replication slot xmin, * catalog_xmin */ + /* * Only flags in PROCARRAY_PROC_FLAGS_MASK are considered when matching * PGXACT->vacuumFlags. Other flags are used for different purposes and * have no corresponding PROC flag equivalent. */ #define PROCARRAY_PROC_FLAGS_MASK (PROCARRAY_VACUUM_FLAG | \ + PROCARRAY_CIC_FLAG | \ PROCARRAY_LOGICAL_DECODING_FLAG) /* Use the following flags as an input "flags" to GetOldestXmin function */ -- 2.20.1 --KsGdsel6WgEHnImy--