PostgreSQL Source Code git master
misc.c File Reference
#include "postgres.h"
#include <sys/file.h>
#include <sys/stat.h>
#include <dirent.h>
#include <fcntl.h>
#include <math.h>
#include <unistd.h>
#include "access/htup_details.h"
#include "access/sysattr.h"
#include "access/table.h"
#include "catalog/pg_tablespace.h"
#include "catalog/pg_type.h"
#include "catalog/system_fk_info.h"
#include "commands/tablespace.h"
#include "common/keywords.h"
#include "funcapi.h"
#include "miscadmin.h"
#include "nodes/miscnodes.h"
#include "parser/parse_type.h"
#include "parser/scansup.h"
#include "pgstat.h"
#include "postmaster/syslogger.h"
#include "rewrite/rewriteHandler.h"
#include "storage/fd.h"
#include "storage/latch.h"
#include "tcop/tcopprot.h"
#include "utils/builtins.h"
#include "utils/fmgroids.h"
#include "utils/lsyscache.h"
#include "utils/ruleutils.h"
#include "utils/syscache.h"
#include "utils/timestamp.h"
Include dependency graph for misc.c:

Go to the source code of this file.

Data Structures

struct  ValidIOData
 

Macros

#define REQ_EVENTS   ((1 << CMD_UPDATE) | (1 << CMD_DELETE))
 

Typedefs

typedef struct ValidIOData ValidIOData
 

Functions

static bool pg_input_is_valid_common (FunctionCallInfo fcinfo, text *txt, text *typname, ErrorSaveContext *escontext)
 
static bool count_nulls (FunctionCallInfo fcinfo, int32 *nargs, int32 *nulls)
 
Datum pg_num_nulls (PG_FUNCTION_ARGS)
 
Datum pg_num_nonnulls (PG_FUNCTION_ARGS)
 
Datum pg_error_on_null (PG_FUNCTION_ARGS)
 
Datum current_database (PG_FUNCTION_ARGS)
 
Datum current_query (PG_FUNCTION_ARGS)
 
Datum pg_tablespace_databases (PG_FUNCTION_ARGS)
 
Datum pg_tablespace_location (PG_FUNCTION_ARGS)
 
Datum pg_sleep (PG_FUNCTION_ARGS)
 
Datum pg_get_keywords (PG_FUNCTION_ARGS)
 
Datum pg_get_catalog_foreign_keys (PG_FUNCTION_ARGS)
 
Datum pg_typeof (PG_FUNCTION_ARGS)
 
Datum pg_basetype (PG_FUNCTION_ARGS)
 
Datum pg_collation_for (PG_FUNCTION_ARGS)
 
Datum pg_relation_is_updatable (PG_FUNCTION_ARGS)
 
Datum pg_column_is_updatable (PG_FUNCTION_ARGS)
 
Datum pg_input_is_valid (PG_FUNCTION_ARGS)
 
Datum pg_input_error_info (PG_FUNCTION_ARGS)
 
static bool is_ident_start (unsigned char c)
 
static bool is_ident_cont (unsigned char c)
 
Datum parse_ident (PG_FUNCTION_ARGS)
 
Datum pg_current_logfile (PG_FUNCTION_ARGS)
 
Datum pg_current_logfile_1arg (PG_FUNCTION_ARGS)
 
Datum pg_get_replica_identity_index (PG_FUNCTION_ARGS)
 
Datum any_value_transfn (PG_FUNCTION_ARGS)
 

Macro Definition Documentation

◆ REQ_EVENTS

#define REQ_EVENTS   ((1 << CMD_UPDATE) | (1 << CMD_DELETE))

Typedef Documentation

◆ ValidIOData

typedef struct ValidIOData ValidIOData

Function Documentation

◆ any_value_transfn()

Datum any_value_transfn ( PG_FUNCTION_ARGS  )

Definition at line 1092 of file misc.c.

1093{
1095}
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:268
#define PG_RETURN_DATUM(x)
Definition: fmgr.h:353

References PG_GETARG_DATUM, and PG_RETURN_DATUM.

◆ count_nulls()

static bool count_nulls ( FunctionCallInfo  fcinfo,
int32 nargs,
int32 nulls 
)
static

Definition at line 76 of file misc.c.

78{
79 int32 count = 0;
80 int i;
81
82 /* Did we get a VARIADIC array argument, or separate arguments? */
83 if (get_fn_expr_variadic(fcinfo->flinfo))
84 {
85 ArrayType *arr;
86 int ndims,
87 nitems,
88 *dims;
89 bits8 *bitmap;
90
91 Assert(PG_NARGS() == 1);
92
93 /*
94 * If we get a null as VARIADIC array argument, we can't say anything
95 * useful about the number of elements, so return NULL. This behavior
96 * is consistent with other variadic functions - see concat_internal.
97 */
98 if (PG_ARGISNULL(0))
99 return false;
100
101 /*
102 * Non-null argument had better be an array. We assume that any call
103 * context that could let get_fn_expr_variadic return true will have
104 * checked that a VARIADIC-labeled parameter actually is an array. So
105 * it should be okay to just Assert that it's an array rather than
106 * doing a full-fledged error check.
107 */
109
110 /* OK, safe to fetch the array value */
111 arr = PG_GETARG_ARRAYTYPE_P(0);
112
113 /* Count the array elements */
114 ndims = ARR_NDIM(arr);
115 dims = ARR_DIMS(arr);
116 nitems = ArrayGetNItems(ndims, dims);
117
118 /* Count those that are NULL */
119 bitmap = ARR_NULLBITMAP(arr);
120 if (bitmap)
121 {
122 int bitmask = 1;
123
124 for (i = 0; i < nitems; i++)
125 {
126 if ((*bitmap & bitmask) == 0)
127 count++;
128
129 bitmask <<= 1;
130 if (bitmask == 0x100)
131 {
132 bitmap++;
133 bitmask = 1;
134 }
135 }
136 }
137
138 *nargs = nitems;
139 *nulls = count;
140 }
141 else
142 {
143 /* Separate arguments, so just count 'em */
144 for (i = 0; i < PG_NARGS(); i++)
145 {
146 if (PG_ARGISNULL(i))
147 count++;
148 }
149
150 *nargs = PG_NARGS();
151 *nulls = count;
152 }
153
154 return true;
155}
#define ARR_NDIM(a)
Definition: array.h:290
#define PG_GETARG_ARRAYTYPE_P(n)
Definition: array.h:263
#define ARR_NULLBITMAP(a)
Definition: array.h:300
#define ARR_DIMS(a)
Definition: array.h:294
int ArrayGetNItems(int ndim, const int *dims)
Definition: arrayutils.c:57
uint8 bits8
Definition: c.h:548
int32_t int32
Definition: c.h:537
#define OidIsValid(objectId)
Definition: c.h:777
bool get_fn_expr_variadic(FmgrInfo *flinfo)
Definition: fmgr.c:2009
Oid get_fn_expr_argtype(FmgrInfo *flinfo, int argnum)
Definition: fmgr.c:1875
#define PG_ARGISNULL(n)
Definition: fmgr.h:209
#define PG_NARGS()
Definition: fmgr.h:203
Assert(PointerIsAligned(start, uint64))
#define nitems(x)
Definition: indent.h:31
int i
Definition: isn.c:77
Oid get_base_element_type(Oid typid)
Definition: lsyscache.c:2999
FmgrInfo * flinfo
Definition: fmgr.h:87

References ARR_DIMS, ARR_NDIM, ARR_NULLBITMAP, ArrayGetNItems(), Assert(), FunctionCallInfoBaseData::flinfo, get_base_element_type(), get_fn_expr_argtype(), get_fn_expr_variadic(), i, nitems, OidIsValid, PG_ARGISNULL, PG_GETARG_ARRAYTYPE_P, and PG_NARGS.

Referenced by pg_num_nonnulls(), and pg_num_nulls().

◆ current_database()

Datum current_database ( PG_FUNCTION_ARGS  )

Definition at line 209 of file misc.c.

210{
211 Name db;
212
213 db = (Name) palloc(NAMEDATALEN);
214
216 PG_RETURN_NAME(db);
217}
NameData * Name
Definition: c.h:752
#define PG_RETURN_NAME(x)
Definition: fmgr.h:363
Oid MyDatabaseId
Definition: globals.c:94
char * get_database_name(Oid dbid)
Definition: lsyscache.c:1259
void * palloc(Size size)
Definition: mcxt.c:1365
void namestrcpy(Name name, const char *str)
Definition: name.c:233
#define NAMEDATALEN
Definition: c.h:749

References get_database_name(), MyDatabaseId, NAMEDATALEN, namestrcpy(), palloc(), and PG_RETURN_NAME.

Referenced by ExecEvalSQLValueFunction().

◆ current_query()

Datum current_query ( PG_FUNCTION_ARGS  )

Definition at line 226 of file misc.c.

227{
228 /* there is no easy way to access the more concise 'query_string' */
231 else
233}
#define PG_RETURN_NULL()
Definition: fmgr.h:345
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:372
const char * debug_query_string
Definition: postgres.c:89
text * cstring_to_text(const char *s)
Definition: varlena.c:181

References cstring_to_text(), debug_query_string, PG_RETURN_NULL, and PG_RETURN_TEXT_P.

Referenced by dblink_current_query().

◆ is_ident_cont()

static bool is_ident_cont ( unsigned char  c)
static

Definition at line 817 of file misc.c.

818{
819 /* Can be digit or dollar sign ... */
820 if ((c >= '0' && c <= '9') || c == '$')
821 return true;
822 /* ... or an identifier start character */
823 return is_ident_start(c);
824}
static bool is_ident_start(unsigned char c)
Definition: misc.c:799
char * c

References is_ident_start().

Referenced by parse_ident().

◆ is_ident_start()

static bool is_ident_start ( unsigned char  c)
static

Definition at line 799 of file misc.c.

800{
801 /* Underscores and ASCII letters are OK */
802 if (c == '_')
803 return true;
804 if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'))
805 return true;
806 /* Any high-bit-set character is OK (might be part of a multibyte char) */
807 if (IS_HIGHBIT_SET(c))
808 return true;
809 return false;
810}
#define IS_HIGHBIT_SET(ch)
Definition: c.h:1143

References IS_HIGHBIT_SET.

Referenced by is_ident_cont(), and parse_ident().

◆ parse_ident()

Datum parse_ident ( PG_FUNCTION_ARGS  )

Definition at line 832 of file misc.c.

833{
834 text *qualname = PG_GETARG_TEXT_PP(0);
835 bool strict = PG_GETARG_BOOL(1);
836 char *qualname_str = text_to_cstring(qualname);
837 ArrayBuildState *astate = NULL;
838 char *nextp;
839 bool after_dot = false;
840
841 /*
842 * The code below scribbles on qualname_str in some cases, so we should
843 * reconvert qualname if we need to show the original string in error
844 * messages.
845 */
846 nextp = qualname_str;
847
848 /* skip leading whitespace */
849 while (scanner_isspace(*nextp))
850 nextp++;
851
852 for (;;)
853 {
854 char *curname;
855 bool missing_ident = true;
856
857 if (*nextp == '"')
858 {
859 char *endp;
860
861 curname = nextp + 1;
862 for (;;)
863 {
864 endp = strchr(nextp + 1, '"');
865 if (endp == NULL)
867 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
868 errmsg("string is not a valid identifier: \"%s\"",
869 text_to_cstring(qualname)),
870 errdetail("String has unclosed double quotes.")));
871 if (endp[1] != '"')
872 break;
873 memmove(endp, endp + 1, strlen(endp));
874 nextp = endp;
875 }
876 nextp = endp + 1;
877 *endp = '\0';
878
879 if (endp - curname == 0)
881 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
882 errmsg("string is not a valid identifier: \"%s\"",
883 text_to_cstring(qualname)),
884 errdetail("Quoted identifier must not be empty.")));
885
886 astate = accumArrayResult(astate, CStringGetTextDatum(curname),
887 false, TEXTOID, CurrentMemoryContext);
888 missing_ident = false;
889 }
890 else if (is_ident_start((unsigned char) *nextp))
891 {
892 char *downname;
893 int len;
894 text *part;
895
896 curname = nextp++;
897 while (is_ident_cont((unsigned char) *nextp))
898 nextp++;
899
900 len = nextp - curname;
901
902 /*
903 * We don't implicitly truncate identifiers. This is useful for
904 * allowing the user to check for specific parts of the identifier
905 * being too long. It's easy enough for the user to get the
906 * truncated names by casting our output to name[].
907 */
908 downname = downcase_identifier(curname, len, false, false);
909 part = cstring_to_text_with_len(downname, len);
910 astate = accumArrayResult(astate, PointerGetDatum(part), false,
911 TEXTOID, CurrentMemoryContext);
912 missing_ident = false;
913 }
914
915 if (missing_ident)
916 {
917 /* Different error messages based on where we failed. */
918 if (*nextp == '.')
920 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
921 errmsg("string is not a valid identifier: \"%s\"",
922 text_to_cstring(qualname)),
923 errdetail("No valid identifier before \".\".")));
924 else if (after_dot)
926 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
927 errmsg("string is not a valid identifier: \"%s\"",
928 text_to_cstring(qualname)),
929 errdetail("No valid identifier after \".\".")));
930 else
932 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
933 errmsg("string is not a valid identifier: \"%s\"",
934 text_to_cstring(qualname))));
935 }
936
937 while (scanner_isspace(*nextp))
938 nextp++;
939
940 if (*nextp == '.')
941 {
942 after_dot = true;
943 nextp++;
944 while (scanner_isspace(*nextp))
945 nextp++;
946 }
947 else if (*nextp == '\0')
948 {
949 break;
950 }
951 else
952 {
953 if (strict)
955 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
956 errmsg("string is not a valid identifier: \"%s\"",
957 text_to_cstring(qualname))));
958 break;
959 }
960 }
961
963}
ArrayBuildState * accumArrayResult(ArrayBuildState *astate, Datum dvalue, bool disnull, Oid element_type, MemoryContext rcontext)
Definition: arrayfuncs.c:5351
Datum makeArrayResult(ArrayBuildState *astate, MemoryContext rcontext)
Definition: arrayfuncs.c:5421
static bool is_ident_cont(unsigned char c)
Definition: misc.c:817
#define CStringGetTextDatum(s)
Definition: builtins.h:97
int errdetail(const char *fmt,...)
Definition: elog.c:1216
int errcode(int sqlerrcode)
Definition: elog.c:863
int errmsg(const char *fmt,...)
Definition: elog.c:1080
#define ERROR
Definition: elog.h:39
#define ereport(elevel,...)
Definition: elog.h:150
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:309
#define PG_GETARG_BOOL(n)
Definition: fmgr.h:274
MemoryContext CurrentMemoryContext
Definition: mcxt.c:160
const void size_t len
static Datum PointerGetDatum(const void *X)
Definition: postgres.h:332
char * downcase_identifier(const char *ident, int len, bool warn, bool truncate)
Definition: scansup.c:46
bool scanner_isspace(char ch)
Definition: scansup.c:117
Definition: c.h:695
text * cstring_to_text_with_len(const char *s, int len)
Definition: varlena.c:193
char * text_to_cstring(const text *t)
Definition: varlena.c:214

References accumArrayResult(), cstring_to_text_with_len(), CStringGetTextDatum, CurrentMemoryContext, downcase_identifier(), ereport, errcode(), errdetail(), errmsg(), ERROR, is_ident_cont(), is_ident_start(), len, makeArrayResult(), PG_GETARG_BOOL, PG_GETARG_TEXT_PP, PG_RETURN_DATUM, PointerGetDatum(), scanner_isspace(), and text_to_cstring().

◆ pg_basetype()

Datum pg_basetype ( PG_FUNCTION_ARGS  )

Definition at line 554 of file misc.c.

555{
556 Oid typid = PG_GETARG_OID(0);
557
558 /*
559 * We loop to find the bottom base type in a stack of domains.
560 */
561 for (;;)
562 {
563 HeapTuple tup;
564 Form_pg_type typTup;
565
566 tup = SearchSysCache1(TYPEOID, ObjectIdGetDatum(typid));
567 if (!HeapTupleIsValid(tup))
568 PG_RETURN_NULL(); /* return NULL for bogus OID */
569 typTup = (Form_pg_type) GETSTRUCT(tup);
570 if (typTup->typtype != TYPTYPE_DOMAIN)
571 {
572 /* Not a domain, so done */
573 ReleaseSysCache(tup);
574 break;
575 }
576
577 typid = typTup->typbasetype;
578 ReleaseSysCache(tup);
579 }
580
581 PG_RETURN_OID(typid);
582}
#define PG_GETARG_OID(n)
Definition: fmgr.h:275
#define PG_RETURN_OID(x)
Definition: fmgr.h:360
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
static void * GETSTRUCT(const HeapTupleData *tuple)
Definition: htup_details.h:728
FormData_pg_type * Form_pg_type
Definition: pg_type.h:261
static Datum ObjectIdGetDatum(Oid X)
Definition: postgres.h:262
unsigned int Oid
Definition: postgres_ext.h:32
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:264
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Definition: syscache.c:220

References GETSTRUCT(), HeapTupleIsValid, ObjectIdGetDatum(), PG_GETARG_OID, PG_RETURN_NULL, PG_RETURN_OID, ReleaseSysCache(), and SearchSysCache1().

◆ pg_collation_for()

Datum pg_collation_for ( PG_FUNCTION_ARGS  )

Definition at line 590 of file misc.c.

591{
592 Oid typeid;
593 Oid collid;
594
595 typeid = get_fn_expr_argtype(fcinfo->flinfo, 0);
596 if (!typeid)
598 if (!type_is_collatable(typeid) && typeid != UNKNOWNOID)
600 (errcode(ERRCODE_DATATYPE_MISMATCH),
601 errmsg("collations are not supported by type %s",
602 format_type_be(typeid))));
603
605 if (!collid)
608}
Oid collid
#define PG_GET_COLLATION()
Definition: fmgr.h:198
char * format_type_be(Oid type_oid)
Definition: format_type.c:343
bool type_is_collatable(Oid typid)
Definition: lsyscache.c:3248
char * generate_collation_name(Oid collid)
Definition: ruleutils.c:13578

References collid, cstring_to_text(), ereport, errcode(), errmsg(), ERROR, format_type_be(), generate_collation_name(), get_fn_expr_argtype(), PG_GET_COLLATION, PG_RETURN_NULL, PG_RETURN_TEXT_P, and type_is_collatable().

◆ pg_column_is_updatable()

Datum pg_column_is_updatable ( PG_FUNCTION_ARGS  )

Definition at line 636 of file misc.c.

637{
638 Oid reloid = PG_GETARG_OID(0);
641 bool include_triggers = PG_GETARG_BOOL(2);
642 int events;
643
644 /* System columns are never updatable */
645 if (attnum <= 0)
646 PG_RETURN_BOOL(false);
647
648 events = relation_is_updatable(reloid, NIL, include_triggers,
649 bms_make_singleton(col));
650
651 /* We require both updatability and deletability of the relation */
652#define REQ_EVENTS ((1 << CMD_UPDATE) | (1 << CMD_DELETE))
653
654 PG_RETURN_BOOL((events & REQ_EVENTS) == REQ_EVENTS);
655}
int16 AttrNumber
Definition: attnum.h:21
#define REQ_EVENTS
Bitmapset * bms_make_singleton(int x)
Definition: bitmapset.c:216
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:359
#define PG_GETARG_INT16(n)
Definition: fmgr.h:271
int16 attnum
Definition: pg_attribute.h:74
#define NIL
Definition: pg_list.h:68
int relation_is_updatable(Oid reloid, List *outer_reloids, bool include_triggers, Bitmapset *include_cols)
#define FirstLowInvalidHeapAttributeNumber
Definition: sysattr.h:27

References attnum, bms_make_singleton(), FirstLowInvalidHeapAttributeNumber, NIL, PG_GETARG_BOOL, PG_GETARG_INT16, PG_GETARG_OID, PG_RETURN_BOOL, relation_is_updatable(), and REQ_EVENTS.

◆ pg_current_logfile()

Datum pg_current_logfile ( PG_FUNCTION_ARGS  )

Definition at line 971 of file misc.c.

972{
973 FILE *fd;
974 char lbuffer[MAXPGPATH];
975 char *logfmt;
976
977 /* The log format parameter is optional */
978 if (PG_NARGS() == 0 || PG_ARGISNULL(0))
979 logfmt = NULL;
980 else
981 {
983
984 if (strcmp(logfmt, "stderr") != 0 &&
985 strcmp(logfmt, "csvlog") != 0 &&
986 strcmp(logfmt, "jsonlog") != 0)
988 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
989 errmsg("log format \"%s\" is not supported", logfmt),
990 errhint("The supported log formats are \"stderr\", \"csvlog\", and \"jsonlog\".")));
991 }
992
994 if (fd == NULL)
995 {
996 if (errno != ENOENT)
999 errmsg("could not read file \"%s\": %m",
1002 }
1003
1004#ifdef WIN32
1005 /* syslogger.c writes CRLF line endings on Windows */
1006 _setmode(_fileno(fd), _O_TEXT);
1007#endif
1008
1009 /*
1010 * Read the file to gather current log filename(s) registered by the
1011 * syslogger.
1012 */
1013 while (fgets(lbuffer, sizeof(lbuffer), fd) != NULL)
1014 {
1015 char *log_format;
1016 char *log_filepath;
1017 char *nlpos;
1018
1019 /* Extract log format and log file path from the line. */
1020 log_format = lbuffer;
1021 log_filepath = strchr(lbuffer, ' ');
1022 if (log_filepath == NULL)
1023 {
1024 /* Uh oh. No space found, so file content is corrupted. */
1025 elog(ERROR,
1026 "missing space character in \"%s\"", LOG_METAINFO_DATAFILE);
1027 break;
1028 }
1029
1030 *log_filepath = '\0';
1031 log_filepath++;
1032 nlpos = strchr(log_filepath, '\n');
1033 if (nlpos == NULL)
1034 {
1035 /* Uh oh. No newline found, so file content is corrupted. */
1036 elog(ERROR,
1037 "missing newline character in \"%s\"", LOG_METAINFO_DATAFILE);
1038 break;
1039 }
1040 *nlpos = '\0';
1041
1042 if (logfmt == NULL || strcmp(logfmt, log_format) == 0)
1043 {
1044 FreeFile(fd);
1045 PG_RETURN_TEXT_P(cstring_to_text(log_filepath));
1046 }
1047 }
1048
1049 /* Close the current log filename file. */
1050 FreeFile(fd);
1051
1053}
int errcode_for_file_access(void)
Definition: elog.c:886
int errhint(const char *fmt,...)
Definition: elog.c:1330
#define elog(elevel,...)
Definition: elog.h:226
int FreeFile(FILE *file)
Definition: fd.c:2840
FILE * AllocateFile(const char *name, const char *mode)
Definition: fd.c:2641
#define MAXPGPATH
static int fd(const char *x, int i)
Definition: preproc-init.c:105
#define LOG_METAINFO_DATAFILE
Definition: syslogger.h:102

References AllocateFile(), cstring_to_text(), elog, ereport, errcode(), errcode_for_file_access(), errhint(), errmsg(), ERROR, fd(), FreeFile(), LOG_METAINFO_DATAFILE, MAXPGPATH, PG_ARGISNULL, PG_GETARG_TEXT_PP, PG_NARGS, PG_RETURN_NULL, PG_RETURN_TEXT_P, and text_to_cstring().

Referenced by pg_current_logfile_1arg().

◆ pg_current_logfile_1arg()

Datum pg_current_logfile_1arg ( PG_FUNCTION_ARGS  )

Definition at line 1063 of file misc.c.

1064{
1065 return pg_current_logfile(fcinfo);
1066}
Datum pg_current_logfile(PG_FUNCTION_ARGS)
Definition: misc.c:971

References pg_current_logfile().

◆ pg_error_on_null()

Datum pg_error_on_null ( PG_FUNCTION_ARGS  )

Definition at line 194 of file misc.c.

195{
196 if (PG_ARGISNULL(0))
198 (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
199 errmsg("null value not allowed")));
200
202}

References ereport, errcode(), errmsg(), ERROR, PG_ARGISNULL, PG_GETARG_DATUM, and PG_RETURN_DATUM.

◆ pg_get_catalog_foreign_keys()

Datum pg_get_catalog_foreign_keys ( PG_FUNCTION_ARGS  )

Definition at line 467 of file misc.c.

