Subject: [postgresql-interfaces/psqlodbc] issue #98: SELECT of a PostgreSQL CHAR/VARCHAR/TEXT Column Using multiple calls SQLGetData results in data truncation (last byte)

From: "raddakal (@raddakal)" <[email-masked]>
Date: Tue, 18 Mar 2025 05:44:32 +0000

When We execute the SELECT query on a table containing a TEXT column with one record of size 1048000 bytes, our ODBC application is able to retrieve one byte less than the actual data.

DDL and DML used in the test
------------------------------

postgres=# create table public.rantexttab(textcol text);
CREATE TABLE

postgres=# insert into public.rantexttab values(repeat('A', 1048000) || 'BB');
INSERT 0 1
postgres=# select length(textcol) from public.rantexttab;
 length  
---------
 1048002
(1 row)

Please note that We are using SQL_C_BINARY as the c type to receive the text column data.

We had allocated a buffer of 1048000 in our ODBC program and the size of teh actual data was 1048002, so 2 subsequent SQLGetData calls were made. In the first call, driver returned correctly 1048000 bytes and the in the second call  with the same buffer, driver returns two bytes the last byte contained a null (instead of teh actual last byte in the data).

When we did some more analysis, it seems multiple calls to SQLGetData is causing the issue (i.e., there seems to be some gap in the driver code when multiple calls to SQLGetData are made to retrieve the data of the same column in the result set).

Tried to shorten the test case, and instead of using the size of 1048002 bytes, we could reproduce the same issue using 12 bytes of data in TEXT column. When a buffer of size 10 bytes is used and 2 calls to SQLGetData being made, in the second call we could see the last byte was null instead of actual last byte in the column data.

In the shortened test case, the data used:

postgres=# select length(textcol) from public.rantexttab;
 length 
--------
     12
(1 row)

postgres=# SELECT textcol from public.rantexttab
postgres-# ;
   textcol    
--------------
 AAAAAAAAAABB
(1 row)

Standalone ODBC program output:
----------------------------------

bash-4.4$ ./a.out 
Connecting to database.
Connected to database.
Fetching results...
First Call to GetData: 
ind : 12
Data: AAAAAAAAAA

Second Call to GetData: 
ind : 2
Data: B
Free handles.

We can see that in the second call instead of getting BB, we get only B.

Thanks.

[pglob.cpp.txt](https://github.com/user-attachments/files/19307243/pglob.cpp.txt)

---

From: "raddakal (@raddakal)" <[email-masked]>
Date: Tue, 18 Mar 2025 13:54:23 +0000

The same issue is seen with CHAR and VARCHAR type columns as well, along with the TEXT type column. The BYTEA type column does not have this issue.

---

From: "davecramer (@davecramer)" <[email-masked]>
Date: Fri, 21 Mar 2025 00:14:38 +0000

cool, thanks for the report

---

From: "davecramer (@davecramer)" <[email-masked]>
Date: Mon, 24 Mar 2025 19:47:17 +0000

Are you using the latest version?
I just ran your code with some slight changes to connect and the result was 
```
./pglob                                                                        
Connecting to database.
Connected to database.
Fetching results...
First Call to GetData: 
ind : 1048002
Data: AAAAAAAAAA��
Second Call to GetData: 
ind : 1047992
Data: AAAAAAAAAA��
Free handles.
```

---

From: "raddakal (@raddakal)" <[email-masked]>
Date: Mon, 16 Jun 2025 05:53:52 +0000

Can you please confirm:
1) if you could reproduce the reported issue?
2) And the slight changes you have made could resolve the issue? If yes, kindly share the changes.

Thanks.

---

From: "davecramer (@davecramer)" <[email-masked]>
Date: Mon, 16 Jun 2025 10:30:59 +0000

I cannot reproduce the issue.

```
create table public.rantexttab(textcol text);
CREATE TABLE
test=# insert into public.rantexttab values(repeat('A', 1048000) || 'BB');
INSERT 0 1
test=# select length(textcol) from public.rantexttab;
 length
---------
 1048002
(1 row)
```
```
diff pglob.cpp ~/Downloads/pglob.cpp.txt
48,52c48,51
<     ret = SQLConnect(hdlDbc,
<         (SQLCHAR*) "psqlodbc_test_dsn", SQL_NTS,
<         (SQLCHAR*) "test", SQL_NTS,
<         (SQLCHAR*)"test", SQL_NTS);
<
---
>     const char* dsnName = "pg16";
>     const char* userID = "postgres";
>     const char* passwd = "postgres";
>     ret = SQLConnect(hdlDbc, (SQLCHAR*)dsnName, SQL_NTS,(SQLCHAR*)userID,SQL_NTS, (SQLCHAR*)passwd, SQL_NTS);
```

Here is the output from my run
```
./pglob
Connecting to database.
Connected to database.
Fetching results...
First Call to GetData:
ind : 1048002
Data: AAAAAAAAAA�
Second Call to GetData:
ind : 1047992
Data: AAAAAAAAAA�
Free handles.
```

---

From: "Spark098 (@Spark098)" <[email-masked]>
Date: Tue, 08 Jul 2025 06:48:01 +0000

Hello @davecramer 

Can you please try with the shortened test case (as mentioned in the thread before) when we are using buffer of size 10 and inserting data of size 12 bytes of data in the TEXT column.
I could reproduce the issue. 
Native driver version is 16.00.0000

standalone output:

Connecting to database.
Connected to database.
Fetching results...
First Call to GetData:
ind : 12
Data: AAAAAAAAAA
Second Call to GetData:
ind : 2
Data: B
Free handles.

postgres=# select * from rantexttab;
   textcol
--------------
 AAAAAAAAAABB
(1 row)

---

From: "davecramer (@davecramer)" <[email-masked]>
Date: Sat, 12 Jul 2025 20:27:12 +0000

Please use the latest driver.

Dave Cramer

On Tue, 8 Jul 2025 at 02:48, Spark098 ***@***.***> wrote:

> *Spark098* left a comment (postgresql-interfaces/psqlodbc#98)
> <https://github.com/postgresql-interfaces/psqlodbc/issues/98#issuecomment-3047585425>
>
> Hello @davecramer <https://github.com/davecramer>
>
> Can you please try with the shortened test case (as mentioned in the
> thread before) when we are using buffer of size 10 and inserting data of
> size 12 bytes of data in the TEXT column.
> —
> Reply to this email directly, view it on GitHub
> <https://github.com/postgresql-interfaces/psqlodbc/issues/98#issuecomment-3047585425>,
> or unsubscribe
> <https://github.com/notifications/unsubscribe-auth/AADDH5TZWI7H75C7277ZHB33HNSTNAVCNFSM6AAAAABZHG6QMSVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZTANBXGU4DKNBSGU>
> .
> You are receiving this because you were mentioned.Message ID:
> ***@***.***>
>

---

From: "Spark098 (@Spark098)" <[email-masked]>
Date: Mon, 25 Aug 2025 16:53:17 +0000

I tried with the latest driver but still facing the same issue. (last byte is truncated)
can you please check ?

> […](#)

---

From: "logjing (@logjing)" <[email-masked]>
Date: Wed, 01 Apr 2026 02:00:21 +0000

maybe it's solved by https://github.com/postgresql-interfaces/psqlodbc/pull/145