EnterpriseDB / mysql_fdw

PostgreSQL foreign data wrapper for MySQL
Other
530 stars 162 forks source link

Undefined behavior while selecting data with bad connection #120

Open naihil opened 7 years ago

naihil commented 7 years ago

Hello!

I found undefined behavior when your connection of pg-mysql is bad. Sometimes pg raise error "Lost connection to MySQL server during query", but sometimes not.

How to reproduce:

  1. Create big table in mysql and try to insert this data to pg;
  2. kill query in mysql after it change state to sleep;

Successfull query:

# begin;
BEGIN

# insert into public.test2 select * from fdw.gprs_telemetry where created_date_time >= '2016-01-01 00:00:00' and created_date_time < '2016-02-01 00:00:00' on conflict do nothing;
INSERT 0 8718806

# insert into public.test2(objid, state_date_time, created_date_time, loc_date_time) values (1, now(), '2016-01-10 00:00:00', now());
INSERT 0 1

# commit;
COMMIT

Query killed in mysql while its state is active (expected behavior):

# begin;
BEGIN

# insert into public.test2 select * from fdw.gprs_telemetry where created_date_time >= '2016-01-01 00:00:00' and created_date_time < '2016-02-01 00:00:00' on conflict do nothing;
ERROR:   7 failed to execute the MySQL query:
Lost connection to MySQL server during query

# insert into public.test2(objid, state_date_time, created_date_time, loc_date_time) values (1, now(), '2016-01-10 00:00:00', now());
ERROR:  current transaction is aborted, commands ignored until end of transaction block

# rollback;
ROLLBACK

Query killed in mysql while its state is sleep and some data inserts into pg table (unexpected behavior):

# begin;
BEGIN

# insert into public.test2 select * from fdw.gprs_telemetry where created_date_time >= '2016-01-01 00:00:00' and created_date_time < '2016-02-01 00:00:00' on conflict do nothing;
INSERT 0 398536

# insert into public.test2(objid, state_date_time, created_date_time, loc_date_time) values (1, now(), '2016-01-10 00:00:00', now());
INSERT 0 1

# commit;
COMMIT

As a result we insert only small part of rows (398536 of 8718806) without any error that connection is lost.

jmealo commented 7 years ago

@naihil Thank you for reporting this issue.

I have some follow up questions that might help someone triage this issue as serious or "expected behavior when handling user-cancelled queries" (from a connection handling perspective).

  1. If you sever the connection instead of cancelling the query, does that reliably output a connection error?
  2. How are you killing the queries from inside MySQL?
  3. If you restart MySQL and PostgreSQL and try to reproduce the undefined behavior, does it ever happen on the "first run" or does it always happen on subsequent runs? Do you notice any patterns?