PostgreSQL Source Code  git master
name.c File Reference
#include "postgres.h"
#include "catalog/namespace.h"
#include "catalog/pg_collation.h"
#include "catalog/pg_type.h"
#include "libpq/pqformat.h"
#include "mb/pg_wchar.h"
#include "miscadmin.h"
#include "utils/array.h"
#include "utils/builtins.h"
#include "utils/lsyscache.h"
#include "utils/varlena.h"
Include dependency graph for name.c:

Go to the source code of this file.

Functions

Datum namein (PG_FUNCTION_ARGS)
 
Datum nameout (PG_FUNCTION_ARGS)
 
Datum namerecv (PG_FUNCTION_ARGS)
 
Datum namesend (PG_FUNCTION_ARGS)
 
static int namecmp (Name arg1, Name arg2, Oid collid)
 
Datum nameeq (PG_FUNCTION_ARGS)
 
Datum namene (PG_FUNCTION_ARGS)
 
Datum namelt (PG_FUNCTION_ARGS)
 
Datum namele (PG_FUNCTION_ARGS)
 
Datum namegt (PG_FUNCTION_ARGS)
 
Datum namege (PG_FUNCTION_ARGS)
 
Datum btnamecmp (PG_FUNCTION_ARGS)
 
Datum btnamesortsupport (PG_FUNCTION_ARGS)
 
int namecpy (Name n1, const NameData *n2)
 
int namestrcpy (Name name, const char *str)
 
int namestrcmp (Name name, const char *str)
 
Datum current_user (PG_FUNCTION_ARGS)
 
Datum session_user (PG_FUNCTION_ARGS)
 
Datum current_schema (PG_FUNCTION_ARGS)
 
Datum current_schemas (PG_FUNCTION_ARGS)
 
Datum nameconcatoid (PG_FUNCTION_ARGS)
 

Function Documentation

◆ btnamecmp()

Datum btnamecmp ( PG_FUNCTION_ARGS  )

Definition at line 202 of file name.c.

References namecmp(), PG_GET_COLLATION, PG_GETARG_NAME, and PG_RETURN_INT32.

Referenced by leftmostvalue_name().

203 {
204  Name arg1 = PG_GETARG_NAME(0);
205  Name arg2 = PG_GETARG_NAME(1);
206 
207  PG_RETURN_INT32(namecmp(arg1, arg2, PG_GET_COLLATION()));
208 }
#define PG_RETURN_INT32(x)
Definition: fmgr.h:344
#define PG_GET_COLLATION()
Definition: fmgr.h:193
Definition: c.h:603
static int namecmp(Name arg1, Name arg2, Oid collid)
Definition: name.c:135
#define PG_GETARG_NAME(n)
Definition: fmgr.h:273

◆ btnamesortsupport()

Datum btnamesortsupport ( PG_FUNCTION_ARGS  )

Definition at line 211 of file name.c.

References MemoryContextSwitchTo(), PG_GETARG_POINTER, PG_RETURN_VOID, SortSupportData::ssup_collation, SortSupportData::ssup_cxt, and varstr_sortsupport().

212 {
214  Oid collid = ssup->ssup_collation;
215  MemoryContext oldcontext;
216 
217  oldcontext = MemoryContextSwitchTo(ssup->ssup_cxt);
218 
219  /* Use generic string SortSupport */
220  varstr_sortsupport(ssup, NAMEOID, collid);
221 
222  MemoryContextSwitchTo(oldcontext);
223 
224  PG_RETURN_VOID();
225 }
struct SortSupportData * SortSupport
Definition: sortsupport.h:58
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:271
unsigned int Oid
Definition: postgres_ext.h:31
MemoryContext ssup_cxt
Definition: sortsupport.h:66
void varstr_sortsupport(SortSupport ssup, Oid typid, Oid collid)
Definition: varlena.c:1959
#define PG_RETURN_VOID()
Definition: fmgr.h:339

◆ current_schema()

Datum current_schema ( PG_FUNCTION_ARGS  )

