8
8
*
9
9
*
10
10
* IDENTIFICATION
11
- * $PostgreSQL: pgsql/src/backend/utils/adt/regexp.c,v 1.72 2007/08/11 03:56:24 tgl Exp $
11
+ * $PostgreSQL: pgsql/src/backend/utils/adt/regexp.c,v 1.73 2007/08/11 19:16:41 tgl Exp $
12
12
*
13
13
* Alistair Crooks added the code for the regex caching
14
14
* agc - cached the regular expressions used - there's a good chance
@@ -112,6 +112,7 @@ static regexp_matches_ctx *setup_regexp_matches(text *orig_str, text *pattern,
112
112
bool force_glob ,
113
113
bool use_subpatterns ,
114
114
bool ignore_degenerate );
115
+ static void cleanup_regexp_matches (regexp_matches_ctx * matchctx );
115
116
static ArrayType * build_regexp_matches_result (regexp_matches_ctx * matchctx );
116
117
static Datum build_regexp_split_result (regexp_matches_ctx * splitctx );
117
118
@@ -815,6 +816,9 @@ regexp_matches(PG_FUNCTION_ARGS)
815
816
SRF_RETURN_NEXT (funcctx , PointerGetDatum (result_ary ));
816
817
}
817
818
819
+ /* release space in multi-call ctx to avoid intraquery memory leak */
820
+ cleanup_regexp_matches (matchctx );
821
+
818
822
SRF_RETURN_DONE (funcctx );
819
823
}
820
824
@@ -968,6 +972,21 @@ setup_regexp_matches(text *orig_str, text *pattern, text *flags,
968
972
return matchctx ;
969
973
}
970
974
975
+ /*
976
+ * cleanup_regexp_matches - release memory of a regexp_matches_ctx
977
+ */
978
+ static void
979
+ cleanup_regexp_matches (regexp_matches_ctx * matchctx )
980
+ {
981
+ pfree (matchctx -> orig_str );
982
+ pfree (matchctx -> match_locs );
983
+ if (matchctx -> elems )
984
+ pfree (matchctx -> elems );
985
+ if (matchctx -> nulls )
986
+ pfree (matchctx -> nulls );
987
+ pfree (matchctx );
988
+ }
989
+
971
990
/*
972
991
* build_regexp_matches_result - build output array for current match
973
992
*/
@@ -1050,6 +1069,9 @@ regexp_split_to_table(PG_FUNCTION_ARGS)
1050
1069
SRF_RETURN_NEXT (funcctx , result );
1051
1070
}
1052
1071
1072
+ /* release space in multi-call ctx to avoid intraquery memory leak */
1073
+ cleanup_regexp_matches (splitctx );
1074
+
1053
1075
SRF_RETURN_DONE (funcctx );
1054
1076
}
1055
1077
@@ -1084,6 +1106,12 @@ Datum regexp_split_to_array(PG_FUNCTION_ARGS)
1084
1106
splitctx -> next_match ++ ;
1085
1107
}
1086
1108
1109
+ /*
1110
+ * We don't call cleanup_regexp_matches here; it would try to pfree
1111
+ * the input string, which we didn't copy. The space is not in a
1112
+ * long-lived memory context anyway.
1113
+ */
1114
+
1087
1115
PG_RETURN_ARRAYTYPE_P (makeArrayResult (astate , CurrentMemoryContext ));
1088
1116
}
1089
1117
0 commit comments