PostgreSQL Source Code git master
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
pg_prewarm.c File Reference
#include "postgres.h"
#include <sys/stat.h>
#include <unistd.h>
#include "access/relation.h"
#include "fmgr.h"
#include "miscadmin.h"
#include "storage/bufmgr.h"
#include "storage/read_stream.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_MODULE_MAGIC_EXT (.name="pg_prewarm",.version=PG_VERSION)
 
 PG_FUNCTION_INFO_V1 (pg_prewarm)
 
Datum pg_prewarm (PG_FUNCTION_ARGS)
 

Variables

static PGIOAlignedBlock blockbuffer
 

Enumeration Type Documentation

◆ PrewarmType

Enumerator
PREWARM_PREFETCH 
PREWARM_READ 
PREWARM_BUFFER 

Definition at line 36 of file pg_prewarm.c.

37{
PrewarmType
Definition: pg_prewarm.c:37
@ PREWARM_PREFETCH
Definition: pg_prewarm.c:38
@ PREWARM_READ
Definition: pg_prewarm.c:39
@ PREWARM_BUFFER
Definition: pg_prewarm.c:40

Function Documentation

◆ PG_FUNCTION_INFO_V1()

PG_FUNCTION_INFO_V1 ( pg_prewarm  )

◆ PG_MODULE_MAGIC_EXT()

PG_MODULE_MAGIC_EXT ( name = "pg_prewarm",
version = PG_VERSION 
)

◆ pg_prewarm()

Datum pg_prewarm ( PG_FUNCTION_ARGS  )

Definition at line 58 of file pg_prewarm.c.

59{
60 Oid relOid;
61 text *forkName;
62 text *type;
63 int64 first_block;
64 int64 last_block;
65 int64 nblocks;
66 int64 blocks_done = 0;
67 int64 block;
68 Relation rel;
69 ForkNumber forkNumber;
70 char *forkString;
71 char *ttype;
72 PrewarmType ptype;
73 AclResult aclresult;
74
75 /* Basic sanity checking. */
76 if (PG_ARGISNULL(0))
78 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
79 errmsg("relation cannot be null")));
80 relOid = PG_GETARG_OID(0);
81 if (PG_ARGISNULL(1))
83 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
84 errmsg("prewarm type cannot be null")));
86 ttype = text_to_cstring(type);
87 if (strcmp(ttype, "prefetch") == 0)
88 ptype = PREWARM_PREFETCH;
89 else if (strcmp(ttype, "read") == 0)
90 ptype = PREWARM_READ;
91 else if (strcmp(ttype, "buffer") == 0)
92 ptype = PREWARM_BUFFER;
93 else
94 {
96 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
97 errmsg("invalid prewarm type"),
98 errhint("Valid prewarm types are \"prefetch\", \"read\", and \"buffer\".")));
99 PG_RETURN_INT64(0); /* Placate compiler. */
100 }
101 if (PG_ARGISNULL(2))
103 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
104 errmsg("relation fork cannot be null")));
105 forkName = PG_GETARG_TEXT_PP(2);
106 forkString = text_to_cstring(forkName);
107 forkNumber = forkname_to_number(forkString);
108
109 /* Open relation and check privileges. */
110 rel = relation_open(relOid, AccessShareLock);
111 aclresult = pg_class_aclcheck(relOid, GetUserId(), ACL_SELECT);
112 if (aclresult != ACLCHECK_OK)
113 aclcheck_error(aclresult, get_relkind_objtype(rel->rd_rel->relkind), get_rel_name(relOid));
114
115 /* Check that the fork exists. */
116 if (!smgrexists(RelationGetSmgr(rel), forkNumber))
118 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
119 errmsg("fork \"%s\" does not exist for this relation",
120 forkString)));
121
122 /* Validate block numbers, or handle nulls. */
123 nblocks = RelationGetNumberOfBlocksInFork(rel, forkNumber);
124 if (PG_ARGISNULL(3))
125 first_block = 0;
126 else
127 {
128 first_block = PG_GETARG_INT64(3);
129 if (first_block < 0 || first_block >= nblocks)
131 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
132 errmsg("starting block number must be between 0 and %" PRId64,
133 (nblocks - 1))));
134 }
135 if (PG_ARGISNULL(4))
136 last_block = nblocks - 1;
137 else
138 {
139 last_block = PG_GETARG_INT64(4);
140 if (last_block < 0 || last_block >= nblocks)
142 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
143 errmsg("ending block number must be between 0 and %" PRId64,
144 (nblocks - 1))));
145 }
146
147 /* Now we're ready to do the real work. */
148 if (ptype == PREWARM_PREFETCH)
149 {
150#ifdef USE_PREFETCH
151
152 /*
153 * In prefetch mode, we just hint the OS to read the blocks, but we
154 * don't know whether it really does it, and we don't wait for it to
155 * finish.
156 *
157 * It would probably be better to pass our prefetch requests in chunks
158 * of a megabyte or maybe even a whole segment at a time, but there's
159 * no practical way to do that at present without a gross modularity
160 * violation, so we just do this.
161 */
162 for (block = first_block; block <= last_block; ++block)
163 {
165 PrefetchBuffer(rel, forkNumber, block);
166 ++blocks_done;
167 }
168#else
170 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
171 errmsg("prefetch is not supported by this build")));
172#endif
173 }
174 else if (ptype == PREWARM_READ)
175 {
176 /*
177 * In read mode, we actually read the blocks, but not into shared
178 * buffers. This is more portable than prefetch mode (it works
179 * everywhere) and is synchronous.
180 */
181 for (block = first_block; block <= last_block; ++block)
182 {
184 smgrread(RelationGetSmgr(rel), forkNumber, block, blockbuffer.data);
185 ++blocks_done;
186 }
187 }
188 else if (ptype == PREWARM_BUFFER)
189 {
191 ReadStream *stream;
192
193 /*
194 * In buffer mode, we actually pull the data into shared_buffers.
195 */
196
197 /* Set up the private state for our streaming buffer read callback. */
198 p.current_blocknum = first_block;
199 p.last_exclusive = last_block + 1;
200
201 /*
202 * It is safe to use batchmode as block_range_read_stream_cb takes no
203 * locks.
204 */
207 NULL,
208 rel,
209 forkNumber,
211 &p,
212 0);
213
214 for (block = first_block; block <= last_block; ++block)
215 {
216 Buffer buf;
217
219 buf = read_stream_next_buffer(stream, NULL);
221 ++blocks_done;
222 }
224 read_stream_end(stream);
225 }
226
227 /* Close relation, release lock. */
229
230 PG_RETURN_INT64(blocks_done);
231}
AclResult
Definition: acl.h:182
@ ACLCHECK_OK
Definition: acl.h:183
void aclcheck_error(AclResult aclerr, ObjectType objtype, const char *objectname)
Definition: aclchk.c:2639
AclResult pg_class_aclcheck(Oid table_oid, Oid roleid, AclMode mode)
Definition: aclchk.c:4024
int Buffer
Definition: buf.h:23
#define InvalidBuffer
Definition: buf.h:25
PrefetchBufferResult PrefetchBuffer(Relation reln, ForkNumber forkNum, BlockNumber blockNum)
Definition: bufmgr.c:651
BlockNumber RelationGetNumberOfBlocksInFork(Relation relation, ForkNumber forkNum)
Definition: bufmgr.c:4431
void ReleaseBuffer(Buffer buffer)
Definition: bufmgr.c:5373
int64_t int64
Definition: c.h:499
int errhint(const char *fmt,...)
Definition: elog.c:1318
int errcode(int sqlerrcode)
Definition: elog.c:854
int errmsg(const char *fmt,...)
Definition: elog.c:1071
#define ERROR
Definition: elog.h:39
#define ereport(elevel,...)
Definition: elog.h:149
#define PG_GETARG_OID(n)
Definition: fmgr.h:275
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:309
#define PG_ARGISNULL(n)
Definition: fmgr.h:209
#define PG_RETURN_INT64(x)
Definition: fmgr.h:368
#define PG_GETARG_INT64(n)
Definition: fmgr.h:283
Assert(PointerIsAligned(start, uint64))
#define AccessShareLock
Definition: lockdefs.h:36
char * get_rel_name(Oid relid)
Definition: lsyscache.c:2068
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:123
Oid GetUserId(void)
Definition: miscinit.c:520
ObjectType get_relkind_objtype(char relkind)
#define ACL_SELECT
Definition: parsenodes.h:77
static PGIOAlignedBlock blockbuffer
Definition: pg_prewarm.c:43
static char * buf
Definition: pg_test_fsync.c:72
unsigned int Oid
Definition: postgres_ext.h:30
Buffer read_stream_next_buffer(ReadStream *stream, void **per_buffer_data)
Definition: read_stream.c:770
ReadStream * read_stream_begin_relation(int flags, BufferAccessStrategy strategy, Relation rel, ForkNumber forknum, ReadStreamBlockNumberCB callback, void *callback_private_data, size_t per_buffer_data_size)
Definition: read_stream.c:716
void read_stream_end(ReadStream *stream)
Definition: read_stream.c:1055
BlockNumber block_range_read_stream_cb(ReadStream *stream, void *callback_private_data, void *per_buffer_data)
Definition: read_stream.c:162
#define READ_STREAM_USE_BATCHING
Definition: read_stream.h:64
#define READ_STREAM_FULL
Definition: read_stream.h:43
static SMgrRelation RelationGetSmgr(Relation rel)
Definition: rel.h:578
ForkNumber forkname_to_number(const char *forkName)
Definition: relpath.c:50
ForkNumber
Definition: relpath.h:56
bool smgrexists(SMgrRelation reln, ForkNumber forknum)
Definition: smgr.c:462
static void smgrread(SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum, void *buffer)
Definition: smgr.h:124
void relation_close(Relation relation, LOCKMODE lockmode)
Definition: relation.c:205
Relation relation_open(Oid relationId, LOCKMODE lockmode)
Definition: relation.c:47
Form_pg_class rd_rel
Definition: rel.h:111
Definition: c.h:658
char data[BLCKSZ]
Definition: c.h:1108
char * text_to_cstring(const text *t)
Definition: varlena.c:225
const char * type

References AccessShareLock, ACL_SELECT, aclcheck_error(), ACLCHECK_OK, Assert(), block_range_read_stream_cb(), blockbuffer, buf, CHECK_FOR_INTERRUPTS, BlockRangeReadStreamPrivate::current_blocknum, PGIOAlignedBlock::data, ereport, errcode(), errhint(), errmsg(), ERROR, forkname_to_number(), get_rel_name(), get_relkind_objtype(), GetUserId(), InvalidBuffer, BlockRangeReadStreamPrivate::last_exclusive, 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, RelationData::rd_rel, read_stream_begin_relation(), read_stream_end(), READ_STREAM_FULL, read_stream_next_buffer(), READ_STREAM_USE_BATCHING, relation_close(), relation_open(), RelationGetNumberOfBlocksInFork(), RelationGetSmgr(), ReleaseBuffer(), smgrexists(), smgrread(), text_to_cstring(), and type.

Variable Documentation

◆ blockbuffer

PGIOAlignedBlock blockbuffer
static

Definition at line 43 of file pg_prewarm.c.

Referenced by pg_prewarm().