1
0
mirror of https://github.com/postgres/postgres.git synced 2025-07-15 19:21:59 +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

@ -7,7 +7,7 @@
* Copyright (c) 1996-2008, PostgreSQL Global Development Group
*
* IDENTIFICATION
* $PostgreSQL: pgsql/src/backend/commands/comment.c,v 1.100 2008/01/01 19:45:48 momjian Exp $
* $PostgreSQL: pgsql/src/backend/commands/comment.c,v 1.101 2008/03/25 22:42:42 tgl Exp $
*
*-------------------------------------------------------------------------
*/
@ -215,7 +215,7 @@ CreateComments(Oid oid, Oid classoid, int32 subid, char *comment)
values[i++] = ObjectIdGetDatum(oid);
values[i++] = ObjectIdGetDatum(classoid);
values[i++] = Int32GetDatum(subid);
values[i++] = DirectFunctionCall1(textin, CStringGetDatum(comment));
values[i++] = CStringGetTextDatum(comment);
}
/* Use the index to search for a matching old tuple */
@ -314,7 +314,7 @@ CreateSharedComments(Oid oid, Oid classoid, char *comment)
i = 0;
values[i++] = ObjectIdGetDatum(oid);
values[i++] = ObjectIdGetDatum(classoid);
values[i++] = DirectFunctionCall1(textin, CStringGetDatum(comment));
values[i++] = CStringGetTextDatum(comment);
}
/* Use the index to search for a matching old tuple */

View File

@ -10,7 +10,7 @@
*
*
* IDENTIFICATION
* $PostgreSQL: pgsql/src/backend/commands/functioncmds.c,v 1.88 2008/01/01 19:45:49 momjian Exp $
* $PostgreSQL: pgsql/src/backend/commands/functioncmds.c,v 1.89 2008/03/25 22:42:42 tgl Exp $
*
* DESCRIPTION
* These routines take the parse tree and pick out the
@ -237,8 +237,7 @@ examine_parameter_list(List *parameters, Oid languageOid,
if (fp->name && fp->name[0])
{
paramNames[i] = DirectFunctionCall1(textin,
CStringGetDatum(fp->name));
paramNames[i] = CStringGetTextDatum(fp->name);
have_names = true;
}
@ -269,8 +268,7 @@ examine_parameter_list(List *parameters, Oid languageOid,
for (i = 0; i < parameterCount; i++)
{
if (paramNames[i] == PointerGetDatum(NULL))
paramNames[i] = DirectFunctionCall1(textin,
CStringGetDatum(""));
paramNames[i] = CStringGetTextDatum("");
}
*parameterNames = construct_array(paramNames, parameterCount, TEXTOID,
-1, false, 'i');

View File

@ -10,7 +10,7 @@
* Copyright (c) 2002-2008, PostgreSQL Global Development Group
*
* IDENTIFICATION
* $PostgreSQL: pgsql/src/backend/commands/prepare.c,v 1.81 2008/03/25 19:26:53 neilc Exp $
* $PostgreSQL: pgsql/src/backend/commands/prepare.c,v 1.82 2008/03/25 22:42:42 tgl Exp $
*
*-------------------------------------------------------------------------
*/
@ -772,14 +772,12 @@ pg_prepared_statement(PG_FUNCTION_ARGS)
MemSet(nulls, 0, sizeof(nulls));
values[0] = DirectFunctionCall1(textin,
CStringGetDatum(prep_stmt->stmt_name));
values[0] = CStringGetTextDatum(prep_stmt->stmt_name);
if (prep_stmt->plansource->query_string == NULL)
nulls[1] = true;
else
values[1] = DirectFunctionCall1(textin,
CStringGetDatum(prep_stmt->plansource->query_string));
values[1] = CStringGetTextDatum(prep_stmt->plansource->query_string);
values[2] = TimestampTzGetDatum(prep_stmt->prepare_time);
values[3] = build_regtype_array(prep_stmt->plansource->param_types,

View File

@ -7,7 +7,7 @@
* Portions Copyright (c) 1994, Regents of the University of California
*
* IDENTIFICATION
* $PostgreSQL: pgsql/src/backend/commands/proclang.c,v 1.74 2008/01/01 19:45:49 momjian Exp $
* $PostgreSQL: pgsql/src/backend/commands/proclang.c,v 1.75 2008/03/25 22:42:42 tgl Exp $
*
*-------------------------------------------------------------------------
*/
@ -360,20 +360,17 @@ find_language_template(const char *languageName)
datum = heap_getattr(tup, Anum_pg_pltemplate_tmplhandler,
RelationGetDescr(rel), &isnull);
if (!isnull)
result->tmplhandler =
DatumGetCString(DirectFunctionCall1(textout, datum));
result->tmplhandler = TextDatumGetCString(datum);
datum = heap_getattr(tup, Anum_pg_pltemplate_tmplvalidator,
RelationGetDescr(rel), &isnull);
if (!isnull)
result->tmplvalidator =
DatumGetCString(DirectFunctionCall1(textout, datum));
result->tmplvalidator = TextDatumGetCString(datum);
datum = heap_getattr(tup, Anum_pg_pltemplate_tmpllibrary,
RelationGetDescr(rel), &isnull);
if (!isnull)
result->tmpllibrary =
DatumGetCString(DirectFunctionCall1(textout, datum));
result->tmpllibrary = TextDatumGetCString(datum);
/* Ignore template if handler or library info is missing */
if (!result->tmplhandler || !result->tmpllibrary)

View File

@ -8,7 +8,7 @@
*
*
* IDENTIFICATION
* $PostgreSQL: pgsql/src/backend/commands/tablecmds.c,v 1.243 2008/03/19 18:38:30 tgl Exp $
* $PostgreSQL: pgsql/src/backend/commands/tablecmds.c,v 1.244 2008/03/25 22:42:42 tgl Exp $
*
*-------------------------------------------------------------------------
*/
@ -6183,7 +6183,7 @@ decompile_conbin(HeapTuple contup, TupleDesc tupdesc)
expr = DirectFunctionCall2(pg_get_expr, attr,
ObjectIdGetDatum(con->conrelid));
return DatumGetCString(DirectFunctionCall1(textout, expr));
return TextDatumGetCString(expr);
}
/*

View File

@ -37,7 +37,7 @@
*
*
* IDENTIFICATION
* $PostgreSQL: pgsql/src/backend/commands/tablespace.c,v 1.53 2008/01/01 19:45:49 momjian Exp $
* $PostgreSQL: pgsql/src/backend/commands/tablespace.c,v 1.54 2008/03/25 22:42:43 tgl Exp $
*
*-------------------------------------------------------------------------
*/
@ -282,7 +282,7 @@ CreateTableSpace(CreateTableSpaceStmt *stmt)
values[Anum_pg_tablespace_spcowner - 1] =
ObjectIdGetDatum(ownerId);
values[Anum_pg_tablespace_spclocation - 1] =
DirectFunctionCall1(textin, CStringGetDatum(location));
CStringGetTextDatum(location);
nulls[Anum_pg_tablespace_spcacl - 1] = 'n';
tuple = heap_formtuple(rel->rd_att, values, nulls);

View File

@ -9,7 +9,7 @@
*
*
* IDENTIFICATION
* $PostgreSQL: pgsql/src/backend/commands/tsearchcmds.c,v 1.9 2008/01/01 19:45:49 momjian Exp $
* $PostgreSQL: pgsql/src/backend/commands/tsearchcmds.c,v 1.10 2008/03/25 22:42:43 tgl Exp $
*
*-------------------------------------------------------------------------
*/
@ -1999,7 +1999,7 @@ serialize_deflist(List *deflist)
appendStringInfo(&buf, ", ");
}
result = CStringGetTextP(buf.data);
result = cstring_to_text_with_len(buf.data, buf.len);
pfree(buf.data);
return result;
}
@ -2099,7 +2099,7 @@ deserialize_deflist(Datum txt)
ereport(ERROR,
(errcode(ERRCODE_SYNTAX_ERROR),
errmsg("invalid parameter list format: \"%s\"",
TextPGetCString(in))));
text_to_cstring(in))));
break;
case CS_WAITVALUE:
if (*ptr == '\'')
@ -2210,7 +2210,7 @@ deserialize_deflist(Datum txt)
ereport(ERROR,
(errcode(ERRCODE_SYNTAX_ERROR),
errmsg("invalid parameter list format: \"%s\"",
TextPGetCString(in))));
text_to_cstring(in))));
pfree(workspace);

View File

@ -8,7 +8,7 @@
*
*
* IDENTIFICATION
* $PostgreSQL: pgsql/src/backend/commands/typecmds.c,v 1.114 2008/03/19 18:38:30 tgl Exp $
* $PostgreSQL: pgsql/src/backend/commands/typecmds.c,v 1.115 2008/03/25 22:42:43 tgl Exp $
*
* DESCRIPTION
* The "DefineFoo" routines take the parse tree and pick out the
@ -697,13 +697,13 @@ DefineDomain(CreateDomainStmt *stmt)
datum = SysCacheGetAttr(TYPEOID, typeTup,
Anum_pg_type_typdefault, &isnull);
if (!isnull)
defaultValue = DatumGetCString(DirectFunctionCall1(textout, datum));
defaultValue = TextDatumGetCString(datum);
/* Inherited default binary value */
datum = SysCacheGetAttr(TYPEOID, typeTup,
Anum_pg_type_typdefaultbin, &isnull);
if (!isnull)
defaultValueBin = DatumGetCString(DirectFunctionCall1(textout, datum));
defaultValueBin = TextDatumGetCString(datum);
/*
* Run through constraints manually to avoid the additional processing
@ -1497,12 +1497,10 @@ AlterDomainDefault(List *names, Node *defaultRaw)
/*
* Form an updated tuple with the new default and write it back.
*/
new_record[Anum_pg_type_typdefaultbin - 1] = DirectFunctionCall1(textin,
CStringGetDatum(nodeToString(defaultExpr)));
new_record[Anum_pg_type_typdefaultbin - 1] = CStringGetTextDatum(nodeToString(defaultExpr));
new_record_repl[Anum_pg_type_typdefaultbin - 1] = 'r';
new_record[Anum_pg_type_typdefault - 1] = DirectFunctionCall1(textin,
CStringGetDatum(defaultValue));
new_record[Anum_pg_type_typdefault - 1] = CStringGetTextDatum(defaultValue);
new_record_repl[Anum_pg_type_typdefault - 1] = 'r';
}
}
@ -2292,9 +2290,7 @@ GetDomainConstraints(Oid typeOid)
elog(ERROR, "domain \"%s\" constraint \"%s\" has NULL conbin",
NameStr(typTup->typname), NameStr(c->conname));
check_expr = (Expr *)
stringToNode(DatumGetCString(DirectFunctionCall1(textout,
val)));
check_expr = (Expr *) stringToNode(TextDatumGetCString(val));
/* ExecInitExpr assumes we already fixed opfuncids */
fix_opfuncids((Node *) check_expr);

View File

@ -6,7 +6,7 @@
* Portions Copyright (c) 1996-2008, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California
*
* $PostgreSQL: pgsql/src/backend/commands/user.c,v 1.179 2008/03/24 19:47:35 tgl Exp $
* $PostgreSQL: pgsql/src/backend/commands/user.c,v 1.180 2008/03/25 22:42:43 tgl Exp $
*
*-------------------------------------------------------------------------
*/
@ -311,14 +311,14 @@ CreateRole(CreateRoleStmt *stmt)
{
if (!encrypt_password || isMD5(password))
new_record[Anum_pg_authid_rolpassword - 1] =
DirectFunctionCall1(textin, CStringGetDatum(password));
CStringGetTextDatum(password);
else
{
if (!pg_md5_encrypt(password, stmt->role, strlen(stmt->role),
encrypted_password))
elog(ERROR, "password encryption failed");
new_record[Anum_pg_authid_rolpassword - 1] =
DirectFunctionCall1(textin, CStringGetDatum(encrypted_password));
CStringGetTextDatum(encrypted_password);
}
}
else
@ -639,14 +639,14 @@ AlterRole(AlterRoleStmt *stmt)
{
if (!encrypt_password || isMD5(password))
new_record[Anum_pg_authid_rolpassword - 1] =
DirectFunctionCall1(textin, CStringGetDatum(password));
CStringGetTextDatum(password);
else
{
if (!pg_md5_encrypt(password, stmt->role, strlen(stmt->role),
encrypted_password))
elog(ERROR, "password encryption failed");
new_record[Anum_pg_authid_rolpassword - 1] =
DirectFunctionCall1(textin, CStringGetDatum(encrypted_password));
CStringGetTextDatum(encrypted_password);
}
new_record_repl[Anum_pg_authid_rolpassword - 1] = 'r';
}
@ -1060,7 +1060,7 @@ RenameRole(const char *oldname, const char *newname)
datum = heap_getattr(oldtuple, Anum_pg_authid_rolpassword, dsc, &isnull);
if (!isnull && isMD5(DatumGetCString(DirectFunctionCall1(textout, datum))))
if (!isnull && isMD5(TextDatumGetCString(datum)))
{
/* MD5 uses the username as salt, so just clear it on a rename */
repl_repl[Anum_pg_authid_rolpassword - 1] = 'r';