PostgreSQL Source Code  git master
relpath.c File Reference
#include "postgres.h"
#include "catalog/pg_tablespace_d.h"
#include "common/relpath.h"
#include "storage/backendid.h"
Include dependency graph for relpath.c:

Go to the source code of this file.

Functions

 StaticAssertDecl (lengthof(forkNames)==(MAX_FORKNUM+1), "array length mismatch")
 
ForkNumber forkname_to_number (const char *forkName)
 
int forkname_chars (const char *str, ForkNumber *fork)
 
char * GetDatabasePath (Oid dbNode, Oid spcNode)
 
char * GetRelationPath (Oid dbNode, Oid spcNode, Oid relNode, int backendId, ForkNumber forkNumber)
 

Variables

const char *const forkNames []
 

Function Documentation

◆ forkname_chars()

int forkname_chars ( const char *  str,
ForkNumber fork 
)

Definition at line 81 of file relpath.c.

References forkNames, InvalidForkNumber, and MAX_FORKNUM.

Referenced by looks_like_temp_rel_name(), and parse_filename_for_nontemp_relation().

82 {
83  ForkNumber forkNum;
84 
85  for (forkNum = 1; forkNum <= MAX_FORKNUM; forkNum++)
86  {
87  int len = strlen(forkNames[forkNum]);
88 
89  if (strncmp(forkNames[forkNum], str, len) == 0)
90  {
91  if (fork)
92  *fork = forkNum;
93  return len;
94  }
95  }
96  if (fork)
97  *fork = InvalidForkNumber;
98  return 0;
99 }
ForkNumber
Definition: relpath.h:40
#define MAX_FORKNUM
Definition: relpath.h:55
const char *const forkNames[]
Definition: relpath.c:33

◆ forkname_to_number()

ForkNumber forkname_to_number ( const char *  forkName)

Definition at line 50 of file relpath.c.

References ereport, errcode(), errhint(), errmsg(), ERROR, forkNames, InvalidForkNumber, and MAX_FORKNUM.

Referenced by get_raw_page_fork(), pg_prewarm(), and pg_relation_size().

51 {
52  ForkNumber forkNum;
53 
54  for (forkNum = 0; forkNum <= MAX_FORKNUM; forkNum++)
55  if (strcmp(forkNames[forkNum], forkName) == 0)
56  return forkNum;
57 
58 #ifndef FRONTEND
59  ereport(ERROR,
60  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
61  errmsg("invalid fork name"),
62  errhint("Valid fork names are \"main\", \"fsm\", "
63  "\"vm\", and \"init\".")));
64 #endif
65 
66  return InvalidForkNumber;
67 }
int errhint(const char *fmt,...)
Definition: elog.c:1069
int errcode(int sqlerrcode)
Definition: elog.c:608
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:141
ForkNumber
Definition: relpath.h:40
#define MAX_FORKNUM
Definition: relpath.h:55
int errmsg(const char *fmt,...)
Definition: elog.c:822
const char *const forkNames[]
Definition: relpath.c:33

◆ GetDatabasePath()

char* GetDatabasePath ( Oid  dbNode,
Oid  spcNode 
)

Definition at line 110 of file relpath.c.

References Assert, psprintf(), pstrdup(), and TABLESPACE_VERSION_DIRECTORY.

Referenced by check_db_file_conflict(), createdb(), dbase_redo(), InitPostgres(), movedb(), movedb_failure_callback(), ProcessCommittedInvalidationMessages(), relmap_redo(), remove_dbtablespaces(), and TablespaceCreateDbspace().

