PostgreSQL Source Code git master
Loading...
Searching...
No Matches
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)
 
 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)
 
charprocess_pgfdw_appname (const char *appname)
 
void _PG_init (void)
 

Variables

static PgFdwOptionpostgres_fdw_options
 
charpgfdw_application_name = NULL
 

Typedef Documentation

◆ PgFdwOption

Function Documentation

◆ _PG_init()

void _PG_init ( void  )

Definition at line 573 of file option.c.

574{
575 /*
576 * Unlike application_name GUC, don't set GUC_IS_NAME flag nor check_hook
577 * to allow postgres_fdw.application_name to be any string more than
578 * NAMEDATALEN characters and to include non-ASCII characters. Instead,
579 * remote server truncates application_name of remote connection to less
580 * than NAMEDATALEN and replaces any non-ASCII characters in it with a '?'
581 * character.
582 */
583 DefineCustomStringVariable("postgres_fdw.application_name",
584 "Sets the application name to be used on the remote server.",
585 NULL,
587 NULL,
589 0,
590 NULL,
591 NULL,
592 NULL);
593
594 MarkGUCPrefixReserved("postgres_fdw");
595}
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:5129
void MarkGUCPrefixReserved(const char *className)
Definition guc.c:5186
@ PGC_USERSET
Definition guc.h:79
static int fb(int x)

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

◆ ExtractConnectionOptions()

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

Definition at line 419 of file option.c.

421{
422 ListCell *lc;
423 int i;
424
425 /* Build our options lists if we didn't yet. */
427
428 i = 0;
429 foreach(lc, defelems)
430 {
431 DefElem *d = (DefElem *) lfirst(lc);
432
433 if (is_libpq_option(d->defname))
434 {
435 keywords[i] = d->defname;
436 values[i] = defGetString(d);
437 i++;
438 }
439 }
440 return i;
441}
static Datum values[MAXATTR]
Definition bootstrap.c:190
static bool is_libpq_option(const char *keyword)
Definition option.c:398
static void InitPgFdwOptions(void)
Definition option.c:236
char * defGetString(DefElem *def)
Definition define.c:34
int i
Definition isn.c:77
static const JsonPathKeyword keywords[]
#define lfirst(lc)
Definition pg_list.h:172
char * defname
Definition parsenodes.h:860

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

Referenced by construct_connection_params().

◆ ExtractExtensionList()

List * ExtractExtensionList ( const char extensionsString,
bool  warnOnMissing 
)

Definition at line 450 of file option.c.

451{
452 List *extensionOids = NIL;
453 List *extlist;
454 ListCell *lc;
455
456 /* SplitIdentifierString scribbles on its input, so pstrdup first */
458 {
459 /* syntax error in name list */
462 errmsg("parameter \"%s\" must be a list of extension names",
463 "extensions")));
464 }
465
466 foreach(lc, extlist)
467 {
468 const char *extension_name = (const char *) lfirst(lc);
470
472 {
473 extensionOids = lappend_oid(extensionOids, extension_oid);
474 }
475 else if (warnOnMissing)
476 {
479 errmsg("extension \"%s\" is not installed",
481 }
482 }
483
485 return extensionOids;
486}
#define OidIsValid(objectId)
Definition c.h:858
int errcode(int sqlerrcode)
Definition elog.c:875
#define WARNING
Definition elog.h:37
#define ERROR
Definition elog.h:40
#define ereport(elevel,...)
Definition elog.h:152
Oid get_extension_oid(const char *extname, bool missing_ok)
Definition extension.c:229
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:1910
static char * errmsg
#define NIL
Definition pg_list.h:68
unsigned int Oid
Definition pg_list.h:54
bool SplitIdentifierString(char *rawstring, char separator, List **namelist)
Definition varlena.c:2870

References ereport, errcode(), errmsg, ERROR, fb(), 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 236 of file option.c.

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

References ereport, errcode(), errdetail(), errmsg, ERROR, fb(), InvalidOid, PgFdwOption::is_libpq_opt, PgFdwOption::keyword, memcpy(), 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 398 of file option.c.

399{
400 PgFdwOption *opt;
401
402 Assert(postgres_fdw_options); /* must be initialized already */
403
404 for (opt = postgres_fdw_options; opt->keyword; opt++)
405 {
406 if (opt->is_libpq_opt && strcmp(opt->keyword, keyword) == 0)
407 return true;
408 }
409
410 return false;
411}
#define Assert(condition)
Definition c.h:943

References Assert, fb(), 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 379 of file option.c.

380{
381 PgFdwOption *opt;
382
383 Assert(postgres_fdw_options); /* must be initialized already */
384
385 for (opt = postgres_fdw_options; opt->keyword; opt++)
386 {
387 if (context == opt->optcontext && strcmp(opt->keyword, keyword) == 0)
388 return true;
389 }
390
391 return false;
392}

References Assert, fb(), 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{
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
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
107 errmsg("invalid option \"%s\"", def->defname),
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 strcmp(def->defname, "restore_stats") == 0 ||
125 strcmp(def->defname, "use_scram_passthrough") == 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)
147 errmsg("invalid value for floating point option \"%s\": %s",
148 def->defname, value)));
149
150 if (real_val < 0)
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 */
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)
174 errmsg("invalid value for integer option \"%s\": %s",
175 def->defname, value)));
176
177 if (int_val <= 0)
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)
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 */
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 else if (strcmp(def->defname, "analyze_sampling") == 0)
211 {
212 char *value;
213
214 value = defGetString(def);
215
216 /* we recognize off/auto/random/system/bernoulli */
217 if (strcmp(value, "off") != 0 &&
218 strcmp(value, "auto") != 0 &&
219 strcmp(value, "random") != 0 &&
220 strcmp(value, "system") != 0 &&
221 strcmp(value, "bernoulli") != 0)
224 errmsg("invalid value for string option \"%s\": %s",
225 def->defname, value)));
226 }
227 }
228
230}
static bool is_valid_option(const char *keyword, Oid context)
Definition option.c:379
List * ExtractExtensionList(const char *extensionsString, bool warnOnMissing)
Definition option.c:450
bool defGetBoolean(DefElem *def)
Definition define.c:93
int errhint(const char *fmt,...) pg_attribute_printf(1
#define PG_RETURN_VOID()
Definition fmgr.h:350
#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:2775
bool parse_real(const char *value, double *result, int flags, const char **hintmsg)
Definition guc.c:2865
struct parser_state match_state[5]
static struct @177 value
List * untransformRelOptions(Datum options)
bool superuser(void)
Definition superuser.c:47
const char * getClosestMatch(ClosestMatchState *state)
Definition varlena.c:5389
void initClosestMatch(ClosestMatchState *state, const char *source, int max_d)
Definition varlena.c:5334
void updateClosestMatch(ClosestMatchState *state, const char *candidate)
Definition varlena.c:5354

References defGetBoolean(), defGetString(), DefElem::defname, ereport, errcode(), errhint(), errmsg, ERROR, ExtractExtensionList(), fb(), 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 496 of file option.c.

497{
498 const char *p;
500
502
503 for (p = appname; *p != '\0'; p++)
504 {
505 if (*p != '%')
506 {
507 /* literal char, just copy */
509 continue;
510 }
511
512 /* must be a '%', so skip to the next char */
513 p++;
514 if (*p == '\0')
515 break; /* format error - ignore it */
516 else if (*p == '%')
517 {
518 /* string contains %% */
520 continue;
521 }
522
523 /* process the option */
524 switch (*p)
525 {
526 case 'a':
528 break;
529 case 'c':
531 break;
532 case 'C':
534 break;
535 case 'd':
536 if (MyProcPort)
537 {
538 const char *dbname = MyProcPort->database_name;
539
540 if (dbname)
542 else
543 appendStringInfoString(&buf, "[unknown]");
544 }
545 break;
546 case 'p':
548 break;
549 case 'u':
550 if (MyProcPort)
551 {
552 const char *username = MyProcPort->user_name;
553
554 if (username)
556 else
557 appendStringInfoString(&buf, "[unknown]");
558 }
559 break;
560 default:
561 /* format error - ignore it */
562 break;
563 }
564 }
565
566 return buf.data;
567}
int MyProcPid
Definition globals.c:49
struct Port * MyProcPort
Definition globals.c:53
pg_time_t MyStartTime
Definition globals.c:50
char * cluster_name
Definition guc_tables.c:582
char * application_name
Definition guc_tables.c:589
static char * username
Definition initdb.c:153
static char buf[DEFAULT_XLOG_SEG_SIZE]
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, fb(), initStringInfo(), MyProcPid, MyProcPort, MyStartTime, Port::user_name, and username.

Referenced by construct_connection_params().

Variable Documentation

◆ pgfdw_application_name

char* pgfdw_application_name = NULL

Definition at line 46 of file option.c.

Referenced by _PG_init(), and construct_connection_params().

◆ postgres_fdw_options

PgFdwOption* postgres_fdw_options
static