PostgreSQL Source Code  git master
option.c File Reference
Include dependency graph for option.c:

Go to the source code of this file.

Data Structures

struct  PgFdwOption
 

Typedefs

typedef struct PgFdwOption PgFdwOption
 

Functions

static void InitPgFdwOptions (void)
 
static bool is_valid_option (const char *keyword, Oid context)
 
static bool is_libpq_option (const char *keyword)
 
 PG_FUNCTION_INFO_V1 (postgres_fdw_validator)
 
Datum postgres_fdw_validator (PG_FUNCTION_ARGS)
 
int ExtractConnectionOptions (List *defelems, const char **keywords, const char **values)
 
ListExtractExtensionList (const char *extensionsString, bool warnOnMissing)
 

Variables

static PgFdwOptionpostgres_fdw_options
 
static PQconninfoOptionlibpq_options
 

Typedef Documentation

◆ PgFdwOption

typedef struct PgFdwOption PgFdwOption

Function Documentation

◆ ExtractConnectionOptions()

int ExtractConnectionOptions ( List defelems,
const char **  keywords,
const char **  values 
)

Definition at line 333 of file option.c.

References defGetString(), DefElem::defname, i, InitPgFdwOptions(), is_libpq_option(), and lfirst.

Referenced by connect_pg_server().

335 {
336  ListCell *lc;
337  int i;
338 
339  /* Build our options lists if we didn't yet. */
341 
342  i = 0;
343  foreach(lc, defelems)
344  {
345  DefElem *d = (DefElem *) lfirst(lc);
346 
347  if (is_libpq_option(d->defname))
348  {
349  keywords[i] = d->defname;
350  values[i] = defGetString(d);
351  i++;
352  }
353  }
354  return i;
355 }
char * defGetString(DefElem *def)
Definition: define.c:49
#define lfirst(lc)
Definition: pg_list.h:169
static Datum values[MAXATTR]
Definition: bootstrap.c:165
int i
static bool is_libpq_option(const char *keyword)
Definition: option.c:312
char * defname
Definition: parsenodes.h:733
static void InitPgFdwOptions(void)
Definition: option.c:181

◆ ExtractExtensionList()

List* ExtractExtensionList ( const char *  extensionsString,
bool  warnOnMissing 
)

Definition at line 364 of file option.c.

References ereport, errcode(), errmsg(), ERROR, get_extension_oid(), lappend_oid(), lfirst, list_free(), NIL, OidIsValid, pstrdup(), SplitIdentifierString(), and WARNING.

Referenced by apply_server_options(), and postgres_fdw_validator().

365 {
366  List *extensionOids = NIL;
367  List *extlist;
368  ListCell *lc;
369 
370  /* SplitIdentifierString scribbles on its input, so pstrdup first */
371  if (!SplitIdentifierString(pstrdup(extensionsString), ',', &extlist))
372  {
373  /* syntax error in name list */
374  ereport(ERROR,
375  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
376  errmsg("parameter \"%s\" must be a list of extension names",
377  "extensions")));
378  }
379 
380  foreach(lc, extlist)
381  {
382  const char *extension_name = (const char *) lfirst(lc);
383  Oid extension_oid = get_extension_oid(extension_name, true);
384 
385  if (OidIsValid(extension_oid))
386  {
387  extensionOids = lappend_oid(extensionOids, extension_oid);
388  }
389  else if (warnOnMissing)
390  {
392  (errcode(ERRCODE_UNDEFINED_OBJECT),
393  errmsg("extension \"%s\" is not installed",
394  extension_name)));
395  }
396  }
397 
398  list_free(extlist);
399  return extensionOids;
400 }
#define NIL
Definition: pg_list.h:65
char * pstrdup(const char *in)
Definition: mcxt.c:1187
int errcode(int sqlerrcode)
Definition: elog.c:691
unsigned int Oid
Definition: postgres_ext.h:31
List * lappend_oid(List *list, Oid datum)
Definition: list.c:357
#define OidIsValid(objectId)
Definition: c.h:706
#define ERROR
Definition: elog.h:43
bool SplitIdentifierString(char *rawstring, char separator, List **namelist)
Definition: varlena.c:3716
#define WARNING
Definition: elog.h:40
#define ereport(elevel,...)
Definition: elog.h:155
#define lfirst(lc)
Definition: pg_list.h:169
Oid get_extension_oid(const char *extname, bool missing_ok)
Definition: extension.c:140
int errmsg(const char *fmt,...)
Definition: elog.c:902
void list_free(List *list)
Definition: list.c:1376
Definition: pg_list.h:50