Definition at line 319 of file name.c.

References CStringGetDatum, DirectFunctionCall1, fetch_search_path(), get_namespace_name(), linitial_oid, list_free(), namein(), NIL, PG_RETURN_DATUM, and PG_RETURN_NULL.

Referenced by ExecEvalSQLValueFunction().

320 {
321  List *search_path = fetch_search_path(false);
322  char *nspname;
323 
324  if (search_path == NIL)
325  PG_RETURN_NULL();
326  nspname = get_namespace_name(linitial_oid(search_path));
327  list_free(search_path);
328  if (!nspname)
329  PG_RETURN_NULL(); /* recently-deleted namespace? */
331 }
#define NIL
Definition: pg_list.h:65
Datum namein(PG_FUNCTION_ARGS)
Definition: name.c:48
#define DirectFunctionCall1(func, arg1)
Definition: fmgr.h:617
char * get_namespace_name(Oid nspid)
Definition: lsyscache.c:3094
#define CStringGetDatum(X)
Definition: postgres.h:578
#define PG_RETURN_DATUM(x)
Definition: fmgr.h:343
#define linitial_oid(l)
Definition: pg_list.h:197
void list_free(List *list)
Definition: list.c:1377
Definition: pg_list.h:50
List * fetch_search_path(bool includeImplicit)
Definition: namespace.c:4305
#define PG_RETURN_NULL()
Definition: fmgr.h:335

◆ current_schemas()

Datum current_schemas ( PG_FUNCTION_ARGS  )

Definition at line 334 of file name.c.

References construct_array(), CStringGetDatum, DirectFunctionCall1, fetch_search_path(), get_namespace_name(), i, lfirst_oid, list_free(), list_length(), NAMEDATALEN, namein(), palloc(), PG_GETARG_BOOL, and PG_RETURN_POINTER.

335 {
336  List *search_path = fetch_search_path(PG_GETARG_BOOL(0));
337  ListCell *l;
338  Datum *names;
339  int i;
340  ArrayType *array;
341 
342  names = (Datum *) palloc(list_length(search_path) * sizeof(Datum));
343  i = 0;
344  foreach(l, search_path)
345  {
346  char *nspname;
347 
348  nspname = get_namespace_name(lfirst_oid(l));
349  if (nspname) /* watch out for deleted namespace */
350  {
351  names[i] = DirectFunctionCall1(namein, CStringGetDatum(nspname));
352  i++;
353  }
354  }
355  list_free(search_path);
356 
357  array = construct_array(names, i,
358  NAMEOID,
359  NAMEDATALEN, /* sizeof(Name) */
360  false, /* Name is not by-val */
361  'c'); /* alignment of Name */
362 
363  PG_RETURN_POINTER(array);
364 }
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:351
Datum namein(PG_FUNCTION_ARGS)
Definition: name.c:48
ArrayType * construct_array(Datum *elems, int nelems, Oid elmtype, int elmlen, bool elmbyval, char elmalign)
Definition: arrayfuncs.c:3291
#define DirectFunctionCall1(func, arg1)
Definition: fmgr.h:617
#define PG_GETARG_BOOL(n)
Definition: fmgr.h:269
#define NAMEDATALEN
char * get_namespace_name(Oid nspid)
Definition: lsyscache.c:3094
#define CStringGetDatum(X)
Definition: postgres.h:578
uintptr_t Datum
Definition: postgres.h:367
static int list_length(const List *l)
Definition: pg_list.h:169
void * palloc(Size size)
Definition: mcxt.c:949
void list_free(List *list)
Definition: list.c:1377
int i
Definition: pg_list.h:50
List * fetch_search_path(bool includeImplicit)
Definition: namespace.c:4305
#define lfirst_oid(lc)
Definition: pg_list.h:192

◆ current_user()

Datum current_user ( PG_FUNCTION_ARGS  )

Definition at line 303 of file name.c.

References CStringGetDatum, DirectFunctionCall1, GetUserId(), GetUserNameFromId(), namein(), and PG_RETURN_DATUM.

Referenced by ExecEvalSQLValueFunction().

304 {
306 }
Datum namein(PG_FUNCTION_ARGS)
Definition: name.c:48
Oid GetUserId(void)
Definition: miscinit.c:380
#define DirectFunctionCall1(func, arg1)
Definition: fmgr.h:617
#define CStringGetDatum(X)
Definition: postgres.h:578
#define PG_RETURN_DATUM(x)
Definition: fmgr.h:343
char * GetUserNameFromId(Oid roleid, bool noerr)
Definition: miscinit.c:795

◆ namecmp()

static int namecmp ( Name  arg1,
Name  arg2,
Oid  collid 
)
static

Definition at line 135 of file name.c.

References NAMEDATALEN, NameStr, and varstr_cmp().

Referenced by btnamecmp(), nameeq(), namege(), namegt(), namele(), namelt(), and namene().

136 {
137  /* Fast path for common case used in system catalogs */
138  if (collid == C_COLLATION_OID)
139  return strncmp(NameStr(*arg1), NameStr(*arg2), NAMEDATALEN);
140 
141  /* Else rely on the varstr infrastructure */
142  return varstr_cmp(NameStr(*arg1), strlen(NameStr(*arg1)),
143  NameStr(*arg2), strlen(NameStr(*arg2)),
144  collid);
145 }
#define NAMEDATALEN
int varstr_cmp(const char *arg1, int len1, const char *arg2, int len2, Oid collid)
Definition: varlena.c:1474
#define NameStr(name)
Definition: c.h:609

◆ nameconcatoid()

Datum nameconcatoid ( PG_FUNCTION_ARGS  )

Definition at line 377 of file name.c.

References NAMEDATALEN, NameStr, palloc0(), PG_GETARG_NAME, PG_GETARG_OID, pg_mbcliplen(), PG_RETURN_NAME, and snprintf.

378 {
379  Name nam = PG_GETARG_NAME(0);
380  Oid oid = PG_GETARG_OID(1);
381  Name result;
382  char suffix[20];
383  int suflen;
384  int namlen;
385 
386  suflen = snprintf(suffix, sizeof(suffix), "_%u", oid);
387  namlen = strlen(NameStr(*nam));
388 
389  /* Truncate oversize input by truncating name part, not suffix */
390  if (namlen + suflen >= NAMEDATALEN)
391  namlen = pg_mbcliplen(NameStr(*nam), namlen, NAMEDATALEN - 1 - suflen);
392 
393  /* We use palloc0 here to ensure result is zero-padded */
394  result = (Name) palloc0(NAMEDATALEN);
395  memcpy(NameStr(*result), NameStr(*nam), namlen);
396  memcpy(NameStr(*result) + namlen, suffix, suflen);
397 
398  PG_RETURN_NAME(result);
399 }
unsigned int Oid
Definition: postgres_ext.h:31
#define NAMEDATALEN
int pg_mbcliplen(const char *mbstr, int len, int limit)
Definition: mbutils.c:862
Definition: c.h:603
#define PG_GETARG_OID(n)
Definition: fmgr.h:270
void * palloc0(Size size)
Definition: mcxt.c:980
#define NameStr(name)
Definition: c.h:609
NameData * Name
Definition: c.h:607
#define snprintf
Definition: port.h:192
#define PG_RETURN_NAME(x)
Definition: fmgr.h:353
#define PG_GETARG_NAME(n)
Definition: fmgr.h:273

◆ namecpy()

int namecpy ( Name  n1,
const NameData n2 
)

Definition at line 233 of file name.c.

References NAMEDATALEN, NameStr, and StrNCpy.

Referenced by ConstructTupleDescriptor(), and pg_get_replication_slots().

234 {
235  if (!n1 || !n2)
236  return -1;
237  StrNCpy(NameStr(*n1), NameStr(*n2), NAMEDATALEN);
238  return 0;
239 }
#define NAMEDATALEN
#define StrNCpy(dst, src, len)
Definition: c.h:928
#define NameStr(name)
Definition: c.h:609

◆ nameeq()

Datum nameeq ( PG_FUNCTION_ARGS  )

Definition at line 148 of file name.c.

References namecmp(), PG_GET_COLLATION, PG_GETARG_NAME, and PG_RETURN_BOOL.

149 {
150  Name arg1 = PG_GETARG_NAME(0);
151  Name arg2 = PG_GETARG_NAME(1);
152 
153  PG_RETURN_BOOL(namecmp(arg1, arg2, PG_GET_COLLATION()) == 0);
154 }
#define PG_GET_COLLATION()
Definition: fmgr.h:193
Definition: c.h:603
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:349
static int namecmp(Name arg1, Name arg2, Oid collid)
Definition: name.c:135
#define PG_GETARG_NAME(n)
Definition: fmgr.h:273

◆ namege()

Datum namege ( PG_FUNCTION_ARGS  )

Definition at line 193 of file name.c.

References namecmp(), PG_GET_COLLATION, PG_GETARG_NAME, and PG_RETURN_BOOL.

194 {
195  Name arg1 = PG_GETARG_NAME(0);
196  Name arg2 = PG_GETARG_NAME(1);
197 
198  PG_RETURN_BOOL(namecmp(arg1, arg2, PG_GET_COLLATION()) >= 0);
199 }
#define PG_GET_COLLATION()
Definition: fmgr.h:193
Definition: c.h:603
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:349
static int namecmp(Name arg1, Name arg2, Oid collid)
Definition: name.c:135
#define PG_GETARG_NAME(n)
Definition: fmgr.h:273

◆ namegt()

Datum namegt ( PG_FUNCTION_ARGS  )

Definition at line 184 of file name.c.

References namecmp(), PG_GET_COLLATION, PG_GETARG_NAME, and PG_RETURN_BOOL.

185 {
186  Name arg1 = PG_GETARG_NAME(0);
187  Name arg2 = PG_GETARG_NAME(1);
188 
189  PG_RETURN_BOOL(namecmp(arg1, arg2, PG_GET_COLLATION()) > 0);
190 }
#define PG_GET_COLLATION()
Definition: fmgr.h:193
Definition: c.h:603
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:349
static int namecmp(Name arg1, Name arg2, Oid collid)
Definition: name.c:135
#define PG_GETARG_NAME(n)
Definition: fmgr.h:273

◆ namein()

Datum namein ( PG_FUNCTION_ARGS  )

Definition at line 48 of file name.c.

References NAMEDATALEN, NameStr, palloc0(), PG_GETARG_CSTRING, pg_mbcliplen(), and PG_RETURN_NAME.

Referenced by AlterSubscription(), convert_requires_to_datum(), CreateAccessMethod(), createdb(), CreateForeignDataWrapper(), CreateForeignServer(), CreatePolicy(), CreatePublication(), CreateRole(), CreateSubscription(), CreateTableSpace(), CreateTrigger(), current_schema(), current_schemas(), current_user(), get_available_versions_for_extension(), getdatabaseencoding(), InsertExtensionTuple(), pg_available_extensions(), pg_client_encoding(), pg_convert_from(), pg_convert_to(), PG_encoding_to_char(), pg_get_viewdef_worker(), plpgsql_fulfill_promise(), RenameRole(), session_user(), and string_to_datum().

49 {
50  char *s = PG_GETARG_CSTRING(0);
51  Name result;
52  int len;
53 
54  len = strlen(s);
55 
56  /* Truncate oversize input */
57  if (len >= NAMEDATALEN)
58  len = pg_mbcliplen(s, len, NAMEDATALEN - 1);
59 
60  /* We use palloc0 here to ensure result is zero-padded */
61  result = (Name) palloc0(NAMEDATALEN);
62  memcpy(NameStr(*result), s, len);
63 
64  PG_RETURN_NAME(result);
65 }
#define NAMEDATALEN
int pg_mbcliplen(const char *mbstr, int len, int limit)
Definition: mbutils.c:862
Definition: c.h:603
void * palloc0(Size size)
Definition: mcxt.c:980
#define NameStr(name)
Definition: c.h:609
#define PG_GETARG_CSTRING(n)
Definition: fmgr.h:272
NameData * Name
Definition: c.h:607
#define PG_RETURN_NAME(x)
Definition: fmgr.h:353

◆ namele()

Datum namele ( PG_FUNCTION_ARGS  )

Definition at line 175 of file name.c.

References namecmp(), PG_GET_COLLATION, PG_GETARG_NAME, and PG_RETURN_BOOL.

176 {
177  Name arg1 = PG_GETARG_NAME(0);
178  Name arg2 = PG_GETARG_NAME(1);
179 
180  PG_RETURN_BOOL(namecmp(arg1, arg2, PG_GET_COLLATION()) <= 0);
181 }
#define PG_GET_COLLATION()
Definition: fmgr.h:193
Definition: c.h:603
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:349
static int namecmp(Name arg1, Name arg2, Oid collid)
Definition: name.c:135
#define PG_GETARG_NAME(n)
Definition: fmgr.h:273

◆ namelt()

Datum namelt ( PG_FUNCTION_ARGS  )

Definition at line 166 of file name.c.

References namecmp(), PG_GET_COLLATION, PG_GETARG_NAME, and PG_RETURN_BOOL.

167 {
168  Name arg1 = PG_GETARG_NAME(0);
169  Name arg2 = PG_GETARG_NAME(1);
170 
171  PG_RETURN_BOOL(namecmp(arg1, arg2, PG_GET_COLLATION()) < 0);
172 }
#define PG_GET_COLLATION()
Definition: fmgr.h:193
Definition: c.h:603
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:349
static int namecmp(Name arg1, Name arg2, Oid collid)
Definition: name.c:135
#define PG_GETARG_NAME(n)
Definition: fmgr.h:273

◆ namene()

Datum namene ( PG_FUNCTION_ARGS  )

Definition at line 157 of file name.c.

References namecmp(), PG_GET_COLLATION, PG_GETARG_NAME, and PG_RETURN_BOOL.

158 {
159  Name arg1 = PG_GETARG_NAME(0);
160  Name arg2 = PG_GETARG_NAME(1);
161 
162  PG_RETURN_BOOL(namecmp(arg1, arg2, PG_GET_COLLATION()) != 0);
163 }
#define PG_GET_COLLATION()
Definition: fmgr.h:193
Definition: c.h:603
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:349
static int namecmp(Name arg1, Name arg2, Oid collid)
Definition: name.c:135
#define PG_GETARG_NAME(n)
Definition: fmgr.h:273

◆ nameout()

Datum nameout ( PG_FUNCTION_ARGS  )

Definition at line 71 of file name.c.

References NameStr, PG_GETARG_NAME, PG_RETURN_CSTRING, and pstrdup().

Referenced by make_greater_string(), and RelationBuildTriggers().

72 {
73  Name s = PG_GETARG_NAME(0);
74 
76 }
char * pstrdup(const char *in)
Definition: mcxt.c:1186
Definition: c.h:603
#define PG_RETURN_CSTRING(x)
Definition: fmgr.h:352
#define NameStr(name)
Definition: c.h:609
#define PG_GETARG_NAME(n)
Definition: fmgr.h:273

◆ namerecv()

Datum namerecv ( PG_FUNCTION_ARGS  )

Definition at line 82 of file name.c.

References buf, StringInfoData::cursor, ereport, errcode(), errdetail(), errmsg(), ERROR, StringInfoData::len, NAMEDATALEN, palloc0(), pfree(), PG_GETARG_POINTER, PG_RETURN_NAME, pq_getmsgtext(), and generate_unaccent_rules::str.

