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 357 of file option.c.

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

Referenced by connect_pg_server().

359 {
360  ListCell *lc;
361  int i;
362 
363  /* Build our options lists if we didn't yet. */
365 
366  i = 0;
367  foreach(lc, defelems)
368  {
369  DefElem *d = (DefElem *) lfirst(lc);
370 
371  if (is_libpq_option(d->defname))
372  {
373  keywords[i] = d->defname;
374  values[i] = defGetString(d);
375  i++;
376  }
377  }
378  return i;
379 }
char * defGetString(DefElem *def)
Definition: define.c:49
#define lfirst(lc)
Definition: pg_list.h:169
static Datum values[MAXATTR]
Definition: bootstrap.c:166
int i
static bool is_libpq_option(const char *keyword)
Definition: option.c:336
char * defname
Definition: parsenodes.h:746
static void InitPgFdwOptions(void)
Definition: option.c:195

◆ ExtractExtensionList()

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

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

389 {
390  List *extensionOids = NIL;
391  List *extlist;
392  ListCell *lc;
393 
394  /* SplitIdentifierString scribbles on its input, so pstrdup first */
395  if (!SplitIdentifierString(pstrdup(extensionsString), ',', &extlist))
396  {
397  /* syntax error in name list */
398  ereport(ERROR,
399  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
400  errmsg("parameter \"%s\" must be a list of extension names",
401  "extensions")));
402  }
403 
404  foreach(lc, extlist)
405  {
406  const char *extension_name = (const char *) lfirst(lc);
407  Oid extension_oid = get_extension_oid(extension_name, true);
408 
409  if (OidIsValid(extension_oid))
410  {
411  extensionOids = lappend_oid(extensionOids, extension_oid);
412  }
413  else if (warnOnMissing)
414  {
416  (errcode(ERRCODE_UNDEFINED_OBJECT),
417  errmsg("extension \"%s\" is not installed",
418  extension_name)));
419  }
420  }
421 
422  list_free(extlist);
423  return extensionOids;
424 }
#define NIL
Definition: pg_list.h:65
char * pstrdup(const char *in)
Definition: mcxt.c:1299
int errcode(int sqlerrcode)
Definition: elog.c:698
unsigned int Oid
Definition: postgres_ext.h:31
List * lappend_oid(List *list, Oid datum)
Definition: list.c:372
#define OidIsValid(objectId)
Definition: c.h:710
#define ERROR
Definition: elog.h:46
bool SplitIdentifierString(char *rawstring, char separator, List **namelist)
Definition: varlena.c:3753
#define WARNING
Definition: elog.h:40
#define ereport(elevel,...)
Definition: elog.h:157
#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:909
void list_free(List *list)
Definition: list.c:1391
Definition: pg_list.h:50

◆ InitPgFdwOptions()

static void InitPgFdwOptions ( void  )
static

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

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

◆ is_libpq_option()

static bool is_libpq_option ( const char *  keyword)
static

Definition at line 336 of file option.c.

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

Referenced by ExtractConnectionOptions().

337 {
338  PgFdwOption *opt;
339 
340  Assert(postgres_fdw_options); /* must be initialized already */
341 
342  for (opt = postgres_fdw_options; opt->keyword; opt++)
343  {
344  if (opt->is_libpq_opt && strcmp(opt->keyword, keyword) == 0)
345  return true;
346  }
347 
348  return false;
349 }
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:804

◆ is_valid_option()

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

Definition at line 317 of file option.c.

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

Referenced by postgres_fdw_validator().

318 {
319  PgFdwOption *opt;
320 
321  Assert(postgres_fdw_options); /* must be initialized already */
322 
323  for (opt = postgres_fdw_options; opt->keyword; opt++)
324  {
325  if (context == opt->optcontext && strcmp(opt->keyword, keyword) == 0)
326  return true;
327  }
328 
329  return false;
330 }
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:804

◆ 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  strcmp(def->defname, "truncatable") == 0 ||
112  strcmp(def->defname, "async_capable") == 0 ||
113  strcmp(def->defname, "keep_connections") == 0)
114  {
115  /* these accept only boolean values */
116  (void) defGetBoolean(def);
117  }
118  else if (strcmp(def->defname, "fdw_startup_cost") == 0 ||
119  strcmp(def->defname, "fdw_tuple_cost") == 0)
120  {
121  /* these must have a non-negative numeric value */
122  double val;
123  char *endp;
124 
125  val = strtod(defGetString(def), &endp);
126  if (*endp || val < 0)
127  ereport(ERROR,
128  (errcode(ERRCODE_SYNTAX_ERROR),
129  errmsg("%s requires a non-negative numeric value",
130  def->defname)));
131  }
132  else if (strcmp(def->defname, "extensions") == 0)
133  {
134  /* check list syntax, warn about uninstalled extensions */
135  (void) ExtractExtensionList(defGetString(def), true);
136  }
137  else if (strcmp(def->defname, "fetch_size") == 0)
138  {
139  int fetch_size;
140 
141  fetch_size = strtol(defGetString(def), NULL, 10);
142  if (fetch_size <= 0)
143  ereport(ERROR,
144  (errcode(ERRCODE_SYNTAX_ERROR),
145  errmsg("%s requires a non-negative integer value",
146  def->defname)));
147  }
148  else if (strcmp(def->defname, "batch_size") == 0)
149  {
150  int batch_size;
151 
152  batch_size = strtol(defGetString(def), NULL, 10);
153  if (batch_size <= 0)
154  ereport(ERROR,
155  (errcode(ERRCODE_SYNTAX_ERROR),
156  errmsg("%s requires a non-negative integer value",
157  def->defname)));
158  }
159  else if (strcmp(def->defname, "password_required") == 0)
160  {
161  bool pw_required = defGetBoolean(def);
162 
163  /*
164  * Only the superuser may set this option on a user mapping, or
165  * alter a user mapping on which this option is set. We allow a
166  * user to clear this option if it's set - in fact, we don't have
167  * a choice since we can't see the old mapping when validating an
168  * alter.
169  */
170  if (!superuser() && !pw_required)
171  ereport(ERROR,
172  (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
173  errmsg("password_required=false is superuser-only"),
174  errhint("User mappings with the password_required option set to false may only be created or modified by the superuser")));
175  }
176  else if (strcmp(def->defname, "sslcert") == 0 ||
177  strcmp(def->defname, "sslkey") == 0)
178  {
179  /* similarly for sslcert / sslkey on user mapping */
180  if (catalog == UserMappingRelationId && !superuser())
181  ereport(ERROR,
182  (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
183  errmsg("sslcert and sslkey are superuser-only"),
184  errhint("User mappings with the sslcert or sslkey options set may only be created or modified by the superuser")));
185  }
186  }
187 
188  PG_RETURN_VOID();
189 }
int errhint(const char *fmt,...)
Definition: elog.c:1156
#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:317
int errcode(int sqlerrcode)
Definition: elog.c:698
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:46
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:388
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:157
#define PG_RETURN_VOID()
Definition: fmgr.h:349
#define lfirst(lc)
Definition: pg_list.h:169
int errmsg(const char *fmt,...)
Definition: elog.c:909
char * defname
Definition: parsenodes.h:746
Definition: pg_list.h:50
long val
Definition: informix.c:664
static void InitPgFdwOptions(void)
Definition: option.c:195

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().