-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpg_hook_lua.c
80 lines (58 loc) · 1.63 KB
/
pg_hook_lua.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
#include "postgres.h"
#include "fmgr.h"
PG_MODULE_MAGIC;
#include <lua.h>
#include <lualib.h>
#include <lauxlib.h>
#include <luajit.h>
PGDLLEXPORT Datum _PG_init(PG_FUNCTION_ARGS);
PGDLLEXPORT Datum _PG_fini(PG_FUNCTION_ARGS);
#define out(...) ereport(INFO, (errmsg(__VA_ARGS__)))
#define dolog(...) ereport(LOG, (errmsg(__VA_ARGS__)))
#define pg_throw(...) ereport(ERROR, (errmsg(__VA_ARGS__)))
static lua_State *L = NULL;
static int init_ref = 0;
static int fini_ref = 0;
PG_FUNCTION_INFO_V1(_PG_init);
Datum _PG_init(PG_FUNCTION_ARGS)
{
int status;
L = lua_open();
LUAJIT_VERSION_SYM();
lua_gc(L, LUA_GCSTOP, 0);
luaL_openlibs(L);
lua_gc(L, LUA_GCRESTART, -1);
dolog("pglj_hook start");
lua_getglobal(L, "require");
lua_pushstring(L, "pglj_hook");
status = lua_pcall(L, 1, 1, 0);
if (status)
pg_throw("pglj_hook loading error");
lua_getfield(L, 1, "_PG_init");
init_ref = luaL_ref(L, LUA_REGISTRYINDEX);
lua_getfield(L, 1, "_PG_fini");
fini_ref = luaL_ref(L, LUA_REGISTRYINDEX);
lua_settop(L, 0);
lua_rawgeti(L, LUA_REGISTRYINDEX, init_ref);
status = lua_pcall(L, 0, 0, 0);
if (status == 0){
PG_RETURN_VOID();
}
if( status == LUA_ERRRUN) {
pg_throw("%s %s","Error:",lua_tostring(L, -1));
} else if (status == LUA_ERRMEM) {
pg_throw("%s %s","Memory error:",lua_tostring(L, -1));
} else if (status == LUA_ERRERR) {
pg_throw("%s %s","Error:",lua_tostring(L, -1));
}
pg_throw("pllj unknown error");
PG_RETURN_VOID();
}
PG_FUNCTION_INFO_V1(_PG_fini);
Datum _PG_fini(PG_FUNCTION_ARGS) {
lua_settop(L, 0);
lua_rawgeti(L, LUA_REGISTRYINDEX, fini_ref);
lua_pcall(L, 0, 0, 0);
lua_close(L);
PG_RETURN_VOID();
}