-
Notifications
You must be signed in to change notification settings - Fork 77
/
Copy pathwriter_binary.c
537 lines (441 loc) · 13.3 KB
/
writer_binary.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
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
/*
* pg_bulkload: lib/writer_binary.c
*
* Copyright (c) 2011-2025, NIPPON TELEGRAPH AND TELEPHONE CORPORATION
*/
#include "pg_bulkload.h"
#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>
#include "access/heapam.h"
#include "catalog/pg_type.h"
#include "mb/pg_wchar.h"
#include "miscadmin.h"
#include "utils/memutils.h"
#include "binary.h"
#include "logger.h"
#include "pg_profile.h"
#include "pg_strutil.h"
#include "storage/fd.h"
#include "writer.h"
#ifndef UINT16_MAX
#define UINT16_MAX (65535U)
#endif
#ifndef UINT32_MAX
#define UINT32_MAX (4294967295U)
#endif
/**
* @brief The number of records write at one time
*/
#define WRITE_LINE_NUM 100
/**
* @brief output a binary format file
*/
typedef struct BinaryWriter
{
Writer base;
int bin_fd; /**< File descriptor of binary file to output */
int ctl_fd; /**< File descriptor of control file to output */
size_t rec_len; /**< One record length */
char *buffer; /**< record buffer to keep output data */
int used_rec_cnt; /**< # of used records in buffer */
int nfield; /**< number of fields */
Field *fields; /**< array of field descriptor */
Datum *values;
bool *nulls;
} BinaryWriter;
static void BinaryWriterInit(BinaryWriter *self);
static void BinaryWriterInsert(BinaryWriter *self, HeapTuple tuple);
static WriterResult BinaryWriterClose(BinaryWriter *self, bool onError);
static bool BinaryWriterParam(BinaryWriter *self, const char *keyword, char *value);
static void BinaryWriterDumpParams(BinaryWriter *self);
static int BinaryWriterSendQuery(BinaryWriter *self, PGconn *conn, char *queueName, char *logfile, bool verbose);
/* Signature of static functions */
static int open_output_file(char *fname, char *filetype, bool check);
static void close_output_file(int *fd, char *filetype);
static HeapTuple BinaryWriterCheckerTuple(TupleChecker *self, HeapTuple tuple, int *parsing_field);
/* ========================================================================
* Implementation
* ========================================================================*/
/**
* @brief Create a new BinaryWriter
*/
Writer *
CreateBinaryWriter(void *opt)
{
BinaryWriter *self;
self = palloc0(sizeof(BinaryWriter));
self->base.init = (WriterInitProc) BinaryWriterInit;
self->base.insert = (WriterInsertProc) BinaryWriterInsert;
self->base.close = (WriterCloseProc) BinaryWriterClose;
self->base.param = (WriterParamProc) BinaryWriterParam;
self->base.dumpParams = (WriterDumpParamsProc) BinaryWriterDumpParams;
self->base.sendQuery = (WriterSendQueryProc) BinaryWriterSendQuery;
self->bin_fd = -1;
self->ctl_fd = -1;
return (Writer *) self;
}
/**
* @brief Initialize a BinaryWriter
*/
static void
BinaryWriterInit(BinaryWriter *self)
{
char path[MAXPGPATH];
TupleDesc tupdesc;
int i;
bool need_check = false;
Assert(self->base.truncate == false);
/* exist check of output file */
self->bin_fd = open_output_file(self->base.output,
"binary output file", true);
snprintf(path, MAXPGPATH, "%s.ctl", self->base.output);
self->ctl_fd = open_output_file(path, "sample control file", true);
/* create TupleDesc */
#if PG_VERSION_NUM >= 120000
tupdesc = CreateTemplateTupleDesc(self->nfield);
#else
tupdesc = CreateTemplateTupleDesc(self->nfield, false);
#endif
for (i = 0; i < self->nfield; i++)
{
TupleDescInitEntry(tupdesc, i + 1, "out col", self->fields[i].typeid,
-1, 0);
self->rec_len += self->fields[i].len;
if (self->fields[i].nulllen == 0 ||
self->fields[i].typeid == CSTRINGOID ||
(self->fields[i].typeid == INT4OID && self->fields[i].len == 2) ||
(self->fields[i].typeid == INT8OID && self->fields[i].len == 4) )
need_check = true;
}
self->base.desc = tupdesc;
self->base.tchecker = CreateTupleChecker(tupdesc);
if (need_check)
{
self->base.tchecker->checker =
(CheckerTupleProc) BinaryWriterCheckerTuple;
self->base.tchecker->opt = self->fields;
}
else
self->base.tchecker->checker = (CheckerTupleProc) CoercionCheckerTuple;
self->buffer = palloc(self->rec_len * WRITE_LINE_NUM);
self->used_rec_cnt = 0;
self->values = (Datum *) palloc(self->nfield * sizeof(Datum));
self->nulls = (bool *) palloc(self->nfield * sizeof(bool));
self->base.context = AllocSetContextCreate(
CurrentMemoryContext,
"BinaryWriter",
#if PG_VERSION_NUM >= 90600
ALLOCSET_DEFAULT_SIZES);
#else
ALLOCSET_SMALL_MINSIZE,
ALLOCSET_SMALL_INITSIZE,
ALLOCSET_DEFAULT_MAXSIZE);
#endif
}
static void
BinaryWriterInsert(BinaryWriter *self, HeapTuple tuple)
{
int i;
char *col;
col = self->buffer + (self->rec_len * self->used_rec_cnt);
/* Break down the tuple into fields */
heap_deform_tuple(tuple, self->base.desc, self->values, self->nulls);
for (i = 0; i < self->nfield; i++)
{
Field *field = self->fields + i;
if (!self->nulls[i])
field->write(col, field->len, self->values[i], self->nulls[i]);
else
field->write(col, field->len, PointerGetDatum(field->nullif), field->nulllen);
col += field->len;
}
/* open file */
if (self->bin_fd == -1)
{
char path[MAXPGPATH];
self->bin_fd = open_output_file(self->base.output,
"binary output file", false);
snprintf(path, MAXPGPATH, "%s.ctl", self->base.output);
self->ctl_fd = open_output_file(path, "sample control file", false);
}
self->used_rec_cnt++;
if (self->used_rec_cnt >= WRITE_LINE_NUM)
{
int len = self->rec_len * self->used_rec_cnt;
if (write(self->bin_fd, self->buffer, len) != len)
ereport(ERROR,
(errcode_for_file_access(),
errmsg("could not write to binary output file: %m")));
self->used_rec_cnt = 0;
}
BULKLOAD_PROFILE(&prof_writer_table);
}
/*
* Clean up BinaryWriter
*/
static WriterResult
BinaryWriterClose(BinaryWriter *self, bool onError)
{
WriterResult ret = { 0 };
Assert(self != NULL);
if (self->used_rec_cnt > 0)
{
int len = self->rec_len * self->used_rec_cnt;
if (write(self->bin_fd, self->buffer, len) != len)
ereport(WARNING,
(errcode_for_file_access(),
errmsg("could not write to binary output file: %m")));
self->used_rec_cnt = 0;
}
/* create sample of control file */
if (self->base.count > 0)
{
char *filepath;
char *filename;
char *extension;
StringInfoData buf;
filepath = self->base.output;
/* remove extension of outfile */
filename = strrchr(self->base.output, '/');
Assert(filename);
filename++;
filename = pstrdup(filename);
extension = strrchr(filename, '.');
if (extension && filename < extension)
*extension = '\0';
initStringInfo(&buf);
appendStringInfo(&buf, "INPUT = %s\n", filepath);
appendStringInfo(&buf, "OUTPUT = %s\n", filename);
appendStringInfo(&buf, "LOGFILE = %s.log\n", filepath);
appendStringInfo(&buf, "PARSE_BADFILE = %s.prs\n", filepath);
appendStringInfo(&buf, "DUPLICATE_BADFILE = %s.dup\n", filepath);
appendStringInfoString(&buf,
"PARSE_ERRORS = INFINITE\n"
"DUPLICATE_ERRORS = 0\n"
"ON_DUPLICATE_KEEP = NEW\n"
"SKIP = 0\n"
"LIMIT = INFINITE\n"
"CHECK_CONSTRAINTS = NO\n"
"MULTI_PROCESS = YES\n"
"VERBOSE = NO\n"
"TRUNCATE = NO\n"
"WRITER = DIRECT\n"
"TYPE = BINARY\n");
BinaryDumpParams(self->fields, self->nfield, &buf, "COL");
appendStringInfo(&buf, "# ENCODING = %s\n", GetDatabaseEncodingName());
if (write(self->ctl_fd, buf.data, buf.len) != buf.len)
ereport(WARNING,
(errcode_for_file_access(),
errmsg("could not write to sample control file: %m")));
pfree(filename);
pfree(buf.data);
}
close_output_file(&self->bin_fd, "binary output file");
close_output_file(&self->ctl_fd, "sample control file");
if (self->base.output)
pfree(self->base.output);
self->base.output = NULL;
if (self->buffer)
pfree(self->buffer);
self->buffer = NULL;
if (self->values)
pfree(self->values);
self->values = NULL;
if (self->nulls)
pfree(self->nulls);
self->nulls = NULL;
if (self->fields)
pfree(self->fields);
self->fields = NULL;
if (!onError)
MemoryContextDelete(self->base.context);
ret.num_dup_new = 0;
ret.num_dup_old = 0;
return ret;
}
static bool
BinaryWriterParam(BinaryWriter *self, const char *keyword, char *value)
{
if (CompareKeyword(keyword, "CHECK_CONSTRAINTS") ||
CompareKeyword(keyword, "FORCE_NOT_NULL"))
{
ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
errmsg("does not support parameter \"%s\" in \"WRITER = BINARY\"", keyword)));
}
else if (CompareKeyword(keyword, "TABLE") ||
CompareKeyword(keyword, "OUTPUT"))
{
if (strlen(value) + strlen(".ctl") >= MAXPGPATH)
ereport(ERROR,
(errcode(ERRCODE_INTERNAL_ERROR),
errmsg("binary output file name is too long")));
if (!is_absolute_path(value))
ereport(ERROR,
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
errmsg("relative path not allowed for OUTPUT: %s", value)));
/* must be the super user if write to file */
if (!superuser())
ereport(ERROR,
(errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
errmsg("must be superuser to use pg_bulkload to a file")));
ASSERT_ONCE(self->base.output == NULL);
self->base.output = pstrdup(value);
}
else if (CompareKeyword(keyword, "OUT_COL"))
{
BinaryParam(&self->fields, &self->nfield, value, false, true);
}
else
return false; /* unknown parameter */
return true;
}
static void
BinaryWriterDumpParams(BinaryWriter *self)
{
StringInfoData buf;
initStringInfo(&buf);
appendStringInfoString(&buf, "WRITER = BINARY\n");
BinaryDumpParams(self->fields, self->nfield, &buf, "OUT_COL");
LoggerLog(INFO, buf.data, 0);
pfree(buf.data);
}
static int
BinaryWriterSendQuery(BinaryWriter *self, PGconn *conn, char *queueName, char *logfile, bool verbose)
{
int i;
int nparam;
const char **params;
StringInfoData buf;
int offset;
int result;
nparam = self->nfield + 4;
params = palloc0(sizeof(char *) * nparam);
/* async query send */
params[0] = queueName;
params[1] = self->base.output;
params[2] = logfile;
params[3] = verbose ? "true" : "no";
initStringInfo(&buf);
appendStringInfoString(&buf,
"SELECT * FROM pgbulkload.pg_bulkload(ARRAY["
"'TYPE=TUPLE',"
"'INPUT=' || $1,"
"'WRITER=BINARY',"
"'OUTPUT=' || $2,"
"'LOGFILE=' || $3,"
"'VERBOSE=' || $4");
offset = 0;
for (i = 0 ; i < self->nfield; i++)
{
StringInfoData param_buf;
appendStringInfo(&buf, ",'OUT_COL=' || $%d", i + 4 + 1);
initStringInfo(¶m_buf);
offset = BinaryDumpParam(self->fields + i, ¶m_buf, offset);
params[i + 4] = param_buf.data;
}
appendStringInfoString(&buf, "])");
result = PQsendQueryParams(conn, buf.data, nparam, NULL, params, NULL, NULL, 0);
pfree(params);
pfree(buf.data);
return result;
}
/*
* Open the output file and returns its descriptor.
*/
static int
open_output_file(char *fname, char *filetype, bool check)
{
int fd = -1;
#if PG_VERSION_NUM >= 110000
fd = BasicOpenFilePerm(fname, O_WRONLY | O_CREAT | O_EXCL | PG_BINARY,
S_IRUSR | S_IWUSR);
#else
fd = BasicOpenFile(fname, O_WRONLY | O_CREAT | O_EXCL | PG_BINARY,
S_IRUSR | S_IWUSR);
#endif
if (fd == -1)
ereport(ERROR, (errcode_for_file_access(),
errmsg("could not open %s: %m", filetype)));
if (check)
{
close_output_file(&fd, filetype);
unlink(fname);
}
return fd;
}
/**
* Flush and close the output file.
*/
static void
close_output_file(int *fd, char *filetype)
{
if (*fd == -1)
return;
if (pg_fsync(*fd) != 0)
ereport(WARNING, (errcode_for_file_access(),
errmsg("could not fsync %s: %m", filetype)));
if (close(*fd) != 0)
ereport(WARNING, (errcode_for_file_access(),
errmsg("could not close %s: %m", filetype)));
*fd = -1;
}
static HeapTuple
BinaryWriterCheckerTuple(TupleChecker *self, HeapTuple tuple, int *parsing_field)
{
TupleDesc desc = self->targetDesc;
Field *fields = self->opt;
int i;
if (self->status == NEED_COERCION_CHECK)
UpdateTupleCheckStatus(self, tuple);
if (self->status == NO_COERCION)
heap_deform_tuple(tuple, desc, self->values, self->nulls);
else
{
CoercionDeformTuple(self, tuple, parsing_field);
tuple = heap_form_tuple(self->targetDesc, self->values, self->nulls);
}
for (i = 0; i < desc->natts; i++)
{
*parsing_field = i + 1; /* 1 origin */
if (self->nulls[i])
{
if (fields[i].nulllen == 0)
ereport(ERROR,
(errcode(ERRCODE_NOT_NULL_VIOLATION),
errmsg("null value violates not-null constraint")));
continue;
}
switch (fields[i].typeid)
{
case CSTRINGOID:
if (strlen(DatumGetCString(self->values[i])) > fields[i].len)
ereport(ERROR,
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
errmsg("value too long for type character(%d)", fields[i].len)));
break;
case INT4OID:
if (fields[i].len == sizeof(uint16))
{
int32 value = DatumGetInt32(self->values[i]);
if (value < 0 || value > UINT16_MAX)
ereport(ERROR,
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
errmsg("value \"%d\" is out of range for type unsigned smallint", value)));
}
break;
case INT8OID:
if (fields[i].len == sizeof(uint32))
{
int64 value = DatumGetInt64(self->values[i]);
if (value < 0 || value > UINT32_MAX)
ereport(ERROR,
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
errmsg("value \"" int64_FMT "\" is out of range for type unsigned integer", value)));
}
break;
}
}
*parsing_field = -1;
return tuple;
}