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