Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 8207a8f

Browse files
author
Nikita Malakhov
committed
Contrib module dummy_toaster for TOAST API
Dummy toaster contrib module implemented via new TOAST API is intriduced to be used as sample no-op Toaster, to demonstrate how Toaster could be implemented via TOAST API, registered and used by end-user. Dummy Toaster implements mandatory operations - toast, detoast, validate only, with toast and detoast return unchanged value. Dummy Toaster does not introduce any heavy internal TOAST mechanics. Author: Teodor Sigaev <teodor@sigaev.ru> Author: Oleg Bartunov <obartunov@postgrespro.ru> Author: Nikita Glukhov <n.gluhov@postgrespro.ru> Author: Nikita Malakhov <n.malakhov@postgrespro.ru> Discussion: https://www.postgresql.org/message-id/flat/224711f9-83b7-a307-b17f-4457ab73aa0a@sigaev.ru
1 parent f10a005 commit 8207a8f

File tree

9 files changed

+512
-0
lines changed

9 files changed

+512
-0
lines changed

contrib/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ SUBDIRS = \
1919
dblink \
2020
dict_int \
2121
dict_xsyn \
22+
dummy_toaster \
2223
earthdistance \
2324
file_fdw \
2425
fuzzystrmatch \

contrib/dummy_toaster/Makefile

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# contrib/dummy_toaster/Makefile
2+
3+
MODULE_big = dummy_toaster
4+
OBJS = \
5+
$(WIN32RES) \
6+
dummy_toaster.o
7+
8+
EXTENSION = dummy_toaster
9+
DATA = dummy_toaster--1.0.sql
10+
PGFILEDESC = "dummy_toaster - toaster example"
11+
12+
REGRESS = dummy_toaster
13+
14+
ifdef USE_PGXS
15+
PG_CONFIG = pg_config
16+
PGXS := $(shell $(PG_CONFIG) --pgxs)
17+
include $(PGXS)
18+
else
19+
subdir = contrib/dummy_toaster
20+
top_builddir = ../..
21+
include $(top_builddir)/src/Makefile.global
22+
include $(top_srcdir)/contrib/contrib-global.mk
23+
endif
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/* contrib/bloom/bloom--1.0.sql */
2+
3+
-- complain if script is sourced in psql, rather than via CREATE EXTENSION
4+
\echo Use "CREATE EXTENSION dummy_toaster" to load this file. \quit
5+
6+
CREATE FUNCTION dummy_toaster_handler(internal)
7+
RETURNS toaster_handler
8+
AS 'MODULE_PATHNAME'
9+
LANGUAGE C;
10+
11+
12+
CREATE TOASTER dummy_toaster HANDLER dummy_toaster_handler;
13+
14+
COMMENT ON TOASTER dummy_toaster IS 'dummy_toaster is a dummy toaster';
15+

contrib/dummy_toaster/dummy_toaster.c

Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
/*-------------------------------------------------------------------------
2+
*
3+
* dummy_toaster.c
4+
* Dummy toaster - sample toaster for Toaster API.
5+
*
6+
* Portions Copyright (c) 2016-2021, PostgreSQL Global Development Group
7+
* Portions Copyright (c) 1990-1993, Regents of the University of California
8+
*
9+
* IDENTIFICATION
10+
* contrib/dummy_toaster/dummy_toaster.c
11+
*
12+
*-------------------------------------------------------------------------
13+
*/
14+
#include "postgres.h"
15+
#include "fmgr.h"
16+
#include "access/toasterapi.h"
17+
#include "access/heaptoast.h"
18+
#include "access/htup_details.h"
19+
#include "catalog/pg_toaster.h"
20+
#include "commands/defrem.h"
21+
#include "utils/builtins.h"
22+
#include "utils/syscache.h"
23+
#include "access/toast_compression.h"
24+
#include "access/xact.h"
25+
#include "catalog/binary_upgrade.h"
26+
#include "catalog/catalog.h"
27+
#include "catalog/dependency.h"
28+
#include "catalog/heap.h"
29+
#include "catalog/index.h"
30+
#include "catalog/namespace.h"
31+
#include "catalog/pg_am.h"
32+
#include "catalog/pg_namespace.h"
33+
#include "catalog/pg_opclass.h"
34+
#include "catalog/pg_type.h"
35+
#include "catalog/toasting.h"
36+
#include "miscadmin.h"
37+
#include "nodes/makefuncs.h"
38+
39+
PG_MODULE_MAGIC;
40+
PG_FUNCTION_INFO_V1(dummy_toaster_handler);
41+
42+
#define MAX_DUMMY_CHUNK_SIZE 1024
43+
44+
/*
45+
* Dummy Detoast function, receives single varatt_custom pointer,
46+
* detoasts it to varlena.
47+
*
48+
*/
49+
static Datum
50+
dummy_detoast(Datum toast_ptr,
51+
int offset, int length)
52+
{
53+
struct varlena *attr = (struct varlena *) DatumGetPointer(toast_ptr);
54+
struct varlena *result;
55+
56+
Assert(VARATT_IS_EXTERNAL(attr));
57+
Assert(VARATT_IS_CUSTOM(attr));
58+
59+
result = palloc(VARATT_CUSTOM_GET_DATA_RAW_SIZE(attr));
60+
SET_VARSIZE(result, VARATT_CUSTOM_GET_DATA_RAW_SIZE(attr));
61+
memcpy(VARDATA(result), VARATT_CUSTOM_GET_DATA(attr),
62+
VARATT_CUSTOM_GET_DATA_RAW_SIZE(attr) - VARHDRSZ);
63+
64+
return PointerGetDatum(result);
65+
}
66+
67+
/*
68+
* Dummy Toast function, receives varlena pointer, creates single varatt_custom
69+
* varlena size is limited to 1024 bytes
70+
*/
71+
static Datum
72+
dummy_toast(Relation toast_rel, Oid toasterid,
73+
Datum value, Datum oldvalue,
74+
int max_inline_size, int options)
75+
{
76+
struct varlena *attr;
77+
struct varlena *result;
78+
int len;
79+
attr = (struct varlena*)DatumGetPointer(value); //pg_detoast_datum((struct varlena*)DatumGetPointer(value));
80+
81+
if(VARSIZE_ANY_EXHDR(attr) > MAX_DUMMY_CHUNK_SIZE)
82+
{
83+
ereport(ERROR,
84+
(errcode(ERRCODE_DATA_CORRUPTED),
85+
errmsg_internal("Data <%d> size exceeds MAX_DUMMY_CHUNK_SIZE <%d>",
86+
(int)VARSIZE_ANY_EXHDR(attr), MAX_DUMMY_CHUNK_SIZE)));
87+
88+
}
89+
90+
len = VARATT_CUSTOM_SIZE(VARSIZE_ANY_EXHDR(attr));
91+
92+
if (max_inline_size > 0 && len > max_inline_size)
93+
{
94+
ereport(ERROR,
95+
(errcode(ERRCODE_DATA_CORRUPTED),
96+
errmsg_internal("Data <%d> size exceeds max inline size <%d>",
97+
len, max_inline_size)));
98+
}
99+
100+
result = palloc(len);
101+
102+
SET_VARTAG_EXTERNAL(result, VARTAG_CUSTOM);
103+
VARATT_CUSTOM_SET_DATA_RAW_SIZE(result, VARSIZE_ANY_EXHDR(attr) + VARHDRSZ);
104+
VARATT_CUSTOM_SET_DATA_SIZE(result, len);
105+
VARATT_CUSTOM_SET_TOASTERID(result, toasterid);
106+
107+
memcpy(VARATT_CUSTOM_GET_DATA(result), VARDATA_ANY(attr),
108+
VARSIZE_ANY_EXHDR(attr));
109+
110+
if ((char*)attr != DatumGetPointer(value))
111+
pfree(attr);
112+
113+
return PointerGetDatum(result);
114+
}
115+
116+
/*
117+
* Dummy delete function
118+
*/
119+
static void
120+
dummy_delete(Datum value, bool is_speculative)
121+
{
122+
}
123+
124+
/*
125+
* Dummy Validate, always returns True
126+
*
127+
*/
128+
static bool
129+
dummy_toaster_validate(Oid typeoid, char storage, char compression,
130+
Oid amoid, bool false_ok)
131+
{
132+
bool result = true;
133+
return result;
134+
}
135+
136+
/*
137+
* Dummy validation function, always returns TRUE
138+
*/
139+
static void
140+
dummy_toast_init(Relation rel, Oid toastoid, Oid toastindexoid, Datum reloptions, LOCKMODE lockmode,
141+
bool check, Oid OIDOldToast)
142+
{
143+
}
144+
145+
Datum
146+
dummy_toaster_handler(PG_FUNCTION_ARGS)
147+
{
148+
TsrRoutine *tsr = makeNode(TsrRoutine);
149+
tsr->init = dummy_toast_init;
150+
tsr->toast = dummy_toast;
151+
tsr->update_toast = NULL;
152+
tsr->copy_toast = NULL;
153+
tsr->detoast = dummy_detoast;
154+
tsr->deltoast = dummy_delete;
155+
tsr->get_vtable = NULL;
156+
tsr->toastervalidate = dummy_toaster_validate;
157+
158+
PG_RETURN_POINTER(tsr);
159+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# dummy_toaster extension
2+
comment = 'dummy_toaster - dummy toaster'
3+
default_version = '1.0'
4+
module_pathname = '$libdir/dummy_toaster'
5+
relocatable = true

0 commit comments

Comments
 (0)