Skip to content

Commit e5ad134

Browse files
committed
make load_config() return status, rework pg_pathman's utility relations (e.g. pathman_config) Oid cache
1 parent e373da0 commit e5ad134

File tree

3 files changed

+63
-10
lines changed

3 files changed

+63
-10
lines changed

src/init.c

Lines changed: 55 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ bool initialization_needed = true;
5353
static bool relcache_callback_needed = true;
5454

5555

56+
static bool init_pathman_relation_oids(void);
57+
static void fini_pathman_relation_oids(void);
5658
static void init_local_cache(void);
5759
static void fini_local_cache(void);
5860
static void read_pathman_config(void);
@@ -79,12 +81,22 @@ static int oid_cmp(const void *p1, const void *p2);
7981

8082
/*
8183
* Create local PartRelationInfo cache & load pg_pathman's config.
84+
* Return true on success. May occasionally emit ERROR.
8285
*/
83-
void
86+
bool
8487
load_config(void)
8588
{
86-
/* Cache PATHMAN_CONFIG relation's Oid */
87-
pathman_config_relid = get_relname_relid(PATHMAN_CONFIG, get_pathman_schema());
89+
/*
90+
* Try to cache important relids.
91+
*
92+
* Once CREATE EXTENSION stmt is processed, get_pathman_schema()
93+
* function starts returning perfectly valid schema Oid, which
94+
* means we have to check that *ALL* pg_pathman's relations' Oids
95+
* have been cached properly. Only then can we assume that
96+
* initialization is not needed anymore.
97+
*/
98+
if (!init_pathman_relation_oids())
99+
return false; /* remain 'uninitialized', exit before creating main caches */
88100

89101
init_local_cache(); /* create 'partitioned_rels' hash table */
90102
read_pathman_config(); /* read PATHMAN_CONFIG table & fill cache */
@@ -100,6 +112,8 @@ load_config(void)
100112
initialization_needed = false;
101113

102114
elog(DEBUG2, "pg_pathman's config has been loaded successfully [%u]", MyProcPid);
115+
116+
return true;
103117
}
104118

105119
/*
@@ -108,10 +122,11 @@ load_config(void)
108122
void
109123
unload_config(void)
110124
{
111-
/* Don't forget to reset cached PATHMAN_CONFIG relation's Oid */
112-
pathman_config_relid = InvalidOid;
125+
/* Don't forget to reset pg_pathman's cached relids */
126+
fini_pathman_relation_oids();
113127

114-
fini_local_cache(); /* destroy 'partitioned_rels' hash table */
128+
/* Destroy 'partitioned_rels' & 'parent_cache' hash tables */
129+
fini_local_cache();
115130

116131
/* Mark pg_pathman as uninitialized */
117132
initialization_needed = true;
@@ -128,6 +143,40 @@ estimate_pathman_shmem_size(void)
128143
return estimate_dsm_config_size() + MAXALIGN(sizeof(PathmanState));
129144
}
130145

146+
/*
147+
* Cache *all* important pg_pathman's relids at once.
148+
* We should NOT rely on any previously cached values.
149+
*/
150+
static bool
151+
init_pathman_relation_oids(void)
152+
{
153+
Oid schema = get_pathman_schema();
154+
Assert(schema != InvalidOid);
155+
156+
/* Cache PATHMAN_CONFIG relation's Oid */
157+
pathman_config_relid = get_relname_relid(PATHMAN_CONFIG, schema);
158+
/* NOTE: add more relations to be cached right here ^^^ */
159+
160+
/* Return false if *any* relation doesn't exist yet */
161+
if (pathman_config_relid == InvalidOid)
162+
{
163+
return false;
164+
}
165+
166+
/* Everything is fine, proceed */
167+
return true;
168+
}
169+
170+
/*
171+
* Forget *all* pg_pathman's cached relids.
172+
*/
173+
static void
174+
fini_pathman_relation_oids(void)
175+
{
176+
pathman_config_relid = InvalidOid;
177+
/* NOTE: add more relations to be forgotten right here ^^^ */
178+
}
179+
131180
/*
132181
* Initialize per-process resources.
133182
*/

src/init.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ extern bool initialization_needed;
2626

2727
Size estimate_pathman_shmem_size(void);
2828
void init_shmem_config(void);
29-
void load_config(void);
29+
bool load_config(void);
3030
void unload_config(void);
3131

3232
void fill_prel_with_partitions(const Oid *partitions,

src/worker.c

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,9 +104,13 @@ UnpackDatumFromByteArray(Datum *datum, Size datum_size, bool typbyval,
104104
static void
105105
bg_worker_load_config(const char *bgw_name)
106106
{
107-
load_config();
108-
elog(LOG, "%s: loaded pg_pathman's config [%u]",
109-
bgw_name, MyProcPid);
107+
/* Try to load config */
108+
if (!load_config())
109+
elog(ERROR, "%s: could not load pg_pathman's config [%u]",
110+
bgw_name, MyProcPid);
111+
else
112+
elog(LOG, "%s: loaded pg_pathman's config [%u]",
113+
bgw_name, MyProcPid);
110114
}
111115

112116
/*

0 commit comments

Comments
 (0)