IBM i

IBM i 

A space for professionals working with IBM’s integrated OS for Power systems to exchange ideas, ask questions, and share expertise on topics like RPG and COBOL development, application modernization, open source integration, system administration, and business continuity.


#Power


#IBMi
#Power
#Operatingsystems
#Servers
 View Only

Know where a trigger fired: Trigger pseudo columns in Db2 for i

By Jayasree Thatikonda posted 13 days ago

  

On IBM i, teams commonly write shared trigger logic once and reuse it across multiple triggers using the SQL INCLUDE statement, keeping audit and compliance code in a single place. This eliminates the need to duplicate the same routine across every trigger. When a change is required, only one source member needs to be updated. But that shared code had a blind spot: there was no way to know which file, library, or member it was running for.

A physical file can contain multiple members, each accessible by using its own Structured Query Language (SQL) alias. SQL triggers fire on data changes, but the trigger body had no built-in way to determine which member caused the trigger to fire.

Trigger pseudo columns close this gap. Three new identifiers, TRIGGER_FILE_NAME, TRIGGER_FILE_LIBRARY_NAME, and TRIGGER_FILE_MEMBER_NAME, are available directly in a trigger body. Shared included logic can now identify exactly where the trigger fired, without external calls or duplicate code.

The problem statement

Inside the trigger body, there was previously no standard SQL mechanism to determine the runtime identity of the object that caused the trigger to fire, specifically its file name, the library it resides in, and the member that was affected. Developers had to resort to non-SQL workarounds, such as external program calls, system application programming interface (API) queries, or journal inspection.

Why does this matter in practice?

  • Multi-member files: IBM i supports multiple members within a single physical file. A trigger needs to know which member's data was modified to route downstream processing correctly.
  • Audit and compliance: Regulatory requirements demand that change records capture the full object identity, including the file, library, and member.
  • Conditional routing: A trigger might need to behave differently depending on which library or member is active at runtime.

The solution: Trigger pseudo columns

Introduced in July 2026 for both IBM i 7.6 TR2 and IBM i 7.5 TR8, three trigger pseudo columns can be referenced directly within an SQL trigger body. A pseudo column is an identifier that occupies the same namespace as regular columns and variables.

These pseudo columns give triggers a first-class, SQL-native way to answer the question: Which file, in which library, and from which member was this trigger invoked?

How trigger pseudo columns work

The three pseudo columns

The following table summarizes the trigger pseudo columns and the information that each pseudo column returns at runtime.

Length

Pseudo column Returns Data type Length
TRIGGER_FILE_NAME System name of the triggering file CHAR or VARCHAR, no CCSID 10
TRIGGER_FILE_LIBRARY_NAME Name of the library containing the triggering file CHAR or VARCHAR, no CCSID 10
TRIGGER_FILE_MEMBER_NAME Name of the triggering file member CHAR or VARCHAR, no CCSID 10

Real-world scenarios

Trigger pseudo columns are most valuable when trigger logic must operate across multiple files, libraries, or members while still identifying the runtime context. The following examples demonstrate how the pseudo columns simplify common auditing and compliance requirements by providing direct access to file, library, and member information.

Shared trigger logic with INCLUDE

The scenario: a bank has two tables, BANKLIB.Orders and BANKLIB.Payments. Both require identical audit logging on every INSERT. Instead of duplicating the logic in each trigger, the team writes it once in a shared SQL source member and includes it in both triggers.

The following example demonstrates how to implement shared trigger logic by using the SQL INCLUDE statement and trigger pseudo columns, from creating the required objects through invoking the triggers and verifying the audit records.

