Skip to content

Commit f5f1005

Browse files
tdrztudor
andauthored
Tdrz/backend pgdump (#52)
Use emscripten built pg_dump. --------- Co-authored-by: tudor <tudor@swisstch.com>
1 parent 774390f commit f5f1005

File tree

25 files changed

+159
-221
lines changed

25 files changed

+159
-221
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,10 @@ lib*.pc
4646
/dist
4747

4848
# emscripten build excludes
49+
*.js
50+
*.data
4951
*.cjs
52+
pg_config.mjs
5053
*.wasm
5154
pglite.data
5255
pglite.js

build-pglite.sh

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,25 @@ else
3737
echo "$CONFIG_STATUS exists and is newer than $REF_FILE. ./configure will NOT be run."
3838
fi
3939

40+
# we define here "all" emscripten flags in order to allow native builds (like libpglite)
41+
EXPORTED_RUNTIME_METHODS="addFunction,removeFunction,FS,MEMFS"
42+
PGLITE_EMSCRIPTEN_FLAGS="-sWASM_BIGINT \
43+
-sSUPPORT_LONGJMP=emscripten \
44+
-sFORCE_FILESYSTEM=1 \
45+
-sNO_EXIT_RUNTIME=0 -sENVIRONMENT=node,web,worker \
46+
-sMAIN_MODULE=2 -sMODULARIZE=1 -sEXPORT_ES6=1 \
47+
-sEXPORT_NAME=Module -sALLOW_TABLE_GROWTH -sALLOW_MEMORY_GROWTH \
48+
-sERROR_ON_UNDEFINED_SYMBOLS=0 \
49+
-sEXPORTED_RUNTIME_METHODS=$EXPORTED_RUNTIME_METHODS \
50+
-sTOTAL_MEMORY=32MB \
51+
--embed-file $(pwd)/other/PGPASSFILE@/home/web_user/.pgpass"
52+
4053
# Step 1: configure the project
4154
if [ "$RUN_CONFIGURE" = true ]; then
42-
LDFLAGS="-sWASM_BIGINT -sUSE_PTHREADS=0" CFLAGS="${PGLITE_CFLAGS} -sWASM_BIGINT -fpic -sENVIRONMENT=node,web,worker -sSUPPORT_LONGJMP=emscripten -Wno-declaration-after-statement -Wno-macro-redefined -Wno-unused-function -Wno-missing-prototypes -Wno-incompatible-pointer-types" emconfigure ./configure ac_cv_exeext=.cjs --disable-spinlocks --disable-largefile --without-llvm --without-pam --disable-largefile --with-openssl=no --without-readline --without-icu --with-includes=$INSTALL_PREFIX/include:$INSTALL_PREFIX/include/libxml2 --with-libraries=$INSTALL_PREFIX/lib --with-uuid=ossp --with-zlib --with-libxml --with-libxslt --with-template=emscripten --prefix=$INSTALL_FOLDER || { echo 'error: emconfigure failed' ; exit 11; }
55+
LDFLAGS="-sWASM_BIGINT -sUSE_PTHREADS=0" \
56+
LDFLAGS_SL="-sSIDE_MODULE=1" \
57+
LDFLAGS_EX=$PGLITE_EMSCRIPTEN_FLAGS \
58+
CFLAGS="${PGLITE_CFLAGS} -sWASM_BIGINT -fpic -sENVIRONMENT=node,web,worker -sSUPPORT_LONGJMP=emscripten -Wno-declaration-after-statement -Wno-macro-redefined -Wno-unused-function -Wno-missing-prototypes -Wno-incompatible-pointer-types" emconfigure ./configure ac_cv_exeext=.js --host aarch64-unknown-linux-gnu --disable-spinlocks --disable-largefile --without-llvm --without-pam --disable-largefile --with-openssl=no --without-readline --without-icu --with-includes=$INSTALL_PREFIX/include:$INSTALL_PREFIX/include/libxml2:$(pwd)/pglite/includes --with-libraries=$INSTALL_PREFIX/lib --with-uuid=ossp --with-zlib --with-libxml --with-libxslt --with-template=emscripten --prefix=$INSTALL_FOLDER || { echo 'error: emconfigure failed' ; exit 11; }
4359
else
4460
echo "Warning: configure has not been run because RUN_CONFIGURE=${RUN_CONFIGURE}"
4561
fi
@@ -49,7 +65,7 @@ emmake make PORTNAME=emscripten -j || { echo 'error: emmake make PORTNAME=emscri
4965
emmake make PORTNAME=emscripten install || { echo 'error: emmake make PORTNAME=emscripten install' ; exit 22; }
5066

5167
# Step 3.1: make all contrib extensions - do not install
52-
emmake make PORTNAME=emscripten LDFLAGS_SL="-sSIDE_MODULE=1" -C contrib/ -j || { echo 'error: emmake make PORTNAME=emscripten -C contrib/ -j' ; exit 31; }
68+
emmake make PORTNAME=emscripten -C contrib/ -j || { echo 'error: emmake make PORTNAME=emscripten -C contrib/ -j' ; exit 31; }
5369
# Step 3.2: make dist contrib extensions - this will create an archive for each extension
5470
emmake make PORTNAME=emscripten -C contrib/ dist || { echo 'error: emmake make PORTNAME=emscripten -C contrib/ dist' ; exit 32; }
5571
# the above will also create a file with the imports that each extension needs - we pass these as input in the next step for emscripten to keep alive
@@ -58,20 +74,18 @@ emmake make PORTNAME=emscripten -C contrib/ dist || { echo 'error: emmake make P
5874
SAVE_PATH=$PATH
5975
PATH=$PATH:$INSTALL_FOLDER/bin
6076
emmake make OPTFLAGS="" PORTNAME=emscripten -j -C pglite || { echo 'error: emmake make OPTFLAGS="" PORTNAME=emscripten -j -C pglite' ; exit 41; }
61-
emmake make OPTFLAGS="" PORTNAME=emscripten LDFLAGS_SL="-sSIDE_MODULE=1" -C pglite/ dist || { echo 'error: make OPTFLAGS="" PORTNAME=emscripten LDFLAGS_SL="-sSIDE_MODULE=1" -C pglite/ dist ' ; exit 42; }
77+
emmake make OPTFLAGS="" PORTNAME=emscripten -C pglite/ dist || { echo 'error: make OPTFLAGS="" PORTNAME=emscripten -C pglite/ dist ' ; exit 42; }
6278
PATH=$SAVE_PATH
6379

6480
# Step 5: make and install pglite
65-
# we define here "all" emscripten flags in order to allow native builds (like libpglite)
6681
EXPORTED_RUNTIME_METHODS="MEMFS,IDBFS,FS,setValue,getValue,UTF8ToString,stringToNewUTF8,stringToUTF8OnStack,addFunction,removeFunction"
6782
PGLITE_EMSCRIPTEN_FLAGS="-sWASM_BIGINT \
6883
-sSUPPORT_LONGJMP=emscripten \
6984
-sFORCE_FILESYSTEM=1 \
7085
-sNO_EXIT_RUNTIME=1 -sENVIRONMENT=node,web,worker \
7186
-sMAIN_MODULE=2 -sMODULARIZE=1 -sEXPORT_ES6=1 \
7287
-sEXPORT_NAME=Module -sALLOW_TABLE_GROWTH -sALLOW_MEMORY_GROWTH \
73-
-sERROR_ON_UNDEFINED_SYMBOLS=1 \
88+
-sERROR_ON_UNDEFINED_SYMBOLS=0 \
7489
-sEXPORTED_RUNTIME_METHODS=$EXPORTED_RUNTIME_METHODS"
75-
7690
# Building pglite itself needs to be the last step because of the PRELOAD_FILES parameter (a list of files and folders) need to be available.
7791
PGLITE_CFLAGS="$PGLITE_CFLAGS $PGLITE_EMSCRIPTEN_FLAGS" emmake make PORTNAME=emscripten -j -C src/backend/ install-pglite || { echo 'emmake make OPTFLAGS="" PORTNAME=emscripten -j -C pglite' ; exit 51; }

pglite-wasm/interactive_one.c

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,10 @@
11
#include <unistd.h> // access, unlink
22
#define PGL_LOOP
3-
#if defined(__wasi__)
4-
// volatile sigjmp_buf void*;
5-
#else
63
volatile sigjmp_buf local_sigjmp_buf;
7-
#endif
84

95
// track back how many ex raised in steps of the loop until sucessfull clear_error
106
volatile int canary_ex = 0;
117

12-
// read FROM JS
13-
// (i guess return number of bytes written)
14-
// ssize_t pglite_read(/* ignored */ int socket, void *buffer, size_t length,/* ignored */ int flags,/* ignored */ void *address,/* ignored */ socklen_t *address_len);
15-
//typedef ssize_t (*pglite_read_t)(/* ignored */ int socket, void *buffer, size_t length,/* ignored */ int flags,/* ignored */ void *address,/* ignored */ unsigned int *address_len);
16-
typedef ssize_t (*pglite_read_t)(void *buffer, size_t max_length);
17-
extern pglite_read_t pglite_read;
18-
19-
// write TO JS
20-
// (i guess return number of bytes read)
21-
// ssize_t pglite_write(/* ignored */ int sockfd, const void *buf, size_t len, /* ignored */ int flags);
22-
// typedef ssize_t (*pglite_write_t)(/* ignored */ int sockfd, const void *buf, size_t len, /* ignored */ int flags);
23-
typedef ssize_t (*pglite_write_t)(void *buffer, size_t length);
24-
extern pglite_write_t pglite_write;
25-
26-
__attribute__((export_name("set_read_write_cbs")))
27-
void
28-
set_read_write_cbs(pglite_read_t read_cb, pglite_write_t write_cb) {
29-
pglite_read = read_cb;
30-
pglite_write = write_cb;
31-
}
32-
338
extern void AbortTransaction(void);
349
extern void CleanupTransaction(void);
3510
extern void ClientAuthentication(Port *port);

pglite-wasm/pgl_mains.c

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -218,13 +218,9 @@ PDEBUG("# 164:" __FILE__);
218218
initStringInfo(&row_description_buf);
219219
MemoryContextSwitchTo(TopMemoryContext);
220220

221-
#if defined(__wasi__)
222-
puts("# 210: sjlj exception handler off in initdb-wasi");
223-
#else
224221
# define INITDB_SINGLE
225222
# include "pgl_sjlj.c"
226223
# undef INITDB_SINGLE
227-
#endif // sjlj
228224

229225
if (!ignore_till_sync)
230226
send_ready_for_query = true; /* initially, or after error */

pglite-wasm/pgl_sjlj.c

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
#if defined(__wasi__)
2-
PDEBUG("# 2:" __FILE__ ": sjlj exception handler off");
3-
#else
41
if (sigsetjmp(local_sigjmp_buf, 1) != 0)
52
{
63
#if !defined(INITDB_SINGLE)
@@ -67,5 +64,4 @@
6764
}
6865

6966
PG_exception_stack = &local_sigjmp_buf;
70-
#endif
7167

pglite-wasm/pgl_stubs.h

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,6 @@
11
#pragma once
22

3-
4-
// wasi only stubs
5-
#if defined(__wasi__)
6-
# undef PQEXPBUFFER_H
7-
# include "../src/interfaces/libpq/pqexpbuffer.h"
8-
9-
#else
10-
# include "../src/interfaces/libpq/pqexpbuffer.h"
11-
#endif
12-
3+
#include "../src/interfaces/libpq/pqexpbuffer.h"
134

145
// option_parse_int parse_sync_method
156
#include "../src/fe_utils/option_utils.c"

pglite/includes/pglite-comm.h

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
#if defined(__EMSCRIPTEN__)
2+
3+
#ifndef PGLITE_COMM_H
4+
#define PGLITE_COMM_H
5+
6+
#include <emscripten/emscripten.h>
7+
8+
volatile int querylen = 0;
9+
volatile FILE* queryfp = NULL;
10+
11+
// read FROM JS
12+
// (i guess return number of bytes written)
13+
// ssize_t pglite_read(/* ignored */ int socket, void *buffer, size_t length,/* ignored */ int flags,/* ignored */ void *address,/* ignored */ socklen_t *address_len);
14+
//typedef ssize_t (*pglite_read_t)(/* ignored */ int socket, void *buffer, size_t length,/* ignored */ int flags,/* ignored */ void *address,/* ignored */ unsigned int *address_len);
15+
typedef ssize_t (*pglite_read_t)(void *buffer, size_t max_length);
16+
pglite_read_t pglite_read;
17+
18+
// write TO JS
19+
// (i guess return number of bytes read)
20+
// ssize_t pglite_write(/* ignored */ int sockfd, const void *buf, size_t len, /* ignored */ int flags);
21+
// typedef ssize_t (*pglite_write_t)(/* ignored */ int sockfd, const void *buf, size_t len, /* ignored */ int flags);
22+
typedef ssize_t (*pglite_write_t)(void *buffer, size_t length);
23+
pglite_write_t pglite_write;
24+
25+
__attribute__((export_name("set_read_write_cbs")))
26+
void
27+
set_read_write_cbs(pglite_read_t read_cb, pglite_write_t write_cb) {
28+
pglite_read = read_cb;
29+
pglite_write = write_cb;
30+
}
31+
32+
int EMSCRIPTEN_KEEPALIVE fcntl(int __fd, int __cmd, ...) {
33+
// dummy
34+
return 0;
35+
}
36+
37+
int EMSCRIPTEN_KEEPALIVE setsockopt(int __fd, int __level, int __optname,
38+
const void *__optval, socklen_t __optlen) {
39+
// dummy
40+
return 0;
41+
}
42+
43+
int EMSCRIPTEN_KEEPALIVE getsockopt(int __fd, int __level, int __optname,
44+
void *__restrict __optval,
45+
socklen_t *__restrict __optlen) {
46+
// dummy
47+
return 0;
48+
}
49+
50+
int EMSCRIPTEN_KEEPALIVE getsockname(int __fd, struct sockaddr * __addr,
51+
socklen_t *__restrict __len) {
52+
// dummy
53+
return 0;
54+
}
55+
56+
ssize_t EMSCRIPTEN_KEEPALIVE
57+
recv(int __fd, void *__buf, size_t __n, int __flags) {
58+
ssize_t got = pglite_read(__buf, __n);
59+
return got;
60+
}
61+
62+
ssize_t EMSCRIPTEN_KEEPALIVE
63+
send(int __fd, const void *__buf, size_t __n, int __flags) {
64+
ssize_t wrote = pglite_write(__buf, __n);
65+
return wrote;
66+
}
67+
68+
int EMSCRIPTEN_KEEPALIVE
69+
connect(int socket, const struct sockaddr *address, socklen_t address_len) {
70+
// dummy
71+
return 0;
72+
}
73+
74+
struct pollfd {
75+
int fd; /* file descriptor */
76+
short events; /* requested events */
77+
short revents; /* returned events */
78+
};
79+
80+
int EMSCRIPTEN_KEEPALIVE
81+
poll(struct pollfd fds[], ssize_t nfds, int timeout) {
82+
// dummy
83+
return nfds;
84+
}
85+
86+
#endif
87+
88+
#endif // ifndef PGLITE_COMM_H

src/backend/libpq/auth.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1856,7 +1856,7 @@ auth_peer(hbaPort *port)
18561856
{
18571857
uid_t uid;
18581858
gid_t gid;
1859-
#if !defined(WIN32) && !defined(__wasi__)
1859+
#if !defined(WIN32)
18601860
struct passwd *pw;
18611861
int ret;
18621862
#endif
@@ -1875,7 +1875,7 @@ auth_peer(hbaPort *port)
18751875
return STATUS_ERROR;
18761876
}
18771877

1878-
#if !defined(WIN32) && !defined(__wasi__)
1878+
#if !defined(WIN32)
18791879
errno = 0; /* clear errno before call */
18801880
pw = getpwuid(uid);
18811881
if (!pw)

src/backend/libpq/be-fsstubs.c

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -150,12 +150,8 @@ be_lo_close(PG_FUNCTION_ARGS)
150150
*
151151
*****************************************************************************/
152152

153-
#if defined(__EMSCRIPTEN__) || defined(__wasi__)
154-
static int
155-
#else
156153
int
157-
#endif
158-
lo_read3(int fd, char *buf, int len)
154+
lo_read(int fd, char *buf, int len)
159155
{
160156
int status;
161157
LargeObjectDesc *lobj;
@@ -182,12 +178,8 @@ lo_read3(int fd, char *buf, int len)
182178
return status;
183179
}
184180

185-
#if defined(__EMSCRIPTEN__) || defined(__wasi__)
186-
static int
187-
#else
188181
int
189-
#endif
190-
lo_write3(int fd, const char *buf, int len)
182+
lo_write(int fd, const char *buf, int len)
191183
{
192184
int status;
193185
LargeObjectDesc *lobj;
@@ -198,7 +190,7 @@ lo_write3(int fd, const char *buf, int len)
198190
errmsg("invalid large-object descriptor: %d", fd)));
199191
lobj = cookies[fd];
200192

201-
/* see comment in lo_read3() */
193+
/* see comment in lo_read() */
202194
if ((lobj->flags & IFS_WRLOCK) == 0)
203195
ereport(ERROR,
204196
(errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
@@ -373,7 +365,7 @@ be_loread(PG_FUNCTION_ARGS)
373365
len = 0;
374366

375367
retval = (bytea *) palloc(VARHDRSZ + len);
376-
totalread = lo_read3(fd, VARDATA(retval), len);
368+
totalread = lo_read(fd, VARDATA(retval), len);
377369
SET_VARSIZE(retval, totalread + VARHDRSZ);
378370

379371
PG_RETURN_BYTEA_P(retval);
@@ -390,7 +382,7 @@ be_lowrite(PG_FUNCTION_ARGS)
390382
PreventCommandIfReadOnly("lowrite()");
391383

392384
bytestowrite = VARSIZE_ANY_EXHDR(wbuf);
393-
totalwritten = lo_write3(fd, VARDATA_ANY(wbuf), bytestowrite);
385+
totalwritten = lo_write(fd, VARDATA_ANY(wbuf), bytestowrite);
394386
PG_RETURN_INT32(totalwritten);
395387
}
396388

@@ -568,7 +560,7 @@ lo_truncate_internal(int32 fd, int64 len)
568560
errmsg("invalid large-object descriptor: %d", fd)));
569561
lobj = cookies[fd];
570562

571-
/* see comment in lo_read3() */
563+
/* see comment in lo_read() */
572564
if ((lobj->flags & IFS_WRLOCK) == 0)
573565
ereport(ERROR,
574566
(errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),

src/backend/libpq/be-secure.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@
3232
#include "tcop/tcopprot.h"
3333
#include "utils/wait_event.h"
3434

35+
#if defined(__EMSCRIPTEN__)
36+
#include "pglite-comm.h"
37+
#endif
38+
3539
char *ssl_library;
3640
char *ssl_cert_file;
3741
char *ssl_key_file;

0 commit comments

Comments
 (0)