Subject: CPU usage for queries, psycopg 2 vs 3

From: David Raymond <[email-masked]>
Date: Tue, 5 Sep 2023 17:51:50 +0000

I've noticed that CPU usage for queries seems to have drastically increased going from psycopg2 to psycopg3.

In psycopg2 a connection would sit quietly while waiting for the results, using next to no CPU.
In psycopg it looks like the threads are using their core at full power, continuously checking for the result as fast as possible, and never sleeping or relaxing.

See below for a simple example script. During the psycopg2 part while the 8 threads are waiting on results my processor usage sits at the normal idling 2-3%. But in the psycopg3 part, while the 8 threads are waiting on results my processor usage jumps up to 62-63% or so.

Is there a setting or configuration I'm missing?
This is on Windows, where I install with "pip install psycopg2-binary" and "pip install psycopg[binary]"
Current versions shown by pip list:
psycopg            3.1.10    (This gets installed even when telling it to install psycopg[binary])
psycopg-binary     3.1.10
psycopg2-binary    2.9.7

And their reported .__version__
psycopg.__version__: 3.1.10
psycopg2.__version__: 2.9.7 (dt dec pq3 ext lo64)

I've got several scripts where I open up multiple connections to multiple servers and run a number of queries on them, where 98% of my machine's time should be just relaxing and waiting for the server to finish the next 1 to 15 minute long query. I don't need to continue the very microsecond the query completes. Is there anything I can do to bring CPU usage back down with psycopg (3) ?

Thank you,

import sys
import threading

import psycopg
import psycopg2

def connectAndRunSleep3():
    conn = psycopg.connect(host = "localhost", dbname = "...", user = "...", password = "...", autocommit = True)
    with conn.cursor() as cur:
        cur.execute("select pg_sleep(15);")
    conn.close()

def connectAndRunSleep2():
    conn = psycopg2.connect(host = "localhost", dbname = "...", user = "...", password = "...")
    conn.autocommit = True
    with conn.cursor() as cur:
        cur.execute("select pg_sleep(15);")
    conn.close()

if __name__ == "__main__":
    print("Python version:", sys.version)
    print("psycopg.__version__:", psycopg.__version__)
    print("psycopg2.__version__:", psycopg2.__version__)
    
    numThreads = 8
    
    print("Check your CPU usage during this run")
    
    print(f"Querying with {numThreads:,d} threads in psycopg2...")
    threads = []
    for n in range(numThreads):
        thread = threading.Thread(target = connectAndRunSleep2)
        threads.append(thread)
        thread.start()
    for thread in threads:
        thread.join()
    print("All joined.")
    
    print(f"Querying with {numThreads:,d} threads in psycopg3...")
    threads = []
    for n in range(numThreads):
        thread = threading.Thread(target = connectAndRunSleep3)
        threads.append(thread)
        thread.start()
    for thread in threads:
        thread.join()
    print("All joined.")

---

From: Adrian Klaver <[email-masked]>
Date: Tue, 5 Sep 2023 11:22:52 -0700

> Is there a setting or configuration I'm missing?

Take a look at this:

https://github.com/psycopg/psycopg/issues/448

It seems to be related.

---

From: Daniele Varrazzo <[email-masked]>
Date: Thu, 7 Sep 2023 00:05:43 +0100

> I've noticed that CPU usage for queries seems to have drastically increased going from psycopg2 to psycopg3.

Hello David,

thank you for the report. No, this is not expected: psycopg 3 uses I/O
completion functions to wait idle for results from the server, so it
shouldn't consume more cpu than psycopg2. I will investigate (but on
Linux, I don't have a Windows machine handy to test with).

There was a change in the waiting function in psycopg 3.1.5, related
to <https://github.com/psycopg/psycopg/issues/414>. I wonder if it was
a regression with that change? Could you please be so kind as to
repeat your tests with psycopg 3.1.4?

Also, did you try only to connect on localhost? Are you able to run
the same test with a server running on a different network location?

Thank you very much

-- Daniele

---

From: David Raymond <[email-masked]>
Date: Thu, 7 Sep 2023 12:17:40 +0000

> Could you please be so kind as to repeat your tests with psycopg 3.1.4?
Tried with 3.1.4 and it has idle processor usage while waiting for the 8 results, just like psycopg2.
Tried with 3.1.5 and it spikes up to 62% reported CPU usage while waiting for the 8 results, just like the current release.

> Also, did you try only to connect on localhost? Are you able to run the same test with a server running on a different network location?
Have now tried with both a local host, and spreading the connections out over a number of remote servers, and the results are the same for both situations.

---

From: Daniele Varrazzo <[email-masked]>
Date: Thu, 7 Sep 2023 13:43:58 +0100

> Tried with 3.1.5 and it spikes up to 62% reported CPU usage while waiting for the 8 results, just like the current release.

Thank you, this gives a good indication about where to look.

-- Daniele

---

From: Daniele Varrazzo <[email-masked]>
Date: Mon, 11 Sep 2023 16:26:21 +0100

Hello David,

On Tue, 5 Sept 2023 at 18:52, David Raymond <[email-masked]> wrote:
> [...]

I have tested your script on Linux and on this platform I can't
reproduce the issue. Psycopg 3 CPU usage remains low.

I will try to repeat your test on Windows.

-- Daniele

---

From: Daniele Varrazzo <[email-masked]>
Date: Tue, 19 Sep 2023 22:44:20 +0200

Hello David,

> Tried with 3.1.5 and it spikes up to 62% reported CPU usage while waiting for the 8 results, just like the current release.

I am not able to quickly investigate this issue, because I don't have
a Windows machine handy. However I would like to release 3.1.11 soon,
so I would like to merge this changeset, which should revert the wait
function to what was in 3.1.4 on Windows:

https://github.com/psycopg/psycopg/commit/36f70532c4451d04c0052c6eac4d05380159e809

Are you able to test it to verify that CPU usage is regular with this changeset?

Also please note that you sent your last, very informative, message,
to me only: maybe you want to send it to the ML?

If someone is able to look into this issue better, it would be some
welcome help. FYI, I've reported it at
https://github.com/psycopg/psycopg/issues/645

Cheers

-- Daniele

---

From: David Raymond <[email-masked]>
Date: Wed, 20 Sep 2023 14:27:27 +0000

Looks good. With the change it quietly idles while waiting for query results, with no CPU spike.

-David

---

From: Daniele Varrazzo <[email-masked]>
Date: Thu, 21 Sep 2023 11:28:28 +0200

> Looks good. With the change it quietly idles while waiting for query results, with no CPU spike.

Thank you for confirming. I will release Psycopg 3.1.11 in the next
few days, with this changeset included.

Cheers

-- Daniele