1
0
mirror of https://github.com/postgres/postgres.git synced 2025-10-13 18:28:01 +03:00

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
This commit is contained in:
Tom Lane
2008-03-25 22:42:46 +00:00
parent f948197b40
commit 220db7ccd8
94 changed files with 771 additions and 1211 deletions

View File

@@ -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);