◆ InitPgFdwOptions()

static void InitPgFdwOptions ( void  )
static

Definition at line 181 of file option.c.

References _PQconninfoOption::dispchar, ereport, errcode(), errdetail(), errmsg(), ERROR, InvalidOid, PgFdwOption::is_libpq_opt, PgFdwOption::keyword, _PQconninfoOption::keyword, malloc, PgFdwOption::optcontext, postgres_fdw_options, and PQconndefaults().

Referenced by ExtractConnectionOptions(), and postgres_fdw_validator().

182 {
183  int num_libpq_opts;
184  PQconninfoOption *lopt;
185  PgFdwOption *popt;
186 
187  /* non-libpq FDW-specific FDW options */
188  static const PgFdwOption non_libpq_options[] = {
189  {"schema_name", ForeignTableRelationId, false},
190  {"table_name", ForeignTableRelationId, false},
191  {"column_name", AttributeRelationId, false},
192  /* use_remote_estimate is available on both server and table */
193  {"use_remote_estimate", ForeignServerRelationId, false},
194  {"use_remote_estimate", ForeignTableRelationId, false},
195  /* cost factors */
196  {"fdw_startup_cost", ForeignServerRelationId, false},
197  {"fdw_tuple_cost", ForeignServerRelationId, false},
198  /* shippable extensions */
199  {"extensions", ForeignServerRelationId, false},
200  /* updatable is available on both server and table */
201  {"updatable", ForeignServerRelationId, false},
202  {"updatable", ForeignTableRelationId, false},
203  /* fetch_size is available on both server and table */
204  {"fetch_size", ForeignServerRelationId, false},
205  {"fetch_size", ForeignTableRelationId, false},
206  {"password_required", UserMappingRelationId, false},
207 
208  /*
209  * sslcert and sslkey are in fact libpq options, but we repeat them
210  * here to allow them to appear in both foreign server context (when
211  * we generate libpq options) and user mapping context (from here).
212  */
213  {"sslcert", UserMappingRelationId, true},
214  {"sslkey", UserMappingRelationId, true},
215 
216  {NULL, InvalidOid, false}
217  };
218 
219  /* Prevent redundant initialization. */
221  return;
222 
223  /*
224  * Get list of valid libpq options.
225  *
226  * To avoid unnecessary work, we get the list once and use it throughout
227  * the lifetime of this backend process. We don't need to care about
228  * memory context issues, because PQconndefaults allocates with malloc.
229  */
231  if (!libpq_options) /* assume reason for failure is OOM */
232  ereport(ERROR,
233  (errcode(ERRCODE_FDW_OUT_OF_MEMORY),
234  errmsg("out of memory"),
235  errdetail("Could not get libpq's default connection options.")));
236 
237  /* Count how many libpq options are available. */
238  num_libpq_opts = 0;
239  for (lopt = libpq_options; lopt->keyword; lopt++)
240  num_libpq_opts++;
241 
242  /*
243  * Construct an array which consists of all valid options for
244  * postgres_fdw, by appending FDW-specific options to libpq options.
245  *
246  * We use plain malloc here to allocate postgres_fdw_options because it
247  * lives as long as the backend process does. Besides, keeping
248  * libpq_options in memory allows us to avoid copying every keyword
249  * string.
250  */
252  malloc(sizeof(PgFdwOption) * num_libpq_opts +
253  sizeof(non_libpq_options));
254  if (postgres_fdw_options == NULL)
255  ereport(ERROR,
256  (errcode(ERRCODE_FDW_OUT_OF_MEMORY),
257  errmsg("out of memory")));
258 
259  popt = postgres_fdw_options;
260  for (lopt = libpq_options; lopt->keyword; lopt++)
261  {
262  /* Hide debug options, as well as settings we override internally. */
263  if (strchr(lopt->dispchar, 'D') ||
264  strcmp(lopt->keyword, "fallback_application_name") == 0 ||
265  strcmp(lopt->keyword, "client_encoding") == 0)
266  continue;
267 
268  /* We don't have to copy keyword string, as described above. */
269  popt->keyword = lopt->keyword;
270 
271  /*
272  * "user" and any secret options are allowed only on user mappings.
273  * Everything else is a server option.
274  */
275  if (strcmp(lopt->keyword, "user") == 0 || strchr(lopt->dispchar, '*'))
276  popt->optcontext = UserMappingRelationId;
277  else
278  popt->optcontext = ForeignServerRelationId;
279  popt->is_libpq_opt = true;
280 
281  popt++;
282  }
283 
284  /* Append FDW-specific options and dummy terminator. */
285  memcpy(popt, non_libpq_options, sizeof(non_libpq_options));
286 }
const char * keyword
Definition: option.c:30
bool is_libpq_opt
Definition: option.c:32
int errcode(int sqlerrcode)
Definition: elog.c:691
static PgFdwOption * postgres_fdw_options
Definition: option.c:39
#define malloc(a)
Definition: header.h:50
#define ERROR
Definition: elog.h:43
int errdetail(const char *fmt,...)
Definition: elog.c:1035
Oid optcontext
Definition: option.c:31
#define InvalidOid
Definition: postgres_ext.h:36
#define ereport(elevel,...)
Definition: elog.h:155
PQconninfoOption * PQconndefaults(void)
Definition: fe-connect.c:1445
int errmsg(const char *fmt,...)
Definition: elog.c:902
static PQconninfoOption * libpq_options
Definition: option.c:45

◆ is_libpq_option()

static bool is_libpq_option ( const char *  keyword)
static

Definition at line 312 of file option.c.

References Assert, PgFdwOption::is_libpq_opt, and PgFdwOption::keyword.

Referenced by ExtractConnectionOptions().

313 {
314  PgFdwOption *opt;
315 
316  Assert(postgres_fdw_options); /* must be initialized already */
317 
318  for (opt = postgres_fdw_options; opt->keyword; opt++)
319  {
320  if (opt->is_libpq_opt && strcmp(opt->keyword, keyword) == 0)
321  return true;
322  }
323 
324  return false;
325 }
const char * keyword
Definition: option.c:30
bool is_libpq_opt
Definition: option.c:32
static PgFdwOption * postgres_fdw_options
Definition: option.c:39
#define Assert(condition)
Definition: c.h:800

◆ is_valid_option()

static bool is_valid_option ( const char *  keyword,
Oid  context 
)
static

Definition at line 293 of file option.c.

References Assert, PgFdwOption::keyword, and PgFdwOption::optcontext.

Referenced by postgres_fdw_validator().

294 {
295  PgFdwOption *opt;
296 
297  Assert(postgres_fdw_options); /* must be initialized already */
298 
299  for (opt = postgres_fdw_options; opt->keyword; opt++)
300  {
301  if (context == opt->optcontext && strcmp(opt->keyword, keyword) == 0)
302  return true;
303  }
304 
305  return false;
306 }
const char * keyword
Definition: option.c:30
static PgFdwOption * postgres_fdw_options
Definition: option.c:39
Oid optcontext
Definition: option.c:31
#define Assert(condition)
Definition: c.h:800

◆ PG_FUNCTION_INFO_V1()

PG_FUNCTION_INFO_V1 ( postgres_fdw_validator  )

◆ postgres_fdw_validator()

Datum postgres_fdw_validator ( PG_FUNCTION_ARGS  )

Definition at line 65 of file option.c.

References appendStringInfo(), buf, StringInfoData::data, defGetBoolean(), defGetString(), DefElem::defname, ereport, errcode(), errhint(), errmsg(), ERROR, ExtractExtensionList(), fetch_size, InitPgFdwOptions(), initStringInfo(), is_valid_option(), PgFdwOption::keyword, StringInfoData::len, lfirst, PgFdwOption::optcontext, PG_GETARG_DATUM, PG_GETARG_OID, PG_RETURN_VOID, superuser(), untransformRelOptions(), and val.

