44import math
55import os
66import subprocess
7- from datetime import date , timedelta
87
98DAYS_BACK = 30
109DAYS_AHEAD = 2
@@ -23,20 +22,25 @@ def main() -> None:
2322
2423 row_count = scaled (200_000 , args .scale , 2_000 )
2524
26- today = date .today ()
27- partitions = []
28- for offset in range (- DAYS_BACK , DAYS_AHEAD + 1 ):
29- day = today + timedelta (days = offset )
30- nxt = day + timedelta (days = 1 )
31- partitions .append (
32- f"CREATE TABLE partition_aging.events_{ day :%Y_%m_%d} "
33- "PARTITION OF partition_aging.events "
34- f"FOR VALUES FROM ('{ day .isoformat ()} ') TO ('{ nxt .isoformat ()} ');"
35- )
36-
37- sql = (
38- "\n " .join (partitions )
39- + f"""
25+ partition_count = DAYS_BACK + DAYS_AHEAD + 1
26+ sql = f"""
27+ DO $partition_setup$
28+ DECLARE
29+ partition_day date;
30+ BEGIN
31+ FOR day_offset IN -{ DAYS_BACK } ..{ DAYS_AHEAD } LOOP
32+ partition_day := current_date + day_offset;
33+ EXECUTE format(
34+ 'CREATE TABLE partition_aging.%I '
35+ 'PARTITION OF partition_aging.events '
36+ 'FOR VALUES FROM (%L) TO (%L)',
37+ 'events_' || to_char(partition_day, 'YYYY_MM_DD'),
38+ partition_day,
39+ partition_day + 1
40+ );
41+ END LOOP;
42+ END
43+ $partition_setup$;
4044
4145 SELECT setseed(0.55083122);
4246
@@ -49,15 +53,14 @@ def main() -> None:
4953 'event ' || g || ' session=' || floor(power(random(), 2) * 100000)::integer
5054 FROM generate_series(1, { row_count } ) AS g;
5155 """
52- )
5356 subprocess .run (
5457 [os .environ .get ("PG_WORKLOAD_PSQL" , "psql" ), "-X" , "-q" , "-v" , "ON_ERROR_STOP=1" ],
5558 input = sql ,
5659 text = True ,
5760 check = True ,
5861 )
5962
60- print (f"Generated partition_aging: partitions={ len ( partitions ) } , events={ row_count } " )
63+ print (f"Generated partition_aging: partitions={ partition_count } , events={ row_count } " )
6164
6265
6366if __name__ == "__main__" :
0 commit comments