* CheckMyDatabase -- fetch information from the pg_database entry for our DB */
| 365 | * CheckMyDatabase -- fetch information from the pg_database entry for our DB |
| 366 | */ |
| 367 | static void |
| 368 | CheckMyDatabase(const char *name, bool am_superuser, bool override_allow_connections) |
| 369 | { |
| 370 | HeapTuple tup; |
| 371 | Form_pg_database dbform; |
| 372 | char *collate; |
| 373 | char *ctype; |
| 374 | |
| 375 | /* Fetch our pg_database row normally, via syscache */ |
| 376 | tup = SearchSysCache1(DATABASEOID, ObjectIdGetDatum(MyDatabaseId)); |
| 377 | if (!HeapTupleIsValid(tup)) |
| 378 | elog(ERROR, "cache lookup failed for database %u", MyDatabaseId); |
| 379 | dbform = (Form_pg_database) GETSTRUCT(tup); |
| 380 | |
| 381 | /* This recheck is strictly paranoia */ |
| 382 | if (strcmp(name, NameStr(dbform->datname)) != 0) |
| 383 | ereport(FATAL, |
| 384 | (errcode(ERRCODE_UNDEFINED_DATABASE), |
| 385 | errmsg("database \"%s\" has disappeared from pg_database", |
| 386 | name), |
| 387 | errdetail("Database OID %u now seems to belong to \"%s\".", |
| 388 | MyDatabaseId, NameStr(dbform->datname)))); |
| 389 | |
| 390 | /* |
| 391 | * Check permissions to connect to the database. |
| 392 | * |
| 393 | * These checks are not enforced when in standalone mode, so that there is |
| 394 | * a way to recover from disabling all access to all databases, for |
| 395 | * example "UPDATE pg_database SET datallowconn = false;". |
| 396 | * |
| 397 | * We do not enforce them for autovacuum worker and login monitor processes |
| 398 | * either. |
| 399 | */ |
| 400 | if (IsUnderPostmaster && !IsAutoVacuumWorkerProcess() && !IsLoginMonitorWorkerProcess()) |
| 401 | { |
| 402 | /* |
| 403 | * Check that the database is currently allowing connections. |
| 404 | */ |
| 405 | if (!dbform->datallowconn && !override_allow_connections) |
| 406 | ereport(FATAL, |
| 407 | (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), |
| 408 | errmsg("database \"%s\" is not currently accepting connections", |
| 409 | name))); |
| 410 | |
| 411 | /* |
| 412 | * Check privilege to connect to the database. (The am_superuser test |
| 413 | * is redundant, but since we have the flag, might as well check it |
| 414 | * and save a few cycles.) |
| 415 | */ |
| 416 | if (!am_superuser && |
| 417 | pg_database_aclcheck(MyDatabaseId, GetUserId(), |
| 418 | ACL_CONNECT) != ACLCHECK_OK) |
| 419 | ereport(FATAL, |
| 420 | (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE), |
| 421 | errmsg("permission denied for database \"%s\"", name), |
| 422 | errdetail("User does not have CONNECT privilege."))); |
| 423 | |
| 424 | /* |
no test coverage detected