-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathtoastinfo.c
233 lines (201 loc) · 6.29 KB
/
toastinfo.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
/*
Author: Christoph Berg <cb@df7cb.de>
Copyright: Copyright (c) 1996-2021, The PostgreSQL Global Development Group
Permission to use, copy, modify, and distribute this software and its
documentation for any purpose, without fee, and without a written agreement
is hereby granted, provided that the above copyright notice and this
paragraph and the following two paragraphs appear in all copies.
IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR
DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING
LOST PROFITS, ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS
DOCUMENTATION, EVEN IF THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
ON AN "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATIONS TO
PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
*/
#include <postgres.h>
#include <fmgr.h>
#include <utils/lsyscache.h>
#if PG_VERSION_NUM >= 130000
#include <access/detoast.h>
#else
#include <access/tuptoaster.h>
#endif
#if PG_VERSION_NUM >= 90500
#include <utils/expandeddatum.h>
#endif
#if PG_VERSION_NUM >= 140000
#include <access/toast_compression.h>
#endif
#if PG_VERSION_NUM < 90400
/*
* Macro to fetch the possibly-unaligned contents of an EXTERNAL datum
* into a local "struct varatt_external" toast pointer. This should be
* just a memcpy, but some versions of gcc seem to produce broken code
* that assumes the datum contents are aligned. Introducing an explicit
* intermediate "varattrib_1b_e *" variable seems to fix it.
*/
#define VARATT_EXTERNAL_GET_POINTER(toast_pointer, attr) \
do { \
varattrib_1b_e *attre = (varattrib_1b_e *) (attr); \
Assert(VARATT_IS_EXTERNAL(attre)); \
Assert(VARSIZE_EXTERNAL(attre) == sizeof(toast_pointer) + VARHDRSZ_EXTERNAL); \
memcpy(&(toast_pointer), VARDATA_EXTERNAL(attre), sizeof(toast_pointer)); \
} while (0)
#endif
PG_MODULE_MAGIC;
char *toast_datum_info(Datum value);
char *
toast_datum_info(Datum value)
{
struct varlena *attr = (struct varlena *) DatumGetPointer(value);
#if PG_VERSION_NUM >= 90400
if (VARATT_IS_EXTERNAL_ONDISK(attr))
#else
if (VARATT_IS_EXTERNAL(attr))
#endif
{
struct varatt_external toast_pointer;
VARATT_EXTERNAL_GET_POINTER(toast_pointer, attr);
#if PG_VERSION_NUM >= 90400
if (VARATT_EXTERNAL_IS_COMPRESSED(toast_pointer))
#else
if (toast_pointer.va_extsize < toast_pointer.va_rawsize - VARHDRSZ)
#endif
#if PG_VERSION_NUM >= 140000
switch (VARATT_EXTERNAL_GET_COMPRESS_METHOD(toast_pointer))
{
case TOAST_PGLZ_COMPRESSION_ID:
return "toasted varlena, compressed (pglz)";
case TOAST_LZ4_COMPRESSION_ID:
return "toasted varlena, compressed (lz4)";
default:
return "toasted varlena, compressed (invalid/unknown method)";
}
#else
return "toasted varlena, compressed";
#endif
else
return "toasted varlena, uncompressed";
}
#if PG_VERSION_NUM >= 90400
else if (VARATT_IS_EXTERNAL_INDIRECT(attr))
{
struct varatt_indirect toast_pointer;
VARATT_EXTERNAL_GET_POINTER(toast_pointer, attr);
return "indirect in-memory varlena";
}
#endif
#if PG_VERSION_NUM >= 90500
else if (VARATT_IS_EXTERNAL_EXPANDED(attr))
{
return "expanded in-memory varlena";
}
#endif
else if (VARATT_IS_SHORT(attr))
{
return "short inline varlena";
}
else
{
if (VARATT_IS_COMPRESSED(attr))
#if PG_VERSION_NUM >= 140000
switch (VARDATA_COMPRESSED_GET_COMPRESS_METHOD(attr))
{
case TOAST_PGLZ_COMPRESSION_ID:
return "long inline varlena, compressed (pglz)";
case TOAST_LZ4_COMPRESSION_ID:
return "long inline varlena, compressed (lz4)";
default:
return "long inline varlena, compressed (invalid/unknown method)";
}
#else
return "long inline varlena, compressed";
#endif
else
return "long inline varlena, uncompressed";
}
}
PG_FUNCTION_INFO_V1 (pg_toastinfo);
Datum pg_toastinfo (PG_FUNCTION_ARGS);
Datum
pg_toastinfo (PG_FUNCTION_ARGS)
{
Datum value;
char *result;
int typlen;
/* On first call, get the input type's typlen, and save at *fn_extra */
if (fcinfo->flinfo->fn_extra == NULL)
{
/* Lookup the datatype of the supplied argument */
Oid argtypeid = get_fn_expr_argtype(fcinfo->flinfo, 0);
typlen = get_typlen(argtypeid);
if (typlen == 0) /* should not happen */
elog(ERROR, "cache lookup failed for type %u", argtypeid);
fcinfo->flinfo->fn_extra = MemoryContextAlloc(fcinfo->flinfo->fn_mcxt,
sizeof(int));
*((int *) fcinfo->flinfo->fn_extra) = typlen;
}
else
typlen = *((int *) fcinfo->flinfo->fn_extra);
if (PG_ARGISNULL(0))
result = "null";
else
{
value = PG_GETARG_DATUM(0);
if (typlen == -1)
{
/* varlena type, possibly toasted */
result = toast_datum_info(value);
}
else if (typlen == -2)
{
/* cstring */
result = "cstring";
}
else
{
/* ordinary fixed-width type */
result = "ordinary";
}
}
PG_RETURN_CSTRING(result);
}
PG_FUNCTION_INFO_V1 (pg_toastpointer);
Datum pg_toastpointer (PG_FUNCTION_ARGS);
Datum
pg_toastpointer (PG_FUNCTION_ARGS)
{
Datum value = PG_GETARG_DATUM(0);
int typlen;
struct varlena *attr;
struct varatt_external toast_pointer;
/* On first call, get the input type's typlen, and save at *fn_extra */
if (fcinfo->flinfo->fn_extra == NULL)
{
/* Lookup the datatype of the supplied argument */
Oid argtypeid = get_fn_expr_argtype(fcinfo->flinfo, 0);
typlen = get_typlen(argtypeid);
if (typlen == 0) /* should not happen */
elog(ERROR, "cache lookup failed for type %u", argtypeid);
fcinfo->flinfo->fn_extra = MemoryContextAlloc(fcinfo->flinfo->fn_mcxt,
sizeof(int));
*((int *) fcinfo->flinfo->fn_extra) = typlen;
}
else
typlen = *((int *) fcinfo->flinfo->fn_extra);
if (typlen != -1) /* not a varlena, return NULL */
PG_RETURN_NULL();
attr = (struct varlena *) DatumGetPointer(value);
#if PG_VERSION_NUM >= 90400
if (!VARATT_IS_EXTERNAL_ONDISK(attr)) /* non-toasted varlena, return NULL */
#else
if (!VARATT_IS_EXTERNAL(attr)) /* non-toasted varlena, return NULL */
#endif
PG_RETURN_NULL();
VARATT_EXTERNAL_GET_POINTER(toast_pointer, attr);
PG_RETURN_OID(toast_pointer.va_valueid);
}