暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

PostgreSQL 14 preview - kill 会话增加超时等待. pg_wait_for_backend_termination(pid, ms)

digoal 2021-01-04
442

作者

digoal

日期

2021-04-08

标签

PostgreSQL , pg_wait_for_backend_termination


背景

pg_wait_for_backend_termination(pid, ms), 发送terminate信号给进程, 并等待若干ms, 如果PID未被kill, 则返回false和warning.

之前的版本terminate pid时, 不等待, 只发信号, 也不知道pid是否真实被kill.

+ * Wait until there is no backend process with the given PID and return true. + * On timeout, a warning is emitted and false is returned. + */ +static bool +pg_wait_until_termination(int pid, int64 timeout) +{ + /* + * Wait in steps of waittime milliseconds until this function exits or + * timeout. + */ + int64 waittime = 100; + /* + * Initially remaining time is the entire timeout specified by the user. + */ + int64 remainingtime = timeout; + + /* + * Check existence of the backend. If the backend still exists, then wait + * for waittime milliseconds, again check for the existence. Repeat this + * until timeout or an error occurs or a pending interrupt such as query + * cancel gets processed. + */ + do + { + if (remainingtime < waittime) + waittime = remainingtime; + + if (kill(pid, 0) == -1) + { + if (errno == ESRCH) + return true; + else + ereport(ERROR, + (errcode(ERRCODE_INTERNAL_ERROR), + errmsg("could not check the existence of the backend with PID %d: %m", + pid))); + } + + /* Process interrupts, if any, before waiting */ + CHECK_FOR_INTERRUPTS(); + + (void) WaitLatch(MyLatch, + WL_LATCH_SET | WL_TIMEOUT | WL_EXIT_ON_PM_DEATH, + waittime, + WAIT_EVENT_BACKEND_TERMINATION); + + ResetLatch(MyLatch); + + remainingtime -= waittime; + } while (remainingtime > 0); + + ereport(WARNING, + (errmsg("backend with PID %d did not terminate within %lld milliseconds", + pid, (long long int) timeout))); + + return false; +}

+ </para> + <para> + If <parameter>timeout</parameter> is not specified or zero, this + function returns <literal>true</literal> whether the process actually + terminates or not, indicating only that the sending of the signal was + successful. If the <parameter>timeout</parameter> is specified (in + milliseconds) and greater than zero, the function waits until the + process is actually terminated or until the given time has passed. If + the process is terminated, the function + returns <literal>true</literal>. On timeout a warning is emitted and + <literal>false</literal> is returned. + </para></entry> + </row> + + <row> + <entry role="func_table_entry"><para role="func_signature"> + <indexterm> + <primary>pg_wait_for_backend_termination</primary> + </indexterm> + <function>pg_wait_for_backend_termination</function> ( <parameter>pid</parameter> <type>integer</type>, <parameter>timeout</parameter> <type>bigint</type> <literal>DEFAULT</literal> <literal>5000</literal> ) + <returnvalue>boolean</returnvalue> + </para> + <para> + Waits for the backend process with the specified Process ID to + terminate. If the process terminates before + the <parameter>timeout</parameter> (in milliseconds) + expires, <literal>true</literal> is returned. On timeout, a warning + is emitted and <literal>false</literal> is returned.

https://git.postgresql.org/gitweb/?p=postgresql.git;a=commit;h=aaf043257205ec523f1ba09a3856464d17cf2281

```
Add functions to wait for backend termination
author Magnus Hagander magnus@hagander.net
Thu, 8 Apr 2021 09:32:14 +0000 (11:32 +0200)
committer Magnus Hagander magnus@hagander.net
Thu, 8 Apr 2021 09:40:54 +0000 (11:40 +0200)
commit aaf043257205ec523f1ba09a3856464d17cf2281
tree 61ed249bb2622454f53511d051dc1508980ab8e3 tree
parent fb310f17812e7321599845a29af2f36c7f1191c3 commit | diff
Add functions to wait for backend termination

This adds a function, pg_wait_for_backend_termination(), and a new
timeout argument to pg_terminate_backend(), which will wait for the
backend to actually terminate (with or without signaling it to do so
depending on which function is called). The default behaviour of
pg_terminate_backend() remains being timeout=0 which does not waiting.
For pg_wait_for_backend_termination() the default wait is 5 seconds.

Author: Bharath Rupireddy
Reviewed-By: Fujii Masao, David Johnston, Muhammad Usama,
Hou Zhijie, Magnus Hagander
Discussion: https://postgr.es/m/CALj2ACUBpunmyhYZw-kXCYs5NM+h6oG_7Df_Tn4mLmmUQifkqA@mail.gmail.com
```

PostgreSQL 许愿链接

您的愿望将传达给PG kernel hacker、数据库厂商等, 帮助提高数据库产品质量和功能, 说不定下一个PG版本就有您提出的功能点. 针对非常好的提议,奖励限量版PG文化衫、纪念品、贴纸、PG热门书籍等,奖品丰富,快来许愿。开不开森.

9.9元购买3个月阿里云RDS PostgreSQL实例

PostgreSQL 解决方案集合

德哥 / digoal's github - 公益是一辈子的事.

digoal's wechat

文章转载自digoal,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论