PostgreSQL Source Code git master
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
hashfuncs.c File Reference
#include "postgres.h"
#include "access/hash.h"
#include "access/htup_details.h"
#include "access/relation.h"
#include "catalog/pg_am.h"
#include "catalog/pg_type.h"
#include "funcapi.h"
#include "miscadmin.h"
#include "pageinspect.h"
#include "utils/array.h"
#include "utils/builtins.h"
#include "utils/rel.h"
Include dependency graph for hashfuncs.c:

Go to the source code of this file.

Data Structures

struct  HashPageStat
 
struct  user_args
 

Macros

#define IS_INDEX(r)   ((r)->rd_rel->relkind == RELKIND_INDEX)
 
#define IS_HASH(r)   ((r)->rd_rel->relam == HASH_AM_OID)
 

Typedefs

typedef struct HashPageStat HashPageStat
 

Functions

 PG_FUNCTION_INFO_V1 (hash_page_type)
 
 PG_FUNCTION_INFO_V1 (hash_page_stats)
 
 PG_FUNCTION_INFO_V1 (hash_page_items)
 
 PG_FUNCTION_INFO_V1 (hash_bitmap_info)
 
 PG_FUNCTION_INFO_V1 (hash_metapage_info)
 
static Page verify_hash_page (bytea *raw_page, int flags)
 
static void GetHashPageStatistics (Page page, HashPageStat *stat)
 
Datum hash_page_type (PG_FUNCTION_ARGS)
 
Datum hash_page_stats (PG_FUNCTION_ARGS)
 
Datum hash_page_items (PG_FUNCTION_ARGS)
 
Datum hash_bitmap_info (PG_FUNCTION_ARGS)
 
Datum hash_metapage_info (PG_FUNCTION_ARGS)
 

Macro Definition Documentation

◆ IS_HASH

#define IS_HASH (   r)    ((r)->rd_rel->relam == HASH_AM_OID)

Definition at line 32 of file hashfuncs.c.

◆ IS_INDEX

#define IS_INDEX (   r)    ((r)->rd_rel->relkind == RELKIND_INDEX)

Definition at line 31 of file hashfuncs.c.

Typedef Documentation

◆ HashPageStat

typedef struct HashPageStat HashPageStat

Function Documentation

◆ GetHashPageStatistics()

static void GetHashPageStatistics ( Page  page,
HashPageStat stat 
)
static

Definition at line 156 of file hashfuncs.c.

157{
159 HashPageOpaque opaque = HashPageGetOpaque(page);
160 int off;
161
162 stat->dead_items = stat->live_items = 0;
163 stat->page_size = PageGetPageSize(page);
164
165 /* hash page opaque data */
166 stat->hasho_prevblkno = opaque->hasho_prevblkno;
167 stat->hasho_nextblkno = opaque->hasho_nextblkno;
168 stat->hasho_bucket = opaque->hasho_bucket;
169 stat->hasho_flag = opaque->hasho_flag;
170 stat->hasho_page_id = opaque->hasho_page_id;
171
172 /* count live and dead tuples, and free space */
173 for (off = FirstOffsetNumber; off <= maxoff; off++)
174 {
175 ItemId id = PageGetItemId(page, off);
176
177 if (!ItemIdIsDead(id))
178 stat->live_items++;
179 else
180 stat->dead_items++;
181 }
182 stat->free_size = PageGetFreeSpace(page);
183}
Size PageGetFreeSpace(Page page)
Definition: bufpage.c:896
static Size PageGetPageSize(Page page)
Definition: bufpage.h:276
static ItemId PageGetItemId(Page page, OffsetNumber offsetNumber)
Definition: bufpage.h:243
static OffsetNumber PageGetMaxOffsetNumber(Page page)
Definition: bufpage.h:372
#define HashPageGetOpaque(page)
Definition: hash.h:88
#define ItemIdIsDead(itemId)
Definition: itemid.h:113
uint16 OffsetNumber
Definition: off.h:24
#define FirstOffsetNumber
Definition: off.h:27
BlockNumber hasho_nextblkno
Definition: hash.h:80
uint16 hasho_flag
Definition: hash.h:82
BlockNumber hasho_prevblkno
Definition: hash.h:79
uint16 hasho_page_id
Definition: hash.h:83
Bucket hasho_bucket
Definition: hash.h:81

References FirstOffsetNumber, HashPageOpaqueData::hasho_bucket, HashPageOpaqueData::hasho_flag, HashPageOpaqueData::hasho_nextblkno, HashPageOpaqueData::hasho_page_id, HashPageOpaqueData::hasho_prevblkno, HashPageGetOpaque, ItemIdIsDead, PageGetFreeSpace(), PageGetItemId(), PageGetMaxOffsetNumber(), and PageGetPageSize().

Referenced by hash_page_stats().

◆ hash_bitmap_info()

Datum hash_bitmap_info ( PG_FUNCTION_ARGS  )

Definition at line 391 of file hashfuncs.c.

392{
393 Oid indexRelid = PG_GETARG_OID(0);
394 int64 ovflblkno = PG_GETARG_INT64(1);
395 HashMetaPage metap;
396 Buffer metabuf,
397 mapbuf;
398 BlockNumber bitmapblkno;
399 Page mappage;
400 bool bit = false;
401 TupleDesc tupleDesc;
402 Relation indexRel;
403 uint32 ovflbitno;
404 int32 bitmappage,
405 bitmapbit;
406 HeapTuple tuple;
407 int i,
408 j;
409 Datum values[3];
410 bool nulls[3] = {0};
411 uint32 *freep;
412
413 if (!superuser())
415 (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
416 errmsg("must be superuser to use raw page functions")));
417
418 indexRel = relation_open(indexRelid, AccessShareLock);
419
420 if (!IS_INDEX(indexRel) || !IS_HASH(indexRel))
422 (errcode(ERRCODE_WRONG_OBJECT_TYPE),
423 errmsg("\"%s\" is not a %s index",
424 RelationGetRelationName(indexRel), "hash")));
425
426 if (RELATION_IS_OTHER_TEMP(indexRel))
428 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
429 errmsg("cannot access temporary tables of other sessions")));
430
431 if (ovflblkno < 0 || ovflblkno > MaxBlockNumber)
433 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
434 errmsg("invalid block number")));
435
436 if (ovflblkno >= RelationGetNumberOfBlocks(indexRel))
438 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
439 errmsg("block number %lld is out of range for relation \"%s\"",
440 (long long int) ovflblkno, RelationGetRelationName(indexRel))));
441
442 /* Read the metapage so we can determine which bitmap page to use */
443 metabuf = _hash_getbuf(indexRel, HASH_METAPAGE, HASH_READ, LH_META_PAGE);
444 metap = HashPageGetMeta(BufferGetPage(metabuf));
445
446 /*
447 * Reject attempt to read the bit for a metapage or bitmap page; this is
448 * only meaningful for overflow pages.
449 */
450 if (ovflblkno == 0)
452 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
453 errmsg("invalid overflow block number %u",
454 (BlockNumber) ovflblkno)));
455 for (i = 0; i < metap->hashm_nmaps; i++)
456 if (metap->hashm_mapp[i] == ovflblkno)
458 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
459 errmsg("invalid overflow block number %u",
460 (BlockNumber) ovflblkno)));
461
462 /*
463 * Identify overflow bit number. This will error out for primary bucket
464 * pages, and we've already rejected the metapage and bitmap pages above.
465 */
466 ovflbitno = _hash_ovflblkno_to_bitno(metap, (BlockNumber) ovflblkno);
467
468 bitmappage = ovflbitno >> BMPG_SHIFT(metap);
469 bitmapbit = ovflbitno & BMPG_MASK(metap);
470
471 if (bitmappage >= metap->hashm_nmaps)
473 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
474 errmsg("invalid overflow block number %u",
475 (BlockNumber) ovflblkno)));
476
477 bitmapblkno = metap->hashm_mapp[bitmappage];
478
479 _hash_relbuf(indexRel, metabuf);
480
481 /* Check the status of bitmap bit for overflow page */
482 mapbuf = _hash_getbuf(indexRel, bitmapblkno, HASH_READ, LH_BITMAP_PAGE);
483 mappage = BufferGetPage(mapbuf);
484 freep = HashPageGetBitmap(mappage);
485
486 bit = ISSET(freep, bitmapbit) != 0;
487
488 _hash_relbuf(indexRel, mapbuf);
489 index_close(indexRel, AccessShareLock);
490
491 /* Build a tuple descriptor for our result type */
492 if (get_call_result_type(fcinfo, NULL, &tupleDesc) != TYPEFUNC_COMPOSITE)
493 elog(ERROR, "return type must be a row type");
494 tupleDesc = BlessTupleDesc(tupleDesc);
495
496 j = 0;
497 values[j++] = Int64GetDatum((int64) bitmapblkno);
498 values[j++] = Int32GetDatum(bitmapbit);
499 values[j++] = BoolGetDatum(bit);
500
501 tuple = heap_form_tuple(tupleDesc, values, nulls);
502
504}
uint32 BlockNumber
Definition: block.h:31
#define MaxBlockNumber
Definition: block.h:35
static Datum values[MAXATTR]
Definition: bootstrap.c:151
int Buffer
Definition: buf.h:23
#define RelationGetNumberOfBlocks(reln)
Definition: bufmgr.h:273
static Page BufferGetPage(Buffer buffer)
Definition: bufmgr.h:400
Pointer Page
Definition: bufpage.h:81
int64_t int64
Definition: c.h:482
int32_t int32
Definition: c.h:481
uint32_t uint32
Definition: c.h:485
int errcode(int sqlerrcode)
Definition: elog.c:853
int errmsg(const char *fmt,...)
Definition: elog.c:1070
#define ERROR
Definition: elog.h:39
#define elog(elevel,...)
Definition: elog.h:225
#define ereport(elevel,...)
Definition: elog.h:149
TupleDesc BlessTupleDesc(TupleDesc tupdesc)
Definition: execTuples.c:2258
Datum Int64GetDatum(int64 X)
Definition: fmgr.c:1807
#define PG_GETARG_OID(n)
Definition: fmgr.h:275
#define PG_GETARG_INT64(n)
Definition: fmgr.h:283
#define PG_RETURN_DATUM(x)
Definition: fmgr.h:353
TypeFuncClass get_call_result_type(FunctionCallInfo fcinfo, Oid *resultTypeId, TupleDesc *resultTupleDesc)
Definition: funcapi.c:276
@ TYPEFUNC_COMPOSITE
Definition: funcapi.h:149
static Datum HeapTupleGetDatum(const HeapTupleData *tuple)
Definition: funcapi.h:230
#define BMPG_MASK(metap)
Definition: hash.h:314
#define HashPageGetBitmap(page)
Definition: hash.h:316
#define LH_META_PAGE
Definition: hash.h:57
#define ISSET(A, N)
Definition: hash.h:334
#define HashPageGetMeta(page)
Definition: hash.h:323
#define HASH_READ
Definition: hash.h:339
#define HASH_METAPAGE
Definition: hash.h:198
#define LH_BITMAP_PAGE
Definition: hash.h:56
#define BMPG_SHIFT(metap)
Definition: hash.h:313
#define IS_HASH(r)
Definition: hashfuncs.c:32
#define IS_INDEX(r)
Definition: hashfuncs.c:31
uint32 _hash_ovflblkno_to_bitno(HashMetaPage metap, BlockNumber ovflblkno)
Definition: hashovfl.c:62
void _hash_relbuf(Relation rel, Buffer buf)
Definition: hashpage.c:266
Buffer _hash_getbuf(Relation rel, BlockNumber blkno, int access, int flags)
Definition: hashpage.c:70
HeapTuple heap_form_tuple(TupleDesc tupleDescriptor, const Datum *values, const bool *isnull)
Definition: heaptuple.c:1117
void index_close(Relation relation, LOCKMODE lockmode)
Definition: indexam.c:177
int j
Definition: isn.c:73
int i
Definition: isn.c:72
#define AccessShareLock
Definition: lockdefs.h:36
uintptr_t Datum
Definition: postgres.h:64
static Datum BoolGetDatum(bool X)
Definition: postgres.h:102
static Datum Int32GetDatum(int32 X)
Definition: postgres.h:212
unsigned int Oid
Definition: postgres_ext.h:31
#define RelationGetRelationName(relation)
Definition: rel.h:539
#define RELATION_IS_OTHER_TEMP(relation)
Definition: rel.h:658
Relation relation_open(Oid relationId, LOCKMODE lockmode)
Definition: relation.c:47
BlockNumber hashm_mapp[HASH_MAX_BITMAPS]
Definition: hash.h:264
uint32 hashm_nmaps
Definition: hash.h:260
bool superuser(void)
Definition: superuser.c:46
Datum bit(PG_FUNCTION_ARGS)
Definition: varbit.c:391

