I'm running into a strange error -psycopg2.errors.ReadOnlySqlTransaction: transaction is read-only
when i'm trying to read data from my redshift database from my python application. I'm not trying to modify the database so i'm not sure why this happens. I'm using a postgresql plugin (psycopg2) version 2.8.4. And python version 3.5 -I have lot of other queries that i'm running in the same approach and they work without a problem, so i'm a little confused what could be causing this.
Here is the query i'm running
SELECT
stat_date,
age,
name,
SUM(spent) as spent,
FROM (
SELECT stat_date, age,name, spent
FROM mview_a
WHERE id in ('id1', 'id2')
UNION ALL
SELECT stat_date, age,name, spent
FROM mview_b
WHERE id in ('id1', 'id2')
UNION ALL
SELECT stat_date, age,name, spent
FROM mview_c
WHERE id in ('id1', 'id2')
UNION ALL
SELECT stat_date, age,name, spent
FROM view_d (combination of multiple mviews)
WHERE id in ('id1', 'id2')
UNION ALL
SELECT stat_date, age,name, spent
FROM mview_e
WHERE id in ('id1', 'id2')
)
GROUP BY 1;
And this is how i pass my query along with params
dw = self.get_conn(conn)
results = dw.fetch_all_query(query, params=params, **kwargs)
And the database calls fails on this line from psycopg2 package.
self.execute_query(q, dict_cursor=dict_cursor, params=params,
readonly=readonly, autocommit=autocommit, conn=False)
In my case, the issue was happening because one of the mviews i use is a combination (union all) of two different mviews and for some reason, psql was throwing an error saying transaction is read only. The error message was pretty unclear to solve this.