pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Erik Wienhold <ewie@ewie.name>
To: Dominique Devienne <ddevienne@gmail.com>
To: pgsql-general@lists.postgresql.org
Subject: Re: CREATE/DROP ROLE transactional? GRANT/REVOKE?
Date: Mon, 6 Mar 2023 15:02:29 +0100 (CET)
Message-ID: <1316927941.360065.1678111349572@office.mailbox.org> (raw)
In-Reply-To: <CAFCRh-8Q0eFxYPtHf=WstE=2cJertKcjP9k-BjBnTnkBr4B+aw@mail.gmail.com>
References: <CAFCRh-8Q0eFxYPtHf=WstE=2cJertKcjP9k-BjBnTnkBr4B+aw@mail.gmail.com>

> On 06/03/2023 14:19 CET Dominique Devienne <ddevienne@gmail.com> wrote:
>
> Perhaps I missed it in the doc (e.g. [1]), but are DDLs around ROLEs and
> GRANTs transactional?

Have you tried?  DDL is transactional unless stated otherwise (cf. CREATE DATABASE,
CREATE INDEX CONCURRENTLY, CREATE TABLESPACE).

Run the following psql script:

	drop role if exists alice, bob;

	\du

	begin;
	create role alice;
	\du
	rollback;

	\du

	begin;
	create role alice;
	create role bob;
	commit;

	\du

	begin;
	grant alice to bob;
	\du
	rollback;

	\du

	begin;
	drop role alice;
	\du
	rollback;

	\du

Output:

	DROP ROLE
	                                   List of roles
	 Role name |                         Attributes                         | Member of
	-----------+------------------------------------------------------------+-----------
	 postgres  | Superuser, Create role, Create DB, Replication, Bypass RLS | {}

	BEGIN
	CREATE ROLE
	                                   List of roles
	 Role name |                         Attributes                         | Member of
	-----------+------------------------------------------------------------+-----------
	 alice     | Cannot login                                               | {}
	 postgres  | Superuser, Create role, Create DB, Replication, Bypass RLS | {}

	ROLLBACK
	                                   List of roles
	 Role name |                         Attributes                         | Member of
	-----------+------------------------------------------------------------+-----------
	 postgres  | Superuser, Create role, Create DB, Replication, Bypass RLS | {}

	BEGIN
	CREATE ROLE
	CREATE ROLE
	COMMIT
	                                   List of roles
	 Role name |                         Attributes                         | Member of
	-----------+------------------------------------------------------------+-----------
	 alice     | Cannot login                                               | {}
	 bob       | Cannot login                                               | {}
	 postgres  | Superuser, Create role, Create DB, Replication, Bypass RLS | {}

	BEGIN
	GRANT ROLE
	                                   List of roles
	 Role name |                         Attributes                         | Member of
	-----------+------------------------------------------------------------+-----------
	 alice     | Cannot login                                               | {}
	 bob       | Cannot login                                               | {alice}
	 postgres  | Superuser, Create role, Create DB, Replication, Bypass RLS | {}

	ROLLBACK
	                                   List of roles
	 Role name |                         Attributes                         | Member of
	-----------+------------------------------------------------------------+-----------
	 alice     | Cannot login                                               | {}
	 bob       | Cannot login                                               | {}
	 postgres  | Superuser, Create role, Create DB, Replication, Bypass RLS | {}

	BEGIN
	DROP ROLE
	                                   List of roles
	 Role name |                         Attributes                         | Member of
	-----------+------------------------------------------------------------+-----------
	 bob       | Cannot login                                               | {}
	 postgres  | Superuser, Create role, Create DB, Replication, Bypass RLS | {}

	ROLLBACK
	                                   List of roles
	 Role name |                         Attributes                         | Member of
	-----------+------------------------------------------------------------+-----------
	 alice     | Cannot login                                               | {}
	 bob       | Cannot login                                               | {}
	 postgres  | Superuser, Create role, Create DB, Replication, Bypass RLS | {}

> Since I'm creating many ROLEs and making many GRANTs, based info I read from
> PostgreSQL itself (in pg_catalog and elsewhere), should everything be in a
> single transaction?

If it should be atomic and the commands are allowed in transactions, then yes,
use transactions.

--
Erik





view thread (7+ messages)  latest in thread

Message-ID: <1316927941.360065.1678111349572@office.mailbox.org>
Permalink:  ../1316927941.360065.1678111349572@office.mailbox.org/
Also on:    postgresql.org/message-id/1316927941.360065.1678111349572@office.mailbox.org

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-general@postgresql.org
  Cc: ewie@ewie.name, ddevienne@gmail.com
  Subject: Re: CREATE/DROP ROLE transactional? GRANT/REVOKE?
  In-Reply-To: <1316927941.360065.1678111349572@office.mailbox.org>
  References: <CAFCRh-8Q0eFxYPtHf=WstE=2cJertKcjP9k-BjBnTnkBr4B+aw@mail.gmail.com> <1316927941.360065.1678111349572@office.mailbox.org>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox