Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

databases.rake: db:dump task abuses "schema_search_path" and makes false assumptions about `pg_dump --schema`

#4330

The db:rake task as defined in databases.rake is problematic on PostgreSQL databases:

  • It misinterprets the schema_search_path option defined in config/database.yml as an authoritative, complete list of schemas relevant to the application. It uses it to construct a list of --schema= options to pg_dump to dump only the schemas listed there.
  • It mistakenly assumes that "pg_dump --schema=foo,bar" will create a self-sufficient dump of the "foo" and "bar" schemas. This is not the case. pg_dump(1) explicitly points out that non-schema objects will not be dumped if --schema=... is specified, because they do not belong to any given schema.

This has the following real world consequence:

If an explicit schema_search_path is specified in config/database.yml, and the database has a procedural language — a non-schema object! — installed (e.g., plpgsql) that is not also installed in the template1 database, and any functions defined in any of the schemas specified in schema_search_path use that procedural language, then the dump created will not be self-sufficient and restoring the dump, e.g., during running of unit tests, will blatantly fail.

Note that there are other real-world cases where "pg_dump --schema=foo,bar" will fail to create self-sufficient dumps, e.g., when a table in schema foo has a column default or a constraint referencing a function in schema quux, or when it has a foreign key to a table in schema quux. And there are very legitimate reasons for not simply including all of a database's schemas in schema_search_path. It is a search path, not an authoritative, complete list of schemas being used by the application, after all!

I think a proper fix would be to never use the --schema= option in the db:dump task's invocation of pg_dump, thereby not limiting the dump to only a specific (or rather arbitrary) set of schemas. Since no data is being dumped this should not be prohibitive.

Reported by Julian Mehnle · April 6th, 2010 @ 12:45 AM

State: stale
Milestone: none
Assigned to: nobody
Importance: Low

Activity

  1. ...Paul
    ...Paul
    • Tag changed from databases.rake, pg_dump, postgresql, schema_search_path to 2.3.5, databases.rake, pg_dump, postgresql, schema_search_path

    April 6th, 2010 @ 12:49 AM

  2. Julian Mehnle
    Julian Mehnle

    This problem exists not only in 2.3.5 but also in the latest versions of Rails.

    April 6th, 2010 @ 12:55 AM

  3. Aditya Sanghi
    Aditya Sanghi
    • Tag changed from 2, databases.rake, pg_dump, postgresql, schema_search_path to 2.3.5 databases.rake pg_dump postgresql schema_search_path
    • Importance changed from to Low

    Automatic cleanup of spam.

    October 12th, 2010 @ 10:11 AM

  4. Aditya Sanghi
    Aditya Sanghi
    • Tag cleared.

    Automatic cleanup of spam.

    October 12th, 2010 @ 10:14 AM

  5. Ryan Bigg
    Ryan Bigg

    Automatic cleanup of spam.

    October 19th, 2010 @ 08:26 AM

  6. Santiago Pastorino
    Santiago Pastorino
    • State changed from new to open

    This issue has been automatically marked as stale because it has not been commented on for at least three months.

    The resources of the Rails core team are limited, and so we are asking for your help. If you can still reproduce this error on the 3-0-stable branch or on master, please reply with all of the information you have about it and add "[state:open]" to your comment. This will reopen the ticket for review. Likewise, if you feel that this is a very important feature for Rails to include, please reply with your explanation so we can consider it.

    Thank you for all your contributions, and we hope you will understand this step to focus our efforts where they are most helpful.

    February 2nd, 2011 @ 04:48 PM

  7. Santiago Pastorino
    Santiago Pastorino
    • State changed from open to stale

    February 2nd, 2011 @ 04:48 PM

  8. bingbing