PostgreSQL Source Code git master
Data Structures | Macros | Typedefs | Functions
dbcommands_xlog.h File Reference
#include "access/xlogreader.h"
#include "lib/stringinfo.h"
Include dependency graph for dbcommands_xlog.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

 
 
struct   xl_dbase_drop_rec
 

Macros

#define  XLOG_DBASE_CREATE_FILE_COPY   0x00
 
#define  XLOG_DBASE_CREATE_WAL_LOG   0x10
 
#define  XLOG_DBASE_DROP   0x20
 
#define  MinSizeOfDbaseDropRec   offsetof(xl_dbase_drop_rec, tablespace_ids)
 

Typedefs

 
 
 

Functions

void  dbase_redo (XLogReaderState *record)
 
 
const char *  dbase_identify (uint8 info)
 

Macro Definition Documentation

MinSizeOfDbaseDropRec

#define MinSizeOfDbaseDropRec   offsetof(xl_dbase_drop_rec, tablespace_ids)

Definition at line 54 of file dbcommands_xlog.h.

XLOG_DBASE_CREATE_FILE_COPY

#define XLOG_DBASE_CREATE_FILE_COPY   0x00

Definition at line 21 of file dbcommands_xlog.h.

XLOG_DBASE_CREATE_WAL_LOG

#define XLOG_DBASE_CREATE_WAL_LOG   0x10

Definition at line 22 of file dbcommands_xlog.h.

XLOG_DBASE_DROP

#define XLOG_DBASE_DROP   0x20

Definition at line 23 of file dbcommands_xlog.h.

Typedef Documentation

xl_dbase_create_file_copy_rec

xl_dbase_create_wal_log_rec

xl_dbase_drop_rec

Function Documentation

dbase_desc()

void dbase_desc ( StringInfo  buf,
XLogReaderStaterecord 
)

Definition at line 22 of file dbasedesc.c.

23{
24 char *rec = XLogRecGetData(record);
25 uint8 info = XLogRecGetInfo(record) & ~XLR_INFO_MASK;
26
28 {
31
32 appendStringInfo(buf, "copy dir %u/%u to %u/%u",
33 xlrec->src_tablespace_id, xlrec->src_db_id,
34 xlrec->tablespace_id, xlrec->db_id);
35 }
36 else if (info == XLOG_DBASE_CREATE_WAL_LOG)
37 {
40
41 appendStringInfo(buf, "create dir %u/%u",
42 xlrec->tablespace_id, xlrec->db_id);
43 }
44 else if (info == XLOG_DBASE_DROP)
45 {
46 xl_dbase_drop_rec *xlrec = (xl_dbase_drop_rec *) rec;
47 int i;
48
50 for (i = 0; i < xlrec->ntablespaces; i++)
51 appendStringInfo(buf, " %u/%u",
52 xlrec->tablespace_ids[i], xlrec->db_id);
53 }
54}
uint8_t uint8
Definition: c.h:536
#define XLOG_DBASE_CREATE_WAL_LOG
#define XLOG_DBASE_DROP
#define XLOG_DBASE_CREATE_FILE_COPY
i
int i
Definition: isn.c:77
static char * buf
Definition: pg_test_fsync.c:72
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:145
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:230
Oid tablespace_ids[FLEXIBLE_ARRAY_MEMBER]
#define XLogRecGetInfo(decoder)
Definition: xlogreader.h:410
#define XLogRecGetData(decoder)
Definition: xlogreader.h:415

References appendStringInfo(), appendStringInfoString(), buf, xl_dbase_create_file_copy_rec::db_id, xl_dbase_create_wal_log_rec::db_id, xl_dbase_drop_rec::db_id, i, xl_dbase_drop_rec::ntablespaces, xl_dbase_create_file_copy_rec::src_db_id, xl_dbase_create_file_copy_rec::src_tablespace_id, xl_dbase_create_file_copy_rec::tablespace_id, xl_dbase_create_wal_log_rec::tablespace_id, xl_dbase_drop_rec::tablespace_ids, XLOG_DBASE_CREATE_FILE_COPY, XLOG_DBASE_CREATE_WAL_LOG, XLOG_DBASE_DROP, XLogRecGetData, and XLogRecGetInfo.

dbase_identify()

const char * dbase_identify ( uint8  info )

Definition at line 57 of file dbasedesc.c.

58{
59 const char *id = NULL;
60
61 switch (info & ~XLR_INFO_MASK)
62 {
64 id = "CREATE_FILE_COPY";
65 break;
67 id = "CREATE_WAL_LOG";
68 break;
69 case XLOG_DBASE_DROP:
70 id = "DROP";
71 break;
72 }
73
74 return id;
75}
#define XLR_INFO_MASK
Definition: xlogrecord.h:62

References XLOG_DBASE_CREATE_FILE_COPY, XLOG_DBASE_CREATE_WAL_LOG, XLOG_DBASE_DROP, and XLR_INFO_MASK.

dbase_redo()

void dbase_redo ( XLogReaderStaterecord )

Definition at line 3286 of file dbcommands.c.

3287{
3288 uint8 info = XLogRecGetInfo(record) & ~XLR_INFO_MASK;
3289
3290 /* Backup blocks are not used in dbase records */
3292
3293 if (info == XLOG_DBASE_CREATE_FILE_COPY)
3294 {
3297 char *src_path;
3298 char *dst_path;
3299 char *parent_path;
3300 struct stat st;
3301
3302 src_path = GetDatabasePath(xlrec->src_db_id, xlrec->src_tablespace_id);
3303 dst_path = GetDatabasePath(xlrec->db_id, xlrec->tablespace_id);
3304
3305 /*
3306 * Our theory for replaying a CREATE is to forcibly drop the target
3307 * subdirectory if present, then re-copy the source data. This may be
3308 * more work than needed, but it is simple to implement.
3309 */
3310 if (stat(dst_path, &st) == 0 && S_ISDIR(st.st_mode))
3311 {
3312 if (!rmtree(dst_path, true))
3313 /* If this failed, copydir() below is going to error. */
3315 (errmsg("some useless files may be left behind in old database directory \"%s\"",
3316 dst_path)));
3317 }
3318
3319 /*
3320 * If the parent of the target path doesn't exist, create it now. This
3321 * enables us to create the target underneath later.
3322 */
3323 parent_path = pstrdup(dst_path);
3324 get_parent_directory(parent_path);
3325 if (stat(parent_path, &st) < 0)
3326 {
3327 if (errno != ENOENT)
3328 ereport(FATAL,
3329 errmsg("could not stat directory \"%s\": %m",
3330 dst_path));
3331
3332 /* create the parent directory if needed and valid */
3333 recovery_create_dbdir(parent_path, true);
3334 }
3335 pfree(parent_path);
3336
3337 /*
3338 * There's a case where the copy source directory is missing for the
3339 * same reason above. Create the empty source directory so that
3340 * copydir below doesn't fail. The directory will be dropped soon by
3341 * recovery.
3342 */
3343 if (stat(src_path, &st) < 0 && errno == ENOENT)
3344 recovery_create_dbdir(src_path, false);
3345
3346 /*
3347 * Force dirty buffers out to disk, to ensure source database is
3348 * up-to-date for the copy.
3349 */
3351
3352 /* Close all smgr fds in all backends. */
3354
3355 /*
3356 * Copy this subdirectory to the new location
3357 *
3358 * We don't need to copy subdirectories
3359 */
3360 copydir(src_path, dst_path, false);
3361
3362 pfree(src_path);
3363 pfree(dst_path);
3364 }
3365 else if (info == XLOG_DBASE_CREATE_WAL_LOG)
3366 {
3369 char *dbpath;
3370 char *parent_path;
3371
3372 dbpath = GetDatabasePath(xlrec->db_id, xlrec->tablespace_id);
3373
3374 /* create the parent directory if needed and valid */
3375 parent_path = pstrdup(dbpath);
3376 get_parent_directory(parent_path);
3377 recovery_create_dbdir(parent_path, true);
3378
3379 /* Create the database directory with the version file. */
3380 CreateDirAndVersionFile(dbpath, xlrec->db_id, xlrec->tablespace_id,
3381 true);
3382 pfree(dbpath);
3383 }
3384 else if (info == XLOG_DBASE_DROP)
3385 {
3387 char *dst_path;
3388 int i;
3389
3390 if (InHotStandby)
3391 {
3392 /*
3393 * Lock database while we resolve conflicts to ensure that
3394 * InitPostgres() cannot fully re-execute concurrently. This
3395 * avoids backends re-connecting automatically to same database,
3396 * which can happen in some cases.
3397 *
3398 * This will lock out walsenders trying to connect to db-specific
3399 * slots for logical decoding too, so it's safe for us to drop
3400 * slots.
3401 */
3402 LockSharedObjectForSession(DatabaseRelationId, xlrec->db_id, 0, AccessExclusiveLock);
3404 }
3405
3406 /* Drop any database-specific replication slots */
3408
3409 /* Drop pages for this database that are in the shared buffer cache */
3410 DropDatabaseBuffers(xlrec->db_id);
3411
3412 /* Also, clean out any fsync requests that might be pending in md.c */
3414
3415 /* Clean out the xlog relcache too */
3416 XLogDropDatabase(xlrec->db_id);
3417
3418 /* Close all smgr fds in all backends. */
3420
3421 for (i = 0; i < xlrec->ntablespaces; i++)
3422 {
3423 dst_path = GetDatabasePath(xlrec->db_id, xlrec->tablespace_ids[i]);
3424
3425 /* And remove the physical files */
3426 if (!rmtree(dst_path, true))
3428 (errmsg("some useless files may be left behind in old database directory \"%s\"",
3429 dst_path)));
3430 pfree(dst_path);
3431 }
3432
3433 if (InHotStandby)
3434 {
3435 /*
3436 * Release locks prior to commit. XXX There is a race condition
3437 * here that may allow backends to reconnect, but the window for
3438 * this is small because the gap between here and commit is mostly
3439 * fairly small and it is unlikely that people will be dropping
3440 * databases that we are trying to connect to anyway.
3441 */
3442 UnlockSharedObjectForSession(DatabaseRelationId, xlrec->db_id, 0, AccessExclusiveLock);
3443 }
3444 }
3445 else
3446 elog(PANIC, "dbase_redo: unknown op code %u", info);
3447}
void DropDatabaseBuffers(Oid dbid)
Definition: bufmgr.c:4860
void FlushDatabaseBuffers(Oid dbid)
Definition: bufmgr.c:5276
void copydir(const char *fromdir, const char *todir, bool recurse)
Definition: copydir.c:48
static void CreateDirAndVersionFile(char *dbpath, Oid dbid, Oid tsid, bool isRedo)
Definition: dbcommands.c:458
static void recovery_create_dbdir(char *path, bool only_tblspc)
Definition: dbcommands.c:3257
int errmsg(const char *fmt,...)
Definition: elog.c:1071
#define FATAL
Definition: elog.h:41
#define WARNING
Definition: elog.h:36
#define PANIC
Definition: elog.h:42
#define elog(elevel,...)
Definition: elog.h:226
#define ereport(elevel,...)
Definition: elog.h:150
Assert(PointerIsAligned(start, uint64))
void UnlockSharedObjectForSession(Oid classid, Oid objid, uint16 objsubid, LOCKMODE lockmode)
Definition: lmgr.c:1187
void LockSharedObjectForSession(Oid classid, Oid objid, uint16 objsubid, LOCKMODE lockmode)
Definition: lmgr.c:1169
#define AccessExclusiveLock
Definition: lockdefs.h:43
char * pstrdup(const char *in)
Definition: mcxt.c:1759
void pfree(void *pointer)
Definition: mcxt.c:1594
void ForgetDatabaseSyncRequests(Oid dbid)
Definition: md.c:1569
void get_parent_directory(char *path)
Definition: path.c:1068
void WaitForProcSignalBarrier(uint64 generation)
Definition: procsignal.c:424
uint64 EmitProcSignalBarrier(ProcSignalBarrierType type)
Definition: procsignal.c:356
@ PROCSIGNAL_BARRIER_SMGRRELEASE
Definition: procsignal.h:56
char * GetDatabasePath(Oid dbOid, Oid spcOid)
Definition: relpath.c:110
bool rmtree(const char *path, bool rmtopdir)
Definition: rmtree.c:50
void ReplicationSlotsDropDBSlots(Oid dboid)
Definition: slot.c:1408
void ResolveRecoveryConflictWithDatabase(Oid dbid)
Definition: standby.c:569
Definition: win32_port.h:255
#define stat
Definition: win32_port.h:274
#define S_ISDIR(m)
Definition: win32_port.h:315
#define XLogRecHasAnyBlockRefs(decoder)
Definition: xlogreader.h:417
void XLogDropDatabase(Oid dbid)
Definition: xlogutils.c:641
#define InHotStandby
Definition: xlogutils.h:60