83 {
85  Name result;
86  char *str;
87  int nbytes;
88 
89  str = pq_getmsgtext(buf, buf->len - buf->cursor, &nbytes);
90  if (nbytes >= NAMEDATALEN)
91  ereport(ERROR,
92  (errcode(ERRCODE_NAME_TOO_LONG),
93  errmsg("identifier too long"),
94  errdetail("Identifier must be less than %d characters.",
95  NAMEDATALEN)));
96  result = (NameData *) palloc0(NAMEDATALEN);
97  memcpy(result, str, nbytes);
98  pfree(str);
99  PG_RETURN_NAME(result);
100 }
StringInfoData * StringInfo
Definition: stringinfo.h:43
int errcode(int sqlerrcode)
Definition: elog.c:570
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:271
#define NAMEDATALEN
void pfree(void *pointer)
Definition: mcxt.c:1056
#define ERROR
Definition: elog.h:43
Definition: c.h:603
static char * buf
Definition: pg_test_fsync.c:68
int errdetail(const char *fmt,...)
Definition: elog.c:860
#define ereport(elevel, rest)
Definition: elog.h:141
char * pq_getmsgtext(StringInfo msg, int rawbytes, int *nbytes)
Definition: pqformat.c:548
void * palloc0(Size size)
Definition: mcxt.c:980
int errmsg(const char *fmt,...)
Definition: elog.c:784
#define PG_RETURN_NAME(x)
Definition: fmgr.h:353

◆ namesend()

Datum namesend ( PG_FUNCTION_ARGS  )

Definition at line 106 of file name.c.

References buf, NameStr, PG_GETARG_NAME, PG_RETURN_BYTEA_P, pq_begintypsend(), pq_endtypsend(), and pq_sendtext().

107 {
108  Name s = PG_GETARG_NAME(0);
110 
111  pq_begintypsend(&buf);
112  pq_sendtext(&buf, NameStr(*s), strlen(NameStr(*s)));
114 }
void pq_begintypsend(StringInfo buf)
Definition: pqformat.c:328
void pq_sendtext(StringInfo buf, const char *str, int slen)
Definition: pqformat.c:174
#define PG_RETURN_BYTEA_P(x)
Definition: fmgr.h:360
bytea * pq_endtypsend(StringInfo buf)
Definition: pqformat.c:348
Definition: c.h:603
static char * buf
Definition: pg_test_fsync.c:68
#define NameStr(name)
Definition: c.h:609
#define PG_GETARG_NAME(n)
Definition: fmgr.h:273

◆ namestrcmp()

int namestrcmp ( Name  name,
const char *  str 
)

Definition at line 287 of file name.c.

References NAMEDATALEN, and NameStr.

Referenced by attnameAttNum(), CopyGetAttnums(), CreateTrigger(), expanded_record_lookup_field(), GetAttributeByName(), and SPI_fnumber().

288 {
289  if (!name && !str)
290  return 0;
291  if (!name)
292  return -1; /* NULL < anything */
293  if (!str)
294  return 1; /* NULL < anything */
295  return strncmp(NameStr(*name), str, NAMEDATALEN);
296 }
#define NAMEDATALEN
#define NameStr(name)
Definition: c.h:609

◆ namestrcpy()

int namestrcpy ( Name  name,
const char *  str 
)

◆ session_user()

Datum session_user ( PG_FUNCTION_ARGS  )

Definition at line 309 of file name.c.

References CStringGetDatum, DirectFunctionCall1, GetSessionUserId(), GetUserNameFromId(), namein(), and PG_RETURN_DATUM.

Referenced by ExecEvalSQLValueFunction().

310 {
312 }
Datum namein(PG_FUNCTION_ARGS)
Definition: name.c:48
#define DirectFunctionCall1(func, arg1)
Definition: fmgr.h:617
Oid GetSessionUserId(void)
Definition: miscinit.c:414
#define CStringGetDatum(X)
Definition: postgres.h:578
#define PG_RETURN_DATUM(x)
Definition: fmgr.h:343
char * GetUserNameFromId(Oid roleid, bool noerr)
Definition: miscinit.c:795