PostgreSQL Source Code  git master
filemap.c File Reference
#include "postgres_fe.h"
#include <sys/stat.h>
#include <unistd.h>
#include "catalog/pg_tablespace_d.h"
#include "common/file_utils.h"
#include "common/hashfn.h"
#include "common/string.h"
#include "datapagemap.h"
#include "filemap.h"
#include "pg_rewind.h"
#include "lib/simplehash.h"
Include dependency graph for filemap.c:

Go to the source code of this file.

Data Structures

struct  exclude_list_item
 

Macros

#define SH_PREFIX   filehash
 
#define SH_ELEMENT_TYPE   file_entry_t
 
#define SH_KEY_TYPE   const char *
 
#define SH_KEY   path
 
#define SH_HASH_KEY(tb, key)   hash_string_pointer(key)
 
#define SH_EQUAL(tb, a, b)   (strcmp(a, b) == 0)
 
#define SH_SCOPE   static inline
 
#define SH_RAW_ALLOCATOR   pg_malloc0
 
#define SH_DECLARE
 
#define SH_DEFINE
 
#define FILEHASH_INITIAL_SIZE   1000
 

Functions

static uint32 hash_string_pointer (const char *s)
 
static bool isRelDataFile (const char *path)
 
static char * datasegpath (RelFileLocator rlocator, ForkNumber forknum, BlockNumber segno)
 
static file_entry_tinsert_filehash_entry (const char *path)
 
static file_entry_tlookup_filehash_entry (const char *path)
 
static int final_filemap_cmp (const void *a, const void *b)
 
static bool check_file_excluded (const char *path, bool is_source)
 
void filehash_init (void)
 
void process_source_file (const char *path, file_type_t type, size_t size, const char *link_target)
 
void process_target_file (const char *path, file_type_t type, size_t size, const char *link_target)
 
void process_target_wal_block_change (ForkNumber forknum, RelFileLocator rlocator, BlockNumber blkno)
 
static const char * action_to_str (file_action_t action)
 
void calculate_totals (filemap_t *filemap)
 
void print_filemap (filemap_t *filemap)
 
static file_action_t decide_file_action (file_entry_t *entry)
 
filemap_tdecide_file_actions (void)
 

Variables

static filehash_hash * filehash
 
static const char *const excludeDirContents []
 
static const struct exclude_list_item excludeFiles []
 

Macro Definition Documentation

◆ FILEHASH_INITIAL_SIZE

#define FILEHASH_INITIAL_SIZE   1000

Definition at line 54 of file filemap.c.

◆ SH_DECLARE

#define SH_DECLARE

Definition at line 50 of file filemap.c.

◆ SH_DEFINE

#define SH_DEFINE

Definition at line 51 of file filemap.c.

◆ SH_ELEMENT_TYPE

#define SH_ELEMENT_TYPE   file_entry_t

Definition at line 43 of file filemap.c.

◆ SH_EQUAL

#define SH_EQUAL (   tb,
  a,
  b 
)    (strcmp(a, b) == 0)

Definition at line 47 of file filemap.c.

◆ SH_HASH_KEY

#define SH_HASH_KEY (   tb,
  key 
)    hash_string_pointer(key)

Definition at line 46 of file filemap.c.

◆ SH_KEY

#define SH_KEY   path

Definition at line 45 of file filemap.c.

◆ SH_KEY_TYPE

#define SH_KEY_TYPE   const char *

Definition at line 44 of file filemap.c.

◆ SH_PREFIX

#define SH_PREFIX   filehash

Definition at line 42 of file filemap.c.

◆ SH_RAW_ALLOCATOR

#define SH_RAW_ALLOCATOR   pg_malloc0

Definition at line 49 of file filemap.c.

◆ SH_SCOPE

#define SH_SCOPE   static inline

Definition at line 48 of file filemap.c.

Function Documentation

◆ action_to_str()

static const char* action_to_str ( file_action_t  action)
static

Definition at line 412 of file filemap.c.

413 {
414  switch (action)
415  {
416  case FILE_ACTION_NONE:
417  return "NONE";
418  case FILE_ACTION_COPY:
419  return "COPY";
421  return "TRUNCATE";
423  return "COPY_TAIL";
424  case FILE_ACTION_CREATE:
425  return "CREATE";
426  case FILE_ACTION_REMOVE:
427  return "REMOVE";
428 
429  default:
430  return "unknown";
431  }
432 }
@ FILE_ACTION_REMOVE
Definition: filemap.h:27
@ FILE_ACTION_COPY
Definition: filemap.h:21
@ FILE_ACTION_NONE
Definition: filemap.h:24
@ FILE_ACTION_COPY_TAIL
Definition: filemap.h:22
@ FILE_ACTION_TRUNCATE
Definition: filemap.h:26
@ FILE_ACTION_CREATE
Definition: filemap.h:20

References generate_unaccent_rules::action, FILE_ACTION_COPY, FILE_ACTION_COPY_TAIL, FILE_ACTION_CREATE, FILE_ACTION_NONE, FILE_ACTION_REMOVE, and FILE_ACTION_TRUNCATE.

Referenced by print_filemap().

◆ calculate_totals()

void calculate_totals ( filemap_t filemap)

Definition at line 438 of file filemap.c.

439 {
440  file_entry_t *entry;
441  int i;
442 
443  filemap->total_size = 0;
444  filemap->fetch_size = 0;
445 
446  for (i = 0; i < filemap->nentries; i++)
447  {
448  entry = filemap->entries[i];
449 
450  if (entry->source_type != FILE_TYPE_REGULAR)
451  continue;
452 
453  filemap->total_size += entry->source_size;
454 
455  if (entry->action == FILE_ACTION_COPY)
456  {
457  filemap->fetch_size += entry->source_size;
458  continue;
459  }
460 
461  if (entry->action == FILE_ACTION_COPY_TAIL)
462  filemap->fetch_size += (entry->source_size - entry->target_size);
463 
464  if (entry->target_pages_to_overwrite.bitmapsize > 0)
465  {
467  BlockNumber blk;
468 
470  while (datapagemap_next(iter, &blk))
471  filemap->fetch_size += BLCKSZ;
472 
473  pg_free(iter);
474  }
475  }
476 }
uint32 BlockNumber
Definition: block.h:31
bool datapagemap_next(datapagemap_iterator_t *iter, BlockNumber *blkno)
Definition: datapagemap.c:87
datapagemap_iterator_t * datapagemap_iterate(datapagemap_t *map)
Definition: datapagemap.c:75
void pg_free(void *ptr)
Definition: fe_memutils.c:105
@ FILE_TYPE_REGULAR
Definition: filemap.h:34
int i
Definition: isn.c:73
int bitmapsize
Definition: datapagemap.h:18
Definition: filemap.h:50
datapagemap_t target_pages_to_overwrite
Definition: filemap.h:68
size_t source_size
Definition: filemap.h:75
file_type_t source_type
Definition: filemap.h:74
size_t target_size
Definition: filemap.h:61
file_action_t action
Definition: filemap.h:81
file_entry_t * entries[FLEXIBLE_ARRAY_MEMBER]
Definition: filemap.h:96
int nentries
Definition: filemap.h:95
uint64 total_size
Definition: filemap.h:92
uint64 fetch_size
Definition: filemap.h:93

References file_entry_t::action, datapagemap::bitmapsize, datapagemap_iterate(), datapagemap_next(), filemap_t::entries, filemap_t::fetch_size, FILE_ACTION_COPY, FILE_ACTION_COPY_TAIL, FILE_TYPE_REGULAR, i, filemap_t::nentries, pg_free(), file_entry_t::source_size, file_entry_t::source_type, file_entry_t::target_pages_to_overwrite, file_entry_t::target_size, and filemap_t::total_size.

Referenced by main().

◆ check_file_excluded()

static bool check_file_excluded ( const char *  path,
bool  is_source 
)
static

Definition at line 348 of file filemap.c.

349 {
350  char localpath[MAXPGPATH];
351  int excludeIdx;
352  const char *filename;
353 
354  /*
355  * Skip all temporary files, .../pgsql_tmp/... and .../pgsql_tmp.*
356  */
357  if (strstr(path, "/" PG_TEMP_FILE_PREFIX) != NULL ||
358  strstr(path, "/" PG_TEMP_FILES_DIR "/") != NULL)
359  {
360  return true;
361  }
362 
363  /* check individual files... */
364  for (excludeIdx = 0; excludeFiles[excludeIdx].name != NULL; excludeIdx++)
365  {
366  int cmplen = strlen(excludeFiles[excludeIdx].name);
367 
369  if (filename == NULL)
370  filename = path;
371  else
372  filename++;
373 
374  if (!excludeFiles[excludeIdx].match_prefix)
375  cmplen++;
376  if (strncmp(filename, excludeFiles[excludeIdx].name, cmplen) == 0)
377  {
378  if (is_source)
379  pg_log_debug("entry \"%s\" excluded from source file list",
380  path);
381  else
382  pg_log_debug("entry \"%s\" excluded from target file list",
383  path);
384  return true;
385  }
386  }
387 
388  /*
389  * ... And check some directories. Note that this includes any contents
390  * within the directories themselves.
391  */
392  for (excludeIdx = 0; excludeDirContents[excludeIdx] != NULL; excludeIdx++)
393  {
394  snprintf(localpath, sizeof(localpath), "%s/",
395  excludeDirContents[excludeIdx]);
396  if (strstr(path, localpath) == path)
397  {
398  if (is_source)
399  pg_log_debug("entry \"%s\" excluded from source file list",
400  path);
401  else
402  pg_log_debug("entry \"%s\" excluded from target file list",
403  path);
404  return true;
405  }
406  }
407 
408  return false;
409 }
#define PG_TEMP_FILES_DIR
Definition: file_utils.h:62
#define PG_TEMP_FILE_PREFIX
Definition: file_utils.h:63
static const struct exclude_list_item excludeFiles[]
Definition: filemap.c:129
static const char *const excludeDirContents[]
Definition: filemap.c:88
#define pg_log_debug(...)
Definition: logging.h:133
#define MAXPGPATH
static char * filename
Definition: pg_dumpall.c:121
char * last_dir_separator(const char *filename)
Definition: path.c:139
#define snprintf
Definition: port.h:238
const char * name
Definition: basebackup.c:139
const char * name

References excludeDirContents, excludeFiles, filename, last_dir_separator(), exclude_list_item::match_prefix, MAXPGPATH, exclude_list_item::name, name, pg_log_debug, PG_TEMP_FILE_PREFIX, PG_TEMP_FILES_DIR, and snprintf.

Referenced by decide_file_action().

◆ datasegpath()

static char * datasegpath ( RelFileLocator  rlocator,
ForkNumber  forknum,
BlockNumber  segno 
)
static

Definition at line 592 of file filemap.c.

593 {
594  char *path;
595  char *segpath;
596 
597  path = relpathperm(rlocator, forknum);
598  if (segno > 0)
599  {
600  segpath = psprintf("%s.%u", path, segno);
601  pfree(path);
602  return segpath;
603  }
604  else
605  return path;
606 }
void pfree(void *pointer)
Definition: mcxt.c:1508
char * psprintf(const char *fmt,...)
Definition: psprintf.c:46
#define relpathperm(rlocator, forknum)
Definition: relpath.h:90

References pfree(), psprintf(), and relpathperm.

Referenced by isRelDataFile(), and process_target_wal_block_change().

◆ decide_file_action()

static file_action_t decide_file_action ( file_entry_t entry)
static

Definition at line 639 of file filemap.c.

640 {
641  const char *path = entry->path;
642 
643  /*
644  * Don't touch the control file. It is handled specially, after copying
645  * all the other files.
646  */
647  if (strcmp(path, "global/pg_control") == 0)
648  return FILE_ACTION_NONE;
649 
650  /* Skip macOS system files */
651  if (strstr(path, ".DS_Store") != NULL)
652  return FILE_ACTION_NONE;
653 
654  /*
655  * Remove all files matching the exclusion filters in the target.
656  */
657  if (check_file_excluded(path, true))
658  {
659  if (entry->target_exists)
660  return FILE_ACTION_REMOVE;
661  else
662  return FILE_ACTION_NONE;
663  }
664 
665  /*
666  * Handle cases where the file is missing from one of the systems.
667  */
668  if (!entry->target_exists && entry->source_exists)
669  {
670  /*
671  * File exists in source, but not in target. Copy it in toto. (If it's
672  * a relation data file, WAL replay after rewinding should re-create
673  * it anyway. But there's no harm in copying it now.)
674  */
675  switch (entry->source_type)
676  {
677  case FILE_TYPE_DIRECTORY:
678  case FILE_TYPE_SYMLINK:
679  return FILE_ACTION_CREATE;
680  case FILE_TYPE_REGULAR:
681  return FILE_ACTION_COPY;
682  case FILE_TYPE_UNDEFINED:
683  pg_fatal("unknown file type for \"%s\"", entry->path);
684  break;
685  }
686  }
687  else if (entry->target_exists && !entry->source_exists)
688  {
689  /* File exists in target, but not source. Remove it. */
690  return FILE_ACTION_REMOVE;
691  }
692  else if (!entry->target_exists && !entry->source_exists)
693  {
694  /*
695  * Doesn't exist in either server. Why does it have an entry in the
696  * first place??
697  */
698  Assert(false);
699  return FILE_ACTION_NONE;
700  }
701 
702  /*
703  * Otherwise, the file exists on both systems
704  */
705  Assert(entry->target_exists && entry->source_exists);
706 
707  if (entry->source_type != entry->target_type)
708  {
709  /* But it's a different kind of object. Strange.. */
710  pg_fatal("file \"%s\" is of different type in source and target", entry->path);
711  }
712 
713  /*
714  * PG_VERSION files should be identical on both systems, but avoid
715  * overwriting them for paranoia.
716  */
717  if (pg_str_endswith(entry->path, "PG_VERSION"))
718  return FILE_ACTION_NONE;
719 
720  switch (entry->source_type)
721  {
722  case FILE_TYPE_DIRECTORY:
723  return FILE_ACTION_NONE;
724 
725  case FILE_TYPE_SYMLINK:
726 
727  /*
728  * XXX: Should we check if it points to the same target?
729  */
730  return FILE_ACTION_NONE;
731 
732  case FILE_TYPE_REGULAR:
733  if (!entry->isrelfile)
734  {
735  /*
736  * It's a non-data file that we have no special processing
737  * for. Copy it in toto.
738  */
739  return FILE_ACTION_COPY;
740  }
741  else
742  {
743  /*
744  * It's a data file that exists in both systems.
745  *
746  * If it's larger in target, we can truncate it. There will
747  * also be a WAL record of the truncation in the source
748  * system, so WAL replay would eventually truncate the target
749  * too, but we might as well do it now.
750  *
751  * If it's smaller in the target, it means that it has been
752  * truncated in the target, or enlarged in the source, or
753  * both. If it was truncated in the target, we need to copy
754  * the missing tail from the source system. If it was enlarged
755  * in the source system, there will be WAL records in the
756  * source system for the new blocks, so we wouldn't need to
757  * copy them here. But we don't know which scenario we're
758  * dealing with, and there's no harm in copying the missing
759  * blocks now, so do it now.
760  *
761  * If it's the same size, do nothing here. Any blocks modified
762  * in the target will be copied based on parsing the target
763  * system's WAL, and any blocks modified in the source will be
764  * updated after rewinding, when the source system's WAL is
765  * replayed.
766  */
767  if (entry->target_size < entry->source_size)
768  return FILE_ACTION_COPY_TAIL;
769  else if (entry->target_size > entry->source_size)
770  return FILE_ACTION_TRUNCATE;
771  else
772  return FILE_ACTION_NONE;
773  }
774  break;
775 
776  case FILE_TYPE_UNDEFINED:
777  pg_fatal("unknown file type for \"%s\"", path);
778  break;
779  }
780 
781  /* unreachable */
782  pg_fatal("could not decide what to do with file \"%s\"", path);
783 }
static bool check_file_excluded(const char *path, bool is_source)
Definition: filemap.c:348
@ FILE_TYPE_UNDEFINED
Definition: filemap.h:32
@ FILE_TYPE_SYMLINK
Definition: filemap.h:36
@ FILE_TYPE_DIRECTORY
Definition: filemap.h:35
Assert(fmt[strlen(fmt) - 1] !='\n')
#define pg_fatal(...)
bool pg_str_endswith(const char *str, const char *end)
Definition: string.c:32
const char * path
Definition: filemap.h:53
bool source_exists
Definition: filemap.h:73
bool target_exists
Definition: filemap.h:59
file_type_t target_type
Definition: filemap.h:60
bool isrelfile
Definition: filemap.h:54

