How to debug ORA-01775: looping chain of synonyms? - sql

I'm familiar with the issue behind ORA-01775: looping chain of synonyms, but is there any trick to debugging it, or do I just have to "create or replace" my way out of it?
Is there a way to query the schema or whatever to find out what the current definition of a public synonym is?
Even more awesome would be a graphical tool, but at this point, anything would be helpful.

As it turns out, the problem wasn't actually a looping chain of synonyms, but the fact that the synonym was pointing to a view that did not exist.
Oracle apparently errors out as a looping chain in this condition.

If you are using TOAD, go to View>Toad Options>Oracle>General and remove TOAD_PLAN_TABLE from EXPLAIN PLAN section and put PLAN_TABLE

The data dictionary table DBA_SYNONYMS has information about all the synonyms in a database. So you can run the query
SELECT table_owner, table_name, db_link
FROM dba_synonyms
WHERE owner = 'PUBLIC'
AND synonym_name = <<synonym name>>
to see what the public synonym currently points at.

The less intuitive solution to this error code seems to be problems with the objects that the synonym is pointing to.
Here is my SQL for finding synonyms that point to erroneous objects.
SELECT S.OWNER as SYN_OWNER, S.SYNONYM_NAME as SYN_NAME,
S.TABLE_OWNER as OBJ_OWNER, S.TABLE_NAME as OBJ_NAME,
CASE WHEN O.OWNER is null THEN 'MISSING' ELSE O.STATUS END as OBJ_STATUS
FROM DBA_SYNONYMS S
LEFT JOIN DBA_OBJECTS O ON S.TABLE_OWNER = O.OWNER AND S.TABLE_NAME = O.OBJECT_NAME
WHERE O.OWNER is null
OR O.STATUS != 'VALID';

Try this select to find the problematic synonyms, it lists all synonyms that are pointing to an object that does not exist (tables,views,sequences,packages, procedures, functions)
SELECT *
FROM dba_synonyms
WHERE table_owner = 'USER'
AND (
NOT EXISTS (
SELECT *
FROM dba_tables
WHERE dba_synonyms.table_name = dba_tables.TABLE_NAME
)
AND NOT EXISTS (
SELECT *
FROM dba_views
WHERE dba_synonyms.table_name = dba_views.VIEW_NAME
)
AND NOT EXISTS (
SELECT *
FROM dba_sequences
WHERE dba_synonyms.table_name = dba_sequences.sequence_NAME
)
AND NOT EXISTS (
SELECT *
FROM dba_dependencies
WHERE type IN (
'PACKAGE'
,'PROCEDURE'
,'FUNCTION'
)
AND dba_synonyms.table_name = dba_dependencies.NAME
)
)

Today I got this error, and after debugging I figured out that the actual tables were misssing, which I was referring using synonyms. So I suggest - first check that whether the tables exists!! :-))

Step 1) See what Objects exist with the name:
select * from all_objects where object_name = upper('&object_name');
It could be that a Synonym exists but no Table?
Step 2) If that's not the problem, investigate the Synonym:
select * from all_synonyms where synonym_name = upper('&synonym_name');
It could be that an underlying Table or View to that Synonym is missing?

A developer accidentally wrote code that generated and ran the following SQL statement CREATE OR REPLACE PUBLIC SYNONYM "DUAL" FOR "DUAL"; which caused select * from dba_synonyms where table_name = 'DUAL';
to return PUBLIC DUAL SOME_USER DUAL rather than PUBLIC DUAL SYS DUAL.
We were able to fix it (thanks to How to recreate public synonym "DUAL"?) by running
ALTER SYSTEM SET "_SYSTEM_TRIG_ENABLED"=FALSE SCOPE=MEMORY;
CREATE OR REPLACE PUBLIC SYNONYM DUAL FOR SYS.DUAL;
ALTER SYSTEM SET "_SYSTEM_TRIG_ENABLED"=true SCOPE=MEMORY;

