Skip to content

Commit

Permalink
Fix dangling file storage when aborting a transaction with a temp tab…
Browse files Browse the repository at this point in the history
…le. (#2930)

There were some cases when a transaction or procedure which created a
temp table was aborted where the physical on-disk file associated with
the temp table was not properly being deleted. This commit fixes the
behavior by triggering a manual call to smgrDoPendingDeletes() with the
appropriate isCommit flag when necessary during transaction aborts.

Task: BABEL-4751
Signed-off-by: Jason Teng <[email protected]>
  • Loading branch information
Sairakan authored Sep 23, 2024
1 parent 0636924 commit e2de6f4
Show file tree
Hide file tree
Showing 12 changed files with 376 additions and 5 deletions.
2 changes: 1 addition & 1 deletion contrib/babelfishpg_tsql/src/guc.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,5 @@ void pltsql_revert_guc(int nest_level);

extern int pltsql_new_scope_identity_nest_level(void);
extern void pltsql_revert_last_scope_identity(int nest_level);
extern void pltsql_remove_current_query_env(void);
extern void pltsql_remove_current_query_env(bool is_abort);
#endif
4 changes: 2 additions & 2 deletions contrib/babelfishpg_tsql/src/pl_exec-2.c
Original file line number Diff line number Diff line change
Expand Up @@ -1666,14 +1666,14 @@ execute_batch(PLtsql_execstate *estate, char *batch, InlineCodeBlockArgs *args,
PG_CATCH();
{
/* Delete temporary tables as ENR */
pltsql_remove_current_query_env();
pltsql_remove_current_query_env(true);

PG_RE_THROW();
}
PG_END_TRY();

/* Delete temporary tables as ENR */
pltsql_remove_current_query_env();
pltsql_remove_current_query_env(false);

after_lxid = MyProc->lxid;

Expand Down
7 changes: 5 additions & 2 deletions contrib/babelfishpg_tsql/src/pl_handler.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#include "catalog/pg_type.h"
#include "catalog/pg_default_acl.h"
#include "catalog/pg_shdepend.h"
#include "catalog/storage.h"
#include "commands/createas.h"
#include "commands/dbcommands.h"
#include "commands/defrem.h"
Expand Down Expand Up @@ -5211,7 +5212,7 @@ pltsql_call_handler(PG_FUNCTION_ARGS)

func->cur_estate = save_cur_estate;

pltsql_remove_current_query_env();
pltsql_remove_current_query_env(send_error);
pltsql_revert_guc(save_nestlevel);
pltsql_revert_last_scope_identity(scope_level);
}
Expand Down Expand Up @@ -6485,7 +6486,7 @@ set_current_query_is_create_tbl_check_constraint(Node *expr)
}

void
pltsql_remove_current_query_env(void)
pltsql_remove_current_query_env(bool is_abort)
{
bool old_abort_curr_txn = AbortCurTransaction;

Expand All @@ -6495,6 +6496,8 @@ pltsql_remove_current_query_env(void)
AbortCurTransaction = false;

ENRDropTempTables(currentQueryEnv);
if (is_abort)
smgrDoPendingDeletes(!old_abort_curr_txn);
}
PG_FINALLY();
{
Expand Down
3 changes: 3 additions & 0 deletions test/JDBC/expected/temp_table_rollback-vu-cleanup.out
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,6 @@ GO

DROP PROCEDURE tv_base_rollback
GO

DROP PROCEDURE tv_tt_no_error
GO
10 changes: 10 additions & 0 deletions test/JDBC/expected/temp_table_rollback-vu-prepare.out
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,13 @@ BEGIN
INSERT INTO @tv VALUES (1)
END
GO

CREATE PROCEDURE tv_tt_no_error AS
BEGIN
DECLARE @tv TABLE (a int)
CREATE TABLE #t1 (a int)
INSERT INTO temp_tab_rollback_mytab VALUES (1)
INSERT INTO @tv VALUES (1)
INSERT INTO #t1 VALUES (1)
END
GO
69 changes: 69 additions & 0 deletions test/JDBC/expected/temp_table_rollback-vu-verify.out
Original file line number Diff line number Diff line change
Expand Up @@ -859,6 +859,75 @@ int
~~END~~


BEGIN TRAN
INSERT INTO temp_tab_rollback_mytab VALUES (2)
EXEC tv_tt_no_error
SELECT * FROM temp_tab_rollback_mytab
DROP TABLE temp_tab_rollback_mytab
ROLLBACK
GO
~~ROW COUNT: 1~~

~~ROW COUNT: 1~~

~~ROW COUNT: 1~~

~~ROW COUNT: 1~~

~~START~~
int
2
1
~~END~~


SELECT * FROM temp_tab_rollback_mytab
GO
~~START~~
int
~~END~~


BEGIN TRAN
INSERT INTO temp_tab_rollback_mytab VALUES (2)
CREATE TABLE #outer_table (a int)
INSERT INTO #outer_table VALUES (1)
EXEC tv_tt_no_error
SELECT * FROM temp_tab_rollback_mytab
DROP TABLE temp_tab_rollback_mytab
ROLLBACK
GO
~~ROW COUNT: 1~~

~~ROW COUNT: 1~~

~~ROW COUNT: 1~~

~~ROW COUNT: 1~~

~~ROW COUNT: 1~~

~~START~~
int
2
1
~~END~~


SELECT * FROM temp_tab_rollback_mytab
GO
~~START~~
int
~~END~~


SELECT * FROM #outer_table
GO
~~ERROR (Code: 33557097)~~

~~ERROR (Message: relation "#outer_table" does not exist)~~


DROP TABLE temp_tab_rollback_mytab
GO

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,16 @@ BEGIN
END
GO

CREATE PROCEDURE tv_tt_no_error AS
BEGIN
DECLARE @tv TABLE (a int)
CREATE TABLE #t1 (a int)
INSERT INTO temp_tab_rollback_mytab VALUES (1)
INSERT INTO @tv VALUES (1)
INSERT INTO #t1 VALUES (1)
END
GO

-- pg_class, pg_type, pg_depend, pg_attribute, pg_constraint, pg_index, pg_sequence are all covered by below tests.
-------------------------------
-- Temp Table CREATE + ROLLBACK
Expand Down Expand Up @@ -927,6 +937,75 @@ int
~~END~~


BEGIN TRAN
INSERT INTO temp_tab_rollback_mytab VALUES (2)
EXEC tv_tt_no_error
SELECT * FROM temp_tab_rollback_mytab
DROP TABLE temp_tab_rollback_mytab
ROLLBACK
GO
~~ROW COUNT: 1~~

~~ROW COUNT: 1~~

~~ROW COUNT: 1~~

~~ROW COUNT: 1~~

~~START~~
int
2
1
~~END~~


SELECT * FROM temp_tab_rollback_mytab
GO
~~START~~
int
~~END~~


BEGIN TRAN
INSERT INTO temp_tab_rollback_mytab VALUES (2)
CREATE TABLE #outer_table (a int)
INSERT INTO #outer_table VALUES (1)
EXEC tv_tt_no_error
SELECT * FROM temp_tab_rollback_mytab
DROP TABLE temp_tab_rollback_mytab
ROLLBACK
GO
~~ROW COUNT: 1~~

~~ROW COUNT: 1~~

~~ROW COUNT: 1~~

~~ROW COUNT: 1~~

~~ROW COUNT: 1~~

~~START~~
int
2
1
~~END~~


SELECT * FROM temp_tab_rollback_mytab
GO
~~START~~
int
~~END~~


SELECT * FROM #outer_table
GO
~~ERROR (Code: 33557097)~~

~~ERROR (Message: relation "#outer_table" does not exist)~~


DROP TABLE temp_tab_rollback_mytab
GO

Expand Down Expand Up @@ -1398,3 +1477,6 @@ GO

DROP PROCEDURE tv_base_rollback
GO

DROP PROCEDURE tv_tt_no_error
GO
82 changes: 82 additions & 0 deletions test/JDBC/expected/temp_table_rollback_isolation_snapshot.out
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,16 @@ BEGIN
END
GO

CREATE PROCEDURE tv_tt_no_error AS
BEGIN
DECLARE @tv TABLE (a int)
CREATE TABLE #t1 (a int)
INSERT INTO temp_tab_rollback_mytab VALUES (1)
INSERT INTO @tv VALUES (1)
INSERT INTO #t1 VALUES (1)
END
GO

-- pg_class, pg_type, pg_depend, pg_attribute, pg_constraint, pg_index, pg_sequence are all covered by below tests.
-------------------------------
-- Temp Table CREATE + ROLLBACK
Expand Down Expand Up @@ -927,6 +937,75 @@ int
~~END~~


BEGIN TRAN
INSERT INTO temp_tab_rollback_mytab VALUES (2)
EXEC tv_tt_no_error
SELECT * FROM temp_tab_rollback_mytab
DROP TABLE temp_tab_rollback_mytab
ROLLBACK
GO
~~ROW COUNT: 1~~

~~ROW COUNT: 1~~

~~ROW COUNT: 1~~

~~ROW COUNT: 1~~

~~START~~
int
2
1
~~END~~


SELECT * FROM temp_tab_rollback_mytab
GO
~~START~~
int
~~END~~


BEGIN TRAN
INSERT INTO temp_tab_rollback_mytab VALUES (2)
CREATE TABLE #outer_table (a int)
INSERT INTO #outer_table VALUES (1)
EXEC tv_tt_no_error
SELECT * FROM temp_tab_rollback_mytab
DROP TABLE temp_tab_rollback_mytab
ROLLBACK
GO
~~ROW COUNT: 1~~

~~ROW COUNT: 1~~

~~ROW COUNT: 1~~

~~ROW COUNT: 1~~

~~ROW COUNT: 1~~

~~START~~
int
2
1
~~END~~


SELECT * FROM temp_tab_rollback_mytab
GO
~~START~~
int
~~END~~


SELECT * FROM #outer_table
GO
~~ERROR (Code: 33557097)~~

~~ERROR (Message: relation "#outer_table" does not exist)~~


DROP TABLE temp_tab_rollback_mytab
GO

Expand Down Expand Up @@ -1398,3 +1477,6 @@ GO

DROP PROCEDURE tv_base_rollback
GO

DROP PROCEDURE tv_tt_no_error
GO
Loading

0 comments on commit e2de6f4

Please sign in to comment.