forked from vmware-archive/pg_rewind
-
Notifications
You must be signed in to change notification settings - Fork 0
/
libpq_fetch.c
408 lines (342 loc) · 9.8 KB
/
libpq_fetch.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
/*-------------------------------------------------------------------------
*
* libpq_fetch.c
* Functions for fetching files from a remote server.
*
* Portions Copyright (c) 1996-2015, PostgreSQL Global Development Group
* Portions Copyright (c) 2013-2015 VMware, Inc. All Rights Reserved.
*
*-------------------------------------------------------------------------
*/
#include "postgres_fe.h"
#include "catalog/catalog.h"
#include "catalog/pg_type.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#include <fcntl.h>
#include <unistd.h>
#include <libpq-fe.h>
#include "pg_rewind.h"
#include "fetch.h"
#include "filemap.h"
#include "datapagemap.h"
static PGconn *conn = NULL;
#define CHUNKSIZE 1000000
static void receiveFileChunks(const char *sql);
static void execute_pagemap(datapagemap_t *pagemap, const char *path);
void
libpqConnect(const char *connstr)
{
conn = PQconnectdb(connstr);
if (PQstatus(conn) == CONNECTION_BAD)
{
fprintf(stderr, "could not connect to remote server: %s\n",
PQerrorMessage(conn));
exit(1);
}
if (verbose)
printf("connected to remote server\n");
}
/*
* Get a file list.
*/
void
libpqProcessFileList(void)
{
PGresult *res;
const char *sql;
int i;
sql =
"-- Create a recursive directory listing of the whole data directory\n"
"with recursive files (path, filename, size, isdir) as (\n"
" select '' as path, filename, size, isdir from\n"
" (select pg_ls_dir('.') as filename) as fn,\n"
" pg_stat_file(fn.filename) as this\n"
" union all\n"
" select parent.path || parent.filename || '/' as path,\n"
" fn, this.size, this.isdir\n"
" from files as parent,\n"
" pg_ls_dir(parent.path || parent.filename) as fn,\n"
" pg_stat_file(parent.path || parent.filename || '/' || fn) as this\n"
" where parent.isdir = 't'\n"
")\n"
"-- Using the cte, fetch a listing of the all the files.\n"
"--\n"
"-- For tablespaces, use pg_tablespace_location() function to fetch\n"
"-- the link target (there is no backend function to get a symbolic\n"
"-- link's target in general, so if the admin has put any custom\n"
"-- symbolic links in the data directory, they won't be copied\n"
"-- correctly)\n"
"select path || filename, size, isdir,\n"
" pg_tablespace_location(pg_tablespace.oid) as link_target\n"
"from files\n"
"left outer join pg_tablespace on files.path = 'pg_tblspc/'\n"
" and oid::text = files.filename\n";
res = PQexec(conn, sql);
if (PQresultStatus(res) != PGRES_TUPLES_OK)
{
fprintf(stderr, "unexpected result while fetching file list: %s\n",
PQresultErrorMessage(res));
exit(1);
}
/* sanity check the result set */
if (!(PQnfields(res) == 4))
{
fprintf(stderr, "unexpected result set while fetching file list\n");
exit(1);
}
/* Read result to local variables */
for (i = 0; i < PQntuples(res); i++)
{
char *path = PQgetvalue(res, i, 0);
int filesize = atoi(PQgetvalue(res, i, 1));
bool isdir = (strcmp(PQgetvalue(res, i, 2), "t") == 0);
char *link_target = PQgetvalue(res, i, 3);
file_type_t type;
if (link_target[0])
type = FILE_TYPE_SYMLINK;
else if (isdir)
type = FILE_TYPE_DIRECTORY;
else
type = FILE_TYPE_REGULAR;
process_remote_file(path, type, filesize, link_target);
}
}
/*
* Runs a query, which returns pieces of files from the remote source data
* directory, and overwrites the corresponding parts of target files with
* the received parts. The result set is expected to be of format:
*
* path text -- path in the data directory, e.g "base/1/123"
* begin int4 -- offset within the file
* chunk bytea -- file content
*
*/
static void
receiveFileChunks(const char *sql)
{
PGresult *res;
if (PQsendQueryParams(conn, sql, 0, NULL, NULL, NULL, NULL, 1) != 1)
{
fprintf(stderr, "could not send query: %s\n", PQerrorMessage(conn));
exit(1);
}
if (verbose)
fprintf(stderr, "getting chunks: %s\n", sql);
if (PQsetSingleRowMode(conn) != 1)
{
fprintf(stderr, "could not set libpq connection to single row mode\n");
exit(1);
}
if (verbose)
fprintf(stderr, "sent query\n");
while ((res = PQgetResult(conn)) != NULL)
{
char *filename;
int filenamelen;
int chunkoff;
int chunksize;
char *chunk;
switch(PQresultStatus(res))
{
case PGRES_SINGLE_TUPLE:
break;
case PGRES_TUPLES_OK:
continue; /* final zero-row result */
default:
fprintf(stderr, "unexpected result while fetching remote files: %s\n",
PQresultErrorMessage(res));
exit(1);
}
/* sanity check the result set */
if (!(PQnfields(res) == 3 && PQntuples(res) == 1))
{
fprintf(stderr, "unexpected result set size while fetching remote files\n");
exit(1);
}
if (!(PQftype(res, 0) == TEXTOID && PQftype(res, 1) == INT4OID && PQftype(res, 2) == BYTEAOID))
{
fprintf(stderr, "unexpected data types in result set while fetching remote files: %u %u %u\n", PQftype(res, 0), PQftype(res, 1), PQftype(res, 2));
exit(1);
}
if (!(PQfformat(res, 0) == 1 && PQfformat(res, 1) == 1 && PQfformat(res, 2) == 1))
{
fprintf(stderr, "unexpected result format while fetching remote files\n");
exit(1);
}
if (!(!PQgetisnull(res, 0, 0) && !PQgetisnull(res, 0, 1) && !PQgetisnull(res, 0, 2) &&
PQgetlength(res, 0, 1) == sizeof(int32)))
{
fprintf(stderr, "unexpected result set while fetching remote files\n");
exit(1);
}
/* Read result set to local variables */
memcpy(&chunkoff, PQgetvalue(res, 0, 1), sizeof(int32));
chunkoff = ntohl(chunkoff);
chunksize = PQgetlength(res, 0, 2);
filenamelen = PQgetlength(res, 0, 0);
filename = pg_malloc(filenamelen + 1);
memcpy(filename, PQgetvalue(res, 0, 0), filenamelen);
filename[filenamelen] = '\0';
chunk = PQgetvalue(res, 0, 2);
if (verbose)
fprintf(stderr, "received chunk for file \"%s\", off %d, len %d\n",
filename, chunkoff, chunksize);
open_target_file(filename, false);
write_file_range(chunk, chunkoff, chunksize);
}
}
/*
* Receive a single file.
*/
char *
libpqGetFile(const char *filename, size_t *filesize)
{
PGresult *res;
char *result;
int len;
const char *paramValues[1];
paramValues[0] = filename;
res = PQexecParams(conn, "select pg_read_binary_file($1)",
1, NULL, paramValues, NULL, NULL, 1);
if (PQresultStatus(res) != PGRES_TUPLES_OK)
{
fprintf(stderr, "unexpected result while fetching remote file \"%s\": %s\n",
filename, PQresultErrorMessage(res));
exit(1);
}
/* sanity check the result set */
if (!(PQntuples(res) == 1 && !PQgetisnull(res, 0, 0)))
{
fprintf(stderr, "unexpected result set while fetching remote file \"%s\"\n",
filename);
exit(1);
}
/* Read result to local variables */
len = PQgetlength(res, 0, 0);
result = pg_malloc(len + 1);
memcpy(result, PQgetvalue(res, 0, 0), len);
result[len] = '\0';
if (verbose)
printf("fetched file \"%s\", length %d\n", filename, len);
if (filesize)
*filesize = len;
return result;
}
static void
copy_file_range(const char *path, unsigned int begin, unsigned int end)
{
char linebuf[MAXPGPATH + 23];
/* Split the range into CHUNKSIZE chunks */
while (end - begin > 0)
{
unsigned int len;
if (end - begin > CHUNKSIZE)
len = CHUNKSIZE;
else
len = end - begin;
snprintf(linebuf, sizeof(linebuf), "%s\t%u\t%u\n", path, begin, len);
if (PQputCopyData(conn, linebuf, strlen(linebuf)) != 1)
{
fprintf(stderr, "error sending COPY data: %s\n",
PQerrorMessage(conn));
exit(1);
}
begin += len;
}
}
/*
* Fetch all changed blocks from remote source data directory.
*/
void
libpq_executeFileMap(filemap_t *map)
{
file_entry_t *entry;
const char *sql;
PGresult *res;
int i;
/*
* First create a temporary table, and load it with the blocks that
* we need to fetch.
*/
sql = "create temporary table fetchchunks(path text, begin int4, len int4);";
res = PQexec(conn, sql);
if (PQresultStatus(res) != PGRES_COMMAND_OK)
{
fprintf(stderr, "error creating temporary table: %s\n",
PQresultErrorMessage(res));
exit(1);
}
sql = "copy fetchchunks from stdin";
res = PQexec(conn, sql);
if (PQresultStatus(res) != PGRES_COPY_IN)
{
fprintf(stderr, "unexpected result while sending file list: %s\n",
PQresultErrorMessage(res));
exit(1);
}
for (i = 0; i < map->narray; i++)
{
entry = map->array[i];
execute_pagemap(&entry->pagemap, entry->path);
switch (entry->action)
{
case FILE_ACTION_NONE:
/* ok, do nothing.. */
break;
case FILE_ACTION_COPY:
/* Truncate the old file out of the way, if any */
open_target_file(entry->path, true);
copy_file_range(entry->path, 0, entry->newsize);
break;
case FILE_ACTION_TRUNCATE:
truncate_target_file(entry->path, entry->newsize);
break;
case FILE_ACTION_COPY_TAIL:
copy_file_range(entry->path, entry->oldsize, entry->newsize);
break;
case FILE_ACTION_REMOVE:
remove_target(entry);
break;
case FILE_ACTION_CREATE:
create_target(entry);
break;
}
}
if (PQputCopyEnd(conn, NULL) != 1)
{
fprintf(stderr, "error sending end-of-COPY: %s\n",
PQerrorMessage(conn));
exit(1);
}
while ((res = PQgetResult(conn)) != NULL)
{
if (PQresultStatus(res) != PGRES_COMMAND_OK)
{
fprintf(stderr, "unexpected result while sending file list: %s\n",
PQresultErrorMessage(res));
exit(1);
}
}
/* Ok, we've sent the file list. Now receive the files */
sql =
"-- fetch all the blocks listed in the temp table.\n"
"select path, begin, \n"
" pg_read_binary_file(path, begin, len) as chunk\n"
"from fetchchunks\n";
receiveFileChunks(sql);
}
static void
execute_pagemap(datapagemap_t *pagemap, const char *path)
{
datapagemap_iterator_t *iter;
BlockNumber blkno;
iter = datapagemap_iterate(pagemap);
while (datapagemap_next(iter, &blkno))
{
off_t offset = blkno * BLCKSZ;
copy_file_range(path, offset, offset + BLCKSZ);
}
free(iter);
}