Uploaded image for project: 'Derby'
  1. Derby
  2. DERBY-4984

ALTER TABLE DROP COLUMN may leave triggers invalid even if they are not using the column getting dropped.

    XMLWordPrintableJSON

Details

    • Normal
    • Repro attached

    Description

      While doing testing for DERBY-4887, I found a case where ALTER TABLE DROP COLUMN will leave triggers in invalid state even if those triggers are not using the column getting dropped. eg
      CREATE TABLE tab (
      element_id INTEGER NOT NULL,
      altered_id VARCHAR(30) NOT NULL
      );
      insert into tab values(1,'aa');
      – Create a trigger against the table
      CREATE TRIGGER mytrig
      AFTER UPDATE OF altered_id ON tab
      REFERENCING NEW AS newt OLD AS oldt
      FOR EACH ROW MODE DB2SQL
      SELECT newt.altered_id from tab;
      --Drop the first column in the table. This will cause the column positions to be recalculated within the table
      alter table tab drop column element_id;
      --mytrig is still looking for column altered_id at position 2 but drop column has changed it's position within the table to 1
      update tab set altered_id='bb';

      As shown in the example above, table "TAB" only has 2 columns. The trigger "MYTRIG" uses the 2nd column in it's trigger action through the REFERENCING clause. During trigger action sql parsing, every column referenced through REFERENCING clause gets transformed into a reference to the column through it's column position in the trigger table(this change to look for columns based on their column positions rather than the name went in as revision 397959 with following commit comments DERBY-1258 Change the generated code for a new/old column in a row trigger to access columns by position and not name to avoid the case-insensitive name lookup specified by JDBC.) When in the script above, we drop the column in position 1, the trigger "MYTRIG" ends up becoming invalid because column being used in the trigger action is no more in column position 2.

      One possible solution is to regenerate the SPSDescriptor associated with the trigger action for all the triggers defined on the table whose column is getting dropped. We could be little smarter and only regenerate the SPSDescriptor for the triggers who use the REFERENCING clause. But we need to do more testing to make sure that triggers without REFERENCING clause do not get impacted by a drop of column which is not the last column of the table. This optimization of recognizing the right triggers may not be worth it since performance may not be that big a criteria for an ALTER TABLE DROP COLUMN which should be a rare operation in a production system.

      An interim solution to this problem is obviously to drop and recreate the triggers

      Attachments

        Issue Links

          Activity

            People

              mamtas Mamta A. Satoor
              mamtas Mamta A. Satoor
              Votes:
              0 Vote for this issue
              Watchers:
              1 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: