简体   繁体   中英

Suppress “current transaction is aborted…” messages in PostgreSQL

I have a very big SQL dump I'm working on. The overall structure looks like this:

BEGIN;
SET CONSTRAINTS ALL DEFERRED;
INSERT …;
-- … ~300K insert operations
INSERT …;
COMMIT;

The problem is if there is an error in any single statement, then error is shown and the message current transaction is aborted, commands ignored until end of transaction block is generated for EACH statement that follows it.

Why does it behave so weirdly? Is there a way to suppress the following messages? It's enough to just show the real error message and to skip transaction execution. I don't want to see ~300K meaningful error messages.

Do I need to structure my dump differently? Or is there a flag/option I can use?

Presumably you're using psql to send the queries to the server.
You may set the ON_ERROR_STOP built-in variable to on .

From https://www.postgresql.org/docs/current/static/app-psql.html :

  ON_ERROR_STOP

  By default, command processing continues after an error. When this
  variable is set to on, processing will instead stop
  immediately. In interactive mode, psql will return to the command
  prompt; otherwise, psql will exit, returning error code 3 to
  distinguish this case from fatal error conditions, which are
  reported using error code 1.

It may be set from outside psql with psql -v ON_ERROR_STOP=on -f script.sql , or from inside the script or interactively with the meta-command \\set ON_ERROR_STOP on .

pg_dump does not have an option to add this automatically to a dump (as far as I know, it doesn't emit any psql meta-command anyway, only pure SQL commands).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM