Skip to content

CockroachDB first query huge 7 seconds delay!! #1163

Closed as not planned
Closed as not planned
@tamis-laan

Description

@tamis-laan
  • asyncpg version: 0.29.0
  • CockroachDB version: v23.1.23
  • Python version: 3.12.4
  • Platform: Linux
  • Do you use pgbouncer?: no
  • Did you install asyncpg with pip?: yes
  • Can the issue be reproduced under both asyncio and
    uvloop?
    : don't know?
import os
import asyncpg
import logging

# Singleton
_db = None

async def get():
    # Get singleton
    global _db

    # Return database singleton if already connected
    if _db:
        return _db

    # Log to console
    logging.info("Connecting to database")
    _db = await asyncpg.connect(
        database = os.getenv("DB_NAME","defaultdb"),
        user     = os.getenv("DB_USER", "root"),
        password = os.getenv("DB_PASS", "root"),
        host     = os.getenv("DB_HOST", "localhost"),
        port     = os.getenv("DB_PORT", "26257")
    )

    # Return database singleton
    return _db

async def main():
    from datetime import datetime

    db = await get()

    start = datetime.now()
    await db.fetch("SELECT * FROM EVENTS")
    stop = datetime.now()
    print(stop-start)
    start = datetime.now()
    await db.fetch("SELECT * FROM EVENTS")
    stop = datetime.now()
    print(stop-start)

# Entry point
if __name__ == "__main__":
    import asyncio
    asyncio.run(main())

There is a huge 7 seconds delay running the first query. Subsequent queries are very fast.

(.venv) > python src/database2.py
0:00:06.947757
0:00:00.000738

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