Safely handle PostgreSQL transaction failures

I have an n8n workflow that performs several PostgreSQL operations for the same customer. Sometimes the first few queries succeed, but a later query fails. This leaves the database in a partially updated state.
UPDATE customers
SET status = ‘processing’
WHERE id = $1;
INSERT INTO customer_logs (
customer_id,
action,
created_at
)
VALUES ($1, ‘processing_started’, NOW());
If the second operation fails, the customer can remain marked as processing without the corresponding log.
Should I use a database transaction, separate queries with rollback logic, or redesign the workflow to make each operation idempotent?
And how would you structure this in n8n so a failure halfway through doesn’t leave inconsistent data?

Describe the problem/error/question

What is the error message (if any)?

Please share your workflow

(Select the nodes on your canvas and use the keyboard shortcuts CMD+C/CTRL+C and CMD+V/CTRL+V to copy and paste the workflow.)

Share the output returned by the last node

Information on your n8n setup

  • n8n version:
  • Database (default: SQLite):
  • n8n EXECUTIONS_PROCESS setting (default: own, main):
  • Running n8n via (Docker, npm, n8n cloud, desktop app):
  • Operating system:

Hey @Selena_Gloria, while you wait for a response, here are some things that might help:

Suggested resources

Automatically matched to your question.

Docs:

Forum:

@Emmas, @Niffzy - you’ve helped with similar issues before, can you take a look?

Automatically suggested by n8n’s community bot. It’s a pilot - please share feedback here.

Hi @Selena_Gloria For this situation, I’d use a PostgreSQL transaction when the operations must succeed or fail together.

Keep related operations in one transaction

Conceptually:

BEGIN;

UPDATE customers
SET status = ‘processing’
WHERE id = $1;

INSERT INTO customer_logs (
customer_id,
action,
created_at
)
VALUES ($1, ‘processing_started’, NOW());

COMMIT;

If anything fails, use:ROLLBACK;

This prevents the database from being left half-updated.

Don’t keep transactions open too long

I wouldn’t keep a transaction open while n8n is calling external APIs or performing slow operations.

Keep the transaction limited to the PostgreSQL operations that need to be atomic.

For operations that don’t need to be atomic, make them idempotent instead of putting everything into one large transaction.

Hi @Selena_Gloria

To ensure data consistency, the most effective solution is to use a database transaction by combining your queries into a single PostgreSQL node. Instead of using separate nodes, wrap your UPDATE and INSERT statements within a BEGIN; and COMMIT; block. This ensures “atomicity,” meaning the database treats the operations as a single unit: if the second query fails, the entire transaction is discarded, preventing the customer from being stuck in a “processing” state without a corresponding log.

For long-term reliability, you should complement transactions with idempotency and error handling. Design your workflow so that if it is re-run after a failure, it can safely skip already-completed steps or handle duplicates without causing errors. Additionally, implement an Error Trigger workflow to catch failures and notify you immediately via Slack or email. This layered approach—combining database-level atomicity with n8n-level resilience and visibility—is the professional standard for production workflows

Hi @Selena_Gloria

1. Combine queries into a single Postgres node (Pure DB operations)

Run both operations atomically in one Postgres node using a Common Table Expression (CTE):

WITH updated_customer AS (
  UPDATE customers
  SET status = 'processing'
  WHERE id = $1
  RETURNING id
)
INSERT INTO customer_logs (customer_id, action, created_at)
SELECT id, 'processing_started', NOW()
FROM updated_customer;

(Alternatively, wrap the logic in a Postgres function/procedure and call* SELECT process_customer($1); *from a single node.)

2. If external APIs are between queries, use a compensating step.

If non-DB steps (like HTTP requests) are between your queries:

You can enable “Continue on Fail” or use an “Error Trigger.”

If a downstream node fails, use this route: “UPDATE customers SET status = ‘failed’ WHERE id = $1;”.

To avoid creating duplicate records when retrying, use an “idempotency_key” or timestamps.

Hi @Selena_Gloria
The Postgres node has a transaction mode built in. In the node, Add Option > Query Batching and set it to Transaction, then Postgres rolls back everything that node did if any query in it fails. It also covers the multi-item case, if the run is processing 50 customers and number 40 fails, the first 39 roll back with it.
The scope is the node, not the workflow, so separate Postgres nodes commit on their own: