Skip to content

Commit 9dcdedc

Browse files
committed
use new syscache for pg_extension
1 parent 26933b0 commit 9dcdedc

File tree

3 files changed

+90
-7
lines changed

3 files changed

+90
-7
lines changed

src/catalog.c

Lines changed: 50 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,7 @@
1111

1212
#include "plpgsql_check.h"
1313

14-
#include "access/genam.h"
15-
16-
#include "access/htup_details.h"
17-
#include "access/table.h"
18-
1914
#include "catalog/pg_extension.h"
20-
#include "catalog/indexing.h"
2115
#include "catalog/pg_language.h"
2216
#include "catalog/pg_operator.h"
2317
#include "catalog/pg_proc.h"
@@ -31,10 +25,24 @@
3125
#include "utils/syscache.h"
3226
#include "utils/regproc.h"
3327

34-
#include "utils/rel.h"
3528
#include "catalog/pg_proc.h"
3629
#include "utils/syscache.h"
3730

31+
/*
32+
* Pre pg 18 doesn't support system cache for pg_extension,
33+
* we need to manipulate with table on low level.
34+
*/
35+
#if PG_VERSION_NUM < 180000
36+
37+
#include "access/genam.h"
38+
39+
#include "access/htup_details.h"
40+
#include "access/table.h"
41+
#include "catalog/indexing.h"
42+
#include "utils/rel.h"
43+
44+
#endif
45+
3846
static Oid plpgsql_check_PLpgSQLlanguageId = InvalidOid;
3947

4048
/*
@@ -216,6 +224,8 @@ get_extension_schema(Oid ext_oid)
216224

217225
#endif
218226

227+
#if PG_VERSION_NUM < 180000
228+
219229
/*
220230
* get_extension_version - given an extension OID, look up the version
221231
*
@@ -266,6 +276,39 @@ get_extension_version(Oid ext_oid)
266276
return result;
267277
}
268278

279+
#else
280+
281+
/*
282+
* Returns a palloc'd string, or NULL if no such extension.
283+
* Use extension syscache from PostgreSQL 18+
284+
*/
285+
char *
286+
get_extension_version2(Oid ext_oid)
287+
{
288+
HeapTuple extTuple;
289+
Datum extversiondatum;
290+
char *result;
291+
bool isnull;
292+
293+
extTuple = SearchSysCache1(EXTENSIONOID, ObjectIdGetDatum(ext_oid));
294+
if (!HeapTupleIsValid(extTuple))
295+
elog(ERROR, "cache lookup failed for extension %u", ext_oid);
296+
297+
extversiondatum = SysCacheGetAttr(EXTENSIONOID, extTuple,
298+
Anum_pg_extension_extversion, &isnull);
299+
300+
if (isnull)
301+
elog(ERROR, "extversion is null");
302+
303+
result = TextDatumGetCString(extversiondatum);
304+
305+
ReleaseSysCache(extTuple);
306+
307+
return result;
308+
}
309+
310+
#endif
311+
269312
/*
270313
* Returns oid of pragma function. It is used for elimination
271314
* pragma function from volatility tests.

src/plpgsql_check.c

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,13 @@
3535
#include "utils/guc.h"
3636
#include "utils/memutils.h"
3737

38+
#if PG_VERSION_NUM >= 180000
39+
40+
#include "utils/inval.h"
41+
#include "utils/syscache.h"
42+
43+
#endif
44+
3845
#ifdef PG_MODULE_MAGIC
3946
PG_MODULE_MAGIC;
4047
#endif
@@ -109,6 +116,15 @@ plpgsql_check__ns_lookup_t plpgsql_check__ns_lookup_p;
109116

110117
static bool is_expected_extversion = false;
111118

119+
#if PG_VERSION_NUM >= 180000
120+
121+
static void
122+
pg_extension_cache_callback(Datum arg, int cacheid, uint32 hashvalue)
123+
{
124+
is_expected_extversion = false;
125+
}
126+
127+
#endif
112128

113129
/*
114130
* load_external_function retursn PGFunctions - we need generic function, so
@@ -129,8 +145,16 @@ plpgsql_check_check_ext_version(Oid fn_oid)
129145
extoid = getExtensionOfObject(ProcedureRelationId, fn_oid);
130146
Assert(OidIsValid(extoid));
131147

148+
#if PG_VERSION_NUM >= 180000
149+
150+
extver = get_extension_version2(extoid);
151+
152+
#else
153+
132154
extver = get_extension_version(extoid);
133155

156+
#endif
157+
134158
Assert(extver);
135159

136160
if (strcmp(EXPECTED_EXTVERSION, extver) != 0)
@@ -421,6 +445,14 @@ _PG_init(void)
421445
plpgsql_check_tracer_init();
422446
plpgsql_check_cursors_leaks_init();
423447

448+
449+
#if PG_VERSION_NUM >= 180000
450+
451+
CacheRegisterSyscacheCallback(EXTENSIONOID,
452+
pg_extension_cache_callback,
453+
(Datum) 0);
454+
455+
#endif
424456
inited = true;
425457
}
426458

src/plpgsql_check.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,8 +230,16 @@ extern char *plpgsql_check_get_src(HeapTuple procTuple);
230230
extern Oid plpgsql_check_pragma_func_oid(void);
231231
extern bool plpgsql_check_is_plpgsql_function(Oid foid);
232232
extern Oid plpgsql_check_get_op_namespace(Oid opno);
233+
234+
#if PG_VERSION_NUM < 180000
235+
233236
extern char *get_extension_version(Oid ext_oid);
234237

238+
#else
239+
240+
extern char *get_extension_version2(Oid ext_oid);
241+
242+
#endif
235243

236244
/*
237245
* functions from tablefunc.c

0 commit comments

Comments
 (0)