mirror of
https://git.postgresql.org/git/postgresql.git
synced 2024-09-30 21:41:18 +02:00
6708e447ef
The logic around shm_mq_detach was a few bricks shy of a load, because (contrary to the comments for shm_mq_attach) all it did was update the shared shm_mq state. That left us leaking a bit of process-local memory, but much worse, the on_dsm_detach callback for shm_mq_detach was still armed. That means that whenever we ultimately detach from the DSM segment, we'd run shm_mq_detach again for already-detached, possibly long-dead queues. This accidentally fails to fail today, because we only ever re-use a shm_mq's memory for another shm_mq, and multiple detach attempts on the last such shm_mq are fairly harmless. But it's gonna bite us someday, so let's clean it up. To do that, change shm_mq_detach's API so it takes a shm_mq_handle not the underlying shm_mq. This makes the callers simpler in most cases anyway. Also fix a few places in parallel.c that were just pfree'ing the handle structs rather than doing proper cleanup. Back-patch to v10 because of the risk that the revenant shm_mq_detach callbacks would cause a live bug sometime. Since this is an API change, it's too late to do it in 9.6. (We could make a variant patch that preserves API, but I'm not excited enough to do that.) Discussion: https://postgr.es/m/8670.1504192177@sss.pgh.pa.us
86 lines
2.6 KiB
C
86 lines
2.6 KiB
C
/*-------------------------------------------------------------------------
|
|
*
|
|
* shm_mq.h
|
|
* single-reader, single-writer shared memory message queue
|
|
*
|
|
* Portions Copyright (c) 1996-2017, PostgreSQL Global Development Group
|
|
* Portions Copyright (c) 1994, Regents of the University of California
|
|
*
|
|
* src/include/storage/shm_mq.h
|
|
*
|
|
*-------------------------------------------------------------------------
|
|
*/
|
|
#ifndef SHM_MQ_H
|
|
#define SHM_MQ_H
|
|
|
|
#include "postmaster/bgworker.h"
|
|
#include "storage/dsm.h"
|
|
#include "storage/proc.h"
|
|
|
|
/* The queue itself, in shared memory. */
|
|
struct shm_mq;
|
|
typedef struct shm_mq shm_mq;
|
|
|
|
/* Backend-private state. */
|
|
struct shm_mq_handle;
|
|
typedef struct shm_mq_handle shm_mq_handle;
|
|
|
|
/* Descriptors for a single write spanning multiple locations. */
|
|
typedef struct
|
|
{
|
|
const char *data;
|
|
Size len;
|
|
} shm_mq_iovec;
|
|
|
|
/* Possible results of a send or receive operation. */
|
|
typedef enum
|
|
{
|
|
SHM_MQ_SUCCESS, /* Sent or received a message. */
|
|
SHM_MQ_WOULD_BLOCK, /* Not completed; retry later. */
|
|
SHM_MQ_DETACHED /* Other process has detached queue. */
|
|
} shm_mq_result;
|
|
|
|
/*
|
|
* Primitives to create a queue and set the sender and receiver.
|
|
*
|
|
* Both the sender and the receiver must be set before any messages are read
|
|
* or written, but they need not be set by the same process. Each must be
|
|
* set exactly once.
|
|
*/
|
|
extern shm_mq *shm_mq_create(void *address, Size size);
|
|
extern void shm_mq_set_receiver(shm_mq *mq, PGPROC *);
|
|
extern void shm_mq_set_sender(shm_mq *mq, PGPROC *);
|
|
|
|
/* Accessor methods for sender and receiver. */
|
|
extern PGPROC *shm_mq_get_receiver(shm_mq *);
|
|
extern PGPROC *shm_mq_get_sender(shm_mq *);
|
|
|
|
/* Set up backend-local queue state. */
|
|
extern shm_mq_handle *shm_mq_attach(shm_mq *mq, dsm_segment *seg,
|
|
BackgroundWorkerHandle *handle);
|
|
|
|
/* Associate worker handle with shm_mq. */
|
|
extern void shm_mq_set_handle(shm_mq_handle *, BackgroundWorkerHandle *);
|
|
|
|
/* Break connection, release handle resources. */
|
|
extern void shm_mq_detach(shm_mq_handle *mqh);
|
|
|
|
/* Get the shm_mq from handle. */
|
|
extern shm_mq *shm_mq_get_queue(shm_mq_handle *mqh);
|
|
|
|
/* Send or receive messages. */
|
|
extern shm_mq_result shm_mq_send(shm_mq_handle *mqh,
|
|
Size nbytes, const void *data, bool nowait);
|
|
extern shm_mq_result shm_mq_sendv(shm_mq_handle *mqh,
|
|
shm_mq_iovec *iov, int iovcnt, bool nowait);
|
|
extern shm_mq_result shm_mq_receive(shm_mq_handle *mqh,
|
|
Size *nbytesp, void **datap, bool nowait);
|
|
|
|
/* Wait for our counterparty to attach to the queue. */
|
|
extern shm_mq_result shm_mq_wait_for_attach(shm_mq_handle *mqh);
|
|
|
|
/* Smallest possible queue. */
|
|
extern PGDLLIMPORT const Size shm_mq_minimum_size;
|
|
|
|
#endif /* SHM_MQ_H */
|