From 42cb8a21c70ac09116f8a6f1a5c8ffe23cc11e18 Mon Sep 17 00:00:00 2001 From: "Peter J. Holzer" Date: Sat, 13 Apr 2019 23:44:19 +0200 Subject: [PATCH] Measure bloat by continuously updating a table --- update-bloat | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100755 update-bloat diff --git a/update-bloat b/update-bloat new file mode 100755 index 0000000..96a19bd --- /dev/null +++ b/update-bloat @@ -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) + +