Trying to conversion a view from oracle to PostgreSQL getting error relation "sys.dba_data_files" does not exist - postgresql-9.5

CREATE OR REPLACE VIEW vtot_space (tablespace_name, tbytes)
AS
SELECT tablespace_name, sum(bytes) as tbytes
FROM sys.dba_data_files
group by tablespace_name
order by 1;

In postgress, to find the tablespace and its size you need following query:
SELECT spcname as tablespace_name,
pg_size_pretty(pg_tablespace_size(spcname)) as tbytes
FROM pg_tablespace;

Related

An equivalence of ".schema sqlite_master" in Oracle

I'm trying to do the same exercise for both SQLite and Oracle. In SQLite, there is a table sqlite_master containing a description of all of the other tables, indexes, triggers, and views that are contained within the database. I can see the query to generate sqlite_master with .schema sqlite_master.
In Oracle, the data dictionary is presented to us in the form of a number of views (DBA, ALL or USER). Let's take the table USER_TABLES as an example. We can do query on USER_TABLES, for example
SELECT table_name
FROM USER_TABLES;
Is there anyway to get the query used to create the table USER_TABLES in Oracle? I tried
SELECT dbms_metadata.get_ddl('TABLE', 'USER_TABLES')
FROM dual;
but it does not work.
I am not sure what are you looking for but if you want the definition of any table in oracle you can use :
Describe TableName
Or if you want to have list of tables, views or columns you can use below queries:
For Tables :
select * from select * from all_tables
For Columns :
select * from all_tab_columns
For Views :
select * from select * from all_views
To get all the column information of a table:
select *
from all_tab_columns
where upper(table_name) = upper('Test')
order by column_id
Typically oracle stores table_name in uppercase so I used upper() or you can just type 'TEST'
You could use this one:
DECLARE
DDL CLOB;
BEGIN
FOR aTab IN (SELECT TABLE_NAME FROM USER_TABLES) LOOP
DDL := DBMS_METADATA.GET_DDL('TABLE', aTab.TABLE_NAME);
DBMS_OUTPUT.PUT_LINE(DDL);
END LOOP;
END;
You may customize the output with DBMS_METADATA.SET_TRANSFORM_PARAM() before you run the query.

Trying to store a procedure in Oracle 11g to monitor tablespace storage

