diff --git a/mysql-test/r/delete_returning.result b/mysql-test/r/delete_returning.result
index fd66c3bb75d..4d8044c1a7a 100644
--- a/mysql-test/r/delete_returning.result
+++ b/mysql-test/r/delete_returning.result
@@ -1,3 +1,6 @@
+drop table if exists t1,t2;
+drop view if exists v1;
+drop procedure if exists p1;
CREATE TABLE t1 (a int, b varchar(32));
INSERT INTO t1 VALUES
(7,'ggggggg'), (1,'a'), (3,'ccc'),
@@ -243,3 +246,22 @@ INSERT INTO t2 VALUES (1),(2);
DELETE FROM t1 ORDER BY i1 RETURNING ( SELECT i2 FROM t2 );
ERROR 21000: Subquery returns more than 1 row
DROP TABLE t1,t2;
+#
+# MDEV-4919: Packets out of order on a SELECT after calling a procedure with DELETE .. RETURNING
+#
+CREATE TABLE t1 (i INT);
+INSERT INTO t1 VALUES (1),(2);
+CREATE PROCEDURE p1 (a INT)
+BEGIN
+DELETE FROM t1 WHERE i = a RETURNING *;
+INSERT INTO t1 VALUES (a);
+END |
+CALL p1(1);
+i
+1
+SELECT * FROM t1;
+i
+1
+2
+DROP PROCEDURE p1;
+DROP TABLE t1;
diff --git a/mysql-test/t/delete_returning.test b/mysql-test/t/delete_returning.test
index 3f40428e0ab..6ca88ee921f 100644
--- a/mysql-test/t/delete_returning.test
+++ b/mysql-test/t/delete_returning.test
@@ -1,6 +1,11 @@
#
# Tests for DELETE FROM
... RETURNING ,...
#
+--disable_warnings
+drop table if exists t1,t2;
+drop view if exists v1;
+drop procedure if exists p1;
+--enable_warnings
CREATE TABLE t1 (a int, b varchar(32));
INSERT INTO t1 VALUES
@@ -199,4 +204,23 @@ INSERT INTO t2 VALUES (1),(2);
--error ER_SUBQUERY_NO_1_ROW
DELETE FROM t1 ORDER BY i1 RETURNING ( SELECT i2 FROM t2 );
-DROP TABLE t1,t2;
\ No newline at end of file
+DROP TABLE t1,t2;
+
+--echo #
+--echo # MDEV-4919: Packets out of order on a SELECT after calling a procedure with DELETE .. RETURNING
+--echo #
+CREATE TABLE t1 (i INT);
+INSERT INTO t1 VALUES (1),(2);
+--delimiter |
+CREATE PROCEDURE p1 (a INT)
+BEGIN
+ DELETE FROM t1 WHERE i = a RETURNING *;
+ INSERT INTO t1 VALUES (a);
+END |
+--delimiter ;
+
+CALL p1(1);
+SELECT * FROM t1;
+DROP PROCEDURE p1;
+DROP TABLE t1;
+
diff --git a/sql/sp_head.cc b/sql/sp_head.cc
index 7e7ea4364e4..b3b1abe891d 100644
--- a/sql/sp_head.cc
+++ b/sql/sp_head.cc
@@ -312,6 +312,17 @@ sp_get_flags_for_command(LEX *lex)
case SQLCOM_UNINSTALL_PLUGIN:
flags= sp_head::HAS_COMMIT_OR_ROLLBACK;
break;
+ case SQLCOM_DELETE:
+ {
+ if (lex->select_lex.item_list.is_empty())
+ flags= 0;
+ else
+ {
+ /* This is DELETE ... RETURNING ... */
+ flags= sp_head::MULTI_RESULTS;
+ }
+ break;
+ }
default:
flags= 0;
break;