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.

◆ XLOG_DBASE_CREATE

#define XLOG_DBASE_CREATE   0x00

Definition at line 21 of file dbcommands_xlog.h.

◆ XLOG_DBASE_DROP

#define XLOG_DBASE_DROP   0x10

Definition at line 22 of file dbcommands_xlog.h.

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.

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 
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:439
#define XLOG_DBASE_DROP
#define XLOG_DBASE_CREATE
int i
Definition: isn.c:73
static char * buf
Definition: pg_test_fsync.c:70
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:91
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:176
Oid tablespace_ids[FLEXIBLE_ARRAY_MEMBER]
#define XLogRecGetInfo(decoder)
Definition: xlogreader.h:315
#define XLogRecGetData(decoder)
Definition: xlogreader.h:320
#define XLR_INFO_MASK
Definition: xlogrecord.h:62

References appendStringInfo(), appendStringInfoString(), buf, 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.

◆ dbase_identify()

const char* dbase_identify ( uint8  info)

Definition at line 48 of file dbasedesc.c.

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 }

References XLOG_DBASE_CREATE, XLOG_DBASE_DROP, and XLR_INFO_MASK.

◆ dbase_redo()

void dbase_redo ( XLogReaderState rptr)

Definition at line 2183 of file dbcommands.c.

2184 {
2185  uint8 info = XLogRecGetInfo(record) & ~XLR_INFO_MASK;
2186 
2187  /* Backup blocks are not used in dbase records */
2188  Assert(!XLogRecHasAnyBlockRefs(record));
2189 
2190  if (info == XLOG_DBASE_CREATE)
2191  {
2193  char *src_path;
2194  char *dst_path;
2195  struct stat st;
2196 
2197  src_path = GetDatabasePath(xlrec->src_db_id, xlrec->src_tablespace_id);
2198  dst_path = GetDatabasePath(xlrec->db_id, xlrec->tablespace_id);
2199 
2200  /*
2201  * Our theory for replaying a CREATE is to forcibly drop the target
2202  * subdirectory if present, then re-copy the source data. This may be
2203  * more work than needed, but it is simple to implement.
2204  */
2205  if (stat(dst_path, &st) == 0 && S_ISDIR(st.st_mode))
2206  {
2207  if (!rmtree(dst_path, true))
2208  /* If this failed, copydir() below is going to error. */
2209  ereport(WARNING,
2210  (errmsg("some useless files may be left behind in old database directory \"%s\"",
2211  dst_path)));
2212  }
2213 
2214  /*
2215  * Force dirty buffers out to disk, to ensure source database is
2216  * up-to-date for the copy.
2217  */
2219 
2220  /*
2221  * Copy this subdirectory to the new location
2222  *
2223  * We don't need to copy subdirectories
2224  */
2225  copydir(src_path, dst_path, false);
2226  }
2227  else if (info == XLOG_DBASE_DROP)
2228  {
2229  xl_dbase_drop_rec *xlrec = (xl_dbase_drop_rec *) XLogRecGetData(record);
2230  char *dst_path;
2231  int i;
2232 
2233  if (InHotStandby)
2234  {
2235  /*
2236  * Lock database while we resolve conflicts to ensure that
2237  * InitPostgres() cannot fully re-execute concurrently. This
2238  * avoids backends re-connecting automatically to same database,
2239  * which can happen in some cases.
2240  *
2241  * This will lock out walsenders trying to connect to db-specific
2242  * slots for logical decoding too, so it's safe for us to drop
2243  * slots.
2244  */
2245  LockSharedObjectForSession(DatabaseRelationId, xlrec->db_id, 0, AccessExclusiveLock);
2247  }
2248 
2249  /* Drop any database-specific replication slots */
2251 
2252  /* Drop pages for this database that are in the shared buffer cache */
2253  DropDatabaseBuffers(xlrec->db_id);
2254 
2255  /* Also, clean out any fsync requests that might be pending in md.c */
2257 
2258  /* Clean out the xlog relcache too */
2259  XLogDropDatabase(xlrec->db_id);
2260 
2261  for (i = 0; i < xlrec->ntablespaces; i++)
2262  {
2263  dst_path = GetDatabasePath(xlrec->db_id, xlrec->tablespace_ids[i]);
2264 
2265  /* And remove the physical files */
2266  if (!rmtree(dst_path, true))
2267  ereport(WARNING,
2268  (errmsg("some useless files may be left behind in old database directory \"%s\"",
2269  dst_path)));
2270  pfree(dst_path);
2271  }
2272 
2273  if (InHotStandby)
2274  {
2275  /*
2276  * Release locks prior to commit. XXX There is a race condition
2277  * here that may allow backends to reconnect, but the window for
2278  * this is small because the gap between here and commit is mostly
2279  * fairly small and it is unlikely that people will be dropping
2280  * databases that we are trying to connect to anyway.
2281  */
2282  UnlockSharedObjectForSession(DatabaseRelationId, xlrec->db_id, 0, AccessExclusiveLock);
2283  }
2284  }
2285  else
2286  elog(PANIC, "dbase_redo: unknown op code %u", info);
2287 }
void DropDatabaseBuffers(Oid dbid)
Definition: bufmgr.c:3397
void FlushDatabaseBuffers(Oid dbid)
Definition: bufmgr.c:3694
void copydir(char *fromdir, char *todir, bool recurse)
Definition: copydir.c:37
int errmsg(const char *fmt,...)
Definition: elog.c:904
#define WARNING
Definition: elog.h:30
#define PANIC
Definition: elog.h:36
#define elog(elevel,...)
Definition: elog.h:218
#define ereport(elevel,...)
Definition: elog.h:143
Assert(fmt[strlen(fmt) - 1] !='\n')
void UnlockSharedObjectForSession(Oid classid, Oid objid, uint16 objsubid, LOCKMODE lockmode)
Definition: lmgr.c:1078
void LockSharedObjectForSession(Oid classid, Oid objid, uint16 objsubid, LOCKMODE lockmode)
Definition: lmgr.c:1060
#define AccessExclusiveLock
Definition: lockdefs.h:43
void pfree(void *pointer)
Definition: mcxt.c:1169
void ForgetDatabaseSyncRequests(Oid dbid)
Definition: md.c:1032
char * GetDatabasePath(Oid dbNode, Oid spcNode)
Definition: relpath.c:110
bool rmtree(const char *path, bool rmtopdir)
Definition: rmtree.c:42
void ReplicationSlotsDropDBSlots(Oid dboid)
Definition: slot.c:968
void ResolveRecoveryConflictWithDatabase(Oid dbid)
Definition: standby.c:529
#define stat
Definition: win32_port.h:283
#define S_ISDIR(m)
Definition: win32_port.h:324
#define XLogRecHasAnyBlockRefs(decoder)
Definition: xlogreader.h:322
void XLogDropDatabase(Oid dbid)
Definition: xlogutils.c:643
#define InHotStandby
Definition: xlogutils.h:57

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::st_mode, 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.