diff --git a/src/backend/access/external/url_execute.c b/src/backend/access/external/url_execute.c index 0205004dd3d3..1bae6ce7026e 100644 --- a/src/backend/access/external/url_execute.c +++ b/src/backend/access/external/url_execute.c @@ -23,6 +23,7 @@ #include "cdb/cdbtimer.h" #include "cdb/cdbvars.h" #include "libpq/pqsignal.h" +#include "mb/pg_wchar.h" #include "utils/resowner.h" #define EXEC_DATA_P 0 /* index to data pipe */ @@ -484,7 +485,12 @@ interpretError(int rc, char *buf, size_t buflen, char *err, size_t errlen) /* Exit codes from commands rarely map to strerror() strings. In here * we show the error string returned from pclose, and omit the non * friendly exit code interpretation */ - snprintf(buf, buflen, "error. %s", err); + int len = snprintf(buf, buflen, "error. %s", err); + + if (len >= buflen) + { + buf[pg_mbcliplen(buf, len, buflen - 1)] = '\0'; + } } } else if (WIFSIGNALED(rc)) diff --git a/src/backend/cdb/cdbthreadlog.c b/src/backend/cdb/cdbthreadlog.c index 700258436560..297d469bbeef 100644 --- a/src/backend/cdb/cdbthreadlog.c +++ b/src/backend/cdb/cdbthreadlog.c @@ -117,7 +117,12 @@ write_log(const char *fmt,...) { char errbuf[2048]; /* Arbitrary size? */ - vsnprintf(errbuf, sizeof(errbuf), fmt, ap); + int len = vsnprintf(errbuf, sizeof(errbuf), fmt, ap); + + if (len >= sizeof(errbuf)) + { + errbuf[pg_mbcliplen(errbuf, len, sizeof(errbuf) - 1)] = '\0'; + } /* Write the message in the CSV format */ write_message_to_server_log(LOG, diff --git a/src/test/regress/input/external_table.source b/src/test/regress/input/external_table.source index dab9b775638a..3f6d94ae2da4 100644 --- a/src/test/regress/input/external_table.source +++ b/src/test/regress/input/external_table.source @@ -135,6 +135,15 @@ FETCH FORWARD 1 FROM _psql_cursor; CLOSE _psql_cursor; COMMIT; +-- ensure correct truncate error log according to database encoding +CREATE EXTERNAL WEB TABLE table_test (text text) EXECUTE $$ (echo -n ‘ && seq 1 500 | cut -b1 | tr -d '\n' && echo -n ’) >/dev/stderr && false $$ ON ALL FORMAT 'CSV'; +SELECT true FROM table_test; +DROP EXTERNAL TABLE IF EXISTS table_test; +CREATE EXTERNAL WEB TABLE table_test (text text) EXECUTE $$ (echo -n ‘ && seq 1 2006 | cut -b1 | tr -d '\n' && echo -n ’) >/dev/stderr && true $$ ON ALL FORMAT 'CSV'; +SELECT true FROM table_test; +SELECT logseverity FROM gp_toolkit.gp_log_system WHERE logmessage LIKE 'read err msg from pipe%' AND logdatabase = current_database() LIMIT 1; +DROP EXTERNAL TABLE IF EXISTS table_test; + -- echo will behave differently on different platforms, force to use bash with -E option CREATE EXTERNAL WEB TABLE table_qry (val TEXT) EXECUTE E'/usr/bin/env bash -c ''echo -E "$GP_QUERY_STRING"''' ON SEGMENT 0 diff --git a/src/test/regress/output/external_table.source b/src/test/regress/output/external_table.source index 8438693ca229..d32b4bd6c8df 100644 --- a/src/test/regress/output/external_table.source +++ b/src/test/regress/output/external_table.source @@ -166,6 +166,25 @@ FETCH FORWARD 1 FROM _psql_cursor; CLOSE _psql_cursor; COMMIT; +-- ensure correct truncate error log according to database encoding +CREATE EXTERNAL WEB TABLE table_test (text text) EXECUTE $$ (echo -n ‘ && seq 1 500 | cut -b1 | tr -d '\n' && echo -n ’) >/dev/stderr && false $$ ON ALL FORMAT 'CSV'; +SELECT true FROM table_test; +ERROR: external table table_test command ended with error. ‘12345678911111111112222222222333333333344444444445555555555666666666677777777778888888888999999999911111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111112222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333344444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444445 (seg0 slice1 172.19.0.4:6434 pid=107133) +DETAIL: Command: execute: (echo -n ‘ && seq 1 500 | cut -b1 | tr -d '\n' && echo -n ’) >/dev/stderr && false +DROP EXTERNAL TABLE IF EXISTS table_test; +CREATE EXTERNAL WEB TABLE table_test (text text) EXECUTE $$ (echo -n ‘ && seq 1 2006 | cut -b1 | tr -d '\n' && echo -n ’) >/dev/stderr && true $$ ON ALL FORMAT 'CSV'; +SELECT true FROM table_test; + bool +------ +(0 rows) + +SELECT logseverity FROM gp_toolkit.gp_log_system WHERE logmessage LIKE 'read err msg from pipe%' AND logdatabase = current_database() LIMIT 1; + logseverity +------------- + LOG +(1 row) + +DROP EXTERNAL TABLE IF EXISTS table_test; -- echo will behave differently on different platforms, force to use bash with -E option CREATE EXTERNAL WEB TABLE table_qry (val TEXT) EXECUTE E'/usr/bin/env bash -c ''echo -E "$GP_QUERY_STRING"''' ON SEGMENT 0 diff --git a/src/test/regress/output/external_table_optimizer.source b/src/test/regress/output/external_table_optimizer.source index 63c99689d4d2..782c62fc2c90 100644 --- a/src/test/regress/output/external_table_optimizer.source +++ b/src/test/regress/output/external_table_optimizer.source @@ -166,6 +166,25 @@ FETCH FORWARD 1 FROM _psql_cursor; CLOSE _psql_cursor; COMMIT; +-- ensure correct truncate error log according to database encoding +CREATE EXTERNAL WEB TABLE table_test (text text) EXECUTE $$ (echo -n ‘ && seq 1 500 | cut -b1 | tr -d '\n' && echo -n ’) >/dev/stderr && false $$ ON ALL FORMAT 'CSV'; +SELECT true FROM table_test; +ERROR: external table table_test command ended with error. ‘12345678911111111112222222222333333333344444444445555555555666666666677777777778888888888999999999911111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111112222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333344444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444445 (seg0 slice1 172.19.0.4:6434 pid=107133) +DETAIL: Command: execute: (echo -n ‘ && seq 1 500 | cut -b1 | tr -d '\n' && echo -n ’) >/dev/stderr && false +DROP EXTERNAL TABLE IF EXISTS table_test; +CREATE EXTERNAL WEB TABLE table_test (text text) EXECUTE $$ (echo -n ‘ && seq 1 2006 | cut -b1 | tr -d '\n' && echo -n ’) >/dev/stderr && true $$ ON ALL FORMAT 'CSV'; +SELECT true FROM table_test; + bool +------ +(0 rows) + +SELECT logseverity FROM gp_toolkit.gp_log_system WHERE logmessage LIKE 'read err msg from pipe%' AND logdatabase = current_database() LIMIT 1; + logseverity +------------- + LOG +(1 row) + +DROP EXTERNAL TABLE IF EXISTS table_test; -- echo will behave differently on different platforms, force to use bash with -E option CREATE EXTERNAL WEB TABLE table_qry (val TEXT) EXECUTE E'/usr/bin/env bash -c ''echo -E "$GP_QUERY_STRING"''' ON SEGMENT 0