public inbox for [email protected]
help / color / mirror / Atom feedRe: PQexecPrepared() question
19+ messages / 7 participants
[nested] [flat]
* Re: PQexecPrepared() question
@ 2025-12-18 15:20 Igor Korot <[email protected]>
2025-12-18 15:40 ` Re: PQexecPrepared() question David G. Johnston <[email protected]>
2025-12-19 07:43 ` Re: PQexecPrepared() question Laurenz Albe <[email protected]>
0 siblings, 2 replies; 19+ messages in thread
From: Igor Korot @ 2025-12-18 15:20 UTC (permalink / raw)
To: Laurenz Albe <[email protected]>; pgsql-generallists.postgresql.org <[email protected]>
Hi,
On Tue, Dec 16, 2025 at 11:32 PM Laurenz Albe <[email protected]>
wrote:
> On Tue, 2025-12-16 at 21:53 -0800, Igor Korot wrote:
> > Is there some default value for client_encoding?
>
> The default value for a client connection is whatever the parameter
> "client_encoding" was set to in the PostgreSQL server configuration.
Imagine following scenario:
I have 2 machines. One is running PG server on *nix. Second is my app on
Windows.
An application starts for the first time.
What is “clientencoding in this case?
Thank you.
>
> But that value can be overridden in several ways:
>
> - explicitly by setting "client_encoding" in the connect string
>
> - with the SQL statement SET by the client
>
> - if the client has "client_encoding=auto", by the encoding set in
> the client's environment
>
> - by setting the PGCLIENTENCODING environment variable
>
> Yours,
> Laurenz Albe
>
^ permalink raw reply [nested|flat] 19+ messages in thread
* Re: PQexecPrepared() question
2025-12-18 15:20 Re: PQexecPrepared() question Igor Korot <[email protected]>
@ 2025-12-18 15:40 ` David G. Johnston <[email protected]>
1 sibling, 0 replies; 19+ messages in thread
From: David G. Johnston @ 2025-12-18 15:40 UTC (permalink / raw)
To: Igor Korot <[email protected]>; +Cc: Laurenz Albe <[email protected]>; pgsql-generallists.postgresql.org <[email protected]>
On Thu, Dec 18, 2025 at 8:20 AM Igor Korot <[email protected]> wrote:
> Imagine following scenario:
>
> I have 2 machines. One is running PG server on *nix. Second is my app on
> Windows.
>
> An application starts for the first time.
>
> What is “clientencoding in this case?
>
This day in age, probably UTF-8; which is what most servers are
initialized using. If you aren't having issues with encoding I suggest you
just take for granted that the defaults work in 99% of the cases. If you
are having issues, share the details.
David J.
^ permalink raw reply [nested|flat] 19+ messages in thread
* Re: PQexecPrepared() question
2025-12-18 15:20 Re: PQexecPrepared() question Igor Korot <[email protected]>
@ 2025-12-19 07:43 ` Laurenz Albe <[email protected]>
2025-12-19 18:49 ` Fwd: PQexecPrepared() question Igor Korot <[email protected]>
2025-12-20 04:10 ` Re: PQexecPrepared() question Igor Korot <[email protected]>
1 sibling, 2 replies; 19+ messages in thread
From: Laurenz Albe @ 2025-12-19 07:43 UTC (permalink / raw)
To: Igor Korot <[email protected]>; pgsql-generallists.postgresql.org <[email protected]>
On Thu, 2025-12-18 at 07:20 -0800, Igor Korot wrote:
> Imagine following scenario:
>
> I have 2 machines. One is running PG server on *nix. Second is my app on Windows.
>
> An application starts for the first time.
>
> What is “clientencoding in this case?
If I read the code correctly:
- if PGCLIENTENCODING is set in the environment of the client executable, that
- otherwise, if "client_encoding" is set on the server, that
- otherwise, SQL_ASCII
Yours,
Laurenz Albe
^ permalink raw reply [nested|flat] 19+ messages in thread
* Fwd: PQexecPrepared() question
2025-12-18 15:20 Re: PQexecPrepared() question Igor Korot <[email protected]>
2025-12-19 07:43 ` Re: PQexecPrepared() question Laurenz Albe <[email protected]>
@ 2025-12-19 18:49 ` Igor Korot <[email protected]>
2025-12-19 18:52 ` Re: Fwd: PQexecPrepared() question Tom Lane <[email protected]>
1 sibling, 1 reply; 19+ messages in thread
From: Igor Korot @ 2025-12-19 18:49 UTC (permalink / raw)
To: pgsql-generallists.postgresql.org <[email protected]>
---------- Forwarded message ---------
From: Igor Korot <[email protected]>
Date: Fri, Dec 19, 2025 at 9:17 AM
Subject: Re: PQexecPrepared() question
To: Laurenz Albe <[email protected]>
Hi, Lauren’s,
On Thu, Dec 18, 2025 at 11:43 PM Laurenz Albe <[email protected]>
wrote:
> On Thu, 2025-12-18 at 07:20 -0800, Igor Korot wrote:
> > Imagine following scenario:
> >
> > I have 2 machines. One is running PG server on *nix. Second is my app on
> Windows.
> >
> > An application starts for the first time.
> >
> > What is “clientencoding in this case?
>
> If I read the code correctly:
>
> - if PGCLIENTENCODING is set in the environment of the client executable,
> that
No it is not.
>
> - otherwise, if "client_encoding" is set on the server, that
I guess I is.
But what is the purpose of setting client encoding on the server? It is for
the client
Thank you.
>
> - otherwise, SQL_ASCII
>
> Yours,
> Laurenz Albe
>
^ permalink raw reply [nested|flat] 19+ messages in thread
* Re: Fwd: PQexecPrepared() question
2025-12-18 15:20 Re: PQexecPrepared() question Igor Korot <[email protected]>
2025-12-19 07:43 ` Re: PQexecPrepared() question Laurenz Albe <[email protected]>
2025-12-19 18:49 ` Fwd: PQexecPrepared() question Igor Korot <[email protected]>
@ 2025-12-19 18:52 ` Tom Lane <[email protected]>
2025-12-19 19:12 ` Re: Fwd: PQexecPrepared() question Ron Johnson <[email protected]>
0 siblings, 1 reply; 19+ messages in thread
From: Tom Lane @ 2025-12-19 18:52 UTC (permalink / raw)
To: Igor Korot <[email protected]>; +Cc: pgsql-generallists.postgresql.org <[email protected]>
Igor Korot <[email protected]> writes:
> But what is the purpose of setting client encoding on the server? It is for
> the client
No, it's to tell the server what encoding to transmit to the client
(as well as what encoding strings coming from the client are in).
regards, tom lane
^ permalink raw reply [nested|flat] 19+ messages in thread
* Re: Fwd: PQexecPrepared() question
2025-12-18 15:20 Re: PQexecPrepared() question Igor Korot <[email protected]>
2025-12-19 07:43 ` Re: PQexecPrepared() question Laurenz Albe <[email protected]>
2025-12-19 18:49 ` Fwd: PQexecPrepared() question Igor Korot <[email protected]>
2025-12-19 18:52 ` Re: Fwd: PQexecPrepared() question Tom Lane <[email protected]>
@ 2025-12-19 19:12 ` Ron Johnson <[email protected]>
2025-12-19 21:21 ` Re: Fwd: PQexecPrepared() question Adrian Klaver <[email protected]>
0 siblings, 1 reply; 19+ messages in thread
From: Ron Johnson @ 2025-12-19 19:12 UTC (permalink / raw)
To: pgsql-generallists.postgresql.org <[email protected]>
On Fri, Dec 19, 2025 at 1:52 PM Tom Lane <[email protected]> wrote:
> Igor Korot <[email protected]> writes:
> > But what is the purpose of setting client encoding on the server? It is
> for
> > the client
>
> No, it's to tell the server what encoding to transmit to the client
> (as well as what encoding strings coming from the client are in).
>
If there's an encoding mismatch between the server and client, does
"something" (like a PQ driver) convert text in the data stream from the
source encoding to the target encoding?
--
Death to <Redacted>, and butter sauce.
Don't boil me, I'm still alive.
<Redacted> lobster!
^ permalink raw reply [nested|flat] 19+ messages in thread
* Re: Fwd: PQexecPrepared() question
2025-12-18 15:20 Re: PQexecPrepared() question Igor Korot <[email protected]>
2025-12-19 07:43 ` Re: PQexecPrepared() question Laurenz Albe <[email protected]>
2025-12-19 18:49 ` Fwd: PQexecPrepared() question Igor Korot <[email protected]>
2025-12-19 18:52 ` Re: Fwd: PQexecPrepared() question Tom Lane <[email protected]>
2025-12-19 19:12 ` Re: Fwd: PQexecPrepared() question Ron Johnson <[email protected]>
@ 2025-12-19 21:21 ` Adrian Klaver <[email protected]>
0 siblings, 0 replies; 19+ messages in thread
From: Adrian Klaver @ 2025-12-19 21:21 UTC (permalink / raw)
To: Ron Johnson <[email protected]>; pgsql-generallists.postgresql.org <[email protected]>
On 12/19/25 11:12, Ron Johnson wrote:
> On Fri, Dec 19, 2025 at 1:52 PM Tom Lane <[email protected]
> <mailto:[email protected]>> wrote:
>
> Igor Korot <[email protected] <mailto:[email protected]>> writes:
> > But what is the purpose of setting client encoding on the server?
> It is for
> > the client
>
> No, it's to tell the server what encoding to transmit to the client
> (as well as what encoding strings coming from the client are in).
>
>
> If there's an encoding mismatch between the server and client, does
> "something" (like a PQ driver) convert text in the data stream from the
> source encoding to the target encoding?
It is explained here:
https://www.postgresql.org/docs/current/multibyte.html#MULTIBYTE-AUTOMATIC-CONVERSION
>
> --
> Death to <Redacted>, and butter sauce.
> Don't boil me, I'm still alive.
> <Redacted> lobster!
--
Adrian Klaver
[email protected]
^ permalink raw reply [nested|flat] 19+ messages in thread
* Re: PQexecPrepared() question
2025-12-18 15:20 Re: PQexecPrepared() question Igor Korot <[email protected]>
2025-12-19 07:43 ` Re: PQexecPrepared() question Laurenz Albe <[email protected]>
@ 2025-12-20 04:10 ` Igor Korot <[email protected]>
2025-12-20 06:24 ` Re: PQexecPrepared() question Laurenz Albe <[email protected]>
1 sibling, 1 reply; 19+ messages in thread
From: Igor Korot @ 2025-12-20 04:10 UTC (permalink / raw)
To: Laurenz Albe <[email protected]>; pgsql-generallists.postgresql.org <[email protected]>
Hi, ALL,
On Fri, Dec 19, 2025 at 9:17 AM Igor Korot <[email protected]> wrote:
> Hi, Lauren’s,
>
> On Thu, Dec 18, 2025 at 11:43 PM Laurenz Albe <[email protected]>
> wrote:
>
>> On Thu, 2025-12-18 at 07:20 -0800, Igor Korot wrote:
>> > Imagine following scenario:
>> >
>> > I have 2 machines. One is running PG server on *nix. Second is my app
>> on Windows.
>> >
>> > An application starts for the first time.
>> >
>> > What is “clientencoding in this case?
>>
>> If I read the code correctly:
>>
>> - if PGCLIENTENCODING is set in the environment of the client executable,
>> that
>
>
> No it is not.
>
>
>>
>> - otherwise, if "client_encoding" is set on the server, that
>
>
I just checked the postgres.conf.
This file does not have any client_encoding.
> I guess I is.
> But what is the purpose of setting client encoding on the server? It is
> for the client
>
> Thank you.
>
>
>>
>> - otherwise, SQL_ASCII
>
>
Which means that this is an encoding that will be used.
But then I don’t understand anything.
The code I posted above worked fine on SELECT, but INSERT failed.
If the SQL_ASCII is the encoding used both operations should fail. Or both
succeeds.
Could someone explain what happened?
Thank you.
>>
>> Yours,
>> Laurenz Albe
>>
>
^ permalink raw reply [nested|flat] 19+ messages in thread
* Re: PQexecPrepared() question
2025-12-18 15:20 Re: PQexecPrepared() question Igor Korot <[email protected]>
2025-12-19 07:43 ` Re: PQexecPrepared() question Laurenz Albe <[email protected]>
2025-12-20 04:10 ` Re: PQexecPrepared() question Igor Korot <[email protected]>
@ 2025-12-20 06:24 ` Laurenz Albe <[email protected]>
2025-12-20 07:04 ` Re: PQexecPrepared() question Igor Korot <[email protected]>
2025-12-21 05:33 ` Re: PQexecPrepared() question Igor Korot <[email protected]>
0 siblings, 2 replies; 19+ messages in thread
From: Laurenz Albe @ 2025-12-20 06:24 UTC (permalink / raw)
To: Igor Korot <[email protected]>; pgsql-generallists.postgresql.org <[email protected]>
On Fri, 2025-12-19 at 20:10 -0800, Igor Korot wrote:
> > > > What is “clientencoding in this case?
> > >
> > > - if PGCLIENTENCODING is set in the environment of the client executable, that
> >
> > No it is not.
> >
> > > - otherwise, if "client_encoding" is set on the server, that
>
> I just checked the postgres.conf.
>
> This file does not have any client_encoding.
>
> > > - otherwise, SQL_ASCII
>
> Which means that this is an encoding that will be used.
You can verify that with the SQL statement "SHOW client_encoding"
in your sample program.
> But then I don’t understand anything.
>
> The code I posted above worked fine on SELECT, but INSERT failed.
>
> If the SQL_ASCII is the encoding used both operations should fail. Or both succeeds.
>
> Could someone explain what happened?
SQL_ASCII as client encoding means that no conversion will take place.
Still, the database encoding (I suspect UTF8) will govern what can be stored
in the database. Anything that is not valid UTF-8 will be rejected.
A SELECT will never cause an error - the client will just receive data
in UTF-8.
Yours,
Laurenz Albe
^ permalink raw reply [nested|flat] 19+ messages in thread
* Re: PQexecPrepared() question
2025-12-18 15:20 Re: PQexecPrepared() question Igor Korot <[email protected]>
2025-12-19 07:43 ` Re: PQexecPrepared() question Laurenz Albe <[email protected]>
2025-12-20 04:10 ` Re: PQexecPrepared() question Igor Korot <[email protected]>
2025-12-20 06:24 ` Re: PQexecPrepared() question Laurenz Albe <[email protected]>
@ 2025-12-20 07:04 ` Igor Korot <[email protected]>
2025-12-20 17:02 ` Re: PQexecPrepared() question Laurenz Albe <[email protected]>
1 sibling, 1 reply; 19+ messages in thread
From: Igor Korot @ 2025-12-20 07:04 UTC (permalink / raw)
To: Laurenz Albe <[email protected]>; +Cc: pgsql-generallists.postgresql.org <[email protected]>
Hi, Lauren’z,
On Fri, Dec 19, 2025 at 10:24 PM Laurenz Albe <[email protected]>
wrote:
> On Fri, 2025-12-19 at 20:10 -0800, Igor Korot wrote:
> > > > > What is “clientencoding in this case?
> > > >
> > > > - if PGCLIENTENCODING is set in the environment of the client
> executable, that
> > >
> > > No it is not.
> > >
> > > > - otherwise, if "client_encoding" is set on the server, that
> >
> > I just checked the postgres.conf.
> >
> > This file does not have any client_encoding.
> >
> > > > - otherwise, SQL_ASCII
> >
> > Which means that this is an encoding that will be used.
>
> You can verify that with the SQL statement "SHOW client_encoding"
> in your sample program.
Thx, will check.
>
> > But then I don’t understand anything.
> >
> > The code I posted above worked fine on SELECT, but INSERT failed.
> >
> > If the SQL_ASCII is the encoding used both operations should fail. Or
> both succeeds.
> >
> > Could someone explain what happened?
>
> SQL_ASCII as client encoding means that no conversion will take place.
>
> Still, the database encoding (I suspect UTF8) will govern what can be
> stored
> in the database. Anything that is not valid UTF-8 will be rejected.
Rejected how?
>
> A SELECT will never cause an error - the client will just receive data
> in UTF-8.
And then what?
I’ll check the encoding and report back..
Thank you.
>
> Yours,
> Laurenz Albe
>
^ permalink raw reply [nested|flat] 19+ messages in thread
* Re: PQexecPrepared() question
2025-12-18 15:20 Re: PQexecPrepared() question Igor Korot <[email protected]>
2025-12-19 07:43 ` Re: PQexecPrepared() question Laurenz Albe <[email protected]>
2025-12-20 04:10 ` Re: PQexecPrepared() question Igor Korot <[email protected]>
2025-12-20 06:24 ` Re: PQexecPrepared() question Laurenz Albe <[email protected]>
2025-12-20 07:04 ` Re: PQexecPrepared() question Igor Korot <[email protected]>
@ 2025-12-20 17:02 ` Laurenz Albe <[email protected]>
0 siblings, 0 replies; 19+ messages in thread
From: Laurenz Albe @ 2025-12-20 17:02 UTC (permalink / raw)
To: Igor Korot <[email protected]>; +Cc: pgsql-generallists.postgresql.org <[email protected]>
On Fri, 2025-12-19 at 23:04 -0800, Igor Korot wrote:
> > SQL_ASCII as client encoding means that no conversion will take place.
> >
> > Still, the database encoding (I suspect UTF8) will govern what can be stored
> > in the database. Anything that is not valid UTF-8 will be rejected.
>
> Rejected how?
As you experienced: by throwing an error.
> > A SELECT will never cause an error - the client will just receive data
> > in UTF-8.
>
> And then what?
The *client application* will receive UTF-8 data.
Yours,
Laurenz Albe
^ permalink raw reply [nested|flat] 19+ messages in thread
* Re: PQexecPrepared() question
2025-12-18 15:20 Re: PQexecPrepared() question Igor Korot <[email protected]>
2025-12-19 07:43 ` Re: PQexecPrepared() question Laurenz Albe <[email protected]>
2025-12-20 04:10 ` Re: PQexecPrepared() question Igor Korot <[email protected]>
2025-12-20 06:24 ` Re: PQexecPrepared() question Laurenz Albe <[email protected]>
@ 2025-12-21 05:33 ` Igor Korot <[email protected]>
2025-12-21 05:39 ` Re: PQexecPrepared() question Tom Lane <[email protected]>
1 sibling, 1 reply; 19+ messages in thread
From: Igor Korot @ 2025-12-21 05:33 UTC (permalink / raw)
To: Laurenz Albe <[email protected]>; pgsql-generallists.postgresql.org <[email protected]>
Hi, Laurenz,
On Fri, Dec 19, 2025 at 10:24 PM Laurenz Albe <[email protected]> wrote:
>
> On Fri, 2025-12-19 at 20:10 -0800, Igor Korot wrote:
> > > > > What is “clientencoding in this case?
> > > >
> > > > - if PGCLIENTENCODING is set in the environment of the client executable, that
> > >
> > > No it is not.
> > >
> > > > - otherwise, if "client_encoding" is set on the server, that
> >
> > I just checked the postgres.conf.
> >
> > This file does not have any client_encoding.
> >
> > > > - otherwise, SQL_ASCII
> >
> > Which means that this is an encoding that will be used.
>
> You can verify that with the SQL statement "SHOW client_encoding"
> in your sample program.
I added the following code in my app:
res = PQexec( m_db, "SHOW client_encoding" );
auto value = PQgetvalue( res, 0, 0 );
PQclear( res );
and the value of the "value" variable is "UTF8".
>
> > But then I don’t understand anything.
> >
> > The code I posted above worked fine on SELECT, but INSERT failed.
> >
> > If the SQL_ASCII is the encoding used both operations should fail. Or both succeeds.
> >
> > Could someone explain what happened?
>
> SQL_ASCII as client encoding means that no conversion will take place.
>
> Still, the database encoding (I suspect UTF8) will govern what can be stored
> in the database. Anything that is not valid UTF-8 will be rejected.
>
> A SELECT will never cause an error - the client will just receive data
> in UTF-8.
The exact error message is:
ERROR: invalid byte sequence for encoding UTF8: 0xdf
CONTEXT: unnamed portal parameter $1
on the INSERT.
Thank you.
>
> Yours,
> Laurenz Albe
^ permalink raw reply [nested|flat] 19+ messages in thread
* Re: PQexecPrepared() question
2025-12-18 15:20 Re: PQexecPrepared() question Igor Korot <[email protected]>
2025-12-19 07:43 ` Re: PQexecPrepared() question Laurenz Albe <[email protected]>
2025-12-20 04:10 ` Re: PQexecPrepared() question Igor Korot <[email protected]>
2025-12-20 06:24 ` Re: PQexecPrepared() question Laurenz Albe <[email protected]>
2025-12-21 05:33 ` Re: PQexecPrepared() question Igor Korot <[email protected]>
@ 2025-12-21 05:39 ` Tom Lane <[email protected]>
2025-12-21 21:19 ` Re: PQexecPrepared() question Peter J. Holzer <[email protected]>
0 siblings, 1 reply; 19+ messages in thread
From: Tom Lane @ 2025-12-21 05:39 UTC (permalink / raw)
To: Igor Korot <[email protected]>; +Cc: Laurenz Albe <[email protected]>; pgsql-generallists.postgresql.org <[email protected]>
Igor Korot <[email protected]> writes:
> I added the following code in my app:
> res = PQexec( m_db, "SHOW client_encoding" );
> auto value = PQgetvalue( res, 0, 0 );
> PQclear( res );
> and the value of the "value" variable is "UTF8".
Okay ...
> The exact error message is:
> ERROR: invalid byte sequence for encoding UTF8: 0xdf
> CONTEXT: unnamed portal parameter $1
> on the INSERT.
client_encoding governs both the encoding that the server will
send, and the encoding that it expects to receive. You are
sending a parameter string that is not valid UTF8.
regards, tom lane
^ permalink raw reply [nested|flat] 19+ messages in thread
* Re: PQexecPrepared() question
2025-12-18 15:20 Re: PQexecPrepared() question Igor Korot <[email protected]>
2025-12-19 07:43 ` Re: PQexecPrepared() question Laurenz Albe <[email protected]>
2025-12-20 04:10 ` Re: PQexecPrepared() question Igor Korot <[email protected]>
2025-12-20 06:24 ` Re: PQexecPrepared() question Laurenz Albe <[email protected]>
2025-12-21 05:33 ` Re: PQexecPrepared() question Igor Korot <[email protected]>
2025-12-21 05:39 ` Re: PQexecPrepared() question Tom Lane <[email protected]>
@ 2025-12-21 21:19 ` Peter J. Holzer <[email protected]>
2025-12-22 01:30 ` Re: PQexecPrepared() question Igor Korot <[email protected]>
0 siblings, 1 reply; 19+ messages in thread
From: Peter J. Holzer @ 2025-12-21 21:19 UTC (permalink / raw)
To: [email protected]
On 2025-12-21 00:39:23 -0500, Tom Lane wrote:
> Igor Korot <[email protected]> writes:
> > I added the following code in my app:
> > res = PQexec( m_db, "SHOW client_encoding" );
> > auto value = PQgetvalue( res, 0, 0 );
> > PQclear( res );
> > and the value of the "value" variable is "UTF8".
>
> Okay ...
>
> > The exact error message is:
> > ERROR: invalid byte sequence for encoding UTF8: 0xdf
> > CONTEXT: unnamed portal parameter $1
> > on the INSERT.
>
> client_encoding governs both the encoding that the server will
> send, and the encoding that it expects to receive. You are
> sending a parameter string that is not valid UTF8.
As I see it, Igor has two options:
1) Send UTF8. wcstombs is the wrong tool for this, since it uses the
current locale which is obviously not using UTF-8. I don't program in
C++, but I'm pretty sure there is a portable way to encode a string
in UTF-8.
2) Figure out which encoding his locale is using and then set
client_encoding appropriately. The first step to do that is to find
the current locale.
Igor, add
#include <ctype.h>
...
char *current_locale = setlocale(LC_CTYPE, "");
to your code. What's the value of current_locale?
I think 1) is the better strategy, but them I've been a UTF-8 fan-boy
for over 30 years ;-).
hjp
--
_ | Peter J. Holzer | Story must make more sense than reality.
|_|_) | |
| | | [email protected] | -- Charles Stross, "Creative writing
__/ | http://www.hjp.at/ | challenge!"
Attachments:
[application/pgp-signature] signature.asc (833B, 2-signature.asc)
download
^ permalink raw reply [nested|flat] 19+ messages in thread
* Re: PQexecPrepared() question
2025-12-18 15:20 Re: PQexecPrepared() question Igor Korot <[email protected]>
2025-12-19 07:43 ` Re: PQexecPrepared() question Laurenz Albe <[email protected]>
2025-12-20 04:10 ` Re: PQexecPrepared() question Igor Korot <[email protected]>
2025-12-20 06:24 ` Re: PQexecPrepared() question Laurenz Albe <[email protected]>
2025-12-21 05:33 ` Re: PQexecPrepared() question Igor Korot <[email protected]>
2025-12-21 05:39 ` Re: PQexecPrepared() question Tom Lane <[email protected]>
2025-12-21 21:19 ` Re: PQexecPrepared() question Peter J. Holzer <[email protected]>
@ 2025-12-22 01:30 ` Igor Korot <[email protected]>
2025-12-22 09:55 ` Re: PQexecPrepared() question Peter J. Holzer <[email protected]>
0 siblings, 1 reply; 19+ messages in thread
From: Igor Korot @ 2025-12-22 01:30 UTC (permalink / raw)
To: [email protected]
Hi,
On Sun, Dec 21, 2025 at 1:19 PM Peter J. Holzer <[email protected]> wrote:
>
> On 2025-12-21 00:39:23 -0500, Tom Lane wrote:
> > Igor Korot <[email protected]> writes:
> > > I added the following code in my app:
> > > res = PQexec( m_db, "SHOW client_encoding" );
> > > auto value = PQgetvalue( res, 0, 0 );
> > > PQclear( res );
> > > and the value of the "value" variable is "UTF8".
> >
> > Okay ...
> >
> > > The exact error message is:
> > > ERROR: invalid byte sequence for encoding UTF8: 0xdf
> > > CONTEXT: unnamed portal parameter $1
> > > on the INSERT.
> >
> > client_encoding governs both the encoding that the server will
> > send, and the encoding that it expects to receive. You are
> > sending a parameter string that is not valid UTF8.
>
> As I see it, Igor has two options:
>
> 1) Send UTF8. wcstombs is the wrong tool for this, since it uses the
> current locale which is obviously not using UTF-8. I don't program in
> C++, but I'm pretty sure there is a portable way to encode a string
> in UTF-8.
> 2) Figure out which encoding his locale is using and then set
> client_encoding appropriately. The first step to do that is to find
> the current locale.
> Igor, add
> #include <ctype.h>
> ...
> char *current_locale = setlocale(LC_CTYPE, "");
> to your code. What's the value of current_locale?
>
> I think 1) is the better strategy, but them I've been a UTF-8 fan-boy
> for over 30 years ;-).
Maybe all I need is something like this:
convert(char *dest, const std::wstring src)
{
const wchar_t *temp = src.c_str();
while( *dest )
{
dest++;
}
while( *temp )
{
*dest = *temp;
dest++;
temp++;
}
*dest++ = '\0';
*dest = '\0';
}
?
Thank you.
>
> hjp
>
> --
> _ | Peter J. Holzer | Story must make more sense than reality.
> |_|_) | |
> | | | [email protected] | -- Charles Stross, "Creative writing
> __/ | http://www.hjp.at/ | challenge!"
^ permalink raw reply [nested|flat] 19+ messages in thread
* Re: PQexecPrepared() question
2025-12-18 15:20 Re: PQexecPrepared() question Igor Korot <[email protected]>
2025-12-19 07:43 ` Re: PQexecPrepared() question Laurenz Albe <[email protected]>
2025-12-20 04:10 ` Re: PQexecPrepared() question Igor Korot <[email protected]>
2025-12-20 06:24 ` Re: PQexecPrepared() question Laurenz Albe <[email protected]>
2025-12-21 05:33 ` Re: PQexecPrepared() question Igor Korot <[email protected]>
2025-12-21 05:39 ` Re: PQexecPrepared() question Tom Lane <[email protected]>
2025-12-21 21:19 ` Re: PQexecPrepared() question Peter J. Holzer <[email protected]>
2025-12-22 01:30 ` Re: PQexecPrepared() question Igor Korot <[email protected]>
@ 2025-12-22 09:55 ` Peter J. Holzer <[email protected]>
2025-12-22 23:07 ` Re: PQexecPrepared() question Igor Korot <[email protected]>
0 siblings, 1 reply; 19+ messages in thread
From: Peter J. Holzer @ 2025-12-22 09:55 UTC (permalink / raw)
To: [email protected]
On 2025-12-21 17:30:38 -0800, Igor Korot wrote:
> On Sun, Dec 21, 2025 at 1:19 PM Peter J. Holzer <[email protected]> wrote:
> > As I see it, Igor has two options:
> >
> > 1) Send UTF8. wcstombs is the wrong tool for this, since it uses the
> > current locale which is obviously not using UTF-8. I don't program in
> > C++, but I'm pretty sure there is a portable way to encode a string
> > in UTF-8.
[...]
> > I think 1) is the better strategy, but them I've been a UTF-8 fan-boy
> > for over 30 years ;-).
>
> Maybe all I need is something like this:
>
> convert(char *dest, const std::wstring src)
> {
> const wchar_t *temp = src.c_str();
> while( *dest )
> {
> dest++;
> }
> while( *temp )
> {
> *dest = *temp;
> dest++;
> temp++;
> }
> *dest++ = '\0';
> *dest = '\0';
> }
>
> ?
I hope your compiler complains loudly about that code. You can't just
assign a wchar_t to a char and expect it to do anything sensible.
A short search points to std::wstring_convert, but that has been
deprecated in C++17, and it's not obvious to me what the replacement is.
There is always ICU, of course, but that feels like cracking a nut with
a sledgehammer.
Personally, I might roll my own if an hour of googling doesn't turn up
anything promising (UTF-8 encoding is quite simple), but that shouldn't
be necessary.
hjp
--
_ | Peter J. Holzer | Story must make more sense than reality.
|_|_) | |
| | | [email protected] | -- Charles Stross, "Creative writing
__/ | http://www.hjp.at/ | challenge!"
Attachments:
[application/pgp-signature] signature.asc (833B, 2-signature.asc)
download
^ permalink raw reply [nested|flat] 19+ messages in thread
* Re: PQexecPrepared() question
2025-12-18 15:20 Re: PQexecPrepared() question Igor Korot <[email protected]>
2025-12-19 07:43 ` Re: PQexecPrepared() question Laurenz Albe <[email protected]>
2025-12-20 04:10 ` Re: PQexecPrepared() question Igor Korot <[email protected]>
2025-12-20 06:24 ` Re: PQexecPrepared() question Laurenz Albe <[email protected]>
2025-12-21 05:33 ` Re: PQexecPrepared() question Igor Korot <[email protected]>
2025-12-21 05:39 ` Re: PQexecPrepared() question Tom Lane <[email protected]>
2025-12-21 21:19 ` Re: PQexecPrepared() question Peter J. Holzer <[email protected]>
2025-12-22 01:30 ` Re: PQexecPrepared() question Igor Korot <[email protected]>
2025-12-22 09:55 ` Re: PQexecPrepared() question Peter J. Holzer <[email protected]>
@ 2025-12-22 23:07 ` Igor Korot <[email protected]>
2025-12-25 01:28 ` Re: PQexecPrepared() question Igor Korot <[email protected]>
0 siblings, 1 reply; 19+ messages in thread
From: Igor Korot @ 2025-12-22 23:07 UTC (permalink / raw)
To: [email protected]
Hi, Peter,
On Mon, Dec 22, 2025 at 1:55 AM Peter J. Holzer <[email protected]> wrote:
>
> On 2025-12-21 17:30:38 -0800, Igor Korot wrote:
> > On Sun, Dec 21, 2025 at 1:19 PM Peter J. Holzer <[email protected]> wrote:
> > > As I see it, Igor has two options:
> > >
> > > 1) Send UTF8. wcstombs is the wrong tool for this, since it uses the
> > > current locale which is obviously not using UTF-8. I don't program in
> > > C++, but I'm pretty sure there is a portable way to encode a string
> > > in UTF-8.
> [...]
> > > I think 1) is the better strategy, but them I've been a UTF-8 fan-boy
> > > for over 30 years ;-).
> >
> > Maybe all I need is something like this:
> >
> > convert(char *dest, const std::wstring src)
> > {
> > const wchar_t *temp = src.c_str();
> > while( *dest )
> > {
> > dest++;
> > }
> > while( *temp )
> > {
> > *dest = *temp;
> > dest++;
> > temp++;
> > }
> > *dest++ = '\0';
> > *dest = '\0';
> > }
> >
> > ?
>
> I hope your compiler complains loudly about that code. You can't just
> assign a wchar_t to a char and expect it to do anything sensible.
At least MSVC 2017 did not.
However, trying didn't solve it. Same error.
>
> A short search points to std::wstring_convert, but that has been
> deprecated in C++17, and it's not obvious to me what the replacement is.
>
> There is always ICU, of course, but that feels like cracking a nut with
> a sledgehammer.
I think ICU IS a replacement for wstring_comvert.
Will check it.
>
> Personally, I might roll my own if an hour of googling doesn't turn up
> anything promising (UTF-8 encoding is quite simple), but that shouldn't
> be necessary.
>
>
> hjp
>
> --
> _ | Peter J. Holzer | Story must make more sense than reality.
> |_|_) | |
> | | | [email protected] | -- Charles Stross, "Creative writing
> __/ | http://www.hjp.at/ | challenge!"
^ permalink raw reply [nested|flat] 19+ messages in thread
* Re: PQexecPrepared() question
2025-12-18 15:20 Re: PQexecPrepared() question Igor Korot <[email protected]>
2025-12-19 07:43 ` Re: PQexecPrepared() question Laurenz Albe <[email protected]>
2025-12-20 04:10 ` Re: PQexecPrepared() question Igor Korot <[email protected]>
2025-12-20 06:24 ` Re: PQexecPrepared() question Laurenz Albe <[email protected]>
2025-12-21 05:33 ` Re: PQexecPrepared() question Igor Korot <[email protected]>
2025-12-21 05:39 ` Re: PQexecPrepared() question Tom Lane <[email protected]>
2025-12-21 21:19 ` Re: PQexecPrepared() question Peter J. Holzer <[email protected]>
2025-12-22 01:30 ` Re: PQexecPrepared() question Igor Korot <[email protected]>
2025-12-22 09:55 ` Re: PQexecPrepared() question Peter J. Holzer <[email protected]>
2025-12-22 23:07 ` Re: PQexecPrepared() question Igor Korot <[email protected]>
@ 2025-12-25 01:28 ` Igor Korot <[email protected]>
2025-12-25 09:32 ` Re: PQexecPrepared() question Laurenz Albe <[email protected]>
0 siblings, 1 reply; 19+ messages in thread
From: Igor Korot @ 2025-12-25 01:28 UTC (permalink / raw)
To: [email protected]
Hi, ALL,
My current code is below:
std::wstring query1 = L"SELECT t.table_catalog AS catalog,
t.table_schema AS schema, t.table_name AS table, u.usename AS owner,
c.oid AS table_id FROM information_schema.tables t,
pg_catalog.pg_class c, pg_catalog.pg_user u WHERE t.table_name =
c.relname AND c.relowner = usesysid AND (t.table_type = 'BASE TABLE'
OR t.table_type = 'VIEW' OR t.table_type = 'LOCAL TEMPORARY') ORDER BY
table_name;";
std::wstring query7 = L"INSERT INTO \"abcattbl\" VALUES(
0, $1, (SELECT c.oid FROM pg_class c, pg_namespace nc WHERE nc.oid =
c.relnamespace AND c.relname = $2 AND nc.nspname = $3), \'\', 8, 400,
\'N\', \'N\', 0, 1, 0, \'MS Sans Serif\', 8, 400, \'N\', \'N\', 0, 1,
0, \'MS Sans Serif\', 8, 400, \'N\', \'N\', 0, 1, 0, \'MS Sans
Serif\', \'\' ) ON CONFLICT DO NOTHING;";
for( int i = 0; i < PQntuples( res ) && !result; i++ )
{
std::wstring cat = m_pimpl->m_myconv.from_bytes(
PQgetvalue( res, i, 0 ) );
std::wstring schema = m_pimpl->m_myconv.from_bytes(
PQgetvalue( res, i, 1 ) );
std::wstring table = m_pimpl->m_myconv.from_bytes(
PQgetvalue( res, i, 2 ) );
std::wstring table_owner = m_pimpl->m_myconv.from_bytes(
PQgetvalue( res, i, 3 ) );
pimpl.m_tableDefinitions[cat].push_back( TableDefinition(
cat, schema, table ) );
count++;
paramValues = schema + L"." + table;
params[0] = new char[paramValues.length() + 2];
memset( params[0], '\0', paramValues.length() + 2 );
auto temp = m_pimpl->m_myconv.to_bytes(
paramValues.c_str() ).c_str();
params[0] = const_cast<char *>(
m_pimpl->m_myconv.to_bytes( paramValues.c_str() ).c_str() );
params[1] = new char[table.length() + 2];
memset( params[1], '\0', table.length() + 2 );
params[1] = const_cast<char *>(
m_pimpl->m_myconv.to_bytes( table.c_str() ).c_str() );
params[2] = new char[table_owner.length() + 2];
memset( params[2], '\0', table_owner.length() + 2 );
params[2] = const_cast<char *>(
m_pimpl->m_myconv.to_bytes( table_owner.c_str() ).c_str() );
paramFormat[0] = paramFormat[1] = paramFormat[2] = 0;
paramLength[0] = paramValues.length();
paramLength[1] = table.length();
paramLength[2] = table_owner.length();
res8 = PQexecPrepared( m_db, "set_table_prop", 3, params,
paramLength, paramFormat, 0 );
if( PQresultStatus( res8 ) != PGRES_COMMAND_OK )
{
std::wstring err = m_pimpl->m_myconv.from_bytes(
PQerrorMessage( m_db ) );
errorMsg.push_back( L"Error executing query: " + err );
result = 1;
}
PQclear( res8 );
delete[] params[0];
params[0] = nullptr;
delete[] params[1];
params[1] = nullptr;
delete[] params[2];
params[2] = nullptr;
}
This code crashes on the PQexecParam.
I am completely puzzled.
Could anyone help?
Thank you.
On Mon, Dec 22, 2025 at 3:07 PM Igor Korot <[email protected]> wrote:
>
> Hi, Peter,
>
> On Mon, Dec 22, 2025 at 1:55 AM Peter J. Holzer <[email protected]> wrote:
> >
> > On 2025-12-21 17:30:38 -0800, Igor Korot wrote:
> > > On Sun, Dec 21, 2025 at 1:19 PM Peter J. Holzer <[email protected]> wrote:
> > > > As I see it, Igor has two options:
> > > >
> > > > 1) Send UTF8. wcstombs is the wrong tool for this, since it uses the
> > > > current locale which is obviously not using UTF-8. I don't program in
> > > > C++, but I'm pretty sure there is a portable way to encode a string
> > > > in UTF-8.
> > [...]
> > > > I think 1) is the better strategy, but them I've been a UTF-8 fan-boy
> > > > for over 30 years ;-).
> > >
> > > Maybe all I need is something like this:
> > >
> > > convert(char *dest, const std::wstring src)
> > > {
> > > const wchar_t *temp = src.c_str();
> > > while( *dest )
> > > {
> > > dest++;
> > > }
> > > while( *temp )
> > > {
> > > *dest = *temp;
> > > dest++;
> > > temp++;
> > > }
> > > *dest++ = '\0';
> > > *dest = '\0';
> > > }
> > >
> > > ?
> >
> > I hope your compiler complains loudly about that code. You can't just
> > assign a wchar_t to a char and expect it to do anything sensible.
>
> At least MSVC 2017 did not.
> However, trying didn't solve it. Same error.
>
> >
> > A short search points to std::wstring_convert, but that has been
> > deprecated in C++17, and it's not obvious to me what the replacement is.
> >
> > There is always ICU, of course, but that feels like cracking a nut with
> > a sledgehammer.
>
> I think ICU IS a replacement for wstring_comvert.
> Will check it.
>
> >
> > Personally, I might roll my own if an hour of googling doesn't turn up
> > anything promising (UTF-8 encoding is quite simple), but that shouldn't
> > be necessary.
> >
> >
> > hjp
> >
> > --
> > _ | Peter J. Holzer | Story must make more sense than reality.
> > |_|_) | |
> > | | | [email protected] | -- Charles Stross, "Creative writing
> > __/ | http://www.hjp.at/ | challenge!"
^ permalink raw reply [nested|flat] 19+ messages in thread
* Re: PQexecPrepared() question
2025-12-18 15:20 Re: PQexecPrepared() question Igor Korot <[email protected]>
2025-12-19 07:43 ` Re: PQexecPrepared() question Laurenz Albe <[email protected]>
2025-12-20 04:10 ` Re: PQexecPrepared() question Igor Korot <[email protected]>
2025-12-20 06:24 ` Re: PQexecPrepared() question Laurenz Albe <[email protected]>
2025-12-21 05:33 ` Re: PQexecPrepared() question Igor Korot <[email protected]>
2025-12-21 05:39 ` Re: PQexecPrepared() question Tom Lane <[email protected]>
2025-12-21 21:19 ` Re: PQexecPrepared() question Peter J. Holzer <[email protected]>
2025-12-22 01:30 ` Re: PQexecPrepared() question Igor Korot <[email protected]>
2025-12-22 09:55 ` Re: PQexecPrepared() question Peter J. Holzer <[email protected]>
2025-12-22 23:07 ` Re: PQexecPrepared() question Igor Korot <[email protected]>
2025-12-25 01:28 ` Re: PQexecPrepared() question Igor Korot <[email protected]>
@ 2025-12-25 09:32 ` Laurenz Albe <[email protected]>
0 siblings, 0 replies; 19+ messages in thread
From: Laurenz Albe @ 2025-12-25 09:32 UTC (permalink / raw)
To: Igor Korot <[email protected]>; [email protected]
On Wed, 2025-12-24 at 17:28 -0800, Igor Korot wrote:
> My current code is below:
>
> [...]
> res8 = PQexecPrepared( m_db, "set_table_prop", 3, params, paramLength, paramFormat, 0 );
> [...]
>
> This code crashes on the PQexecParam.
Your code does not contain PQexecParam.
It is also incomplete and doesn't contain where the statement is prepared,
nor does it contain the definition of the variables you are using.
I recommend using a debugger.
Yours,
Laurenz Albe
^ permalink raw reply [nested|flat] 19+ messages in thread
end of thread, other threads:[~2025-12-25 09:32 UTC | newest]
Thread overview: 19+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2025-12-18 15:20 Re: PQexecPrepared() question Igor Korot <[email protected]>
2025-12-18 15:40 ` David G. Johnston <[email protected]>
2025-12-19 07:43 ` Laurenz Albe <[email protected]>
2025-12-19 18:49 ` Igor Korot <[email protected]>
2025-12-19 18:52 ` Tom Lane <[email protected]>
2025-12-19 19:12 ` Ron Johnson <[email protected]>
2025-12-19 21:21 ` Adrian Klaver <[email protected]>
2025-12-20 04:10 ` Igor Korot <[email protected]>
2025-12-20 06:24 ` Laurenz Albe <[email protected]>
2025-12-20 07:04 ` Igor Korot <[email protected]>
2025-12-20 17:02 ` Laurenz Albe <[email protected]>
2025-12-21 05:33 ` Igor Korot <[email protected]>
2025-12-21 05:39 ` Tom Lane <[email protected]>
2025-12-21 21:19 ` Peter J. Holzer <[email protected]>
2025-12-22 01:30 ` Igor Korot <[email protected]>
2025-12-22 09:55 ` Peter J. Holzer <[email protected]>
2025-12-22 23:07 ` Igor Korot <[email protected]>
2025-12-25 01:28 ` Igor Korot <[email protected]>
2025-12-25 09:32 ` Laurenz Albe <[email protected]>
This inbox is served by agora; see mirroring instructions
for how to clone and mirror all data and code used for this inbox