MCPcopy Create free account
hub / github.com/apache/cloudberry / CronBackgroundWorker

Function CronBackgroundWorker

src/backend/task/pg_cron.c:1762–1833  ·  view source on GitHub ↗

* Background worker logic. */

Source from the content-addressed store, hash-verified

1760 * Background worker logic.
1761 */
1762void
1763CronBackgroundWorker(Datum main_arg)
1764{
1765 dsm_segment *seg;
1766 shm_toc *toc;
1767 char *database;
1768 char *username;
1769 char *command;
1770 shm_mq *mq;
1771 shm_mq_handle *responseq;
1772
1773 /* handle SIGTERM like regular backend */
1774 pqsignal(SIGTERM, die);
1775 BackgroundWorkerUnblockSignals();
1776
1777 /* Set up a memory context and resource owner. */
1778 Assert(CurrentResourceOwner == NULL);
1779 CurrentResourceOwner = ResourceOwnerCreate(NULL, "pg_cron");
1780 CurrentMemoryContext = AllocSetContextCreate(TopMemoryContext,
1781 "pg_cron worker",
1782 ALLOCSET_DEFAULT_MINSIZE,
1783 ALLOCSET_DEFAULT_INITSIZE,
1784 ALLOCSET_DEFAULT_MAXSIZE);
1785
1786 /* Set up a dynamic shared memory segment. */
1787 seg = dsm_attach(DatumGetUInt32(main_arg));
1788 if (seg == NULL)
1789 ereport(ERROR,
1790 (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
1791 errmsg("unable to map dynamic shared memory segment")));
1792 toc = shm_toc_attach(PG_CRON_MAGIC, dsm_segment_address(seg));
1793 if (toc == NULL)
1794 ereport(ERROR,
1795 (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
1796 errmsg("bad magic number in dynamic shared memory segment")));
1797
1798 database = shm_toc_lookup(toc, PG_CRON_KEY_DATABASE, false);
1799 username = shm_toc_lookup(toc, PG_CRON_KEY_USERNAME, false);
1800 command = shm_toc_lookup(toc, PG_CRON_KEY_COMMAND, false);
1801 mq = shm_toc_lookup(toc, PG_CRON_KEY_QUEUE, false);
1802
1803 shm_mq_set_sender(mq, MyProc);
1804 responseq = shm_mq_attach(mq, seg, NULL);
1805 pq_redirect_to_shm_mq(seg, responseq);
1806
1807 BackgroundWorkerInitializeConnection(database, username, 0);
1808
1809 /* Prepare to execute the query. */
1810 SetCurrentStatementStartTimestamp();
1811 debug_query_string = command;
1812 pgstat_report_activity(STATE_RUNNING, command);
1813 StartTransactionCommand();
1814 if (StatementTimeout > 0)
1815 enable_timeout_after(STATEMENT_TIMEOUT, StatementTimeout);
1816 else
1817 disable_timeout(STATEMENT_TIMEOUT, false);
1818
1819 /* Execute the query. */

Callers

nothing calls this directly

Calls 15

ResourceOwnerCreateFunction · 0.85
dsm_attachFunction · 0.85
DatumGetUInt32Function · 0.85
shm_toc_attachFunction · 0.85
dsm_segment_addressFunction · 0.85
shm_toc_lookupFunction · 0.85
shm_mq_set_senderFunction · 0.85
shm_mq_attachFunction · 0.85
pq_redirect_to_shm_mqFunction · 0.85

Tested by

no test coverage detected