Skip to content

Commit 99d03f1

Browse files
committed
fix build against pg 18
1 parent 3897083 commit 99d03f1

File tree

4 files changed

+152
-0
lines changed

4 files changed

+152
-0
lines changed

src/check_function.c

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,16 @@ int plpgsql_check_mode = PLPGSQL_CHECK_MODE_BY_FUNCTION;
4040

4141
typedef struct plpgsql_hashent
4242
{
43+
#if PG_VERSION_NUM >= 180000
44+
45+
CachedFunctionHashKey key;
46+
47+
#else
48+
4349
PLpgSQL_func_hashkey key;
50+
51+
#endif
52+
4453
TransactionId fn_xmin;
4554
ItemPointerData fn_tid;
4655
bool is_checked;
@@ -210,8 +219,16 @@ plpgsql_check_function_internal(plpgsql_check_result_info *ri,
210219
* Mark the function as busy, ensure higher than zero usage. There is no
211220
* reason for protection function against delete, but I afraid of asserts.
212221
*/
222+
#if PG_VERSION_NUM >= 180000
223+
224+
function->cfunc.use_count++;
225+
226+
#else
227+
213228
function->use_count++;
214229

230+
#endif
231+
215232
/* Create a fake runtime environment and process check */
216233
switch (cinfo->trigtype)
217234
{
@@ -229,7 +246,16 @@ plpgsql_check_function_internal(plpgsql_check_result_info *ri,
229246
}
230247

231248
function->cur_estate = cur_estate;
249+
250+
#if PG_VERSION_NUM >= 180000
251+
252+
function->cfunc.use_count--;
253+
254+
#else
255+
232256
function->use_count--;
257+
258+
#endif
233259
}
234260
else
235261
elog(NOTICE, "plpgsql_check is disabled");
@@ -268,7 +294,17 @@ plpgsql_check_function_internal(plpgsql_check_result_info *ri,
268294
if (function)
269295
{
270296
function->cur_estate = cur_estate;
297+
298+
#if PG_VERSION_NUM >= 180000
299+
300+
function->cfunc.use_count--;
301+
302+
#else
303+
271304
function->use_count--;
305+
306+
#endif
307+
272308
release_exprs(cstate.exprs);
273309
}
274310

@@ -1320,7 +1356,17 @@ plpgsql_check_HashTableInit(void)
13201356
Assert(plpgsql_check_HashTable == NULL);
13211357

13221358
memset(&ctl, 0, sizeof(ctl));
1359+
1360+
#if PG_VERSION_NUM >= 180000
1361+
1362+
ctl.keysize = sizeof(CachedFunctionHashKey);
1363+
1364+
#else
1365+
13231366
ctl.keysize = sizeof(PLpgSQL_func_hashkey);
1367+
1368+
#endif
1369+
13241370
ctl.entrysize = sizeof(plpgsql_check_HashEnt);
13251371
plpgsql_check_HashTable = hash_create("plpgsql_check function cache",
13261372
FUNCS_PER_USER,
@@ -1337,6 +1383,22 @@ plpgsql_check_is_checked(PLpgSQL_function *func)
13371383
{
13381384
plpgsql_check_HashEnt *hentry;
13391385

1386+
#if PG_VERSION_NUM >= 180000
1387+
1388+
if (!func->cfunc.fn_hashkey)
1389+
return false;
1390+
1391+
hentry = (plpgsql_check_HashEnt *) hash_search(plpgsql_check_HashTable,
1392+
(void *) func->cfunc.fn_hashkey,
1393+
HASH_FIND,
1394+
NULL);
1395+
1396+
if (hentry != NULL && hentry->fn_xmin == func->cfunc.fn_xmin &&
1397+
ItemPointerEquals(&hentry->fn_tid, &func->cfunc.fn_tid))
1398+
return hentry->is_checked;
1399+
1400+
#else
1401+
13401402
if (!func->fn_hashkey)
13411403
return false;
13421404

@@ -1349,6 +1411,8 @@ plpgsql_check_is_checked(PLpgSQL_function *func)
13491411
ItemPointerEquals(&hentry->fn_tid, &func->fn_tid))
13501412
return hentry->is_checked;
13511413

1414+
#endif
1415+
13521416
return false;
13531417
}
13541418

@@ -1365,6 +1429,18 @@ plpgsql_check_mark_as_checked(PLpgSQL_function *func)
13651429
plpgsql_check_HashEnt *hentry;
13661430
bool found;
13671431

1432+
#if PG_VERSION_NUM >= 180000
1433+
1434+
hentry = (plpgsql_check_HashEnt *) hash_search(plpgsql_check_HashTable,
1435+
(void *) func->cfunc.fn_hashkey,
1436+
HASH_ENTER,
1437+
&found);
1438+
1439+
hentry->fn_xmin = func->cfunc.fn_xmin;
1440+
hentry->fn_tid = func->cfunc.fn_tid;
1441+
1442+
#else
1443+
13681444
hentry = (plpgsql_check_HashEnt *) hash_search(plpgsql_check_HashTable,
13691445
(void *) func->fn_hashkey,
13701446
HASH_ENTER,
@@ -1373,6 +1449,7 @@ plpgsql_check_mark_as_checked(PLpgSQL_function *func)
13731449
hentry->fn_xmin = func->fn_xmin;
13741450
hentry->fn_tid = func->fn_tid;
13751451

1452+
#endif
13761453
hentry->is_checked = true;
13771454
}
13781455
}

src/cursors_leaks.c

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,12 @@
1717
#include "utils/guc.h"
1818
#include "utils/memutils.h"
1919

20+
#if PG_VERSION_NUM >= 180000
21+
22+
#include "utils/funccache.h"
23+
24+
#endif
25+
2026
bool plpgsql_check_cursors_leaks = true;
2127
bool plpgsql_check_cursors_leaks_strict = false;
2228
int plpgsql_check_cursors_leaks_level = WARNING;
@@ -102,8 +108,17 @@ get_function_trace(PLpgSQL_function *func)
102108
}
103109

104110
key.fn_oid = func->fn_oid;
111+
112+
#if PG_VERSION_NUM >= 180000
113+
114+
key.fn_xmin = func->cfunc.fn_xmin;
115+
116+
#else
117+
105118
key.fn_xmin = func->fn_xmin;
106119

120+
#endif
121+
107122
ftrace = (FunctionTrace *) hash_search(traces,
108123
(void *) &key,
109124
HASH_ENTER,
@@ -112,7 +127,17 @@ get_function_trace(PLpgSQL_function *func)
112127
if (!found)
113128
{
114129
ftrace->key.fn_oid = func->fn_oid;
130+
131+
#if PG_VERSION_NUM >= 180000
132+
133+
ftrace->key.fn_xmin = func->cfunc.fn_xmin;
134+
135+
#else
136+
115137
ftrace->key.fn_xmin = func->fn_xmin;
138+
139+
#endif
140+
116141
ftrace->ncursors = 0;
117142
ftrace->cursors_size = 0;
118143
ftrace->cursors_traces = NULL;
@@ -164,7 +189,16 @@ func_end(PLpgSQL_execstate *estate,
164189
* Iterate over traced cursors. Remove slots for tracing
165190
* immediately, when traced cursor is closed already.
166191
*/
192+
#if PG_VERSION_NUM >= 180000
193+
194+
if (ct->curname && ct->rec_level == func->cfunc.use_count)
195+
196+
#else
197+
167198
if (ct->curname && ct->rec_level == func->use_count)
199+
200+
#endif
201+
168202
{
169203
if (SPI_cursor_find(ct->curname))
170204
{
@@ -246,7 +280,16 @@ stmt_end(PLpgSQL_execstate *estate, PLpgSQL_stmt *stmt, void **plugin2_info)
246280

247281
if (SPI_cursor_find(ct->curname))
248282
{
283+
#if PG_VERSION_NUM >= 180000
284+
285+
if (estate->func->cfunc.use_count == 1 && !plpgsql_check_cursors_leaks_strict)
286+
287+
#else
288+
249289
if (estate->func->use_count == 1 && !plpgsql_check_cursors_leaks_strict)
290+
291+
#endif
292+
250293
{
251294
char *context;
252295

@@ -312,7 +355,17 @@ stmt_end(PLpgSQL_execstate *estate, PLpgSQL_stmt *stmt, void **plugin2_info)
312355
}
313356

314357
ct->stmtid = stmt->stmtid;
358+
359+
#if PG_VERSION_NUM >= 180000
360+
361+
ct->rec_level = estate->func->cfunc.use_count;
362+
363+
#else
364+
315365
ct->rec_level = estate->func->use_count;
366+
367+
#endif
368+
316369
ct->curname = pstrdup(curname);
317370

318371
MemoryContextSwitchTo(oldcxt);

src/pldbgapi2.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,8 +244,19 @@ func_info_init_hashkey(func_info_hashkey *hk, PLpgSQL_function *func)
244244
memset(hk, 0, sizeof(func_info_hashkey));
245245

246246
hk->fn_oid = func->fn_oid;
247+
248+
#if PG_VERSION_NUM >= 180000
249+
250+
hk->fn_xmin = func->cfunc.fn_xmin;
251+
hk->fn_tid = func->cfunc.fn_tid;
252+
253+
#else
254+
247255
hk->fn_xmin = func->fn_xmin;
248256
hk->fn_tid = func->fn_tid;
257+
258+
#endif
259+
249260
}
250261

251262
/*

src/profiler.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -411,8 +411,19 @@ profiler_init_hashkey(profiler_hashkey *hk, PLpgSQL_function *func)
411411

412412
hk->db_oid = MyDatabaseId;
413413
hk->fn_oid = func->fn_oid;
414+
415+
#if PG_VERSION_NUM >= 180000
416+
417+
hk->fn_xmin = func->cfunc.fn_xmin;
418+
hk->fn_tid = func->cfunc.fn_tid;
419+
420+
#else
421+
414422
hk->fn_xmin = func->fn_xmin;
415423
hk->fn_tid = func->fn_tid;
424+
425+
#endif
426+
416427
hk->chunk_num = 1;
417428
}
418429

0 commit comments

Comments
 (0)