Skip to Main Content

SQL & PL/SQL

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Using synonyms with dynamic SQL

chris_hereJul 19 2010 — edited Jul 20 2010
Hi Oracle experts,

I would like to have your opinion about a somewhat unorthodox use of synonyms that I'm currently considering. I'm using Oracle version 11.1.0.7.

Basically the situation is as follows: I have about 20 external tables with similar structures, let call them A1, A2 ... A20. I want to transform and load them into regular partitioned tables B1, B2 ... B20. The transformation from A to B is identical for each pair and can be expressed with a view V. What I'm considering is using synonyms in the definition of V in order to be able to reuse the code without duplicating the view. For instance I would write something like this (in reality the transformation is much more complex):
CREATE OR REPLACE SYNONYM source FOR A1;

CREATE OR REPLACE VIEW v AS
  SELECT col1, trim(col2), decode(col3, bla, bla)
  FROM source;

CREATE OR REPLACE PROCEDURE upload_data(suffix IN varchar2) AS
BEGIN
  EXECUTE IMMEDIATE 'CREATE OR REPLACE SYNONYM source FOR A' || suffix;
  EXECUTE IMMEDIATE 'CREATE OR REPLACE SYNONYM dest FOR B' || suffix;
  
  INSERT INTO dest
  SELECT * FROM v;
  
  COMMIT;
END;

EXECUTE upload_data(1);
EXECUTE upload_data(2);
...
EXECUTE upload_data(20);
Now this seems to work pretty well as long as the calls to the procedure are serialized. I experimented a bit (although not on this precise example I have to admit) and it seems that I can even in fact launch a new upload_data while a preceding one is still executing; i.e., the synonyms are used only during the parsing phase of the SQL statement and can be modified right after it.

So far so good. But what is going to happen if I try that in parallel? I have a potential race condition where two procedures could simultaneously try to update the synonyms. EXECUTE IMMEDIATE statements are not part of the MVCC system if I understand correctly. So my questions are the following:

1) Is there a way to put a lock on the synonyms somehow, or to use a semaphore of some kind here?
2) Is it worth the trouble, and a robust/supported solution, or is it just to crazy and dangereous and I should definitely duplicate my view 20 times (which can even be automated) ?

Thanks for your insight,
Chris
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Aug 17 2010
Added on Jul 19 2010
12 comments
3,331 views