【发布时间】:2020-10-03 02:38:21
【问题描述】:
我正在学习如何创建 C 聚合扩展并在客户端使用 libpqxx 和 C++ 来处理数据。
我的玩具聚合扩展有一个bytea 类型的参数,状态也是bytea 类型。以下是我的问题的最简单示例:
服务器端:
PG_FUNCTION_INFO_V1( simple_func );
Datum simple_func( PG_FUNCTION_ARGS ){
bytea *new_state = (bytea *) palloc( 128 + VARHDRSZ );
memset(new_state, 0, 128 + VARHDRSZ );
SET_VARSIZE( new_state,128 + VARHDRSZ );
PG_RETURN_BYTEA_P( new_state );
}
客户端:
std::basic_string< std::byte > buffer;
pqxx::connection c{"postgresql://user:simplepassword@localhost/contrib_regression"};
pqxx::work w(c);
c.prepare( "simple_func", "SELECT simple_func( $1 ) FROM table" );
pqxx::result r = w.exec_prepared( "simple_func", buffer );
for (auto row: r){
cout << " Result Size: " << row[ "simple_func" ].size() << endl;
cout << "Raw Result Data: ";
for( int jj=0; jj < row[ "simple_func" ].size(); jj++ ) printf( "%02" PRIx8, (uint8_t) row[ "simple_func" ].c_str()[jj] ) ;
cout << endl;
}
客户端打印结果:
Result Size: 258
Raw Result Data: 5c783030303030303030303030303030...
30 模式重复到字符串结尾,打印的十六进制字符串为 512 个字节。
我希望收到一个长度为 128 字节的数组,其中每个字节都设置为零。我做错了什么?
libpqxx 版本是 7.2 和 Ubuntu 20.04 上的 PostgreSQL 12。
附录
安装扩展sql语句;
CREATE OR REPLACE FUNCTION agg_simple_func( state bytea, arg1 bytea)
RETURNS bytea
AS '$libdir/agg_simple_func'
LANGUAGE C IMMUTABLE STRICT;
CREATE OR REPLACE AGGREGATE simple_func( arg1 bytea)
(
sfunc = agg_simple_func,
stype = bytea,
initcond = "\xFFFF"
);
【问题讨论】:
标签: postgresql postgresql-12 libpq libpqxx postgresql-extensions