BUG #17142: COPY ignores client_encoding for octal digit characters

From: PG Bug reporting form <noreply(at)postgresql(dot)org>
To: pgsql-bugs(at)lists(dot)postgresql(dot)org
Cc: vilarion(at)illarion(dot)org
Subject: BUG #17142: COPY ignores client_encoding for octal digit characters
Date: 2021-08-11 21:24:45
Message-ID: 17142-9181542ca1df75ab@postgresql.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

The following bug has been logged on the website:

Bug reference: 17142
Logged by: Andreas Grob
Email address: vilarion(at)illarion(dot)org
PostgreSQL version: 13.3
Operating system: Debian GNU/Linux 11 (bullseye)
Description:

Test db and table:
```
CREATE DATABASE test WITH TEMPLATE = template0 ENCODING = 'UTF8' LC_COLLATE
= 'C' LC_CTYPE = 'C';
CREATE TABLE test (text character varying(50));
```

Test program in C:
```
#include <postgresql/libpq-fe.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(int argc, char **argv)
{
const char *conninfo;
char *errmsg;
PGconn *conn;
PGresult *res;
int a, b;
ExecStatusType status;
int enc;

char buffer[] = "\\304\\366\\337"; //Äöß
// char buffer[] = "\304\366\337"; //Äöß

if (argc > 1)
conninfo = argv[1];
else
conninfo = "user=postgres dbname=test port=5433
client_encoding=LATIN1";

/* Make a connection to the database */
conn = PQconnectdb(conninfo);

/* Check to see that the backend connection was successfully made */
if (PQstatus(conn) != CONNECTION_OK)
{
fprintf(stderr, "Connection to database failed: %s"
, PQerrorMessage(conn));
PQfinish(conn);
exit(1);
}

res = PQexec(conn, "BEGIN");

res = PQexec(conn, "COPY public.test(text) from STDIN;");
a = PQputCopyData(conn, buffer, strlen(buffer));
b = PQputCopyEnd(conn, NULL);
res = PQgetResult(conn);
status = PQresultStatus(res);
enc = PQclientEncoding(conn);
errmsg = PQresultErrorMessage(res);

printf("status=%d a=%d,b=%d, enc=%d\n", status, a, b, enc);

if (status != PGRES_COMMAND_OK)
printf("%s\n", errmsg);
else
printf("worked.\n");

res = PQexec(conn, "COMMIT");

/* close the connection to the database and cleanup */
PQfinish(conn);

return 0;
}
```

Output:
```
status=7 a=1,b=1, enc=8
ERROR: invalid byte sequence for encoding "UTF8": 0xc4 0xf6
CONTEXT: COPY test, line 1: "\304\366\337"
```

Expected output:
```
status=1 a=1,b=1, enc=8
worked.
```
(Äöß got inserted into the table.)

Characters in octal digits should be possible as per
https://www.postgresql.org/docs/13/sql-copy.html
When using characters directly (char buffer[] = "\304\366\337") the expected
output is displayed.

My apologies if I misunderstood something.

Responses

Browse pgsql-bugs by date

  From Date Subject
Next Message David Christensen 2021-08-11 21:39:00 Re: BUG #17141: SELECT LIMIT WITH TIES FOR UPDATE SKIP LOCKED returns wrong number of rows
Previous Message Alvaro Herrera 2021-08-11 20:07:09 Re: BUG #17141: SELECT LIMIT WITH TIES FOR UPDATE SKIP LOCKED returns wrong number of rows