pg.ddx.io  pgvector/pgvector GitHub issues and pull requests  
help / color / mirror / Atom feed
[pgvector/pgvector] PR #1012: Fix parallel aggregate failure on empty sets in vector_combine.
3+ messages / 2 participants
[nested] [flat]

* [pgvector/pgvector] PR #1012: Fix parallel aggregate failure on empty sets in vector_combine.
@ 2026-08-07 11:18 "jainvaibhavjain (@jainvaibhavjain)" <noreply+jainvaibhavjain@github.com>
  0 siblings, 0 replies; 3+ messages in thread

From: jainvaibhavjain (@jainvaibhavjain) @ 2026-08-07 11:18 UTC (permalink / raw)
  To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>

PostgreSQL parallel aggregation uses `INITCOND='{0}'` as the initial working state, resulting in a 0-dimensional float array when a table (or sequential scan shard) contains no rows. When multiple background workers yield empty shards, `vector_combine` erroneously evaluated `CheckDim(0)` on this initialization state, throwing error `vector must have at least 1 dimension` error and failing the query.

This modifies `vector_combine_impl` to conditionally bypass `CheckDim(dim)` only when both incoming parallel states are completely empty (`n == 0.0`), while fully preserving dimensionality checks against malformed inputs in all other code paths.

Also includes a dedicated TAP test to guarantee safety under forced parallel execution limits.

inline patch 1: src/vector.c, test/expected/vector_type.out, test/t/049_aggregates_empty.pl (+42/-2 lines) download inline-1.patchdiff --git a/src/vector.c b/src/vector.c index b75cfc4b7..d00628b3b 100644 --- a/src/vector.c +++ b/src/vector.c @@ -1233,7 +1233,9 @@ vector_combine(PG_FUNCTION_ARGS) { n = n2; dim = STATE_DIMS(statearray2); - CheckDim(dim); + if (n > 0.0) { + CheckDim(dim); + } statedatums = CreateStateDatums(dim); for (int i = 0; i < dim; i++) statedatums[i + 1] = Float8GetDatum(statevalues2[i + 1]); diff --git a/test/expected/vector_type.out b/test/expected/vector_type.out index b6dc9bc19..43a0815fc 100644 --- a/test/expected/vector_type.out +++ b/test/expected/vector_type.out @@ -713,7 +713,11 @@ ERROR: vector_combine: expected state array SELECT vector_combine('{0}', '{}'); ERROR: vector_combine: expected state array SELECT vector_combine('{0}', '{0}'); -ERROR: vector must have at least 1 dimension + vector_combine +---------------- + {0} +(1 row) + SELECT vector_combine('{0}', (SELECT array_agg(n) FROM generate_series(1, 16002) n)); ERROR: vector cannot have more than 16000 dimensions SELECT vector_combine((SELECT array_agg(n) FROM generate_series(1, 16002) n), '{0}'); diff --git a/test/t/049_aggregates_empty.pl b/test/t/049_aggregates_empty.pl new file mode 100644 index 000000000..e77d9cd2f --- /dev/null +++ b/test/t/049_aggregates_empty.pl @@ -0,0 +1,34 @@ +use strict; +use warnings FATAL => 'all'; +use PostgreSQL::Test::Cluster; +use PostgreSQL::Test::Utils; +use Test::More; + +# Initialize node +my $node = PostgreSQL::Test::Cluster->new('node'); +$node->init; +$node->start; + +# Create table and force parallel execution +$node->safe_psql("postgres", "CREATE EXTENSION vector;"); +$node->safe_psql("postgres", "CREATE TABLE tst_empty (id int, v vector(3));"); +# Insert enough rows to make parallel scan look attractive +$node->safe_psql("postgres", "INSERT INTO tst_empty SELECT i, '[1,2,3]' FROM generate_series(1, 1000000) i;"); + +# Force parallel scanning and aggregation even for subsets +$node->safe_psql("postgres", "ALTER TABLE tst_empty SET (parallel_workers = 4);"); + +my $gucs = "SET max_parallel_workers_per_gather = 4; SET min_parallel_table_scan_size = 0; SET parallel_setup_cost = 0; SET parallel_tuple_cost = 0;"; +my $query = "SELECT avg(v) FROM tst_empty WHERE id < 0;"; + +# Verify it performs a Partial Aggregate +my $explain = $node->safe_psql("postgres", "$gucs EXPLAIN $query"); +like($explain, qr/Partial Aggregate/, "Query uses Parallel Aggregation"); + +# Execute the query on empty dataset +my ($ret, $out, $err) = $node->psql("postgres", "$gucs $query"); + +is($err, "", "Query should not fail due to CheckDim(0) on empty response from parallel workers"); +is($out, "", "Average of 0 vectors is NULL"); + +done_testing();
^ permalink raw [nested|flat] 3+ messages in thread

* Re: [pgvector/pgvector] PR #1012: Fix parallel aggregate failure on empty sets in vector_combine.
@ 2026-08-07 17:50 ` "ankane (@ankane)" <noreply+ankane@github.com>
  1 sibling, 0 replies; 3+ messages in thread

From: ankane (@ankane) @ 2026-08-07 17:50 UTC (permalink / raw)
  To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>

@ankane closed this.

^ permalink  raw  [nested|flat] 3+ messages in thread

* Re: [pgvector/pgvector] PR #1012: Fix parallel aggregate failure on empty sets in vector_combine.
@ 2026-08-07 17:52 ` "ankane (@ankane)" <noreply+ankane@github.com>
  1 sibling, 0 replies; 3+ messages in thread

From: ankane (@ankane) @ 2026-08-07 17:52 UTC (permalink / raw)
  To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>

Hi @jainvaibhavjain, thanks for the PR! Pushed a version of this in the commit above. It looks like this was broken in the previous release as part of hardening efforts.

^ permalink  raw  [nested|flat] 3+ messages in thread


end of thread, other threads:[~2026-08-07 17:52 UTC | newest]

Thread overview: 3+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2026-08-07 11:18 [pgvector/pgvector] PR #1012: Fix parallel aggregate failure on empty sets in vector_combine. "jainvaibhavjain (@jainvaibhavjain)" <noreply+jainvaibhavjain@github.com>
2026-08-07 17:50 ` "ankane (@ankane)" <noreply+ankane@github.com>
2026-08-07 17:52 ` "ankane (@ankane)" <noreply+ankane@github.com>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox