Derby
  1. Derby
  2. DERBY-4984

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

    Details

    • Urgency:
      Normal
    • Issue & fix info:
      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

        Issue Links

          Activity

          Hide
          Knut Anders Hatlen added a comment -

          [bulk update] Close all resolved issues that haven't been updated for more than one year.

          Show
          Knut Anders Hatlen added a comment - [bulk update] Close all resolved issues that haven't been updated for more than one year.
          Hide
          Mamta A. Satoor added a comment -

          If we do not have a customer request for backporting DERBY-4984, then I would rather us not backport this jira. The primary reason is we did work on several trigger related jiras around the same time and some of those jiras depended on changes to system tables. Those different jiras worked on quite a few common classes and around the same code and hence I am worried that we might backport something which depends on codes which should not be backported.

          Show
          Mamta A. Satoor added a comment - If we do not have a customer request for backporting DERBY-4984 , then I would rather us not backport this jira. The primary reason is we did work on several trigger related jiras around the same time and some of those jiras depended on changes to system tables. Those different jiras worked on quite a few common classes and around the same code and hence I am worried that we might backport something which depends on codes which should not be backported.
          Hide
          Kathey Marsden added a comment -

          Reopen for 10.5 backport consideration. If working on the backport for this issue. Temporarily assign yourself and add a comment that you are working on it. When finished, reresolve with the new fix versions or label backport_reject_10_x as appropriate.

          Show
          Kathey Marsden added a comment - Reopen for 10.5 backport consideration. If working on the backport for this issue. Temporarily assign yourself and add a comment that you are working on it. When finished, reresolve with the new fix versions or label backport_reject_10_x as appropriate.
          Hide
          Mamta A. Satoor added a comment -

          The trigger with the wrong column number in it's generated trigger action will cause the trigger to error out when it gets fired next time around after ALTER TABLE DROP COLUMN. I have committed changes today for this jira which will fix the generated trigger action to use the correct column positions for columns used through the REFERENCING clause.

          Show
          Mamta A. Satoor added a comment - The trigger with the wrong column number in it's generated trigger action will cause the trigger to error out when it gets fired next time around after ALTER TABLE DROP COLUMN. I have committed changes today for this jira which will fix the generated trigger action to use the correct column positions for columns used through the REFERENCING clause.
          Hide
          Mamta A. Satoor added a comment -

          The changes committed for this jira can be backported all the way to 10.3

          Show
          Mamta A. Satoor added a comment - The changes committed for this jira can be backported all the way to 10.3
          Hide
          Dag H. Wanvik added a comment -

          What happens presently? Does the trigger with the wrong code cause a crash down the line, or ?

          Show
          Dag H. Wanvik added a comment - What happens presently? Does the trigger with the wrong code cause a crash down the line, or ?

            People

            • Assignee:
              Mamta A. Satoor
              Reporter:
              Mamta A. Satoor
            • Votes:
              0 Vote for this issue
              Watchers:
              1 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved:

                Development