impact analysis with pl/scope

16
Copyright © 2015 Oracle and/or its affiliates. All rights reserved. | Impact Analysis with PL/Scope 1 Steven Feuerstein Oracle Developer Advocate for PL/SQL Oracle CorporaDon Email: [email protected] TwiLer: @sfonplsql Blog: stevenfeuersteinonplsql.blogspot.com YouTube: PracDcally Perfect PL/SQL

Upload: steven-feuerstein

Post on 19-Mar-2017

139 views

Category:

Technology


0 download

TRANSCRIPT

Copyright©2015Oracleand/oritsaffiliates.Allrightsreserved.|

Impact Analysis with PL/Scope

1

StevenFeuersteinOracleDeveloperAdvocateforPL/SQL

OracleCorporaDon

Email:[email protected]:@sfonplsql

Blog:stevenfeuersteinonplsql.blogspot.comYouTube:PracDcallyPerfectPL/SQL

Copyright©2017Oracleand/oritsaffiliates.Allrightsreserved.| Page2

ResourcesforOracleDatabaseDevelopers• OfficialhomeofPL/SQL-oracle.com/plsql•  SQL-PL/SQLdiscussionforumonOTNhLps://community.oracle.com/community/database/developer-tools/sql_and_pl_sql

•  PL/SQLandEBRblogbyBrynLlewellyn-hLps://blogs.oracle.com/plsql-and-ebr

• OracleLearningLibrary-oracle.com/oll• WeeklyPL/SQLandSQLquizzes,andmore-plsqlchallenge.oracle.com•  AskTom-asktom.oracle.com–'nuffsaid•  LiveSQL-livesql.oracle.com–scriptrepositoryand12/712cdatabase•  oracle-developer.net-greatcontentfromAdrianBillington•  oracle-base.com-greatcontentfromTimHall

Copyright©2017Oracleand/oritsaffiliates.Allrightsreserved.| Page3

PL/Scope• Addedin11.1,compiler-driventoolthatcollectsinformaDonaboutidenDfiersandstatements,andstoresitindatadicDonaryviews.

• UsePL/ScopetoanswerquesDonslike:– Whereisavariableassignedavalueinaprogram?– Whatvariablesaredeclaredinsideagivenprogram?– Whichprogramscallanotherprogram(thatis,youcangetdowntoasubprograminapackage)?

– FindthetypeofavariablefromitsdeclaraDon.

• Andwith12.2,youcannowalsoanalyzeSQLstatementsinPL/SQL.

Copyright©2017Oracleand/oritsaffiliates.Allrightsreserved.| Page4

LifeWithoutPL/Scope• PriortoPL/Scope,analyzingimpactmostlymeanttextsearchesthroughfiles,orqueriesagainstALL_SOURCEandALL_DEPENDENCIESviews.

• ALL_DEPENDENCIESisfineforgivingyoudependencyinfoatthedatabaseobjectlevel,butnotbelow.– "FindallthepackagesthatreferencetableX".

• With11.1,Oraclenowsupportsfine-graineddependenciesforinvalidaDon,butthatinformaDonisnotavailableviadatadicDonaryviews.

SELECT owner , name , type

, referenced_owner || '.' || FROM all_dependencies AND referenced_type IN ('TABLE', 'VIEW') AND referenced_name = 'MY_TABLE' ORDER BY name, referenced_owner, referenced_name

11g_fgd*.sql

Copyright©2017Oracleand/oritsaffiliates.Allrightsreserved.| Page5

Ge<ngStartedwithPL/Scope

• PL/Scopemustbeenabled;itisoffbydefault.• Whenyourprogramiscompiled,informaDonaboutallidenDfiersarewriLentotheALL_IDENTIFIERSview.

•  YouthenquerythecontentsoftheviewtogetinformaDonaboutyourcode.

• ChecktheALL_PLSQL_OBJECT_SETTINGSviewforthePL/ScopeseongofaparDcularprogramunit.

ALTER SESSION SET plscope_settings='IDENTIFIERS:ALL'

Copyright©2017Oracleand/oritsaffiliates.Allrightsreserved.| Page6

KeyColumnsinALL_IDENTIFIERS•  TYPE

– ThetypeofidenDfier(VARIABLE,CONSTANT,etc.)• USAGE

– ThewaytheidenDfierisused(DECLARATION,ASSIGNMENT,etc.)•  LINEandCOL

– LineandcolumnwithinlineinwhichtheidenDfierisfound•  SIGNATURE

– UniquevalueforanidenDfier.EspeciallyhelpfulwhendisDnguishingbetweenoverloadingsofasubprogramor"connecDng"subprogramdeclaraDonsinpackagewithdefiniDoninpackagebody.

• USAGE_IDandUSAGE_CONTEXT_ID– RevealhierarchyofidenDfiersinaprogramunit

Copyright©2017Oracleand/oritsaffiliates.Allrightsreserved.| Page7

Startwithsomesimpleexamples•  ShowalltheidenDfiersinaprogramunit•  Showallvariablesdeclaredinasubprogram(notatpackagelevel)•  ShowallvariablesdeclaredinthepackagespecificaDons•  ShowthelocaDonswhereavariablecouldbemodified

plscope_demo_setup.sqlplscope_all_idents.sql