I'm trying to store this code in Oracle and everytime I get an error of compilation. I search about storing procedures without parameters and I didn't find a solution to fix this.
Here is the procedure that I'm trying to store:
CREATE OR REPLACE PROCEDURE Espacio_libre
BEGIN
select df.tablespace_name "Tablespace",
totalusedspace "MB Usados",
(df.totalspace - tu.totalusedspace) "MB Libres",
df.totalspace "MB Totales",
round(100 * ( (df.totalspace - tu.totalusedspace)/ df.totalspace))
"Pct Libre"
from
(select tablespace_name,
round(sum(bytes) / 1048576) TotalSpace
from dba_data_files
group by tablespace_name) df,
(select round(sum(bytes)/(1024*1024)) totalusedspace, tablespace_name
from dba_segments
group by tablespace_name) tu
where df.tablespace_name = tu.tablespace_name;
END Espacio_libre;
/
Thanks guys.
when you create a stored procedure in oracle, you are using PL/SQL code instead of SQL.
There is no select column,[column] from table in pl/SQL
You have to do 'Select column into variable from table'.
Please see the documentation below to learn elementary of PL/SQL programming.
[https://docs.oracle.com/cd/B19306_01/appdev.102/b14261/selectinto_statement.htm][1]
create GLOBAL TEMPORARY table test22jan16 (tablespace_name varchar2(100),MB_Libres varchar2(100),MB_Totales varchar2(100),Pct_Libre varchar2(100))
ON COMMIT DELETE ROWS;
insert into test22jan16
select df.tablespace_name ,
(df.totalspace - tu.totalusedspace),
df.totalspace,
round(100 * ( (df.totalspace - tu.totalusedspace)/ df.totalspace))
from
(select tablespace_name,
round(sum(bytes) / 1048576) TotalSpace
from dba_data_files
group by tablespace_name) df,
(select round(sum(bytes)/(1024*1024)) totalusedspace, tablespace_name
from dba_segments
group by tablespace_name) tu
where df.tablespace_name = tu.tablespace_name;
create a Global Temporary Table which stores the data then store the data in this table using above query
Create a view, it'll do what you are expecting from this procedure.
You cannot just have a select statement in a PL/SQL block without an INTO clause and variable.
You could also create a Refcursor as an OUT paramter

Oracle: Drop all tablespaces that meet a condition

I want to drop all tablespaces in my DB that have a particular pattern in their datafile names.
The below query gives me all the tablespaces whose datafile names obey this pattern:
SELECT TABLESPACE_NAME FROM DBA_DATA_FILES WHERE FILE_NAME LIKE '/vol1/u06%' ;
I want to drop all the tablespaces returned by the above query. But I'm unable to figure out how the outer query should be, because DROP TABLESPACE doesn't take a WHERE clause.
So, the outer query should look like DROP TABLESPACE tablespace_name..... where the tablespace_name comes one-by-one from the above pattern matching query.
(I'm using Oracle)
Thanks!
Here is what you need. But let me say that I would not recommend though because it can be dangerous to delete tablespaces in a dynamic script like this.
BEGIN
FOR rs in (SELECT TABLESPACE_NAME
FROM DBA_DATA_FILES WHERE FILE_NAME LIKE '/vol1/u06%') LOOP
BEGIN
EXECUTE IMMEDIATE 'DROP TABLESPACE ' || rs.TABLESPACE_NAME || ' INCLUDING CONTENTS AND DATAFILES CASCADE CONSTRAINTS';
END;
END LOOP;
END;

How to prevent a user from using space in a tablespace?

I tried the following commands,but i can still insert into the table on appts. Why?
MICHAEL#orcl#SQL> alter user michael quota 0M on appts;
User altered.
MICHAEL#orcl#SQL> select tablespace_name,max_bytes from user_ts_quotas;
TABLESPACE_NAME , MAX_BYTES
------------------------------,----------------
APPTS , 0
MICHAEL#orcl#SQL> select tablespace_name,table_name from user_tables;
TABLESPACE_NAME ,TABLE_NAME
------------------------------,------------------------------
APPTS ,TEST_D
....
MICHAEL#orcl#SQL> insert into test_d values(292,'Test',500,2100);
1 row created.
What about using ALTER TABLESPACE to make it read only? You could enter:
ALTER TABLESPACE APPTS READ ONLY

Oracle Schema tables row count

Oracle: 11g
OS: Linux
I have this very tricky questions which I am trying to solve but not able to get definite answers...
I did search on google...etc but not luck with my requirement...
Schema statistics are not reliable so want to query dba_tables..
also don't want to create any procedures or functions under database.. just trying to achieve with simple SQL.
Q.
How to spool all table row count of a particular schema and also display table_name?
A.
I can display count easily in spool but not able to get table name beside count..
e.g.
Table_Name Count
tab1 200
tab2 500
tab3 300
with below I can get count but not able to figure out table_name display in result...
spool runme.sql
select 'select count(*) from '|| owner || '.' || table_name || ';'
from dba_tables
where owner = 'user1'
order by table_name;
spool off
You can use a function like this, but it will be very slow:
create or replace
function get_rows( p_tname in varchar2 ) return number
as
l_columnValue number default NULL;
begin
execute immediate
'select count(*)
from ' || p_tname INTO l_columnValue;
return l_columnValue;
end;
/
select user, table_name,
get_rows( user||'.'||table_name) cnt
from user_tables
/
Code taken from this answer on Tom Kyte's site:
http://asktom.oracle.com/pls/asktom/f?p=100:11:::::P11_QUESTION_ID:1660875645686
Without a function call it is also possible:
select
table_name,
to_number(
extractvalue(
xmltype(
dbms_xmlgen.getxml('select count(*) c from '||table_name))
,'/ROWSET/ROW/C')) count
from user_tables;
From the tip here:
http://laurentschneider.com/wordpress/2007/04/how-do-i-store-the-counts-of-all-tables.html