PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
name.c File Reference
#include "postgres.h"
#include "catalog/namespace.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 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)
 
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)
 
int namecpy (Name n1, Name 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)
 

Function Documentation

Datum current_schema ( PG_FUNCTION_ARGS  )

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

281 {
282  List *search_path = fetch_search_path(false);
283  char *nspname;
284 
285  if (search_path == NIL)
286  PG_RETURN_NULL();
287  nspname = get_namespace_name(linitial_oid(search_path));
288  list_free(search_path);
289  if (!nspname)
290  PG_RETURN_NULL(); /* recently-deleted namespace? */
292 }
#define NIL
Definition: pg_list.h:69
Datum namein(PG_FUNCTION_ARGS)
Definition: name.c:46
#define DirectFunctionCall1(func, arg1)
Definition: fmgr.h:584
char * get_namespace_name(Oid nspid)
Definition: lsyscache.c:3038
#define CStringGetDatum(X)
Definition: postgres.h:584
#define PG_RETURN_DATUM(x)
Definition: fmgr.h:313
#define linitial_oid(l)
Definition: pg_list.h:113
void list_free(List *list)
Definition: list.c:1133
Definition: pg_list.h:45
List * fetch_search_path(bool includeImplicit)
Definition: namespace.c:4074
#define PG_RETURN_NULL()
Definition: fmgr.h:305
Datum current_schemas ( PG_FUNCTION_ARGS  )

Definition at line 295 of file name.c.

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

296 {
297  List *search_path = fetch_search_path(PG_GETARG_BOOL(0));
298  ListCell *l;
299  Datum *names;
300  int i;
301  ArrayType *array;
302 
303  names = (Datum *) palloc(list_length(search_path) * sizeof(Datum));
304  i = 0;
305  foreach(l, search_path)
306  {
307  char *nspname;
308 
309  nspname = get_namespace_name(lfirst_oid(l));
310  if (nspname) /* watch out for deleted namespace */
311  {
312  names[i] = DirectFunctionCall1(namein, CStringGetDatum(nspname));
313  i++;
314  }
315  }
316  list_free(search_path);
317 
318  array = construct_array(names, i,
319  NAMEOID,
320  NAMEDATALEN, /* sizeof(Name) */
321  false, /* Name is not by-val */
322  'c'); /* alignment of Name */
323 
324  PG_RETURN_POINTER(array);
325 }
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:321
Datum namein(PG_FUNCTION_ARGS)
Definition: name.c:46
#define NAMEOID
Definition: pg_type.h:300
ArrayType * construct_array(Datum *elems, int nelems, Oid elmtype, int elmlen, bool elmbyval, char elmalign)
Definition: arrayfuncs.c:3306
#define DirectFunctionCall1(func, arg1)
Definition: fmgr.h:584
#define PG_GETARG_BOOL(n)
Definition: fmgr.h:239
#define NAMEDATALEN
char * get_namespace_name(Oid nspid)
Definition: lsyscache.c:3038
#define CStringGetDatum(X)
Definition: postgres.h:584
uintptr_t Datum
Definition: postgres.h:372
static int list_length(const List *l)
Definition: pg_list.h:89
void * palloc(Size size)
Definition: mcxt.c:849
void list_free(List *list)
Definition: list.c:1133
int i
Definition: pg_list.h:45
List * fetch_search_path(bool includeImplicit)
Definition: namespace.c:4074
#define lfirst_oid(lc)
Definition: pg_list.h:108
Datum current_user ( PG_FUNCTION_ARGS  )

Definition at line 264 of file name.c.

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

Referenced by ExecEvalSQLValueFunction().

265 {
267 }
Datum namein(PG_FUNCTION_ARGS)
Definition: name.c:46
Oid GetUserId(void)
Definition: miscinit.c:283
#define DirectFunctionCall1(func, arg1)
Definition: fmgr.h:584
#define CStringGetDatum(X)
Definition: postgres.h:584
#define PG_RETURN_DATUM(x)
Definition: fmgr.h:313
char * GetUserNameFromId(Oid roleid, bool noerr)
Definition: miscinit.c:691
int namecpy ( Name  n1,
Name  n2 
)

Definition at line 191 of file name.c.

References NAMEDATALEN, NameStr, and StrNCpy.

Referenced by pg_get_replication_slots().

192 {
193  if (!n1 || !n2)
194  return -1;
195  StrNCpy(NameStr(*n1), NameStr(*n2), NAMEDATALEN);
196  return 0;
197 }
#define NAMEDATALEN
#define StrNCpy(dst, src, len)
Definition: c.h:830
#define NameStr(name)
Definition: c.h:499
Datum nameeq ( PG_FUNCTION_ARGS  )

Definition at line 134 of file name.c.

References NAMEDATALEN, NameStr, PG_GETARG_NAME, and PG_RETURN_BOOL.

135 {
136  Name arg1 = PG_GETARG_NAME(0);
137  Name arg2 = PG_GETARG_NAME(1);
138 
139  PG_RETURN_BOOL(strncmp(NameStr(*arg1), NameStr(*arg2), NAMEDATALEN) == 0);
140 }
#define NAMEDATALEN
Definition: c.h:493
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:319
#define NameStr(name)
Definition: c.h:499
#define PG_GETARG_NAME(n)
Definition: fmgr.h:243
Datum namege ( PG_FUNCTION_ARGS  )

Definition at line 179 of file name.c.

References NAMEDATALEN, NameStr, PG_GETARG_NAME, and PG_RETURN_BOOL.

180 {
181  Name arg1 = PG_GETARG_NAME(0);
182  Name arg2 = PG_GETARG_NAME(1);
183 
184  PG_RETURN_BOOL(strncmp(NameStr(*arg1), NameStr(*arg2), NAMEDATALEN) >= 0);
185 }
#define NAMEDATALEN
Definition: c.h:493
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:319
#define NameStr(name)
Definition: c.h:499
#define PG_GETARG_NAME(n)
Definition: fmgr.h:243
Datum namegt ( PG_FUNCTION_ARGS  )

Definition at line 170 of file name.c.

References NAMEDATALEN, NameStr, PG_GETARG_NAME, and PG_RETURN_BOOL.

171 {
172  Name arg1 = PG_GETARG_NAME(0);
173  Name arg2 = PG_GETARG_NAME(1);
174 
175  PG_RETURN_BOOL(strncmp(NameStr(*arg1), NameStr(*arg2), NAMEDATALEN) > 0);
176 }
#define NAMEDATALEN
Definition: c.h:493
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:319
#define NameStr(name)
Definition: c.h:499
#define PG_GETARG_NAME(n)
Definition: fmgr.h:243
Datum namein ( PG_FUNCTION_ARGS  )

Definition at line 46 of file name.c.

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

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_exec_trigger(), RenameRole(), session_user(), and string_to_datum().

47 {
48  char *s = PG_GETARG_CSTRING(0);
49  Name result;
50  int len;
51 
52  len = strlen(s);
53 
54  /* Truncate oversize input */
55  if (len >= NAMEDATALEN)
56  len = pg_mbcliplen(s, len, NAMEDATALEN - 1);
57 
58  /* We use palloc0 here to ensure result is zero-padded */
59  result = (Name) palloc0(NAMEDATALEN);
60  memcpy(NameStr(*result), s, len);
61 
62  PG_RETURN_NAME(result);
63 }
return result
Definition: formatting.c:1618
#define NAMEDATALEN
int pg_mbcliplen(const char *mbstr, int len, int limit)
Definition: mbutils.c:831
Definition: c.h:493
void * palloc0(Size size)
Definition: mcxt.c:878
#define NameStr(name)
Definition: c.h:499
#define PG_GETARG_CSTRING(n)
Definition: fmgr.h:242
NameData * Name
Definition: c.h:497
#define PG_RETURN_NAME(x)
Definition: fmgr.h:323
Datum namele ( PG_FUNCTION_ARGS  )

Definition at line 161 of file name.c.

References NAMEDATALEN, NameStr, PG_GETARG_NAME, and PG_RETURN_BOOL.

162 {
163  Name arg1 = PG_GETARG_NAME(0);
164  Name arg2 = PG_GETARG_NAME(1);
165 
166  PG_RETURN_BOOL(strncmp(NameStr(*arg1), NameStr(*arg2), NAMEDATALEN) <= 0);
167 }
#define NAMEDATALEN
Definition: c.h:493
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:319
#define NameStr(name)
Definition: c.h:499
#define PG_GETARG_NAME(n)
Definition: fmgr.h:243
Datum namelt ( PG_FUNCTION_ARGS  )

Definition at line 152 of file name.c.

References NAMEDATALEN, NameStr, PG_GETARG_NAME, and PG_RETURN_BOOL.

153 {
154  Name arg1 = PG_GETARG_NAME(0);
155  Name arg2 = PG_GETARG_NAME(1);
156 
157  PG_RETURN_BOOL(strncmp(NameStr(*arg1), NameStr(*arg2), NAMEDATALEN) < 0);
158 }
#define NAMEDATALEN
Definition: c.h:493
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:319
#define NameStr(name)
Definition: c.h:499
#define PG_GETARG_NAME(n)
Definition: fmgr.h:243
Datum namene ( PG_FUNCTION_ARGS  )

