summaryrefslogtreecommitdiff
path: root/src/backend/utils/adt
diff options
context:
space:
mode:
authorTom Lane2000-07-12 02:37:39 +0000
committerTom Lane2000-07-12 02:37:39 +0000
commitbadce86a2c327b40c6146242526d1523455d64a6 (patch)
tree6e0cb658889a2688e76d9ac19a56555c5eb0e738 /src/backend/utils/adt
parent46fb9c29e2990ba470bb741ff6dd60f2ae218e64 (diff)
First stage of reclaiming memory in executor by resetting short-term
memory contexts. Currently, only leaks in expressions executed as quals or projections are handled. Clean up some old dead cruft in executor while at it --- unused fields in state nodes, that sort of thing.
Diffstat (limited to 'src/backend/utils/adt')
-rw-r--r--src/backend/utils/adt/datum.c135
-rw-r--r--src/backend/utils/adt/varlena.c108
2 files changed, 124 insertions, 119 deletions
diff --git a/src/backend/utils/adt/datum.c b/src/backend/utils/adt/datum.c
index a86ec87067b..7f590e06e46 100644
--- a/src/backend/utils/adt/datum.c
+++ b/src/backend/utils/adt/datum.c
@@ -1,13 +1,14 @@
/*-------------------------------------------------------------------------
*
* datum.c
+ * POSTGRES Datum (abstract data type) manipulation routines.
*
* Portions Copyright (c) 1996-2000, PostgreSQL, Inc
* Portions Copyright (c) 1994, Regents of the University of California
*
*
* IDENTIFICATION
- * $Header: /cvsroot/pgsql/src/backend/utils/adt/datum.c,v 1.17 2000/01/26 05:57:13 momjian Exp $
+ * $Header: /cvsroot/pgsql/src/backend/utils/adt/datum.c,v 1.18 2000/07/12 02:37:19 tgl Exp $
*
*-------------------------------------------------------------------------
*/
@@ -16,7 +17,7 @@
*
* A) if a type is "byVal" then all the information is stored in the
* Datum itself (i.e. no pointers involved!). In this case the
- * length of the type is always greater than zero and less than
+ * length of the type is always greater than zero and not more than
* "sizeof(Datum)"
* B) if a type is not "byVal" and it has a fixed length, then
* the "Datum" always contain a pointer to a stream of bytes.
@@ -27,15 +28,19 @@
* This varlena structure has information about the actual length of this
* particular instance of the type and about its value.
*
+ * Note that we do not treat "toasted" datums specially; therefore what
+ * will be copied or compared is the compressed data or toast reference.
*/
+
#include "postgres.h"
+
#include "utils/datum.h"
/*-------------------------------------------------------------------------
* datumGetSize
*
* Find the "real" size of a datum, given the datum value,
- * its type, whether it is a "by value", and its length.
+ * whether it is a "by value", and its length.
*
* To cut a long story short, usually the real size is equal to the
* type length, with the exception of variable length types which have
@@ -45,47 +50,31 @@
*-------------------------------------------------------------------------
*/
Size
-datumGetSize(Datum value, Oid type, bool byVal, Size len)
+datumGetSize(Datum value, bool typByVal, int typLen)
{
+ Size size;
- struct varlena *s;
- Size size = 0;
-
- if (byVal)
+ if (typByVal)
{
- if (len <= sizeof(Datum))
- size = len;
- else
- {
- elog(ERROR,
- "datumGetSize: Error: type=%ld, byVaL with len=%d",
- (long) type, len);
- }
+ /* Pass-by-value types are always fixed-length */
+ Assert(typLen > 0 && typLen <= sizeof(Datum));
+ size = (Size) typLen;
}
else
- { /* not byValue */
- if (len == -1)
+ {
+ if (typLen == -1)
{
+ /* Assume it is a varlena datatype */
+ struct varlena *s = (struct varlena *) DatumGetPointer(value);
- /*
- * variable length type Look at the varlena struct for its
- * real length...
- */
- s = (struct varlena *) DatumGetPointer(value);
if (!PointerIsValid(s))
- {
- elog(ERROR,
- "datumGetSize: Invalid Datum Pointer");
- }
+ elog(ERROR, "datumGetSize: Invalid Datum Pointer");
size = (Size) VARSIZE(s);
}
else
{
-
- /*
- * fixed length type
- */
- size = len;
+ /* Fixed-length pass-by-ref type */
+ size = (Size) typLen;
}
}
@@ -97,37 +86,29 @@ datumGetSize(Datum value, Oid type, bool byVal, Size len)
*
* make a copy of a datum
*
- * If the type of the datum is not passed by value (i.e. "byVal=false")
- * then we assume that the datum contains a pointer and we copy all the
- * bytes pointed by this pointer
+ * If the datatype is pass-by-reference, memory is obtained with palloc().
*-------------------------------------------------------------------------
*/
Datum
-datumCopy(Datum value, Oid type, bool byVal, Size len)
+datumCopy(Datum value, bool typByVal, int typLen)
{
-
- Size realSize;
Datum res;
- char *s;
-
- if (byVal)
+ if (typByVal)
res = value;
else
{
- if (value == 0)
- return (Datum) NULL;
- realSize = datumGetSize(value, type, byVal, len);
+ Size realSize;
+ char *s;
+
+ if (DatumGetPointer(value) == NULL)
+ return PointerGetDatum(NULL);
+
+ realSize = datumGetSize(value, typByVal, typLen);
- /*
- * the value is a pointer. Allocate enough space and copy the
- * pointed data.
- */
s = (char *) palloc(realSize);
- if (s == NULL)
- elog(ERROR, "datumCopy: out of memory\n");
- memmove(s, DatumGetPointer(value), realSize);
- res = (Datum) s;
+ memcpy(s, DatumGetPointer(value), realSize);
+ res = PointerGetDatum(s);
}
return res;
}
@@ -143,21 +124,12 @@ datumCopy(Datum value, Oid type, bool byVal, Size len)
*/
#ifdef NOT_USED
void
-datumFree(Datum value, Oid type, bool byVal, Size len)
+datumFree(Datum value, bool typByVal, int typLen)
{
-
- Size realSize;
- Pointer s;
-
- realSize = datumGetSize(value, type, byVal, len);
-
- if (!byVal)
+ if (!typByVal)
{
+ Pointer s = DatumGetPointer(value);
- /*
- * free the space palloced by "datumCopy()"
- */
- s = DatumGetPointer(value);
pfree(s);
}
}
@@ -174,46 +146,41 @@ datumFree(Datum value, Oid type, bool byVal, Size len)
* This routine will return false if there are 2 different
* representations of the same value (something along the lines
* of say the representation of zero in one's complement arithmetic).
- *
+ * Also, it will probably not give the answer you want if either
+ * datum has been "toasted".
*-------------------------------------------------------------------------
*/
bool
-datumIsEqual(Datum value1, Datum value2, Oid type, bool byVal, Size len)
+datumIsEqual(Datum value1, Datum value2, bool typByVal, int typLen)
{
- Size size1,
- size2;
- char *s1,
- *s2;
+ bool res;
- if (byVal)
+ if (typByVal)
{
-
/*
* just compare the two datums. NOTE: just comparing "len" bytes
* will not do the work, because we do not know how these bytes
* are aligned inside the "Datum".
*/
- if (value1 == value2)
- return true;
- else
- return false;
+ res = (value1 == value2);
}
else
{
+ Size size1,
+ size2;
+ char *s1,
+ *s2;
/*
- * byVal = false Compare the bytes pointed by the pointers stored
- * in the datums.
+ * Compare the bytes pointed by the pointers stored in the datums.
*/
- size1 = datumGetSize(value1, type, byVal, len);
- size2 = datumGetSize(value2, type, byVal, len);
+ size1 = datumGetSize(value1, typByVal, typLen);
+ size2 = datumGetSize(value2, typByVal, typLen);
if (size1 != size2)
return false;
s1 = (char *) DatumGetPointer(value1);
s2 = (char *) DatumGetPointer(value2);
- if (!memcmp(s1, s2, size1))
- return true;
- else
- return false;
+ res = (memcmp(s1, s2, size1) == 0);
}
+ return res;
}
diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c
index 8247e16812d..b5380a8c52a 100644
--- a/src/backend/utils/adt/varlena.c
+++ b/src/backend/utils/adt/varlena.c
@@ -8,7 +8,7 @@
*
*
* IDENTIFICATION
- * $Header: /cvsroot/pgsql/src/backend/utils/adt/varlena.c,v 1.63 2000/07/06 05:48:11 tgl Exp $
+ * $Header: /cvsroot/pgsql/src/backend/utils/adt/varlena.c,v 1.64 2000/07/12 02:37:19 tgl Exp $
*
*-------------------------------------------------------------------------
*/
@@ -436,25 +436,38 @@ textpos(PG_FUNCTION_ARGS)
/*
* texteq - returns true iff arguments are equal
* textne - returns true iff arguments are not equal
+ *
+ * Note: btree indexes need these routines not to leak memory; therefore,
+ * be careful to free working copies of toasted datums. Most places don't
+ * need to be so careful.
*/
Datum
texteq(PG_FUNCTION_ARGS)
{
text *arg1 = PG_GETARG_TEXT_P(0);
text *arg2 = PG_GETARG_TEXT_P(1);
- int len;
- char *a1p,
- *a2p;
+ bool result;
if (VARSIZE(arg1) != VARSIZE(arg2))
- PG_RETURN_BOOL(false);
+ result = false;
+ else
+ {
+ int len;
+ char *a1p,
+ *a2p;
- len = VARSIZE(arg1) - VARHDRSZ;
+ len = VARSIZE(arg1) - VARHDRSZ;
- a1p = VARDATA(arg1);
- a2p = VARDATA(arg2);
+ a1p = VARDATA(arg1);
+ a2p = VARDATA(arg2);
- PG_RETURN_BOOL(memcmp(a1p, a2p, len) == 0);
+ result = (memcmp(a1p, a2p, len) == 0);
+ }
+
+ PG_FREE_IF_COPY(arg1, 0);
+ PG_FREE_IF_COPY(arg2, 1);
+
+ PG_RETURN_BOOL(result);
}
Datum
@@ -462,19 +475,28 @@ textne(PG_FUNCTION_ARGS)
{
text *arg1 = PG_GETARG_TEXT_P(0);
text *arg2 = PG_GETARG_TEXT_P(1);
- int len;
- char *a1p,
- *a2p;
+ bool result;
if (VARSIZE(arg1) != VARSIZE(arg2))
- PG_RETURN_BOOL(true);
+ result = true;
+ else
+ {
+ int len;
+ char *a1p,
+ *a2p;
- len = VARSIZE(arg1) - VARHDRSZ;
+ len = VARSIZE(arg1) - VARHDRSZ;
- a1p = VARDATA(arg1);
- a2p = VARDATA(arg2);
+ a1p = VARDATA(arg1);
+ a2p = VARDATA(arg2);
+
+ result = (memcmp(a1p, a2p, len) != 0);
+ }
- PG_RETURN_BOOL(memcmp(a1p, a2p, len) != 0);
+ PG_FREE_IF_COPY(arg1, 0);
+ PG_FREE_IF_COPY(arg2, 1);
+
+ PG_RETURN_BOOL(result);
}
/* varstr_cmp()
@@ -545,6 +567,10 @@ text_cmp(text *arg1, text *arg2)
/*
* Comparison functions for text strings.
+ *
+ * Note: btree indexes need these routines not to leak memory; therefore,
+ * be careful to free working copies of toasted datums. Most places don't
+ * need to be so careful.
*/
Datum
@@ -552,8 +578,14 @@ text_lt(PG_FUNCTION_ARGS)
{
text *arg1 = PG_GETARG_TEXT_P(0);
text *arg2 = PG_GETARG_TEXT_P(1);
+ bool result;
- PG_RETURN_BOOL(text_cmp(arg1, arg2) < 0);
+ result = (text_cmp(arg1, arg2) < 0);
+
+ PG_FREE_IF_COPY(arg1, 0);
+ PG_FREE_IF_COPY(arg2, 1);
+
+ PG_RETURN_BOOL(result);
}
Datum
@@ -561,8 +593,14 @@ text_le(PG_FUNCTION_ARGS)
{
text *arg1 = PG_GETARG_TEXT_P(0);
text *arg2 = PG_GETARG_TEXT_P(1);
+ bool result;
+
+ result = (text_cmp(arg1, arg2) <= 0);
+
+ PG_FREE_IF_COPY(arg1, 0);
+ PG_FREE_IF_COPY(arg2, 1);
- PG_RETURN_BOOL(text_cmp(arg1, arg2) <= 0);
+ PG_RETURN_BOOL(result);
}
Datum
@@ -570,8 +608,14 @@ text_gt(PG_FUNCTION_ARGS)
{
text *arg1 = PG_GETARG_TEXT_P(0);
text *arg2 = PG_GETARG_TEXT_P(1);
+ bool result;
- PG_RETURN_BOOL(text_cmp(arg1, arg2) > 0);
+ result = (text_cmp(arg1, arg2) > 0);
+
+ PG_FREE_IF_COPY(arg1, 0);
+ PG_FREE_IF_COPY(arg2, 1);
+
+ PG_RETURN_BOOL(result);
}
Datum
@@ -579,8 +623,14 @@ text_ge(PG_FUNCTION_ARGS)
{
text *arg1 = PG_GETARG_TEXT_P(0);
text *arg2 = PG_GETARG_TEXT_P(1);
+ bool result;
+
+ result = (text_cmp(arg1, arg2) >= 0);
+
+ PG_FREE_IF_COPY(arg1, 0);
+ PG_FREE_IF_COPY(arg2, 1);
- PG_RETURN_BOOL(text_cmp(arg1, arg2) >= 0);
+ PG_RETURN_BOOL(result);
}
Datum
@@ -589,14 +639,8 @@ text_larger(PG_FUNCTION_ARGS)
text *arg1 = PG_GETARG_TEXT_P(0);
text *arg2 = PG_GETARG_TEXT_P(1);
text *result;
- text *temp;
- temp = ((text_cmp(arg1, arg2) > 0) ? arg1 : arg2);
-
- /* Make a copy --- temporary hack until nodeAgg.c is smarter */
-
- result = (text *) palloc(VARSIZE(temp));
- memcpy((char *) result, (char *) temp, VARSIZE(temp));
+ result = ((text_cmp(arg1, arg2) > 0) ? arg1 : arg2);
PG_RETURN_TEXT_P(result);
}
@@ -607,14 +651,8 @@ text_smaller(PG_FUNCTION_ARGS)
text *arg1 = PG_GETARG_TEXT_P(0);
text *arg2 = PG_GETARG_TEXT_P(1);
text *result;
- text *temp;
-
- temp = ((text_cmp(arg1, arg2) < 0) ? arg1 : arg2);
-
- /* Make a copy --- temporary hack until nodeAgg.c is smarter */
- result = (text *) palloc(VARSIZE(temp));
- memcpy((char *) result, (char *) temp, VARSIZE(temp));
+ result = ((text_cmp(arg1, arg2) < 0) ? arg1 : arg2);
PG_RETURN_TEXT_P(result);
}