sinval.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /*-------------------------------------------------------------------------
  2. *
  3. * sinval.h
  4. * POSTGRES shared cache invalidation communication definitions.
  5. *
  6. *
  7. * Portions Copyright (c) 1996-2016, PostgreSQL Global Development Group
  8. * Portions Copyright (c) 1994, Regents of the University of California
  9. *
  10. * src/include/storage/sinval.h
  11. *
  12. *-------------------------------------------------------------------------
  13. */
  14. #ifndef SINVAL_H
  15. #define SINVAL_H
  16. #include <signal.h>
  17. #include "storage/relfilenode.h"
  18. /*
  19. * We support several types of shared-invalidation messages:
  20. * * invalidate a specific tuple in a specific catcache
  21. * * invalidate all catcache entries from a given system catalog
  22. * * invalidate a relcache entry for a specific logical relation
  23. * * invalidate an smgr cache entry for a specific physical relation
  24. * * invalidate the mapped-relation mapping for a given database
  25. * * invalidate any saved snapshot that might be used to scan a given relation
  26. * More types could be added if needed. The message type is identified by
  27. * the first "int8" field of the message struct. Zero or positive means a
  28. * specific-catcache inval message (and also serves as the catcache ID field).
  29. * Negative values identify the other message types, as per codes below.
  30. *
  31. * Catcache inval events are initially driven by detecting tuple inserts,
  32. * updates and deletions in system catalogs (see CacheInvalidateHeapTuple).
  33. * An update can generate two inval events, one for the old tuple and one for
  34. * the new, but this is reduced to one event if the tuple's hash key doesn't
  35. * change. Note that the inval events themselves don't actually say whether
  36. * the tuple is being inserted or deleted. Also, since we transmit only a
  37. * hash key, there is a small risk of unnecessary invalidations due to chance
  38. * matches of hash keys.
  39. *
  40. * Note that some system catalogs have multiple caches on them (with different
  41. * indexes). On detecting a tuple invalidation in such a catalog, separate
  42. * catcache inval messages must be generated for each of its caches, since
  43. * the hash keys will generally be different.
  44. *
  45. * Catcache, relcache, and snapshot invalidations are transactional, and so
  46. * are sent to other backends upon commit. Internally to the generating
  47. * backend, they are also processed at CommandCounterIncrement so that later
  48. * commands in the same transaction see the new state. The generating backend
  49. * also has to process them at abort, to flush out any cache state it's loaded
  50. * from no-longer-valid entries.
  51. *
  52. * smgr and relation mapping invalidations are non-transactional: they are
  53. * sent immediately when the underlying file change is made.
  54. */
  55. typedef struct
  56. {
  57. int8 id; /* cache ID --- must be first */
  58. Oid dbId; /* database ID, or 0 if a shared relation */
  59. uint32 hashValue; /* hash value of key for this catcache */
  60. } SharedInvalCatcacheMsg;
  61. #define SHAREDINVALCATALOG_ID (-1)
  62. typedef struct
  63. {
  64. int8 id; /* type field --- must be first */
  65. Oid dbId; /* database ID, or 0 if a shared catalog */
  66. Oid catId; /* ID of catalog whose contents are invalid */
  67. } SharedInvalCatalogMsg;
  68. #define SHAREDINVALRELCACHE_ID (-2)
  69. typedef struct
  70. {
  71. int8 id; /* type field --- must be first */
  72. Oid dbId; /* database ID, or 0 if a shared relation */
  73. Oid relId; /* relation ID */
  74. } SharedInvalRelcacheMsg;
  75. #define SHAREDINVALSMGR_ID (-3)
  76. typedef struct
  77. {
  78. /* note: field layout chosen to pack into 16 bytes */
  79. int8 id; /* type field --- must be first */
  80. int8 backend_hi; /* high bits of backend ID, if temprel */
  81. uint16 backend_lo; /* low bits of backend ID, if temprel */
  82. RelFileNode rnode; /* spcNode, dbNode, relNode */
  83. } SharedInvalSmgrMsg;
  84. #define SHAREDINVALRELMAP_ID (-4)
  85. typedef struct
  86. {
  87. int8 id; /* type field --- must be first */
  88. Oid dbId; /* database ID, or 0 for shared catalogs */
  89. } SharedInvalRelmapMsg;
  90. #define SHAREDINVALSNAPSHOT_ID (-5)
  91. typedef struct
  92. {
  93. int8 id; /* type field --- must be first */
  94. Oid dbId; /* database ID, or 0 if a shared relation */
  95. Oid relId; /* relation ID */
  96. } SharedInvalSnapshotMsg;
  97. typedef union
  98. {
  99. int8 id; /* type field --- must be first */
  100. SharedInvalCatcacheMsg cc;
  101. SharedInvalCatalogMsg cat;
  102. SharedInvalRelcacheMsg rc;
  103. SharedInvalSmgrMsg sm;
  104. SharedInvalRelmapMsg rm;
  105. SharedInvalSnapshotMsg sn;
  106. } SharedInvalidationMessage;
  107. /* Counter of messages processed; don't worry about overflow. */
  108. extern uint64 SharedInvalidMessageCounter;
  109. extern volatile sig_atomic_t catchupInterruptPending;
  110. extern void SendSharedInvalidMessages(const SharedInvalidationMessage *msgs,
  111. int n);
  112. extern void ReceiveSharedInvalidMessages(
  113. void (*invalFunction) (SharedInvalidationMessage *msg),
  114. void (*resetFunction) (void));
  115. /* signal handler for catchup events (PROCSIG_CATCHUP_INTERRUPT) */
  116. extern void HandleCatchupInterrupt(void);
  117. /*
  118. * enable/disable processing of catchup events directly from signal handler.
  119. * The enable routine first performs processing of any catchup events that
  120. * have occurred since the last disable.
  121. */
  122. extern void ProcessCatchupInterrupt(void);
  123. extern int xactGetCommittedInvalidationMessages(SharedInvalidationMessage **msgs,
  124. bool *RelcacheInitFileInval);
  125. extern void ProcessCommittedInvalidationMessages(SharedInvalidationMessage *msgs,
  126. int nmsgs, bool RelcacheInitFileInval,
  127. Oid dbid, Oid tsid);
  128. extern void LocalExecuteInvalidationMessage(SharedInvalidationMessage *msg);
  129. #endif /* SINVAL_H */