Running pg_dump while insert queries are still running?

pg-dump, postgresql, sql

Solution

It will obtain a shared lock on your tables when you run the `pg_dump`. Any transactions completed after you run the dump will not be included. So when the dump is finished, if there are current transactions in process that haven't been committed, they won't be included in the dump.

There is another `pg_dump` option with which it can be run:

`--lock-wait-timeout=timeout`

Do not wait forever to acquire shared table locks at the beginning of the dump. Instead fail if unable to lock a table within the specified timeout. The timeout may be specified in any of the formats accepted by SET statement_timeout. (Allowed formats vary depending on the server version you are dumping from, but an integer number of milliseconds is accepted by all versions.)

Problem

If I run `pg_dump` to dump a table into a SQL file, does it take a snapshot of the last row in the table, and dump all the rows up to this row? Or does it keep dumping all the rows, even those that were inserted after `pg_dump` was ran? A secondary question is: Is it a good idea to stop all insert queries before running `pg_dump`?

Original source