From baf90a4b66e5936f2847566e0793c81d12bc9bff Mon Sep 17 00:00:00 2001 From: Tony Locke Date: Fri, 19 Apr 2024 20:36:18 +0100 Subject: [PATCH] i --- README.md | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 3fdd2b1..e9b13e0 100644 --- a/README.md +++ b/README.md @@ -372,23 +372,27 @@ pg8000.exceptions.DatabaseError: ... ``` -instead you can write it using the [unnest -](https://www.postgresql.org/docs/current/functions-array.html) function: +the most straightforward way to get around this problem is to rewrie the query using the [`ANY`]( +https://www.postgresql.org/docs/current/functions-comparisons.html#FUNCTIONS-COMPARISONS-ANY-SOME) +function: ```python >>> import pg8000.native >>> >>> con = pg8000.native.Connection("postgres", password="cpsnow") >>> ->>> con.run( -... "SELECT 'silo 1' WHERE 'a' IN (SELECT unnest(CAST(:v as varchar[])))", -... v=['a', 'b']) +>>> con.run("SELECT 'silo 1' WHERE 'a' = ANY(:v)", v=['a', 'b']) [['silo 1']] >>> con.close() ``` -or `any`: +However, using the array variant of `ANY` [may cause a performance problem]( +https://stackoverflow.com/questions/34627026/in-vs-any-operator-in-postgresql) and so +you can use the [subquery variant of `IN`]( +https://www.postgresql.org/docs/current/functions-subquery.html#FUNCTIONS-SUBQUERY-IN) +with the [unnest +](https://www.postgresql.org/docs/current/functions-array.html) function: ```python >>> import pg8000.native @@ -396,7 +400,7 @@ or `any`: >>> con = pg8000.native.Connection("postgres", password="cpsnow") >>> >>> con.run( -... "SELECT 'silo 1' WHERE 'a' = any(:v)", +... "SELECT 'silo 1' WHERE 'a' IN (SELECT unnest(CAST(:v as varchar[])))", ... v=['a', 'b']) [['silo 1']] >>> con.close()