23
23
#include "utils/builtins.h"
24
24
#include "catalog/pg_db_role_setting.h"
25
25
#include "commands/dbcommands.h"
26
+ #include "utils/lsyscache.h"
27
+ #include "catalog/pg_extension.h"
28
+ #include "catalog/indexing.h"
29
+ #include "commands/extension.h"
30
+ #include "access/sysattr.h"
31
+ #include "access/htup_details.h"
32
+ #include "utils/fmgroids.h"
26
33
27
34
28
35
#include "char_array.h"
@@ -52,6 +59,7 @@ bool scheduler_service_enabled = false;
52
59
char * scheduler_schema = NULL ;
53
60
/* Custom GUC done */
54
61
62
+ Oid scheduler_schema_oid = InvalidOid ;
55
63
Oid scheduler_atjob_id_OID = InvalidOid ;
56
64
57
65
extern void
@@ -166,11 +174,59 @@ bool is_scheduler_enabled(void)
166
174
return false;
167
175
}
168
176
177
+ char * get_scheduler_schema_name (void )
178
+ {
179
+ Oid ns_oid ;
180
+ Oid ext_oid ;
181
+
182
+ Relation rel ;
183
+ SysScanDesc scandesc ;
184
+ HeapTuple tuple ;
185
+ ScanKeyData entry [1 ];
186
+ LOCKMODE heap_lock = AccessShareLock ;
187
+
188
+
189
+ if (scheduler_schema_oid == InvalidOid )
190
+ {
191
+ if (!IsTransactionState ())
192
+ elog (ERROR , "pgpro_scheduler: cannot get extension scheme (1)" );
193
+ ext_oid = get_extension_oid ("pgpro_scheduler" , true);
194
+ if (ext_oid == InvalidOid )
195
+ elog (ERROR , "pgpro_scheduler: cannot get extension id" );
196
+
197
+ ScanKeyInit (& entry [0 ],
198
+ ObjectIdAttributeNumber ,
199
+ BTEqualStrategyNumber ,
200
+ F_OIDEQ ,
201
+ ObjectIdGetDatum (ext_oid ));
202
+ rel = heap_open (ExtensionRelationId , heap_lock );
203
+ scandesc = systable_beginscan (rel , ExtensionOidIndexId , true,
204
+ NULL , 1 , entry );
205
+ tuple = systable_getnext (scandesc );
206
+ if (HeapTupleIsValid (tuple ))
207
+ ns_oid = ((Form_pg_extension ) GETSTRUCT (tuple ))-> extnamespace ;
208
+ else
209
+ ns_oid = InvalidOid ;
210
+ systable_endscan (scandesc );
211
+ heap_close (rel , heap_lock );
212
+
213
+ if (ns_oid == InvalidOid )
214
+ elog (ERROR , "pgpro_scheduler: cannot get extension schema oid" );
215
+
216
+ scheduler_schema_oid = ns_oid ;
217
+ }
218
+ else
219
+ {
220
+ ns_oid = scheduler_schema_oid ;
221
+ }
222
+
223
+ return get_namespace_name (ns_oid );
224
+ }
225
+
169
226
char * set_schema (const char * name , bool get_old )
170
227
{
171
228
char * schema_name = NULL ;
172
229
char * current = NULL ;
173
- bool free_name = false;
174
230
175
231
if (get_old )
176
232
current = _mcopy_string (NULL , (char * )GetConfigOption ("search_path" , true, false));
@@ -180,11 +236,9 @@ char *set_schema(const char *name, bool get_old)
180
236
}
181
237
else
182
238
{
183
- schema_name = _mcopy_string (NULL , (char * )GetConfigOption ("schedule.schema" , true, false));
184
- free_name = true;
239
+ schema_name = get_scheduler_schema_name ();
185
240
}
186
241
SetConfigOption ("search_path" , schema_name , PGC_USERSET , PGC_S_SESSION );
187
- if (free_name ) pfree (schema_name );
188
242
189
243
return current ;
190
244
}
0 commit comments