References _hash_getbuf(), _hash_ovflblkno_to_bitno(), _hash_relbuf(), AccessShareLock, bit(), BlessTupleDesc(), BMPG_MASK, BMPG_SHIFT, BoolGetDatum(), BufferGetPage(), elog, ereport, errcode(), errmsg(), ERROR, get_call_result_type(), HASH_METAPAGE, HASH_READ, HashMetaPageData::hashm_mapp, HashMetaPageData::hashm_nmaps, HashPageGetBitmap, HashPageGetMeta, heap_form_tuple(), HeapTupleGetDatum(), i, index_close(), Int32GetDatum(), Int64GetDatum(), IS_HASH, IS_INDEX, ISSET, j, LH_BITMAP_PAGE, LH_META_PAGE, MaxBlockNumber, PG_GETARG_INT64, PG_GETARG_OID, PG_RETURN_DATUM, RELATION_IS_OTHER_TEMP, relation_open(), RelationGetNumberOfBlocks, RelationGetRelationName, superuser(), TYPEFUNC_COMPOSITE, and values.

◆ hash_metapage_info()

Datum hash_metapage_info ( PG_FUNCTION_ARGS  )

Definition at line 515 of file hashfuncs.c.

516{
517 bytea *raw_page = PG_GETARG_BYTEA_P(0);
518 Page page;
519 HashMetaPageData *metad;
520 TupleDesc tupleDesc;
521 HeapTuple tuple;
522 int i,
523 j;
524 Datum values[16];
525 bool nulls[16] = {0};
528
529 if (!superuser())
531 (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
532 errmsg("must be superuser to use raw page functions")));
533
534 page = verify_hash_page(raw_page, LH_META_PAGE);
535
536 /* Build a tuple descriptor for our result type */
537 if (get_call_result_type(fcinfo, NULL, &tupleDesc) != TYPEFUNC_COMPOSITE)
538 elog(ERROR, "return type must be a row type");
539 tupleDesc = BlessTupleDesc(tupleDesc);
540
541 metad = HashPageGetMeta(page);
542
543 j = 0;
544 values[j++] = Int64GetDatum((int64) metad->hashm_magic);
548 values[j++] = Int32GetDatum((int32) metad->hashm_bsize);
549 values[j++] = Int32GetDatum((int32) metad->hashm_bmsize);
556 values[j++] = Int64GetDatum((int64) metad->hashm_nmaps);
558
559 for (i = 0; i < HASH_MAX_SPLITPOINTS; i++)
560 spares[i] = Int64GetDatum((int64) metad->hashm_spares[i]);
562
563 for (i = 0; i < HASH_MAX_BITMAPS; i++)
564 mapp[i] = Int64GetDatum((int64) metad->hashm_mapp[i]);
566
567 tuple = heap_form_tuple(tupleDesc, values, nulls);
568
570}
ArrayType * construct_array_builtin(Datum *elems, int nelems, Oid elmtype)
Definition: arrayfuncs.c:3381
Datum Float8GetDatum(float8 X)
Definition: fmgr.c:1816
#define PG_GETARG_BYTEA_P(n)
Definition: fmgr.h:335
#define HASH_MAX_BITMAPS
Definition: hash.h:230
#define HASH_MAX_SPLITPOINTS
Definition: hash.h:239
static Page verify_hash_page(bytea *raw_page, int flags)
Definition: hashfuncs.c:59
static Datum PointerGetDatum(const void *X)
Definition: postgres.h:322
static Datum ObjectIdGetDatum(Oid X)
Definition: postgres.h:252
uint32 hashm_version
Definition: hash.h:247
uint32 hashm_lowmask
Definition: hash.h:256
uint32 hashm_maxbucket
Definition: hash.h:254
RegProcedure hashm_procid
Definition: hash.h:261
uint32 hashm_spares[HASH_MAX_SPLITPOINTS]
Definition: hash.h:262
double hashm_ntuples
Definition: hash.h:248
uint32 hashm_firstfree
Definition: hash.h:259
uint16 hashm_bmsize
Definition: hash.h:251
uint16 hashm_bsize
Definition: hash.h:250
uint32 hashm_ovflpoint
Definition: hash.h:257
uint32 hashm_highmask
Definition: hash.h:255
uint32 hashm_magic
Definition: hash.h:246
uint16 hashm_bmshift
Definition: hash.h:253
uint16 hashm_ffactor
Definition: hash.h:249
Definition: c.h:641

References BlessTupleDesc(), construct_array_builtin(), elog, ereport, errcode(), errmsg(), ERROR, Float8GetDatum(), get_call_result_type(), HASH_MAX_BITMAPS, HASH_MAX_SPLITPOINTS, HashMetaPageData::hashm_bmshift, HashMetaPageData::hashm_bmsize, HashMetaPageData::hashm_bsize, HashMetaPageData::hashm_ffactor, HashMetaPageData::hashm_firstfree, HashMetaPageData::hashm_highmask, HashMetaPageData::hashm_lowmask, HashMetaPageData::hashm_magic, HashMetaPageData::hashm_mapp, HashMetaPageData::hashm_maxbucket, HashMetaPageData::hashm_nmaps, HashMetaPageData::hashm_ntuples, HashMetaPageData::hashm_ovflpoint, HashMetaPageData::hashm_procid, HashMetaPageData::hashm_spares, HashMetaPageData::hashm_version, HashPageGetMeta, heap_form_tuple(), HeapTupleGetDatum(), i, Int32GetDatum(), Int64GetDatum(), j, LH_META_PAGE, ObjectIdGetDatum(), user_args::page, PG_GETARG_BYTEA_P, PG_RETURN_DATUM, PointerGetDatum(), superuser(), TYPEFUNC_COMPOSITE, values, and verify_hash_page().

◆ hash_page_items()

Datum hash_page_items ( PG_FUNCTION_ARGS  )

Definition at line 300 of file hashfuncs.c.

301{
302 bytea *raw_page = PG_GETARG_BYTEA_P(0);
303 Page page;
304 Datum result;
305 Datum values[3];
306 bool nulls[3] = {0};
307 uint32 hashkey;
308 HeapTuple tuple;
309 FuncCallContext *fctx;
310 MemoryContext mctx;
311 struct user_args *uargs;
312
313 if (!superuser())
315 (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
316 errmsg("must be superuser to use raw page functions")));
317
318 if (SRF_IS_FIRSTCALL())
319 {
320 TupleDesc tupleDesc;
321
322 fctx = SRF_FIRSTCALL_INIT();
323
325
327
328 uargs = palloc(sizeof(struct user_args));
329
330 uargs->page = page;
331
332 uargs->offset = FirstOffsetNumber;
333
334 fctx->max_calls = PageGetMaxOffsetNumber(uargs->page);
335
336 /* Build a tuple descriptor for our result type */
337 if (get_call_result_type(fcinfo, NULL, &tupleDesc) != TYPEFUNC_COMPOSITE)
338 elog(ERROR, "return type must be a row type");
339 tupleDesc = BlessTupleDesc(tupleDesc);
340
341 fctx->attinmeta = TupleDescGetAttInMetadata(tupleDesc);
342
343 fctx->user_fctx = uargs;
344
346 }
347
348 fctx = SRF_PERCALL_SETUP();
349 uargs = fctx->user_fctx;
350
351 if (fctx->call_cntr < fctx->max_calls)
352 {
353 ItemId id;
354 IndexTuple itup;
355 int j;
356
357 id = PageGetItemId(uargs->page, uargs->offset);
358
359 if (!ItemIdIsValid(id))
360 elog(ERROR, "invalid ItemId");
361
362 itup = (IndexTuple) PageGetItem(uargs->page, id);
363
364 j = 0;
365 values[j++] = Int32GetDatum((int32) uargs->offset);
366 values[j++] = PointerGetDatum(&itup->t_tid);
367
368 hashkey = _hash_get_indextuple_hashkey(itup);
369 values[j] = Int64GetDatum((int64) hashkey);
370
371 tuple = heap_form_tuple(fctx->attinmeta->tupdesc, values, nulls);
372 result = HeapTupleGetDatum(tuple);
373
374 uargs->offset = uargs->offset + 1;
375
376 SRF_RETURN_NEXT(fctx, result);
377 }
378
379 SRF_RETURN_DONE(fctx);
380}
static Item PageGetItem(Page page, ItemId itemId)
Definition: bufpage.h:354
AttInMetadata * TupleDescGetAttInMetadata(TupleDesc tupdesc)
Definition: execTuples.c:2273
#define SRF_IS_FIRSTCALL()
Definition: funcapi.h:304
#define SRF_PERCALL_SETUP()
Definition: funcapi.h:308
#define SRF_RETURN_NEXT(_funcctx, _result)
Definition: funcapi.h:310
#define SRF_FIRSTCALL_INIT()
Definition: funcapi.h:306
#define SRF_RETURN_DONE(_funcctx)
Definition: funcapi.h:328
#define LH_BUCKET_PAGE
Definition: hash.h:55
#define LH_OVERFLOW_PAGE
Definition: hash.h:54
uint32 _hash_get_indextuple_hashkey(IndexTuple itup)
Definition: hashutil.c:291
#define ItemIdIsValid(itemId)
Definition: itemid.h:86
IndexTupleData * IndexTuple
Definition: itup.h:53
void * palloc(Size size)
Definition: mcxt.c:1317
MemoryContextSwitchTo(old_ctx)
TupleDesc tupdesc
Definition: funcapi.h:38
void * user_fctx
Definition: funcapi.h:82
uint64 max_calls
Definition: funcapi.h:74
uint64 call_cntr
Definition: funcapi.h:65
AttInMetadata * attinmeta
Definition: funcapi.h:91
MemoryContext multi_call_memory_ctx
Definition: funcapi.h:101
ItemPointerData t_tid
Definition: itup.h:37
OffsetNumber offset
Definition: hashfuncs.c:288
Page page
Definition: hashfuncs.c:287

