diff --git a/patches-REL_17_4_WASM/postgresql-debug/configure.diff b/patches-REL_17_4_WASM/postgresql-debug/configure.diff deleted file mode 100644 index d2d620958ea73..0000000000000 --- a/patches-REL_17_4_WASM/postgresql-debug/configure.diff +++ /dev/null @@ -1,147 +0,0 @@ ---- REL_17_4/configure -+++ pglite-REL_17_4/configure -@@ -4328,7 +4328,7 @@ - ac_save_c_werror_flag=$ac_c_werror_flag - ac_c_werror_flag=yes - ac_cv_prog_cc_g=no -- CFLAGS="-g" -+ CFLAGS="-g2" - cat confdefs.h - <<_ACEOF >conftest.$ac_ext - /* end confdefs.h. */ - -@@ -4359,7 +4359,7 @@ - - else - ac_c_werror_flag=$ac_save_c_werror_flag -- CFLAGS="-g" -+ CFLAGS="-g2" - cat confdefs.h - <<_ACEOF >conftest.$ac_ext - /* end confdefs.h. */ - -@@ -4387,13 +4387,13 @@ - CFLAGS=$ac_save_CFLAGS - elif test $ac_cv_prog_cc_g = yes; then - if test "$GCC" = yes; then -- CFLAGS="-g -O2" -+ CFLAGS="-g2 -Os" - else -- CFLAGS="-g" -+ CFLAGS="-g2" - fi - else - if test "$GCC" = yes; then -- CFLAGS="-O2" -+ CFLAGS="-Os" - else - CFLAGS= - fi -@@ -4859,7 +4859,7 @@ - ac_save_cxx_werror_flag=$ac_cxx_werror_flag - ac_cxx_werror_flag=yes - ac_cv_prog_cxx_g=no -- CXXFLAGS="-g" -+ CXXFLAGS="-g2" - cat confdefs.h - <<_ACEOF >conftest.$ac_ext - /* end confdefs.h. */ - -@@ -4890,7 +4890,7 @@ - - else - ac_cxx_werror_flag=$ac_save_cxx_werror_flag -- CXXFLAGS="-g" -+ CXXFLAGS="-g2" - cat confdefs.h - <<_ACEOF >conftest.$ac_ext - /* end confdefs.h. */ - -@@ -4918,13 +4918,13 @@ - CXXFLAGS=$ac_save_CXXFLAGS - elif test $ac_cv_prog_cxx_g = yes; then - if test "$GXX" = yes; then -- CXXFLAGS="-g -O2" -+ CXXFLAGS="-g2 -Os" - else -- CXXFLAGS="-g" -+ CXXFLAGS="-g2" - fi - else - if test "$GXX" = yes; then -- CXXFLAGS="-O2" -+ CXXFLAGS="-Os" - else - CXXFLAGS= - fi -@@ -5284,11 +5284,11 @@ - elif test "$enable_coverage" = yes; then - : # no optimization by default - elif test "$GCC" = yes; then -- CFLAGS="-O2" -+ CFLAGS="-Os" - else - # if the user selected debug mode, don't use -O - if test "$enable_debug" != yes; then -- CFLAGS="-O" -+ CFLAGS="-Os" - fi - fi - -@@ -5299,11 +5299,11 @@ - elif test "$enable_coverage" = yes; then - : # no optimization by default - elif test "$GCC" = yes; then -- CXXFLAGS="-O2" -+ CXXFLAGS="-Os" - else - # if the user selected debug mode, don't use -O - if test "$enable_debug" != yes; then -- CXXFLAGS="-O" -+ CXXFLAGS="-Os" - fi - fi - -@@ -5316,12 +5316,12 @@ - if test "$ac_env_BITCODE_CFLAGS_set" = set; then - BITCODE_CFLAGS=$ac_env_BITCODE_CFLAGS_value - else -- BITCODE_CFLAGS="-O2 $BITCODE_CFLAGS" -+ BITCODE_CFLAGS="-Os $BITCODE_CFLAGS" - fi - if test "$ac_env_BITCODE_CXXFLAGS_set" = set; then - BITCODE_CXXFLAGS=$ac_env_BITCODE_CXXFLAGS_value - else -- BITCODE_CXXFLAGS="-O2 $BITCODE_CXXFLAGS" -+ BITCODE_CXXFLAGS="-Os $BITCODE_CXXFLAGS" - fi - - # C[XX]FLAGS we determined above will be added back at the end -@@ -7656,11 +7656,11 @@ - - # supply -g if --enable-debug - if test "$enable_debug" = yes && test "$ac_cv_prog_cc_g" = yes; then -- CFLAGS="$CFLAGS -g" -+ CFLAGS="$CFLAGS -g2" - fi - - if test "$enable_debug" = yes && test "$ac_cv_prog_cxx_g" = yes; then -- CXXFLAGS="$CXXFLAGS -g" -+ CXXFLAGS="$CXXFLAGS -g2" - fi - - # enable code coverage if --enable-coverage -@@ -13313,7 +13313,7 @@ - #ifdef __cplusplus - extern "C" - #endif --char uuid_export (); -+extern char uuid_export (); - int - main () - { -@@ -13336,7 +13336,7 @@ - if test "x$ac_cv_lib_uuid_uuid_export" = xyes; then : - UUID_LIBS="-luuid" - else -- as_fn_error $? "library 'ossp-uuid' or 'uuid' is required for OSSP UUID" "$LINENO" 5 -+ ac_cv_lib_ossp_uuid_uuid_export=yes - fi - - fi diff --git a/patches-REL_17_4_WASM/postgresql-emscripten/src-Makefile.shlib.diff b/patches-REL_17_4_WASM/postgresql-emscripten/src-Makefile.shlib.diff deleted file mode 100644 index 06697ff6ade24..0000000000000 --- a/patches-REL_17_4_WASM/postgresql-emscripten/src-Makefile.shlib.diff +++ /dev/null @@ -1,49 +0,0 @@ ---- REL_17_4/src/Makefile.shlib -+++ pglite-REL_17_4/src/Makefile.shlib -@@ -224,6 +224,33 @@ - override CXXFLAGS += $(CXXFLAGS_SL_MODULE) - endif - -+ifeq ($(PORTNAME), emscripten) -+ LINK.shared = emsdk-shared -+ ifdef soname -+ # emscripten uses unversioned shared libraries -+ shlib = $(shlib_bare) -+ soname = $(shlib_bare) -+ endif -+ BUILD.exports = ( echo '{ global:'; $(AWK) '/^[^\#]/ {printf "%s;\n",$$1}' $<; echo ' local: *; };' ) >$@ -+ exports_file = $(SHLIB_EXPORTS:%.txt=%.list) -+# ifneq (,$(exports_file)) -+# LINK.shared += -Wl,--version-script=$(exports_file) -+# endif -+endif -+ -+ifeq ($(PORTNAME), wasi) -+ LINK.shared = wasi-shared -+ ifdef soname -+ # emscripten uses unversioned shared libraries -+ shlib = $(shlib_bare) -+ soname = $(shlib_bare) -+ endif -+ BUILD.exports = ( echo '{ global:'; $(AWK) '/^[^\#]/ {printf "%s;\n",$$1}' $<; echo ' local: *; };' ) >$@ -+ exports_file = $(SHLIB_EXPORTS:%.txt=%.list) -+# ifneq (,$(exports_file)) -+# LINK.shared += -Wl,--version-script=$(exports_file) -+# endif -+endif - - ## - ## BUILD -@@ -239,8 +266,11 @@ - endif - - all-static-lib: $(stlib) -- -+ifdef wasi -+all-shared-lib: all-static-lib -+else - all-shared-lib: $(shlib) -+endif - - # In this rule, "touch $@" works around a problem on some platforms wherein - # ar updates the library file's mod time with a value calculated to diff --git a/patches-REL_17_4_WASM/postgresql-emscripten/src-backend-commands-async.c.diff b/patches-REL_17_4_WASM/postgresql-emscripten/src-backend-commands-async.c.diff deleted file mode 100644 index 0299b57028d65..0000000000000 --- a/patches-REL_17_4_WASM/postgresql-emscripten/src-backend-commands-async.c.diff +++ /dev/null @@ -1,15 +0,0 @@ ---- REL_17_4/src/backend/commands/async.c -+++ pglite-REL_17_4/src/backend/commands/async.c -@@ -1651,8 +1651,12 @@ - * NotifyQueueLock; which is unlikely but certainly possible. So we - * just log a low-level debug message if it happens. - */ -+#if defined(__EMSCRIPTEN__) || defined(__wasi__) -+ HandleNotifyInterrupt(); -+#else - if (SendProcSignal(pid, PROCSIG_NOTIFY_INTERRUPT, procnos[i]) < 0) - elog(DEBUG3, "could not signal backend with PID %d: %m", pid); -+#endif - } - - pfree(pids); diff --git a/patches-REL_17_4_WASM/postgresql-pglite/src-backend-libpq-pqcomm.c.diff b/patches-REL_17_4_WASM/postgresql-pglite/src-backend-libpq-pqcomm.c.diff deleted file mode 100644 index 68b1992aac4bc..0000000000000 --- a/patches-REL_17_4_WASM/postgresql-pglite/src-backend-libpq-pqcomm.c.diff +++ /dev/null @@ -1,261 +0,0 @@ ---- REL_17_4/src/backend/libpq/pqcomm.c -+++ pglite-REL_17_4/src/backend/libpq/pqcomm.c -@@ -122,10 +122,18 @@ - static int PqSendBufferSize; /* Size send buffer */ - static size_t PqSendPointer; /* Next index to store a byte in PqSendBuffer */ - static size_t PqSendStart; /* Next index to send a byte in PqSendBuffer */ -- -+#if !defined(__EMSCRIPTEN__) && !defined(__wasi__) - static char PqRecvBuffer[PQ_RECV_BUFFER_SIZE]; - static int PqRecvPointer; /* Next index to read a byte from PqRecvBuffer */ - static int PqRecvLength; /* End of data available in PqRecvBuffer */ -+#else -+static char PqRecvBuffer_static[PQ_RECV_BUFFER_SIZE]; -+static char *PqRecvBuffer; -+static int PqRecvPointer; -+static int PqRecvLength; -+volatile int querylen = 0; -+volatile FILE* queryfp = NULL; -+#endif - - /* - * Message status -@@ -135,6 +143,7 @@ - - - /* Internal functions */ -+ - static void socket_comm_reset(void); - static void socket_close(int code, Datum arg); - static void socket_set_nonblocking(bool nonblocking); -@@ -148,9 +157,6 @@ - static pg_noinline int internal_flush_buffer(const char *buf, size_t *start, - size_t *end); - --static int Lock_AF_UNIX(const char *unixSocketDir, const char *unixSocketPath); --static int Setup_AF_UNIX(const char *sock_path); -- - static const PQcommMethods PqCommSocketMethods = { - .comm_reset = socket_comm_reset, - .flush = socket_flush, -@@ -160,6 +166,10 @@ - .putmessage_noblock = 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; -@@ -181,7 +191,7 @@ - port->sock = client_sock->sock; - memcpy(&port->raddr.addr, &client_sock->raddr.addr, client_sock->raddr.salen); - port->raddr.salen = client_sock->raddr.salen; -- -+#if !defined(__EMSCRIPTEN__) && !defined(__wasi__) - /* fill in the server (local) address */ - port->laddr.salen = sizeof(port->laddr.addr); - if (getsockname(port->sock, -@@ -273,14 +283,15 @@ - (void) pq_setkeepalivescount(tcp_keepalives_count, port); - (void) pq_settcpusertimeout(tcp_user_timeout, port); - } -- -+#endif /* WASM */ -+PDEBUG("# 285:" __FILE__); - /* initialize state variables */ - PqSendBufferSize = PQ_SEND_BUFFER_SIZE; - PqSendBuffer = MemoryContextAlloc(TopMemoryContext, PqSendBufferSize); - 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); - -@@ -310,7 +321,12 @@ - MyLatch, NULL); - AddWaitEventToSet(FeBeWaitSet, WL_POSTMASTER_DEATH, PGINVALID_SOCKET, - NULL, NULL); -- -+#else /* WASM */ -+ PDEBUG("# 323: FIXME: socketfile"); -+ #pragma message "FIXME: use socketfile when overflowing PqRecvBuffer_static" -+ /* because we fill before starting reading message */ -+ PqRecvBuffer = &PqRecvBuffer_static[0]; -+#endif /* WASM */ - /* - * The event positions match the order we added them, but let's sanity - * check them to be sure. -@@ -730,7 +746,7 @@ - Assert(Unix_socket_group); - if (Unix_socket_group[0] != '\0') - { --#ifdef WIN32 -+#if defined(WIN32) || defined(__wasi__) - elog(WARNING, "configuration item \"unix_socket_group\" is not supported on this platform"); - #else - char *endptr; -@@ -909,6 +925,20 @@ - else - PqRecvLength = PqRecvPointer = 0; - } -+#if defined(__EMSCRIPTEN__) || defined(__wasi__) -+ if (queryfp && querylen) { -+ int got = fread( PqRecvBuffer, 1, PQ_RECV_BUFFER_SIZE - PqRecvPointer, queryfp); -+ querylen -= got; -+ PqRecvLength += got; -+ if (querylen<=0) { -+ puts("# 931: could close fp early here " __FILE__); -+ queryfp = NULL; -+ } -+ if (got>0) -+ return 0; -+ } -+ return EOF; -+#endif - - /* Ensure that we're in blocking mode */ - socket_set_nonblocking(false); -@@ -1011,7 +1041,7 @@ - *c = PqRecvBuffer[PqRecvPointer++]; - return 1; - } -- -+puts("# 1028: pq_getbyte_if_available N/I in " __FILE__ ); abort(); - /* Put the socket into non-blocking mode */ - socket_set_nonblocking(true); - -@@ -1115,6 +1145,7 @@ - return 0; - } - -+ - /* -------------------------------- - * pq_buffer_remaining_data - return number of bytes in receive buffer - * -@@ -1136,6 +1167,26 @@ - * This must be called before any of the pq_get* functions. - * -------------------------------- - */ -+#if defined(__EMSCRIPTEN__) || defined(__wasi__) -+EMSCRIPTEN_KEEPALIVE void -+pq_recvbuf_fill(FILE* fp, int packetlen) { -+ if (packetlen>PQ_RECV_BUFFER_SIZE) { -+ int got = fread( PqRecvBuffer, 1, PQ_RECV_BUFFER_SIZE, fp); -+ queryfp = fp; -+ querylen = packetlen - got; -+ PqRecvLength = got; -+puts("# 1160: input overflow"); -+ } else { -+ fread( PqRecvBuffer, packetlen, 1, fp); -+ PqRecvLength = packetlen; -+ queryfp = NULL; -+ querylen = 0; -+ } -+ PqRecvPointer = 0; -+} -+#endif -+extern int cma_rsize; -+static char * PqSendBuffer_save; - void - pq_startmsgread(void) - { -@@ -1147,7 +1198,29 @@ - ereport(FATAL, - (errcode(ERRCODE_PROTOCOL_VIOLATION), - errmsg("terminating connection because protocol synchronization was lost"))); -+#if defined(__EMSCRIPTEN__) || defined(__wasi__) -+ if (!pq_buffer_remaining_data()) { -+ if (cma_rsize) { -+ PqRecvPointer = 0; -+ PqRecvLength = cma_rsize; -+ PqRecvBuffer = (char*)0x1; -+ -+ PqSendPointer = 0; -+ PqSendBuffer_save = PqSendBuffer; -+ PqSendBuffer = 2 + (char*)(cma_rsize); -+ PqSendBufferSize = (CMA_MB*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]); -+#endif - -+#endif - PqCommReadingMsg = true; - } - -@@ -1270,7 +1343,55 @@ - - return 0; - } -+#if defined(__EMSCRIPTEN__) || defined(__wasi__) -+extern FILE* SOCKET_FILE; -+extern int SOCKET_DATA; -+static int -+internal_putbytes(const char *s, size_t len) { -+ if (PqSendPointer >= PqSendBufferSize) { -+ fprintf(stderr, "# 1329: overflow %zu >= %d cma_rsize=%d CMA=%d\n", PqSendPointer, PqSendBufferSize,cma_rsize, CMA_MB); -+ } -+ -+ if (!cma_rsize) { -+ int wc= fwrite(s, 1, len, SOCKET_FILE); -+ 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; -+ } -+ } -+ 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 inline int - internal_putbytes(const char *s, size_t len) -@@ -1421,7 +1542,7 @@ - *start = *end = 0; - return 0; - } -- -+#endif /* wasm */ - /* -------------------------------- - * pq_flush_if_writable - flush pending output if writable without blocking - * diff --git a/patches-REL_17_4_WASM/postgresql-pglite/src-bin-initdb-initdb.c.diff b/patches-REL_17_4_WASM/postgresql-pglite/src-bin-initdb-initdb.c.diff deleted file mode 100644 index 3e110b9c43ff3..0000000000000 --- a/patches-REL_17_4_WASM/postgresql-pglite/src-bin-initdb-initdb.c.diff +++ /dev/null @@ -1,217 +0,0 @@ ---- REL_17_4/src/bin/initdb/initdb.c -+++ pglite-REL_17_4/src/bin/initdb/initdb.c -@@ -171,7 +171,11 @@ - - - /* internal vars */ -+#if !defined(PGL_MAIN) - static const char *progname; -+#else -+# define dynamic_shared_memory_type initdb_dynamic_shared_memory_type -+#endif - static int encodingid; - static char *bki_file; - static char *hba_file; -@@ -811,6 +815,7 @@ - static char * - get_id(void) - { -+#if !defined(__EMSCRIPTEN__) && !defined(__wasi__) - const char *username; - - #ifndef WIN32 -@@ -825,6 +830,10 @@ - username = get_user_name_or_exit(progname); - - return pg_strdup(username); -+#else -+ setenv("PGUSER", WASM_USERNAME, 0); -+ return pg_strdup(getenv("PGUSER")); -+#endif /* wasm */ - } - - static char * -@@ -1070,6 +1079,9 @@ - static const char * - choose_dsm_implementation(void) - { -+#if defined(__wasi__) || defined(__EMSCRIPTEN__) -+ return "posix"; -+#endif - #if defined(HAVE_SHM_OPEN) && !defined(__sun__) - int ntries = 10; - pg_prng_state prng_state; -@@ -1608,10 +1620,9 @@ - } - - PG_CMD_CLOSE(); -- -- termPQExpBuffer(&cmd); -+ termPQExpBuffer(&cmd); - free(bki_lines); -- -+PDEBUG("# 1624: BOOT pipe complete"); - check_ok(); - } - -@@ -1711,16 +1722,16 @@ - setup_run_file(FILE *cmdfd, const char *filename) - { - char **lines; -- -+int count=0; - lines = readfile(filename); -- - for (char **line = lines; *line != NULL; line++) - { - PG_CMD_PUTS(*line); - free(*line); -+ count ++ ; - } -- - PG_CMD_PUTS("\n\n"); -+fprintf(stderr, "# 1733: --------------------------------- added %s, %d lines\n", filename, count); - - free(lines); - } -@@ -2636,8 +2647,13 @@ - strlcpy(full_path, progname, sizeof(full_path)); - - if (ret == -1) -+#if defined(__EMSCRIPTEN__) || defined(__wasi__) -+ printf("# WARNING: program \"%s\" is needed by %s but was not found in the same directory as \"%s\"\n", -+ "postgres", progname, full_path); -+#else - pg_fatal("program \"%s\" is needed by %s but was not found in the same directory as \"%s\"", - "postgres", progname, full_path); -+#endif // wasm - else - pg_fatal("program \"%s\" was found by \"%s\" but was not the same version as %s", - "postgres", full_path, progname); -@@ -3096,7 +3112,7 @@ - initPQExpBuffer(&cmd); - printfPQExpBuffer(&cmd, "\"%s\" %s %s template1 >%s", - backend_exec, backend_options, extra_options, DEVNULL); -- -+PDEBUG("# 3115: post-bootstrap sql begin"); - PG_CMD_OPEN(cmd.data); - - setup_auth(cmdfd); -@@ -3134,14 +3150,53 @@ - - PG_CMD_CLOSE(); - termPQExpBuffer(&cmd); -- -+PDEBUG("# 3115: post-bootstrap sql end"); - check_ok(); - } - -+/* pglite entry point */ -+#if defined(PGL_INITDB_MAIN) -+extern void MemoryContextInit(void); -+extern volatile char *PREFIX; -+extern volatile char *PGDATA; -+extern char tmpstr[]; -+char * strcat_alloc(const char *head, const char *tail); -+void strconcat(char*p, const char *head, const char *tail); -+ - - int --main(int argc, char *argv[]) --{ -+pgl_initdb_main() { -+ char *pwfile = NULL; -+ char *pgdata = NULL; -+ -+ strconcat(tmpstr, "--pwfile=", PREFIX); -+ pwfile = strcat_alloc(tmpstr, "/password"); -+ -+ -+ strconcat(tmpstr, "--pwfile=", PREFIX); -+ pgdata = strcat_alloc("--pgdata=", PGDATA); -+ -+ char *argv[] = { -+ strcat_alloc(PREFIX,"/bin/initdb"), -+ // "--no-clean", -+ "--wal-segsize=1", -+ "--allow-group-access", "--no-sync", -+ "-E", "UTF8", -+ "--locale=C.UTF-8", "--locale-provider=libc", -+// "--builtin-locale=en_US.UTF-8", "--locale-provider=builtin", -+// "--locale-provider=icu", "--icu-locale=en-US", "--locale-provider=icu", -+ "-U", WASM_USERNAME, pwfile, //"--pwfile=" WASM_PREFIX "/password", -+ pgdata, // "--pgdata=" WASM_PREFIX "/base", -+ NULL -+ }; -+ -+ int argc = sizeof(argv) / sizeof(char*) - 1; -+ -+ -+#else -+int -+main(int argc, char *argv[]) { -+#endif - static struct option long_options[] = { - {"pgdata", required_argument, NULL, 'D'}, - {"encoding", required_argument, NULL, 'E'}, -@@ -3201,9 +3256,15 @@ - * POSIX says we must do this before any other usage of these files. - */ - setvbuf(stdout, NULL, PG_IOLBF, 0); -- -+#if defined(PGL_INITDB_MAIN) -+ progname = get_progname(argv[0]); -+printf("# 3245:" __FILE__ " calling pg_initdb_main for %s\n", progname); -+ MemoryContextInit(); -+ pg_logging_init(progname); -+#else - pg_logging_init(argv[0]); - progname = get_progname(argv[0]); -+#endif - set_pglocale_pgservice(argv[0], PG_TEXTDOMAIN("initdb")); - - if (argc > 1) -@@ -3409,7 +3470,7 @@ - if (icu_rules && locale_provider != COLLPROVIDER_ICU) - pg_fatal("%s cannot be specified unless locale provider \"%s\" is chosen", - "--icu-rules", "icu"); -- -+PDEBUG("# 3463:"__FILE__ " TODO: atexit(cleanup_directories_atexit)"); - atexit(cleanup_directories_atexit); - - /* If we only need to sync, just do it and exit */ -@@ -3445,13 +3506,13 @@ - get_restricted_token(); - - setup_pgdata(); -- -+PDEBUG("# 3493:pgl_initdb_main " __FILE__); - setup_bin_paths(argv[0]); -- -+PDEBUG("# 3495:pgl_initdb_main " __FILE__); - effective_user = get_id(); - if (!username) - username = effective_user; -- -+PDEBUG("# 3514:pgl_initdb_main " __FILE__); - if (strncmp(username, "pg_", 3) == 0) - pg_fatal("superuser name \"%s\" is disallowed; role names cannot begin with \"pg_\"", username); - -@@ -3479,7 +3540,7 @@ - get_su_pwd(); - - printf("\n"); -- -+puts("# 3527:" __FILE__); - initialize_data_directory(); - - if (do_sync) -@@ -3535,7 +3596,7 @@ - destroyPQExpBuffer(start_db_cmd); - } - -- -+PDEBUG("# 3583"); - success = true; - return 0; - } diff --git a/pglite-REL_17_4_WASM/build.sh b/pglite-REL_17_4_WASM/build.sh index bc8f709f84eed..71b209c0521db 100755 --- a/pglite-REL_17_4_WASM/build.sh +++ b/pglite-REL_17_4_WASM/build.sh @@ -52,8 +52,7 @@ Folders : build : $BUILD_PATH target : $PGROOT retarget : ${PGL_DIST_C} - native : ${PGL_DIST_NATIVE} - + native : ${PGL_DIST_NATIVE} build $(arch) : ${NATIVE} CPOPTS : $COPTS DEBUG : $DEBUG @@ -108,6 +107,7 @@ ________________________________________________________ cat > ${PGL_BUILD_NATIVE}/pglite-native.sh < 0 cma addr +volatile int channel = 0; + /* TODO : prevent multiple write and write while reading ? */ volatile int cma_wsize = 0; -volatile int cma_rsize = 0; // defined in postgres.c +volatile int cma_rsize = 0; // also defined in postgres.c for pqcomm +volatile bool sockfiles = false; // also defined in postgres.c for pqcomm + +__attribute__((export_name("get_buffer_size"))) +int +get_buffer_size(int fd) { + return (CMA_MB * 1024 * 1024) / CMA_FD; +} + +// TODO add query size +__attribute__((export_name("get_buffer_addr"))) +int +get_buffer_addr(int fd) { + return 1 + ( get_buffer_size(fd) *fd); +} + +__attribute__((export_name("get_channel"))) +int +get_channel() { + return channel; +} __attribute__((export_name("interactive_read"))) @@ -32,6 +55,8 @@ extern void ClientAuthentication(Port *port); extern FILE* SOCKET_FILE; extern int SOCKET_DATA; + + /* init sequence ___________________________________ @@ -162,16 +187,12 @@ static void io_init(bool in_auth, bool out_auth) { MyProcPort->canAcceptConnections = CAC_OK; #endif ClientAuthInProgress = out_auth; - SOCKET_FILE = NULL; SOCKET_DATA = 0; PDEBUG("\n\n\n# 165: io_init --------- Ready for CLIENT ---------"); } - - -volatile bool sockfiles = false; volatile bool is_wire = true; extern char * cma_port; extern void pq_startmsgread(void); @@ -191,13 +212,13 @@ use_wire(int state) { #endif if (state>0) { #if PGDEBUG - printf("# 199: wire mode, repl off, echo %d\n", force_echo); + printf("\n\n# 194: PACKET START: wire mode, repl off, echo %d\n", force_echo); #endif is_wire = true; is_repl = false; } else { #if PGDEBUG - printf("# 205: repl mode, no wire, echo %d\n", force_echo); + printf("\n\n# 200: PACKET START: repl mode, no wire, echo %d\n", force_echo); #endif is_wire = false; is_repl = true; @@ -359,7 +380,7 @@ interactive_one() { bool had_notification = notifyInterruptPending; bool notified = false; - send_ready_for_query = false; + // send_ready_for_query = false; if (!MyProcPort) { PDEBUG("# 353: client created"); @@ -367,9 +388,9 @@ interactive_one() { } #if PGDEBUG - puts("\n\n# 377: interactive_one"); + puts("\n\n# 369: interactive_one"); if (notifyInterruptPending) - PDEBUG("# 359: has notification !"); + PDEBUG("# 371: has notification !"); #endif // this could be pg_flush in sync mode. @@ -380,14 +401,19 @@ interactive_one() { } if (!cma_rsize) { - puts("# 388: socketfiles"); // prepare reply queue + // no cma : reading from file. writing to file. if (!SOCKET_FILE) { SOCKET_FILE = fopen(PGS_OLOCK, "w") ; MyProcPort->sock = fileno(SOCKET_FILE); } + } else { + // prepare file reply queue, just in case of cma overflow + // if unused the file will be kept open till next query. + if (!SOCKET_FILE) { + SOCKET_FILE = fopen(PGS_OLOCK, "w") ; + } } - MemoryContextSwitchTo(MessageContext); MemoryContextResetAndDeleteChildren(MessageContext); @@ -400,10 +426,10 @@ interactive_one() { if (send_ready_for_query) { if (IsAbortedTransactionBlockState()) { - puts("@@@@ TODO 390: idle in transaction (aborted)"); + PDEBUG("@@@@ TODO 403: idle in transaction (aborted)"); } else if (IsTransactionOrTransactionBlock()) { - puts("@@@@ TODO 393: idle in transaction"); + PDEBUG("@@@@ TODO 406: idle in transaction"); } else { if (notifyInterruptPending) { ProcessNotifyInterrupt(false); @@ -425,7 +451,9 @@ interactive_one() { /* * in cma mode (cma_rsize>0), client call the wire loop itself waiting synchronously for the results * in socketfiles mode, the wire loop polls a pseudo socket made from incoming and outgoing files. - * in repl mode (cma_rsize==0) output is on stdout not cma/socketfiles wire. repl mode is default. + * in repl mode (cma_rsize==0) output is on stdout not cma/socketfiles wire. + * repl mode is the simpleset mode where stdin is just copied into input buffer (limited by CMA size). + * TODO: allow to redirect stdout for fully external repl. */ peek = IO[0]; @@ -436,15 +464,15 @@ interactive_one() { if (!is_repl) { whereToSendOutput = DestRemote; if (!is_wire) - PDEBUG("# 426: repl message in cma buffer !"); + PDEBUG("# 439: repl message in cma buffer !"); } else { if (is_wire) - PDEBUG("# 429: wire message in cma buffer for REPL !"); + PDEBUG("# 442: wire message in cma buffer for REPL !"); whereToSendOutput = DestDebug; } } else { fp = fopen(PGS_IN, "r"); -PDEBUG("# 451:" PGS_IN); +PDEBUG("# 452:" PGS_IN "\n"); // read file in socket buffer for SocketBackend to consumme. if (fp) { fseek(fp, 0L, SEEK_END); @@ -489,7 +517,7 @@ PDEBUG("# 451:" PGS_IN); if (packetlen) { // it was startup/auth , write and return fast. if (peek<0) { - PDEBUG("# 471: handshake/auth/pass skip"); + PDEBUG("# 492: handshake/auth/pass skip"); goto wire_flush; } @@ -504,7 +532,7 @@ PDEBUG("# 451:" PGS_IN); firstchar = peek; goto incoming; } // wire msg -PDEBUG("# 500: NO DATA:" PGS_IN ); +PDEBUG("# 507: NO DATA:" PGS_IN "\n"); } // fp data read // is it REPL in cma ? @@ -521,7 +549,7 @@ PDEBUG("# 500: NO DATA:" PGS_IN ); #if PGDEBUG if (packetlen) IO[packetlen]=0; // wire blocks are not zero terminated - printf("\n# 500: fd=%d is_embed=%d is_repl=%d is_wire=%d fd %s,len=%d cma=%d peek=%d [%s]\n", MyProcPort->sock, is_embed, is_repl, is_wire, PGS_OLOCK, packetlen,cma_rsize, peek, IO); + printf("\n# 524: fd=%d is_embed=%d is_repl=%d is_wire=%d fd %s,len=%d cma=%d peek=%d [%s]\n", MyProcPort->sock, is_embed, is_repl, is_wire, PGS_OLOCK, packetlen,cma_rsize, peek, IO); #endif resetStringInfo(inBuf); @@ -531,9 +559,9 @@ PDEBUG("# 500: NO DATA:" PGS_IN ); appendStringInfoChar(inBuf, IO[i]); } } - + if (packetlen<2) { - puts("# 512: WARNING: empty packet"); + puts("# 536: WARNING: empty packet"); //cma_rsize= 0; if (is_repl) pg_prompt(); @@ -601,7 +629,7 @@ PDEBUG("# 500: NO DATA:" PGS_IN ); } DoingCommandRead = false; -#if PGDEBUG +#if 0 // PGDEBUG if (!pipelining) { printf("# 573: wire=%d 1stchar=%c Q: %s\n", is_wire, firstchar, inBuf->data); force_echo = false; @@ -612,7 +640,7 @@ PDEBUG("# 500: NO DATA:" PGS_IN ); if (!ignore_till_sync) { /* initially, or after error */ - send_ready_for_query = true; + // send_ready_for_query = true; if (notifyInterruptPending) ProcessClientReadInterrupt(true); } else { @@ -657,34 +685,41 @@ puts("# 631: PIPELINING + rfq"); if (SOCKET_DATA>0) { if (sockfiles) { - if (cma_wsize) + channel = -1; + if (cma_wsize) { puts("ERROR: cma was not flushed before socketfile interface"); + } } else { /* wsize may have increased with previous rfq so assign here */ cma_wsize = SOCKET_DATA; + channel = cma_rsize + 2; } if (SOCKET_FILE) { int outb = SOCKET_DATA; fclose(SOCKET_FILE); SOCKET_FILE = NULL; SOCKET_DATA = 0; - if (cma_wsize) - PDEBUG("# 626: cma and sockfile ???\n"); + + if (cma_wsize) { + PDEBUG("# 672: cma and sockfile ???\n"); + } + if (sockfiles) { #if PGDEBUG - printf("# 629: client:ready -> read(%d) " PGS_OLOCK "->" PGS_OUT"\n", outb); + printf("# 675: client:ready -> read(%d) " PGS_OLOCK "->" PGS_OUT"\n", outb); #endif rename(PGS_OLOCK, PGS_OUT); } } else { #if PGDEBUG - printf("\n# 635: in[%d] out[%d] flushed\n", cma_rsize, cma_wsize); + printf("\n# 681: in[%d] out[%d] flushed\n", cma_rsize, cma_wsize); #endif SOCKET_DATA = 0; } } else { - cma_wsize = 0; PDEBUG("# 680: no socket data"); + cma_wsize = 0; + PDEBUG("# 698: no data, send empty ?"); } } else { pg_prompt(); diff --git a/pglite-REL_17_4_WASM/native.sh b/pglite-REL_17_4_WASM/native.sh new file mode 100755 index 0000000000000..6b3777ae22391 --- /dev/null +++ b/pglite-REL_17_4_WASM/native.sh @@ -0,0 +1,189 @@ +#!/bin/bash + +[ -f /alpine ] && /sbin/apk add clang gcc + +mkdir -p ${SDKROOT}/src ${SDKROOT}/native +if [ -d ${SDKROOT}/src/w2c2 ] +then + echo " using local w2c2" +else + pushd ${SDKROOT}/src + git clone https://github.com/pygame-web/w2c2 + cp -R w2c2/w2c2/w2c2_base.h ${SDKROOT}/native/w2c2/ + cp -R w2c2/wasi ${SDKROOT}/native/ + popd +fi + +mv ${PGROOT}/bin/pglite.wasi pglite.wasi +${SDKROOT}/native/w2c2/w2c2 -f 0 -t 1 -p pglite.wasi pglite.c +mv pglite.wasi ${PGL_DIST_NATIVE}/ + +cat > tmp.py <0: + try: + bodies.write( makefunc(l) ) + except Exception as e: + print("?", l, e) + continue + +print("="*70) +with open('${WORKSPACE}/pglite-${PG_BRANCH}/pglite-modpython.c','r') as source: + with open('tmp.c','w') as out: + out.write( source.read().replace('\${WASM2C}','${WASM2C}') ) +END + +python3 tmp.py + +echo " building loader ..." + + +PYLD="-lm" +PYEXT="" + +if echo $CC|grep gcc +then + COPTS="-O0 -g0" + CCOPTS="-Wno-attributes" +else + COPTS="-O0 -g0" + CCOPTS="-fbracket-depth=4096 -Wno-unknown-attributes" +fi + +COMPILE="$CC -fPIC $PYINC $COPTS $CCOPTS -I${SDKROOT}/native -I${SDKROOT}/native/w2c2 -o ${WASM2C}$PYEXT tmp.c ${SDKROOT}/native/wasi/libw2c2wasi.a $PYLD -lc" +echo $COMPILE + +PYVER=$($PYTHON -V|cut -d' ' -f2|cut -d. -f1-2) +PYVER=${PYVER}$(${PYTHON}-config --abiflags) + +PYINC="-D__PYDK__=1 -shared $(${PYTHON}-config --includes)" +PYEXT=$(${PYTHON}-config --extension-suffix) +PYLD="-lpython$PYVER $(${PYTHON}-config --ldflags)" + +echo " +======================================================================== +WASM2C=$WASM2C +COPTS=$COPTS + +PYVER=$PYVER +PYEXT=$PYEXT + +PYINC=$PYINC +PYLD=$PYLD + +PGL_BUILD_NATIVE=${PGL_BUILD_NATIVE} +PGL_DIST_NATIVE=${PGL_DIST_NATIVE} +TARGET: ${WASM2C}$PYEXT +======================================================================== +" + +$COMPILE + + +[ -f ${WASM2C}$PYEXT ] && rm ${WASM2C}$PYEXT + +# -I${SDKROOT}/src/w2c2/w2c2 + +COMPILE="$CC -fPIC -Os -g0 $PYINC $CCOPTS -I${SDKROOT}/native -I${SDKROOT}/native/w2c2 -o ${WASM2C}$PYEXT tmp.c ${SDKROOT}/native/wasi/libw2c2wasi.a $PYLD -lc" +echo $COMPILE + +time $COMPILE + +if [ -f ${WASM2C} ] +then + du -hs ${WASM2C} + # ./${WASM2C} $@ +else + echo build native ${WASM2C} failed +fi + +PY=$(command -v python${PYMAJOR}.${PYMINOR}) + +echo "__________________________________________" +echo $PY +echo "__________________________________________" + + + +if [ -f ${WASM2C}$PYEXT ] +then + env -i $PY <= PqSendBufferSize) { - fprintf(stderr, "# 1329: overflow %zu >= %d cma_rsize=%d CMA=%d\n", PqSendPointer, PqSendBufferSize,cma_rsize, CMA_MB); - } - - if (!cma_rsize) { - int wc= fwrite(s, 1, len, SOCKET_FILE); - SOCKET_DATA+=wc; - } else { - size_t amount; + size_t amount; + if (!sockfiles) { while (len > 0) { - /* If buffer is full, then flush it out */ + /* If buffer is full, then flush it out from cma to file and continue from there */ if (PqSendPointer >= PqSendBufferSize) { - socket_set_nonblocking(false); - if (internal_flush()) - return EOF; + int redirected = fwrite(PqSendBuffer, 1, PqSendPointer, SOCKET_FILE); + sockfiles = true; +#if PGDEBUG + fprintf(stderr, "# 1364: overflow %zu >= %d redirect=%d cma_rsize=%d CMA_MB=%d \n", PqSendPointer, PqSendBufferSize, redirected, cma_rsize, CMA_MB); +#endif + break; } amount = PqSendBufferSize - PqSendPointer; if (amount > len) @@ -1376,6 +1375,11 @@ internal_putbytes(const char *s, size_t len) { SOCKET_DATA+=amount; } } + + if (sockfiles) { + int wc= fwrite(s, 1, len, SOCKET_FILE); + SOCKET_DATA+=wc; + } return 0; } @@ -1387,7 +1391,7 @@ socket_flush(void) { static int internal_flush(void) { /* no flush for raw wire */ - if (!cma_rsize) { + if (sockfiles) { PqSendStart = PqSendPointer = 0; } return 0; @@ -2204,4 +2208,4 @@ pq_check_connection(void) } return true; -} \ No newline at end of file +} diff --git a/src/backend/tcop/postgres.c b/src/backend/tcop/postgres.c index 767cda0ceebcd..59e94bddaa79c 100644 --- a/src/backend/tcop/postgres.c +++ b/src/backend/tcop/postgres.c @@ -110,6 +110,7 @@ int restrict_nonsystem_relation_kind; #if (defined(__EMSCRIPTEN__) || defined(__wasi__)) #if !defined(PGL_MAIN) volatile int cma_rsize = 0; + volatile bool sockfiles = false; #endif // PGL_MAIN bool quote_all_identifiers = false; FILE* SOCKET_FILE = NULL; diff --git a/src/port/pqsignal.c b/src/port/pqsignal.c index 8ceb097ac45df..5eb4d98840af6 100644 --- a/src/port/pqsignal.c +++ b/src/port/pqsignal.c @@ -170,7 +170,13 @@ pqsignal(int signo, pqsigfunc func) #endif } -/* sneak stubs into libpgport */ +/* sneak emsdk or wasi wasm port support into libpgport */ #if defined(SDK_PORT) -# include "sdk_port.c" +# if defined(__wasi__) +# include "sdk_port-wasi.c" +# endif +# if defined(__EMSCRIPTEN__) +// # include "sdk_port-emscripten.c" +# endif #endif +