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.

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_GETARG_NAME(n)
Definition: fmgr.h:278
#define PG_RETURN_INT32(x)
Definition: fmgr.h:354
#define PG_GET_COLLATION()
Definition: fmgr.h:198
static int namecmp(Name arg1, Name arg2, Oid collid)
Definition: name.c:135
Definition: c.h:725

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

◆ btnamesortsupport()

Datum btnamesortsupport ( PG_FUNCTION_ARGS  )

Definition at line 211 of file name.c.

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 }
Oid collid
#define PG_RETURN_VOID()
Definition: fmgr.h:349
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:276
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:138
unsigned int Oid
Definition: postgres_ext.h:31
struct SortSupportData * SortSupport
Definition: sortsupport.h:58
MemoryContext ssup_cxt
Definition: sortsupport.h:66
void varstr_sortsupport(SortSupport ssup, Oid typid, Oid collid)
Definition: varlena.c:2037

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

◆ current_schema()

Datum current_schema ( PG_FUNCTION_ARGS  )

Definition at line 279 of file name.c.

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 DirectFunctionCall1(func, arg1)
Definition: fmgr.h:642
#define PG_RETURN_NULL()
Definition: fmgr.h:345
#define PG_RETURN_DATUM(x)
Definition: fmgr.h:353
void list_free(List *list)
Definition: list.c:1545
char * get_namespace_name(Oid nspid)
Definition: lsyscache.c:3331
Datum namein(PG_FUNCTION_ARGS)
Definition: name.c:48
List * fetch_search_path(bool includeImplicit)
Definition: namespace.c:4428
#define NIL
Definition: pg_list.h:68
#define linitial_oid(l)
Definition: pg_list.h:180
static Datum CStringGetDatum(const char *X)
Definition: postgres.h:350
Definition: pg_list.h:54

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

◆ current_schemas()

Datum current_schemas ( PG_FUNCTION_ARGS  )

Definition at line 294 of file name.c.

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_builtin(names, i, NAMEOID);
318 
319  PG_RETURN_POINTER(array);
320 }
ArrayType * construct_array_builtin(Datum *elems, int nelems, Oid elmtype)
Definition: arrayfuncs.c:3363
#define PG_GETARG_BOOL(n)
Definition: fmgr.h:274
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:361
int i
Definition: isn.c:73
void * palloc(Size size)
Definition: mcxt.c:1210
static int list_length(const List *l)
Definition: pg_list.h:152
#define lfirst_oid(lc)
Definition: pg_list.h:174
uintptr_t Datum
Definition: postgres.h:64

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

◆ current_user()

Datum current_user ( PG_FUNCTION_ARGS  )

Definition at line 263 of file name.c.

264 {
266 }
char * GetUserNameFromId(Oid roleid, bool noerr)
Definition: miscinit.c:972
Oid GetUserId(void)
Definition: miscinit.c:502

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

◆ namecmp()

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

Definition at line 135 of file name.c.

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 NameStr(name)
Definition: c.h:730
#define NAMEDATALEN
int varstr_cmp(const char *arg1, int len1, const char *arg2, int len2, Oid collid)
Definition: varlena.c:1543

References collid, NAMEDATALEN, NameStr, and varstr_cmp().

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

◆ nameconcatoid()

Datum nameconcatoid ( PG_FUNCTION_ARGS  )

Definition at line 333 of file name.c.

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

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

◆ nameeq()

Datum nameeq ( PG_FUNCTION_ARGS  )

Definition at line 148 of file name.c.

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_RETURN_BOOL(x)
Definition: fmgr.h:359

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

◆ namege()

Datum namege ( PG_FUNCTION_ARGS  )

Definition at line 193 of file name.c.

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 }

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

◆ namegt()

Datum namegt ( PG_FUNCTION_ARGS  )

Definition at line 184 of file name.c.

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 }

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

◆ namein()

◆ namele()

Datum namele ( PG_FUNCTION_ARGS  )

Definition at line 175 of file name.c.

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 }

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

◆ namelt()

Datum namelt ( PG_FUNCTION_ARGS  )

Definition at line 166 of file name.c.

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 }

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

◆ namene()

Datum namene ( PG_FUNCTION_ARGS  )

Definition at line 157 of file name.c.

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 }

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

◆ nameout()

Datum nameout ( PG_FUNCTION_ARGS  )

Definition at line 71 of file name.c.

72 {
73  Name s = PG_GETARG_NAME(0);
74 
76 }
#define PG_RETURN_CSTRING(x)
Definition: fmgr.h:362
char * pstrdup(const char *in)
Definition: mcxt.c:1624

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

Referenced by make_greater_string(), and RelationBuildTriggers().

◆ namerecv()

Datum namerecv ( PG_FUNCTION_ARGS  )

Definition at line 82 of file name.c.

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 }
int errdetail(const char *fmt,...)
Definition: elog.c:1202
int errcode(int sqlerrcode)
Definition: elog.c:858
int errmsg(const char *fmt,...)
Definition: elog.c:1069
#define ERROR
Definition: elog.h:39
#define ereport(elevel,...)
Definition: elog.h:149
void pfree(void *pointer)
Definition: mcxt.c:1436
static char * buf
Definition: pg_test_fsync.c:67
char * pq_getmsgtext(StringInfo msg, int rawbytes, int *nbytes)
Definition: pqformat.c:549
StringInfoData * StringInfo
Definition: stringinfo.h:44

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

◆ namesend()

Datum namesend ( PG_FUNCTION_ARGS  )

Definition at line 106 of file name.c.

107 {
108  Name s = PG_GETARG_NAME(0);
110 
112  pq_sendtext(&buf, NameStr(*s), strlen(NameStr(*s)));
114 }
#define PG_RETURN_BYTEA_P(x)
Definition: fmgr.h:371
void pq_sendtext(StringInfo buf, const char *str, int slen)
Definition: pqformat.c:175
void pq_begintypsend(StringInfo buf)
Definition: pqformat.c:329
bytea * pq_endtypsend(StringInfo buf)
Definition: pqformat.c:349

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

◆ namestrcmp()

int namestrcmp ( Name  name,
const char *  str 
)

Definition at line 247 of file name.c.

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 }
const char * name
Definition: encode.c:571

References name, NAMEDATALEN, NameStr, and generate_unaccent_rules::str.

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

◆ namestrcpy()

◆ session_user()