Measure bloat by continuously updating a table

This commit is contained in:
Peter J. Holzer 2019-04-13 23:44:19 +02:00
commit 42cb8a21c7
1 changed files with 43 additions and 0 deletions

43
update-bloat Executable file
View File

@ -0,0 +1,43 @@
#!/usr/bin/python3
import argparse
import random
import time
import psycopg2
ap = argparse.ArgumentParser()
ap.add_argument("--dbname", default="")
ap.add_argument("--rows", type=int, default=int(1E6))
ap.add_argument("--updates-per-transaction", type=int, default=1)
ap.add_argument("--transactions-per-sample", type=int, default=1)
ap.add_argument("--sleep-between-transactions", type=float, default=1.0)
args = ap.parse_args()
db = psycopg2.connect("dbname='%s'" % (args.dbname, ))
csr = db.cursor()
csr.execute("drop table if exists tmp_update_bloat")
csr.execute("create table tmp_update_bloat(id serial primary key, t text)")
for i in range(args.rows):
t = "%s.%d" % ("a" * random.randrange(4, 64), i)
csr.execute("insert into tmp_update_bloat(t) values(%s)", (t,))
db.commit()
n_updates = 0
n_transactions = 0
while True:
csr.execute("select pg_total_relation_size('tmp_update_bloat')")
size = csr.fetchone()[0]
print(n_transactions, n_updates, size)
for i in range(args.transactions_per_sample):
for j in range(args.updates_per_transaction):
id = random.randrange(args.rows) + 1
t = "%s.%d" % ("b" * random.randrange(4, 64), n_updates)
csr.execute("update tmp_update_bloat set t = %s where id = %s", (t, id,))
n_updates += 1
db.commit()
n_transactions += 1
time.sleep(args.sleep_between_transactions)