References Assert(), check_file_excluded(), FILE_ACTION_COPY, FILE_ACTION_COPY_TAIL, FILE_ACTION_CREATE, FILE_ACTION_NONE, FILE_ACTION_REMOVE, FILE_ACTION_TRUNCATE, FILE_TYPE_DIRECTORY, FILE_TYPE_REGULAR, FILE_TYPE_SYMLINK, FILE_TYPE_UNDEFINED, file_entry_t::isrelfile, file_entry_t::path, pg_fatal, pg_str_endswith(), file_entry_t::source_exists, file_entry_t::source_size, file_entry_t::source_type, file_entry_t::target_exists, file_entry_t::target_size, and file_entry_t::target_type.

Referenced by decide_file_actions().

◆ decide_file_actions()

filemap_t* decide_file_actions ( void  )

Definition at line 792 of file filemap.c.

793 {
794  int i;
795  filehash_iterator it;
796  file_entry_t *entry;
797  filemap_t *filemap;
798 
799  filehash_start_iterate(filehash, &it);
800  while ((entry = filehash_iterate(filehash, &it)) != NULL)
801  {
802  entry->action = decide_file_action(entry);
803  }
804 
805  /*
806  * Turn the hash table into an array, and sort in the order that the
807  * actions should be performed.
808  */
809  filemap = pg_malloc(offsetof(filemap_t, entries) +
810  filehash->members * sizeof(file_entry_t *));
811  filemap->nentries = filehash->members;
812  filehash_start_iterate(filehash, &it);
813  i = 0;
814  while ((entry = filehash_iterate(filehash, &it)) != NULL)
815  {
816  filemap->entries[i++] = entry;
817  }
818 
819  qsort(&filemap->entries, filemap->nentries, sizeof(file_entry_t *),
821 
822  return filemap;
823 }
void * pg_malloc(size_t size)
Definition: fe_memutils.c:47
static filehash_hash * filehash
Definition: filemap.c:56
static file_action_t decide_file_action(file_entry_t *entry)
Definition: filemap.c:639
static int final_filemap_cmp(const void *a, const void *b)
Definition: filemap.c:619
#define qsort(a, b, c, d)
Definition: port.h:449

References file_entry_t::action, decide_file_action(), filemap_t::entries, filehash, final_filemap_cmp(), i, filemap_t::nentries, pg_malloc(), and qsort.

Referenced by main().

◆ filehash_init()

void filehash_init ( void  )

Definition at line 168 of file filemap.c.

169 {
170  filehash = filehash_create(FILEHASH_INITIAL_SIZE, NULL);
171 }
#define FILEHASH_INITIAL_SIZE
Definition: filemap.c:54

References filehash, and FILEHASH_INITIAL_SIZE.

Referenced by main().

◆ final_filemap_cmp()

static int final_filemap_cmp ( const void *  a,
const void *  b 
)
static

Definition at line 619 of file filemap.c.

620 {
621  file_entry_t *fa = *((file_entry_t **) a);
622  file_entry_t *fb = *((file_entry_t **) b);
623 
624  if (fa->action > fb->action)
625  return 1;
626  if (fa->action < fb->action)
627  return -1;
628 
629  if (fa->action == FILE_ACTION_REMOVE)
630  return strcmp(fb->path, fa->path);
631  else
632  return strcmp(fa->path, fb->path);
633 }
int b
Definition: isn.c:70
int a
Definition: isn.c:69
static int fa(void)
Definition: preproc-init.c:85
static int fb(int x)
Definition: preproc-init.c:92

References a, b, fa(), fb(), and FILE_ACTION_REMOVE.

Referenced by decide_file_actions().

◆ hash_string_pointer()

static uint32 hash_string_pointer ( const char *  s)
static

Definition at line 830 of file filemap.c.

831 {
832  unsigned char *ss = (unsigned char *) s;
833 
834  return hash_bytes(ss, strlen(s));
835 }
uint32 hash_bytes(const unsigned char *k, int keylen)
Definition: hashfn.c:146

References hash_bytes().

◆ insert_filehash_entry()

static file_entry_t * insert_filehash_entry ( const char *  path)
static

Definition at line 175 of file filemap.c.

176 {
177  file_entry_t *entry;
178  bool found;
179 
180  entry = filehash_insert(filehash, path, &found);
181  if (!found)
182  {
183  entry->path = pg_strdup(path);
184  entry->isrelfile = isRelDataFile(path);
185 
186  entry->target_exists = false;
188  entry->target_size = 0;
189  entry->target_link_target = NULL;
190  entry->target_pages_to_overwrite.bitmap = NULL;
192 
193  entry->source_exists = false;
195  entry->source_size = 0;
196  entry->source_link_target = NULL;
197 
198  entry->action = FILE_ACTION_UNDECIDED;
199  }
200 
201  return entry;
202 }
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
static bool isRelDataFile(const char *path)
Definition: filemap.c:509
@ FILE_ACTION_UNDECIDED
Definition: filemap.h:18
char * bitmap
Definition: datapagemap.h:17
char * source_link_target
Definition: filemap.h:76
char * target_link_target
Definition: filemap.h:62

References file_entry_t::action, datapagemap::bitmap, datapagemap::bitmapsize, FILE_ACTION_UNDECIDED, FILE_TYPE_UNDEFINED, filehash, isRelDataFile(), file_entry_t::isrelfile, file_entry_t::path, pg_strdup(), file_entry_t::source_exists, file_entry_t::source_link_target, file_entry_t::source_size, file_entry_t::source_type, file_entry_t::target_exists, file_entry_t::target_link_target, file_entry_t::target_pages_to_overwrite, file_entry_t::target_size, and file_entry_t::target_type.

Referenced by process_source_file(), and process_target_file().

◆ isRelDataFile()

static bool isRelDataFile ( const char *  path)
static

Definition at line 509 of file filemap.c.

510 {
511  RelFileLocator rlocator;
512  unsigned int segNo;
513  int nmatch;
514  bool matched;
515 
516  /*----
517  * Relation data files can be in one of the following directories:
518  *
519  * global/
520  * shared relations
521  *
522  * base/<db oid>/
523  * regular relations, default tablespace
524  *
525  * pg_tblspc/<tblspc oid>/<tblspc version>/
526  * within a non-default tablespace (the name of the directory
527  * depends on version)
528  *
529  * And the relation data files themselves have a filename like:
530  *
531  * <oid>.<segment number>
532  *
533  *----
534  */
535  rlocator.spcOid = InvalidOid;
536  rlocator.dbOid = InvalidOid;
537  rlocator.relNumber = InvalidRelFileNumber;
538  segNo = 0;
539  matched = false;
540 
541  nmatch = sscanf(path, "global/%u.%u", &rlocator.relNumber, &segNo);
542  if (nmatch == 1 || nmatch == 2)
543  {
544  rlocator.spcOid = GLOBALTABLESPACE_OID;
545  rlocator.dbOid = 0;
546  matched = true;
547  }
548  else
549  {
550  nmatch = sscanf(path, "base/%u/%u.%u",
551  &rlocator.dbOid, &rlocator.relNumber, &segNo);
552  if (nmatch == 2 || nmatch == 3)
553  {
554  rlocator.spcOid = DEFAULTTABLESPACE_OID;
555  matched = true;
556  }
557  else
558  {
559  nmatch = sscanf(path, "pg_tblspc/%u/" TABLESPACE_VERSION_DIRECTORY "/%u/%u.%u",
560  &rlocator.spcOid, &rlocator.dbOid, &rlocator.relNumber,
561  &segNo);
562  if (nmatch == 3 || nmatch == 4)
563  matched = true;
564  }
565  }
566 
567  /*
568  * The sscanf tests above can match files that have extra characters at
569  * the end. To eliminate such cases, cross-check that GetRelationPath
570  * creates the exact same filename, when passed the RelFileLocator
571  * information we extracted from the filename.
572  */
573  if (matched)
574  {
575  char *check_path = datasegpath(rlocator, MAIN_FORKNUM, segNo);
576 
577  if (strcmp(check_path, path) != 0)
578  matched = false;
579 
580  pfree(check_path);
581  }
582 
583  return matched;
584 }
static char * datasegpath(RelFileLocator rlocator, ForkNumber forknum, BlockNumber segno)
Definition: filemap.c:592
#define InvalidOid
Definition: postgres_ext.h:36
@ MAIN_FORKNUM
Definition: relpath.h:50
#define InvalidRelFileNumber
Definition: relpath.h:26
#define TABLESPACE_VERSION_DIRECTORY
Definition: relpath.h:33
RelFileNumber relNumber

References datasegpath(), RelFileLocator::dbOid, InvalidOid, InvalidRelFileNumber, MAIN_FORKNUM, pfree(), RelFileLocator::relNumber, RelFileLocator::spcOid, and TABLESPACE_VERSION_DIRECTORY.

Referenced by insert_filehash_entry(), and process_source_file().

◆ lookup_filehash_entry()

static file_entry_t * lookup_filehash_entry ( const char *  path)
static

Definition at line 205 of file filemap.c.

206 {
207  return filehash_lookup(filehash, path);
208 }

References filehash.

Referenced by process_target_wal_block_change().

◆ print_filemap()

void print_filemap ( filemap_t filemap)

Definition at line 479 of file filemap.c.

480 {
481  file_entry_t *entry;
482  int i;
483 
484  for (i = 0; i < filemap->nentries; i++)
485  {
486  entry = filemap->entries[i];
487  if (entry->action != FILE_ACTION_NONE ||
489  {
490  pg_log_debug("%s (%s)", entry->path,
491  action_to_str(entry->action));
492 
493  if (entry->target_pages_to_overwrite.bitmapsize > 0)
495  }
496  }
497  fflush(stdout);
498 }
void datapagemap_print(datapagemap_t *map)
Definition: datapagemap.c:117
static const char * action_to_str(file_action_t action)
Definition: filemap.c:412
static void const char fflush(stdout)

References file_entry_t::action, action_to_str(), datapagemap::bitmapsize, datapagemap_print(), filemap_t::entries, fflush(), FILE_ACTION_NONE, i, filemap_t::nentries, file_entry_t::path, pg_log_debug, generate_unaccent_rules::stdout, and file_entry_t::target_pages_to_overwrite.

Referenced by main().

◆ process_source_file()

void process_source_file ( const char *  path,
file_type_t  type,
size_t  size,
const char *  link_target 
)

Definition at line 218 of file filemap.c.

220 {
221  file_entry_t *entry;
222 
223  /*
224  * Pretend that pg_wal is a directory, even if it's really a symlink. We
225  * don't want to mess with the symlink itself, nor complain if it's a
226  * symlink in source but not in target or vice versa.
227  */
228  if (strcmp(path, "pg_wal") == 0 && type == FILE_TYPE_SYMLINK)
230 
231  /*
232  * sanity check: a filename that looks like a data file better be a
233  * regular file
234  */
235  if (type != FILE_TYPE_REGULAR && isRelDataFile(path))
236  pg_fatal("data file \"%s\" in source is not a regular file", path);
237 
238  /* Remember this source file */
239  entry = insert_filehash_entry(path);
240  if (entry->source_exists)
241  pg_fatal("duplicate source file \"%s\"", path);
242  entry->source_exists = true;
243  entry->source_type = type;
244  entry->source_size = size;
245  entry->source_link_target = link_target ? pg_strdup(link_target) : NULL;
246 }
static file_entry_t * insert_filehash_entry(const char *path)
Definition: filemap.c:175
static pg_noinline void Size size
Definition: slab.c:607
const char * type

References FILE_TYPE_DIRECTORY, FILE_TYPE_REGULAR, FILE_TYPE_SYMLINK, insert_filehash_entry(), isRelDataFile(), pg_fatal, pg_strdup(), size, file_entry_t::source_exists, file_entry_t::source_link_target, file_entry_t::source_size, file_entry_t::source_type, and type.

Referenced by main().

◆ process_target_file()

void process_target_file ( const char *  path,
file_type_t  type,
size_t  size,
const char *  link_target 
)

Definition at line 254 of file filemap.c.

256 {
257  file_entry_t *entry;
258 
259  /*
260  * Do not apply any exclusion filters here. This has advantage to remove
261  * from the target data folder all paths which have been filtered out from
262  * the source data folder when processing the source files.
263  */
264 
265  /*
266  * Like in process_source_file, pretend that pg_wal is always a directory.
267  */
268  if (strcmp(path, "pg_wal") == 0 && type == FILE_TYPE_SYMLINK)
270 
271  /* Remember this target file */
272  entry = insert_filehash_entry(path);
273  if (entry->target_exists)
274  pg_fatal("duplicate source file \"%s\"", path);
275  entry->target_exists = true;
276  entry->target_type = type;
277  entry->target_size = size;
278  entry->target_link_target = link_target ? pg_strdup(link_target) : NULL;
279 }

References FILE_TYPE_DIRECTORY, FILE_TYPE_SYMLINK, insert_filehash_entry(), pg_fatal, pg_strdup(), size, file_entry_t::target_exists, file_entry_t::target_link_target, file_entry_t::target_size, file_entry_t::target_type, and type.

Referenced by main().

◆ process_target_wal_block_change()

void process_target_wal_block_change ( ForkNumber  forknum,
RelFileLocator  rlocator,
BlockNumber  blkno 
)

Definition at line 291 of file filemap.c.

293 {
294  char *path;
295  file_entry_t *entry;
296  BlockNumber blkno_inseg;
297  int segno;
298 
299  segno = blkno / RELSEG_SIZE;
300  blkno_inseg = blkno % RELSEG_SIZE;
301 
302  path = datasegpath(rlocator, forknum, segno);
303  entry = lookup_filehash_entry(path);
304  pfree(path);
305 
306  /*
307  * If the block still exists in both systems, remember it. Otherwise we
308  * can safely ignore it.
309  *
310  * If the block is beyond the EOF in the source system, or the file
311  * doesn't exist in the source at all, we're going to truncate/remove it
312  * away from the target anyway. Likewise, if it doesn't exist in the
313  * target anymore, we will copy it over with the "tail" from the source
314  * system, anyway.
315  *
316  * It is possible to find WAL for a file that doesn't exist on either
317  * system anymore. It means that the relation was dropped later in the
318  * target system, and independently on the source system too, or that it
319  * was created and dropped in the target system and it never existed in
320  * the source. Either way, we can safely ignore it.
321  */
322  if (entry)
323  {
324  Assert(entry->isrelfile);
325 
326  if (entry->target_exists)
327  {
328  if (entry->target_type != FILE_TYPE_REGULAR)
329  pg_fatal("unexpected page modification for non-regular file \"%s\"",
330  entry->path);
331 
332  if (entry->source_exists)
333  {
334  off_t end_offset;
335 
336  end_offset = (blkno_inseg + 1) * BLCKSZ;
337  if (end_offset <= entry->source_size && end_offset <= entry->target_size)
338  datapagemap_add(&entry->target_pages_to_overwrite, blkno_inseg);
339  }
340  }
341  }
342 }
void datapagemap_add(datapagemap_t *map, BlockNumber blkno)
Definition: datapagemap.c:32
static file_entry_t * lookup_filehash_entry(const char *path)
Definition: filemap.c:205

References Assert(), datapagemap_add(), datasegpath(), FILE_TYPE_REGULAR, file_entry_t::isrelfile, lookup_filehash_entry(), file_entry_t::path, pfree(), pg_fatal, file_entry_t::source_exists, file_entry_t::target_exists, file_entry_t::target_pages_to_overwrite, and file_entry_t::target_type.

Referenced by extractPageInfo().

Variable Documentation

◆ excludeDirContents

const char* const excludeDirContents[]
static
Initial value:
=
{
"pg_stat_tmp",
"pg_replslot",
"pg_dynshmem",
"pg_notify",
"pg_serial",
"pg_snapshots",
"pg_subtrans",
NULL
}

Definition at line 88 of file filemap.c.

Referenced by check_file_excluded().

◆ excludeFiles

const struct exclude_list_item excludeFiles[]
static
Initial value:
=
{
{"postgresql.auto.conf.tmp", false},
{"current_logfiles.tmp", false},
{"pg_internal.init", true},
{"backup_label", false},
{"tablespace_map", false},
{"backup_manifest", false},
{"postmaster.pid", false},
{"postmaster.opts", false},
{NULL, false}
}

Definition at line 88 of file filemap.c.

Referenced by check_file_excluded().

◆ filehash

filehash_hash* filehash
static