Were you aware of the two hidden columns in CDB-Views?

Learning something new every day is the best part of my job. And as you can see and read on the blog, thanks to YOU, I learn a lot. This one I learned from Johannes Ahrends at my workshop for ora2know in Munich in early July this year. Were you aware of the two hidden columns in CDB-Views? I confess honestly: I wasn’t.

Two hidden columns?

Johannes asked me whether I know the reason for the two hidden columns in every CDB-view. His question was more like: It’s super-useful but why are these hidden? I did not even know what he was talking about. But we’ve exchanged emails the following day.

I was hooked up immediately and needed to investigate a bit more.

At first, I tried out a few things, and there they are, the two hidden columns:

  • CON$NAME
  • CDB$NAME

As Johannes, told me, they are in in every CDB_* view I checked. And as he praised, they are quite useful saving you one or the other join making queries easier, and potentially even faster due to the absence of an unnecessary join. I was really surprised. And I am fully aware that some of you may now smile and say: Under which rock has Mike lived the past years? Maybe … but for others it may be helpful to know.

Of course, I found blog posts about it written a while back. But even better, it is officially documented in the Oracle documentation. For instance, here in the 19c docs:

How could I miss this? Well, it is easy to not recognize this little paragraph:

CDB_* views include these hidden columns:

    • CON$NAME: This column includes the name of the container whose data a given CDB_* row represents
    • CDB$NAME: This column displays the name of the CDB whose data a given CDB_* row represents

That’s neat – and important.

 

Why hidden?

To be frank, I don’t know. But I have an idea. From a search in our bugDB I quickly found some interesting references. Without checking the code it looks to me as if this came in in Oracle Database 12.2.0.1. The reason for these hidden columns seems to be related to the CONTAINERS clause allowing to read data with one single SQL from various containers or PDBs.

When we discussed this internally in our team, Rodrigo immediately commented that those columns aren’t listed in DBA_TAB_COLUMNS, or anywhere else. And I found a hint for the reason why that is:

“These columns will continue to be hidden but they will not be marked as invisible.”

Some coding had been done to keep them hidden but not list them as invisible. Quite an interesting way to do things.

 

How can you use them?

Since the columns are there, you certainly are free to use them – especially since they are even documented in our documentation.

Let me take a simple query from my blog:

select CON_ID, DIRECTORY_NAME, DIRECTORY_PATH
  from CDB_DIRECTORIES
  where DIRECTORY_NAME='DATA_PUMP_DIR'
  order by CON_ID, DIRECTORY_NAME
;

The result looks like:

   CON_ID DIRECTORY_NAME    DIRECTORY_PATH                                                           
_________ _________________ ________________________________________________________________________ 
        1 DATA_PUMP_DIR     /u01/app/oracle/product/23/rdbms/log/                                    
        3 DATA_PUMP_DIR     /u01/app/oracle/product/23/rdbms/log/3BA33326F4C4F5BCE0634B00000A6175    
        4 DATA_PUMP_DIR     /u01/app/oracle/product/23/rdbms/log/3BA33326F4C6F5BCE0634B00000A6175    
        5 DATA_PUMP_DIR     /u01/app/oracle/product/23/rdbms/log/3BA33326F4C8F5BCE0634B00000A6175

But what if I want to see each container‘s name?

Then I need a join since the name of the PDB or root container is not in CDB_DIRECTORIES (I thought). This may lead to an imperfect query with an extra join, such as:

select d.CON_ID, p.NAME, d.DIRECTORY_NAME, d.DIRECTORY_PATH
  from CDB_DIRECTORIES d, V$PDBS p
  where d.DIRECTORY_NAME='DATA_PUMP_DIR' and
        d.CON_ID=p.CON_ID
  order by CON_ID, DIRECTORY_NAME
;

giving me this result (where CDB$ROOT is now missing):

   CON_ID NAME     DIRECTORY_NAME    DIRECTORY_PATH                                                           
_________ ________ _________________ ________________________________________________________________________ 
        3 RED      DATA_PUMP_DIR     /u01/app/oracle/product/23/rdbms/log/3BA33326F4C4F5BCE0634B00000A6175    
        4 BLUE     DATA_PUMP_DIR     /u01/app/oracle/product/23/rdbms/log/3BA33326F4C6F5BCE0634B00000A6175    
        5 GREEN    DATA_PUMP_DIR     /u01/app/oracle/product/23/rdbms/log/3BA33326F4C8F5BCE0634B00000A6175    

Not ideal – and complex.

If I’d known about the hidden columns before, it would have been much simpler (and quicker):

select CON_ID, CON$NAME, DIRECTORY_NAME, DIRECTORY_PATH
  from CDB_DIRECTORIES 
  where DIRECTORY_NAME='DATA_PUMP_DIR'
  order by CON_ID, DIRECTORY_NAME
;

By adding one additional column only (CON$NAME), I change the output to what I really want to see:

   CON_ID CON$NAME    DIRECTORY_NAME    DIRECTORY_PATH                                                           
_________ ___________ _________________ ________________________________________________________________________ 
        1 CDB$ROOT    DATA_PUMP_DIR     /u01/app/oracle/product/23/rdbms/log/                                    
        3 RED         DATA_PUMP_DIR     /u01/app/oracle/product/23/rdbms/log/3BA33326F4C4F5BCE0634B00000A6175    
        4 BLUE        DATA_PUMP_DIR     /u01/app/oracle/product/23/rdbms/log/3BA33326F4C6F5BCE0634B00000A6175    
        5 GREEN       DATA_PUMP_DIR     /u01/app/oracle/product/23/rdbms/log/3BA33326F4C8F5BCE0634B00000A6175

No unnecessary join anymore. And even CDB$ROOT is still there.

It could have been that easy.

Thanks, Johannes – and apologies to everyone who knew this for years smiling now.

 

Update:

Thanks to Birgit for telling me, these are documented now:

https://docs.oracle.com/en/database/oracle/oracle-database/19/refrn/cdb_-views.html

 

Further Links and Information

–Mike