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

Migrate from asyncpg to psycopg v3 #93

Closed
aasseman opened this issue Nov 8, 2023 · 0 comments · Fixed by #94
Closed

Migrate from asyncpg to psycopg v3 #93

aasseman opened this issue Nov 8, 2023 · 0 comments · Fixed by #94

Comments

@aasseman
Copy link
Contributor

aasseman commented Nov 8, 2023

psycopg v3 supports async, while psycopg2 did not: https://www.psycopg.org/psycopg3/docs/advanced/async.html

Also, it will solve the issue in #84 (comment). I tried on a little snippet to make sure, and it works well:

async def test_psycopg3(self, postgresql):
    conn_string =   f"host={postgresql.info.host} " \
        f"dbname={postgresql.info.dbname} "\
        f"user={postgresql.info.user} "\
        f"password=\"{postgresql.info.password}\" "\
        f"port={postgresql.info.port}"\
        
    print(conn_string)
    pool = psycopg_pool.AsyncConnectionPool(
        conn_string,
        min_size=2,
        max_size=10,
    )
    await pool.wait()
    async with pool.connection() as conn:
        await conn.execute(
            """
            CREATE TABLE query_skeletons (
                hash BYTEA PRIMARY KEY,
                query TEXT NOT NULL
            )
        """
        )
        await conn.execute(
            """
            CREATE TABLE query_logs (
                id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
                query_hash BYTEA REFERENCES query_skeletons(hash),
                subgraph CHAR(46) NOT NULL,
                timestamp TIMESTAMPTZ NOT NULL,
                query_time_ms INTEGER,
                query_variables TEXT
            )
        """
        )
        await conn.execute(
            """
            INSERT INTO query_skeletons (hash, query)
            VALUES ('hash1', 'query getData{ values { id } }'), ('hash2', 'query getInfo{ info { id text} }')
        """
        )
        await conn.execute(
            """
            INSERT INTO query_logs (query_hash, subgraph, timestamp, query_time_ms)
            VALUES ('hash1', 'QmPnu3R7Fm4RmBF21aCYUohDmWbKd3VMXo64ACiRtwUQrn', '2023-05-18T21:47:41+00:00', 100),
            ('hash1', 'QmPnu3R7Fm4RmBF21aCYUohDmWbKd3VMXo64ACiRtwUQrn', '2023-05-18T21:47:41+00:00', 200),
            ('hash2', 'QmTJBvvpknMow6n4YU8R9Swna6N8mHK8N2WufetysBiyuL', '2023-05-18T21:47:41+00:00', 50),
            ('hash1', 'QmTJBvvpknMow6n4YU8R9Swna6N8mHK8N2WufetysBiyuL', '2023-05-18T21:47:41+00:00', 10)
        """)
    async with pool.connection() as conn:
        result = await conn.execute(
            psycopg.sql.SQL("""
            SELECT
                query,
                count_id,
                min_time,
                max_time,
                avg_time,
                stddev_time
            FROM
                query_skeletons
            INNER JOIN
            (
                SELECT
                    query_hash as qhash,
                    count(id) as count_id,
                    Min(query_time_ms) as min_time,
                    Max(query_time_ms) as max_time,
                    Avg(query_time_ms) as avg_time,
                    Stddev(query_time_ms) as stddev_time 
                FROM
                    {}
                WHERE
                    subgraph = 'QmTJBvvpknMow6n4YU8R9Swna6N8mHK8N2WufetysBiyuL'
                    AND query_time_ms IS NOT NULL
                GROUP BY
                    qhash
                HAVING
                    Count(id) >= 0
            ) as query_logs
            ON
                qhash = hash
            ORDER BY
                count_id DESC
        """).format(psycopg.sql.Identifier("query_logs")),
        )
    print(await result.fetchall())
    await pool.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants