PostgreSQL Source Code  git master
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_rec
 
struct  xl_dbase_drop_rec
 

Macros

#define XLOG_DBASE_CREATE   0x00
 
#define XLOG_DBASE_DROP   0x10
 

Typedefs

typedef struct xl_dbase_create_rec xl_dbase_create_rec
 
typedef struct xl_dbase_drop_rec xl_dbase_drop_rec
 

Functions

void dbase_redo (XLogReaderState *rptr)
 
void dbase_desc (StringInfo buf, XLogReaderState *rptr)
 
const char * dbase_identify (uint8 info)
 

Macro Definition Documentation

◆ XLOG_DBASE_CREATE

#define XLOG_DBASE_CREATE   0x00

◆ XLOG_DBASE_DROP

#define XLOG_DBASE_DROP   0x10

Typedef Documentation

◆ xl_dbase_create_rec

◆ xl_dbase_drop_rec

Function Documentation

◆ dbase_desc()

void dbase_desc ( StringInfo  buf,
XLogReaderState rptr 
)

Definition at line 22 of file dbasedesc.c.

References appendStringInfo(), xl_dbase_create_rec::db_id, xl_dbase_drop_rec::db_id, xl_dbase_create_rec::src_db_id, xl_dbase_create_rec::src_tablespace_id, xl_dbase_create_rec::tablespace_id, xl_dbase_drop_rec::tablespace_id, XLOG_DBASE_CREATE, XLOG_DBASE_DROP, XLogRecGetData, XLogRecGetInfo, and XLR_INFO_MASK.

23 {
24  char *rec = XLogRecGetData(record);
25  uint8 info = XLogRecGetInfo(record) & ~XLR_INFO_MASK;
26 
27  if (info == XLOG_DBASE_CREATE)
28  {
30 
31  appendStringInfo(buf, "copy dir %u/%u to %u/%u",
32  xlrec->src_tablespace_id, xlrec->src_db_id,
33  xlrec->tablespace_id, xlrec->db_id);
34  }
35  else if (info == XLOG_DBASE_DROP)
36  {
37  xl_dbase_drop_rec *xlrec = (xl_dbase_drop_rec *) rec;
38 
39  appendStringInfo(buf, "dir %u/%u",
40  xlrec->tablespace_id, xlrec->db_id);
41  }
42 }
unsigned char uint8
Definition: c.h:356
#define XLogRecGetData(decoder)
Definition: xlogreader.h:233
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:78
#define XLOG_DBASE_DROP
#define XLogRecGetInfo(decoder)
Definition: xlogreader.h:229
#define XLR_INFO_MASK
Definition: xlogrecord.h:62
#define XLOG_DBASE_CREATE

◆ dbase_identify()

const char* dbase_identify ( uint8  info)

Definition at line 45 of file dbasedesc.c.

References XLOG_DBASE_CREATE, XLOG_DBASE_DROP, and XLR_INFO_MASK.

46 {
47  const char *id = NULL;
48 
49  switch (info & ~XLR_INFO_MASK)
50  {
51  case XLOG_DBASE_CREATE:
52  id = "CREATE";
53  break;
54  case XLOG_DBASE_DROP:
55  id = "DROP";
56  break;
57  }
58 
59  return id;
60 }
#define XLOG_DBASE_DROP
#define XLR_INFO_MASK
Definition: xlogrecord.h:62
#define XLOG_DBASE_CREATE

◆ dbase_redo()

void dbase_redo ( XLogReaderState rptr)

Definition at line 2120 of file dbcommands.c.

References AccessExclusiveLock, Assert, copydir(), xl_dbase_create_rec::db_id, xl_dbase_drop_rec::db_id, DropDatabaseBuffers(), elog, ereport, errmsg(), FlushDatabaseBuffers(), ForgetDatabaseSyncRequests(), GetDatabasePath(), InHotStandby, LockSharedObjectForSession(), PANIC, ReplicationSlotsDropDBSlots(), ResolveRecoveryConflictWithDatabase(), rmtree(), S_ISDIR, xl_dbase_create_rec::src_db_id, xl_dbase_create_rec::src_tablespace_id, stat, xl_dbase_create_rec::tablespace_id, xl_dbase_drop_rec::tablespace_id, UnlockSharedObjectForSession(), WARNING, XLOG_DBASE_CREATE, XLOG_DBASE_DROP, XLogDropDatabase(), XLogRecGetData, XLogRecGetInfo, XLogRecHasAnyBlockRefs, and XLR_INFO_MASK.

