Skip to content

Commit

Permalink
Fixed error with Psycopg 2 and pgvector < 0.7 - fixes #79
Browse files Browse the repository at this point in the history
  • Loading branch information
ankane committed Jul 10, 2024
1 parent a07361b commit 2e253ce
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 14 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.3.1 (unreleased)

- Fixed error with Psycopg 2 and pgvector < 0.7

## 0.3.0 (2024-06-25)

- Added support for `halfvec`, `bit`, and `sparsevec` types to Django
Expand Down
24 changes: 10 additions & 14 deletions pgvector/psycopg2/register.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,16 @@
def register_vector(conn_or_curs=None):
cur = conn_or_curs.cursor() if hasattr(conn_or_curs, 'cursor') else conn_or_curs

try:
cur.execute('SELECT NULL::vector')
register_vector_info(cur.description[0][1])
except psycopg2.errors.UndefinedObject:
cur.execute("SELECT typname, oid FROM pg_type WHERE typname IN ('vector', 'halfvec', 'sparsevec')")
type_info = dict(cur.fetchall())

if 'vector' not in type_info:
raise psycopg2.ProgrammingError('vector type not found in the database')

try:
cur.execute('SELECT NULL::halfvec')
register_halfvec_info(cur.description[0][1])
except psycopg2.errors.UndefinedObject:
pass
register_vector_info(type_info['vector'])

if 'halfvec' in type_info:
register_halfvec_info(type_info['halfvec'])

try:
cur.execute('SELECT NULL::sparsevec')
register_sparsevec_info(cur.description[0][1])
except psycopg2.errors.UndefinedObject:
pass
if 'sparsevec' in type_info:
register_sparsevec_info(type_info['sparsevec'])

0 comments on commit 2e253ce

Please sign in to comment.