I have a kubernete microservice that can access Postgres, both in Azure. If I run the microservice container in my local and access the same Postgres in Azure, it has no problem. But if run the same container in AKS vm, the fetchall just hangs for hours or never return. It doesn't error out and no exception happens. If calling another endpoint in the microservice, I get expected response so the microservice is still running. However, the fetch works sometimes! But hanging happens more often. Below is the code I tried to use named cursor but not help.
For the Azure Postgres, I already bumped up to use 8-core, 100G, Optimized memory option. Also removed the kubernete POD memory limitation. The data records to retrieve from Postgres is around 8000 and each record is about couple hundreds bytes. When it hangs, I use psql inside the POD to do the same "select * tablename;" query, it returns immediately. Use fetchmany or fetch not working either. The psycopg2 is the 2.9.3
select_query = "select * from database_table;"
try:
with connection:
with connection.cursor("my_cursor_to_the_server_select_all") as cursor:
cursor.itersize = 2000
logger.debug("Before execute")
cursor.execute(select_query)
logger.debug("After execute")
while True:
rows = cursor.fetchmany(200) **<-- always stuck here for very long time**
logger.debug("fetch here")
if not rows:
break
count += 1
records.extend(rows)
logger.debug(f"After fetch: {len(records)}")
except Exception as err:
logger.error(f"[{request_id}]: Exception in executing DB select_all: {err}")
raise Exception(err)
finally:
connection.close()
Addition info: used htop to monitor performance, the CPU is memory usage is fine.
Edit:
- I used the fetchmany and result is same. Sometimes works but mostly not.
- The CPU usage from the container is about 2% and MEM is about 10%
- When it "hangs", I use pgadmin to do the same query, it returns very quickly so the postgres is ok.
- Found out that The "hanging" only happens when the REST endpoint is called from Azure Data Factory's Web Activity. If I call the REST API using REST client from local command line, the query can be performed normally. Wonder why this can happen?