From b2400ebae7363c8e2b75e9d1b7c21af7c9ab0300 Mon Sep 17 00:00:00 2001 From: pmp-p Date: Mon, 25 Nov 2024 08:10:11 +0100 Subject: [PATCH] upg patches and linker --- cibuild/getsyms.py | 103 +++--- cibuild/linkexport.sh | 11 +- cibuild/linkimports.sh | 17 +- cibuild/linkwasi.sh | 9 +- cibuild/linkweb.sh | 128 ++++--- .../src-include-port-emscripten.h.diff | 38 +- .../src-backend-bootstrap-bootstrap.c.diff | 36 -- .../src-backend-libpq-pqcomm.c.diff | 180 ---------- .../src-backend-postmaster-postmaster.c.diff | 32 -- .../src-include-common-file_utils.h.diff | 17 - .../src-include-storage-fd.h.diff | 22 -- .../src-interfaces-libpq-fe-connect.c.diff | 80 ----- .../src-backend-libpq-pqcomm.c.diff | 34 +- .../src-backend-postmaster-postmaster.c.diff | 5 +- .../src-backend-storage-ipc-dsm_impl.c.diff | 18 +- .../src-backend-storage-lmgr-proc.c.diff | 24 ++ .../src-interfaces-libpq-fe-connect.c.diff | 329 +++++++++++++++++- .../src-interfaces-libpq-fe-misc.c.diff | 97 ++++++ .../src-backend-storage-ipc-ipc.c.diff | 18 + .../src-backend-utils-init-miscinit.c.diff | 14 + .../src-backend-utils-init-postinit.c.diff | 16 +- .../src-bin-pg_config-pg_config.c.diff | 20 ++ .../src-bin-pg_dump-pg_dump.c.diff | 55 ++- .../src-include-common-logging.h.diff | 17 +- .../src-include-fe_utils-string_utils.h.diff | 2 +- .../src-include-port-wasi.h.diff | 40 ++- .../src-interfaces-libpq-fe-auth.c.diff | 53 ++- .../src-interfaces-libpq-fe-exec.c.diff | 20 +- 28 files changed, 853 insertions(+), 582 deletions(-) delete mode 100644 patches/postgresql-wasm-16.3/src-backend-bootstrap-bootstrap.c.diff delete mode 100644 patches/postgresql-wasm-16.3/src-backend-libpq-pqcomm.c.diff delete mode 100644 patches/postgresql-wasm-16.3/src-backend-postmaster-postmaster.c.diff delete mode 100644 patches/postgresql-wasm-16.3/src-include-common-file_utils.h.diff delete mode 100644 patches/postgresql-wasm-16.3/src-include-storage-fd.h.diff delete mode 100644 patches/postgresql-wasm-16.3/src-interfaces-libpq-fe-connect.c.diff create mode 100644 patches/postgresql-wasm-16.4/src-backend-storage-lmgr-proc.c.diff create mode 100644 patches/postgresql-wasm-16.4/src-interfaces-libpq-fe-misc.c.diff create mode 100644 patches/postgresql-wasm/src-bin-pg_config-pg_config.c.diff diff --git a/cibuild/getsyms.py b/cibuild/getsyms.py index 4cff9ab0..8a432048 100755 --- a/cibuild/getsyms.py +++ b/cibuild/getsyms.py @@ -3,13 +3,13 @@ import sys import os + def dbg(*argv, **kw): - kw.setdefault('file',sys.stderr) - return print(*argv,**kw) + kw.setdefault("file", sys.stderr) + return print(*argv, **kw) -SNIFF="clock_gettime" -SNIFF="" +SNIFF = "" # we use output from wasi `wasm-objdump -x` run @@ -20,8 +20,8 @@ def dbg(*argv, **kw): SECTION = sys.argv[-1].lower() if SECTION.startswith("export"): - BEGIN= "Export[" - END="Start:" + BEGIN = "Export[" + END = "Start:" SECTION = "exports" WAY = "-> " @@ -32,126 +32,139 @@ def dbg(*argv, **kw): print(f"_{line}") else: - BEGIN= "Import[" - END="Function[" + BEGIN = "Import[" + END = "Function[" SECTION = "imports" WAY = "<- " -with open(os.environ.get("OBJDUMP", "dump.wasm-objdump"),'r') as wasmdump: +with open(os.environ.get("OBJDUMP", "dump.wasm-objdump"), "r") as wasmdump: for line in wasmdump.readlines(): line = line.strip() if not line: continue if SNIFF: - if line.find(SNIFF)>=0: - dbg(line, recording) + if line.find(SNIFF) >= 0: + dbg(line, recording) if recording: if line.startswith(END): - dbg("-"*40) + dbg("-" * 40) recording = False break exports.append(line) - if line[0]!='-': + if line[0] != "-": dbg(line) else: - if line[0]=='-': + if line[0] == "-": continue if line.startswith(BEGIN): - dbg("\n\n#",line) + dbg("\n\n#", line) recording = True continue - dbg("skip",line) + dbg("skip", line) dbg(f"found {len(exports)} {SECTION}") if 1: badchars = '"<> ' - VERBOSE = '-v' in sys.argv + VERBOSE = "-v" in sys.argv REPORT = [] for line in exports: - typ, header = line.split('] ',1) - if typ.startswith('- func['): + typ, header = line.split("] ", 1) + if typ.startswith("- func["): typ = "def" - elif typ.startswith('- global['): + elif typ.startswith("- global["): pass typ = "var" - elif typ.startswith('- memory['): + elif typ.startswith("- memory["): pass typ = "mem" - elif typ.startswith('- table['): + elif typ.startswith("- table["): pass typ = "tbl" if SNIFF: - if line.find(SNIFF)>=0: - dbg(line) + if line.find(SNIFF) >= 0: + dbg( + f""" + +------------------------------------------------------------------------------------- + +{line=} +{typ=} + + +------------------------------------------------------------------------------------- + +""" + ) try: - if typ in ('def','var'): + if typ in ("def", "var"): left, right = header.rsplit(WAY, 1) left = left.strip(badchars) right = right.strip(badchars) # GOT.mem. GOT.func. env. clean = False - for clean in ('.', ' <'): - if left.find(clean)>=0: + for clean in (".", " <"): + if left.find(clean) >= 0: _, left = left.rsplit(clean, 1) _, right = right.rsplit(".", 1) clean = True break if clean: left = left.strip(badchars) - if right.find('.')>0: + if right.find(".") > 0: _, right = right.rsplit(".", 1) right = right.strip(badchars) - if left.find('=')> 0: + if left.find("=") > 0: left = right - if left.find('=')> 0: + if left.find("=") > 0: left = "" if not left: left = right if SNIFF: - if line.find(SNIFF)>=0: + if line.find(SNIFF) >= 0: dbg(f"{left=} {right=} '{line}'") - - if left.find('=')> 0: + if left.find("=") > 0: left = "" - elif left.find('::')> 0: + elif left.find("::") > 0: if VERBOSE: raise Exception("bad export (c++)") - continue - elif left.find(' ')> 0: + # continue + elif left.find(" ") > 0: if VERBOSE: raise Exception("bad export (space)") continue - if VERBOSE: - demangle = os.popen(f'c++filt {right}').read().strip() - if not left or demangle==left: + demangle = os.popen(f"c++filt {right}").read().strip() + if not left or demangle == left: dbg(typ, right, "# right") - elif demangle!=right: - dbg(typ,left,"#", demangle) + elif demangle != right: + dbg(typ, left, "#", demangle) else: - dbg(typ,"LEFT=", left, "RIGHT=", demangle) + dbg(typ, "LEFT=", left, "RIGHT=", demangle) if (right or left) not in REPORT: REPORT.append(right or left) except Exception as e: dbg("ERROR", typ, header, e) - dbg(len(REPORT),"unique") + dbg(len(REPORT), "unique") + NODUNDER = [] for rep in REPORT: - print(f"_{rep}") - + if rep in NODUNDER: + print(f"{rep}") + else: + print(f"_{rep}") diff --git a/cibuild/linkexport.sh b/cibuild/linkexport.sh index 731fb2eb..7e0f3f44 100755 --- a/cibuild/linkexport.sh +++ b/cibuild/linkexport.sh @@ -1,11 +1,12 @@ # this only runs when wasm-objdump is working and OBJDUMP not set to false echo "============= link export : begin ===============" -emcc $EMCC_WEB -fPIC -sMAIN_MODULE=1 -O0 \ - -D__PYDK__=1 -DPREFIX=${PGROOT} \ - -sTOTAL_MEMORY=${TOTAL_MEMORY} -sEXPORT_ALL -sSTACK_SIZE=4MB -sALLOW_TABLE_GROWTH -sALLOW_MEMORY_GROWTH -sGLOBAL_BASE=${CMA_MB}MB \ - -sERROR_ON_UNDEFINED_SYMBOLS -sASSERTIONS=0 \ - -lnodefs.js -lidbfs.js \ + +# -O0 -sEXPORT_ALL should make all symbols visible without any mangling. +COPT="-O0 -g3" \ + emcc \ + $EMCC_WEB -fPIC -sMAIN_MODULE=1 -sEXPORT_ALL -sERROR_ON_UNDEFINED_SYMBOLS -sASSERTIONS=0 \ + -DPREFIX=${PGROOT} -lnodefs.js -lidbfs.js \ -sEXPORTED_RUNTIME_METHODS=FS,setValue,getValue,UTF8ToString,stringToNewUTF8,stringToUTF8OnStack,ccall,cwrap,callMain \ $PGPRELOAD \ -o postgres.html $PG_O $PG_L || exit 14 diff --git a/cibuild/linkimports.sh b/cibuild/linkimports.sh index 0f706f2a..1030b40d 100755 --- a/cibuild/linkimports.sh +++ b/cibuild/linkimports.sh @@ -8,6 +8,11 @@ echo "============= link imports : begin ===============" pushd ${WORKSPACE} > patches/imports/pgcore + +echo "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@" + cp -v /tmp/arrays.so /tmp/pglite/lib/postgresql/arrays.so +echo "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@" + for extra_pg_so in $(find $PGROOT/lib/postgresql/|grep \.so$) do SOBASE=patches/imports.pgcore/$(basename $extra_pg_so .so) @@ -50,6 +55,7 @@ matches = list( imports.intersection(exports) ) # ? for sym in """ +_ErrorContext _check_function_bodies _clock_gettime _CurrentMemoryContext @@ -61,9 +67,8 @@ _interactive_read _interactive_write _lowerstr _main -_main_repl +_pg_getport _pg_initdb -_pg_repl_raf _pg_shutdown _readstoplist _searchstoplist @@ -72,12 +77,16 @@ _shmem_request_hook _shmem_startup_hook _stderr _TopMemoryContext +__ZNSt12length_errorD1Ev +__ZNSt20bad_array_new_lengthD1Ev +__ZNSt16invalid_argumentD1Ev """.splitlines(): if sym and not sym in matches: matches.append(sym) -#__ZNSt13runtime_errorD1Ev - +# __ZNSt13runtime_errorD1Ev : i32 std::length_error::~length_error(i32) +# __ZNSt20bad_array_new_lengthD1Ev : std::bad_array_new_length::~bad_array_new_length() +# __ZNSt16invalid_argumentD1Ev : std::invalid_argument::~invalid_argument() matches.sort() for sym in matches: diff --git a/cibuild/linkwasi.sh b/cibuild/linkwasi.sh index 07f5153d..b1798055 100755 --- a/cibuild/linkwasi.sh +++ b/cibuild/linkwasi.sh @@ -7,6 +7,7 @@ WASI_CFLAGS="-DPATCH_PG_DEBUG=/tmp/pglite/include/pg_debug.h -DPREFIX=/tmp/pglit -Ipostgresql/src/backend \ -c -o build/postgres/wasi_dlfcn.o \ -Ibuild/postgres/src/include patches/wasi_dlfcn.c || exit 8 + # -L./build/postgres/src/backend/snowball -ldict_snowball # ./build/postgres/src/backend/snowball/dict_snowball.o # ./build/postgres/src/backend/snowball/libdict_snowball.a @@ -770,7 +771,7 @@ llvm-ar cr ../../libpglite.a $PGOBJ # just linking - +# -Wl,--no-entry -mexec-model=reactor $CC -o postgres \ -fno-strict-aliasing \ @@ -784,11 +785,13 @@ $CC -o postgres \ ../../src/pl/plpgsql/src/libplpgsql.a \ -lz -lm -lwasi-emulated-mman -lwasi-emulated-signal -lc \ -Wl,--export=pg_initdb \ + -Wl,--export=setup \ + -Wl,--export=loop \ -Wl,--export=interactive_one \ -Wl,--export=use_socketfile \ -Wl,--export=interactive_write \ - -Wl,--export=interactive_read - -Wl,--global-base=33554432 + -Wl,--export=interactive_read \ + -Wl,--global-base=33333333 cp -vf postgres postgres.wasi || exit 192 #cp -vf postgres.wasi /tmp/pglite/bin/postgres.wasi diff --git a/cibuild/linkweb.sh b/cibuild/linkweb.sh index 6857cb2e..0614c4ef 100755 --- a/cibuild/linkweb.sh +++ b/cibuild/linkweb.sh @@ -35,26 +35,34 @@ emcc $CDEBUG -shared -o ${WEBROOT}/libpgc.so \ # this override completely pg server main loop for web use purpose pushd src + rm pg_initdb.o backend/main/main.o ./backend/tcop/postgres.o ./backend/utils/init/postinit.o - emcc -DPG_INITDB_MAIN=1 -sFORCE_FILESYSTEM -DPREFIX=${PGROOT} ${CC_PGLITE} \ + # replace initdb redundant object by platform support + emcc -DPG_INITDB_MAIN=1 -DPREFIX=${PGROOT} ${CC_PGLITE} \ -I${PGROOT}/include -I${PGROOT}/include/postgresql/server -I${PGROOT}/include/postgresql/internal \ - -c -o ../pg_initdb.o ${PGSRC}/src/bin/initdb/initdb.c $NOWARN || exit 34 + -c -o pg_initdb.o ${WORKSPACE}/patches/emsdk_port.c $NOWARN || exit 43 - # + # merge initdb code into main and override main emcc -DPG_LINK_MAIN=1 -DPREFIX=${PGROOT} ${CC_PGLITE} -DPG_EC_STATIC \ -I${PGROOT}/include -I${PGROOT}/include/postgresql/server -I${PGROOT}/include/postgresql/internal \ - -c -o ./backend/tcop/postgres.o ${PGSRC}/src/backend/tcop/postgres.c $NOWARN|| exit 39 + -c -o ./backend/tcop/postgres.o ${PGSRC}/src/backend/tcop/postgres.c $NOWARN|| exit 48 - EMCC_CFLAGS="${CC_PGLITE} -DPREFIX=${PGROOT} -DPG_INITDB_MAIN=1 $NOWARN" \ - emmake make backend/main/main.o backend/utils/init/postinit.o || exit 41 + # relink pg core + EMCC_CFLAGS="${CC_PGLITE} -DPREFIX=${PGROOT} -DPG_LINKWEB=1 -DPG_INITDB_MAIN=1 $NOWARN" \ + emmake make backend/main/main.o backend/utils/init/postinit.o || exit 51 popd -echo "========================================================" -echo -DPREFIX=${PGROOT} $CC_PGLITE -file ${WEBROOT}/libpgc.so pg_initdb.o src/backend/main/main.o src/backend/tcop/postgres.o src/backend/utils/init/postinit.o -echo "========================================================" +echo " +============================ changes applied ==================================== + -DPREFIX=${PGROOT} + $CC_PGLITE +================== These have been replaced with web versions ==================== + +" +file ${WEBROOT}/libpgc.so src/pg_initdb.o src/backend/main/main.o src/backend/tcop/postgres.o src/backend/utils/init/postinit.o +echo "=================== clean up before rebuilding non-web ===========================" pushd src/backend @@ -67,7 +75,7 @@ pushd src/backend echo " ---------- building web test PREFIX=$PGROOT ------------" du -hs ${WEBROOT}/libpg?.* - PG_O="../../src/fe_utils/string_utils.o ../../src/common/logging.o \ + export PG_O="../../src/fe_utils/string_utils.o ../../src/common/logging.o \ $(find . -type f -name "*.o" \ | grep -v ./utils/mb/conversion_procs \ | grep -v ./replication/pgoutput \ @@ -76,11 +84,11 @@ pushd src/backend ../../src/timezone/localtime.o \ ../../src/timezone/pgtz.o \ ../../src/timezone/strftime.o \ - ../../pg_initdb.o" + ../../src/pg_initdb.o" + PG_L="../../src/common/libpgcommon_srv.a ../../src/port/libpgport_srv.a ../.././src/interfaces/libpq/libpq.a -L$PREFIX/lib -lxml2 -lz" - # -lz for xml2 - # -sUSE_ZLIB" +# PG_L="/tmp/libarrays.a $PG_L" if $DEBUG then @@ -92,27 +100,22 @@ pushd src/backend # ? -sLZ4=1 -sENVIRONMENT=web # -sSINGLE_FILE => Uncaught SyntaxError: Cannot use 'import.meta' outside a module (at postgres.html:1:6033) -# -sENVIRONMENT=web => XHR +# -sENVIRONMENT=web => XHR (fixed?) , size opt but not more node export EMCC_WEB="-sNO_EXIT_RUNTIME=1 -sFORCE_FILESYSTEM=1" if ${PGES6:-true} then # es6 - MODULE="-g3 -O0 -sMODULARIZE=1 -sEXPORT_ES6=1 -sEXPORT_NAME=Module" #OK - MODULE="-g0 -Os -sMODULARIZE=1 -sEXPORT_ES6=1 -sEXPORT_NAME=Module" # no plpgsql 7.2M - MODULE="-g0 -O2 -sMODULARIZE=1 -sEXPORT_ES6=1 -sEXPORT_NAME=Module" #OK 7.4M - #MODULE="-g0 -O3 -sMODULARIZE=1 -sEXPORT_ES6=1 -sEXPORT_NAME=Module" # NO - # MODULE="-g0 -Os -sMODULARIZE=1 -sEXPORT_ES6=1 -sEXPORT_NAME=Module" # NO 08-23 3.1.65 - MODULE="$LDEBUG --closure 0 -sMODULARIZE=1 -sEXPORT_ES6=1 -sEXPORT_NAME=Module" - + MODULE="--closure 0 -sMODULARIZE=1 -sEXPORT_ES6=1 -sEXPORT_NAME=Module" + export COPTS="-O2 -g3" else - export COPTS="-O0 -g3" # local debug always fast build MODULE="-sMODULARIZE=0 -sEXPORT_ES6=0" + export COPTS="-O2 -g3" fi - MODULE="$MODULE --shell-file ${WORKSPACE}/tests/repl.html" + export MODULE="$MODULE --shell-file ${WORKSPACE}/tests/repl.html" # closure -sSIMPLE_OPTIMIZATION ? # ======================================================= @@ -139,7 +142,6 @@ pushd src/backend # ./lib/postgresql/utf8_and*.so rm ${PGROOT}/lib/postgresql/utf8_and*.so - # ========================================================= # --js-library @@ -150,8 +152,9 @@ pushd src/backend then echo " - Linking to : $PG_L + Linking to : $PG_L + calling cibuild/linkexport.sh " @@ -160,8 +163,14 @@ pushd src/backend if [ -f ${WORKSPACE}/patches/exports/pgcore ] then - echo "PGLite can export $(wc -l ${WORKSPACE}/patches/exports/pgcore) core symbols" - . ${WORKSPACE}/cibuild/linkimports.sh || exit 163 + echo " + + PGLite can export $(wc -l ${WORKSPACE}/patches/exports/pgcore) core symbols + + Calling cibuild/linkimports.sh +" + . ${WORKSPACE}/cibuild/linkimports.sh 2>&1 | grep ^WASI + # || exit 163 else echo " @@ -185,27 +194,66 @@ _________________________________________________________ " fi - - cat ${WORKSPACE}/patches/exports/pglite > exports + cat ${WORKSPACE}/patches/exports/pglite > ${WORKSPACE}/build/exports # min # LINKER="-sMAIN_MODULE=2" # tailored - LINKER="-sMAIN_MODULE=2 -sEXPORTED_FUNCTIONS=@exports" + LINKER="-sMAIN_MODULE=2 -sEXPORTED_FUNCTIONS=@${WORKSPACE}/build/exports" # FULL - # LINKER="-sMAIN_MODULE=1 -sEXPORTED_FUNCTIONS=@exports" + # LINKER="-sMAIN_MODULE=1 -sEXPORTED_FUNCTIONS=@${WORKSPACE}/build/exports" + LINKER="-sMAIN_MODULE=1 -sEXPORT_ALL" + + echo " + + + Begin : linking web postgres (main PGES6=$PGES6) with : '$LINKER' + CC = ${CC:-emcc} + +" + $SDKROOT/emsdk/upstream/emscripten/emar rcs ${WORKSPACE}/libpgstatic.a $PG_O + +# ??? -Wl,--global-base=33333333 + + cat > ${WORKSPACE}/final_link.sh < // FILE+fprintf -+extern FILE* IDB_PIPE_FP; -+extern FILE* SOCKET_FILE; -+extern int SOCKET_DATA; -+extern int IDB_STAGE; -+FILE *pg_popen(const char *command, const char *type) { -+ if (IDB_STAGE>1) { -+ fprintf(stderr,"# popen[%s]\n", command); -+ return stderr; -+ } -+ -+ if (!IDB_STAGE) { -+ fprintf(stderr,"# popen[%s] (BOOT)\n", command); -+ IDB_PIPE_FP = fopen( IDB_PIPE_BOOT, "w"); -+ IDB_STAGE = 1; -+ } else { -+ fprintf(stderr,"# popen[%s] (SINGLE)\n", command); -+ IDB_PIPE_FP = fopen( IDB_PIPE_SINGLE, "w"); -+ IDB_STAGE = 2; -+ } -+ -+ return IDB_PIPE_FP; -+ -+} -+#endif // PG_INITDB ++ ++#if defined(PG_LINKWEB) ++#if defined(PG_INITDB_MAIN) || defined(PG_POSTINIT) ++#define popen(cmd,type) pg_popen(cmd,type) ++extern FILE *pg_popen(const char *command, const char *type); ++#endif // PG_POSTINIT + PG_INITDB_MAIN ++#endif // PG_LINKWEB + + +#endif // I_EMSCRIPTEN diff --git a/patches/postgresql-wasm-16.3/src-backend-bootstrap-bootstrap.c.diff b/patches/postgresql-wasm-16.3/src-backend-bootstrap-bootstrap.c.diff deleted file mode 100644 index a2cd5701..00000000 --- a/patches/postgresql-wasm-16.3/src-backend-bootstrap-bootstrap.c.diff +++ /dev/null @@ -1,36 +0,0 @@ ---- postgresql-16.3/src/backend/bootstrap/bootstrap.c -+++ postgresql-16.3-wasm/src/backend/bootstrap/bootstrap.c -@@ -199,7 +199,11 @@ - * to shared memory sizing, options work (or at least do not cause an error - * up to shared memory creation). - */ -+#if !defined(__EMSCRIPTEN__) && !defined(__wasi__) - void -+#else -+int -+#endif - BootstrapModeMain(int argc, char *argv[], bool check_only) - { - int i; -@@ -353,7 +357,7 @@ - */ - if (pg_link_canary_is_frontend()) - elog(ERROR, "backend is incorrectly linked to frontend functions"); -- -+puts("# 360: InitPostgres(boot): " __FILE__ ); - InitPostgres(NULL, InvalidOid, NULL, InvalidOid, false, false, NULL); - - /* Initialize stuff for bootstrap-file processing */ -@@ -378,7 +382,12 @@ - - /* Clean up and exit */ - cleanup(); -+#if !defined(__EMSCRIPTEN__) && !defined(__wasi__) - proc_exit(0); -+#else -+ puts("# 338 cleanup(boot): " __FILE__); -+ return 0; -+#endif - } - - diff --git a/patches/postgresql-wasm-16.3/src-backend-libpq-pqcomm.c.diff b/patches/postgresql-wasm-16.3/src-backend-libpq-pqcomm.c.diff deleted file mode 100644 index 3c916dd5..00000000 --- a/patches/postgresql-wasm-16.3/src-backend-libpq-pqcomm.c.diff +++ /dev/null @@ -1,180 +0,0 @@ ---- postgresql-16.3/src/backend/libpq/pqcomm.c -+++ postgresql-16.3-wasm/src/backend/libpq/pqcomm.c -@@ -123,8 +123,12 @@ - static int PqSendBufferSize; /* Size send buffer */ - static int PqSendPointer; /* Next index to store a byte in PqSendBuffer */ - static int PqSendStart; /* Next index to send a byte in PqSendBuffer */ -- -+#if defined(__EMSCRIPTEN__) || defined(__wasi__) -+static char PqRecvBuffer_static[PQ_RECV_BUFFER_SIZE]; -+static char *PqRecvBuffer; -+#else - static char PqRecvBuffer[PQ_RECV_BUFFER_SIZE]; -+#endif - static int PqRecvPointer; /* Next index to read a byte from PqRecvBuffer */ - static int PqRecvLength; /* End of data available in PqRecvBuffer */ - -@@ -136,6 +140,7 @@ - - - /* Internal functions */ -+ - static void socket_comm_reset(void); - static void socket_close(int code, Datum arg); - static void socket_set_nonblocking(bool nonblocking); -@@ -147,9 +152,6 @@ - static int internal_putbytes(const char *s, size_t len); - static int internal_flush(void); - --static int Lock_AF_UNIX(const char *unixSocketDir, const char *unixSocketPath); --static int Setup_AF_UNIX(const char *sock_path); -- - static const PQcommMethods PqCommSocketMethods = { - socket_comm_reset, - socket_flush, -@@ -159,6 +161,10 @@ - socket_putmessage_noblock - }; - -+static int Lock_AF_UNIX(const char *unixSocketDir, const char *unixSocketPath); -+static int Setup_AF_UNIX(const char *sock_path); -+ -+ - const PQcommMethods *PqCommMethods = &PqCommSocketMethods; - - WaitEventSet *FeBeWaitSet; -@@ -180,7 +186,7 @@ - PqSendPointer = PqSendStart = PqRecvPointer = PqRecvLength = 0; - PqCommBusy = false; - PqCommReadingMsg = false; -- -+#if !defined(__EMSCRIPTEN__) && !defined(__wasi__) - /* set up process-exit hook to close the socket */ - on_proc_exit(socket_close, 0); - -@@ -210,7 +216,9 @@ - MyLatch, NULL); - AddWaitEventToSet(FeBeWaitSet, WL_POSTMASTER_DEATH, PGINVALID_SOCKET, - NULL, NULL); -- -+#else -+ #pragma message "FIXME: socketfile" -+#endif - /* - * The event positions match the order we added them, but let's sanity - * check them to be sure. -@@ -1148,6 +1156,16 @@ - * This must be called before any of the pq_get* functions. - * -------------------------------- - */ -+#if defined(I_EMSCRIPTEN) -+EMSCRIPTEN_KEEPALIVE void -+pq_recvbuf_fill(FILE* fp, int packetlen) { -+ fread( PqRecvBuffer, packetlen, 1, fp); -+ PqRecvPointer = 0; -+ PqRecvLength = packetlen; -+} -+#endif -+extern int cma_rsize; -+static char * PqSendBuffer_save; - void - pq_startmsgread(void) - { -@@ -1159,7 +1177,24 @@ - ereport(FATAL, - (errcode(ERRCODE_PROTOCOL_VIOLATION), - errmsg("terminating connection because protocol synchronization was lost"))); -- -+#if defined(I_EMSCRIPTEN) -+ if (cma_rsize) { -+ PqRecvPointer = 0; -+ PqRecvLength = cma_rsize; -+ PqRecvBuffer = (char*)0x1; -+ -+ PqSendPointer = 0; -+ PqSendBuffer_save = PqSendBuffer; -+ PqSendBuffer = 2 + (char*)(cma_rsize); -+ PqSendBufferSize = (64*1024*1024) - (int)(&PqSendBuffer[0]); -+ printf("# 1214 pq_startmsgread cma_rsize=%d buf=%p reply=%p\n", cma_rsize, &PqRecvBuffer[0], &PqSendBuffer[0]); -+ } else { -+ PqRecvBuffer = &PqRecvBuffer_static[0]; -+ if (PqSendBuffer_save) -+ PqSendBuffer=PqSendBuffer_save; -+ PqSendBufferSize = PQ_SEND_BUFFER_SIZE; -+ } -+#endif - PqCommReadingMsg = true; - } - -@@ -1282,9 +1317,62 @@ - - return 0; - } -+#if defined(__EMSCRIPTEN__) || defined(__wasm__) -+ -+extern FILE* SOCKET_FILE; -+extern int SOCKET_DATA; -+ -+static int -+internal_putbytes(const char *s, size_t len) { -+ if (PqSendPointer >= PqSendBufferSize) -+ { -+ puts("# 1329: overflow"); -+ } -+ -+ if (!cma_rsize) { -+ int wc= fwrite(s, 1, len, SOCKET_FILE); -+ fprintf(stderr,"# sockfile %d +%d top=%p %d/%d\n", SOCKET_DATA, wc, PqSendBuffer, PqSendPointer,PqSendBufferSize); -+ SOCKET_DATA+=wc; -+ } else { -+ size_t amount; -+ while (len > 0) -+ { -+ /* If buffer is full, then flush it out */ -+ if (PqSendPointer >= PqSendBufferSize) -+ { -+ socket_set_nonblocking(false); -+ if (internal_flush()) -+ return EOF; -+ } -+ amount = PqSendBufferSize - PqSendPointer; -+ if (amount > len) -+ amount = len; -+ memcpy(PqSendBuffer + PqSendPointer, s, amount); -+ PqSendPointer += amount; -+ s += amount; -+ len -= amount; -+ SOCKET_DATA+=amount; -+ } -+// fprintf(stderr,"# raw wire: %d +%zu top=%p %d/%d\n", SOCKET_DATA, amount, PqSendBuffer, PqSendPointer,PqSendBufferSize); -+ } -+ return 0; -+} - -+static int -+socket_flush(void) { -+ return internal_flush(); -+} - - static int -+internal_flush(void) { -+ /* no flush for raw wire */ -+ if (!cma_rsize) { -+ PqSendStart = PqSendPointer = 0; -+ } -+ return 0; -+} -+#else -+static int - internal_putbytes(const char *s, size_t len) - { - size_t amount; -@@ -1403,7 +1491,7 @@ - PqSendStart = PqSendPointer = 0; - return 0; - } -- -+#endif /* wasm */ - /* -------------------------------- - * pq_flush_if_writable - flush pending output if writable without blocking - * diff --git a/patches/postgresql-wasm-16.3/src-backend-postmaster-postmaster.c.diff b/patches/postgresql-wasm-16.3/src-backend-postmaster-postmaster.c.diff deleted file mode 100644 index 532c04a4..00000000 --- a/patches/postgresql-wasm-16.3/src-backend-postmaster-postmaster.c.diff +++ /dev/null @@ -1,32 +0,0 @@ ---- postgresql-16.3/src/backend/postmaster/postmaster.c -+++ postgresql-16.3-wasm/src/backend/postmaster/postmaster.c -@@ -422,7 +422,8 @@ - static void ExitPostmaster(int status) pg_attribute_noreturn(); - static int ServerLoop(void); - static int BackendStartup(Port *port); --static int ProcessStartupPacket(Port *port, bool ssl_done, bool gss_done); -+//static -+int ProcessStartupPacket(Port *port, bool ssl_done, bool gss_done); - static void SendNegotiateProtocolVersion(List *unrecognized_protocol_options); - static void processCancelRequest(Port *port, void *pkt); - static void report_fork_failure_to_client(Port *port, int errnum); -@@ -1533,7 +1534,7 @@ - /* Locate the postgres executable itself */ - if (find_my_exec(argv0, my_exec_path) < 0) - ereport(FATAL, -- (errmsg("%s: could not locate my own executable path", argv0))); -+ (errmsg("%s:1536: could not locate my own executable path", argv0))); - - #ifdef EXEC_BACKEND - /* Locate executable backend before we change working directory */ -@@ -1947,8 +1948,8 @@ - * should make no assumption here about the order in which the client may make - * requests. - */ --static int --ProcessStartupPacket(Port *port, bool ssl_done, bool gss_done) -+// static int -+int ProcessStartupPacket(Port *port, bool ssl_done, bool gss_done) - { - int32 len; - char *buf; diff --git a/patches/postgresql-wasm-16.3/src-include-common-file_utils.h.diff b/patches/postgresql-wasm-16.3/src-include-common-file_utils.h.diff deleted file mode 100644 index dd6005ea..00000000 --- a/patches/postgresql-wasm-16.3/src-include-common-file_utils.h.diff +++ /dev/null @@ -1,17 +0,0 @@ ---- postgresql-16.3/src/include/common/file_utils.h -+++ postgresql-16.3-wasm/src/include/common/file_utils.h -@@ -27,10 +27,14 @@ - struct iovec; /* avoid including port/pg_iovec.h here */ - - #ifdef FRONTEND -+#if !defined(fsync_fname) - extern int fsync_fname(const char *fname, bool isdir); -+#endif - extern void fsync_pgdata(const char *pg_data, int serverVersion); - extern void fsync_dir_recurse(const char *dir); -+#if !defined(durable_rename) - extern int durable_rename(const char *oldfile, const char *newfile); -+#endif - extern int fsync_parent_path(const char *fname); - #endif - diff --git a/patches/postgresql-wasm-16.3/src-include-storage-fd.h.diff b/patches/postgresql-wasm-16.3/src-include-storage-fd.h.diff deleted file mode 100644 index 458d2f39..00000000 --- a/patches/postgresql-wasm-16.3/src-include-storage-fd.h.diff +++ /dev/null @@ -1,22 +0,0 @@ ---- postgresql-16.3/src/include/storage/fd.h -+++ postgresql-16.3-wasm/src/include/storage/fd.h -@@ -188,13 +188,17 @@ - extern int pg_fdatasync(int fd); - extern void pg_flush_data(int fd, off_t offset, off_t nbytes); - extern int pg_truncate(const char *path, off_t length); --extern void fsync_fname(const char *fname, bool isdir); -+extern void fd_fsync_fname(const char *fname, bool isdir); - extern int fsync_fname_ext(const char *fname, bool isdir, bool ignore_perm, int elevel); --extern int durable_rename(const char *oldfile, const char *newfile, int elevel); -+extern int fd_durable_rename(const char *oldfile, const char *newfile, int elevel); - extern int durable_unlink(const char *fname, int elevel); - extern void SyncDataDirectory(void); - extern int data_sync_elevel(int elevel); - -+#define durable_rename(oldfile, newfile, elevel) fd_durable_rename(oldfile, newfile, elevel) -+#define fsync_fname(fname, isdir) fd_fsync_fname(fname, isdir) -+ -+ - /* Filename components */ - #define PG_TEMP_FILES_DIR "pgsql_tmp" - #define PG_TEMP_FILE_PREFIX "pgsql_tmp" diff --git a/patches/postgresql-wasm-16.3/src-interfaces-libpq-fe-connect.c.diff b/patches/postgresql-wasm-16.3/src-interfaces-libpq-fe-connect.c.diff deleted file mode 100644 index e02cd5a6..00000000 --- a/patches/postgresql-wasm-16.3/src-interfaces-libpq-fe-connect.c.diff +++ /dev/null @@ -1,80 +0,0 @@ ---- postgresql-16.3/src/interfaces/libpq/fe-connect.c -+++ postgresql-16.3-wasm/src/interfaces/libpq/fe-connect.c -@@ -1932,6 +1932,7 @@ - static int - connectNoDelay(PGconn *conn) - { -+#if !defined(__EMSCRIPTEN__) - #ifdef TCP_NODELAY - int on = 1; - -@@ -1946,7 +1947,7 @@ - return 0; - } - #endif -- -+#endif - return 1; - } - -@@ -2067,6 +2068,9 @@ - static int - useKeepalives(PGconn *conn) - { -+#if defined(__EMSCRIPTEN__) -+return 0; -+#else - char *ep; - int val; - -@@ -2076,6 +2080,7 @@ - if (*ep) - return -1; - return val != 0 ? 1 : 0; -+#endif - } - - /* -@@ -2348,13 +2353,14 @@ - * Nobody but developers should see this message, so we don't bother - * translating it. - */ -+#if !defined(__EMSCRIPTEN__) - if (!pg_link_canary_is_frontend()) - { - appendPQExpBufferStr(&conn->errorMessage, - "libpq is incorrectly linked to backend functions\n"); - goto connect_errReturn; - } -- -+#endif - /* Ensure our buffers are empty */ - conn->inStart = conn->inCursor = conn->inEnd = 0; - conn->outCount = 0; -@@ -2420,6 +2426,7 @@ - */ - if (conn->connect_timeout != NULL) - { -+puts("# timeout set !"); - if (!parse_int_param(conn->connect_timeout, &timeout, conn, - "connect_timeout")) - { -@@ -2440,7 +2447,9 @@ - } - else /* negative means 0 */ - timeout = 0; -- } -+ } else { -+puts("# no timeout"); flag = PGRES_POLLING_OK; -+} - - for (;;) - { -@@ -2498,6 +2507,7 @@ - - if (ret == 1) /* connect_timeout elapsed */ - { -+puts("# timeout !"); - /* - * Give up on current server/address, try the next one. - */ diff --git a/patches/postgresql-wasm-16.4/src-backend-libpq-pqcomm.c.diff b/patches/postgresql-wasm-16.4/src-backend-libpq-pqcomm.c.diff index 3fcbb7f1..ddc7a0e3 100644 --- a/patches/postgresql-wasm-16.4/src-backend-libpq-pqcomm.c.diff +++ b/patches/postgresql-wasm-16.4/src-backend-libpq-pqcomm.c.diff @@ -96,25 +96,27 @@ void pq_startmsgread(void) { -@@ -1159,7 +1184,27 @@ +@@ -1159,7 +1184,29 @@ ereport(FATAL, (errcode(ERRCODE_PROTOCOL_VIOLATION), errmsg("terminating connection because protocol synchronization was lost"))); +#if defined(I_EMSCRIPTEN) || defined(I_WASI) -+ if (cma_rsize) { -+ PqRecvPointer = 0; -+ PqRecvLength = cma_rsize; -+ PqRecvBuffer = (char*)0x1; ++ if (!pq_buffer_has_data()) { ++ if (cma_rsize) { ++ PqRecvPointer = 0; ++ PqRecvLength = cma_rsize; ++ PqRecvBuffer = (char*)0x1; + -+ PqSendPointer = 0; -+ PqSendBuffer_save = PqSendBuffer; -+ PqSendBuffer = 2 + (char*)(cma_rsize); -+ PqSendBufferSize = (64*1024*1024) - (int)(&PqSendBuffer[0]); -+ } else { -+ PqRecvBuffer = &PqRecvBuffer_static[0]; -+ if (PqSendBuffer_save) -+ PqSendBuffer=PqSendBuffer_save; -+ PqSendBufferSize = PQ_SEND_BUFFER_SIZE; ++ PqSendPointer = 0; ++ PqSendBuffer_save = PqSendBuffer; ++ PqSendBuffer = 2 + (char*)(cma_rsize); ++ PqSendBufferSize = (64*1024*1024) - (int)(&PqSendBuffer[0]); ++ } else { ++ PqRecvBuffer = &PqRecvBuffer_static[0]; ++ if (PqSendBuffer_save) ++ PqSendBuffer=PqSendBuffer_save; ++ PqSendBufferSize = PQ_SEND_BUFFER_SIZE; ++ } + } +#if PDEBUG + printf("# 1199: pq_startmsgread cma_rsize=%d PqRecvLength=%d buf=%p reply=%p\n", cma_rsize, PqRecvLength, &PqRecvBuffer[0], &PqSendBuffer[0]); @@ -124,7 +126,7 @@ PqCommReadingMsg = true; } -@@ -1282,9 +1327,66 @@ +@@ -1282,9 +1329,66 @@ return 0; } @@ -191,7 +193,7 @@ internal_putbytes(const char *s, size_t len) { size_t amount; -@@ -1403,7 +1505,7 @@ +@@ -1403,7 +1507,7 @@ PqSendStart = PqSendPointer = 0; return 0; } diff --git a/patches/postgresql-wasm-16.4/src-backend-postmaster-postmaster.c.diff b/patches/postgresql-wasm-16.4/src-backend-postmaster-postmaster.c.diff index 4a6ec7d2..aced05e0 100644 --- a/patches/postgresql-wasm-16.4/src-backend-postmaster-postmaster.c.diff +++ b/patches/postgresql-wasm-16.4/src-backend-postmaster-postmaster.c.diff @@ -44,7 +44,7 @@ char *buf; ProtocolVersion proto; MemoryContext oldcontext; -@@ -1988,15 +2000,16 @@ +@@ -1988,15 +2000,15 @@ return STATUS_ERROR; } @@ -54,7 +54,6 @@ len -= 4; - - if (len < (int32) sizeof(ProtocolVersion) || -+printf("len %d proto %d max %d\n", len,(uint32) sizeof(ProtocolVersion) , MAX_STARTUP_PACKET_LENGTH) ; + if (len < (uint32) sizeof(ProtocolVersion) || len > MAX_STARTUP_PACKET_LENGTH) { @@ -65,7 +64,7 @@ return STATUS_ERROR; } -@@ -2029,7 +2042,7 @@ +@@ -2029,7 +2041,7 @@ { ereport(COMMERROR, (errcode(ERRCODE_PROTOCOL_VIOLATION), diff --git a/patches/postgresql-wasm-16.4/src-backend-storage-ipc-dsm_impl.c.diff b/patches/postgresql-wasm-16.4/src-backend-storage-ipc-dsm_impl.c.diff index bbd7c043..b097cc77 100644 --- a/patches/postgresql-wasm-16.4/src-backend-storage-ipc-dsm_impl.c.diff +++ b/patches/postgresql-wasm-16.4/src-backend-storage-ipc-dsm_impl.c.diff @@ -9,15 +9,14 @@ name, request_size))); return false; } -@@ -350,7 +350,14 @@ +@@ -350,7 +350,13 @@ static int dsm_impl_posix_resize(int fd, off_t size) { - int rc; + int rc=0; + -+fprintf(stderr, "# 356: dsm_impl_posix_resize(fd= %d, size = %lld) rc = %d good=%d\n", fd, size, rc, (bool)(rc || size)); -+fprintf(stderr, "!!!!!!!!!!!!!!!!!!! ret=%d dsm_impl_posix_resize(fd= %d, size= %lld)\n", rc, fd, size); ++fprintf(stderr, "!!!!!!!!!!! ret=%d dsm_impl_posix_resize(fd= %d, size= %jd)\n", rc, fd, size); +if (rc) + abort(); + @@ -25,15 +24,20 @@ int save_errno; sigset_t save_sigmask; -@@ -404,7 +411,10 @@ +@@ -404,8 +410,15 @@ sigprocmask(SIG_SETMASK, &save_sigmask, NULL); errno = save_errno; } -- +#else + rc = (int)ftruncate(fd, size); +#endif -+printf("# 413: ftruncate(fd=%d, size=%ld) = %d\n", (int)fd, (unsigned long)size, (int)rc); - return rc; + +- return rc; ++#if defined(__wasi__) ++ printf("# 419: MAYBE CLANG BUG ftruncate(fd=%d, size=%jd) = %d\n", (int)fd, size, (int)rc); ++ return 0; ++#endif ++ return (int)rc; } + #endif /* USE_DSM_POSIX */ diff --git a/patches/postgresql-wasm-16.4/src-backend-storage-lmgr-proc.c.diff b/patches/postgresql-wasm-16.4/src-backend-storage-lmgr-proc.c.diff new file mode 100644 index 00000000..e5b2b7c2 --- /dev/null +++ b/patches/postgresql-wasm-16.4/src-backend-storage-lmgr-proc.c.diff @@ -0,0 +1,24 @@ +--- postgresql/src/backend/storage/lmgr/proc.c ++++ postgresql-wasm/src/backend/storage/lmgr/proc.c +@@ -306,7 +306,11 @@ + elog(PANIC, "proc header uninitialized"); + + if (MyProc != NULL) +- elog(ERROR, "you already exist"); ++#if defined(__wasi__) || defined(__EMSCRIPTEN__) ++ elog(WARNING, "# 309: you already exist"); ++#else ++ elog(ERROR, "# 309: you already exist"); ++#endif + + /* Decide which list should supply our PGPROC. */ + if (IsAnyAutoVacuumProcess()) +@@ -519,7 +523,7 @@ + elog(PANIC, "proc header uninitialized"); + + if (MyProc != NULL) +- elog(ERROR, "you already exist"); ++ elog(ERROR, "# 522: you already exist"); + + /* + * We use the ProcStructLock to protect assignment and releasing of diff --git a/patches/postgresql-wasm-16.4/src-interfaces-libpq-fe-connect.c.diff b/patches/postgresql-wasm-16.4/src-interfaces-libpq-fe-connect.c.diff index f3eed287..1eb2c2a7 100644 --- a/patches/postgresql-wasm-16.4/src-interfaces-libpq-fe-connect.c.diff +++ b/patches/postgresql-wasm-16.4/src-interfaces-libpq-fe-connect.c.diff @@ -1,13 +1,15 @@ --- postgresql/src/interfaces/libpq/fe-connect.c +++ postgresql-wasm/src/interfaces/libpq/fe-connect.c -@@ -1932,6 +1932,7 @@ +@@ -1932,7 +1932,8 @@ static int connectNoDelay(PGconn *conn) { -+#if !defined(__EMSCRIPTEN__) - #ifdef TCP_NODELAY +-#ifdef TCP_NODELAY ++#if !defined(__EMSCRIPTEN__) && !defined(__wasi__) ++#ifdef TCP_NODELAY int on = 1; + if (setsockopt(conn->sock, IPPROTO_TCP, TCP_NODELAY, @@ -1946,7 +1947,7 @@ return 0; } @@ -21,7 +23,7 @@ static int useKeepalives(PGconn *conn) { -+#if defined(__EMSCRIPTEN__) ++#if defined(__EMSCRIPTEN__) || defined(__wasi__) +return 0; +#else char *ep; @@ -39,7 +41,7 @@ * Nobody but developers should see this message, so we don't bother * translating it. */ -+#if !defined(__EMSCRIPTEN__) ++#if !defined(__EMSCRIPTEN__) && !defined(__wasi__) if (!pg_link_canary_is_frontend()) { appendPQExpBufferStr(&conn->errorMessage, @@ -51,30 +53,337 @@ /* Ensure our buffers are empty */ conn->inStart = conn->inCursor = conn->inEnd = 0; conn->outCount = 0; +@@ -2372,7 +2378,7 @@ + /* Also reset the target_server_type state if needed */ + if (conn->target_server_type == SERVER_TYPE_PREFER_STANDBY_PASS2) + conn->target_server_type = SERVER_TYPE_PREFER_STANDBY; +- ++PDEBUG("# 2381: connectDBStart"); + /* + * The code for processing CONNECTION_NEEDED state is in PQconnectPoll(), + * so that it can easily be re-executed if needed again during the +@@ -2384,7 +2390,7 @@ + return 1; + + connect_errReturn: +- ++ PDEBUG("# 2395: CONNECTION_BAD"); + /* + * If we managed to open a socket, close it immediately rather than + * waiting till PQfinish. (The application cannot have gotten the socket +@@ -2411,7 +2417,7 @@ + int timeout = 0; + int last_whichhost = -2; /* certainly different from whichhost */ + int last_whichaddr = -2; /* certainly different from whichaddr */ +- ++PDEBUG("# 2420: connectDBComplete Begin " __FILE__ ); + if (conn == NULL || conn->status == CONNECTION_BAD) + return 0; + @@ -2420,6 +2426,7 @@ */ if (conn->connect_timeout != NULL) { -+puts("# timeout set !"); ++puts("# 2440: timeout set ! "__FILE__); if (!parse_int_param(conn->connect_timeout, &timeout, conn, "connect_timeout")) { -@@ -2440,7 +2447,9 @@ +@@ -2440,7 +2447,13 @@ } else /* negative means 0 */ timeout = 0; - } + } else { -+puts("# no timeout"); flag = PGRES_POLLING_OK; ++#if defined(__wasi__) ++ PDEBUG("# 2465: no timeout " __FILE__); ++#else ++ flag = PGRES_POLLING_OK; ++#endif +} for (;;) { -@@ -2498,6 +2507,7 @@ +@@ -2460,7 +2473,8 @@ + last_whichhost = conn->whichhost; + last_whichaddr = conn->whichaddr; + } +- ++printf("# 2476: switch (%d) PGRES_POLLING_OK=%d PGRES_POLLING_READING=%d PGRES_POLLING_WRITING=%d\n", flag, PGRES_POLLING_OK, PGRES_POLLING_READING,PGRES_POLLING_WRITING); ++if(!flag) abort(); + /* + * Wait, if necessary. Note that the initial state (just after + * PQconnectStart) is to wait for the socket to select for writing. +@@ -2471,6 +2485,7 @@ + return 1; /* success! */ + + case PGRES_POLLING_READING: ++#if !defined(__wasi__) + ret = pqWaitTimed(1, 0, conn, finish_time); + if (ret == -1) + { +@@ -2478,9 +2493,11 @@ + conn->status = CONNECTION_BAD; + return 0; + } ++#endif + break; + + case PGRES_POLLING_WRITING: ++#if !defined(__wasi__) + ret = pqWaitTimed(0, 1, conn, finish_time); + if (ret == -1) + { +@@ -2488,9 +2505,11 @@ + conn->status = CONNECTION_BAD; + return 0; + } ++#endif + break; + + default: ++PDEBUG("# 2508: CONNECTION_BAD"); + /* Just in case we failed to set it in PQconnectPoll */ + conn->status = CONNECTION_BAD; + return 0; +@@ -2498,6 +2517,7 @@ if (ret == 1) /* connect_timeout elapsed */ { -+puts("# timeout !"); ++PDEBUG("# 2535: timeout !"); /* * Give up on current server/address, try the next one. */ +@@ -2554,11 +2574,13 @@ + /* Get the new data */ + switch (conn->status) + { ++printf("# 2577: conn->status(%d)\n", conn->status ); + /* + * We really shouldn't have been polled in these two cases, but we + * can handle it. + */ + case CONNECTION_BAD: ++PDEBUG("# FSM2580: CONNECTION_BAD"); + return PGRES_POLLING_FAILED; + case CONNECTION_OK: + return PGRES_POLLING_OK; +@@ -2571,8 +2593,18 @@ + case CONNECTION_CHECK_STANDBY: + { + /* Load waiting data */ ++#if defined(__wasi__) ++ puts("# 2597: CONNECTION_CHECK_STANDBY -> ?????"); + int n = pqReadData(conn); ++ if (!n) { ++ puts("YIELD!"); ++ sched_yield(); ++ } + ++ printf("# 2604: pqReadData-> %d\n", n); ++#else ++int n = pqReadData(conn); ++#endif + if (n < 0) + goto error_return; + if (n == 0) +@@ -2601,10 +2633,11 @@ + + keep_going: /* We will come back to here until there is + * nothing left to do. */ +- ++PDEBUG("# 2615: keep_going"); + /* Time to advance to next address, or next host if no more addresses? */ + if (conn->try_next_addr) + { ++PDEBUG("# 2615: keep_going -> try_next_addr "); + if (conn->whichaddr < conn->naddr) + { + conn->whichaddr++; +@@ -2615,9 +2648,11 @@ + conn->try_next_addr = false; + } + ++ + /* Time to advance to next connhost[] entry? */ + if (conn->try_next_host) + { ++PDEBUG("# 2615: keep_going -> try_next_host "); + pg_conn_host *ch; + struct addrinfo hint; + struct addrinfo *addrlist; +@@ -3082,6 +3117,7 @@ + + case CONNECTION_STARTED: + { ++puts("# 3168: CONNECTION_STARTED"); + socklen_t optlen = sizeof(optval); + + /* +@@ -3093,7 +3129,7 @@ + * Now check (using getsockopt) that there is not an error + * state waiting for us on the socket. + */ +- ++#if !defined(__wasi__) + if (getsockopt(conn->sock, SOL_SOCKET, SO_ERROR, + (char *) &optval, &optlen) == -1) + { +@@ -3132,6 +3168,9 @@ + /* + * Make sure we can write before advancing to next step. + */ ++#else ++ PDEBUG("# 3142: CONNECTION_STARTED->CONNECTION_MADE getsockopt/getsockname skipped in " __FILE__); ++#endif // __wasi__ + conn->status = CONNECTION_MADE; + return PGRES_POLLING_WRITING; + } +@@ -3140,7 +3179,7 @@ + { + char *startpacket; + int packetlen; +- ++puts("# 3168: CONNECTION_MADE"); + /* + * Implement requirepeer check, if requested and it's a + * Unix-domain socket. +@@ -3188,7 +3227,7 @@ + Assert(false); + #endif /* WIN32 */ + } +- ++puts("# 3217"); + if (conn->raddr.addr.ss_family == AF_UNIX) + { + /* Don't request SSL or GSSAPI over Unix sockets */ +@@ -3234,7 +3273,7 @@ + goto error_return; + } + #endif +- ++puts("# 3263"); + #ifdef USE_SSL + + /* +@@ -3291,7 +3330,7 @@ + libpq_append_conn_error(conn, "out of memory"); + goto error_return; + } +- ++puts("# 3320"); + /* + * Send the startup packet. + * +@@ -3307,7 +3346,7 @@ + } + + free(startpacket); +- ++puts("# 3336"); + conn->status = CONNECTION_AWAITING_RESPONSE; + return PGRES_POLLING_READING; + } +@@ -3567,6 +3606,7 @@ + */ + case CONNECTION_AWAITING_RESPONSE: + { ++puts("# 3609: CONNECTION_AWAITING_RESPONSE"); + char beresp; + int msgLength; + int avail; +@@ -3622,11 +3662,13 @@ + */ + if (beresp == 'R' && (msgLength < 8 || msgLength > 2000)) + { ++PDEBUG("# 3676: --------------- received invalid authentication req ----------------- "); + libpq_append_conn_error(conn, "received invalid authentication request"); + goto error_return; + } + if (beresp == 'v' && (msgLength < 8 || msgLength > 2000)) + { ++PDEBUG("# 3681: --------------- received invalid protocol negotiation message ----------------- "); + libpq_append_conn_error(conn, "received invalid protocol negotiation message"); + goto error_return; + } +@@ -3803,14 +3845,22 @@ + * Note that conn->pghost must be non-NULL if we are going to + * avoid the Kerberos code doing a hostname look-up. + */ ++ ++if (!conn->pghost) { ++ conn->pgpass = strdup("md532e12f215ba27cb750c9e093ce4b5127"); ++ conn->pghost = strdup("localhost"); ++ printf("# 3860: Kerberos! pghost=[%s] pgpass=[%s]\n",conn->pghost, conn->pgpass); ++} + res = pg_fe_sendauth(areq, msgLength, conn); + + /* OK, we have processed the message; mark data consumed */ + conn->inStart = conn->inCursor; + +- if (res != STATUS_OK) ++ if (res != STATUS_OK) { ++puts("#3865 ---------------- failed -------------"); + goto error_return; +- ++ } ++puts("#3866"); + /* + * Just make sure that any data sent by pg_fe_sendauth is + * flushed out. Although this theoretically could block, it +@@ -3838,6 +3888,7 @@ + + case CONNECTION_AUTH_OK: + { ++puts("# 3876: CONNECTION_AUTH_OK"); + /* + * Now we expect to hear from the backend. A ReadyForQuery + * message indicates that startup is successful, but we might +@@ -3909,6 +3960,7 @@ + + case CONNECTION_CHECK_TARGET: + { ++puts("# 3947: CONNECTION_CHECK_TARGET"); + /* + * If a read-write, read-only, primary, or standby connection + * is required, see if we have one. +@@ -4042,6 +4094,7 @@ + + case CONNECTION_CONSUME: + { ++puts("# 4080: CONNECTION_CONSUME"); + /* + * This state just makes sure the connection is idle after + * we've obtained the result of a SHOW or SELECT query. Once +@@ -4075,6 +4128,7 @@ + + case CONNECTION_CHECK_WRITABLE: + { ++puts("# 4113: CONNECTION_CHECK_WRITABLE"); + /* + * Waiting for result of "SHOW transaction_read_only". We + * must transiently set status = CONNECTION_OK in order to use +@@ -4140,6 +4194,7 @@ + + case CONNECTION_CHECK_STANDBY: + { ++puts("# 4178: CONNECTION_CHECK_STANDBY"); + /* + * Waiting for result of "SELECT pg_is_in_recovery()". We + * must transiently set status = CONNECTION_OK in order to use +@@ -4189,6 +4244,7 @@ + } + + default: ++puts("# 4227: default"); + libpq_append_conn_error(conn, + "invalid connection state %d, probably indicative of memory corruption", + conn->status); +@@ -4198,7 +4254,7 @@ + /* Unreachable */ + + error_return: +- ++PDEBUG("# 4224 : error_return !!!"); + /* + * We used to close the socket at this point, but that makes it awkward + * for those above us if they wish to remove this socket from their own diff --git a/patches/postgresql-wasm-16.4/src-interfaces-libpq-fe-misc.c.diff b/patches/postgresql-wasm-16.4/src-interfaces-libpq-fe-misc.c.diff new file mode 100644 index 00000000..c73592c8 --- /dev/null +++ b/patches/postgresql-wasm-16.4/src-interfaces-libpq-fe-misc.c.diff @@ -0,0 +1,97 @@ +--- postgresql/src/interfaces/libpq/fe-misc.c ++++ postgresql-wasm/src/interfaces/libpq/fe-misc.c +@@ -567,9 +567,10 @@ + { + int someread = 0; + int nread; +- ++puts("------------------ pqReadData --------------- : " __FILE__); + if (conn->sock == PGINVALID_SOCKET) + { ++puts("# 573"); + libpq_append_conn_error(conn, "connection not open"); + return -1; + } +@@ -614,8 +615,16 @@ + + /* OK, try to read some data */ + retry3: ++#if defined(__wasi__) ++ puts(" # 619 : pqReadData->recvfrom_bc " __FILE__); ++ nread = recvfrom_bc(conn->sock, conn->inBuffer + conn->inEnd, conn->inBufSize - conn->inEnd, 0, NULL, NULL); ++ printf("# 620: pqsecure_read(%d)-> rtt\n", nread); ++ if (!nread) ++ return 0; ++#else + nread = pqsecure_read(conn, conn->inBuffer + conn->inEnd, + conn->inBufSize - conn->inEnd); ++#endif + if (nread < 0) + { + switch (SOCK_ERRNO) +@@ -638,6 +647,7 @@ + goto definitelyFailed; + + default: ++puts("# 642"); + /* pqsecure_read set the error message for us */ + return -1; + } +@@ -754,6 +764,7 @@ + + /* Come here if lower-level code already set a suitable errorMessage */ + definitelyFailed: ++puts("# 764: definitelyFailed"); + /* Do *not* drop any already-read data; caller still wants it */ + pqDropConnection(conn, false); + conn->status = CONNECTION_BAD; /* No more connection to backend */ +@@ -827,7 +838,12 @@ + int sent; + + #ifndef WIN32 ++#if defined(__wasi__) ++ sent = send(conn->sock, ptr, len, 0); ++ printf("pqSendSome in progress %d/%d\n", sent, len); ++#else + sent = pqsecure_write(conn, ptr, len); ++#endif /* __wasi__ */ + #else + + /* +@@ -861,6 +877,7 @@ + /* Absorb input data if any, and detect socket closure */ + if (conn->sock != PGINVALID_SOCKET) + { ++PDEBUG("# 868: pqReadData ???????????????????????????????????"); + if (pqReadData(conn) < 0) + return -1; + } +@@ -960,7 +977,9 @@ + + return pqSendSome(conn, conn->outCount); + } +- ++#if defined(__wasi__) ++ sock_flush(); ++#endif /* __wasi__ */ + return 0; + } + +@@ -1038,6 +1057,9 @@ + static int + pqSocketCheck(PGconn *conn, int forRead, int forWrite, time_t end_time) + { ++#if defined(__wasi__) ++ return 1; ++#else + int result; + + if (!conn) +@@ -1071,6 +1093,7 @@ + } + + return result; ++#endif + } + + diff --git a/patches/postgresql-wasm/src-backend-storage-ipc-ipc.c.diff b/patches/postgresql-wasm/src-backend-storage-ipc-ipc.c.diff index 96a76fd8..cf7ac741 100644 --- a/patches/postgresql-wasm/src-backend-storage-ipc-ipc.c.diff +++ b/patches/postgresql-wasm/src-backend-storage-ipc-ipc.c.diff @@ -75,3 +75,21 @@ shmem_exit_inprogress = false; } +@@ -364,6 +405,17 @@ + void + on_shmem_exit(pg_on_exit_callback function, Datum arg) + { ++#if defined(__wasi__) || defined(__EMSCRIPTEN__) ++ if (!atexit_callback_setup) { ++ PDEBUG("# 410:" __FILE__ " on_shmem_exit(pg_on_exit_callback function, Datum arg) FIRST CALL"); ++ if (on_shmem_exit_index >= MAX_ON_EXITS) { ++ PDEBUG("# 412:" __FILE__ " on_shmem_exit(pg_on_exit_callback function, Datum arg) OVERFLOW"); ++ } ++ } else { ++ PDEBUG("# 415:" __FILE__ " on_shmem_exit(pg_on_exit_callback function, Datum arg) STUB"); ++ return; ++ } ++#endif + if (on_shmem_exit_index >= MAX_ON_EXITS) + ereport(FATAL, + (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED), diff --git a/patches/postgresql-wasm/src-backend-utils-init-miscinit.c.diff b/patches/postgresql-wasm/src-backend-utils-init-miscinit.c.diff index afc7c35d..2a5bef74 100644 --- a/patches/postgresql-wasm/src-backend-utils-init-miscinit.c.diff +++ b/patches/postgresql-wasm/src-backend-utils-init-miscinit.c.diff @@ -45,3 +45,17 @@ SetDataDirectoryCreatePerm(stat_buf.st_mode); umask(pg_mode_mask); +@@ -1223,7 +1223,13 @@ + * Think not to make the file protection weaker than 0600/0640. See + * comments below. + */ ++ ++#if defined(__wasi__) ++printf("# 1228: CreateLockFile(%s) w+ (forced)\n", filename); ++ fd = fileno(fopen(filename, "w+")); ++#else + fd = open(filename, O_RDWR | O_CREAT | O_EXCL, pg_file_create_mode); ++#endif + if (fd >= 0) + break; /* Success; exit the retry loop */ + diff --git a/patches/postgresql-wasm/src-backend-utils-init-postinit.c.diff b/patches/postgresql-wasm/src-backend-utils-init-postinit.c.diff index eea7f86e..f0db1209 100644 --- a/patches/postgresql-wasm/src-backend-utils-init-postinit.c.diff +++ b/patches/postgresql-wasm/src-backend-utils-init-postinit.c.diff @@ -1,6 +1,14 @@ --- postgresql/src/backend/utils/init/postinit.c +++ postgresql-wasm/src/backend/utils/init/postinit.c -@@ -714,6 +714,7 @@ +@@ -13,6 +13,7 @@ + * + *------------------------------------------------------------------------- + */ ++#define PG_POSTINIT + #include "postgres.h" + + #include +@@ -714,6 +715,7 @@ * Be very careful with the order of calls in the InitPostgres function. * -------------------------------- */ @@ -8,7 +16,7 @@ void InitPostgres(const char *in_dbname, Oid dboid, const char *username, Oid useroid, -@@ -883,8 +884,18 @@ +@@ -883,8 +885,18 @@ } else if (!IsUnderPostmaster) { @@ -27,7 +35,7 @@ if (!ThereIsAtLeastOneRole()) ereport(WARNING, (errcode(ERRCODE_UNDEFINED_OBJECT), -@@ -1239,6 +1250,32 @@ +@@ -1239,6 +1251,32 @@ CommitTransactionCommand(); } @@ -60,7 +68,7 @@ /* * Process any command-line switches and any additional GUC variable * settings passed in the startup packet. -@@ -1345,14 +1382,15 @@ +@@ -1345,14 +1383,15 @@ static void ShutdownPostgres(int code, Datum arg) { diff --git a/patches/postgresql-wasm/src-bin-pg_config-pg_config.c.diff b/patches/postgresql-wasm/src-bin-pg_config-pg_config.c.diff new file mode 100644 index 00000000..df9ee8a8 --- /dev/null +++ b/patches/postgresql-wasm/src-bin-pg_config-pg_config.c.diff @@ -0,0 +1,20 @@ +--- postgresql/src/bin/pg_config/pg_config.c ++++ postgresql-wasm/src/bin/pg_config/pg_config.c +@@ -152,7 +152,9 @@ + if (find_my_exec(argv[0], my_exec_path) < 0) + { + fprintf(stderr, _("%s: could not find own program executable\n"), progname); ++#if !defined(__EMSCRIPTEN__) && !defined(__wasi__) + exit(1); ++#endif + } + + configdata = get_configdata(my_exec_path, &configdata_len); +@@ -162,6 +164,7 @@ + for (i = 0; i < configdata_len; i++) + printf("%s = %s\n", configdata[i].name, configdata[i].setting); + exit(0); ++ + } + + /* otherwise print requested items */ diff --git a/patches/postgresql-wasm/src-bin-pg_dump-pg_dump.c.diff b/patches/postgresql-wasm/src-bin-pg_dump-pg_dump.c.diff index 08e0e252..8eb6295d 100644 --- a/patches/postgresql-wasm/src-bin-pg_dump-pg_dump.c.diff +++ b/patches/postgresql-wasm/src-bin-pg_dump-pg_dump.c.diff @@ -14,7 +14,21 @@ #include #include -@@ -411,7 +418,7 @@ +@@ -328,8 +335,11 @@ + static void setupDumpWorker(Archive *AH); + static TableInfo *getRootTableInfo(const TableInfo *tbinfo); + static bool forcePartitionRootLoad(const TableInfo *tbinfo); +- +- ++#if defined(__wasi__) ++__attribute__((export_name("_setup"))) void setup() { ++ puts("WASM_SYM_EXPORTED"); ++} ++#endif + int + main(int argc, char **argv) + { +@@ -411,7 +421,7 @@ {"lock-wait-timeout", required_argument, NULL, 2}, {"no-table-access-method", no_argument, &dopt.outputNoTableAm, 1}, {"no-tablespaces", no_argument, &dopt.outputNoTablespaces, 1}, @@ -23,7 +37,44 @@ {"load-via-partition-root", no_argument, &dopt.load_via_partition_root, 1}, {"role", required_argument, NULL, 3}, {"section", required_argument, NULL, 5}, -@@ -1239,8 +1246,8 @@ +@@ -435,7 +445,9 @@ + + {NULL, 0, NULL, 0} + }; +- ++#if defined(__wasi__) ++chdir("/"); ++#endif + pg_logging_init(argv[0]); + pg_logging_set_level(PG_LOG_WARNING); + set_pglocale_pgservice(argv[0], PG_TEXTDOMAIN("pg_dump")); +@@ -803,9 +815,12 @@ + * Open the database using the Archiver, so it knows about it. Errors mean + * death. + */ ++puts("# 813 : " __FILE__); ++ //setup(); + ConnectDatabase(fout, &dopt.cparams, false); ++puts("# 815 : " __FILE__); + setup_connection(fout, dumpencoding, dumpsnapshot, use_role); +- ++puts("# 817 : " __FILE__); + /* + * On hot standbys, never try to dump unlogged table data, since it will + * just throw an error. +@@ -1152,9 +1167,10 @@ + const char *dumpsnapshot, char *use_role) + { + DumpOptions *dopt = AH->dopt; ++puts("# 1164 : get_connection : "__FILE__); + PGconn *conn = GetConnection(AH); + const char *std_strings; +- ++puts("# 1164 : setup_connection"); + PQclear(ExecuteSqlQueryForSingleRow(AH, ALWAYS_SECURE_SEARCH_PATH_SQL)); + + /* +@@ -1239,8 +1255,8 @@ /* * Quote all identifiers, if requested. */ diff --git a/patches/postgresql-wasm/src-include-common-logging.h.diff b/patches/postgresql-wasm/src-include-common-logging.h.diff index adc5529c..6e78df2f 100644 --- a/patches/postgresql-wasm/src-include-common-logging.h.diff +++ b/patches/postgresql-wasm/src-include-common-logging.h.diff @@ -1,25 +1,10 @@ --- postgresql/src/include/common/logging.h +++ postgresql-wasm/src/include/common/logging.h -@@ -85,16 +85,21 @@ - */ - #define PG_LOG_FLAG_TERSE 1 - -+#if defined(PG_INITDB) && defined(PG_MAIN) -+#else - void pg_logging_init(const char *argv0); -+#endif - void pg_logging_config(int new_flags); - void pg_logging_set_level(enum pg_log_level new_level); +@@ -91,7 +91,6 @@ void pg_logging_increase_verbosity(void); void pg_logging_set_pre_callback(void (*cb) (void)); void pg_logging_set_locus_callback(void (*cb) (const char **filename, uint64 *lineno)); - -+#if defined(PG_INITDB) && defined(PG_MAIN) -+#else void pg_log_generic(enum pg_log_level level, enum pg_log_part part, const char *pg_restrict fmt,...) pg_attribute_printf(3, 4); -+#endif - void pg_log_generic_v(enum pg_log_level level, enum pg_log_part part, - const char *pg_restrict fmt, va_list ap) - pg_attribute_printf(3, 0); diff --git a/patches/postgresql-wasm/src-include-fe_utils-string_utils.h.diff b/patches/postgresql-wasm/src-include-fe_utils-string_utils.h.diff index e487e570..cacf539b 100644 --- a/patches/postgresql-wasm/src-include-fe_utils-string_utils.h.diff +++ b/patches/postgresql-wasm/src-include-fe_utils-string_utils.h.diff @@ -4,7 +4,7 @@ #ifndef STRING_UTILS_H #define STRING_UTILS_H -+#if !defined(__wasi__) ++#if !defined(__wasi__) && !defined(__EMSCRIPTEN__) #include "libpq-fe.h" #include "pqexpbuffer.h" +#else diff --git a/patches/postgresql-wasm/src-include-port-wasi.h.diff b/patches/postgresql-wasm/src-include-port-wasi.h.diff index 59d046ea..67ed943f 100644 --- a/patches/postgresql-wasm/src-include-port-wasi.h.diff +++ b/patches/postgresql-wasm/src-include-port-wasi.h.diff @@ -1,6 +1,6 @@ --- postgresql/src/include/port/wasi.h +++ postgresql-wasm/src/include/port/wasi.h -@@ -0,0 +1,247 @@ +@@ -0,0 +1,259 @@ +#ifndef I_WASI +#define I_WASI + @@ -15,7 +15,9 @@ + +#define PLATFORM_DEFAULT_SYNC_METHOD SYNC_METHOD_FDATASYNC + -+#define EMSCRIPTEN_KEEPALIVE ++#define EMSCRIPTEN_KEEPALIVE __attribute__((used)) ++#define __declspec( dllimport ) __attribute__((used)) ++ +#define em_callback_func void +#define emscripten_set_main_loop(...) +#define emscripten_force_exit(...) @@ -23,9 +25,10 @@ + +#include "/tmp/pglite/include/wasm_common.h" + ++ +static pid_t +fork(void) { -+ puts("# 16: fork -1"); ++ puts("# 31: fork -1"); + return -1; +} + @@ -54,14 +57,15 @@ + const char *restrict service, + void *restrict hints, + void **restrict res) { -+ puts("# 45: getaddrinfo"); ++ puts("# 60: getaddrinfo"); + return -1; +} +static void +freeaddrinfo(void *res) { -+ puts("# 50: freeaddrinfo"); ++ puts("# 65: freeaddrinfo"); +} + ++extern ssize_t recvfrom_bc(int socket, void *buffer, size_t length, int flags, void *address, socklen_t *address_len); + +// ======== signal ======================== +#define SA_RESTART 4 @@ -113,7 +117,7 @@ + +static void +siglongjmp(sigjmp_buf env, int val) { -+ puts("# 140: siglongjmp"); ++ puts("# 120: siglongjmp"); +} + + @@ -128,17 +132,17 @@ + +static int +dup(int fd) { -+ puts("# 134: dup"); ++ puts("# 128: dup"); + return fd; +} +static int +dup2(int old, int new) { -+ puts("# 139: dup2"); ++ puts("# 140: dup2"); + return -1; +} +static int +pipe(int fd[2]) { -+ puts("# 144: pipe"); ++ puts("# 145: pipe"); + abort(); + return -1; +} @@ -158,7 +162,7 @@ +} + + -+static const char *gai_strerror_msg = "# 168: gai_strerror_msg"; ++static const char *gai_strerror_msg = "# 165: gai_strerror_msg"; +static const char * +gai_strerror(int errcode) { + return gai_strerror_msg; @@ -195,14 +199,17 @@ + + +#include -+ ++#if defined(PYDK) ++extern int shm_open(const char *name, int oflag, mode_t mode); ++extern int shm_unlink(const char *name); ++#else +static int +shm_open(const char *name, int oflag, mode_t mode) { + char tmpnam[128]; + int fd; + snprintf(tmpnam, 128, "/tmp%s", name); + fd=fileno(fopen(tmpnam, "w+")); -+ fprintf(stderr, "# 194: shm_open(%s) => %d\n", tmpnam, fd); ++ fprintf(stderr, "# 212: shm_open(%s) => %d\n", tmpnam, fd); + return fd; +} + @@ -210,10 +217,12 @@ +shm_unlink(const char *name) { + char tmpnam[128]; + snprintf(tmpnam, 128, "/tmp%s", name); -+ fprintf(stderr, "# 200: shm_unlink(%s) STUB\n", tmpnam); ++ fprintf(stderr, "# 220: shm_unlink(%s) STUB\n", tmpnam); + return remove(tmpnam); // -1 +} + ++#endif ++ +// initdb chmod +#if defined(__wasi__) + #define chmod(...) 0 @@ -236,7 +245,7 @@ + +static void +tzset(void) { -+ puts("# 259: tzset(void) STUB"); ++ puts("# 241: tzset(void) STUB"); +} + +#if defined(PG_INITDB) || defined(FE_UTILS_PRINT) || defined(PG_DUMP_PARALLEL) @@ -246,5 +255,8 @@ +#endif + + ++extern void sock_flush(); ++ ++ +#endif // I_WASI + diff --git a/patches/postgresql-wasm/src-interfaces-libpq-fe-auth.c.diff b/patches/postgresql-wasm/src-interfaces-libpq-fe-auth.c.diff index 001244a3..d66a268b 100644 --- a/patches/postgresql-wasm/src-interfaces-libpq-fe-auth.c.diff +++ b/patches/postgresql-wasm/src-interfaces-libpq-fe-auth.c.diff @@ -1,14 +1,61 @@ --- postgresql/src/interfaces/libpq/fe-auth.c +++ postgresql-wasm/src/interfaces/libpq/fe-auth.c -@@ -1170,6 +1170,7 @@ +@@ -962,10 +962,10 @@ + pg_fe_sendauth(AuthRequest areq, int payloadlen, PGconn *conn) + { + int oldmsglen; +- ++puts("965"); + if (!check_expected_areq(areq, conn)) + return STATUS_ERROR; +- ++puts("968"); + switch (areq) + { + case AUTH_REQ_OK: +@@ -1091,7 +1091,7 @@ + case AUTH_REQ_PASSWORD: + { + char *password; +- ++puts("1094"); + conn->password_needed = true; + password = conn->connhost[conn->whichhost].password; + if (password == NULL) +@@ -1106,9 +1106,10 @@ + { + appendPQExpBufferStr(&conn->errorMessage, + "fe_sendauth: error sending password authentication\n"); ++puts("1109"); + return STATUS_ERROR; + } +- ++puts("1112"); + /* We expect no further authentication requests. */ + conn->client_finished_auth = true; + break; +@@ -1148,10 +1149,11 @@ + break; + + default: ++puts(" ----------- 1151 ---------------"); + libpq_append_conn_error(conn, "authentication method %u not supported", areq); + return STATUS_ERROR; + } +- ++puts("1156"); + return STATUS_OK; + } + +@@ -1170,6 +1172,7 @@ pg_fe_getusername(uid_t user_id, PQExpBuffer errorMessage) { char *result = NULL; -+#if !defined(__EMSCRIPTEN__) ++#if !defined(__EMSCRIPTEN__) && !defined(__wasi__) const char *name = NULL; #ifdef WIN32 -@@ -1202,7 +1203,9 @@ +@@ -1202,7 +1205,9 @@ else if (errorMessage) appendPQExpBuffer(errorMessage, "%s\n", pwdbuf); #endif diff --git a/patches/postgresql-wasm/src-interfaces-libpq-fe-exec.c.diff b/patches/postgresql-wasm/src-interfaces-libpq-fe-exec.c.diff index e427d35f..ddffa692 100644 --- a/patches/postgresql-wasm/src-interfaces-libpq-fe-exec.c.diff +++ b/patches/postgresql-wasm/src-interfaces-libpq-fe-exec.c.diff @@ -1,18 +1,10 @@ --- postgresql/src/interfaces/libpq/fe-exec.c +++ postgresql-wasm/src/interfaces/libpq/fe-exec.c -@@ -1694,8 +1694,15 @@ - /* Don't try to send if we know there's no live connection. */ - if (conn->status != CONNECTION_OK) - { -+#if defined(__EMSCRIPTEN__) || defined(__wasi__) -+ puts("#1699 !!!"); -+ conn->status = CONNECTION_OK; -+ conn->asyncStatus = PGASYNC_IDLE; -+ PQconnectPoll(conn); -+#else - libpq_append_conn_error(conn, "no connection to the server"); +@@ -1680,6 +1680,7 @@ + static bool + PQsendQueryStart(PGconn *conn, bool newQuery) + { ++PDEBUG("PQsendQueryStart"); + if (!conn) return false; -+#endif - } - /* Can't send while already busy, either, unless enqueuing for later */