-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathpg_filenodemapdata.c
428 lines (389 loc) · 12.6 KB
/
pg_filenodemapdata.c
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
/*
* pg_filenodemapdata.c - PostgreSQL utility
* that prints the contents of pg_filenode.map files.
*
* Copyright (c) 2018-2021, Crunchy Data Solutions, Inc.
* Copyright (c) 2018, VMware, Inc.
* Copyright (c) 2018-2021, PostgreSQL Global Development Group
*
* This is a standalone utlity for displaying the mappings within either a
* global or per-database pg_filenode.map. It is heavily based on the native
* PostgreSQL logic for reading pg_filenode.map files, which can be found
* within cache/relfilenodemap.c
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* pg_filenodemapdata author: Peter Geoghegan <pg@bowt.ie>
*/
#include "postgres.h"
#include "catalog/indexing.h"
#include "catalog/pg_auth_members.h"
#include "catalog/pg_authid.h"
#include "catalog/pg_class.h"
#include "catalog/pg_database.h"
#include "catalog/pg_db_role_setting.h"
#if PG_VERSION_NUM < 130000
#include "catalog/pg_pltemplate.h"
#endif
#include "catalog/pg_proc.h"
#include "catalog/pg_replication_origin.h"
#include "catalog/pg_shdepend.h"
#include "catalog/pg_shdescription.h"
#include "catalog/pg_shseclabel.h"
#include "catalog/pg_subscription.h"
#include "catalog/pg_tablespace.h"
#include "catalog/pg_type.h"
#include "catalog/toasting.h"
#include "port/pg_crc32c.h"
#define HEXEDIT_VERSION "0.1"
/* Provide system catalog OIDs for old unsupported versions */
#define PgAuthidToastTable 4175
#define PgAuthidToastIndex 4176
#define PgDatabaseToastTable 4177
#define PgDatabaseToastIndex 4178
#define PgPlTemplateToastTable 4179
#define PgPlTemplateToastIndex 4180
#define PgReplicationOriginToastTable 4181
#define PgReplicationOriginToastIndex 4182
#define PgSubscriptionToastTable 4183
#define PgSubscriptionToastIndex 4184
#define PgTablespaceToastTable 4185
#define PgTablespaceToastIndex 4186
/*
* Postgres 13 commit 50fc694e removed pl_template.h
*/
#if PG_VERSION_NUM >= 130000
#define PLTemplateRelationId 1136
#define PgPlTemplateToastTable 4179
#define PgPlTemplateToastIndex 4180
#define PLTemplateNameIndexId 1137
#endif
/*
* catalog/toasting.h doesn't bother to provide constants for these two pg_proc
* TOAST relations, presumably because no core code needs to reference the
* toast tables. We invent our own constants, to be consistent.
*
* XXX: This list isn't comprehensive.
*/
#define OID_PG_TOAST_1255 2836
#define OID_PG_TOAST_1255_INDEX 2837
/* For calculating display padding */
#define CATALOG_NAME_COLS 45
#define ENTRY_NUM_COLS 2
/* These constants appear at the top of PostgreSQL's relmapper.c */
#define RELMAPPER_FILEMAGIC 0x592717 /* version ID value */
#define MAX_MAPPINGS 62 /* 62 * 8 + 16 = 512 */
/* Struct definitions from the top of PostgreSQL's relmapper.c */
typedef struct RelMapping
{
Oid mapoid; /* OID of a catalog */
Oid mapfilenode; /* its filenode number */
} RelMapping;
typedef struct RelMapFile
{
int32 magic; /* always RELMAPPER_FILEMAGIC */
int32 num_mappings; /* number of valid RelMapping entries */
RelMapping mappings[MAX_MAPPINGS];
pg_crc32c crc; /* CRC of all above */
int32 pad; /* to make the struct size be 512 exactly */
} RelMapFile;
/* Possible return codes from option validation routine */
typedef enum optionReturnCodes
{
OPT_RC_VALID, /* All options are valid */
OPT_RC_INVALID, /* Improper option string */
OPT_RC_COPYRIGHT /* Copyright should be displayed */
} optionReturnCodes;
/* Program exit code */
static int exitCode = 0;
static void DisplayOptions(unsigned int validOptions);
static unsigned int ConsumeOptions(int numOptions, char **options);
static const char *GetCatalogNameFromOid(Oid classOid);
static void PrintRelMapContents(RelMapFile *map);
static void VerifyRelMapContents(RelMapFile *map);
static bool InitRelMapFromFile(char *mapFileName, RelMapFile *map);
/*
* Send properly formed usage information to the user
*/
static void
DisplayOptions(unsigned int validOptions)
{
if (validOptions == OPT_RC_COPYRIGHT)
printf
("pg_filenodemapdata %s (for PostgreSQL %s)\n"
"Copyright (c) 2018-2021, Crunchy Data Solutions, Inc.\n"
"Copyright (c) 2018, VMware, Inc.\n"
"Copyright (c) 2018-2021, PostgreSQL Global Development Group\n",
HEXEDIT_VERSION, PG_VERSION);
printf
("\nUsage: pg_filenodemapdata file\n\n"
"Displays details from a PostgreSQL pg_filenode.map file\n"
"\nReport bugs to <pg@bowt.ie>\n");
}
/*
* Iterate through the provided options and set the option flags. An error
* will result in a positive rc and will force a display of the usage
* information. This routine returns enum option ReturnCode values.
*/
static unsigned int
ConsumeOptions(int numOptions, char **options)
{
unsigned int rc = OPT_RC_VALID;
/* For now, only accept a single file argument, without flags */
if (numOptions != 2)
rc = OPT_RC_INVALID;
return rc;
}
/*
* Get the name of a known system catalog from its pg_class OID
*/
static const char *
GetCatalogNameFromOid(Oid classOid)
{
switch (classOid)
{
/* Local/nailed mappings */
case RelationRelationId:
return "pg_class";
case AttributeRelationId:
return "pg_attribute";
case ProcedureRelationId:
return "pg_proc";
case TypeRelationId:
return "pg_type";
case OID_PG_TOAST_1255:
return "pg_toast_1255";
case OID_PG_TOAST_1255_INDEX:
return "pg_toast_1255_index";
case AttributeRelidNameIndexId:
return "pg_attribute_relid_attnam_index";
case AttributeRelidNumIndexId:
return "pg_attribute_relid_attnum_index";
case ClassOidIndexId:
return "pg_class_oid_index";
case ClassNameNspIndexId:
return "pg_class_relname_nsp_index";
case ClassTblspcRelfilenodeIndexId:
return "pg_class_tblspc_relfilenode_index";
case ProcedureOidIndexId:
return "pg_proc_oid_index";
case ProcedureNameArgsNspIndexId:
return "pg_proc_proname_args_nsp_index";
case TypeOidIndexId:
return "pg_type_oid_index";
case TypeNameNspIndexId:
return "pg_type_typname_nsp_index";
/* Global/shared mappings */
case DatabaseRelationId:
return "pg_database";
case DbRoleSettingRelationId:
return "pg_db_role_setting";
case TableSpaceRelationId:
return "pg_tablespace";
case PLTemplateRelationId:
return "pg_pltemplate";
case AuthIdRelationId:
return "pg_authid";
case AuthMemRelationId:
return "pg_auth_members";
case SharedDependRelationId:
return "pg_shdepend";
case ReplicationOriginRelationId:
return "pg_replication_origin";
case SharedDescriptionRelationId:
return "pg_shdescription";
case SharedSecLabelRelationId:
return "pg_shseclabel";
case SubscriptionRelationId:
return "pg_subscription";
case PgAuthidToastTable:
return "pg_toast_4175";
case PgAuthidToastIndex:
return "pg_toast_4176_index";
case PgDatabaseToastTable:
return "pg_toast_4177";
case PgDatabaseToastIndex:
return "pg_toast_4178_index";
case PgDbRoleSettingToastTable:
return "pg_toast_2964";
case PgDbRoleSettingToastIndex:
return "pg_toast_2964_index";
case PgPlTemplateToastTable:
return "pg_toast_4179";
case PgPlTemplateToastIndex:
return "pg_toast_4180_index";
case PgReplicationOriginToastTable:
return "pg_toast_4181";
case PgReplicationOriginToastIndex:
return "pg_toast_4182_index";
case PgShdescriptionToastTable:
return "pg_toast_2396";
case PgShdescriptionToastIndex:
return "pg_toast_2396_index";
case PgShseclabelToastTable:
return "pg_toast_3592";
case PgShseclabelToastIndex:
return "pg_toast_3592_index";
case PgSubscriptionToastTable:
return "pg_toast_4183";
case PgSubscriptionToastIndex:
return "pg_toast_4184_index";
case PgTablespaceToastTable:
return "pg_toast_4185";
case PgTablespaceToastIndex:
return "pg_toast_4186_index";
case AuthIdRolnameIndexId:
return "pg_authid_rolname_index";
case AuthIdOidIndexId:
return "pg_authid_oid_index";
case AuthMemRoleMemIndexId:
return "pg_auth_members_role_member_index";
case AuthMemMemRoleIndexId:
return "pg_auth_members_member_role_index";
case DatabaseNameIndexId:
return "pg_database_datname_index";
case DatabaseOidIndexId:
return "pg_database_oid_index";
case SharedDescriptionObjIndexId:
return "pg_shdescription_o_c_index";
case PLTemplateNameIndexId:
return "pg_pltemplate_name_index";
case SharedDependDependerIndexId:
return "pg_shdepend_depender_index";
case SharedDependReferenceIndexId:
return "pg_shdepend_reference_index";
case TablespaceOidIndexId:
return "pg_tablespace_oid_index";
case TablespaceNameIndexId:
return "pg_tablespace_spcname_index";
case DbRoleSettingDatidRolidIndexId:
return "pg_db_role_setting_databaseid_rol_index";
case SharedSecLabelObjectIndexId:
return "pg_shseclabel_object_index";
case ReplicationOriginIdentIndex:
return "pg_replication_origin_roiident_index";
case ReplicationOriginNameIndex:
return "pg_replication_origin_roname_index";
case SubscriptionObjectIndexId:
return "pg_subscription_oid_index";
case SubscriptionNameIndexId:
return "pg_subscription_subname_index";
/*
* We expect to be able to identify every mapped catalog on
* supported versions. If we haven't got a record of this
* catalog's OID, our assumption is that that's because it's from
* a future PostgreSQL version.
*/
default:
return "unlisted system catalog relation";
}
}
static void
PrintRelMapContents(RelMapFile *map)
{
int32 num_mappings = Min(MAX_MAPPINGS, map->num_mappings);
int i;
/* Print pg_filenode.map file's header */
printf("magic: 0x%.8X\n"
"num_mappings: %d\n\n", map->magic, map->num_mappings);
/* Print mappings from file */
for (i = 0; i < num_mappings; i++)
{
Oid reloid = map->mappings[i].mapoid;
Oid relfilenode = map->mappings[i].mapfilenode;
const char *catalogname;
catalogname = GetCatalogNameFromOid(reloid);
printf("%*d) %u - %s: %*u\n", ENTRY_NUM_COLS, i, reloid, catalogname,
CATALOG_NAME_COLS - (int) strlen(catalogname), relfilenode);
}
/* Print CRC-32C checksum at the end of the file */
printf("\nfile checksum: 0x%.8X\n", map->crc);
}
/*
* Verify the consistency of a pg_relfilenode.map file.
*
* Checks that magic number matches, and verifies CRC.
*/
static void
VerifyRelMapContents(RelMapFile *map)
{
pg_crc32c crc;
if (map->magic != RELMAPPER_FILEMAGIC ||
map->num_mappings < 0 ||
map->num_mappings > MAX_MAPPINGS)
{
fprintf(stderr, "relation mapping file contains invalid data\n");
exitCode = 1;
}
/* Print our own CRC-32/CRC-32C calculation */
INIT_CRC32C(crc);
COMP_CRC32C(crc, (char *) map, offsetof(RelMapFile, crc));
FIN_CRC32C(crc);
/* Raise error if they don't match */
if (!EQ_CRC32C(crc, map->crc))
{
fprintf(stderr, "calculated checksum 0x%.8X does not match file checksum\n",
crc);
exitCode = 1;
}
}
/*
* Given a path to a pg_relfilenode.map file, initialized caller's RelMapFile
* structure. Returns true on success.
*/
static bool
InitRelMapFromFile(char *mapFileName, RelMapFile *map)
{
FILE *file = fopen(mapFileName, "rb");
if (!file)
{
fprintf(stderr, "could not open file \"%s\": %s\n", mapFileName,
strerror(errno));
exitCode = 1;
return false;
}
if (fread(map, 1, sizeof(RelMapFile), file) != sizeof(RelMapFile))
{
fprintf(stderr, "could not read relation mapping file \"%s\": %s\n",
mapFileName, strerror(errno));
exitCode = 1;
fclose(file);
return false;
}
fclose(file);
return true;
}
int
main(int argv, char **argc)
{
unsigned int validOptions;
RelMapFile map;
/* If there is a parameter list, validate the options */
validOptions = (argv < 2) ? OPT_RC_COPYRIGHT : ConsumeOptions(argv, argc);
/*
* Display valid options if no parameters are received or invalid options
* where encountered
*/
if (validOptions != OPT_RC_VALID)
DisplayOptions(validOptions);
else if (InitRelMapFromFile(argc[1], &map))
{
/* Print file header, contents, and footer */
PrintRelMapContents(&map);
/* Verify consistency of file last, so that errors appear last */
VerifyRelMapContents(&map);
}
exit(exitCode);
}