References _hash_get_indextuple_hashkey(), FuncCallContext::attinmeta, BlessTupleDesc(), FuncCallContext::call_cntr, elog, ereport, errcode(), errmsg(), ERROR, FirstOffsetNumber, get_call_result_type(), heap_form_tuple(), HeapTupleGetDatum(), Int32GetDatum(), Int64GetDatum(), ItemIdIsValid, j, LH_BUCKET_PAGE, LH_OVERFLOW_PAGE, FuncCallContext::max_calls, MemoryContextSwitchTo(), FuncCallContext::multi_call_memory_ctx, user_args::offset, user_args::page, PageGetItem(), PageGetItemId(), PageGetMaxOffsetNumber(), palloc(), PG_GETARG_BYTEA_P, PointerGetDatum(), SRF_FIRSTCALL_INIT, SRF_IS_FIRSTCALL, SRF_PERCALL_SETUP, SRF_RETURN_DONE, SRF_RETURN_NEXT, superuser(), IndexTupleData::t_tid, AttInMetadata::tupdesc, TupleDescGetAttInMetadata(), TYPEFUNC_COMPOSITE, FuncCallContext::user_fctx, values, and verify_hash_page().

◆ hash_page_stats()

Datum hash_page_stats ( PG_FUNCTION_ARGS  )

Definition at line 237 of file hashfuncs.c.

