statement ok
CREATE SCHEMA s;
CREATE SEQUENCE seq;
CREATE SEQUENCE s.seq;
CREATE USER testuser2

# Ensure user must exist for set owner.
statement error pq: role/user "fake_user" does not exist
ALTER SEQUENCE seq OWNER TO fake_user

# Superusers can alter owner to any user which has CREATE privileges on the
# parent schema. This succeeds since all users have CREATE on the public schema
# by default.
statement ok
ALTER SEQUENCE seq OWNER TO testuser

statement error pq: user testuser does not have CREATE privilege on schema s
ALTER SEQUENCE s.seq OWNER TO testuser

# ALTER SEQUENCE IF EXISTS OWNER succeeds if the sequence does not exist.
statement ok
ALTER SEQUENCE IF EXISTS does_not_exist OWNER TO testuser

statement ok
GRANT CREATE ON SCHEMA s TO testuser, testuser2

statement ok
ALTER TABLE seq OWNER TO root

# ALTER VIEW cannot be used for sequences.
statement error pq: "seq" is not a view
ALTER VIEW seq OWNER TO testuser

statement ok
ALTER SEQUENCE seq OWNER TO testuser;
ALTER SEQUENCE s.seq OWNER TO testuser;
ALTER SEQUENCE seq OWNER TO root;
ALTER SEQUENCE s.seq OWNER TO root;

# Other users must be owner to alter the owner.
user testuser

statement error pq: must be owner of table seq
ALTER SEQUENCE seq OWNER TO testuser2

# Non-superusers also must be a member of the new owning role.
user root

statement ok
ALTER SEQUENCE seq OWNER TO testuser

user testuser

statement error must be member of role "testuser2"
ALTER SEQUENCE seq OWNER TO testuser2

user root

statement ok
GRANT testuser2 TO testuser

user testuser

statement ok
ALTER SEQUENCE seq OWNER TO testuser2
