PostgreSQL Source Code  git master
pg_prewarm.c File Reference
#include "postgres.h"
#include <sys/stat.h>
#include <unistd.h>
#include "access/heapam.h"
#include "fmgr.h"
#include "miscadmin.h"
#include "storage/bufmgr.h"
#include "storage/smgr.h"
#include "utils/acl.h"
#include "utils/builtins.h"
#include "utils/lsyscache.h"
#include "utils/rel.h"
Include dependency graph for pg_prewarm.c:

Go to the source code of this file.

Enumerations

enum  PrewarmType { PREWARM_PREFETCH, PREWARM_READ, PREWARM_BUFFER }
 

Functions

 PG_FUNCTION_INFO_V1 (pg_prewarm)
 
Datum pg_prewarm (PG_FUNCTION_ARGS)
 

Variables

 PG_MODULE_MAGIC
 
static char blockbuffer [BLCKSZ]
 

Enumeration Type Documentation

◆ PrewarmType

Enumerator
PREWARM_PREFETCH 
PREWARM_READ 
PREWARM_BUFFER 

Definition at line 32 of file pg_prewarm.c.

Function Documentation

◆ PG_FUNCTION_INFO_V1()

PG_FUNCTION_INFO_V1 ( pg_prewarm  )

◆ pg_prewarm()

Datum pg_prewarm ( PG_FUNCTION_ARGS  )

Definition at line 54 of file pg_prewarm.c.

References AccessShareLock, ACL_SELECT, aclcheck_error(), ACLCHECK_OK, blockbuffer, buf, CHECK_FOR_INTERRUPTS, ereport, errcode(), errhint(), errmsg(), ERROR, forkname_to_number(), get_rel_name(), get_relkind_objtype(), GetUserId(), INT64_FORMAT, PG_ARGISNULL, pg_class_aclcheck(), PG_GETARG_INT64, PG_GETARG_OID, PG_GETARG_TEXT_PP, PG_RETURN_INT64, PrefetchBuffer(), PREWARM_BUFFER, PREWARM_PREFETCH, PREWARM_READ, RBM_NORMAL, RelationData::rd_rel, RelationData::rd_smgr, ReadBufferExtended(), relation_close(), relation_open(), RelationGetNumberOfBlocksInFork(), RelationOpenSmgr, ReleaseBuffer(), smgrexists(), smgrread(), text_to_cstring(), and generate_unaccent_rules::type.

55 {
56  Oid relOid;
57  text *forkName;
58  text *type;
59  int64 first_block;
60  int64 last_block;
61  int64 nblocks;
62  int64 blocks_done = 0;
63  int64 block;
64  Relation rel;
65  ForkNumber forkNumber;
66  char *forkString;
67  char *ttype;
68  PrewarmType ptype;
69  AclResult aclresult;
70 
71  /* Basic sanity checking. */
72  if (PG_ARGISNULL(0))
73  ereport(ERROR,
74  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
75  errmsg("relation cannot be null")));
76  relOid = PG_GETARG_OID(0);
77  if (PG_ARGISNULL(1))
78  ereport(ERROR,
79  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
80  (errmsg("prewarm type cannot be null"))));
81  type = PG_GETARG_TEXT_PP(1);
82  ttype = text_to_cstring(type);
83  if (strcmp(ttype, "prefetch") == 0)
84  ptype = PREWARM_PREFETCH;
85  else if (strcmp(ttype, "read") == 0)
86  ptype = PREWARM_READ;
87  else if (strcmp(ttype, "buffer") == 0)
88  ptype = PREWARM_BUFFER;
89  else
90  {
91  ereport(ERROR,
92  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
93  errmsg("invalid prewarm type"),
94  errhint("Valid prewarm types are \"prefetch\", \"read\", and \"buffer\".")));
95  PG_RETURN_INT64(0); /* Placate compiler. */
96  }
97  if (PG_ARGISNULL(2))
98  ereport(ERROR,
99  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
100  (errmsg("relation fork cannot be null"))));
101  forkName = PG_GETARG_TEXT_PP(2);
102  forkString = text_to_cstring(forkName);
103  forkNumber = forkname_to_number(forkString);
104 
105  /* Open relation and check privileges. */
106  rel = relation_open(relOid, AccessShareLock);
107  aclresult = pg_class_aclcheck(relOid, GetUserId(), ACL_SELECT);
108  if (aclresult != ACLCHECK_OK)
109  aclcheck_error(aclresult, get_relkind_objtype(rel->rd_rel->relkind), get_rel_name(relOid));
110 
111  /* Check that the fork exists. */
112  RelationOpenSmgr(rel);
113  if (!smgrexists(rel->rd_smgr, forkNumber))
114  ereport(ERROR,
115  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
116  errmsg("fork \"%s\" does not exist for this relation",
117  forkString)));
118 
119  /* Validate block numbers, or handle nulls. */
120  nblocks = RelationGetNumberOfBlocksInFork(rel, forkNumber);
121  if (PG_ARGISNULL(3))
122  first_block = 0;
123  else
124  {
125  first_block = PG_GETARG_INT64(3);
126  if (first_block < 0 || first_block >= nblocks)
127  ereport(ERROR,
128  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
129  errmsg("starting block number must be between 0 and " INT64_FORMAT,
130  nblocks - 1)));
131  }
132  if (PG_ARGISNULL(4))
133  last_block = nblocks - 1;
134  else
135  {
136  last_block = PG_GETARG_INT64(4);
137  if (last_block < 0 || last_block >= nblocks)
138  ereport(ERROR,
139  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
140  errmsg("ending block number must be between 0 and " INT64_FORMAT,
141  nblocks - 1)));
142  }
143 
144  /* Now we're ready to do the real work. */
145  if (ptype == PREWARM_PREFETCH)
146  {
147 #ifdef USE_PREFETCH
148 
149  /*
150  * In prefetch mode, we just hint the OS to read the blocks, but we
151  * don't know whether it really does it, and we don't wait for it to
152  * finish.
153  *
154  * It would probably be better to pass our prefetch requests in chunks
155  * of a megabyte or maybe even a whole segment at a time, but there's
156  * no practical way to do that at present without a gross modularity
157  * violation, so we just do this.
158  */
159  for (block = first_block; block <= last_block; ++block)
160  {
162  PrefetchBuffer(rel, forkNumber, block);
163  ++blocks_done;
164  }
165 #else
166  ereport(ERROR,
167  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
168  errmsg("prefetch is not supported by this build")));
169 #endif
170  }
171  else if (ptype == PREWARM_READ)
172  {
173  /*
174  * In read mode, we actually read the blocks, but not into shared
175  * buffers. This is more portable than prefetch mode (it works
176  * everywhere) and is synchronous.
177  */
178  for (block = first_block; block <= last_block; ++block)
179  {
181  smgrread(rel->rd_smgr, forkNumber, block, blockbuffer);
182  ++blocks_done;
183  }
184  }
185  else if (ptype == PREWARM_BUFFER)
186  {
187  /*
188  * In buffer mode, we actually pull the data into shared_buffers.
189  */
190  for (block = first_block; block <= last_block; ++block)
191  {
192  Buffer buf;
193 
195  buf = ReadBufferExtended(rel, forkNumber, block, RBM_NORMAL, NULL);
196  ReleaseBuffer(buf);
197  ++blocks_done;
198  }
199  }
200 
201  /* Close relation, release lock. */
203 
204  PG_RETURN_INT64(blocks_done);
205 }
PrewarmType
Definition: pg_prewarm.c:32
int errhint(const char *fmt,...)
Definition: elog.c:987
Oid GetUserId(void)
Definition: miscinit.c:379
#define PG_RETURN_INT64(x)
Definition: fmgr.h:332
struct SMgrRelationData * rd_smgr
Definition: rel.h:57
Buffer ReadBufferExtended(Relation reln, ForkNumber forkNum, BlockNumber blockNum, ReadBufferMode mode, BufferAccessStrategy strategy)
Definition: bufmgr.c:640
#define AccessShareLock
Definition: lockdefs.h:36
int errcode(int sqlerrcode)
Definition: elog.c:575
void relation_close(Relation relation, LOCKMODE lockmode)
Definition: heapam.c:1270
void ReleaseBuffer(Buffer buffer)
Definition: bufmgr.c:3309
bool smgrexists(SMgrRelation reln, ForkNumber forknum)
Definition: smgr.c:287
Form_pg_class rd_rel
Definition: rel.h:84
unsigned int Oid
Definition: postgres_ext.h:31
ForkNumber forkname_to_number(const char *forkName)
Definition: relpath.c:47
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:278
void smgrread(SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum, char *buffer)
Definition: smgr.c:625
#define RelationOpenSmgr(relation)
Definition: rel.h:465
void aclcheck_error(AclResult aclerr, ObjectType objtype, const char *objectname)
Definition: aclchk.c:3348
#define ERROR
Definition: elog.h:43
static char * buf
Definition: pg_test_fsync.c:67
#define PG_GETARG_OID(n)
Definition: fmgr.h:245
static char blockbuffer[BLCKSZ]
Definition: pg_prewarm.c:39
#define ereport(elevel, rest)
Definition: elog.h:122
ForkNumber
Definition: relpath.h:40
AclResult
Definition: acl.h:178
#define ACL_SELECT
Definition: parsenodes.h:75
void PrefetchBuffer(Relation reln, ForkNumber forkNum, BlockNumber blockNum)
Definition: bufmgr.c:529
BlockNumber RelationGetNumberOfBlocksInFork(Relation relation, ForkNumber forkNum)
Definition: bufmgr.c:2788
#define PG_ARGISNULL(n)
Definition: fmgr.h:179
#define INT64_FORMAT
Definition: c.h:367
AclResult pg_class_aclcheck(Oid table_oid, Oid roleid, AclMode mode)
Definition: aclchk.c:4624
char * text_to_cstring(const text *t)
Definition: varlena.c:182
int errmsg(const char *fmt,...)
Definition: elog.c:797
ObjectType get_relkind_objtype(char relkind)
Relation relation_open(Oid relationId, LOCKMODE lockmode)
Definition: heapam.c:1124
Definition: c.h:516
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:98
#define PG_GETARG_INT64(n)
Definition: fmgr.h:252
char * get_rel_name(Oid relid)
Definition: lsyscache.c:1730
int Buffer
Definition: buf.h:23

Variable Documentation

◆ blockbuffer

char blockbuffer[BLCKSZ]
static

Definition at line 39 of file pg_prewarm.c.

Referenced by pg_prewarm().

◆ PG_MODULE_MAGIC

PG_MODULE_MAGIC

Definition at line 28 of file pg_prewarm.c.