Skip to content

Add command_start hook for rollback at statement level purpose. #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions src/backend/tcop/postgres.c
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,8 @@ static void log_disconnections(int code, Datum arg);
static void enable_statement_timeout(void);
static void disable_statement_timeout(void);

/* Hooks for plugins to get control at end of start_xact_command() */
XactCommandStart_hook_type start_xact_command_hook = NULL;

/* ----------------------------------------------------------------
* routines to obtain user input
Expand Down Expand Up @@ -1025,6 +1027,13 @@ exec_simple_query(const char *query_string)
*/
start_xact_command();

/*
* Now give loadable modules a chance to execute code
* before a transaction command is processed.
*/
if (start_xact_command_hook)
(*start_xact_command_hook) ();

/*
* Zap any pre-existing unnamed statement. (While not strictly necessary,
* it seems best to define simple-Query mode as if it used the unnamed
Expand Down Expand Up @@ -1118,6 +1127,13 @@ exec_simple_query(const char *query_string)
/* Make sure we are in a transaction command */
start_xact_command();

/*
* Now give loadable modules a chance to execute code
* before a transaction command is processed.
*/
if (start_xact_command_hook)
(*start_xact_command_hook) ();

/*
* If using an implicit transaction block, and we're not already in a
* transaction block, start an implicit block to force this statement
Expand Down Expand Up @@ -1397,6 +1413,13 @@ exec_parse_message(const char *query_string, /* string to execute */
*/
start_xact_command();

/*
* Now give loadable modules a chance to execute code
* before a transaction command is processed.
*/
if (start_xact_command_hook)
(*start_xact_command_hook) ();

/*
* Switch to appropriate context for constructing parsetrees.
*
Expand Down Expand Up @@ -1661,6 +1684,13 @@ exec_bind_message(StringInfo input_message)
*/
start_xact_command();

/*
* Now give loadable modules a chance to execute code
* before a transaction command is processed.
*/
if (start_xact_command_hook)
(*start_xact_command_hook) ();

/* Switch back to message context */
MemoryContextSwitchTo(MessageContext);

Expand Down Expand Up @@ -2154,6 +2184,13 @@ exec_execute_message(const char *portal_name, long max_rows)
*/
start_xact_command();

/*
* Now give loadable modules a chance to execute code
* before a transaction command is processed.
*/
if (start_xact_command_hook)
(*start_xact_command_hook) ();

/*
* If we re-issue an Execute protocol request against an existing portal,
* then we are only fetching more rows rather than completely re-executing
Expand Down Expand Up @@ -2560,6 +2597,13 @@ exec_describe_statement_message(const char *stmt_name)
*/
start_xact_command();

/*
* Now give loadable modules a chance to execute code
* before a transaction command is processed.
*/
if (start_xact_command_hook)
(*start_xact_command_hook) ();

/* Switch back to message context */
MemoryContextSwitchTo(MessageContext);

Expand Down Expand Up @@ -2654,6 +2698,13 @@ exec_describe_portal_message(const char *portal_name)
*/
start_xact_command();

/*
* Now give loadable modules a chance to execute code
* before a transaction command is processed.
*/
if (start_xact_command_hook)
(*start_xact_command_hook) ();

/* Switch back to message context */
MemoryContextSwitchTo(MessageContext);

Expand Down Expand Up @@ -4619,6 +4670,13 @@ PostgresMain(const char *dbname, const char *username)
/* start an xact for this function invocation */
start_xact_command();

/*
* Now give loadable modules a chance to execute code
* before a transaction command is processed.
*/
if (start_xact_command_hook)
(*start_xact_command_hook) ();

/*
* Note: we may at this point be inside an aborted
* transaction. We can't throw error for that until we've
Expand Down
4 changes: 4 additions & 0 deletions src/include/tcop/pquery.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,8 @@ extern bool PlannedStmtRequiresSnapshot(struct PlannedStmt *pstmt);

extern void EnsurePortalSnapshotExists(void);

/* Hook for plugins to get control in start_xact_command() and finish_xact_command() */
typedef void (*XactCommandStart_hook_type) (void);
extern PGDLLIMPORT XactCommandStart_hook_type start_xact_command_hook;

#endif /* PQUERY_H */
1 change: 1 addition & 0 deletions src/tools/pgindent/typedefs.list
Original file line number Diff line number Diff line change
Expand Up @@ -3052,6 +3052,7 @@ XPVIV
XPVMG
XactCallback
XactCallbackItem
XactCommandStart_hook_type
XactEvent
XactLockTableWaitInfo
XidBoundsViolation
Expand Down