| 1 |
/*------------------------------------------------------------------------- |
| 2 |
* |
| 3 |
* foreigncmds.c |
| 4 |
* foreign-data wrapper/server creation/manipulation commands |
| 5 |
* |
| 6 |
* Portions Copyright (c) 1996-2025, PostgreSQL Global Development Group |
| 7 |
* |
| 8 |
* |
| 9 |
* IDENTIFICATION |
| 10 |
* src/backend/commands/foreigncmds.c |
| 11 |
* |
| 12 |
*------------------------------------------------------------------------- |
| 13 |
*/ |
| 14 |
#include "postgres.h" |
| 15 |
|
| 16 |
#include "access/htup_details.h" |
| 17 |
#include "access/reloptions.h" |
| 18 |
#include "access/table.h" |
| 19 |
#include "access/xact.h" |
| 20 |
#include "catalog/catalog.h" |
| 21 |
#include "catalog/dependency.h" |
| 22 |
#include "catalog/indexing.h" |
| 23 |
#include "catalog/objectaccess.h" |
| 24 |
#include "catalog/pg_foreign_data_wrapper.h" |
| 25 |
#include "catalog/pg_foreign_server.h" |
| 26 |
#include "catalog/pg_foreign_table.h" |
| 27 |
#include "catalog/pg_proc.h" |
| 28 |
#include "catalog/pg_type.h" |
| 29 |
#include "catalog/pg_user_mapping.h" |
| 30 |
#include "commands/defrem.h" |
| 31 |
#include "foreign/fdwapi.h" |
| 32 |
#include "foreign/foreign.h" |
| 33 |
#include "miscadmin.h" |
| 34 |
#include "parser/parse_func.h" |
| 35 |
#include "tcop/utility.h" |
| 36 |
#include "utils/acl.h" |
| 37 |
#include "utils/builtins.h" |
| 38 |
#include "utils/lsyscache.h" |
| 39 |
#include "utils/rel.h" |
| 40 |
#include "utils/syscache.h" |
| 41 |
|
| 42 |
|
| 43 |
typedef struct |
| 44 |
{ |
| 45 |
char *tablename; |
| 46 |
char *cmd; |
| 47 |
} import_error_callback_arg; |
| 48 |
|
| 49 |
/* Internal functions */ |
| 50 |
static void import_error_callback(void *arg); |
| 51 |
|
| 52 |
|
| 53 |
/* |
| 54 |
* Convert a DefElem list to the text array format that is used in |
| 55 |
* pg_foreign_data_wrapper, pg_foreign_server, pg_user_mapping, and |
| 56 |
* pg_foreign_table. |
| 57 |
* |
| 58 |
* Returns the array in the form of a Datum, or PointerGetDatum(NULL) |
| 59 |
* if the list is empty. |
| 60 |
* |
| 61 |
* Note: The array is usually stored to database without further |
| 62 |
* processing, hence any validation should be done before this |
| 63 |
* conversion. |
| 64 |
*/ |
| 65 |
static Datum |
| 66 |
optionListToArray(List *options) |
| 67 |
{ |
| 68 |
ArrayBuildState *astate = NULL; |
| 69 |
ListCell *cell; |
| 70 |
|
| 71 |
foreach(cell, options) |
| 72 |
{ |
| 73 |
DefElem *def = lfirst(cell); |
| 74 |
const char *name; |
| 75 |
const char *value; |
| 76 |
Size len; |
| 77 |
text *t; |
| 78 |
|
| 79 |
name = def->defname; |
| 80 |
value = defGetString(def); |
| 81 |
|
| 82 |
/* Insist that name not contain "=", else "a=b=c" is ambiguous */ |
| 83 |
if (strchr(name, '=') != NULL) |
| 84 |
ereport(ERROR, |
| 85 |
(errcode(ERRCODE_INVALID_PARAMETER_VALUE), |
| 86 |
errmsg("invalid option name \"%s\": must not contain \"=\"", |
| 87 |
name))); |
| 88 |
|
| 89 |
len = VARHDRSZ + strlen(name) + 1 + strlen(value); |
| 90 |
/* +1 leaves room for sprintf's trailing null */ |
| 91 |
t = palloc(len + 1); |
| 92 |
SET_VARSIZE(t, len); |
| 93 |
sprintf(VARDATA(t), "%s=%s", name, value); |
| 94 |
|
| 95 |
astate = accumArrayResult(astate, PointerGetDatum(t), |
| 96 |
false, TEXTOID, |
| 97 |
CurrentMemoryContext); |
| 98 |
} |
| 99 |
|
| 100 |
if (astate) |
| 101 |
return makeArrayResult(astate, CurrentMemoryContext); |
| 102 |
|
| 103 |
return PointerGetDatum(NULL); |
| 104 |
} |
| 105 |
|
| 106 |
|
| 107 |
/* |
| 108 |
* Transform a list of DefElem into text array format. This is substantially |
| 109 |
* the same thing as optionListToArray(), except we recognize SET/ADD/DROP |
| 110 |
* actions for modifying an existing list of options, which is passed in |
| 111 |
* Datum form as oldOptions. Also, if fdwvalidator isn't InvalidOid |
| 112 |
* it specifies a validator function to call on the result. |
| 113 |
* |
| 114 |
* Returns the array in the form of a Datum, or PointerGetDatum(NULL) |
| 115 |
* if the list is empty. |
| 116 |
* |
| 117 |
* This is used by CREATE/ALTER of FOREIGN DATA WRAPPER/SERVER/USER MAPPING/ |
| 118 |
* FOREIGN TABLE. |
| 119 |
*/ |
| 120 |
Datum |
| 121 |
transformGenericOptions(Oid catalogId, |
| 122 |
Datum oldOptions, |
| 123 |
List *options, |
| 124 |
Oid fdwvalidator) |
| 125 |
{ |
| 126 |
List *resultOptions = untransformRelOptions(oldOptions); |
| 127 |
ListCell *optcell; |
| 128 |
Datum result; |
| 129 |
|
| 130 |
foreach(optcell, options) |
| 131 |
{ |
| 132 |
DefElem *od = lfirst(optcell); |
| 133 |
ListCell *cell; |
| 134 |
|
| 135 |
/* |
| 136 |
* Find the element in resultOptions. We need this for validation in |
| 137 |
* all cases. |
| 138 |
*/ |
| 139 |
foreach(cell, resultOptions) |
| 140 |
{ |
| 141 |
DefElem *def = lfirst(cell); |
| 142 |
|
| 143 |
if (strcmp(def->defname, od->defname) == 0) |
| 144 |
break; |
| 145 |
} |
| 146 |
|
| 147 |
/* |
| 148 |
* It is possible to perform multiple SET/DROP actions on the same |
| 149 |
* option. The standard permits this, as long as the options to be |
| 150 |
* added are unique. Note that an unspecified action is taken to be |
| 151 |
* ADD. |
| 152 |
*/ |
| 153 |
switch (od->defaction) |
| 154 |
{ |
| 155 |
case DEFELEM_DROP: |
| 156 |
if (!cell) |
| 157 |
ereport(ERROR, |
| 158 |
(errcode(ERRCODE_UNDEFINED_OBJECT), |
| 159 |
errmsg("option \"%s\" not found", |
| 160 |
od->defname))); |
| 161 |
resultOptions = list_delete_cell(resultOptions, cell); |
| 162 |
break; |
| 163 |
|
| 164 |
case DEFELEM_SET: |
| 165 |
if (!cell) |
| 166 |
ereport(ERROR, |
| 167 |
(errcode(ERRCODE_UNDEFINED_OBJECT), |
| 168 |
errmsg("option \"%s\" not found", |
| 169 |
od->defname))); |
| 170 |
lfirst(cell) = od; |
| 171 |
break; |
| 172 |
|
| 173 |
case DEFELEM_ADD: |
| 174 |
case DEFELEM_UNSPEC: |
| 175 |
if (cell) |
| 176 |
ereport(ERROR, |
| 177 |
(errcode(ERRCODE_DUPLICATE_OBJECT), |
| 178 |
errmsg("option \"%s\" provided more than once", |
| 179 |
od->defname))); |
| 180 |
resultOptions = lappend(resultOptions, od); |
| 181 |
break; |
| 182 |
|
| 183 |
default: |
| 184 |
elog(ERROR, "unrecognized action %d on option \"%s\"", |
| 185 |
(int) od->defaction, od->defname); |
| 186 |
break; |
| 187 |
} |
| 188 |
} |
| 189 |
|
| 190 |
result = optionListToArray(resultOptions); |
| 191 |
|
| 192 |
if (OidIsValid(fdwvalidator)) |
| 193 |
{ |
| 194 |
Datum valarg = result; |
| 195 |
|
| 196 |
/* |
| 197 |
* Pass a null options list as an empty array, so that validators |
| 198 |
* don't have to be declared non-strict to handle the case. |
| 199 |
*/ |
| 200 |
if (DatumGetPointer(valarg) == NULL) |
| 201 |
valarg = PointerGetDatum(construct_empty_array(TEXTOID)); |
| 202 |
OidFunctionCall2(fdwvalidator, valarg, ObjectIdGetDatum(catalogId)); |
| 203 |
} |
| 204 |
|
| 205 |
return result; |
| 206 |
} |
| 207 |
|
| 208 |
|
| 209 |
/* |
| 210 |
* Internal workhorse for changing a data wrapper's owner. |
| 211 |
* |
| 212 |
* Allow this only for superusers; also the new owner must be a |
| 213 |
* superuser. |
| 214 |
*/ |
| 215 |
static void |
| 216 |
AlterForeignDataWrapperOwner_internal(Relation rel, HeapTuple tup, Oid newOwnerId) |
| 217 |
{ |
| 218 |
Form_pg_foreign_data_wrapper form; |
| 219 |
Datum repl_val[Natts_pg_foreign_data_wrapper]; |
| 220 |
bool repl_null[Natts_pg_foreign_data_wrapper]; |
| 221 |
bool repl_repl[Natts_pg_foreign_data_wrapper]; |
| 222 |
Acl *newAcl; |
| 223 |
Datum aclDatum; |
| 224 |
bool isNull; |
| 225 |
|
| 226 |
form = (Form_pg_foreign_data_wrapper) GETSTRUCT(tup); |
| 227 |
|
| 228 |
/* Must be a superuser to change a FDW owner */ |
| 229 |
if (!superuser()) |
| 230 |
ereport(ERROR, |
| 231 |
(errcode(ERRCODE_INSUFFICIENT_PRIVILEGE), |
| 232 |
errmsg("permission denied to change owner of foreign-data wrapper \"%s\"", |
| 233 |
NameStr(form->fdwname)), |
| 234 |
errhint("Must be superuser to change owner of a foreign-data wrapper."))); |
| 235 |
|
| 236 |
/* New owner must also be a superuser */ |
| 237 |
if (!superuser_arg(newOwnerId)) |
| 238 |
ereport(ERROR, |
| 239 |
(errcode(ERRCODE_INSUFFICIENT_PRIVILEGE), |
| 240 |
errmsg("permission denied to change owner of foreign-data wrapper \"%s\"", |
| 241 |
NameStr(form->fdwname)), |
| 242 |
errhint("The owner of a foreign-data wrapper must be a superuser."))); |
| 243 |
|
| 244 |
if (form->fdwowner != newOwnerId) |
| 245 |
{ |
| 246 |
memset(repl_null, false, sizeof(repl_null)); |
| 247 |
memset(repl_repl, false, sizeof(repl_repl)); |
| 248 |
|
| 249 |
repl_repl[Anum_pg_foreign_data_wrapper_fdwowner - 1] = true; |
| 250 |
repl_val[Anum_pg_foreign_data_wrapper_fdwowner - 1] = ObjectIdGetDatum(newOwnerId); |
| 251 |
|
| 252 |
aclDatum = heap_getattr(tup, |
| 253 |
Anum_pg_foreign_data_wrapper_fdwacl, |
| 254 |
RelationGetDescr(rel), |
| 255 |
&isNull); |
| 256 |
/* Null ACLs do not require changes */ |
| 257 |
if (!isNull) |
| 258 |
{ |
| 259 |
newAcl = aclnewowner(DatumGetAclP(aclDatum), |
| 260 |
form->fdwowner, newOwnerId); |
| 261 |
repl_repl[Anum_pg_foreign_data_wrapper_fdwacl - 1] = true; |
| 262 |
repl_val[Anum_pg_foreign_data_wrapper_fdwacl - 1] = PointerGetDatum(newAcl); |
| 263 |
} |
| 264 |
|
| 265 |
tup = heap_modify_tuple(tup, RelationGetDescr(rel), repl_val, repl_null, |
| 266 |
repl_repl); |
| 267 |
|
| 268 |
CatalogTupleUpdate(rel, &tup->t_self, tup); |
| 269 |
|
| 270 |
/* Update owner dependency reference */ |
| 271 |
changeDependencyOnOwner(ForeignDataWrapperRelationId, |
| 272 |
form->oid, |
| 273 |
newOwnerId); |
| 274 |
} |
| 275 |
|
| 276 |
InvokeObjectPostAlterHook(ForeignDataWrapperRelationId, |
| 277 |
form->oid, 0); |
| 278 |
} |
| 279 |
|
| 280 |
/* |
| 281 |
* Change foreign-data wrapper owner -- by name |
| 282 |
* |
| 283 |
* Note restrictions in the "_internal" function, above. |
| 284 |
*/ |
| 285 |
ObjectAddress |
| 286 |
AlterForeignDataWrapperOwner(const char *name, Oid newOwnerId) |
| 287 |
{ |
| 288 |
Oid fdwId; |
| 289 |
HeapTuple tup; |
| 290 |
Relation rel; |
| 291 |
ObjectAddress address; |
| 292 |
Form_pg_foreign_data_wrapper form; |
| 293 |
|
| 294 |
|
| 295 |
rel = table_open(ForeignDataWrapperRelationId, RowExclusiveLock); |
| 296 |
|
| 297 |
tup = SearchSysCacheCopy1(FOREIGNDATAWRAPPERNAME, CStringGetDatum(name)); |
| 298 |
|
| 299 |
if (!HeapTupleIsValid(tup)) |
| 300 |
ereport(ERROR, |
| 301 |
(errcode(ERRCODE_UNDEFINED_OBJECT), |
| 302 |
errmsg("foreign-data wrapper \"%s\" does not exist", name))); |
| 303 |
|
| 304 |
form = (Form_pg_foreign_data_wrapper) GETSTRUCT(tup); |
| 305 |
fdwId = form->oid; |
| 306 |
|
| 307 |
AlterForeignDataWrapperOwner_internal(rel, tup, newOwnerId); |
| 308 |
|
| 309 |
ObjectAddressSet(address, ForeignDataWrapperRelationId, fdwId); |
| 310 |
|
| 311 |
heap_freetuple(tup); |
| 312 |
|
| 313 |
table_close(rel, RowExclusiveLock); |
| 314 |
|
| 315 |
return address; |
| 316 |
} |
| 317 |
|
| 318 |
/* |
| 319 |
* Change foreign-data wrapper owner -- by OID |
| 320 |
* |
| 321 |
* Note restrictions in the "_internal" function, above. |
| 322 |
*/ |
| 323 |
void |
| 324 |
AlterForeignDataWrapperOwner_oid(Oid fwdId, Oid newOwnerId) |
| 325 |
{ |
| 326 |
HeapTuple tup; |
| 327 |
Relation rel; |
| 328 |
|
| 329 |
rel = table_open(ForeignDataWrapperRelationId, RowExclusiveLock); |
| 330 |
|
| 331 |
tup = SearchSysCacheCopy1(FOREIGNDATAWRAPPEROID, ObjectIdGetDatum(fwdId)); |
| 332 |
|
| 333 |
if (!HeapTupleIsValid(tup)) |
| 334 |
ereport(ERROR, |
| 335 |
(errcode(ERRCODE_UNDEFINED_OBJECT), |
| 336 |
errmsg("foreign-data wrapper with OID %u does not exist", fwdId))); |
| 337 |
|
| 338 |
AlterForeignDataWrapperOwner_internal(rel, tup, newOwnerId); |
| 339 |
|
| 340 |
heap_freetuple(tup); |
| 341 |
|
| 342 |
table_close(rel, RowExclusiveLock); |
| 343 |
} |
| 344 |
|
| 345 |
/* |
| 346 |
* Internal workhorse for changing a foreign server's owner |
| 347 |
*/ |
| 348 |
static void |
| 349 |
AlterForeignServerOwner_internal(Relation rel, HeapTuple tup, Oid newOwnerId) |
| 350 |
{ |
| 351 |
Form_pg_foreign_server form; |
| 352 |
Datum repl_val[Natts_pg_foreign_server]; |
| 353 |
bool repl_null[Natts_pg_foreign_server]; |
| 354 |
bool repl_repl[Natts_pg_foreign_server]; |
| 355 |
Acl *newAcl; |
| 356 |
Datum aclDatum; |
| 357 |
bool isNull; |
| 358 |
|
| 359 |
form = (Form_pg_foreign_server) GETSTRUCT(tup); |
| 360 |
|
| 361 |
if (form->srvowner != newOwnerId) |
| 362 |
{ |
| 363 |
/* Superusers can always do it */ |
| 364 |
if (!superuser()) |
| 365 |
{ |
| 366 |
Oid srvId; |
| 367 |
AclResult aclresult; |
| 368 |
|
| 369 |
srvId = form->oid; |
| 370 |
|
| 371 |
/* Must be owner */ |
| 372 |
if (!object_ownercheck(ForeignServerRelationId, srvId, GetUserId())) |
| 373 |
aclcheck_error(ACLCHECK_NOT_OWNER, OBJECT_FOREIGN_SERVER, |
| 374 |
NameStr(form->srvname)); |
| 375 |
|
| 376 |
/* Must be able to become new owner */ |
| 377 |
check_can_set_role(GetUserId(), newOwnerId); |
| 378 |
|
| 379 |
/* New owner must have USAGE privilege on foreign-data wrapper */ |
| 380 |
aclresult = object_aclcheck(ForeignDataWrapperRelationId, form->srvfdw, newOwnerId, ACL_USAGE); |
| 381 |
if (aclresult != ACLCHECK_OK) |
| 382 |
{ |
| 383 |
ForeignDataWrapper *fdw = GetForeignDataWrapper(form->srvfdw); |
| 384 |
|
| 385 |
aclcheck_error(aclresult, OBJECT_FDW, fdw->fdwname); |
| 386 |
} |
| 387 |
} |
| 388 |
|
| 389 |
memset(repl_null, false, sizeof(repl_null)); |
| 390 |
memset(repl_repl, false, sizeof(repl_repl)); |
| 391 |
|
| 392 |
repl_repl[Anum_pg_foreign_server_srvowner - 1] = true; |
| 393 |
repl_val[Anum_pg_foreign_server_srvowner - 1] = ObjectIdGetDatum(newOwnerId); |
| 394 |
|
| 395 |
aclDatum = heap_getattr(tup, |
| 396 |
Anum_pg_foreign_server_srvacl, |
| 397 |
RelationGetDescr(rel), |
| 398 |
&isNull); |
| 399 |
/* Null ACLs do not require changes */ |
| 400 |
if (!isNull) |
| 401 |
{ |
| 402 |
newAcl = aclnewowner(DatumGetAclP(aclDatum), |
| 403 |
form->srvowner, newOwnerId); |
| 404 |
repl_repl[Anum_pg_foreign_server_srvacl - 1] = true; |
| 405 |
repl_val[Anum_pg_foreign_server_srvacl - 1] = PointerGetDatum(newAcl); |
| 406 |
} |
| 407 |
|
| 408 |
tup = heap_modify_tuple(tup, RelationGetDescr(rel), repl_val, repl_null, |
| 409 |
repl_repl); |
| 410 |
|
| 411 |
CatalogTupleUpdate(rel, &tup->t_self, tup); |
| 412 |
|
| 413 |
/* Update owner dependency reference */ |
| 414 |
changeDependencyOnOwner(ForeignServerRelationId, form->oid, |
| 415 |
newOwnerId); |
| 416 |
} |
| 417 |
|
| 418 |
InvokeObjectPostAlterHook(ForeignServerRelationId, |
| 419 |
form->oid, 0); |
| 420 |
} |
| 421 |
|
| 422 |
/* |
| 423 |
* Change foreign server owner -- by name |
| 424 |
*/ |
| 425 |
ObjectAddress |
| 426 |
AlterForeignServerOwner(const char *name, Oid newOwnerId) |
| 427 |
{ |
| 428 |
Oid servOid; |
| 429 |
HeapTuple tup; |
| 430 |
Relation rel; |
| 431 |
ObjectAddress address; |
| 432 |
Form_pg_foreign_server form; |
| 433 |
|
| 434 |
rel = table_open(ForeignServerRelationId, RowExclusiveLock); |
| 435 |
|
| 436 |
tup = SearchSysCacheCopy1(FOREIGNSERVERNAME, CStringGetDatum(name)); |
| 437 |
|
| 438 |
if (!HeapTupleIsValid(tup)) |
| 439 |
ereport(ERROR, |
| 440 |
(errcode(ERRCODE_UNDEFINED_OBJECT), |
| 441 |
errmsg("server \"%s\" does not exist", name))); |
| 442 |
|
| 443 |
form = (Form_pg_foreign_server) GETSTRUCT(tup); |
| 444 |
servOid = form->oid; |
| 445 |
|
| 446 |
AlterForeignServerOwner_internal(rel, tup, newOwnerId); |
| 447 |
|
| 448 |
ObjectAddressSet(address, ForeignServerRelationId, servOid); |
| 449 |
|
| 450 |
heap_freetuple(tup); |
| 451 |
|
| 452 |
table_close(rel, RowExclusiveLock); |
| 453 |
|
| 454 |
return address; |
| 455 |
} |
| 456 |
|
| 457 |
/* |
| 458 |
* Change foreign server owner -- by OID |
| 459 |
*/ |
| 460 |
void |
| 461 |
AlterForeignServerOwner_oid(Oid srvId, Oid newOwnerId) |
| 462 |
{ |
| 463 |
HeapTuple tup; |
| 464 |
Relation rel; |
| 465 |
|
| 466 |
rel = table_open(ForeignServerRelationId, RowExclusiveLock); |
| 467 |
|
| 468 |
tup = SearchSysCacheCopy1(FOREIGNSERVEROID, ObjectIdGetDatum(srvId)); |
| 469 |
|
| 470 |
if (!HeapTupleIsValid(tup)) |
| 471 |
ereport(ERROR, |
| 472 |
(errcode(ERRCODE_UNDEFINED_OBJECT), |
| 473 |
errmsg("foreign server with OID %u does not exist", srvId))); |
| 474 |
|
| 475 |
AlterForeignServerOwner_internal(rel, tup, newOwnerId); |
| 476 |
|
| 477 |
heap_freetuple(tup); |
| 478 |
|
| 479 |
table_close(rel, RowExclusiveLock); |
| 480 |
} |
| 481 |
|
| 482 |
/* |
| 483 |
* Convert a handler function name passed from the parser to an Oid. |
| 484 |
*/ |
| 485 |
static Oid |
| 486 |
lookup_fdw_handler_func(DefElem *handler) |
| 487 |
{ |
| 488 |
Oid handlerOid; |
| 489 |
|
| 490 |
if (handler == NULL || handler->arg == NULL) |
| 491 |
return InvalidOid; |
| 492 |
|
| 493 |
/* handlers have no arguments */ |
| 494 |
handlerOid = LookupFuncName((List *) handler->arg, 0, NULL, false); |
| 495 |
|
| 496 |
/* check that handler has correct return type */ |
| 497 |
if (get_func_rettype(handlerOid) != FDW_HANDLEROID) |
| 498 |
ereport(ERROR, |
| 499 |
(errcode(ERRCODE_WRONG_OBJECT_TYPE), |
| 500 |
errmsg("function %s must return type %s", |
| 501 |
NameListToString((List *) handler->arg), "fdw_handler"))); |
| 502 |
|
| 503 |
return handlerOid; |
| 504 |
} |
| 505 |
|
| 506 |
/* |
| 507 |
* Convert a validator function name passed from the parser to an Oid. |
| 508 |
*/ |
| 509 |
static Oid |
| 510 |
lookup_fdw_validator_func(DefElem *validator) |
| 511 |
{ |
| 512 |
Oid funcargtypes[2]; |
| 513 |
|
| 514 |
if (validator == NULL || validator->arg == NULL) |
| 515 |
return InvalidOid; |
| 516 |
|
| 517 |
/* validators take text[], oid */ |
| 518 |
funcargtypes[0] = TEXTARRAYOID; |
| 519 |
funcargtypes[1] = OIDOID; |
| 520 |
|
| 521 |
return LookupFuncName((List *) validator->arg, 2, funcargtypes, false); |
| 522 |
/* validator's return value is ignored, so we don't check the type */ |
| 523 |
} |
| 524 |
|
| 525 |
/* |
| 526 |
* Process function options of CREATE/ALTER FDW |
| 527 |
*/ |
| 528 |
static void |
| 529 |
parse_func_options(ParseState *pstate, List *func_options, |
| 530 |
bool *handler_given, Oid *fdwhandler, |
| 531 |
bool *validator_given, Oid *fdwvalidator) |
| 532 |
{ |
| 533 |
ListCell *cell; |
| 534 |
|
| 535 |
*handler_given = false; |
| 536 |
*validator_given = false; |
| 537 |
/* return InvalidOid if not given */ |
| 538 |
*fdwhandler = InvalidOid; |
| 539 |
*fdwvalidator = InvalidOid; |
| 540 |
|
| 541 |
foreach(cell, func_options) |
| 542 |
{ |
| 543 |
DefElem *def = (DefElem *) lfirst(cell); |
| 544 |
|
| 545 |
if (strcmp(def->defname, "handler") == 0) |
| 546 |
{ |
| 547 |
if (*handler_given) |
| 548 |
errorConflictingDefElem(def, pstate); |
| 549 |
*handler_given = true; |
| 550 |
*fdwhandler = lookup_fdw_handler_func(def); |
| 551 |
} |
| 552 |
else if (strcmp(def->defname, "validator") == 0) |
| 553 |
{ |
| 554 |
if (*validator_given) |
| 555 |
errorConflictingDefElem(def, pstate); |
| 556 |
*validator_given = true; |
| 557 |
*fdwvalidator = lookup_fdw_validator_func(def); |
| 558 |
} |
| 559 |
else |
| 560 |
elog(ERROR, "option \"%s\" not recognized", |
| 561 |
def->defname); |
| 562 |
} |
| 563 |
} |
| 564 |
|
| 565 |
/* |
| 566 |
* Create a foreign-data wrapper |
| 567 |
*/ |
| 568 |
ObjectAddress |
| 569 |
CreateForeignDataWrapper(ParseState *pstate, CreateFdwStmt *stmt) |
| 570 |
{ |
| 571 |
Relation rel; |
| 572 |
Datum values[Natts_pg_foreign_data_wrapper]; |
| 573 |
bool nulls[Natts_pg_foreign_data_wrapper]; |
| 574 |
HeapTuple tuple; |
| 575 |
Oid fdwId; |
| 576 |
bool handler_given; |
| 577 |
bool validator_given; |
| 578 |
Oid fdwhandler; |
| 579 |
Oid fdwvalidator; |
| 580 |
Datum fdwoptions; |
| 581 |
Oid ownerId; |
| 582 |
ObjectAddress myself; |
| 583 |
ObjectAddress referenced; |
| 584 |
|
| 585 |
rel = table_open(ForeignDataWrapperRelationId, RowExclusiveLock); |
| 586 |
|
| 587 |
/* Must be superuser */ |
| 588 |
if (!superuser()) |
| 589 |
ereport(ERROR, |
| 590 |
(errcode(ERRCODE_INSUFFICIENT_PRIVILEGE), |
| 591 |
errmsg("permission denied to create foreign-data wrapper \"%s\"", |
| 592 |
stmt->fdwname), |
| 593 |
errhint("Must be superuser to create a foreign-data wrapper."))); |
| 594 |
|
| 595 |
/* For now the owner cannot be specified on create. Use effective user ID. */ |
| 596 |
ownerId = GetUserId(); |
| 597 |
|
| 598 |
/* |
| 599 |
* Check that there is no other foreign-data wrapper by this name. |
| 600 |
*/ |
| 601 |
if (GetForeignDataWrapperByName(stmt->fdwname, true) != NULL) |
| 602 |
ereport(ERROR, |
| 603 |
(errcode(ERRCODE_DUPLICATE_OBJECT), |
| 604 |
errmsg("foreign-data wrapper \"%s\" already exists", |
| 605 |
stmt->fdwname))); |
| 606 |
|
| 607 |
/* |
| 608 |
* Insert tuple into pg_foreign_data_wrapper. |
| 609 |
*/ |
| 610 |
memset(values, 0, sizeof(values)); |
| 611 |
memset(nulls, false, sizeof(nulls)); |
| 612 |
|
| 613 |
fdwId = GetNewOidWithIndex(rel, ForeignDataWrapperOidIndexId, |
| 614 |
Anum_pg_foreign_data_wrapper_oid); |
| 615 |
values[Anum_pg_foreign_data_wrapper_oid - 1] = ObjectIdGetDatum(fdwId); |
| 616 |
values[Anum_pg_foreign_data_wrapper_fdwname - 1] = |
| 617 |
DirectFunctionCall1(namein, CStringGetDatum(stmt->fdwname)); |
| 618 |
values[Anum_pg_foreign_data_wrapper_fdwowner - 1] = ObjectIdGetDatum(ownerId); |
| 619 |
|
| 620 |
/* Lookup handler and validator functions, if given */ |
| 621 |
parse_func_options(pstate, stmt->func_options, |
| 622 |
&handler_given, &fdwhandler, |
| 623 |
&validator_given, &fdwvalidator); |
| 624 |
|
| 625 |
values[Anum_pg_foreign_data_wrapper_fdwhandler - 1] = ObjectIdGetDatum(fdwhandler); |
| 626 |
values[Anum_pg_foreign_data_wrapper_fdwvalidator - 1] = ObjectIdGetDatum(fdwvalidator); |
| 627 |
|
| 628 |
nulls[Anum_pg_foreign_data_wrapper_fdwacl - 1] = true; |
| 629 |
|
| 630 |
fdwoptions = transformGenericOptions(ForeignDataWrapperRelationId, |
| 631 |
PointerGetDatum(NULL), |
| 632 |
stmt->options, |
| 633 |
fdwvalidator); |
| 634 |
|
| 635 |
if (PointerIsValid(DatumGetPointer(fdwoptions))) |
| 636 |
values[Anum_pg_foreign_data_wrapper_fdwoptions - 1] = fdwoptions; |
| 637 |
else |
| 638 |
nulls[Anum_pg_foreign_data_wrapper_fdwoptions - 1] = true; |
| 639 |
|
| 640 |
tuple = heap_form_tuple(rel->rd_att, values, nulls); |
| 641 |
|
| 642 |
CatalogTupleInsert(rel, tuple); |
| 643 |
|
| 644 |
heap_freetuple(tuple); |
| 645 |
|
| 646 |
/* record dependencies */ |
| 647 |
myself.classId = ForeignDataWrapperRelationId; |
| 648 |
myself.objectId = fdwId; |
| 649 |
myself.objectSubId = 0; |
| 650 |
|
| 651 |
if (OidIsValid(fdwhandler)) |
| 652 |
{ |
| 653 |
referenced.classId = ProcedureRelationId; |
| 654 |
referenced.objectId = fdwhandler; |
| 655 |
referenced.objectSubId = 0; |
| 656 |
recordDependencyOn(&myself, &referenced, DEPENDENCY_NORMAL); |
| 657 |
} |
| 658 |
|
| 659 |
if (OidIsValid(fdwvalidator)) |
| 660 |
{ |
| 661 |
referenced.classId = ProcedureRelationId; |
| 662 |
referenced.objectId = fdwvalidator; |
| 663 |
referenced.objectSubId = 0; |
| 664 |
recordDependencyOn(&myself, &referenced, DEPENDENCY_NORMAL); |
| 665 |
} |
| 666 |
|
| 667 |
recordDependencyOnOwner(ForeignDataWrapperRelationId, fdwId, ownerId); |
| 668 |
|
| 669 |
/* dependency on extension */ |
| 670 |
recordDependencyOnCurrentExtension(&myself, false); |
| 671 |
|
| 672 |
/* Post creation hook for new foreign data wrapper */ |
| 673 |
InvokeObjectPostCreateHook(ForeignDataWrapperRelationId, fdwId, 0); |
| 674 |
|
| 675 |
table_close(rel, RowExclusiveLock); |
| 676 |
|
| 677 |
return myself; |
| 678 |
} |
| 679 |
|
| 680 |
|
| 681 |
/* |
| 682 |
* Alter foreign-data wrapper |
| 683 |
*/ |
| 684 |
ObjectAddress |
| 685 |
AlterForeignDataWrapper(ParseState *pstate, AlterFdwStmt *stmt) |
| 686 |
{ |
| 687 |
Relation rel; |
| 688 |
HeapTuple tp; |
| 689 |
Form_pg_foreign_data_wrapper fdwForm; |
| 690 |
Datum repl_val[Natts_pg_foreign_data_wrapper]; |
| 691 |
bool repl_null[Natts_pg_foreign_data_wrapper]; |
| 692 |
bool repl_repl[Natts_pg_foreign_data_wrapper]; |
| 693 |
Oid fdwId; |
| 694 |
bool isnull; |
| 695 |
Datum datum; |
| 696 |
bool handler_given; |
| 697 |
bool validator_given; |
| 698 |
Oid fdwhandler; |
| 699 |
Oid fdwvalidator; |
| 700 |
ObjectAddress myself; |
| 701 |
|
| 702 |
rel = table_open(ForeignDataWrapperRelationId, RowExclusiveLock); |
| 703 |
|
| 704 |
/* Must be superuser */ |
| 705 |
if (!superuser()) |
| 706 |
ereport(ERROR, |
| 707 |
(errcode(ERRCODE_INSUFFICIENT_PRIVILEGE), |
| 708 |
errmsg("permission denied to alter foreign-data wrapper \"%s\"", |
| 709 |
stmt->fdwname), |
| 710 |
errhint("Must be superuser to alter a foreign-data wrapper."))); |
| 711 |
|
| 712 |
tp = SearchSysCacheCopy1(FOREIGNDATAWRAPPERNAME, |
| 713 |
CStringGetDatum(stmt->fdwname)); |
| 714 |
|
| 715 |
if (!HeapTupleIsValid(tp)) |
| 716 |
ereport(ERROR, |
| 717 |
(errcode(ERRCODE_UNDEFINED_OBJECT), |
| 718 |
errmsg("foreign-data wrapper \"%s\" does not exist", stmt->fdwname))); |
| 719 |
|
| 720 |
fdwForm = (Form_pg_foreign_data_wrapper) GETSTRUCT(tp); |
| 721 |
fdwId = fdwForm->oid; |
| 722 |
|
| 723 |
memset(repl_val, 0, sizeof(repl_val)); |
| 724 |
memset(repl_null, false, sizeof(repl_null)); |
| 725 |
memset(repl_repl, false, sizeof(repl_repl)); |
| 726 |
|
| 727 |
parse_func_options(pstate, stmt->func_options, |
| 728 |
&handler_given, &fdwhandler, |
| 729 |
&validator_given, &fdwvalidator); |
| 730 |
|
| 731 |
if (handler_given) |
| 732 |
{ |
| 733 |
repl_val[Anum_pg_foreign_data_wrapper_fdwhandler - 1] = ObjectIdGetDatum(fdwhandler); |
| 734 |
repl_repl[Anum_pg_foreign_data_wrapper_fdwhandler - 1] = true; |
| 735 |
|
| 736 |
/* |
| 737 |
* It could be that the behavior of accessing foreign table changes |
| 738 |
* with the new handler. Warn about this. |
| 739 |
*/ |
| 740 |
ereport(WARNING, |
| 741 |
(errmsg("changing the foreign-data wrapper handler can change behavior of existing foreign tables"))); |
| 742 |
} |
| 743 |
else |
| 744 |
{ |
| 745 |
/* handler unchanged */ |
| 746 |
fdwhandler = fdwForm->fdwhandler; |
| 747 |
} |
| 748 |
|
| 749 |
if (validator_given) |
| 750 |
{ |
| 751 |
repl_val[Anum_pg_foreign_data_wrapper_fdwvalidator - 1] = ObjectIdGetDatum(fdwvalidator); |
| 752 |
repl_repl[Anum_pg_foreign_data_wrapper_fdwvalidator - 1] = true; |
| 753 |
|
| 754 |
/* |
| 755 |
* It could be that existing options for the FDW or dependent SERVER, |
| 756 |
* USER MAPPING or FOREIGN TABLE objects are no longer valid according |
| 757 |
* to the new validator. Warn about this. |
| 758 |
*/ |
| 759 |
if (OidIsValid(fdwvalidator)) |
| 760 |
ereport(WARNING, |
| 761 |
(errmsg("changing the foreign-data wrapper validator can cause " |
| 762 |
"the options for dependent objects to become invalid"))); |
| 763 |
} |
| 764 |
else |
| 765 |
{ |
| 766 |
/* |
| 767 |
* Validator is not changed, but we need it for validating options. |
| 768 |
*/ |
| 769 |
fdwvalidator = fdwForm->fdwvalidator; |
| 770 |
} |
| 771 |
|
| 772 |
/* |
| 773 |
* If options specified, validate and update. |
| 774 |
*/ |
| 775 |
if (stmt->options) |
| 776 |
{ |
| 777 |
/* Extract the current options */ |
| 778 |
datum = SysCacheGetAttr(FOREIGNDATAWRAPPEROID, |
| 779 |
tp, |
| 780 |
Anum_pg_foreign_data_wrapper_fdwoptions, |
| 781 |
&isnull); |
| 782 |
if (isnull) |
| 783 |
datum = PointerGetDatum(NULL); |
| 784 |
|
| 785 |
/* Transform the options */ |
| 786 |
datum = transformGenericOptions(ForeignDataWrapperRelationId, |
| 787 |
datum, |
| 788 |
stmt->options, |
| 789 |
fdwvalidator); |
| 790 |
|
| 791 |
if (PointerIsValid(DatumGetPointer(datum))) |
| 792 |
repl_val[Anum_pg_foreign_data_wrapper_fdwoptions - 1] = datum; |
| 793 |
else |
| 794 |
repl_null[Anum_pg_foreign_data_wrapper_fdwoptions - 1] = true; |
| 795 |
|
| 796 |
repl_repl[Anum_pg_foreign_data_wrapper_fdwoptions - 1] = true; |
| 797 |
} |
| 798 |
|
| 799 |
/* Everything looks good - update the tuple */ |
| 800 |
tp = heap_modify_tuple(tp, RelationGetDescr(rel), |
| 801 |
repl_val, repl_null, repl_repl); |
| 802 |
|
| 803 |
CatalogTupleUpdate(rel, &tp->t_self, tp); |
| 804 |
|
| 805 |
heap_freetuple(tp); |
| 806 |
|
| 807 |
ObjectAddressSet(myself, ForeignDataWrapperRelationId, fdwId); |
| 808 |
|
| 809 |
/* Update function dependencies if we changed them */ |
| 810 |
if (handler_given || validator_given) |
| 811 |
{ |
| 812 |
ObjectAddress referenced; |
| 813 |
|
| 814 |
/* |
| 815 |
* Flush all existing dependency records of this FDW on functions; we |
| 816 |
* assume there can be none other than the ones we are fixing. |
| 817 |
*/ |
| 818 |
deleteDependencyRecordsForClass(ForeignDataWrapperRelationId, |
| 819 |
fdwId, |
| 820 |
ProcedureRelationId, |
| 821 |
DEPENDENCY_NORMAL); |
| 822 |
|
| 823 |
/* And build new ones. */ |
| 824 |
|
| 825 |
if (OidIsValid(fdwhandler)) |
| 826 |
{ |
| 827 |
referenced.classId = ProcedureRelationId; |
| 828 |
referenced.objectId = fdwhandler; |
| 829 |
referenced.objectSubId = 0; |
| 830 |
recordDependencyOn(&myself, &referenced, DEPENDENCY_NORMAL); |
| 831 |
} |
| 832 |
|
| 833 |
if (OidIsValid(fdwvalidator)) |
| 834 |
{ |
| 835 |
referenced.classId = ProcedureRelationId; |
| 836 |
referenced.objectId = fdwvalidator; |
| 837 |
referenced.objectSubId = 0; |
| 838 |
recordDependencyOn(&myself, &referenced, DEPENDENCY_NORMAL); |
| 839 |
} |
| 840 |
} |
| 841 |
|
| 842 |
InvokeObjectPostAlterHook(ForeignDataWrapperRelationId, fdwId, 0); |
| 843 |
|
| 844 |
table_close(rel, RowExclusiveLock); |
| 845 |
|
| 846 |
return myself; |
| 847 |
} |
| 848 |
|
| 849 |
|
| 850 |
/* |
| 851 |
* Create a foreign server |
| 852 |
*/ |
| 853 |
ObjectAddress |
| 854 |
CreateForeignServer(CreateForeignServerStmt *stmt) |
| 855 |
{ |
| 856 |
Relation rel; |
| 857 |
Datum srvoptions; |
| 858 |
Datum values[Natts_pg_foreign_server]; |
| 859 |
bool nulls[Natts_pg_foreign_server]; |
| 860 |
HeapTuple tuple; |
| 861 |
Oid srvId; |
| 862 |
Oid ownerId; |
| 863 |
AclResult aclresult; |
| 864 |
ObjectAddress myself; |
| 865 |
ObjectAddress referenced; |
| 866 |
ForeignDataWrapper *fdw; |
| 867 |
|
| 868 |
rel = table_open(ForeignServerRelationId, RowExclusiveLock); |
| 869 |
|
| 870 |
/* For now the owner cannot be specified on create. Use effective user ID. */ |
| 871 |
ownerId = GetUserId(); |
| 872 |
|
| 873 |
/* |
| 874 |
* Check that there is no other foreign server by this name. If there is |
| 875 |
* one, do nothing if IF NOT EXISTS was specified. |
| 876 |
*/ |
| 877 |
srvId = get_foreign_server_oid(stmt->servername, true); |
| 878 |
if (OidIsValid(srvId)) |
| 879 |
{ |
| 880 |
if (stmt->if_not_exists) |
| 881 |
{ |
| 882 |
/* |
| 883 |
* If we are in an extension script, insist that the pre-existing |
| 884 |
* object be a member of the extension, to avoid security risks. |
| 885 |
*/ |
| 886 |
ObjectAddressSet(myself, ForeignServerRelationId, srvId); |
| 887 |
checkMembershipInCurrentExtension(&myself); |
| 888 |
|
| 889 |
/* OK to skip */ |
| 890 |
ereport(NOTICE, |
| 891 |
(errcode(ERRCODE_DUPLICATE_OBJECT), |
| 892 |
errmsg("server \"%s\" already exists, skipping", |
| 893 |
stmt->servername))); |
| 894 |
table_close(rel, RowExclusiveLock); |
| 895 |
return InvalidObjectAddress; |
| 896 |
} |
| 897 |
else |
| 898 |
ereport(ERROR, |
| 899 |
(errcode(ERRCODE_DUPLICATE_OBJECT), |
| 900 |
errmsg("server \"%s\" already exists", |
| 901 |
stmt->servername))); |
| 902 |
} |
| 903 |
|
| 904 |
/* |
| 905 |
* Check that the FDW exists and that we have USAGE on it. Also get the |
| 906 |
* actual FDW for option validation etc. |
| 907 |
*/ |
| 908 |
fdw = GetForeignDataWrapperByName(stmt->fdwname, false); |
| 909 |
|
| 910 |
aclresult = object_aclcheck(ForeignDataWrapperRelationId, fdw->fdwid, ownerId, ACL_USAGE); |
| 911 |
if (aclresult != ACLCHECK_OK) |
| 912 |
aclcheck_error(aclresult, OBJECT_FDW, fdw->fdwname); |
| 913 |
|
| 914 |
/* |
| 915 |
* Insert tuple into pg_foreign_server. |
| 916 |
*/ |
| 917 |
memset(values, 0, sizeof(values)); |
| 918 |
memset(nulls, false, sizeof(nulls)); |
| 919 |
|
| 920 |
srvId = GetNewOidWithIndex(rel, ForeignServerOidIndexId, |
| 921 |
Anum_pg_foreign_server_oid); |
| 922 |
values[Anum_pg_foreign_server_oid - 1] = ObjectIdGetDatum(srvId); |
| 923 |
values[Anum_pg_foreign_server_srvname - 1] = |
| 924 |
DirectFunctionCall1(namein, CStringGetDatum(stmt->servername)); |
| 925 |
values[Anum_pg_foreign_server_srvowner - 1] = ObjectIdGetDatum(ownerId); |
| 926 |
values[Anum_pg_foreign_server_srvfdw - 1] = ObjectIdGetDatum(fdw->fdwid); |
| 927 |
|
| 928 |
/* Add server type if supplied */ |
| 929 |
if (stmt->servertype) |
| 930 |
values[Anum_pg_foreign_server_srvtype - 1] = |
| 931 |
CStringGetTextDatum(stmt->servertype); |
| 932 |
else |
| 933 |
nulls[Anum_pg_foreign_server_srvtype - 1] = true; |
| 934 |
|
| 935 |
/* Add server version if supplied */ |
| 936 |
if (stmt->version) |
| 937 |
values[Anum_pg_foreign_server_srvversion - 1] = |
| 938 |
CStringGetTextDatum(stmt->version); |
| 939 |
else |
| 940 |
nulls[Anum_pg_foreign_server_srvversion - 1] = true; |
| 941 |
|
| 942 |
/* Start with a blank acl */ |
| 943 |
nulls[Anum_pg_foreign_server_srvacl - 1] = true; |
| 944 |
|
| 945 |
/* Add server options */ |
| 946 |
srvoptions = transformGenericOptions(ForeignServerRelationId, |
| 947 |
PointerGetDatum(NULL), |
| 948 |
stmt->options, |
| 949 |
fdw->fdwvalidator); |
| 950 |
|
| 951 |
if (PointerIsValid(DatumGetPointer(srvoptions))) |
| 952 |
values[Anum_pg_foreign_server_srvoptions - 1] = srvoptions; |
| 953 |
else |
| 954 |
nulls[Anum_pg_foreign_server_srvoptions - 1] = true; |
| 955 |
|
| 956 |
tuple = heap_form_tuple(rel->rd_att, values, nulls); |
| 957 |
|
| 958 |
CatalogTupleInsert(rel, tuple); |
| 959 |
|
| 960 |
heap_freetuple(tuple); |
| 961 |
|
| 962 |
/* record dependencies */ |
| 963 |
myself.classId = ForeignServerRelationId; |
| 964 |
myself.objectId = srvId; |
| 965 |
myself.objectSubId = 0; |
| 966 |
|
| 967 |
referenced.classId = ForeignDataWrapperRelationId; |
| 968 |
referenced.objectId = fdw->fdwid; |
| 969 |
referenced.objectSubId = 0; |
| 970 |
recordDependencyOn(&myself, &referenced, DEPENDENCY_NORMAL); |
| 971 |
|
| 972 |
recordDependencyOnOwner(ForeignServerRelationId, srvId, ownerId); |
| 973 |
|
| 974 |
/* dependency on extension */ |
| 975 |
recordDependencyOnCurrentExtension(&myself, false); |
| 976 |
|
| 977 |
/* Post creation hook for new foreign server */ |
| 978 |
InvokeObjectPostCreateHook(ForeignServerRelationId, srvId, 0); |
| 979 |
|
| 980 |
table_close(rel, RowExclusiveLock); |
| 981 |
|
| 982 |
return myself; |
| 983 |
} |
| 984 |
|
| 985 |
|
| 986 |
/* |
| 987 |
* Alter foreign server |
| 988 |
*/ |
| 989 |
ObjectAddress |
| 990 |
AlterForeignServer(AlterForeignServerStmt *stmt) |
| 991 |
{ |
| 992 |
Relation rel; |
| 993 |
HeapTuple tp; |
| 994 |
Datum repl_val[Natts_pg_foreign_server]; |
| 995 |
bool repl_null[Natts_pg_foreign_server]; |
| 996 |
bool repl_repl[Natts_pg_foreign_server]; |
| 997 |
Oid srvId; |
| 998 |
Form_pg_foreign_server srvForm; |
| 999 |
ObjectAddress address; |
| 1000 |
|
| 1001 |
rel = table_open(ForeignServerRelationId, RowExclusiveLock); |
| 1002 |
|
| 1003 |
tp = SearchSysCacheCopy1(FOREIGNSERVERNAME, |
| 1004 |
CStringGetDatum(stmt->servername)); |
| 1005 |
|
| 1006 |
if (!HeapTupleIsValid(tp)) |
| 1007 |
ereport(ERROR, |
| 1008 |
(errcode(ERRCODE_UNDEFINED_OBJECT), |
| 1009 |
errmsg("server \"%s\" does not exist", stmt->servername))); |
| 1010 |
|
| 1011 |
srvForm = (Form_pg_foreign_server) GETSTRUCT(tp); |
| 1012 |
srvId = srvForm->oid; |
| 1013 |
|
| 1014 |
/* |
| 1015 |
* Only owner or a superuser can ALTER a SERVER. |
| 1016 |
*/ |
| 1017 |
if (!object_ownercheck(ForeignServerRelationId, srvId, GetUserId())) |
| 1018 |
aclcheck_error(ACLCHECK_NOT_OWNER, OBJECT_FOREIGN_SERVER, |
| 1019 |
stmt->servername); |
| 1020 |
|
| 1021 |
memset(repl_val, 0, sizeof(repl_val)); |
| 1022 |
memset(repl_null, false, sizeof(repl_null)); |
| 1023 |
memset(repl_repl, false, sizeof(repl_repl)); |
| 1024 |
|
| 1025 |
if (stmt->has_version) |
| 1026 |
{ |
| 1027 |
/* |
| 1028 |
* Change the server VERSION string. |
| 1029 |
*/ |
| 1030 |
if (stmt->version) |
| 1031 |
repl_val[Anum_pg_foreign_server_srvversion - 1] = |
| 1032 |
CStringGetTextDatum(stmt->version); |
| 1033 |
else |
| 1034 |
repl_null[Anum_pg_foreign_server_srvversion - 1] = true; |
| 1035 |
|
| 1036 |
repl_repl[Anum_pg_foreign_server_srvversion - 1] = true; |
| 1037 |
} |
| 1038 |
|
| 1039 |
if (stmt->options) |
| 1040 |
{ |
| 1041 |
ForeignDataWrapper *fdw = GetForeignDataWrapper(srvForm->srvfdw); |
| 1042 |
Datum datum; |
| 1043 |
bool isnull; |
| 1044 |
|
| 1045 |
/* Extract the current srvoptions */ |
| 1046 |
datum = SysCacheGetAttr(FOREIGNSERVEROID, |
| 1047 |
tp, |
| 1048 |
Anum_pg_foreign_server_srvoptions, |
| 1049 |
&isnull); |
| 1050 |
if (isnull) |
| 1051 |
datum = PointerGetDatum(NULL); |
| 1052 |
|
| 1053 |
/* Prepare the options array */ |
| 1054 |
datum = transformGenericOptions(ForeignServerRelationId, |
| 1055 |
datum, |
| 1056 |
stmt->options, |
| 1057 |
fdw->fdwvalidator); |
| 1058 |
|
| 1059 |
if (PointerIsValid(DatumGetPointer(datum))) |
| 1060 |
repl_val[Anum_pg_foreign_server_srvoptions - 1] = datum; |
| 1061 |
else |
| 1062 |
repl_null[Anum_pg_foreign_server_srvoptions - 1] = true; |
| 1063 |
|
| 1064 |
repl_repl[Anum_pg_foreign_server_srvoptions - 1] = true; |
| 1065 |
} |
| 1066 |
|
| 1067 |
/* Everything looks good - update the tuple */ |
| 1068 |
tp = heap_modify_tuple(tp, RelationGetDescr(rel), |
| 1069 |
repl_val, repl_null, repl_repl); |
| 1070 |
|
| 1071 |
CatalogTupleUpdate(rel, &tp->t_self, tp); |
| 1072 |
|
| 1073 |
InvokeObjectPostAlterHook(ForeignServerRelationId, srvId, 0); |
| 1074 |
|
| 1075 |
ObjectAddressSet(address, ForeignServerRelationId, srvId); |
| 1076 |
|
| 1077 |
heap_freetuple(tp); |
| 1078 |
|
| 1079 |
table_close(rel, RowExclusiveLock); |
| 1080 |
|
| 1081 |
return address; |
| 1082 |
} |
| 1083 |
|
| 1084 |
|
| 1085 |
/* |
| 1086 |
* Common routine to check permission for user-mapping-related DDL |
| 1087 |
* commands. We allow server owners to operate on any mapping, and |
| 1088 |
* users to operate on their own mapping. |
| 1089 |
*/ |
| 1090 |
static void |
| 1091 |
user_mapping_ddl_aclcheck(Oid umuserid, Oid serverid, const char *servername) |
| 1092 |
{ |
| 1093 |
Oid curuserid = GetUserId(); |
| 1094 |
|
| 1095 |
if (!object_ownercheck(ForeignServerRelationId, serverid, curuserid)) |
| 1096 |
{ |
| 1097 |
if (umuserid == curuserid) |
| 1098 |
{ |
| 1099 |
AclResult aclresult; |
| 1100 |
|
| 1101 |
aclresult = object_aclcheck(ForeignServerRelationId, serverid, curuserid, ACL_USAGE); |
| 1102 |
if (aclresult != ACLCHECK_OK) |
| 1103 |
aclcheck_error(aclresult, OBJECT_FOREIGN_SERVER, servername); |
| 1104 |
} |
| 1105 |
else |
| 1106 |
aclcheck_error(ACLCHECK_NOT_OWNER, OBJECT_FOREIGN_SERVER, |
| 1107 |
servername); |
| 1108 |
} |
| 1109 |
} |
| 1110 |
|
| 1111 |
|
| 1112 |
/* |
| 1113 |
* Create user mapping |
| 1114 |
*/ |
| 1115 |
ObjectAddress |
| 1116 |
CreateUserMapping(CreateUserMappingStmt *stmt) |
| 1117 |
{ |
| 1118 |
Relation rel; |
| 1119 |
Datum useoptions; |
| 1120 |
Datum values[Natts_pg_user_mapping]; |
| 1121 |
bool nulls[Natts_pg_user_mapping]; |
| 1122 |
HeapTuple tuple; |
| 1123 |
Oid useId; |
| 1124 |
Oid umId; |
| 1125 |
ObjectAddress myself; |
| 1126 |
ObjectAddress referenced; |
| 1127 |
ForeignServer *srv; |
| 1128 |
ForeignDataWrapper *fdw; |
| 1129 |
RoleSpec *role = (RoleSpec *) stmt->user; |
| 1130 |
|
| 1131 |
rel = table_open(UserMappingRelationId, RowExclusiveLock); |
| 1132 |
|
| 1133 |
if (role->roletype == ROLESPEC_PUBLIC) |
| 1134 |
useId = ACL_ID_PUBLIC; |
| 1135 |
else |
| 1136 |
useId = get_rolespec_oid(stmt->user, false); |
| 1137 |
|
| 1138 |
/* Check that the server exists. */ |
| 1139 |
srv = GetForeignServerByName(stmt->servername, false); |
| 1140 |
|
| 1141 |
user_mapping_ddl_aclcheck(useId, srv->serverid, stmt->servername); |
| 1142 |
|
| 1143 |
/* |
| 1144 |
* Check that the user mapping is unique within server. |
| 1145 |
*/ |
| 1146 |
umId = GetSysCacheOid2(USERMAPPINGUSERSERVER, Anum_pg_user_mapping_oid, |
| 1147 |
ObjectIdGetDatum(useId), |
| 1148 |
ObjectIdGetDatum(srv->serverid)); |
| 1149 |
|
| 1150 |
if (OidIsValid(umId)) |
| 1151 |
{ |
| 1152 |
if (stmt->if_not_exists) |
| 1153 |
{ |
| 1154 |
/* |
| 1155 |
* Since user mappings aren't members of extensions (see comments |
| 1156 |
* below), no need for checkMembershipInCurrentExtension here. |
| 1157 |
*/ |
| 1158 |
ereport(NOTICE, |
| 1159 |
(errcode(ERRCODE_DUPLICATE_OBJECT), |
| 1160 |
errmsg("user mapping for \"%s\" already exists for server \"%s\", skipping", |
| 1161 |
MappingUserName(useId), |
| 1162 |
stmt->servername))); |
| 1163 |
|
| 1164 |
table_close(rel, RowExclusiveLock); |
| 1165 |
return InvalidObjectAddress; |
| 1166 |
} |
| 1167 |
else |
| 1168 |
ereport(ERROR, |
| 1169 |
(errcode(ERRCODE_DUPLICATE_OBJECT), |
| 1170 |
errmsg("user mapping for \"%s\" already exists for server \"%s\"", |
| 1171 |
MappingUserName(useId), |
| 1172 |
stmt->servername))); |
| 1173 |
} |
| 1174 |
|
| 1175 |
fdw = GetForeignDataWrapper(srv->fdwid); |
| 1176 |
|
| 1177 |
/* |
| 1178 |
* Insert tuple into pg_user_mapping. |
| 1179 |
*/ |
| 1180 |
memset(values, 0, sizeof(values)); |
| 1181 |
memset(nulls, false, sizeof(nulls)); |
| 1182 |
|
| 1183 |
umId = GetNewOidWithIndex(rel, UserMappingOidIndexId, |
| 1184 |
Anum_pg_user_mapping_oid); |
| 1185 |
values[Anum_pg_user_mapping_oid - 1] = ObjectIdGetDatum(umId); |
| 1186 |
values[Anum_pg_user_mapping_umuser - 1] = ObjectIdGetDatum(useId); |
| 1187 |
values[Anum_pg_user_mapping_umserver - 1] = ObjectIdGetDatum(srv->serverid); |
| 1188 |
|
| 1189 |
/* Add user options */ |
| 1190 |
useoptions = transformGenericOptions(UserMappingRelationId, |
| 1191 |
PointerGetDatum(NULL), |
| 1192 |
stmt->options, |
| 1193 |
fdw->fdwvalidator); |
| 1194 |
|
| 1195 |
if (PointerIsValid(DatumGetPointer(useoptions))) |
| 1196 |
values[Anum_pg_user_mapping_umoptions - 1] = useoptions; |
| 1197 |
else |
| 1198 |
nulls[Anum_pg_user_mapping_umoptions - 1] = true; |
| 1199 |
|
| 1200 |
tuple = heap_form_tuple(rel->rd_att, values, nulls); |
| 1201 |
|
| 1202 |
CatalogTupleInsert(rel, tuple); |
| 1203 |
|
| 1204 |
heap_freetuple(tuple); |
| 1205 |
|
| 1206 |
/* Add dependency on the server */ |
| 1207 |
myself.classId = UserMappingRelationId; |
| 1208 |
myself.objectId = umId; |
| 1209 |
myself.objectSubId = 0; |
| 1210 |
|
| 1211 |
referenced.classId = ForeignServerRelationId; |
| 1212 |
referenced.objectId = srv->serverid; |
| 1213 |
referenced.objectSubId = 0; |
| 1214 |
recordDependencyOn(&myself, &referenced, DEPENDENCY_NORMAL); |
| 1215 |
|
| 1216 |
if (OidIsValid(useId)) |
| 1217 |
{ |
| 1218 |
/* Record the mapped user dependency */ |
| 1219 |
recordDependencyOnOwner(UserMappingRelationId, umId, useId); |
| 1220 |
} |
| 1221 |
|
| 1222 |
/* |
| 1223 |
* Perhaps someday there should be a recordDependencyOnCurrentExtension |
| 1224 |
* call here; but since roles aren't members of extensions, it seems like |
| 1225 |
* user mappings shouldn't be either. Note that the grammar and pg_dump |
| 1226 |
* would need to be extended too if we change this. |
| 1227 |
*/ |
| 1228 |
|
| 1229 |
/* Post creation hook for new user mapping */ |
| 1230 |
InvokeObjectPostCreateHook(UserMappingRelationId, umId, 0); |
| 1231 |
|
| 1232 |
table_close(rel, RowExclusiveLock); |
| 1233 |
|
| 1234 |
return myself; |
| 1235 |
} |
| 1236 |
|
| 1237 |
|
| 1238 |
/* |
| 1239 |
* Alter user mapping |
| 1240 |
*/ |
| 1241 |
ObjectAddress |
| 1242 |
AlterUserMapping(AlterUserMappingStmt *stmt) |
| 1243 |
{ |
| 1244 |
Relation rel; |
| 1245 |
HeapTuple tp; |
| 1246 |
Datum repl_val[Natts_pg_user_mapping]; |
| 1247 |
bool repl_null[Natts_pg_user_mapping]; |
| 1248 |
bool repl_repl[Natts_pg_user_mapping]; |
| 1249 |
Oid useId; |
| 1250 |
Oid umId; |
| 1251 |
ForeignServer *srv; |
| 1252 |
ObjectAddress address; |
| 1253 |
RoleSpec *role = (RoleSpec *) stmt->user; |
| 1254 |
|
| 1255 |
rel = table_open(UserMappingRelationId, RowExclusiveLock); |
| 1256 |
|
| 1257 |
if (role->roletype == ROLESPEC_PUBLIC) |
| 1258 |
useId = ACL_ID_PUBLIC; |
| 1259 |
else |
| 1260 |
useId = get_rolespec_oid(stmt->user, false); |
| 1261 |
|
| 1262 |
srv = GetForeignServerByName(stmt->servername, false); |
| 1263 |
|
| 1264 |
umId = GetSysCacheOid2(USERMAPPINGUSERSERVER, Anum_pg_user_mapping_oid, |
| 1265 |
ObjectIdGetDatum(useId), |
| 1266 |
ObjectIdGetDatum(srv->serverid)); |
| 1267 |
if (!OidIsValid(umId)) |
| 1268 |
ereport(ERROR, |
| 1269 |
(errcode(ERRCODE_UNDEFINED_OBJECT), |
| 1270 |
errmsg("user mapping for \"%s\" does not exist for server \"%s\"", |
| 1271 |
MappingUserName(useId), stmt->servername))); |
| 1272 |
|
| 1273 |
user_mapping_ddl_aclcheck(useId, srv->serverid, stmt->servername); |
| 1274 |
|
| 1275 |
tp = SearchSysCacheCopy1(USERMAPPINGOID, ObjectIdGetDatum(umId)); |
| 1276 |
|
| 1277 |
if (!HeapTupleIsValid(tp)) |
| 1278 |
elog(ERROR, "cache lookup failed for user mapping %u", umId); |
| 1279 |
|
| 1280 |
memset(repl_val, 0, sizeof(repl_val)); |
| 1281 |
memset(repl_null, false, sizeof(repl_null)); |
| 1282 |
memset(repl_repl, false, sizeof(repl_repl)); |
| 1283 |
|
| 1284 |
if (stmt->options) |
| 1285 |
{ |
| 1286 |
ForeignDataWrapper *fdw; |
| 1287 |
Datum datum; |
| 1288 |
bool isnull; |
| 1289 |
|
| 1290 |
/* |
| 1291 |
* Process the options. |
| 1292 |
*/ |
| 1293 |
|
| 1294 |
fdw = GetForeignDataWrapper(srv->fdwid); |
| 1295 |
|
| 1296 |
datum = SysCacheGetAttr(USERMAPPINGUSERSERVER, |
| 1297 |
tp, |
| 1298 |
Anum_pg_user_mapping_umoptions, |
| 1299 |
&isnull); |
| 1300 |
if (isnull) |
| 1301 |
datum = PointerGetDatum(NULL); |
| 1302 |
|
| 1303 |
/* Prepare the options array */ |
| 1304 |
datum = transformGenericOptions(UserMappingRelationId, |
| 1305 |
datum, |
| 1306 |
stmt->options, |
| 1307 |
fdw->fdwvalidator); |
| 1308 |
|
| 1309 |
if (PointerIsValid(DatumGetPointer(datum))) |
| 1310 |
repl_val[Anum_pg_user_mapping_umoptions - 1] = datum; |
| 1311 |
else |
| 1312 |
repl_null[Anum_pg_user_mapping_umoptions - 1] = true; |
| 1313 |
|
| 1314 |
repl_repl[Anum_pg_user_mapping_umoptions - 1] = true; |
| 1315 |
} |
| 1316 |
|
| 1317 |
/* Everything looks good - update the tuple */ |
| 1318 |
tp = heap_modify_tuple(tp, RelationGetDescr(rel), |
| 1319 |
repl_val, repl_null, repl_repl); |
| 1320 |
|
| 1321 |
CatalogTupleUpdate(rel, &tp->t_self, tp); |
| 1322 |
|
| 1323 |
InvokeObjectPostAlterHook(UserMappingRelationId, |
| 1324 |
umId, 0); |
| 1325 |
|
| 1326 |
ObjectAddressSet(address, UserMappingRelationId, umId); |
| 1327 |
|
| 1328 |
heap_freetuple(tp); |
| 1329 |
|
| 1330 |
table_close(rel, RowExclusiveLock); |
| 1331 |
|
| 1332 |
return address; |
| 1333 |
} |
| 1334 |
|
| 1335 |
|
| 1336 |
/* |
| 1337 |
* Drop user mapping |
| 1338 |
*/ |
| 1339 |
Oid |
| 1340 |
RemoveUserMapping(DropUserMappingStmt *stmt) |
| 1341 |
{ |
| 1342 |
ObjectAddress object; |
| 1343 |
Oid useId; |
| 1344 |
Oid umId; |
| 1345 |
ForeignServer *srv; |
| 1346 |
RoleSpec *role = (RoleSpec *) stmt->user; |
| 1347 |
|
| 1348 |
if (role->roletype == ROLESPEC_PUBLIC) |
| 1349 |
useId = ACL_ID_PUBLIC; |
| 1350 |
else |
| 1351 |
{ |
| 1352 |
useId = get_rolespec_oid(stmt->user, stmt->missing_ok); |
| 1353 |
if (!OidIsValid(useId)) |
| 1354 |
{ |
| 1355 |
/* |
| 1356 |
* IF EXISTS specified, role not found and not public. Notice this |
| 1357 |
* and leave. |
| 1358 |
*/ |
| 1359 |
elog(NOTICE, "role \"%s\" does not exist, skipping", |
| 1360 |
role->rolename); |
| 1361 |
return InvalidOid; |
| 1362 |
} |
| 1363 |
} |
| 1364 |
|
| 1365 |
srv = GetForeignServerByName(stmt->servername, true); |
| 1366 |
|
| 1367 |
if (!srv) |
| 1368 |
{ |
| 1369 |
if (!stmt->missing_ok) |
| 1370 |
ereport(ERROR, |
| 1371 |
(errcode(ERRCODE_UNDEFINED_OBJECT), |
| 1372 |
errmsg("server \"%s\" does not exist", |
| 1373 |
stmt->servername))); |
| 1374 |
/* IF EXISTS, just note it */ |
| 1375 |
ereport(NOTICE, |
| 1376 |
(errmsg("server \"%s\" does not exist, skipping", |
| 1377 |
stmt->servername))); |
| 1378 |
return InvalidOid; |
| 1379 |
} |
| 1380 |
|
| 1381 |
umId = GetSysCacheOid2(USERMAPPINGUSERSERVER, Anum_pg_user_mapping_oid, |
| 1382 |
ObjectIdGetDatum(useId), |
| 1383 |
ObjectIdGetDatum(srv->serverid)); |
| 1384 |
|
| 1385 |
if (!OidIsValid(umId)) |
| 1386 |
{ |
| 1387 |
if (!stmt->missing_ok) |
| 1388 |
ereport(ERROR, |
| 1389 |
(errcode(ERRCODE_UNDEFINED_OBJECT), |
| 1390 |
errmsg("user mapping for \"%s\" does not exist for server \"%s\"", |
| 1391 |
MappingUserName(useId), stmt->servername))); |
| 1392 |
|
| 1393 |
/* IF EXISTS specified, just note it */ |
| 1394 |
ereport(NOTICE, |
| 1395 |
(errmsg("user mapping for \"%s\" does not exist for server \"%s\", skipping", |
| 1396 |
MappingUserName(useId), stmt->servername))); |
| 1397 |
return InvalidOid; |
| 1398 |
} |
| 1399 |
|
| 1400 |
user_mapping_ddl_aclcheck(useId, srv->serverid, srv->servername); |
| 1401 |
|
| 1402 |
/* |
| 1403 |
* Do the deletion |
| 1404 |
*/ |
| 1405 |
object.classId = UserMappingRelationId; |
| 1406 |
object.objectId = umId; |
| 1407 |
object.objectSubId = 0; |
| 1408 |
|
| 1409 |
performDeletion(&object, DROP_CASCADE, 0); |
| 1410 |
|
| 1411 |
return umId; |
| 1412 |
} |
| 1413 |
|
| 1414 |
|
| 1415 |
/* |
| 1416 |
* Create a foreign table |
| 1417 |
* call after DefineRelation(). |
| 1418 |
*/ |
| 1419 |
void |
| 1420 |
CreateForeignTable(CreateForeignTableStmt *stmt, Oid relid) |
| 1421 |
{ |
| 1422 |
Relation ftrel; |
| 1423 |
Datum ftoptions; |
| 1424 |
Datum values[Natts_pg_foreign_table]; |
| 1425 |
bool nulls[Natts_pg_foreign_table]; |
| 1426 |
HeapTuple tuple; |
| 1427 |
AclResult aclresult; |
| 1428 |
ObjectAddress myself; |
| 1429 |
ObjectAddress referenced; |
| 1430 |
Oid ownerId; |
| 1431 |
ForeignDataWrapper *fdw; |
| 1432 |
ForeignServer *server; |
| 1433 |
|
| 1434 |
/* |
| 1435 |
* Advance command counter to ensure the pg_attribute tuple is visible; |
| 1436 |
* the tuple might be updated to add constraints in previous step. |
| 1437 |
*/ |
| 1438 |
CommandCounterIncrement(); |
| 1439 |
|
| 1440 |
ftrel = table_open(ForeignTableRelationId, RowExclusiveLock); |
| 1441 |
|
| 1442 |
/* |
| 1443 |
* For now the owner cannot be specified on create. Use effective user ID. |
| 1444 |
*/ |
| 1445 |
ownerId = GetUserId(); |
| 1446 |
|
| 1447 |
/* |
| 1448 |
* Check that the foreign server exists and that we have USAGE on it. Also |
| 1449 |
* get the actual FDW for option validation etc. |
| 1450 |
*/ |
| 1451 |
server = GetForeignServerByName(stmt->servername, false); |
| 1452 |
aclresult = object_aclcheck(ForeignServerRelationId, server->serverid, ownerId, ACL_USAGE); |
| 1453 |
if (aclresult != ACLCHECK_OK) |
| 1454 |
aclcheck_error(aclresult, OBJECT_FOREIGN_SERVER, server->servername); |
| 1455 |
|
| 1456 |
fdw = GetForeignDataWrapper(server->fdwid); |
| 1457 |
|
| 1458 |
/* |
| 1459 |
* Insert tuple into pg_foreign_table. |
| 1460 |
*/ |
| 1461 |
memset(values, 0, sizeof(values)); |
| 1462 |
memset(nulls, false, sizeof(nulls)); |
| 1463 |
|
| 1464 |
values[Anum_pg_foreign_table_ftrelid - 1] = ObjectIdGetDatum(relid); |
| 1465 |
values[Anum_pg_foreign_table_ftserver - 1] = ObjectIdGetDatum(server->serverid); |
| 1466 |
/* Add table generic options */ |
| 1467 |
ftoptions = transformGenericOptions(ForeignTableRelationId, |
| 1468 |
PointerGetDatum(NULL), |
| 1469 |
stmt->options, |
| 1470 |
fdw->fdwvalidator); |
| 1471 |
|
| 1472 |
if (PointerIsValid(DatumGetPointer(ftoptions))) |
| 1473 |
values[Anum_pg_foreign_table_ftoptions - 1] = ftoptions; |
| 1474 |
else |
| 1475 |
nulls[Anum_pg_foreign_table_ftoptions - 1] = true; |
| 1476 |
|
| 1477 |
tuple = heap_form_tuple(ftrel->rd_att, values, nulls); |
| 1478 |
|
| 1479 |
CatalogTupleInsert(ftrel, tuple); |
| 1480 |
|
| 1481 |
heap_freetuple(tuple); |
| 1482 |
|
| 1483 |
/* Add pg_class dependency on the server */ |
| 1484 |
myself.classId = RelationRelationId; |
| 1485 |
myself.objectId = relid; |
| 1486 |
myself.objectSubId = 0; |
| 1487 |
|
| 1488 |
referenced.classId = ForeignServerRelationId; |
| 1489 |
referenced.objectId = server->serverid; |
| 1490 |
referenced.objectSubId = 0; |
| 1491 |
recordDependencyOn(&myself, &referenced, DEPENDENCY_NORMAL); |
| 1492 |
|
| 1493 |
table_close(ftrel, RowExclusiveLock); |
| 1494 |
} |
| 1495 |
|
| 1496 |
/* |
| 1497 |
* Import a foreign schema |
| 1498 |
*/ |
| 1499 |
void |
| 1500 |
ImportForeignSchema(ImportForeignSchemaStmt *stmt) |
| 1501 |
{ |
| 1502 |
ForeignServer *server; |
| 1503 |
ForeignDataWrapper *fdw; |
| 1504 |
FdwRoutine *fdw_routine; |
| 1505 |
AclResult aclresult; |
| 1506 |
List *cmd_list; |
| 1507 |
ListCell *lc; |
| 1508 |
|
| 1509 |
/* Check that the foreign server exists and that we have USAGE on it */ |
| 1510 |
server = GetForeignServerByName(stmt->server_name, false); |
| 1511 |
aclresult = object_aclcheck(ForeignServerRelationId, server->serverid, GetUserId(), ACL_USAGE); |
| 1512 |
if (aclresult != ACLCHECK_OK) |
| 1513 |
aclcheck_error(aclresult, OBJECT_FOREIGN_SERVER, server->servername); |
| 1514 |
|
| 1515 |
/* Check that the schema exists and we have CREATE permissions on it */ |
| 1516 |
(void) LookupCreationNamespace(stmt->local_schema); |
| 1517 |
|
| 1518 |
/* Get the FDW and check it supports IMPORT */ |
| 1519 |
fdw = GetForeignDataWrapper(server->fdwid); |
| 1520 |
if (!OidIsValid(fdw->fdwhandler)) |
| 1521 |
ereport(ERROR, |
| 1522 |
(errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), |
| 1523 |
errmsg("foreign-data wrapper \"%s\" has no handler", |
| 1524 |
fdw->fdwname))); |
| 1525 |
fdw_routine = GetFdwRoutine(fdw->fdwhandler); |
| 1526 |
if (fdw_routine->ImportForeignSchema == NULL) |
| 1527 |
ereport(ERROR, |
| 1528 |
(errcode(ERRCODE_FDW_NO_SCHEMAS), |
| 1529 |
errmsg("foreign-data wrapper \"%s\" does not support IMPORT FOREIGN SCHEMA", |
| 1530 |
fdw->fdwname))); |
| 1531 |
|
| 1532 |
/* Call FDW to get a list of commands */ |
| 1533 |
cmd_list = fdw_routine->ImportForeignSchema(stmt, server->serverid); |
| 1534 |
|
| 1535 |
/* Parse and execute each command */ |
| 1536 |
foreach(lc, cmd_list) |
| 1537 |
{ |
| 1538 |
char *cmd = (char *) lfirst(lc); |
| 1539 |
import_error_callback_arg callback_arg; |
| 1540 |
ErrorContextCallback sqlerrcontext; |
| 1541 |
List *raw_parsetree_list; |
| 1542 |
ListCell *lc2; |
| 1543 |
|
| 1544 |
/* |
| 1545 |
* Setup error traceback support for ereport(). This is so that any |
| 1546 |
* error in the generated SQL will be displayed nicely. |
| 1547 |
*/ |
| 1548 |
callback_arg.tablename = NULL; /* not known yet */ |
| 1549 |
callback_arg.cmd = cmd; |
| 1550 |
sqlerrcontext.callback = import_error_callback; |
| 1551 |
sqlerrcontext.arg = &callback_arg; |
| 1552 |
sqlerrcontext.previous = error_context_stack; |
| 1553 |
error_context_stack = &sqlerrcontext; |
| 1554 |
|
| 1555 |
/* |
| 1556 |
* Parse the SQL string into a list of raw parse trees. |
| 1557 |
*/ |
| 1558 |
raw_parsetree_list = pg_parse_query(cmd); |
| 1559 |
|
| 1560 |
/* |
| 1561 |
* Process each parse tree (we allow the FDW to put more than one |
| 1562 |
* command per string, though this isn't really advised). |
| 1563 |
*/ |
| 1564 |
foreach(lc2, raw_parsetree_list) |
| 1565 |
{ |
| 1566 |
RawStmt *rs = lfirst_node(RawStmt, lc2); |
| 1567 |
CreateForeignTableStmt *cstmt = (CreateForeignTableStmt *) rs->stmt; |
| 1568 |
PlannedStmt *pstmt; |
| 1569 |
|
| 1570 |
/* |
| 1571 |
* Because we only allow CreateForeignTableStmt, we can skip parse |
| 1572 |
* analysis, rewrite, and planning steps here. |
| 1573 |
*/ |
| 1574 |
if (!IsA(cstmt, CreateForeignTableStmt)) |
| 1575 |
elog(ERROR, |
| 1576 |
"foreign-data wrapper \"%s\" returned incorrect statement type %d", |
| 1577 |
fdw->fdwname, (int) nodeTag(cstmt)); |
| 1578 |
|
| 1579 |
/* Ignore commands for tables excluded by filter options */ |
| 1580 |
if (!IsImportableForeignTable(cstmt->base.relation->relname, stmt)) |
| 1581 |
continue; |
| 1582 |
|
| 1583 |
/* Enable reporting of current table's name on error */ |
| 1584 |
callback_arg.tablename = cstmt->base.relation->relname; |
| 1585 |
|
| 1586 |
/* Ensure creation schema is the one given in IMPORT statement */ |
| 1587 |
cstmt->base.relation->schemaname = pstrdup(stmt->local_schema); |
| 1588 |
|
| 1589 |
/* No planning needed, just make a wrapper PlannedStmt */ |
| 1590 |
pstmt = makeNode(PlannedStmt); |
| 1591 |
pstmt->commandType = CMD_UTILITY; |
| 1592 |
pstmt->canSetTag = false; |
| 1593 |
pstmt->utilityStmt = (Node *) cstmt; |
| 1594 |
pstmt->stmt_location = rs->stmt_location; |
| 1595 |
pstmt->stmt_len = rs->stmt_len; |
| 1596 |
|
| 1597 |
/* Execute statement */ |
| 1598 |
ProcessUtility(pstmt, cmd, false, |
| 1599 |
PROCESS_UTILITY_SUBCOMMAND, NULL, NULL, |
| 1600 |
None_Receiver, NULL); |
| 1601 |
|
| 1602 |
/* Be sure to advance the command counter between subcommands */ |
| 1603 |
CommandCounterIncrement(); |
| 1604 |
|
| 1605 |
callback_arg.tablename = NULL; |
| 1606 |
} |
| 1607 |
|
| 1608 |
error_context_stack = sqlerrcontext.previous; |
| 1609 |
} |
| 1610 |
} |
| 1611 |
|
| 1612 |
/* |
| 1613 |
* error context callback to let us supply the failing SQL statement's text |
| 1614 |
*/ |
| 1615 |
static void |
| 1616 |
import_error_callback(void *arg) |
| 1617 |
{ |
| 1618 |
import_error_callback_arg *callback_arg = (import_error_callback_arg *) arg; |
| 1619 |
int syntaxerrposition; |
| 1620 |
|
| 1621 |
/* If it's a syntax error, convert to internal syntax error report */ |
| 1622 |
syntaxerrposition = geterrposition(); |
| 1623 |
if (syntaxerrposition > 0) |
| 1624 |
{ |
| 1625 |
errposition(0); |
| 1626 |
internalerrposition(syntaxerrposition); |
| 1627 |
internalerrquery(callback_arg->cmd); |
| 1628 |
} |
| 1629 |
|
| 1630 |
if (callback_arg->tablename) |
| 1631 |
errcontext("importing foreign table \"%s\"", |
| 1632 |
callback_arg->tablename); |
| 1633 |
} |