The following steps show how the shared trigger logic is implemented, invoked, and validated.

  1. Create the audit log table, SQL source file, and shared member.
    1. Create the schema and a dedicated audit log table for this scenario:
      CREATE SCHEMA BANKLIB;
      CREATE TABLE BANKLIB.TRG_AUDIT_LOG (
          LOG_ID     INTEGER        GENERATED ALWAYS AS IDENTITY NOT NULL,
          LOG_TIMESTAMP     TIMESTAMP      NOT NULL DEFAULT CURRENT TIMESTAMP,
          FILE_NAME  VARCHAR(10)    NOT NULL,
          LIBRARY_NAME   VARCHAR(10)    NOT NULL,
          MEMBER_NAME VARCHAR(10)    NOT NULL,
          ACTION     VARCHAR(10)    NOT NULL,
          CHANGED_BY    VARCHAR(10)   NOT NULL,
          CONSTRAINT BANKLIB.TRG_AUDIT_LOG_PK PRIMARY KEY (LOG_ID));
    2. Create a source member named COMAUDIT that contains the following SQL statements:
      DECLARE V_FILE    VARCHAR(10);
      DECLARE V_LIB     VARCHAR(10);
      DECLARE V_MEMBER  VARCHAR(10);
      DECLARE V_ACTION  VARCHAR(10) DEFAULT 'INSERT';
      SET V_FILE   = TRIGGER_FILE_NAME;
      SET V_LIB    = TRIGGER_FILE_LIBRARY_NAME;
      SET V_MEMBER = TRIGGER_FILE_MEMBER_NAME;
      INSERT INTO BANKLIB.TRG_AUDIT_LOG
       (FILE_NAME, LIBRARY_NAME, MEMBER_NAME, ACTION, CHANGED_BY)
        VALUES  (V_FILE, V_LIB, V_MEMBER, V_ACTION, USER);
  2. Create the supporting tables.
    CREATE TABLE BANKLIB.ORDERS (
        ORDER_ID  INTEGER      GENERATED ALWAYS AS IDENTITY NOT NULL,
        ORDER_AMT DECIMAL(13,2)  NOT NULL,
        CONSTRAINT BANKLIB.ORDERS_PK PRIMARY KEY (ORDER_ID));
    
    CREATE TABLE BANKLIB.PAYMENTS (
        PAYMENT_ID  INTEGER      GENERATED ALWAYS AS IDENTITY NOT NULL,
        PAYMENT_AMT DECIMAL(13,2) NOT NULL,
        CONSTRAINT BANKLIB.PAYMENTS_PK PRIMARY KEY (PAYMENT_ID));
  3. Create two triggers that include the shared logic.
    -- Trigger on ORDERS
    CREATE OR REPLACE TRIGGER BANKLIB.TRG_ORDERS
        AFTER INSERT ON BANKLIB.ORDERS
        FOR EACH ROW MODE DB2ROW
    BEGIN
        INCLUDE SQL BANKLIB/QSQLSRC(COMAUDIT);
    END;
    -- Trigger on PAYMENTS — identical structure, same INCLUDE
    CREATE OR REPLACE TRIGGER BANKLIB.TRG_PAYMENTS
        AFTER INSERT ON BANKLIB.PAYMENTS
        FOR EACH ROW MODE DB2ROW
    BEGIN
         INCLUDE SQL BANKLIB/QSQLSRC(COMAUDIT);
    END;

    Both triggers share identical INCLUDE logic. At runtime, TRIGGER_FILE_NAME automatically returns ORDERS for the first trigger and PAYMENTS for the second trigger.

  4. Insert the test data.
    INSERT INTO BANKLIB.ORDERS   (ORDER_AMT)   VALUES (1500.00);
    INSERT INTO BANKLIB.PAYMENTS (PAYMENT_AMT) VALUES (750.00);
  5. Query the audit log.
    SELECT FILE_NAME,
           LIBRARY_NAME,
           MEMBER_NAME,
           ACTION,
           LOG_TIMESTAMP
      FROM BANKLIB.TRG_AUDIT_LOG
     ORDER BY LOG_TIMESTAMP;

Expected output:

image

Figure 1. Audit log entries

Both triggers fired. Both recorded their own FILE_NAME and MEMBER_NAME automatically from a single shared source member, with no duplication, no branching, and no external program calls.

Audit log using multi-member files

A regional bank processes customer transactions, including deposits, withdrawals, and transfers, across three city branches: New York (NYC), Chicago (CHI), and Los Angeles (LAX). All transactions flow into a single physical file BANKLIB.TRANSDATA, which is partitioned into one member per branch. Each branch inserts its rows through a dedicated SQL alias that points to its member.

The compliance requirement is that every INSERT and UPDATE operation must be audited with full object identity, including the file name, the library name, and, most critically, the branch partition from which the transaction originated. A single trigger, BANKLIB.TRG_TRANSACTION_AUDIT, handles all three branches. It reads TRIGGER_FILE_MEMBER_NAME at runtime, and the pseudo column automatically returns the branch member name (BRANCH_NYC, BRANCH_CHI, or BRANCH_LAX), with no branching logic, no CASE expression, and no external program calls required.

