aboutsummaryrefslogtreecommitdiff
path: root/src/backend/utils/adt/jsonfuncs.c
diff options
context:
space:
mode:
authorMichael Paquier <michael@paquier.xyz>2022-03-07 10:26:29 +0900
committerMichael Paquier <michael@paquier.xyz>2022-03-07 10:26:29 +0900
commit9e98583898c347e007958c8a09911be2ea4acfb9 (patch)
tree0b66299e16644567b821540245ea4e53d684d3ca /src/backend/utils/adt/jsonfuncs.c
parent770011e3f39f21f2095d3a044b72460c4efac345 (diff)
downloadpostgresql-9e98583898c347e007958c8a09911be2ea4acfb9.tar.gz
postgresql-9e98583898c347e007958c8a09911be2ea4acfb9.zip
Create routine able to set single-call SRFs for Materialize mode
Set-returning functions that use the Materialize mode, creating a tuplestore to include all the tuples returned in a set rather than doing so in multiple calls, use roughly the same set of steps to prepare ReturnSetInfo for this job: - Check if ReturnSetInfo supports returning a tuplestore and if the materialize mode is enabled. - Create a tuplestore for all the tuples part of the returned set in the per-query memory context, stored in ReturnSetInfo->setResult. - Build a tuple descriptor mostly from get_call_result_type(), then stored in ReturnSetInfo->setDesc. Note that there are some cases where the SRF's tuple descriptor has to be the one specified by the function caller. This refactoring is done so as there are (well, should be) no behavior changes in any of the in-core functions refactored, and the centralized function that checks and sets up the function's ReturnSetInfo can be controlled with a set of bits32 options. Two of them prove to be necessary now: - SRF_SINGLE_USE_EXPECTED to use expectedDesc as tuple descriptor, as expected by the function's caller. - SRF_SINGLE_BLESS to validate the tuple descriptor for the SRF. The same initialization pattern is simplified in 28 places per my count as of src/backend/, shaving up to ~900 lines of code. These mostly come from the removal of the per-query initializations and the sanity checks now grouped in a single location. There are more locations that could be simplified in contrib/, that are left for a follow-up cleanup. fcc2817, 07daca5 and d61a361 have prepared the areas of the code related to this change, to ease this refactoring. Author: Melanie Plageman, Michael Paquier Reviewed-by: Álvaro Herrera, Justin Pryzby Discussion: https://postgr.es/m/CAAKRu_azyd1Z3W_r7Ou4sorTjRCs+PxeHw1CWJeXKofkE6TuZg@mail.gmail.com
Diffstat (limited to 'src/backend/utils/adt/jsonfuncs.c')
-rw-r--r--src/backend/utils/adt/jsonfuncs.c141
1 files changed, 11 insertions, 130 deletions
diff --git a/src/backend/utils/adt/jsonfuncs.c b/src/backend/utils/adt/jsonfuncs.c
index 2457061f97e..29664aa6e40 100644
--- a/src/backend/utils/adt/jsonfuncs.c
+++ b/src/backend/utils/adt/jsonfuncs.c
@@ -1909,9 +1909,6 @@ each_worker_jsonb(FunctionCallInfo fcinfo, const char *funcname, bool as_text)
{
Jsonb *jb = PG_GETARG_JSONB_P(0);
ReturnSetInfo *rsi;
- Tuplestorestate *tuple_store;
- TupleDesc tupdesc;
- TupleDesc ret_tdesc;
MemoryContext old_cxt,
tmp_cxt;
bool skipNested = false;
@@ -1926,30 +1923,7 @@ each_worker_jsonb(FunctionCallInfo fcinfo, const char *funcname, bool as_text)
funcname)));
rsi = (ReturnSetInfo *) fcinfo->resultinfo;
-
- if (!rsi || !IsA(rsi, ReturnSetInfo))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("set-valued function called in context that cannot accept a set")));
- if (!(rsi->allowedModes & SFRM_Materialize))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("materialize mode required, but it is not allowed in this context")));
-
- rsi->returnMode = SFRM_Materialize;
-
- if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE)
- elog(ERROR, "return type must be a row type");
-
- old_cxt = MemoryContextSwitchTo(rsi->econtext->ecxt_per_query_memory);
-
- ret_tdesc = CreateTupleDescCopy(tupdesc);
- BlessTupleDesc(ret_tdesc);
- tuple_store =
- tuplestore_begin_heap(rsi->allowedModes & SFRM_Materialize_Random,
- false, work_mem);
-
- MemoryContextSwitchTo(old_cxt);
+ SetSingleFuncCall(fcinfo, SRF_SINGLE_BLESS);
tmp_cxt = AllocSetContextCreate(CurrentMemoryContext,
"jsonb_each temporary cxt",
@@ -1964,7 +1938,6 @@ each_worker_jsonb(FunctionCallInfo fcinfo, const char *funcname, bool as_text)
if (r == WJB_KEY)
{
text *key;
- HeapTuple tuple;
Datum values[2];
bool nulls[2] = {false, false};
@@ -2001,9 +1974,7 @@ each_worker_jsonb(FunctionCallInfo fcinfo, const char *funcname, bool as_text)
values[1] = PointerGetDatum(val);
}
- tuple = heap_form_tuple(ret_tdesc, values, nulls);
-
- tuplestore_puttuple(tuple_store, tuple);
+ tuplestore_putvalues(rsi->setResult, rsi->setDesc, values, nulls);
/* clean up and switch back */
MemoryContextSwitchTo(old_cxt);
@@ -2013,9 +1984,6 @@ each_worker_jsonb(FunctionCallInfo fcinfo, const char *funcname, bool as_text)
MemoryContextDelete(tmp_cxt);
- rsi->setResult = tuple_store;
- rsi->setDesc = ret_tdesc;
-
PG_RETURN_NULL();
}
@@ -2027,8 +1995,6 @@ each_worker(FunctionCallInfo fcinfo, bool as_text)
JsonLexContext *lex;
JsonSemAction *sem;
ReturnSetInfo *rsi;
- MemoryContext old_cxt;
- TupleDesc tupdesc;
EachState *state;
lex = makeJsonLexContext(json, true);
@@ -2037,30 +2003,9 @@ each_worker(FunctionCallInfo fcinfo, bool as_text)
rsi = (ReturnSetInfo *) fcinfo->resultinfo;
- if (!rsi || !IsA(rsi, ReturnSetInfo))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("set-valued function called in context that cannot accept a set")));
-
- if (!(rsi->allowedModes & SFRM_Materialize))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("materialize mode required, but it is not allowed in this context")));
-
- rsi->returnMode = SFRM_Materialize;
-
- (void) get_call_result_type(fcinfo, NULL, &tupdesc);
-
- /* make these in a sufficiently long-lived memory context */
- old_cxt = MemoryContextSwitchTo(rsi->econtext->ecxt_per_query_memory);
-
- state->ret_tdesc = CreateTupleDescCopy(tupdesc);
- BlessTupleDesc(state->ret_tdesc);
- state->tuple_store =
- tuplestore_begin_heap(rsi->allowedModes & SFRM_Materialize_Random,
- false, work_mem);
-
- MemoryContextSwitchTo(old_cxt);
+ SetSingleFuncCall(fcinfo, SRF_SINGLE_BLESS);
+ state->tuple_store = rsi->setResult;
+ state->ret_tdesc = rsi->setDesc;
sem->semstate = (void *) state;
sem->array_start = each_array_start;
@@ -2079,9 +2024,6 @@ each_worker(FunctionCallInfo fcinfo, bool as_text)
MemoryContextDelete(state->tmp_cxt);
- rsi->setResult = state->tuple_store;
- rsi->setDesc = state->ret_tdesc;
-
PG_RETURN_NULL();
}
@@ -2206,9 +2148,6 @@ elements_worker_jsonb(FunctionCallInfo fcinfo, const char *funcname,
{
Jsonb *jb = PG_GETARG_JSONB_P(0);
ReturnSetInfo *rsi;
- Tuplestorestate *tuple_store;
- TupleDesc tupdesc;
- TupleDesc ret_tdesc;
MemoryContext old_cxt,
tmp_cxt;
bool skipNested = false;
@@ -2227,31 +2166,8 @@ elements_worker_jsonb(FunctionCallInfo fcinfo, const char *funcname,
rsi = (ReturnSetInfo *) fcinfo->resultinfo;
- if (!rsi || !IsA(rsi, ReturnSetInfo))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("set-valued function called in context that cannot accept a set")));
-
- if (!(rsi->allowedModes & SFRM_Materialize) ||
- rsi->expectedDesc == NULL)
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("materialize mode required, but it is not allowed in this context")));
-
- rsi->returnMode = SFRM_Materialize;
-
- /* it's a simple type, so don't use get_call_result_type() */
- tupdesc = rsi->expectedDesc;
-
- old_cxt = MemoryContextSwitchTo(rsi->econtext->ecxt_per_query_memory);
-
- ret_tdesc = CreateTupleDescCopy(tupdesc);
- BlessTupleDesc(ret_tdesc);
- tuple_store =
- tuplestore_begin_heap(rsi->allowedModes & SFRM_Materialize_Random,
- false, work_mem);
-
- MemoryContextSwitchTo(old_cxt);
+ SetSingleFuncCall(fcinfo,
+ SRF_SINGLE_USE_EXPECTED | SRF_SINGLE_BLESS);
tmp_cxt = AllocSetContextCreate(CurrentMemoryContext,
"jsonb_array_elements temporary cxt",
@@ -2265,7 +2181,6 @@ elements_worker_jsonb(FunctionCallInfo fcinfo, const char *funcname,
if (r == WJB_ELEM)
{
- HeapTuple tuple;
Datum values[1];
bool nulls[1] = {false};
@@ -2291,9 +2206,7 @@ elements_worker_jsonb(FunctionCallInfo fcinfo, const char *funcname,
values[0] = PointerGetDatum(val);
}
- tuple = heap_form_tuple(ret_tdesc, values, nulls);
-
- tuplestore_puttuple(tuple_store, tuple);
+ tuplestore_putvalues(rsi->setResult, rsi->setDesc, values, nulls);
/* clean up and switch back */
MemoryContextSwitchTo(old_cxt);
@@ -2303,9 +2216,6 @@ elements_worker_jsonb(FunctionCallInfo fcinfo, const char *funcname,
MemoryContextDelete(tmp_cxt);
- rsi->setResult = tuple_store;
- rsi->setDesc = ret_tdesc;
-
PG_RETURN_NULL();
}
@@ -2330,41 +2240,15 @@ elements_worker(FunctionCallInfo fcinfo, const char *funcname, bool as_text)
JsonLexContext *lex = makeJsonLexContext(json, as_text);
JsonSemAction *sem;
ReturnSetInfo *rsi;
- MemoryContext old_cxt;
- TupleDesc tupdesc;
ElementsState *state;
state = palloc0(sizeof(ElementsState));
sem = palloc0(sizeof(JsonSemAction));
+ SetSingleFuncCall(fcinfo, SRF_SINGLE_USE_EXPECTED | SRF_SINGLE_BLESS);
rsi = (ReturnSetInfo *) fcinfo->resultinfo;
-
- if (!rsi || !IsA(rsi, ReturnSetInfo))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("set-valued function called in context that cannot accept a set")));
-
- if (!(rsi->allowedModes & SFRM_Materialize) ||
- rsi->expectedDesc == NULL)
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("materialize mode required, but it is not allowed in this context")));
-
- rsi->returnMode = SFRM_Materialize;
-
- /* it's a simple type, so don't use get_call_result_type() */
- tupdesc = rsi->expectedDesc;
-
- /* make these in a sufficiently long-lived memory context */
- old_cxt = MemoryContextSwitchTo(rsi->econtext->ecxt_per_query_memory);
-
- state->ret_tdesc = CreateTupleDescCopy(tupdesc);
- BlessTupleDesc(state->ret_tdesc);
- state->tuple_store =
- tuplestore_begin_heap(rsi->allowedModes & SFRM_Materialize_Random,
- false, work_mem);
-
- MemoryContextSwitchTo(old_cxt);
+ state->tuple_store = rsi->setResult;
+ state->ret_tdesc = rsi->setDesc;
sem->semstate = (void *) state;
sem->object_start = elements_object_start;
@@ -2384,9 +2268,6 @@ elements_worker(FunctionCallInfo fcinfo, const char *funcname, bool as_text)
MemoryContextDelete(state->tmp_cxt);
- rsi->setResult = state->tuple_store;
- rsi->setDesc = state->ret_tdesc;
-
PG_RETURN_NULL();
}