execute_values with SELECT statement

Viewed 281

I am using table merging in order to select items from my db against a list of parameter tuples. The query works fine, but cur.fetchall() does not return the entire table that I want.

For example:

data = (
    (1, '2020-11-19'),
    (1, '2020-11-20'),
    (1, '2020-11-21'),
    (2, '2020-11-19'),
    (2, '2020-11-20'),
    (2, '2020-11-21')
)
        
query = """
    with data(song_id, date) as (
        values %s
    )
    select t.*
    from my_table t
    join data d 
    on t.song_id = d.song_id and t.date = d.date::date
"""
execute_values(cursor, query, data)
results = cursor.fetchall()

In practice, my list of tuples to check against is thousands of rows long, and I expect the response to also be thousands of rows long.

But I am only getting 5 rows back if I call cur.fetchall() at the end of this request.

I know that this is because execute_values batches the requests, but there is some strange behavior.

If I pass page_size=10 then I get 2 items back. And if I set fetch=True then I get no results at all (even though the rowcount does not match that).

My thought was to batch these requests, but the page_size for the batch is not matching the number of items that I'm expecting per batch.

How should I change this request so that I can get all the results I'm expecting?

0 Answers
Related