From 41c53e1c780618ddbe0765a519e49da8b7e76117 Mon Sep 17 00:00:00 2001 From: Gaurav Kukreja Date: Tue, 14 May 2024 09:19:40 +0200 Subject: [PATCH] [BACKPORT 2024.1][#21892] YSQL: Fix index tuple width estimation in cost model Summary: Original commit: fa7c8bbcb52c39757707e4db2f099e6471d9f332 / D33947 The YB base scans cost model calculates the index tuple width to estimate the cost of fetching the index from the disk to memory. This change fixes a bug in the index tuple width calculation. Jira: DB-10792 Test Plan: ./yb_build.sh --java-test 'org.yb.pgsql.TestPgRegressTAQO#testPgRegressTAQO' Reviewers: tverona, tnayak, telgersma Reviewed By: telgersma Subscribers: yql Tags: #jenkins-ready Differential Revision: https://phorge.dev.yugabyte.com/D35028 --- .../src/backend/optimizer/path/costsize.c | 3 ++- .../expected/yb_planner_taqo_tuning_tests.out | 24 +++++++++---------- 2 files changed, 14 insertions(+), 13 deletions(-) diff --git a/src/postgres/src/backend/optimizer/path/costsize.c b/src/postgres/src/backend/optimizer/path/costsize.c index 6c67b32c03eb..5756dd87d1a6 100644 --- a/src/postgres/src/backend/optimizer/path/costsize.c +++ b/src/postgres/src/backend/optimizer/path/costsize.c @@ -6344,7 +6344,8 @@ yb_get_index_tuple_width(IndexOptInfo *index, Oid baserel_oid, /* Aggregate the width of the columns in the secondary index */ for (int i = 0; i < index->ncolumns; i++) { - index_tuple_width += index->rel->attr_widths[index->indexkeys[i]]; + index_tuple_width += + index->rel->attr_widths[index->indexkeys[i] - index->rel->min_attr]; } index_tuple_width += HIDDEN_COLUMNS_SIZE; } diff --git a/src/postgres/src/test/regress/expected/yb_planner_taqo_tuning_tests.out b/src/postgres/src/test/regress/expected/yb_planner_taqo_tuning_tests.out index 019df9e4afe0..34ba6d6bb006 100644 --- a/src/postgres/src/test/regress/expected/yb_planner_taqo_tuning_tests.out +++ b/src/postgres/src/test/regress/expected/yb_planner_taqo_tuning_tests.out @@ -1218,26 +1218,26 @@ EXPLAIN (COSTS OFF) SELECT * FROM t_range_100k where v1 > 100000; -- Query Hash: d93f6e4d8c79ed9ff1777a8346135486 EXPLAIN (COSTS OFF) SELECT v1 FROM t_range_100k where v1 > 0; - QUERY PLAN ------------------------------------------------- - Index Only Scan using tr100kv1 on t_range_100k - Index Cond: (v1 > 0) + QUERY PLAN +---------------------------- + Seq Scan on t_range_100k + Storage Filter: (v1 > 0) (2 rows) -- Query Hash: ad243137d04da666112652bc4daee5bb EXPLAIN (COSTS OFF) SELECT v1 FROM t_range_100k where v1 > 1; - QUERY PLAN ------------------------------------------------- - Index Only Scan using tr100kv1 on t_range_100k - Index Cond: (v1 > 1) + QUERY PLAN +---------------------------- + Seq Scan on t_range_100k + Storage Filter: (v1 > 1) (2 rows) -- Query Hash: bfeb69f5dd583f251a20785be7fff7f8 EXPLAIN (COSTS OFF) SELECT v1 FROM t_range_100k where v1 > 10; - QUERY PLAN ------------------------------------------------- - Index Only Scan using tr100kv1 on t_range_100k - Index Cond: (v1 > 10) + QUERY PLAN +----------------------------- + Seq Scan on t_range_100k + Storage Filter: (v1 > 10) (2 rows) -- Query Hash: 6c0176a4d1a4d77205315f39efea625c