Definition at line 143 of file name.c.

References NAMEDATALEN, NameStr, PG_GETARG_NAME, and PG_RETURN_BOOL.

144 {
145  Name arg1 = PG_GETARG_NAME(0);
146  Name arg2 = PG_GETARG_NAME(1);
147 
148  PG_RETURN_BOOL(strncmp(NameStr(*arg1), NameStr(*arg2), NAMEDATALEN) != 0);
149 }
#define NAMEDATALEN
Definition: c.h:493
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:319
#define NameStr(name)
Definition: c.h:499
#define PG_GETARG_NAME(n)
Definition: fmgr.h:243
Datum nameout ( PG_FUNCTION_ARGS  )

Definition at line 69 of file name.c.

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

Referenced by make_greater_string(), RelationBuildTriggers(), and set_timetravel().

70 {
71  Name s = PG_GETARG_NAME(0);
72 
74 }
char * pstrdup(const char *in)
Definition: mcxt.c:1077
Definition: c.h:493
#define PG_RETURN_CSTRING(x)
Definition: fmgr.h:322
#define NameStr(name)
Definition: c.h:499
#define PG_GETARG_NAME(n)
Definition: fmgr.h:243
Datum namerecv ( PG_FUNCTION_ARGS  )

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

81 {
83  Name result;
84  char *str;
85  int nbytes;
86 
87  str = pq_getmsgtext(buf, buf->len - buf->cursor, &nbytes);
88  if (nbytes >= NAMEDATALEN)
89  ereport(ERROR,
90  (errcode(ERRCODE_NAME_TOO_LONG),
91  errmsg("identifier too long"),
92  errdetail("Identifier must be less than %d characters.",
93  NAMEDATALEN)));
94  result = (NameData *) palloc0(NAMEDATALEN);
95  memcpy(result, str, nbytes);
96  pfree(str);
97  PG_RETURN_NAME(result);
98 }
StringInfoData * StringInfo
Definition: stringinfo.h:46
int errcode(int sqlerrcode)
Definition: elog.c:575
return result
Definition: formatting.c:1618
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:241
#define NAMEDATALEN
void pfree(void *pointer)
Definition: mcxt.c:950
#define ERROR
Definition: elog.h:43
Definition: c.h:493
static char * buf
Definition: pg_test_fsync.c:66
int errdetail(const char *fmt,...)
Definition: elog.c:873
#define ereport(elevel, rest)
Definition: elog.h:122
char * pq_getmsgtext(StringInfo msg, int rawbytes, int *nbytes)
Definition: pqformat.c:588
void * palloc0(Size size)
Definition: mcxt.c:878
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define PG_RETURN_NAME(x)
Definition: fmgr.h:323
Datum namesend ( PG_FUNCTION_ARGS  )

Definition at line 104 of file name.c.

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

105 {
106  Name s = PG_GETARG_NAME(0);
108 
109  pq_begintypsend(&buf);
110  pq_sendtext(&buf, NameStr(*s), strlen(NameStr(*s)));
112 }
void pq_begintypsend(StringInfo buf)
Definition: pqformat.c:359
void pq_sendtext(StringInfo buf, const char *str, int slen)
Definition: pqformat.c:163
#define PG_RETURN_BYTEA_P(x)
Definition: fmgr.h:329
bytea * pq_endtypsend(StringInfo buf)
Definition: pqformat.c:379
Definition: c.h:493
static char * buf
Definition: pg_test_fsync.c:66
#define NameStr(name)
Definition: c.h:499
#define PG_GETARG_NAME(n)
Definition: fmgr.h:243
int namestrcmp ( Name  name,
const char *  str 
)

Definition at line 248 of file name.c.

References NameStr.

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

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

Definition at line 270 of file name.c.

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

Referenced by ExecEvalSQLValueFunction().

271 {
273 }
Datum namein(PG_FUNCTION_ARGS)
Definition: name.c:46
#define DirectFunctionCall1(func, arg1)
Definition: fmgr.h:584
Oid GetSessionUserId(void)
Definition: miscinit.c:317
#define CStringGetDatum(X)
Definition: postgres.h:584
#define PG_RETURN_DATUM(x)
Definition: fmgr.h:313
char * GetUserNameFromId(Oid roleid, bool noerr)
Definition: miscinit.c:691