Skip to content

Commit 1bbbd78

Browse files
committed
introduce safe wrapper function pathman_cache_search_relid()
1 parent b8659fa commit 1bbbd78

File tree

4 files changed

+73
-29
lines changed

4 files changed

+73
-29
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,4 @@ regression.out
99
*.gcda
1010
*.gcno
1111
*.gcov
12-
pg_pathman--1.3.sql
12+
pg_pathman--*.sql

src/init.c

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,44 @@ static uint32 build_sql_facade_version(char *version_cstr);
105105
static uint32 get_sql_facade_version(void);
106106
static void validate_sql_facade_version(uint32 ver);
107107

108+
109+
/*
110+
* Safe hash search (takes care of disabled pg_pathman).
111+
*/
112+
void *
113+
pathman_cache_search_relid(HTAB *cache_table,
114+
Oid relid,
115+
HASHACTION action,
116+
bool *found)
117+
{
118+
switch (action)
119+
{
120+
/* May return NULL */
121+
case HASH_FIND:
122+
case HASH_REMOVE:
123+
if (!cache_table)
124+
return NULL;
125+
break;
126+
127+
/* Must return valid pointer */
128+
case HASH_ENTER:
129+
if (!cache_table)
130+
elog(ERROR, "pg_pathman is not initialized yet");
131+
break;
132+
133+
/* Something strange has just happened */
134+
default:
135+
elog(ERROR, "unexpected action in function "
136+
CppAsString(pathman_cache_search_relid));
137+
break;
138+
}
139+
140+
Assert(cache_table);
141+
142+
/* Everything is fine */
143+
return hash_search(cache_table, (const void *) &relid, action, found);
144+
}
145+
108146
/*
109147
* Save and restore main init state.
110148
*/
@@ -279,6 +317,10 @@ init_local_cache(void)
279317
{
280318
HASHCTL ctl;
281319

320+
/* Destroy caches, just in case */
321+
hash_destroy(partitioned_rels);
322+
hash_destroy(parent_cache);
323+
282324
memset(&ctl, 0, sizeof(ctl));
283325
ctl.keysize = sizeof(Oid);
284326
ctl.entrysize = sizeof(PartRelationInfo);

src/init.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,11 @@ extern PathmanInitState pg_pathman_init_state;
102102
#define CURRENT_LIB_VERSION 0x010300
103103

104104

105+
void *pathman_cache_search_relid(HTAB *cache_table,
106+
Oid relid,
107+
HASHACTION action,
108+
bool *found);
109+
105110
/*
106111
* Save and restore PathmanInitState.
107112
*/

src/relation_info.c

Lines changed: 25 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,9 @@ refresh_pathman_relation_info(Oid relid,
8181
Datum param_values[Natts_pathman_config_params];
8282
bool param_isnull[Natts_pathman_config_params];
8383

84-
prel = (PartRelationInfo *) hash_search(partitioned_rels,
85-
(const void *) &relid,
86-
HASH_ENTER, &found_entry);
84+
prel = (PartRelationInfo *) pathman_cache_search_relid(partitioned_rels,
85+
relid, HASH_ENTER,
86+
&found_entry);
8787
elog(DEBUG2,
8888
found_entry ?
8989
"Refreshing record for relation %u in pg_pathman's cache [%u]" :
@@ -239,9 +239,9 @@ invalidate_pathman_relation_info(Oid relid, bool *found)
239239
HASHACTION action = found ? HASH_FIND : HASH_ENTER;
240240
PartRelationInfo *prel;
241241

242-
prel = hash_search(partitioned_rels,
243-
(const void *) &relid,
244-
action, &prel_found);
242+
prel = pathman_cache_search_relid(partitioned_rels,
243+
relid, action,
244+
&prel_found);
245245

246246
if ((action == HASH_FIND ||
247247
(action == HASH_ENTER && prel_found)) && PrelIsValid(prel))
@@ -272,10 +272,9 @@ invalidate_pathman_relation_info(Oid relid, bool *found)
272272
const PartRelationInfo *
273273
get_pathman_relation_info(Oid relid)
274274
{
275-
const PartRelationInfo *prel = hash_search(partitioned_rels,
276-
(const void *) &relid,
277-
HASH_FIND, NULL);
278-
275+
const PartRelationInfo *prel = pathman_cache_search_relid(partitioned_rels,
276+
relid, HASH_FIND,
277+
NULL);
279278
/* Refresh PartRelationInfo if needed */
280279
if (prel && !PrelIsValid(prel))
281280
{
@@ -345,20 +344,19 @@ get_pathman_relation_info_after_lock(Oid relid,
345344
void
346345
remove_pathman_relation_info(Oid relid)
347346
{
348-
PartRelationInfo *prel = hash_search(partitioned_rels,
349-
(const void *) &relid,
350-
HASH_FIND, NULL);
351-
if (prel && PrelIsValid(prel))
347+
PartRelationInfo *prel = pathman_cache_search_relid(partitioned_rels,
348+
relid, HASH_FIND,
349+
NULL);
350+
if (PrelIsValid(prel))
352351
{
353352
/* Free these arrays iff they're not NULL */
354353
FreeChildrenArray(prel);
355354
FreeRangesArray(prel);
356355
}
357356

358357
/* Now let's remove the entry completely */
359-
hash_search(partitioned_rels,
360-
(const void *) &relid,
361-
HASH_REMOVE, NULL);
358+
pathman_cache_search_relid(partitioned_rels, relid,
359+
HASH_REMOVE, NULL);
362360

363361
elog(DEBUG2,
364362
"Removing record for relation %u in pg_pathman's cache [%u]",
@@ -509,10 +507,10 @@ cache_parent_of_partition(Oid partition, Oid parent)
509507
bool found;
510508
PartParentInfo *ppar;
511509

512-
ppar = hash_search(parent_cache,
513-
(const void *) &partition,
514-
HASH_ENTER, &found);
515-
510+
ppar = pathman_cache_search_relid(parent_cache,
511+
partition,
512+
HASH_ENTER,
513+
&found);
516514
elog(DEBUG2,
517515
found ?
518516
"Refreshing record for child %u in pg_pathman's cache [%u]" :
@@ -551,10 +549,10 @@ get_parent_of_partition_internal(Oid partition,
551549
{
552550
const char *action_str; /* "Fetching"\"Resetting" */
553551
Oid parent;
554-
PartParentInfo *ppar = hash_search(parent_cache,
555-
(const void *) &partition,
556-
HASH_FIND, NULL);
557-
552+
PartParentInfo *ppar = pathman_cache_search_relid(parent_cache,
553+
partition,
554+
HASH_FIND,
555+
NULL);
558556
/* Set 'action_str' */
559557
switch (action)
560558
{
@@ -581,9 +579,8 @@ get_parent_of_partition_internal(Oid partition,
581579

582580
/* Remove entry if necessary */
583581
if (action == HASH_REMOVE)
584-
hash_search(parent_cache,
585-
(const void *) &partition,
586-
HASH_REMOVE, NULL);
582+
pathman_cache_search_relid(parent_cache, partition,
583+
HASH_REMOVE, NULL);
587584
}
588585
/* Try fetching parent from syscache if 'status' is provided */
589586
else if (status)

0 commit comments

Comments
 (0)