References AccessExclusiveLock, Assert(), copydir(), CreateDirAndVersionFile(), xl_dbase_create_file_copy_rec::db_id, xl_dbase_create_wal_log_rec::db_id, xl_dbase_drop_rec::db_id, DropDatabaseBuffers(), elog, EmitProcSignalBarrier(), ereport, errmsg(), FATAL, FlushDatabaseBuffers(), ForgetDatabaseSyncRequests(), get_parent_directory(), GetDatabasePath(), i, InHotStandby, LockSharedObjectForSession(), xl_dbase_drop_rec::ntablespaces, PANIC, pfree(), PROCSIGNAL_BARRIER_SMGRRELEASE, pstrdup(), recovery_create_dbdir(), ReplicationSlotsDropDBSlots(), ResolveRecoveryConflictWithDatabase(), rmtree(), S_ISDIR, xl_dbase_create_file_copy_rec::src_db_id, xl_dbase_create_file_copy_rec::src_tablespace_id, stat::st_mode, stat, xl_dbase_create_file_copy_rec::tablespace_id, xl_dbase_create_wal_log_rec::tablespace_id, xl_dbase_drop_rec::tablespace_ids, UnlockSharedObjectForSession(), WaitForProcSignalBarrier(), WARNING, XLOG_DBASE_CREATE_FILE_COPY, XLOG_DBASE_CREATE_WAL_LOG, XLOG_DBASE_DROP, XLogDropDatabase(), XLogRecGetData, XLogRecGetInfo, and XLogRecHasAnyBlockRefs.

AltStyle によって変換されたページ (->オリジナル) /