2121 {
2122  uint8 info = XLogRecGetInfo(record) & ~XLR_INFO_MASK;
2123 
2124  /* Backup blocks are not used in dbase records */
2125  Assert(!XLogRecHasAnyBlockRefs(record));
2126 
2127  if (info == XLOG_DBASE_CREATE)
2128  {
2130  char *src_path;
2131  char *dst_path;
2132  struct stat st;
2133 
2134  src_path = GetDatabasePath(xlrec->src_db_id, xlrec->src_tablespace_id);
2135  dst_path = GetDatabasePath(xlrec->db_id, xlrec->tablespace_id);
2136 
2137  /*
2138  * Our theory for replaying a CREATE is to forcibly drop the target
2139  * subdirectory if present, then re-copy the source data. This may be
2140  * more work than needed, but it is simple to implement.
2141  */
2142  if (stat(dst_path, &st) == 0 && S_ISDIR(st.st_mode))
2143  {
2144  if (!rmtree(dst_path, true))
2145  /* If this failed, copydir() below is going to error. */
2146  ereport(WARNING,
2147  (errmsg("some useless files may be left behind in old database directory \"%s\"",
2148  dst_path)));
2149  }
2150 
2151  /*
2152  * Force dirty buffers out to disk, to ensure source database is
2153  * up-to-date for the copy.
2154  */
2156 
2157  /*
2158  * Copy this subdirectory to the new location
2159  *
2160  * We don't need to copy subdirectories
2161  */
2162  copydir(src_path, dst_path, false);
2163  }
2164  else if (info == XLOG_DBASE_DROP)
2165  {
2166  xl_dbase_drop_rec *xlrec = (xl_dbase_drop_rec *) XLogRecGetData(record);
2167  char *dst_path;
2168 
2169  dst_path = GetDatabasePath(xlrec->db_id, xlrec->tablespace_id);
2170 
2171  if (InHotStandby)
2172  {
2173  /*
2174  * Lock database while we resolve conflicts to ensure that
2175  * InitPostgres() cannot fully re-execute concurrently. This
2176  * avoids backends re-connecting automatically to same database,
2177  * which can happen in some cases.
2178  *
2179  * This will lock out walsenders trying to connect to db-specific
2180  * slots for logical decoding too, so it's safe for us to drop
2181  * slots.
2182  */
2183  LockSharedObjectForSession(DatabaseRelationId, xlrec->db_id, 0, AccessExclusiveLock);
2185  }
2186 
2187  /* Drop any database-specific replication slots */
2189 
2190  /* Drop pages for this database that are in the shared buffer cache */
2191  DropDatabaseBuffers(xlrec->db_id);
2192 
2193  /* Also, clean out any fsync requests that might be pending in md.c */
2195 
2196  /* Clean out the xlog relcache too */
2197  XLogDropDatabase(xlrec->db_id);
2198 
2199  /* And remove the physical files */
2200  if (!rmtree(dst_path, true))
2201  ereport(WARNING,
2202  (errmsg("some useless files may be left behind in old database directory \"%s\"",
2203  dst_path)));
2204 
2205  if (InHotStandby)
2206  {
2207  /*
2208  * Release locks prior to commit. XXX There is a race condition
2209  * here that may allow backends to reconnect, but the window for
2210  * this is small because the gap between here and commit is mostly
2211  * fairly small and it is unlikely that people will be dropping
2212  * databases that we are trying to connect to anyway.
2213  */
2214  UnlockSharedObjectForSession(DatabaseRelationId, xlrec->db_id, 0, AccessExclusiveLock);
2215  }
2216  }
2217  else
2218  elog(PANIC, "dbase_redo: unknown op code %u", info);
2219 }
unsigned char uint8
Definition: c.h:356
#define InHotStandby
Definition: xlog.h:74
void copydir(char *fromdir, char *todir, bool recurse)
Definition: copydir.c:37
#define PANIC
Definition: elog.h:53
void LockSharedObjectForSession(Oid classid, Oid objid, uint16 objsubid, LOCKMODE lockmode)
Definition: lmgr.c:1046
#define XLogRecGetData(decoder)
Definition: xlogreader.h:233
#define XLOG_DBASE_DROP
void UnlockSharedObjectForSession(Oid classid, Oid objid, uint16 objsubid, LOCKMODE lockmode)
Definition: lmgr.c:1064
void FlushDatabaseBuffers(Oid dbid)
Definition: bufmgr.c:3290
#define ereport(elevel, rest)
Definition: elog.h:141
#define XLogRecGetInfo(decoder)
Definition: xlogreader.h:229
char * GetDatabasePath(Oid dbNode, Oid spcNode)
Definition: relpath.c:107
#define WARNING
Definition: elog.h:40
#define stat(a, b)
Definition: win32_port.h:264
bool rmtree(const char *path, bool rmtopdir)
Definition: rmtree.c:42
void ForgetDatabaseSyncRequests(Oid dbid)
Definition: md.c:983
void ResolveRecoveryConflictWithDatabase(Oid dbid)
Definition: standby.c:346
#define Assert(condition)
Definition: c.h:732
#define XLR_INFO_MASK
Definition: xlogrecord.h:62
#define XLOG_DBASE_CREATE
#define S_ISDIR(m)
Definition: win32_port.h:305
#define AccessExclusiveLock
Definition: lockdefs.h:45
int errmsg(const char *fmt,...)
Definition: elog.c:784
void XLogDropDatabase(Oid dbid)
Definition: xlogutils.c:617
#define elog(elevel,...)
Definition: elog.h:226
#define XLogRecHasAnyBlockRefs(decoder)
Definition: xlogreader.h:235
void DropDatabaseBuffers(Oid dbid)
Definition: bufmgr.c:3087
void ReplicationSlotsDropDBSlots(Oid dboid)
Definition: slot.c:896