Catching query cancelations in PLPython3u - Mailing list pgsql-hackers

From Mat Arye
Subject Catching query cancelations in PLPython3u
Date
Msg-id CADsUR0Aauim-HULPsks0PGAW6jJ=-VYfFOahvqGQxk6zBak7Vg@mail.gmail.com
Whole thread Raw
List pgsql-hackers
Hi All,

We've been using plpython3u in our pgai project recently and recently got a PR[1]
that uses some async code to communicate with openAI. That code uses async to be able to respond in a timely manner to queries being cancelled. Right now it uses a timed loop to test for query cancellations using:

def is_query_cancelled(plpy):
    try:
        plpy.execute("SELECT 1")
        return False
    except plpy.SPIError:
        return True

The big problem with this I think is that it uses up a subtransaction, which seems suboptimal. Is there a better way? Should Postgres add a function to plpy for this?

I've thought about trying to catch the sigint signal from Python, but it seems that the Python signal machinery would conflict badly with the Postgres signal machinery.


Thanks,
Mat


--
Mat Arye, Technical lead of all thing AI @ Timescale

pgsql-hackers by date:

Previous
From: Mats Kindahl
Date:
Subject: Re: Potential ABI breakage in upcoming minor releases
Next
From: vignesh C
Date:
Subject: Re: Improve the error message for logical replication of regular column to generated column.