The following example demonstrates how a single trigger can audit transactions across multiple members of a physical file by using trigger pseudo columns to identify the originating branch at runtime.

The following steps walk through the implementation and validation process.

  1. Create the schema, physical file, branch members, and audit log table.
    CREATE SCHEMA BANKLIB;

    Create and define the record format in data description specifications (DDS):

    CALL QSYS2.QCMDEXC('CRTSRCPF FILE(BANKLIB/QDDSSRC) RCDLEN(112)');
    CALL QSYS2.QCMDEXC('QSYS/ADDPFM FILE(BANKLIB/QDDSSRC) MBR(TRANSACT) SRCTYPE(PF) TEXT(''TRANSDATA DDS'')');

    --Write below to BANKLIB/QDDSSRC(TRANSACT):

    A          R TRANSREC
    A             ACCOUNTNO    12A
    A             BRANCHCODE   10A
    A             TXNDATE      10A
    A             TXNTYPE      10A
    A             AMOUNT       13P 2
    A             TXNSTATUS    10A
    A          K ACCOUNTNO
    A          K TXNDATE
    A          K TXNTYPE

    Then, create a physical file from DDS:

    CALL QSYS2.QCMDEXC('CRTPF FILE(BANKLIB/TRANSDATA) SRCFILE(BANKLIB/QDDSSRC) SRCMBR(TRANSACT) MAXMBRS(*NOMAX) MBR(*NONE)');

    Each member holds the transaction rows for a single branch. Adding a new branch in the future requires only one additional ADDPFM command and one new alias, without the need for schema changes or trigger changes.

    Add one member per branch:

    CALL QSYS2.QCMDEXC('QSYS/ADDPFM FILE(BANKLIB/TRANSDATA) MBR(BRANCH_NYC) TEXT(''TRANSDATA - NYC'')');
    CALL QSYS2.QCMDEXC('QSYS/ADDPFM FILE(BANKLIB/TRANSDATA) MBR(BRANCH_CHI) TEXT(''TRANSDATA - CHI'')');
    CALL QSYS2.QCMDEXC('QSYS/ADDPFM FILE(BANKLIB/TRANSDATA) MBR(BRANCH_LAX) TEXT(''TRANSDATA - LAX'')');

    The audit log table is a standard SQL table. It stores one row per trigger execution and captures the values returned by the TRIGGER_FILE_NAME, TRIGGER_FILE_LIBRARY_NAME, and TRIGGER_FILE_MEMBER_NAME pseudo columns, alongside the transaction details, the action performed (INSERT or UPDATE), the before and after status values, and the database user who made the change.

    CREATE TABLE BANKLIB.TRANSACTION_AUDIT_LOG (
        LOG_ID       INTEGER        NOT NULL 
                     GENERATED ALWAYS AS IDENTITY,
        LOG_TIMESTAMP       TIMESTAMP      NOT NULL 
                     DEFAULT CURRENT TIMESTAMP,
        FILE_NAME    VARCHAR(10)    NOT NULL ,
        LIBRARY_NAME VARCHAR(10)    NOT NULL ,
        MEMBER_NAME  VARCHAR(10)    NOT NULL ,
        ACCOUNT_NUMBER   VARCHAR(12)       NOT NULL ,
        BRANCH_CODE  CHAR(10)       NOT NULL ,
        TRANSACTION_DATE     DATE           NOT NULL ,
        TRANSACTION_TYPE     CHAR(10)       NOT NULL ,
        AMOUNT       DECIMAL(13,2)  NOT NULL ,
        ACTION       VARCHAR(10)    NOT NULL ,
        OLD_STATUS   CHAR(10)                ,
        NEW_STATUS   CHAR(10)       NOT NULL ,
        CHANGED_BY   VARCHAR(10)   NOT NULL ,
        CONSTRAINT BANKLIB.TRANSACTION_AUDIT_LOG_PK PRIMARY KEY (LOG_ID));
  2. Create SQL aliases (one per branch member).
    CREATE OR REPLACE ALIAS BANKLIB.TXN_NYC FOR BANKLIB.TRANSDATA(BRANCH_NYC);
    CREATE OR REPLACE ALIAS BANKLIB.TXN_CHI FOR BANKLIB.TRANSDATA(BRANCH_CHI);
    CREATE OR REPLACE ALIAS BANKLIB.TXN_LAX FOR BANKLIB.TRANSDATA(BRANCH_LAX);
  3. Create the trigger.
    CREATE OR REPLACE TRIGGER BANKLIB.TRG_TRANSACTION_AUDIT
        AFTER INSERT OR UPDATE ON BANKLIB.TRANSDATA
        REFERENCING NEW AS N OLD AS O
        FOR EACH ROW
        MODE DB2ROW
    BEGIN
        -- Declare VARCHAR(10) — trailing spaces trimmed automatically on assignment
        DECLARE V_FILE    VARCHAR(10);
        DECLARE V_LIB     VARCHAR(10);
        DECLARE V_MEMBER  VARCHAR(10);
        DECLARE V_ACTION  VARCHAR(10);
        -- All three pseudo columns return meaningful values automatically:
        -- V_FILE   = 'TRANSDATA' 
        -- V_LIB    = 'BANKLIB'
        -- V_MEMBER = 'BRANCH_NYC' or 'BRANCH_CHI' or 'BRANCH_LAX'
        SET V_FILE   =  TRIGGER_FILE_NAME;
        SET V_LIB    = TRIGGER_FILE_LIBRARY_NAME;
        SET V_MEMBER = TRIGGER_FILE_MEMBER_NAME;
        SET V_ACTION = CASE WHEN INSERTING THEN 'INSERT'
                            WHEN UPDATING  THEN 'UPDATE'
                            ELSE                'UNKNOWN'
                       END;
        INSERT INTO BANKLIB.TRANSACTION_AUDIT_LOG
            (LOG_TIMESTAMP, FILE_NAME, LIBRARY_NAME, MEMBER_NAME,
             ACCOUNT_NUMBER, BRANCH_CODE, TRANSACTION_DATE, TRANSACTION_TYPE, AMOUNT,
             ACTION, OLD_STATUS, NEW_STATUS, CHANGED_BY)
        VALUES
            (CURRENT TIMESTAMP, V_FILE, V_LIB, V_MEMBER,
             N.ACCOUNTNO, N.BRANCHCODE, N.TXNDATE,
             N.TXNTYPE, N.AMOUNT,
             V_ACTION,
             O.TXNSTATUS,            --OLD_STATUS is NULL on INSERT; populated on UPDATE
             N.TXNSTATUS,
             USER); 
    END;
  4. Insert the test data through branch aliases.
    INSERT INTO BANKLIB.TXN_NYC
        (ACCOUNTNO, BRANCHCODE, TXNDATE, TXNTYPE, AMOUNT, TXNSTATUS)
        VALUES ('ACC0010001', 'NYC', '2026-08-01', 'DEPOSIT',   5000.00, 'PENDING');
    INSERT INTO BANKLIB.TXN_NYC
        (ACCOUNTNO, BRANCHCODE, TXNDATE, TXNTYPE, AMOUNT, TXNSTATUS)
        VALUES ('ACC0010002', 'NYC', '2026-08-02', 'WITHDRAW',  1200.00, 'PENDING');
    INSERT INTO BANKLIB.TXN_CHI
        (ACCOUNTNO, BRANCHCODE, TXNDATE, TXNTYPE, AMOUNT, TXNSTATUS)
        VALUES ('ACC0020001', 'CHI', '2026-08-01', 'TRANSFER',  3500.00, 'PENDING');
    INSERT INTO BANKLIB.TXN_LAX
        (ACCOUNTNO, BRANCHCODE, TXNDATE, TXNTYPE, AMOUNT, TXNSTATUS)
        VALUES ('ACC0030001', 'LAX', '2026-08-01', 'DEPOSIT',  25000.00, 'PENDING');
    UPDATE BANKLIB.TXN_NYC
        SET   TXNSTATUS = 'APPROVED'
        WHERE ACCOUNTNO = 'ACC0010001'
        AND   TXNDATE   = '2026-08-01';
  5. Query the audit log results.

    The following queries confirm that MEMBER_NAME is recorded correctly for each event and that the audit trail is complete.

    1. Full audit trail: Retrieves the full audit trail from TRANSACTION_AUDIT_LOG, showing file, library, and member associated with each INSERT and UPDATE event.
      SELECT
          LOG_ID,
          LOG_TIMESTAMP,
          FILE_NAME,
          LIBRARY_NAME,
          MEMBER_NAME,
          ACCOUNT_NUMBER,
          BRANCH_CODE,
          TRANSACTION_TYPE,
          AMOUNT,
          ACTION,
          OLD_STATUS,
          NEW_STATUS
      FROM BANKLIB.TRANSACTION_AUDIT_LOG
      ORDER BY LOG_TIMESTAMP;

      Expected output:

      image

      Figure 2. Full audit trail showing FILE_NAME, LIBRARY_NAME, and MEMBER_NAME

    2. Filter to one branch: Filters the audit log to show only the events triggered from the BRANCH_NYC member.
      SELECT
          LOG_ID,
          LOG_TIMESTAMP,
          MEMBER_NAME,
          ACCOUNT_NUMBER,
          TRANSACTION_TYPE,
          AMOUNT,
          ACTION,
          OLD_STATUS,
          NEW_STATUS
      FROM  BANKLIB.TRANSACTION_AUDIT_LOG
      WHERE MEMBER_NAME = 'BRANCH_NYC'
      ORDER BY LOG_TIMESTAMP;

      Expected output:

      image

      Figure 3. Audit log filtered to BRANCH_NYC, showing only the New York transactions

    3. Compliance summary: Produces a per-branch compliance summary aggregating INSERT count, UPDATE count, total event count, and total transaction amount grouped by member name.
      SELECT
          MEMBER_NAME,
          SUM(CASE WHEN ACTION = 'INSERT' THEN 1 ELSE 0 END)       AS INSERTS,
          SUM(CASE WHEN ACTION = 'UPDATE' THEN 1 ELSE 0 END)       AS UPDATES,
          COUNT(*)                                                  AS TOTAL_EVENTS,
          SUM(CASE WHEN ACTION = 'INSERT' THEN AMOUNT ELSE 0 END)   AS TOTAL_AMOUNT
      FROM  BANKLIB.TRANSACTION_AUDIT_LOG
      GROUP BY MEMBER_NAME
      ORDER BY MEMBER_NAME;

      Expected output:

      image

      Figure 4. Per-branch compliance summary

The audit records confirm that TRIGGER_FILE_MEMBER_NAME accurately identifies the originating branch for every transaction. A single trigger can therefore provide complete audit coverage across multiple file members without additional routing logic or trigger duplication.

Summary

On IBM i, physical files can span multiple members, libraries, and aliases. Trigger pseudo columns give the trigger body direct, runtime visibility into exactly which file, which library, and which member caused it to fire, natively in SQL and without external calls or API queries.

Combined with the SQL INCLUDE statement, shared audit logic written once and stored in a source member or an IFS file can be reused across multiple triggers. At runtime, the trigger logic can access the file information that caused the trigger to fire, allowing the shared logic to automatically record the correct file, library, and member information without duplication or branching logic.

2 comments
25 views

Permalink

Comments

12 days ago

Thank you for the kind words. I am glad the examples helped make things clearer!

For me, the main benefit is getting rid of external dependencies, which also helps with compliance.

Do you mostly work with multi-member files, or single-member tables spread across different libraries? 

Thanks again for starting this conversation!

12 days ago

Thank you, Jayasree, for this excellent article!

I really like this type of topic with databases. Audit, compliance, and knowing exactly where a change came from is what makes a system trustworthy.

What I liked most was understanding the 3 places where it helps:

  1. Audit and compliance - the log records by itself the file, library and member
  2. Multi-member files - one trigger knows which member fired it, without duplication
  3. Conditional routing - we can decide the behavior at runtime

And the idea of using INCLUDE with shared logic, written once and reused, makes a lot of sense.

I'm always studying Db2 for i and looking to go deeper and deeper.

A question to start a discussion: in your experience, where did you see the biggest gain with these pseudo-columns? Was it more in compliance, in removing external programs and journal inspection, or in simplifying the trigger code?

Thank you again for sharing the examples with BANKLIB. It made it very easy to understand!