PostgreSQL Source Code  git master
reinit.c File Reference
#include "postgres.h"
#include <unistd.h>
#include "catalog/catalog.h"
#include "common/relpath.h"
#include "storage/copydir.h"
#include "storage/fd.h"
#include "storage/reinit.h"
#include "utils/hsearch.h"
#include "utils/memutils.h"
Include dependency graph for reinit.c:

Go to the source code of this file.

Data Structures

struct  unlogged_relation_entry
 

Functions

static void ResetUnloggedRelationsInTablespaceDir (const char *tsdirname, int op)
 
static void ResetUnloggedRelationsInDbspaceDir (const char *dbspacedirname, int op)
 
static bool parse_filename_for_nontemp_relation (const char *name, int *oidchars, ForkNumber *fork)
 
void ResetUnloggedRelations (int op)
 

Function Documentation

◆ parse_filename_for_nontemp_relation()

static bool parse_filename_for_nontemp_relation ( const char *  name,
int *  oidchars,
ForkNumber fork 
)
static

Definition at line 377 of file reinit.c.

References forkname_chars(), MAIN_FORKNUM, and OIDCHARS.

Referenced by ResetUnloggedRelationsInDbspaceDir().

379 {
380  int pos;
381 
382  /* Look for a non-empty string of digits (that isn't too long). */
383  for (pos = 0; isdigit((unsigned char) name[pos]); ++pos)
384  ;
385  if (pos == 0 || pos > OIDCHARS)
386  return false;
387  *oidchars = pos;
388 
389  /* Check for a fork name. */
390  if (name[pos] != '_')
391  *fork = MAIN_FORKNUM;
392  else
393  {
394  int forkchar;
395 
396  forkchar = forkname_chars(&name[pos + 1], fork);
397  if (forkchar <= 0)
398  return false;
399  pos += forkchar + 1;
400  }
401 
402  /* Check for a segment number. */
403  if (name[pos] == '.')
404  {
405  int segchar;
406 
407  for (segchar = 1; isdigit((unsigned char) name[pos + segchar]); ++segchar)
408  ;
409  if (segchar <= 1)
410  return false;
411  pos += segchar;
412  }
413 
414  /* Now we should be at the end. */
415  if (name[pos] != '\0')
416  return false;
417  return true;
418 }
#define OIDCHARS
Definition: catalog.h:25
int forkname_chars(const char *str, ForkNumber *fork)
Definition: relpath.c:79
const char * name
Definition: encode.c:521

◆ ResetUnloggedRelations()

void ResetUnloggedRelations ( int  op)

Definition at line 49 of file reinit.c.

References AllocateDir(), ALLOCSET_DEFAULT_SIZES, AllocSetContextCreate, CurrentMemoryContext, dirent::d_name, DEBUG1, elog, FreeDir(), MAXPGPATH, MemoryContextDelete(), MemoryContextSwitchTo(), ReadDir(), ResetUnloggedRelationsInTablespaceDir(), snprintf(), TABLESPACE_VERSION_DIRECTORY, UNLOGGED_RELATION_CLEANUP, and UNLOGGED_RELATION_INIT.

Referenced by StartupXLOG().

