Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Table being used by active queries in this session #1008

Closed
arnaudsjs opened this issue Mar 1, 2023 · 0 comments · Fixed by #1088
Closed

Table being used by active queries in this session #1008

arnaudsjs opened this issue Mar 1, 2023 · 0 comments · Fixed by #1088

Comments

@arnaudsjs
Copy link

  • asyncpg version: 0.27.0
  • PostgreSQL version: 13.6
  • Do you use a PostgreSQL SaaS? If so, which? Can you reproduce
    the issue with a local PostgreSQL install?
    : No, I am not using PostgreSQL SaaS
  • Python version: 3.9.16
  • Platform: Fedora 36
  • Do you use pgbouncer?: No
  • Did you install asyncpg with pip?: Yes
  • If you built asyncpg locally, which version of Cython did you use?: No local build was done
  • Can the issue be reproduced under both asyncio and
    uvloop?
    :

The following two test cases do exactly the same, but one of them uses a cursor and the other one doesn't:

async def test_cursor(postgresql_client: asyncpg.Connection):
    async with postgresql_client.transaction():
        await postgresql_client.execute("CREATE TABLE test(id INTEGER PRIMARY KEY)")
        await postgresql_client.execute("INSERT INTO test VALUES(1)")
        async for record in postgresql_client.cursor("SELECT * FROM test"):
            print(record["id"])
        await postgresql_client.execute("ALTER TABLE test ADD COLUMN test boolean")

async def test_no_cursor(postgresql_client: asyncpg.Connection):
    async with postgresql_client.transaction():
        await postgresql_client.execute("CREATE TABLE test(id INTEGER PRIMARY KEY)")
        await postgresql_client.execute("INSERT INTO test VALUES(1)")
        records = await postgresql_client.fetch("SELECT * FROM test")
        for record in records:
            print(record["id"])
        await postgresql_client.execute("ALTER TABLE test ADD COLUMN test boolean")

Observed behavior

The behavior I observe is that the test_no_cursor test case works as expected without any failure. The test_cursor test case on the other hand fails with the following error:

asyncpg.exceptions.ObjectInUseError: cannot ALTER TABLE "test" because it is being used by active queries in this session.

Expected behavior

I would expect that both tests cases succeed and behave identically. I have been following this documentation page on how to use cursors.

elprans added a commit that referenced this issue Oct 9, 2023
When iterating on a cursor, make sure to close the portal once iteration
is done.  This prevents the cursor from holding onto resources until the
end of transaction.

Fixes: #1008
elprans added a commit that referenced this issue Oct 9, 2023
When iterating on a cursor, make sure to close the portal once iteration
is done.  This prevents the cursor from holding onto resources until the
end of transaction.

Fixes: #1008
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant