@@ -88,6 +88,8 @@ typedef struct fmgr_plpgsql_cache
8888 bool is_late ;
8989 Datum arg ;
9090
91+ PLpgSQL_execstate * estate ;
92+
9193 void * plugin2_info [MAX_PLDBGAPI2_PLUGINS ];
9294
9395 MemoryContext fn_mcxt ;
@@ -883,6 +885,7 @@ pldbgapi2_func_setup(PLpgSQL_execstate *estate, PLpgSQL_function *func)
883885 func_info -> use_count ++ ;
884886
885887 fcache_plpgsql -> func_info = func_info ;
888+ fcache_plpgsql -> estate = estate ;
886889
887890 estate -> plugin_info = plugin_info ;
888891
@@ -980,6 +983,7 @@ pldbgapi2_func_beg(PLpgSQL_execstate *estate, PLpgSQL_function *func)
980983 if (fcache_plpgsql -> funcid != PLpgSQLinlineFunc )
981984 {
982985 Assert (fcache_plpgsql -> funcid == func -> fn_oid );
986+ Assert (fcache_plpgsql -> estate = estate );
983987 Assert (fcache_plpgsql -> funcid == estate -> func -> fn_oid );
984988 }
985989 else
@@ -1049,6 +1053,7 @@ pldbgapi2_func_end(PLpgSQL_execstate *estate, PLpgSQL_function *func)
10491053 if (fcache_plpgsql -> funcid != PLpgSQLinlineFunc )
10501054 {
10511055 Assert (fcache_plpgsql -> funcid == func -> fn_oid );
1056+ Assert (fcache_plpgsql -> estate = estate );
10521057 Assert (fcache_plpgsql -> funcid == estate -> func -> fn_oid );
10531058 }
10541059 else
@@ -1113,7 +1118,8 @@ pldbgapi2_stmt_beg(PLpgSQL_execstate *estate, PLpgSQL_stmt *stmt)
11131118
11141119 if (!fcache_plpgsql ||
11151120 fcache_plpgsql -> magic != FMGR_CACHE_MAGIC ||
1116- !fcache_plpgsql -> is_plpgsql )
1121+ !fcache_plpgsql -> is_plpgsql ||
1122+ fcache_plpgsql -> estate != estate )
11171123 elog (ERROR , "broken fcache_plpgsql" );
11181124
11191125 /*
@@ -1234,6 +1240,7 @@ pldbgapi2_stmt_end(PLpgSQL_execstate *estate, PLpgSQL_stmt *stmt)
12341240
12351241 if (fcache_plpgsql -> funcid != PLpgSQLinlineFunc )
12361242 Assert (fcache_plpgsql -> funcid == estate -> func -> fn_oid );
1243+ Assert (fcache_plpgsql -> estate = estate );
12371244 else
12381245 Assert (!OidIsValid (estate -> func -> fn_oid ));
12391246
0 commit comments