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 "libpq/libpq-be.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)
 
char * process_pgfdw_appname (const char *appname)
 
void _PG_init (void)
 

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

569 {
570  /*
571  * Unlike application_name GUC, don't set GUC_IS_NAME flag nor check_hook
572  * to allow postgres_fdw.application_name to be any string more than
573  * NAMEDATALEN characters and to include non-ASCII characters. Instead,
574  * remote server truncates application_name of remote connection to less
575  * than NAMEDATALEN and replaces any non-ASCII characters in it with a '?'
576  * character.
577  */
578  DefineCustomStringVariable("postgres_fdw.application_name",
579  "Sets the application name to be used on the remote server.",
580  NULL,
582  NULL,
583  PGC_USERSET,
584  0,
585  NULL,
586  NULL,
587  NULL);
588 
589  MarkGUCPrefixReserved("postgres_fdw");
590 }
char * pgfdw_application_name
Definition: option.c:52
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:5171
void MarkGUCPrefixReserved(const char *className)
Definition: guc.c:5232
@ PGC_USERSET
Definition: guc.h:75

References DefineCustomStringVariable(), MarkGUCPrefixReserved(), PGC_USERSET, and pgfdw_application_name.

◆ ExtractConnectionOptions()

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

Definition at line 414 of file option.c.

416 {
417  ListCell *lc;
418  int i;
419 
420  /* Build our options lists if we didn't yet. */
422 
423  i = 0;
424  foreach(lc, defelems)
425  {
426  DefElem *d = (DefElem *) lfirst(lc);
427 
428  if (is_libpq_option(d->defname))
429  {
430  keywords[i] = d->defname;
431  values[i] = defGetString(d);
432  i++;
433  }
434  }
435  return i;
436 }
static Datum values[MAXATTR]
Definition: bootstrap.c:152
static bool is_libpq_option(const char *keyword)
Definition: option.c:393
static void InitPgFdwOptions(void)
Definition: option.c:240
char * defGetString(DefElem *def)
Definition: define.c:48
int i
Definition: isn.c:73
#define lfirst(lc)
Definition: pg_list.h:172
char * defname
Definition: parsenodes.h:815

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

Referenced by connect_pg_server().

◆ ExtractExtensionList()

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

Definition at line 445 of file option.c.

446 {
447  List *extensionOids = NIL;
448  List *extlist;
449  ListCell *lc;
450 
451  /* SplitIdentifierString scribbles on its input, so pstrdup first */
452  if (!SplitIdentifierString(pstrdup(extensionsString), ',', &extlist))
453  {
454  /* syntax error in name list */
455  ereport(ERROR,
456  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
457  errmsg("parameter \"%s\" must be a list of extension names",
458  "extensions")));
459  }
460 
461  foreach(lc, extlist)
462  {
463  const char *extension_name = (const char *) lfirst(lc);
464  Oid extension_oid = get_extension_oid(extension_name, true);
465 
466  if (OidIsValid(extension_oid))
467  {
468  extensionOids = lappend_oid(extensionOids, extension_oid);
469  }
470  else if (warnOnMissing)
471  {
473  (errcode(ERRCODE_UNDEFINED_OBJECT),
474  errmsg("extension \"%s\" is not installed",
475  extension_name)));
476  }
477  }
478 
479  list_free(extlist);
480  return extensionOids;
481 }
#define OidIsValid(objectId)
Definition: c.h:775
int errcode(int sqlerrcode)
Definition: elog.c:859
int errmsg(const char *fmt,...)
Definition: elog.c:1072
#define WARNING
Definition: elog.h:36
#define ERROR
Definition: elog.h:39
#define ereport(elevel,...)
Definition: elog.h:149
Oid get_extension_oid(const char *extname, bool missing_ok)
Definition: extension.c:145
List * lappend_oid(List *list, Oid datum)
Definition: list.c:375
void list_free(List *list)
Definition: list.c:1546
char * pstrdup(const char *in)
Definition: mcxt.c:1695
#define NIL
Definition: pg_list.h:68
unsigned int Oid
Definition: postgres_ext.h:31
Definition: pg_list.h:54
bool SplitIdentifierString(char *rawstring, char separator, List **namelist)
Definition: varlena.c:3457

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

◆ InitPgFdwOptions()

static void InitPgFdwOptions ( void  )
static

