How to collect more statistics in Oracle? - oracle

I'm new to Oracle and would like to know the way how to get more statistics in the case i'll describe below. I'm using SQL Developer.
First, I execute this:
SET autotrace on;
SELECT NAME
FROM PASSENGER
WHERE ID_PSG IN (
SELECT ID_PSG
FROM PASS_IN_TRIP PIT JOIN TRIP T on T.TRIP_NO = PIT.TRIP_NO
WHERE UPPER(TOWN_FROM) = 'MOSCOW'
)
In Script Output I can see info about the query from PLAN_TABLE table and after that there's the text:
Unable to gather statistics please ensure user has correct access. The
statistic feature requires that the user is granted select on
v_$sesstat, v_$statname and v_$session.
I've tried to find a solution here already, there's a link:
SQL Developer : Unable to gather system statistics : insufficient privileges
So I executed the same commands and the grants were all succeded:
GRANT CREATE session TO PRACTICE;
GRANT GATHER_SYSTEM_STATISTICS TO PRACTICE;
GRANT CONNECT TO PRACTICE;
Then I disconnected, closed SQL dev, opened it, connected again as it was described in the solution from the link, ran
execute dbms_stats.gather_system_stats ('START');
and got this:
PL/SQL procedure successfully completed.
Then I thought everything is fine and tried to execute the code from the very beginning and its Script Output was still the same as before.
Do I have to grant anything else or this statistics can be found in the other place or I just did everything wrong?

That error is about sqlplus autotrace, it's not about gathering system statistics like your linked post seems to be about.
There is a role created specifically for these grants called plustrace https://docs.oracle.com/database/121/SQPUG/ch_eight.htm#SQPUG535
To use this feature, you must create a PLAN_TABLE table in your schema
and then have the PLUSTRACE role granted to you. DBA privileges are
required to grant the PLUSTRACE role. For information on how to grant
a role and how to create the PLAN_TABLE table, see the Oracle Database
SQL Language Reference.

Related

What user permissions are needed to reflect an Oracle Database?

I am trying to create a read-only user for an Oracle 11g Database that will be used by SQLAlchemy to query the database.
I was using an existing DBA user with SQLAlchemy and wasn't having any problems, but now with the new user, I am unable to reflect database tables.
Could not reflect: requested table(s) not available in Engine
Note that I can SELECT the tables, just not reflect them.
I am wondering what kind of permissions I need to give to the new user for it to able to reflect through SQLALchemy.
I tried copying all roles from the existing DBA to the new user, but still get the same error
I even tried some advanced roles that weren't used before (I plan on deleting and adding the user correctly again later.
DBACONSULTA is the new user I am creating.
GRANT DBA TO DBACONSULTA
GRANT EXECUTE ANY EVALUATION CONTEXT TO DBACONSULTA
GRANT ANALYZE ANY TO DBACONSULTA
GRANT SELECT ANY TABLE TO DBACONSULTA
GRANT EXECUTE ANY PROGRAM TO DBACONSULTA
With Python I use the following code:
engine=create_engine('oracle+cx_oracle://...')
metadata = MetaData()
metadata.reflect(engine, only=['tablename'])
Get the error:
Could not reflect: requested table(s) not available in Engine
I want to be able to reflect tables, without using the Declarative form from SQLAlchemy
Thanks in advance.
I believe I have found the answers.
Two things that are important:
the table name had to be in lowercase (didn't work using uppercase)
the schema was not defined (turn out it was working because the user I was using was the owner of the schema of the tables)
So, when i declare the schema and use lowercase for the tablename the reflection works.

Oracle SYS.AUD$ Audit Actions

We are running into tablespace issues as a result of a large SYS.AUD$ table. Upon further investigation, we identified that 99% of the items in this table were SELECT actions
SELECT COUNT(*) from SYS.AUD$ where ACTION# = 3;
334698880
SELECT COUNT(*) FROM SYS.AUD$;
335176012
However, we cannot find WHY these are being logged.
No system-wide auditing privileges set for SELECT (DBA_PRIV_AUDIT_OPTS)
No system-wide statement options set for SELECT (DBA_STMT_AUDIT_OPTS)
No specific objects being tracked (DBA_OBJ_AUDIT_OPTS)
Somehow these settings are being overridden. Any suggestions with regards to places to look?
We have resolved the issue - the log-stamps allowed us to correlate the issue with a scheduled job:
Privilege auditing audits statements that use a system privilege - SQL statements that require the audited privilege to succeed are recorded
The auditing privilege EXEMPT ACCESS POLICY was enabled system wide
The EXEMPT ACCESS POLICY privilege allows a role to execute a SQL command without invoking any policy function that may be associated with the target database object
A scheduled job was executing a package that built database objects - this privilege was required to execute the package
The Auditing Section of the Oracle 11g Security Guide was extremely useful in this investigation
Oracle 11g Security Guide

Is it possible to create trigger for "after connect" in oracle?

I am trying to do execute a trigger whenever a connect user/pass is executed in Oracle.
Tried with following after logon trigger, but it didn't execute:
CREATE OR REPLACE TRIGGER logon_trigger
AFTER LOGON
ON DATABASE
BEGIN
dbms_output.put_line('Logon detected');
END;
/
After creating the above trigger, tried the following connect operation but it didn't succeed:
conn scott/tiger#pdb1
conn system/manager#pdb1
Searched in AskTom and other Oracle related Q&A sites, but didn't get any right pointers to implement this. It would be great is someone can point out a right way to achieve this.
Your code as written would not succeed because the DBMS_OUTPUT would not be visible to you. It defaults to server output: off on connect. (Thanks Alex) You need to write to a table, or even better, use the built in Oracle auditing that writes to SYS.AUD$
ALTER SYSTEM SET AUDIT_TRAIL=DB SCOPE=SPFILE;
AUDIT ALL BY Your_User BY ACCESS;
Or just the basic access
AUDIT SESSION;

SQL Developer : Unable to gather system statistics : insufficient privileges

I was trying to gather system statistics to get the report information.
I tried the same via SQL developer and found some privillage issues and I referred this link for the solution,
GRANT CREATE session TO TEST_DB;
GRANT GATHER_SYSTEM_STATISTICS TO TEST_DB;
GRANT CONNECT TO TEST_DB;
All grant succeeded.But,
execute dbms_stats.gather_system_stats ('START');
gave me the error
ORA-20000: Unable to gather system statistics : insufficient privileges
ORA-06512: at "SYS.DBMS_STATS", line 23190
Finally, I tried with command line and finished the things without any issues,
PL/SQL procedure successfully completed.
why it was showing insufficient privileges issue in SQL developer ?
Role privileges like GATHER_SYSTEM_STATISTICS (as opposed to sys privileges, like CREATE TABLE) don't take effect immediately; they only take effect at the next logon (presumably because Oracle does some kind of caching internally).
So if you'd logged off and on in SQL Developer, it would have worked, as well.

Oracle how to "hide" table for other users

I'm using Oracle's 10g version.
In the database, I would like to create a configuration table and fill it with data.
Then the other users can not change anything in it, and even better that it was not at all visible to other users. Is it possible to somehow hide the table?
Regards
Create a separate schema for that table. Create a package that provides an API to your configuration data (e.g. to get a value that is needed by another program).
Revoke CREATE SESSION privilege from that schema (i.e. just don't grant any privileges to the schema at all). Don't grant any privileges on the table. The only users who will be able to see the table are those with DBA privileges.
The only thing that database sessions will be able to do is execute the package, IF they have been granted EXECUTE privilege on it.
If you do not grant enough privileges to other users, they could not see your objects.

Resources