From 57e6a01214f04446fde8f72c98369add33cf8829 Mon Sep 17 00:00:00 2001 From: Andrew Kane Date: Wed, 9 Jun 2021 03:13:22 -0700 Subject: [PATCH] Fixed segmentation fault with COUNT - fixes #9 --- CHANGELOG.md | 4 ++++ src/ivfflat.c | 13 +++++++++++++ src/ivfscan.c | 4 ++++ test/expected/ivfflat_l2.out | 6 ++++++ test/sql/ivfflat_l2.sql | 1 + 5 files changed, 28 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index f92b38b..79d2748 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +## 0.1.6 (unreleased) + +- Fixed segmentation fault with `COUNT` + ## 0.1.5 (2021-05-25) - Reduced memory usage during index creation diff --git a/src/ivfflat.c b/src/ivfflat.c index 4192cfb..29b8248 100644 --- a/src/ivfflat.c +++ b/src/ivfflat.c @@ -51,6 +51,19 @@ ivfflatcostestimate(PlannerInfo *root, IndexPath *path, double loop_count, qinfos = deconstruct_indexquals(path); #endif + /* Never use index without order */ + if (path->indexorderbys == NULL) + { + *indexStartupCost = 1.0e10; + *indexTotalCost = 1.0e10; + *indexSelectivity = 0; + *indexCorrelation = 0; +#if PG_VERSION_NUM >= 100000 + *indexPages = 0; +#endif + return; + } + MemSet(&costs, 0, sizeof(costs)); #if PG_VERSION_NUM >= 120000 diff --git a/src/ivfscan.c b/src/ivfscan.c index 99dc0f3..7dfdf49 100644 --- a/src/ivfscan.c +++ b/src/ivfscan.c @@ -251,6 +251,10 @@ ivfflatgettuple(IndexScanDesc scan, ScanDirection dir) { Datum value; + /* Safety check */ + if (scan->orderByData == NULL) + elog(ERROR, "cannot scan ivfflat index without order"); + /* No items will match if null */ if (scan->orderByData->sk_flags & SK_ISNULL) return false; diff --git a/test/expected/ivfflat_l2.out b/test/expected/ivfflat_l2.out index 506cf48..449ea73 100644 --- a/test/expected/ivfflat_l2.out +++ b/test/expected/ivfflat_l2.out @@ -19,4 +19,10 @@ SELECT * FROM t ORDER BY val <-> (SELECT NULL::vector); ----- (0 rows) +SELECT COUNT(*) FROM t; + count +------- + 5 +(1 row) + DROP TABLE t; diff --git a/test/sql/ivfflat_l2.sql b/test/sql/ivfflat_l2.sql index 336434e..f4078a7 100644 --- a/test/sql/ivfflat_l2.sql +++ b/test/sql/ivfflat_l2.sql @@ -10,5 +10,6 @@ INSERT INTO t (val) VALUES ('[1,2,4]'); SELECT * FROM t ORDER BY val <-> '[3,3,3]'; SELECT * FROM t ORDER BY val <-> (SELECT NULL::vector); +SELECT COUNT(*) FROM t; DROP TABLE t;