While Jarrod's answer is a good idea, and catches a broader range of related problems, I found this query found in Oracle forums to more directly address the (originally stated) issue:
select owner, synonym_name, connect_by_iscycle CYCLE
from dba_synonyms
where connect_by_iscycle > 0
connect by nocycle prior table_name = synonym_name
and prior table_owner = owner
union
select 'PUBLIC', synonym_name, 1
from dba_synonyms
where owner = 'PUBLIC'
and table_name = synonym_name
and (table_name, table_owner) not in (select object_name, owner from dba_objects
where object_type != 'SYNONYM')
https://community.oracle.com/message/4176300#4176300
You will not have to wade through other kinds of invalid objects. Just those that are actually in endless loops.

I had a similar problem, which turned out to be caused by missing double quotes off the table and schema name.

We had the same ORA-01775 error but in our case, the schema user was missing some 'grant select' on a couple of the public synonyms.

We encountered this error today.
This is how we debugged and fixed it.
Package went to invalid state due to this error ORA-01775.
With the error line number , We went thru the package body code and found the code which was trying to insert data into a table.
We ran below queries to check if the above table and synonym exists.
SELECT * FROM DBA_TABLES WHERE TABLE_NAME = '&TABLE_NAME'; -- No rows returned
SELECT * FROM DBA_SYNONYMS WHERE SYNONYM_NAME = '&SYNONYM_NAME'; -- 1 row returned
With this we concluded that the table needs to be re- created. As the synonym was pointing to a table that did not exist.
DBA team re-created the table and this fixed the issue.

ORA-01775: looping chain of synonyms
I faced the above error while I was trying to compile a Package which was using an object for which synonym was created however underlying object was not available.

I'm using the following sql to find entries in all_synonyms where there is no corresponding object for the object_name (in user_objects):
select *
from all_synonyms
where table_owner = 'SCOTT'
and synonym_name not like '%/%'
and table_name not in (
select object_name from user_objects
where object_type in (
'TABLE', 'VIEW', 'PACKAGE', 'SEQUENCE',
'PROCEDURE', 'FUNCTION', 'TYPE'
)
);

http://ora-01775.ora-code.com/ suggests:
ORA-01775: looping chain of synonyms
Cause: Through a series of CREATE synonym statements, a synonym was defined that referred to itself. For example, the following definitions are circular:
CREATE SYNONYM s1 for s2 CREATE SYNONYM s2 for s3 CREATE SYNONYM s3 for s1
Action: Change one synonym definition so that it applies to a base table or view and retry the operation.

If you are compiling a PROCEDURE, possibly this is referring to a table or view that does not exist as it is created in the same PROCEDURE. In this case the solution is to make the query declared as String eg v_query: = 'insert into table select * from table2 and then execute immediate on v_query;
This is because the compiler does not yet recognize the object and therefore does not find the reference. Greetings.

I had a function defined in the wrong schema and without a public synonym. I.e. my proc was in schema "Dogs" and the function was in schema "Cats". The function didn't have a public synonym on it to allow Dogs to access the cats' function.

For me, the table name and the synonym both existed but under different owner names. I re-created the tables under the owner name that matched the owner name in synonyms.
I used the queries posted by #Mahi_0707

Related

How to get the name of stored procedure within a package and a schema accessing a particular table in Oracle?

