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)
 
void 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:353
#define PG_GET_COLLATION()
Definition: fmgr.h:198
Definition: c.h:617
static int namecmp(Name arg1, Name arg2, Oid collid)
Definition: name.c:135
#define PG_GETARG_NAME(n)
Definition: fmgr.h:278

◆ 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:276
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:1995
#define PG_RETURN_VOID()
Definition: fmgr.h:348

◆ current_schema()

Datum current_schema ( PG_FUNCTION_ARGS  )

Definition at line 279 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().

280 {
281  List *search_path = fetch_search_path(false);
282  char *nspname;
283 
284  if (search_path == NIL)
285  PG_RETURN_NULL();
286  nspname = get_namespace_name(linitial_oid(search_path));
287  list_free(search_path);
288  if (!nspname)
289  PG_RETURN_NULL(); /* recently-deleted namespace? */
291 }
#define NIL
Definition: pg_list.h:65
Datum namein(PG_FUNCTION_ARGS)
Definition: name.c:48
#define DirectFunctionCall1(func, arg1)
Definition: fmgr.h:624
char * get_namespace_name(Oid nspid)
Definition: lsyscache.c:3191
#define CStringGetDatum(X)
Definition: postgres.h:578
#define PG_RETURN_DATUM(x)
Definition: fmgr.h:352
#define linitial_oid(l)
Definition: pg_list.h:176
void list_free(List *list)
Definition: list.c:1376
Definition: pg_list.h:50
List * fetch_search_path(bool includeImplicit)
Definition: namespace.c:4382
#define PG_RETURN_NULL()
Definition: fmgr.h:344

◆ current_schemas()

Datum current_schemas ( PG_FUNCTION_ARGS  )

Definition at line 294 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.

295 {
296  List *search_path = fetch_search_path(PG_GETARG_BOOL(0));
297  ListCell *l;
298  Datum *names;
299  int i;
300  ArrayType *array;
301 
302  names = (Datum *) palloc(list_length(search_path) * sizeof(Datum));
303  i = 0;
304  foreach(l, search_path)
305  {
306  char *nspname;
307 
308  nspname = get_namespace_name(lfirst_oid(l));
309  if (nspname) /* watch out for deleted namespace */
310  {
311  names[i] = DirectFunctionCall1(namein, CStringGetDatum(nspname));
312  i++;
313  }
314  }
315  list_free(search_path);
316 
317  array = construct_array(names, i,
318  NAMEOID,
319  NAMEDATALEN, /* sizeof(Name) */
320  false, /* Name is not by-val */
321  TYPALIGN_CHAR); /* alignment of Name */
322 
323  PG_RETURN_POINTER(array);
324 }
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:360
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:3313
#define DirectFunctionCall1(func, arg1)
Definition: fmgr.h:624
#define PG_GETARG_BOOL(n)
Definition: fmgr.h:274
#define NAMEDATALEN
char * get_namespace_name(Oid nspid)
Definition: lsyscache.c:3191
#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:149
void * palloc(Size size)
Definition: mcxt.c:950
void list_free(List *list)
Definition: list.c:1376
int i
Definition: pg_list.h:50
List * fetch_search_path(bool includeImplicit)
Definition: namespace.c:4382
#define lfirst_oid(lc)
Definition: pg_list.h:171

◆ current_user()

Datum current_user ( PG_FUNCTION_ARGS  )

Definition at line 263 of file name.c.

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

Referenced by ExecEvalSQLValueFunction().

264 {
266 }
Datum namein(PG_FUNCTION_ARGS)
Definition: name.c:48
Oid GetUserId(void)
Definition: miscinit.c:476
#define DirectFunctionCall1(func, arg1)
Definition: fmgr.h:624
#define CStringGetDatum(X)
Definition: postgres.h:578
#define PG_RETURN_DATUM(x)
Definition: fmgr.h:352
char * GetUserNameFromId(Oid roleid, bool noerr)
Definition: miscinit.c:891

◆ 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:1510
#define NameStr(name)
Definition: c.h:623

◆ nameconcatoid()

Datum nameconcatoid ( PG_FUNCTION_ARGS  )

Definition at line 337 of file name.c.

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

338 {
339  Name nam = PG_GETARG_NAME(0);
340  Oid oid = PG_GETARG_OID(1);
341  Name result;
342  char suffix[20];
343  int suflen;
344  int namlen;
345 
346  suflen = snprintf(suffix, sizeof(suffix), "_%u", oid);
347  namlen = strlen(NameStr(*nam));
348 
349  /* Truncate oversize input by truncating name part, not suffix */
350  if (namlen + suflen >= NAMEDATALEN)
351  namlen = pg_mbcliplen(NameStr(*nam), namlen, NAMEDATALEN - 1 - suflen);
352 
353  /* We use palloc0 here to ensure result is zero-padded */
354  result = (Name) palloc0(NAMEDATALEN);
355  memcpy(NameStr(*result), NameStr(*nam), namlen);
356  memcpy(NameStr(*result) + namlen, suffix, suflen);
357 
358  PG_RETURN_NAME(result);
359 }
unsigned int Oid
Definition: postgres_ext.h:31
#define NAMEDATALEN
int pg_mbcliplen(const char *mbstr, int len, int limit)
Definition: mbutils.c:967
Definition: c.h:617
#define PG_GETARG_OID(n)
Definition: fmgr.h:275
void * palloc0(Size size)
Definition: mcxt.c:981
#define NameStr(name)
Definition: c.h:623
NameData * Name
Definition: c.h:621
#define snprintf
Definition: port.h:215
#define PG_RETURN_NAME(x)
Definition: fmgr.h:362
#define PG_GETARG_NAME(n)
Definition: fmgr.h:278

