|
5 | 5 | from airflow.providers.postgres.hooks.postgres import PostgresHook |
6 | 6 | from libs.enums import JobStageType, StageStatusType |
7 | 7 | from libs.queries import create_fields_query |
8 | | -from libs.settings import AIRFLOW_DAGRUN_TIMEOUT, AIRFLOW_DEBUG_MODE |
| 8 | +from libs.settings import ( |
| 9 | + AIRFLOW_DAGRUN_TIMEOUT, |
| 10 | + AIRFLOW_DEBUG_MODE, |
| 11 | + EXECUTE_VALUES_PAGE_SIZE, |
| 12 | +) |
9 | 13 | from libs.utils import update_job_status |
10 | 14 | from openpyxl.worksheet.worksheet import Worksheet |
| 15 | +from psycopg2.extras import execute_values |
11 | 16 |
|
12 | 17 | # PostgreSQL connection hook |
13 | 18 | pg_hook = PostgresHook( |
@@ -107,26 +112,26 @@ def update_temp_data_dictionary_table( |
107 | 112 |
|
108 | 113 | # Insert records into the temporary table |
109 | 114 | if dictionary_records: |
110 | | - pg_hook.insert_rows( |
111 | | - table=f"temp_data_dictionary_{scan_report_id}", |
112 | | - rows=[ |
113 | | - ( |
114 | | - d["table_name"], |
115 | | - d["field_name"], |
116 | | - d["value"], |
117 | | - d["value_description"], |
| 115 | + # Get a database connection - this automatically commits on success or rolls back on error |
| 116 | + with pg_hook.get_conn() as conn: |
| 117 | + # Create a cursor for executing SQL - this automatically closes when we're done |
| 118 | + with conn.cursor() as cursor: |
| 119 | + # Bulk insert all records efficiently using execute_values |
| 120 | + execute_values( |
| 121 | + cursor, |
| 122 | + f"INSERT INTO temp_data_dictionary_{scan_report_id} (table_name, field_name, value, value_description) VALUES %s", |
| 123 | + [ |
| 124 | + ( |
| 125 | + d["table_name"], |
| 126 | + d["field_name"], |
| 127 | + d["value"], |
| 128 | + d["value_description"], |
| 129 | + ) |
| 130 | + for d in dictionary_records |
| 131 | + ], |
| 132 | + page_size=EXECUTE_VALUES_PAGE_SIZE, |
118 | 133 | ) |
119 | | - for d in dictionary_records |
120 | | - ], |
121 | | - target_fields=[ |
122 | | - "table_name", |
123 | | - "field_name", |
124 | | - "value", |
125 | | - "value_description", |
126 | | - ], |
127 | | - fast_executemany=True, |
128 | | - commit_every=10000, # commit every 10k |
129 | | - ) |
| 134 | + conn.commit() |
130 | 135 |
|
131 | 136 | logging.info( |
132 | 137 | f"Created temporary data dictionary table with {len(dictionary_records)} records" |
@@ -198,24 +203,18 @@ def create_temp_field_values_table( |
198 | 203 | ) |
199 | 204 |
|
200 | 205 | if field_values_data: |
201 | | - pg_hook.insert_rows( |
202 | | - table=f"temp_field_values_{table_id}", |
203 | | - rows=[ |
204 | | - ( |
205 | | - d["field_name"], |
206 | | - d["value"], |
207 | | - d["frequency"], |
| 206 | + with pg_hook.get_conn() as conn: |
| 207 | + with conn.cursor() as cursor: |
| 208 | + execute_values( |
| 209 | + cursor, |
| 210 | + f"INSERT INTO temp_field_values_{table_id} (field_name, value, frequency) VALUES %s", |
| 211 | + [ |
| 212 | + (d["field_name"], d["value"], d["frequency"]) |
| 213 | + for d in field_values_data |
| 214 | + ], |
| 215 | + page_size=EXECUTE_VALUES_PAGE_SIZE, |
208 | 216 | ) |
209 | | - for d in field_values_data |
210 | | - ], |
211 | | - target_fields=[ |
212 | | - "field_name", |
213 | | - "value", |
214 | | - "frequency", |
215 | | - ], |
216 | | - fast_executemany=True, |
217 | | - commit_every=10000, # commit every 10k |
218 | | - ) |
| 217 | + conn.commit() |
219 | 218 |
|
220 | 219 | except Exception as e: |
221 | 220 | logging.error(f"Error creating data dictionary table: {str(e)}") |
|
0 commit comments