Skip to content

Commit

Permalink
getFreePartCount query optimization and additional comments for reada…
Browse files Browse the repository at this point in the history
…bility
  • Loading branch information
jobinau committed Dec 1, 2020
1 parent 5ad55c6 commit 955d9ad
Showing 1 changed file with 10 additions and 5 deletions.
15 changes: 10 additions & 5 deletions pg_partmaint.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,20 +55,22 @@ class PartTable:
'Class representing the a Paritioned table' #this is __doc__
def __init__(self,name):
self.name = name
#Query to identify the partitioning column and its type
sql= """SELECT c.oid,a.attname, t.typname
FROM pg_attribute a
JOIN pg_class c ON a.attrelid = c.oid
JOIN pg_namespace n ON c.relnamespace = n.oid
JOIN pg_type t ON a.atttypid = t.oid
WHERE attnum IN (SELECT unnest(partattrs) FROM pg_partitioned_table p WHERE a.attrelid = p.partrelid)""" + \
" AND n.nspname = split_part('" + str(args.table) + "', '.', 1)::name AND c.relname = split_part('" + str(args.table) + "', '.', 2)::name"
#print(sql)

#print('########## find the partition key ######\n'+sql+'\n###########################')
cur = conn.cursor()
cur.execute(sql)
if cur.rowcount < 1 :
print("ERROR : Unable to locate a partitioned table \"" + str(args.table) + "\"")
print("ERROR : No partitioned table with name :\"" + str(args.table) + "\"")
sys.exit()
print('Verified that table : ' + self.name + ' is a partitioned table')
#print('Verified that table : ' + self.name + ' is a partitioned table')
self.attr = cur.fetchone()
#attr[0] = oid of table, attr[1] = column name, attr[2] = column type
cur.close()
Expand Down Expand Up @@ -96,8 +98,11 @@ def __init__(self,name):


def getFreePartCount(self): ## Get the number of empty / free partitions in the table
sql = ("SELECT count(*) FROM pg_catalog.pg_class c, pg_catalog.pg_inherits i, pg_stat_user_tables s " +
"WHERE c.oid=i.inhrelid AND i.inhparent = '" + str(self.attr[0]) + "' and c.oid = s.relid and s.n_live_tup = 0 ")
#sql = ("SELECT count(*) FROM pg_catalog.pg_class c, pg_catalog.pg_inherits i, pg_stat_user_tables s " +
#"WHERE c.oid=i.inhrelid AND i.inhparent = '" + str(self.attr[0]) + "' and c.oid = s.relid and s.n_live_tup = 0 ")
sql=" SELECT COUNT(*) FROM pg_catalog.pg_inherits i JOIN pg_stat_user_tables s ON i.inhrelid = s.relid \
WHERE i.inhparent = '" + str(self.attr[0]) + "' AND s.n_live_tup = 0"
print('########## No. of empty partitions ######\n'+sql+'\n###########################')
cur = conn.cursor()
cur.execute(sql)
parts = cur.fetchone()
Expand Down

0 comments on commit 955d9ad

Please sign in to comment.