@@ -24,7 +24,8 @@ def print_timers() -> None:
2424def get_connection_string () -> str :
2525 port = os .environ .get ("BFD_DB_PORT" ) or "5432"
2626 dbname = os .environ .get ("BFD_DB_NAME" ) or "idr"
27- return f"host={ os .environ ['BFD_DB_ENDPOINT' ]} port={ port } dbname={ dbname } user={ os .environ ['BFD_DB_USERNAME' ]} password={ os .environ ['BFD_DB_PASSWORD' ]} "
27+ return f"host={ os .environ ['BFD_DB_ENDPOINT' ]} port={ port } dbname={ dbname } \
28+ user={ os .environ ['BFD_DB_USERNAME' ]} password={ os .environ ['BFD_DB_PASSWORD' ]} "
2829
2930
3031class PostgresLoader :
@@ -52,17 +53,21 @@ def load(
5253 update_set = ", " .join ([f"{ v } =EXCLUDED.{ v } " for v in insert_cols if v not in unique_key ])
5354 timestamp = datetime .now (UTC )
5455 table = model .table ()
55- # trim the schema from the table name to create the temp table (temp tables can't be created with an explicit schema set)
56+ # trim the schema from the table name to create the temp table
57+ # (temp tables can't be created with an explicit schema set)
5658 temp_table = table .split ("." )[1 ] + "_temp"
5759 with self .conn .cursor () as cur :
5860 # load each batch in a separate transaction
5961 for results in fetch_results :
6062 # Load each batch into a temp table
61- # This is necessary because we want to use COPY to quickly transfer everything into Postgres
62- # but COPY can't handle constraint conflicts natively.
63+ # This is necessary because we want to use COPY to quickly
64+ # transfer everything into Postgres, but COPY can't handle
65+ # constraint conflicts natively.
66+ #
6367 # Note that temp tables don't use WAL so that helps with throughput as well.
6468 #
65- # For simplicity's sake, we'll create our temp tables using the existing schema and just drop the columns we need to ignore
69+ # For simplicity's sake, we'll create our temp tables using the existing schema and
70+ # just drop the columns we need to ignore.
6671 temp_table_timer .start ()
6772 cur .execute (
6873 f"CREATE TEMPORARY TABLE { temp_table } (LIKE { table } ) ON COMMIT DROP" # type: ignore
@@ -78,12 +83,13 @@ def load(
7883 temp_table_timer .stop ()
7984
8085 # Use COPY to load the batch into Postgres.
81- # COPY has a number of optimizations that make bulk loading more efficient than a bunch of INSERTs.
82- # The entire operation is performed in a single statement, resulting in fewer network round-trips,
83- # less WAL activity, and less context switching.
86+ # COPY has a number of optimizations that make bulk loading more efficient
87+ # than a bunch of INSERTs.
88+ # The entire operation is performed in a single statement, resulting in
89+ # fewer network round-trips, less WAL activity, and less context switching.
8490
85- # Even though we need to move the data from the temp table in the next step, it should still be
86- # faster than alternatives.
91+ # Even though we need to move the data from the temp table in the next step,
92+ # it should still be faster than alternatives.
8793 copy_timer .start ()
8894 with cur .copy (f"COPY { temp_table } ({ cols_str } ) FROM STDIN" ) as copy : # type: ignore
8995 for row in results :
@@ -92,8 +98,10 @@ def load(
9298 copy_timer .stop ()
9399
94100 if len (results ) > 0 :
95- # For immutable tables, we may still be attempting to re-load some data due to a batch cancellation.
96- # In these cases, we can assume any conflicting rows have already been loaded so "DO NOTHING" is appropriate here.
101+ # For immutable tables, we may still be attempting to re-load some data
102+ # due to a batch cancellation.
103+ # In these cases, we can assume any conflicting rows have already been loaded so
104+ # "DO NOTHING" is appropriate here.
97105 on_conflict = (
98106 "DO NOTHING"
99107 if immutable
@@ -113,7 +121,8 @@ def load(
113121 insert_timer .stop ()
114122
115123 last = results [len (results ) - 1 ].model_dump ()
116- # Some tables that contain reference data (like contract info) may not have the normal IDR timestamps
124+ # Some tables that contain reference data (like contract info) may not have the
125+ # normal IDR timestamps.
117126 # For now we won't support incremental refreshes for those tables
118127 batch_timestamp_col = model .batch_timestamp_col (
119128 progress is None or progress .is_historical ()
0 commit comments