*
*
* IDENTIFICATION
- * $PostgreSQL: pgsql/src/backend/utils/adt/regexp.c,v 1.72 2007/08/11 03:56:24 tgl Exp $
+ * $PostgreSQL: pgsql/src/backend/utils/adt/regexp.c,v 1.73 2007/08/11 19:16:41 tgl Exp $
*
* Alistair Crooks added the code for the regex caching
* agc - cached the regular expressions used - there's a good chance
bool force_glob,
bool use_subpatterns,
bool ignore_degenerate);
+static void cleanup_regexp_matches(regexp_matches_ctx *matchctx);
static ArrayType *build_regexp_matches_result(regexp_matches_ctx *matchctx);
static Datum build_regexp_split_result(regexp_matches_ctx *splitctx);
SRF_RETURN_NEXT(funcctx, PointerGetDatum(result_ary));
}
+ /* release space in multi-call ctx to avoid intraquery memory leak */
+ cleanup_regexp_matches(matchctx);
+
SRF_RETURN_DONE(funcctx);
}
return matchctx;
}
+/*
+ * cleanup_regexp_matches - release memory of a regexp_matches_ctx
+ */
+static void
+cleanup_regexp_matches(regexp_matches_ctx *matchctx)
+{
+ pfree(matchctx->orig_str);
+ pfree(matchctx->match_locs);
+ if (matchctx->elems)
+ pfree(matchctx->elems);
+ if (matchctx->nulls)
+ pfree(matchctx->nulls);
+ pfree(matchctx);
+}
+
/*
* build_regexp_matches_result - build output array for current match
*/
SRF_RETURN_NEXT(funcctx, result);
}
+ /* release space in multi-call ctx to avoid intraquery memory leak */
+ cleanup_regexp_matches(splitctx);
+
SRF_RETURN_DONE(funcctx);
}
splitctx->next_match++;
}
+ /*
+ * We don't call cleanup_regexp_matches here; it would try to pfree
+ * the input string, which we didn't copy. The space is not in a
+ * long-lived memory context anyway.
+ */
+
PG_RETURN_ARRAYTYPE_P(makeArrayResult(astate, CurrentMemoryContext));
}