Skip to content

Commit

Permalink
Replace TypenameGetTypid with custom TypenameGetTypidAllNsp funct…
Browse files Browse the repository at this point in the history
…ion.

After release of Postgres 17, search_path is restricted to (pg_catalog, pg_temp)
for maintenance operations [ref: postgres/postgres@2af07e2]
So when pgvector was installed in public schema and we will try to get
the oid for vector type using `TypenameGetTypid("vector")` it would return `InvalidOid`
By this change the function `TypenameGetTypidAllNsp` will iterate over all available
namespaces and search for the type in syscache returning the oid if found.
  • Loading branch information
var77 committed Oct 29, 2024
1 parent 1a7fee8 commit 89a4492
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lantern_hnsw/src/hnsw.c
Original file line number Diff line number Diff line change
Expand Up @@ -452,7 +452,7 @@ HnswColumnType GetColumnTypeFromOid(Oid oid)

if(oid == FLOAT4ARRAYOID) {
return REAL_ARRAY;
} else if(oid == TypenameGetTypid("vector")) {
} else if(oid == TypenameGetTypidAllNsp("vector")) {
return VECTOR;
} else if(oid == INT4ARRAYOID) {
return INT_ARRAY;
Expand Down
42 changes: 42 additions & 0 deletions lantern_hnsw/src/hnsw/utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,18 @@

#include "utils.h"

#include <access/heapam.h>
#include <assert.h>
#include <catalog/pg_namespace.h>
#include <catalog/pg_type_d.h>
#include <executor/spi.h>
#include <math.h>
#include <miscadmin.h>
#include <regex.h>
#include <string.h>
#include <utils/builtins.h>
#include <utils/rel.h>
#include <utils/syscache.h>

#if PG_VERSION_NUM >= 130000
#include <utils/memutils.h>
Expand Down Expand Up @@ -271,3 +275,41 @@ usearch_metric_kind_t GetMetricKindFromStr(char *metric_kind_str)

elog(ERROR, "Unsupported metric kind: %s . Should be one of (l2sq, cos, hamming)", metric_kind_str);
}

/*
* We are not using existing TypenameGetTypid because after Postgres 17
* The maintenance operations have restricted search_path for namepsaces (pg_catalog, pg_temp)
* Thus if the type will be installed in public schema, it will not be able to find the type
* Here we will iterate over all available namespaces and try to find the specified type on each namepsace
*/
Oid TypenameGetTypidAllNsp(char *typname)
{
Relation nsp_rel;
TableScanDesc scan;
HeapTuple tuple;
Oid typid = InvalidOid;

// Open pg_namespace relation
nsp_rel = table_open(NamespaceRelationId, AccessShareLock);

// Begin a scan on pg_namespace
scan = table_beginscan_catalog(nsp_rel, 0, NULL);

// Loop through all tuples in pg_namespace
while((tuple = heap_getnext(scan, ForwardScanDirection)) != NULL) {
Form_pg_namespace nsp_form = (Form_pg_namespace)GETSTRUCT(tuple);

typid
= GetSysCacheOid2(TYPENAMENSP, Anum_pg_type_oid, PointerGetDatum(typname), ObjectIdGetDatum(nsp_form->oid));

if(OidIsValid(typid)) {
break;
}
}

// End scan and close relation
table_endscan(scan);
table_close(nsp_rel, AccessShareLock);

return typid;
}
1 change: 1 addition & 0 deletions lantern_hnsw/src/hnsw/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ void CheckExtensionVersions();
uint32 EstimateRowCount(Relation heap);
int32 GetColumnAttributeNumber(Relation rel, const char *columnName);
usearch_metric_kind_t GetMetricKindFromStr(char *metric_kind_str);
Oid TypenameGetTypidAllNsp(char *typname);

// hoping to throw the error via an assertion, if those are on, before elog(ERROR)-ing as a last resort
// We prefer Assert() because this function is used in contexts where the stack contains non-POD types
Expand Down

0 comments on commit 89a4492

Please sign in to comment.