PostgreSQL Source Code  git master
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 "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

◆ pg_config()

Datum pg_config ( PG_FUNCTION_ARGS  )

Definition at line 25 of file pg_config.c.

References ReturnSetInfo::allowedModes, atttypid, 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, TupleDescAttr, TupleDescGetAttInMetadata(), tuplestore_begin_heap(), tuplestore_donestoring, tuplestore_puttuple(), values, and work_mem.

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