PostgreSQL Source Code git master
Loading...
Searching...
No Matches
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_create_file_copy_rec
 
struct  xl_dbase_create_wal_log_rec
 
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

typedef struct xl_dbase_create_file_copy_rec xl_dbase_create_file_copy_rec
 
typedef struct xl_dbase_create_wal_log_rec xl_dbase_create_wal_log_rec
 
typedef struct xl_dbase_drop_rec xl_dbase_drop_rec
 

Functions

void dbase_redo (XLogReaderState *record)
 
void dbase_desc (StringInfo buf, XLogReaderState *record)
 
const chardbase_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,
XLogReaderState record 
)
extern

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 {
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:616
#define XLOG_DBASE_CREATE_WAL_LOG
#define XLOG_DBASE_DROP
#define XLOG_DBASE_CREATE_FILE_COPY
int i
Definition isn.c:77
static char buf[DEFAULT_XLOG_SEG_SIZE]
static int fb(int x)
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition stringinfo.c:145
void appendStringInfoString(StringInfo str, const char *s)
Definition stringinfo.c:230
#define XLogRecGetInfo(decoder)
Definition xlogreader.h:409
#define XLogRecGetData(decoder)
Definition xlogreader.h:414

References appendStringInfo(), appendStringInfoString(), buf, fb(), i, XLOG_DBASE_CREATE_FILE_COPY, XLOG_DBASE_CREATE_WAL_LOG, XLOG_DBASE_DROP, XLogRecGetData, and XLogRecGetInfo.

◆ dbase_identify()

const char * dbase_identify ( uint8  info)
extern

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 fb(), XLOG_DBASE_CREATE_FILE_COPY, XLOG_DBASE_CREATE_WAL_LOG, XLOG_DBASE_DROP, and XLR_INFO_MASK.

◆ dbase_redo()

void dbase_redo ( XLogReaderState record)
extern

Definition at line 3301 of file dbcommands.c.

3302{
3303 uint8 info = XLogRecGetInfo(record) & ~XLR_INFO_MASK;
3304
3305 /* Backup blocks are not used in dbase records */
3307
3308 if (info == XLOG_DBASE_CREATE_FILE_COPY)
3309 {
3312 char *src_path;
3313 char *dst_path;
3314 char *parent_path;
3315 struct stat st;
3316
3317 src_path = GetDatabasePath(xlrec->src_db_id, xlrec->src_tablespace_id);
3318 dst_path = GetDatabasePath(xlrec->db_id, xlrec->tablespace_id);
3319
3320 /*
3321 * Our theory for replaying a CREATE is to forcibly drop the target
3322 * subdirectory if present, then re-copy the source data. This may be
3323 * more work than needed, but it is simple to implement.
3324 */
3325 if (stat(dst_path, &st) == 0 && S_ISDIR(st.st_mode))
3326 {
3327 if (!rmtree(dst_path, true))
3328 /* If this failed, copydir() below is going to error. */
3330 (errmsg("some useless files may be left behind in old database directory \"%s\"",
3331 dst_path)));
3332 }
3333
3334 /*
3335 * If the parent of the target path doesn't exist, create it now. This
3336 * enables us to create the target underneath later.
3337 */
3340 if (stat(parent_path, &st) < 0)
3341 {
3342 if (errno != ENOENT)
3343 ereport(FATAL,
3344 errmsg("could not stat directory \"%s\": %m",
3345 dst_path));
3346
3347 /* create the parent directory if needed and valid */
3349 }
3351
3352 /*
3353 * There's a case where the copy source directory is missing for the
3354 * same reason above. Create the empty source directory so that
3355 * copydir below doesn't fail. The directory will be dropped soon by
3356 * recovery.
3357 */
3358 if (stat(src_path, &st) < 0 && errno == ENOENT)
3360
3361 /*
3362 * Force dirty buffers out to disk, to ensure source database is
3363 * up-to-date for the copy.
3364 */
3365 FlushDatabaseBuffers(xlrec->src_db_id);
3366
3367 /* Close all smgr fds in all backends. */
3369
3370 /*
3371 * Copy this subdirectory to the new location
3372 *
3373 * We don't need to copy subdirectories
3374 */
3375 copydir(src_path, dst_path, false);
3376
3377 pfree(src_path);
3378 pfree(dst_path);
3379 }
3380 else if (info == XLOG_DBASE_CREATE_WAL_LOG)
3381 {
3384 char *dbpath;
3385 char *parent_path;
3386
3387 dbpath = GetDatabasePath(xlrec->db_id, xlrec->tablespace_id);
3388
3389 /* create the parent directory if needed and valid */
3394
3395 /* Create the database directory with the version file. */
3396 CreateDirAndVersionFile(dbpath, xlrec->db_id, xlrec->tablespace_id,
3397 true);
3398 pfree(dbpath);
3399 }
3400 else if (info == XLOG_DBASE_DROP)
3401 {
3403 char *dst_path;
3404 int i;
3405
3406 if (InHotStandby)
3407 {
3408 /*
3409 * Lock database while we resolve conflicts to ensure that
3410 * InitPostgres() cannot fully re-execute concurrently. This
3411 * avoids backends re-connecting automatically to same database,
3412 * which can happen in some cases.
3413 *
3414 * This will lock out walsenders trying to connect to db-specific
3415 * slots for logical decoding too, so it's safe for us to drop
3416 * slots.
3417 */
3420 }
3421
3422 /* Drop any database-specific replication slots */
3424
3425 /* Drop pages for this database that are in the shared buffer cache */
3426 DropDatabaseBuffers(xlrec->db_id);
3427
3428 /* Also, clean out any fsync requests that might be pending in md.c */
3430
3431 /* Clean out the xlog relcache too */
3432 XLogDropDatabase(xlrec->db_id);
3433
3434 /* Close all smgr fds in all backends. */
3436
3437 for (i = 0; i < xlrec->ntablespaces; i++)
3438 {
3439 dst_path = GetDatabasePath(xlrec->db_id, xlrec->tablespace_ids[i]);
3440
3441 /* And remove the physical files */
3442 if (!rmtree(dst_path, true))
3444 (errmsg("some useless files may be left behind in old database directory \"%s\"",
3445 dst_path)));
3446 pfree(dst_path);
3447 }
3448
3449 if (InHotStandby)
3450 {
3451 /*
3452 * Release locks prior to commit. XXX There is a race condition
3453 * here that may allow backends to reconnect, but the window for
3454 * this is small because the gap between here and commit is mostly
3455 * fairly small and it is unlikely that people will be dropping
3456 * databases that we are trying to connect to anyway.
3457 */
3459 }
3460 }
3461 else
3462 elog(PANIC, "dbase_redo: unknown op code %u", info);
3463}
void DropDatabaseBuffers(Oid dbid)
Definition bufmgr.c:5034
void FlushDatabaseBuffers(Oid dbid)
Definition bufmgr.c:5445
#define Assert(condition)
Definition c.h:945
void copydir(const char *fromdir, const char *todir, bool recurse)
Definition copydir.c:49
static void CreateDirAndVersionFile(char *dbpath, Oid dbid, Oid tsid, bool isRedo)
Definition dbcommands.c:460
static void recovery_create_dbdir(char *path, bool only_tblspc)
#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
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:1781
void pfree(void *pointer)
Definition mcxt.c:1616
void ForgetDatabaseSyncRequests(Oid dbid)
Definition md.c:1594
static char * errmsg
void get_parent_directory(char *path)
Definition path.c:1068
void WaitForProcSignalBarrier(uint64 generation)
Definition procsignal.c:427
uint64 EmitProcSignalBarrier(ProcSignalBarrierType type)
Definition procsignal.c:359
@ PROCSIGNAL_BARRIER_SMGRRELEASE
Definition procsignal.h:48
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:1511
void ResolveRecoveryConflictWithDatabase(Oid dbid)
Definition standby.c:571
#define stat
Definition win32_port.h:74
#define S_ISDIR(m)
Definition win32_port.h:315
#define XLogRecHasAnyBlockRefs(decoder)
Definition xlogreader.h:416
void XLogDropDatabase(Oid dbid)
Definition xlogutils.c:641
#define InHotStandby
Definition xlogutils.h:60

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