-
Notifications
You must be signed in to change notification settings - Fork 0
/
mdb_scoof.c
52 lines (39 loc) · 1.25 KB
/
mdb_scoof.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
#include "postgres.h"
#include "fmgr.h"
#include "varatt.h"
#include "access/detoast.h"
#include "utils/lsyscache.h"
PG_MODULE_MAGIC;
PG_FUNCTION_INFO_V1(mdb_column_toast_chunk_id);
/*
* Return the chunk_id of the on-disk TOASTed value. Return NULL if the value
* is un-TOASTed or not on-disk.
*/
Datum
mdb_column_toast_chunk_id(PG_FUNCTION_ARGS)
{
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)
PG_RETURN_NULL();
attr = (struct varlena *) DatumGetPointer(PG_GETARG_DATUM(0));
if (!VARATT_IS_EXTERNAL_ONDISK(attr))
PG_RETURN_NULL();
VARATT_EXTERNAL_GET_POINTER(toast_pointer, attr);
PG_RETURN_OID(toast_pointer.va_valueid);
}