I have used following query to get the procedure list but I am getting just the schema name and package name. If the TYPE Column returns PACKAGE BODY, then how to get the procedure name within the package accessing the table EXCEPTIONAL_INFO, please help.
SELECT * FROM All_DEPENDENCIES WHERE REFERENCED_NAME = 'EXCEPTIONAL_INFO';
You can scan the source texts of the packages. I've used the view DBA_SOURCE because I believe ALL_SOURCE shows only the code of procedures I can execute.
CREATE TABLE exceptional_info (i INT);
CREATE OR REPLACE PACKAGE p IS
PROCEDURE a;
PROCEDURE b(table_name VARCHAR2);
END p;
/
CREATE OR REPLACE PACKAGE BODY p IS
PROCEDURE a AS
n NUMBER;
BEGIN
SELECT count(*) INTO n FROM exceptional_info;
END a;
PROCEDURE b(table_name VARCHAR2) AS
BEGIN
EXECUTE IMMEDIATE 'TRUNCATE TABLE '||table_name;
END b;
END;
/
You can find the packages with DBA_DEPENDENCIES and scan the source text of those dependencies:
SELECT owner, type, name, s.line, s.text
FROM dba_dependencies d
LEFT JOIN dba_source s USING (owner, name, type)
WHERE d.referenced_name = 'EXCEPTIONAL_INFO'
AND upper(s.text) like '%EXCEPTIONAL_INFO%';
OWNER TYPE NAME LINE TEXT
SO PACKAGE BODY P 10 SELECT count(*) INTO n FROM exceptional_info;
You need to go up from line 10 to find the name of the procedure in the source text. I'm too lazy to code that now.
However, this method will find only static text. It cannot find PROCEDURE b in the example, which also can modify table exceptional_info, without having the name hard coded. Do you need to catch those usages, too?
you can try this query:
select owner, object_name, procedure_name
from all_procedures
where object_name =
(
SELECT name FROM All_DEPENDENCIES WHERE REFERENCED_NAME = 'EXCEPTIONAL_INFO'
and type='PACKAGE BODY' and rownum=1
)
;

How to find the tables affected in a PL SQL package?

I have to find out the list of tables into which insertion or updation happens in my existing PL SQL package. I started analyzing the package. The concern is the package code runs in thousands of lines of code and in turn
calls many other packages. Also, the code was not written by me. I cannot run AWR report since it is Development environment.
Is there a way to get the tables into which insertion/updation happens after initiating the transaction?
Could a trigger be written to suit my requirement?
-- plain
select *
from dba_dependencies
where name = 'PACKAGE_NAME' and owner = 'PACKAGE_OWNER'
and type in ('PACKAGE', 'PACKAGE BODY') and referenced_type = 'TABLE';
-- hierarchy
select distinct referenced_owner, referenced_name, referenced_type, referenced_link_name
from dba_dependencies
where referenced_type = 'TABLE'
start with name = 'PACKAGE_NAME' and owner = 'PACKAGE_OWNER'
and type in ('PACKAGE', 'PACKAGE BODY')
connect by nocycle prior referenced_name = name and prior referenced_owner = owner
and replace(prior referenced_type, 'PACKAGE BODY', 'PACKAGE') = replace(type, 'PACKAGE BODY', 'PACKAGE')
and referenced_owner not in ('SYS', 'SYSTEM', 'OUTLN' , 'AUDSYS')
order by 1, 2, 3;
#akk0rd87 has the better answer -- DBA_DEPENDENCIES, with a CONNECT BY to get table usages by called procedures.
The only thing his answer wouldn't find is tables used directly because of dynamic SQLs (e.g., EXECUTE IMMEDIATE). For that, you can use fine-grained auditing.
To me, this approach is just a backup after you follow #akk0rd87's advice.
Since SO in not a free code writing service, I'll just give you the broad strokes.
1) Create a table to serve as your audit trail. Make sure it has the following columns, at least:
SCHEMA_NAME (30 chars)
TABLE_NAME (30 chars)
CALL_STACK (4000 chars)
SQL_STMT (4000 chars)
2) Create a package to serve as an audit handler. I will give you code for this, because it must follow the exact API I give to be usable with fine grained auditing.
CREATE OR REPLACE PACKAGE BODY xxcust_table_access_aud_pkg AS
PROCEDURE audit_access ( schema_name VARCHAR2, table_Name VARCHAR2, policy_name VARCHAR2 ) IS
BEGIN
INSERT INTO your_audit_table ( SCHEMA_NAME, TABLE_NAME, CALL_STACK, SQL_STMT )
VALUES ( schema_name,
table_Name,
substr(DBMS_UTILITY.format_call_stack,1,4000),
substr(SYS_CONTEXT ('userenv', 'CURRENT_SQL'),1,4000)
)
EXCEPTION
WHEN others THEN
null;
END;
END xxcust_table_access_aud_pkg ;
3) Loop through all the tables in your application schema and call DBMS_FGA.ADD_POLICY for each one. E.g.,
FOR r IN ( ... all my tables ... ) LOOP
DBMS_FGA.add_policy(object_schema=> r.owner,
object_name => r.table_name,
policy_Name => -- make up something unique, maybe table_name plus some number,
audit_condition => '1=1',
audit_column => null,
handler_schema => -- your schema,
handler_module => 'XXCUST_TABLE_ACCESS_AUD_PKG', -- the package above
enable => true);
4) Run your package and check the table for results
5) Repeat step 3, but drop the policies instead of adding them.
Not sure if a Trigger can be a solution, Below can be one of the solutions, with some manual effort though:
DO a grep on the main package to find the other package names, note the names of all packages including the main one.
Get the source code of all these packages from " all_source" table, with where clause on "NAME" attribute and specifying the package names identified above.
Spool the source for above packages in a text file and do a grep on insert and update commands.

