Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: add support for optional comments on statements #35

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ Requires psycopg2
[--exclude-table-in-database EXCLUDE_TABLE_IN_DATABASE] [--no-freeze] [--no-analyze] [--vacuum]
[--pause PAUSE_TIME] [--freezeage FREEZEAGE] [--costdelay COSTDELAY] [--costlimit COSTLIMIT] [-t]
[--enforce-time] [-l LOGFILE] [-v] [--debug] [-U DBUSER] [-H DBHOST] [-p DBPORT] [-w DBPASS] [-st TABLE]
[--comment COMMENT]

optional arguments:
-h, --help show this help message and exit
Expand Down Expand Up @@ -61,6 +62,8 @@ Requires psycopg2
database password
-st TABLE, --table TABLE
only process specified table
-c COMMENT, --comment COMMMENT
specify an optional comment to annotate the query with

Notes:

Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
psycopg2-binary==2.7.4
psycopg2-binary>=2.8
36 changes: 29 additions & 7 deletions scripts/flexible_freeze.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ def timestamp():
help="database password")
parser.add_argument("-st", "--table", dest="table",
help="only process specified table", default=False)
parser.add_argument("-c", "--comment", dest="comment",
help="optional comment to annotate commands with", default=None)

args = parser.parse_args()

Expand Down Expand Up @@ -121,6 +123,25 @@ def dbconnect(dbname, dbuser, dbhost, dbport, dbpass):

return conn


comment_start = "/*"
comment_end = "*/"


def build_statement(sql):
if args.comment is None:
return sql

comment = args.comment.replace(comment_start, "").replace(comment_end, "")

if "/*" in sql or "*/" in sql:
debug_print(f"sql statement already has comment, not appending: {sql}, {comment}")
return sql

sql += f" /* {comment} */"
debug_print(f"appended comment to sql: {sql}")
return sql

def signal_handler(signal, frame):
_print('exiting due to user interrupt')
if conn:
Expand Down Expand Up @@ -191,9 +212,10 @@ def signal_handler(signal, frame):
sys.exit(1)

cur = conn.cursor()
cur.execute("""SELECT datname FROM pg_database
stmt = build_statement("""SELECT datname FROM pg_database
WHERE datname NOT IN ('postgres','template1','template0')
ORDER BY age(datfrozenxid) DESC""")
cur.execute(stmt)
dblist = []
for dbname in cur:
dblist.append(dbname[0])
Expand Down Expand Up @@ -229,8 +251,8 @@ def signal_handler(signal, frame):
continue

cur = conn.cursor()
cur.execute("SET vacuum_cost_delay = {0}".format(args.costdelay))
cur.execute("SET vacuum_cost_limit = {0}".format(args.costlimit))
cur.execute(build_statement("SET vacuum_cost_delay = {0}".format(args.costdelay)))
cur.execute(build_statement("SET vacuum_cost_limit = {0}".format(args.costlimit)))

# if vacuuming, get list of top tables to vacuum
if args.skip_freeze:
Expand Down Expand Up @@ -271,7 +293,7 @@ def signal_handler(signal, frame):
ORDER BY freeze_age DESC, table_bytes DESC
LIMIT 1000;""".format(args.freezeage, args.minsizemb)

cur.execute(tabquery)
cur.execute(build_statement(tabquery))
verbose_print("getting list of tables")

table_resultset = cur.fetchall()
Expand Down Expand Up @@ -322,11 +344,11 @@ def signal_handler(signal, frame):

try:
if args.enforcetime:
excur.execute(timeout_query)
excur.execute(build_statement(timeout_query))
else:
excur.execute("SET statement_timeout = 0")
excur.execute(build_statement("SET statement_timeout = 0"))

excur.execute(exquery)
excur.execute(build_statement(exquery))
except Exception as ex:
_print("VACUUMing %s failed." % table)
_print(str(ex))
Expand Down