PostgreSQL Source Code git master
Data Structures | Typedefs | Functions | Variables
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/guc.h"
#include "utils/memutils.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)
 
 
 
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

 
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,
584 0,
585 NULL,
586 NULL,
587 NULL);
588
589 MarkGUCPrefixReserved("postgres_fdw");
590}
char * pgfdw_application_name
Definition: option.c:46
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:5240
void MarkGUCPrefixReserved(const char *className)
Definition: guc.c:5301
@ PGC_USERSET
Definition: guc.h:79

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

ExtractConnectionOptions()

int ExtractConnectionOptions ( Listdefelems,
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:153
static bool is_libpq_option(const char *keyword)
Definition: option.c:393
static void InitPgFdwOptions(void)
Definition: option.c:234
char * defGetString(DefElem *def)
Definition: define.c:35
i
int i
Definition: isn.c:77
static const JsonPathKeyword keywords[]
Definition: jsonpath_scan.l:402
#define lfirst(lc)
Definition: pg_list.h:172
char * defname
Definition: parsenodes.h:843
Definition: pg_list.h:46

References defGetString(), DefElem::defname, i, InitPgFdwOptions(), is_libpq_option(), keywords, 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 */
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:774
int errcode(int sqlerrcode)
Definition: elog.c:854
int errmsg(const char *fmt,...)
Definition: elog.c:1071
#define WARNING
Definition: elog.h:36
#define ERROR
Definition: elog.h:39
#define ereport(elevel,...)
Definition: elog.h:150
Oid get_extension_oid(const char *extname, bool missing_ok)
Definition: extension.c:167
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:1759
#define NIL
Definition: pg_list.h:68
unsigned int Oid
Definition: postgres_ext.h:32
Definition: pg_list.h:54
bool SplitIdentifierString(char *rawstring, char separator, List **namelist)
Definition: varlena.c:2744

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

235{
236 int num_libpq_opts;
237 PQconninfoOption *libpq_options;
238 PQconninfoOption *lopt;
239 PgFdwOption *popt;
240
241 /* non-libpq FDW-specific FDW options */
242 static const PgFdwOption non_libpq_options[] = {
243 {"schema_name", ForeignTableRelationId, false},
244 {"table_name", ForeignTableRelationId, false},
245 {"column_name", AttributeRelationId, false},
246 /* use_remote_estimate is available on both server and table */
247 {"use_remote_estimate", ForeignServerRelationId, false},
248 {"use_remote_estimate", ForeignTableRelationId, false},
249 /* cost factors */
250 {"fdw_startup_cost", ForeignServerRelationId, false},
251 {"fdw_tuple_cost", ForeignServerRelationId, false},
252 /* shippable extensions */
253 {"extensions", ForeignServerRelationId, false},
254 /* updatable is available on both server and table */
255 {"updatable", ForeignServerRelationId, false},
256 {"updatable", ForeignTableRelationId, false},
257 /* truncatable is available on both server and table */
258 {"truncatable", ForeignServerRelationId, false},
259 {"truncatable", ForeignTableRelationId, false},
260 /* fetch_size is available on both server and table */
261 {"fetch_size", ForeignServerRelationId, false},
262 {"fetch_size", ForeignTableRelationId, false},
263 /* batch_size is available on both server and table */
264 {"batch_size", ForeignServerRelationId, false},
265 {"batch_size", ForeignTableRelationId, false},
266 /* async_capable is available on both server and table */
267 {"async_capable", ForeignServerRelationId, false},
268 {"async_capable", ForeignTableRelationId, false},
269 {"parallel_commit", ForeignServerRelationId, false},
270 {"parallel_abort", ForeignServerRelationId, false},
271 {"keep_connections", ForeignServerRelationId, false},
272 {"password_required", UserMappingRelationId, false},
273
274 /* sampling is available on both server and table */
275 {"analyze_sampling", ForeignServerRelationId, false},
276 {"analyze_sampling", ForeignTableRelationId, false},
277
278 {"use_scram_passthrough", ForeignServerRelationId, false},
279 {"use_scram_passthrough", UserMappingRelationId, false},
280
281 /*
282 * sslcert and sslkey are in fact libpq options, but we repeat them
283 * here to allow them to appear in both foreign server context (when
284 * we generate libpq options) and user mapping context (from here).
285 */
286 {"sslcert", UserMappingRelationId, true},
287 {"sslkey", UserMappingRelationId, true},
288
289 /*
290 * gssdelegation is also a libpq option but should be allowed in a
291 * user mapping context too
292 */
293 {"gssdelegation", UserMappingRelationId, true},
294
295 {NULL, InvalidOid, false}
296 };
297
298 /* Prevent redundant initialization. */
300 return;
301
302 /*
303 * Get list of valid libpq options.
304 *
305 * To avoid unnecessary work, we get the list once and use it throughout
306 * the lifetime of this backend process. Hence, we'll allocate it in
307 * TopMemoryContext.
308 */
309 libpq_options = PQconndefaults();
310 if (!libpq_options) /* assume reason for failure is OOM */
312 (errcode(ERRCODE_FDW_OUT_OF_MEMORY),
313 errmsg("out of memory"),
314 errdetail("Could not get libpq's default connection options.")));
315
316 /* Count how many libpq options are available. */
317 num_libpq_opts = 0;
318 for (lopt = libpq_options; lopt->keyword; lopt++)
319 num_libpq_opts++;
320
321 /*
322 * Construct an array which consists of all valid options for
323 * postgres_fdw, by appending FDW-specific options to libpq options.
324 */
327 sizeof(PgFdwOption) * num_libpq_opts +
328 sizeof(non_libpq_options));
329
331 for (lopt = libpq_options; lopt->keyword; lopt++)
332 {
333 /* Hide debug options, as well as settings we override internally. */
334 if (strchr(lopt->dispchar, 'D') ||
335 strcmp(lopt->keyword, "fallback_application_name") == 0 ||
336 strcmp(lopt->keyword, "client_encoding") == 0)
337 continue;
338
339 /*
340 * Disallow OAuth options for now, since the builtin flow communicates
341 * on stderr by default and can't cache tokens yet.
342 */
343 if (strncmp(lopt->keyword, "oauth_", strlen("oauth_")) == 0)
344 continue;
345
347 lopt->keyword);
348
349 /*
350 * "user" and any secret options are allowed only on user mappings.
351 * Everything else is a server option.
352 */
353 if (strcmp(lopt->keyword, "user") == 0 || strchr(lopt->dispchar, '*'))
354 popt->optcontext = UserMappingRelationId;
355 else
356 popt->optcontext = ForeignServerRelationId;
357 popt->is_libpq_opt = true;
358
359 popt++;
360 }
361
362 /* Done with libpq's output structure. */
363 PQconninfoFree(libpq_options);
364
365 /* Append FDW-specific options and dummy terminator. */
366 memcpy(popt, non_libpq_options, sizeof(non_libpq_options));
367}
static PgFdwOption * postgres_fdw_options
Definition: option.c:41
int errdetail(const char *fmt,...)
Definition: elog.c:1207
void PQconninfoFree(PQconninfoOption *connOptions)
Definition: fe-connect.c:7500
PQconninfoOption * PQconndefaults(void)
Definition: fe-connect.c:2196
char * MemoryContextStrdup(MemoryContext context, const char *string)
Definition: mcxt.c:1746
void * MemoryContextAlloc(MemoryContext context, Size size)
Definition: mcxt.c:1229
MemoryContext TopMemoryContext
Definition: mcxt.c:166
#define InvalidOid
Definition: postgres_ext.h:37
Oid optcontext
Definition: option.c:33
const char * keyword
Definition: option.c:32
bool is_libpq_opt
Definition: option.c:34
char * keyword
Definition: libpq-fe.h:273
char * dispchar
Definition: libpq-fe.h:278

References _PQconninfoOption::dispchar, ereport, errcode(), errdetail(), errmsg(), ERROR, InvalidOid, PgFdwOption::is_libpq_opt, PgFdwOption::keyword, _PQconninfoOption::keyword, MemoryContextAlloc(), MemoryContextStrdup(), PgFdwOption::optcontext, postgres_fdw_options, PQconndefaults(), PQconninfoFree(), and TopMemoryContext.

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}
Assert(PointerIsAligned(start, uint64))

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}

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

67{
68 List *options_list = untransformRelOptions(PG_GETARG_DATUM(0));
69 Oid catalog = PG_GETARG_OID(1);
70 ListCell *cell;
71
72 /* Build our options lists if we didn't yet. */
74
75 /*
76 * Check that only options supported by postgres_fdw, and allowed for the
77 * current object type, are given.
78 */
79 foreach(cell, options_list)
80 {
81 DefElem *def = (DefElem *) lfirst(cell);
82
83 if (!is_valid_option(def->defname, catalog))
84 {
85 /*
86 * Unknown option specified, complain about it. Provide a hint
87 * with a valid option that looks similar, if there is one.
88 */
89 PgFdwOption *opt;
90 const char *closest_match;
92 bool has_valid_options = false;
93
95 for (opt = postgres_fdw_options; opt->keyword; opt++)
96 {
97 if (catalog == opt->optcontext)
98 {
99 has_valid_options = true;
101 }
102 }
103
104 closest_match = getClosestMatch(&match_state);
106 (errcode(ERRCODE_FDW_INVALID_OPTION_NAME),
107 errmsg("invalid option \"%s\"", def->defname),
108 has_valid_options ? closest_match ?
109 errhint("Perhaps you meant the option \"%s\".",
110 closest_match) : 0 :
111 errhint("There are no valid options in this context.")));
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, "parallel_commit") == 0 ||
122 strcmp(def->defname, "parallel_abort") == 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)
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)
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)
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)
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)
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())
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 else if (strcmp(def->defname, "analyze_sampling") == 0)
209 {
210 char *value;
211
212 value = defGetString(def);
213
214 /* we recognize off/auto/random/system/bernoulli */
215 if (strcmp(value, "off") != 0 &&
216 strcmp(value, "auto") != 0 &&
217 strcmp(value, "random") != 0 &&
218 strcmp(value, "system") != 0 &&
219 strcmp(value, "bernoulli") != 0)
221 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
222 errmsg("invalid value for string option \"%s\": %s",
223 def->defname, value)));
224 }
225 }
226
228}
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:94
int errhint(const char *fmt,...)
Definition: elog.c:1321
#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:2876
bool parse_real(const char *value, double *result, int flags, const char **hintmsg)
Definition: guc.c:2966
struct parser_state match_state[5]
static struct @169 value
List * untransformRelOptions(Datum options)
Definition: reloptions.c:1351
bool superuser(void)
Definition: superuser.c:46
const char * getClosestMatch(ClosestMatchState *state)
Definition: varlena.c:5339
void initClosestMatch(ClosestMatchState *state, const char *source, int max_d)
Definition: varlena.c:5284
void updateClosestMatch(ClosestMatchState *state, const char *candidate)
Definition: varlena.c:5304

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 %% */
515 continue;
516 }
517
518 /* process the option */
519 switch (*p)
520 {
521 case 'a':
523 break;
524 case 'c':
525 appendStringInfo(&buf, "%" PRIx64 ".%x", 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':
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:47
struct Port * MyProcPort
Definition: globals.c:51
pg_time_t MyStartTime
Definition: globals.c:48
char * cluster_name
Definition: guc_tables.c:555
char * application_name
Definition: guc_tables.c:561
static char * username
Definition: initdb.c:153
static char * buf
Definition: pg_test_fsync.c:72
char * dbname
Definition: streamutil.c:49
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:145
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:230
void appendStringInfoChar(StringInfo str, char ch)
Definition: stringinfo.c:242
void initStringInfo(StringInfo str)
Definition: stringinfo.c:97
char * user_name
Definition: libpq-be.h:151
char * database_name
Definition: libpq-be.h:150

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

pgfdw_application_name

char* pgfdw_application_name = NULL

Definition at line 46 of file option.c.

Referenced by _PG_init(), and connect_pg_server().

postgres_fdw_options

PgFdwOption* postgres_fdw_options
static

Definition at line 41 of file option.c.

Referenced by InitPgFdwOptions(), is_libpq_option(), is_valid_option(), and postgres_fdw_validator().

AltStyle によって変換されたページ (->オリジナル) /