Bug #32868 Stored routines do not detect changes in meta-data.
Submitted: 30 Nov 2007 10:40 Modified: 30 Nov 2007 10:51
Reporter: Alexander Nozdrin
Status: Verified
Category:Server: SP Severity:S2 (Serious)
Version:5.1, 5.0 BK, 5.1.24-BK OS:Any
Assigned to: Konstantin Osipov Target Version:
Triage: Triaged: D2 (Serious) / R4 (High) / E3 (Medium)

[30 Nov 2007 10:40] Alexander Nozdrin
Description:
The SP-cache does not detect when meta-data of the underlying objects
is changed. So, if a stored routine uses a table and the table was changed
since the stored routine was loaded in the cache, the stored routine
will operate with the outdated meta-data.

In particular:
  - if a stored procedure uses a table, and a new column is added to
    the table after the stored procedure was firstly executed,
    further executions of the store procedure will not detect the new
    column.

  - if a stored procedure uses a table, and a column is dropped after
    the stored procedure was firstly exectured, the stored procedure
    will return an error on further executions.

How to repeat:
1. A new column is added after first execution:

> CREATE TABLE t1(a INT, b INT);
> INSERT INTO t1 VALUES (1, 2), (3, 4);

> CREATE PROCEDURE p1 SELECT * FROM t1;

> CALL p1(); -- at this point the procedure is compiled
             -- and put into the cache.
+------+------+
| a    | b    |
+------+------+
|    1 |    2 |
|    3 |    4 |
+------+------+
2 rows in set (0.03 sec)

> ALTER TABLE t1 ADD COLUMN c INT AFTER b;

> SELECT * FROM t1;
+------+------+------+
| a    | b    | c    |
+------+------+------+
|    1 |    2 | NULL |
|    3 |    4 | NULL |
+------+------+------+
2 rows in set (0.01 sec)

> CALL p1(); -- the procedure from the cache is used;
             -- no new column in the result.
+------+------+
| a    | b    |
+------+------+
|    1 |    2 |
|    3 |    4 |
+------+------+
2 rows in set (0.00 sec)

If the user triggers the SP-cache invalidation somehow at this point,
and repeat then "CALL p1()", the procedure will return the correct
recordset (of three columns).

2. An existing column is dropped after first execution:

> CREATE TABLE t1(a INT, b INT);
> INSERT INTO t1 VALUES (1, 2), (3, 4);

> CREATE PROCEDURE p1() SELECT * FROM t1;

> CALL p1();
+------+------+
| a    | b    |
+------+------+
|    1 |    2 |
|    3 |    4 |
+------+------+
2 rows in set (0.03 sec)

> ALTER TABLE t1 DROP COLUMN b;

> CALL p1();
ERROR 1054 (42S22): Unknown column 'db1.t1.b' in 'field list'
[30 Nov 2007 10:51] Sveta Smirnova
Thank you for the report.

Verified as described.
[5 Dec 2007 15:31] Alexander Nozdrin
See also: Bug#33000 (Triggers do not detect changes in meta-data.)
[25 Feb 2008 19:37] Valeriy Kravchuk
Bug is still repeatable with latest 5.1.24-BK.
[17 May 2008 16:02] Sveta Smirnova
Bug #36774 was marked as duplicate of this one.
[21 Oct 2008 7:39] Sveta Smirnova
Bug #40191 was marked as duplicate of this one.
[23 Mar 2009 7:54] Sveta Smirnova
Bug #43792 was marked as duplicate of this one.
[27 Mar 2009 19:29] Konstantin Osipov
Risk and effort are for 6.0.
In 5.1 both are higher (the fix depends on WL#3726 and WL#4284).