From 53c950916355881f05b3b5096ac07f2029f469c7 Mon Sep 17 00:00:00 2001 From: Yugo Nagata Date: Tue, 21 Jun 2022 23:08:54 +0900 Subject: [PATCH] Fix check for aggregate functions to work with PostgreSQL 13 In PostgreSQL 14 or later, OIDs of aggregate functions are described in fmrgoids.h, but that in PostgreSQL 13 doesn't contain aggregate function OIDs. Therefore, we get the OID by passing the function name and arg type to to_regprocedure(). --- createas.c | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ pg_ivm.h | 4 ---- 2 files changed, 50 insertions(+), 4 deletions(-) diff --git a/createas.c b/createas.c index 2d20c00..4075a43 100644 --- a/createas.c +++ b/createas.c @@ -387,7 +387,11 @@ makeIvmAggColumn(ParseState *pstate, Aggref *aggref, char *resname, AttrNumber * */ if (strcmp(aggname, "count") != 0) { +#if defined(PG_VERSION_NUM) && (PG_VERSION_NUM >= 140000) fn = makeFuncCall(list_make1(makeString("count")), NIL, COERCE_EXPLICIT_CALL, -1); +#else + fn = makeFuncCall(list_make1(makeString("count")), NIL, -1); +#endif /* Make a Func with a dummy arg, and then override this by the original agg's args. */ node = ParseFuncOrColumn(pstate, fn->funcname, list_make1(dmy_arg), NULL, fn, false, -1); @@ -419,7 +423,11 @@ makeIvmAggColumn(ParseState *pstate, Aggref *aggref, char *resname, AttrNumber * dmy_args = lappend(dmy_args, con); ReleaseSysCache(type); } +#if defined(PG_VERSION_NUM) && (PG_VERSION_NUM >= 140000) fn = makeFuncCall(list_make1(makeString("sum")), NIL, COERCE_EXPLICIT_CALL, -1); +#else + fn = makeFuncCall(list_make1(makeString("sum")), NIL, -1); +#endif /* Make a Func with dummy args, and then override this by the original agg's args. */ node = ParseFuncOrColumn(pstate, fn->funcname, dmy_args, NULL, fn, false, -1); @@ -847,6 +855,7 @@ check_ivm_restriction_walker(Node *node, check_ivm_restriction_context *context) static bool check_aggregate_supports_ivm(Oid aggfnoid) { +#if defined(PG_VERSION_NUM) && (PG_VERSION_NUM >= 140000) switch (aggfnoid) { /* count */ @@ -877,6 +886,47 @@ check_aggregate_supports_ivm(Oid aggfnoid) default: return false; } + +#else + char *funcs[] = { + /* count */ + "count(\"any\")", + "count()", + + /* sum */ + "sum(int8)", + "sum(int4)", + "sum(int2)", + "sum(float4)", + "sum(float8)", + "sum(money)", + "sum(interval)", + "sum(numeric)", + + /* avg */ + "avg(int8)", + "avg(int4)", + "avg(int2)", + "avg(numeric)", + "avg(float4)", + "avg(float8)", + "avg(interval)", + + NULL + }; + + char **fname = funcs; + + while (*fname != NULL) + { + if (DatumGetObjectId(DirectFunctionCall1(to_regprocedure, CStringGetTextDatum(*fname))) == aggfnoid) + return true; + fname++; + } + + return false; + +#endif } /* diff --git a/pg_ivm.h b/pg_ivm.h index 53faac9..4b71f24 100644 --- a/pg_ivm.h +++ b/pg_ivm.h @@ -50,8 +50,4 @@ extern Datum IVM_immediate_maintenance(PG_FUNCTION_ARGS); extern void AtAbort_IVM(void); extern bool isIvmName(const char *s); -extern Datum pg_get_viewdef(PG_FUNCTION_ARGS); - - - #endif