238{
239 bytea *raw_page = PG_GETARG_BYTEA_P(0);
240 Page page;
241 int j;
242 Datum values[9];
243 bool nulls[9] = {0};
245 HeapTuple tuple;
246 TupleDesc tupleDesc;
247
248 if (!superuser())
250 (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
251 errmsg("must be superuser to use raw page functions")));
252
254
255 /* keep compiler quiet */
256 stat.hasho_prevblkno = stat.hasho_nextblkno = InvalidBlockNumber;
257 stat.hasho_flag = stat.hasho_page_id = stat.free_size = 0;
258
260
261 /* Build a tuple descriptor for our result type */
262 if (get_call_result_type(fcinfo, NULL, &tupleDesc) != TYPEFUNC_COMPOSITE)
263 elog(ERROR, "return type must be a row type");
264 tupleDesc = BlessTupleDesc(tupleDesc);
265
266 j = 0;
267 values[j++] = Int32GetDatum(stat.live_items);
268 values[j++] = Int32GetDatum(stat.dead_items);
269 values[j++] = Int32GetDatum(stat.page_size);
270 values[j++] = Int32GetDatum(stat.free_size);
271 values[j++] = Int64GetDatum((int64) stat.hasho_prevblkno);
272 values[j++] = Int64GetDatum((int64) stat.hasho_nextblkno);
273 values[j++] = Int64GetDatum((int64) stat.hasho_bucket);
274 values[j++] = Int32GetDatum((int32) stat.hasho_flag);
275 values[j++] = Int32GetDatum((int32) stat.hasho_page_id);
276
277 tuple = heap_form_tuple(tupleDesc, values, nulls);
278
280}
#define InvalidBlockNumber
Definition: block.h:33
static void GetHashPageStatistics(Page page, HashPageStat *stat)
Definition: hashfuncs.c:156
#define stat
Definition: win32_port.h:284

References BlessTupleDesc(), elog, ereport, errcode(), errmsg(), ERROR, get_call_result_type(), GetHashPageStatistics(), heap_form_tuple(), HeapTupleGetDatum(), Int32GetDatum(), Int64GetDatum(), InvalidBlockNumber, j, LH_BUCKET_PAGE, LH_OVERFLOW_PAGE, PG_GETARG_BYTEA_P, PG_RETURN_DATUM, stat, superuser(), TYPEFUNC_COMPOSITE, values, and verify_hash_page().

◆ hash_page_type()

Datum hash_page_type ( PG_FUNCTION_ARGS  )

Definition at line 192 of file hashfuncs.c.

193{
194 bytea *raw_page = PG_GETARG_BYTEA_P(0);
195 Page page;
196 HashPageOpaque opaque;
197 int pagetype;
198 const char *type;
199
200 if (!superuser())
202 (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
203 errmsg("must be superuser to use raw page functions")));
204
205 page = verify_hash_page(raw_page, 0);
206
207 if (PageIsNew(page))
208 type = "unused";
209 else
210 {
211 opaque = HashPageGetOpaque(page);
212
213 /* page type (flags) */
214 pagetype = opaque->hasho_flag & LH_PAGE_TYPE;
215 if (pagetype == LH_META_PAGE)
216 type = "metapage";
217 else if (pagetype == LH_OVERFLOW_PAGE)
218 type = "overflow";
219 else if (pagetype == LH_BUCKET_PAGE)
220 type = "bucket";
221 else if (pagetype == LH_BITMAP_PAGE)
222 type = "bitmap";
223 else
224 type = "unused";
225 }
226
228}
static bool PageIsNew(Page page)
Definition: bufpage.h:233
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:372
#define LH_PAGE_TYPE
Definition: hash.h:63
text * cstring_to_text(const char *s)
Definition: varlena.c:184
const char * type

References cstring_to_text(), ereport, errcode(), errmsg(), ERROR, HashPageOpaqueData::hasho_flag, HashPageGetOpaque, LH_BITMAP_PAGE, LH_BUCKET_PAGE, LH_META_PAGE, LH_OVERFLOW_PAGE, LH_PAGE_TYPE, PageIsNew(), PG_GETARG_BYTEA_P, PG_RETURN_TEXT_P, superuser(), type, and verify_hash_page().

◆ PG_FUNCTION_INFO_V1() [1/5]

PG_FUNCTION_INFO_V1 ( hash_bitmap_info  )

◆ PG_FUNCTION_INFO_V1() [2/5]

PG_FUNCTION_INFO_V1 ( hash_metapage_info  )

◆ PG_FUNCTION_INFO_V1() [3/5]

PG_FUNCTION_INFO_V1 ( hash_page_items  )

◆ PG_FUNCTION_INFO_V1() [4/5]

PG_FUNCTION_INFO_V1 ( hash_page_stats  )

◆ PG_FUNCTION_INFO_V1() [5/5]

PG_FUNCTION_INFO_V1 ( hash_page_type  )

◆ verify_hash_page()

