PostgreSQL Source Code  git master
option.c
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * option.c
4  * FDW and GUC option handling for postgres_fdw
5  *
6  * Portions Copyright (c) 2012-2021, PostgreSQL Global Development Group
7  *
8  * IDENTIFICATION
9  * contrib/postgres_fdw/option.c
10  *
11  *-------------------------------------------------------------------------
12  */
13 #include "postgres.h"
14 
15 #include "access/reloptions.h"
19 #include "commands/defrem.h"
20 #include "commands/extension.h"
21 #include "postgres_fdw.h"
22 #include "utils/builtins.h"
23 #include "utils/guc.h"
24 #include "utils/varlena.h"
25 
26 /*
27  * Describes the valid options for objects that this wrapper uses.
28  */
29 typedef struct PgFdwOption
30 {
31  const char *keyword;
32  Oid optcontext; /* OID of catalog in which option may appear */
33  bool is_libpq_opt; /* true if it's used in libpq */
35 
36 /*
37  * Valid options for postgres_fdw.
38  * Allocated and filled in InitPgFdwOptions.
39  */
41 
42 /*
43  * Valid options for libpq.
44  * Allocated and filled in InitPgFdwOptions.
45  */
47 
48 /*
49  * GUC parameters
50  */
52 
53 void _PG_init(void);
54 
55 /*
56  * Helper functions
57  */
58 static void InitPgFdwOptions(void);
59 static bool is_valid_option(const char *keyword, Oid context);
60 static bool is_libpq_option(const char *keyword);
61 
62 #include "miscadmin.h"
63 
64 /*
65  * Validate the generic options given to a FOREIGN DATA WRAPPER, SERVER,
66  * USER MAPPING or FOREIGN TABLE that uses postgres_fdw.
67  *
68  * Raise an ERROR if the option or its value is considered invalid.
69  */
71 
72 Datum
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 
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  buf.len > 0
111  ? errhint("Valid options in this context are: %s",
112  buf.data)
113  : errhint("There are no valid options in this context.")));
114  }
115 
116  /*
117  * Validate option value, when we can do so without any context.
118  */
119  if (strcmp(def->defname, "use_remote_estimate") == 0 ||
120  strcmp(def->defname, "updatable") == 0 ||
121  strcmp(def->defname, "truncatable") == 0 ||
122  strcmp(def->defname, "async_capable") == 0 ||
123  strcmp(def->defname, "keep_connections") == 0)
124  {
125  /* these accept only boolean values */
126  (void) defGetBoolean(def);
127  }
128  else if (strcmp(def->defname, "fdw_startup_cost") == 0 ||
129  strcmp(def->defname, "fdw_tuple_cost") == 0)
130  {
131  /*
132  * These must have a floating point value greater than or equal to
133  * zero.
134  */
135  char *value;
136  double real_val;
137  bool is_parsed;
138 
139  value = defGetString(def);
140  is_parsed = parse_real(value, &real_val, 0, NULL);
141 
142  if (!is_parsed)
143  ereport(ERROR,
144  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
145  errmsg("invalid value for floating point option \"%s\": %s",
146  def->defname, value)));
147 
148  if (real_val < 0)
149  ereport(ERROR,
150  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
151  errmsg("\"%s\" must be a floating point value greater than or equal to zero",
152  def->defname)));
153  }
154  else if (strcmp(def->defname, "extensions") == 0)
155  {
156  /* check list syntax, warn about uninstalled extensions */
157  (void) ExtractExtensionList(defGetString(def), true);
158  }
159  else if (strcmp(def->defname, "fetch_size") == 0 ||
160  strcmp(def->defname, "batch_size") == 0)
161  {
162  char *value;
163  int int_val;
164  bool is_parsed;
165 
166  value = defGetString(def);
167  is_parsed = parse_int(value, &int_val, 0, NULL);
168 
169  if (!is_parsed)
170  ereport(ERROR,
171  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
172  errmsg("invalid value for integer option \"%s\": %s",
173  def->defname, value)));
174 
175  if (int_val <= 0)
176  ereport(ERROR,
177  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
178  errmsg("\"%s\" must be an integer value greater than zero",
179  def->defname)));
180  }
181  else if (strcmp(def->defname, "password_required") == 0)
182  {
183  bool pw_required = defGetBoolean(def);
184 
185  /*
186  * Only the superuser may set this option on a user mapping, or
187  * alter a user mapping on which this option is set. We allow a
188  * user to clear this option if it's set - in fact, we don't have
189  * a choice since we can't see the old mapping when validating an
190  * alter.
191  */
192  if (!superuser() && !pw_required)
193  ereport(ERROR,
194  (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
195  errmsg("password_required=false is superuser-only"),
196  errhint("User mappings with the password_required option set to false may only be created or modified by the superuser")));
197  }
198  else if (strcmp(def->defname, "sslcert") == 0 ||
199  strcmp(def->defname, "sslkey") == 0)
200  {
201  /* similarly for sslcert / sslkey on user mapping */
202  if (catalog == UserMappingRelationId && !superuser())
203  ereport(ERROR,
204  (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
205  errmsg("sslcert and sslkey are superuser-only"),
206  errhint("User mappings with the sslcert or sslkey options set may only be created or modified by the superuser")));
207  }
208  }
209 
210  PG_RETURN_VOID();
211 }
212 
213 /*
214  * Initialize option lists.
215  */
216 static void
218 {
219  int num_libpq_opts;
220  PQconninfoOption *lopt;
221  PgFdwOption *popt;
222 
223  /* non-libpq FDW-specific FDW options */
224  static const PgFdwOption non_libpq_options[] = {
225  {"schema_name", ForeignTableRelationId, false},
226  {"table_name", ForeignTableRelationId, false},
227  {"column_name", AttributeRelationId, false},
228  /* use_remote_estimate is available on both server and table */
229  {"use_remote_estimate", ForeignServerRelationId, false},
230  {"use_remote_estimate", ForeignTableRelationId, false},
231  /* cost factors */
232  {"fdw_startup_cost", ForeignServerRelationId, false},
233  {"fdw_tuple_cost", ForeignServerRelationId, false},
234  /* shippable extensions */
235  {"extensions", ForeignServerRelationId, false},
236  /* updatable is available on both server and table */
237  {"updatable", ForeignServerRelationId, false},
238  {"updatable", ForeignTableRelationId, false},
239  /* truncatable is available on both server and table */
240  {"truncatable", ForeignServerRelationId, false},
241  {"truncatable", ForeignTableRelationId, false},
242  /* fetch_size is available on both server and table */
243  {"fetch_size", ForeignServerRelationId, false},
244  {"fetch_size", ForeignTableRelationId, false},
245  /* batch_size is available on both server and table */
246  {"batch_size", ForeignServerRelationId, false},
247  {"batch_size", ForeignTableRelationId, false},
248  /* async_capable is available on both server and table */
249  {"async_capable", ForeignServerRelationId, false},
250  {"async_capable", ForeignTableRelationId, false},
251  {"keep_connections", ForeignServerRelationId, false},
252  {"password_required", UserMappingRelationId, false},
253 
254  /*
255  * sslcert and sslkey are in fact libpq options, but we repeat them
256  * here to allow them to appear in both foreign server context (when
257  * we generate libpq options) and user mapping context (from here).
258  */
259  {"sslcert", UserMappingRelationId, true},
260  {"sslkey", UserMappingRelationId, true},
261 
262  {NULL, InvalidOid, false}
263  };
264 
265  /* Prevent redundant initialization. */
267  return;
268 
269  /*
270  * Get list of valid libpq options.
271  *
272  * To avoid unnecessary work, we get the list once and use it throughout
273  * the lifetime of this backend process. We don't need to care about
274  * memory context issues, because PQconndefaults allocates with malloc.
275  */
277  if (!libpq_options) /* assume reason for failure is OOM */
278  ereport(ERROR,
279  (errcode(ERRCODE_FDW_OUT_OF_MEMORY),
280  errmsg("out of memory"),
281  errdetail("Could not get libpq's default connection options.")));
282 
283  /* Count how many libpq options are available. */
284  num_libpq_opts = 0;
285  for (lopt = libpq_options; lopt->keyword; lopt++)
286  num_libpq_opts++;
287 
288  /*
289  * Construct an array which consists of all valid options for
290  * postgres_fdw, by appending FDW-specific options to libpq options.
291  *
292  * We use plain malloc here to allocate postgres_fdw_options because it
293  * lives as long as the backend process does. Besides, keeping
294  * libpq_options in memory allows us to avoid copying every keyword
295  * string.
296  */
298  malloc(sizeof(PgFdwOption) * num_libpq_opts +
299  sizeof(non_libpq_options));
300  if (postgres_fdw_options == NULL)
301  ereport(ERROR,
302  (errcode(ERRCODE_FDW_OUT_OF_MEMORY),
303  errmsg("out of memory")));
304 
305  popt = postgres_fdw_options;
306  for (lopt = libpq_options; lopt->keyword; lopt++)
307  {
308  /* Hide debug options, as well as settings we override internally. */
309  if (strchr(lopt->dispchar, 'D') ||
310  strcmp(lopt->keyword, "fallback_application_name") == 0 ||
311  strcmp(lopt->keyword, "client_encoding") == 0)
312  continue;
313 
314  /* We don't have to copy keyword string, as described above. */
315  popt->keyword = lopt->keyword;
316 
317  /*
318  * "user" and any secret options are allowed only on user mappings.
319  * Everything else is a server option.
320  */
321  if (strcmp(lopt->keyword, "user") == 0 || strchr(lopt->dispchar, '*'))
322  popt->optcontext = UserMappingRelationId;
323  else
324  popt->optcontext = ForeignServerRelationId;
325  popt->is_libpq_opt = true;
326 
327  popt++;
328  }
329 
330  /* Append FDW-specific options and dummy terminator. */
331  memcpy(popt, non_libpq_options, sizeof(non_libpq_options));
332 }
333 
334 /*
335  * Check whether the given option is one of the valid postgres_fdw options.
336  * context is the Oid of the catalog holding the object the option is for.
337  */
338 static bool
339 is_valid_option(const char *keyword, Oid context)
340 {
341  PgFdwOption *opt;
342 
343  Assert(postgres_fdw_options); /* must be initialized already */
344 
345  for (opt = postgres_fdw_options; opt->keyword; opt++)
346  {
347  if (context == opt->optcontext && strcmp(opt->keyword, keyword) == 0)
348  return true;
349  }
350 
351  return false;
352 }
353 
354 /*
355  * Check whether the given option is one of the valid libpq options.
356  */
357 static bool
358 is_libpq_option(const char *keyword)
359 {
360  PgFdwOption *opt;
361 
362  Assert(postgres_fdw_options); /* must be initialized already */
363 
364  for (opt = postgres_fdw_options; opt->keyword; opt++)
365  {
366  if (opt->is_libpq_opt && strcmp(opt->keyword, keyword) == 0)
367  return true;
368  }
369 
370  return false;
371 }
372 
373 /*
374  * Generate key-value arrays which include only libpq options from the
375  * given list (which can contain any kind of options). Caller must have
376  * allocated large-enough arrays. Returns number of options found.
377  */
378 int
379 ExtractConnectionOptions(List *defelems, const char **keywords,
380  const char **values)
381 {
382  ListCell *lc;
383  int i;
384 
385  /* Build our options lists if we didn't yet. */
387 
388  i = 0;
389  foreach(lc, defelems)
390  {
391  DefElem *d = (DefElem *) lfirst(lc);
392 
393  if (is_libpq_option(d->defname))
394  {
395  keywords[i] = d->defname;
396  values[i] = defGetString(d);
397  i++;
398  }
399  }
400  return i;
401 }
402 
403 /*
404  * Parse a comma-separated string and return a List of the OIDs of the
405  * extensions named in the string. If any names in the list cannot be
406  * found, report a warning if warnOnMissing is true, else just silently
407  * ignore them.
408  */
409 List *
410 ExtractExtensionList(const char *extensionsString, bool warnOnMissing)
411 {
412  List *extensionOids = NIL;
413  List *extlist;
414  ListCell *lc;
415 
416  /* SplitIdentifierString scribbles on its input, so pstrdup first */
417  if (!SplitIdentifierString(pstrdup(extensionsString), ',', &extlist))
418  {
419  /* syntax error in name list */
420  ereport(ERROR,
421  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
422  errmsg("parameter \"%s\" must be a list of extension names",
423  "extensions")));
424  }
425 
426  foreach(lc, extlist)
427  {
428  const char *extension_name = (const char *) lfirst(lc);
429  Oid extension_oid = get_extension_oid(extension_name, true);
430 
431  if (OidIsValid(extension_oid))
432  {
433  extensionOids = lappend_oid(extensionOids, extension_oid);
434  }
435  else if (warnOnMissing)
436  {
438  (errcode(ERRCODE_UNDEFINED_OBJECT),
439  errmsg("extension \"%s\" is not installed",
440  extension_name)));
441  }
442  }
443 
444  list_free(extlist);
445  return extensionOids;
446 }
447 
448 /*
449  * Module load callback
450  */
451 void
452 _PG_init(void)
453 {
454  /*
455  * Unlike application_name GUC, don't set GUC_IS_NAME flag nor check_hook
456  * to allow postgres_fdw.application_name to be any string more than
457  * NAMEDATALEN characters and to include non-ASCII characters. Instead,
458  * remote server truncates application_name of remote connection to less
459  * than NAMEDATALEN and replaces any non-ASCII characters in it with a '?'
460  * character.
461  */
462  DefineCustomStringVariable("postgres_fdw.application_name",
463  "Sets the application name to be used on the remote server.",
464  NULL,
466  NULL,
467  PGC_USERSET,
468  0,
469  NULL,
470  NULL,
471  NULL);
472 }
static Datum values[MAXATTR]
Definition: bootstrap.c:156
#define OidIsValid(objectId)
Definition: c.h:710
void _PG_init(void)
Definition: option.c:452
static bool is_valid_option(const char *keyword, Oid context)
Definition: option.c:339
static PQconninfoOption * libpq_options
Definition: option.c:46
int ExtractConnectionOptions(List *defelems, const char **keywords, const char **values)
Definition: option.c:379
static bool is_libpq_option(const char *keyword)
Definition: option.c:358
List * ExtractExtensionList(const char *extensionsString, bool warnOnMissing)
Definition: option.c:410
Datum postgres_fdw_validator(PG_FUNCTION_ARGS)
Definition: option.c:73
static PgFdwOption * postgres_fdw_options
Definition: option.c:40
struct PgFdwOption PgFdwOption
PG_FUNCTION_INFO_V1(postgres_fdw_validator)
char * pgfdw_application_name
Definition: option.c:51
static void InitPgFdwOptions(void)
Definition: option.c:217
bool defGetBoolean(DefElem *def)
Definition: define.c:106
char * defGetString(DefElem *def)
Definition: define.c:49
int errdetail(const char *fmt,...)
Definition: elog.c:1042
int errhint(const char *fmt,...)
Definition: elog.c:1156
int errcode(int sqlerrcode)
Definition: elog.c:698
int errmsg(const char *fmt,...)
Definition: elog.c:909
#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
PQconninfoOption * PQconndefaults(void)
Definition: fe-connect.c:1472
#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
#define PG_FUNCTION_ARGS
Definition: fmgr.h:193
bool parse_int(const char *value, int *result, int flags, const char **hintmsg)
Definition: guc.c:6871
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:9272
bool parse_real(const char *value, double *result, int flags, const char **hintmsg)
Definition: guc.c:6961
@ PGC_USERSET
Definition: guc.h:76
#define malloc(a)
Definition: header.h:50
static struct @142 value
int i
Definition: isn.c:73
Assert(fmt[strlen(fmt) - 1] !='\n')
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:1299
#define lfirst(lc)
Definition: pg_list.h:169
#define NIL
Definition: pg_list.h:65
static char * buf
Definition: pg_test_fsync.c:70
uintptr_t Datum
Definition: postgres.h:411
#define InvalidOid
Definition: postgres_ext.h:36
unsigned int Oid
Definition: postgres_ext.h:31
List * untransformRelOptions(Datum options)
Definition: reloptions.c:1325
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:91
void initStringInfo(StringInfo str)
Definition: stringinfo.c:59
char * defname
Definition: parsenodes.h:757
Definition: pg_list.h:51
Oid optcontext
Definition: option.c:32
const char * keyword
Definition: option.c:31
bool is_libpq_opt
Definition: option.c:33
bool superuser(void)
Definition: superuser.c:46
bool SplitIdentifierString(char *rawstring, char separator, List **namelist)
Definition: varlena.c:3746