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