◆ 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:198
Definition: c.h:617
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:358
static int namecmp(Name arg1, Name arg2, Oid collid)
Definition: name.c:135
#define PG_GETARG_NAME(n)
Definition: fmgr.h:278

◆ 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:198
Definition: c.h:617
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:358
static int namecmp(Name arg1, Name arg2, Oid collid)
Definition: name.c:135
#define PG_GETARG_NAME(n)
Definition: fmgr.h:278

◆ 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:198
Definition: c.h:617
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:358
static int namecmp(Name arg1, Name arg2, Oid collid)
Definition: name.c:135
#define PG_GETARG_NAME(n)
Definition: fmgr.h:278

◆ 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:967
Definition: c.h:617
void * palloc0(Size size)
Definition: mcxt.c:981
#define NameStr(name)
Definition: c.h:623
#define PG_GETARG_CSTRING(n)
Definition: fmgr.h:277
NameData * Name
Definition: c.h:621
#define PG_RETURN_NAME(x)
Definition: fmgr.h:362

◆ 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:198
Definition: c.h:617
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:358
static int namecmp(Name arg1, Name arg2, Oid collid)
Definition: name.c:135
#define PG_GETARG_NAME(n)
Definition: fmgr.h:278

◆ 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:198
Definition: c.h:617
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:358
static int namecmp(Name arg1, Name arg2, Oid collid)
Definition: name.c:135
#define PG_GETARG_NAME(n)
Definition: fmgr.h:278

◆ 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:198
Definition: c.h:617
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:358
static int namecmp(Name arg1, Name arg2, Oid collid)
Definition: name.c:135
#define PG_GETARG_NAME(n)
Definition: fmgr.h:278

◆ 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:1187
Definition: c.h:617
#define PG_RETURN_CSTRING(x)
Definition: fmgr.h:361
#define NameStr(name)
Definition: c.h:623
#define PG_GETARG_NAME(n)
Definition: fmgr.h:278

◆ 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:44
int errcode(int sqlerrcode)
Definition: elog.c:610
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:276
#define NAMEDATALEN
void pfree(void *pointer)
Definition: mcxt.c:1057
#define ERROR
Definition: elog.h:43
Definition: c.h:617
static char * buf
Definition: pg_test_fsync.c:68
int errdetail(const char *fmt,...)
Definition: elog.c:954
char * pq_getmsgtext(StringInfo msg, int rawbytes, int *nbytes)
Definition: pqformat.c:548
void * palloc0(Size size)
Definition: mcxt.c:981
#define ereport(elevel,...)
Definition: elog.h:144
int errmsg(const char *fmt,...)
Definition: elog.c:821
#define PG_RETURN_NAME(x)
Definition: fmgr.h:362

◆ 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:369
bytea * pq_endtypsend(StringInfo buf)
Definition: pqformat.c:348
Definition: c.h:617
static char * buf
Definition: pg_test_fsync.c:68
#define NameStr(name)
Definition: c.h:623
#define PG_GETARG_NAME(n)
Definition: fmgr.h:278

◆ namestrcmp()

int namestrcmp ( Name  name,
const char *  str 
)

Definition at line 247 of file name.c.

References NAMEDATALEN, and NameStr.

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

248 {
249  if (!name && !str)
250  return 0;
251  if (!name)
252  return -1; /* NULL < anything */
253  if (!str)
254  return 1; /* NULL < anything */
255  return strncmp(NameStr(*name), str, NAMEDATALEN);
256 }
#define NAMEDATALEN
#define NameStr(name)
Definition: c.h:623

◆ namestrcpy()

void namestrcpy ( Name  name,
const char *  str 
)

◆ session_user()

Datum session_user ( PG_FUNCTION_ARGS  )

Definition at line 269 of file name.c.

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

Referenced by ExecEvalSQLValueFunction().

270 {
272 }
Datum namein(PG_FUNCTION_ARGS)
Definition: name.c:48
#define DirectFunctionCall1(func, arg1)
Definition: fmgr.h:624
Oid GetSessionUserId(void)
Definition: miscinit.c:510
#define CStringGetDatum(X)
Definition: postgres.h:578
#define PG_RETURN_DATUM(x)
Definition: fmgr.h:352
char * GetUserNameFromId(Oid roleid, bool noerr)
Definition: miscinit.c:891