[MDEV-17252] mysql_stmt_fetch error during fetch of stored procedure (with cursor) results Created: 2018-09-18  Updated: 2019-06-30

Status: Stalled
Project: MariaDB Server
Component/s: Server
Affects Version/s: 10.2
Fix Version/s: 10.2

Type: Bug Priority: Minor
Reporter: David Fehrmann Assignee: Oleksandr Byelkin
Resolution: Unresolved Votes: 0
Labels: None
Environment:

Linux/Debian, X64, mariadb-10.3.9-linux-x86_64


Attachments: File demo.c    
Issue Links:
Problem/Incident
causes CONC-424 mysql_stmt_store_result returns empty... Closed
Relates
relates to MDEV-19321 Cursor flag set incorrectly in COM_ST... Open

 Description   

Hi,

I've tried to fetch result sets from a stored procedure which using cursors in a business application. The mysql_stmt_fetch reported an error (Unknown with code 0) during the first result set or "Commands out of Sync" with an older library (5.5.0). I've searched for days now to how to get the correct result without success. All other statements are working fine (selects/inserts/deletes, calls to procedures without cursor).

The applications mysql and HeidiSql are working properly and return the expected results. So the server side seems to be working as expected.

Here my minimized test set:


Database:

CREATE DATABASE demo;
USE demo;
 
CREATE TABLE demo (id BIGINT(20) NOT NULL, PRIMARY KEY(id)) ENGINE=InnoDB;
 
DELIMITER //
CREATE PROCEDURE test1() BEGIN SELECT 1 AS ID; SELECT 2 AS ID; END//
CREATE PROCEDURE test2() BEGIN DECLARE cur1 CURSOR FOR SELECT id FROM demo; OPEN cur1; CLOSE cur1; SELECT 3 AS ID; SELECT 4 AS ID; END//


The C code is attached (mostly taken from https://dev.mysql.com/doc/refman/5.7/en/c-api-prepared-call-statements.html), compiled with:

gcc demo.c libmariadb.so -o demo


Expected result:

Client info: 10.3.6
Statement: CALL test1()
Server info: 10.3.9-MariaDB
 Fields: 1
  val[0] = 1;
 Fields: 1
  val[0] = 2;
Statement: CALL test2()
Server info: 10.3.9-MariaDB
 Fields: 1
  val[0] = 3;
 Fields: 1
  val[0] = 4;


Actual result:

Client info: 10.3.6
Statement: CALL test1()
Server info: 10.3.9-MariaDB
 Fields: 1
  val[0] = 1;
 Fields: 1
  val[0] = 2;
Statement: CALL test2()
Server info: 10.3.9-MariaDB
 Fields: 1
# Error:  (errno: 0)


I'm pretty sure I'm doing something wrong, but I couldn't figure out what to do. At least the error code is misleading and doesn't help to solve the issue.



 Comments   
Comment by Georg Richter [ 2018-09-19 ]

Hmm.. this looks like a server bug:

When executing CALL test2() server sends in first packet server status flag SERVER_STATUS_CURSOR_EXISTS which indicates that subsequent rows must be fetched from client via COM_STMT_FETCH command until server sets status flag SERVER_STATUS_LAST_ROW_SENT. However this can never work unless all results from the stored procedure are processed.

See also:

Comment by David Fehrmann [ 2018-09-20 ]

Thank you very much. Shall I test the workaround in my environments?

Comment by Georg Richter [ 2018-09-20 ]

There is already a test in ps_bugs.c which tests the workaround.

Comment by David Fehrmann [ 2018-09-20 ]

Okay, thank you.

Comment by Georg Richter [ 2018-09-20 ]

I added a workaround in Connector/C, but this needs to be fixed also in server.

For both result sets from call test2() the server sets the status "SERVER_STATUS_CURSOR_EXISTS".

Generated at Thu Feb 08 08:35:03 UTC 2024 using Jira 8.20.16#820016-sha1:9d11dbea5f4be3d4cc21f03a88dd11d8c8687422.