AMADANON Inc.
AMADANON Inc.

Reputation: 5919

using psycopg2, how do I get "INFO:" lines?

I want to write a script in python, which analyzes a table, then uses the output of that analysis to do some more processing.

Here is what I get in psql:

bacula=# analyze verbose version;
INFO:  analyzing "public.version"
INFO:  "version": scanned 1 of 1 pages, containing 1 live rows and 0 dead rows; 1 rows in sample, 1 estimated total rows
ANALYZE
bacula=# 

The lines I want are the ones starting with "INFO:" (specifically, info about live/dead rows, with more complicated logic, to figure out if I want to do a vacuum full).

Here is what I have so far:

import psycopg2
conn = psycopg2.connect(connectionstring)
conn.set_isolation_level(0)
cur.execute("analyze verbose version")
print(cur.statusmessage)

cur.statusmessage shows the last line of psql's output ("ANALYZE"). How do I get the INFO: lines?

I've tried "fetchall", which didn't work.

Upvotes: 5

Views: 2103

Answers (1)

Michael Robellard
Michael Robellard

Reputation: 2358

I think what you are looking for is the notices attribute on the connection object.

According to the docs:

notices

A list containing all the database messages sent to the client during the session.

http://initd.org/psycopg/docs/connection.html

Upvotes: 4

Related Questions