forked from snaga/xlogdump
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxlogdump_oid2name.c
408 lines (335 loc) · 7.97 KB
/
xlogdump_oid2name.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
/*
* xlogdump_oid2name.c
*
* a collection of functions to get database object names from the oids
* by looking up the system catalog.
*/
#include "xlogdump_oid2name.h"
#include "pqexpbuffer.h"
#include "postgres.h"
static PGconn *conn = NULL; /* Connection for translating oids of global objects */
static PGconn *lastDbConn = NULL; /* Connection for translating oids of per database objects */
static PGresult *_res = NULL; /* a result set variable for relname2attr_*() functions */
static char *pghost = NULL;
static char *pgport = NULL;
static char *pguser = NULL;
static char *pgpass = NULL;
/*
* Structure for the linked-list to hold oid-name lookup cache.
*/
struct oid2name_t {
Oid oid;
char *name;
struct oid2name_t *next;
};
/*
* Head element of the linked-list.
*/
struct oid2name_t *oid2name_table = NULL;
static char *cache_get(Oid);
static struct oid2name_t *cache_put(Oid, char *);
/*
* cache_get()
*
* looks up the oid-name mapping cache table. If not found, returns NULL.
*/
static char *
cache_get(Oid oid)
{
struct oid2name_t *curr = oid2name_table;
while (curr!=NULL)
{
// printf("DEBUG: find %d %s\n", curr->oid, curr->name);
if (curr->oid == oid)
return curr->name;
curr = curr->next;
}
return NULL;
}
/*
* cache_put()
*
* puts a new entry to the tail of the oid-name mapping cache table.
*/
static struct oid2name_t *
cache_put(Oid oid, char *name)
{
struct oid2name_t *curr = oid2name_table;
if (!curr)
{
curr = (struct oid2name_t *)malloc( sizeof(struct oid2name_t) );
memset(curr, 0, sizeof(struct oid2name_t));
curr->oid = oid;
curr->name = strdup(name);
oid2name_table = curr;
// printf("DEBUG: put %d %s\n", curr->oid, curr->name);
return curr;
}
while (curr->next!=NULL)
{
curr = curr->next;
}
curr->next = (struct oid2name_t *)malloc( sizeof(struct oid2name_t) );
memset(curr->next, 0, sizeof(struct oid2name_t));
curr->next->oid = oid;
curr->next->name = strdup(name);
// printf("DEBUG: put %d %s\n", curr->next->oid, curr->next->name);
return curr->next;
}
bool
oid2name_from_file(const char *file)
{
FILE *fp;
Oid oid;
char name[NAMEDATALEN];
if ( (fp = fopen(file, "r"))==NULL )
{
fprintf(stderr, "ERROR: %s not found.\n", file);
return false;
}
printf("NOTICE: Using '%s' as an oid2name cache file.\n", file);
while ( fscanf(fp, "%d %s", &oid, name)>=2 )
{
// printf("oid=%d, name=%s\n", oid, name);
cache_put(oid, name);
}
fclose(fp);
return true;
}
bool
oid2name_to_file(const char *file)
{
const char *oid2name_stmt[] = {
"SELECT oid,spcname FROM pg_tablespace ORDER BY oid",
"SELECT oid,datname FROM pg_database ORDER BY oid",
"SELECT oid,relname FROM pg_class ORDER BY oid"
};
PGresult *res = NULL;
int i, j;
FILE *fp;
if ( (fp = fopen(file, "w"))==NULL )
{
fprintf(stderr, "ERROR: Can't write %s.\n", file);
return false;
}
for (i=0 ; i<3 ; i++)
{
res = PQexec(conn, oid2name_stmt[i]);
if (PQresultStatus(res) != PGRES_TUPLES_OK)
{
fprintf(stderr, "SELECT FAILED: %s", PQerrorMessage(conn));
PQclear(res);
return false;
}
for (j=0 ; j<PQntuples(res) ; j++)
fprintf(fp, "%s %s\n", PQgetvalue(res, j, 0), PQgetvalue(res, j, 1));
PQclear(res);
}
fclose(fp);
return true;
}
/*
* Open a database connection
*/
bool
DBConnect(const char *host, const char *port, char *database, const char *user)
{
pghost = strdup(host);
pgport = strdup(port);
pguser = strdup(user);
pgpass = NULL;
retry_login:
conn = PQsetdbLogin(pghost,
pgport,
NULL,
NULL,
database,
pguser,
pgpass);
if (PQstatus(conn) == CONNECTION_BAD)
{
/* wait a password if required to login, then retry. */
if (strcmp(PQerrorMessage(conn), PQnoPasswordSupplied) == 0 &&
!feof(stdin))
{
PQfinish(conn);
conn = NULL;
pgpass = simple_prompt("Password: ", 100, false);
goto retry_login;
}
fprintf(stderr, "Connection to database failed: %s",
PQerrorMessage(conn));
PQfinish(conn);
conn = NULL;
return false;
}
return true;
}
/*
* Atempt to read the name of tablespace into lastSpcName
* (if there's a database connection and the oid changed since lastSpcOid)
*/
char *
getSpaceName(uint32 space, char *buf, size_t buflen)
{
char dbQry[1024];
PGresult *res = NULL;
if (cache_get(space))
{
snprintf(buf, buflen, "%s", cache_get(space));
return buf;
}
if (conn)
{
// printf("DEBUG: getSpaceName: SELECT spcname FROM pg_tablespace WHERE oid = %i\n", space);
snprintf(dbQry, sizeof(dbQry), "SELECT spcname FROM pg_tablespace WHERE oid = %i", space);
res = PQexec(conn, dbQry);
if (PQresultStatus(res) != PGRES_TUPLES_OK)
{
fprintf(stderr, "SELECT FAILED: %s", PQerrorMessage(conn));
PQclear(res);
return NULL;
}
if(PQntuples(res) > 0)
{
strncpy(buf, PQgetvalue(res, 0, 0), buflen);
// printf("DEBUG: getSpaceName: spcname = %s\n", buf);
cache_put(space, buf);
PQclear(res);
return buf;
}
}
PQclear(res);
/* Didn't find the name, return string with oid */
snprintf(buf, buflen, "%u", space);
return buf;
}
/*
* Atempt to get the name of database (if there's a database connection)
*/
char *
getDbName(uint32 db, char *buf, size_t buflen)
{
char dbQry[1024];
PGresult *res = NULL;
if (cache_get(db))
{
snprintf(buf, buflen, "%s", cache_get(db));
return buf;
}
if (conn)
{
snprintf(dbQry, sizeof(dbQry), "SELECT datname FROM pg_database WHERE oid = %i", db);
res = PQexec(conn, dbQry);
if (PQresultStatus(res) != PGRES_TUPLES_OK)
{
fprintf(stderr, "SELECT FAILED: %s", PQerrorMessage(conn));
PQclear(res);
return NULL;
}
if(PQntuples(res) > 0)
{
strncpy(buf, PQgetvalue(res, 0, 0), buflen);
cache_put(db, buf);
// Database changed makes new connection
if (lastDbConn)
PQfinish(lastDbConn);
lastDbConn = PQsetdbLogin(pghost,
pgport,
NULL,
NULL,
buf,
pguser,
pgpass);
PQclear(res);
return buf;
}
}
PQclear(res);
/* Didn't find the name, return string with oid */
snprintf(buf, buflen, "%u", db);
return buf;
}
/*
* Atempt to get the name of relation and copy to relName
* (if there's a database connection and the reloid changed)
* Copy a string with oid if not found
*/
char *
getRelName(uint32 relid, char *buf, size_t buflen)
{
char dbQry[1024];
PGresult *res = NULL;
if (cache_get(relid))
{
snprintf(buf, buflen, "%s", cache_get(relid));
return buf;
}
if (conn && lastDbConn)
{
/* Try the relfilenode and oid just in case the filenode has changed
If it has changed more than once we can't translate it's name */
snprintf(dbQry, sizeof(dbQry), "SELECT relname, oid FROM pg_class WHERE relfilenode = %i OR oid = %i", relid, relid);
res = PQexec(lastDbConn, dbQry);
if (PQresultStatus(res) != PGRES_TUPLES_OK)
{
fprintf(stderr, "SELECT FAILED: %s", PQerrorMessage(conn));
PQclear(res);
return NULL;
}
if(PQntuples(res) > 0)
{
strncpy(buf, PQgetvalue(res, 0, 0), buflen);
cache_put(relid, buf);
PQclear(res);
return buf;
}
}
PQclear(res);
/* Didn't find the name, return string with oid */
snprintf(buf, buflen, "%u", relid);
return buf;
}
int
relname2attr_begin(const char *relname)
{
char dbQry[1024];
if ( _res!=NULL )
PQclear(_res);
snprintf(dbQry, sizeof(dbQry), "SELECT attname, atttypid FROM pg_attribute a, pg_class c WHERE attnum > 0 AND attrelid = c.oid AND c.relname='%s' ORDER BY attnum", relname);
_res = PQexec(lastDbConn, dbQry);
if (PQresultStatus(_res) != PGRES_TUPLES_OK)
{
fprintf(stderr, "SELECT FAILED: %s", PQerrorMessage(conn));
PQclear(_res);
_res = NULL;
return -1;
}
return PQntuples(_res);
}
int
relname2attr_fetch(int i, char *attname, Oid *atttypid)
{
snprintf(attname, NAMEDATALEN, "%s", PQgetvalue(_res, i, 0));
*atttypid = atoi( PQgetvalue(_res, i, 1) );
return i;
}
void
relname2attr_end()
{
PQclear(_res);
_res = NULL;
}
bool
oid2name_enabled(void)
{
return (conn!=NULL);
}
void
DBDisconnect(void)
{
if(lastDbConn)
PQfinish(lastDbConn);
if(conn)
PQfinish(conn);
}