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

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)
 
char * process_pgfdw_appname (const char *appname)
 

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

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

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

◆ ExtractConnectionOptions()

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

Definition at line 382 of file option.c.

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

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

414 {
415  List *extensionOids = NIL;
416  List *extlist;
417  ListCell *lc;
418 
419  /* SplitIdentifierString scribbles on its input, so pstrdup first */
420  if (!SplitIdentifierString(pstrdup(extensionsString), ',', &extlist))
421  {
422  /* syntax error in name list */
423  ereport(ERROR,
424  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
425  errmsg("parameter \"%s\" must be a list of extension names",
426  "extensions")));
427  }
428 
429  foreach(lc, extlist)
430  {
431  const char *extension_name = (const char *) lfirst(lc);
432  Oid extension_oid = get_extension_oid(extension_name, true);
433 
434  if (OidIsValid(extension_oid))
435  {
436  extensionOids = lappend_oid(extensionOids, extension_oid);
437  }
438  else if (warnOnMissing)
439  {
441  (errcode(ERRCODE_UNDEFINED_OBJECT),
442  errmsg("extension \"%s\" is not installed",
443  extension_name)));
444  }
445  }
446 
447  list_free(extlist);
448  return extensionOids;
449 }
#define OidIsValid(objectId)
Definition: c.h:710
int errcode(int sqlerrcode)
Definition: elog.c:693
int errmsg(const char *fmt,...)
Definition: elog.c:904
#define WARNING
Definition: elog.h:30
#define ERROR
Definition: elog.h:33
#define ereport(elevel,...)
Definition: elog.h:143
Oid get_extension_oid(const char *extname, bool missing_ok)
Definition: extension.c:140
List * lappend_oid(List *list, Oid datum)
Definition: list.c:372
void list_free(List *list)
Definition: list.c:1505
char * pstrdup(const char *in)
Definition: mcxt.c:1305
#define NIL
Definition: pg_list.h:65
unsigned int Oid
Definition: postgres_ext.h:31
Definition: pg_list.h:51
bool SplitIdentifierString(char *rawstring, char separator, List **namelist)
Definition: varlena.c:3715

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

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

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

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

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

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

References appendStringInfo(), buf, defGetBoolean(), defGetString(), DefElem::defname, ereport, errcode(), errhint(), errmsg(), ERROR, ExtractExtensionList(), InitPgFdwOptions(), initStringInfo(), 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(), and value.

◆ process_pgfdw_appname()

char* process_pgfdw_appname ( const char *  appname)

Definition at line 459 of file option.c.

460 {
461  const char *p;
463 
464  Assert(MyProcPort != NULL);
465 
467 
468  for (p = appname; *p != '\0'; p++)
469  {
470  if (*p != '%')
471  {
472  /* literal char, just copy */
474  continue;
475  }
476 
477  /* must be a '%', so skip to the next char */
478  p++;
479  if (*p == '\0')
480  break; /* format error - ignore it */
481  else if (*p == '%')
482  {
483  /* string contains %% */
484  appendStringInfoChar(&buf, '%');
485  continue;
486  }
487 
488  /* process the option */
489  switch (*p)
490  {
491  case 'a':
493  break;
494  case 'c':
495  appendStringInfo(&buf, "%lx.%x", (long) (MyStartTime), MyProcPid);
496  break;
497  case 'C':
499  break;
500  case 'd':
502  break;
503  case 'p':
504  appendStringInfo(&buf, "%d", MyProcPid);
505  break;
506  case 'u':
508  break;
509  default:
510  /* format error - ignore it */
511  break;
512  }
513  }
514 
515  return buf.data;
516 }
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.c:653
char * application_name
Definition: guc.c:661
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:176
void appendStringInfoChar(StringInfo str, char ch)
Definition: stringinfo.c:188
char * user_name
Definition: libpq-be.h:145
char * database_name
Definition: libpq-be.h:144

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