OSDN Git Service

ae55f2c7281cc0fd9b205c2f87a766541e6f29c6
[pg-rex/syncrep.git] / src / include / storage / sinval.h
1 /*-------------------------------------------------------------------------
2  *
3  * sinval.h
4  *        POSTGRES shared cache invalidation communication definitions.
5  *
6  *
7  * Portions Copyright (c) 1996-2004, PostgreSQL Global Development Group
8  * Portions Copyright (c) 1994, Regents of the University of California
9  *
10  * $PostgreSQL: pgsql/src/include/storage/sinval.h,v 1.37 2004/08/29 04:13:10 momjian Exp $
11  *
12  *-------------------------------------------------------------------------
13  */
14 #ifndef SINVAL_H
15 #define SINVAL_H
16
17 #include "storage/backendid.h"
18 #include "storage/itemptr.h"
19 #include "storage/relfilenode.h"
20
21
22 /*
23  * We currently support two types of shared-invalidation messages: one that
24  * invalidates an entry in a catcache, and one that invalidates a relcache
25  * entry.  More types could be added if needed.  The message type is
26  * identified by the first "int16" field of the message struct.  Zero or
27  * positive means a catcache inval message (and also serves as the catcache
28  * ID field).  -1 means a relcache inval message.  Other negative values
29  * are available to identify other inval message types.
30  *
31  * Relcache invalidation messages usually also cause invalidation of entries
32  * in the smgr's relation cache.  This means they must carry both logical
33  * and physical relation ID info (ie, both dbOID/relOID and RelFileNode).
34  * In some cases RelFileNode information is not available so the sender fills
35  * those fields with zeroes --- this is okay so long as no smgr cache flush
36  * is required.
37  *
38  * Shared-inval events are initially driven by detecting tuple inserts,
39  * updates and deletions in system catalogs (see CacheInvalidateHeapTuple).
40  * An update generates two inval events, one for the old tuple and one for
41  * the new --- this is needed to get rid of both positive entries for the
42  * old tuple, and negative cache entries associated with the new tuple's
43  * cache key.  (This could perhaps be optimized down to one event when the
44  * cache key is not changing, but for now we don't bother to try.)  Note that
45  * the inval events themselves don't actually say whether the tuple is being
46  * inserted or deleted.
47  *
48  * Note that some system catalogs have multiple caches on them (with different
49  * indexes).  On detecting a tuple invalidation in such a catalog, separate
50  * catcache inval messages must be generated for each of its caches.  The
51  * catcache inval messages carry the hash value for the target tuple, so
52  * that the catcache only needs to search one hash chain not all its chains,
53  * and so that negative cache entries can be recognized with good accuracy.
54  * (Of course this assumes that all the backends are using identical hashing
55  * code, but that should be OK.)
56  */
57
58 typedef struct
59 {
60         /* note: field layout chosen with an eye to alignment concerns */
61         int16           id;                             /* cache ID --- must be first */
62         ItemPointerData tuplePtr;       /* tuple identifier in cached relation */
63         Oid                     dbId;                   /* database ID, or 0 if a shared relation */
64         uint32          hashValue;              /* hash value of key for this catcache */
65 } SharedInvalCatcacheMsg;
66
67 #define SHAREDINVALRELCACHE_ID  (-1)
68
69 typedef struct
70 {
71         int16           id;                             /* type field --- must be first */
72         Oid                     dbId;                   /* database ID, or 0 if a shared relation */
73         Oid                     relId;                  /* relation ID */
74         RelFileNode     physId;                 /* physical file ID */
75         /*
76          * Note: it is likely that RelFileNode will someday be changed to
77          * include database ID.  In that case the dbId field will be redundant
78          * and should be removed to save space.
79          */
80 } SharedInvalRelcacheMsg;
81
82 typedef union
83 {
84         int16           id;                             /* type field --- must be first */
85         SharedInvalCatcacheMsg cc;
86         SharedInvalRelcacheMsg rc;
87 } SharedInvalidationMessage;
88
89
90 extern int      SInvalShmemSize(int maxBackends);
91 extern void CreateSharedInvalidationState(int maxBackends);
92 extern void InitBackendSharedInvalidationState(void);
93 extern void SendSharedInvalidMessage(SharedInvalidationMessage *msg);
94 extern void ReceiveSharedInvalidMessages(
95                                   void (*invalFunction) (SharedInvalidationMessage *msg),
96                                                          void (*resetFunction) (void));
97
98 extern bool DatabaseHasActiveBackends(Oid databaseId, bool ignoreMyself);
99 extern bool TransactionIdIsInProgress(TransactionId xid);
100 extern bool IsBackendPid(int pid);
101 extern TransactionId GetOldestXmin(bool allDbs);
102 extern int      CountActiveBackends(void);
103 extern int      CountEmptyBackendSlots(void);
104 /* Use "struct PGPROC", not PGPROC, to avoid including proc.h here */
105 extern struct PGPROC *BackendIdGetProc(BackendId procId);
106
107 extern void XidCacheRemoveRunningXids(TransactionId xid,
108                                                                           int nxids, TransactionId *xids);
109
110 /* signal handler for catchup events (SIGUSR1) */
111 extern void CatchupInterruptHandler(SIGNAL_ARGS);
112
113 /*
114  * enable/disable processing of catchup events directly from signal handler.
115  * The enable routine first performs processing of any catchup events that
116  * have occurred since the last disable.
117  */
118 extern void EnableCatchupInterrupt(void);
119 extern bool DisableCatchupInterrupt(void);
120
121 #endif   /* SINVAL_H */