diff --git a/sql/rpl_slave.cc b/sql/rpl_slave.cc
index ccf8dd1..8e3c113 100644
--- a/sql/rpl_slave.cc
+++ b/sql/rpl_slave.cc
@@ -4055,6 +4055,7 @@ static int init_slave_thread(THD* thd, SLAVE_THD_TYPE thd_type)
   thd->system_thread= (thd_type == SLAVE_THD_WORKER) ?
     SYSTEM_THREAD_SLAVE_WORKER : (thd_type == SLAVE_THD_SQL) ?
     SYSTEM_THREAD_SLAVE_SQL : SYSTEM_THREAD_SLAVE_IO;
+  thd->thread_priority= SYS_PRIORITY;
   thd->security_ctx->skip_grants();
   my_net_init(&thd->net, 0);
   thd->slave_thread = 1;
diff --git a/sql/sql_class.cc b/sql/sql_class.cc
index 238585c..b4c0744 100644
--- a/sql/sql_class.cc
+++ b/sql/sql_class.cc
@@ -1179,6 +1179,7 @@ THD::THD(bool enable_plugins)
   client_capabilities= 0;                       // minimalistic client
   ull=0;
   system_thread= NON_SYSTEM_THREAD;
+  thread_priority= NON_PRIORITY;
   cleanup_done= abort_on_warning= 0;
   m_release_resources_done= false;
   peer_port= 0;					// For SHOW PROCESSLIST
@@ -4847,6 +4848,14 @@ extern "C" bool thd_is_strict_mode(const MYSQL_THD thd)
   return thd->is_strict_mode();
 }

+/**
+ *   Reaturn the priority of the thread.
+ */
+extern "C" uint thd_get_priority(const MYSQL_THD thd)
+{
+	return (uint)thd->thread_priority;
+}
+
 #ifndef EMBEDDED_LIBRARY
 extern "C" void thd_pool_wait_begin(MYSQL_THD thd, int wait_type);
 extern "C" void thd_pool_wait_end(MYSQL_THD thd);
diff --git a/sql/sql_class.h b/sql/sql_class.h
index d27b348..9f0c625 100644
--- a/sql/sql_class.h
+++ b/sql/sql_class.h
@@ -1928,6 +1928,15 @@ enum enum_thread_type
   SYSTEM_THREAD_FAILOVER_SERVICE = 256
 };

+enum enum_thread_priority
+{
+	NON_PRIORITY= 0,
+	USR_PRIORITY= 1,
+	RDS_PRIORITY= 2,
+	SYS_PRIORITY= 4,
+	SUP_PRIORITY= 8
+};
+
 inline char const *
 show_system_thread(enum_thread_type thread)
 {
@@ -3271,6 +3280,12 @@ public:
   uint	     tmp_table;
   uint	     server_status,open_options;
   enum enum_thread_type system_thread;
+  /*
+	Represent the priority of thread, default value is 0. System threads
+	may get higher value so that they wouldn't be chosed as victim when
+	dead lock occur.
+  */
+  enum enum_thread_priority thread_priority;
   uint       select_number;             //number of select (used for EXPLAIN)
   /*
     Current or next transaction isolation level.
diff --git a/storage/innobase/handler/ha_innodb.cc b/storage/innobase/handler/ha_innodb.cc
index 573af88..b82005f 100644
--- a/storage/innobase/handler/ha_innodb.cc
+++ b/storage/innobase/handler/ha_innodb.cc
@@ -18218,3 +18218,14 @@ innobase_convert_to_system_charset(

 	return(strconvert(cs1, from, cs2, to, len, errors));
 }
+
+/**********************************************************************
+Reaturn the priority of the thread */
+UNIV_INTERN
+uint
+thd_priority(
+/*===============================*/
+	THD*  thd)  /* in: thread handle*/
+{
+	return (thd_get_priority(thd));
+}
diff --git a/storage/innobase/handler/ha_innodb.h b/storage/innobase/handler/ha_innodb.h
index 28cae6c..a6605fc 100644
--- a/storage/innobase/handler/ha_innodb.h
+++ b/storage/innobase/handler/ha_innodb.h
@@ -413,6 +413,13 @@ void rds_update_strict_trx_idle_timeout(uint seconds);
 int thd_non_transactional_update(const MYSQL_THD thd);

 /**
+  Reaturn the priority of the thread
+  @param thd user thread
+  @retval value of enum type enum_thread_priority
+*/
+uint thd_get_priority(const MYSQL_THD thd);
+
+/**
   Get the user thread's binary logging format
   @param thd  user thread
   @return Value to be used as index into the binlog_format_names array
diff --git a/storage/innobase/include/ha_prototypes.h b/storage/innobase/include/ha_prototypes.h
index 3a57efb..74d3f7f 100644
--- a/storage/innobase/include/ha_prototypes.h
+++ b/storage/innobase/include/ha_prototypes.h
@@ -628,5 +628,12 @@ innobase_convert_to_filename_charset(
 	const char*     from,   /* in: identifier to convert */
 	ulint           len);   /* in: length of 'to', in bytes */

+/**********************************************************************
+Reaturn the priority of the thread */
+UNIV_INTERN
+uint
+thd_priority(
+/*=================================*/
+	THD*  thd);  /* in: thread handle */

 #endif /* HA_INNODB_PROTOTYPES_H */
diff --git a/storage/innobase/trx/trx0trx.cc b/storage/innobase/trx/trx0trx.cc
index 7c17ae4..703cb4a 100644
--- a/storage/innobase/trx/trx0trx.cc
+++ b/storage/innobase/trx/trx0trx.cc
@@ -2135,8 +2135,8 @@ trx_assert_started(

 /*******************************************************************//**
 Compares the "weight" (or size) of two transactions. Transactions that
-have edited non-transactional tables are considered heavier than ones
-that have not.
+have higher priority or have edited non-transactional tables are considered
+heavier than ones that have not.
 @return	TRUE if weight(a) >= weight(b) */
 UNIV_INTERN
 ibool
@@ -2148,6 +2148,15 @@ trx_weight_ge(
 	ibool	a_notrans_edit;
 	ibool	b_notrans_edit;

+	 /* If the priority of the threads is not same, we chose the lower
+	 one to be the victim. */
+
+	if (a->mysql_thd != NULL && b->mysql_thd != NULL
+			&& thd_priority(a->mysql_thd) != thd_priority(b->mysql_thd))
+	{
+		return thd_priority(a->mysql_thd) > thd_priority(b->mysql_thd);
+	}
+
 	/* If mysql_thd is NULL for a transaction we assume that it has
 	not edited non-transactional tables. */
