Db2

Db2

Where DBAs and data experts come together to stop operating and start innovating. Connect, share, and shape the AI era with us.


#Data


#Data
#Databases
#Operatingsystems
#Db2
#Databasesolutions
 View Only
Expand all | Collapse all

How to audit the ALTER TABLE statements?

  • 1.  How to audit the ALTER TABLE statements?

    Posted 01/06/26 06:19 AM
    Hi All,
     
    A (quick?) question for the community:
     
    Have you ever tried auditing (or monitoring) the ALTER TABLE statements in DB2 and how did you go around doing it?
     
    I'm specifically interested in monitoring the creating/dropping of the FKs (ALTER TABLE ... ADD/DROP FOREIGN KEY ...), and I would like to know which user ID executed the SQL statement, when and from where (IP address - in case of remote client connections).
     
    The seemingly simplest choice - setting up the Audit facility for the OBJMAINT category - doesn't work, as it only collects the CREATE_OBJECT, DROP_OBJECT and RENAME_OBJECT events.
    But it doesn't collect the ALTER_OBJECT events which I need here (except some exotic exceptions, related to "protected tables or modules").
     :-(
     
    Of course, if I wanted to monitor this in a live PRODUCTION datbase (as I do :-), then activating the EXECUTE category of the Audit facility isn't really an option, as this will try to record every single SQL execution (and most certainly very quickly bring down any busy PROD environment).
     
    The third option - setting up an Event Monitor for Change History - does record the ALTER TABLE statements, and it tells you "when", but it cannot tell you who executed the statement (user ID), nor from where (IP address).
     
    Any other ideas?
    Kind regards, Damir


    ------------------------------
    Damir Wilder
    Senior Consultant
    Triton Consulting
    London
    ------------------------------


  • 2.  RE: How to audit the ALTER TABLE statements?

    Posted 01/06/26 06:47 AM

    Perhaps the table CHANGESUMMARY_evmon-name has the info you are looking for? 

    When you created Change history event monitor - did you look which this event monitor related tables were modified when you issued for example ALTER TABLE ... ADD/DROP FOREIGN KEY?



    ------------------------------
    Jan Nelken
    Db2 (LUW) DBA
    Open for work
    Katowice or Krakow
    +48 783 109 863
    ------------------------------



  • 3.  RE: How to audit the ALTER TABLE statements?

    Posted 01/06/26 09:42 AM

    Thanks for that Jan (and Madhusudan),

    I will look into setting up this table and checking it out.

    The reason I haven't already spotted it is because the Event Monitor was created just to collect the DDLSTMTEXEC data:

    db2 "CREATE EVENT MONITOR EVMON_CHANGE_HISTORY FOR CHANGE HISTORY WHERE EVENT IN (DDLALL) WRITE TO TABLE DDLSTMTEXEC (TABLE DB2INST1.DDLSTMT_HISTORY IN USERSPACE1) AUTOSTART";

    So, if I change the definition (and re-create the Ev.Mon.) as:

    db2 "CREATE EVENT MONITOR EVMON_CHANGE_HISTORY FOR CHANGE HISTORY WHERE EVENT IN (DDLALL) WRITE TO TABLE DDLSTMTEXEC (TABLE DB2INST1.DDLSTMT_HISTORY IN USERSPACE1), CHANGESUMMARY (TABLE DB2INST1.DDLSTMT_HISTORY_SUMMARY) AUTOSTART";

    That should give me the info that I need, right?

    I will give it a go A.S.A.P. and report back the result!

    Kind regards, Damir



    ------------------------------
    Damir Wilder
    Senior Consultant
    Triton Consulting
    London
    ------------------------------



  • 4.  RE: How to audit the ALTER TABLE statements?

    Posted 01/06/26 03:29 PM

    Keep in mind that each change-related event is uniquely identified by the following three key fields:

    Event timestamp
    The time that the event occurred.
    Event ID
    A numeric token that ensures uniqueness in cases where the event timestamp is common.
    Member
    The database manager process where the event occurred.


    ------------------------------
    Jan Nelken
    Db2 (LUW) DBA
    Open for work
    Katowice or Krakow
    +48 783 109 863
    ------------------------------



  • 5.  RE: How to audit the ALTER TABLE statements?

    Posted 01/07/26 04:48 AM

    Hi Jan,

    Just to confirm: after updating the Ev.Mon. for Change History (to include the event group CHANGESUMMARY), I can see the USERID and the remote client's IP address in the DDLSTMT_HISTORY_SUMMARY table!

    Regarding the unique identification of monitoring events, the Db2 documentation goes even one step further, specifying four fields:

    Change history event monitor record
    The ID is recycled at database activation time. The uniqueness of events is guaranteed by the combination of event_timestamp, event_id, member, and event-type.

    ...and one would naively believe that using just the EVENT_ID would be sufficient for the purpose :-)

    Kind regards, Damir



    ------------------------------
    Damir Wilder
    Senior Consultant
    Triton Consulting
    London
    ------------------------------



  • 6.  RE: How to audit the ALTER TABLE statements?

    Posted 01/07/26 06:27 AM

    Hi Damir, 

    V12.1 documentation states only three fields:
    https://www.ibm.com/docs/en/db2/12.1.x?topic=monitoring-data-generated-by-change-history-event-monitors

    but if you found that four fields will give you "more unique" selection :-)  than a/m three fields giving "only unique" :-)
    - better for you. Where you found those 4 fields mentioned?



    ------------------------------
    Jan Nelken
    Db2 (LUW) DBA
    Open for work
    Katowice or Krakow
    +48 783 109 863
    ------------------------------



  • 7.  RE: How to audit the ALTER TABLE statements?

    Posted 01/07/26 08:32 AM

    Hi Jan,

    I was looking at the v11.5 docs, here's the link:

      https://www.ibm.com/docs/en/db2/11.5.x?topic=reference-e#r0060168

    However, I can see the same description in the v12.1 docs:

      https://www.ibm.com/docs/en/db2/12.1.x?topic=reference-e#r0060168

    But the page you sent indeed shows only three fields (omitting the EVENT_TYPE), so this is probably just a case of partially outdated documentation...

    In any case, I am sure I will get uniqueness by only using the first two fields (as not using DPF anywhere): EVENT_TIMESTAMP and EVENT_ID.

    Kind regards, Damir



    ------------------------------
    Damir Wilder
    Senior Consultant
    Triton Consulting
    London
    ------------------------------



  • 8.  RE: How to audit the ALTER TABLE statements?

    Posted 01/07/26 10:18 AM

    I am also assuming that you are not using PureScale - as in such case member-id may be needed...

    As for docs - I am always using online documentation as this is updated "almost" on time as opposed to static links to PDF documents.
     



    ------------------------------
    Jan Nelken
    Db2 (LUW) DBA
    Open for work
    Katowice or Krakow
    +48 783 109 863
    ------------------------------



  • 9.  RE: How to audit the ALTER TABLE statements?

    Posted 01/08/26 03:39 AM

    That's correct, I forgot to mention pureScale, but that too is not used where I'm running the monitoring, so don't need to worry about the member info!

    I agree on the issue of online vs. PDF documentation, the former is my preference as well (even though, in this case we've found an inconsistency in the online version!).



    ------------------------------
    Damir Wilder
    Senior Consultant
    Triton Consulting
    London
    ------------------------------



  • 10.  RE: How to audit the ALTER TABLE statements?

    Posted 01/06/26 08:48 AM

    The table CHANGESUMMARY_evmon-name contains the info - appl_id  (user ID)  and client_hostname.







  • 11.  RE: How to audit the ALTER TABLE statements?

    Posted 01/07/26 11:19 AM

    I use the statement event monitor to capture this (and all data manipulations).



    ------------------------------
    [Lars] [Barlow-Hansen]
    [DBA]
    [Memorial Sloan Kettering Cancer Center]
    ------------------------------



  • 12.  RE: How to audit the ALTER TABLE statements?

    Posted 01/08/26 03:47 AM

    Hi Lars,

    Can you please elaborate on this - what exactly do you mean by "statement event monitor"?
    I'm curious because I can't find it in the (online) docs, so just wondering if you are using the "event monitor for activities" or the Audit category "execute" or something else that I missed?
    (both of these would be very unsuitable in my case, as I need to monitor a very lively transactional database with quite a lot of activities, which would put too much strain on the whole server)



    ------------------------------
    Damir Wilder
    Senior Consultant
    Triton Consulting
    London
    ------------------------------



  • 13.  RE: How to audit the ALTER TABLE statements?

    Posted 01/08/26 11:08 AM

    Hi Damir,

    Let's just say that the documentation in this area is not IBM's finest...
    Here's the actual syntax:

    create event monitor stmts for statements
    write to table
    stmt(table tabschema.tabname1 in tbspace
            trunc includes(agent_id, start_time, stop_time, stmt_operation, stmt_type, stmt_text, sqlcode)),
    connheader(table tabschema.tabname2 in tbspace
            includes(agent_id, auth_id, appl_id, conn_time));

    You join the stmt and connheader tables on agent_id.
    I was never able to find the rules for when an agent_id can be reused, so to play it safe, I merge and save the data hourly.
    The first part of appl_id contains the IP address followed by port and timestamp (unless it's a local connection).



    ------------------------------
    [Lars] [Barlow-Hansen]
    [DBA]
    [Memorial Sloan Kettering Cancer Center]
    ------------------------------



  • 14.  RE: How to audit the ALTER TABLE statements?

    Posted 01/09/26 03:21 AM

    Hi Lars,

    Understood, thanks on the into!

    All the info is actually right there, in the DB2 docs, only I haven't seen it at once as the Statement Ev.mon. is - so to speak - hidden within the "general" CREATE EVENT MONITOR statement, amongst about 8 different type-of-event categories (database, tables, deadlocks, ...).

    And I wrongly thought this whole Ev.Mon. definition was deprecated (whereas only the Deadlock category is), so didn't give it too much attention :-)

    In any case, the Statement Ev.Mon. does not work very well for my requirements, as the database is far too busy (too many SQL stmts executed per second) to keep this Ev.Mon. active for extended periods of time, so I will stick with the Ev.Mon. for Change History, as discussed previously.



    ------------------------------
    Damir Wilder
    Senior Consultant
    Triton Consulting
    London
    ------------------------------



  • 15.  RE: How to audit the ALTER TABLE statements?

    Posted 01/09/26 09:19 AM
    like Barlow-Hansen, Lars reacted to your message: