Uploaded image for project: 'MariaDB Server'
  1. MariaDB Server
  2. MDEV-34309

CHECK TABLE should check that foreign key relations are correct

Details

    • New Feature
    • Status: In Testing (View Workflow)
    • Critical
    • Resolution: Unresolved
    • 12.0
    • None

    Description

      Currently there is no easy way for a user to check if a database is consistent.
      One can check individual tables with CHECK TABLE but there is now to check that
      all foreign key tables has all the required keys and that the original table has all the rows
      expected by the foreign key table.

      We should extended CHECK TABLE .. EXTENDED or alternative CHECK TABLE ... REFERENCES to check that all foreign key relations are consistent.

      This should preferably be done on the upper, sql, level and not in InnoDB.

      Attachments

        Issue Links

          Activity

            monty Michael Widenius created issue -
            monty Michael Widenius made changes -
            Field Original Value New Value
            Summary CHECK TABLE should also that foreign key tables are correct CHECK TABLE should check that foreign key relations are correct
            julien.fritsch Julien Fritsch made changes -
            Issue Type Task [ 3 ] New Feature [ 2 ]
            julien.fritsch Julien Fritsch made changes -
            Assignee Michael Widenius [ monty ]
            monty Michael Widenius made changes -
            Assignee Michael Widenius [ monty ] Nikita Malyavin [ nikitamalyavin ]
            monty Michael Widenius made changes -
            Fix Version/s 11.7 [ 29815 ]

            I think that the same code should be useful for implementing the tricky part of MDEV-16356: validating the to-be-created constraints and then just adding the constraint without rebuilding the child table, like we currently do (when foreign_key_checks are enabled).

            marko Marko Mäkelä added a comment - I think that the same code should be useful for implementing the tricky part of MDEV-16356 : validating the to-be-created constraints and then just adding the constraint without rebuilding the child table, like we currently do (when foreign_key_checks are enabled).
            marko Marko Mäkelä made changes -
            mariadb-jira-automation Jira Automation (IT) made changes -
            Zendesk Related Tickets 201536
            Zendesk active tickets 201536
            nikitamalyavin Nikita Malyavin made changes -
            Status Open [ 1 ] In Progress [ 3 ]
            nikitamalyavin Nikita Malyavin made changes -
            Description Currently there is no easy way for a user to check if a database is consistent.
            One can check individual tables with CHECK TABLE but there is now to check that
            all foreign key tables has all the required keys and that the original table has all the rows
            expected by the foreign key table.

            We should extended CHECK TABLE .. EXTENDED or alternative CHECK TABLE ... CONSTRAINTS to check that all foreign key relations are consistent.

            This should preferably be done on the upper, sql, level and not in InnoDB.
            Currently there is no easy way for a user to check if a database is consistent.
            One can check individual tables with CHECK TABLE but there is now to check that
            all foreign key tables has all the required keys and that the original table has all the rows
            expected by the foreign key table.

            We should extended CHECK TABLE .. EXTENDED or alternative CHECK TABLE ... REFERENCES to check that all foreign key relations are consistent.

            This should preferably be done on the upper, sql, level and not in InnoDB.
            nikitamalyavin Nikita Malyavin made changes -
            Status In Progress [ 3 ] Stalled [ 10000 ]
            serg Sergei Golubchik made changes -
            Fix Version/s 11.8 [ 29921 ]
            Fix Version/s 11.7 [ 29815 ]

            I think both should be the default. However we should add options for CHECK TABLE to allow user to specify
            NO FOREIGN KEY CHECKS, NO CHILD CHECK, NO PARENT CHECK.

            I think doing this with a select will be 100x slower compared to do it with explicit code.
            A merge on ordered key is something the optimizer is not good at doing.
            It is also much easier to give a complete list of errors with explicit code.

            With explicit code we can also easier implement a FAST check, where we do a checksum for all unique reference keys in the child and check that the checksum for the parent over all keys are the same.

            monty Michael Widenius added a comment - I think both should be the default. However we should add options for CHECK TABLE to allow user to specify NO FOREIGN KEY CHECKS, NO CHILD CHECK, NO PARENT CHECK. I think doing this with a select will be 100x slower compared to do it with explicit code. A merge on ordered key is something the optimizer is not good at doing. It is also much easier to give a complete list of errors with explicit code. With explicit code we can also easier implement a FAST check, where we do a checksum for all unique reference keys in the child and check that the checksum for the parent over all keys are the same.
            julien.fritsch Julien Fritsch made changes -
            Priority Major [ 3 ] Critical [ 2 ]
            • how "foreign keys not being up to date" can lead to crashes? this looks like a bug
            • NO FOREIGN KEY CHECKS, NO CHILD CHECK, NO PARENT CHECK, etc looks too complex, is there anyone actually requesting it? If not, then

              SET STATEMENT foreign_key_checks=0 CHECK TABLE

              can be used to disable foreign key checks. This can be later extended to

              SET STATEMENT check_constraint_checks=0 CHECK TABLE

              etc

            serg Sergei Golubchik added a comment - how "foreign keys not being up to date" can lead to crashes ? this looks like a bug NO FOREIGN KEY CHECKS, NO CHILD CHECK, NO PARENT CHECK, etc looks too complex, is there anyone actually requesting it? If not, then SET STATEMENT foreign_key_checks=0 CHECK TABLE can be used to disable foreign key checks. This can be later extended to SET STATEMENT check_constraint_checks=0 CHECK TABLE etc
            nikitamalyavin Nikita Malyavin made changes -
            Status Stalled [ 10000 ] In Progress [ 3 ]

            monty,

            With explicit code we can also easier implement a FAST check, where we do a checksum for all unique reference keys in the child and check that the checksum for the parent over all keys are the same.

            It seems to me that we can ensure the integrity with a checksum only for one-to-one relationships (bijections). Normal foreign key relation doesn't have this property.

            nikitamalyavin Nikita Malyavin added a comment - monty , With explicit code we can also easier implement a FAST check, where we do a checksum for all unique reference keys in the child and check that the checksum for the parent over all keys are the same. It seems to me that we can ensure the integrity with a checksum only for one-to-one relationships (bijections). Normal foreign key relation doesn't have this property.
            nikitamalyavin Nikita Malyavin made changes -
            Status In Progress [ 3 ] Stalled [ 10000 ]
            nikitamalyavin Nikita Malyavin made changes -
            Status Stalled [ 10000 ] In Progress [ 3 ]
            nikitamalyavin Nikita Malyavin made changes -
            Status In Progress [ 3 ] Stalled [ 10000 ]
            nikitamalyavin Nikita Malyavin made changes -
            Status Stalled [ 10000 ] In Progress [ 3 ]
            nikitamalyavin Nikita Malyavin added a comment - - edited

            The implementation is merely straightforward: both for parent and child relations, check that for each record in child table, there is a record in parent table, that matches a given foreign key.

            I had to modify the prelocking to lock both parents and children.
            Also I made some refactoring around FOREIGN_KEY_INFO: now its fields are correct Lex_cstring-derived types, and are stored by value, not by pointer.

            sanja, please review the following commits on branch bb-11.7-check-table:

            2dea7185 MDEV-34309 [2/2] CHECK TABLE: implement referential integrity check
            ac27db6f MDEV-34309 [1/2] prelock referentially related tables for CHECK TABLE
            ee5cf521 FOREIGN_KEY_INFO: Store Lex_* strings without extra pointer indirection.
            6c1a01d9 Fix deallocation of Sql_cmd_dml::result
            

            nikitamalyavin Nikita Malyavin added a comment - - edited The implementation is merely straightforward: both for parent and child relations, check that for each record in child table, there is a record in parent table, that matches a given foreign key. I had to modify the prelocking to lock both parents and children. Also I made some refactoring around FOREIGN_KEY_INFO: now its fields are correct Lex_cstring-derived types, and are stored by value, not by pointer. sanja , please review the following commits on branch bb-11.7-check-table : 2dea7185 MDEV-34309 [2/2] CHECK TABLE: implement referential integrity check ac27db6f MDEV-34309 [1/2] prelock referentially related tables for CHECK TABLE ee5cf521 FOREIGN_KEY_INFO: Store Lex_* strings without extra pointer indirection. 6c1a01d9 Fix deallocation of Sql_cmd_dml::result
            nikitamalyavin Nikita Malyavin made changes -
            Assignee Nikita Malyavin [ nikitamalyavin ] Oleksandr Byelkin [ sanja ]
            Status In Progress [ 3 ] In Review [ 10002 ]

            I rebased it on main to bb-11.8-check-table.

            I also see 8 commits there who review other 4 ?

            sanja Oleksandr Byelkin added a comment - I rebased it on main to bb-11.8-check-table. I also see 8 commits there who review other 4 ?

            Sergei reviews them.

            nikitamalyavin Nikita Malyavin added a comment - Sergei reviews them.

            It is OK to push after renaming commits related to this MDEV starting with: MDEV-34309, i.e something like this.:

            MDEV-34309 prereq. FOREIGN_KEY_INFO: Store Lex_* strings without extra pointer indirection.

            MDEV-34309 prereq. Fix deallocation of Sql_cmd_dml::result

            Just to link them to the task which inspeared you for such changes (I'd put the MDEV-34309 before your cleanup also)

            sanja Oleksandr Byelkin added a comment - It is OK to push after renaming commits related to this MDEV starting with: MDEV-34309 , i.e something like this.: MDEV-34309 prereq. FOREIGN_KEY_INFO: Store Lex_* strings without extra pointer indirection. MDEV-34309 prereq. Fix deallocation of Sql_cmd_dml::result Just to link them to the task which inspeared you for such changes (I'd put the MDEV-34309 before your cleanup also)
            sanja Oleksandr Byelkin made changes -
            Assignee Oleksandr Byelkin [ sanja ] Nikita Malyavin [ nikitamalyavin ]
            Status In Review [ 10002 ] Stalled [ 10000 ]
            nikitamalyavin Nikita Malyavin made changes -
            Status Stalled [ 10000 ] In Progress [ 3 ]

            cleanup is ok too

            serg Sergei Golubchik added a comment - cleanup is ok too
            serg Sergei Golubchik made changes -
            nikitamalyavin Nikita Malyavin made changes -
            Status In Progress [ 3 ] In Testing [ 10301 ]
            serg Sergei Golubchik made changes -
            Assignee Nikita Malyavin [ nikitamalyavin ] Elena Stepanova [ elenst ]
            elenst Elena Stepanova made changes -
            elenst Elena Stepanova made changes -
            elenst Elena Stepanova made changes -
            elenst Elena Stepanova made changes -
            elenst Elena Stepanova made changes -
            pandi.gurusamy Pandikrishnan Gurusamy made changes -
            elenst Elena Stepanova made changes -
            elenst Elena Stepanova made changes -
            elenst Elena Stepanova made changes -
            elenst Elena Stepanova made changes -
            elenst Elena Stepanova made changes -
            elenst Elena Stepanova made changes -
            ralf.gebhardt Ralf Gebhardt made changes -
            Labels Preview_11.8

            It's been agreed that the feature won't go to 11.8 as we try to find legacy issues when

            • foreign keys are claimed to be broken when they should be okay (example: MDEV-35962),
            • foreign keys pretend to work when they probably shouldn't (example: MDEV-35908, INET6 column referencing TIME column),
            • there is no clarity whether the foreign keys should work or not (example: MDEV-35936 – different field lengths, more in the comments to MDEV-35908) – this may be just a documentation issue, but it would be really good to get it documented while we are at it.

            The goal is to avoid the hard choice between releasing CHECK which alerts about fake problems, or CHECK which doesn't show real problems, or CHECK which is inconsistent with DML/DDL related to the same foreign keys.

            Thus, while the bugs will probably be fixed outside the scope of this task, as the fixes would go to earlier versions when possible, the testing scope of the task will increase.

            elenst Elena Stepanova added a comment - It's been agreed that the feature won't go to 11.8 as we try to find legacy issues when foreign keys are claimed to be broken when they should be okay (example: MDEV-35962 ), foreign keys pretend to work when they probably shouldn't (example: MDEV-35908 , INET6 column referencing TIME column), there is no clarity whether the foreign keys should work or not (example: MDEV-35936 – different field lengths, more in the comments to MDEV-35908 ) – this may be just a documentation issue, but it would be really good to get it documented while we are at it. The goal is to avoid the hard choice between releasing CHECK which alerts about fake problems, or CHECK which doesn't show real problems, or CHECK which is inconsistent with DML/DDL related to the same foreign keys. Thus, while the bugs will probably be fixed outside the scope of this task, as the fixes would go to earlier versions when possible, the testing scope of the task will increase.
            elenst Elena Stepanova made changes -
            Fix Version/s 11.9 [ 29945 ]
            Fix Version/s 11.8 [ 29921 ]

            People

              elenst Elena Stepanova
              monty Michael Widenius
              Votes:
              0 Vote for this issue
              Watchers:
              10 Start watching this issue

              Dates

                Created:
                Updated:

                Git Integration

                  Error rendering 'com.xiplink.jira.git.jira_git_plugin:git-issue-webpanel'. Please contact your Jira administrators.