BackendKeyData
此消息提供了前端必须保存的密钥数据,以便以后能够发出取消请求。 前端不应响应此消息,而应继续侦听 ReadyForQuery 消息。This message provides secret-key data that the frontend must save if it wants to be able to issue cancel requests later. The frontend should not respond to this message, but should continue listening for a ReadyForQuery message.
/* Send this backend's cancellation info to the frontend. */
if (whereToSendOutput == DestRemote){
StringInfoData buf;
pq_beginmessage(&buf, 'K');
pq_sendint32(&buf, (int32) MyProcPid);
pq_sendint32(&buf, (int32) MyCancelKey);
pq_endmessage(&buf);
/* Need not flush since ReadyForQuery will do it. */
}
MyCancelKey来源于哪里呢?
在static int BackendStartup(Port *port)
函数中调用RandomCancelKey(&MyCancelKey)
,RandomCancelKey计算将分配给此后端的取消键。
static bool RandomCancelKey(int32 *cancel_key){
return pg_strong_random(cancel_key, sizeof(int32));
}