forked from snaga/xlogdump
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxlogdump_statement.c
246 lines (205 loc) · 5.97 KB
/
xlogdump_statement.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
/*
* xlogdump_statement.c
*
* a collection of functions to build/re-produce (fake) SQL statements
* from xlog records.
*/
#include "xlogdump_statement.h"
#include "access/tupmacs.h"
#include "storage/bufpage.h"
#include "xlogdump_oid2name.h"
static int printField(char *, int, int, uint32);
#if PG_VERSION_NUM < 80300
#define MaxHeapTupleSize (BLCKSZ - MAXALIGN(sizeof(PageHeaderData)))
#endif
/*
* Print a insert command that contains all the data on a xl_heap_insert
*/
void
printInsert(xl_heap_insert *xlrecord, uint32 datalen, const char *relName)
{
char data[MaxHeapTupleSize];
xl_heap_header hhead;
int offset;
bits8 nullBitMap[MaxNullBitmapLen];
MemSet((char *) data, 0, MaxHeapTupleSize * sizeof(char));
MemSet(nullBitMap, 0, MaxNullBitmapLen);
if(datalen > MaxHeapTupleSize)
return;
/* Copy the heap header into hhead,
the the heap data into data
and the tuple null bitmap into nullBitMap */
memcpy(&hhead, (char *) xlrecord + SizeOfHeapInsert, SizeOfHeapHeader);
memcpy(&data, (char *) xlrecord + hhead.t_hoff - 4, datalen);
#if PG_VERSION_NUM >= 80300
memcpy(&nullBitMap, (bits8 *) xlrecord + SizeOfHeapInsert + SizeOfHeapHeader, BITMAPLEN(HeapTupleHeaderGetNatts(&hhead)) * sizeof(bits8));
#else
#warning "Copying null bitmap is not implemented for 8.2.x." /* FIXME: need to fix for 8.2 */
#endif
printf("INSERT INTO \"%s\" (", relName);
// Get relation field names and types
if (oid2name_enabled())
{
int i, rows = 0, fieldSize = 0;
rows = relname2attr_begin(relName);
for(i = 0; i < rows; i++)
{
char attname[NAMEDATALEN];
Oid atttypid;
relname2attr_fetch(i, attname, &atttypid);
printf("%s%s", (i == 0 ? "" : ", "), attname);
}
printf(") VALUES (");
offset = 0;
for(i = 0; i < rows; i++)
{
char attname[NAMEDATALEN];
Oid atttypid;
relname2attr_fetch(i, attname, &atttypid);
/* is the attribute value null? */
if((hhead.t_infomask & HEAP_HASNULL) && (att_isnull(i, nullBitMap)))
{
printf("%sNULL", (i == 0 ? "" : ", "));
}
else
{
printf("%s'", (i == 0 ? "" : ", "));
if(!(fieldSize = printField(data, offset, atttypid, datalen)))
{
printf("'");
break;
}
else
printf("'");
offset += fieldSize;
}
}
printf(");\n");
relname2attr_end();
}
}
/*
* Print a update command that contains all the data on a xl_heap_update
*/
void
printUpdate(xl_heap_update *xlrecord, uint32 datalen, const char *relName)
{
char data[MaxHeapTupleSize];
xl_heap_header hhead;
int offset;
bits8 nullBitMap[MaxNullBitmapLen];
MemSet((char *) data, 0, MaxHeapTupleSize * sizeof(char));
MemSet(nullBitMap, 0, MaxNullBitmapLen);
if(datalen > MaxHeapTupleSize)
return;
/* Copy the heap header into hhead,
the the heap data into data
and the tuple null bitmap into nullBitMap */
memcpy(&hhead, (char *) xlrecord + SizeOfHeapUpdate, SizeOfHeapHeader);
memcpy(&data, (char *) xlrecord + hhead.t_hoff + 4, datalen);
#if PG_VERSION_NUM >= 80300
memcpy(&nullBitMap, (bits8 *) xlrecord + SizeOfHeapUpdate + SizeOfHeapHeader, BITMAPLEN(HeapTupleHeaderGetNatts(&hhead)) * sizeof(bits8));
#else
#warning "Copying null bitmap is not implemented for 8.2.x." /* FIXME: need to fix for 8.2 */
#endif
printf("UPDATE \"%s\" SET ", relName);
// Get relation field names and types
if (oid2name_enabled())
{
int i, rows = 0, fieldSize = 0;
rows = relname2attr_begin(relName);
offset = 0;
for(i = 0; i < rows; i++)
{
char attname[NAMEDATALEN];
Oid atttypid;
relname2attr_fetch(i, attname, &atttypid);
printf("%s%s = ", (i == 0 ? "" : ", "), attname);
/* is the attribute value null? */
if((hhead.t_infomask & HEAP_HASNULL) && (att_isnull(i, nullBitMap)))
{
printf("NULL");
}
else
{
printf("'");
if(!(fieldSize = printField(data, offset, atttypid, datalen)))
break;
printf("'");
offset += fieldSize;
}
}
printf(" WHERE ... ;\n");
relname2attr_end();
}
}
/*
* Print the field based on a chunk of xlog data and the field type
* The maxfield len is just for error detection on variable length data,
* actualy is based on the xlog record total lenght
*/
static int
printField(char *data, int offset, int type, uint32 maxFieldLen)
{
int32 i, size;
int64 bigint;
int16 smallint;
float4 floatNumber;
float8 doubleNumber;
Oid objectId;
// Just print out the value of a specific data type from the data array
switch(type)
{
case 700: //float4
memcpy(&floatNumber, &data[offset], sizeof(float4));
printf("%f", floatNumber);
return sizeof(float4);
case 701: //float8
memcpy(&doubleNumber, &data[offset], sizeof(float8));
printf("%f", doubleNumber);
return sizeof(float8);
case 16: //boolean
printf("%c", (data[offset] == 0 ? 'f' : 't'));
return MAXALIGN(sizeof(bool));
case 1043: //varchar
case 1042: //bpchar
case 25: //text
case 18: //char
memcpy(&size, &data[offset], sizeof(int32));
//@todo usar putc
if(size > maxFieldLen || size < 0)
{
fprintf(stderr, "ERROR: Invalid field size\n");
return 0;
}
for(i = sizeof(int32); i < size; i++)
printf("%c", data[offset + i]);
//return ( (size % sizeof(int)) ? size + sizeof(int) - (size % sizeof(int)):size);
return MAXALIGN(size * sizeof(char));
case 19: //name
for(i = 0; i < NAMEDATALEN && data[offset + i] != '\0'; i++)
printf("%c", data[offset + i]);
return NAMEDATALEN;
case 21: //smallint
memcpy(&smallint, &data[offset], sizeof(int16));
printf("%i", (int) smallint);
return sizeof(int16);
case 23: //int
memcpy(&i, &data[offset], sizeof(int32));
printf("%i", i);
return sizeof(int32);
case 26: //oid
memcpy(&objectId, &data[offset], sizeof(Oid));
printf("%i", (int) objectId);
return sizeof(Oid);
case 20: //bigint
//@todo como imprimir int64?
memcpy(&bigint, &data[offset], sizeof(int64));
printf("%i", (int) bigint);
return sizeof(int64);
case 1005: //int2vector
memcpy(&size, &data[offset], sizeof(int32));
return MAXALIGN(size);
}
return 0;
}