asyncpg icon indicating copy to clipboard operation
asyncpg copied to clipboard

Segfault with NULL value in cell

Open henry232323 opened this issue 4 years ago • 1 comments

  • asyncpg version: 0.23.0
  • PostgreSQL version: 13
  • Do you use a PostgreSQL SaaS? If so, which? Can you reproduce the issue with a local PostgreSQL install?: No
  • Python version: 3.8
  • Platform: Ubuntu Xenial
  • Did you install asyncpg with pip?: Yes
  • Can the issue be reproduced under both asyncio and uvloop?: Yes

My table looks like this, and is using the citext extension

CREATE TABLE public.items
(
    item_id     bigint                       NOT NULL,
    tags        citext[] default '{}' not null
);

I have a simple query

query = """SELECT tags FROM items WHERE item_id = 304028"""

async with pool.acquire() as c:
    result = await c.fetchval(query)
    print(result)
    val = result[2]

This gives the following output ['debuff', 'buff', <NULL>] And upon trying to access the second item of the list results a segmentation fault occurs.

Inspecting the database with PyCharm's database tab shows this cell as having the value {debuff,buff,""}

henry232323 avatar May 29 '21 15:05 henry232323

import asyncpg

async def retrieve_tags(pool):
    query = "SELECT tags FROM items WHERE item_id = $1"
    item_id = 304028

    async with pool.acquire() as connection:
        result = await connection.fetchval(query, item_id)
        
        if result is not None:
            tags = [tag for tag in result if tag]
            print(tags)
        else:
            print("No tags found for item_id:", item_id)

# Assuming you have already created an asyncpg pool
pool = await asyncpg.create_pool(database="your_db", user="your_user", password="your_password", host="your_host")

# Call the function to retrieve and handle the tags
await retrieve_tags(pool)

ljluestc avatar Jan 15 '24 03:01 ljluestc