468{
469 FuncCallContext *funcctx;
470 FmgrInfo *arrayinp;
471
472 if (SRF_IS_FIRSTCALL())
473 {
474 MemoryContext oldcontext;
475 TupleDesc tupdesc;
476
477 funcctx = SRF_FIRSTCALL_INIT();
478 oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
479
480 if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE)
481 elog(ERROR, "return type must be a row type");
482 funcctx->tuple_desc = BlessTupleDesc(tupdesc);
483
484 /*
485 * We use array_in to convert the C strings in sys_fk_relationships[]
486 * to text arrays. But we cannot use DirectFunctionCallN to call
487 * array_in, and it wouldn't be very efficient if we could. Fill an
488 * FmgrInfo to use for the call.
489 */
490 arrayinp = (FmgrInfo *) palloc(sizeof(FmgrInfo));
491 fmgr_info(F_ARRAY_IN, arrayinp);
492 funcctx->user_fctx = arrayinp;
493
494 MemoryContextSwitchTo(oldcontext);
495 }
496
497 funcctx = SRF_PERCALL_SETUP();
498 arrayinp = (FmgrInfo *) funcctx->user_fctx;
499
500 if (funcctx->call_cntr < lengthof(sys_fk_relationships))
501 {
502 const SysFKRelationship *fkrel = &sys_fk_relationships[funcctx->call_cntr];
503 Datum values[6];
504 bool nulls[6];
505 HeapTuple tuple;
506
507 memset(nulls, false, sizeof(nulls));
508
509 values[0] = ObjectIdGetDatum(fkrel->fk_table);
510 values[1] = FunctionCall3(arrayinp,
511 CStringGetDatum(fkrel->fk_columns),
512 ObjectIdGetDatum(TEXTOID),
513 Int32GetDatum(-1));
514 values[2] = ObjectIdGetDatum(fkrel->pk_table);
515 values[3] = FunctionCall3(arrayinp,
516 CStringGetDatum(fkrel->pk_columns),
517 ObjectIdGetDatum(TEXTOID),
518 Int32GetDatum(-1));
519 values[4] = BoolGetDatum(fkrel->is_array);
520 values[5] = BoolGetDatum(fkrel->is_opt);
521
522 tuple = heap_form_tuple(funcctx->tuple_desc, values, nulls);
523
524 SRF_RETURN_NEXT(funcctx, HeapTupleGetDatum(tuple));
525 }
526
527 SRF_RETURN_DONE(funcctx);
528}
static Datum values[MAXATTR]
Definition: bootstrap.c:153
#define lengthof(array)
Definition: c.h:790
TupleDesc BlessTupleDesc(TupleDesc tupdesc)
Definition: execTuples.c:2260
void fmgr_info(Oid functionId, FmgrInfo *finfo)
Definition: fmgr.c:128
#define FunctionCall3(flinfo, arg1, arg2, arg3)
Definition: fmgr.h:704
TypeFuncClass get_call_result_type(FunctionCallInfo fcinfo, Oid *resultTypeId, TupleDesc *resultTupleDesc)
Definition: funcapi.c:276
#define SRF_IS_FIRSTCALL()
Definition: funcapi.h:304
#define SRF_PERCALL_SETUP()
Definition: funcapi.h:308
@ TYPEFUNC_COMPOSITE
Definition: funcapi.h:149
#define SRF_RETURN_NEXT(_funcctx, _result)
Definition: funcapi.h:310
#define SRF_FIRSTCALL_INIT()
Definition: funcapi.h:306
static Datum HeapTupleGetDatum(const HeapTupleData *tuple)
Definition: funcapi.h:230
#define SRF_RETURN_DONE(_funcctx)
Definition: funcapi.h:328
HeapTuple heap_form_tuple(TupleDesc tupleDescriptor, const Datum *values, const bool *isnull)
Definition: heaptuple.c:1117
if(TABLE==NULL||TABLE_index==NULL)
Definition: isn.c:81
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:124
static Datum BoolGetDatum(bool X)
Definition: postgres.h:112
uint64_t Datum
Definition: postgres.h:70
static Datum CStringGetDatum(const char *X)
Definition: postgres.h:360
static Datum Int32GetDatum(int32 X)
Definition: postgres.h:222
Definition: fmgr.h:57
void * user_fctx
Definition: funcapi.h:82
uint64 call_cntr
Definition: funcapi.h:65
MemoryContext multi_call_memory_ctx
Definition: funcapi.h:101
TupleDesc tuple_desc
Definition: funcapi.h:112

References BlessTupleDesc(), BoolGetDatum(), FuncCallContext::call_cntr, CStringGetDatum(), elog, ERROR, fmgr_info(), FunctionCall3, get_call_result_type(), heap_form_tuple(), HeapTupleGetDatum(), if(), Int32GetDatum(), lengthof, MemoryContextSwitchTo(), FuncCallContext::multi_call_memory_ctx, ObjectIdGetDatum(), palloc(), SRF_FIRSTCALL_INIT, SRF_IS_FIRSTCALL, SRF_PERCALL_SETUP, SRF_RETURN_DONE, SRF_RETURN_NEXT, FuncCallContext::tuple_desc, TYPEFUNC_COMPOSITE, FuncCallContext::user_fctx, and values.

◆ pg_get_keywords()

Datum pg_get_keywords ( PG_FUNCTION_ARGS  )

Definition at line 389 of file misc.c.

390{
391 FuncCallContext *funcctx;
392
393 if (SRF_IS_FIRSTCALL())
394 {
395 MemoryContext oldcontext;
396 TupleDesc tupdesc;
397
398 funcctx = SRF_FIRSTCALL_INIT();
399 oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
400
401 if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE)
402 elog(ERROR, "return type must be a row type");
403 funcctx->tuple_desc = tupdesc;
404 funcctx->attinmeta = TupleDescGetAttInMetadata(tupdesc);
405
406 MemoryContextSwitchTo(oldcontext);
407 }
408
409 funcctx = SRF_PERCALL_SETUP();
410
411 if (funcctx->call_cntr < ScanKeywords.num_keywords)
412 {
413 char *values[5];
414 HeapTuple tuple;
415
416 /* cast-away-const is ugly but alternatives aren't much better */
417 values[0] = unconstify(char *,
418 GetScanKeyword(funcctx->call_cntr,
419 &ScanKeywords));
420
421 switch (ScanKeywordCategories[funcctx->call_cntr])
422 {
424 values[1] = "U";
425 values[3] = _("unreserved");
426 break;
427 case COL_NAME_KEYWORD:
428 values[1] = "C";
429 values[3] = _("unreserved (cannot be function or type name)");
430 break;
432 values[1] = "T";
433 values[3] = _("reserved (can be function or type name)");
434 break;
435 case RESERVED_KEYWORD:
436 values[1] = "R";
437 values[3] = _("reserved");
438 break;
439 default: /* shouldn't be possible */
440 values[1] = NULL;
441 values[3] = NULL;
442 break;
443 }
444
445 if (ScanKeywordBareLabel[funcctx->call_cntr])
446 {
447 values[2] = "true";
448 values[4] = _("can be bare label");
449 }
450 else
451 {
452 values[2] = "false";
453 values[4] = _("requires AS");
454 }
455
456 tuple = BuildTupleFromCStrings(funcctx->attinmeta, values);
457
458 SRF_RETURN_NEXT(funcctx, HeapTupleGetDatum(tuple));
459 }
460
461 SRF_RETURN_DONE(funcctx);
462}
#define unconstify(underlying_type, expr)
Definition: c.h:1233
const uint8 ScanKeywordCategories[SCANKEYWORDS_NUM_KEYWORDS]
Definition: keywords.c:29
const bool ScanKeywordBareLabel[SCANKEYWORDS_NUM_KEYWORDS]
Definition: keywords.c:42
#define _(x)
Definition: elog.c:91
HeapTuple BuildTupleFromCStrings(AttInMetadata *attinmeta, char **values)
Definition: execTuples.c:2324
AttInMetadata * TupleDescGetAttInMetadata(TupleDesc tupdesc)
Definition: execTuples.c:2275
PGDLLIMPORT const ScanKeywordList ScanKeywords
#define COL_NAME_KEYWORD
Definition: keywords.h:21
#define UNRESERVED_KEYWORD
Definition: keywords.h:20
#define TYPE_FUNC_NAME_KEYWORD
Definition: keywords.h:22
#define RESERVED_KEYWORD
Definition: keywords.h:23
static const char * GetScanKeyword(int n, const ScanKeywordList *keywords)
Definition: kwlookup.h:39
AttInMetadata * attinmeta
Definition: funcapi.h:91
int num_keywords
Definition: kwlookup.h:30

References _, FuncCallContext::attinmeta, BuildTupleFromCStrings(), FuncCallContext::call_cntr, COL_NAME_KEYWORD, elog, ERROR, get_call_result_type(), GetScanKeyword(), HeapTupleGetDatum(), MemoryContextSwitchTo(), FuncCallContext::multi_call_memory_ctx, ScanKeywordList::num_keywords, RESERVED_KEYWORD, ScanKeywordBareLabel, ScanKeywordCategories, ScanKeywords, SRF_FIRSTCALL_INIT, SRF_IS_FIRSTCALL, SRF_PERCALL_SETUP, SRF_RETURN_DONE, SRF_RETURN_NEXT, FuncCallContext::tuple_desc, TupleDescGetAttInMetadata(), TYPE_FUNC_NAME_KEYWORD, TYPEFUNC_COMPOSITE, unconstify, UNRESERVED_KEYWORD, and values.

◆ pg_get_replica_identity_index()

Datum pg_get_replica_identity_index ( PG_FUNCTION_ARGS  )

Definition at line 1072 of file misc.c.

1073{
1074 Oid reloid = PG_GETARG_OID(0);
1075 Oid idxoid;
1076 Relation rel;
1077
1078 rel = table_open(reloid, AccessShareLock);
1079 idxoid = RelationGetReplicaIndex(rel);
1081
1082 if (OidIsValid(idxoid))
1083 PG_RETURN_OID(idxoid);
1084 else
1086}
#define AccessShareLock
Definition: lockdefs.h:36
Oid RelationGetReplicaIndex(Relation relation)
Definition: relcache.c:5072
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:126
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition: table.c:40

References AccessShareLock, OidIsValid, PG_GETARG_OID, PG_RETURN_NULL, PG_RETURN_OID, RelationGetReplicaIndex(), table_close(), and table_open().

◆ pg_input_error_info()

Datum pg_input_error_info ( PG_FUNCTION_ARGS  )

Definition at line 687 of file misc.c.

688{
689 text *txt = PG_GETARG_TEXT_PP(0);
691 ErrorSaveContext escontext = {T_ErrorSaveContext};
692 TupleDesc tupdesc;
693 Datum values[4];
694 bool isnull[4];
695
696 if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE)
697 elog(ERROR, "return type must be a row type");
698
699 /* Enable details_wanted */
700 escontext.details_wanted = true;
701
702 if (pg_input_is_valid_common(fcinfo, txt, typname,
703 &escontext))
704 memset(isnull, true, sizeof(isnull));
705 else
706 {
707 char *sqlstate;
708
709 Assert(escontext.error_occurred);
710 Assert(escontext.error_data != NULL);
711 Assert(escontext.error_data->message != NULL);
712
713 memset(isnull, false, sizeof(isnull));
714
716
717 if (escontext.error_data->detail != NULL)
719 else
720 isnull[1] = true;
721
722 if (escontext.error_data->hint != NULL)
723 values[2] = CStringGetTextDatum(escontext.error_data->hint);
724 else
725 isnull[2] = true;
726
727 sqlstate = unpack_sql_state(escontext.error_data->sqlerrcode);
728 values[3] = CStringGetTextDatum(sqlstate);
729 }
730
731 return HeapTupleGetDatum(heap_form_tuple(tupdesc, values, isnull));
732}
static bool pg_input_is_valid_common(FunctionCallInfo fcinfo, text *txt, text *typname, ErrorSaveContext *escontext)
Definition: misc.c:736
char * unpack_sql_state(int sql_state)
Definition: elog.c:3222
NameData typname
Definition: pg_type.h:41
int sqlerrcode
Definition: elog.h:431
char * detail
Definition: elog.h:433
char * message
Definition: elog.h:432
char * hint
Definition: elog.h:435
bool details_wanted
Definition: miscnodes.h:48
ErrorData * error_data
Definition: miscnodes.h:49
bool error_occurred
Definition: miscnodes.h:47

References Assert(), CStringGetTextDatum, ErrorData::detail, ErrorSaveContext::details_wanted, elog, ERROR, ErrorSaveContext::error_data, ErrorSaveContext::error_occurred, get_call_result_type(), heap_form_tuple(), HeapTupleGetDatum(), ErrorData::hint, ErrorData::message, PG_GETARG_TEXT_PP, pg_input_is_valid_common(), ErrorData::sqlerrcode, TYPEFUNC_COMPOSITE, typname, unpack_sql_state(), and values.

◆ pg_input_is_valid()

Datum pg_input_is_valid ( PG_FUNCTION_ARGS  )

Definition at line 667 of file misc.c.

668{
669 text *txt = PG_GETARG_TEXT_PP(0);
671 ErrorSaveContext escontext = {T_ErrorSaveContext};
672
674 &escontext));
675}

References PG_GETARG_TEXT_PP, pg_input_is_valid_common(), PG_RETURN_BOOL, and typname.

◆ pg_input_is_valid_common()

static bool pg_input_is_valid_common ( FunctionCallInfo  fcinfo,
text txt,
text typname,
ErrorSaveContext escontext 
)
static

Definition at line 736 of file misc.c.

739{
740 char *str = text_to_cstring(txt);
741 ValidIOData *my_extra;
742 Datum converted;
743
744 /*
745 * We arrange to look up the needed I/O info just once per series of
746 * calls, assuming the data type doesn't change underneath us.
747 */
748 my_extra = (ValidIOData *) fcinfo->flinfo->fn_extra;
749 if (my_extra == NULL)
750 {
751 fcinfo->flinfo->fn_extra =
753 sizeof(ValidIOData));
754 my_extra = (ValidIOData *) fcinfo->flinfo->fn_extra;
755 my_extra->typoid = InvalidOid;
756 /* Detect whether typname argument is constant. */
757 my_extra->typname_constant = get_fn_expr_arg_stable(fcinfo->flinfo, 1);
758 }
759
760 /*
761 * If the typname argument is constant, we only need to parse it the first
762 * time through.
763 */
764 if (my_extra->typoid == InvalidOid || !my_extra->typname_constant)
765 {
766 char *typnamestr = text_to_cstring(typname);
767 Oid typoid;
768
769 /* Parse type-name argument to obtain type OID and encoded typmod. */
770 (void) parseTypeString(typnamestr, &typoid, &my_extra->typmod, NULL);
771
772 /* Update type-specific info if typoid changed. */
773 if (my_extra->typoid != typoid)
774 {
775 getTypeInputInfo(typoid,
776 &my_extra->typiofunc,
777 &my_extra->typioparam);
778 fmgr_info_cxt(my_extra->typiofunc, &my_extra->inputproc,
779 fcinfo->flinfo->fn_mcxt);
780 my_extra->typoid = typoid;
781 }
782 }
783
784 /* Now we can try to perform the conversion. */
785 return InputFunctionCallSafe(&my_extra->inputproc,
786 str,
787 my_extra->typioparam,
788 my_extra->typmod,
789 (Node *) escontext,
790 &converted);
791}
bool get_fn_expr_arg_stable(FmgrInfo *flinfo, int argnum)
Definition: fmgr.c:1940
void fmgr_info_cxt(Oid functionId, FmgrInfo *finfo, MemoryContext mcxt)
Definition: fmgr.c:138
bool InputFunctionCallSafe(FmgrInfo *flinfo, char *str, Oid typioparam, int32 typmod, Node *escontext, Datum *result)
Definition: fmgr.c:1585
const char * str
void getTypeInputInfo(Oid type, Oid *typInput, Oid *typIOParam)
Definition: lsyscache.c:3041
void * MemoryContextAlloc(MemoryContext context, Size size)
Definition: mcxt.c:1229
bool parseTypeString(const char *str, Oid *typeid_p, int32 *typmod_p, Node *escontext)
Definition: parse_type.c:785
#define InvalidOid
Definition: postgres_ext.h:37
void * fn_extra
Definition: fmgr.h:64
MemoryContext fn_mcxt
Definition: fmgr.h:65
Definition: nodes.h:135
int32 typmod
Definition: misc.c:57
Oid typoid
Definition: misc.c:56
bool typname_constant
Definition: misc.c:58
FmgrInfo inputproc
Definition: misc.c:61
Oid typiofunc
Definition: misc.c:59
Oid typioparam
Definition: misc.c:60

References FunctionCallInfoBaseData::flinfo, fmgr_info_cxt(), FmgrInfo::fn_extra, FmgrInfo::fn_mcxt, get_fn_expr_arg_stable(), getTypeInputInfo(), if(), InputFunctionCallSafe(), ValidIOData::inputproc, InvalidOid, MemoryContextAlloc(), parseTypeString(), str, text_to_cstring(), ValidIOData::typiofunc, ValidIOData::typioparam, ValidIOData::typmod, typname, ValidIOData::typname_constant, and ValidIOData::typoid.

Referenced by pg_input_error_info(), and pg_input_is_valid().

◆ pg_num_nonnulls()

Datum pg_num_nonnulls ( PG_FUNCTION_ARGS  )

Definition at line 178 of file misc.c.

179{
180 int32 nargs,
181 nulls;
182
183 if (!count_nulls(fcinfo, &nargs, &nulls))
185
186 PG_RETURN_INT32(nargs - nulls);
187}
static bool count_nulls(FunctionCallInfo fcinfo, int32 *nargs, int32 *nulls)
Definition: misc.c:76
#define PG_RETURN_INT32(x)
Definition: fmgr.h:354

References count_nulls(), PG_RETURN_INT32, and PG_RETURN_NULL.

◆ pg_num_nulls()

Datum pg_num_nulls ( PG_FUNCTION_ARGS  )

Definition at line 162 of file misc.c.

163{
164 int32 nargs,
165 nulls;
166
167 if (!count_nulls(fcinfo, &nargs, &nulls))
169
170 PG_RETURN_INT32(nulls);
171}

References count_nulls(), PG_RETURN_INT32, and PG_RETURN_NULL.

◆ pg_relation_is_updatable()

Datum pg_relation_is_updatable ( PG_FUNCTION_ARGS  )

Definition at line 619 of file misc.c.

620{
621 Oid reloid = PG_GETARG_OID(0);
622 bool include_triggers = PG_GETARG_BOOL(1);
623
624 PG_RETURN_INT32(relation_is_updatable(reloid, NIL, include_triggers, NULL));
625}

References NIL, PG_GETARG_BOOL, PG_GETARG_OID, PG_RETURN_INT32, and relation_is_updatable().

◆ pg_sleep()

Datum pg_sleep ( PG_FUNCTION_ARGS  )

Definition at line 330 of file misc.c.

331{
332 float8 secs = PG_GETARG_FLOAT8(0);
333 int64 usecs;
334 TimestampTz endtime;
335
336 /*
337 * Convert the delay to int64 microseconds, rounding up any fraction, and
338 * silently limiting it to PG_INT64_MAX/2 microseconds (about 150K years)
339 * to ensure the computation of endtime won't overflow. Historically
340 * we've treated NaN as "no wait", not an error, so keep that behavior.
341 */
342 if (isnan(secs) || secs <= 0.0)
344 secs *= USECS_PER_SEC; /* we assume overflow will produce +Inf */
345 secs = ceil(secs); /* round up any fractional microsecond */
346 usecs = (int64) Min(secs, (float8) (PG_INT64_MAX / 2));
347
348 /*
349 * We sleep using WaitLatch, to ensure that we'll wake up promptly if an
350 * important signal (such as SIGALRM or SIGINT) arrives. Because
351 * WaitLatch's upper limit of delay is INT_MAX milliseconds, and the user
352 * might ask for more than that, we sleep for at most 10 minutes and then
353 * loop.
354 *
355 * By computing the intended stop time initially, we avoid accumulation of
356 * extra delay across multiple sleeps. This also ensures we won't delay
357 * less than the specified time when WaitLatch is terminated early by a
358 * non-query-canceling signal such as SIGHUP.
359 */
360 endtime = GetCurrentTimestamp() + usecs;
361
362 for (;;)
363 {
364 TimestampTz delay;
365 long delay_ms;
366
368
369 delay = endtime - GetCurrentTimestamp();
370 if (delay >= 600 * USECS_PER_SEC)
371 delay_ms = 600000;
372 else if (delay > 0)
373 delay_ms = (long) ((delay + 999) / 1000);
374 else
375 break;
376
377 (void) WaitLatch(MyLatch,
379 delay_ms,
380 WAIT_EVENT_PG_SLEEP);
382 }
383
385}
TimestampTz GetCurrentTimestamp(void)
Definition: timestamp.c:1645
#define Min(x, y)
Definition: c.h:1006
int64_t int64
Definition: c.h:538
double float8
Definition: c.h:638
#define PG_INT64_MAX
Definition: c.h:600
int64 TimestampTz
Definition: timestamp.h:39
#define USECS_PER_SEC
Definition: timestamp.h:134
#define PG_RETURN_VOID()
Definition: fmgr.h:349
#define PG_GETARG_FLOAT8(n)
Definition: fmgr.h:282
struct Latch * MyLatch
Definition: globals.c:63
void ResetLatch(Latch *latch)
Definition: latch.c:374
int WaitLatch(Latch *latch, int wakeEvents, long timeout, uint32 wait_event_info)
Definition: latch.c:172
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:123
#define WL_TIMEOUT
Definition: waiteventset.h:37
#define WL_EXIT_ON_PM_DEATH
Definition: waiteventset.h:39
#define WL_LATCH_SET
Definition: waiteventset.h:34

