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

void _PG_init (void)
 
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
 
char * pgfdw_application_name = NULL
 

Typedef Documentation

◆ PgFdwOption

typedef struct PgFdwOption PgFdwOption

Function Documentation

◆ _PG_init()

void _PG_init ( void  )

Definition at line 450 of file option.c.

References DefineCustomStringVariable(), PGC_USERSET, and pgfdw_application_name.

451 {
452  /*
453  * Unlike application_name GUC, don't set GUC_IS_NAME flag nor check_hook
454  * to allow postgres_fdw.application_name to be any string more than
455  * NAMEDATALEN characters and to include non-ASCII characters. Instead,
456  * remote server truncates application_name of remote connection to less
457  * than NAMEDATALEN and replaces any non-ASCII characters in it with a '?'
458  * character.
459  */
460  DefineCustomStringVariable("postgres_fdw.application_name",
461  "Sets the application name to be used on the remote server.",
462  NULL,
464  NULL,
465  PGC_USERSET,
466  0,
467  NULL,
468  NULL,
469  NULL);
470 }
char * pgfdw_application_name
Definition: option.c:51
void DefineCustomStringVariable(const char *name, const char *short_desc, const char *long_desc, char **valueAddr, const char *bootValue, GucContext context, int flags, GucStringCheckHook check_hook, GucStringAssignHook assign_hook, GucShowHook show_hook)
Definition: guc.c:9260

◆ ExtractConnectionOptions()

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

Definition at line 377 of file option.c.

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

Referenced by connect_pg_server().

379 {
380  ListCell *lc;
381  int i;
382 
383  /* Build our options lists if we didn't yet. */
385 
386  i = 0;
387  foreach(lc, defelems)
388  {
389  DefElem *d = (DefElem *) lfirst(lc);
390 
391  if (is_libpq_option(d->defname))
392  {
393  keywords[i] = d->defname;
394  values[i] = defGetString(d);
395  i++;
396  }
397  }
398  return i;
399 }
char * defGetString(DefElem *def)
Definition: define.c:49
#define lfirst(lc)
Definition: pg_list.h:169
static Datum values[MAXATTR]
Definition: bootstrap.c:156
int i
static bool is_libpq_option(const char *keyword)
Definition: option.c:356
char * defname
Definition: parsenodes.h:758
static void InitPgFdwOptions(void)
Definition: option.c:215

◆ ExtractExtensionList()

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

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

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

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

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

Referenced by ExtractConnectionOptions().

357 {
358  PgFdwOption *opt;
359 
360  Assert(postgres_fdw_options); /* must be initialized already */
361 
362  for (opt = postgres_fdw_options; opt->keyword; opt++)
363  {
364  if (opt->is_libpq_opt && strcmp(opt->keyword, keyword) == 0)
365  return true;
366  }
367 
368  return false;
369 }
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 337 of file option.c.

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

Referenced by postgres_fdw_validator().

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

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

Variable Documentation

◆ libpq_options

PQconninfoOption* libpq_options
static

Definition at line 46 of file option.c.

◆ pgfdw_application_name

char* pgfdw_application_name = NULL

Definition at line 51 of file option.c.

Referenced by _PG_init(), and connect_pg_server().

◆ postgres_fdw_options

PgFdwOption* postgres_fdw_options
static

Definition at line 40 of file option.c.

Referenced by InitPgFdwOptions().