• Stephen Frost's avatar
    pg_dump: Properly handle public schema ACLs with --clean · 330b84d8
    Stephen Frost authored
    pg_dump has always handled the public schema in a special way when it
    comes to the "--clean" option.  To wit, we do not drop or recreate the
    public schema in "normal" mode, but when we are run in "--clean" mode
    then we do drop and recreate the public schema.
    
    When running in "--clean" mode, the public schema is dropped and then
    recreated and it is recreated with the normal schema-default privileges
    of "nothing".  This is unlike how the public schema starts life, which
    is to have CREATE and USAGE GRANT'd to the PUBLIC role, and that is what
    is recorded in pg_init_privs.
    
    Due to this, in "--clean" mode, pg_dump would mistakenly only dump out
    the set of privileges required to go from the initdb-time privileges on
    the public schema to whatever the current-state privileges are.  If the
    privileges were not changed from initdb time, then no privileges would
    be dumped out for the public schema, but with the schema being dropped
    and recreated, the result was that the public schema would have no ACLs
    on it instead of what it should have, which is the initdb-time
    privileges.
    
    Practically speaking, this meant that pg_dump with --clean mode dumping
    a database where the ACLs on the public schema were not changed from the
    default would, upon restore, result in a public schema with *no*
    privileges GRANT'd, not matching the state of the existing database
    (where the initdb-time privileges would have been CREATE and USAGE to
    the PUBLIC role for the public schema).
    
    To fix, adjust the query in getNamespaces() to ignore the pg_init_privs
    entry for the public schema when running in "--clean" mode, meaning that
    the privileges for the public schema would be dumped, correctly, as if
    it was going from a newly-created schema to the current state (which is,
    indeed, what will happen during the restore thanks to the DROP/CREATE).
    
    Only the public schema is handled in this special way by pg_dump, no
    other initdb-time objects are dropped/recreated in --clean mode.
    
    Back-patch to 9.6 where the bug was introduced.
    
    Discussion: https://postgr.es/m/3534542.o3cNaKiDID%40techfox
    330b84d8
002_pg_dump.pl 109 KB