From 0b5f37f0e2367020983cc70c53a3368f3b768982 Mon Sep 17 00:00:00 2001 From: Abdel Jaidi Date: Wed, 1 May 2024 10:08:54 +0100 Subject: [PATCH] feat: add commit_transaction to postgres.to_sql --- awswrangler/postgresql.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/awswrangler/postgresql.py b/awswrangler/postgresql.py index 88c8f06cc..0617aea74 100644 --- a/awswrangler/postgresql.py +++ b/awswrangler/postgresql.py @@ -502,6 +502,7 @@ def to_sql( chunksize: int = 200, upsert_conflict_columns: list[str] | None = None, insert_conflict_columns: list[str] | None = None, + commit_transaction: bool = True, ) -> None: """Write records stored in a DataFrame into PostgreSQL. @@ -542,6 +543,8 @@ def to_sql( insert_conflict_columns: List[str], optional This parameter is only supported if `mode` is set top `append`. In this case conflicts for the given columns are checked for evaluating the insert 'ON CONFLICT DO NOTHING'. + commit_transaction: bool + Whether to commit the transaction. True by default. Returns ------- @@ -606,7 +609,8 @@ def to_sql( sql: str = f"INSERT INTO {_identifier(schema)}.{_identifier(table)} {insertion_columns} VALUES {placeholders}{upsert_str}" _logger.debug("sql: %s", sql) cursor.executemany(sql, (parameters,)) - con.commit() + if commit_transaction: + con.commit() except Exception as ex: con.rollback() _logger.error(ex)