50 {
51  char temp_path[MAXPGPATH + 10 + sizeof(TABLESPACE_VERSION_DIRECTORY)];
52  DIR *spc_dir;
53  struct dirent *spc_de;
54  MemoryContext tmpctx,
55  oldctx;
56 
57  /* Log it. */
58  elog(DEBUG1, "resetting unlogged relations: cleanup %d init %d",
59  (op & UNLOGGED_RELATION_CLEANUP) != 0,
60  (op & UNLOGGED_RELATION_INIT) != 0);
61 
62  /*
63  * Just to be sure we don't leak any memory, let's create a temporary
64  * memory context for this operation.
65  */
67  "ResetUnloggedRelations",
69  oldctx = MemoryContextSwitchTo(tmpctx);
70 
71  /*
72  * First process unlogged files in pg_default ($PGDATA/base)
73  */
75 
76  /*
77  * Cycle through directories for all non-default tablespaces.
78  */
79  spc_dir = AllocateDir("pg_tblspc");
80 
81  while ((spc_de = ReadDir(spc_dir, "pg_tblspc")) != NULL)
82  {
83  if (strcmp(spc_de->d_name, ".") == 0 ||
84  strcmp(spc_de->d_name, "..") == 0)
85  continue;
86 
87  snprintf(temp_path, sizeof(temp_path), "pg_tblspc/%s/%s",
90  }
91 
92  FreeDir(spc_dir);
93 
94  /*
95  * Restore memory context.
96  */
97  MemoryContextSwitchTo(oldctx);
98  MemoryContextDelete(tmpctx);
99 }
void MemoryContextDelete(MemoryContext context)
Definition: mcxt.c:198
#define DEBUG1
Definition: elog.h:25
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
int snprintf(char *str, size_t count, const char *fmt,...) pg_attribute_printf(3
Definition: dirent.h:9
Definition: dirent.c:25
#define MAXPGPATH
#define ALLOCSET_DEFAULT_SIZES
Definition: memutils.h:197
#define UNLOGGED_RELATION_INIT
Definition: reinit.h:21
DIR * AllocateDir(const char *dirname)
Definition: fd.c:2597
MemoryContext CurrentMemoryContext
Definition: mcxt.c:37
#define AllocSetContextCreate(parent, name, allocparams)
Definition: memutils.h:165
struct dirent * ReadDir(DIR *dir, const char *dirname)
Definition: fd.c:2663
#define TABLESPACE_VERSION_DIRECTORY
Definition: catalog.h:26
#define UNLOGGED_RELATION_CLEANUP
Definition: reinit.h:20
static void ResetUnloggedRelationsInTablespaceDir(const char *tsdirname, int op)
Definition: reinit.c:105
char d_name[MAX_PATH]
Definition: dirent.h:14
#define elog
Definition: elog.h:219
int FreeDir(DIR *dir)
Definition: fd.c:2715

◆ ResetUnloggedRelationsInDbspaceDir()

static void ResetUnloggedRelationsInDbspaceDir ( const char *  dbspacedirname,
int  op 
)
static

Definition at line 152 of file reinit.c.

References AllocateDir(), Assert, copy_file(), dirent::d_name, DEBUG2, dstpath, elog, HASHCTL::entrysize, ereport, errcode_for_file_access(), errmsg(), ERROR, forkNames, FreeDir(), fsync_fname(), hash(), hash_create(), hash_destroy(), HASH_ELEM, HASH_ENTER, HASH_FIND, hash_get_num_entries(), hash_search(), INIT_FORKNUM, HASHCTL::keysize, MAXPGPATH, unlogged_relation_entry::oid, OIDCHARS, parse_filename_for_nontemp_relation(), ReadDir(), snprintf(), UNLOGGED_RELATION_CLEANUP, and UNLOGGED_RELATION_INIT.

Referenced by ResetUnloggedRelationsInTablespaceDir().

153 {
154  DIR *dbspace_dir;
155  struct dirent *de;
156  char rm_path[MAXPGPATH * 2];
157 
158  /* Caller must specify at least one operation. */
160 
161  /*
162  * Cleanup is a two-pass operation. First, we go through and identify all
163  * the files with init forks. Then, we go through again and nuke
164  * everything with the same OID except the init fork.
165  */
166  if ((op & UNLOGGED_RELATION_CLEANUP) != 0)
167  {
168  HTAB *hash;
169  HASHCTL ctl;
170 
171  /*
172  * It's possible that someone could create a ton of unlogged relations
173  * in the same database & tablespace, so we'd better use a hash table
174  * rather than an array or linked list to keep track of which files
175  * need to be reset. Otherwise, this cleanup operation would be
176  * O(n^2).
177  */
178  memset(&ctl, 0, sizeof(ctl));
179  ctl.keysize = sizeof(unlogged_relation_entry);
180  ctl.entrysize = sizeof(unlogged_relation_entry);
181  hash = hash_create("unlogged hash", 32, &ctl, HASH_ELEM);
182 
183  /* Scan the directory. */
184  dbspace_dir = AllocateDir(dbspacedirname);
185  while ((de = ReadDir(dbspace_dir, dbspacedirname)) != NULL)
186  {
187  ForkNumber forkNum;
188  int oidchars;
190 
191  /* Skip anything that doesn't look like a relation data file. */
192  if (!parse_filename_for_nontemp_relation(de->d_name, &oidchars,
193  &forkNum))
194  continue;
195 
196  /* Also skip it unless this is the init fork. */
197  if (forkNum != INIT_FORKNUM)
198  continue;
199 
200  /*
201  * Put the OID portion of the name into the hash table, if it
202  * isn't already.
203  */
204  memset(ent.oid, 0, sizeof(ent.oid));
205  memcpy(ent.oid, de->d_name, oidchars);
206  hash_search(hash, &ent, HASH_ENTER, NULL);
207  }
208 
209  /* Done with the first pass. */
210  FreeDir(dbspace_dir);
211 
212  /*
213  * If we didn't find any init forks, there's no point in continuing;
214  * we can bail out now.
215  */
216  if (hash_get_num_entries(hash) == 0)
217  {
218  hash_destroy(hash);
219  return;
220  }
221 
222  /*
223  * Now, make a second pass and remove anything that matches.
224  */
225  dbspace_dir = AllocateDir(dbspacedirname);
226  while ((de = ReadDir(dbspace_dir, dbspacedirname)) != NULL)
227  {
228  ForkNumber forkNum;
229  int oidchars;
230  bool found;
232 
233  /* Skip anything that doesn't look like a relation data file. */
234  if (!parse_filename_for_nontemp_relation(de->d_name, &oidchars,
235  &forkNum))
236  continue;
237 
238  /* We never remove the init fork. */
239  if (forkNum == INIT_FORKNUM)
240  continue;
241 
242  /*
243  * See whether the OID portion of the name shows up in the hash
244  * table.
245  */
246  memset(ent.oid, 0, sizeof(ent.oid));
247  memcpy(ent.oid, de->d_name, oidchars);
248  hash_search(hash, &ent, HASH_FIND, &found);
249 
250  /* If so, nuke it! */
251  if (found)
252  {
253  snprintf(rm_path, sizeof(rm_path), "%s/%s",
254  dbspacedirname, de->d_name);
255  if (unlink(rm_path) < 0)
256  ereport(ERROR,
258  errmsg("could not remove file \"%s\": %m",
259  rm_path)));
260  else
261  elog(DEBUG2, "unlinked file \"%s\"", rm_path);
262  }
263  }
264 
265  /* Cleanup is complete. */
266  FreeDir(dbspace_dir);
267  hash_destroy(hash);
268  }
269 
270  /*
271  * Initialization happens after cleanup is complete: we copy each init
272  * fork file to the corresponding main fork file. Note that if we are
273  * asked to do both cleanup and init, we may never get here: if the
274  * cleanup code determines that there are no init forks in this dbspace,
275  * it will return before we get to this point.
276  */
277  if ((op & UNLOGGED_RELATION_INIT) != 0)
278  {
279  /* Scan the directory. */
280  dbspace_dir = AllocateDir(dbspacedirname);
281  while ((de = ReadDir(dbspace_dir, dbspacedirname)) != NULL)
282  {
283  ForkNumber forkNum;
284  int oidchars;
285  char oidbuf[OIDCHARS + 1];
286  char srcpath[MAXPGPATH * 2];
287  char dstpath[MAXPGPATH];
288 
289  /* Skip anything that doesn't look like a relation data file. */
290  if (!parse_filename_for_nontemp_relation(de->d_name, &oidchars,
291  &forkNum))
292  continue;
293 
294  /* Also skip it unless this is the init fork. */
295  if (forkNum != INIT_FORKNUM)
296  continue;
297 
298  /* Construct source pathname. */
299  snprintf(srcpath, sizeof(srcpath), "%s/%s",
300  dbspacedirname, de->d_name);
301 
302  /* Construct destination pathname. */
303  memcpy(oidbuf, de->d_name, oidchars);
304  oidbuf[oidchars] = '\0';
305  snprintf(dstpath, sizeof(dstpath), "%s/%s%s",
306  dbspacedirname, oidbuf, de->d_name + oidchars + 1 +
307  strlen(forkNames[INIT_FORKNUM]));
308 
309  /* OK, we're ready to perform the actual copy. */
310  elog(DEBUG2, "copying %s to %s", srcpath, dstpath);
311  copy_file(srcpath, dstpath);
312  }
313 
314  FreeDir(dbspace_dir);
315 
316  /*
317  * copy_file() above has already called pg_flush_data() on the files
318  * it created. Now we need to fsync those files, because a checkpoint
319  * won't do it for us while we're in recovery. We do this in a
320  * separate pass to allow the kernel to perform all the flushes
321  * (especially the metadata ones) at once.
322  */
323  dbspace_dir = AllocateDir(dbspacedirname);
324  while ((de = ReadDir(dbspace_dir, dbspacedirname)) != NULL)
325  {
326  ForkNumber forkNum;
327  int oidchars;
328  char oidbuf[OIDCHARS + 1];
329  char mainpath[MAXPGPATH];
330 
331  /* Skip anything that doesn't look like a relation data file. */
332  if (!parse_filename_for_nontemp_relation(de->d_name, &oidchars,
333  &forkNum))
334  continue;
335 
336  /* Also skip it unless this is the init fork. */
337  if (forkNum != INIT_FORKNUM)
338  continue;
339 
340  /* Construct main fork pathname. */
341  memcpy(oidbuf, de->d_name, oidchars);
342  oidbuf[oidchars] = '\0';
343  snprintf(mainpath, sizeof(mainpath), "%s/%s%s",
344  dbspacedirname, oidbuf, de->d_name + oidchars + 1 +
345  strlen(forkNames[INIT_FORKNUM]));
346 
347  fsync_fname(mainpath, false);
348  }
349 
350  FreeDir(dbspace_dir);
351 
352  /*
353  * Lastly, fsync the database directory itself, ensuring the
354  * filesystem remembers the file creations and deletions we've done.
355  * We don't bother with this during a call that does only
356  * UNLOGGED_RELATION_CLEANUP, because if recovery crashes before we
357  * get to doing UNLOGGED_RELATION_INIT, we'll redo the cleanup step
358  * too at the next startup attempt.
359  */
360  fsync_fname(dbspacedirname, true);
361  }
362 }
void hash_destroy(HTAB *hashp)
Definition: dynahash.c:812
#define OIDCHARS
Definition: catalog.h:25
#define HASH_ELEM
Definition: hsearch.h:87
void fsync_fname(const char *fname, bool isdir)
Definition: fd.c:582
Size entrysize
Definition: hsearch.h:73
int snprintf(char *str, size_t count, const char *fmt,...) pg_attribute_printf(3
long hash_get_num_entries(HTAB *hashp)
Definition: dynahash.c:1333
void * hash_search(HTAB *hashp, const void *keyPtr, HASHACTION action, bool *foundPtr)
Definition: dynahash.c:904
Definition: dirent.h:9
Definition: reinit.c:34
Definition: dynahash.c:208
Definition: dirent.c:25
#define ERROR
Definition: elog.h:43
#define MAXPGPATH
#define DEBUG2
Definition: elog.h:24
#define UNLOGGED_RELATION_INIT
Definition: reinit.h:21
int errcode_for_file_access(void)
Definition: elog.c:598
void copy_file(char *fromfile, char *tofile)
Definition: copydir.c:127
DIR * AllocateDir(const char *dirname)
Definition: fd.c:2597
#define ereport(elevel, rest)
Definition: elog.h:122
static char dstpath[MAXPGPATH]
Definition: file_ops.c:30
ForkNumber
Definition: relpath.h:24
static bool parse_filename_for_nontemp_relation(const char *name, int *oidchars, ForkNumber *fork)
Definition: reinit.c:377
HTAB * hash_create(const char *tabname, long nelem, HASHCTL *info, int flags)
Definition: dynahash.c:316
Size keysize
Definition: hsearch.h:72
#define Assert(condition)
Definition: c.h:680
char oid[OIDCHARS+1]
Definition: reinit.c:36
struct dirent * ReadDir(DIR *dir, const char *dirname)
Definition: fd.c:2663
#define UNLOGGED_RELATION_CLEANUP
Definition: reinit.h:20
int errmsg(const char *fmt,...)
Definition: elog.c:797
char d_name[MAX_PATH]
Definition: dirent.h:14
#define elog
Definition: elog.h:219
const char *const forkNames[]
Definition: relpath.c:34
static unsigned hash(unsigned *uv, int n)
Definition: rege_dfa.c:541
int FreeDir(DIR *dir)
Definition: fd.c:2715

◆ ResetUnloggedRelationsInTablespaceDir()

static void ResetUnloggedRelationsInTablespaceDir ( const char *  tsdirname,
int  op 
)
static

Definition at line 105 of file reinit.c.

References AllocateDir(), dirent::d_name, ereport, errcode_for_file_access(), errmsg(), FreeDir(), LOG, MAXPGPATH, ReadDir(), ResetUnloggedRelationsInDbspaceDir(), and snprintf().

Referenced by ResetUnloggedRelations().

106 {
107  DIR *ts_dir;
108  struct dirent *de;
109  char dbspace_path[MAXPGPATH * 2];
110 
111  ts_dir = AllocateDir(tsdirname);
112 
113  /*
114  * If we get ENOENT on a tablespace directory, log it and return. This
115  * can happen if a previous DROP TABLESPACE crashed between removing the
116  * tablespace directory and removing the symlink in pg_tblspc. We don't
117  * really want to prevent database startup in that scenario, so let it
118  * pass instead. Any other type of error will be reported by ReadDir
119  * (causing a startup failure).
120  */
121  if (ts_dir == NULL && errno == ENOENT)
122  {
123  ereport(LOG,
125  errmsg("could not open directory \"%s\": %m",
126  tsdirname)));
127  return;
128  }
129 
130  while ((de = ReadDir(ts_dir, tsdirname)) != NULL)
131  {
132  /*
133  * We're only interested in the per-database directories, which have
134  * numeric names. Note that this code will also (properly) ignore "."
135  * and "..".
136  */
137  if (strspn(de->d_name, "0123456789") != strlen(de->d_name))
138  continue;
139 
140  snprintf(dbspace_path, sizeof(dbspace_path), "%s/%s",
141  tsdirname, de->d_name);
142  ResetUnloggedRelationsInDbspaceDir(dbspace_path, op);
143  }
144 
145  FreeDir(ts_dir);
146 }
int snprintf(char *str, size_t count, const char *fmt,...) pg_attribute_printf(3
#define LOG
Definition: elog.h:26
Definition: dirent.h:9
static void ResetUnloggedRelationsInDbspaceDir(const char *dbspacedirname, int op)
Definition: reinit.c:152
Definition: dirent.c:25
#define MAXPGPATH
int errcode_for_file_access(void)
Definition: elog.c:598
DIR * AllocateDir(const char *dirname)
Definition: fd.c:2597
#define ereport(elevel, rest)
Definition: elog.h:122
struct dirent * ReadDir(DIR *dir, const char *dirname)
Definition: fd.c:2663
int errmsg(const char *fmt,...)
Definition: elog.c:797
char d_name[MAX_PATH]
Definition: dirent.h:14
int FreeDir(DIR *dir)
Definition: fd.c:2715