Skip to content

Commit b01bc44

Browse files
authored
Fixed pg re2 cache cleanup (#6978)
1 parent c9ea844 commit b01bc44

File tree

5 files changed

+61
-5
lines changed

5 files changed

+61
-5
lines changed

ydb/library/yql/parser/pg_wrapper/arena_ctx.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ TArenaMemoryContext::TArenaMemoryContext() {
118118
}
119119

120120
TArenaMemoryContext::~TArenaMemoryContext() {
121+
MemoryContextDeleteChildren(MyContext);
121122
Release();
122123
free(MyContext);
123124
}

ydb/library/yql/parser/pg_wrapper/comp_factory.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4948,7 +4948,10 @@ void* PgInitializeMainContext() {
49484948
}
49494949

49504950
void PgDestroyMainContext(void* ctx) {
4951-
delete (TMainContext*)ctx;
4951+
auto typedCtx = (TMainContext*)ctx;
4952+
MemoryContextDeleteChildren((MemoryContext)&typedCtx->Data);
4953+
MemoryContextDeleteChildren((MemoryContext)&typedCtx->ErrorData);
4954+
delete typedCtx;
49524955
}
49534956

49544957
void PgAcquireThreadContext(void* ctx) {

ydb/library/yql/parser/pg_wrapper/postgresql/src/backend/utils/adt/regexp.c

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -114,10 +114,9 @@ static __thread int num_res = 0; /* # of cached re's */
114114
static __thread cached_re_str re_array[MAX_CACHED_RES]; /* cached re's */
115115

116116
void RE_cleanup_cache(void) {
117-
int i;
118-
for (i = 0; i < num_res; ++i) {
119-
pg_regfree(&re_array[i].cre_re);
120-
free(re_array[i].cre_pat);
117+
if (RegexpCacheMemoryContext) {
118+
MemoryContextDelete(RegexpCacheMemoryContext);
119+
RegexpCacheMemoryContext = NULL;
121120
}
122121

123122
num_res = 0;
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#include "../pg_compat.h"
2+
3+
extern "C" {
4+
#include <ydb/library/yql/parser/pg_wrapper/postgresql/src/include/postgres.h>
5+
#include <ydb/library/yql/parser/pg_wrapper/postgresql/src/include/utils/memutils.h>
6+
}
7+
8+
#include "arena_ctx.h"
9+
10+
#include <ydb/library/yql/minikql/mkql_alloc.h>
11+
12+
#include <library/cpp/testing/unittest/registar.h>
13+
14+
namespace NYql {
15+
16+
Y_UNIT_TEST_SUITE(TPGMemoryTests) {
17+
Y_UNIT_TEST(TestArenaContextBasic) {
18+
TArenaMemoryContext arena;
19+
auto p1 = palloc(123);
20+
auto p2 = palloc(456);
21+
Y_UNUSED(p2);
22+
pfree(p1);
23+
}
24+
25+
Y_UNIT_TEST(TestMkqlContextBasic) {
26+
NKikimr::NMiniKQL::TScopedAlloc alloc(__LOCATION__);
27+
auto p1 = palloc(123);
28+
auto p2 = palloc(456);
29+
Y_UNUSED(p2);
30+
pfree(p1);
31+
}
32+
33+
Y_UNIT_TEST(TestArenaContextCleanupChild) {
34+
TArenaMemoryContext arena;
35+
auto tmpContext = AllocSetContextCreate(CurrentMemoryContext, "Tmp", ALLOCSET_SMALL_SIZES);
36+
auto oldcontext = MemoryContextSwitchTo(tmpContext);
37+
auto p1 = palloc(123);
38+
Y_UNUSED(p1);
39+
MemoryContextSwitchTo(oldcontext);
40+
}
41+
42+
Y_UNIT_TEST(TestMkqlContextCleanupChild) {
43+
NKikimr::NMiniKQL::TScopedAlloc alloc(__LOCATION__);
44+
auto tmpContext = AllocSetContextCreate(CurrentMemoryContext, "Tmp", ALLOCSET_SMALL_SIZES);
45+
auto oldcontext = MemoryContextSwitchTo(tmpContext);
46+
auto p1 = palloc(123);
47+
Y_UNUSED(p1);
48+
MemoryContextSwitchTo(oldcontext);
49+
}
50+
}
51+
52+
}

ydb/library/yql/parser/pg_wrapper/ut/ya.make

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ SRCS(
99
arrow_ut.cpp
1010
codegen_ut.cpp
1111
error_ut.cpp
12+
memory_ut.cpp
1213
parser_ut.cpp
1314
sort_ut.cpp
1415
type_cache_ut.cpp

0 commit comments

Comments
 (0)