| 1 |
/*------------------------------------------------------------------------- |
| 2 |
* |
| 3 |
* dbcommands.c |
| 4 |
* Database management commands (create/drop database). |
| 5 |
* |
| 6 |
* Note: database creation/destruction commands use exclusive locks on |
| 7 |
* the database objects (as expressed by LockSharedObject()) to avoid |
| 8 |
* stepping on each others' toes. Formerly we used table-level locks |
| 9 |
* on pg_database, but that's too coarse-grained. |
| 10 |
* |
| 11 |
* Portions Copyright (c) 1996-2025, PostgreSQL Global Development Group |
| 12 |
* Portions Copyright (c) 1994, Regents of the University of California |
| 13 |
* |
| 14 |
* |
| 15 |
* IDENTIFICATION |
| 16 |
* src/backend/commands/dbcommands.c |
| 17 |
* |
| 18 |
*------------------------------------------------------------------------- |
| 19 |
*/ |
| 20 |
#include "postgres.h" |
| 21 |
|
| 22 |
#include <fcntl.h> |
| 23 |
#include <unistd.h> |
| 24 |
#include <sys/stat.h> |
| 25 |
|
| 26 |
#include "access/genam.h" |
| 27 |
#include "access/heapam.h" |
| 28 |
#include "access/htup_details.h" |
| 29 |
#include "access/multixact.h" |
| 30 |
#include "access/tableam.h" |
| 31 |
#include "access/xact.h" |
| 32 |
#include "access/xloginsert.h" |
| 33 |
#include "access/xlogrecovery.h" |
| 34 |
#include "access/xlogutils.h" |
| 35 |
#include "catalog/catalog.h" |
| 36 |
#include "catalog/dependency.h" |
| 37 |
#include "catalog/indexing.h" |
| 38 |
#include "catalog/objectaccess.h" |
| 39 |
#include "catalog/pg_authid.h" |
| 40 |
#include "catalog/pg_collation.h" |
| 41 |
#include "catalog/pg_database.h" |
| 42 |
#include "catalog/pg_db_role_setting.h" |
| 43 |
#include "catalog/pg_subscription.h" |
| 44 |
#include "catalog/pg_tablespace.h" |
| 45 |
#include "commands/comment.h" |
| 46 |
#include "commands/dbcommands.h" |
| 47 |
#include "commands/dbcommands_xlog.h" |
| 48 |
#include "commands/defrem.h" |
| 49 |
#include "commands/seclabel.h" |
| 50 |
#include "commands/tablespace.h" |
| 51 |
#include "common/file_perm.h" |
| 52 |
#include "mb/pg_wchar.h" |
| 53 |
#include "miscadmin.h" |
| 54 |
#include "pgstat.h" |
| 55 |
#include "postmaster/bgwriter.h" |
| 56 |
#include "replication/slot.h" |
| 57 |
#include "storage/copydir.h" |
| 58 |
#include "storage/fd.h" |
| 59 |
#include "storage/ipc.h" |
| 60 |
#include "storage/lmgr.h" |
| 61 |
#include "storage/md.h" |
| 62 |
#include "storage/procarray.h" |
| 63 |
#include "storage/smgr.h" |
| 64 |
#include "utils/acl.h" |
| 65 |
#include "utils/builtins.h" |
| 66 |
#include "utils/fmgroids.h" |
| 67 |
#include "utils/pg_locale.h" |
| 68 |
#include "utils/relmapper.h" |
| 69 |
#include "utils/snapmgr.h" |
| 70 |
#include "utils/syscache.h" |
| 71 |
|
| 72 |
/* |
| 73 |
* Create database strategy. |
| 74 |
* |
| 75 |
* CREATEDB_WAL_LOG will copy the database at the block level and WAL log each |
| 76 |
* copied block. |
| 77 |
* |
| 78 |
* CREATEDB_FILE_COPY will simply perform a file system level copy of the |
| 79 |
* database and log a single record for each tablespace copied. To make this |
| 80 |
* safe, it also triggers checkpoints before and after the operation. |
| 81 |
*/ |
| 82 |
typedef enum CreateDBStrategy |
| 83 |
{ |
| 84 |
CREATEDB_WAL_LOG, |
| 85 |
CREATEDB_FILE_COPY, |
| 86 |
} CreateDBStrategy; |
| 87 |
|
| 88 |
typedef struct |
| 89 |
{ |
| 90 |
Oid src_dboid; /* source (template) DB */ |
| 91 |
Oid dest_dboid; /* DB we are trying to create */ |
| 92 |
CreateDBStrategy strategy; /* create db strategy */ |
| 93 |
} createdb_failure_params; |
| 94 |
|
| 95 |
typedef struct |
| 96 |
{ |
| 97 |
Oid dest_dboid; /* DB we are trying to move */ |
| 98 |
Oid dest_tsoid; /* tablespace we are trying to move to */ |
| 99 |
} movedb_failure_params; |
| 100 |
|
| 101 |
/* |
| 102 |
* Information about a relation to be copied when creating a database. |
| 103 |
*/ |
| 104 |
typedef struct CreateDBRelInfo |
| 105 |
{ |
| 106 |
RelFileLocator rlocator; /* physical relation identifier */ |
| 107 |
Oid reloid; /* relation oid */ |
| 108 |
bool permanent; /* relation is permanent or unlogged */ |
| 109 |
} CreateDBRelInfo; |
| 110 |
|
| 111 |
|
| 112 |
/* non-export function prototypes */ |
| 113 |
static void createdb_failure_callback(int code, Datum arg); |
| 114 |
static void movedb(const char *dbname, const char *tblspcname); |
| 115 |
static void movedb_failure_callback(int code, Datum arg); |
| 116 |
static bool get_db_info(const char *name, LOCKMODE lockmode, |
| 117 |
Oid *dbIdP, Oid *ownerIdP, |
| 118 |
int *encodingP, bool *dbIsTemplateP, bool *dbAllowConnP, bool *dbHasLoginEvtP, |
| 119 |
TransactionId *dbFrozenXidP, MultiXactId *dbMinMultiP, |
| 120 |
Oid *dbTablespace, char **dbCollate, char **dbCtype, char **dbLocale, |
| 121 |
char **dbIcurules, |
| 122 |
char *dbLocProvider, |
| 123 |
char **dbCollversion); |
| 124 |
static void remove_dbtablespaces(Oid db_id); |
| 125 |
static bool check_db_file_conflict(Oid db_id); |
| 126 |
static int errdetail_busy_db(int notherbackends, int npreparedxacts); |
| 127 |
static void CreateDatabaseUsingWalLog(Oid src_dboid, Oid dst_dboid, Oid src_tsid, |
| 128 |
Oid dst_tsid); |
| 129 |
static List *ScanSourceDatabasePgClass(Oid tbid, Oid dbid, char *srcpath); |
| 130 |
static List *ScanSourceDatabasePgClassPage(Page page, Buffer buf, Oid tbid, |
| 131 |
Oid dbid, char *srcpath, |
| 132 |
List *rlocatorlist, Snapshot snapshot); |
| 133 |
static CreateDBRelInfo *ScanSourceDatabasePgClassTuple(HeapTupleData *tuple, |
| 134 |
Oid tbid, Oid dbid, |
| 135 |
char *srcpath); |
| 136 |
static void CreateDirAndVersionFile(char *dbpath, Oid dbid, Oid tsid, |
| 137 |
bool isRedo); |
| 138 |
static void CreateDatabaseUsingFileCopy(Oid src_dboid, Oid dst_dboid, |
| 139 |
Oid src_tsid, Oid dst_tsid); |
| 140 |
static void recovery_create_dbdir(char *path, bool only_tblspc); |
| 141 |
|
| 142 |
/* |
| 143 |
* Create a new database using the WAL_LOG strategy. |
| 144 |
* |
| 145 |
* Each copied block is separately written to the write-ahead log. |
| 146 |
*/ |
| 147 |
static void |
| 148 |
CreateDatabaseUsingWalLog(Oid src_dboid, Oid dst_dboid, |
| 149 |
Oid src_tsid, Oid dst_tsid) |
| 150 |
{ |
| 151 |
char *srcpath; |
| 152 |
char *dstpath; |
| 153 |
List *rlocatorlist = NULL; |
| 154 |
ListCell *cell; |
| 155 |
LockRelId srcrelid; |
| 156 |
LockRelId dstrelid; |
| 157 |
RelFileLocator srcrlocator; |
| 158 |
RelFileLocator dstrlocator; |
| 159 |
CreateDBRelInfo *relinfo; |
| 160 |
|
| 161 |
/* Get source and destination database paths. */ |
| 162 |
srcpath = GetDatabasePath(src_dboid, src_tsid); |
| 163 |
dstpath = GetDatabasePath(dst_dboid, dst_tsid); |
| 164 |
|
| 165 |
/* Create database directory and write PG_VERSION file. */ |
| 166 |
CreateDirAndVersionFile(dstpath, dst_dboid, dst_tsid, false); |
| 167 |
|
| 168 |
/* Copy relmap file from source database to the destination database. */ |
| 169 |
RelationMapCopy(dst_dboid, dst_tsid, srcpath, dstpath); |
| 170 |
|
| 171 |
/* Get list of relfilelocators to copy from the source database. */ |
| 172 |
rlocatorlist = ScanSourceDatabasePgClass(src_tsid, src_dboid, srcpath); |
| 173 |
Assert(rlocatorlist != NIL); |
| 174 |
|
| 175 |
/* |
| 176 |
* Database IDs will be the same for all relations so set them before |
| 177 |
* entering the loop. |
| 178 |
*/ |
| 179 |
srcrelid.dbId = src_dboid; |
| 180 |
dstrelid.dbId = dst_dboid; |
| 181 |
|
| 182 |
/* Loop over our list of relfilelocators and copy each one. */ |
| 183 |
foreach(cell, rlocatorlist) |
| 184 |
{ |
| 185 |
relinfo = lfirst(cell); |
| 186 |
srcrlocator = relinfo->rlocator; |
| 187 |
|
| 188 |
/* |
| 189 |
* If the relation is from the source db's default tablespace then we |
| 190 |
* need to create it in the destination db's default tablespace. |
| 191 |
* Otherwise, we need to create in the same tablespace as it is in the |
| 192 |
* source database. |
| 193 |
*/ |
| 194 |
if (srcrlocator.spcOid == src_tsid) |
| 195 |
dstrlocator.spcOid = dst_tsid; |
| 196 |
else |
| 197 |
dstrlocator.spcOid = srcrlocator.spcOid; |
| 198 |
|
| 199 |
dstrlocator.dbOid = dst_dboid; |
| 200 |
dstrlocator.relNumber = srcrlocator.relNumber; |
| 201 |
|
| 202 |
/* |
| 203 |
* Acquire locks on source and target relations before copying. |
| 204 |
* |
| 205 |
* We typically do not read relation data into shared_buffers without |
| 206 |
* holding a relation lock. It's unclear what could go wrong if we |
| 207 |
* skipped it in this case, because nobody can be modifying either the |
| 208 |
* source or destination database at this point, and we have locks on |
| 209 |
* both databases, too, but let's take the conservative route. |
| 210 |
*/ |
| 211 |
dstrelid.relId = srcrelid.relId = relinfo->reloid; |
| 212 |
LockRelationId(&srcrelid, AccessShareLock); |
| 213 |
LockRelationId(&dstrelid, AccessShareLock); |
| 214 |
|
| 215 |
/* Copy relation storage from source to the destination. */ |
| 216 |
CreateAndCopyRelationData(srcrlocator, dstrlocator, relinfo->permanent); |
| 217 |
|
| 218 |
/* Release the relation locks. */ |
| 219 |
UnlockRelationId(&srcrelid, AccessShareLock); |
| 220 |
UnlockRelationId(&dstrelid, AccessShareLock); |
| 221 |
} |
| 222 |
|
| 223 |
pfree(srcpath); |
| 224 |
pfree(dstpath); |
| 225 |
list_free_deep(rlocatorlist); |
| 226 |
} |
| 227 |
|
| 228 |
/* |
| 229 |
* Scan the pg_class table in the source database to identify the relations |
| 230 |
* that need to be copied to the destination database. |
| 231 |
* |
| 232 |
* This is an exception to the usual rule that cross-database access is |
| 233 |
* not possible. We can make it work here because we know that there are no |
| 234 |
* connections to the source database and (since there can't be prepared |
| 235 |
* transactions touching that database) no in-doubt tuples either. This |
| 236 |
* means that we don't need to worry about pruning removing anything from |
| 237 |
* under us, and we don't need to be too picky about our snapshot either. |
| 238 |
* As long as it sees all previously-committed XIDs as committed and all |
| 239 |
* aborted XIDs as aborted, we should be fine: nothing else is possible |
| 240 |
* here. |
| 241 |
* |
| 242 |
* We can't rely on the relcache for anything here, because that only knows |
| 243 |
* about the database to which we are connected, and can't handle access to |
| 244 |
* other databases. That also means we can't rely on the heap scan |
| 245 |
* infrastructure, which would be a bad idea anyway since it might try |
| 246 |
* to do things like HOT pruning which we definitely can't do safely in |
| 247 |
* a database to which we're not even connected. |
| 248 |
*/ |
| 249 |
static List * |
| 250 |
ScanSourceDatabasePgClass(Oid tbid, Oid dbid, char *srcpath) |
| 251 |
{ |
| 252 |
RelFileLocator rlocator; |
| 253 |
BlockNumber nblocks; |
| 254 |
BlockNumber blkno; |
| 255 |
Buffer buf; |
| 256 |
RelFileNumber relfilenumber; |
| 257 |
Page page; |
| 258 |
List *rlocatorlist = NIL; |
| 259 |
LockRelId relid; |
| 260 |
Snapshot snapshot; |
| 261 |
SMgrRelation smgr; |
| 262 |
BufferAccessStrategy bstrategy; |
| 263 |
|
| 264 |
/* Get pg_class relfilenumber. */ |
| 265 |
relfilenumber = RelationMapOidToFilenumberForDatabase(srcpath, |
| 266 |
RelationRelationId); |
| 267 |
|
| 268 |
/* Don't read data into shared_buffers without holding a relation lock. */ |
| 269 |
relid.dbId = dbid; |
| 270 |
relid.relId = RelationRelationId; |
| 271 |
LockRelationId(&relid, AccessShareLock); |
| 272 |
|
| 273 |
/* Prepare a RelFileLocator for the pg_class relation. */ |
| 274 |
rlocator.spcOid = tbid; |
| 275 |
rlocator.dbOid = dbid; |
| 276 |
rlocator.relNumber = relfilenumber; |
| 277 |
|
| 278 |
smgr = smgropen(rlocator, INVALID_PROC_NUMBER); |
| 279 |
nblocks = smgrnblocks(smgr, MAIN_FORKNUM); |
| 280 |
smgrclose(smgr); |
| 281 |
|
| 282 |
/* Use a buffer access strategy since this is a bulk read operation. */ |
| 283 |
bstrategy = GetAccessStrategy(BAS_BULKREAD); |
| 284 |
|
| 285 |
/* |
| 286 |
* As explained in the function header comments, we need a snapshot that |
| 287 |
* will see all committed transactions as committed, and our transaction |
| 288 |
* snapshot - or the active snapshot - might not be new enough for that, |
| 289 |
* but the return value of GetLatestSnapshot() should work fine. |
| 290 |
*/ |
| 291 |
snapshot = RegisterSnapshot(GetLatestSnapshot()); |
| 292 |
|
| 293 |
/* Process the relation block by block. */ |
| 294 |
for (blkno = 0; blkno < nblocks; blkno++) |
| 295 |
{ |
| 296 |
CHECK_FOR_INTERRUPTS(); |
| 297 |
|
| 298 |
buf = ReadBufferWithoutRelcache(rlocator, MAIN_FORKNUM, blkno, |
| 299 |
RBM_NORMAL, bstrategy, true); |
| 300 |
|
| 301 |
LockBuffer(buf, BUFFER_LOCK_SHARE); |
| 302 |
page = BufferGetPage(buf); |
| 303 |
if (PageIsNew(page) || PageIsEmpty(page)) |
| 304 |
{ |
| 305 |
UnlockReleaseBuffer(buf); |
| 306 |
continue; |
| 307 |
} |
| 308 |
|
| 309 |
/* Append relevant pg_class tuples for current page to rlocatorlist. */ |
| 310 |
rlocatorlist = ScanSourceDatabasePgClassPage(page, buf, tbid, dbid, |
| 311 |
srcpath, rlocatorlist, |
| 312 |
snapshot); |
| 313 |
|
| 314 |
UnlockReleaseBuffer(buf); |
| 315 |
} |
| 316 |
UnregisterSnapshot(snapshot); |
| 317 |
|
| 318 |
/* Release relation lock. */ |
| 319 |
UnlockRelationId(&relid, AccessShareLock); |
| 320 |
|
| 321 |
return rlocatorlist; |
| 322 |
} |
| 323 |
|
| 324 |
/* |
| 325 |
* Scan one page of the source database's pg_class relation and add relevant |
| 326 |
* entries to rlocatorlist. The return value is the updated list. |
| 327 |
*/ |
| 328 |
static List * |
| 329 |
ScanSourceDatabasePgClassPage(Page page, Buffer buf, Oid tbid, Oid dbid, |
| 330 |
char *srcpath, List *rlocatorlist, |
| 331 |
Snapshot snapshot) |
| 332 |
{ |
| 333 |
BlockNumber blkno = BufferGetBlockNumber(buf); |
| 334 |
OffsetNumber offnum; |
| 335 |
OffsetNumber maxoff; |
| 336 |
HeapTupleData tuple; |
| 337 |
|
| 338 |
maxoff = PageGetMaxOffsetNumber(page); |
| 339 |
|
| 340 |
/* Loop over offsets. */ |
| 341 |
for (offnum = FirstOffsetNumber; |
| 342 |
offnum <= maxoff; |
| 343 |
offnum = OffsetNumberNext(offnum)) |
| 344 |
{ |
| 345 |
ItemId itemid; |
| 346 |
|
| 347 |
itemid = PageGetItemId(page, offnum); |
| 348 |
|
| 349 |
/* Nothing to do if slot is empty or already dead. */ |
| 350 |
if (!ItemIdIsUsed(itemid) || ItemIdIsDead(itemid) || |
| 351 |
ItemIdIsRedirected(itemid)) |
| 352 |
continue; |
| 353 |
|
| 354 |
Assert(ItemIdIsNormal(itemid)); |
| 355 |
ItemPointerSet(&(tuple.t_self), blkno, offnum); |
| 356 |
|
| 357 |
/* Initialize a HeapTupleData structure. */ |
| 358 |
tuple.t_data = (HeapTupleHeader) PageGetItem(page, itemid); |
| 359 |
tuple.t_len = ItemIdGetLength(itemid); |
| 360 |
tuple.t_tableOid = RelationRelationId; |
| 361 |
|
| 362 |
/* Skip tuples that are not visible to this snapshot. */ |
| 363 |
if (HeapTupleSatisfiesVisibility(&tuple, snapshot, buf)) |
| 364 |
{ |
| 365 |
CreateDBRelInfo *relinfo; |
| 366 |
|
| 367 |
/* |
| 368 |
* ScanSourceDatabasePgClassTuple is in charge of constructing a |
| 369 |
* CreateDBRelInfo object for this tuple, but can also decide that |
| 370 |
* this tuple isn't something we need to copy. If we do need to |
| 371 |
* copy the relation, add it to the list. |
| 372 |
*/ |
| 373 |
relinfo = ScanSourceDatabasePgClassTuple(&tuple, tbid, dbid, |
| 374 |
srcpath); |
| 375 |
if (relinfo != NULL) |
| 376 |
rlocatorlist = lappend(rlocatorlist, relinfo); |
| 377 |
} |
| 378 |
} |
| 379 |
|
| 380 |
return rlocatorlist; |
| 381 |
} |
| 382 |
|
| 383 |
/* |
| 384 |
* Decide whether a certain pg_class tuple represents something that |
| 385 |
* needs to be copied from the source database to the destination database, |
| 386 |
* and if so, construct a CreateDBRelInfo for it. |
| 387 |
* |
| 388 |
* Visibility checks are handled by the caller, so our job here is just |
| 389 |
* to assess the data stored in the tuple. |
| 390 |
*/ |
| 391 |
CreateDBRelInfo * |
| 392 |
ScanSourceDatabasePgClassTuple(HeapTupleData *tuple, Oid tbid, Oid dbid, |
| 393 |
char *srcpath) |
| 394 |
{ |
| 395 |
CreateDBRelInfo *relinfo; |
| 396 |
Form_pg_class classForm; |
| 397 |
RelFileNumber relfilenumber = InvalidRelFileNumber; |
| 398 |
|
| 399 |
classForm = (Form_pg_class) GETSTRUCT(tuple); |
| 400 |
|
| 401 |
/* |
| 402 |
* Return NULL if this object does not need to be copied. |
| 403 |
* |
| 404 |
* Shared objects don't need to be copied, because they are shared. |
| 405 |
* Objects without storage can't be copied, because there's nothing to |
| 406 |
* copy. Temporary relations don't need to be copied either, because they |
| 407 |
* are inaccessible outside of the session that created them, which must |
| 408 |
* be gone already, and couldn't connect to a different database if it |
| 409 |
* still existed. autovacuum will eventually remove the pg_class entries |
| 410 |
* as well. |
| 411 |
*/ |
| 412 |
if (classForm->reltablespace == GLOBALTABLESPACE_OID || |
| 413 |
!RELKIND_HAS_STORAGE(classForm->relkind) || |
| 414 |
classForm->relpersistence == RELPERSISTENCE_TEMP) |
| 415 |
return NULL; |
| 416 |
|
| 417 |
/* |
| 418 |
* If relfilenumber is valid then directly use it. Otherwise, consult the |
| 419 |
* relmap. |
| 420 |
*/ |
| 421 |
if (RelFileNumberIsValid(classForm->relfilenode)) |
| 422 |
relfilenumber = classForm->relfilenode; |
| 423 |
else |
| 424 |
relfilenumber = RelationMapOidToFilenumberForDatabase(srcpath, |
| 425 |
classForm->oid); |
| 426 |
|
| 427 |
/* We must have a valid relfilenumber. */ |
| 428 |
if (!RelFileNumberIsValid(relfilenumber)) |
| 429 |
elog(ERROR, "relation with OID %u does not have a valid relfilenumber", |
| 430 |
classForm->oid); |
| 431 |
|
| 432 |
/* Prepare a rel info element and add it to the list. */ |
| 433 |
relinfo = (CreateDBRelInfo *) palloc(sizeof(CreateDBRelInfo)); |
| 434 |
if (OidIsValid(classForm->reltablespace)) |
| 435 |
relinfo->rlocator.spcOid = classForm->reltablespace; |
| 436 |
else |
| 437 |
relinfo->rlocator.spcOid = tbid; |
| 438 |
|
| 439 |
relinfo->rlocator.dbOid = dbid; |
| 440 |
relinfo->rlocator.relNumber = relfilenumber; |
| 441 |
relinfo->reloid = classForm->oid; |
| 442 |
|
| 443 |
/* Temporary relations were rejected above. */ |
| 444 |
Assert(classForm->relpersistence != RELPERSISTENCE_TEMP); |
| 445 |
relinfo->permanent = |
| 446 |
(classForm->relpersistence == RELPERSISTENCE_PERMANENT) ? true : false; |
| 447 |
|
| 448 |
return relinfo; |
| 449 |
} |
| 450 |
|
| 451 |
/* |
| 452 |
* Create database directory and write out the PG_VERSION file in the database |
| 453 |
* path. If isRedo is true, it's okay for the database directory to exist |
| 454 |
* already. |
| 455 |
*/ |
| 456 |
static void |
| 457 |
CreateDirAndVersionFile(char *dbpath, Oid dbid, Oid tsid, bool isRedo) |
| 458 |
{ |
| 459 |
int fd; |
| 460 |
int nbytes; |
| 461 |
char versionfile[MAXPGPATH]; |
| 462 |
char buf[16]; |
| 463 |
|
| 464 |
/* |
| 465 |
* Note that we don't have to copy version data from the source database; |
| 466 |
* there's only one legal value. |
| 467 |
*/ |
| 468 |
sprintf(buf, "%s\n", PG_MAJORVERSION); |
| 469 |
nbytes = strlen(PG_MAJORVERSION) + 1; |
| 470 |
|
| 471 |
/* Create database directory. */ |
| 472 |
if (MakePGDirectory(dbpath) < 0) |
| 473 |
{ |
| 474 |
/* Failure other than already exists or not in WAL replay? */ |
| 475 |
if (errno != EEXIST || !isRedo) |
| 476 |
ereport(ERROR, |
| 477 |
(errcode_for_file_access(), |
| 478 |
errmsg("could not create directory \"%s\": %m", dbpath))); |
| 479 |
} |
| 480 |
|
| 481 |
/* |
| 482 |
* Create PG_VERSION file in the database path. If the file already |
| 483 |
* exists and we are in WAL replay then try again to open it in write |
| 484 |
* mode. |
| 485 |
*/ |
| 486 |
snprintf(versionfile, sizeof(versionfile), "%s/%s", dbpath, "PG_VERSION"); |
| 487 |
|
| 488 |
fd = OpenTransientFile(versionfile, O_WRONLY | O_CREAT | O_EXCL | PG_BINARY); |
| 489 |
if (fd < 0 && errno == EEXIST && isRedo) |
| 490 |
fd = OpenTransientFile(versionfile, O_WRONLY | O_TRUNC | PG_BINARY); |
| 491 |
|
| 492 |
if (fd < 0) |
| 493 |
ereport(ERROR, |
| 494 |
(errcode_for_file_access(), |
| 495 |
errmsg("could not create file \"%s\": %m", versionfile))); |
| 496 |
|
| 497 |
/* Write PG_MAJORVERSION in the PG_VERSION file. */ |
| 498 |
pgstat_report_wait_start(WAIT_EVENT_VERSION_FILE_WRITE); |
| 499 |
errno = 0; |
| 500 |
if ((int) write(fd, buf, nbytes) != nbytes) |
| 501 |
{ |
| 502 |
/* If write didn't set errno, assume problem is no disk space. */ |
| 503 |
if (errno == 0) |
| 504 |
errno = ENOSPC; |
| 505 |
ereport(ERROR, |
| 506 |
(errcode_for_file_access(), |
| 507 |
errmsg("could not write to file \"%s\": %m", versionfile))); |
| 508 |
} |
| 509 |
pgstat_report_wait_end(); |
| 510 |
|
| 511 |
pgstat_report_wait_start(WAIT_EVENT_VERSION_FILE_SYNC); |
| 512 |
if (pg_fsync(fd) != 0) |
| 513 |
ereport(data_sync_elevel(ERROR), |
| 514 |
(errcode_for_file_access(), |
| 515 |
errmsg("could not fsync file \"%s\": %m", versionfile))); |
| 516 |
fsync_fname(dbpath, true); |
| 517 |
pgstat_report_wait_end(); |
| 518 |
|
| 519 |
/* Close the version file. */ |
| 520 |
CloseTransientFile(fd); |
| 521 |
|
| 522 |
/* If we are not in WAL replay then write the WAL. */ |
| 523 |
if (!isRedo) |
| 524 |
{ |
| 525 |
xl_dbase_create_wal_log_rec xlrec; |
| 526 |
|
| 527 |
START_CRIT_SECTION(); |
| 528 |
|
| 529 |
xlrec.db_id = dbid; |
| 530 |
xlrec.tablespace_id = tsid; |
| 531 |
|
| 532 |
XLogBeginInsert(); |
| 533 |
XLogRegisterData(&xlrec, |
| 534 |
sizeof(xl_dbase_create_wal_log_rec)); |
| 535 |
|
| 536 |
(void) XLogInsert(RM_DBASE_ID, XLOG_DBASE_CREATE_WAL_LOG); |
| 537 |
|
| 538 |
END_CRIT_SECTION(); |
| 539 |
} |
| 540 |
} |
| 541 |
|
| 542 |
/* |
| 543 |
* Create a new database using the FILE_COPY strategy. |
| 544 |
* |
| 545 |
* Copy each tablespace at the filesystem level, and log a single WAL record |
| 546 |
* for each tablespace copied. This requires a checkpoint before and after the |
| 547 |
* copy, which may be expensive, but it does greatly reduce WAL generation |
| 548 |
* if the copied database is large. |
| 549 |
*/ |
| 550 |
static void |
| 551 |
CreateDatabaseUsingFileCopy(Oid src_dboid, Oid dst_dboid, Oid src_tsid, |
| 552 |
Oid dst_tsid) |
| 553 |
{ |
| 554 |
TableScanDesc scan; |
| 555 |
Relation rel; |
| 556 |
HeapTuple tuple; |
| 557 |
|
| 558 |
/* |
| 559 |
* Force a checkpoint before starting the copy. This will force all dirty |
| 560 |
* buffers, including those of unlogged tables, out to disk, to ensure |
| 561 |
* source database is up-to-date on disk for the copy. |
| 562 |
* FlushDatabaseBuffers() would suffice for that, but we also want to |
| 563 |
* process any pending unlink requests. Otherwise, if a checkpoint |
| 564 |
* happened while we're copying files, a file might be deleted just when |
| 565 |
* we're about to copy it, causing the lstat() call in copydir() to fail |
| 566 |
* with ENOENT. |
| 567 |
* |
| 568 |
* In binary upgrade mode, we can skip this checkpoint because pg_upgrade |
| 569 |
* is careful to ensure that template0 is fully written to disk prior to |
| 570 |
* any CREATE DATABASE commands. |
| 571 |
*/ |
| 572 |
if (!IsBinaryUpgrade) |
| 573 |
RequestCheckpoint(CHECKPOINT_IMMEDIATE | CHECKPOINT_FORCE | |
| 574 |
CHECKPOINT_WAIT | CHECKPOINT_FLUSH_ALL); |
| 575 |
|
| 576 |
/* |
| 577 |
* Iterate through all tablespaces of the template database, and copy each |
| 578 |
* one to the new database. |
| 579 |
*/ |
| 580 |
rel = table_open(TableSpaceRelationId, AccessShareLock); |
| 581 |
scan = table_beginscan_catalog(rel, 0, NULL); |
| 582 |
while ((tuple = heap_getnext(scan, ForwardScanDirection)) != NULL) |
| 583 |
{ |
| 584 |
Form_pg_tablespace spaceform = (Form_pg_tablespace) GETSTRUCT(tuple); |
| 585 |
Oid srctablespace = spaceform->oid; |
| 586 |
Oid dsttablespace; |
| 587 |
char *srcpath; |
| 588 |
char *dstpath; |
| 589 |
struct stat st; |
| 590 |
|
| 591 |
/* No need to copy global tablespace */ |
| 592 |
if (srctablespace == GLOBALTABLESPACE_OID) |
| 593 |
continue; |
| 594 |
|
| 595 |
srcpath = GetDatabasePath(src_dboid, srctablespace); |
| 596 |
|
| 597 |
if (stat(srcpath, &st) < 0 || !S_ISDIR(st.st_mode) || |
| 598 |
directory_is_empty(srcpath)) |
| 599 |
{ |
| 600 |
/* Assume we can ignore it */ |
| 601 |
pfree(srcpath); |
| 602 |
continue; |
| 603 |
} |
| 604 |
|
| 605 |
if (srctablespace == src_tsid) |
| 606 |
dsttablespace = dst_tsid; |
| 607 |
else |
| 608 |
dsttablespace = srctablespace; |
| 609 |
|
| 610 |
dstpath = GetDatabasePath(dst_dboid, dsttablespace); |
| 611 |
|
| 612 |
/* |
| 613 |
* Copy this subdirectory to the new location |
| 614 |
* |
| 615 |
* We don't need to copy subdirectories |
| 616 |
*/ |
| 617 |
copydir(srcpath, dstpath, false); |
| 618 |
|
| 619 |
/* Record the filesystem change in XLOG */ |
| 620 |
{ |
| 621 |
xl_dbase_create_file_copy_rec xlrec; |
| 622 |
|
| 623 |
xlrec.db_id = dst_dboid; |
| 624 |
xlrec.tablespace_id = dsttablespace; |
| 625 |
xlrec.src_db_id = src_dboid; |
| 626 |
xlrec.src_tablespace_id = srctablespace; |
| 627 |
|
| 628 |
XLogBeginInsert(); |
| 629 |
XLogRegisterData(&xlrec, |
| 630 |
sizeof(xl_dbase_create_file_copy_rec)); |
| 631 |
|
| 632 |
(void) XLogInsert(RM_DBASE_ID, |
| 633 |
XLOG_DBASE_CREATE_FILE_COPY | XLR_SPECIAL_REL_UPDATE); |
| 634 |
} |
| 635 |
pfree(srcpath); |
| 636 |
pfree(dstpath); |
| 637 |
} |
| 638 |
table_endscan(scan); |
| 639 |
table_close(rel, AccessShareLock); |
| 640 |
|
| 641 |
/* |
| 642 |
* We force a checkpoint before committing. This effectively means that |
| 643 |
* committed XLOG_DBASE_CREATE_FILE_COPY operations will never need to be |
| 644 |
* replayed (at least not in ordinary crash recovery; we still have to |
| 645 |
* make the XLOG entry for the benefit of PITR operations). This avoids |
| 646 |
* two nasty scenarios: |
| 647 |
* |
| 648 |
* #1: At wal_level=minimal, we don't XLOG the contents of newly created |
| 649 |
* relfilenodes; therefore the drop-and-recreate-whole-directory behavior |
| 650 |
* of DBASE_CREATE replay would lose such files created in the new |
| 651 |
* database between our commit and the next checkpoint. |
| 652 |
* |
| 653 |
* #2: Since we have to recopy the source database during DBASE_CREATE |
| 654 |
* replay, we run the risk of copying changes in it that were committed |
| 655 |
* after the original CREATE DATABASE command but before the system crash |
| 656 |
* that led to the replay. This is at least unexpected and at worst could |
| 657 |
* lead to inconsistencies, eg duplicate table names. |
| 658 |
* |
| 659 |
* (Both of these were real bugs in releases 8.0 through 8.0.3.) |
| 660 |
* |
| 661 |
* In PITR replay, the first of these isn't an issue, and the second is |
| 662 |
* only a risk if the CREATE DATABASE and subsequent template database |
| 663 |
* change both occur while a base backup is being taken. There doesn't |
| 664 |
* seem to be much we can do about that except document it as a |
| 665 |
* limitation. |
| 666 |
* |
| 667 |
* In binary upgrade mode, we can skip this checkpoint because neither of |
| 668 |
* these problems applies: we don't ever replay the WAL generated during |
| 669 |
* pg_upgrade, and we don't support taking base backups during pg_upgrade |
| 670 |
* (not to mention that we don't concurrently modify template0, either). |
| 671 |
* |
| 672 |
* See CreateDatabaseUsingWalLog() for a less cheesy CREATE DATABASE |
| 673 |
* strategy that avoids these problems. |
| 674 |
*/ |
| 675 |
if (!IsBinaryUpgrade) |
| 676 |
RequestCheckpoint(CHECKPOINT_IMMEDIATE | CHECKPOINT_FORCE | |
| 677 |
CHECKPOINT_WAIT); |
| 678 |
} |
| 679 |
|
| 680 |
/* |
| 681 |
* CREATE DATABASE |
| 682 |
*/ |
| 683 |
Oid |
| 684 |
createdb(ParseState *pstate, const CreatedbStmt *stmt) |
| 685 |
{ |
| 686 |
Oid src_dboid; |
| 687 |
Oid src_owner; |
| 688 |
int src_encoding = -1; |
| 689 |
char *src_collate = NULL; |
| 690 |
char *src_ctype = NULL; |
| 691 |
char *src_locale = NULL; |
| 692 |
char *src_icurules = NULL; |
| 693 |
char src_locprovider = '\0'; |
| 694 |
char *src_collversion = NULL; |
| 695 |
bool src_istemplate; |
| 696 |
bool src_hasloginevt = false; |
| 697 |
bool src_allowconn; |
| 698 |
TransactionId src_frozenxid = InvalidTransactionId; |
| 699 |
MultiXactId src_minmxid = InvalidMultiXactId; |
| 700 |
Oid src_deftablespace; |
| 701 |
volatile Oid dst_deftablespace; |
| 702 |
Relation pg_database_rel; |
| 703 |
HeapTuple tuple; |
| 704 |
Datum new_record[Natts_pg_database] = {0}; |
| 705 |
bool new_record_nulls[Natts_pg_database] = {0}; |
| 706 |
Oid dboid = InvalidOid; |
| 707 |
Oid datdba; |
| 708 |
ListCell *option; |
| 709 |
DefElem *tablespacenameEl = NULL; |
| 710 |
DefElem *ownerEl = NULL; |
| 711 |
DefElem *templateEl = NULL; |
| 712 |
DefElem *encodingEl = NULL; |
| 713 |
DefElem *localeEl = NULL; |
| 714 |
DefElem *builtinlocaleEl = NULL; |
| 715 |
DefElem *collateEl = NULL; |
| 716 |
DefElem *ctypeEl = NULL; |
| 717 |
DefElem *iculocaleEl = NULL; |
| 718 |
DefElem *icurulesEl = NULL; |
| 719 |
DefElem *locproviderEl = NULL; |
| 720 |
DefElem *istemplateEl = NULL; |
| 721 |
DefElem *allowconnectionsEl = NULL; |
| 722 |
DefElem *connlimitEl = NULL; |
| 723 |
DefElem *collversionEl = NULL; |
| 724 |
DefElem *strategyEl = NULL; |
| 725 |
char *dbname = stmt->dbname; |
| 726 |
char *dbowner = NULL; |
| 727 |
const char *dbtemplate = NULL; |
| 728 |
char *dbcollate = NULL; |
| 729 |
char *dbctype = NULL; |
| 730 |
const char *dblocale = NULL; |
| 731 |
char *dbicurules = NULL; |
| 732 |
char dblocprovider = '\0'; |
| 733 |
char *canonname; |
| 734 |
int encoding = -1; |
| 735 |
bool dbistemplate = false; |
| 736 |
bool dballowconnections = true; |
| 737 |
int dbconnlimit = DATCONNLIMIT_UNLIMITED; |
| 738 |
char *dbcollversion = NULL; |
| 739 |
int notherbackends; |
| 740 |
int npreparedxacts; |
| 741 |
CreateDBStrategy dbstrategy = CREATEDB_WAL_LOG; |
| 742 |
createdb_failure_params fparms; |
| 743 |
|
| 744 |
/* Extract options from the statement node tree */ |
| 745 |
foreach(option, stmt->options) |
| 746 |
{ |
| 747 |
DefElem *defel = (DefElem *) lfirst(option); |
| 748 |
|
| 749 |
if (strcmp(defel->defname, "tablespace") == 0) |
| 750 |
{ |
| 751 |
if (tablespacenameEl) |
| 752 |
errorConflictingDefElem(defel, pstate); |
| 753 |
tablespacenameEl = defel; |
| 754 |
} |
| 755 |
else if (strcmp(defel->defname, "owner") == 0) |
| 756 |
{ |
| 757 |
if (ownerEl) |
| 758 |
errorConflictingDefElem(defel, pstate); |
| 759 |
ownerEl = defel; |
| 760 |
} |
| 761 |
else if (strcmp(defel->defname, "template") == 0) |
| 762 |
{ |
| 763 |
if (templateEl) |
| 764 |
errorConflictingDefElem(defel, pstate); |
| 765 |
templateEl = defel; |
| 766 |
} |
| 767 |
else if (strcmp(defel->defname, "encoding") == 0) |
| 768 |
{ |
| 769 |
if (encodingEl) |
| 770 |
errorConflictingDefElem(defel, pstate); |
| 771 |
encodingEl = defel; |
| 772 |
} |
| 773 |
else if (strcmp(defel->defname, "locale") == 0) |
| 774 |
{ |
| 775 |
if (localeEl) |
| 776 |
errorConflictingDefElem(defel, pstate); |
| 777 |
localeEl = defel; |
| 778 |
} |
| 779 |
else if (strcmp(defel->defname, "builtin_locale") == 0) |
| 780 |
{ |
| 781 |
if (builtinlocaleEl) |
| 782 |
errorConflictingDefElem(defel, pstate); |
| 783 |
builtinlocaleEl = defel; |
| 784 |
} |
| 785 |
else if (strcmp(defel->defname, "lc_collate") == 0) |
| 786 |
{ |
| 787 |
if (collateEl) |
| 788 |
errorConflictingDefElem(defel, pstate); |
| 789 |
collateEl = defel; |
| 790 |
} |
| 791 |
else if (strcmp(defel->defname, "lc_ctype") == 0) |
| 792 |
{ |
| 793 |
if (ctypeEl) |
| 794 |
errorConflictingDefElem(defel, pstate); |
| 795 |
ctypeEl = defel; |
| 796 |
} |
| 797 |
else if (strcmp(defel->defname, "icu_locale") == 0) |
| 798 |
{ |
| 799 |
if (iculocaleEl) |
| 800 |
errorConflictingDefElem(defel, pstate); |
| 801 |
iculocaleEl = defel; |
| 802 |
} |
| 803 |
else if (strcmp(defel->defname, "icu_rules") == 0) |
| 804 |
{ |
| 805 |
if (icurulesEl) |
| 806 |
errorConflictingDefElem(defel, pstate); |
| 807 |
icurulesEl = defel; |
| 808 |
} |
| 809 |
else if (strcmp(defel->defname, "locale_provider") == 0) |
| 810 |
{ |
| 811 |
if (locproviderEl) |
| 812 |
errorConflictingDefElem(defel, pstate); |
| 813 |
locproviderEl = defel; |
| 814 |
} |
| 815 |
else if (strcmp(defel->defname, "is_template") == 0) |
| 816 |
{ |
| 817 |
if (istemplateEl) |
| 818 |
errorConflictingDefElem(defel, pstate); |
| 819 |
istemplateEl = defel; |
| 820 |
} |
| 821 |
else if (strcmp(defel->defname, "allow_connections") == 0) |
| 822 |
{ |
| 823 |
if (allowconnectionsEl) |
| 824 |
errorConflictingDefElem(defel, pstate); |
| 825 |
allowconnectionsEl = defel; |
| 826 |
} |
| 827 |
else if (strcmp(defel->defname, "connection_limit") == 0) |
| 828 |
{ |
| 829 |
if (connlimitEl) |
| 830 |
errorConflictingDefElem(defel, pstate); |
| 831 |
connlimitEl = defel; |
| 832 |
} |
| 833 |
else if (strcmp(defel->defname, "collation_version") == 0) |
| 834 |
{ |
| 835 |
if (collversionEl) |
| 836 |
errorConflictingDefElem(defel, pstate); |
| 837 |
collversionEl = defel; |
| 838 |
} |
| 839 |
else if (strcmp(defel->defname, "location") == 0) |
| 840 |
{ |
| 841 |
ereport(WARNING, |
| 842 |
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED), |
| 843 |
errmsg("LOCATION is not supported anymore"), |
| 844 |
errhint("Consider using tablespaces instead."), |
| 845 |
parser_errposition(pstate, defel->location))); |
| 846 |
} |
| 847 |
else if (strcmp(defel->defname, "oid") == 0) |
| 848 |
{ |
| 849 |
dboid = defGetObjectId(defel); |
| 850 |
|
| 851 |
/* |
| 852 |
* We don't normally permit new databases to be created with |
| 853 |
* system-assigned OIDs. pg_upgrade tries to preserve database |
| 854 |
* OIDs, so we can't allow any database to be created with an OID |
| 855 |
* that might be in use in a freshly-initialized cluster created |
| 856 |
* by some future version. We assume all such OIDs will be from |
| 857 |
* the system-managed OID range. |
| 858 |
* |
| 859 |
* As an exception, however, we permit any OID to be assigned when |
| 860 |
* allow_system_table_mods=on (so that initdb can assign system |
| 861 |
* OIDs to template0 and postgres) or when performing a binary |
| 862 |
* upgrade (so that pg_upgrade can preserve whatever OIDs it finds |
| 863 |
* in the source cluster). |
| 864 |
*/ |
| 865 |
if (dboid < FirstNormalObjectId && |
| 866 |
!allowSystemTableMods && !IsBinaryUpgrade) |
| 867 |
ereport(ERROR, |
| 868 |
(errcode(ERRCODE_INVALID_PARAMETER_VALUE)), |
| 869 |
errmsg("OIDs less than %u are reserved for system objects", FirstNormalObjectId)); |
| 870 |
} |
| 871 |
else if (strcmp(defel->defname, "strategy") == 0) |
| 872 |
{ |
| 873 |
if (strategyEl) |
| 874 |
errorConflictingDefElem(defel, pstate); |
| 875 |
strategyEl = defel; |
| 876 |
} |
| 877 |
else |
| 878 |
ereport(ERROR, |
| 879 |
(errcode(ERRCODE_SYNTAX_ERROR), |
| 880 |
errmsg("option \"%s\" not recognized", defel->defname), |
| 881 |
parser_errposition(pstate, defel->location))); |
| 882 |
} |
| 883 |
|
| 884 |
if (ownerEl && ownerEl->arg) |
| 885 |
dbowner = defGetString(ownerEl); |
| 886 |
if (templateEl && templateEl->arg) |
| 887 |
dbtemplate = defGetString(templateEl); |
| 888 |
if (encodingEl && encodingEl->arg) |
| 889 |
{ |
| 890 |
const char *encoding_name; |
| 891 |
|
| 892 |
if (IsA(encodingEl->arg, Integer)) |
| 893 |
{ |
| 894 |
encoding = defGetInt32(encodingEl); |
| 895 |
encoding_name = pg_encoding_to_char(encoding); |
| 896 |
if (strcmp(encoding_name, "") == 0 || |
| 897 |
pg_valid_server_encoding(encoding_name) < 0) |
| 898 |
ereport(ERROR, |
| 899 |
(errcode(ERRCODE_UNDEFINED_OBJECT), |
| 900 |
errmsg("%d is not a valid encoding code", |
| 901 |
encoding), |
| 902 |
parser_errposition(pstate, encodingEl->location))); |
| 903 |
} |
| 904 |
else |
| 905 |
{ |
| 906 |
encoding_name = defGetString(encodingEl); |
| 907 |
encoding = pg_valid_server_encoding(encoding_name); |
| 908 |
if (encoding < 0) |
| 909 |
ereport(ERROR, |
| 910 |
(errcode(ERRCODE_UNDEFINED_OBJECT), |
| 911 |
errmsg("%s is not a valid encoding name", |
| 912 |
encoding_name), |
| 913 |
parser_errposition(pstate, encodingEl->location))); |
| 914 |
} |
| 915 |
} |
| 916 |
if (localeEl && localeEl->arg) |
| 917 |
{ |
| 918 |
dbcollate = defGetString(localeEl); |
| 919 |
dbctype = defGetString(localeEl); |
| 920 |
dblocale = defGetString(localeEl); |
| 921 |
} |
| 922 |
if (builtinlocaleEl && builtinlocaleEl->arg) |
| 923 |
dblocale = defGetString(builtinlocaleEl); |
| 924 |
if (collateEl && collateEl->arg) |
| 925 |
dbcollate = defGetString(collateEl); |
| 926 |
if (ctypeEl && ctypeEl->arg) |
| 927 |
dbctype = defGetString(ctypeEl); |
| 928 |
if (iculocaleEl && iculocaleEl->arg) |
| 929 |
dblocale = defGetString(iculocaleEl); |
| 930 |
if (icurulesEl && icurulesEl->arg) |
| 931 |
dbicurules = defGetString(icurulesEl); |
| 932 |
if (locproviderEl && locproviderEl->arg) |
| 933 |
{ |
| 934 |
char *locproviderstr = defGetString(locproviderEl); |
| 935 |
|
| 936 |
if (pg_strcasecmp(locproviderstr, "builtin") == 0) |
| 937 |
dblocprovider = COLLPROVIDER_BUILTIN; |
| 938 |
else if (pg_strcasecmp(locproviderstr, "icu") == 0) |
| 939 |
dblocprovider = COLLPROVIDER_ICU; |
| 940 |
else if (pg_strcasecmp(locproviderstr, "libc") == 0) |
| 941 |
dblocprovider = COLLPROVIDER_LIBC; |
| 942 |
else |
| 943 |
ereport(ERROR, |
| 944 |
(errcode(ERRCODE_INVALID_OBJECT_DEFINITION), |
| 945 |
errmsg("unrecognized locale provider: %s", |
| 946 |
locproviderstr))); |
| 947 |
} |
| 948 |
if (istemplateEl && istemplateEl->arg) |
| 949 |
dbistemplate = defGetBoolean(istemplateEl); |
| 950 |
if (allowconnectionsEl && allowconnectionsEl->arg) |
| 951 |
dballowconnections = defGetBoolean(allowconnectionsEl); |
| 952 |
if (connlimitEl && connlimitEl->arg) |
| 953 |
{ |
| 954 |
dbconnlimit = defGetInt32(connlimitEl); |
| 955 |
if (dbconnlimit < DATCONNLIMIT_UNLIMITED) |
| 956 |
ereport(ERROR, |
| 957 |
(errcode(ERRCODE_INVALID_PARAMETER_VALUE), |
| 958 |
errmsg("invalid connection limit: %d", dbconnlimit))); |
| 959 |
} |
| 960 |
if (collversionEl) |
| 961 |
dbcollversion = defGetString(collversionEl); |
| 962 |
|
| 963 |
/* obtain OID of proposed owner */ |
| 964 |
if (dbowner) |
| 965 |
datdba = get_role_oid(dbowner, false); |
| 966 |
else |
| 967 |
datdba = GetUserId(); |
| 968 |
|
| 969 |
/* |
| 970 |
* To create a database, must have createdb privilege and must be able to |
| 971 |
* become the target role (this does not imply that the target role itself |
| 972 |
* must have createdb privilege). The latter provision guards against |
| 973 |
* "giveaway" attacks. Note that a superuser will always have both of |
| 974 |
* these privileges a fortiori. |
| 975 |
*/ |
| 976 |
if (!have_createdb_privilege()) |
| 977 |
ereport(ERROR, |
| 978 |
(errcode(ERRCODE_INSUFFICIENT_PRIVILEGE), |
| 979 |
errmsg("permission denied to create database"))); |
| 980 |
|
| 981 |
check_can_set_role(GetUserId(), datdba); |
| 982 |
|
| 983 |
/* |
| 984 |
* Lookup database (template) to be cloned, and obtain share lock on it. |
| 985 |
* ShareLock allows two CREATE DATABASEs to work from the same template |
| 986 |
* concurrently, while ensuring no one is busy dropping it in parallel |
| 987 |
* (which would be Very Bad since we'd likely get an incomplete copy |
| 988 |
* without knowing it). This also prevents any new connections from being |
| 989 |
* made to the source until we finish copying it, so we can be sure it |
| 990 |
* won't change underneath us. |
| 991 |
*/ |
| 992 |
if (!dbtemplate) |
| 993 |
dbtemplate = "template1"; /* Default template database name */ |
| 994 |
|
| 995 |
if (!get_db_info(dbtemplate, ShareLock, |
| 996 |
&src_dboid, &src_owner, &src_encoding, |
| 997 |
&src_istemplate, &src_allowconn, &src_hasloginevt, |
| 998 |
&src_frozenxid, &src_minmxid, &src_deftablespace, |
| 999 |
&src_collate, &src_ctype, &src_locale, &src_icurules, &src_locprovider, |
| 1000 |
&src_collversion)) |
| 1001 |
ereport(ERROR, |
| 1002 |
(errcode(ERRCODE_UNDEFINED_DATABASE), |
| 1003 |
errmsg("template database \"%s\" does not exist", |
| 1004 |
dbtemplate))); |
| 1005 |
|
| 1006 |
/* |
| 1007 |
* If the source database was in the process of being dropped, we can't |
| 1008 |
* use it as a template. |
| 1009 |
*/ |
| 1010 |
if (database_is_invalid_oid(src_dboid)) |
| 1011 |
ereport(ERROR, |
| 1012 |
errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), |
| 1013 |
errmsg("cannot use invalid database \"%s\" as template", dbtemplate), |
| 1014 |
errhint("Use DROP DATABASE to drop invalid databases.")); |
| 1015 |
|
| 1016 |
/* |
| 1017 |
* Permission check: to copy a DB that's not marked datistemplate, you |
| 1018 |
* must be superuser or the owner thereof. |
| 1019 |
*/ |
| 1020 |
if (!src_istemplate) |
| 1021 |
{ |
| 1022 |
if (!object_ownercheck(DatabaseRelationId, src_dboid, GetUserId())) |
| 1023 |
ereport(ERROR, |
| 1024 |
(errcode(ERRCODE_INSUFFICIENT_PRIVILEGE), |
| 1025 |
errmsg("permission denied to copy database \"%s\"", |
| 1026 |
dbtemplate))); |
| 1027 |
} |
| 1028 |
|
| 1029 |
/* Validate the database creation strategy. */ |
| 1030 |
if (strategyEl && strategyEl->arg) |
| 1031 |
{ |
| 1032 |
char *strategy; |
| 1033 |
|
| 1034 |
strategy = defGetString(strategyEl); |
| 1035 |
if (pg_strcasecmp(strategy, "wal_log") == 0) |
| 1036 |
dbstrategy = CREATEDB_WAL_LOG; |
| 1037 |
else if (pg_strcasecmp(strategy, "file_copy") == 0) |
| 1038 |
dbstrategy = CREATEDB_FILE_COPY; |
| 1039 |
else |
| 1040 |
ereport(ERROR, |
| 1041 |
(errcode(ERRCODE_INVALID_PARAMETER_VALUE), |
| 1042 |
errmsg("invalid create database strategy \"%s\"", strategy), |
| 1043 |
errhint("Valid strategies are \"wal_log\" and \"file_copy\"."))); |
| 1044 |
} |
| 1045 |
|
| 1046 |
/* If encoding or locales are defaulted, use source's setting */ |
| 1047 |
if (encoding < 0) |
| 1048 |
encoding = src_encoding; |
| 1049 |
if (dbcollate == NULL) |
| 1050 |
dbcollate = src_collate; |
| 1051 |
if (dbctype == NULL) |
| 1052 |
dbctype = src_ctype; |
| 1053 |
if (dblocprovider == '\0') |
| 1054 |
dblocprovider = src_locprovider; |
| 1055 |
if (dblocale == NULL && dblocprovider == src_locprovider) |
| 1056 |
dblocale = src_locale; |
| 1057 |
if (dbicurules == NULL) |
| 1058 |
dbicurules = src_icurules; |
| 1059 |
|
| 1060 |
/* Some encodings are client only */ |
| 1061 |
if (!PG_VALID_BE_ENCODING(encoding)) |
| 1062 |
ereport(ERROR, |
| 1063 |
(errcode(ERRCODE_WRONG_OBJECT_TYPE), |
| 1064 |
errmsg("invalid server encoding %d", encoding))); |
| 1065 |
|
| 1066 |
/* Check that the chosen locales are valid, and get canonical spellings */ |
| 1067 |
if (!check_locale(LC_COLLATE, dbcollate, &canonname)) |
| 1068 |
{ |
| 1069 |
if (dblocprovider == COLLPROVIDER_BUILTIN) |
| 1070 |
ereport(ERROR, |
| 1071 |
(errcode(ERRCODE_WRONG_OBJECT_TYPE), |
| 1072 |
errmsg("invalid LC_COLLATE locale name: \"%s\"", dbcollate), |
| 1073 |
errhint("If the locale name is specific to the builtin provider, use BUILTIN_LOCALE."))); |
| 1074 |
else if (dblocprovider == COLLPROVIDER_ICU) |
| 1075 |
ereport(ERROR, |
| 1076 |
(errcode(ERRCODE_WRONG_OBJECT_TYPE), |
| 1077 |
errmsg("invalid LC_COLLATE locale name: \"%s\"", dbcollate), |
| 1078 |
errhint("If the locale name is specific to the ICU provider, use ICU_LOCALE."))); |
| 1079 |
else |
| 1080 |
ereport(ERROR, |
| 1081 |
(errcode(ERRCODE_WRONG_OBJECT_TYPE), |
| 1082 |
errmsg("invalid LC_COLLATE locale name: \"%s\"", dbcollate))); |
| 1083 |
} |
| 1084 |
dbcollate = canonname; |
| 1085 |
if (!check_locale(LC_CTYPE, dbctype, &canonname)) |
| 1086 |
{ |
| 1087 |
if (dblocprovider == COLLPROVIDER_BUILTIN) |
| 1088 |
ereport(ERROR, |
| 1089 |
(errcode(ERRCODE_WRONG_OBJECT_TYPE), |
| 1090 |
errmsg("invalid LC_CTYPE locale name: \"%s\"", dbctype), |
| 1091 |
errhint("If the locale name is specific to the builtin provider, use BUILTIN_LOCALE."))); |
| 1092 |
else if (dblocprovider == COLLPROVIDER_ICU) |
| 1093 |
ereport(ERROR, |
| 1094 |
(errcode(ERRCODE_WRONG_OBJECT_TYPE), |
| 1095 |
errmsg("invalid LC_CTYPE locale name: \"%s\"", dbctype), |
| 1096 |
errhint("If the locale name is specific to the ICU provider, use ICU_LOCALE."))); |
| 1097 |
else |
| 1098 |
ereport(ERROR, |
| 1099 |
(errcode(ERRCODE_WRONG_OBJECT_TYPE), |
| 1100 |
errmsg("invalid LC_CTYPE locale name: \"%s\"", dbctype))); |
| 1101 |
} |
| 1102 |
|
| 1103 |
dbctype = canonname; |
| 1104 |
|
| 1105 |
check_encoding_locale_matches(encoding, dbcollate, dbctype); |
| 1106 |
|
| 1107 |
/* validate provider-specific parameters */ |
| 1108 |
if (dblocprovider != COLLPROVIDER_BUILTIN) |
| 1109 |
{ |
| 1110 |
if (builtinlocaleEl) |
| 1111 |
ereport(ERROR, |
| 1112 |
(errcode(ERRCODE_INVALID_OBJECT_DEFINITION), |
| 1113 |
errmsg("BUILTIN_LOCALE cannot be specified unless locale provider is builtin"))); |
| 1114 |
} |
| 1115 |
|
| 1116 |
if (dblocprovider != COLLPROVIDER_ICU) |
| 1117 |
{ |
| 1118 |
if (iculocaleEl) |
| 1119 |
ereport(ERROR, |
| 1120 |
(errcode(ERRCODE_INVALID_OBJECT_DEFINITION), |
| 1121 |
errmsg("ICU locale cannot be specified unless locale provider is ICU"))); |
| 1122 |
|
| 1123 |
if (dbicurules) |
| 1124 |
ereport(ERROR, |
| 1125 |
(errcode(ERRCODE_INVALID_OBJECT_DEFINITION), |
| 1126 |
errmsg("ICU rules cannot be specified unless locale provider is ICU"))); |
| 1127 |
} |
| 1128 |
|
| 1129 |
/* validate and canonicalize locale for the provider */ |
| 1130 |
if (dblocprovider == COLLPROVIDER_BUILTIN) |
| 1131 |
{ |
| 1132 |
/* |
| 1133 |
* This would happen if template0 uses the libc provider but the new |
| 1134 |
* database uses builtin. |
| 1135 |
*/ |
| 1136 |
if (!dblocale) |
| 1137 |
ereport(ERROR, |
| 1138 |
(errcode(ERRCODE_INVALID_PARAMETER_VALUE), |
| 1139 |
errmsg("LOCALE or BUILTIN_LOCALE must be specified"))); |
| 1140 |
|
| 1141 |
dblocale = builtin_validate_locale(encoding, dblocale); |
| 1142 |
} |
| 1143 |
else if (dblocprovider == COLLPROVIDER_ICU) |
| 1144 |
{ |
| 1145 |
if (!(is_encoding_supported_by_icu(encoding))) |
| 1146 |
ereport(ERROR, |
| 1147 |
(errcode(ERRCODE_INVALID_PARAMETER_VALUE), |
| 1148 |
errmsg("encoding \"%s\" is not supported with ICU provider", |
| 1149 |
pg_encoding_to_char(encoding)))); |
| 1150 |
|
| 1151 |
/* |
| 1152 |
* This would happen if template0 uses the libc provider but the new |
| 1153 |
* database uses icu. |
| 1154 |
*/ |
| 1155 |
if (!dblocale) |
| 1156 |
ereport(ERROR, |
| 1157 |
(errcode(ERRCODE_INVALID_PARAMETER_VALUE), |
| 1158 |
errmsg("LOCALE or ICU_LOCALE must be specified"))); |
| 1159 |
|
| 1160 |
/* |
| 1161 |
* During binary upgrade, or when the locale came from the template |
| 1162 |
* database, preserve locale string. Otherwise, canonicalize to a |
| 1163 |
* language tag. |
| 1164 |
*/ |
| 1165 |
if (!IsBinaryUpgrade && dblocale != src_locale) |
| 1166 |
{ |
| 1167 |
char *langtag = icu_language_tag(dblocale, |
| 1168 |
icu_validation_level); |
| 1169 |
|
| 1170 |
if (langtag && strcmp(dblocale, langtag) != 0) |
| 1171 |
{ |
| 1172 |
ereport(NOTICE, |
| 1173 |
(errmsg("using standard form \"%s\" for ICU locale \"%s\"", |
| 1174 |
langtag, dblocale))); |
| 1175 |
|
| 1176 |
dblocale = langtag; |
| 1177 |
} |
| 1178 |
} |
| 1179 |
|
| 1180 |
icu_validate_locale(dblocale); |
| 1181 |
} |
| 1182 |
|
| 1183 |
/* for libc, locale comes from datcollate and datctype */ |
| 1184 |
if (dblocprovider == COLLPROVIDER_LIBC) |
| 1185 |
dblocale = NULL; |
| 1186 |
|
| 1187 |
/* |
| 1188 |
* Check that the new encoding and locale settings match the source |
| 1189 |
* database. We insist on this because we simply copy the source data --- |
| 1190 |
* any non-ASCII data would be wrongly encoded, and any indexes sorted |
| 1191 |
* according to the source locale would be wrong. |
| 1192 |
* |
| 1193 |
* However, we assume that template0 doesn't contain any non-ASCII data |
| 1194 |
* nor any indexes that depend on collation or ctype, so template0 can be |
| 1195 |
* used as template for creating a database with any encoding or locale. |
| 1196 |
*/ |
| 1197 |
if (strcmp(dbtemplate, "template0") != 0) |
| 1198 |
{ |
| 1199 |
if (encoding != src_encoding) |
| 1200 |
ereport(ERROR, |
| 1201 |
(errcode(ERRCODE_INVALID_PARAMETER_VALUE), |
| 1202 |
errmsg("new encoding (%s) is incompatible with the encoding of the template database (%s)", |
| 1203 |
pg_encoding_to_char(encoding), |
| 1204 |
pg_encoding_to_char(src_encoding)), |
| 1205 |
errhint("Use the same encoding as in the template database, or use template0 as template."))); |
| 1206 |
|
| 1207 |
if (strcmp(dbcollate, src_collate) != 0) |
| 1208 |
ereport(ERROR, |
| 1209 |
(errcode(ERRCODE_INVALID_PARAMETER_VALUE), |
| 1210 |
errmsg("new collation (%s) is incompatible with the collation of the template database (%s)", |
| 1211 |
dbcollate, src_collate), |
| 1212 |
errhint("Use the same collation as in the template database, or use template0 as template."))); |
| 1213 |
|
| 1214 |
if (strcmp(dbctype, src_ctype) != 0) |
| 1215 |
ereport(ERROR, |
| 1216 |
(errcode(ERRCODE_INVALID_PARAMETER_VALUE), |
| 1217 |
errmsg("new LC_CTYPE (%s) is incompatible with the LC_CTYPE of the template database (%s)", |
| 1218 |
dbctype, src_ctype), |
| 1219 |
errhint("Use the same LC_CTYPE as in the template database, or use template0 as template."))); |
| 1220 |
|
| 1221 |
if (dblocprovider != src_locprovider) |
| 1222 |
ereport(ERROR, |
| 1223 |
(errcode(ERRCODE_INVALID_PARAMETER_VALUE), |
| 1224 |
errmsg("new locale provider (%s) does not match locale provider of the template database (%s)", |
| 1225 |
collprovider_name(dblocprovider), collprovider_name(src_locprovider)), |
| 1226 |
errhint("Use the same locale provider as in the template database, or use template0 as template."))); |
| 1227 |
|
| 1228 |
if (dblocprovider == COLLPROVIDER_ICU) |
| 1229 |
{ |
| 1230 |
char *val1; |
| 1231 |
char *val2; |
| 1232 |
|
| 1233 |
Assert(dblocale); |
| 1234 |
Assert(src_locale); |
| 1235 |
if (strcmp(dblocale, src_locale) != 0) |
| 1236 |
ereport(ERROR, |
| 1237 |
(errcode(ERRCODE_INVALID_PARAMETER_VALUE), |
| 1238 |
errmsg("new ICU locale (%s) is incompatible with the ICU locale of the template database (%s)", |
| 1239 |
dblocale, src_locale), |
| 1240 |
errhint("Use the same ICU locale as in the template database, or use template0 as template."))); |
| 1241 |
|
| 1242 |
val1 = dbicurules; |
| 1243 |
if (!val1) |
| 1244 |
val1 = ""; |
| 1245 |
val2 = src_icurules; |
| 1246 |
if (!val2) |
| 1247 |
val2 = ""; |
| 1248 |
if (strcmp(val1, val2) != 0) |
| 1249 |
ereport(ERROR, |
| 1250 |
(errcode(ERRCODE_INVALID_PARAMETER_VALUE), |
| 1251 |
errmsg("new ICU collation rules (%s) are incompatible with the ICU collation rules of the template database (%s)", |
| 1252 |
val1, val2), |
| 1253 |
errhint("Use the same ICU collation rules as in the template database, or use template0 as template."))); |
| 1254 |
} |
| 1255 |
} |
| 1256 |
|
| 1257 |
/* |
| 1258 |
* If we got a collation version for the template database, check that it |
| 1259 |
* matches the actual OS collation version. Otherwise error; the user |
| 1260 |
* needs to fix the template database first. Don't complain if a |
| 1261 |
* collation version was specified explicitly as a statement option; that |
| 1262 |
* is used by pg_upgrade to reproduce the old state exactly. |
| 1263 |
* |
| 1264 |
* (If the template database has no collation version, then either the |
| 1265 |
* platform/provider does not support collation versioning, or it's |
| 1266 |
* template0, for which we stipulate that it does not contain |
| 1267 |
* collation-using objects.) |
| 1268 |
*/ |
| 1269 |
if (src_collversion && !collversionEl) |
| 1270 |
{ |
| 1271 |
char *actual_versionstr; |
| 1272 |
const char *locale; |
| 1273 |
|
| 1274 |
if (dblocprovider == COLLPROVIDER_LIBC) |
| 1275 |
locale = dbcollate; |
| 1276 |
else |
| 1277 |
locale = dblocale; |
| 1278 |
|
| 1279 |
actual_versionstr = get_collation_actual_version(dblocprovider, locale); |
| 1280 |
if (!actual_versionstr) |
| 1281 |
ereport(ERROR, |
| 1282 |
(errmsg("template database \"%s\" has a collation version, but no actual collation version could be determined", |
| 1283 |
dbtemplate))); |
| 1284 |
|
| 1285 |
if (strcmp(actual_versionstr, src_collversion) != 0) |
| 1286 |
ereport(ERROR, |
| 1287 |
(errmsg("template database \"%s\" has a collation version mismatch", |
| 1288 |
dbtemplate), |
| 1289 |
errdetail("The template database was created using collation version %s, " |
| 1290 |
"but the operating system provides version %s.", |
| 1291 |
src_collversion, actual_versionstr), |
| 1292 |
errhint("Rebuild all objects in the template database that use the default collation and run " |
| 1293 |
"ALTER DATABASE %s REFRESH COLLATION VERSION, " |
| 1294 |
"or build PostgreSQL with the right library version.", |
| 1295 |
quote_identifier(dbtemplate)))); |
| 1296 |
} |
| 1297 |
|
| 1298 |
if (dbcollversion == NULL) |
| 1299 |
dbcollversion = src_collversion; |
| 1300 |
|
| 1301 |
/* |
| 1302 |
* Normally, we copy the collation version from the template database. |
| 1303 |
* This last resort only applies if the template database does not have a |
| 1304 |
* collation version, which is normally only the case for template0. |
| 1305 |
*/ |
| 1306 |
if (dbcollversion == NULL) |
| 1307 |
{ |
| 1308 |
const char *locale; |
| 1309 |
|
| 1310 |
if (dblocprovider == COLLPROVIDER_LIBC) |
| 1311 |
locale = dbcollate; |
| 1312 |
else |
| 1313 |
locale = dblocale; |
| 1314 |
|
| 1315 |
dbcollversion = get_collation_actual_version(dblocprovider, locale); |
| 1316 |
} |
| 1317 |
|
| 1318 |
/* Resolve default tablespace for new database */ |
| 1319 |
if (tablespacenameEl && tablespacenameEl->arg) |
| 1320 |
{ |
| 1321 |
char *tablespacename; |
| 1322 |
AclResult aclresult; |
| 1323 |
|
| 1324 |
tablespacename = defGetString(tablespacenameEl); |
| 1325 |
dst_deftablespace = get_tablespace_oid(tablespacename, false); |
| 1326 |
/* check permissions */ |
| 1327 |
aclresult = object_aclcheck(TableSpaceRelationId, dst_deftablespace, GetUserId(), |
| 1328 |
ACL_CREATE); |
| 1329 |
if (aclresult != ACLCHECK_OK) |
| 1330 |
aclcheck_error(aclresult, OBJECT_TABLESPACE, |
| 1331 |
tablespacename); |
| 1332 |
|
| 1333 |
/* pg_global must never be the default tablespace */ |
| 1334 |
if (dst_deftablespace == GLOBALTABLESPACE_OID) |
| 1335 |
ereport(ERROR, |
| 1336 |
(errcode(ERRCODE_INVALID_PARAMETER_VALUE), |
| 1337 |
errmsg("pg_global cannot be used as default tablespace"))); |
| 1338 |
|
| 1339 |
/* |
| 1340 |
* If we are trying to change the default tablespace of the template, |
| 1341 |
* we require that the template not have any files in the new default |
| 1342 |
* tablespace. This is necessary because otherwise the copied |
| 1343 |
* database would contain pg_class rows that refer to its default |
| 1344 |
* tablespace both explicitly (by OID) and implicitly (as zero), which |
| 1345 |
* would cause problems. For example another CREATE DATABASE using |
| 1346 |
* the copied database as template, and trying to change its default |
| 1347 |
* tablespace again, would yield outright incorrect results (it would |
| 1348 |
* improperly move tables to the new default tablespace that should |
| 1349 |
* stay in the same tablespace). |
| 1350 |
*/ |
| 1351 |
if (dst_deftablespace != src_deftablespace) |
| 1352 |
{ |
| 1353 |
char *srcpath; |
| 1354 |
struct stat st; |
| 1355 |
|
| 1356 |
srcpath = GetDatabasePath(src_dboid, dst_deftablespace); |
| 1357 |
|
| 1358 |
if (stat(srcpath, &st) == 0 && |
| 1359 |
S_ISDIR(st.st_mode) && |
| 1360 |
!directory_is_empty(srcpath)) |
| 1361 |
ereport(ERROR, |
| 1362 |
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED), |
| 1363 |
errmsg("cannot assign new default tablespace \"%s\"", |
| 1364 |
tablespacename), |
| 1365 |
errdetail("There is a conflict because database \"%s\" already has some tables in this tablespace.", |
| 1366 |
dbtemplate))); |
| 1367 |
pfree(srcpath); |
| 1368 |
} |
| 1369 |
} |
| 1370 |
else |
| 1371 |
{ |
| 1372 |
/* Use template database's default tablespace */ |
| 1373 |
dst_deftablespace = src_deftablespace; |
| 1374 |
/* Note there is no additional permission check in this path */ |
| 1375 |
} |
| 1376 |
|
| 1377 |
/* |
| 1378 |
* If built with appropriate switch, whine when regression-testing |
| 1379 |
* conventions for database names are violated. But don't complain during |
| 1380 |
* initdb. |
| 1381 |
*/ |
| 1382 |
#ifdef ENFORCE_REGRESSION_TEST_NAME_RESTRICTIONS |
| 1383 |
if (IsUnderPostmaster && strstr(dbname, "regression") == NULL) |
| 1384 |
elog(WARNING, "databases created by regression test cases should have names including \"regression\""); |
| 1385 |
#endif |
| 1386 |
|
| 1387 |
/* |
| 1388 |
* Check for db name conflict. This is just to give a more friendly error |
| 1389 |
* message than "unique index violation". There's a race condition but |
| 1390 |
* we're willing to accept the less friendly message in that case. |
| 1391 |
*/ |
| 1392 |
if (OidIsValid(get_database_oid(dbname, true))) |
| 1393 |
ereport(ERROR, |
| 1394 |
(errcode(ERRCODE_DUPLICATE_DATABASE), |
| 1395 |
errmsg("database \"%s\" already exists", dbname))); |
| 1396 |
|
| 1397 |
/* |
| 1398 |
* The source DB can't have any active backends, except this one |
| 1399 |
* (exception is to allow CREATE DB while connected to template1). |
| 1400 |
* Otherwise we might copy inconsistent data. |
| 1401 |
* |
| 1402 |
* This should be last among the basic error checks, because it involves |
| 1403 |
* potential waiting; we may as well throw an error first if we're gonna |
| 1404 |
* throw one. |
| 1405 |
*/ |
| 1406 |
if (CountOtherDBBackends(src_dboid, ¬herbackends, &npreparedxacts)) |
| 1407 |
ereport(ERROR, |
| 1408 |
(errcode(ERRCODE_OBJECT_IN_USE), |
| 1409 |
errmsg("source database \"%s\" is being accessed by other users", |
| 1410 |
dbtemplate), |
| 1411 |
errdetail_busy_db(notherbackends, npreparedxacts))); |
| 1412 |
|
| 1413 |
/* |
| 1414 |
* Select an OID for the new database, checking that it doesn't have a |
| 1415 |
* filename conflict with anything already existing in the tablespace |
| 1416 |
* directories. |
| 1417 |
*/ |
| 1418 |
pg_database_rel = table_open(DatabaseRelationId, RowExclusiveLock); |
| 1419 |
|
| 1420 |
/* |
| 1421 |
* If database OID is configured, check if the OID is already in use or |
| 1422 |
* data directory already exists. |
| 1423 |
*/ |
| 1424 |
if (OidIsValid(dboid)) |
| 1425 |
{ |
| 1426 |
char *existing_dbname = get_database_name(dboid); |
| 1427 |
|
| 1428 |
if (existing_dbname != NULL) |
| 1429 |
ereport(ERROR, |
| 1430 |
(errcode(ERRCODE_INVALID_PARAMETER_VALUE)), |
| 1431 |
errmsg("database OID %u is already in use by database \"%s\"", |
| 1432 |
dboid, existing_dbname)); |
| 1433 |
|
| 1434 |
if (check_db_file_conflict(dboid)) |
| 1435 |
ereport(ERROR, |
| 1436 |
(errcode(ERRCODE_INVALID_PARAMETER_VALUE)), |
| 1437 |
errmsg("data directory with the specified OID %u already exists", dboid)); |
| 1438 |
} |
| 1439 |
else |
| 1440 |
{ |
| 1441 |
/* Select an OID for the new database if is not explicitly configured. */ |
| 1442 |
do |
| 1443 |
{ |
| 1444 |
dboid = GetNewOidWithIndex(pg_database_rel, DatabaseOidIndexId, |
| 1445 |
Anum_pg_database_oid); |
| 1446 |
} while (check_db_file_conflict(dboid)); |
| 1447 |
} |
| 1448 |
|
| 1449 |
/* |
| 1450 |
* Insert a new tuple into pg_database. This establishes our ownership of |
| 1451 |
* the new database name (anyone else trying to insert the same name will |
| 1452 |
* block on the unique index, and fail after we commit). |
| 1453 |
*/ |
| 1454 |
|
| 1455 |
Assert((dblocprovider != COLLPROVIDER_LIBC && dblocale) || |
| 1456 |
(dblocprovider == COLLPROVIDER_LIBC && !dblocale)); |
| 1457 |
|
| 1458 |
/* Form tuple */ |
| 1459 |
new_record[Anum_pg_database_oid - 1] = ObjectIdGetDatum(dboid); |
| 1460 |
new_record[Anum_pg_database_datname - 1] = |
| 1461 |
DirectFunctionCall1(namein, CStringGetDatum(dbname)); |
| 1462 |
new_record[Anum_pg_database_datdba - 1] = ObjectIdGetDatum(datdba); |
| 1463 |
new_record[Anum_pg_database_encoding - 1] = Int32GetDatum(encoding); |
| 1464 |
new_record[Anum_pg_database_datlocprovider - 1] = CharGetDatum(dblocprovider); |
| 1465 |
new_record[Anum_pg_database_datistemplate - 1] = BoolGetDatum(dbistemplate); |
| 1466 |
new_record[Anum_pg_database_datallowconn - 1] = BoolGetDatum(dballowconnections); |
| 1467 |
new_record[Anum_pg_database_dathasloginevt - 1] = BoolGetDatum(src_hasloginevt); |
| 1468 |
new_record[Anum_pg_database_datconnlimit - 1] = Int32GetDatum(dbconnlimit); |
| 1469 |
new_record[Anum_pg_database_datfrozenxid - 1] = TransactionIdGetDatum(src_frozenxid); |
| 1470 |
new_record[Anum_pg_database_datminmxid - 1] = TransactionIdGetDatum(src_minmxid); |
| 1471 |
new_record[Anum_pg_database_dattablespace - 1] = ObjectIdGetDatum(dst_deftablespace); |
| 1472 |
new_record[Anum_pg_database_datcollate - 1] = CStringGetTextDatum(dbcollate); |
| 1473 |
new_record[Anum_pg_database_datctype - 1] = CStringGetTextDatum(dbctype); |
| 1474 |
if (dblocale) |
| 1475 |
new_record[Anum_pg_database_datlocale - 1] = CStringGetTextDatum(dblocale); |
| 1476 |
else |
| 1477 |
new_record_nulls[Anum_pg_database_datlocale - 1] = true; |
| 1478 |
if (dbicurules) |
| 1479 |
new_record[Anum_pg_database_daticurules - 1] = CStringGetTextDatum(dbicurules); |
| 1480 |
else |
| 1481 |
new_record_nulls[Anum_pg_database_daticurules - 1] = true; |
| 1482 |
if (dbcollversion) |
| 1483 |
new_record[Anum_pg_database_datcollversion - 1] = CStringGetTextDatum(dbcollversion); |
| 1484 |
else |
| 1485 |
new_record_nulls[Anum_pg_database_datcollversion - 1] = true; |
| 1486 |
|
| 1487 |
/* |
| 1488 |
* We deliberately set datacl to default (NULL), rather than copying it |
| 1489 |
* from the template database. Copying it would be a bad idea when the |
| 1490 |
* owner is not the same as the template's owner. |
| 1491 |
*/ |
| 1492 |
new_record_nulls[Anum_pg_database_datacl - 1] = true; |
| 1493 |
|
| 1494 |
tuple = heap_form_tuple(RelationGetDescr(pg_database_rel), |
| 1495 |
new_record, new_record_nulls); |
| 1496 |
|
| 1497 |
CatalogTupleInsert(pg_database_rel, tuple); |
| 1498 |
|
| 1499 |
/* |
| 1500 |
* Now generate additional catalog entries associated with the new DB |
| 1501 |
*/ |
| 1502 |
|
| 1503 |
/* Register owner dependency */ |
| 1504 |
recordDependencyOnOwner(DatabaseRelationId, dboid, datdba); |
| 1505 |
|
| 1506 |
/* Create pg_shdepend entries for objects within database */ |
| 1507 |
copyTemplateDependencies(src_dboid, dboid); |
| 1508 |
|
| 1509 |
/* Post creation hook for new database */ |
| 1510 |
InvokeObjectPostCreateHook(DatabaseRelationId, dboid, 0); |
| 1511 |
|
| 1512 |
/* |
| 1513 |
* If we're going to be reading data for the to-be-created database into |
| 1514 |
* shared_buffers, take a lock on it. Nobody should know that this |
| 1515 |
* database exists yet, but it's good to maintain the invariant that an |
| 1516 |
* AccessExclusiveLock on the database is sufficient to drop all of its |
| 1517 |
* buffers without worrying about more being read later. |
| 1518 |
* |
| 1519 |
* Note that we need to do this before entering the |
| 1520 |
* PG_ENSURE_ERROR_CLEANUP block below, because createdb_failure_callback |
| 1521 |
* expects this lock to be held already. |
| 1522 |
*/ |
| 1523 |
if (dbstrategy == CREATEDB_WAL_LOG) |
| 1524 |
LockSharedObject(DatabaseRelationId, dboid, 0, AccessShareLock); |
| 1525 |
|
| 1526 |
/* |
| 1527 |
* Once we start copying subdirectories, we need to be able to clean 'em |
| 1528 |
* up if we fail. Use an ENSURE block to make sure this happens. (This |
| 1529 |
* is not a 100% solution, because of the possibility of failure during |
| 1530 |
* transaction commit after we leave this routine, but it should handle |
| 1531 |
* most scenarios.) |
| 1532 |
*/ |
| 1533 |
fparms.src_dboid = src_dboid; |
| 1534 |
fparms.dest_dboid = dboid; |
| 1535 |
fparms.strategy = dbstrategy; |
| 1536 |
|
| 1537 |
PG_ENSURE_ERROR_CLEANUP(createdb_failure_callback, |
| 1538 |
PointerGetDatum(&fparms)); |
| 1539 |
{ |
| 1540 |
/* |
| 1541 |
* If the user has asked to create a database with WAL_LOG strategy |
| 1542 |
* then call CreateDatabaseUsingWalLog, which will copy the database |
| 1543 |
* at the block level and it will WAL log each copied block. |
| 1544 |
* Otherwise, call CreateDatabaseUsingFileCopy that will copy the |
| 1545 |
* database file by file. |
| 1546 |
*/ |
| 1547 |
if (dbstrategy == CREATEDB_WAL_LOG) |
| 1548 |
CreateDatabaseUsingWalLog(src_dboid, dboid, src_deftablespace, |
| 1549 |
dst_deftablespace); |
| 1550 |
else |
| 1551 |
CreateDatabaseUsingFileCopy(src_dboid, dboid, src_deftablespace, |
| 1552 |
dst_deftablespace); |
| 1553 |
|
| 1554 |
/* |
| 1555 |
* Close pg_database, but keep lock till commit. |
| 1556 |
*/ |
| 1557 |
table_close(pg_database_rel, NoLock); |
| 1558 |
|
| 1559 |
/* |
| 1560 |
* Force synchronous commit, thus minimizing the window between |
| 1561 |
* creation of the database files and committal of the transaction. If |
| 1562 |
* we crash before committing, we'll have a DB that's taking up disk |
| 1563 |
* space but is not in pg_database, which is not good. |
| 1564 |
*/ |
| 1565 |
ForceSyncCommit(); |
| 1566 |
} |
| 1567 |
PG_END_ENSURE_ERROR_CLEANUP(createdb_failure_callback, |
| 1568 |
PointerGetDatum(&fparms)); |
| 1569 |
|
| 1570 |
return dboid; |
| 1571 |
} |
| 1572 |
|
| 1573 |
/* |
| 1574 |
* Check whether chosen encoding matches chosen locale settings. This |
| 1575 |
* restriction is necessary because libc's locale-specific code usually |
| 1576 |
* fails when presented with data in an encoding it's not expecting. We |
| 1577 |
* allow mismatch in four cases: |
| 1578 |
* |
| 1579 |
* 1. locale encoding = SQL_ASCII, which means that the locale is C/POSIX |
| 1580 |
* which works with any encoding. |
| 1581 |
* |
| 1582 |
* 2. locale encoding = -1, which means that we couldn't determine the |
| 1583 |
* locale's encoding and have to trust the user to get it right. |
| 1584 |
* |
| 1585 |
* 3. selected encoding is UTF8 and platform is win32. This is because |
| 1586 |
* UTF8 is a pseudo codepage that is supported in all locales since it's |
| 1587 |
* converted to UTF16 before being used. |
| 1588 |
* |
| 1589 |
* 4. selected encoding is SQL_ASCII, but only if you're a superuser. This |
| 1590 |
* is risky but we have historically allowed it --- notably, the |
| 1591 |
* regression tests require it. |
| 1592 |
* |
| 1593 |
* Note: if you change this policy, fix initdb to match. |
| 1594 |
*/ |
| 1595 |
void |
| 1596 |
check_encoding_locale_matches(int encoding, const char *collate, const char *ctype) |
| 1597 |
{ |
| 1598 |
int ctype_encoding = pg_get_encoding_from_locale(ctype, true); |
| 1599 |
int collate_encoding = pg_get_encoding_from_locale(collate, true); |
| 1600 |
|
| 1601 |
if (!(ctype_encoding == encoding || |
| 1602 |
ctype_encoding == PG_SQL_ASCII || |
| 1603 |
ctype_encoding == -1 || |
| 1604 |
#ifdef WIN32 |
| 1605 |
encoding == PG_UTF8 || |
| 1606 |
#endif |
| 1607 |
(encoding == PG_SQL_ASCII && superuser()))) |
| 1608 |
ereport(ERROR, |
| 1609 |
(errcode(ERRCODE_INVALID_PARAMETER_VALUE), |
| 1610 |
errmsg("encoding \"%s\" does not match locale \"%s\"", |
| 1611 |
pg_encoding_to_char(encoding), |
| 1612 |
ctype), |
| 1613 |
errdetail("The chosen LC_CTYPE setting requires encoding \"%s\".", |
| 1614 |
pg_encoding_to_char(ctype_encoding)))); |
| 1615 |
|
| 1616 |
if (!(collate_encoding == encoding || |
| 1617 |
collate_encoding == PG_SQL_ASCII || |
| 1618 |
collate_encoding == -1 || |
| 1619 |
#ifdef WIN32 |
| 1620 |
encoding == PG_UTF8 || |
| 1621 |
#endif |
| 1622 |
(encoding == PG_SQL_ASCII && superuser()))) |
| 1623 |
ereport(ERROR, |
| 1624 |
(errcode(ERRCODE_INVALID_PARAMETER_VALUE), |
| 1625 |
errmsg("encoding \"%s\" does not match locale \"%s\"", |
| 1626 |
pg_encoding_to_char(encoding), |
| 1627 |
collate), |
| 1628 |
errdetail("The chosen LC_COLLATE setting requires encoding \"%s\".", |
| 1629 |
pg_encoding_to_char(collate_encoding)))); |
| 1630 |
} |
| 1631 |
|
| 1632 |
/* Error cleanup callback for createdb */ |
| 1633 |
static void |
| 1634 |
createdb_failure_callback(int code, Datum arg) |
| 1635 |
{ |
| 1636 |
createdb_failure_params *fparms = (createdb_failure_params *) DatumGetPointer(arg); |
| 1637 |
|
| 1638 |
/* |
| 1639 |
* If we were copying database at block levels then drop pages for the |
| 1640 |
* destination database that are in the shared buffer cache. And tell |
| 1641 |
* checkpointer to forget any pending fsync and unlink requests for files |
| 1642 |
* in the database. The reasoning behind doing this is same as explained |
| 1643 |
* in dropdb function. But unlike dropdb we don't need to call |
| 1644 |
* pgstat_drop_database because this database is still not created so |
| 1645 |
* there should not be any stat for this. |
| 1646 |
*/ |
| 1647 |
if (fparms->strategy == CREATEDB_WAL_LOG) |
| 1648 |
{ |
| 1649 |
DropDatabaseBuffers(fparms->dest_dboid); |
| 1650 |
ForgetDatabaseSyncRequests(fparms->dest_dboid); |
| 1651 |
|
| 1652 |
/* Release lock on the target database. */ |
| 1653 |
UnlockSharedObject(DatabaseRelationId, fparms->dest_dboid, 0, |
| 1654 |
AccessShareLock); |
| 1655 |
} |
| 1656 |
|
| 1657 |
/* |
| 1658 |
* Release lock on source database before doing recursive remove. This is |
| 1659 |
* not essential but it seems desirable to release the lock as soon as |
| 1660 |
* possible. |
| 1661 |
*/ |
| 1662 |
UnlockSharedObject(DatabaseRelationId, fparms->src_dboid, 0, ShareLock); |
| 1663 |
|
| 1664 |
/* Throw away any successfully copied subdirectories */ |
| 1665 |
remove_dbtablespaces(fparms->dest_dboid); |
| 1666 |
} |
| 1667 |
|
| 1668 |
|
| 1669 |
/* |
| 1670 |
* DROP DATABASE |
| 1671 |
*/ |
| 1672 |
void |
| 1673 |
dropdb(const char *dbname, bool missing_ok, bool force) |
| 1674 |
{ |
| 1675 |
Oid db_id; |
| 1676 |
bool db_istemplate; |
| 1677 |
Relation pgdbrel; |
| 1678 |
HeapTuple tup; |
| 1679 |
ScanKeyData scankey; |
| 1680 |
void *inplace_state; |
| 1681 |
Form_pg_database datform; |
| 1682 |
int notherbackends; |
| 1683 |
int npreparedxacts; |
| 1684 |
int nslots, |
| 1685 |
nslots_active; |
| 1686 |
int nsubscriptions; |
| 1687 |
|
| 1688 |
/* |
| 1689 |
* Look up the target database's OID, and get exclusive lock on it. We |
| 1690 |
* need this to ensure that no new backend starts up in the target |
| 1691 |
* database while we are deleting it (see postinit.c), and that no one is |
| 1692 |
* using it as a CREATE DATABASE template or trying to delete it for |
| 1693 |
* themselves. |
| 1694 |
*/ |
| 1695 |
pgdbrel = table_open(DatabaseRelationId, RowExclusiveLock); |
| 1696 |
|
| 1697 |
if (!get_db_info(dbname, AccessExclusiveLock, &db_id, NULL, NULL, |
| 1698 |
&db_istemplate, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL)) |
| 1699 |
{ |
| 1700 |
if (!missing_ok) |
| 1701 |
{ |
| 1702 |
ereport(ERROR, |
| 1703 |
(errcode(ERRCODE_UNDEFINED_DATABASE), |
| 1704 |
errmsg("database \"%s\" does not exist", dbname))); |
| 1705 |
} |
| 1706 |
else |
| 1707 |
{ |
| 1708 |
/* Close pg_database, release the lock, since we changed nothing */ |
| 1709 |
table_close(pgdbrel, RowExclusiveLock); |
| 1710 |
ereport(NOTICE, |
| 1711 |
(errmsg("database \"%s\" does not exist, skipping", |
| 1712 |
dbname))); |
| 1713 |
return; |
| 1714 |
} |
| 1715 |
} |
| 1716 |
|
| 1717 |
/* |
| 1718 |
* Permission checks |
| 1719 |
*/ |
| 1720 |
if (!object_ownercheck(DatabaseRelationId, db_id, GetUserId())) |
| 1721 |
aclcheck_error(ACLCHECK_NOT_OWNER, OBJECT_DATABASE, |
| 1722 |
dbname); |
| 1723 |
|
| 1724 |
/* DROP hook for the database being removed */ |
| 1725 |
InvokeObjectDropHook(DatabaseRelationId, db_id, 0); |
| 1726 |
|
| 1727 |
/* |
| 1728 |
* Disallow dropping a DB that is marked istemplate. This is just to |
| 1729 |
* prevent people from accidentally dropping template0 or template1; they |
| 1730 |
* can do so if they're really determined ... |
| 1731 |
*/ |
| 1732 |
if (db_istemplate) |
| 1733 |
ereport(ERROR, |
| 1734 |
(errcode(ERRCODE_WRONG_OBJECT_TYPE), |
| 1735 |
errmsg("cannot drop a template database"))); |
| 1736 |
|
| 1737 |
/* Obviously can't drop my own database */ |
| 1738 |
if (db_id == MyDatabaseId) |
| 1739 |
ereport(ERROR, |
| 1740 |
(errcode(ERRCODE_OBJECT_IN_USE), |
| 1741 |
errmsg("cannot drop the currently open database"))); |
| 1742 |
|
| 1743 |
/* |
| 1744 |
* Check whether there are active logical slots that refer to the |
| 1745 |
* to-be-dropped database. The database lock we are holding prevents the |
| 1746 |
* creation of new slots using the database or existing slots becoming |
| 1747 |
* active. |
| 1748 |
*/ |
| 1749 |
(void) ReplicationSlotsCountDBSlots(db_id, &nslots, &nslots_active); |
| 1750 |
if (nslots_active) |
| 1751 |
{ |
| 1752 |
ereport(ERROR, |
| 1753 |
(errcode(ERRCODE_OBJECT_IN_USE), |
| 1754 |
errmsg("database \"%s\" is used by an active logical replication slot", |
| 1755 |
dbname), |
| 1756 |
errdetail_plural("There is %d active slot.", |
| 1757 |
"There are %d active slots.", |
| 1758 |
nslots_active, nslots_active))); |
| 1759 |
} |
| 1760 |
|
| 1761 |
/* |
| 1762 |
* Check if there are subscriptions defined in the target database. |
| 1763 |
* |
| 1764 |
* We can't drop them automatically because they might be holding |
| 1765 |
* resources in other databases/instances. |
| 1766 |
*/ |
| 1767 |
if ((nsubscriptions = CountDBSubscriptions(db_id)) > 0) |
| 1768 |
ereport(ERROR, |
| 1769 |
(errcode(ERRCODE_OBJECT_IN_USE), |
| 1770 |
errmsg("database \"%s\" is being used by logical replication subscription", |
| 1771 |
dbname), |
| 1772 |
errdetail_plural("There is %d subscription.", |
| 1773 |
"There are %d subscriptions.", |
| 1774 |
nsubscriptions, nsubscriptions))); |
| 1775 |
|
| 1776 |
|
| 1777 |
/* |
| 1778 |
* Attempt to terminate all existing connections to the target database if |
| 1779 |
* the user has requested to do so. |
| 1780 |
*/ |
| 1781 |
if (force) |
| 1782 |
TerminateOtherDBBackends(db_id); |
| 1783 |
|
| 1784 |
/* |
| 1785 |
* Check for other backends in the target database. (Because we hold the |
| 1786 |
* database lock, no new ones can start after this.) |
| 1787 |
* |
| 1788 |
* As in CREATE DATABASE, check this after other error conditions. |
| 1789 |
*/ |
| 1790 |
if (CountOtherDBBackends(db_id, ¬herbackends, &npreparedxacts)) |
| 1791 |
ereport(ERROR, |
| 1792 |
(errcode(ERRCODE_OBJECT_IN_USE), |
| 1793 |
errmsg("database \"%s\" is being accessed by other users", |
| 1794 |
dbname), |
| 1795 |
errdetail_busy_db(notherbackends, npreparedxacts))); |
| 1796 |
|
| 1797 |
/* |
| 1798 |
* Delete any comments or security labels associated with the database. |
| 1799 |
*/ |
| 1800 |
DeleteSharedComments(db_id, DatabaseRelationId); |
| 1801 |
DeleteSharedSecurityLabel(db_id, DatabaseRelationId); |
| 1802 |
|
| 1803 |
/* |
| 1804 |
* Remove settings associated with this database |
| 1805 |
*/ |
| 1806 |
DropSetting(db_id, InvalidOid); |
| 1807 |
|
| 1808 |
/* |
| 1809 |
* Remove shared dependency references for the database. |
| 1810 |
*/ |
| 1811 |
dropDatabaseDependencies(db_id); |
| 1812 |
|
| 1813 |
/* |
| 1814 |
* Tell the cumulative stats system to forget it immediately, too. |
| 1815 |
*/ |
| 1816 |
pgstat_drop_database(db_id); |
| 1817 |
|
| 1818 |
/* |
| 1819 |
* Except for the deletion of the catalog row, subsequent actions are not |
| 1820 |
* transactional (consider DropDatabaseBuffers() discarding modified |
| 1821 |
* buffers). But we might crash or get interrupted below. To prevent |
| 1822 |
* accesses to a database with invalid contents, mark the database as |
| 1823 |
* invalid using an in-place update. |
| 1824 |
* |
| 1825 |
* We need to flush the WAL before continuing, to guarantee the |
| 1826 |
* modification is durable before performing irreversible filesystem |
| 1827 |
* operations. |
| 1828 |
*/ |
| 1829 |
ScanKeyInit(&scankey, |
| 1830 |
Anum_pg_database_datname, |
| 1831 |
BTEqualStrategyNumber, F_NAMEEQ, |
| 1832 |
CStringGetDatum(dbname)); |
| 1833 |
systable_inplace_update_begin(pgdbrel, DatabaseNameIndexId, true, |
| 1834 |
NULL, 1, &scankey, &tup, &inplace_state); |
| 1835 |
if (!HeapTupleIsValid(tup)) |
| 1836 |
elog(ERROR, "cache lookup failed for database %u", db_id); |
| 1837 |
datform = (Form_pg_database) GETSTRUCT(tup); |
| 1838 |
datform->datconnlimit = DATCONNLIMIT_INVALID_DB; |
| 1839 |
systable_inplace_update_finish(inplace_state, tup); |
| 1840 |
XLogFlush(XactLastRecEnd); |
| 1841 |
|
| 1842 |
/* |
| 1843 |
* Also delete the tuple - transactionally. If this transaction commits, |
| 1844 |
* the row will be gone, but if we fail, dropdb() can be invoked again. |
| 1845 |
*/ |
| 1846 |
CatalogTupleDelete(pgdbrel, &tup->t_self); |
| 1847 |
heap_freetuple(tup); |
| 1848 |
|
| 1849 |
/* |
| 1850 |
* Drop db-specific replication slots. |
| 1851 |
*/ |
| 1852 |
ReplicationSlotsDropDBSlots(db_id); |
| 1853 |
|
| 1854 |
/* |
| 1855 |
* Drop pages for this database that are in the shared buffer cache. This |
| 1856 |
* is important to ensure that no remaining backend tries to write out a |
| 1857 |
* dirty buffer to the dead database later... |
| 1858 |
*/ |
| 1859 |
DropDatabaseBuffers(db_id); |
| 1860 |
|
| 1861 |
/* |
| 1862 |
* Tell checkpointer to forget any pending fsync and unlink requests for |
| 1863 |
* files in the database; else the fsyncs will fail at next checkpoint, or |
| 1864 |
* worse, it will delete files that belong to a newly created database |
| 1865 |
* with the same OID. |
| 1866 |
*/ |
| 1867 |
ForgetDatabaseSyncRequests(db_id); |
| 1868 |
|
| 1869 |
/* |
| 1870 |
* Force a checkpoint to make sure the checkpointer has received the |
| 1871 |
* message sent by ForgetDatabaseSyncRequests. |
| 1872 |
*/ |
| 1873 |
RequestCheckpoint(CHECKPOINT_IMMEDIATE | CHECKPOINT_FORCE | CHECKPOINT_WAIT); |
| 1874 |
|
| 1875 |
/* Close all smgr fds in all backends. */ |
| 1876 |
WaitForProcSignalBarrier(EmitProcSignalBarrier(PROCSIGNAL_BARRIER_SMGRRELEASE)); |
| 1877 |
|
| 1878 |
/* |
| 1879 |
* Remove all tablespace subdirs belonging to the database. |
| 1880 |
*/ |
| 1881 |
remove_dbtablespaces(db_id); |
| 1882 |
|
| 1883 |
/* |
| 1884 |
* Close pg_database, but keep lock till commit. |
| 1885 |
*/ |
| 1886 |
table_close(pgdbrel, NoLock); |
| 1887 |
|
| 1888 |
/* |
| 1889 |
* Force synchronous commit, thus minimizing the window between removal of |
| 1890 |
* the database files and committal of the transaction. If we crash before |
| 1891 |
* committing, we'll have a DB that's gone on disk but still there |
| 1892 |
* according to pg_database, which is not good. |
| 1893 |
*/ |
| 1894 |
ForceSyncCommit(); |
| 1895 |
} |
| 1896 |
|
| 1897 |
|
| 1898 |
/* |
| 1899 |
* Rename database |
| 1900 |
*/ |
| 1901 |
ObjectAddress |
| 1902 |
RenameDatabase(const char *oldname, const char *newname) |
| 1903 |
{ |
| 1904 |
Oid db_id; |
| 1905 |
HeapTuple newtup; |
| 1906 |
ItemPointerData otid; |
| 1907 |
Relation rel; |
| 1908 |
int notherbackends; |
| 1909 |
int npreparedxacts; |
| 1910 |
ObjectAddress address; |
| 1911 |
|
| 1912 |
/* |
| 1913 |
* Look up the target database's OID, and get exclusive lock on it. We |
| 1914 |
* need this for the same reasons as DROP DATABASE. |
| 1915 |
*/ |
| 1916 |
rel = table_open(DatabaseRelationId, RowExclusiveLock); |
| 1917 |
|
| 1918 |
if (!get_db_info(oldname, AccessExclusiveLock, &db_id, NULL, NULL, NULL, |
| 1919 |
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL)) |
| 1920 |
ereport(ERROR, |
| 1921 |
(errcode(ERRCODE_UNDEFINED_DATABASE), |
| 1922 |
errmsg("database \"%s\" does not exist", oldname))); |
| 1923 |
|
| 1924 |
/* must be owner */ |
| 1925 |
if (!object_ownercheck(DatabaseRelationId, db_id, GetUserId())) |
| 1926 |
aclcheck_error(ACLCHECK_NOT_OWNER, OBJECT_DATABASE, |
| 1927 |
oldname); |
| 1928 |
|
| 1929 |
/* must have createdb rights */ |
| 1930 |
if (!have_createdb_privilege()) |
| 1931 |
ereport(ERROR, |
| 1932 |
(errcode(ERRCODE_INSUFFICIENT_PRIVILEGE), |
| 1933 |
errmsg("permission denied to rename database"))); |
| 1934 |
|
| 1935 |
/* |
| 1936 |
* If built with appropriate switch, whine when regression-testing |
| 1937 |
* conventions for database names are violated. |
| 1938 |
*/ |
| 1939 |
#ifdef ENFORCE_REGRESSION_TEST_NAME_RESTRICTIONS |
| 1940 |
if (strstr(newname, "regression") == NULL) |
| 1941 |
elog(WARNING, "databases created by regression test cases should have names including \"regression\""); |
| 1942 |
#endif |
| 1943 |
|
| 1944 |
/* |
| 1945 |
* Make sure the new name doesn't exist. See notes for same error in |
| 1946 |
* CREATE DATABASE. |
| 1947 |
*/ |
| 1948 |
if (OidIsValid(get_database_oid(newname, true))) |
| 1949 |
ereport(ERROR, |
| 1950 |
(errcode(ERRCODE_DUPLICATE_DATABASE), |
| 1951 |
errmsg("database \"%s\" already exists", newname))); |
| 1952 |
|
| 1953 |
/* |
| 1954 |
* XXX Client applications probably store the current database somewhere, |
| 1955 |
* so renaming it could cause confusion. On the other hand, there may not |
| 1956 |
* be an actual problem besides a little confusion, so think about this |
| 1957 |
* and decide. |
| 1958 |
*/ |
| 1959 |
if (db_id == MyDatabaseId) |
| 1960 |
ereport(ERROR, |
| 1961 |
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED), |
| 1962 |
errmsg("current database cannot be renamed"))); |
| 1963 |
|
| 1964 |
/* |
| 1965 |
* Make sure the database does not have active sessions. This is the same |
| 1966 |
* concern as above, but applied to other sessions. |
| 1967 |
* |
| 1968 |
* As in CREATE DATABASE, check this after other error conditions. |
| 1969 |
*/ |
| 1970 |
if (CountOtherDBBackends(db_id, ¬herbackends, &npreparedxacts)) |
| 1971 |
ereport(ERROR, |
| 1972 |
(errcode(ERRCODE_OBJECT_IN_USE), |
| 1973 |
errmsg("database \"%s\" is being accessed by other users", |
| 1974 |
oldname), |
| 1975 |
errdetail_busy_db(notherbackends, npreparedxacts))); |
| 1976 |
|
| 1977 |
/* rename */ |
| 1978 |
newtup = SearchSysCacheLockedCopy1(DATABASEOID, ObjectIdGetDatum(db_id)); |
| 1979 |
if (!HeapTupleIsValid(newtup)) |
| 1980 |
elog(ERROR, "cache lookup failed for database %u", db_id); |
| 1981 |
otid = newtup->t_self; |
| 1982 |
namestrcpy(&(((Form_pg_database) GETSTRUCT(newtup))->datname), newname); |
| 1983 |
CatalogTupleUpdate(rel, &otid, newtup); |
| 1984 |
UnlockTuple(rel, &otid, InplaceUpdateTupleLock); |
| 1985 |
|
| 1986 |
InvokeObjectPostAlterHook(DatabaseRelationId, db_id, 0); |
| 1987 |
|
| 1988 |
ObjectAddressSet(address, DatabaseRelationId, db_id); |
| 1989 |
|
| 1990 |
/* |
| 1991 |
* Close pg_database, but keep lock till commit. |
| 1992 |
*/ |
| 1993 |
table_close(rel, NoLock); |
| 1994 |
|
| 1995 |
return address; |
| 1996 |
} |
| 1997 |
|
| 1998 |
|
| 1999 |
/* |
| 2000 |
* ALTER DATABASE SET TABLESPACE |
| 2001 |
*/ |
| 2002 |
static void |
| 2003 |
movedb(const char *dbname, const char *tblspcname) |
| 2004 |
{ |
| 2005 |
Oid db_id; |
| 2006 |
Relation pgdbrel; |
| 2007 |
int notherbackends; |
| 2008 |
int npreparedxacts; |
| 2009 |
HeapTuple oldtuple, |
| 2010 |
newtuple; |
| 2011 |
Oid src_tblspcoid, |
| 2012 |
dst_tblspcoid; |
| 2013 |
ScanKeyData scankey; |
| 2014 |
SysScanDesc sysscan; |
| 2015 |
AclResult aclresult; |
| 2016 |
char *src_dbpath; |
| 2017 |
char *dst_dbpath; |
| 2018 |
DIR *dstdir; |
| 2019 |
struct dirent *xlde; |
| 2020 |
movedb_failure_params fparms; |
| 2021 |
|
| 2022 |
/* |
| 2023 |
* Look up the target database's OID, and get exclusive lock on it. We |
| 2024 |
* need this to ensure that no new backend starts up in the database while |
| 2025 |
* we are moving it, and that no one is using it as a CREATE DATABASE |
| 2026 |
* template or trying to delete it. |
| 2027 |
*/ |
| 2028 |
pgdbrel = table_open(DatabaseRelationId, RowExclusiveLock); |
| 2029 |
|
| 2030 |
if (!get_db_info(dbname, AccessExclusiveLock, &db_id, NULL, NULL, NULL, |
| 2031 |
NULL, NULL, NULL, NULL, &src_tblspcoid, NULL, NULL, NULL, NULL, NULL, NULL)) |
| 2032 |
ereport(ERROR, |
| 2033 |
(errcode(ERRCODE_UNDEFINED_DATABASE), |
| 2034 |
errmsg("database \"%s\" does not exist", dbname))); |
| 2035 |
|
| 2036 |
/* |
| 2037 |
* We actually need a session lock, so that the lock will persist across |
| 2038 |
* the commit/restart below. (We could almost get away with letting the |
| 2039 |
* lock be released at commit, except that someone could try to move |
| 2040 |
* relations of the DB back into the old directory while we rmtree() it.) |
| 2041 |
*/ |
| 2042 |
LockSharedObjectForSession(DatabaseRelationId, db_id, 0, |
| 2043 |
AccessExclusiveLock); |
| 2044 |
|
| 2045 |
/* |
| 2046 |
* Permission checks |
| 2047 |
*/ |
| 2048 |
if (!object_ownercheck(DatabaseRelationId, db_id, GetUserId())) |
| 2049 |
aclcheck_error(ACLCHECK_NOT_OWNER, OBJECT_DATABASE, |
| 2050 |
dbname); |
| 2051 |
|
| 2052 |
/* |
| 2053 |
* Obviously can't move the tables of my own database |
| 2054 |
*/ |
| 2055 |
if (db_id == MyDatabaseId) |
| 2056 |
ereport(ERROR, |
| 2057 |
(errcode(ERRCODE_OBJECT_IN_USE), |
| 2058 |
errmsg("cannot change the tablespace of the currently open database"))); |
| 2059 |
|
| 2060 |
/* |
| 2061 |
* Get tablespace's oid |
| 2062 |
*/ |
| 2063 |
dst_tblspcoid = get_tablespace_oid(tblspcname, false); |
| 2064 |
|
| 2065 |
/* |
| 2066 |
* Permission checks |
| 2067 |
*/ |
| 2068 |
aclresult = object_aclcheck(TableSpaceRelationId, dst_tblspcoid, GetUserId(), |
| 2069 |
ACL_CREATE); |
| 2070 |
if (aclresult != ACLCHECK_OK) |
| 2071 |
aclcheck_error(aclresult, OBJECT_TABLESPACE, |
| 2072 |
tblspcname); |
| 2073 |
|
| 2074 |
/* |
| 2075 |
* pg_global must never be the default tablespace |
| 2076 |
*/ |
| 2077 |
if (dst_tblspcoid == GLOBALTABLESPACE_OID) |
| 2078 |
ereport(ERROR, |
| 2079 |
(errcode(ERRCODE_INVALID_PARAMETER_VALUE), |
| 2080 |
errmsg("pg_global cannot be used as default tablespace"))); |
| 2081 |
|
| 2082 |
/* |
| 2083 |
* No-op if same tablespace |
| 2084 |
*/ |
| 2085 |
if (src_tblspcoid == dst_tblspcoid) |
| 2086 |
{ |
| 2087 |
table_close(pgdbrel, NoLock); |
| 2088 |
UnlockSharedObjectForSession(DatabaseRelationId, db_id, 0, |
| 2089 |
AccessExclusiveLock); |
| 2090 |
return; |
| 2091 |
} |
| 2092 |
|
| 2093 |
/* |
| 2094 |
* Check for other backends in the target database. (Because we hold the |
| 2095 |
* database lock, no new ones can start after this.) |
| 2096 |
* |
| 2097 |
* As in CREATE DATABASE, check this after other error conditions. |
| 2098 |
*/ |
| 2099 |
if (CountOtherDBBackends(db_id, ¬herbackends, &npreparedxacts)) |
| 2100 |
ereport(ERROR, |
| 2101 |
(errcode(ERRCODE_OBJECT_IN_USE), |
| 2102 |
errmsg("database \"%s\" is being accessed by other users", |
| 2103 |
dbname), |
| 2104 |
errdetail_busy_db(notherbackends, npreparedxacts))); |
| 2105 |
|
| 2106 |
/* |
| 2107 |
* Get old and new database paths |
| 2108 |
*/ |
| 2109 |
src_dbpath = GetDatabasePath(db_id, src_tblspcoid); |
| 2110 |
dst_dbpath = GetDatabasePath(db_id, dst_tblspcoid); |
| 2111 |
|
| 2112 |
/* |
| 2113 |
* Force a checkpoint before proceeding. This will force all dirty |
| 2114 |
* buffers, including those of unlogged tables, out to disk, to ensure |
| 2115 |
* source database is up-to-date on disk for the copy. |
| 2116 |
* FlushDatabaseBuffers() would suffice for that, but we also want to |
| 2117 |
* process any pending unlink requests. Otherwise, the check for existing |
| 2118 |
* files in the target directory might fail unnecessarily, not to mention |
| 2119 |
* that the copy might fail due to source files getting deleted under it. |
| 2120 |
* On Windows, this also ensures that background procs don't hold any open |
| 2121 |
* files, which would cause rmdir() to fail. |
| 2122 |
*/ |
| 2123 |
RequestCheckpoint(CHECKPOINT_IMMEDIATE | CHECKPOINT_FORCE | CHECKPOINT_WAIT |
| 2124 |
| CHECKPOINT_FLUSH_ALL); |
| 2125 |
|
| 2126 |
/* Close all smgr fds in all backends. */ |
| 2127 |
WaitForProcSignalBarrier(EmitProcSignalBarrier(PROCSIGNAL_BARRIER_SMGRRELEASE)); |
| 2128 |
|
| 2129 |
/* |
| 2130 |
* Now drop all buffers holding data of the target database; they should |
| 2131 |
* no longer be dirty so DropDatabaseBuffers is safe. |
| 2132 |
* |
| 2133 |
* It might seem that we could just let these buffers age out of shared |
| 2134 |
* buffers naturally, since they should not get referenced anymore. The |
| 2135 |
* problem with that is that if the user later moves the database back to |
| 2136 |
* its original tablespace, any still-surviving buffers would appear to |
| 2137 |
* contain valid data again --- but they'd be missing any changes made in |
| 2138 |
* the database while it was in the new tablespace. In any case, freeing |
| 2139 |
* buffers that should never be used again seems worth the cycles. |
| 2140 |
* |
| 2141 |
* Note: it'd be sufficient to get rid of buffers matching db_id and |
| 2142 |
* src_tblspcoid, but bufmgr.c presently provides no API for that. |
| 2143 |
*/ |
| 2144 |
DropDatabaseBuffers(db_id); |
| 2145 |
|
| 2146 |
/* |
| 2147 |
* Check for existence of files in the target directory, i.e., objects of |
| 2148 |
* this database that are already in the target tablespace. We can't |
| 2149 |
* allow the move in such a case, because we would need to change those |
| 2150 |
* relations' pg_class.reltablespace entries to zero, and we don't have |
| 2151 |
* access to the DB's pg_class to do so. |
| 2152 |
*/ |
| 2153 |
dstdir = AllocateDir(dst_dbpath); |
| 2154 |
if (dstdir != NULL) |
| 2155 |
{ |
| 2156 |
while ((xlde = ReadDir(dstdir, dst_dbpath)) != NULL) |
| 2157 |
{ |
| 2158 |
if (strcmp(xlde->d_name, ".") == 0 || |
| 2159 |
strcmp(xlde->d_name, "..") == 0) |
| 2160 |
continue; |
| 2161 |
|
| 2162 |
ereport(ERROR, |
| 2163 |
(errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), |
| 2164 |
errmsg("some relations of database \"%s\" are already in tablespace \"%s\"", |
| 2165 |
dbname, tblspcname), |
| 2166 |
errhint("You must move them back to the database's default tablespace before using this command."))); |
| 2167 |
} |
| 2168 |
|
| 2169 |
FreeDir(dstdir); |
| 2170 |
|
| 2171 |
/* |
| 2172 |
* The directory exists but is empty. We must remove it before using |
| 2173 |
* the copydir function. |
| 2174 |
*/ |
| 2175 |
if (rmdir(dst_dbpath) != 0) |
| 2176 |
elog(ERROR, "could not remove directory \"%s\": %m", |
| 2177 |
dst_dbpath); |
| 2178 |
} |
| 2179 |
|
| 2180 |
/* |
| 2181 |
* Use an ENSURE block to make sure we remove the debris if the copy fails |
| 2182 |
* (eg, due to out-of-disk-space). This is not a 100% solution, because |
| 2183 |
* of the possibility of failure during transaction commit, but it should |
| 2184 |
* handle most scenarios. |
| 2185 |
*/ |
| 2186 |
fparms.dest_dboid = db_id; |
| 2187 |
fparms.dest_tsoid = dst_tblspcoid; |
| 2188 |
PG_ENSURE_ERROR_CLEANUP(movedb_failure_callback, |
| 2189 |
PointerGetDatum(&fparms)); |
| 2190 |
{ |
| 2191 |
Datum new_record[Natts_pg_database] = {0}; |
| 2192 |
bool new_record_nulls[Natts_pg_database] = {0}; |
| 2193 |
bool new_record_repl[Natts_pg_database] = {0}; |
| 2194 |
|
| 2195 |
/* |
| 2196 |
* Copy files from the old tablespace to the new one |
| 2197 |
*/ |
| 2198 |
copydir(src_dbpath, dst_dbpath, false); |
| 2199 |
|
| 2200 |
/* |
| 2201 |
* Record the filesystem change in XLOG |
| 2202 |
*/ |
| 2203 |
{ |
| 2204 |
xl_dbase_create_file_copy_rec xlrec; |
| 2205 |
|
| 2206 |
xlrec.db_id = db_id; |
| 2207 |
xlrec.tablespace_id = dst_tblspcoid; |
| 2208 |
xlrec.src_db_id = db_id; |
| 2209 |
xlrec.src_tablespace_id = src_tblspcoid; |
| 2210 |
|
| 2211 |
XLogBeginInsert(); |
| 2212 |
XLogRegisterData(&xlrec, |
| 2213 |
sizeof(xl_dbase_create_file_copy_rec)); |
| 2214 |
|
| 2215 |
(void) XLogInsert(RM_DBASE_ID, |
| 2216 |
XLOG_DBASE_CREATE_FILE_COPY | XLR_SPECIAL_REL_UPDATE); |
| 2217 |
} |
| 2218 |
|
| 2219 |
/* |
| 2220 |
* Update the database's pg_database tuple |
| 2221 |
*/ |
| 2222 |
ScanKeyInit(&scankey, |
| 2223 |
Anum_pg_database_datname, |
| 2224 |
BTEqualStrategyNumber, F_NAMEEQ, |
| 2225 |
CStringGetDatum(dbname)); |
| 2226 |
sysscan = systable_beginscan(pgdbrel, DatabaseNameIndexId, true, |
| 2227 |
NULL, 1, &scankey); |
| 2228 |
oldtuple = systable_getnext(sysscan); |
| 2229 |
if (!HeapTupleIsValid(oldtuple)) /* shouldn't happen... */ |
| 2230 |
ereport(ERROR, |
| 2231 |
(errcode(ERRCODE_UNDEFINED_DATABASE), |
| 2232 |
errmsg("database \"%s\" does not exist", dbname))); |
| 2233 |
LockTuple(pgdbrel, &oldtuple->t_self, InplaceUpdateTupleLock); |
| 2234 |
|
| 2235 |
new_record[Anum_pg_database_dattablespace - 1] = ObjectIdGetDatum(dst_tblspcoid); |
| 2236 |
new_record_repl[Anum_pg_database_dattablespace - 1] = true; |
| 2237 |
|
| 2238 |
newtuple = heap_modify_tuple(oldtuple, RelationGetDescr(pgdbrel), |
| 2239 |
new_record, |
| 2240 |
new_record_nulls, new_record_repl); |
| 2241 |
CatalogTupleUpdate(pgdbrel, &oldtuple->t_self, newtuple); |
| 2242 |
UnlockTuple(pgdbrel, &oldtuple->t_self, InplaceUpdateTupleLock); |
| 2243 |
|
| 2244 |
InvokeObjectPostAlterHook(DatabaseRelationId, db_id, 0); |
| 2245 |
|
| 2246 |
systable_endscan(sysscan); |
| 2247 |
|
| 2248 |
/* |
| 2249 |
* Force another checkpoint here. As in CREATE DATABASE, this is to |
| 2250 |
* ensure that we don't have to replay a committed |
| 2251 |
* XLOG_DBASE_CREATE_FILE_COPY operation, which would cause us to lose |
| 2252 |
* any unlogged operations done in the new DB tablespace before the |
| 2253 |
* next checkpoint. |
| 2254 |
*/ |
| 2255 |
RequestCheckpoint(CHECKPOINT_IMMEDIATE | CHECKPOINT_FORCE | CHECKPOINT_WAIT); |
| 2256 |
|
| 2257 |
/* |
| 2258 |
* Force synchronous commit, thus minimizing the window between |
| 2259 |
* copying the database files and committal of the transaction. If we |
| 2260 |
* crash before committing, we'll leave an orphaned set of files on |
| 2261 |
* disk, which is not fatal but not good either. |
| 2262 |
*/ |
| 2263 |
ForceSyncCommit(); |
| 2264 |
|
| 2265 |
/* |
| 2266 |
* Close pg_database, but keep lock till commit. |
| 2267 |
*/ |
| 2268 |
table_close(pgdbrel, NoLock); |
| 2269 |
} |
| 2270 |
PG_END_ENSURE_ERROR_CLEANUP(movedb_failure_callback, |
| 2271 |
PointerGetDatum(&fparms)); |
| 2272 |
|
| 2273 |
/* |
| 2274 |
* Commit the transaction so that the pg_database update is committed. If |
| 2275 |
* we crash while removing files, the database won't be corrupt, we'll |
| 2276 |
* just leave some orphaned files in the old directory. |
| 2277 |
* |
| 2278 |
* (This is OK because we know we aren't inside a transaction block.) |
| 2279 |
* |
| 2280 |
* XXX would it be safe/better to do this inside the ensure block? Not |
| 2281 |
* convinced it's a good idea; consider elog just after the transaction |
| 2282 |
* really commits. |
| 2283 |
*/ |
| 2284 |
PopActiveSnapshot(); |
| 2285 |
CommitTransactionCommand(); |
| 2286 |
|
| 2287 |
/* Start new transaction for the remaining work; don't need a snapshot */ |
| 2288 |
StartTransactionCommand(); |
| 2289 |
|
| 2290 |
/* |
| 2291 |
* Remove files from the old tablespace |
| 2292 |
*/ |
| 2293 |
if (!rmtree(src_dbpath, true)) |
| 2294 |
ereport(WARNING, |
| 2295 |
(errmsg("some useless files may be left behind in old database directory \"%s\"", |
| 2296 |
src_dbpath))); |
| 2297 |
|
| 2298 |
/* |
| 2299 |
* Record the filesystem change in XLOG |
| 2300 |
*/ |
| 2301 |
{ |
| 2302 |
xl_dbase_drop_rec xlrec; |
| 2303 |
|
| 2304 |
xlrec.db_id = db_id; |
| 2305 |
xlrec.ntablespaces = 1; |
| 2306 |
|
| 2307 |
XLogBeginInsert(); |
| 2308 |
XLogRegisterData(&xlrec, sizeof(xl_dbase_drop_rec)); |
| 2309 |
XLogRegisterData(&src_tblspcoid, sizeof(Oid)); |
| 2310 |
|
| 2311 |
(void) XLogInsert(RM_DBASE_ID, |
| 2312 |
XLOG_DBASE_DROP | XLR_SPECIAL_REL_UPDATE); |
| 2313 |
} |
| 2314 |
|
| 2315 |
/* Now it's safe to release the database lock */ |
| 2316 |
UnlockSharedObjectForSession(DatabaseRelationId, db_id, 0, |
| 2317 |
AccessExclusiveLock); |
| 2318 |
|
| 2319 |
pfree(src_dbpath); |
| 2320 |
pfree(dst_dbpath); |
| 2321 |
} |
| 2322 |
|
| 2323 |
/* Error cleanup callback for movedb */ |
| 2324 |
static void |
| 2325 |
movedb_failure_callback(int code, Datum arg) |
| 2326 |
{ |
| 2327 |
movedb_failure_params *fparms = (movedb_failure_params *) DatumGetPointer(arg); |
| 2328 |
char *dstpath; |
| 2329 |
|
| 2330 |
/* Get rid of anything we managed to copy to the target directory */ |
| 2331 |
dstpath = GetDatabasePath(fparms->dest_dboid, fparms->dest_tsoid); |
| 2332 |
|
| 2333 |
(void) rmtree(dstpath, true); |
| 2334 |
|
| 2335 |
pfree(dstpath); |
| 2336 |
} |
| 2337 |
|
| 2338 |
/* |
| 2339 |
* Process options and call dropdb function. |
| 2340 |
*/ |
| 2341 |
void |
| 2342 |
DropDatabase(ParseState *pstate, DropdbStmt *stmt) |
| 2343 |
{ |
| 2344 |
bool force = false; |
| 2345 |
ListCell *lc; |
| 2346 |
|
| 2347 |
foreach(lc, stmt->options) |
| 2348 |
{ |
| 2349 |
DefElem *opt = (DefElem *) lfirst(lc); |
| 2350 |
|
| 2351 |
if (strcmp(opt->defname, "force") == 0) |
| 2352 |
force = true; |
| 2353 |
else |
| 2354 |
ereport(ERROR, |
| 2355 |
(errcode(ERRCODE_SYNTAX_ERROR), |
| 2356 |
errmsg("unrecognized %s option \"%s\"", |
| 2357 |
"DROP DATABASE", opt->defname), |
| 2358 |
parser_errposition(pstate, opt->location))); |
| 2359 |
} |
| 2360 |
|
| 2361 |
dropdb(stmt->dbname, stmt->missing_ok, force); |
| 2362 |
} |
| 2363 |
|
| 2364 |
/* |
| 2365 |
* ALTER DATABASE name ... |
| 2366 |
*/ |
| 2367 |
Oid |
| 2368 |
AlterDatabase(ParseState *pstate, AlterDatabaseStmt *stmt, bool isTopLevel) |
| 2369 |
{ |
| 2370 |
Relation rel; |
| 2371 |
Oid dboid; |
| 2372 |
HeapTuple tuple, |
| 2373 |
newtuple; |
| 2374 |
Form_pg_database datform; |
| 2375 |
ScanKeyData scankey; |
| 2376 |
SysScanDesc scan; |
| 2377 |
ListCell *option; |
| 2378 |
bool dbistemplate = false; |
| 2379 |
bool dballowconnections = true; |
| 2380 |
int dbconnlimit = DATCONNLIMIT_UNLIMITED; |
| 2381 |
DefElem *distemplate = NULL; |
| 2382 |
DefElem *dallowconnections = NULL; |
| 2383 |
DefElem *dconnlimit = NULL; |
| 2384 |
DefElem *dtablespace = NULL; |
| 2385 |
Datum new_record[Natts_pg_database] = {0}; |
| 2386 |
bool new_record_nulls[Natts_pg_database] = {0}; |
| 2387 |
bool new_record_repl[Natts_pg_database] = {0}; |
| 2388 |
|
| 2389 |
/* Extract options from the statement node tree */ |
| 2390 |
foreach(option, stmt->options) |
| 2391 |
{ |
| 2392 |
DefElem *defel = (DefElem *) lfirst(option); |
| 2393 |
|
| 2394 |
if (strcmp(defel->defname, "is_template") == 0) |
| 2395 |
{ |
| 2396 |
if (distemplate) |
| 2397 |
errorConflictingDefElem(defel, pstate); |
| 2398 |
distemplate = defel; |
| 2399 |
} |
| 2400 |
else if (strcmp(defel->defname, "allow_connections") == 0) |
| 2401 |
{ |
| 2402 |
if (dallowconnections) |
| 2403 |
errorConflictingDefElem(defel, pstate); |
| 2404 |
dallowconnections = defel; |
| 2405 |
} |
| 2406 |
else if (strcmp(defel->defname, "connection_limit") == 0) |
| 2407 |
{ |
| 2408 |
if (dconnlimit) |
| 2409 |
errorConflictingDefElem(defel, pstate); |
| 2410 |
dconnlimit = defel; |
| 2411 |
} |
| 2412 |
else if (strcmp(defel->defname, "tablespace") == 0) |
| 2413 |
{ |
| 2414 |
if (dtablespace) |
| 2415 |
errorConflictingDefElem(defel, pstate); |
| 2416 |
dtablespace = defel; |
| 2417 |
} |
| 2418 |
else |
| 2419 |
ereport(ERROR, |
| 2420 |
(errcode(ERRCODE_SYNTAX_ERROR), |
| 2421 |
errmsg("option \"%s\" not recognized", defel->defname), |
| 2422 |
parser_errposition(pstate, defel->location))); |
| 2423 |
} |
| 2424 |
|
| 2425 |
if (dtablespace) |
| 2426 |
{ |
| 2427 |
/* |
| 2428 |
* While the SET TABLESPACE syntax doesn't allow any other options, |
| 2429 |
* somebody could write "WITH TABLESPACE ...". Forbid any other |
| 2430 |
* options from being specified in that case. |
| 2431 |
*/ |
| 2432 |
if (list_length(stmt->options) != 1) |
| 2433 |
ereport(ERROR, |
| 2434 |
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED), |
| 2435 |
errmsg("option \"%s\" cannot be specified with other options", |
| 2436 |
dtablespace->defname), |
| 2437 |
parser_errposition(pstate, dtablespace->location))); |
| 2438 |
/* this case isn't allowed within a transaction block */ |
| 2439 |
PreventInTransactionBlock(isTopLevel, "ALTER DATABASE SET TABLESPACE"); |
| 2440 |
movedb(stmt->dbname, defGetString(dtablespace)); |
| 2441 |
return InvalidOid; |
| 2442 |
} |
| 2443 |
|
| 2444 |
if (distemplate && distemplate->arg) |
| 2445 |
dbistemplate = defGetBoolean(distemplate); |
| 2446 |
if (dallowconnections && dallowconnections->arg) |
| 2447 |
dballowconnections = defGetBoolean(dallowconnections); |
| 2448 |
if (dconnlimit && dconnlimit->arg) |
| 2449 |
{ |
| 2450 |
dbconnlimit = defGetInt32(dconnlimit); |
| 2451 |
if (dbconnlimit < DATCONNLIMIT_UNLIMITED) |
| 2452 |
ereport(ERROR, |
| 2453 |
(errcode(ERRCODE_INVALID_PARAMETER_VALUE), |
| 2454 |
errmsg("invalid connection limit: %d", dbconnlimit))); |
| 2455 |
} |
| 2456 |
|
| 2457 |
/* |
| 2458 |
* Get the old tuple. We don't need a lock on the database per se, |
| 2459 |
* because we're not going to do anything that would mess up incoming |
| 2460 |
* connections. |
| 2461 |
*/ |
| 2462 |
rel = table_open(DatabaseRelationId, RowExclusiveLock); |
| 2463 |
ScanKeyInit(&scankey, |
| 2464 |
Anum_pg_database_datname, |
| 2465 |
BTEqualStrategyNumber, F_NAMEEQ, |
| 2466 |
CStringGetDatum(stmt->dbname)); |
| 2467 |
scan = systable_beginscan(rel, DatabaseNameIndexId, true, |
| 2468 |
NULL, 1, &scankey); |
| 2469 |
tuple = systable_getnext(scan); |
| 2470 |
if (!HeapTupleIsValid(tuple)) |
| 2471 |
ereport(ERROR, |
| 2472 |
(errcode(ERRCODE_UNDEFINED_DATABASE), |
| 2473 |
errmsg("database \"%s\" does not exist", stmt->dbname))); |
| 2474 |
LockTuple(rel, &tuple->t_self, InplaceUpdateTupleLock); |
| 2475 |
|
| 2476 |
datform = (Form_pg_database) GETSTRUCT(tuple); |
| 2477 |
dboid = datform->oid; |
| 2478 |
|
| 2479 |
if (database_is_invalid_form(datform)) |
| 2480 |
{ |
| 2481 |
ereport(FATAL, |
| 2482 |
errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), |
| 2483 |
errmsg("cannot alter invalid database \"%s\"", stmt->dbname), |
| 2484 |
errhint("Use DROP DATABASE to drop invalid databases.")); |
| 2485 |
} |
| 2486 |
|
| 2487 |
if (!object_ownercheck(DatabaseRelationId, dboid, GetUserId())) |
| 2488 |
aclcheck_error(ACLCHECK_NOT_OWNER, OBJECT_DATABASE, |
| 2489 |
stmt->dbname); |
| 2490 |
|
| 2491 |
/* |
| 2492 |
* In order to avoid getting locked out and having to go through |
| 2493 |
* standalone mode, we refuse to disallow connections to the database |
| 2494 |
* we're currently connected to. Lockout can still happen with concurrent |
| 2495 |
* sessions but the likeliness of that is not high enough to worry about. |
| 2496 |
*/ |
| 2497 |
if (!dballowconnections && dboid == MyDatabaseId) |
| 2498 |
ereport(ERROR, |
| 2499 |
(errcode(ERRCODE_INVALID_PARAMETER_VALUE), |
| 2500 |
errmsg("cannot disallow connections for current database"))); |
| 2501 |
|
| 2502 |
/* |
| 2503 |
* Build an updated tuple, perusing the information just obtained |
| 2504 |
*/ |
| 2505 |
if (distemplate) |
| 2506 |
{ |
| 2507 |
new_record[Anum_pg_database_datistemplate - 1] = BoolGetDatum(dbistemplate); |
| 2508 |
new_record_repl[Anum_pg_database_datistemplate - 1] = true; |
| 2509 |
} |
| 2510 |
if (dallowconnections) |
| 2511 |
{ |
| 2512 |
new_record[Anum_pg_database_datallowconn - 1] = BoolGetDatum(dballowconnections); |
| 2513 |
new_record_repl[Anum_pg_database_datallowconn - 1] = true; |
| 2514 |
} |
| 2515 |
if (dconnlimit) |
| 2516 |
{ |
| 2517 |
new_record[Anum_pg_database_datconnlimit - 1] = Int32GetDatum(dbconnlimit); |
| 2518 |
new_record_repl[Anum_pg_database_datconnlimit - 1] = true; |
| 2519 |
} |
| 2520 |
|
| 2521 |
newtuple = heap_modify_tuple(tuple, RelationGetDescr(rel), new_record, |
| 2522 |
new_record_nulls, new_record_repl); |
| 2523 |
CatalogTupleUpdate(rel, &tuple->t_self, newtuple); |
| 2524 |
UnlockTuple(rel, &tuple->t_self, InplaceUpdateTupleLock); |
| 2525 |
|
| 2526 |
InvokeObjectPostAlterHook(DatabaseRelationId, dboid, 0); |
| 2527 |
|
| 2528 |
systable_endscan(scan); |
| 2529 |
|
| 2530 |
/* Close pg_database, but keep lock till commit */ |
| 2531 |
table_close(rel, NoLock); |
| 2532 |
|
| 2533 |
return dboid; |
| 2534 |
} |
| 2535 |
|
| 2536 |
|
| 2537 |
/* |
| 2538 |
* ALTER DATABASE name REFRESH COLLATION VERSION |
| 2539 |
*/ |
| 2540 |
ObjectAddress |
| 2541 |
AlterDatabaseRefreshColl(AlterDatabaseRefreshCollStmt *stmt) |
| 2542 |
{ |
| 2543 |
Relation rel; |
| 2544 |
ScanKeyData scankey; |
| 2545 |
SysScanDesc scan; |
| 2546 |
Oid db_id; |
| 2547 |
HeapTuple tuple; |
| 2548 |
Form_pg_database datForm; |
| 2549 |
ObjectAddress address; |
| 2550 |
Datum datum; |
| 2551 |
bool isnull; |
| 2552 |
char *oldversion; |
| 2553 |
char *newversion; |
| 2554 |
|
| 2555 |
rel = table_open(DatabaseRelationId, RowExclusiveLock); |
| 2556 |
ScanKeyInit(&scankey, |
| 2557 |
Anum_pg_database_datname, |
| 2558 |
BTEqualStrategyNumber, F_NAMEEQ, |
| 2559 |
CStringGetDatum(stmt->dbname)); |
| 2560 |
scan = systable_beginscan(rel, DatabaseNameIndexId, true, |
| 2561 |
NULL, 1, &scankey); |
| 2562 |
tuple = systable_getnext(scan); |
| 2563 |
if (!HeapTupleIsValid(tuple)) |
| 2564 |
ereport(ERROR, |
| 2565 |
(errcode(ERRCODE_UNDEFINED_DATABASE), |
| 2566 |
errmsg("database \"%s\" does not exist", stmt->dbname))); |
| 2567 |
|
| 2568 |
datForm = (Form_pg_database) GETSTRUCT(tuple); |
| 2569 |
db_id = datForm->oid; |
| 2570 |
|
| 2571 |
if (!object_ownercheck(DatabaseRelationId, db_id, GetUserId())) |
| 2572 |
aclcheck_error(ACLCHECK_NOT_OWNER, OBJECT_DATABASE, |
| 2573 |
stmt->dbname); |
| 2574 |
LockTuple(rel, &tuple->t_self, InplaceUpdateTupleLock); |
| 2575 |
|
| 2576 |
datum = heap_getattr(tuple, Anum_pg_database_datcollversion, RelationGetDescr(rel), &isnull); |
| 2577 |
oldversion = isnull ? NULL : TextDatumGetCString(datum); |
| 2578 |
|
| 2579 |
if (datForm->datlocprovider == COLLPROVIDER_LIBC) |
| 2580 |
{ |
| 2581 |
datum = heap_getattr(tuple, Anum_pg_database_datcollate, RelationGetDescr(rel), &isnull); |
| 2582 |
if (isnull) |
| 2583 |
elog(ERROR, "unexpected null in pg_database"); |
| 2584 |
} |
| 2585 |
else |
| 2586 |
{ |
| 2587 |
datum = heap_getattr(tuple, Anum_pg_database_datlocale, RelationGetDescr(rel), &isnull); |
| 2588 |
if (isnull) |
| 2589 |
elog(ERROR, "unexpected null in pg_database"); |
| 2590 |
} |
| 2591 |
|
| 2592 |
newversion = get_collation_actual_version(datForm->datlocprovider, |
| 2593 |
TextDatumGetCString(datum)); |
| 2594 |
|
| 2595 |
/* cannot change from NULL to non-NULL or vice versa */ |
| 2596 |
if ((!oldversion && newversion) || (oldversion && !newversion)) |
| 2597 |
elog(ERROR, "invalid collation version change"); |
| 2598 |
else if (oldversion && newversion && strcmp(newversion, oldversion) != 0) |
| 2599 |
{ |
| 2600 |
bool nulls[Natts_pg_database] = {0}; |
| 2601 |
bool replaces[Natts_pg_database] = {0}; |
| 2602 |
Datum values[Natts_pg_database] = {0}; |
| 2603 |
HeapTuple newtuple; |
| 2604 |
|
| 2605 |
ereport(NOTICE, |
| 2606 |
(errmsg("changing version from %s to %s", |
| 2607 |
oldversion, newversion))); |
| 2608 |
|
| 2609 |
values[Anum_pg_database_datcollversion - 1] = CStringGetTextDatum(newversion); |
| 2610 |
replaces[Anum_pg_database_datcollversion - 1] = true; |
| 2611 |
|
| 2612 |
newtuple = heap_modify_tuple(tuple, RelationGetDescr(rel), |
| 2613 |
values, nulls, replaces); |
| 2614 |
CatalogTupleUpdate(rel, &tuple->t_self, newtuple); |
| 2615 |
heap_freetuple(newtuple); |
| 2616 |
} |
| 2617 |
else |
| 2618 |
ereport(NOTICE, |
| 2619 |
(errmsg("version has not changed"))); |
| 2620 |
UnlockTuple(rel, &tuple->t_self, InplaceUpdateTupleLock); |
| 2621 |
|
| 2622 |
InvokeObjectPostAlterHook(DatabaseRelationId, db_id, 0); |
| 2623 |
|
| 2624 |
ObjectAddressSet(address, DatabaseRelationId, db_id); |
| 2625 |
|
| 2626 |
systable_endscan(scan); |
| 2627 |
|
| 2628 |
table_close(rel, NoLock); |
| 2629 |
|
| 2630 |
return address; |
| 2631 |
} |
| 2632 |
|
| 2633 |
|
| 2634 |
/* |
| 2635 |
* ALTER DATABASE name SET ... |
| 2636 |
*/ |
| 2637 |
Oid |
| 2638 |
AlterDatabaseSet(AlterDatabaseSetStmt *stmt) |
| 2639 |
{ |
| 2640 |
Oid datid = get_database_oid(stmt->dbname, false); |
| 2641 |
|
| 2642 |
/* |
| 2643 |
* Obtain a lock on the database and make sure it didn't go away in the |
| 2644 |
* meantime. |
| 2645 |
*/ |
| 2646 |
shdepLockAndCheckObject(DatabaseRelationId, datid); |
| 2647 |
|
| 2648 |
if (!object_ownercheck(DatabaseRelationId, datid, GetUserId())) |
| 2649 |
aclcheck_error(ACLCHECK_NOT_OWNER, OBJECT_DATABASE, |
| 2650 |
stmt->dbname); |
| 2651 |
|
| 2652 |
AlterSetting(datid, InvalidOid, stmt->setstmt); |
| 2653 |
|
| 2654 |
UnlockSharedObject(DatabaseRelationId, datid, 0, AccessShareLock); |
| 2655 |
|
| 2656 |
return datid; |
| 2657 |
} |
| 2658 |
|
| 2659 |
|
| 2660 |
/* |
| 2661 |
* ALTER DATABASE name OWNER TO newowner |
| 2662 |
*/ |
| 2663 |
ObjectAddress |
| 2664 |
AlterDatabaseOwner(const char *dbname, Oid newOwnerId) |
| 2665 |
{ |
| 2666 |
Oid db_id; |
| 2667 |
HeapTuple tuple; |
| 2668 |
Relation rel; |
| 2669 |
ScanKeyData scankey; |
| 2670 |
SysScanDesc scan; |
| 2671 |
Form_pg_database datForm; |
| 2672 |
ObjectAddress address; |
| 2673 |
|
| 2674 |
/* |
| 2675 |
* Get the old tuple. We don't need a lock on the database per se, |
| 2676 |
* because we're not going to do anything that would mess up incoming |
| 2677 |
* connections. |
| 2678 |
*/ |
| 2679 |
rel = table_open(DatabaseRelationId, RowExclusiveLock); |
| 2680 |
ScanKeyInit(&scankey, |
| 2681 |
Anum_pg_database_datname, |
| 2682 |
BTEqualStrategyNumber, F_NAMEEQ, |
| 2683 |
CStringGetDatum(dbname)); |
| 2684 |
scan = systable_beginscan(rel, DatabaseNameIndexId, true, |
| 2685 |
NULL, 1, &scankey); |
| 2686 |
tuple = systable_getnext(scan); |
| 2687 |
if (!HeapTupleIsValid(tuple)) |
| 2688 |
ereport(ERROR, |
| 2689 |
(errcode(ERRCODE_UNDEFINED_DATABASE), |
| 2690 |
errmsg("database \"%s\" does not exist", dbname))); |
| 2691 |
|
| 2692 |
datForm = (Form_pg_database) GETSTRUCT(tuple); |
| 2693 |
db_id = datForm->oid; |
| 2694 |
|
| 2695 |
/* |
| 2696 |
* If the new owner is the same as the existing owner, consider the |
| 2697 |
* command to have succeeded. This is to be consistent with other |
| 2698 |
* objects. |
| 2699 |
*/ |
| 2700 |
if (datForm->datdba != newOwnerId) |
| 2701 |
{ |
| 2702 |
Datum repl_val[Natts_pg_database]; |
| 2703 |
bool repl_null[Natts_pg_database] = {0}; |
| 2704 |
bool repl_repl[Natts_pg_database] = {0}; |
| 2705 |
Acl *newAcl; |
| 2706 |
Datum aclDatum; |
| 2707 |
bool isNull; |
| 2708 |
HeapTuple newtuple; |
| 2709 |
|
| 2710 |
/* Otherwise, must be owner of the existing object */ |
| 2711 |
if (!object_ownercheck(DatabaseRelationId, db_id, GetUserId())) |
| 2712 |
aclcheck_error(ACLCHECK_NOT_OWNER, OBJECT_DATABASE, |
| 2713 |
dbname); |
| 2714 |
|
| 2715 |
/* Must be able to become new owner */ |
| 2716 |
check_can_set_role(GetUserId(), newOwnerId); |
| 2717 |
|
| 2718 |
/* |
| 2719 |
* must have createdb rights |
| 2720 |
* |
| 2721 |
* NOTE: This is different from other alter-owner checks in that the |
| 2722 |
* current user is checked for createdb privileges instead of the |
| 2723 |
* destination owner. This is consistent with the CREATE case for |
| 2724 |
* databases. Because superusers will always have this right, we need |
| 2725 |
* no special case for them. |
| 2726 |
*/ |
| 2727 |
if (!have_createdb_privilege()) |
| 2728 |
ereport(ERROR, |
| 2729 |
(errcode(ERRCODE_INSUFFICIENT_PRIVILEGE), |
| 2730 |
errmsg("permission denied to change owner of database"))); |
| 2731 |
|
| 2732 |
LockTuple(rel, &tuple->t_self, InplaceUpdateTupleLock); |
| 2733 |
|
| 2734 |
repl_repl[Anum_pg_database_datdba - 1] = true; |
| 2735 |
repl_val[Anum_pg_database_datdba - 1] = ObjectIdGetDatum(newOwnerId); |
| 2736 |
|
| 2737 |
/* |
| 2738 |
* Determine the modified ACL for the new owner. This is only |
| 2739 |
* necessary when the ACL is non-null. |
| 2740 |
*/ |
| 2741 |
aclDatum = heap_getattr(tuple, |
| 2742 |
Anum_pg_database_datacl, |
| 2743 |
RelationGetDescr(rel), |
| 2744 |
&isNull); |
| 2745 |
if (!isNull) |
| 2746 |
{ |
| 2747 |
newAcl = aclnewowner(DatumGetAclP(aclDatum), |
| 2748 |
datForm->datdba, newOwnerId); |
| 2749 |
repl_repl[Anum_pg_database_datacl - 1] = true; |
| 2750 |
repl_val[Anum_pg_database_datacl - 1] = PointerGetDatum(newAcl); |
| 2751 |
} |
| 2752 |
|
| 2753 |
newtuple = heap_modify_tuple(tuple, RelationGetDescr(rel), repl_val, repl_null, repl_repl); |
| 2754 |
CatalogTupleUpdate(rel, &newtuple->t_self, newtuple); |
| 2755 |
UnlockTuple(rel, &tuple->t_self, InplaceUpdateTupleLock); |
| 2756 |
|
| 2757 |
heap_freetuple(newtuple); |
| 2758 |
|
| 2759 |
/* Update owner dependency reference */ |
| 2760 |
changeDependencyOnOwner(DatabaseRelationId, db_id, newOwnerId); |
| 2761 |
} |
| 2762 |
|
| 2763 |
InvokeObjectPostAlterHook(DatabaseRelationId, db_id, 0); |
| 2764 |
|
| 2765 |
ObjectAddressSet(address, DatabaseRelationId, db_id); |
| 2766 |
|
| 2767 |
systable_endscan(scan); |
| 2768 |
|
| 2769 |
/* Close pg_database, but keep lock till commit */ |
| 2770 |
table_close(rel, NoLock); |
| 2771 |
|
| 2772 |
return address; |
| 2773 |
} |
| 2774 |
|
| 2775 |
|
| 2776 |
Datum |
| 2777 |
pg_database_collation_actual_version(PG_FUNCTION_ARGS) |
| 2778 |
{ |
| 2779 |
Oid dbid = PG_GETARG_OID(0); |
| 2780 |
HeapTuple tp; |
| 2781 |
char datlocprovider; |
| 2782 |
Datum datum; |
| 2783 |
char *version; |
| 2784 |
|
| 2785 |
tp = SearchSysCache1(DATABASEOID, ObjectIdGetDatum(dbid)); |
| 2786 |
if (!HeapTupleIsValid(tp)) |
| 2787 |
ereport(ERROR, |
| 2788 |
(errcode(ERRCODE_UNDEFINED_OBJECT), |
| 2789 |
errmsg("database with OID %u does not exist", dbid))); |
| 2790 |
|
| 2791 |
datlocprovider = ((Form_pg_database) GETSTRUCT(tp))->datlocprovider; |
| 2792 |
|
| 2793 |
if (datlocprovider == COLLPROVIDER_LIBC) |
| 2794 |
datum = SysCacheGetAttrNotNull(DATABASEOID, tp, Anum_pg_database_datcollate); |
| 2795 |
else |
| 2796 |
datum = SysCacheGetAttrNotNull(DATABASEOID, tp, Anum_pg_database_datlocale); |
| 2797 |
|
| 2798 |
version = get_collation_actual_version(datlocprovider, |
| 2799 |
TextDatumGetCString(datum)); |
| 2800 |
|
| 2801 |
ReleaseSysCache(tp); |
| 2802 |
|
| 2803 |
if (version) |
| 2804 |
PG_RETURN_TEXT_P(cstring_to_text(version)); |
| 2805 |
else |
| 2806 |
PG_RETURN_NULL(); |
| 2807 |
} |
| 2808 |
|
| 2809 |
|
| 2810 |
/* |
| 2811 |
* Helper functions |
| 2812 |
*/ |
| 2813 |
|
| 2814 |
/* |
| 2815 |
* Look up info about the database named "name". If the database exists, |
| 2816 |
* obtain the specified lock type on it, fill in any of the remaining |
| 2817 |
* parameters that aren't NULL, and return true. If no such database, |
| 2818 |
* return false. |
| 2819 |
*/ |
| 2820 |
static bool |
| 2821 |
get_db_info(const char *name, LOCKMODE lockmode, |
| 2822 |
Oid *dbIdP, Oid *ownerIdP, |
| 2823 |
int *encodingP, bool *dbIsTemplateP, bool *dbAllowConnP, bool *dbHasLoginEvtP, |
| 2824 |
TransactionId *dbFrozenXidP, MultiXactId *dbMinMultiP, |
| 2825 |
Oid *dbTablespace, char **dbCollate, char **dbCtype, char **dbLocale, |
| 2826 |
char **dbIcurules, |
| 2827 |
char *dbLocProvider, |
| 2828 |
char **dbCollversion) |
| 2829 |
{ |
| 2830 |
bool result = false; |
| 2831 |
Relation relation; |
| 2832 |
|
| 2833 |
Assert(name); |
| 2834 |
|
| 2835 |
/* Caller may wish to grab a better lock on pg_database beforehand... */ |
| 2836 |
relation = table_open(DatabaseRelationId, AccessShareLock); |
| 2837 |
|
| 2838 |
/* |
| 2839 |
* Loop covers the rare case where the database is renamed before we can |
| 2840 |
* lock it. We try again just in case we can find a new one of the same |
| 2841 |
* name. |
| 2842 |
*/ |
| 2843 |
for (;;) |
| 2844 |
{ |
| 2845 |
ScanKeyData scanKey; |
| 2846 |
SysScanDesc scan; |
| 2847 |
HeapTuple tuple; |
| 2848 |
Oid dbOid; |
| 2849 |
|
| 2850 |
/* |
| 2851 |
* there's no syscache for database-indexed-by-name, so must do it the |
| 2852 |
* hard way |
| 2853 |
*/ |
| 2854 |
ScanKeyInit(&scanKey, |
| 2855 |
Anum_pg_database_datname, |
| 2856 |
BTEqualStrategyNumber, F_NAMEEQ, |
| 2857 |
CStringGetDatum(name)); |
| 2858 |
|
| 2859 |
scan = systable_beginscan(relation, DatabaseNameIndexId, true, |
| 2860 |
NULL, 1, &scanKey); |
| 2861 |
|
| 2862 |
tuple = systable_getnext(scan); |
| 2863 |
|
| 2864 |
if (!HeapTupleIsValid(tuple)) |
| 2865 |
{ |
| 2866 |
/* definitely no database of that name */ |
| 2867 |
systable_endscan(scan); |
| 2868 |
break; |
| 2869 |
} |
| 2870 |
|
| 2871 |
dbOid = ((Form_pg_database) GETSTRUCT(tuple))->oid; |
| 2872 |
|
| 2873 |
systable_endscan(scan); |
| 2874 |
|
| 2875 |
/* |
| 2876 |
* Now that we have a database OID, we can try to lock the DB. |
| 2877 |
*/ |
| 2878 |
if (lockmode != NoLock) |
| 2879 |
LockSharedObject(DatabaseRelationId, dbOid, 0, lockmode); |
| 2880 |
|
| 2881 |
/* |
| 2882 |
* And now, re-fetch the tuple by OID. If it's still there and still |
| 2883 |
* the same name, we win; else, drop the lock and loop back to try |
| 2884 |
* again. |
| 2885 |
*/ |
| 2886 |
tuple = SearchSysCache1(DATABASEOID, ObjectIdGetDatum(dbOid)); |
| 2887 |
if (HeapTupleIsValid(tuple)) |
| 2888 |
{ |
| 2889 |
Form_pg_database dbform = (Form_pg_database) GETSTRUCT(tuple); |
| 2890 |
|
| 2891 |
if (strcmp(name, NameStr(dbform->datname)) == 0) |
| 2892 |
{ |
| 2893 |
Datum datum; |
| 2894 |
bool isnull; |
| 2895 |
|
| 2896 |
/* oid of the database */ |
| 2897 |
if (dbIdP) |
| 2898 |
*dbIdP = dbOid; |
| 2899 |
/* oid of the owner */ |
| 2900 |
if (ownerIdP) |
| 2901 |
*ownerIdP = dbform->datdba; |
| 2902 |
/* character encoding */ |
| 2903 |
if (encodingP) |
| 2904 |
*encodingP = dbform->encoding; |
| 2905 |
/* allowed as template? */ |
| 2906 |
if (dbIsTemplateP) |
| 2907 |
*dbIsTemplateP = dbform->datistemplate; |
| 2908 |
/* Has on login event trigger? */ |
| 2909 |
if (dbHasLoginEvtP) |
| 2910 |
*dbHasLoginEvtP = dbform->dathasloginevt; |
| 2911 |
/* allowing connections? */ |
| 2912 |
if (dbAllowConnP) |
| 2913 |
*dbAllowConnP = dbform->datallowconn; |
| 2914 |
/* limit of frozen XIDs */ |
| 2915 |
if (dbFrozenXidP) |
| 2916 |
*dbFrozenXidP = dbform->datfrozenxid; |
| 2917 |
/* minimum MultiXactId */ |
| 2918 |
if (dbMinMultiP) |
| 2919 |
*dbMinMultiP = dbform->datminmxid; |
| 2920 |
/* default tablespace for this database */ |
| 2921 |
if (dbTablespace) |
| 2922 |
*dbTablespace = dbform->dattablespace; |
| 2923 |
/* default locale settings for this database */ |
| 2924 |
if (dbLocProvider) |
| 2925 |
*dbLocProvider = dbform->datlocprovider; |
| 2926 |
if (dbCollate) |
| 2927 |
{ |
| 2928 |
datum = SysCacheGetAttrNotNull(DATABASEOID, tuple, Anum_pg_database_datcollate); |
| 2929 |
*dbCollate = TextDatumGetCString(datum); |
| 2930 |
} |
| 2931 |
if (dbCtype) |
| 2932 |
{ |
| 2933 |
datum = SysCacheGetAttrNotNull(DATABASEOID, tuple, Anum_pg_database_datctype); |
| 2934 |
*dbCtype = TextDatumGetCString(datum); |
| 2935 |
} |
| 2936 |
if (dbLocale) |
| 2937 |
{ |
| 2938 |
datum = SysCacheGetAttr(DATABASEOID, tuple, Anum_pg_database_datlocale, &isnull); |
| 2939 |
if (isnull) |
| 2940 |
*dbLocale = NULL; |
| 2941 |
else |
| 2942 |
*dbLocale = TextDatumGetCString(datum); |
| 2943 |
} |
| 2944 |
if (dbIcurules) |
| 2945 |
{ |
| 2946 |
datum = SysCacheGetAttr(DATABASEOID, tuple, Anum_pg_database_daticurules, &isnull); |
| 2947 |
if (isnull) |
| 2948 |
*dbIcurules = NULL; |
| 2949 |
else |
| 2950 |
*dbIcurules = TextDatumGetCString(datum); |
| 2951 |
} |
| 2952 |
if (dbCollversion) |
| 2953 |
{ |
| 2954 |
datum = SysCacheGetAttr(DATABASEOID, tuple, Anum_pg_database_datcollversion, &isnull); |
| 2955 |
if (isnull) |
| 2956 |
*dbCollversion = NULL; |
| 2957 |
else |
| 2958 |
*dbCollversion = TextDatumGetCString(datum); |
| 2959 |
} |
| 2960 |
ReleaseSysCache(tuple); |
| 2961 |
result = true; |
| 2962 |
break; |
| 2963 |
} |
| 2964 |
/* can only get here if it was just renamed */ |
| 2965 |
ReleaseSysCache(tuple); |
| 2966 |
} |
| 2967 |
|
| 2968 |
if (lockmode != NoLock) |
| 2969 |
UnlockSharedObject(DatabaseRelationId, dbOid, 0, lockmode); |
| 2970 |
} |
| 2971 |
|
| 2972 |
table_close(relation, AccessShareLock); |
| 2973 |
|
| 2974 |
return result; |
| 2975 |
} |
| 2976 |
|
| 2977 |
/* Check if current user has createdb privileges */ |
| 2978 |
bool |
| 2979 |
have_createdb_privilege(void) |
| 2980 |
{ |
| 2981 |
bool result = false; |
| 2982 |
HeapTuple utup; |
| 2983 |
|
| 2984 |
/* Superusers can always do everything */ |
| 2985 |
if (superuser()) |
| 2986 |
return true; |
| 2987 |
|
| 2988 |
utup = SearchSysCache1(AUTHOID, ObjectIdGetDatum(GetUserId())); |
| 2989 |
if (HeapTupleIsValid(utup)) |
| 2990 |
{ |
| 2991 |
result = ((Form_pg_authid) GETSTRUCT(utup))->rolcreatedb; |
| 2992 |
ReleaseSysCache(utup); |
| 2993 |
} |
| 2994 |
return result; |
| 2995 |
} |
| 2996 |
|
| 2997 |
/* |
| 2998 |
* Remove tablespace directories |
| 2999 |
* |
| 3000 |
* We don't know what tablespaces db_id is using, so iterate through all |
| 3001 |
* tablespaces removing <tablespace>/db_id |
| 3002 |
*/ |
| 3003 |
static void |
| 3004 |
remove_dbtablespaces(Oid db_id) |
| 3005 |
{ |
| 3006 |
Relation rel; |
| 3007 |
TableScanDesc scan; |
| 3008 |
HeapTuple tuple; |
| 3009 |
List *ltblspc = NIL; |
| 3010 |
ListCell *cell; |
| 3011 |
int ntblspc; |
| 3012 |
int i; |
| 3013 |
Oid *tablespace_ids; |
| 3014 |
|
| 3015 |
rel = table_open(TableSpaceRelationId, AccessShareLock); |
| 3016 |
scan = table_beginscan_catalog(rel, 0, NULL); |
| 3017 |
while ((tuple = heap_getnext(scan, ForwardScanDirection)) != NULL) |
| 3018 |
{ |
| 3019 |
Form_pg_tablespace spcform = (Form_pg_tablespace) GETSTRUCT(tuple); |
| 3020 |
Oid dsttablespace = spcform->oid; |
| 3021 |
char *dstpath; |
| 3022 |
struct stat st; |
| 3023 |
|
| 3024 |
/* Don't mess with the global tablespace */ |
| 3025 |
if (dsttablespace == GLOBALTABLESPACE_OID) |
| 3026 |
continue; |
| 3027 |
|
| 3028 |
dstpath = GetDatabasePath(db_id, dsttablespace); |
| 3029 |
|
| 3030 |
if (lstat(dstpath, &st) < 0 || !S_ISDIR(st.st_mode)) |
| 3031 |
{ |
| 3032 |
/* Assume we can ignore it */ |
| 3033 |
pfree(dstpath); |
| 3034 |
continue; |
| 3035 |
} |
| 3036 |
|
| 3037 |
if (!rmtree(dstpath, true)) |
| 3038 |
ereport(WARNING, |
| 3039 |
(errmsg("some useless files may be left behind in old database directory \"%s\"", |
| 3040 |
dstpath))); |
| 3041 |
|
| 3042 |
ltblspc = lappend_oid(ltblspc, dsttablespace); |
| 3043 |
pfree(dstpath); |
| 3044 |
} |
| 3045 |
|
| 3046 |
ntblspc = list_length(ltblspc); |
| 3047 |
if (ntblspc == 0) |
| 3048 |
{ |
| 3049 |
table_endscan(scan); |
| 3050 |
table_close(rel, AccessShareLock); |
| 3051 |
return; |
| 3052 |
} |
| 3053 |
|
| 3054 |
tablespace_ids = (Oid *) palloc(ntblspc * sizeof(Oid)); |
| 3055 |
i = 0; |
| 3056 |
foreach(cell, ltblspc) |
| 3057 |
tablespace_ids[i++] = lfirst_oid(cell); |
| 3058 |
|
| 3059 |
/* Record the filesystem change in XLOG */ |
| 3060 |
{ |
| 3061 |
xl_dbase_drop_rec xlrec; |
| 3062 |
|
| 3063 |
xlrec.db_id = db_id; |
| 3064 |
xlrec.ntablespaces = ntblspc; |
| 3065 |
|
| 3066 |
XLogBeginInsert(); |
| 3067 |
XLogRegisterData(&xlrec, MinSizeOfDbaseDropRec); |
| 3068 |
XLogRegisterData(tablespace_ids, ntblspc * sizeof(Oid)); |
| 3069 |
|
| 3070 |
(void) XLogInsert(RM_DBASE_ID, |
| 3071 |
XLOG_DBASE_DROP | XLR_SPECIAL_REL_UPDATE); |
| 3072 |
} |
| 3073 |
|
| 3074 |
list_free(ltblspc); |
| 3075 |
pfree(tablespace_ids); |
| 3076 |
|
| 3077 |
table_endscan(scan); |
| 3078 |
table_close(rel, AccessShareLock); |
| 3079 |
} |
| 3080 |
|
| 3081 |
/* |
| 3082 |
* Check for existing files that conflict with a proposed new DB OID; |
| 3083 |
* return true if there are any |
| 3084 |
* |
| 3085 |
* If there were a subdirectory in any tablespace matching the proposed new |
| 3086 |
* OID, we'd get a create failure due to the duplicate name ... and then we'd |
| 3087 |
* try to remove that already-existing subdirectory during the cleanup in |
| 3088 |
* remove_dbtablespaces. Nuking existing files seems like a bad idea, so |
| 3089 |
* instead we make this extra check before settling on the OID of the new |
| 3090 |
* database. This exactly parallels what GetNewRelFileNumber() does for table |
| 3091 |
* relfilenumber values. |
| 3092 |
*/ |
| 3093 |
static bool |
| 3094 |
check_db_file_conflict(Oid db_id) |
| 3095 |
{ |
| 3096 |
bool result = false; |
| 3097 |
Relation rel; |
| 3098 |
TableScanDesc scan; |
| 3099 |
HeapTuple tuple; |
| 3100 |
|
| 3101 |
rel = table_open(TableSpaceRelationId, AccessShareLock); |
| 3102 |
scan = table_beginscan_catalog(rel, 0, NULL); |
| 3103 |
while ((tuple = heap_getnext(scan, ForwardScanDirection)) != NULL) |
| 3104 |
{ |
| 3105 |
Form_pg_tablespace spcform = (Form_pg_tablespace) GETSTRUCT(tuple); |
| 3106 |
Oid dsttablespace = spcform->oid; |
| 3107 |
char *dstpath; |
| 3108 |
struct stat st; |
| 3109 |
|
| 3110 |
/* Don't mess with the global tablespace */ |
| 3111 |
if (dsttablespace == GLOBALTABLESPACE_OID) |
| 3112 |
continue; |
| 3113 |
|
| 3114 |
dstpath = GetDatabasePath(db_id, dsttablespace); |
| 3115 |
|
| 3116 |
if (lstat(dstpath, &st) == 0) |
| 3117 |
{ |
| 3118 |
/* Found a conflicting file (or directory, whatever) */ |
| 3119 |
pfree(dstpath); |
| 3120 |
result = true; |
| 3121 |
break; |
| 3122 |
} |
| 3123 |
|
| 3124 |
pfree(dstpath); |
| 3125 |
} |
| 3126 |
|
| 3127 |
table_endscan(scan); |
| 3128 |
table_close(rel, AccessShareLock); |
| 3129 |
|
| 3130 |
return result; |
| 3131 |
} |
| 3132 |
|
| 3133 |
/* |
| 3134 |
* Issue a suitable errdetail message for a busy database |
| 3135 |
*/ |
| 3136 |
static int |
| 3137 |
errdetail_busy_db(int notherbackends, int npreparedxacts) |
| 3138 |
{ |
| 3139 |
if (notherbackends > 0 && npreparedxacts > 0) |
| 3140 |
|
| 3141 |
/* |
| 3142 |
* We don't deal with singular versus plural here, since gettext |
| 3143 |
* doesn't support multiple plurals in one string. |
| 3144 |
*/ |
| 3145 |
errdetail("There are %d other session(s) and %d prepared transaction(s) using the database.", |
| 3146 |
notherbackends, npreparedxacts); |
| 3147 |
else if (notherbackends > 0) |
| 3148 |
errdetail_plural("There is %d other session using the database.", |
| 3149 |
"There are %d other sessions using the database.", |
| 3150 |
notherbackends, |
| 3151 |
notherbackends); |
| 3152 |
else |
| 3153 |
errdetail_plural("There is %d prepared transaction using the database.", |
| 3154 |
"There are %d prepared transactions using the database.", |
| 3155 |
npreparedxacts, |
| 3156 |
npreparedxacts); |
| 3157 |
return 0; /* just to keep ereport macro happy */ |
| 3158 |
} |
| 3159 |
|
| 3160 |
/* |
| 3161 |
* get_database_oid - given a database name, look up the OID |
| 3162 |
* |
| 3163 |
* If missing_ok is false, throw an error if database name not found. If |
| 3164 |
* true, just return InvalidOid. |
| 3165 |
*/ |
| 3166 |
Oid |
| 3167 |
get_database_oid(const char *dbname, bool missing_ok) |
| 3168 |
{ |
| 3169 |
Relation pg_database; |
| 3170 |
ScanKeyData entry[1]; |
| 3171 |
SysScanDesc scan; |
| 3172 |
HeapTuple dbtuple; |
| 3173 |
Oid oid; |
| 3174 |
|
| 3175 |
/* |
| 3176 |
* There's no syscache for pg_database indexed by name, so we must look |
| 3177 |
* the hard way. |
| 3178 |
*/ |
| 3179 |
pg_database = table_open(DatabaseRelationId, AccessShareLock); |
| 3180 |
ScanKeyInit(&entry[0], |
| 3181 |
Anum_pg_database_datname, |
| 3182 |
BTEqualStrategyNumber, F_NAMEEQ, |
| 3183 |
CStringGetDatum(dbname)); |
| 3184 |
scan = systable_beginscan(pg_database, DatabaseNameIndexId, true, |
| 3185 |
NULL, 1, entry); |
| 3186 |
|
| 3187 |
dbtuple = systable_getnext(scan); |
| 3188 |
|
| 3189 |
/* We assume that there can be at most one matching tuple */ |
| 3190 |
if (HeapTupleIsValid(dbtuple)) |
| 3191 |
oid = ((Form_pg_database) GETSTRUCT(dbtuple))->oid; |
| 3192 |
else |
| 3193 |
oid = InvalidOid; |
| 3194 |
|
| 3195 |
systable_endscan(scan); |
| 3196 |
table_close(pg_database, AccessShareLock); |
| 3197 |
|
| 3198 |
if (!OidIsValid(oid) && !missing_ok) |
| 3199 |
ereport(ERROR, |
| 3200 |
(errcode(ERRCODE_UNDEFINED_DATABASE), |
| 3201 |
errmsg("database \"%s\" does not exist", |
| 3202 |
dbname))); |
| 3203 |
|
| 3204 |
return oid; |
| 3205 |
} |
| 3206 |
|
| 3207 |
|
| 3208 |
/* |
| 3209 |
* get_database_name - given a database OID, look up the name |
| 3210 |
* |
| 3211 |
* Returns a palloc'd string, or NULL if no such database. |
| 3212 |
*/ |
| 3213 |
char * |
| 3214 |
get_database_name(Oid dbid) |
| 3215 |
{ |
| 3216 |
HeapTuple dbtuple; |
| 3217 |
char *result; |
| 3218 |
|
| 3219 |
dbtuple = SearchSysCache1(DATABASEOID, ObjectIdGetDatum(dbid)); |
| 3220 |
if (HeapTupleIsValid(dbtuple)) |
| 3221 |
{ |
| 3222 |
result = pstrdup(NameStr(((Form_pg_database) GETSTRUCT(dbtuple))->datname)); |
| 3223 |
ReleaseSysCache(dbtuple); |
| 3224 |
} |
| 3225 |
else |
| 3226 |
result = NULL; |
| 3227 |
|
| 3228 |
return result; |
| 3229 |
} |
| 3230 |
|
| 3231 |
|
| 3232 |
/* |
| 3233 |
* While dropping a database the pg_database row is marked invalid, but the |
| 3234 |
* catalog contents still exist. Connections to such a database are not |
| 3235 |
* allowed. |
| 3236 |
*/ |
| 3237 |
bool |
| 3238 |
database_is_invalid_form(Form_pg_database datform) |
| 3239 |
{ |
| 3240 |
return datform->datconnlimit == DATCONNLIMIT_INVALID_DB; |
| 3241 |
} |
| 3242 |
|
| 3243 |
|
| 3244 |
/* |
| 3245 |
* Convenience wrapper around database_is_invalid_form() |
| 3246 |
*/ |
| 3247 |
bool |
| 3248 |
database_is_invalid_oid(Oid dboid) |
| 3249 |
{ |
| 3250 |
HeapTuple dbtup; |
| 3251 |
Form_pg_database dbform; |
| 3252 |
bool invalid; |
| 3253 |
|
| 3254 |
dbtup = SearchSysCache1(DATABASEOID, ObjectIdGetDatum(dboid)); |
| 3255 |
if (!HeapTupleIsValid(dbtup)) |
| 3256 |
elog(ERROR, "cache lookup failed for database %u", dboid); |
| 3257 |
dbform = (Form_pg_database) GETSTRUCT(dbtup); |
| 3258 |
|
| 3259 |
invalid = database_is_invalid_form(dbform); |
| 3260 |
|
| 3261 |
ReleaseSysCache(dbtup); |
| 3262 |
|
| 3263 |
return invalid; |
| 3264 |
} |
| 3265 |
|
| 3266 |
|
| 3267 |
/* |
| 3268 |
* recovery_create_dbdir() |
| 3269 |
* |
| 3270 |
* During recovery, there's a case where we validly need to recover a missing |
| 3271 |
* tablespace directory so that recovery can continue. This happens when |
| 3272 |
* recovery wants to create a database but the holding tablespace has been |
| 3273 |
* removed before the server stopped. Since we expect that the directory will |
| 3274 |
* be gone before reaching recovery consistency, and we have no knowledge about |
| 3275 |
* the tablespace other than its OID here, we create a real directory under |
| 3276 |
* pg_tblspc here instead of restoring the symlink. |
| 3277 |
* |
| 3278 |
* If only_tblspc is true, then the requested directory must be in pg_tblspc/ |
| 3279 |
*/ |
| 3280 |
static void |
| 3281 |
recovery_create_dbdir(char *path, bool only_tblspc) |
| 3282 |
{ |
| 3283 |
struct stat st; |
| 3284 |
|
| 3285 |
Assert(RecoveryInProgress()); |
| 3286 |
|
| 3287 |
if (stat(path, &st) == 0) |
| 3288 |
return; |
| 3289 |
|
| 3290 |
if (only_tblspc && strstr(path, PG_TBLSPC_DIR_SLASH) == NULL) |
| 3291 |
elog(PANIC, "requested to created invalid directory: %s", path); |
| 3292 |
|
| 3293 |
if (reachedConsistency && !allow_in_place_tablespaces) |
| 3294 |
ereport(PANIC, |
| 3295 |
errmsg("missing directory \"%s\"", path)); |
| 3296 |
|
| 3297 |
elog(reachedConsistency ? WARNING : DEBUG1, |
| 3298 |
"creating missing directory: %s", path); |
| 3299 |
|
| 3300 |
if (pg_mkdir_p(path, pg_dir_create_mode) != 0) |
| 3301 |
ereport(PANIC, |
| 3302 |
errmsg("could not create missing directory \"%s\": %m", path)); |
| 3303 |
} |
| 3304 |
|
| 3305 |
|
| 3306 |
/* |
| 3307 |
* DATABASE resource manager's routines |
| 3308 |
*/ |
| 3309 |
void |
| 3310 |
dbase_redo(XLogReaderState *record) |
| 3311 |
{ |
| 3312 |
uint8 info = XLogRecGetInfo(record) & ~XLR_INFO_MASK; |
| 3313 |
|
| 3314 |
/* Backup blocks are not used in dbase records */ |
| 3315 |
Assert(!XLogRecHasAnyBlockRefs(record)); |
| 3316 |
|
| 3317 |
if (info == XLOG_DBASE_CREATE_FILE_COPY) |
| 3318 |
{ |
| 3319 |
xl_dbase_create_file_copy_rec *xlrec = |
| 3320 |
(xl_dbase_create_file_copy_rec *) XLogRecGetData(record); |
| 3321 |
char *src_path; |
| 3322 |
char *dst_path; |
| 3323 |
char *parent_path; |
| 3324 |
struct stat st; |
| 3325 |
|
| 3326 |
src_path = GetDatabasePath(xlrec->src_db_id, xlrec->src_tablespace_id); |
| 3327 |
dst_path = GetDatabasePath(xlrec->db_id, xlrec->tablespace_id); |
| 3328 |
|
| 3329 |
/* |
| 3330 |
* Our theory for replaying a CREATE is to forcibly drop the target |
| 3331 |
* subdirectory if present, then re-copy the source data. This may be |
| 3332 |
* more work than needed, but it is simple to implement. |
| 3333 |
*/ |
| 3334 |
if (stat(dst_path, &st) == 0 && S_ISDIR(st.st_mode)) |
| 3335 |
{ |
| 3336 |
if (!rmtree(dst_path, true)) |
| 3337 |
/* If this failed, copydir() below is going to error. */ |
| 3338 |
ereport(WARNING, |
| 3339 |
(errmsg("some useless files may be left behind in old database directory \"%s\"", |
| 3340 |
dst_path))); |
| 3341 |
} |
| 3342 |
|
| 3343 |
/* |
| 3344 |
* If the parent of the target path doesn't exist, create it now. This |
| 3345 |
* enables us to create the target underneath later. |
| 3346 |
*/ |
| 3347 |
parent_path = pstrdup(dst_path); |
| 3348 |
get_parent_directory(parent_path); |
| 3349 |
if (stat(parent_path, &st) < 0) |
| 3350 |
{ |
| 3351 |
if (errno != ENOENT) |
| 3352 |
ereport(FATAL, |
| 3353 |
errmsg("could not stat directory \"%s\": %m", |
| 3354 |
dst_path)); |
| 3355 |
|
| 3356 |
/* create the parent directory if needed and valid */ |
| 3357 |
recovery_create_dbdir(parent_path, true); |
| 3358 |
} |
| 3359 |
pfree(parent_path); |
| 3360 |
|
| 3361 |
/* |
| 3362 |
* There's a case where the copy source directory is missing for the |
| 3363 |
* same reason above. Create the empty source directory so that |
| 3364 |
* copydir below doesn't fail. The directory will be dropped soon by |
| 3365 |
* recovery. |
| 3366 |
*/ |
| 3367 |
if (stat(src_path, &st) < 0 && errno == ENOENT) |
| 3368 |
recovery_create_dbdir(src_path, false); |
| 3369 |
|
| 3370 |
/* |
| 3371 |
* Force dirty buffers out to disk, to ensure source database is |
| 3372 |
* up-to-date for the copy. |
| 3373 |
*/ |
| 3374 |
FlushDatabaseBuffers(xlrec->src_db_id); |
| 3375 |
|
| 3376 |
/* Close all smgr fds in all backends. */ |
| 3377 |
WaitForProcSignalBarrier(EmitProcSignalBarrier(PROCSIGNAL_BARRIER_SMGRRELEASE)); |
| 3378 |
|
| 3379 |
/* |
| 3380 |
* Copy this subdirectory to the new location |
| 3381 |
* |
| 3382 |
* We don't need to copy subdirectories |
| 3383 |
*/ |
| 3384 |
copydir(src_path, dst_path, false); |
| 3385 |
|
| 3386 |
pfree(src_path); |
| 3387 |
pfree(dst_path); |
| 3388 |
} |
| 3389 |
else if (info == XLOG_DBASE_CREATE_WAL_LOG) |
| 3390 |
{ |
| 3391 |
xl_dbase_create_wal_log_rec *xlrec = |
| 3392 |
(xl_dbase_create_wal_log_rec *) XLogRecGetData(record); |
| 3393 |
char *dbpath; |
| 3394 |
char *parent_path; |
| 3395 |
|
| 3396 |
dbpath = GetDatabasePath(xlrec->db_id, xlrec->tablespace_id); |
| 3397 |
|
| 3398 |
/* create the parent directory if needed and valid */ |
| 3399 |
parent_path = pstrdup(dbpath); |
| 3400 |
get_parent_directory(parent_path); |
| 3401 |
recovery_create_dbdir(parent_path, true); |
| 3402 |
pfree(parent_path); |
| 3403 |
|
| 3404 |
/* Create the database directory with the version file. */ |
| 3405 |
CreateDirAndVersionFile(dbpath, xlrec->db_id, xlrec->tablespace_id, |
| 3406 |
true); |
| 3407 |
pfree(dbpath); |
| 3408 |
} |
| 3409 |
else if (info == XLOG_DBASE_DROP) |
| 3410 |
{ |
| 3411 |
xl_dbase_drop_rec *xlrec = (xl_dbase_drop_rec *) XLogRecGetData(record); |
| 3412 |
char *dst_path; |
| 3413 |
int i; |
| 3414 |
|
| 3415 |
if (InHotStandby) |
| 3416 |
{ |
| 3417 |
/* |
| 3418 |
* Lock database while we resolve conflicts to ensure that |
| 3419 |
* InitPostgres() cannot fully re-execute concurrently. This |
| 3420 |
* avoids backends re-connecting automatically to same database, |
| 3421 |
* which can happen in some cases. |
| 3422 |
* |
| 3423 |
* This will lock out walsenders trying to connect to db-specific |
| 3424 |
* slots for logical decoding too, so it's safe for us to drop |
| 3425 |
* slots. |
| 3426 |
*/ |
| 3427 |
LockSharedObjectForSession(DatabaseRelationId, xlrec->db_id, 0, AccessExclusiveLock); |
| 3428 |
ResolveRecoveryConflictWithDatabase(xlrec->db_id); |
| 3429 |
} |
| 3430 |
|
| 3431 |
/* Drop any database-specific replication slots */ |
| 3432 |
ReplicationSlotsDropDBSlots(xlrec->db_id); |
| 3433 |
|
| 3434 |
/* Drop pages for this database that are in the shared buffer cache */ |
| 3435 |
DropDatabaseBuffers(xlrec->db_id); |
| 3436 |
|
| 3437 |
/* Also, clean out any fsync requests that might be pending in md.c */ |
| 3438 |
ForgetDatabaseSyncRequests(xlrec->db_id); |
| 3439 |
|
| 3440 |
/* Clean out the xlog relcache too */ |
| 3441 |
XLogDropDatabase(xlrec->db_id); |
| 3442 |
|
| 3443 |
/* Close all smgr fds in all backends. */ |
| 3444 |
WaitForProcSignalBarrier(EmitProcSignalBarrier(PROCSIGNAL_BARRIER_SMGRRELEASE)); |
| 3445 |
|
| 3446 |
for (i = 0; i < xlrec->ntablespaces; i++) |
| 3447 |
{ |
| 3448 |
dst_path = GetDatabasePath(xlrec->db_id, xlrec->tablespace_ids[i]); |
| 3449 |
|
| 3450 |
/* And remove the physical files */ |
| 3451 |
if (!rmtree(dst_path, true)) |
| 3452 |
ereport(WARNING, |
| 3453 |
(errmsg("some useless files may be left behind in old database directory \"%s\"", |
| 3454 |
dst_path))); |
| 3455 |
pfree(dst_path); |
| 3456 |
} |
| 3457 |
|
| 3458 |
if (InHotStandby) |
| 3459 |
{ |
| 3460 |
/* |
| 3461 |
* Release locks prior to commit. XXX There is a race condition |
| 3462 |
* here that may allow backends to reconnect, but the window for |
| 3463 |
* this is small because the gap between here and commit is mostly |
| 3464 |
* fairly small and it is unlikely that people will be dropping |
| 3465 |
* databases that we are trying to connect to anyway. |
| 3466 |
*/ |
| 3467 |
UnlockSharedObjectForSession(DatabaseRelationId, xlrec->db_id, 0, AccessExclusiveLock); |
| 3468 |
} |
| 3469 |
} |
| 3470 |
else |
| 3471 |
elog(PANIC, "dbase_redo: unknown op code %u", info); |
| 3472 |
} |