Re: `pg_restore --if-exists` clarification

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: "David G(dot) Johnston" <david(dot)g(dot)johnston(at)gmail(dot)com>
Cc: Gulyás Attila <toraritte(at)gmail(dot)com>, "pgsql-docs(at)lists(dot)postgresql(dot)org" <pgsql-docs(at)lists(dot)postgresql(dot)org>
Subject: Re: `pg_restore --if-exists` clarification
Date: 2023-09-28 17:03:46
Message-ID: 998115.1695920626@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-docs

"David G. Johnston" <david(dot)g(dot)johnston(at)gmail(dot)com> writes:
>>> .. But pragmatically it doesn't matter, so why not
>>> reclassify this as a "warning" or a "notice"?

> Because pg_restore is just a client and it is repeating back what the
> server tells it. And for the server it is an error to drop an object that
> doesn’t exist.
> psql and pg_restore, as clients, can choose to ignore the errors they see,
> regardless of what kind of error it is, but they don’t take on the added
> burden of trying to reclassify errors into something else.

If you actually try this, you'll get output like

...
pg_restore: error: could not execute query: ERROR: schema "fkpart6" does not exist
Command was: DROP SCHEMA fkpart6;
pg_restore: error: could not execute query: ERROR: schema "fkpart5" does not exist
Command was: DROP SCHEMA fkpart5;
pg_restore: error: could not execute query: ERROR: schema "fkpart4" does not exist
Command was: DROP SCHEMA fkpart4;
pg_restore: error: could not execute query: ERROR: schema "fkpart3" does not exist
Command was: DROP SCHEMA fkpart3;
...
pg_restore: warning: errors ignored on restore: 1488

so the only real problem is that there are (typically) enough of these
to obscure any errors that might be of greater significance. Still,
genuine restore errors would come out after the blizzard of failed
DROPs, so I don't think there's a big problem in practice.

If we actually wanted to change any behavior here, I think what would
be most profitable to discuss is making --if-exists the default.
Not sure if we want to go there, but if we'd had the DROP IF EXISTS
option all along I bet it would have been done that way.

In any case, it's fair to complain about the documentation.
How about

--clean

Before restoring database objects, issue commands to DROP all the
objects that will be restored. This option is useful for overwriting
an existing database. If any of the objects do not exist in the
destination database, ignorable error messages will be reported,
unless --if-exists is also specified.

--if-exists

Use DROP ... IF EXISTS commands to drop objects in --clean mode.
This suppresses "does not exist" errors that might otherwise be
reported. This option is not valid unless --clean is also specified.

regards, tom lane

In response to

Responses

Browse pgsql-docs by date

  From Date Subject
Next Message Gulyás Attila 2023-09-29 14:30:47 Re: `pg_restore --if-exists` clarification
Previous Message David G. Johnston 2023-09-28 16:36:34 Re: `pg_restore --if-exists` clarification