PostgreSQL Source Code  git master
hashfuncs.c File Reference
#include "postgres.h"
#include "access/hash.h"
#include "access/htup_details.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_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 30 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 154 of file hashfuncs.c.

155 {
156  OffsetNumber maxoff = PageGetMaxOffsetNumber(page);
157  HashPageOpaque opaque = HashPageGetOpaque(page);
158  int off;
159 
160  stat->dead_items = stat->live_items = 0;
161  stat->page_size = PageGetPageSize(page);
162 
163  /* hash page opaque data */
164  stat->hasho_prevblkno = opaque->hasho_prevblkno;
165  stat->hasho_nextblkno = opaque->hasho_nextblkno;
166  stat->hasho_bucket = opaque->hasho_bucket;
167  stat->hasho_flag = opaque->hasho_flag;
168  stat->hasho_page_id = opaque->hasho_page_id;
169 
170  /* count live and dead tuples, and free space */
171  for (off = FirstOffsetNumber; off <= maxoff; off++)
172  {
173  ItemId id = PageGetItemId(page, off);
174 
175  if (!ItemIdIsDead(id))
176  stat->live_items++;
177  else
178  stat->dead_items++;
179  }
180  stat->free_size = PageGetFreeSpace(page);
181 }
Size PageGetFreeSpace(Page page)
Definition: bufpage.c:907
#define PageGetMaxOffsetNumber(page)
Definition: bufpage.h:356
#define PageGetItemId(page, offsetNumber)
Definition: bufpage.h:234
#define PageGetPageSize(page)
Definition: bufpage.h:267
#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 393 of file hashfuncs.c.

394 {
395  Oid indexRelid = PG_GETARG_OID(0);
396  int64 ovflblkno = PG_GETARG_INT64(1);
397  HashMetaPage metap;
398  Buffer metabuf,
399  mapbuf;
400  BlockNumber bitmapblkno;
401  Page mappage;
402  bool bit = false;
403  TupleDesc tupleDesc;
404  Relation indexRel;
405  uint32 ovflbitno;
406  int32 bitmappage,
407  bitmapbit;
408  HeapTuple tuple;
409  int i,
410  j;
411  Datum values[3];
412  bool nulls[3];
413  uint32 *freep;
414 
415  if (!superuser())
416  ereport(ERROR,
417  (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
418  errmsg("must be superuser to use raw page functions")));
419 
420  indexRel = index_open(indexRelid, AccessShareLock);
421 
422  if (!IS_HASH(indexRel))
423  ereport(ERROR,
424  (errcode(ERRCODE_WRONG_OBJECT_TYPE),
425  errmsg("\"%s\" is not a %s index",
426  RelationGetRelationName(indexRel), "hash")));
427 
428  if (RELATION_IS_OTHER_TEMP(indexRel))
429  ereport(ERROR,
430  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
431  errmsg("cannot access temporary tables of other sessions")));
432 
433  if (ovflblkno < 0 || ovflblkno > MaxBlockNumber)
434  ereport(ERROR,
435  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
436  errmsg("invalid block number")));
437 
438  if (ovflblkno >= RelationGetNumberOfBlocks(indexRel))
439  ereport(ERROR,
440  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
441  errmsg("block number %lld is out of range for relation \"%s\"",
442  (long long int) ovflblkno, RelationGetRelationName(indexRel))));
443 
444  /* Read the metapage so we can determine which bitmap page to use */
445  metabuf = _hash_getbuf(indexRel, HASH_METAPAGE, HASH_READ, LH_META_PAGE);
446  metap = HashPageGetMeta(BufferGetPage(metabuf));
447 
448  /*
449  * Reject attempt to read the bit for a metapage or bitmap page; this is
450  * only meaningful for overflow pages.
451  */
452  if (ovflblkno == 0)
453  ereport(ERROR,
454  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
455  errmsg("invalid overflow block number %u",
456  (BlockNumber) ovflblkno)));
457  for (i = 0; i < metap->hashm_nmaps; i++)
458  if (metap->hashm_mapp[i] == ovflblkno)
459  ereport(ERROR,
460  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
461  errmsg("invalid overflow block number %u",
462  (BlockNumber) ovflblkno)));
463 
464  /*
465  * Identify overflow bit number. This will error out for primary bucket
466  * pages, and we've already rejected the metapage and bitmap pages above.
467  */
468  ovflbitno = _hash_ovflblkno_to_bitno(metap, (BlockNumber) ovflblkno);
469 
470  bitmappage = ovflbitno >> BMPG_SHIFT(metap);
471  bitmapbit = ovflbitno & BMPG_MASK(metap);
472 
473  if (bitmappage >= metap->hashm_nmaps)
474  ereport(ERROR,
475  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
476  errmsg("invalid overflow block number %u",
477  (BlockNumber) ovflblkno)));
478 
479  bitmapblkno = metap->hashm_mapp[bitmappage];
480 
481  _hash_relbuf(indexRel, metabuf);
482 
483  /* Check the status of bitmap bit for overflow page */
484  mapbuf = _hash_getbuf(indexRel, bitmapblkno, HASH_READ, LH_BITMAP_PAGE);
485  mappage = BufferGetPage(mapbuf);
486  freep = HashPageGetBitmap(mappage);
487 
488  bit = ISSET(freep, bitmapbit) != 0;
489 
490  _hash_relbuf(indexRel, mapbuf);
491  index_close(indexRel, AccessShareLock);
492 
493  /* Build a tuple descriptor for our result type */
494  if (get_call_result_type(fcinfo, NULL, &tupleDesc) != TYPEFUNC_COMPOSITE)
495  elog(ERROR, "return type must be a row type");
496  tupleDesc = BlessTupleDesc(tupleDesc);
497 
498  MemSet(nulls, 0, sizeof(nulls));
499 
500  j = 0;
501  values[j++] = Int64GetDatum((int64) bitmapblkno);
502  values[j++] = Int32GetDatum(bitmapbit);
503  values[j++] = BoolGetDatum(bit);
504 
505  tuple = heap_form_tuple(tupleDesc, values, nulls);
506 
508 }
uint32 BlockNumber
Definition: block.h:31
#define MaxBlockNumber
Definition: block.h:35
static Datum values[MAXATTR]
Definition: bootstrap.c:156
int Buffer
Definition: buf.h:23
#define RelationGetNumberOfBlocks(reln)
Definition: bufmgr.h:216
#define BufferGetPage(buffer)
Definition: bufmgr.h:169
Pointer Page
Definition: bufpage.h:78
unsigned int uint32
Definition: c.h:441
signed int int32
Definition: c.h:429
#define MemSet(start, val, len)
Definition: c.h:1008
int errcode(int sqlerrcode)
Definition: elog.c:693
int errmsg(const char *fmt,...)
Definition: elog.c:904
#define ERROR
Definition: elog.h:33
#define elog(elevel,...)
Definition: elog.h:218
#define ereport(elevel,...)
Definition: elog.h:143
TupleDesc BlessTupleDesc(TupleDesc tupdesc)
Definition: execTuples.c:2071
Datum Int64GetDatum(int64 X)
Definition: fmgr.c:1683
#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
#define HeapTupleGetDatum(tuple)
Definition: funcapi.h:220
#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:30
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, Datum *values, bool *isnull)
Definition: heaptuple.c:1020
void index_close(Relation relation, LOCKMODE lockmode)
Definition: indexam.c:158
Relation index_open(Oid relationId, LOCKMODE lockmode)
Definition: indexam.c:132
int j
Definition: isn.c:74
int i
Definition: isn.c:73
#define AccessShareLock
Definition: lockdefs.h:36
uintptr_t Datum
Definition: postgres.h:411
#define BoolGetDatum(X)
Definition: postgres.h:446
#define Int32GetDatum(X)
Definition: postgres.h:523
unsigned int Oid
Definition: postgres_ext.h:31
#define RelationGetRelationName(relation)
Definition: rel.h:523
#define RELATION_IS_OTHER_TEMP(relation)
Definition: rel.h:643
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(), index_open(), Int32GetDatum, Int64GetDatum(), IS_HASH, ISSET, j, LH_BITMAP_PAGE, LH_META_PAGE, MaxBlockNumber, MemSet, PG_GETARG_INT64, PG_GETARG_OID, PG_RETURN_DATUM, RELATION_IS_OTHER_TEMP, RelationGetNumberOfBlocks, RelationGetRelationName, superuser(), TYPEFUNC_COMPOSITE, and values.

◆ hash_metapage_info()

Datum hash_metapage_info ( PG_FUNCTION_ARGS  )

Definition at line 519 of file hashfuncs.c.

520 {
521  bytea *raw_page = PG_GETARG_BYTEA_P(0);
522  Page page;
523  HashMetaPageData *metad;
524  TupleDesc tupleDesc;
525  HeapTuple tuple;
526  int i,
527  j;
528  Datum values[16];
529  bool nulls[16];
530  Datum spares[HASH_MAX_SPLITPOINTS];
531  Datum mapp[HASH_MAX_BITMAPS];
532 
533  if (!superuser())
534  ereport(ERROR,
535  (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
536  errmsg("must be superuser to use raw page functions")));
537 
538  page = verify_hash_page(raw_page, LH_META_PAGE);
539 
540  /* Build a tuple descriptor for our result type */
541  if (get_call_result_type(fcinfo, NULL, &tupleDesc) != TYPEFUNC_COMPOSITE)
542  elog(ERROR, "return type must be a row type");
543  tupleDesc = BlessTupleDesc(tupleDesc);
544 
545  metad = HashPageGetMeta(page);
546 
547  MemSet(nulls, 0, sizeof(nulls));
548 
549  j = 0;
550  values[j++] = Int64GetDatum((int64) metad->hashm_magic);
551  values[j++] = Int64GetDatum((int64) metad->hashm_version);
552  values[j++] = Float8GetDatum(metad->hashm_ntuples);
553  values[j++] = Int32GetDatum((int32) metad->hashm_ffactor);
554  values[j++] = Int32GetDatum((int32) metad->hashm_bsize);
555  values[j++] = Int32GetDatum((int32) metad->hashm_bmsize);
556  values[j++] = Int32GetDatum((int32) metad->hashm_bmshift);
557  values[j++] = Int64GetDatum((int64) metad->hashm_maxbucket);
558  values[j++] = Int64GetDatum((int64) metad->hashm_highmask);
559  values[j++] = Int64GetDatum((int64) metad->hashm_lowmask);
560  values[j++] = Int64GetDatum((int64) metad->hashm_ovflpoint);
561  values[j++] = Int64GetDatum((int64) metad->hashm_firstfree);
562  values[j++] = Int64GetDatum((int64) metad->hashm_nmaps);
563  values[j++] = ObjectIdGetDatum((Oid) metad->hashm_procid);
564 
565  for (i = 0; i < HASH_MAX_SPLITPOINTS; i++)
566  spares[i] = Int64GetDatum((int64) metad->hashm_spares[i]);
569  INT8OID,
570  sizeof(int64),
572  TYPALIGN_DOUBLE));
573 
574  for (i = 0; i < HASH_MAX_BITMAPS; i++)
575  mapp[i] = Int64GetDatum((int64) metad->hashm_mapp[i]);
578  INT8OID,
579  sizeof(int64),
581  TYPALIGN_DOUBLE));
582 
583  tuple = heap_form_tuple(tupleDesc, values, nulls);
584 
586 }
ArrayType * construct_array(Datum *elems, int nelems, Oid elmtype, int elmlen, bool elmbyval, char elmalign)
Definition: arrayfuncs.c:3319
#define FLOAT8PASSBYVAL
Definition: c.h:570
Datum Float8GetDatum(float8 X)
Definition: fmgr.c:1692
#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:57
#define ObjectIdGetDatum(X)
Definition: postgres.h:551
#define PointerGetDatum(X)
Definition: postgres.h:600
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:622

References BlessTupleDesc(), construct_array(), elog, ereport, errcode(), errmsg(), ERROR, Float8GetDatum(), FLOAT8PASSBYVAL, 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, MemSet, 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];
307  uint32 hashkey;
308  HeapTuple tuple;
309  FuncCallContext *fctx;
310  MemoryContext mctx;
311  struct user_args *uargs;
312 
313  if (!superuser())
314  ereport(ERROR,
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 
345  MemoryContextSwitchTo(mctx);
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  MemSet(nulls, 0, sizeof(nulls));
365 
366  j = 0;
367  values[j++] = Int32GetDatum((int32) uargs->offset);
368  values[j++] = PointerGetDatum(&itup->t_tid);
369 
370  hashkey = _hash_get_indextuple_hashkey(itup);
371  values[j] = Int64GetDatum((int64) hashkey);
372 
373  tuple = heap_form_tuple(fctx->attinmeta->tupdesc, values, nulls);
374  result = HeapTupleGetDatum(tuple);
375 
376  uargs->offset = uargs->offset + 1;
377 
378  SRF_RETURN_NEXT(fctx, result);
379  }
380 
381  SRF_RETURN_DONE(fctx);
382 }
#define PageGetItem(page, itemId)
Definition: bufpage.h:339
AttInMetadata * TupleDescGetAttInMetadata(TupleDesc tupdesc)
Definition: execTuples.c:2086
#define SRF_IS_FIRSTCALL()
Definition: funcapi.h:299
#define SRF_PERCALL_SETUP()
Definition: funcapi.h:303
#define SRF_RETURN_NEXT(_funcctx, _result)
Definition: funcapi.h:305
#define SRF_FIRSTCALL_INIT()
Definition: funcapi.h:301
#define SRF_RETURN_DONE(_funcctx)
Definition: funcapi.h:323
#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:292
#define ItemIdIsValid(itemId)
Definition: itemid.h:86
IndexTupleData * IndexTuple
Definition: itup.h:53
void * palloc(Size size)
Definition: mcxt.c:1068
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
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: btreefuncs.c:293
Page page
Definition: btreefuncs.c:292

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(), MemSet, 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 235 of file hashfuncs.c.

236 {
237  bytea *raw_page = PG_GETARG_BYTEA_P(0);
238  Page page;
239  int j;
240  Datum values[9];
241  bool nulls[9];
243  HeapTuple tuple;
244  TupleDesc tupleDesc;
245 
246  if (!superuser())
247  ereport(ERROR,
248  (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
249  errmsg("must be superuser to use raw page functions")));
250 
252 
253  /* keep compiler quiet */
254  stat.hasho_prevblkno = stat.hasho_nextblkno = InvalidBlockNumber;
255  stat.hasho_flag = stat.hasho_page_id = stat.free_size = 0;
256 
257  GetHashPageStatistics(page, &stat);
258 
259  /* Build a tuple descriptor for our result type */
260  if (get_call_result_type(fcinfo, NULL, &tupleDesc) != TYPEFUNC_COMPOSITE)
261  elog(ERROR, "return type must be a row type");
262  tupleDesc = BlessTupleDesc(tupleDesc);
263 
264  MemSet(nulls, 0, sizeof(nulls));
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:154
#define stat
Definition: win32_port.h:283

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, MemSet, 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 190 of file hashfuncs.c.

191 {
192  bytea *raw_page = PG_GETARG_BYTEA_P(0);
193  Page page;
194  HashPageOpaque opaque;
195  int pagetype;
196  const char *type;
197 
198  if (!superuser())
199  ereport(ERROR,
200  (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
201  errmsg("must be superuser to use raw page functions")));
202 
203  page = verify_hash_page(raw_page, 0);
204 
205  if (PageIsNew(page))
206  type = "unused";
207  else
208  {
209  opaque = HashPageGetOpaque(page);
210 
211  /* page type (flags) */
212  pagetype = opaque->hasho_flag & LH_PAGE_TYPE;
213  if (pagetype == LH_META_PAGE)
214  type = "metapage";
215  else if (pagetype == LH_OVERFLOW_PAGE)
216  type = "overflow";
217  else if (pagetype == LH_BUCKET_PAGE)
218  type = "bucket";
219  else if (pagetype == LH_BITMAP_PAGE)
220  type = "bitmap";
221  else
222  type = "unused";
223  }
224 
226 }
#define PageIsNew(page)
Definition: bufpage.h:228
#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:188

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(), generate_unaccent_rules::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 57 of file hashfuncs.c.

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