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

526 {
527  /*
528  * Unlike application_name GUC, don't set GUC_IS_NAME flag nor check_hook
529  * to allow postgres_fdw.application_name to be any string more than
530  * NAMEDATALEN characters and to include non-ASCII characters. Instead,
531  * remote server truncates application_name of remote connection to less
532  * than NAMEDATALEN and replaces any non-ASCII characters in it with a '?'
533  * character.
534  */
535  DefineCustomStringVariable("postgres_fdw.application_name",
536  "Sets the application name to be used on the remote server.",
537  NULL,
539  NULL,
540  PGC_USERSET,
541  0,
542  NULL,
543  NULL,
544  NULL);
545 
546  MarkGUCPrefixReserved("postgres_fdw");
547 }
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:5026
void MarkGUCPrefixReserved(const char *className)
Definition: guc.c:5087
@ 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 385 of file option.c.

387 {
388  ListCell *lc;
389  int i;
390 
391  /* Build our options lists if we didn't yet. */
393 
394  i = 0;
395  foreach(lc, defelems)
396  {
397  DefElem *d = (DefElem *) lfirst(lc);
398 
399  if (is_libpq_option(d->defname))
400  {
401  keywords[i] = d->defname;
402  values[i] = defGetString(d);
403  i++;
404  }
405  }
406  return i;
407 }
static Datum values[MAXATTR]
Definition: bootstrap.c:156
static bool is_libpq_option(const char *keyword)
Definition: option.c:364
static void InitPgFdwOptions(void)
Definition: option.c:222
char * defGetString(DefElem *def)
Definition: define.c:49
int i
Definition: isn.c:73
#define lfirst(lc)
Definition: pg_list.h:170
char * defname
Definition: parsenodes.h:777

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

417 {
418  List *extensionOids = NIL;
419  List *extlist;
420  ListCell *lc;
421 
422  /* SplitIdentifierString scribbles on its input, so pstrdup first */
423  if (!SplitIdentifierString(pstrdup(extensionsString), ',', &extlist))
424  {
425  /* syntax error in name list */
426  ereport(ERROR,
427  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
428  errmsg("parameter \"%s\" must be a list of extension names",
429  "extensions")));
430  }
431 
432  foreach(lc, extlist)
433  {
434  const char *extension_name = (const char *) lfirst(lc);
435  Oid extension_oid = get_extension_oid(extension_name, true);
436 
437  if (OidIsValid(extension_oid))
438  {
439  extensionOids = lappend_oid(extensionOids, extension_oid);
440  }
441  else if (warnOnMissing)
442  {
444  (errcode(ERRCODE_UNDEFINED_OBJECT),
445  errmsg("extension \"%s\" is not installed",
446  extension_name)));
447  }
448  }
449 
450  list_free(extlist);
451  return extensionOids;
452 }
#define OidIsValid(objectId)
Definition: c.h:711
int errcode(int sqlerrcode)
Definition: elog.c:695
int errmsg(const char *fmt,...)
Definition: elog.c:906
#define WARNING
Definition: elog.h:32
#define ERROR
Definition: elog.h:35
#define ereport(elevel,...)
Definition: elog.h:145
Oid get_extension_oid(const char *extname, bool missing_ok)
Definition: extension.c:142
List * lappend_oid(List *list, Oid datum)
Definition: list.c:374
void list_free(List *list)
Definition: list.c:1545
char * pstrdup(const char *in)
Definition: mcxt.c:1483
#define NIL
Definition: pg_list.h:66
unsigned int Oid
Definition: postgres_ext.h:31
Definition: pg_list.h:52
bool SplitIdentifierString(char *rawstring, char separator, List **namelist)
Definition: varlena.c:3712

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

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

365 {
366  PgFdwOption *opt;
367 
368  Assert(postgres_fdw_options); /* must be initialized already */
369 
370  for (opt = postgres_fdw_options; opt->keyword; opt++)
371  {
372  if (opt->is_libpq_opt && strcmp(opt->keyword, keyword) == 0)
373  return true;
374  }
375 
376  return false;
377 }
Assert(fmt[strlen(fmt) - 1] !='\n')

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

346 {
347  PgFdwOption *opt;
348 
349  Assert(postgres_fdw_options); /* must be initialized already */
350 
351  for (opt = postgres_fdw_options; opt->keyword; opt++)
352  {
353  if (context == opt->optcontext && strcmp(opt->keyword, keyword) == 0)
354  return true;
355  }
356 
357  return false;
358 }

References Assert(), 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;
97  ClosestMatchState match_state;
98  bool has_valid_options = false;
99 
100  initClosestMatch(&match_state, def->defname, 4);
101  for (opt = postgres_fdw_options; opt->keyword; opt++)
102  {
103  if (catalog == opt->optcontext)
104  {
105  has_valid_options = true;
106  updateClosestMatch(&match_state, opt->keyword);
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, "keep_connections") == 0)
129  {
130  /* these accept only boolean values */
131  (void) defGetBoolean(def);
132  }
133  else if (strcmp(def->defname, "fdw_startup_cost") == 0 ||
134  strcmp(def->defname, "fdw_tuple_cost") == 0)
135  {
136  /*
137  * These must have a floating point value greater than or equal to
138  * zero.
139  */
140  char *value;
141  double real_val;
142  bool is_parsed;
143 
144  value = defGetString(def);
145  is_parsed = parse_real(value, &real_val, 0, NULL);
146 
147  if (!is_parsed)
148  ereport(ERROR,
149  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
150  errmsg("invalid value for floating point option \"%s\": %s",
151  def->defname, value)));
152 
153  if (real_val < 0)
154  ereport(ERROR,
155  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
156  errmsg("\"%s\" must be a floating point value greater than or equal to zero",
157  def->defname)));
158  }
159  else if (strcmp(def->defname, "extensions") == 0)
160  {
161  /* check list syntax, warn about uninstalled extensions */
162  (void) ExtractExtensionList(defGetString(def), true);
163  }
164  else if (strcmp(def->defname, "fetch_size") == 0 ||
165  strcmp(def->defname, "batch_size") == 0)
166  {
167  char *value;
168  int int_val;
169  bool is_parsed;
170 
171  value = defGetString(def);
172  is_parsed = parse_int(value, &int_val, 0, NULL);
173 
174  if (!is_parsed)
175  ereport(ERROR,
176  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
177  errmsg("invalid value for integer option \"%s\": %s",
178  def->defname, value)));
179 
180  if (int_val <= 0)
181  ereport(ERROR,
182  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
183  errmsg("\"%s\" must be an integer value greater than zero",
184  def->defname)));
185  }
186  else if (strcmp(def->defname, "password_required") == 0)
187  {
188  bool pw_required = defGetBoolean(def);
189 
190  /*
191  * Only the superuser may set this option on a user mapping, or
192  * alter a user mapping on which this option is set. We allow a
193  * user to clear this option if it's set - in fact, we don't have
194  * a choice since we can't see the old mapping when validating an
195  * alter.
196  */
197  if (!superuser() && !pw_required)
198  ereport(ERROR,
199  (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
200  errmsg("password_required=false is superuser-only"),
201  errhint("User mappings with the password_required option set to false may only be created or modified by the superuser.")));
202  }
203  else if (strcmp(def->defname, "sslcert") == 0 ||
204  strcmp(def->defname, "sslkey") == 0)
205  {
206  /* similarly for sslcert / sslkey on user mapping */
207  if (catalog == UserMappingRelationId && !superuser())
208  ereport(ERROR,
209  (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
210  errmsg("sslcert and sslkey are superuser-only"),
211  errhint("User mappings with the sslcert or sslkey options set may only be created or modified by the superuser.")));
212  }
213  }
214 
215  PG_RETURN_VOID();
216 }
static bool is_valid_option(const char *keyword, Oid context)
Definition: option.c:345
List * ExtractExtensionList(const char *extensionsString, bool warnOnMissing)
Definition: option.c:416
bool defGetBoolean(DefElem *def)
Definition: define.c:108
int errhint(const char *fmt,...)
Definition: elog.c:1153
#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:2806
bool parse_real(const char *value, double *result, int flags, const char **hintmsg)
Definition: guc.c:2896
static struct @143 value
List * untransformRelOptions(Datum options)
Definition: reloptions.c:1333
bool superuser(void)
Definition: superuser.c:46
const char * getClosestMatch(ClosestMatchState *state)
Definition: varlena.c:6274
void initClosestMatch(ClosestMatchState *state, const char *source, int max_d)
Definition: varlena.c:6219
void updateClosestMatch(ClosestMatchState *state, const char *candidate)
Definition: varlena.c:6239

References defGetBoolean(), defGetString(), DefElem::defname, ereport, errcode(), errhint(), errmsg(), ERROR, ExtractExtensionList(), getClosestMatch(), initClosestMatch(), InitPgFdwOptions(), is_valid_option(), PgFdwOption::keyword, lfirst, 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 462 of file option.c.

463 {
464  const char *p;
466 
467  Assert(MyProcPort != NULL);
468 
470 
471  for (p = appname; *p != '\0'; p++)
472  {
473  if (*p != '%')
474  {
475  /* literal char, just copy */
477  continue;
478  }
479 
480  /* must be a '%', so skip to the next char */
481  p++;
482  if (*p == '\0')
483  break; /* format error - ignore it */
484  else if (*p == '%')
485  {
486  /* string contains %% */
487  appendStringInfoChar(&buf, '%');
488  continue;
489  }
490 
491  /* process the option */
492  switch (*p)
493  {
494  case 'a':
496  break;
497  case 'c':
498  appendStringInfo(&buf, "%lx.%x", (long) (MyStartTime), MyProcPid);
499  break;
500  case 'C':
502  break;
503  case 'd':
505  break;
506  case 'p':
507  appendStringInfo(&buf, "%d", MyProcPid);
508  break;
509  case 'u':
511  break;
512  default:
513  /* format error - ignore it */
514  break;
515  }
516  }
517 
518  return buf.data;
519 }
int MyProcPid
Definition: globals.c:44
struct Port * MyProcPort
Definition: globals.c:47
pg_time_t MyStartTime
Definition: globals.c:45
char * cluster_name
Definition: guc_tables.c:502
char * application_name
Definition: guc_tables.c:510
static char * buf
Definition: pg_test_fsync.c:67
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:91
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:176
void appendStringInfoChar(StringInfo str, char ch)
Definition: stringinfo.c:188
void initStringInfo(StringInfo str)
Definition: stringinfo.c:59
char * user_name
Definition: libpq-be.h:166
char * database_name
Definition: libpq-be.h:165

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

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