diff options
author | Bruce Momjian <bruce@momjian.us> | 1999-06-12 14:05:41 +0000 |
---|---|---|
committer | Bruce Momjian <bruce@momjian.us> | 1999-06-12 14:05:41 +0000 |
commit | 603e153bb8acc27773807a7eacf4e7c3cf8a5a17 (patch) | |
tree | 4d1754201cddb914b0522968e2925aaf9e9baf49 /src/backend/executor/nodeAgg.c | |
parent | bd470ba47e6a99c4667ac8e5e2435d0d33868e2f (diff) | |
download | postgresql-603e153bb8acc27773807a7eacf4e7c3cf8a5a17.tar.gz postgresql-603e153bb8acc27773807a7eacf4e7c3cf8a5a17.zip |
I don't like last minute patches before the final freeze, but I believe that
this one could be useful for people experiencing out-of-memory crashes while
executing queries which retrieve or use a very large number of tuples.
The problem happens when storage is allocated for functions results used in
a large query, for example:
select upper(name) from big_table;
select big_table.array[1] from big_table;
select count(upper(name)) from big_table;
This patch is a dirty hack that fixes the out-of-memory problem for the most
common cases, like the above ones. It is not the final solution for the
problem but it can work for some people, so I'm posting it.
The patch should be safe because all changes are under #ifdef. Furthermore
the feature can be enabled or disabled at runtime by the `free_tuple_memory'
options in the pg_options file. The option is disabled by default and must
be explicitly enabled at runtime to have any effect.
To enable the patch add the follwing line to Makefile.custom:
CUSTOM_COPT += -DFREE_TUPLE_MEMORY
To enable the option at runtime add the following line to pg_option:
free_tuple_memory=1
Massimo
Diffstat (limited to 'src/backend/executor/nodeAgg.c')
-rw-r--r-- | src/backend/executor/nodeAgg.c | 48 |
1 files changed, 47 insertions, 1 deletions
diff --git a/src/backend/executor/nodeAgg.c b/src/backend/executor/nodeAgg.c index a47a9ad5492..9d0f4ea1036 100644 --- a/src/backend/executor/nodeAgg.c +++ b/src/backend/executor/nodeAgg.c @@ -31,6 +31,11 @@ #include "utils/syscache.h" #include "optimizer/clauses.h" +#ifdef FREE_TUPLE_MEMORY +#include <utils/portal.h> +#include <utils/trace.h> +#endif + /* * AggFuncInfo - * keeps the transition functions information around @@ -113,7 +118,9 @@ ExecAgg(Agg *node) isNull1 = FALSE, isNull2 = FALSE; bool qual_result; - +#ifdef FREE_TUPLE_MEMORY + bool free_tuple_memory = pg_options[OPT_FREE_TUPLE_MEMORY]; +#endif /* --------------------- * get state info from node @@ -241,6 +248,10 @@ ExecAgg(Agg *node) for (;;) { TupleTableSlot *outerslot; +#ifdef FREE_TUPLE_MEMORY + Oid valueType; + bool isByValue = 0; +#endif isNull = isNull1 = isNull2 = 0; outerslot = ExecProcNode(outerPlan, (Plan *) node); @@ -293,6 +304,31 @@ ExecAgg(Agg *node) newVal = ExecEvalExpr(aggref->target, econtext, &isNull, &isDone); } +#ifdef FREE_TUPLE_MEMORY + if (free_tuple_memory) { + switch (nodeTag(aggref->target)) { + case T_Const: + isByValue = ((Const*) (aggref->target))->constbyval; + break; + case T_Var: + valueType = ((Var*) (aggref->target))->vartype; + isByValue = typeByVal(typeidType(valueType)); + break; + case T_Array: + isByValue = ((Array*)(aggref->target))->arrayelembyval; + break; + case T_ArrayRef: + isByValue =((ArrayRef*)(aggref->target))->refelembyval; + break; + case T_Expr: + valueType = ((Expr*) (aggref->target))->typeOid; + isByValue = typeByVal(typeidType(valueType)); + break; + default: + break; + } + } +#endif if (isNull && !aggref->usenulls) continue; /* ignore this tuple for this agg */ @@ -353,6 +389,16 @@ ExecAgg(Agg *node) (FmgrValues *) args, &isNull2); Assert(!isNull2); } + +#ifdef FREE_TUPLE_MEMORY + /* try to pfree newVal if not isByValue - dz */ + if (free_tuple_memory && !isByValue && + PortalHeapMemoryIsValid(CurrentMemoryContext, + (Pointer) newVal)) + { + pfree(newVal); + } +#endif } /* |