summaryrefslogtreecommitdiffstats
path: root/src/backend/access/rmgrdesc/gindesc.c
blob: ee9e69cdd094e6b958445a97d0c2229295e6c47c (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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
/*-------------------------------------------------------------------------
 *
 * gindesc.c
 *	  rmgr descriptor routines for access/transam/gin/ginxlog.c
 *
 * Portions Copyright (c) 1996-2021, PostgreSQL Global Development Group
 * Portions Copyright (c) 1994, Regents of the University of California
 *
 *
 * IDENTIFICATION
 *	  src/backend/access/rmgrdesc/gindesc.c
 *
 *-------------------------------------------------------------------------
 */
#include "postgres.h"

#include "access/ginxlog.h"
#include "access/xlogutils.h"
#include "lib/stringinfo.h"
#include "storage/relfilenode.h"

static void
desc_recompress_leaf(StringInfo buf, ginxlogRecompressDataLeaf *insertData)
{
	int			i;
	char	   *walbuf = ((char *) insertData) + sizeof(ginxlogRecompressDataLeaf);

	appendStringInfo(buf, " %d segments:", (int) insertData->nactions);

	for (i = 0; i < insertData->nactions; i++)
	{
		uint8		a_segno = *((uint8 *) (walbuf++));
		uint8		a_action = *((uint8 *) (walbuf++));
		uint16		nitems = 0;
		int			newsegsize = 0;

		if (a_action == GIN_SEGMENT_INSERT ||
			a_action == GIN_SEGMENT_REPLACE)
		{
			newsegsize = SizeOfGinPostingList((GinPostingList *) walbuf);
			walbuf += SHORTALIGN(newsegsize);
		}

		if (a_action == GIN_SEGMENT_ADDITEMS)
		{
			memcpy(&nitems, walbuf, sizeof(uint16));
			walbuf += sizeof(uint16);
			walbuf += nitems * sizeof(ItemPointerData);
		}

		switch (a_action)
		{
			case GIN_SEGMENT_ADDITEMS:
				appendStringInfo(buf, " %d (add %d items)", a_segno, nitems);
				break;
			case GIN_SEGMENT_DELETE:
				appendStringInfo(buf, " %d (delete)", a_segno);
				break;
			case GIN_SEGMENT_INSERT:
				appendStringInfo(buf, " %d (insert)", a_segno);
				break;
			case GIN_SEGMENT_REPLACE:
				appendStringInfo(buf, " %d (replace)", a_segno);
				break;
			default:
				appendStringInfo(buf, " %d unknown action %d ???", a_segno, a_action);
				/* cannot decode unrecognized actions further */
				return;
		}
	}
}

void
gin_desc(StringInfo buf, XLogReaderState *record)
{
	char	   *rec = XLogRecGetData(record);
	uint8		info = XLogRecGetInfo(record) & ~XLR_INFO_MASK;

	switch (info)
	{
		case XLOG_GIN_CREATE_PTREE:
			/* no further information */
			break;
		case XLOG_GIN_INSERT:
			{
				ginxlogInsert *xlrec = (ginxlogInsert *) rec;

				appendStringInfo(buf, "isdata: %c isleaf: %c",
								 (xlrec->flags & GIN_INSERT_ISDATA) ? 'T' : 'F',
								 (xlrec->flags & GIN_INSERT_ISLEAF) ? 'T' : 'F');
				if (!(xlrec->flags & GIN_INSERT_ISLEAF))
				{
					char	   *payload = rec + sizeof(ginxlogInsert);
					BlockNumber leftChildBlkno;
					BlockNumber rightChildBlkno;

					leftChildBlkno = BlockIdGetBlockNumber((BlockId) payload);
					payload += sizeof(BlockIdData);
					rightChildBlkno = BlockIdGetBlockNumber((BlockId) payload);
					payload += sizeof(BlockNumber);
					appendStringInfo(buf, " children: %u/%u",
									 leftChildBlkno, rightChildBlkno);
				}
				if (XLogRecHasBlockImage(record, 0))
				{
					if (XLogRecBlockImageApply(record, 0))
						appendStringInfoString(buf, " (full page image)");
					else
						appendStringInfoString(buf, " (full page image, for WAL verification)");
				}
				else
				{
					char	   *payload = XLogRecGetBlockData(record, 0, NULL);

					if (!(xlrec->flags & GIN_INSERT_ISDATA))
						appendStringInfo(buf, " isdelete: %c",
										 (((ginxlogInsertEntry *) payload)->isDelete) ? 'T' : 'F');
					else if (xlrec->flags & GIN_INSERT_ISLEAF)
						desc_recompress_leaf(buf, (ginxlogRecompressDataLeaf *) payload);
					else
					{
						ginxlogInsertDataInternal *insertData =
						(ginxlogInsertDataInternal *) payload;

						appendStringInfo(buf, " pitem: %u-%u/%u",
										 PostingItemGetBlockNumber(&insertData->newitem),
										 ItemPointerGetBlockNumber(&insertData->newitem.key),
										 ItemPointerGetOffsetNumber(&insertData->newitem.key));
					}
				}
			}
			break;
		case XLOG_GIN_SPLIT:
			{
				ginxlogSplit *xlrec = (ginxlogSplit *) rec;

				appendStringInfo(buf, "isrootsplit: %c",
								 (((ginxlogSplit *) rec)->flags & GIN_SPLIT_ROOT) ? 'T' : 'F');
				appendStringInfo(buf, " isdata: %c isleaf: %c",
								 (xlrec->flags & GIN_INSERT_ISDATA) ? 'T' : 'F',
								 (xlrec->flags & GIN_INSERT_ISLEAF) ? 'T' : 'F');
			}
			break;
		case XLOG_GIN_VACUUM_PAGE:
			/* no further information */
			break;
		case XLOG_GIN_VACUUM_DATA_LEAF_PAGE:
			{
				if (XLogRecHasBlockImage(record, 0))
				{
					if (XLogRecBlockImageApply(record, 0))
						appendStringInfoString(buf, " (full page image)");
					else
						appendStringInfoString(buf, " (full page image, for WAL verification)");
				}
				else
				{
					ginxlogVacuumDataLeafPage *xlrec =
					(ginxlogVacuumDataLeafPage *) XLogRecGetBlockData(record, 0, NULL);

					desc_recompress_leaf(buf, &xlrec->data);
				}
			}
			break;
		case XLOG_GIN_DELETE_PAGE:
			/* no further information */
			break;
		case XLOG_GIN_UPDATE_META_PAGE:
			/* no further information */
			break;
		case XLOG_GIN_INSERT_LISTPAGE:
			/* no further information */
			break;
		case XLOG_GIN_DELETE_LISTPAGE:
			appendStringInfo(buf, "ndeleted: %d",
							 ((ginxlogDeleteListPages *) rec)->ndeleted);
			break;
	}
}

const char *
gin_identify(uint8 info)
{
	const char *id = NULL;

	switch (info & ~XLR_INFO_MASK)
	{
		case XLOG_GIN_CREATE_PTREE:
			id = "CREATE_PTREE";
			break;
		case XLOG_GIN_INSERT:
			id = "INSERT";
			break;
		case XLOG_GIN_SPLIT:
			id = "SPLIT";
			break;
		case XLOG_GIN_VACUUM_PAGE:
			id = "VACUUM_PAGE";
			break;
		case XLOG_GIN_VACUUM_DATA_LEAF_PAGE:
			id = "VACUUM_DATA_LEAF_PAGE";
			break;
		case XLOG_GIN_DELETE_PAGE:
			id = "DELETE_PAGE";
			break;
		case XLOG_GIN_UPDATE_META_PAGE:
			id = "UPDATE_META_PAGE";
			break;
		case XLOG_GIN_INSERT_LISTPAGE:
			id = "INSERT_LISTPAGE";
			break;
		case XLOG_GIN_DELETE_LISTPAGE:
			id = "DELETE_LISTPAGE";
			break;
	}

	return id;
}