Oracle 26ai New Feature: Read-Only Users and Sessions

When should we use read-only users and sessions?

At the user level: use READ ONLY when you need to temporarily prevent a local PDB user from modifying data or database objects, without revoking existing privileges. This can be useful during maintenance or investigations.

At the session level: use READ_ONLY when an application or user needs to temporarily prevent accidental writes in a particular session. If the user establishes a session while the user account is in READ WRITE mode, they can switch between read-only and read-write modes at the session level. However, if the session is established while the user account is in READ ONLY mode, the user cannot restore write access by changing the session-level setting.

Oracle AI Database 26ai supports both options without requiring the entire database or PDB to be opened in read-only mode. Let’s test how they work and, more importantly, what happens to existing sessions.

1. Test environment

I am running the tests on Oracle AI Database 26ai Enterprise Edition, version 23.26.3.0.0, in a RAC environment. The test user is local to ORCLPDB.

2. Test setup

As SYSDBA, create a local user in the PDB. Substitute a suitable test password for the placeholder.

ALTER SESSION SET CONTAINER = ORCLPDB;
CREATE USER ro_test IDENTIFIED BY "<test_password>"
DEFAULT TABLESPACE users
QUOTA 10M ON users;
GRANT CREATE SESSION, CREATE TABLE TO ro_test;

Connect as RO_TEST (sqlplus RO_TEST/"<test_password>"@ORCLPDB) and create a test table. Keep this connection open (Session 1).

SQL> CREATE TABLE accounts (
account_id NUMBER PRIMARY KEY,
balance NUMBER
);
Table created.
SQL> INSERT INTO accounts VALUES (101, 1000);
1 row created.
SQL> COMMIT;
Commit complete.
SQL> SELECT * FROM accounts;
ACCOUNT_ID BALANCE
---------- ----------
101 1000

3. Enable READ ONLY at the user level

From the SYSDBA session in ORCLPDB:

SQL> ALTER USER ro_test READ ONLY;
User altered.
SQL> SELECT username, account_status, read_only
FROM dba_users
WHERE username = 'RO_TEST';
USERNAME ACCOUNT_STATUS READ_ONLY
--------- --------------- ---------
RO_TEST OPEN YES

Notice that the original system privileges are still present:

SQL> SELECT privilege
FROM dba_sys_privs
WHERE grantee = 'RO_TEST';
PRIVILEGE
----------------------------------------
CREATE TABLE
CREATE SESSION

What happens to the existing RO_TEST connection? In Session 1, which was established in read-write mode, the UPDATE still succeeds:

SQL> UPDATE accounts SET balance = 2000
WHERE account_id = 101;
1 row updated.
SQL> ROLLBACK;
Rollback complete.

Now establish a new connection as RO_TEST (Session 2), while the user is marked READ ONLY.

SQL> SELECT * FROM accounts;
ACCOUNT_ID BALANCE
---------- ----------
101 1000
SQL> INSERT INTO accounts VALUES (102, 3000);
ERROR at line 1:
ORA-28194: Can perform read operations only
SQL> DELETE FROM accounts WHERE account_id = 101;
ERROR at line 1:
ORA-28194: Can perform read operations only
SQL> CREATE TABLE test_ddl (id NUMBER);
ERROR at line 1:
ORA-28194: Can perform read operations only
SQL> SELECT * FROM accounts FOR UPDATE;
ERROR at line 1:
ORA-28194: Can perform read operations only

The new session can read data, but DML, CREATE TABLE, and even SELECT FOR UPDATE are blocked.

4. Can a read-only user remove the restriction?

From Session 2, try disabling the session-level READ_ONLY parameter:

SQL> ALTER SESSION SET READ_ONLY = FALSE;
Session altered.
SQL> SELECT * FROM accounts FOR UPDATE;
ERROR at line 1:
ORA-28194: Can perform read operations only

Interestingly, ALTER SESSION succeeds, but the subsequent operation is still blocked. Changing the session parameter does not override the restriction inherited from the user account at login.

5. Restore READ WRITE at the user level

From the SYSDBA session:

SQL> ALTER USER ro_test READ WRITE;
User altered.

What happens to Session 2, which was established while the user was read-only? In my test, the existing session remained restricted:

SQL> UPDATE accounts SET balance = 3000
WHERE account_id = 101;
ERROR at line 1:
ORA-28194: Can perform read operations only
SQL> COMMIT;
ERROR at line 1:
ORA-28194: Can perform read operations only

Both enabling and disabling the user-level restriction affect new connections, not existing sessions.

Important for DBAs: If writes must stop immediately, changing the user to READ ONLY is not sufficient by itself. Existing read-write connections must be drained or disconnected as appropriate, including pooled database connections and sessions on other RAC instances. Coordinate this with the application team to avoid interrupting active transactions.

6. Enable READ ONLY at the session level

Now return to Session 1, which was established in read-write mode. First, update the balance to 3,000 and commit the transaction:

SQL> UPDATE accounts SET balance = 3000
WHERE account_id = 101;
1 row updated.
SQL> COMMIT;
Commit complete.

Now enable the session-level restriction:

SQL> ALTER SESSION SET READ_ONLY = TRUE;
Session altered.
SQL> SELECT * FROM accounts;
ACCOUNT_ID BALANCE
---------- ----------
101 3000
SQL> UPDATE accounts SET balance = 4000
WHERE account_id = 101;
ERROR at line 1:
ORA-28193: Can perform read operations only

Notice the different error code: ORA-28193 instead of ORA-28194.

Now disable the session-level restriction and retry the UPDATE in the same session:

SQL> ALTER SESSION SET READ_ONLY = FALSE;
Session altered.
SQL> UPDATE accounts SET balance = 4000
WHERE account_id = 101;
1 row updated.
SQL> COMMIT;
Commit complete.

Unlike the user-level setting, the session-level parameter takes effect in the current session. Enable it when no transaction is active. Since the user can disable this setting, it is useful as a safeguard against accidental writes, not as a substitute for administrator-enforced access control.

7. Verify READ WRITE access in a new session

Finally, open a new connection as RO_TEST (Session 3), after the DBA has restored READ WRITE at the user level in Section 5:

SQL> UPDATE accounts SET balance = 5000
WHERE account_id = 101;
1 row updated.

The UPDATE succeeds in the new session, confirming that write access has been restored, subject to the user’s existing privileges.

Conclusion

ORA-28193: the session is operating in read-only mode because of the session-level setting.

ORA-28194: the read-only restriction originates from the user account.

The user-level READ ONLY setting is an administrator-enforced security restriction, whereas the session-level READ_ONLY setting is primarily a safeguard against accidental writes. The session-level setting takes effect in the current session, but the user-level setting takes effect at login and cannot be overridden by setting READ_ONLY to FALSE.

References

Oracle AI Database Security Guide – Configuring Read-Only Users
Oracle AI Database SQL Language Reference – ALTER USER
Oracle AI Database Reference – READ_ONLY
Oracle Database Error Help – ORA-28193
Oracle Database Error Help – ORA-28194

Unknown's avatarAbout Mariami Kupatadze
Oracle Certified Master Linkedin: https://www.linkedin.com/in/mariami-kupatadze-01074722/

Leave a Reply