Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1ssVKN-008r3K-J2 for pgsql-hackers@arkaria.postgresql.org; Sun, 22 Sep 2024 22:45:16 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1ssVKM-00DeSb-UC for pgsql-hackers@arkaria.postgresql.org; Sun, 22 Sep 2024 22:45:14 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1ssVKM-00DeSR-Jt for pgsql-hackers@lists.postgresql.org; Sun, 22 Sep 2024 22:45:14 +0000 Received: from mail-lf1-x133.google.com ([2a00:1450:4864:20::133]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1ssVKK-000XyN-8Z for pgsql-hackers@lists.postgresql.org; Sun, 22 Sep 2024 22:45:13 +0000 Received: by mail-lf1-x133.google.com with SMTP id 2adb3069b0e04-536562739baso3475710e87.1 for ; Sun, 22 Sep 2024 15:45:12 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1727045111; x=1727649911; darn=lists.postgresql.org; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=tvsIRnnOpShT4XArBbbI1QlLAYSruJGFdPnJSqBArIs=; b=NVUpditWSvtAxM1Ajxv923FmQEijM+tSnRVmUnbCoFaFx+jh5snB0B1x1v98ytQw04 IN9GtpnlJd/OhY3h5gbn6y6z6nqkczkRF8kApl+DJP9eL67wREg1ZnvdCGsvjXYITq5F lyooIO3dX+hv7ja98Zy2Vw0bEh0gSwiD0Cg44jVbW6/x8VH0qlDgJnQDbqdmfI5a9G+T uLUSQsnYaRYm6+uBqid6Qy53teVkPaMDhO/9qYUfI8g43t5L69ylIOZJFWe95LuIOAKP DRmjHFCAjpGe2vN9kQ2FH/mYk37PxNBmPC0frGwMCpamXpI4Ksn8ARmlNtkNEPE/W5P3 mXPw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1727045111; x=1727649911; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=tvsIRnnOpShT4XArBbbI1QlLAYSruJGFdPnJSqBArIs=; b=EjRlO4X7axLEsbqLtZdZ41wB4qSdg2JfdAk/H9EPbf0TuH23CPFj6NYpve9M/ML37+ avqJq+6AU/bLYPTnDSP2M69HWCvV0VvHlAeZiy7T3nhg53VexhJsXATwodRlgb9woIWL ONs9zVL+v5otP3vZqeTCZL1lxqNnULij/FiUOls1CuK+1fIHhdTQgUC0FNYEKlyMgB1i a2sCGbnCpZojsbruPNSbNvWFDCoS9DQJINjz1lgsUPtBs4t4cskFdN8KE6erK/m9rgq7 2rBFwY5hmyoj9vQW0XYFTkqFJDf6MHHBkcYa6RRhKV57xqH8KkGb4jtejzXjvH3BFzCU B58g== X-Forwarded-Encrypted: i=1; AJvYcCWlwYRG9k9mf6jpeQccEhhdOZF0yHw9UtIN1q24Wx9e+FOZz3ohPFGt8cyQ7+fmDlvzGzKr03R7gHv2PUwa@lists.postgresql.org X-Gm-Message-State: AOJu0YzJsZqdxXhQqQdlboo4k/Nplhe4OqUnMrcrMNZdxiW9qnWrJGD0 D+gIdKaDQjuLkTjaBYDKpKjo3Ll9wVDjSz9z1I5H7kwa60/oTHBvFpRfOrvOePbMoT1ahdCmtZ9 28smVEIhboYcrRNC0HZH23StT0r6ZjaDf X-Google-Smtp-Source: AGHT+IFPy47fBkdzCxjJEl0tl2ysT7PHHBuntT+wfOIkVReFm3mbdO8eEEPJySZV2VIy/pF6r9JH4dH8eiQd7wlcWn8= X-Received: by 2002:a05:6512:ac1:b0:536:55ef:69e8 with SMTP id 2adb3069b0e04-536ac179d79mr4065688e87.0.1727045110173; Sun, 22 Sep 2024 15:45:10 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: From: David Rowley Date: Mon, 23 Sep 2024 10:44:58 +1200 Message-ID: Subject: Re: [PATCH] Re: Proposal to Enable/Disable Index using ALTER INDEX To: Shayon Mukherjee Cc: Nathan Bossart , pgsql-hackers@lists.postgresql.org Content-Type: text/plain; charset="UTF-8" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Mon, 23 Sept 2024 at 05:43, Shayon Mukherjee wrote: > - Modified get_index_paths() and build_index_paths() to exclude disabled > indexes from consideration during query planning. There are quite a large number of other places you also need to modify. Here are 2 places where the index should be ignored but isn't: 1. expression indexes seem to still be used for statistical estimations: create table b as select generate_series(1,1000)b; create index on b((b%10)); analyze b; explain select distinct b%10 from b; -- HashAggregate (cost=23.00..23.12 rows=10 width=4) alter index b_expr_idx disable; explain select distinct b%10 from b; -- HashAggregate (cost=23.00..23.12 rows=10 width=4) <-- should be 1000 rows drop index b_expr_idx; explain select distinct b%10 from b; -- HashAggregate (cost=23.00..35.50 rows=1000 width=4) 2. Indexes seem to still be used for join removals. create table c (c int primary key); explain select c1.* from c c1 left join c c2 on c1.c=c2.c; -- correctly removes join. alter index c_pkey disable; explain select c1.* from c c1 left join c c2 on c1.c=c2.c; -- should not remove join. Please carefully look over all places that RelOptInfo.indexlist is looked at and consider skipping disabled indexes. Please also take time to find SQL that exercises each of those places so you can verify that the behaviour is correct after your change. This is also a good way to learn exactly all cases where indexes are used. Using this method would have led you to find places like rel_supports_distinctness(), where you should be skipping disabled indexes. The planner should not be making use of disabled indexes for any optimisations at all. > - catversion.h is updated with a new CATALOG_VERSION_NO to reflect change in pg_index > schema. Please leave that up to the committer. Patch authors doing this just results in the patch no longer applying as soon as someone commits a version bump. Also, please get rid of these notices. The command tag serves that purpose. It's not interesting that the index is already disabled. # alter index a_pkey disable; NOTICE: index "a_pkey" is now disabled ALTER INDEX # alter index a_pkey disable; NOTICE: index "a_pkey" is already disabled ALTER INDEX I've only given the code a very quick glance. I don't quite understand why you're checking the index is enabled in create_index_paths() and get_index_paths(). I think the check should be done only in create_index_paths(). Primarily, you'll find code such as "if (index->indpred != NIL && !index->predOK)" in the locations you need to consider skipping the disabled index. I think your new code should be located very close to those places or perhaps within the same if condition unless it makes it overly complex for the human reader. I think the documents should also mention that disabling an index is a useful way to verify an index is not being used before dropping it as the index can be enabled again at the first sign that performance has been effected. (It might also be good to mention that checking pg_stat_user_indexes.idx_scan should be the first port of call when checking for unused indexes) David