static Page verify_hash_page ( bytea raw_page,
int  flags 
)
static

Definition at line 59 of file hashfuncs.c.

60{
61 Page page = get_page_from_raw(raw_page);
62 int pagetype = LH_UNUSED_PAGE;
63
64 /* Treat new pages as unused. */
65 if (!PageIsNew(page))
66 {
67 HashPageOpaque pageopaque;
68
71 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
72 errmsg("input page is not a valid %s page", "hash"),
73 errdetail("Expected special size %d, got %d.",
74 (int) MAXALIGN(sizeof(HashPageOpaqueData)),
75 (int) PageGetSpecialSize(page))));
76
77 pageopaque = HashPageGetOpaque(page);
78 if (pageopaque->hasho_page_id != HASHO_PAGE_ID)
80 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
81 errmsg("input page is not a valid %s page", "hash"),
82 errdetail("Expected %08x, got %08x.",
83 HASHO_PAGE_ID, pageopaque->hasho_page_id)));
84
85 pagetype = pageopaque->hasho_flag & LH_PAGE_TYPE;
86 }
87
88 /* Check that page type is sane. */
89 if (pagetype != LH_OVERFLOW_PAGE && pagetype != LH_BUCKET_PAGE &&
90 pagetype != LH_BITMAP_PAGE && pagetype != LH_META_PAGE &&
91 pagetype != LH_UNUSED_PAGE)
93 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
94 errmsg("invalid hash page type %08x", pagetype)));
95
96 /* If requested, verify page type. */
97 if (flags != 0 && (pagetype & flags) == 0)
98 {
99 switch (flags)
100 {
101 case LH_META_PAGE:
103 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
104 errmsg("page is not a hash meta page")));
105 break;
108 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
109 errmsg("page is not a hash bucket or overflow page")));
110 break;
111 case LH_OVERFLOW_PAGE:
113 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
114 errmsg("page is not a hash overflow page")));
115 break;
116 default:
117 elog(ERROR,
118 "hash page of type %08x not in mask %08x",
119 pagetype, flags);
120 break;
121 }
122 }
123
124 /*
125 * If it is the metapage, also verify magic number and version.
126 */
127 if (pagetype == LH_META_PAGE)
128 {
129 HashMetaPage metap = HashPageGetMeta(page);
130
131 if (metap->hashm_magic != HASH_MAGIC)
133 (errcode(ERRCODE_INDEX_CORRUPTED),
134 errmsg("invalid magic number for metadata"),
135 errdetail("Expected 0x%08x, got 0x%08x.",
136 HASH_MAGIC, metap->hashm_magic)));
137
138 if (metap->hashm_version != HASH_VERSION)
140 (errcode(ERRCODE_INDEX_CORRUPTED),
141 errmsg("invalid version for metadata"),
142 errdetail("Expected %d, got %d.",
143 HASH_VERSION, metap->hashm_version)));
144 }
145
146 return page;
147}
static uint16 PageGetSpecialSize(Page page)
Definition: bufpage.h:316
#define MAXALIGN(LEN)
Definition: c.h:765
int errdetail(const char *fmt,...)
Definition: elog.c:1203
#define HASH_VERSION
Definition: hash.h:201
#define LH_UNUSED_PAGE
Definition: hash.h:53
#define HASHO_PAGE_ID
Definition: hash.h:101
#define HASH_MAGIC
Definition: hash.h:200
Page get_page_from_raw(bytea *raw_page)
Definition: rawpage.c:215

References elog, ereport, errcode(), errdetail(), errmsg(), ERROR, get_page_from_raw(), HASH_MAGIC, HASH_VERSION, HashMetaPageData::hashm_magic, HashMetaPageData::hashm_version, HashPageOpaqueData::hasho_flag, HashPageOpaqueData::hasho_page_id, HASHO_PAGE_ID, HashPageGetMeta, HashPageGetOpaque, LH_BITMAP_PAGE, LH_BUCKET_PAGE, LH_META_PAGE, LH_OVERFLOW_PAGE, LH_PAGE_TYPE, LH_UNUSED_PAGE, MAXALIGN, PageGetSpecialSize(), and PageIsNew().

Referenced by hash_metapage_info(), hash_page_items(), hash_page_stats(), and hash_page_type().