mirror of
https://github.com/postgres/postgres.git
synced 2025-11-29 23:43:17 +03:00
Allow float8, int8, and related datatypes to be passed by value on machines
where Datum is 8 bytes wide. Since this will break old-style C functions (those still using version 0 calling convention) that have arguments or results of these types, provide a configure option to disable it and retain the old pass-by-reference behavior. Likewise, provide a configure option to disable the recently-committed float4 pass-by-value change. Zoltan Boszormenyi, plus configurability stuff by me.
This commit is contained in:
@@ -13,7 +13,7 @@
|
||||
* this version handles 64 bit numbers and so can hold values up to
|
||||
* $92,233,720,368,547,758.07.
|
||||
*
|
||||
* $PostgreSQL: pgsql/src/backend/utils/adt/cash.c,v 1.78 2008/03/25 22:42:43 tgl Exp $
|
||||
* $PostgreSQL: pgsql/src/backend/utils/adt/cash.c,v 1.79 2008/04/21 00:26:45 tgl Exp $
|
||||
*/
|
||||
|
||||
#include "postgres.h"
|
||||
@@ -34,14 +34,6 @@
|
||||
#define LAST_PAREN (TERMINATOR - 1)
|
||||
#define LAST_DIGIT (LAST_PAREN - 1)
|
||||
|
||||
/*
|
||||
* Cash is a pass-by-ref SQL type, so we must pass and return pointers.
|
||||
* These macros and support routine hide the pass-by-refness.
|
||||
*/
|
||||
#define PG_GETARG_CASH(n) (* ((Cash *) PG_GETARG_POINTER(n)))
|
||||
#define PG_RETURN_CASH(x) return CashGetDatum(x)
|
||||
|
||||
|
||||
|
||||
/*************************************************************************
|
||||
* Private routines
|
||||
@@ -99,15 +91,6 @@ num_word(Cash value)
|
||||
return buf;
|
||||
} /* num_word() */
|
||||
|
||||
static Datum
|
||||
CashGetDatum(Cash value)
|
||||
{
|
||||
Cash *result = (Cash *) palloc(sizeof(Cash));
|
||||
|
||||
*result = value;
|
||||
return PointerGetDatum(result);
|
||||
}
|
||||
|
||||
|
||||
/* cash_in()
|
||||
* Convert a string to a cash data type.
|
||||
|
||||
@@ -8,7 +8,7 @@
|
||||
*
|
||||
*
|
||||
* IDENTIFICATION
|
||||
* $PostgreSQL: pgsql/src/backend/utils/adt/float.c,v 1.154 2008/03/10 12:39:22 tgl Exp $
|
||||
* $PostgreSQL: pgsql/src/backend/utils/adt/float.c,v 1.155 2008/04/21 00:26:45 tgl Exp $
|
||||
*
|
||||
*-------------------------------------------------------------------------
|
||||
*/
|
||||
@@ -1780,7 +1780,7 @@ float8_accum(PG_FUNCTION_ARGS)
|
||||
|
||||
result = construct_array(transdatums, 3,
|
||||
FLOAT8OID,
|
||||
sizeof(float8), false /* float8 byval */ , 'd');
|
||||
sizeof(float8), FLOAT8PASSBYVAL, 'd');
|
||||
|
||||
PG_RETURN_ARRAYTYPE_P(result);
|
||||
}
|
||||
@@ -1833,7 +1833,7 @@ float4_accum(PG_FUNCTION_ARGS)
|
||||
|
||||
result = construct_array(transdatums, 3,
|
||||
FLOAT8OID,
|
||||
sizeof(float8), false /* float8 byval */ , 'd');
|
||||
sizeof(float8), FLOAT8PASSBYVAL, 'd');
|
||||
|
||||
PG_RETURN_ARRAYTYPE_P(result);
|
||||
}
|
||||
@@ -2056,8 +2056,7 @@ float8_regr_accum(PG_FUNCTION_ARGS)
|
||||
|
||||
result = construct_array(transdatums, 6,
|
||||
FLOAT8OID,
|
||||
sizeof(float8),
|
||||
false /* float8 byval */ , 'd');
|
||||
sizeof(float8), FLOAT8PASSBYVAL, 'd');
|
||||
|
||||
PG_RETURN_ARRAYTYPE_P(result);
|
||||
}
|
||||
|
||||
@@ -7,7 +7,7 @@
|
||||
* Portions Copyright (c) 1994, Regents of the University of California
|
||||
*
|
||||
* IDENTIFICATION
|
||||
* $PostgreSQL: pgsql/src/backend/utils/adt/int8.c,v 1.68 2008/01/01 19:45:52 momjian Exp $
|
||||
* $PostgreSQL: pgsql/src/backend/utils/adt/int8.c,v 1.69 2008/04/21 00:26:45 tgl Exp $
|
||||
*
|
||||
*-------------------------------------------------------------------------
|
||||
*/
|
||||
@@ -657,17 +657,16 @@ int8mod(PG_FUNCTION_ARGS)
|
||||
Datum
|
||||
int8inc(PG_FUNCTION_ARGS)
|
||||
{
|
||||
/*
|
||||
* When int8 is pass-by-reference, we provide this special case to avoid
|
||||
* palloc overhead for COUNT(): when called from nodeAgg, we know that the
|
||||
* argument is modifiable local storage, so just update it in-place.
|
||||
* (If int8 is pass-by-value, then of course this is useless as well
|
||||
* as incorrect, so just ifdef it out.)
|
||||
*/
|
||||
#ifndef USE_FLOAT8_BYVAL /* controls int8 too */
|
||||
if (fcinfo->context && IsA(fcinfo->context, AggState))
|
||||
{
|
||||
/*
|
||||
* Special case to avoid palloc overhead for COUNT(): when called from
|
||||
* nodeAgg, we know that the argument is modifiable local storage, so
|
||||
* just update it in-place.
|
||||
*
|
||||
* Note: this assumes int8 is a pass-by-ref type; if we ever support
|
||||
* pass-by-val int8, this should be ifdef'd out when int8 is
|
||||
* pass-by-val.
|
||||
*/
|
||||
int64 *arg = (int64 *) PG_GETARG_POINTER(0);
|
||||
int64 result;
|
||||
|
||||
@@ -682,6 +681,7 @@ int8inc(PG_FUNCTION_ARGS)
|
||||
PG_RETURN_POINTER(arg);
|
||||
}
|
||||
else
|
||||
#endif
|
||||
{
|
||||
/* Not called by nodeAgg, so just do it the dumb way */
|
||||
int64 arg = PG_GETARG_INT64(0);
|
||||
|
||||
@@ -14,7 +14,7 @@
|
||||
* Copyright (c) 1998-2008, PostgreSQL Global Development Group
|
||||
*
|
||||
* IDENTIFICATION
|
||||
* $PostgreSQL: pgsql/src/backend/utils/adt/numeric.c,v 1.109 2008/04/04 18:45:36 tgl Exp $
|
||||
* $PostgreSQL: pgsql/src/backend/utils/adt/numeric.c,v 1.110 2008/04/21 00:26:45 tgl Exp $
|
||||
*
|
||||
*-------------------------------------------------------------------------
|
||||
*/
|
||||
@@ -2599,10 +2599,13 @@ int2_sum(PG_FUNCTION_ARGS)
|
||||
}
|
||||
|
||||
/*
|
||||
* If we're invoked by nodeAgg, we can cheat and modify out first
|
||||
* If we're invoked by nodeAgg, we can cheat and modify our first
|
||||
* parameter in-place to avoid palloc overhead. If not, we need to return
|
||||
* the new value of the transition variable.
|
||||
* (If int8 is pass-by-value, then of course this is useless as well
|
||||
* as incorrect, so just ifdef it out.)
|
||||
*/
|
||||
#ifndef USE_FLOAT8_BYVAL /* controls int8 too */
|
||||
if (fcinfo->context && IsA(fcinfo->context, AggState))
|
||||
{
|
||||
int64 *oldsum = (int64 *) PG_GETARG_POINTER(0);
|
||||
@@ -2614,6 +2617,7 @@ int2_sum(PG_FUNCTION_ARGS)
|
||||
PG_RETURN_POINTER(oldsum);
|
||||
}
|
||||
else
|
||||
#endif
|
||||
{
|
||||
int64 oldsum = PG_GETARG_INT64(0);
|
||||
|
||||
@@ -2644,10 +2648,13 @@ int4_sum(PG_FUNCTION_ARGS)
|
||||
}
|
||||
|
||||
/*
|
||||
* If we're invoked by nodeAgg, we can cheat and modify out first
|
||||
* If we're invoked by nodeAgg, we can cheat and modify our first
|
||||
* parameter in-place to avoid palloc overhead. If not, we need to return
|
||||
* the new value of the transition variable.
|
||||
* (If int8 is pass-by-value, then of course this is useless as well
|
||||
* as incorrect, so just ifdef it out.)
|
||||
*/
|
||||
#ifndef USE_FLOAT8_BYVAL /* controls int8 too */
|
||||
if (fcinfo->context && IsA(fcinfo->context, AggState))
|
||||
{
|
||||
int64 *oldsum = (int64 *) PG_GETARG_POINTER(0);
|
||||
@@ -2659,6 +2666,7 @@ int4_sum(PG_FUNCTION_ARGS)
|
||||
PG_RETURN_POINTER(oldsum);
|
||||
}
|
||||
else
|
||||
#endif
|
||||
{
|
||||
int64 oldsum = PG_GETARG_INT64(0);
|
||||
|
||||
|
||||
@@ -7,7 +7,7 @@
|
||||
*
|
||||
*
|
||||
* IDENTIFICATION
|
||||
* $PostgreSQL: pgsql/src/backend/utils/adt/tsquery_gist.c,v 1.6 2008/04/20 09:17:57 teodor Exp $
|
||||
* $PostgreSQL: pgsql/src/backend/utils/adt/tsquery_gist.c,v 1.7 2008/04/21 00:26:45 tgl Exp $
|
||||
*
|
||||
*-------------------------------------------------------------------------
|
||||
*/
|
||||
@@ -19,7 +19,8 @@
|
||||
#include "tsearch/ts_type.h"
|
||||
#include "tsearch/ts_utils.h"
|
||||
|
||||
#define GETENTRY(vec,pos) ((TSQuerySign *) DatumGetPointer((vec)->vector[(pos)].key))
|
||||
#define GETENTRY(vec,pos) DatumGetTSQuerySign((vec)->vector[pos].key)
|
||||
|
||||
|
||||
Datum
|
||||
gtsquery_compress(PG_FUNCTION_ARGS)
|
||||
@@ -29,12 +30,12 @@ gtsquery_compress(PG_FUNCTION_ARGS)
|
||||
|
||||
if (entry->leafkey)
|
||||
{
|
||||
TSQuerySign *sign = (TSQuerySign *) palloc(sizeof(TSQuerySign));
|
||||
TSQuerySign sign;
|
||||
|
||||
retval = (GISTENTRY *) palloc(sizeof(GISTENTRY));
|
||||
*sign = makeTSQuerySign(DatumGetTSQuery(entry->key));
|
||||
sign = makeTSQuerySign(DatumGetTSQuery(entry->key));
|
||||
|
||||
gistentryinit(*retval, PointerGetDatum(sign),
|
||||
gistentryinit(*retval, TSQuerySignGetDatum(sign),
|
||||
entry->rel, entry->page,
|
||||
entry->offset, FALSE);
|
||||
}
|
||||
@@ -56,7 +57,7 @@ gtsquery_consistent(PG_FUNCTION_ARGS)
|
||||
StrategyNumber strategy = (StrategyNumber) PG_GETARG_UINT16(2);
|
||||
/* Oid subtype = PG_GETARG_OID(3); */
|
||||
bool *recheck = (bool *) PG_GETARG_POINTER(4);
|
||||
TSQuerySign *key = (TSQuerySign *) DatumGetPointer(entry->key);
|
||||
TSQuerySign key = DatumGetTSQuerySign(entry->key);
|
||||
TSQuerySign sq = makeTSQuerySign(query);
|
||||
bool retval;
|
||||
|
||||
@@ -67,15 +68,15 @@ gtsquery_consistent(PG_FUNCTION_ARGS)
|
||||
{
|
||||
case RTContainsStrategyNumber:
|
||||
if (GIST_LEAF(entry))
|
||||
retval = (*key & sq) == sq;
|
||||
retval = (key & sq) == sq;
|
||||
else
|
||||
retval = (*key & sq) != 0;
|
||||
retval = (key & sq) != 0;
|
||||
break;
|
||||
case RTContainedByStrategyNumber:
|
||||
if (GIST_LEAF(entry))
|
||||
retval = (*key & sq) == *key;
|
||||
retval = (key & sq) == key;
|
||||
else
|
||||
retval = (*key & sq) != 0;
|
||||
retval = (key & sq) != 0;
|
||||
break;
|
||||
default:
|
||||
retval = FALSE;
|
||||
@@ -88,27 +89,27 @@ gtsquery_union(PG_FUNCTION_ARGS)
|
||||
{
|
||||
GistEntryVector *entryvec = (GistEntryVector *) PG_GETARG_POINTER(0);
|
||||
int *size = (int *) PG_GETARG_POINTER(1);
|
||||
TSQuerySign *sign = (TSQuerySign *) palloc(sizeof(TSQuerySign));
|
||||
TSQuerySign sign;
|
||||
int i;
|
||||
|
||||
memset(sign, 0, sizeof(TSQuerySign));
|
||||
sign = 0;
|
||||
|
||||
for (i = 0; i < entryvec->n; i++)
|
||||
*sign |= *GETENTRY(entryvec, i);
|
||||
sign |= GETENTRY(entryvec, i);
|
||||
|
||||
*size = sizeof(TSQuerySign);
|
||||
|
||||
PG_RETURN_POINTER(sign);
|
||||
PG_RETURN_TSQUERYSIGN(sign);
|
||||
}
|
||||
|
||||
Datum
|
||||
gtsquery_same(PG_FUNCTION_ARGS)
|
||||
{
|
||||
TSQuerySign *a = (TSQuerySign *) PG_GETARG_POINTER(0);
|
||||
TSQuerySign *b = (TSQuerySign *) PG_GETARG_POINTER(1);
|
||||
bool *result = (bool *) PG_GETARG_POINTER(2);
|
||||
TSQuerySign a = PG_GETARG_TSQUERYSIGN(0);
|
||||
TSQuerySign b = PG_GETARG_TSQUERYSIGN(1);
|
||||
bool *result = (bool *) PG_GETARG_POINTER(2);
|
||||
|
||||
*result = (*a == *b) ? true : false;
|
||||
*result = (a == b) ? true : false;
|
||||
|
||||
PG_RETURN_POINTER(result);
|
||||
}
|
||||
@@ -136,11 +137,11 @@ hemdist(TSQuerySign a, TSQuerySign b)
|
||||
Datum
|
||||
gtsquery_penalty(PG_FUNCTION_ARGS)
|
||||
{
|
||||
TSQuerySign *origval = (TSQuerySign *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(0))->key);
|
||||
TSQuerySign *newval = (TSQuerySign *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(1))->key);
|
||||
TSQuerySign origval = DatumGetTSQuerySign(((GISTENTRY *) PG_GETARG_POINTER(0))->key);
|
||||
TSQuerySign newval = DatumGetTSQuerySign(((GISTENTRY *) PG_GETARG_POINTER(1))->key);
|
||||
float *penalty = (float *) PG_GETARG_POINTER(2);
|
||||
|
||||
*penalty = hemdist(*origval, *newval);
|
||||
*penalty = hemdist(origval, newval);
|
||||
|
||||
PG_RETURN_POINTER(penalty);
|
||||
}
|
||||
@@ -171,9 +172,8 @@ gtsquery_picksplit(PG_FUNCTION_ARGS)
|
||||
OffsetNumber maxoff = entryvec->n - 2;
|
||||
OffsetNumber k,
|
||||
j;
|
||||
|
||||
TSQuerySign *datum_l,
|
||||
*datum_r;
|
||||
TSQuerySign datum_l,
|
||||
datum_r;
|
||||
int4 size_alpha,
|
||||
size_beta;
|
||||
int4 size_waste,
|
||||
@@ -194,7 +194,7 @@ gtsquery_picksplit(PG_FUNCTION_ARGS)
|
||||
for (k = FirstOffsetNumber; k < maxoff; k = OffsetNumberNext(k))
|
||||
for (j = OffsetNumberNext(k); j <= maxoff; j = OffsetNumberNext(j))
|
||||
{
|
||||
size_waste = hemdist(*GETENTRY(entryvec, j), *GETENTRY(entryvec, k));
|
||||
size_waste = hemdist(GETENTRY(entryvec, j), GETENTRY(entryvec, k));
|
||||
if (size_waste > waste)
|
||||
{
|
||||
waste = size_waste;
|
||||
@@ -210,19 +210,16 @@ gtsquery_picksplit(PG_FUNCTION_ARGS)
|
||||
seed_2 = 2;
|
||||
}
|
||||
|
||||
datum_l = (TSQuerySign *) palloc(sizeof(TSQuerySign));
|
||||
*datum_l = *GETENTRY(entryvec, seed_1);
|
||||
datum_r = (TSQuerySign *) palloc(sizeof(TSQuerySign));
|
||||
*datum_r = *GETENTRY(entryvec, seed_2);
|
||||
|
||||
datum_l = GETENTRY(entryvec, seed_1);
|
||||
datum_r = GETENTRY(entryvec, seed_2);
|
||||
|
||||
maxoff = OffsetNumberNext(maxoff);
|
||||
costvector = (SPLITCOST *) palloc(sizeof(SPLITCOST) * maxoff);
|
||||
for (j = FirstOffsetNumber; j <= maxoff; j = OffsetNumberNext(j))
|
||||
{
|
||||
costvector[j - 1].pos = j;
|
||||
size_alpha = hemdist(*GETENTRY(entryvec, seed_1), *GETENTRY(entryvec, j));
|
||||
size_beta = hemdist(*GETENTRY(entryvec, seed_2), *GETENTRY(entryvec, j));
|
||||
size_alpha = hemdist(GETENTRY(entryvec, seed_1), GETENTRY(entryvec, j));
|
||||
size_beta = hemdist(GETENTRY(entryvec, seed_2), GETENTRY(entryvec, j));
|
||||
costvector[j - 1].cost = abs(size_alpha - size_beta);
|
||||
}
|
||||
qsort((void *) costvector, maxoff, sizeof(SPLITCOST), comparecost);
|
||||
@@ -242,26 +239,26 @@ gtsquery_picksplit(PG_FUNCTION_ARGS)
|
||||
v->spl_nright++;
|
||||
continue;
|
||||
}
|
||||
size_alpha = hemdist(*datum_l, *GETENTRY(entryvec, j));
|
||||
size_beta = hemdist(*datum_r, *GETENTRY(entryvec, j));
|
||||
size_alpha = hemdist(datum_l, GETENTRY(entryvec, j));
|
||||
size_beta = hemdist(datum_r, GETENTRY(entryvec, j));
|
||||
|
||||
if (size_alpha < size_beta + WISH_F(v->spl_nleft, v->spl_nright, 0.05))
|
||||
{
|
||||
*datum_l |= *GETENTRY(entryvec, j);
|
||||
datum_l |= GETENTRY(entryvec, j);
|
||||
*left++ = j;
|
||||
v->spl_nleft++;
|
||||
}
|
||||
else
|
||||
{
|
||||
*datum_r |= *GETENTRY(entryvec, j);
|
||||
datum_r |= GETENTRY(entryvec, j);
|
||||
*right++ = j;
|
||||
v->spl_nright++;
|
||||
}
|
||||
}
|
||||
|
||||
*right = *left = FirstOffsetNumber;
|
||||
v->spl_ldatum = PointerGetDatum(datum_l);
|
||||
v->spl_rdatum = PointerGetDatum(datum_r);
|
||||
v->spl_ldatum = TSQuerySignGetDatum(datum_l);
|
||||
v->spl_rdatum = TSQuerySignGetDatum(datum_r);
|
||||
|
||||
PG_RETURN_POINTER(v);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user