Skip to content

Commit 7602328

Browse files
committed
[#26639] YSQL: Add support for matview indexes to index consistency checker
Summary: This revision adds support to check the consistency of materialized view indexes via `yb_index_check()`. This involved creating dummy index objects (`rd_index`, `rd_indam`, and `rd_opfamily`) for materialized view relations during the index check and freeing these at the end. In the passing, also cleanup redundant commands to set/unset `yb_non_ddl_txn_for_sys_tables_allowed` in yb_index_check test. Jira: DB-16013 Test Plan: ./yb_build.sh --java-test 'org.yb.pgsql.TestPgRegressYbIndexCheck' Close: #26639 Reviewers: amartsinchyk, stiwary, #db-approvers Reviewed By: stiwary Subscribers: svc_phabricator, yql Differential Revision: https://phorge.dev.yugabyte.com/D42954
1 parent 4b05726 commit 7602328

File tree

4 files changed

+33
-9
lines changed

4 files changed

+33
-9
lines changed

src/postgres/src/backend/access/index/indexam.c

+6-1
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,8 @@ yb_dummy_baserel_index_open(Oid relationId, LOCKMODE lockmode)
140140

141141
relation = relation_open(relationId, lockmode);
142142

143-
if (relation->rd_rel->relkind == RELKIND_RELATION)
143+
if (relation->rd_rel->relkind == RELKIND_RELATION ||
144+
relation->rd_rel->relkind == RELKIND_MATVIEW)
144145
{
145146
Assert(!relation->rd_index);
146147
Assert(!relation->rd_indam);
@@ -179,6 +180,10 @@ yb_dummy_baserel_index_open(Oid relationId, LOCKMODE lockmode)
179180
void
180181
yb_free_dummy_baserel_index(Relation relation)
181182
{
183+
if (!(relation->rd_rel->relkind == RELKIND_RELATION ||
184+
relation->rd_rel->relkind == RELKIND_MATVIEW))
185+
return;
186+
182187
Assert(relation->rd_index);
183188
Assert(relation->rd_indam);
184189
Assert(relation->rd_opfamily);

src/postgres/src/backend/executor/nodeIndexscan.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -929,9 +929,9 @@ ExecEndIndexScan(IndexScanState *node)
929929
index_endscan(indexScanDesc);
930930
if (indexRelationDesc)
931931
{
932-
if (node->ss.ps.state->yb_exec_params.yb_index_check &&
933-
indexRelationDesc->rd_rel->relkind == RELKIND_RELATION)
932+
if (node->ss.ps.state->yb_exec_params.yb_index_check)
934933
yb_free_dummy_baserel_index(indexRelationDesc);
934+
935935
index_close(indexRelationDesc, NoLock);
936936
}
937937
}

src/postgres/src/test/regress/expected/yb.orig.yb_index_check.out

+17-2
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,6 @@ BEGIN
129129
END;
130130
$$;
131131
$force_cache_refresh$ AS force_cache_refresh \gset
132-
SET yb_non_ddl_txn_for_sys_tables_allowed = TRUE;
133132
-- Missing Index Row
134133
UPDATE pg_index SET indisready = FALSE, indisvalid = FALSE, indislive = FALSE WHERE indexrelid = 'abcd_b_c_d_idx'::regclass;
135134
:force_cache_refresh
@@ -227,7 +226,6 @@ SELECT yb_index_check('abcd_b_c_idx'::regclass::oid);
227226

228227
(1 row)
229228

230-
SET yb_non_ddl_txn_for_sys_tables_allowed = FALSE;
231229
-- Index of a partitioned table
232230
CREATE TABLE part(a int, b int, c int, d int) PARTITION BY RANGE(a);
233231
CREATE INDEX ON part(b) include (c, d);
@@ -266,6 +264,23 @@ SELECT yb_index_check('part_4k_6k_b_c_d_idx'::regclass::oid);
266264

267265
(1 row)
268266

267+
-- Index of materialized view
268+
CREATE MATERIALIZED VIEW matview AS SELECT * FROM abcd;
269+
CREATE INDEX matview_b_idx ON matview (b);
270+
SELECT yb_index_check('matview_b_idx'::regclass);
271+
yb_index_check
272+
----------------
273+
274+
(1 row)
275+
276+
-- Execute the same command second time to check if the dummy index fields are free'd up.
277+
-- See yb_dummy_baserel_index_open()/yb_free_dummy_baserel_index().
278+
SELECT yb_index_check('matview_b_idx'::regclass);
279+
yb_index_check
280+
----------------
281+
282+
(1 row)
283+
269284
-- Index of a colocated relation
270285
CREATE DATABASE colocateddb COLOCATION = TRUE;
271286
\c colocateddb

src/postgres/src/test/regress/sql/yb.orig.yb_index_check.sql

+8-4
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,6 @@ END;
8686
$$;
8787
$force_cache_refresh$ AS force_cache_refresh \gset
8888

89-
90-
SET yb_non_ddl_txn_for_sys_tables_allowed = TRUE;
91-
9289
-- Missing Index Row
9390
UPDATE pg_index SET indisready = FALSE, indisvalid = FALSE, indislive = FALSE WHERE indexrelid = 'abcd_b_c_d_idx'::regclass;
9491
:force_cache_refresh
@@ -155,7 +152,6 @@ SELECT yb_index_check('abcd_b_c_idx'::regclass::oid);
155152

156153
UPDATE abcd SET d = 51 WHERE a = 51;
157154
SELECT yb_index_check('abcd_b_c_idx'::regclass::oid);
158-
SET yb_non_ddl_txn_for_sys_tables_allowed = FALSE;
159155

160156
-- Index of a partitioned table
161157
CREATE TABLE part(a int, b int, c int, d int) PARTITION BY RANGE(a);
@@ -172,6 +168,14 @@ SELECT yb_index_check('part_2_b_c_d_idx'::regclass::oid);
172168
SELECT yb_index_check('part_2k_4k_b_c_d_idx'::regclass::oid);
173169
SELECT yb_index_check('part_4k_6k_b_c_d_idx'::regclass::oid);
174170

171+
-- Index of materialized view
172+
CREATE MATERIALIZED VIEW matview AS SELECT * FROM abcd;
173+
CREATE INDEX matview_b_idx ON matview (b);
174+
SELECT yb_index_check('matview_b_idx'::regclass);
175+
-- Execute the same command second time to check if the dummy index fields are free'd up.
176+
-- See yb_dummy_baserel_index_open()/yb_free_dummy_baserel_index().
177+
SELECT yb_index_check('matview_b_idx'::regclass);
178+
175179
-- Index of a colocated relation
176180
CREATE DATABASE colocateddb COLOCATION = TRUE;
177181
\c colocateddb

0 commit comments

Comments
 (0)