diff options
author | Tom Lane <tgl@sss.pgh.pa.us> | 2008-03-25 22:42:46 +0000 |
---|---|---|
committer | Tom Lane <tgl@sss.pgh.pa.us> | 2008-03-25 22:42:46 +0000 |
commit | 220db7ccd8c88aafea4629f00e8be6f9f073ed00 (patch) | |
tree | 772c9dca19736eb9d417cb665a281686c3570b4c /src/backend/utils/adt | |
parent | f948197b4055bb0e22e508e9d6966217b7dbea3d (diff) | |
download | postgresql-220db7ccd8c88aafea4629f00e8be6f9f073ed00.tar.gz postgresql-220db7ccd8c88aafea4629f00e8be6f9f073ed00.zip |
Simplify and standardize conversions between TEXT datums and ordinary C
strings. This patch introduces four support functions cstring_to_text,
cstring_to_text_with_len, text_to_cstring, and text_to_cstring_buffer, and
two macros CStringGetTextDatum and TextDatumGetCString. A number of
existing macros that provided variants on these themes were removed.
Most of the places that need to make such conversions now require just one
function or macro call, in place of the multiple notational layers that used
to be needed. There are no longer any direct calls of textout or textin,
and we got most of the places that were using handmade conversions via
memcpy (there may be a few still lurking, though).
This commit doesn't make any serious effort to eliminate transient memory
leaks caused by detoasting toasted text objects before they reach
text_to_cstring. We changed PG_GETARG_TEXT_P to PG_GETARG_TEXT_PP in a few
places where it was easy, but much more could be done.
Brendan Jurd and Tom Lane
Diffstat (limited to 'src/backend/utils/adt')
28 files changed, 362 insertions, 535 deletions
diff --git a/src/backend/utils/adt/acl.c b/src/backend/utils/adt/acl.c index 65d7794b4e4..3cf54e58750 100644 --- a/src/backend/utils/adt/acl.c +++ b/src/backend/utils/adt/acl.c @@ -8,7 +8,7 @@ * * * IDENTIFICATION - * $PostgreSQL: pgsql/src/backend/utils/adt/acl.c,v 1.139 2008/01/01 19:45:52 momjian Exp $ + * $PostgreSQL: pgsql/src/backend/utils/adt/acl.c,v 1.140 2008/03/25 22:42:43 tgl Exp $ * *------------------------------------------------------------------------- */ @@ -1313,10 +1313,7 @@ makeaclitem(PG_FUNCTION_ARGS) static AclMode convert_priv_string(text *priv_type_text) { - char *priv_type; - - priv_type = DatumGetCString(DirectFunctionCall1(textout, - PointerGetDatum(priv_type_text))); + char *priv_type = text_to_cstring(priv_type_text); if (pg_strcasecmp(priv_type, "SELECT") == 0) return ACL_SELECT; @@ -1526,10 +1523,7 @@ convert_table_name(text *tablename) static AclMode convert_table_priv_string(text *priv_type_text) { - char *priv_type; - - priv_type = DatumGetCString(DirectFunctionCall1(textout, - PointerGetDatum(priv_type_text))); + char *priv_type = text_to_cstring(priv_type_text); /* * Return mode from priv_type string @@ -1736,12 +1730,9 @@ has_database_privilege_id_id(PG_FUNCTION_ARGS) static Oid convert_database_name(text *databasename) { - char *dbname; + char *dbname = text_to_cstring(databasename); Oid oid; - dbname = DatumGetCString(DirectFunctionCall1(textout, - PointerGetDatum(databasename))); - oid = get_database_oid(dbname); if (!OidIsValid(oid)) ereport(ERROR, @@ -1758,10 +1749,7 @@ convert_database_name(text *databasename) static AclMode convert_database_priv_string(text *priv_type_text) { - char *priv_type; - - priv_type = DatumGetCString(DirectFunctionCall1(textout, - PointerGetDatum(priv_type_text))); + char *priv_type = text_to_cstring(priv_type_text); /* * Return mode from priv_type string @@ -1953,12 +1941,9 @@ has_function_privilege_id_id(PG_FUNCTION_ARGS) static Oid convert_function_name(text *functionname) { - char *funcname; + char *funcname = text_to_cstring(functionname); Oid oid; - funcname = DatumGetCString(DirectFunctionCall1(textout, - PointerGetDatum(functionname))); - oid = DatumGetObjectId(DirectFunctionCall1(regprocedurein, CStringGetDatum(funcname))); @@ -1977,10 +1962,7 @@ convert_function_name(text *functionname) static AclMode convert_function_priv_string(text *priv_type_text) { - char *priv_type; - - priv_type = DatumGetCString(DirectFunctionCall1(textout, - PointerGetDatum(priv_type_text))); + char *priv_type = text_to_cstring(priv_type_text); /* * Return mode from priv_type string @@ -2157,12 +2139,9 @@ has_language_privilege_id_id(PG_FUNCTION_ARGS) static Oid convert_language_name(text *languagename) { - char *langname; + char *langname = text_to_cstring(languagename); Oid oid; - langname = DatumGetCString(DirectFunctionCall1(textout, - PointerGetDatum(languagename))); - oid = GetSysCacheOid(LANGNAME, CStringGetDatum(langname), 0, 0, 0); @@ -2181,10 +2160,7 @@ convert_language_name(text *languagename) static AclMode convert_language_priv_string(text *priv_type_text) { - char *priv_type; - - priv_type = DatumGetCString(DirectFunctionCall1(textout, - PointerGetDatum(priv_type_text))); + char *priv_type = text_to_cstring(priv_type_text); /* * Return mode from priv_type string @@ -2361,12 +2337,9 @@ has_schema_privilege_id_id(PG_FUNCTION_ARGS) static Oid convert_schema_name(text *schemaname) { - char *nspname; + char *nspname = text_to_cstring(schemaname); Oid oid; - nspname = DatumGetCString(DirectFunctionCall1(textout, - PointerGetDatum(schemaname))); - oid = GetSysCacheOid(NAMESPACENAME, CStringGetDatum(nspname), 0, 0, 0); @@ -2385,10 +2358,7 @@ convert_schema_name(text *schemaname) static AclMode convert_schema_priv_string(text *priv_type_text) { - char *priv_type; - - priv_type = DatumGetCString(DirectFunctionCall1(textout, - PointerGetDatum(priv_type_text))); + char *priv_type = text_to_cstring(priv_type_text); /* * Return mode from priv_type string @@ -2569,11 +2539,9 @@ has_tablespace_privilege_id_id(PG_FUNCTION_ARGS) static Oid convert_tablespace_name(text *tablespacename) { - char *spcname; + char *spcname = text_to_cstring(tablespacename); Oid oid; - spcname = DatumGetCString(DirectFunctionCall1(textout, - PointerGetDatum(tablespacename))); oid = get_tablespace_oid(spcname); if (!OidIsValid(oid)) @@ -2591,10 +2559,7 @@ convert_tablespace_name(text *tablespacename) static AclMode convert_tablespace_priv_string(text *priv_type_text) { - char *priv_type; - - priv_type = DatumGetCString(DirectFunctionCall1(textout, - PointerGetDatum(priv_type_text))); + char *priv_type = text_to_cstring(priv_type_text); /* * Return mode from priv_type string @@ -2777,10 +2742,7 @@ pg_has_role_id_id(PG_FUNCTION_ARGS) static AclMode convert_role_priv_string(text *priv_type_text) { - char *priv_type; - - priv_type = DatumGetCString(DirectFunctionCall1(textout, - PointerGetDatum(priv_type_text))); + char *priv_type = text_to_cstring(priv_type_text); /* * Return mode from priv_type string diff --git a/src/backend/utils/adt/arrayfuncs.c b/src/backend/utils/adt/arrayfuncs.c index b2beb18bc5f..f34ee3b773e 100644 --- a/src/backend/utils/adt/arrayfuncs.c +++ b/src/backend/utils/adt/arrayfuncs.c @@ -8,7 +8,7 @@ * * * IDENTIFICATION - * $PostgreSQL: pgsql/src/backend/utils/adt/arrayfuncs.c,v 1.141 2008/02/29 20:58:33 alvherre Exp $ + * $PostgreSQL: pgsql/src/backend/utils/adt/arrayfuncs.c,v 1.142 2008/03/25 22:42:43 tgl Exp $ * *------------------------------------------------------------------------- */ @@ -1533,39 +1533,33 @@ Datum array_dims(PG_FUNCTION_ARGS) { ArrayType *v = PG_GETARG_ARRAYTYPE_P(0); - text *result; char *p; - int nbytes, - i; + int i; int *dimv, *lb; - /* Sanity check: does it look like an array at all? */ - if (ARR_NDIM(v) <= 0 || ARR_NDIM(v) > MAXDIM) - PG_RETURN_NULL(); - - nbytes = ARR_NDIM(v) * 33 + 1; - /* * 33 since we assume 15 digits per number + ':' +'[]' * - * +1 allows for temp trailing null + * +1 for trailing null */ + char buf[MAXDIM * 33 + 1]; - result = (text *) palloc(nbytes + VARHDRSZ); - p = VARDATA(result); + /* Sanity check: does it look like an array at all? */ + if (ARR_NDIM(v) <= 0 || ARR_NDIM(v) > MAXDIM) + PG_RETURN_NULL(); dimv = ARR_DIMS(v); lb = ARR_LBOUND(v); + p = buf; for (i = 0; i < ARR_NDIM(v); i++) { sprintf(p, "[%d:%d]", lb[i], dimv[i] + lb[i] - 1); p += strlen(p); } - SET_VARSIZE(result, strlen(VARDATA(result)) + VARHDRSZ); - PG_RETURN_TEXT_P(result); + PG_RETURN_TEXT_P(cstring_to_text(buf)); } /* diff --git a/src/backend/utils/adt/bool.c b/src/backend/utils/adt/bool.c index 7261ab3e406..4ba234c62c6 100644 --- a/src/backend/utils/adt/bool.c +++ b/src/backend/utils/adt/bool.c @@ -8,7 +8,7 @@ * * * IDENTIFICATION - * $PostgreSQL: pgsql/src/backend/utils/adt/bool.c,v 1.42 2008/01/01 19:45:52 momjian Exp $ + * $PostgreSQL: pgsql/src/backend/utils/adt/bool.c,v 1.43 2008/03/25 22:42:43 tgl Exp $ * *------------------------------------------------------------------------- */ @@ -152,14 +152,14 @@ Datum booltext(PG_FUNCTION_ARGS) { bool arg1 = PG_GETARG_BOOL(0); - char *str; + const char *str; if (arg1) str = "true"; else str = "false"; - PG_RETURN_DATUM(DirectFunctionCall1(textin, CStringGetDatum(str))); + PG_RETURN_TEXT_P(cstring_to_text(str)); } diff --git a/src/backend/utils/adt/cash.c b/src/backend/utils/adt/cash.c index df5d7bf8629..84e8c742ec5 100644 --- a/src/backend/utils/adt/cash.c +++ b/src/backend/utils/adt/cash.c @@ -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.77 2007/11/24 16:18:48 momjian Exp $ + * $PostgreSQL: pgsql/src/backend/utils/adt/cash.c,v 1.78 2008/03/25 22:42:43 tgl Exp $ */ #include "postgres.h" @@ -24,6 +24,7 @@ #include <locale.h> #include "libpq/pqformat.h" +#include "utils/builtins.h" #include "utils/cash.h" #include "utils/pg_locale.h" @@ -796,7 +797,6 @@ cash_words(PG_FUNCTION_ARGS) Cash m4; Cash m5; Cash m6; - text *result; /* work with positive numbers */ if (value < 0) @@ -862,10 +862,6 @@ cash_words(PG_FUNCTION_ARGS) /* capitalize output */ buf[0] = pg_toupper((unsigned char) buf[0]); - /* make a text type for output */ - result = (text *) palloc(strlen(buf) + VARHDRSZ); - SET_VARSIZE(result, strlen(buf) + VARHDRSZ); - memcpy(VARDATA(result), buf, strlen(buf)); - - PG_RETURN_TEXT_P(result); + /* return as text datum */ + PG_RETURN_TEXT_P(cstring_to_text(buf)); } diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 5b6b9ebfa97..e7374388dd1 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -8,7 +8,7 @@ * * * IDENTIFICATION - * $PostgreSQL: pgsql/src/backend/utils/adt/date.c,v 1.140 2008/03/21 01:31:42 tgl Exp $ + * $PostgreSQL: pgsql/src/backend/utils/adt/date.c,v 1.141 2008/03/25 22:42:43 tgl Exp $ * *------------------------------------------------------------------------- */ @@ -1594,15 +1594,15 @@ time_mi_interval(PG_FUNCTION_ARGS) Datum time_part(PG_FUNCTION_ARGS) { - text *units = PG_GETARG_TEXT_P(0); + text *units = PG_GETARG_TEXT_PP(0); TimeADT time = PG_GETARG_TIMEADT(1); float8 result; int type, val; char *lowunits; - lowunits = downcase_truncate_identifier(VARDATA(units), - VARSIZE(units) - VARHDRSZ, + lowunits = downcase_truncate_identifier(VARDATA_ANY(units), + VARSIZE_ANY_EXHDR(units), false); type = DecodeUnits(0, lowunits, &val); @@ -1666,9 +1666,7 @@ time_part(PG_FUNCTION_ARGS) ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE), errmsg("\"time\" units \"%s\" not recognized", - DatumGetCString(DirectFunctionCall1(textout, - PointerGetDatum(units)))))); - + lowunits))); result = 0; } } @@ -1685,8 +1683,7 @@ time_part(PG_FUNCTION_ARGS) ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE), errmsg("\"time\" units \"%s\" not recognized", - DatumGetCString(DirectFunctionCall1(textout, - PointerGetDatum(units)))))); + lowunits))); result = 0; } @@ -2323,15 +2320,15 @@ datetimetz_timestamptz(PG_FUNCTION_ARGS) Datum timetz_part(PG_FUNCTION_ARGS) { - text *units = PG_GETARG_TEXT_P(0); + text *units = PG_GETARG_TEXT_PP(0); TimeTzADT *time = PG_GETARG_TIMETZADT_P(1); float8 result; int type, val; char *lowunits; - lowunits = downcase_truncate_identifier(VARDATA(units), - VARSIZE(units) - VARHDRSZ, + lowunits = downcase_truncate_identifier(VARDATA_ANY(units), + VARSIZE_ANY_EXHDR(units), false); type = DecodeUnits(0, lowunits, &val); @@ -2408,9 +2405,7 @@ timetz_part(PG_FUNCTION_ARGS) ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE), errmsg("\"time with time zone\" units \"%s\" not recognized", - DatumGetCString(DirectFunctionCall1(textout, - PointerGetDatum(units)))))); - + lowunits))); result = 0; } } @@ -2427,9 +2422,7 @@ timetz_part(PG_FUNCTION_ARGS) ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE), errmsg("\"time with time zone\" units \"%s\" not recognized", - DatumGetCString(DirectFunctionCall1(textout, - PointerGetDatum(units)))))); - + lowunits))); result = 0; } @@ -2443,12 +2436,11 @@ timetz_part(PG_FUNCTION_ARGS) Datum timetz_zone(PG_FUNCTION_ARGS) { - text *zone = PG_GETARG_TEXT_P(0); + text *zone = PG_GETARG_TEXT_PP(0); TimeTzADT *t = PG_GETARG_TIMETZADT_P(1); TimeTzADT *result; int tz; char tzname[TZ_STRLEN_MAX + 1]; - int len; pg_tz *tzp; /* @@ -2456,9 +2448,7 @@ timetz_zone(PG_FUNCTION_ARGS) * (to handle cases like "America/New_York"), and if that fails, we look * in the date token table (to handle cases like "EST"). */ - len = Min(VARSIZE(zone) - VARHDRSZ, TZ_STRLEN_MAX); - memcpy(tzname, VARDATA(zone), len); - tzname[len] = '\0'; + text_to_cstring_buffer(zone, tzname, sizeof(tzname)); tzp = pg_tzset(tzname); if (tzp) { @@ -2475,8 +2465,8 @@ timetz_zone(PG_FUNCTION_ARGS) int type, val; - lowzone = downcase_truncate_identifier(VARDATA(zone), - VARSIZE(zone) - VARHDRSZ, + lowzone = downcase_truncate_identifier(tzname, + strlen(tzname), false); type = DecodeSpecial(0, lowzone, &val); diff --git a/src/backend/utils/adt/datetime.c b/src/backend/utils/adt/datetime.c index 08f21b46b0a..ef6a87d3a83 100644 --- a/src/backend/utils/adt/datetime.c +++ b/src/backend/utils/adt/datetime.c @@ -8,7 +8,7 @@ * * * IDENTIFICATION - * $PostgreSQL: pgsql/src/backend/utils/adt/datetime.c,v 1.187 2008/02/25 23:36:28 tgl Exp $ + * $PostgreSQL: pgsql/src/backend/utils/adt/datetime.c,v 1.188 2008/03/25 22:42:43 tgl Exp $ * *------------------------------------------------------------------------- */ @@ -3910,7 +3910,7 @@ pg_timezone_abbrevs(PG_FUNCTION_ARGS) for (p = (unsigned char *) buffer; *p; p++) *p = pg_toupper(*p); - values[0] = DirectFunctionCall1(textin, CStringGetDatum(buffer)); + values[0] = CStringGetTextDatum(buffer); MemSet(&tm, 0, sizeof(struct pg_tm)); tm.tm_min = (-1) * FROMVAL(&timezonetktbl[*pindex]); @@ -4020,11 +4020,8 @@ pg_timezone_names(PG_FUNCTION_ARGS) MemSet(nulls, 0, sizeof(nulls)); - values[0] = DirectFunctionCall1(textin, - CStringGetDatum(pg_get_timezone_name(tz))); - - values[1] = DirectFunctionCall1(textin, - CStringGetDatum(tzn ? tzn : "")); + values[0] = CStringGetTextDatum(pg_get_timezone_name(tz)); + values[1] = CStringGetTextDatum(tzn ? tzn : ""); MemSet(&itm, 0, sizeof(struct pg_tm)); itm.tm_sec = -tzoff; diff --git a/src/backend/utils/adt/dbsize.c b/src/backend/utils/adt/dbsize.c index 00ac3b0e094..72807321ae3 100644 --- a/src/backend/utils/adt/dbsize.c +++ b/src/backend/utils/adt/dbsize.c @@ -5,7 +5,7 @@ * Copyright (c) 2002-2008, PostgreSQL Global Development Group * * IDENTIFICATION - * $PostgreSQL: pgsql/src/backend/utils/adt/dbsize.c,v 1.16 2008/01/01 19:45:52 momjian Exp $ + * $PostgreSQL: pgsql/src/backend/utils/adt/dbsize.c,v 1.17 2008/03/25 22:42:44 tgl Exp $ * */ @@ -393,41 +393,39 @@ Datum pg_size_pretty(PG_FUNCTION_ARGS) { int64 size = PG_GETARG_INT64(0); - char *result = palloc(50 + VARHDRSZ); + char buf[64]; int64 limit = 10 * 1024; int64 mult = 1; if (size < limit * mult) - snprintf(VARDATA(result), 50, INT64_FORMAT " bytes", size); + snprintf(buf, sizeof(buf), INT64_FORMAT " bytes", size); else { mult *= 1024; if (size < limit * mult) - snprintf(VARDATA(result), 50, INT64_FORMAT " kB", + snprintf(buf, sizeof(buf), INT64_FORMAT " kB", (size + mult / 2) / mult); else { mult *= 1024; if (size < limit * mult) - snprintf(VARDATA(result), 50, INT64_FORMAT " MB", + snprintf(buf, sizeof(buf), INT64_FORMAT " MB", (size + mult / 2) / mult); else { mult *= 1024; if (size < limit * mult) - snprintf(VARDATA(result), 50, INT64_FORMAT " GB", + snprintf(buf, sizeof(buf), INT64_FORMAT " GB", (size + mult / 2) / mult); else { mult *= 1024; - snprintf(VARDATA(result), 50, INT64_FORMAT " TB", + snprintf(buf, sizeof(buf), INT64_FORMAT " TB", (size + mult / 2) / mult); } } } } - SET_VARSIZE(result, strlen(VARDATA(result)) + VARHDRSZ); - - PG_RETURN_TEXT_P(result); + PG_RETURN_TEXT_P(cstring_to_text(buf)); } diff --git a/src/backend/utils/adt/encode.c b/src/backend/utils/adt/encode.c index cbbc4b69a2f..290888f996f 100644 --- a/src/backend/utils/adt/encode.c +++ b/src/backend/utils/adt/encode.c @@ -7,7 +7,7 @@ * * * IDENTIFICATION - * $PostgreSQL: pgsql/src/backend/utils/adt/encode.c,v 1.21 2008/02/26 02:54:08 tgl Exp $ + * $PostgreSQL: pgsql/src/backend/utils/adt/encode.c,v 1.22 2008/03/25 22:42:44 tgl Exp $ * *------------------------------------------------------------------------- */ @@ -46,7 +46,7 @@ binary_encode(PG_FUNCTION_ARGS) datalen = VARSIZE(data) - VARHDRSZ; - namebuf = DatumGetCString(DirectFunctionCall1(textout, name)); + namebuf = TextDatumGetCString(name); enc = pg_find_encoding(namebuf); if (enc == NULL) @@ -82,7 +82,7 @@ binary_decode(PG_FUNCTION_ARGS) datalen = VARSIZE(data) - VARHDRSZ; - namebuf = DatumGetCString(DirectFunctionCall1(textout, name)); + namebuf = TextDatumGetCString(name); enc = pg_find_encoding(namebuf); if (enc == NULL) diff --git a/src/backend/utils/adt/format_type.c b/src/backend/utils/adt/format_type.c index 86314e1a751..1ca2462c793 100644 --- a/src/backend/utils/adt/format_type.c +++ b/src/backend/utils/adt/format_type.c @@ -8,7 +8,7 @@ * Portions Copyright (c) 1994, Regents of the University of California * * IDENTIFICATION - * $PostgreSQL: pgsql/src/backend/utils/adt/format_type.c,v 1.49 2008/01/01 19:45:52 momjian Exp $ + * $PostgreSQL: pgsql/src/backend/utils/adt/format_type.c,v 1.50 2008/03/25 22:42:44 tgl Exp $ * *------------------------------------------------------------------------- */ @@ -26,7 +26,6 @@ #include "mb/pg_wchar.h" #define MAX_INT32_LEN 11 -#define _textin(str) DirectFunctionCall1(textin, CStringGetDatum(str)) static char *format_type_internal(Oid type_oid, int32 typemod, bool typemod_given, bool allow_invalid); @@ -84,7 +83,7 @@ format_type(PG_FUNCTION_ARGS) result = format_type_internal(type_oid, typemod, true, true); } - PG_RETURN_DATUM(_textin(result)); + PG_RETURN_TEXT_P(cstring_to_text(result)); } /* @@ -454,7 +453,7 @@ oidvectortypes(PG_FUNCTION_ARGS) left -= slen; } - PG_RETURN_DATUM(_textin(result)); + PG_RETURN_TEXT_P(cstring_to_text(result)); } diff --git a/src/backend/utils/adt/formatting.c b/src/backend/utils/adt/formatting.c index d1cc69a7322..103543817d2 100644 --- a/src/backend/utils/adt/formatting.c +++ b/src/backend/utils/adt/formatting.c @@ -1,7 +1,7 @@ /* ----------------------------------------------------------------------- * formatting.c * - * $PostgreSQL: pgsql/src/backend/utils/adt/formatting.c,v 1.138 2008/03/22 22:32:19 tgl Exp $ + * $PostgreSQL: pgsql/src/backend/utils/adt/formatting.c,v 1.139 2008/03/25 22:42:44 tgl Exp $ * * * Portions Copyright (c) 1999-2008, PostgreSQL Global Development Group @@ -924,7 +924,7 @@ static int seq_search(char *name, char **array, int type, int max, int *len); static void do_to_timestamp(text *date_txt, text *fmt, struct pg_tm * tm, fsec_t *fsec); static char *fill_str(char *str, int c, int max); -static FormatNode *NUM_cache(int len, NUMDesc *Num, char *pars_str, bool *shouldFree); +static FormatNode *NUM_cache(int len, NUMDesc *Num, text *pars_str, bool *shouldFree); static char *int_to_roman(int number); static void NUM_prepare_locale(NUMProc *Np); static char *get_last_relevant_decnum(char *num); @@ -2709,16 +2709,14 @@ datetime_to_char_body(TmToChar *tmtc, text *fmt, bool is_interval) char *fmt_str, *result; bool incache; - int fmt_len = VARSIZE(fmt) - VARHDRSZ; - int reslen; + int fmt_len; text *res; /* * Convert fmt to C string */ - fmt_str = (char *) palloc(fmt_len + 1); - memcpy(fmt_str, VARDATA(fmt), fmt_len); - *(fmt_str + fmt_len) = '\0'; + fmt_str = text_to_cstring(fmt); + fmt_len = strlen(fmt_str); /* * Allocate workspace for result as C string @@ -2779,10 +2777,7 @@ datetime_to_char_body(TmToChar *tmtc, text *fmt, bool is_interval) pfree(fmt_str); /* convert C-string result to TEXT format */ - reslen = strlen(result); - res = (text *) palloc(reslen + VARHDRSZ); - memcpy(VARDATA(res), result, reslen); - SET_VARSIZE(res, reslen + VARHDRSZ); + res = cstring_to_text(result); pfree(result); return res; @@ -3135,18 +3130,15 @@ do_to_timestamp(text *date_txt, text *fmt, ZERO_tm(tm); *fsec = 0; - fmt_len = VARSIZE(fmt) - VARHDRSZ; + fmt_len = VARSIZE_ANY_EXHDR(fmt); if (fmt_len) { - int date_len; char *fmt_str; char *date_str; bool incache; - fmt_str = (char *) palloc(fmt_len + 1); - memcpy(fmt_str, VARDATA(fmt), fmt_len); - *(fmt_str + fmt_len) = '\0'; + fmt_str = text_to_cstring(fmt); /* * Allocate new memory if format picture is bigger than static cache @@ -3195,13 +3187,7 @@ do_to_timestamp(text *date_txt, text *fmt, /* dump_node(format, fmt_len); */ #endif - /* - * Convert date to C string - */ - date_len = VARSIZE(date_txt) - VARHDRSZ; - date_str = (char *) palloc(date_len + 1); - memcpy(date_str, VARDATA(date_txt), date_len); - *(date_str + date_len) = '\0'; + date_str = text_to_cstring(date_txt); DCH_from_char(format, date_str, &tmfc); @@ -3548,17 +3534,12 @@ NUM_cache_remove(NUMCacheEntry *ent) * ---------- */ static FormatNode * -NUM_cache(int len, NUMDesc *Num, char *pars_str, bool *shouldFree) +NUM_cache(int len, NUMDesc *Num, text *pars_str, bool *shouldFree) { FormatNode *format = NULL; char *str; - /* - * Convert VARDATA() to string - */ - str = (char *) palloc(len + 1); - memcpy(str, pars_str, len); - *(str + len) = '\0'; + str = text_to_cstring(pars_str); /* * Allocate new memory if format picture is bigger than static cache and @@ -4597,11 +4578,11 @@ NUM_processor(FormatNode *node, NUMDesc *Num, char *inout, char *number, */ #define NUM_TOCHAR_prepare \ do { \ - len = VARSIZE(fmt) - VARHDRSZ; \ + len = VARSIZE_ANY_EXHDR(fmt); \ if (len <= 0 || len >= (INT_MAX-VARHDRSZ)/NUM_MAX_ITEM_SIZ) \ - return DirectFunctionCall1(textin, CStringGetDatum("")); \ + PG_RETURN_TEXT_P(cstring_to_text("")); \ result = (text *) palloc0((len * NUM_MAX_ITEM_SIZ) + 1 + VARHDRSZ); \ - format = NUM_cache(len, &Num, VARDATA(fmt), &shouldFree); \ + format = NUM_cache(len, &Num, fmt, &shouldFree); \ } while (0) /* ---------- @@ -4647,7 +4628,7 @@ numeric_to_number(PG_FUNCTION_ARGS) if (len <= 0 || len >= INT_MAX / NUM_MAX_ITEM_SIZ) PG_RETURN_NULL(); - format = NUM_cache(len, &Num, VARDATA(fmt), &shouldFree); + format = NUM_cache(len, &Num, fmt, &shouldFree); numstr = (char *) palloc((len * NUM_MAX_ITEM_SIZ) + 1); diff --git a/src/backend/utils/adt/genfile.c b/src/backend/utils/adt/genfile.c index a3a16bcfa50..86408859f22 100644 --- a/src/backend/utils/adt/genfile.c +++ b/src/backend/utils/adt/genfile.c @@ -9,7 +9,7 @@ * Author: Andreas Pflug <pgadmin@pse-consulting.de> * * IDENTIFICATION - * $PostgreSQL: pgsql/src/backend/utils/adt/genfile.c,v 1.17 2008/01/01 19:45:52 momjian Exp $ + * $PostgreSQL: pgsql/src/backend/utils/adt/genfile.c,v 1.18 2008/03/25 22:42:44 tgl Exp $ * *------------------------------------------------------------------------- */ @@ -46,12 +46,9 @@ typedef struct static char * convert_and_check_filename(text *arg) { - int input_len = VARSIZE(arg) - VARHDRSZ; - char *filename = palloc(input_len + 1); - - memcpy(filename, VARDATA(arg), input_len); - filename[input_len] = '\0'; + char *filename; + filename = text_to_cstring(arg); canonicalize_path(filename); /* filename can change length here */ /* Disallow ".." in the path */ @@ -253,18 +250,11 @@ pg_ls_dir(PG_FUNCTION_ARGS) while ((de = ReadDir(fctx->dirdesc, fctx->location)) != NULL) { - int len = strlen(de->d_name); - text *result; - if (strcmp(de->d_name, ".") == 0 || strcmp(de->d_name, "..") == 0) continue; - result = palloc(len + VARHDRSZ); - SET_VARSIZE(result, len + VARHDRSZ); - memcpy(VARDATA(result), de->d_name, len); - - SRF_RETURN_NEXT(funcctx, PointerGetDatum(result)); + SRF_RETURN_NEXT(funcctx, CStringGetTextDatum(de->d_name)); } FreeDir(fctx->dirdesc); diff --git a/src/backend/utils/adt/lockfuncs.c b/src/backend/utils/adt/lockfuncs.c index de5420e7719..55a6adaa5fa 100644 --- a/src/backend/utils/adt/lockfuncs.c +++ b/src/backend/utils/adt/lockfuncs.c @@ -6,7 +6,7 @@ * Copyright (c) 2002-2008, PostgreSQL Global Development Group * * IDENTIFICATION - * $PostgreSQL: pgsql/src/backend/utils/adt/lockfuncs.c,v 1.32 2008/01/08 23:18:51 tgl Exp $ + * $PostgreSQL: pgsql/src/backend/utils/adt/lockfuncs.c,v 1.33 2008/03/25 22:42:44 tgl Exp $ * *------------------------------------------------------------------------- */ @@ -57,7 +57,7 @@ VXIDGetDatum(BackendId bid, LocalTransactionId lxid) snprintf(vxidstr, sizeof(vxidstr), "%d/%u", bid, lxid); - return DirectFunctionCall1(textin, CStringGetDatum(vxidstr)); + return CStringGetTextDatum(vxidstr); } @@ -214,8 +214,7 @@ pg_lock_status(PG_FUNCTION_ARGS) (int) lock->tag.locktag_type); locktypename = tnbuf; } - values[0] = DirectFunctionCall1(textin, - CStringGetDatum(locktypename)); + values[0] = CStringGetTextDatum(locktypename); switch ((LockTagType) lock->tag.locktag_type) { @@ -297,9 +296,7 @@ pg_lock_status(PG_FUNCTION_ARGS) values[11] = Int32GetDatum(proc->pid); else nulls[11] = 'n'; - values[12] = DirectFunctionCall1(textin, - CStringGetDatum(GetLockmodeName(LOCK_LOCKMETHOD(*lock), - mode))); + values[12] = CStringGetTextDatum(GetLockmodeName(LOCK_LOCKMETHOD(*lock), mode)); values[13] = BoolGetDatum(granted); tuple = heap_formtuple(funcctx->tuple_desc, values, nulls); diff --git a/src/backend/utils/adt/nabstime.c b/src/backend/utils/adt/nabstime.c index c1a484d9bb6..a40ca5edd05 100644 --- a/src/backend/utils/adt/nabstime.c +++ b/src/backend/utils/adt/nabstime.c @@ -10,7 +10,7 @@ * * * IDENTIFICATION - * $PostgreSQL: pgsql/src/backend/utils/adt/nabstime.c,v 1.154 2008/03/21 01:31:42 tgl Exp $ + * $PostgreSQL: pgsql/src/backend/utils/adt/nabstime.c,v 1.155 2008/03/25 22:42:44 tgl Exp $ * *------------------------------------------------------------------------- */ @@ -1576,8 +1576,6 @@ timeofday(PG_FUNCTION_ARGS) struct timeval tp; char templ[128]; char buf[128]; - text *result; - int len; pg_time_t tt; gettimeofday(&tp, NULL); @@ -1586,9 +1584,5 @@ timeofday(PG_FUNCTION_ARGS) pg_localtime(&tt, session_timezone)); snprintf(buf, sizeof(buf), templ, tp.tv_usec); - len = VARHDRSZ + strlen(buf); - result = (text *) palloc(len); - SET_VARSIZE(result, len); - memcpy(VARDATA(result), buf, len - VARHDRSZ); - PG_RETURN_TEXT_P(result); + PG_RETURN_TEXT_P(cstring_to_text(buf)); } diff --git a/src/backend/utils/adt/network.c b/src/backend/utils/adt/network.c index 7eb6ffe306f..8e58374e9b1 100644 --- a/src/backend/utils/adt/network.c +++ b/src/backend/utils/adt/network.c @@ -1,7 +1,7 @@ /* * PostgreSQL type definitions for the INET and CIDR types. * - * $PostgreSQL: pgsql/src/backend/utils/adt/network.c,v 1.72 2007/11/15 21:14:39 momjian Exp $ + * $PostgreSQL: pgsql/src/backend/utils/adt/network.c,v 1.73 2008/03/25 22:42:44 tgl Exp $ * * Jon Postel RIP 16 Oct 1998 */ @@ -601,8 +601,6 @@ Datum network_host(PG_FUNCTION_ARGS) { inet *ip = PG_GETARG_INET_P(0); - text *ret; - int len; char *ptr; char tmp[sizeof("xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255/128")]; @@ -617,12 +615,7 @@ network_host(PG_FUNCTION_ARGS) if ((ptr = strchr(tmp, '/')) != NULL) *ptr = '\0'; - /* Return string as a text datum */ - len = strlen(tmp); - ret = (text *) palloc(len + VARHDRSZ); - SET_VARSIZE(ret, len + VARHDRSZ); - memcpy(VARDATA(ret), tmp, len); - PG_RETURN_TEXT_P(ret); + PG_RETURN_TEXT_P(cstring_to_text(tmp)); } /* @@ -634,7 +627,6 @@ Datum network_show(PG_FUNCTION_ARGS) { inet *ip = PG_GETARG_INET_P(0); - text *ret; int len; char tmp[sizeof("xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255/128")]; @@ -651,21 +643,14 @@ network_show(PG_FUNCTION_ARGS) snprintf(tmp + len, sizeof(tmp) - len, "/%u", ip_bits(ip)); } - /* Return string as a text datum */ - len = strlen(tmp); - ret = (text *) palloc(len + VARHDRSZ); - SET_VARSIZE(ret, len + VARHDRSZ); - memcpy(VARDATA(ret), tmp, len); - PG_RETURN_TEXT_P(ret); + PG_RETURN_TEXT_P(cstring_to_text(tmp)); } Datum inet_abbrev(PG_FUNCTION_ARGS) { inet *ip = PG_GETARG_INET_P(0); - text *ret; char *dst; - int len; char tmp[sizeof("xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255/128")]; dst = inet_net_ntop(ip_family(ip), ip_addr(ip), @@ -676,21 +661,14 @@ inet_abbrev(PG_FUNCTION_ARGS) (errcode(ERRCODE_INVALID_BINARY_REPRESENTATION), errmsg("could not format inet value: %m"))); - /* Return string as a text datum */ - len = strlen(tmp); - ret = (text *) palloc(len + VARHDRSZ); - SET_VARSIZE(ret, len + VARHDRSZ); - memcpy(VARDATA(ret), tmp, len); - PG_RETURN_TEXT_P(ret); + PG_RETURN_TEXT_P(cstring_to_text(tmp)); } Datum cidr_abbrev(PG_FUNCTION_ARGS) { inet *ip = PG_GETARG_INET_P(0); - text *ret; char *dst; - int len; char tmp[sizeof("xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255/128")]; dst = inet_cidr_ntop(ip_family(ip), ip_addr(ip), @@ -701,12 +679,7 @@ cidr_abbrev(PG_FUNCTION_ARGS) (errcode(ERRCODE_INVALID_BINARY_REPRESENTATION), errmsg("could not format cidr value: %m"))); - /* Return string as a text datum */ - len = strlen(tmp); - ret = (text *) palloc(len + VARHDRSZ); - SET_VARSIZE(ret, len + VARHDRSZ); - memcpy(VARDATA(ret), tmp, len); - PG_RETURN_TEXT_P(ret); + PG_RETURN_TEXT_P(cstring_to_text(tmp)); } Datum diff --git a/src/backend/utils/adt/oracle_compat.c b/src/backend/utils/adt/oracle_compat.c index 588cf5941db..257f60a8bbf 100644 --- a/src/backend/utils/adt/oracle_compat.c +++ b/src/backend/utils/adt/oracle_compat.c @@ -9,7 +9,7 @@ * * * IDENTIFICATION - * $PostgreSQL: pgsql/src/backend/utils/adt/oracle_compat.c,v 1.77 2008/01/01 19:45:52 momjian Exp $ + * $PostgreSQL: pgsql/src/backend/utils/adt/oracle_compat.c,v 1.78 2008/03/25 22:42:44 tgl Exp $ * *------------------------------------------------------------------------- */ @@ -76,9 +76,7 @@ texttowcs(const text *txt) errmsg("out of memory"))); /* Need a null-terminated version of the input */ - workstr = (char *) palloc(nbytes + 1); - memcpy(workstr, VARDATA_ANY(txt), nbytes); - workstr[nbytes] = '\0'; + workstr = text_to_cstring(txt); /* Output workspace cannot have more codes than input bytes */ result = (wchar_t *) palloc((nbytes + 1) * sizeof(wchar_t)); @@ -275,25 +273,16 @@ wstring_upper(char *str) text *in_text; text *out_text; char *result; - int nbytes = strlen(str); int i; - in_text = palloc(nbytes + VARHDRSZ); - memcpy(VARDATA(in_text), str, nbytes); - SET_VARSIZE(in_text, nbytes + VARHDRSZ); - + in_text = cstring_to_text(str); workspace = texttowcs(in_text); for (i = 0; workspace[i] != 0; i++) workspace[i] = towupper(workspace[i]); out_text = wcstotext(workspace, i); - - nbytes = VARSIZE(out_text) - VARHDRSZ; - result = palloc(nbytes + 1); - memcpy(result, VARDATA(out_text), nbytes); - - result[nbytes] = '\0'; + result = text_to_cstring(out_text); pfree(workspace); pfree(in_text); @@ -309,25 +298,16 @@ wstring_lower(char *str) text *in_text; text *out_text; char *result; - int nbytes = strlen(str); int i; - in_text = palloc(nbytes + VARHDRSZ); - memcpy(VARDATA(in_text), str, nbytes); - SET_VARSIZE(in_text, nbytes + VARHDRSZ); - + in_text = cstring_to_text(str); workspace = texttowcs(in_text); for (i = 0; workspace[i] != 0; i++) workspace[i] = towlower(workspace[i]); out_text = wcstotext(workspace, i); - - nbytes = VARSIZE(out_text) - VARHDRSZ; - result = palloc(nbytes + 1); - memcpy(result, VARDATA(out_text), nbytes); - - result[nbytes] = '\0'; + result = text_to_cstring(out_text); pfree(workspace); pfree(in_text); @@ -801,7 +781,6 @@ dotrim(const char *string, int stringlen, const char *set, int setlen, bool doltrim, bool dortrim) { - text *result; int i; /* Nothing to do if either string or set is empty */ @@ -947,11 +926,7 @@ dotrim(const char *string, int stringlen, } /* Return selected portion of string */ - result = (text *) palloc(VARHDRSZ + stringlen); - SET_VARSIZE(result, VARHDRSZ + stringlen); - memcpy(VARDATA(result), string, stringlen); - - return result; + return cstring_to_text_with_len(string, stringlen); } /******************************************************************** diff --git a/src/backend/utils/adt/pgstatfuncs.c b/src/backend/utils/adt/pgstatfuncs.c index 7fb2d5951ee..f4c047b7eda 100644 --- a/src/backend/utils/adt/pgstatfuncs.c +++ b/src/backend/utils/adt/pgstatfuncs.c @@ -8,7 +8,7 @@ * * * IDENTIFICATION - * $PostgreSQL: pgsql/src/backend/utils/adt/pgstatfuncs.c,v 1.48 2008/01/01 19:45:52 momjian Exp $ + * $PostgreSQL: pgsql/src/backend/utils/adt/pgstatfuncs.c,v 1.49 2008/03/25 22:42:44 tgl Exp $ * *------------------------------------------------------------------------- */ @@ -414,9 +414,7 @@ Datum pg_stat_get_backend_activity(PG_FUNCTION_ARGS) { int32 beid = PG_GETARG_INT32(0); - text *result; PgBackendStatus *beentry; - int len; const char *activity; if ((beentry = pgstat_fetch_stat_beentry(beid)) == NULL) @@ -428,12 +426,7 @@ pg_stat_get_backend_activity(PG_FUNCTION_ARGS) else activity = beentry->st_activity; - len = strlen(activity); - result = palloc(VARHDRSZ + len); - SET_VARSIZE(result, VARHDRSZ + len); - memcpy(VARDATA(result), activity, len); - - PG_RETURN_TEXT_P(result); + PG_RETURN_TEXT_P(cstring_to_text(activity)); } diff --git a/src/backend/utils/adt/quote.c b/src/backend/utils/adt/quote.c index 519c6d874b5..6074414c372 100644 --- a/src/backend/utils/adt/quote.c +++ b/src/backend/utils/adt/quote.c @@ -7,7 +7,7 @@ * * * IDENTIFICATION - * $PostgreSQL: pgsql/src/backend/utils/adt/quote.c,v 1.24 2008/03/23 00:24:19 tgl Exp $ + * $PostgreSQL: pgsql/src/backend/utils/adt/quote.c,v 1.25 2008/03/25 22:42:44 tgl Exp $ * *------------------------------------------------------------------------- */ @@ -23,26 +23,13 @@ Datum quote_ident(PG_FUNCTION_ARGS) { - text *t = PG_GETARG_TEXT_P(0); - text *result; + text *t = PG_GETARG_TEXT_PP(0); const char *qstr; char *str; - int len; - - /* We have to convert to a C string to use quote_identifier */ - len = VARSIZE(t) - VARHDRSZ; - str = (char *) palloc(len + 1); - memcpy(str, VARDATA(t), len); - str[len] = '\0'; + str = text_to_cstring(t); qstr = quote_identifier(str); - - len = strlen(qstr); - result = (text *) palloc(len + VARHDRSZ); - SET_VARSIZE(result, len + VARHDRSZ); - memcpy(VARDATA(result), qstr, len); - - PG_RETURN_TEXT_P(result); + PG_RETURN_TEXT_P(cstring_to_text(qstr)); } /* @@ -106,8 +93,7 @@ Datum quote_nullable(PG_FUNCTION_ARGS) { if (PG_ARGISNULL(0)) - PG_RETURN_DATUM(DirectFunctionCall1(textin, - CStringGetDatum("NULL"))); + PG_RETURN_TEXT_P(cstring_to_text("NULL")); else PG_RETURN_DATUM(DirectFunctionCall1(quote_literal, PG_GETARG_DATUM(0))); diff --git a/src/backend/utils/adt/ruleutils.c b/src/backend/utils/adt/ruleutils.c index db86d69efe5..940cfc2b23d 100644 --- a/src/backend/utils/adt/ruleutils.c +++ b/src/backend/utils/adt/ruleutils.c @@ -9,7 +9,7 @@ * * * IDENTIFICATION - * $PostgreSQL: pgsql/src/backend/utils/adt/ruleutils.c,v 1.269 2008/01/06 01:03:16 tgl Exp $ + * $PostgreSQL: pgsql/src/backend/utils/adt/ruleutils.c,v 1.270 2008/03/25 22:42:44 tgl Exp $ * *------------------------------------------------------------------------- */ @@ -697,8 +697,7 @@ pg_get_indexdef_worker(Oid indexrelid, int colno, bool showTblSpc, exprsDatum = SysCacheGetAttr(INDEXRELID, ht_idx, Anum_pg_index_indexprs, &isnull); Assert(!isnull); - exprsString = DatumGetCString(DirectFunctionCall1(textout, - exprsDatum)); + exprsString = TextDatumGetCString(exprsDatum); indexprs = (List *) stringToNode(exprsString); pfree(exprsString); } @@ -836,8 +835,7 @@ pg_get_indexdef_worker(Oid indexrelid, int colno, bool showTblSpc, predDatum = SysCacheGetAttr(INDEXRELID, ht_idx, Anum_pg_index_indpred, &isnull); Assert(!isnull); - predString = DatumGetCString(DirectFunctionCall1(textout, - predDatum)); + predString = TextDatumGetCString(predDatum); node = (Node *) stringToNode(predString); pfree(predString); @@ -1092,7 +1090,7 @@ pg_get_constraintdef_worker(Oid constraintId, bool fullCommand, elog(ERROR, "null conbin for constraint %u", constraintId); - conbin = DatumGetCString(DirectFunctionCall1(textout, val)); + conbin = TextDatumGetCString(val); expr = stringToNode(conbin); /* Set up deparsing context for Var nodes in constraint */ @@ -1222,8 +1220,7 @@ pg_get_expr_worker(text *expr, Oid relid, char *relname, int prettyFlags) char *str; /* Convert input TEXT object to C string */ - exprstr = DatumGetCString(DirectFunctionCall1(textout, - PointerGetDatum(expr))); + exprstr = text_to_cstring(expr); /* Convert expression to node tree */ node = (Node *) stringToNode(exprstr); @@ -1233,6 +1230,8 @@ pg_get_expr_worker(text *expr, Oid relid, char *relname, int prettyFlags) str = deparse_expression_pretty(node, context, false, false, prettyFlags, 0); + pfree(exprstr); + return str; } @@ -1286,7 +1285,7 @@ Datum pg_get_serial_sequence(PG_FUNCTION_ARGS) { text *tablename = PG_GETARG_TEXT_P(0); - text *columnname = PG_GETARG_TEXT_P(1); + text *columnname = PG_GETARG_TEXT_PP(1); RangeVar *tablerv; Oid tableOid; char *column; @@ -1302,8 +1301,7 @@ pg_get_serial_sequence(PG_FUNCTION_ARGS) tableOid = RangeVarGetRelid(tablerv, false); /* Get the number of the column */ - column = DatumGetCString(DirectFunctionCall1(textout, - PointerGetDatum(columnname))); + column = text_to_cstring(columnname); attnum = get_attnum(tableOid, column); if (attnum == InvalidAttrNumber) @@ -5439,16 +5437,9 @@ static text * string_to_text(char *str) { text *result; - int slen = strlen(str); - int tlen; - - tlen = slen + VARHDRSZ; - result = (text *) palloc(tlen); - SET_VARSIZE(result, tlen); - memcpy(VARDATA(result), str, slen); + result = cstring_to_text(str); pfree(str); - return result; } @@ -5482,9 +5473,9 @@ flatten_reloptions(Oid relid) * array_to_text() relies on flinfo to be valid. So use * OidFunctionCall2. */ - sep = DirectFunctionCall1(textin, CStringGetDatum(", ")); + sep = CStringGetTextDatum(", "); txt = OidFunctionCall2(F_ARRAY_TO_TEXT, reloptions, sep); - result = DatumGetCString(DirectFunctionCall1(textout, txt)); + result = TextDatumGetCString(txt); } ReleaseSysCache(tuple); diff --git a/src/backend/utils/adt/selfuncs.c b/src/backend/utils/adt/selfuncs.c index b6f1026375f..68ce6acb528 100644 --- a/src/backend/utils/adt/selfuncs.c +++ b/src/backend/utils/adt/selfuncs.c @@ -15,7 +15,7 @@ * * * IDENTIFICATION - * $PostgreSQL: pgsql/src/backend/utils/adt/selfuncs.c,v 1.246 2008/03/17 17:13:54 tgl Exp $ + * $PostgreSQL: pgsql/src/backend/utils/adt/selfuncs.c,v 1.247 2008/03/25 22:42:44 tgl Exp $ * *------------------------------------------------------------------------- */ @@ -1089,8 +1089,7 @@ patternsel(PG_FUNCTION_ARGS, Pattern_Type ptype, bool negate) switch (prefix->consttype) { case TEXTOID: - prefixstr = DatumGetCString(DirectFunctionCall1(textout, - prefix->constvalue)); + prefixstr = TextDatumGetCString(prefix->constvalue); break; case BYTEAOID: prefixstr = DatumGetCString(DirectFunctionCall1(byteaout, @@ -3339,15 +3338,8 @@ convert_string_datum(Datum value, Oid typid) case BPCHAROID: case VARCHAROID: case TEXTOID: - { - char *str = (char *) VARDATA(DatumGetPointer(value)); - int strlength = VARSIZE(DatumGetPointer(value)) - VARHDRSZ; - - val = (char *) palloc(strlength + 1); - memcpy(val, str, strlength); - val[strlength] = '\0'; - break; - } + val = TextDatumGetCString(value); + break; case NAMEOID: { NameData *nm = (NameData *) DatumGetPointer(value); @@ -4177,7 +4169,7 @@ like_fixed_prefix(Const *patt_const, bool case_insensitive, if (typeid != BYTEAOID) { - patt = DatumGetCString(DirectFunctionCall1(textout, patt_const->constvalue)); + patt = TextDatumGetCString(patt_const->constvalue); pattlen = strlen(patt); } else @@ -4282,7 +4274,7 @@ regex_fixed_prefix(Const *patt_const, bool case_insensitive, errmsg("regular-expression matching not supported on type bytea"))); /* the right-hand const is type text for all of these */ - patt = DatumGetCString(DirectFunctionCall1(textout, patt_const->constvalue)); + patt = TextDatumGetCString(patt_const->constvalue); /* * Check for ARE director prefix. It's worth our trouble to recognize @@ -4618,7 +4610,7 @@ like_selectivity(Const *patt_const, bool case_insensitive) if (typeid != BYTEAOID) { - patt = DatumGetCString(DirectFunctionCall1(textout, patt_const->constvalue)); + patt = TextDatumGetCString(patt_const->constvalue); pattlen = strlen(patt); } else @@ -4777,7 +4769,7 @@ regex_selectivity(Const *patt_const, bool case_insensitive) errmsg("regular-expression matching not supported on type bytea"))); /* the right-hand const is type text for all of these */ - patt = DatumGetCString(DirectFunctionCall1(textout, patt_const->constvalue)); + patt = TextDatumGetCString(patt_const->constvalue); pattlen = strlen(patt); /* If patt doesn't end with $, consider it to have a trailing wildcard */ @@ -4892,8 +4884,7 @@ make_greater_string(const Const *str_const, FmgrInfo *ltproc) } else { - workstr = DatumGetCString(DirectFunctionCall1(textout, - str_const->constvalue)); + workstr = TextDatumGetCString(str_const->constvalue); len = strlen(workstr); if (lc_collate_is_c() || len == 0) cmpstr = str_const->constvalue; @@ -5000,15 +4991,15 @@ string_to_datum(const char *str, Oid datatype) Assert(str != NULL); /* - * We cheat a little by assuming that textin() will do for bpchar and - * varchar constants too... + * We cheat a little by assuming that CStringGetTextDatum() will do for + * bpchar and varchar constants too... */ if (datatype == NAMEOID) return DirectFunctionCall1(namein, CStringGetDatum(str)); else if (datatype == BYTEAOID) return DirectFunctionCall1(byteain, CStringGetDatum(str)); else - return DirectFunctionCall1(textin, CStringGetDatum(str)); + return CStringGetTextDatum(str); } /* diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index 8ca6f998c98..8a0f37292dc 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -8,7 +8,7 @@ * * * IDENTIFICATION - * $PostgreSQL: pgsql/src/backend/utils/adt/timestamp.c,v 1.186 2008/03/21 01:31:42 tgl Exp $ + * $PostgreSQL: pgsql/src/backend/utils/adt/timestamp.c,v 1.187 2008/03/25 22:42:44 tgl Exp $ * *------------------------------------------------------------------------- */ @@ -3242,7 +3242,7 @@ timestamptz_age(PG_FUNCTION_ARGS) Datum timestamp_trunc(PG_FUNCTION_ARGS) { - text *units = PG_GETARG_TEXT_P(0); + text *units = PG_GETARG_TEXT_PP(0); Timestamp timestamp = PG_GETARG_TIMESTAMP(1); Timestamp result; int type, @@ -3255,8 +3255,8 @@ timestamp_trunc(PG_FUNCTION_ARGS) if (TIMESTAMP_NOT_FINITE(timestamp)) PG_RETURN_TIMESTAMP(timestamp); - lowunits = downcase_truncate_identifier(VARDATA(units), - VARSIZE(units) - VARHDRSZ, + lowunits = downcase_truncate_identifier(VARDATA_ANY(units), + VARSIZE_ANY_EXHDR(units), false); type = DecodeUnits(0, lowunits, &val); @@ -3374,7 +3374,7 @@ timestamp_trunc(PG_FUNCTION_ARGS) Datum timestamptz_trunc(PG_FUNCTION_ARGS) { - text *units = PG_GETARG_TEXT_P(0); + text *units = PG_GETARG_TEXT_PP(0); TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(1); TimestampTz result; int tz; @@ -3390,8 +3390,8 @@ timestamptz_trunc(PG_FUNCTION_ARGS) if (TIMESTAMP_NOT_FINITE(timestamp)) PG_RETURN_TIMESTAMPTZ(timestamp); - lowunits = downcase_truncate_identifier(VARDATA(units), - VARSIZE(units) - VARHDRSZ, + lowunits = downcase_truncate_identifier(VARDATA_ANY(units), + VARSIZE_ANY_EXHDR(units), false); type = DecodeUnits(0, lowunits, &val); @@ -3532,7 +3532,7 @@ timestamptz_trunc(PG_FUNCTION_ARGS) Datum interval_trunc(PG_FUNCTION_ARGS) { - text *units = PG_GETARG_TEXT_P(0); + text *units = PG_GETARG_TEXT_PP(0); Interval *interval = PG_GETARG_INTERVAL_P(1); Interval *result; int type, @@ -3544,8 +3544,8 @@ interval_trunc(PG_FUNCTION_ARGS) result = (Interval *) palloc(sizeof(Interval)); - lowunits = downcase_truncate_identifier(VARDATA(units), - VARSIZE(units) - VARHDRSZ, + lowunits = downcase_truncate_identifier(VARDATA_ANY(units), + VARSIZE_ANY_EXHDR(units), false); type = DecodeUnits(0, lowunits, &val); @@ -3615,9 +3615,7 @@ interval_trunc(PG_FUNCTION_ARGS) ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE), errmsg("interval units \"%s\" not recognized", - DatumGetCString(DirectFunctionCall1(textout, - PointerGetDatum(units)))))); - *result = *interval; + lowunits))); } PG_RETURN_INTERVAL_P(result); @@ -3803,7 +3801,7 @@ date2isoyearday(int year, int mon, int mday) Datum timestamp_part(PG_FUNCTION_ARGS) { - text *units = PG_GETARG_TEXT_P(0); + text *units = PG_GETARG_TEXT_PP(0); Timestamp timestamp = PG_GETARG_TIMESTAMP(1); float8 result; int type, @@ -3819,8 +3817,8 @@ timestamp_part(PG_FUNCTION_ARGS) PG_RETURN_FLOAT8(result); } - lowunits = downcase_truncate_identifier(VARDATA(units), - VARSIZE(units) - VARHDRSZ, + lowunits = downcase_truncate_identifier(VARDATA_ANY(units), + VARSIZE_ANY_EXHDR(units), false); type = DecodeUnits(0, lowunits, &val); @@ -4031,7 +4029,7 @@ timestamp_part(PG_FUNCTION_ARGS) Datum timestamptz_part(PG_FUNCTION_ARGS) { - text *units = PG_GETARG_TEXT_P(0); + text *units = PG_GETARG_TEXT_PP(0); TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(1); float8 result; int tz; @@ -4050,8 +4048,8 @@ timestamptz_part(PG_FUNCTION_ARGS) PG_RETURN_FLOAT8(result); } - lowunits = downcase_truncate_identifier(VARDATA(units), - VARSIZE(units) - VARHDRSZ, + lowunits = downcase_truncate_identifier(VARDATA_ANY(units), + VARSIZE_ANY_EXHDR(units), false); type = DecodeUnits(0, lowunits, &val); @@ -4246,7 +4244,7 @@ timestamptz_part(PG_FUNCTION_ARGS) Datum interval_part(PG_FUNCTION_ARGS) { - text *units = PG_GETARG_TEXT_P(0); + text *units = PG_GETARG_TEXT_PP(0); Interval *interval = PG_GETARG_INTERVAL_P(1); float8 result; int type, @@ -4256,8 +4254,8 @@ interval_part(PG_FUNCTION_ARGS) struct pg_tm tt, *tm = &tt; - lowunits = downcase_truncate_identifier(VARDATA(units), - VARSIZE(units) - VARHDRSZ, + lowunits = downcase_truncate_identifier(VARDATA_ANY(units), + VARSIZE_ANY_EXHDR(units), false); type = DecodeUnits(0, lowunits, &val); @@ -4337,8 +4335,7 @@ interval_part(PG_FUNCTION_ARGS) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), errmsg("interval units \"%s\" not supported", - DatumGetCString(DirectFunctionCall1(textout, - PointerGetDatum(units)))))); + lowunits))); result = 0; } @@ -4365,8 +4362,7 @@ interval_part(PG_FUNCTION_ARGS) ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE), errmsg("interval units \"%s\" not recognized", - DatumGetCString(DirectFunctionCall1(textout, - PointerGetDatum(units)))))); + lowunits))); result = 0; } @@ -4385,13 +4381,12 @@ interval_part(PG_FUNCTION_ARGS) Datum timestamp_zone(PG_FUNCTION_ARGS) { - text *zone = PG_GETARG_TEXT_P(0); + text *zone = PG_GETARG_TEXT_PP(0); Timestamp timestamp = PG_GETARG_TIMESTAMP(1); TimestampTz result; int tz; pg_tz *tzp; char tzname[TZ_STRLEN_MAX + 1]; - int len; if (TIMESTAMP_NOT_FINITE(timestamp)) PG_RETURN_TIMESTAMPTZ(timestamp); @@ -4401,9 +4396,7 @@ timestamp_zone(PG_FUNCTION_ARGS) * (to handle cases like "America/New_York"), and if that fails, we look * in the date token table (to handle cases like "EST"). */ - len = Min(VARSIZE(zone) - VARHDRSZ, TZ_STRLEN_MAX); - memcpy(tzname, VARDATA(zone), len); - tzname[len] = '\0'; + text_to_cstring_buffer(zone, tzname, sizeof(tzname)); tzp = pg_tzset(tzname); if (tzp) { @@ -4428,8 +4421,8 @@ timestamp_zone(PG_FUNCTION_ARGS) int type, val; - lowzone = downcase_truncate_identifier(VARDATA(zone), - VARSIZE(zone) - VARHDRSZ, + lowzone = downcase_truncate_identifier(tzname, + strlen(tzname), false); type = DecodeSpecial(0, lowzone, &val); @@ -4558,13 +4551,12 @@ timestamptz_timestamp(PG_FUNCTION_ARGS) Datum timestamptz_zone(PG_FUNCTION_ARGS) { - text *zone = PG_GETARG_TEXT_P(0); + text *zone = PG_GETARG_TEXT_PP(0); TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(1); Timestamp result; int tz; pg_tz *tzp; char tzname[TZ_STRLEN_MAX + 1]; - int len; if (TIMESTAMP_NOT_FINITE(timestamp)) PG_RETURN_TIMESTAMP(timestamp); @@ -4574,9 +4566,7 @@ timestamptz_zone(PG_FUNCTION_ARGS) * (to handle cases like "America/New_York"), and if that fails, we look * in the date token table (to handle cases like "EST"). */ - len = Min(VARSIZE(zone) - VARHDRSZ, TZ_STRLEN_MAX); - memcpy(tzname, VARDATA(zone), len); - tzname[len] = '\0'; + text_to_cstring_buffer(zone, tzname, sizeof(tzname)); tzp = pg_tzset(tzname); if (tzp) { @@ -4600,8 +4590,8 @@ timestamptz_zone(PG_FUNCTION_ARGS) int type, val; - lowzone = downcase_truncate_identifier(VARDATA(zone), - VARSIZE(zone) - VARHDRSZ, + lowzone = downcase_truncate_identifier(tzname, + strlen(tzname), false); type = DecodeSpecial(0, lowzone, &val); diff --git a/src/backend/utils/adt/tsginidx.c b/src/backend/utils/adt/tsginidx.c index 96510021f6d..add1fc1910c 100644 --- a/src/backend/utils/adt/tsginidx.c +++ b/src/backend/utils/adt/tsginidx.c @@ -7,7 +7,7 @@ * * * IDENTIFICATION - * $PostgreSQL: pgsql/src/backend/utils/adt/tsginidx.c,v 1.9 2008/01/01 19:45:52 momjian Exp $ + * $PostgreSQL: pgsql/src/backend/utils/adt/tsginidx.c,v 1.10 2008/03/25 22:42:44 tgl Exp $ * *------------------------------------------------------------------------- */ @@ -16,6 +16,7 @@ #include "access/skey.h" #include "tsearch/ts_type.h" #include "tsearch/ts_utils.h" +#include "utils/builtins.h" Datum @@ -35,11 +36,9 @@ gin_extract_tsvector(PG_FUNCTION_ARGS) for (i = 0; i < vector->size; i++) { - text *txt = (text *) palloc(VARHDRSZ + we->len); - - SET_VARSIZE(txt, VARHDRSZ + we->len); - memcpy(VARDATA(txt), STRPTR(vector) + we->pos, we->len); + text *txt; + txt = cstring_to_text_with_len(STRPTR(vector) + we->pos, we->len); entries[i] = PointerGetDatum(txt); we++; @@ -87,11 +86,8 @@ gin_extract_tsquery(PG_FUNCTION_ARGS) text *txt; QueryOperand *val = &item[i].operand; - txt = (text *) palloc(VARHDRSZ + val->length); - - SET_VARSIZE(txt, VARHDRSZ + val->length); - memcpy(VARDATA(txt), GETOPERAND(query) + val->distance, val->length); - + txt = cstring_to_text_with_len(GETOPERAND(query) + val->distance, + val->length); entries[j++] = PointerGetDatum(txt); if (strategy != TSearchWithClassStrategyNumber && val->weight != 0) diff --git a/src/backend/utils/adt/tsquery.c b/src/backend/utils/adt/tsquery.c index 41863fb5db5..d84d4ad931e 100644 --- a/src/backend/utils/adt/tsquery.c +++ b/src/backend/utils/adt/tsquery.c @@ -7,7 +7,7 @@ * * * IDENTIFICATION - * $PostgreSQL: pgsql/src/backend/utils/adt/tsquery.c,v 1.15 2008/01/08 01:04:08 tgl Exp $ + * $PostgreSQL: pgsql/src/backend/utils/adt/tsquery.c,v 1.16 2008/03/25 22:42:44 tgl Exp $ * *------------------------------------------------------------------------- */ @@ -19,6 +19,7 @@ #include "tsearch/ts_locale.h" #include "tsearch/ts_type.h" #include "tsearch/ts_utils.h" +#include "utils/builtins.h" #include "utils/memutils.h" #include "utils/pg_crc.h" @@ -954,9 +955,7 @@ tsquerytree(PG_FUNCTION_ARGS) if (!q) { - res = (text *) palloc(1 + VARHDRSZ); - SET_VARSIZE(res, 1 + VARHDRSZ); - *((char *) VARDATA(res)) = 'T'; + res = cstring_to_text("T"); } else { @@ -966,14 +965,11 @@ tsquerytree(PG_FUNCTION_ARGS) *(nrm.cur) = '\0'; nrm.op = GETOPERAND(query); infix(&nrm, true); - - res = (text *) palloc(nrm.cur - nrm.buf + VARHDRSZ); - SET_VARSIZE(res, nrm.cur - nrm.buf + VARHDRSZ); - strncpy(VARDATA(res), nrm.buf, nrm.cur - nrm.buf); + res = cstring_to_text_with_len(nrm.buf, nrm.cur - nrm.buf); pfree(q); } PG_FREE_IF_COPY(query, 0); - PG_RETURN_POINTER(res); + PG_RETURN_TEXT_P(res); } diff --git a/src/backend/utils/adt/tsquery_rewrite.c b/src/backend/utils/adt/tsquery_rewrite.c index 89f11731dd4..df917642062 100644 --- a/src/backend/utils/adt/tsquery_rewrite.c +++ b/src/backend/utils/adt/tsquery_rewrite.c @@ -7,7 +7,7 @@ * * * IDENTIFICATION - * $PostgreSQL: pgsql/src/backend/utils/adt/tsquery_rewrite.c,v 1.11 2008/01/01 19:45:53 momjian Exp $ + * $PostgreSQL: pgsql/src/backend/utils/adt/tsquery_rewrite.c,v 1.12 2008/03/25 22:42:44 tgl Exp $ * *------------------------------------------------------------------------- */ @@ -273,7 +273,7 @@ tsquery_rewrite_query(PG_FUNCTION_ARGS) QTNTernary(tree); QTNSort(tree); - buf = TextPGetCString(in); + buf = text_to_cstring(in); SPI_connect(); diff --git a/src/backend/utils/adt/tsvector_op.c b/src/backend/utils/adt/tsvector_op.c index eee15a673dd..0624c00ea91 100644 --- a/src/backend/utils/adt/tsvector_op.c +++ b/src/backend/utils/adt/tsvector_op.c @@ -7,7 +7,7 @@ * * * IDENTIFICATION - * $PostgreSQL: pgsql/src/backend/utils/adt/tsvector_op.c,v 1.13 2008/03/05 15:50:37 momjian Exp $ + * $PostgreSQL: pgsql/src/backend/utils/adt/tsvector_op.c,v 1.14 2008/03/25 22:42:44 tgl Exp $ * *------------------------------------------------------------------------- */ @@ -1082,7 +1082,7 @@ ts_process_call(FuncCallContext *funcctx) static tsstat * ts_stat_sql(text *txt, text *ws) { - char *query = TextPGetCString(txt); + char *query = text_to_cstring(txt); int i; tsstat *newstat, *stat; diff --git a/src/backend/utils/adt/varchar.c b/src/backend/utils/adt/varchar.c index c954e2034ad..49ce0ef4d69 100644 --- a/src/backend/utils/adt/varchar.c +++ b/src/backend/utils/adt/varchar.c @@ -8,7 +8,7 @@ * * * IDENTIFICATION - * $PostgreSQL: pgsql/src/backend/utils/adt/varchar.c,v 1.126 2008/01/01 19:45:53 momjian Exp $ + * $PostgreSQL: pgsql/src/backend/utils/adt/varchar.c,v 1.127 2008/03/25 22:42:44 tgl Exp $ * *------------------------------------------------------------------------- */ @@ -203,21 +203,16 @@ bpcharin(PG_FUNCTION_ARGS) /* * Convert a CHARACTER value to a C string. + * + * Uses the text conversion functions, which is only appropriate if BpChar + * and text are equivalent types. */ Datum bpcharout(PG_FUNCTION_ARGS) { - BpChar *s = PG_GETARG_BPCHAR_PP(0); - char *result; - int len; + Datum txt = PG_GETARG_DATUM(0); - /* copy and add null term */ - len = VARSIZE_ANY_EXHDR(s); - result = (char *) palloc(len + 1); - memcpy(result, VARDATA_ANY(s), len); - result[len] = '\0'; - - PG_RETURN_CSTRING(result); + PG_RETURN_CSTRING(TextDatumGetCString(txt)); } /* @@ -403,19 +398,17 @@ bpchar_name(PG_FUNCTION_ARGS) /* name_bpchar() * Converts a NameData type to a bpchar type. + * + * Uses the text conversion functions, which is only appropriate if BpChar + * and text are equivalent types. */ Datum name_bpchar(PG_FUNCTION_ARGS) { Name s = PG_GETARG_NAME(0); BpChar *result; - int len; - - len = strlen(NameStr(*s)); - result = (BpChar *) palloc(VARHDRSZ + len); - memcpy(VARDATA(result), NameStr(*s), len); - SET_VARSIZE(result, VARHDRSZ + len); + result = (BpChar *) cstring_to_text(NameStr(*s)); PG_RETURN_BPCHAR_P(result); } @@ -454,6 +447,9 @@ bpchartypmodout(PG_FUNCTION_ARGS) * * If the input string is too long, raise an error, unless the extra * characters are spaces, in which case they're truncated. (per SQL) + * + * Uses the C string to text conversion function, which is only appropriate + * if VarChar and text are equivalent types. */ static VarChar * varchar_input(const char *s, size_t len, int32 atttypmod) @@ -481,10 +477,7 @@ varchar_input(const char *s, size_t len, int32 atttypmod) len = mbmaxlen; } - result = (VarChar *) palloc(len + VARHDRSZ); - SET_VARSIZE(result, len + VARHDRSZ); - memcpy(VARDATA(result), s, len); - + result = (VarChar *) cstring_to_text_with_len(s, len); return result; } @@ -510,21 +503,16 @@ varcharin(PG_FUNCTION_ARGS) /* * Convert a VARCHAR value to a C string. + * + * Uses the text to C string conversion function, which is only appropriate + * if VarChar and text are equivalent types. */ Datum varcharout(PG_FUNCTION_ARGS) { - VarChar *s = PG_GETARG_VARCHAR_PP(0); - char *result; - int32 len; - - /* copy and add null term */ - len = VARSIZE_ANY_EXHDR(s); - result = palloc(len + 1); - memcpy(result, VARDATA_ANY(s), len); - result[len] = '\0'; + Datum txt = PG_GETARG_DATUM(0); - PG_RETURN_CSTRING(result); + PG_RETURN_CSTRING(TextDatumGetCString(txt)); } /* diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 75832856b6c..aa56317c7ff 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -8,7 +8,7 @@ * * * IDENTIFICATION - * $PostgreSQL: pgsql/src/backend/utils/adt/varlena.c,v 1.163 2008/03/13 18:31:56 tgl Exp $ + * $PostgreSQL: pgsql/src/backend/utils/adt/varlena.c,v 1.164 2008/03/25 22:42:44 tgl Exp $ * *------------------------------------------------------------------------- */ @@ -48,15 +48,6 @@ typedef struct #define PG_GETARG_UNKNOWN_P_COPY(n) DatumGetUnknownPCopy(PG_GETARG_DATUM(n)) #define PG_RETURN_UNKNOWN_P(x) PG_RETURN_POINTER(x) -#define PG_TEXTARG_GET_STR(arg_) \ - DatumGetCString(DirectFunctionCall1(textout, PG_GETARG_DATUM(arg_))) -#define PG_TEXT_GET_STR(textp_) \ - DatumGetCString(DirectFunctionCall1(textout, PointerGetDatum(textp_))) -#define PG_STR_GET_TEXT(str_) \ - DatumGetTextP(DirectFunctionCall1(textin, CStringGetDatum(str_))) -#define TEXTLEN(textp) \ - text_length(PointerGetDatum(textp)) - static int text_cmp(text *arg1, text *arg2); static int32 text_length(Datum str); static int text_position(text *t1, text *t2); @@ -67,11 +58,108 @@ static text *text_substring(Datum str, int32 start, int32 length, bool length_not_specified); - static void appendStringInfoText(StringInfo str, const text *t); /***************************************************************************** + * CONVERSION ROUTINES EXPORTED FOR USE BY C CODE * + *****************************************************************************/ + +/* + * cstring_to_text + * + * Create a text value from a null-terminated C string. + * + * The new text value is freshly palloc'd with a full-size VARHDR. + */ +text * +cstring_to_text(const char *s) +{ + return cstring_to_text_with_len(s, strlen(s)); +} + +/* + * cstring_to_text_with_len + * + * Same as cstring_to_text except the caller specifies the string length; + * the string need not be null_terminated. + */ +text * +cstring_to_text_with_len(const char *s, int len) +{ + text *result = (text *) palloc(len + VARHDRSZ); + + SET_VARSIZE(result, len + VARHDRSZ); + memcpy(VARDATA(result), s, len); + + return result; +} + +/* + * text_to_cstring + * + * Create a palloc'd, null-terminated C string from a text value. + * + * We support being passed a compressed or toasted text value. + * This is a bit bogus since such values shouldn't really be referred to as + * "text *", but it seems useful for robustness. If we didn't handle that + * case here, we'd need another routine that did, anyway. + */ +char * +text_to_cstring(const text *t) +{ + /* must cast away the const, unfortunately */ + text *tunpacked = pg_detoast_datum_packed((struct varlena *) t); + int len = VARSIZE_ANY_EXHDR(tunpacked); + char *result; + + result = (char *) palloc(len + 1); + memcpy(result, VARDATA_ANY(tunpacked), len); + result[len] = '\0'; + + if (tunpacked != t) + pfree(tunpacked); + + return result; +} + +/* + * text_to_cstring_buffer + * + * Copy a text value into a caller-supplied buffer of size dst_len. + * + * The text string is truncated if necessary to fit. The result is + * guaranteed null-terminated (unless dst_len == 0). + * + * We support being passed a compressed or toasted text value. + * This is a bit bogus since such values shouldn't really be referred to as + * "text *", but it seems useful for robustness. If we didn't handle that + * case here, we'd need another routine that did, anyway. + */ +void +text_to_cstring_buffer(const text *src, char *dst, size_t dst_len) +{ + /* must cast away the const, unfortunately */ + text *srcunpacked = pg_detoast_datum_packed((struct varlena *) src); + size_t src_len = VARSIZE_ANY_EXHDR(srcunpacked); + + if (dst_len > 0) + { + dst_len--; + if (dst_len >= src_len) + dst_len = src_len; + else /* ensure truncation is encoding-safe */ + dst_len = pg_mbcliplen(VARDATA_ANY(srcunpacked), src_len, dst_len); + memcpy(dst, VARDATA_ANY(srcunpacked), dst_len); + dst[dst_len] = '\0'; + } + + if (srcunpacked != src) + pfree(srcunpacked); +} + + +/***************************************************************************** * USER I/O ROUTINES * *****************************************************************************/ @@ -259,16 +347,8 @@ Datum textin(PG_FUNCTION_ARGS) { char *inputText = PG_GETARG_CSTRING(0); - text *result; - int len; - - len = strlen(inputText); - result = (text *) palloc(len + VARHDRSZ); - SET_VARSIZE(result, len + VARHDRSZ); - memcpy(VARDATA(result), inputText, len); - - PG_RETURN_TEXT_P(result); + PG_RETURN_TEXT_P(cstring_to_text(inputText)); } /* @@ -277,16 +357,9 @@ textin(PG_FUNCTION_ARGS) Datum textout(PG_FUNCTION_ARGS) { - text *t = PG_GETARG_TEXT_PP(0); - int len; - char *result; + Datum txt = PG_GETARG_DATUM(0); - len = VARSIZE_ANY_EXHDR(t); - result = (char *) palloc(len + 1); - memcpy(result, VARDATA_ANY(t), len); - result[len] = '\0'; - - PG_RETURN_CSTRING(result); + PG_RETURN_CSTRING(TextDatumGetCString(txt)); } /* @@ -302,9 +375,7 @@ textrecv(PG_FUNCTION_ARGS) str = pq_getmsgtext(buf, buf->len - buf->cursor, &nbytes); - result = (text *) palloc(nbytes + VARHDRSZ); - SET_VARSIZE(result, nbytes + VARHDRSZ); - memcpy(VARDATA(result), str, nbytes); + result = cstring_to_text_with_len(str, nbytes); pfree(str); PG_RETURN_TEXT_P(result); } @@ -600,7 +671,7 @@ text_substring(Datum str, int32 start, int32 length, bool length_not_specified) * string. */ if (E < 1) - return PG_STR_GET_TEXT(""); + return cstring_to_text(""); L1 = E - S1; } @@ -664,7 +735,7 @@ text_substring(Datum str, int32 start, int32 length, bool length_not_specified) * string. */ if (E < 1) - return PG_STR_GET_TEXT(""); + return cstring_to_text(""); /* * if E is past the end of the string, the tuple toaster will @@ -693,7 +764,7 @@ text_substring(Datum str, int32 start, int32 length, bool length_not_specified) { if (slice != (text *) DatumGetPointer(str)) pfree(slice); - return PG_STR_GET_TEXT(""); + return cstring_to_text(""); } /* Now we can get the actual length of the slice in MB characters */ @@ -708,7 +779,7 @@ text_substring(Datum str, int32 start, int32 length, bool length_not_specified) { if (slice != (text *) DatumGetPointer(str)) pfree(slice); - return PG_STR_GET_TEXT(""); + return cstring_to_text(""); } /* @@ -1759,16 +1830,8 @@ Datum name_text(PG_FUNCTION_ARGS) { Name s = PG_GETARG_NAME(0); - text *result; - int len; - - len = strlen(NameStr(*s)); - - result = palloc(VARHDRSZ + len); - SET_VARSIZE(result, VARHDRSZ + len); - memcpy(VARDATA(result), NameStr(*s), len); - PG_RETURN_TEXT_P(result); + PG_RETURN_TEXT_P(cstring_to_text(NameStr(*s))); } @@ -1790,8 +1853,7 @@ textToQualifiedNameList(text *textval) /* Convert to C string (handles possible detoasting). */ /* Note we rely on being able to modify rawname below. */ - rawname = DatumGetCString(DirectFunctionCall1(textout, - PointerGetDatum(textval))); + rawname = text_to_cstring(textval); if (!SplitIdentifierString(rawname, '.', &namelist)) ereport(ERROR, @@ -2103,7 +2165,7 @@ byteacmp(PG_FUNCTION_ARGS) * appendStringInfoText * * Append a text to str. - * Like appendStringInfoString(str, PG_TEXT_GET_STR(s)) but faster. + * Like appendStringInfoString(str, text_to_cstring(t)) but faster. */ static void appendStringInfoText(StringInfo str, const text *t) @@ -2191,7 +2253,7 @@ replace_text(PG_FUNCTION_ARGS) text_position_cleanup(&state); - ret_text = PG_STR_GET_TEXT(str.data); + ret_text = cstring_to_text_with_len(str.data, str.len); pfree(str.data); PG_RETURN_TEXT_P(ret_text); @@ -2458,7 +2520,7 @@ replace_text_regexp(text *src_text, void *regexp, appendBinaryStringInfo(&buf, start_ptr, chunk_len); } - ret_text = PG_STR_GET_TEXT(buf.data); + ret_text = cstring_to_text_with_len(buf.data, buf.len); pfree(buf.data); pfree(data); @@ -2503,7 +2565,7 @@ split_text(PG_FUNCTION_ARGS) if (inputstring_len < 1) { text_position_cleanup(&state); - PG_RETURN_TEXT_P(PG_STR_GET_TEXT("")); + PG_RETURN_TEXT_P(cstring_to_text("")); } /* empty field separator */ @@ -2514,7 +2576,7 @@ split_text(PG_FUNCTION_ARGS) if (fldnum == 1) PG_RETURN_TEXT_P(inputstring); else - PG_RETURN_TEXT_P(PG_STR_GET_TEXT("")); + PG_RETURN_TEXT_P(cstring_to_text("")); } /* identify bounds of first field */ @@ -2529,7 +2591,7 @@ split_text(PG_FUNCTION_ARGS) if (fldnum == 1) PG_RETURN_TEXT_P(inputstring); else - PG_RETURN_TEXT_P(PG_STR_GET_TEXT("")); + PG_RETURN_TEXT_P(cstring_to_text("")); } while (end_posn > 0 && --fldnum > 0) @@ -2551,7 +2613,7 @@ split_text(PG_FUNCTION_ARGS) -1, true); else - result_text = PG_STR_GET_TEXT(""); + result_text = cstring_to_text(""); } else { @@ -2636,9 +2698,7 @@ text_to_array(PG_FUNCTION_ARGS) } /* must build a temp text datum to pass to accumArrayResult */ - result_text = (text *) palloc(VARHDRSZ + chunk_len); - SET_VARSIZE(result_text, VARHDRSZ + chunk_len); - memcpy(VARDATA(result_text), start_ptr, chunk_len); + result_text = cstring_to_text_with_len(start_ptr, chunk_len); /* stash away this field */ astate = accumArrayResult(astate, @@ -2673,7 +2733,7 @@ Datum array_to_text(PG_FUNCTION_ARGS) { ArrayType *v = PG_GETARG_ARRAYTYPE_P(0); - char *fldsep = PG_TEXTARG_GET_STR(1); + char *fldsep = text_to_cstring(PG_GETARG_TEXT_PP(1)); int nitems, *dims, ndims; @@ -2695,7 +2755,7 @@ array_to_text(PG_FUNCTION_ARGS) /* if there are no elements, return an empty string */ if (nitems == 0) - PG_RETURN_TEXT_P(PG_STR_GET_TEXT("")); + PG_RETURN_TEXT_P(cstring_to_text("")); element_type = ARR_ELEMTYPE(v); initStringInfo(&buf); @@ -2773,7 +2833,7 @@ array_to_text(PG_FUNCTION_ARGS) } } - PG_RETURN_TEXT_P(PG_STR_GET_TEXT(buf.data)); + PG_RETURN_TEXT_P(cstring_to_text_with_len(buf.data, buf.len)); } #define HEXBASE 16 @@ -2785,7 +2845,6 @@ Datum to_hex32(PG_FUNCTION_ARGS) { uint32 value = (uint32) PG_GETARG_INT32(0); - text *result_text; char *ptr; const char *digits = "0123456789abcdef"; char buf[32]; /* bigger than needed, but reasonable */ @@ -2799,8 +2858,7 @@ to_hex32(PG_FUNCTION_ARGS) value /= HEXBASE; } while (ptr > buf && value); - result_text = PG_STR_GET_TEXT(ptr); - PG_RETURN_TEXT_P(result_text); + PG_RETURN_TEXT_P(cstring_to_text(ptr)); } /* @@ -2811,7 +2869,6 @@ Datum to_hex64(PG_FUNCTION_ARGS) { uint64 value = (uint64) PG_GETARG_INT64(0); - text *result_text; char *ptr; const char *digits = "0123456789abcdef"; char buf[32]; /* bigger than needed, but reasonable */ @@ -2825,8 +2882,7 @@ to_hex64(PG_FUNCTION_ARGS) value /= HEXBASE; } while (ptr > buf && value); - result_text = PG_STR_GET_TEXT(ptr); - PG_RETURN_TEXT_P(result_text); + PG_RETURN_TEXT_P(cstring_to_text(ptr)); } /* @@ -2842,7 +2898,6 @@ md5_text(PG_FUNCTION_ARGS) text *in_text = PG_GETARG_TEXT_PP(0); size_t len; char hexsum[MD5_HASH_LEN + 1]; - text *result_text; /* Calculate the length of the buffer using varlena metadata */ len = VARSIZE_ANY_EXHDR(in_text); @@ -2854,8 +2909,7 @@ md5_text(PG_FUNCTION_ARGS) errmsg("out of memory"))); /* convert to text and return it */ - result_text = PG_STR_GET_TEXT(hexsum); - PG_RETURN_TEXT_P(result_text); + PG_RETURN_TEXT_P(cstring_to_text(hexsum)); } /* @@ -2868,7 +2922,6 @@ md5_bytea(PG_FUNCTION_ARGS) bytea *in = PG_GETARG_BYTEA_PP(0); size_t len; char hexsum[MD5_HASH_LEN + 1]; - text *result_text; len = VARSIZE_ANY_EXHDR(in); if (pg_md5_hash(VARDATA_ANY(in), len, hexsum) == false) @@ -2876,8 +2929,7 @@ md5_bytea(PG_FUNCTION_ARGS) (errcode(ERRCODE_OUT_OF_MEMORY), errmsg("out of memory"))); - result_text = PG_STR_GET_TEXT(hexsum); - PG_RETURN_TEXT_P(result_text); + PG_RETURN_TEXT_P(cstring_to_text(hexsum)); } /* diff --git a/src/backend/utils/adt/version.c b/src/backend/utils/adt/version.c index 0ffe0da0429..1a2619c4c6e 100644 --- a/src/backend/utils/adt/version.c +++ b/src/backend/utils/adt/version.c @@ -7,7 +7,7 @@ * * IDENTIFICATION * - * $PostgreSQL: pgsql/src/backend/utils/adt/version.c,v 1.16 2008/01/01 19:45:53 momjian Exp $ + * $PostgreSQL: pgsql/src/backend/utils/adt/version.c,v 1.17 2008/03/25 22:42:44 tgl Exp $ * *------------------------------------------------------------------------- */ @@ -20,11 +20,5 @@ Datum pgsql_version(PG_FUNCTION_ARGS) { - int n = strlen(PG_VERSION_STR); - text *ret = (text *) palloc(n + VARHDRSZ); - - SET_VARSIZE(ret, n + VARHDRSZ); - memcpy(VARDATA(ret), PG_VERSION_STR, n); - - PG_RETURN_TEXT_P(ret); + PG_RETURN_TEXT_P(cstring_to_text(PG_VERSION_STR)); } diff --git a/src/backend/utils/adt/xml.c b/src/backend/utils/adt/xml.c index f0a668b2927..c4960bcecfb 100644 --- a/src/backend/utils/adt/xml.c +++ b/src/backend/utils/adt/xml.c @@ -7,7 +7,7 @@ * Portions Copyright (c) 1996-2008, PostgreSQL Global Development Group * Portions Copyright (c) 1994, Regents of the University of California * - * $PostgreSQL: pgsql/src/backend/utils/adt/xml.c,v 1.70 2008/03/24 19:12:49 tgl Exp $ + * $PostgreSQL: pgsql/src/backend/utils/adt/xml.c,v 1.71 2008/03/25 22:42:44 tgl Exp $ * *------------------------------------------------------------------------- */ @@ -141,10 +141,6 @@ static void SPI_sql_row_to_xmlelement(int rownum, StringInfo result, errhint("You need to rebuild PostgreSQL using --with-libxml."))) -#define _textin(str) DirectFunctionCall1(textin, CStringGetDatum(str)) -#define _textout(x) DatumGetCString(DirectFunctionCall1(textout, PointerGetDatum(x))) - - /* from SQL/XML:2003 section 4.7 */ #define NAMESPACE_XSD "http://www.w3.org/2001/XMLSchema" #define NAMESPACE_XSI "http://www.w3.org/2001/XMLSchema-instance" @@ -168,19 +164,22 @@ xmlChar_to_encoding(xmlChar * encoding_name) #endif +/* + * xml_in uses a plain C string to VARDATA conversion, so for the time being + * we use the conversion function for the text datatype. + * + * This is only acceptable so long as xmltype and text use the same + * representation. + */ Datum xml_in(PG_FUNCTION_ARGS) { #ifdef USE_LIBXML char *s = PG_GETARG_CSTRING(0); - size_t len; xmltype *vardata; xmlDocPtr doc; - len = strlen(s); - vardata = palloc(len + VARHDRSZ); - SET_VARSIZE(vardata, len + VARHDRSZ); - memcpy(VARDATA(vardata), s, len); + vardata = (xmltype *) cstring_to_text(s); /* * Parse the data to check if it is well-formed XML data. Assume that @@ -200,6 +199,13 @@ xml_in(PG_FUNCTION_ARGS) #define PG_XML_DEFAULT_VERSION "1.0" +/* + * xml_out_internal uses a plain VARDATA to C string conversion, so for the + * time being we use the conversion function for the text datatype. + * + * This is only acceptable so long as xmltype and text use the same + * representation. + */ static char * xml_out_internal(xmltype *x, pg_enc target_encoding) { @@ -213,10 +219,8 @@ xml_out_internal(xmltype *x, pg_enc target_encoding) int res_code; #endif - len = VARSIZE(x) - VARHDRSZ; - str = palloc(len + 1); - memcpy(str, VARDATA(x), len); - str[len] = '\0'; + str = text_to_cstring((text *) x); + len = strlen(str); #ifdef USE_LIBXML if ((res_code = parse_xml_decl((xmlChar *) str, @@ -713,7 +717,7 @@ xmlpi(char *target, text *arg, bool arg_is_null, bool *result_is_null) { char *string; - string = _textout(arg); + string = text_to_cstring(arg); if (strstr(string, "?>") != NULL) ereport(ERROR, (errcode(ERRCODE_INVALID_XML_PROCESSING_INSTRUCTION), @@ -1930,7 +1934,7 @@ table_to_xml(PG_FUNCTION_ARGS) Oid relid = PG_GETARG_OID(0); bool nulls = PG_GETARG_BOOL(1); bool tableforest = PG_GETARG_BOOL(2); - const char *targetns = _textout(PG_GETARG_TEXT_P(3)); + const char *targetns = text_to_cstring(PG_GETARG_TEXT_PP(3)); PG_RETURN_XML_P(stringinfo_to_xmltype(table_to_xml_internal(relid, NULL, nulls, tableforest, @@ -1941,10 +1945,10 @@ table_to_xml(PG_FUNCTION_ARGS) Datum query_to_xml(PG_FUNCTION_ARGS) { - char *query = _textout(PG_GETARG_TEXT_P(0)); + char *query = text_to_cstring(PG_GETARG_TEXT_PP(0)); bool nulls = PG_GETARG_BOOL(1); bool tableforest = PG_GETARG_BOOL(2); - const char *targetns = _textout(PG_GETARG_TEXT_P(3)); + const char *targetns = text_to_cstring(PG_GETARG_TEXT_PP(3)); PG_RETURN_XML_P(stringinfo_to_xmltype(query_to_xml_internal(query, NULL, NULL, nulls, tableforest, @@ -1955,11 +1959,11 @@ query_to_xml(PG_FUNCTION_ARGS) Datum cursor_to_xml(PG_FUNCTION_ARGS) { - char *name = _textout(PG_GETARG_TEXT_P(0)); + char *name = text_to_cstring(PG_GETARG_TEXT_PP(0)); int32 count = PG_GETARG_INT32(1); bool nulls = PG_GETARG_BOOL(2); bool tableforest = PG_GETARG_BOOL(3); - const char *targetns = _textout(PG_GETARG_TEXT_P(4)); + const char *targetns = text_to_cstring(PG_GETARG_TEXT_PP(4)); StringInfoData result; Portal portal; @@ -2079,7 +2083,7 @@ table_to_xmlschema(PG_FUNCTION_ARGS) Oid relid = PG_GETARG_OID(0); bool nulls = PG_GETARG_BOOL(1); bool tableforest = PG_GETARG_BOOL(2); - const char *targetns = _textout(PG_GETARG_TEXT_P(3)); + const char *targetns = text_to_cstring(PG_GETARG_TEXT_PP(3)); const char *result; Relation rel; @@ -2095,10 +2099,10 @@ table_to_xmlschema(PG_FUNCTION_ARGS) Datum query_to_xmlschema(PG_FUNCTION_ARGS) { - char *query = _textout(PG_GETARG_TEXT_P(0)); + char *query = text_to_cstring(PG_GETARG_TEXT_PP(0)); bool nulls = PG_GETARG_BOOL(1); bool tableforest = PG_GETARG_BOOL(2); - const char *targetns = _textout(PG_GETARG_TEXT_P(3)); + const char *targetns = text_to_cstring(PG_GETARG_TEXT_PP(3)); const char *result; SPIPlanPtr plan; Portal portal; @@ -2124,10 +2128,10 @@ query_to_xmlschema(PG_FUNCTION_ARGS) Datum cursor_to_xmlschema(PG_FUNCTION_ARGS) { - char *name = _textout(PG_GETARG_TEXT_P(0)); + char *name = text_to_cstring(PG_GETARG_TEXT_PP(0)); bool nulls = PG_GETARG_BOOL(1); bool tableforest = PG_GETARG_BOOL(2); - const char *targetns = _textout(PG_GETARG_TEXT_P(3)); + const char *targetns = text_to_cstring(PG_GETARG_TEXT_PP(3)); const char *xmlschema; Portal portal; @@ -2153,7 +2157,7 @@ table_to_xml_and_xmlschema(PG_FUNCTION_ARGS) Oid relid = PG_GETARG_OID(0); bool nulls = PG_GETARG_BOOL(1); bool tableforest = PG_GETARG_BOOL(2); - const char *targetns = _textout(PG_GETARG_TEXT_P(3)); + const char *targetns = text_to_cstring(PG_GETARG_TEXT_PP(3)); Relation rel; const char *xmlschema; @@ -2171,10 +2175,10 @@ table_to_xml_and_xmlschema(PG_FUNCTION_ARGS) Datum query_to_xml_and_xmlschema(PG_FUNCTION_ARGS) { - char *query = _textout(PG_GETARG_TEXT_P(0)); + char *query = text_to_cstring(PG_GETARG_TEXT_PP(0)); bool nulls = PG_GETARG_BOOL(1); bool tableforest = PG_GETARG_BOOL(2); - const char *targetns = _textout(PG_GETARG_TEXT_P(3)); + const char *targetns = text_to_cstring(PG_GETARG_TEXT_PP(3)); const char *xmlschema; SPIPlanPtr plan; @@ -2255,7 +2259,7 @@ schema_to_xml(PG_FUNCTION_ARGS) Name name = PG_GETARG_NAME(0); bool nulls = PG_GETARG_BOOL(1); bool tableforest = PG_GETARG_BOOL(2); - const char *targetns = _textout(PG_GETARG_TEXT_P(3)); + const char *targetns = text_to_cstring(PG_GETARG_TEXT_PP(3)); char *schemaname; Oid nspid; @@ -2346,7 +2350,7 @@ schema_to_xmlschema(PG_FUNCTION_ARGS) Name name = PG_GETARG_NAME(0); bool nulls = PG_GETARG_BOOL(1); bool tableforest = PG_GETARG_BOOL(2); - const char *targetns = _textout(PG_GETARG_TEXT_P(3)); + const char *targetns = text_to_cstring(PG_GETARG_TEXT_PP(3)); PG_RETURN_XML_P(stringinfo_to_xmltype(schema_to_xmlschema_internal(NameStr(*name), nulls, tableforest, targetns))); @@ -2359,7 +2363,7 @@ schema_to_xml_and_xmlschema(PG_FUNCTION_ARGS) Name name = PG_GETARG_NAME(0); bool nulls = PG_GETARG_BOOL(1); bool tableforest = PG_GETARG_BOOL(2); - const char *targetns = _textout(PG_GETARG_TEXT_P(3)); + const char *targetns = text_to_cstring(PG_GETARG_TEXT_PP(3)); char *schemaname; Oid nspid; StringInfo xmlschema; @@ -2431,7 +2435,7 @@ database_to_xml(PG_FUNCTION_ARGS) { bool nulls = PG_GETARG_BOOL(0); bool tableforest = PG_GETARG_BOOL(1); - const char *targetns = _textout(PG_GETARG_TEXT_P(2)); + const char *targetns = text_to_cstring(PG_GETARG_TEXT_PP(2)); PG_RETURN_XML_P(stringinfo_to_xmltype(database_to_xml_internal(NULL, nulls, tableforest, targetns))); @@ -2486,7 +2490,7 @@ database_to_xmlschema(PG_FUNCTION_ARGS) { bool nulls = PG_GETARG_BOOL(0); bool tableforest = PG_GETARG_BOOL(1); - const char *targetns = _textout(PG_GETARG_TEXT_P(2)); + const char *targetns = text_to_cstring(PG_GETARG_TEXT_PP(2)); PG_RETURN_XML_P(stringinfo_to_xmltype(database_to_xmlschema_internal(nulls, tableforest, targetns))); @@ -2498,7 +2502,7 @@ database_to_xml_and_xmlschema(PG_FUNCTION_ARGS) { bool nulls = PG_GETARG_BOOL(0); bool tableforest = PG_GETARG_BOOL(1); - const char *targetns = _textout(PG_GETARG_TEXT_P(2)); + const char *targetns = text_to_cstring(PG_GETARG_TEXT_PP(2)); StringInfo xmlschema; xmlschema = database_to_xmlschema_internal(nulls, tableforest, targetns); @@ -3198,7 +3202,7 @@ xml_xmlnodetoxmltype(xmlNodePtr cur) { str = xmlXPathCastNodeToString(cur); len = strlen((char *) str); - result = (text *) palloc(len + VARHDRSZ); + result = (xmltype *) palloc(len + VARHDRSZ); SET_VARSIZE(result, len + VARHDRSZ); memcpy(VARDATA(result), str, len); } @@ -3363,8 +3367,8 @@ xpath(PG_FUNCTION_ARGS) ereport(ERROR, (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED), errmsg("neither namespace name nor URI may be null"))); - ns_name = _textout(ns_names_uris[i * 2]); - ns_uri = _textout(ns_names_uris[i * 2 + 1]); + ns_name = TextDatumGetCString(ns_names_uris[i * 2]); + ns_uri = TextDatumGetCString(ns_names_uris[i * 2 + 1]); if (xmlXPathRegisterNs(xpathctx, (xmlChar *) ns_name, (xmlChar *) ns_uri) != 0) |