aboutsummaryrefslogtreecommitdiff
path: root/src/backend/storage/ipc/sinval.c
blob: 220607ac050a816f7af5e93ea5400ff939b9689c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/*-------------------------------------------------------------------------
 *
 * sinval.c
 *	  POSTGRES shared cache invalidation communication code.
 *
 * Copyright (c) 1994, Regents of the University of California
 *
 *
 * IDENTIFICATION
 *	  $Header: /cvsroot/pgsql/src/backend/storage/ipc/sinval.c,v 1.15 1999/05/28 17:03:29 tgl Exp $
 *
 *-------------------------------------------------------------------------
 */
/* #define INVALIDDEBUG 1 */

#include <sys/types.h>

#include "postgres.h"

#include "storage/backendid.h"
#include "storage/sinval.h"
#include "storage/sinvaladt.h"
#include "storage/spin.h"

extern SISeg *shmInvalBuffer;	/* the shared buffer segment, set by */

 /* SISegmentAttach()			   */
extern BackendId MyBackendId;
extern BackendTag MyBackendTag;

SPINLOCK	SInvalLock = (SPINLOCK) NULL;

/****************************************************************************/
/*	CreateSharedInvalidationState()		 Create a buffer segment			*/
/*																			*/
/*	should be called only by the POSTMASTER									*/
/****************************************************************************/
void
CreateSharedInvalidationState(IPCKey key, int maxBackends)
{
	int			status;

	/*
	 * REMOVED SISyncKill(IPCKeyGetSIBufferMemorySemaphoreKey(key));
	 * SISyncInit(IPCKeyGetSIBufferMemorySemaphoreKey(key));
	 */

	/* SInvalLock gets set in spin.c, during spinlock init */
	status = SISegmentInit(true, IPCKeyGetSIBufferMemoryBlock(key),
						   maxBackends);

	if (status == -1)
		elog(FATAL, "CreateSharedInvalidationState: failed segment init");
}

/****************************************************************************/
/*	AttachSharedInvalidationState(key)	 Attach a buffer segment			*/
/*																			*/
/*	should be called only by the POSTMASTER									*/
/****************************************************************************/
void
AttachSharedInvalidationState(IPCKey key)
{
	int			status;

	if (key == PrivateIPCKey)
	{
		CreateSharedInvalidationState(key, 16);
		return;
	}
	/* SInvalLock gets set in spin.c, during spinlock init */
	status = SISegmentInit(false, IPCKeyGetSIBufferMemoryBlock(key), 0);

	if (status == -1)
		elog(FATAL, "AttachSharedInvalidationState: failed segment init");
}

void
InitSharedInvalidationState(void)
{
	SpinAcquire(SInvalLock);
	if (!SIBackendInit(shmInvalBuffer))
	{
		SpinRelease(SInvalLock);
		elog(FATAL, "Backend cache invalidation initialization failed");
	}
	SpinRelease(SInvalLock);
}

/*
 * RegisterSharedInvalid
 *	Returns a new local cache invalidation state containing a new entry.
 *
 * Note:
 *	Assumes hash index is valid.
 *	Assumes item pointer is valid.
 */
/****************************************************************************/
/*	RegisterSharedInvalid(cacheId, hashIndex, pointer)						*/
/*																			*/
/*	register a message in the buffer										*/
/*	should be called by a backend											*/
/****************************************************************************/
void
RegisterSharedInvalid(int cacheId,		/* XXX */
					  Index hashIndex,
					  ItemPointer pointer)
{
	SharedInvalidData newInvalid;

	/*
	 * This code has been hacked to accept two types of messages.  This
	 * might be treated more generally in the future.
	 *
	 * (1) cacheId= system cache id hashIndex= system cache hash index for a
	 * (possibly) cached tuple pointer= pointer of (possibly) cached tuple
	 *
	 * (2) cacheId= special non-syscache id hashIndex= object id contained in
	 * (possibly) cached relation descriptor pointer= null
	 */

	newInvalid.cacheId = cacheId;
	newInvalid.hashIndex = hashIndex;

	if (ItemPointerIsValid(pointer))
		ItemPointerCopy(pointer, &newInvalid.pointerData);
	else
		ItemPointerSetInvalid(&newInvalid.pointerData);

	SpinAcquire(SInvalLock);
	if (!SISetDataEntry(shmInvalBuffer, &newInvalid))
	{
		/* buffer full */
		/* release a message, mark process cache states to be invalid */
		SISetProcStateInvalid(shmInvalBuffer);

		if (!SIDelDataEntry(shmInvalBuffer))
		{
			/* inconsistent buffer state -- shd never happen */
			SpinRelease(SInvalLock);
			elog(FATAL, "RegisterSharedInvalid: inconsistent buffer state");
		}

		/* write again */
		SISetDataEntry(shmInvalBuffer, &newInvalid);
	}
	SpinRelease(SInvalLock);
}

/*
 * InvalidateSharedInvalid
 *	Processes all entries in a shared cache invalidation state.
 */
/****************************************************************************/
/*	InvalidateSharedInvalid(invalFunction, resetFunction)					*/
/*																			*/
/*	invalidate a message in the buffer	 (read and clean up)				*/
/*	should be called by a backend											*/
/****************************************************************************/
void
			InvalidateSharedInvalid(void (*invalFunction) (),
									void (*resetFunction) ())
{
	SpinAcquire(SInvalLock);
	SIReadEntryData(shmInvalBuffer, MyBackendId,
					invalFunction, resetFunction);

	SIDelExpiredDataEntries(shmInvalBuffer);
	SpinRelease(SInvalLock);
}