Skip to content

Problems with async or pooling using asyncpg #465

@MatsGej

Description

@MatsGej

What is the bug or the crash?

Getting error asyncpg.exceptions.UndefinedFunctionError: operator does not exist: text %% text HINT: No operator matches the given name and argument types. You might need to add explicit type casts.

Steps to reproduce the issue

This is my code for initiating the connection:
conn_str = f'postgresql://{user}:{password}@{host}:{port}/{dbname}'

# Create a new connection when your application starts

conn = await asyncpg.connect(conn_str)

and this is the code for using that connection:

async def fuzzy_search(conn, search_term, language):
try:
# Determine the column to search based on the language
column = 'name_en' if language == 'en' else 'name_se'

    # Create the SQL query
    query = f"SELECT DISTINCT *, similarity({column}::text, $1) AS score FROM places WHERE {column}::text %% $1 ORDER BY score DESC"

    # Execute the query
    rows = await conn.fetch(query, search_term)

    if rows == []:
        return None

    places = []
    for row in rows:
        place = extract_place(row, language)
        places.append(place)

    return places

except Exception as e:
    print(f"Error in fuzzy_search: {e}, search_term:{search_term}")
    traceback.print_exc()  # This will print the full stack trace
    return None

The database has been enabled for the pg_trgm extension

Versions

kartoza/postgis:11.0-2.5

Additional context

The database has been loaded with lots of geographical places and works fine with synchronous calls.

Have tried both with async and with pooling and both combined.

Have just had a 2-3 hours session with github chat trying to find a solution. But it gave up.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions