Skip to content

Commit 7a662f5

Browse files
committed
Static linking of plpgsql
1 parent f0d5497 commit 7a662f5

File tree

7 files changed

+100
-12
lines changed

7 files changed

+100
-12
lines changed

src/Makefile.shlib

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,13 +122,13 @@ ifeq ($(PORTNAME), darwin)
122122
ifneq ($(SO_MAJOR_VERSION), 0)
123123
version_link = -compatibility_version $(SO_MAJOR_VERSION) -current_version $(SO_MAJOR_VERSION).$(SO_MINOR_VERSION)
124124
endif
125-
LINK.shared = $(COMPILER) -dynamiclib -install_name '$(libdir)/lib$(NAME).$(SO_MAJOR_VERSION)$(DLSUFFIX)' $(version_link) $(exported_symbols_list) -multiply_defined suppress
125+
LINK.shared = $(COMPILER) -dynamiclib -install_name '$(libdir)/lib$(NAME).$(SO_MAJOR_VERSION)$(DLSUFFIX)' $(version_link) $(exported_symbols_list)
126126
shlib = lib$(NAME).$(SO_MAJOR_VERSION).$(SO_MINOR_VERSION)$(DLSUFFIX)
127127
shlib_major = lib$(NAME).$(SO_MAJOR_VERSION)$(DLSUFFIX)
128128
else
129129
# loadable module
130130
DLSUFFIX = .so
131-
LINK.shared = $(COMPILER) -bundle -multiply_defined suppress
131+
LINK.shared = $(COMPILER) -bundle
132132
endif
133133
BUILD.exports = $(AWK) '/^[^\#]/ {printf "_%s\n",$$1}' $< >$@
134134
exports_file = $(SHLIB_EXPORTS:%.txt=%.list)

src/backend/Makefile

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,14 @@ ifneq ($(PORTNAME), cygwin)
6262
ifneq ($(PORTNAME), win32)
6363
ifneq ($(PORTNAME), aix)
6464

65+
OBJS += \
66+
$(top_builddir)/src/pl/plpgsql/src/pl_comp.o \
67+
$(top_builddir)/src/pl/plpgsql/src/pl_exec.o \
68+
$(top_builddir)/src/pl/plpgsql/src/pl_funcs.o \
69+
$(top_builddir)/src/pl/plpgsql/src/pl_gram.o \
70+
$(top_builddir)/src/pl/plpgsql/src/pl_handler.o \
71+
$(top_builddir)/src/pl/plpgsql/src/pl_scanner.o
72+
6573
postgres: $(OBJS)
6674
$(CC) $(CFLAGS) $(call expand_subsys,$^) $(LDFLAGS) $(LDFLAGS_EX) $(export_dynamic) $(LIBS) -sALLOW_MEMORY_GROWTH=1 -sFORCE_FILESYSTEM=1 -lnodefs.js -lproxyfs.js -lidbfs.js -o $@
6775

src/backend/utils/fmgr/dfmgr.c

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
#include "storage/shmem.h"
3737
#include "utils/hsearch.h"
3838

39+
#include "extensions/plpgsql.h"
3940

4041
/* signatures for PostgreSQL-specific library init/fini functions */
4142
typedef void (*PG_init_t) (void);
@@ -90,6 +91,26 @@ static char *find_in_dynamic_libpath(const char *basename);
9091
static const Pg_magic_struct magic_data = PG_MODULE_MAGIC_DATA;
9192

9293

94+
/* PGlite static handling of extensions */
95+
96+
bool plpgsql_loaded = false;
97+
98+
typedef struct {
99+
const char *name;
100+
void *function;
101+
} func_map;
102+
103+
static const func_map function_map[] = {
104+
{"plpgsql_call_handler", plpgsql_call_handler},
105+
{"plpgsql_inline_handler", plpgsql_inline_handler},
106+
{"plpgsql_validator", plpgsql_validator},
107+
{"pg_finfo_plpgsql_call_handler", pg_finfo_plpgsql_call_handler},
108+
{"pg_finfo_plpgsql_inline_handler", pg_finfo_plpgsql_inline_handler},
109+
{"pg_finfo_plpgsql_validator", pg_finfo_plpgsql_validator},
110+
{NULL, NULL}
111+
};
112+
113+
93114
/*
94115
* Load the specified dynamic-link library file, and look for a function
95116
* named funcname in it.
@@ -107,6 +128,24 @@ void *
107128
load_external_function(const char *filename, const char *funcname,
108129
bool signalNotFound, void **filehandle)
109130
{
131+
// printf("load_external_function - filename: %s, funcname: %s, signalNotFound: %d, filehandle: %p\n", filename, funcname, signalNotFound, filehandle);
132+
133+
/* PGlite static handling of extensions */
134+
if (strcmp(filename, "$libdir/plpgsql") == 0)
135+
{
136+
if (!plpgsql_loaded)
137+
{
138+
plpgsql_PG_init();
139+
plpgsql_loaded = true;
140+
}
141+
142+
for (int i = 0; function_map[i].name != NULL; i++) {
143+
if (strcmp(funcname, function_map[i].name) == 0) {
144+
return function_map[i].function;
145+
}
146+
}
147+
}
148+
110149
char *fullname;
111150
void *lib_handle;
112151
void *retval;
@@ -145,6 +184,19 @@ load_external_function(const char *filename, const char *funcname,
145184
void
146185
load_file(const char *filename, bool restricted)
147186
{
187+
// printf("load_file - filename: %s, restricted: %d\n", filename, restricted);
188+
189+
/* PGlite static handling of extensions */
190+
if (strcmp(filename, "$libdir/plpgsql") == 0)
191+
{
192+
if (!plpgsql_loaded)
193+
{
194+
plpgsql_PG_init();
195+
plpgsql_loaded = true;
196+
}
197+
return;
198+
}
199+
148200
char *fullname;
149201

150202
/* Apply security restriction if requested */
@@ -170,6 +222,15 @@ load_file(const char *filename, bool restricted)
170222
void *
171223
lookup_external_function(void *filehandle, const char *funcname)
172224
{
225+
// printf("lookup_external_function - filehandle: %p, funcname: %s\n", filehandle, funcname);
226+
227+
/* PGlite static handling of extensions */
228+
for (int i = 0; function_map[i].name != NULL; i++) {
229+
if (strcmp(funcname, function_map[i].name) == 0) {
230+
return function_map[i].function;
231+
}
232+
}
233+
173234
return dlsym(filehandle, funcname);
174235
}
175236

@@ -453,6 +514,7 @@ internal_unload_library(const char *libname)
453514
static bool
454515
file_exists(const char *name)
455516
{
517+
printf("file_exists - name: %s\n", name);
456518
struct stat st;
457519

458520
AssertArg(name != NULL);

src/include/extensions/plpgsql.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#include "postgres.h"
2+
3+
extern void plpgsql_PG_init(void);
4+
extern Datum plpgsql_call_handler(PG_FUNCTION_ARGS);
5+
extern Datum plpgsql_inline_handler(PG_FUNCTION_ARGS);
6+
extern Datum plpgsql_validator(PG_FUNCTION_ARGS);
7+
8+
#ifndef PLPGSQL_INFO_DEF
9+
extern Pg_finfo_record *pg_finfo_plpgsql_call_handler(void);
10+
extern Pg_finfo_record *pg_finfo_plpgsql_inline_handler(void);
11+
extern Pg_finfo_record *pg_finfo_plpgsql_validator(void);
12+
#endif

src/makefiles/Makefile.darwin

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ DLSUFFIX = .so
55
# env var name to use in place of LD_LIBRARY_PATH
66
ld_library_path_var = DYLD_LIBRARY_PATH
77

8-
ifdef PGXS
9-
BE_DLLLIBS = -bundle_loader $(bindir)/postgres
10-
else
11-
BE_DLLLIBS = -bundle_loader $(top_builddir)/src/backend/postgres
12-
endif
8+
# ifdef PGXS
9+
# BE_DLLLIBS = -bundle_loader $(bindir)/postgres
10+
# else
11+
# BE_DLLLIBS = -bundle_loader $(top_builddir)/src/backend/postgres
12+
# endif
1313

1414
# Rule for building a shared library from a single .o file
1515
%.so: %.o

src/pl/plpgsql/src/pl_handler.c

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@
2727
#include "utils/syscache.h"
2828
#include "utils/varlena.h"
2929

30+
#ifdef EMSCRIPTEN
31+
#include <emscripten.h>
32+
#endif
33+
3034
static bool plpgsql_extra_checks_check_hook(char **newvalue, void **extra, GucSource source);
3135
static void plpgsql_extra_warnings_assign_hook(const char *newvalue, void *extra);
3236
static void plpgsql_extra_errors_assign_hook(const char *newvalue, void *extra);
@@ -141,8 +145,8 @@ plpgsql_extra_errors_assign_hook(const char *newvalue, void *extra)
141145
*
142146
* DO NOT make this static nor change its name!
143147
*/
144-
void
145-
_PG_init(void)
148+
extern void EMSCRIPTEN_KEEPALIVE
149+
plpgsql_PG_init(void)
146150
{
147151
/* Be sure we do initialization only once (should be redundant now) */
148152
static bool inited = false;
@@ -218,7 +222,7 @@ _PG_init(void)
218222
*/
219223
PG_FUNCTION_INFO_V1(plpgsql_call_handler);
220224

221-
Datum
225+
extern Datum EMSCRIPTEN_KEEPALIVE
222226
plpgsql_call_handler(PG_FUNCTION_ARGS)
223227
{
224228
bool nonatomic;
@@ -311,7 +315,7 @@ plpgsql_call_handler(PG_FUNCTION_ARGS)
311315
*/
312316
PG_FUNCTION_INFO_V1(plpgsql_inline_handler);
313317

314-
Datum
318+
extern Datum EMSCRIPTEN_KEEPALIVE
315319
plpgsql_inline_handler(PG_FUNCTION_ARGS)
316320
{
317321
LOCAL_FCINFO(fake_fcinfo, 0);
@@ -438,7 +442,7 @@ plpgsql_inline_handler(PG_FUNCTION_ARGS)
438442
*/
439443
PG_FUNCTION_INFO_V1(plpgsql_validator);
440444

441-
Datum
445+
extern Datum EMSCRIPTEN_KEEPALIVE
442446
plpgsql_validator(PG_FUNCTION_ARGS)
443447
{
444448
Oid funcoid = PG_GETARG_OID(0);

src/pl/plpgsql/src/plpgsql.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
#include "utils/expandedrecord.h"
2424
#include "utils/typcache.h"
2525

26+
#define PLPGSQL_INFO_DEF
27+
#include "extensions/plpgsql.h"
2628

2729
/**********************************************************************
2830
* Definitions

0 commit comments

Comments
 (0)