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
 
#define MinSizeOfDbaseDropRec   offsetof(xl_dbase_drop_rec, tablespace_ids)
 

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

◆ MinSizeOfDbaseDropRec

#define MinSizeOfDbaseDropRec   offsetof(xl_dbase_drop_rec, tablespace_ids)

Definition at line 39 of file dbcommands_xlog.h.

Referenced by remove_dbtablespaces().

◆ 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, i, xl_dbase_drop_rec::ntablespaces, 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_ids, 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  int i;
39 
40  appendStringInfo(buf, "dir");
41  for (i = 0; i < xlrec->ntablespaces; i++)
42  appendStringInfo(buf, " %u/%u",
43  xlrec->tablespace_ids[i], xlrec->db_id);
44  }
45 }
unsigned char uint8
Definition: c.h:357
#define XLogRecGetData(decoder)
Definition: xlogreader.h:246
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:91
#define XLOG_DBASE_DROP
#define XLogRecGetInfo(decoder)
Definition: xlogreader.h:242
#define XLR_INFO_MASK
Definition: xlogrecord.h:62
#define XLOG_DBASE_CREATE
Oid tablespace_ids[FLEXIBLE_ARRAY_MEMBER]
int i

◆ dbase_identify()

const char* dbase_identify ( uint8  info)

Definition at line 48 of file dbasedesc.c.

References XLOG_DBASE_CREATE, XLOG_DBASE_DROP, and XLR_INFO_MASK.

49 {
50  const char *id = NULL;
51 
52  switch (info & ~XLR_INFO_MASK)
53  {
54  case XLOG_DBASE_CREATE:
55  id = "CREATE";
56  break;
57  case XLOG_DBASE_DROP:
58  id = "DROP";
59  break;
60  }
61 
62  return id;
63 }
#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 2176 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(), i, InHotStandby, LockSharedObjectForSession(), xl_dbase_drop_rec::ntablespaces, PANIC, pfree(), 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_ids, UnlockSharedObjectForSession(), WARNING, XLOG_DBASE_CREATE, XLOG_DBASE_DROP, XLogDropDatabase(), XLogRecGetData, XLogRecGetInfo, XLogRecHasAnyBlockRefs, and XLR_INFO_MASK.

2177 {
2178  uint8 info = XLogRecGetInfo(record) & ~XLR_INFO_MASK;
2179 
2180  /* Backup blocks are not used in dbase records */
2181  Assert(!XLogRecHasAnyBlockRefs(record));
2182 
2183  if (info == XLOG_DBASE_CREATE)
2184  {
2186  char *src_path;
2187  char *dst_path;
2188  struct stat st;
2189 
2190  src_path = GetDatabasePath(xlrec->src_db_id, xlrec->src_tablespace_id);
2191  dst_path = GetDatabasePath(xlrec->db_id, xlrec->tablespace_id);
2192 
2193  /*
2194  * Our theory for replaying a CREATE is to forcibly drop the target
2195  * subdirectory if present, then re-copy the source data. This may be
2196  * more work than needed, but it is simple to implement.
2197  */
2198  if (stat(dst_path, &st) == 0 && S_ISDIR(st.st_mode))
2199  {
2200  if (!rmtree(dst_path, true))
2201  /* If this failed, copydir() below is going to error. */
2202  ereport(WARNING,
2203  (errmsg("some useless files may be left behind in old database directory \"%s\"",
2204  dst_path)));
2205  }
2206 
2207  /*
2208  * Force dirty buffers out to disk, to ensure source database is
2209  * up-to-date for the copy.
2210  */
2212 
2213  /*
2214  * Copy this subdirectory to the new location
2215  *
2216  * We don't need to copy subdirectories
2217  */
2218  copydir(src_path, dst_path, false);
2219  }
2220  else if (info == XLOG_DBASE_DROP)
2221  {
2222  xl_dbase_drop_rec *xlrec = (xl_dbase_drop_rec *) XLogRecGetData(record);
2223  char *dst_path;
2224  int i;
2225 
2226  if (InHotStandby)
2227  {
2228  /*
2229  * Lock database while we resolve conflicts to ensure that
2230  * InitPostgres() cannot fully re-execute concurrently. This
2231  * avoids backends re-connecting automatically to same database,
2232  * which can happen in some cases.
2233  *
2234  * This will lock out walsenders trying to connect to db-specific
2235  * slots for logical decoding too, so it's safe for us to drop
2236  * slots.
2237  */
2238  LockSharedObjectForSession(DatabaseRelationId, xlrec->db_id, 0, AccessExclusiveLock);
2240  }
2241 
2242  /* Drop any database-specific replication slots */
2244 
2245  /* Drop pages for this database that are in the shared buffer cache */
2246  DropDatabaseBuffers(xlrec->db_id);
2247 
2248  /* Also, clean out any fsync requests that might be pending in md.c */
2250 
2251  /* Clean out the xlog relcache too */
2252  XLogDropDatabase(xlrec->db_id);
2253 
2254  for (i = 0; i < xlrec->ntablespaces; i++)
2255  {
2256  dst_path = GetDatabasePath(xlrec->db_id, xlrec->tablespace_ids[i]);
2257 
2258  /* And remove the physical files */
2259  if (!rmtree(dst_path, true))
2260  ereport(WARNING,
2261  (errmsg("some useless files may be left behind in old database directory \"%s\"",
2262  dst_path)));
2263  pfree(dst_path);
2264  }
2265 
2266  if (InHotStandby)
2267  {
2268  /*
2269  * Release locks prior to commit. XXX There is a race condition
2270  * here that may allow backends to reconnect, but the window for
2271  * this is small because the gap between here and commit is mostly
2272  * fairly small and it is unlikely that people will be dropping
2273  * databases that we are trying to connect to anyway.
2274  */
2275  UnlockSharedObjectForSession(DatabaseRelationId, xlrec->db_id, 0, AccessExclusiveLock);
2276  }
2277  }
2278  else
2279  elog(PANIC, "dbase_redo: unknown op code %u", info);
2280 }
unsigned char uint8
Definition: c.h:357
#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:1044
void pfree(void *pointer)
Definition: mcxt.c:1056
#define XLogRecGetData(decoder)
Definition: xlogreader.h:246
#define XLOG_DBASE_DROP
void UnlockSharedObjectForSession(Oid classid, Oid objid, uint16 objsubid, LOCKMODE lockmode)
Definition: lmgr.c:1062
void FlushDatabaseBuffers(Oid dbid)
Definition: bufmgr.c:3302
#define ereport(elevel, rest)
Definition: elog.h:141
#define XLogRecGetInfo(decoder)
Definition: xlogreader.h:242
char * GetDatabasePath(Oid dbNode, Oid spcNode)
Definition: relpath.c:107
#define WARNING
Definition: elog.h:40
#define stat(a, b)
Definition: win32_port.h:255
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:733
#define XLR_INFO_MASK
Definition: xlogrecord.h:62
#define XLOG_DBASE_CREATE
Oid tablespace_ids[FLEXIBLE_ARRAY_MEMBER]
#define S_ISDIR(m)
Definition: win32_port.h:296
#define AccessExclusiveLock
Definition: lockdefs.h:45
int errmsg(const char *fmt,...)
Definition: elog.c:822
void XLogDropDatabase(Oid dbid)
Definition: xlogutils.c:617
#define elog(elevel,...)
Definition: elog.h:228
int i
#define XLogRecHasAnyBlockRefs(decoder)
Definition: xlogreader.h:248
void DropDatabaseBuffers(Oid dbid)
Definition: bufmgr.c:3099
void ReplicationSlotsDropDBSlots(Oid dboid)
Definition: slot.c:896