From 0864fbe1fbff2000050afaee19bd50f34d2c714f Mon Sep 17 00:00:00 2001 From: Julien Rouhaud Date: Fri, 22 Jan 2021 17:49:48 +0800 Subject: [PATCH] Add a new hypo_get_index() function. This will be helpful in a following patch. The immediate effect is to fix a long standing small performance issue, as hypo_explain_get_index_name_hook didn't stop its loop once it found a matching entry. --- hypopg_index.c | 43 +++++++++++++++++++++++------------------- include/hypopg_index.h | 1 + 2 files changed, 25 insertions(+), 19 deletions(-) diff --git a/hypopg_index.c b/hypopg_index.c index d79648c..12d1dc5 100644 --- a/hypopg_index.c +++ b/hypopg_index.c @@ -1116,36 +1116,41 @@ hypo_injectHypotheticalIndex(PlannerInfo *root, rel->indexlist = lcons(index, rel->indexlist); } -/* Return the hypothetical index name is indexId is ours, NULL otherwise, as +/* + * Return the stored hypothetical index for a given oid if any, NULL otherwise + */ +hypoIndex * +hypo_get_index(Oid indexId) +{ + ListCell *lc; + + foreach(lc, hypoIndexes) + { + hypoIndex *entry = (hypoIndex *) lfirst(lc); + + if (entry->oid == indexId) + return entry; + } + + return NULL; +} + +/* Return the hypothetical index name ifs indexId is ours, NULL otherwise, as * this is what explain_get_index_name expects to continue his job. */ const char * hypo_explain_get_index_name_hook(Oid indexId) { - char *ret = NULL; - if (isExplain) { - /* - * we're in an explain-only command. Return the name of the - * hypothetical index name if it's one of ours, otherwise return NULL - */ - ListCell *lc; + hypoIndex *index = NULL; - foreach(lc, hypoIndexes) - { - hypoIndex *entry = (hypoIndex *) lfirst(lc); + index = hypo_get_index(indexId); - if (entry->oid == indexId) - { - ret = entry->indexname; - } - } + if (index) + return index->indexname; } - if (ret) - return ret; - if (prev_explain_get_index_name_hook) return prev_explain_get_index_name_hook(indexId); diff --git a/include/hypopg_index.h b/include/hypopg_index.h index 323cadc..3ffb637 100644 --- a/include/hypopg_index.h +++ b/include/hypopg_index.h @@ -120,6 +120,7 @@ PGDLLEXPORT Datum hypopg_get_indexdef(PG_FUNCTION_ARGS); PGDLLEXPORT Datum hypopg_reset_index(PG_FUNCTION_ARGS); extern explain_get_index_name_hook_type prev_explain_get_index_name_hook; +hypoIndex *hypo_get_index(Oid indexId); const char *hypo_explain_get_index_name_hook(Oid indexId); void hypo_injectHypotheticalIndex(PlannerInfo *root,