This project is archived and is in readonly mode.

#4330 ✓stale
Julian Mehnle

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

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

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.

Comments and changes to this ticket

  • ...Paul

    ...Paul April 6th, 2010 @ 12:49 AM

    • Tag changed from databases.rake, pg_dump, postgresql, schema_search_path to 2.3.5, databases.rake, pg_dump, postgresql, schema_search_path
  • Julian Mehnle

    Julian Mehnle April 6th, 2010 @ 12:55 AM

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

  • Aditya Sanghi

    Aditya Sanghi October 12th, 2010 @ 10:11 AM

    • 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.

  • Aditya Sanghi

    Aditya Sanghi October 12th, 2010 @ 10:14 AM

    • Tag cleared.

    Automatic cleanup of spam.

  • Ryan Bigg

    Ryan Bigg October 19th, 2010 @ 08:26 AM

    Automatic cleanup of spam.

  • Santiago Pastorino

    Santiago Pastorino February 2nd, 2011 @ 04:48 PM

    • 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.

  • Santiago Pastorino

    Santiago Pastorino February 2nd, 2011 @ 04:48 PM

    • State changed from “open” to “stale”
  • bingbing

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

<h2 style="font-size: 14px">Tickets have moved to Github</h2>

The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>

Pages