PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
pg_config.c File Reference
#include "postgres.h"
#include "funcapi.h"
#include "miscadmin.h"
#include "catalog/pg_type.h"
#include "common/config_info.h"
#include "utils/builtins.h"
#include "utils/elog.h"
#include "port.h"
Include dependency graph for pg_config.c:

Go to the source code of this file.

Functions

Datum pg_config (PG_FUNCTION_ARGS)
 

Function Documentation

Datum pg_config ( PG_FUNCTION_ARGS  )

Definition at line 26 of file pg_config.c.

References ReturnSetInfo::allowedModes, BuildTupleFromCStrings(), CreateTupleDescCopy(), ReturnSetInfo::econtext, ExprContext::ecxt_per_query_memory, ereport, errcode(), errmsg(), ERROR, ReturnSetInfo::expectedDesc, get_configdata(), i, MemoryContextSwitchTo(), my_exec_path, ReleaseTupleDesc, ReturnSetInfo::returnMode, ReturnSetInfo::setDesc, ReturnSetInfo::setResult, SFRM_Materialize, TEXTOID, TupleDescGetAttInMetadata(), tuplestore_begin_heap(), tuplestore_donestoring, tuplestore_puttuple(), values, and work_mem.

27 {
28  ReturnSetInfo *rsinfo = (ReturnSetInfo *) fcinfo->resultinfo;
29  Tuplestorestate *tupstore;
30  HeapTuple tuple;
31  TupleDesc tupdesc;
32  AttInMetadata *attinmeta;
33  MemoryContext per_query_ctx;
34  MemoryContext oldcontext;
35  ConfigData *configdata;
36  size_t configdata_len;
37  char *values[2];
38  int i = 0;
39 
40  /* check to see if caller supports us returning a tuplestore */
41  if (!rsinfo || !(rsinfo->allowedModes & SFRM_Materialize))
42  ereport(ERROR,
43  (errcode(ERRCODE_SYNTAX_ERROR),
44  errmsg("materialize mode required, but it is not "
45  "allowed in this context")));
46 
47  per_query_ctx = rsinfo->econtext->ecxt_per_query_memory;
48  oldcontext = MemoryContextSwitchTo(per_query_ctx);
49 
50  /* get the requested return tuple description */
51  tupdesc = CreateTupleDescCopy(rsinfo->expectedDesc);
52 
53  /*
54  * Check to make sure we have a reasonable tuple descriptor
55  */
56  if (tupdesc->natts != 2 ||
57  tupdesc->attrs[0]->atttypid != TEXTOID ||
58  tupdesc->attrs[1]->atttypid != TEXTOID)
59  ereport(ERROR,
60  (errcode(ERRCODE_SYNTAX_ERROR),
61  errmsg("query-specified return tuple and "
62  "function return type are not compatible")));
63 
64  /* OK to use it */
65  attinmeta = TupleDescGetAttInMetadata(tupdesc);
66 
67  /* let the caller know we're sending back a tuplestore */
68  rsinfo->returnMode = SFRM_Materialize;
69 
70  /* initialize our tuplestore */
71  tupstore = tuplestore_begin_heap(true, false, work_mem);
72 
73  configdata = get_configdata(my_exec_path, &configdata_len);
74  for (i = 0; i < configdata_len; i++)
75  {
76  values[0] = configdata[i].name;
77  values[1] = configdata[i].setting;
78 
79  tuple = BuildTupleFromCStrings(attinmeta, values);
80  tuplestore_puttuple(tupstore, tuple);
81  }
82 
83  /*
84  * no longer need the tuple descriptor reference created by
85  * TupleDescGetAttInMetadata()
86  */
87  ReleaseTupleDesc(tupdesc);
88 
89  tuplestore_donestoring(tupstore);
90  rsinfo->setResult = tupstore;
91 
92  /*
93  * SFRM_Materialize mode expects us to return a NULL Datum. The actual
94  * tuples are in our tuplestore and passed back through rsinfo->setResult.
95  * rsinfo->setDesc is set to the tuple description that we actually used
96  * to build our tuples with, so the caller can verify we did what it was
97  * expecting.
98  */
99  rsinfo->setDesc = tupdesc;
100  MemoryContextSwitchTo(oldcontext);
101 
102  return (Datum) 0;
103 }
TupleDesc CreateTupleDescCopy(TupleDesc tupdesc)
Definition: tupdesc.c:141
#define TEXTOID
Definition: pg_type.h:324
#define tuplestore_donestoring(state)
Definition: tuplestore.h:60
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
int errcode(int sqlerrcode)
Definition: elog.c:575
HeapTuple BuildTupleFromCStrings(AttInMetadata *attinmeta, char **values)
Definition: execTuples.c:1115
TupleDesc expectedDesc
Definition: execnodes.h:198
#define ERROR
Definition: elog.h:43
void tuplestore_puttuple(Tuplestorestate *state, HeapTuple tuple)
Definition: tuplestore.c:715
#define ereport(elevel, rest)
Definition: elog.h:122
Tuplestorestate * tuplestore_begin_heap(bool randomAccess, bool interXact, int maxKBytes)
Definition: tuplestore.c:316
char my_exec_path[MAXPGPATH]
Definition: globals.c:63
uintptr_t Datum
Definition: postgres.h:374
AttInMetadata * TupleDescGetAttInMetadata(TupleDesc tupdesc)
Definition: execTuples.c:1068
int work_mem
Definition: globals.c:112
int allowedModes
Definition: execnodes.h:199
SetFunctionReturnMode returnMode
Definition: execnodes.h:201
ConfigData * get_configdata(const char *my_exec_path, size_t *configdata_len)
Definition: config_info.c:34
MemoryContext ecxt_per_query_memory
Definition: execnodes.h:133
Tuplestorestate * setResult
Definition: execnodes.h:204
static Datum values[MAXATTR]
Definition: bootstrap.c:162
ExprContext * econtext
Definition: execnodes.h:197
TupleDesc setDesc
Definition: execnodes.h:205
int errmsg(const char *fmt,...)
Definition: elog.c:797
int i
#define ReleaseTupleDesc(tupdesc)
Definition: tupdesc.h:107