我的问题类似于此链接。我有一个包含数百万行的表,我必须将其放入单个数组中对其进行排序并获取最小值和最大值并将结果作为文本返回。
例如。表名:Sample,列名:id
列 id 保存数百万条记录,数据类型可能是整数、浮点数等,
我必须将结果返回为 min-max(最小值分隔符最大值,如 1->5)
任何帮助表示赞赏。先感谢您。
#ifdef PG_MODULE_MAGIC
PG_MODULE_MAGIC;
#endif
/* Add a prototype marked PGDLLEXPORT */
PGDLLEXPORT Datum make_array(PG_FUNCTION_ARGS);
PG_FUNCTION_INFO_V1(make_array);
Datum
make_array(PG_FUNCTION_ARGS)
{
ArrayType* result;
ArrayType* result1;
Oid element_type = get_fn_expr_argtype(fcinfo->flinfo, 0);
Datum element;
bool isnull;
int16 typlen;
bool typbyval;
char typalign;
int ndims;
int dims[MAXDIM];
int lbs[MAXDIM];
if (!OidIsValid(element_type))
elog(ERROR, "could not determine data type of input");
/* get the provided element, being careful in case it's NULL */
isnull = PG_ARGISNULL(0);
if (isnull)
element = (Datum)0;
else
element = PG_GETARG_DATUM(0);
/* we have one dimension */
ndims = 1;
/* and one element */
dims[0] = 1;
/* and lower bound is 1 */
lbs[0] = 1;
/* get required info about the element type */
get_typlenbyvalalign(element_type, &typlen, &typbyval, &typalign);
/* now build the array */
result = construct_md_array(&element, &isnull, ndims, dims, lbs,
element_type, typlen, typbyval, typalign);
PG_RETURN_ARRAYTYPE_P(result);
}
这段代码编译后的结果如下
我是创建 Postgres C 扩展的新手。
