From 1a5766274d40525ea8d4a994cd739b1f5ecfb5a2 Mon Sep 17 00:00:00 2001 From: Julien Rouhaud Date: Thu, 16 Mar 2023 11:55:44 +0800 Subject: [PATCH] Check that hypopg_relation_size was given a hypothetical index oid. And error out instead of returning 0 if that's not the case. While at it, some iterating over the list of hypothetical indexes once we found the wanted one. --- expected/hypopg.out | 3 +++ hypopg_index.c | 6 ++++++ test/sql/hypopg.sql | 3 +++ 3 files changed, 12 insertions(+) diff --git a/expected/hypopg.out b/expected/hypopg.out index 990cb46..a00648f 100644 --- a/expected/hypopg.out +++ b/expected/hypopg.out @@ -105,6 +105,9 @@ ORDER BY indexrelid; f (3 rows) +-- Should detect invalid argument +SELECT hypopg_relation_size(1); +ERROR: oid 1 is not a hypothetical index -- locally disable hypoopg SET hypopg.enabled to false; -- no hypothetical index should be used diff --git a/hypopg_index.c b/hypopg_index.c index 204fc3e..e7a0b7a 100644 --- a/hypopg_index.c +++ b/hypopg_index.c @@ -1388,6 +1388,7 @@ hypopg_relation_size(PG_FUNCTION_ARGS) double tuples; Oid indexid = PG_GETARG_OID(0); ListCell *lc; + bool found = false; pages = 0; tuples = 0; @@ -1398,9 +1399,14 @@ hypopg_relation_size(PG_FUNCTION_ARGS) if (entry->oid == indexid) { hypo_estimate_index_simple(entry, &pages, &tuples); + found = true; + break; } } + if (!found) + elog(ERROR, "oid %u is not a hypothetical index", indexid); + PG_RETURN_INT64(pages * 1.0L * BLCKSZ); } diff --git a/test/sql/hypopg.sql b/test/sql/hypopg.sql index 28e3fcb..64cfcd5 100644 --- a/test/sql/hypopg.sql +++ b/test/sql/hypopg.sql @@ -70,6 +70,9 @@ SELECT hypopg_relation_size(indexrelid) = current_setting('block_size')::bigint FROM hypopg() ORDER BY indexrelid; +-- Should detect invalid argument +SELECT hypopg_relation_size(1); + -- locally disable hypoopg SET hypopg.enabled to false;