1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
|
/*-------------------------------------------------------------------------
*
* catalog.c
*
*
* Portions Copyright (c) 1996-2000, PostgreSQL, Inc
* Portions Copyright (c) 1994, Regents of the University of California
*
*
* IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/catalog/catalog.c,v 1.37 2000/11/16 22:30:17 tgl Exp $
*
*-------------------------------------------------------------------------
*/
#include "postgres.h"
#include "access/transam.h"
#include "catalog/catalog.h"
#include "catalog/catname.h"
#include "catalog/pg_type.h"
#include "miscadmin.h"
#include "utils/lsyscache.h"
#ifdef OLD_FILE_NAMING
/*
* relpath - construct path to a relation's file
*
* Note that this only works with relations that are visible to the current
* backend, ie, either in the current database or shared system relations.
*
* Result is a palloc'd string.
*/
char *
relpath(const char *relname)
{
char *path;
if (IsSharedSystemRelationName(relname))
{
/* Shared system relations live in {datadir}/global */
size_t bufsize = strlen(DataDir) + 8 + sizeof(NameData) + 1;
path = (char *) palloc(bufsize);
snprintf(path, bufsize, "%s/global/%s", DataDir, relname);
return path;
}
/*
* If it is in the current database, assume it is in current working
* directory. NB: this does not work during bootstrap!
*/
return pstrdup(relname);
}
/*
* relpath_blind - construct path to a relation's file
*
* Construct the path using only the info available to smgrblindwrt,
* namely the names and OIDs of the database and relation. (Shared system
* relations are identified with dbid = 0.) Note that we may have to
* access a relation belonging to a different database!
*
* Result is a palloc'd string.
*/
char *
relpath_blind(const char *dbname, const char *relname,
Oid dbid, Oid relid)
{
char *path;
if (dbid == (Oid) 0)
{
/* Shared system relations live in {datadir}/global */
path = (char *) palloc(strlen(DataDir) + 8 + sizeof(NameData) + 1);
sprintf(path, "%s/global/%s", DataDir, relname);
}
else if (dbid == MyDatabaseId)
{
/* XXX why is this inconsistent with relpath() ? */
path = (char *) palloc(strlen(DatabasePath) + sizeof(NameData) + 2);
sprintf(path, "%s%c%s", DatabasePath, SEP_CHAR, relname);
}
else
{
/* this is work around only !!! */
char dbpathtmp[MAXPGPATH];
Oid id;
char *dbpath;
GetRawDatabaseInfo(dbname, &id, dbpathtmp);
if (id != dbid)
elog(FATAL, "relpath_blind: oid of db %s is not %u",
dbname, dbid);
dbpath = ExpandDatabasePath(dbpathtmp);
if (dbpath == NULL)
elog(FATAL, "relpath_blind: can't expand path for db %s",
dbname);
path = (char *) palloc(strlen(dbpath) + sizeof(NameData) + 2);
sprintf(path, "%s%c%s", dbpath, SEP_CHAR, relname);
pfree(dbpath);
}
return path;
}
#else /* ! OLD_FILE_NAMING */
/*
* relpath - construct path to a relation's file
*
* Result is a palloc'd string.
*/
char *
relpath(RelFileNode rnode)
{
char *path;
if (rnode.tblNode == (Oid) 0) /* "global tablespace" */
{
/* Shared system relations live in {datadir}/global */
path = (char *) palloc(strlen(DataDir) + 8 + sizeof(NameData) + 1);
sprintf(path, "%s%cglobal%c%u", DataDir, SEP_CHAR, SEP_CHAR, rnode.relNode);
}
else
{
path = (char *) palloc(strlen(DataDir) + 6 + 2 * sizeof(NameData) + 3);
sprintf(path, "%s%cbase%c%u%c%u", DataDir, SEP_CHAR, SEP_CHAR,
rnode.tblNode, SEP_CHAR, rnode.relNode);
}
return path;
}
/*
* GetDatabasePath - construct path to a database dir
*
* Result is a palloc'd string.
*/
char *
GetDatabasePath(Oid tblNode)
{
char *path;
if (tblNode == (Oid) 0) /* "global tablespace" */
{
/* Shared system relations live in {datadir}/global */
path = (char *) palloc(strlen(DataDir) + 8);
sprintf(path, "%s%cglobal", DataDir, SEP_CHAR);
}
else
{
path = (char *) palloc(strlen(DataDir) + 6 + sizeof(NameData) + 1);
sprintf(path, "%s%cbase%c%u", DataDir, SEP_CHAR, SEP_CHAR, tblNode);
}
return path;
}
#endif /* OLD_FILE_NAMING */
/*
* IsSystemRelationName
* True iff name is the name of a system catalog relation.
*
* We now make a new requirement where system catalog relns must begin
* with pg_ while user relns are forbidden to do so. Make the test
* trivial and instantaneous.
*
* XXX this is way bogus. -- pma
*/
bool
IsSystemRelationName(const char *relname)
{
if (relname[0] && relname[1] && relname[2])
return (relname[0] == 'p' &&
relname[1] == 'g' &&
relname[2] == '_');
else
return FALSE;
}
/*
* IsSharedSystemRelationName
* True iff name is the name of a shared system catalog relation.
*/
bool
IsSharedSystemRelationName(const char *relname)
{
int i;
/*
* Quick out: if it's not a system relation, it can't be a shared
* system relation.
*/
if (!IsSystemRelationName(relname))
return FALSE;
i = 0;
while (SharedSystemRelationNames[i] != NULL)
{
if (strcmp(SharedSystemRelationNames[i], relname) == 0)
return TRUE;
i++;
}
return FALSE;
}
/*
* newoid - returns a unique identifier across all catalogs.
*
* Object Id allocation is now done by GetNewObjectID in
* access/transam/varsup.c. oids are now allocated correctly.
*
* old comments:
* This needs to change soon, it fails if there are too many more
* than one call per second when postgres restarts after it dies.
*
* The distribution of OID's should be done by the POSTMASTER.
* Also there needs to be a facility to preallocate OID's. Ie.,
* for a block of OID's to be declared as invalid ones to allow
* user programs to use them for temporary object identifiers.
*/
Oid
newoid()
{
Oid lastoid;
GetNewObjectId(&lastoid);
if (!OidIsValid(lastoid))
elog(ERROR, "newoid: GetNewObjectId returns invalid oid");
return lastoid;
}
/*
* fillatt - fills the ATTRIBUTE relation fields from the TYP
*
* Expects that the atttypid domain is set for each att[].
* Returns with the attnum, and attlen domains set.
* attnum, attproc, atttyparg, ... should be set by the user.
*
* In the future, attnum may not be set?!? or may be passed as an arg?!?
*
* Current implementation is very inefficient--should cashe the
* information if this is at all possible.
*
* Check to see if this is really needed, and especially in the case
* of index tuples.
*/
void
fillatt(TupleDesc tupleDesc)
{
int natts = tupleDesc->natts;
Form_pg_attribute *att = tupleDesc->attrs;
Form_pg_attribute *attributeP;
int i;
if (natts < 0 || natts > MaxHeapAttributeNumber)
elog(ERROR, "fillatt: %d attributes is too large", natts);
if (natts == 0)
{
elog(DEBUG, "fillatt: called with natts == 0");
return;
}
attributeP = &att[0];
for (i = 1; i <= natts; i++)
{
(*attributeP)->attnum = (int16) i;
/*
* Check if the attr is a set before messing with the length
* and byval, since those were already set in
* TupleDescInitEntry. In fact, this seems redundant here,
* but who knows what I'll break if I take it out...
*/
if (!(*attributeP)->attisset)
{
get_typlenbyval((*attributeP)->atttypid,
& (*attributeP)->attlen,
& (*attributeP)->attbyval);
}
attributeP++;
}
}
|