Definition at line 240 of file option.c.

241 {
242  int num_libpq_opts;
243  PQconninfoOption *lopt;
244  PgFdwOption *popt;
245 
246  /* non-libpq FDW-specific FDW options */
247  static const PgFdwOption non_libpq_options[] = {
248  {"schema_name", ForeignTableRelationId, false},
249  {"table_name", ForeignTableRelationId, false},
250  {"column_name", AttributeRelationId, false},
251  /* use_remote_estimate is available on both server and table */
252  {"use_remote_estimate", ForeignServerRelationId, false},
253  {"use_remote_estimate", ForeignTableRelationId, false},
254  /* cost factors */
255  {"fdw_startup_cost", ForeignServerRelationId, false},
256  {"fdw_tuple_cost", ForeignServerRelationId, false},
257  /* shippable extensions */
258  {"extensions", ForeignServerRelationId, false},
259  /* updatable is available on both server and table */
260  {"updatable", ForeignServerRelationId, false},
261  {"updatable", ForeignTableRelationId, false},
262  /* truncatable is available on both server and table */
263  {"truncatable", ForeignServerRelationId, false},
264  {"truncatable", ForeignTableRelationId, false},
265  /* fetch_size is available on both server and table */
266  {"fetch_size", ForeignServerRelationId, false},
267  {"fetch_size", ForeignTableRelationId, false},
268  /* batch_size is available on both server and table */
269  {"batch_size", ForeignServerRelationId, false},
270  {"batch_size", ForeignTableRelationId, false},
271  /* async_capable is available on both server and table */
272  {"async_capable", ForeignServerRelationId, false},
273  {"async_capable", ForeignTableRelationId, false},
274  {"parallel_commit", ForeignServerRelationId, false},
275  {"parallel_abort", ForeignServerRelationId, false},
276  {"keep_connections", ForeignServerRelationId, false},
277  {"password_required", UserMappingRelationId, false},
278 
279  /* sampling is available on both server and table */
280  {"analyze_sampling", ForeignServerRelationId, false},
281  {"analyze_sampling", ForeignTableRelationId, false},
282 
283  /*
284  * sslcert and sslkey are in fact libpq options, but we repeat them
285  * here to allow them to appear in both foreign server context (when
286  * we generate libpq options) and user mapping context (from here).
287  */
288  {"sslcert", UserMappingRelationId, true},
289  {"sslkey", UserMappingRelationId, true},
290 
291  /*
292  * gssdelegation is also a libpq option but should be allowed in a
293  * user mapping context too
294  */
295  {"gssdelegation", UserMappingRelationId, true},
296 
297  {NULL, InvalidOid, false}
298  };
299 
300  /* Prevent redundant initialization. */
302  return;
303 
304  /*
305  * Get list of valid libpq options.
306  *
307  * To avoid unnecessary work, we get the list once and use it throughout
308  * the lifetime of this backend process. We don't need to care about
309  * memory context issues, because PQconndefaults allocates with malloc.
310  */
312  if (!libpq_options) /* assume reason for failure is OOM */
313  ereport(ERROR,
314  (errcode(ERRCODE_FDW_OUT_OF_MEMORY),
315  errmsg("out of memory"),
316  errdetail("Could not get libpq's default connection options.")));
317 
318  /* Count how many libpq options are available. */
319  num_libpq_opts = 0;
320  for (lopt = libpq_options; lopt->keyword; lopt++)
321  num_libpq_opts++;
322 
323  /*
324  * Construct an array which consists of all valid options for
325  * postgres_fdw, by appending FDW-specific options to libpq options.
326  *
327  * We use plain malloc here to allocate postgres_fdw_options because it
328  * lives as long as the backend process does. Besides, keeping
329  * libpq_options in memory allows us to avoid copying every keyword
330  * string.
331  */
333  malloc(sizeof(PgFdwOption) * num_libpq_opts +
334  sizeof(non_libpq_options));
335  if (postgres_fdw_options == NULL)
336  ereport(ERROR,
337  (errcode(ERRCODE_FDW_OUT_OF_MEMORY),
338  errmsg("out of memory")));
339 
340  popt = postgres_fdw_options;
341  for (lopt = libpq_options; lopt->keyword; lopt++)
342  {
343  /* Hide debug options, as well as settings we override internally. */
344  if (strchr(lopt->dispchar, 'D') ||
345  strcmp(lopt->keyword, "fallback_application_name") == 0 ||
346  strcmp(lopt->keyword, "client_encoding") == 0)
347  continue;
348 
349  /* We don't have to copy keyword string, as described above. */
350  popt->keyword = lopt->keyword;
351 
352  /*
353  * "user" and any secret options are allowed only on user mappings.
354  * Everything else is a server option.
355  */
356  if (strcmp(lopt->keyword, "user") == 0 || strchr(lopt->dispchar, '*'))
357  popt->optcontext = UserMappingRelationId;
358  else
359  popt->optcontext = ForeignServerRelationId;
360  popt->is_libpq_opt = true;
361 
362  popt++;
363  }
364 
365  /* Append FDW-specific options and dummy terminator. */
366  memcpy(popt, non_libpq_options, sizeof(non_libpq_options));
367 }
static PQconninfoOption * libpq_options
Definition: option.c:47
static PgFdwOption * postgres_fdw_options
Definition: option.c:41
int errdetail(const char *fmt,...)
Definition: elog.c:1205
PQconninfoOption * PQconndefaults(void)
Definition: fe-connect.c:1863
#define malloc(a)
Definition: header.h:50
#define InvalidOid
Definition: postgres_ext.h:36
Oid optcontext
Definition: option.c:33
const char * keyword
Definition: option.c:32
bool is_libpq_opt
Definition: option.c:34

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