References CHECK_FOR_INTERRUPTS, GetCurrentTimestamp(), Min, MyLatch, PG_GETARG_FLOAT8, PG_INT64_MAX, PG_RETURN_VOID, ResetLatch(), USECS_PER_SEC, WaitLatch(), WL_EXIT_ON_PM_DEATH, WL_LATCH_SET, and WL_TIMEOUT.

◆ pg_tablespace_databases()

Datum pg_tablespace_databases ( PG_FUNCTION_ARGS  )

Definition at line 238 of file misc.c.

239{
240 Oid tablespaceOid = PG_GETARG_OID(0);
241 ReturnSetInfo *rsinfo = (ReturnSetInfo *) fcinfo->resultinfo;
242 char *location;
243 DIR *dirdesc;
244 struct dirent *de;
245
247
248 if (tablespaceOid == GLOBALTABLESPACE_OID)
249 {
251 (errmsg("global tablespace never has databases")));
252 /* return empty tuplestore */
253 return (Datum) 0;
254 }
255
256 if (tablespaceOid == DEFAULTTABLESPACE_OID)
257 location = "base";
258 else
259 location = psprintf("%s/%u/%s", PG_TBLSPC_DIR, tablespaceOid,
261
262 dirdesc = AllocateDir(location);
263
264 if (!dirdesc)
265 {
266 /* the only expected error is ENOENT */
267 if (errno != ENOENT)
270 errmsg("could not open directory \"%s\": %m",
271 location)));
273 (errmsg("%u is not a tablespace OID", tablespaceOid)));
274 /* return empty tuplestore */
275 return (Datum) 0;
276 }
277
278 while ((de = ReadDir(dirdesc, location)) != NULL)
279 {
280 Oid datOid = atooid(de->d_name);
281 char *subdir;
282 bool isempty;
283 Datum values[1];
284 bool nulls[1];
285
286 /* this test skips . and .., but is awfully weak */
287 if (!datOid)
288 continue;
289
290 /* if database subdir is empty, don't report tablespace as used */
291
292 subdir = psprintf("%s/%s", location, de->d_name);
293 isempty = directory_is_empty(subdir);
294 pfree(subdir);
295
296 if (isempty)
297 continue; /* indeed, nothing in it */
298
299 values[0] = ObjectIdGetDatum(datOid);
300 nulls[0] = false;
301
302 tuplestore_putvalues(rsinfo->setResult, rsinfo->setDesc,
303 values, nulls);
304 }
305
306 FreeDir(dirdesc);
307 return (Datum) 0;
308}
bool directory_is_empty(const char *path)
Definition: tablespace.c:853
#define WARNING
Definition: elog.h:36
int FreeDir(DIR *dir)
Definition: fd.c:3022
DIR * AllocateDir(const char *dirname)
Definition: fd.c:2904
struct dirent * ReadDir(DIR *dir, const char *dirname)
Definition: fd.c:2970
void InitMaterializedSRF(FunctionCallInfo fcinfo, bits32 flags)
Definition: funcapi.c:76
#define MAT_SRF_USE_EXPECTED_DESC
Definition: funcapi.h:296
void pfree(void *pointer)
Definition: mcxt.c:1594
#define atooid(x)
Definition: postgres_ext.h:43
char * psprintf(const char *fmt,...)
Definition: psprintf.c:43
#define PG_TBLSPC_DIR
Definition: relpath.h:41
#define TABLESPACE_VERSION_DIRECTORY
Definition: relpath.h:33
Definition: dirent.c:26
TupleDesc setDesc
Definition: execnodes.h:364
Tuplestorestate * setResult
Definition: execnodes.h:363
Definition: dirent.h:10
void tuplestore_putvalues(Tuplestorestate *state, TupleDesc tdesc, const Datum *values, const bool *isnull)
Definition: tuplestore.c:784

References AllocateDir(), atooid, directory_is_empty(), ereport, errcode_for_file_access(), errmsg(), ERROR, FreeDir(), InitMaterializedSRF(), MAT_SRF_USE_EXPECTED_DESC, ObjectIdGetDatum(), pfree(), PG_GETARG_OID, PG_TBLSPC_DIR, psprintf(), ReadDir(), ReturnSetInfo::setDesc, ReturnSetInfo::setResult, TABLESPACE_VERSION_DIRECTORY, tuplestore_putvalues(), values, and WARNING.

◆ pg_tablespace_location()

Datum pg_tablespace_location ( PG_FUNCTION_ARGS  )

Definition at line 315 of file misc.c.

316{
317 Oid tablespaceOid = PG_GETARG_OID(0);
318 char *tablespaceLoc;
319
320 /* Get LOCATION string from its OID */
321 tablespaceLoc = get_tablespace_location(tablespaceOid);
322
323 PG_RETURN_TEXT_P(cstring_to_text(tablespaceLoc));
324}
char * get_tablespace_location(Oid tablespaceOid)
Definition: pg_tablespace.c:30

References cstring_to_text(), get_tablespace_location(), PG_GETARG_OID, and PG_RETURN_TEXT_P.

◆ pg_typeof()

Datum pg_typeof ( PG_FUNCTION_ARGS  )

Definition at line 535 of file misc.c.

536{
537 PG_RETURN_OID(get_fn_expr_argtype(fcinfo->flinfo, 0));
538}

References get_fn_expr_argtype(), and PG_RETURN_OID.