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 1ssguj-00A9fK-Fj for pgsql-hackers@arkaria.postgresql.org; Mon, 23 Sep 2024 11:07:34 +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 1ssguh-006bTm-Gu for pgsql-hackers@arkaria.postgresql.org; Mon, 23 Sep 2024 11:07:31 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1ssguh-006bTe-7C for pgsql-hackers@lists.postgresql.org; Mon, 23 Sep 2024 11:07:31 +0000 Received: from mail-qk1-x72d.google.com ([2607:f8b0:4864:20::72d]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1ssguZ-000eke-Hl for pgsql-hackers@lists.postgresql.org; Mon, 23 Sep 2024 11:07:30 +0000 Received: by mail-qk1-x72d.google.com with SMTP id af79cd13be357-7a99fd4ea26so370515085a.1 for ; Mon, 23 Sep 2024 04:07:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1727089643; x=1727694443; darn=lists.postgresql.org; h=to:references:message-id:content-transfer-encoding:cc:date :in-reply-to:from:subject:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=VI9S5JY2iVNj1YVKWaYs6gzxvXEOBHsdwk8/+EbwFcQ=; b=H2UtwHWy5xcF72Wja+LXEQaa04sW39UZnOgkKaLVkyaAEFHGI2RMKZtP4slCyEtC7V 4RCxXnwTjZbXXmK+MOyZVb16iTM9Oq06Nz7sK/atFEa4JD1KqUHWisuy5jFpsmqZ3x7p rIssrt+O2414dcrMVYuvXpdKKJ8Hc/1lzKz/4E6kWttiOG+nxvL80TbwiZ88NecwY5qg n9fFu4vrD79SoUscsiCvcE0cus2P+VYDJZ9dY+p65paRHbXEiIZysfhC5pjXiJ29O90c b8Ywt2zMahI5ssAK5rvnBgDyHObxGTzvBVqSNGprwUg0yDNJE31NA9pDLui/6bfLtHzW vCHw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1727089643; x=1727694443; h=to:references:message-id:content-transfer-encoding:cc:date :in-reply-to:from:subject:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=VI9S5JY2iVNj1YVKWaYs6gzxvXEOBHsdwk8/+EbwFcQ=; b=uWcBJyDDBOmrKSd810YMBWW15s4rkRqmAWfS5SLMVIwvAMLnn/eept3T5wjs/cv1yv nwRTfnyeedZnf0rl3B98fFGaNTfvy7UCyxT2mlR84TpWaT/QvDlsgOFQlqvuqM1T6i1/ 7rvRFE5JUQNS+vAHyuG/SoRSPe9gKCY7mWNMwo2PxgXZVzqtArCPNMZtyUHR7o3x/qHw +qqsKYiQeGEedDw5umJgWe+vn4bR0QjT+rGuCVxjNDZh2v7ruCpi+vk2oYvpZJEGJ23j ccSBpYcMaWjpJ11x4Gb/l5QN6HW+r/AiSZCYdYxI4Mp//gyPwpPsx1W3TKA/synjAvk7 yExg== X-Forwarded-Encrypted: i=1; AJvYcCUs1v+xVdJJ7rOQtRr69lcnlM5DTjglwLkolHj3QHyqSsaihQw/1hCikclTVqkVYttvQOWBLZf0raTEXNfZ@lists.postgresql.org X-Gm-Message-State: AOJu0YwWKqfY47NMrxvCndXrvaHxxndRz8wvdZViAf1abbW2CeXdSq4m YUAyq37Pvfk9kcHQMYRG9Gq6WbhIrRqLq6OeewPuR2Vlse9R6GUt X-Google-Smtp-Source: AGHT+IHNwd1R6aQN5zp2/FCbZFUid5sy3p60sqarhe3sT2Ra+Q87z8X1mmHfoj/3UKg6lQmWcQ7gQA== X-Received: by 2002:a05:6214:3c9f:b0:6c5:aadd:9219 with SMTP id 6a1803df08f44-6c7bc6a4bc5mr191467116d6.11.1727089642897; Mon, 23 Sep 2024 04:07:22 -0700 (PDT) Received: from smtpclient.apple ([2601:192:4e00:11c7::f7]) by smtp.gmail.com with ESMTPSA id 6a1803df08f44-6c75e5872c7sm45915966d6.144.2024.09.23.04.07.22 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Mon, 23 Sep 2024 04:07:22 -0700 (PDT) Content-Type: text/plain; charset=us-ascii Mime-Version: 1.0 (Mac OS X Mail 16.0 \(3731.300.101.1.3\)) Subject: Re: [PATCH] Proposal to Enable/Disable Index using ALTER INDEX From: Shayon Mukherjee In-Reply-To: Date: Mon, 23 Sep 2024 07:07:11 -0400 Cc: Nathan Bossart , pgsql-hackers@lists.postgresql.org Content-Transfer-Encoding: quoted-printable Message-Id: <7529D17D-6620-466F-A78D-CD2D5346D1B0@gmail.com> References: To: David Rowley X-Mailer: Apple Mail (2.3731.300.101.1.3) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Hi David, Thank you so much for the review and pointers. I totally missed = expression indexes. I am going to do another proper pass along with your = feedback and follow up with an updated patch and any questions.=20 Excited to be learning so much about the internals.=20 Shayon > On Sep 22, 2024, at 6:44 PM, David Rowley = wrote: >=20 > 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. >=20 > There are quite a large number of other places you also need to = modify. >=20 > Here are 2 places where the index should be ignored but isn't: >=20 > 1. expression indexes seem to still be used for statistical = estimations: >=20 > 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=3D23.00..23.12 rows=3D10 width=3D4) >=20 > alter index b_expr_idx disable; > explain select distinct b%10 from b; > -- HashAggregate (cost=3D23.00..23.12 rows=3D10 width=3D4) <-- should = be 1000 rows >=20 > drop index b_expr_idx; > explain select distinct b%10 from b; > -- HashAggregate (cost=3D23.00..35.50 rows=3D1000 width=3D4) >=20 > 2. Indexes seem to still be used for join removals. >=20 > create table c (c int primary key); > explain select c1.* from c c1 left join c c2 on c1.c=3Dc2.c; -- > correctly removes join. > alter index c_pkey disable; > explain select c1.* from c c1 left join c c2 on c1.c=3Dc2.c; -- should > not remove join. >=20 > 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. >=20 > The planner should not be making use of disabled indexes for any > optimisations at all. >=20 >> - catversion.h is updated with a new CATALOG_VERSION_NO to reflect = change in pg_index >> schema. >=20 > 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. >=20 > Also, please get rid of these notices. The command tag serves that > purpose. It's not interesting that the index is already disabled. >=20 > # 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 >=20 > 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 !=3D 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. >=20 > 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) >=20 > David