Referenced by ExtractConnectionOptions(), and postgres_fdw_validator().

◆ is_libpq_option()

static bool is_libpq_option ( const char *  keyword)
static

Definition at line 393 of file option.c.

394 {
395  PgFdwOption *opt;
396 
397  Assert(postgres_fdw_options); /* must be initialized already */
398 
399  for (opt = postgres_fdw_options; opt->keyword; opt++)
400  {
401  if (opt->is_libpq_opt && strcmp(opt->keyword, keyword) == 0)
402  return true;
403  }
404 
405  return false;
406 }
#define Assert(condition)
Definition: c.h:858

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

Referenced by ExtractConnectionOptions().

◆ is_valid_option()

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

Definition at line 374 of file option.c.

375 {
376  PgFdwOption *opt;
377 
378  Assert(postgres_fdw_options); /* must be initialized already */
379 
380  for (opt = postgres_fdw_options; opt->keyword; opt++)
381  {
382  if (context == opt->optcontext && strcmp(opt->keyword, keyword) == 0)
383  return true;
384  }
385 
386  return false;
387 }
tree context
Definition: radixtree.h:1833

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

Referenced by postgres_fdw_validator().

◆ PG_FUNCTION_INFO_V1()

PG_FUNCTION_INFO_V1 ( postgres_fdw_validator  )

◆ postgres_fdw_validator()

Datum postgres_fdw_validator ( PG_FUNCTION_ARGS  )

Definition at line 72 of file option.c.

73 {
74  List *options_list = untransformRelOptions(PG_GETARG_DATUM(0));
75  Oid catalog = PG_GETARG_OID(1);
76  ListCell *cell;
77 
78  /* Build our options lists if we didn't yet. */
80 
81  /*
82  * Check that only options supported by postgres_fdw, and allowed for the
83  * current object type, are given.
84  */
85  foreach(cell, options_list)
86  {
87  DefElem *def = (DefElem *) lfirst(cell);
88 
89  if (!is_valid_option(def->defname, catalog))
90  {
91  /*
92  * Unknown option specified, complain about it. Provide a hint
93  * with a valid option that looks similar, if there is one.
94  */
95  PgFdwOption *opt;
96  const char *closest_match;
98  bool has_valid_options = false;
99 
101  for (opt = postgres_fdw_options; opt->keyword; opt++)
102  {
103  if (catalog == opt->optcontext)
104  {
105  has_valid_options = true;
107  }
108  }
109 
110  closest_match = getClosestMatch(&match_state);
111  ereport(ERROR,
112  (errcode(ERRCODE_FDW_INVALID_OPTION_NAME),
113  errmsg("invalid option \"%s\"", def->defname),
114  has_valid_options ? closest_match ?
115  errhint("Perhaps you meant the option \"%s\".",
116  closest_match) : 0 :
117  errhint("There are no valid options in this context.")));
118  }
119 
120  /*
121  * Validate option value, when we can do so without any context.
122  */
123  if (strcmp(def->defname, "use_remote_estimate") == 0 ||
124  strcmp(def->defname, "updatable") == 0 ||
125  strcmp(def->defname, "truncatable") == 0 ||
126  strcmp(def->defname, "async_capable") == 0 ||
127  strcmp(def->defname, "parallel_commit") == 0 ||
128  strcmp(def->defname, "parallel_abort") == 0 ||
129  strcmp(def->defname, "keep_connections") == 0)
130  {
131  /* these accept only boolean values */
132  (void) defGetBoolean(def);
133  }
134  else if (strcmp(def->defname, "fdw_startup_cost") == 0 ||
135  strcmp(def->defname, "fdw_tuple_cost") == 0)
136  {
137  /*
138  * These must have a floating point value greater than or equal to
139  * zero.
140  */
141  char *value;
142  double real_val;
143  bool is_parsed;
144 
145  value = defGetString(def);
146  is_parsed = parse_real(value, &real_val, 0, NULL);
147 
148  if (!is_parsed)
149  ereport(ERROR,
150  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
151  errmsg("invalid value for floating point option \"%s\": %s",
152  def->defname, value)));
153 
154  if (real_val < 0)
155  ereport(ERROR,
156  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
157  errmsg("\"%s\" must be a floating point value greater than or equal to zero",
158  def->defname)));
159  }
160  else if (strcmp(def->defname, "extensions") == 0)
161  {
162  /* check list syntax, warn about uninstalled extensions */
163  (void) ExtractExtensionList(defGetString(def), true);
164  }
165  else if (strcmp(def->defname, "fetch_size") == 0 ||
166  strcmp(def->defname, "batch_size") == 0)
167  {
168  char *value;
169  int int_val;
170  bool is_parsed;
171 
172  value = defGetString(def);
173  is_parsed = parse_int(value, &int_val, 0, NULL);
174 
175  if (!is_parsed)
176  ereport(ERROR,
177  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
178  errmsg("invalid value for integer option \"%s\": %s",
179  def->defname, value)));
180 
181  if (int_val <= 0)
182  ereport(ERROR,
183  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
184  errmsg("\"%s\" must be an integer value greater than zero",
185  def->defname)));
186  }
187  else if (strcmp(def->defname, "password_required") == 0)
188  {
189  bool pw_required = defGetBoolean(def);
190 
191  /*
192  * Only the superuser may set this option on a user mapping, or
193  * alter a user mapping on which this option is set. We allow a
194  * user to clear this option if it's set - in fact, we don't have
195  * a choice since we can't see the old mapping when validating an
196  * alter.
197  */
198  if (!superuser() && !pw_required)
199  ereport(ERROR,
200  (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
201  errmsg("password_required=false is superuser-only"),
202  errhint("User mappings with the password_required option set to false may only be created or modified by the superuser.")));
203  }
204  else if (strcmp(def->defname, "sslcert") == 0 ||
205  strcmp(def->defname, "sslkey") == 0)
206  {
207  /* similarly for sslcert / sslkey on user mapping */
208  if (catalog == UserMappingRelationId && !superuser())
209  ereport(ERROR,
210  (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
211  errmsg("sslcert and sslkey are superuser-only"),
212  errhint("User mappings with the sslcert or sslkey options set may only be created or modified by the superuser.")));
213  }
214  else if (strcmp(def->defname, "analyze_sampling") == 0)
215  {
216  char *value;
217 
218  value = defGetString(def);
219 
220  /* we recognize off/auto/random/system/bernoulli */
221  if (strcmp(value, "off") != 0 &&
222  strcmp(value, "auto") != 0 &&
223  strcmp(value, "random") != 0 &&
224  strcmp(value, "system") != 0 &&
225  strcmp(value, "bernoulli") != 0)
226  ereport(ERROR,
227  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
228  errmsg("invalid value for string option \"%s\": %s",
229  def->defname, value)));
230  }
231  }
232 
233  PG_RETURN_VOID();
234 }
static bool is_valid_option(const char *keyword, Oid context)
Definition: option.c:374
List * ExtractExtensionList(const char *extensionsString, bool warnOnMissing)
Definition: option.c:445
bool defGetBoolean(DefElem *def)
Definition: define.c:107
int errhint(const char *fmt,...)
Definition: elog.c:1319
#define PG_RETURN_VOID()
Definition: fmgr.h:349
#define PG_GETARG_OID(n)
Definition: fmgr.h:275
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:268
bool parse_int(const char *value, int *result, int flags, const char **hintmsg)
Definition: guc.c:2873
bool parse_real(const char *value, double *result, int flags, const char **hintmsg)
Definition: guc.c:2963
struct parser_state match_state[5]
static struct @155 value
List * untransformRelOptions(Datum options)
Definition: reloptions.c:1331
bool superuser(void)
Definition: superuser.c:46
const char * getClosestMatch(ClosestMatchState *state)
Definition: varlena.c:6243
void initClosestMatch(ClosestMatchState *state, const char *source, int max_d)
Definition: varlena.c:6188
void updateClosestMatch(ClosestMatchState *state, const char *candidate)
Definition: varlena.c:6208

References defGetBoolean(), defGetString(), DefElem::defname, ereport, errcode(), errhint(), errmsg(), ERROR, ExtractExtensionList(), getClosestMatch(), initClosestMatch(), InitPgFdwOptions(), is_valid_option(), PgFdwOption::keyword, lfirst, match_state, PgFdwOption::optcontext, parse_int(), parse_real(), PG_GETARG_DATUM, PG_GETARG_OID, PG_RETURN_VOID, postgres_fdw_options, superuser(), untransformRelOptions(), updateClosestMatch(), and value.

◆ process_pgfdw_appname()

char* process_pgfdw_appname ( const char *  appname)

Definition at line 491 of file option.c.

492 {
493  const char *p;
495 
497 
498  for (p = appname; *p != '\0'; p++)
499  {
500  if (*p != '%')
501  {
502  /* literal char, just copy */
504  continue;
505  }
506 
507  /* must be a '%', so skip to the next char */
508  p++;
509  if (*p == '\0')
510  break; /* format error - ignore it */
511  else if (*p == '%')
512  {
513  /* string contains %% */
514  appendStringInfoChar(&buf, '%');
515  continue;
516  }
517 
518  /* process the option */
519  switch (*p)
520  {
521  case 'a':
523  break;
524  case 'c':
525  appendStringInfo(&buf, "%lx.%x", (long) (MyStartTime), MyProcPid);
526  break;
527  case 'C':
529  break;
530  case 'd':
531  if (MyProcPort)
532  {
533  const char *dbname = MyProcPort->database_name;
534 
535  if (dbname)
537  else
538  appendStringInfoString(&buf, "[unknown]");
539  }
540  break;
541  case 'p':
542  appendStringInfo(&buf, "%d", MyProcPid);
543  break;
544  case 'u':
545  if (MyProcPort)
546  {
547  const char *username = MyProcPort->user_name;
548 
549  if (username)
551  else
552  appendStringInfoString(&buf, "[unknown]");
553  }
554  break;
555  default:
556  /* format error - ignore it */
557  break;
558  }
559  }
560 
561  return buf.data;
562 }
int MyProcPid
Definition: globals.c:45
struct Port * MyProcPort
Definition: globals.c:49
pg_time_t MyStartTime
Definition: globals.c:46
char * cluster_name
Definition: guc_tables.c:540
char * application_name
Definition: guc_tables.c:546
static char * buf
Definition: pg_test_fsync.c:73
const char * username
Definition: pgbench.c:296
char * dbname
Definition: streamutil.c:52
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:97
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:182
void appendStringInfoChar(StringInfo str, char ch)
Definition: stringinfo.c:194
void initStringInfo(StringInfo str)
Definition: stringinfo.c:59
char * user_name
Definition: libpq-be.h:152
char * database_name
Definition: libpq-be.h:151

References appendStringInfo(), appendStringInfoChar(), appendStringInfoString(), application_name, buf, cluster_name, Port::database_name, dbname, initStringInfo(), MyProcPid, MyProcPort, MyStartTime, Port::user_name, and username.

Referenced by connect_pg_server().

Variable Documentation

◆ libpq_options

PQconninfoOption* libpq_options
static

Definition at line 47 of file option.c.

Referenced by InitPgFdwOptions().

◆ pgfdw_application_name

char* pgfdw_application_name = NULL

Definition at line 52 of file option.c.

Referenced by _PG_init(), and connect_pg_server().

◆ postgres_fdw_options

PgFdwOption* postgres_fdw_options
static