Creating Synonym in SQLDevloper- Object name drop down empty?

I am trying to create a synonym in using SQLDeveloper with this video tutorial.
When I right click and "create new synonym" I can enter the Synonym name and the Object Owner, but the drop down menu is empty for Object Name.
How do I solve this?
This is the query we run to populate the 'Object Name' list.
select object_name from sys.all_objects where object_type in ('TABLE', 'VIEW', 'SEQUENCE', 'PROCEDURE', 'FUNCTION', 'PACKAGE', 'TYPE', 'SYNONYM')
and owner = :1 order by object_name
You have probably selected a schema for which there are no tables, views, sequences, or PL/SQL objects that you have SELECT access to - hence it doesn't appear in YOUR all_object's view.
Run that query in a SQL Worksheet to confirm that indeed, 0 rows are selected.
Then go ask your DBA to grant you privs such that you can work with those objects.

How to check if a database object in Oracle is a table or view

I have list of object names from which i have to find out whether the object is a table or view. For this reason i have to query and check in all_tables and all_views and confirm whether the object is table or view. I am using below queries and its working. But as i have huge list of object names I want to perform this in single query and check whether the object is table or view and also the owner of the object.
select * from ALL_views where view_name like '%INSTANCE%'
select * from all_tables where table_name like '%INSTANCE%'
select *
from all_objects
where object_name like '%INSTANCE%'
There is an OBJECT_TYPE column in there.
How about using all_objects instead?
E.g.:
select owner,
object_name,
object_type
from all_objects
where object_type in ('TABLE', 'VIEW')
and object_name in (....);

Query to display all tables and views

I've come up with a query that displays all tables and views of a specific owner. What I would like to do now, but am having an issue with, is that I would like to have a second column which line by line will indicate whether the field is a "table" or "view". Is this possible? if so, how might I go about this?
select table_name
from all_tables
where owner = '<owner>'
UNION
select view_name
from all_views
where owner = '<owner>'
order by table_name;
I'd prefer the xxx_objects views myself for this purpose (as Justin says), but if you specifically need other data from the table and view views, you can add extra info thus:
select 'Table' AS object_type, table_name
from all_tables
where owner = '<owner>'
UNION ALL
select 'View' AS object_type, view_name
from all_views
where owner = '<owner>'
order by table_name;
Note I've changed it to use UNION ALL because there will be no collisions between the two result sets.
I'd use all_objects instead
select object_name, object_type
from all_objects
where object_type in ('TABLE', 'VIEW')
and owner = <<schema name>>
order by object_name