@@ -1550,6 +1550,69 @@ TABLE *find_temporary_table(THD *thd, const TABLE_LIST *tl)
1550
1550
}
1551
1551
1552
1552
1553
+ static bool
1554
+ use_temporary_table (THD *thd, TABLE *table, TABLE **out_table)
1555
+ {
1556
+ *out_table= table;
1557
+ if (!table)
1558
+ return false ;
1559
+ /*
1560
+ Temporary tables are not safe for parallel replication. They were
1561
+ designed to be visible to one thread only, so have no table locking.
1562
+ Thus there is no protection against two conflicting transactions
1563
+ committing in parallel and things like that.
1564
+
1565
+ So for now, anything that uses temporary tables will be serialised
1566
+ with anything before it, when using parallel replication.
1567
+
1568
+ ToDo: We might be able to introduce a reference count or something
1569
+ on temp tables, and have slave worker threads wait for it to reach
1570
+ zero before being allowed to use the temp table. Might not be worth
1571
+ it though, as statement-based replication using temporary tables is
1572
+ in any case rather fragile.
1573
+ */
1574
+ if (thd->rgi_slave && thd->rgi_slave ->is_parallel_exec &&
1575
+ thd->wait_for_prior_commit ())
1576
+ return true ;
1577
+ /*
1578
+ We need to set the THD as it may be different in case of
1579
+ parallel replication
1580
+ */
1581
+ if (table->in_use != thd)
1582
+ {
1583
+ table->in_use = thd;
1584
+ #ifdef REMOVE_AFTER_MERGE_WITH_10
1585
+ if (thd->rgi_slave )
1586
+ {
1587
+ /*
1588
+ We may be stealing an opened temporary tables from one slave
1589
+ thread to another, we need to let the performance schema know that,
1590
+ for aggregates per thread to work properly.
1591
+ */
1592
+ table->file ->unbind_psi ();
1593
+ table->file ->rebind_psi ();
1594
+ }
1595
+ #endif
1596
+ }
1597
+ return false ;
1598
+ }
1599
+
1600
+ bool
1601
+ find_and_use_temporary_table (THD *thd, const char *db, const char *table_name,
1602
+ TABLE **out_table)
1603
+ {
1604
+ return use_temporary_table (thd, find_temporary_table (thd, db, table_name),
1605
+ out_table);
1606
+ }
1607
+
1608
+
1609
+ bool
1610
+ find_and_use_temporary_table (THD *thd, const TABLE_LIST *tl, TABLE **out_table)
1611
+ {
1612
+ return use_temporary_table (thd, find_temporary_table (thd, tl), out_table);
1613
+ }
1614
+
1615
+
1553
1616
/* *
1554
1617
Find a temporary table specified by a key in the THD::temporary_tables list.
1555
1618
@@ -1570,26 +1633,6 @@ TABLE *find_temporary_table(THD *thd,
1570
1633
if (table->s ->table_cache_key .length == table_key_length &&
1571
1634
!memcmp (table->s ->table_cache_key .str , table_key, table_key_length))
1572
1635
{
1573
- /*
1574
- We need to set the THD as it may be different in case of
1575
- parallel replication
1576
- */
1577
- if (table->in_use != thd)
1578
- {
1579
- table->in_use = thd;
1580
- #ifdef REMOVE_AFTER_MERGE_WITH_10
1581
- if (thd->rgi_slave )
1582
- {
1583
- /*
1584
- We may be stealing an opened temporary tables from one slave
1585
- thread to another, we need to let the performance schema know that,
1586
- for aggregates per thread to work properly.
1587
- */
1588
- table->file ->unbind_psi ();
1589
- table->file ->rebind_psi ();
1590
- }
1591
- #endif
1592
- }
1593
1636
result= table;
1594
1637
break ;
1595
1638
}
@@ -5822,7 +5865,9 @@ bool open_temporary_table(THD *thd, TABLE_LIST *tl)
5822
5865
DBUG_RETURN (FALSE );
5823
5866
}
5824
5867
5825
- if (!(table= find_temporary_table (thd, tl)))
5868
+ if (find_and_use_temporary_table (thd, tl, &table))
5869
+ DBUG_RETURN (TRUE );
5870
+ if (!table)
5826
5871
{
5827
5872
if (tl->open_type == OT_TEMPORARY_ONLY &&
5828
5873
tl->open_strategy == TABLE_LIST::OPEN_NORMAL)
@@ -5833,25 +5878,6 @@ bool open_temporary_table(THD *thd, TABLE_LIST *tl)
5833
5878
DBUG_RETURN (FALSE );
5834
5879
}
5835
5880
5836
- /*
5837
- Temporary tables are not safe for parallel replication. They were
5838
- designed to be visible to one thread only, so have no table locking.
5839
- Thus there is no protection against two conflicting transactions
5840
- committing in parallel and things like that.
5841
-
5842
- So for now, anything that uses temporary tables will be serialised
5843
- with anything before it, when using parallel replication.
5844
-
5845
- ToDo: We might be able to introduce a reference count or something
5846
- on temp tables, and have slave worker threads wait for it to reach
5847
- zero before being allowed to use the temp table. Might not be worth
5848
- it though, as statement-based replication using temporary tables is
5849
- in any case rather fragile.
5850
- */
5851
- if (thd->rgi_slave && thd->rgi_slave ->is_parallel_exec &&
5852
- thd->wait_for_prior_commit ())
5853
- DBUG_RETURN (true );
5854
-
5855
5881
#ifdef WITH_PARTITION_STORAGE_ENGINE
5856
5882
if (tl->partition_names )
5857
5883
{
0 commit comments