我试图在Linux上复制这个问题,但失败了.我目前没有可用的Windows系统.我创建了一个测试C++文件,该文件向PostgreSQL15.2写入大文本,并且能够使用libpq5毫无问题地写入该文件.
System个
uname -a
Linux 75db15f94bd2 5.15.49-linuxkit #1 SMP Tue Sep 13 07:51:46 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux
cat /etc/*release
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=22.04
DISTRIB_CODENAME=jammy
DISTRIB_DESCRIPTION="Ubuntu 22.04.2 LTS"
PRETTY_NAME="Ubuntu 22.04.2 LTS
DB个
psql (15.2 (Ubuntu 15.2-1.pgdg22.04+1))
g++个
g++ --version
g++ (Ubuntu 11.3.0-1ubuntu1~22.04) 11.3.0
libpq个
apt list --installed | grep libpq
libpq-dev/jammy-pgdg,now 15.2-1.pgdg22.04+1 amd64 [installed]
libpq5/jammy-pgdg,now 15.2-1.pgdg22.04+1 amd64 [installed]
Table个
test=# \dt
List of relations
Schema | Name | Type | Owner
--------+-----------+-------+----------
public | knowledge | table | postgres
(1 row)
test=# \d knowledge
Table "public.knowledge"
Column | Type | Collation | Nullable | Default
-----------+------------------------+-----------+----------+---------
userid | character varying(255) | | |
knowledge | text | | |
C++ code个
#include <stdio.h>
#include <postgresql/libpq-fe.h>
#include <string>
#include <iostream>
int main()
{
PGconn *conn;
PGresult *res;
int rec_count;
int row;
int col;
conn = PQconnectdb("dbname=test host=localhost user=postgres password=test");
if (PQstatus(conn) == CONNECTION_BAD)
{
puts("We were unable to connect to the database");
exit(0);
}
res = PQexec(conn, "select userid, knowledge from knowledge");
if (PQresultStatus(res) != PGRES_TUPLES_OK)
{
puts("We did not get any data!");
exit(0);
}
rec_count = PQntuples(res);
printf("We received %d records.\n", rec_count);
puts("==========================");
PQclear(res);
const char command[] = "insert into knowledge values($1, $2);";
char cid[] = "10";
char name[] = "aaaaaaaaaa bbbbbb...many, many characters";
int nParams = 2;
const char *const paramValues[] = {cid, name};
const int paramLengths[] = {sizeof(cid), sizeof(name)};
const int paramFormats[] = {0, 0};
int resultFormat = 0;
res = PQexecParams(conn, command, nParams, NULL, paramValues, paramLengths, paramFormats,resultFormat);
if (PQresultStatus(res) != PGRES_COMMAND_OK)
{
puts("Data NOT entered");
std::cout << "PQexecParams failed: " << PQresultErrorMessage(res) << std::endl;
exit(0);
}
PQclear(res);
PQfinish(conn);
return 0;
}
Compile and run个
g++ test.cpp -lpq
./a.out
We received 5 records.
==========================
在打印输出之后,它还执行插入操作.
现在让我们来看一下数据库.
test=# select userid, length(knowledge) from knowledge;
userid | length
--------+--------
10 | 11
10 | 393165
10 | 393166
10 | 393167
10 | 393168
10 | 393173
我能够通过参数化来插入大文本.
欢迎您将我的代码提供给它,并提供您的数据,看看问题是否对您仍然存在.
如果您在Windows上看不到这段代码的问题,那么libpq就可以了.
如果你看到这个问题,我会好奇它是libpq还是pg15.2.然后,我们必须做淘汰测试.
- 保留libpq并针对PG 12运行代码,看看是否运行得更好,或者
- 保留第15.2页,并使用与libpq不同的库.
References个