Skip to content

Commit

Permalink
Get PortalContext when starting job
Browse files Browse the repository at this point in the history
When executing functions, SPI assumes that `TopTransactionContext` is
used for atomic execution contexts and `PortalContext` is used for
non-atomic contexts. Since jobs need to be able to commit and start
transactions, they are executing in a non-atomic context hence
`PortalContext` will be used, but `PortalContext` is not set when
starting the job. This is not a problem for PL/PgSQL executor, but for
other executors (such as PL/Python) it would be.

This commit fixes the issue by setting the `PortalContext` variable to
the portal context created for the portal and restores it (to NULL)
after execution.

Fixes #5326

(cherry picked from commit 0cbd740)
  • Loading branch information
mkindahl authored and timescale-automation committed Mar 3, 2023
1 parent 8ddecca commit aaa5992
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ Sooner to that time, we will announce the specific version of TimescaleDB in whi
* #5246 Make connection establishment interruptible
* #5253 Make data node command execution interruptible
* #5262 Extend enabling compression on a continuous aggregrate with 'compress_segmentby' and 'compress_orderby' parameters
* #5343 Set PortalContext when starting job

**Bugfixes**
* #5214 Fix use of prepared statement in async module
Expand All @@ -52,6 +53,9 @@ Sooner to that time, we will announce the specific version of TimescaleDB in whi
* @henriquegelio for reporting the issue on fixed schedules
* #5336 Use NameData and namestrcpy for names

**Thanks**
* @justinozavala for reporting an issue with PL/Python procedures in the background worker

## 2.9.3 (2023-02-03)

This release contains bug fixes since the 2.9.2 release and a fix for a security vulnerability (#5259).
Expand Down
2 changes: 2 additions & 0 deletions tsl/src/bgw_policy/job.c
Original file line number Diff line number Diff line change
Expand Up @@ -597,6 +597,7 @@ job_execute(BgwJob *job)
portal->visible = false;
portal->resowner = CurrentResourceOwner;
ActivePortal = portal;
PortalContext = portal->portalContext;

StartTransactionCommand();
#if (PG12 && PG_VERSION_NUM >= 120008) || (PG13 && PG_VERSION_NUM >= 130004) || PG14_GE
Expand Down Expand Up @@ -670,6 +671,7 @@ job_execute(BgwJob *job)
CommitTransactionCommand();
PortalDrop(portal, false);
ActivePortal = NULL;
PortalContext = NULL;
}

return true;
Expand Down

0 comments on commit aaa5992

Please sign in to comment.