diff --git a/src/backend/commands/copyto.c b/src/backend/commands/copyto.c index ae8b2e36d7..fcb200503f 100644 --- a/src/backend/commands/copyto.c +++ b/src/backend/commands/copyto.c @@ -29,6 +29,7 @@ #include "mb/pg_wchar.h" #include "miscadmin.h" #include "pgstat.h" +#include "port/pg_lfind.h" #include "storage/fd.h" #include "tcop/tcopprot.h" #include "utils/lsyscache.h" @@ -1068,9 +1069,18 @@ CopyAttributeOutText(CopyToState cstate, const char *string) else { start = ptr; - while ((c = *ptr) != '\0') + + while (true) { - if ((unsigned char) c < (unsigned char) 0x20) + while (!pg_lfind8('\\', (uint8 *) ptr, sizeof(Vector8)) && + !pg_lfind8(delimc, (uint8 *) ptr, sizeof(Vector8)) && + !pg_lfind8_le(31, (uint8 *) ptr, sizeof(Vector8))) + ptr += sizeof(Vector8); + + if ((c = *ptr) == '\0') + break; + + if ((unsigned char) c <= (unsigned char) 31) { /* * \r and \n must be escaped, the others are traditional. We