plscope_var_declares.sqlplscope_gvar_declares.sql plscope_var_changes.sql

Copyright©2017Oracleand/oritsaffiliates.Allrightsreserved.| Page8

Moreadvancedexamples•  FindexcepDonsthataredefinedbutneverraised•  ShowthehierarchyofidenDfiersinaprogramunit• ValidatenamingconvenDonswithPL/Scope

plscope_unused_excepDons.sqlplscope_hierarchy.sql

plscope_naming_convenDons.sql

Copyright©2017Oracleand/oritsaffiliates.Allrightsreserved.| Page9

PL/ScopeHelperUNliNes• Clearly,"datamining"inALL_IDENTIFIERScangetcomplicated.•  SuggesDonsforpuongPL/Scopetouse:

– Buildviewstohidesomeofthecomplexity.– Buildpackagestoprovidehigh-levelsubprogramstoperformspecificacDons.

plscope_helper_setup.sqlplscope_helper.pkg

Copyright©2017Oracleand/oritsaffiliates.Allrightsreserved.| Page10

12.2EnhancementstoPL/Scope

• GathersdataonSQLstatementsinPL/SQLprogramunits•  Youcannowfind:

– wherespecificcolumnsarereferenced– allprogramunitsperformingspecificDMLoperaDonsontable(andhelpyouconsolidatesuchstatements)

– allSQLstatementscontaininghints– alldynamicSQLusages–idealforgeongridofSQLinjecDonvulnerabiliDes– locaDonsinyourcodewhereyoucommitorrollback– mulDpleappearancesofsameSQLstatement(sameSQL_ID)

ALTER SESSION SET plscope_settings='IDENTIFIERS:ALL, STATEMENTS:ALL'

Copyright©2017Oracleand/oritsaffiliates.Allrightsreserved.| Page11

NewALL_STATEMENTSView•  TheALL_STATEMENTSview(alongwithUSER_STATEMENTS)containsinformaDonabouteachSQLstatementinprogramunitscompiledwithPL/Scopeenabled.– full_text –textofSQLstatement– has_into_record –INTOplsql_record– has_current_of –UsesCURRENTOFsyntax– has_for_update –UsesFORUPDATEsyntax– has_in_binds-– has_into_bulk –UsesBULKCOLLECTINTO– usage_id –SameaswithALL_IDENTIFIERS–anduniqueacrossbothtables!– sql_id –pointertoSQLstatementinv$views

Copyright©2017Oracleand/oritsaffiliates.Allrightsreserved.| Page12

SomeExamples

•  There'ssomuchyoucando!

SELECT owner, object_name, line, full_text FROM all_statements WHERE has_hint = 'YES'

FindSQLStatementswithHintsSELECT idt.line, idt.owner || '.' || idt.object_name code_unit, RTRIM (src.text, CHR (10)) text FROM all_identifiers idt , all_statements st , all_source src WHERE idt.usage = 'REFERENCE' AND idt.TYPE = 'TABLE' AND idt.name = table_in AND idt.owner = owner_in AND idt.line = src.line AND idt.object_name = src.name AND idt.owner = src.owner AND idt.usage_context_id = st.usage_id

FindAllDMLStatementsOnTable

Copyright©2017Oracleand/oritsaffiliates.Allrightsreserved.| Page13

MoreExamples!

SELECT sql_id, text, COUNT (*) FROM all_statements WHERE sql_id IS NOT NULL GROUP BY sql_id, text HAVING COUNT (*) > 1 /

SameSQLStatementUsed>1?

SameSQL_IDbutdifferentsignature.

SELECT * FROM all_statements WHERE has_into_bulk = 'YES' /

UsesBULKCOLLECTINTO?

Copyright©2017Oracleand/oritsaffiliates.Allrightsreserved.| Page14

MoreExamples:FinddynamicSQLSELECT st.owner, st.object_name, st.line, s.text FROM all_statements st, all_source s WHERE st.TYPE IN ('EXECUTE IMMEDIATE', 'OPEN') AND st.owner = s.owner AND st.object_name = s.name AND st.line = s.line UNION ALL

SELECT idnt.owner, idnt.object_name, idnt.line, src.text FROM all_identifiers idnt, all_source src WHERE idnt.owner <> 'SYS' AND idnt.signature IN (SELECT a.signature FROM all_identifiers a WHERE a.usage = 'DECLARATION' AND a.owner = 'SYS' AND a.object_name = 'DBMS_SQL' AND a.object_type = 'PACKAGE') AND idnt.owner = src.owner AND idnt.name = src.name AND idnt.line = src.line

NaDveDynamicSQL–easy!

DBMS_SQLReferences:mustrecompilebuilt-inwithPL/Scopeenabled!

Copyright©2017Oracleand/oritsaffiliates.Allrightsreserved.| Page15

Conclusions• PL/Scopegivesyoualevelofvisibilityintoyourcodethatwasneverbeforepossible.

• With12.2enhancementsaddinganalysisofSQL,youcannowperformdetailedanalysisoftheimpactofchangingyourdatastructures.

• Checkoutmy(andother)LiveSQLscriptsdemonstraDngPL/ScopecapabiliDes.

livesql.oracle.com

Doc:12.2DatabaseDevelopmentGuide

16