PostgreSQL Source Code  git master
option.c File Reference
#include "postgres.h"
#include "access/reloptions.h"
#include "catalog/pg_foreign_server.h"
#include "catalog/pg_foreign_table.h"
#include "catalog/pg_user_mapping.h"
#include "commands/defrem.h"
#include "commands/extension.h"
#include "postgres_fdw.h"
#include "utils/builtins.h"
#include "utils/guc.h"
#include "utils/varlena.h"
#include "miscadmin.h"
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 370 of file option.c.

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

Referenced by connect_pg_server().

372 {
373  ListCell *lc;
374  int i;
375 
376  /* Build our options lists if we didn't yet. */
378 
379  i = 0;
380  foreach(lc, defelems)
381  {
382  DefElem *d = (DefElem *) lfirst(lc);
383 
384  if (is_libpq_option(d->defname))
385  {
386  keywords[i] = d->defname;
387  values[i] = defGetString(d);
388  i++;
389  }
390  }
391  return i;
392 }
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:349
char * defname
Definition: parsenodes.h:746
static void InitPgFdwOptions(void)
Definition: option.c:208

◆ ExtractExtensionList()

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

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

402 {
403  List *extensionOids = NIL;
404  List *extlist;
405  ListCell *lc;
406 
407  /* SplitIdentifierString scribbles on its input, so pstrdup first */
408  if (!SplitIdentifierString(pstrdup(extensionsString), ',', &extlist))
409  {
410  /* syntax error in name list */
411  ereport(ERROR,
412  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
413  errmsg("parameter \"%s\" must be a list of extension names",
414  "extensions")));
415  }
416 
417  foreach(lc, extlist)
418  {
419  const char *extension_name = (const char *) lfirst(lc);
420  Oid extension_oid = get_extension_oid(extension_name, true);
421 
422  if (OidIsValid(extension_oid))
423  {
424  extensionOids = lappend_oid(extensionOids, extension_oid);
425  }
426  else if (warnOnMissing)
427  {
429  (errcode(ERRCODE_UNDEFINED_OBJECT),
430  errmsg("extension \"%s\" is not installed",
431  extension_name)));
432  }
433  }
434 
435  list_free(extlist);
436  return extensionOids;
437 }
#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 208 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().

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

◆ is_libpq_option()

static bool is_libpq_option ( const char *  keyword)
static

Definition at line 349 of file option.c.

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

Referenced by ExtractConnectionOptions().

350 {
351  PgFdwOption *opt;
352 
353  Assert(postgres_fdw_options); /* must be initialized already */
354 
355  for (opt = postgres_fdw_options; opt->keyword; opt++)
356  {
357  if (opt->is_libpq_opt && strcmp(opt->keyword, keyword) == 0)
358  return true;
359  }
360 
361  return false;
362 }
const char * keyword
Definition: option.c:31
bool is_libpq_opt
Definition: option.c:33
static PgFdwOption * postgres_fdw_options
Definition: option.c:40
#define Assert(condition)
Definition: c.h:804

◆ is_valid_option()

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

Definition at line 330 of file option.c.

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

Referenced by postgres_fdw_validator().

331 {
332  PgFdwOption *opt;
333 
334  Assert(postgres_fdw_options); /* must be initialized already */
335 
336  for (opt = postgres_fdw_options; opt->keyword; opt++)
337  {
338  if (context == opt->optcontext && strcmp(opt->keyword, keyword) == 0)
339  return true;
340  }
341 
342  return false;
343 }
const char * keyword
Definition: option.c:31
static PgFdwOption * postgres_fdw_options
Definition: option.c:40
Oid optcontext
Definition: option.c:32
#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 66 of file option.c.

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

67 {
68  List *options_list = untransformRelOptions(PG_GETARG_DATUM(0));
69  Oid catalog = PG_GETARG_OID(1);
70  ListCell *cell;
71 
72  /* Build our options lists if we didn't yet. */
74 
75  /*
76  * Check that only options supported by postgres_fdw, and allowed for the
77  * current object type, are given.
78  */
79  foreach(cell, options_list)
80  {
81  DefElem *def = (DefElem *) lfirst(cell);
82 
83  if (!is_valid_option(def->defname, catalog))
84  {
85  /*
86  * Unknown option specified, complain about it. Provide a hint
87  * with list of valid options for the object.
88  */
89  PgFdwOption *opt;
91 
92  initStringInfo(&buf);
93  for (opt = postgres_fdw_options; opt->keyword; opt++)
94  {
95  if (catalog == opt->optcontext)
96  appendStringInfo(&buf, "%s%s", (buf.len > 0) ? ", " : "",
97  opt->keyword);
98  }
99 
100  ereport(ERROR,
101  (errcode(ERRCODE_FDW_INVALID_OPTION_NAME),
102  errmsg("invalid option \"%s\"", def->defname),
103  errhint("Valid options in this context are: %s",
104  buf.data)));
105  }
106 
107  /*
108  * Validate option value, when we can do so without any context.
109  */
110  if (strcmp(def->defname, "use_remote_estimate") == 0 ||
111  strcmp(def->defname, "updatable") == 0 ||
112  strcmp(def->defname, "truncatable") == 0 ||
113  strcmp(def->defname, "async_capable") == 0 ||
114  strcmp(def->defname, "keep_connections") == 0)
115  {
116  /* these accept only boolean values */
117  (void) defGetBoolean(def);
118  }
119  else if (strcmp(def->defname, "fdw_startup_cost") == 0 ||
120  strcmp(def->defname, "fdw_tuple_cost") == 0)
121  {
122  /*
123  * These must have a floating point value greater than or equal to
124  * zero.
125  */
126  char *value;
127  double real_val;
128  bool is_parsed;
129 
130  value = defGetString(def);
131  is_parsed = parse_real(value, &real_val, 0, NULL);
132 
133  if (!is_parsed)
134  ereport(ERROR,
135  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
136  errmsg("invalid value for floating point option \"%s\": %s",
137  def->defname, value)));
138 
139  if (real_val < 0)
140  ereport(ERROR,
141  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
142  errmsg("\"%s\" must be a floating point value greater than or equal to zero",
143  def->defname)));
144  }
145  else if (strcmp(def->defname, "extensions") == 0)
146  {
147  /* check list syntax, warn about uninstalled extensions */
148  (void) ExtractExtensionList(defGetString(def), true);
149  }
150  else if (strcmp(def->defname, "fetch_size") == 0 ||
151  strcmp(def->defname, "batch_size") == 0)
152  {
153  char *value;
154  int int_val;
155  bool is_parsed;
156 
157  value = defGetString(def);
158  is_parsed = parse_int(value, &int_val, 0, NULL);
159 
160  if (!is_parsed)
161  ereport(ERROR,
162  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
163  errmsg("invalid value for integer option \"%s\": %s",
164  def->defname, value)));
165 
166  if (int_val <= 0)
167  ereport(ERROR,
168  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
169  errmsg("\"%s\" must be an integer value greater than zero",
170  def->defname)));
171  }
172  else if (strcmp(def->defname, "password_required") == 0)
173  {
174  bool pw_required = defGetBoolean(def);
175 
176  /*
177  * Only the superuser may set this option on a user mapping, or
178  * alter a user mapping on which this option is set. We allow a
179  * user to clear this option if it's set - in fact, we don't have
180  * a choice since we can't see the old mapping when validating an
181  * alter.
182  */
183  if (!superuser() && !pw_required)
184  ereport(ERROR,
185  (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
186  errmsg("password_required=false is superuser-only"),
187  errhint("User mappings with the password_required option set to false may only be created or modified by the superuser")));
188  }
189  else if (strcmp(def->defname, "sslcert") == 0 ||
190  strcmp(def->defname, "sslkey") == 0)
191  {
192  /* similarly for sslcert / sslkey on user mapping */
193  if (catalog == UserMappingRelationId && !superuser())
194  ereport(ERROR,
195  (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
196  errmsg("sslcert and sslkey are superuser-only"),
197  errhint("User mappings with the sslcert or sslkey options set may only be created or modified by the superuser")));
198  }
199  }
200 
201  PG_RETURN_VOID();
202 }
int errhint(const char *fmt,...)
Definition: elog.c:1156
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:268
const char * keyword
Definition: option.c:31
static bool is_valid_option(const char *keyword, Oid context)
Definition: option.c:330
int errcode(int sqlerrcode)
Definition: elog.c:698
bool superuser(void)
Definition: superuser.c:46
bool parse_real(const char *value, double *result, int flags, const char **hintmsg)
Definition: guc.c:6925
unsigned int Oid
Definition: postgres_ext.h:31
static PgFdwOption * postgres_fdw_options
Definition: option.c:40
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
char * defGetString(DefElem *def)
Definition: define.c:49
List * ExtractExtensionList(const char *extensionsString, bool warnOnMissing)
Definition: option.c:401
static char * buf
Definition: pg_test_fsync.c:68
#define PG_GETARG_OID(n)
Definition: fmgr.h:275
Oid optcontext
Definition: option.c:32
void initStringInfo(StringInfo str)
Definition: stringinfo.c:59
bool parse_int(const char *value, int *result, int flags, const char **hintmsg)
Definition: guc.c:6835
List * untransformRelOptions(Datum options)
Definition: reloptions.c:1325
static struct @143 value
#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
static void InitPgFdwOptions(void)
Definition: option.c:208

Variable Documentation

◆ libpq_options

PQconninfoOption* libpq_options
static

Definition at line 46 of file option.c.

◆ postgres_fdw_options

PgFdwOption* postgres_fdw_options
static

Definition at line 40 of file option.c.

Referenced by InitPgFdwOptions().