How to get available space in tablespace for a user (Oracle)

Emrah picture Emrah · May 13, 2011 · Viewed 43k times · Source

I'm working on a web application where I need to warn the user that they're running out of space in the given db user's tablespace. The application doesn't know the credentials of the db's system user, so I can't query views like dba_users, dba_free_space..etc.

My question is, is there a way in Oracle for a user to find out how much space there is left for them in their tablespace?

Thanks!

Answer

Emrah picture Emrah · May 16, 2011

Forgive my ignorance on the subject, for I believed only views available on data storage were dba_free_space etc..

I realized that for the logged user, there are user_free_space.. views for them. Modified version of the query mentioned here would be the answer my question.

Query is as follows: (Getting the space left on the DEFAULT_TABLESPACE of the logged user)

SELECT
  ts.tablespace_name,
  TO_CHAR(SUM(NVL(fs.bytes,0))/1024/1024, '99,999,990.99') AS MB_FREE
FROM
  user_free_space fs,
  user_tablespaces ts,
  user_users us
WHERE
  fs.tablespace_name(+)   = ts.tablespace_name
AND ts.tablespace_name(+) = us.default_tablespace
GROUP BY
  ts.tablespace_name;

It would return free space in MB