diff --git a/mysql-test/suite/maria/create.result b/mysql-test/suite/maria/create.result
index 82c6b8c9871aeb80cdd2b51cb45e93dc747917af..462c148df8e2a7cbcc70b6971258975051f97a08 100644
--- a/mysql-test/suite/maria/create.result
+++ b/mysql-test/suite/maria/create.result
@@ -31,3 +31,36 @@ select * from t2;
 f1	f2
 3	qux
 DROP TABLE t1, t2;
+#
+# MDEV-23159 Assertion `table_share->tmp_table != NO_TMP_TABLE ||
+# m_lock_type != 2' + SIGSEGV in trnman_can_read_from
+# (on optimized builds)
+#
+SET @org_sql_mode=@@SQL_MODE;
+CREATE TABLE t1 (a INT PRIMARY KEY) ENGINE=Aria ROW_FORMAT=COMPRESSED;
+INSERT INTO t1 VALUES(1);
+CREATE TEMPORARY TABLE t2(b INT);
+EXPLAIN SELECT * FROM t1 WHERE a IN (SELECT MAX(a) FROM t2);
+id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
+1	PRIMARY	t1	system	NULL	NULL	NULL	NULL	1	Using index
+2	DEPENDENT SUBQUERY	NULL	NULL	NULL	NULL	NULL	NULL	NULL	Select tables optimized away
+DROP TABLE t1,t2;
+SET SQL_MODE='';
+CREATE TABLE t1 (c INT PRIMARY KEY) ENGINE=Aria;
+CREATE TABLE t2 (d INT);
+INSERT INTO t1 VALUES (1);
+SELECT c FROM t1 WHERE (c) IN (SELECT MIN(c) FROM t2);
+c
+DROP TABLE t1,t2;
+USE test;
+SET SQL_MODE='ONLY_FULL_GROUP_BY';
+CREATE TABLE t3 (c1 DECIMAL(1,1) PRIMARY KEY,c2 DATE,c3 NUMERIC(10) UNSIGNED) ENGINE=Aria;
+CREATE TABLE t2 (f1 INTEGER ) ENGINE=Aria;
+INSERT INTO t3 VALUES (0,0,0);
+SELECT c1 FROM t3 WHERE (c1) IN (SELECT MIN(DISTINCT c1) FROM t2);
+c1
+DROP TABLE t2,t3;
+SET @@SQL_MODE=@org_sql_mode;
+#
+# End of 10.3 tests
+#
diff --git a/mysql-test/suite/maria/create.test b/mysql-test/suite/maria/create.test
index 8f2ffd7492f0a00a793475c75313483439f0d881..0efae8cac1b33bea3a8dc3df3b4bec2750835d26 100644
--- a/mysql-test/suite/maria/create.test
+++ b/mysql-test/suite/maria/create.test
@@ -40,3 +40,36 @@ INSERT IGNORE INTO t1 VALUES (1),(2);
 CREATE OR REPLACE TABLE t2 ENGINE=Aria AS SELECT SUM(a) AS f1, IFNULL( 'qux', ExtractValue( 'foo', 'bar' ) ) AS f2 FROM t1;
 select * from t2;
 DROP TABLE t1, t2;
+
+--echo #
+--echo # MDEV-23159 Assertion `table_share->tmp_table != NO_TMP_TABLE ||
+--echo # m_lock_type != 2' + SIGSEGV in trnman_can_read_from
+--echo # (on optimized builds)
+--echo #
+
+SET @org_sql_mode=@@SQL_MODE;
+CREATE TABLE t1 (a INT PRIMARY KEY) ENGINE=Aria ROW_FORMAT=COMPRESSED;
+INSERT INTO t1 VALUES(1);
+CREATE TEMPORARY TABLE t2(b INT);
+EXPLAIN SELECT * FROM t1 WHERE a IN (SELECT MAX(a) FROM t2);
+DROP TABLE t1,t2;
+
+SET SQL_MODE='';
+CREATE TABLE t1 (c INT PRIMARY KEY) ENGINE=Aria;
+CREATE TABLE t2 (d INT);
+INSERT INTO t1 VALUES (1);
+SELECT c FROM t1 WHERE (c) IN (SELECT MIN(c) FROM t2);
+DROP TABLE t1,t2;
+
+USE test;
+SET SQL_MODE='ONLY_FULL_GROUP_BY';
+CREATE TABLE t3 (c1 DECIMAL(1,1) PRIMARY KEY,c2 DATE,c3 NUMERIC(10) UNSIGNED) ENGINE=Aria;
+CREATE TABLE t2 (f1 INTEGER ) ENGINE=Aria;
+INSERT INTO t3 VALUES (0,0,0);
+SELECT c1 FROM t3 WHERE (c1) IN (SELECT MIN(DISTINCT c1) FROM t2);
+DROP TABLE t2,t3;
+SET @@SQL_MODE=@org_sql_mode;
+
+--echo #
+--echo # End of 10.3 tests
+--echo #
diff --git a/sql/opt_sum.cc b/sql/opt_sum.cc
index adfad29bb343dc97b404e6a5b0d7f8284a8db1ec..525d7390a26c0b5eefa51715553c4061c2025592 100644
--- a/sql/opt_sum.cc
+++ b/sql/opt_sum.cc
@@ -399,20 +399,28 @@ int opt_sum_query(THD *thd,
             break;
           }
           longlong info_limit= 1;
-          table->file->info_push(INFO_KIND_FORCE_LIMIT_BEGIN, &info_limit);
-          if (likely(!(error= table->file->ha_index_init((uint) ref.key, 1))))
-            error= (is_max ? 
-                    get_index_max_value(table, &ref, range_fl) :
-                    get_index_min_value(table, &ref, item_field, range_fl,
-                                        prefix_len));
+          error= 0;
 
+          table->file->info_push(INFO_KIND_FORCE_LIMIT_BEGIN, &info_limit);
+          if (!table->const_table)
+          {
+            if (likely(!(error= table->file->ha_index_init((uint) ref.key,
+                                                           1))))
+              error= (is_max ?
+                      get_index_max_value(table, &ref, range_fl) :
+                      get_index_min_value(table, &ref, item_field, range_fl,
+                                          prefix_len));
+          }
           /* Verify that the read tuple indeed matches the search key */
 	  if (!error &&
               reckey_in_range(is_max, &ref, item_field->field,
                               conds, range_fl, prefix_len))
 	    error= HA_ERR_KEY_NOT_FOUND;
-          table->file->ha_end_keyread();
-          table->file->ha_index_end();
+          if (!table->const_table)
+          {
+            table->file->ha_end_keyread();
+            table->file->ha_index_end();
+          }
           table->file->info_push(INFO_KIND_FORCE_LIMIT_END, NULL);
           if (error)
 	  {