Skip to content

Commit

Permalink
Add hooks for extensiona, allow custom Table-AM to implement custom x…
Browse files Browse the repository at this point in the history
…log (#848)

Add two hook functions for extension, make custom Table-Am can hook in xlog redo

typedef void (*ConsistencyCheck_hook_type) (void);
extern PGDLLIMPORT ConsistencyCheck_hook_type xlog_check_consistency_hook;

typedef void (*XLOGDropDatabase_hook_type)(Oid dbid);
extern XLOGDropDatabase_hook_type XLOGDropDatabase_hook;
In cloudberrydb, when mirror instance redo xlog, StartupXLog will start replaying from the
latest checkpoint's REDO location in pg_control. When processing the xlog of data writing,
we need to write the data in xlog into the data file, but the entire db directory may be deleted
due to the drop database xlog that has been replayed last time.

XLogDropDatabase and XLogCheckInvalidPages limits the table to be organized in page mode.
For custom Table-Am, their data organization is not necessarily in page mode.

We need to provide a hook for the implementation of custom wal log to handle this situation.
master:
\c testdb
t1: create table t1(a int);
t2: select pg_sleep(checkpoint_timeout); --wait checkpoint_timeout, do checkpoint on mirror;
t3: insert into t1 select generate_series(1,10);
t4: dropdb testdb
t5: pkill postgres -- mirror should redo dropdb and not do next checkpoint on mirror

then we restart the cluster, mirror will recovery restart at the xlog of insert sql, but the data path of testdb has been deleted
  • Loading branch information
gongxun0928 authored Jan 8, 2025
1 parent cdbc1f7 commit 8da7660
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/backend/access/transam/xlog.c
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,10 @@ bool StandbyMode = false;

Startup_hook_type Startup_hook = NULL;

ConsistencyCheck_hook_type xlog_check_consistency_hook = NULL;

XLOGDropDatabase_hook_type XLOGDropDatabase_hook = NULL;

/*
* if recoveryStopsBefore/After returns true, it saves information of the stop
* point here
Expand Down Expand Up @@ -8634,6 +8638,10 @@ CheckRecoveryConsistency(void)
*/
XLogCheckInvalidPages();

if (xlog_check_consistency_hook) {
xlog_check_consistency_hook();
}

reachedConsistency = true;
ereport(LOG,
(errmsg("consistent recovery state reached at %X/%X",
Expand Down
4 changes: 4 additions & 0 deletions src/backend/access/transam/xlogutils.c
Original file line number Diff line number Diff line change
Expand Up @@ -674,6 +674,10 @@ XLogDropDatabase(Oid dbid)
smgrcloseall();

forget_invalid_pages_db(dbid);

if (XLOGDropDatabase_hook) {
XLOGDropDatabase_hook(dbid);
}
}

/*
Expand Down
19 changes: 19 additions & 0 deletions src/include/access/xlog.h
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,25 @@ extern int FileEncryptionEnabled;
typedef void (*Startup_hook_type) (void);
extern PGDLLIMPORT Startup_hook_type Startup_hook;

/* Hook for extensions to do consistency check */
typedef void (*ConsistencyCheck_hook_type) (void);
extern PGDLLIMPORT ConsistencyCheck_hook_type xlog_check_consistency_hook;

/* Hook for extensions to do drop database xlog record
*
* For custom wal log, when mirror redo xlog, since each restart will start
* replaying from the latest checkpoint's REDO location in pg_control, when
* processing the xlog of data writing, the entire db directory may be deleted
* due to the drop database xlog that has been replayed last time. We need to
* provide a hook for the implementation of custom wal log to handle this situation.
*
* Why can't forget_invalid_pages_db meet the requirements?
* Because forget_invalid_pages_db limits the table to be organized in page mode.
* For custom Table-Am, their data organization is not necessarily in page mode.
*/
typedef void (*XLOGDropDatabase_hook_type)(Oid dbid);
extern XLOGDropDatabase_hook_type XLOGDropDatabase_hook;

/* Archive modes */
typedef enum ArchiveMode
{
Expand Down

0 comments on commit 8da7660

Please sign in to comment.