Avoid freeing objects during json aggregate finalization

Commit f4fb45d15c tried to free memory during aggregate finalization.
This cause issues, particularly when used as a window function, so stop
doing that.

Per complaint by Jaime Casanova and diagnosis by Andres Freund

Discussion: https://postgr.es/m/YkfeMNYRCGhySKyg@ahch-to
This commit is contained in:
Andrew Dunstan 2022-04-04 10:12:30 -04:00
parent afb529e677
commit 4eb9798879

View File

@ -979,12 +979,6 @@ json_unique_check_init(JsonUniqueCheckState *cxt)
HASH_ELEM | HASH_CONTEXT | HASH_FUNCTION | HASH_COMPARE); HASH_ELEM | HASH_CONTEXT | HASH_FUNCTION | HASH_COMPARE);
} }
static void
json_unique_check_free(JsonUniqueCheckState *cxt)
{
hash_destroy(*cxt);
}
static bool static bool
json_unique_check_key(JsonUniqueCheckState *cxt, const char *key, int object_id) json_unique_check_key(JsonUniqueCheckState *cxt, const char *key, int object_id)
{ {
@ -1009,12 +1003,10 @@ json_unique_builder_init(JsonUniqueBuilderState *cxt)
} }
static void static void
json_unique_builder_free(JsonUniqueBuilderState *cxt) json_unique_builder_clean(JsonUniqueBuilderState *cxt)
{ {
json_unique_check_free(&cxt->check);
if (cxt->skipped_keys.data) if (cxt->skipped_keys.data)
pfree(cxt->skipped_keys.data); resetStringInfo(&cxt->skipped_keys);
} }
/* On-demand initialization of skipped_keys StringInfo structure */ /* On-demand initialization of skipped_keys StringInfo structure */
@ -1224,7 +1216,7 @@ json_object_agg_finalfn(PG_FUNCTION_ARGS)
if (state == NULL) if (state == NULL)
PG_RETURN_NULL(); PG_RETURN_NULL();
json_unique_builder_free(&state->unique_check); json_unique_builder_clean(&state->unique_check);
/* Else return state with appropriate object terminator added */ /* Else return state with appropriate object terminator added */
PG_RETURN_TEXT_P(catenate_stringinfo_string(state->str, " }")); PG_RETURN_TEXT_P(catenate_stringinfo_string(state->str, " }"));
@ -1333,7 +1325,7 @@ json_build_object_worker(int nargs, Datum *args, bool *nulls, Oid *types,
appendStringInfoChar(result, '}'); appendStringInfoChar(result, '}');
if (unique_keys) if (unique_keys)
json_unique_builder_free(&unique_check); json_unique_builder_clean(&unique_check);
return PointerGetDatum(cstring_to_text_with_len(result->data, result->len)); return PointerGetDatum(cstring_to_text_with_len(result->data, result->len));
} }