111 {
112  if (spcNode == GLOBALTABLESPACE_OID)
113  {
114  /* Shared system relations live in {datadir}/global */
115  Assert(dbNode == 0);
116  return pstrdup("global");
117  }
118  else if (spcNode == DEFAULTTABLESPACE_OID)
119  {
120  /* The default tablespace is {datadir}/base */
121  return psprintf("base/%u", dbNode);
122  }
123  else
124  {
125  /* All other tablespaces are accessed via symlinks */
126  return psprintf("pg_tblspc/%u/%s/%u",
127  spcNode, TABLESPACE_VERSION_DIRECTORY, dbNode);
128  }
129 }
char * pstrdup(const char *in)
Definition: mcxt.c:1186
char * psprintf(const char *fmt,...)
Definition: psprintf.c:46
#define TABLESPACE_VERSION_DIRECTORY
Definition: relpath.h:26
#define Assert(condition)
Definition: c.h:738

◆ GetRelationPath()

char* GetRelationPath ( Oid  dbNode,
Oid  spcNode,
Oid  relNode,
int  backendId,
ForkNumber  forkNumber 
)

Definition at line 141 of file relpath.c.

References Assert, forkNames, InvalidBackendId, MAIN_FORKNUM, psprintf(), and TABLESPACE_VERSION_DIRECTORY.

143 {
144  char *path;
145 
146  if (spcNode == GLOBALTABLESPACE_OID)
147  {
148  /* Shared system relations live in {datadir}/global */
149  Assert(dbNode == 0);
150  Assert(backendId == InvalidBackendId);
151  if (forkNumber != MAIN_FORKNUM)
152  path = psprintf("global/%u_%s",
153  relNode, forkNames[forkNumber]);
154  else
155  path = psprintf("global/%u", relNode);
156  }
157  else if (spcNode == DEFAULTTABLESPACE_OID)
158  {
159  /* The default tablespace is {datadir}/base */
160  if (backendId == InvalidBackendId)
161  {
162  if (forkNumber != MAIN_FORKNUM)
163  path = psprintf("base/%u/%u_%s",
164  dbNode, relNode,
165  forkNames[forkNumber]);
166  else
167  path = psprintf("base/%u/%u",
168  dbNode, relNode);
169  }
170  else
171  {
172  if (forkNumber != MAIN_FORKNUM)
173  path = psprintf("base/%u/t%d_%u_%s",
174  dbNode, backendId, relNode,
175  forkNames[forkNumber]);
176  else
177  path = psprintf("base/%u/t%d_%u",
178  dbNode, backendId, relNode);
179  }
180  }
181  else
182  {
183  /* All other tablespaces are accessed via symlinks */
184  if (backendId == InvalidBackendId)
185  {
186  if (forkNumber != MAIN_FORKNUM)
187  path = psprintf("pg_tblspc/%u/%s/%u/%u_%s",
189  dbNode, relNode,
190  forkNames[forkNumber]);
191  else
192  path = psprintf("pg_tblspc/%u/%s/%u/%u",
194  dbNode, relNode);
195  }
196  else
197  {
198  if (forkNumber != MAIN_FORKNUM)
199  path = psprintf("pg_tblspc/%u/%s/%u/t%d_%u_%s",
201  dbNode, backendId, relNode,
202  forkNames[forkNumber]);
203  else
204  path = psprintf("pg_tblspc/%u/%s/%u/t%d_%u",
206  dbNode, backendId, relNode);
207  }
208  }
209  return path;
210 }
char * psprintf(const char *fmt,...)
Definition: psprintf.c:46
#define TABLESPACE_VERSION_DIRECTORY
Definition: relpath.h:26
#define InvalidBackendId
Definition: backendid.h:23
#define Assert(condition)
Definition: c.h:738
const char *const forkNames[]
Definition: relpath.c:33

◆ StaticAssertDecl()

StaticAssertDecl ( lengthof(forkNames = =(MAX_FORKNUM+1),
"array length mismatch"   
)

Variable Documentation

◆ forkNames

const char* const forkNames[]
Initial value:
= {
"main",
"fsm",
"vm",
"init"
}

Definition at line 33 of file relpath.c.

Referenced by forkname_chars(), forkname_to_number(), GetRelationPath(), ResetUnloggedRelationsInDbspaceDir(), and XLogDumpDisplayRecord().