sinval.h 5.5 KB

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