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:556
#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 3300 of file dbcommands.c.

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