Segfault in aod_sketch_get_estimate while lgk=16
Hi team. I am experiencing segfault while I am running datasketches-postgresql tests. Here is the error log
psql:test/aod_sketch_test.sql:25: server closed the connection unexpectedly
This probably means the server terminated abnormally
before or while processing the request.
psql:test/aod_sketch_test.sql:25: error: connection to server was lost
2025-05-23 21:14:29.609 UTC [13567] LOG: server process (PID 13607) was terminated by signal 11: Segmentation fault
2025-05-23 21:14:29.609 UTC [13567] DETAIL: Failed process was running: select aod_sketch_get_estimate(aod_sketch_union(sketch, 16)) from aod_sketch_test;
I am running with version datasketches-postgresql 1.7.0, with datasketches-cpp 5.0.0, on PostgreSQL 15.12 OEL7 linux distribution. Here is my datasketches-postgresql set up script
# install datasketches-cpp
curl -O https://archive.apache.org/dist/datasketches/cpp/5.0.0/apache-datasketches-cpp-5.0.0-src.zip
unzip apache-datasketches-cpp-5.0.0-src.zip
mv apache-datasketches-cpp-5.0.0-src datasketches-cpp
# install boost
curl -O https://archives.boost.io/release/1.75.0/source/boost_1_75_0.tar.bz2
tar -xjf boost_1_75_0.tar.bz2
mv boost_1_75_0 boost
export BUILD_USER_DIR="/home/builduser"
# mark the extension as trusted, so that users can create it without
# superuser permissions
echo "trusted = true" >> datasketches.control
su builduser -c "make"
su builduser -c "make install"
# clean up the already created data directory
su builduser -c "rm -rf ${BUILD_USER_DIR}/data_dir"
# re-create the data directory
su builduser -c "mkdir -p ${BUILD_USER_DIR}"
# to do regression tests, we need to create new data dir
su builduser -c "pg_ctl -D ${BUILD_USER_DIR}/data_dir init"
# start postgres for testing
su builduser -c "pg_ctl start -D ${BUILD_USER_DIR}/data_dir"
# run tests
su builduser -c "psql -h 127.0.0.1 -d postgres -c 'create database test;'"
su builduser -c "make tests"
Is there anything I did wrong? Looking forward to your response.
Hi team. I am wondering if the test script has a bug? If so, I could post a patch. I checked the source code, in test/aod_sketch_test.sql
-- lgk = 16
select aod_sketch_get_estimate(aod_sketch_union(sketch, 16)) from aod_sketch_test;
this line is throwing seg fault. I checked the source code, the 2nd parameter of aod_sketch_union is supposed to be num_values, not lgk. The table was set up with ARRAY[1] which does not have 16 dimensions. that's why it throws seg fault.
Here is part of the implementation of Datum pg_aod_sketch_union_agg(PG_FUNCTION_ARGS)
if (PG_ARGISNULL(0)) {
stateptr = palloc(sizeof(struct aod_agg_state));
stateptr->type = UNION;
stateptr->num_values = PG_NARGS() > 2 ? PG_GETARG_INT32(2) : 1;
stateptr->lg_k = PG_NARGS() > 3 ? PG_GETARG_INT32(3) : 0;
stateptr->ptr = stateptr->lg_k ? aod_union_new_lgk(stateptr->num_values, stateptr->lg_k) : aod_union_new(stateptr->num_values);
} else {
stateptr = (struct aod_agg_state*) PG_GETARG_POINTER(0);
}
I have updated the script to
-- lgk = 16
select aod_sketch_get_estimate(aod_sketch_union(sketch, 1, 16)) from aod_sketch_test;
as the values are ARRAY[1]. Then the test passed.
@AlexanderSaydakov
You must be right about the test. However, I don't think that passing a wrong number of values (or any other input) should lead to a segfault. So there must be some other problem. I am afraid I don't have much time now to look at this.
I have prepared a PR. I will post soon.
https://github.com/apache/datasketches-postgresql/pull/78