pgsql-general
❮
Why that query fails?
- Jump to comment-1Igor Korot<ikorot01@gmail.com>Apr 26, 2026, 8:15 PM UTCHi, ALL,
[quote]
Thread 1 "dbhandler" hit Breakpoint 1,
PostgresDatabase::CreateDatabase (this=0x616000180c80,
weak count 0) = {...},name=L"test", opts=std::shared_ptr<CreateDBOptions> (use count 1,
../../libpostgres/database_postgres.cpp:46errorMsg=std::vector of length 0, capacity 0) at
46 int result = 0;
(gdb) n
47 auto exists = false;
(gdb)
48 std::vector<std::wstring> dbList;
(gdb)
49 std::wstring qry0, qry1, qry2;
(gdb)
50 qry0 = L"SELECT 1 FROM pg_database WHERE datname = $1";
(gdb)
51 if( opts->m_exist )
(gdb)
54 values[0] = NULL;
(gdb)
55 values[0] = new char[name.length() * sizeof( wchar_t ) + 1];
(gdb)
56 memset( values[0], '\0', name.length() * sizeof( wchar_t ) + 1 );
(gdb)
57 strcpy( values[0], mpimpl->mmyconv.to_bytes(
name.cstr() ).cstr() );
(gdb)
58 int len1 = (int) name.length() * sizeof( wchar_t );
(gdb)
59 int length[1] = { len1 };
(gdb)
60 int formats[1] = { 1 };
(gdb)
61 auto res = PQexecParams( m_db,
mpimpl->mmyconv.tobytes( qry0.cstr() ).c_str(), 1, NULL, values,
length, formats, 0 );
(gdb)
62 auto status = PQresultStatus( res );
(gdb)
63 if( status != PGRESTUPLESOK )
(gdb) p status
$1 = PGRESFATALERROR
(gdb) n
65 std::wstring err = mpimpl->mmyconv.from_bytes(
PQerrorMessage( m_db ) );
(gdb)
66 errorMsg.push_back( L"Error executing query: " + err );
(gdb) p err
$2 = L"ERROR: invalid byte sequence for encoding \"UTF8\":
0x00\nCONTEXT: unnamed portal parameter $1\n"
(gdb)
[/quote]
What am I doing wrong?
Thank you.- Jump to comment-1Adrian Klaver<adrian.klaver@aklaver.com>Apr 26, 2026, 8:22 PM UTCOn 4/26/26 1:15 PM, Igor Korot wrote:
Hi, ALL,
[quote]
Thread 1 "dbhandler" hit Breakpoint 1,66 errorMsg.push_back( L"Error executing query: " + err );
I would think the above is a clue.
(gdb) p err
$2 = L"ERROR: invalid byte sequence for encoding \"UTF8\":
Are you sure the value you are passing to the parameter is UTF8?0x00\nCONTEXT: unnamed portal parameter $1\n"
(gdb)
[/quote]What am I doing wrong?
adrian.klaver@aklaver.com
Thank you.
-- Adrian Klaver- Jump to comment-1Igor Korot<ikorot01@gmail.com>Apr 26, 2026, 8:34 PM UTCAdrian,
On Sun, Apr 26, 2026 at 3:22 PM Adrian Klaver <adrian.klaver@aklaver.com> wrote:
On 4/26/26 1:15 PM, Igor Korot wrote:Hi, ALL,
[quote]
Thread 1 "dbhandler" hit Breakpoint 1,66 errorMsg.push_back( L"Error executing query: " + err );
(gdb) p err
$2 = L"ERROR: invalid byte sequence for encoding \"UTF8\":
Positive.
I would think the above is a clue.
Are you sure the value you are passing to the parameter is UTF8?
This is what the following line should be doing:
[code]
57 strcpy( values[0], mpimpl->mmyconv.to_bytes(
name.cstr() ).cstr() );
[/code]
Thank you.0x00\nCONTEXT: unnamed portal parameter $1\n"
(gdb)
[/quote]
What am I doing wrong?
Thank you.
--
Adrian Klaver
adrian.klaver@aklaver.com- Jump to comment-1Igor Korot<ikorot01@gmail.com>Apr 26, 2026, 8:39 PM UTCAdrian,
On Sun, Apr 26, 2026 at 3:34 PM Igor Korot <ikorot01@gmail.com> wrote:
Adrian,On Sun, Apr 26, 2026 at 3:22 PM Adrian Klaver <adrian.klaver@aklaver.com> wrote:
On 4/26/26 1:15 PM, Igor Korot wrote:Hi, ALL,
[quote]
Thread 1 "dbhandler" hit Breakpoint 1,66 errorMsg.push_back( L"Error executing query: " + err );
(gdb) p err
$2 = L"ERROR: invalid byte sequence for encoding \"UTF8\":
I would think the above is a clue.
Are you sure the value you are passing to the parameter is UTF8?
Positive.
This is what the following line should be doing:
[code]57 strcpy( values[0], mpimpl->mmyconv.to_bytes(
name.cstr() ).cstr() );
And the conversion is as follows:
[/code]
[code]
[/code]std::wstring_convert<std::codecvt_utf8<wchar_t> > m_myconv;
Thank you.
Thank you.0x00\nCONTEXT: unnamed portal parameter $1\n"
(gdb)
[/quote]
What am I doing wrong?
Thank you.
--
Adrian Klaver
adrian.klaver@aklaver.com- Jump to comment-1Igor Korot<ikorot01@gmail.com>Apr 26, 2026, 10:06 PM UTCAdrian,
On Sun, Apr 26, 2026 at 3:38 PM Igor Korot <ikorot01@gmail.com> wrote:
Adrian,On Sun, Apr 26, 2026 at 3:34 PM Igor Korot <ikorot01@gmail.com> wrote:
Adrian,On Sun, Apr 26, 2026 at 3:22 PM Adrian Klaver <adrian.klaver@aklaver.com> wrote:
On 4/26/26 1:15 PM, Igor Korot wrote:Hi, ALL,
[quote]
Thread 1 "dbhandler" hit Breakpoint 1,66 errorMsg.push_back( L"Error executing query: " + err );
(gdb) p err
$2 = L"ERROR: invalid byte sequence for encoding \"UTF8\":
I would think the above is a clue.
Are you sure the value you are passing to the parameter is UTF8?
Positive.
This is what the following line should be doing:
[code]57 strcpy( values[0], mpimpl->mmyconv.to_bytes(
name.cstr() ).cstr() );
[/code]
And the conversion is as follows:
[code]std::wstringconvert<std::codecvtutf8<wchart> > mmyconv;
[/code]
Those are the values from gdb:
Thank you.
[code]
(gdb) p values[0]
$1 = 0x603001613380 "test"
(gdb) p strlen(values[0])
$2 = 4
(gdb)
[/code]
Thank you.
Thank you.0x00\nCONTEXT: unnamed portal parameter $1\n"
(gdb)
[/quote]
What am I doing wrong?
Thank you.
--
Adrian Klaver
adrian.klaver@aklaver.com- Jump to comment-1Ron Johnson<ronljohnsonjr@gmail.com>Apr 26, 2026, 10:16 PM UTCWhen in doubt, run the query interactively.
On Sun, Apr 26, 2026 at 6:06 PM Igor Korot <ikorot01@gmail.com> wrote:Adrian,
On Sun, Apr 26, 2026 at 3:38 PM Igor Korot <ikorot01@gmail.com> wrote:
Adrian,On Sun, Apr 26, 2026 at 3:34 PM Igor Korot <ikorot01@gmail.com> wrote:
Adrian,
On Sun, Apr 26, 2026 at 3:22 PM Adrian Klaver <adrian.klaver@aklaver.com> wrote:
On 4/26/26 1:15 PM, Igor Korot wrote:Hi, ALL,
[quote]
Thread 1 "dbhandler" hit Breakpoint 1,66 errorMsg.push_back( L"Error executing query: " +
err );
(gdb) p err
$2 = L"ERROR: invalid byte sequence for encoding \"UTF8\":
I would think the above is a clue.
Are you sure the value you are passing to the parameter is UTF8?
Positive.
This is what the following line should be doing:
[code]57 strcpy( values[0], mpimpl->mmyconv.to_bytes(
name.cstr() ).cstr() );
[/code]
And the conversion is as follows:
[code]std::wstringconvert<std::codecvtutf8<wchart> > mmyconv;
[/code]
Thank you.
Those are the values from gdb:
[code]
(gdb) p values[0]
$1 = 0x603001613380 "test"
(gdb) p strlen(values[0])
$2 = 4
(gdb)
[/code]
Thank you.
Thank you.0x00\nCONTEXT: unnamed portal parameter $1\n"
(gdb)
[/quote]
What am I doing wrong?
Thank you.
--
Adrian Klaver
adrian.klaver@aklaver.com
--
Death to <Redacted>, and butter sauce.
Don't boil me, I'm still alive.
<Redacted> lobster!
- Jump to comment-1Greg Sabino Mullane<htamfids@gmail.com>Apr 26, 2026, 10:17 PM UTCYou are using binary format, but your length calculation is wrong:
int len1 = (int) name.length() * sizeof( wchar_t );
Just use text format for a query like this.- Jump to comment-1Tom Lane<tgl@sss.pgh.pa.us>Apr 26, 2026, 10:59 PM UTCGreg Sabino Mullane <htamfids@gmail.com> writes:
You are using binary format, but your length calculation is wrong:
I think the actual problem is that he's trying to send an array
int len1 = (int) name.length() * sizeof( wchar_t );
of wchar_t to the server. That is not bit-compatible with UTF-8,
even though they are both representations of Unicode. The query
string itself has the same problem I think (I believe the L"..."
syntax in C produces an array of wchar_t).regards, tom lane