66 {
67  List *options_list = untransformRelOptions(PG_GETARG_DATUM(0));
68  Oid catalog = PG_GETARG_OID(1);
69  ListCell *cell;
70 
71  /* Build our options lists if we didn't yet. */
73 
74  /*
75  * Check that only options supported by postgres_fdw, and allowed for the
76  * current object type, are given.
77  */
78  foreach(cell, options_list)
79  {
80  DefElem *def = (DefElem *) lfirst(cell);
81 
82  if (!is_valid_option(def->defname, catalog))
83  {
84  /*
85  * Unknown option specified, complain about it. Provide a hint
86  * with list of valid options for the object.
87  */
88  PgFdwOption *opt;
90 
91  initStringInfo(&buf);
92  for (opt = postgres_fdw_options; opt->keyword; opt++)
93  {
94  if (catalog == opt->optcontext)
95  appendStringInfo(&buf, "%s%s", (buf.len > 0) ? ", " : "",
96  opt->keyword);
97  }
98 
99  ereport(ERROR,
100  (errcode(ERRCODE_FDW_INVALID_OPTION_NAME),
101  errmsg("invalid option \"%s\"", def->defname),
102  errhint("Valid options in this context are: %s",
103  buf.data)));
104  }
105 
106  /*
107  * Validate option value, when we can do so without any context.
108  */
109  if (strcmp(def->defname, "use_remote_estimate") == 0 ||
110  strcmp(def->defname, "updatable") == 0)
111  {
112  /* these accept only boolean values */
113  (void) defGetBoolean(def);
114  }
115  else if (strcmp(def->defname, "fdw_startup_cost") == 0 ||
116  strcmp(def->defname, "fdw_tuple_cost") == 0)
117  {
118  /* these must have a non-negative numeric value */
119  double val;
120  char *endp;
121 
122  val = strtod(defGetString(def), &endp);
123  if (*endp || val < 0)
124  ereport(ERROR,
125  (errcode(ERRCODE_SYNTAX_ERROR),
126  errmsg("%s requires a non-negative numeric value",
127  def->defname)));
128  }
129  else if (strcmp(def->defname, "extensions") == 0)
130  {
131  /* check list syntax, warn about uninstalled extensions */
132  (void) ExtractExtensionList(defGetString(def), true);
133  }
134  else if (strcmp(def->defname, "fetch_size") == 0)
135  {
136  int fetch_size;
137 
138  fetch_size = strtol(defGetString(def), NULL, 10);
139  if (fetch_size <= 0)
140  ereport(ERROR,
141  (errcode(ERRCODE_SYNTAX_ERROR),
142  errmsg("%s requires a non-negative integer value",
143  def->defname)));
144  }
145  else if (strcmp(def->defname, "password_required") == 0)
146  {
147  bool pw_required = defGetBoolean(def);
148 
149  /*
150  * Only the superuser may set this option on a user mapping, or
151  * alter a user mapping on which this option is set. We allow a
152  * user to clear this option if it's set - in fact, we don't have
153  * a choice since we can't see the old mapping when validating an
154  * alter.
155  */
156  if (!superuser() && !pw_required)
157  ereport(ERROR,
158  (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
159  errmsg("password_required=false is superuser-only"),
160  errhint("User mappings with the password_required option set to false may only be created or modified by the superuser")));
161  }
162  else if (strcmp(def->defname, "sslcert") == 0 ||
163  strcmp(def->defname, "sslkey") == 0)
164  {
165  /* similarly for sslcert / sslkey on user mapping */
166  if (catalog == UserMappingRelationId && !superuser())
167  ereport(ERROR,
168  (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
169  errmsg("sslcert and sslkey are superuser-only"),
170  errhint("User mappings with the sslcert or sslkey options set may only be created or modified by the superuser")));
171  }
172  }
173 
174  PG_RETURN_VOID();
175 }
int errhint(const char *fmt,...)
Definition: elog.c:1149
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:268
const char * keyword
Definition: option.c:30
static bool is_valid_option(const char *keyword, Oid context)
Definition: option.c:293
int errcode(int sqlerrcode)
Definition: elog.c:691
bool superuser(void)
Definition: superuser.c:46
unsigned int Oid
Definition: postgres_ext.h:31
static PgFdwOption * postgres_fdw_options
Definition: option.c:39
bool defGetBoolean(DefElem *def)
Definition: define.c:111
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:91
#define ERROR
Definition: elog.h:43
uint64 fetch_size
Definition: pg_rewind.c:75
char * defGetString(DefElem *def)
Definition: define.c:49
List * ExtractExtensionList(const char *extensionsString, bool warnOnMissing)
Definition: option.c:364
static char * buf
Definition: pg_test_fsync.c:68
#define PG_GETARG_OID(n)
Definition: fmgr.h:275
Oid optcontext
Definition: option.c:31
void initStringInfo(StringInfo str)
Definition: stringinfo.c:59
List * untransformRelOptions(Datum options)
Definition: reloptions.c:1308
#define ereport(elevel,...)
Definition: elog.h:155
#define PG_RETURN_VOID()
Definition: fmgr.h:349
#define lfirst(lc)
Definition: pg_list.h:169
int errmsg(const char *fmt,...)
Definition: elog.c:902
char * defname
Definition: parsenodes.h:733
Definition: pg_list.h:50
long val
Definition: informix.c:664
static void InitPgFdwOptions(void)
Definition: option.c:181

Variable Documentation

◆ libpq_options

PQconninfoOption* libpq_options
static

Definition at line 45 of file option.c.

◆ postgres_fdw_options

PgFdwOption* postgres_fdw_options
static

Definition at line 39 of file option.c.

Referenced by InitPgFdwOptions().