LCOV - code coverage report
Current view: top level - contrib/pg_prewarm - pg_prewarm.c (source / functions) Hit Total Coverage
Test: PostgreSQL 13devel Lines: 0 59 0.0 %
Date: 2019-11-13 21:06:57 Functions: 0 3 0.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*-------------------------------------------------------------------------
       2             :  *
       3             :  * pg_prewarm.c
       4             :  *        prewarming utilities
       5             :  *
       6             :  * Copyright (c) 2010-2019, PostgreSQL Global Development Group
       7             :  *
       8             :  * IDENTIFICATION
       9             :  *        contrib/pg_prewarm/pg_prewarm.c
      10             :  *
      11             :  *-------------------------------------------------------------------------
      12             :  */
      13             : #include "postgres.h"
      14             : 
      15             : #include <sys/stat.h>
      16             : #include <unistd.h>
      17             : 
      18             : #include "access/relation.h"
      19             : #include "fmgr.h"
      20             : #include "miscadmin.h"
      21             : #include "storage/bufmgr.h"
      22             : #include "storage/smgr.h"
      23             : #include "utils/acl.h"
      24             : #include "utils/builtins.h"
      25             : #include "utils/lsyscache.h"
      26             : #include "utils/rel.h"
      27             : 
      28           0 : PG_MODULE_MAGIC;
      29             : 
      30           0 : PG_FUNCTION_INFO_V1(pg_prewarm);
      31             : 
      32             : typedef enum
      33             : {
      34             :     PREWARM_PREFETCH,
      35             :     PREWARM_READ,
      36             :     PREWARM_BUFFER
      37             : } PrewarmType;
      38             : 
      39             : static PGAlignedBlock blockbuffer;
      40             : 
      41             : /*
      42             :  * pg_prewarm(regclass, mode text, fork text,
      43             :  *            first_block int8, last_block int8)
      44             :  *
      45             :  * The first argument is the relation to be prewarmed; the second controls
      46             :  * how prewarming is done; legal options are 'prefetch', 'read', and 'buffer'.
      47             :  * The third is the name of the relation fork to be prewarmed.  The fourth
      48             :  * and fifth arguments specify the first and last block to be prewarmed.
      49             :  * If the fourth argument is NULL, it will be taken as 0; if the fifth argument
      50             :  * is NULL, it will be taken as the number of blocks in the relation.  The
      51             :  * return value is the number of blocks successfully prewarmed.
      52             :  */
      53             : Datum
      54           0 : pg_prewarm(PG_FUNCTION_ARGS)
      55             : {
      56             :     Oid         relOid;
      57             :     text       *forkName;
      58             :     text       *type;
      59             :     int64       first_block;
      60             :     int64       last_block;
      61             :     int64       nblocks;
      62           0 :     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           0 :     if (PG_ARGISNULL(0))
      73           0 :         ereport(ERROR,
      74             :                 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
      75             :                  errmsg("relation cannot be null")));
      76           0 :     relOid = PG_GETARG_OID(0);
      77           0 :     if (PG_ARGISNULL(1))
      78           0 :         ereport(ERROR,
      79             :                 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
      80             :                  (errmsg("prewarm type cannot be null"))));
      81           0 :     type = PG_GETARG_TEXT_PP(1);
      82           0 :     ttype = text_to_cstring(type);
      83           0 :     if (strcmp(ttype, "prefetch") == 0)
      84           0 :         ptype = PREWARM_PREFETCH;
      85           0 :     else if (strcmp(ttype, "read") == 0)
      86           0 :         ptype = PREWARM_READ;
      87           0 :     else if (strcmp(ttype, "buffer") == 0)
      88           0 :         ptype = PREWARM_BUFFER;
      89             :     else
      90             :     {
      91           0 :         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           0 :     if (PG_ARGISNULL(2))
      98           0 :         ereport(ERROR,
      99             :                 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
     100             :                  (errmsg("relation fork cannot be null"))));
     101           0 :     forkName = PG_GETARG_TEXT_PP(2);
     102           0 :     forkString = text_to_cstring(forkName);
     103           0 :     forkNumber = forkname_to_number(forkString);
     104             : 
     105             :     /* Open relation and check privileges. */
     106           0 :     rel = relation_open(relOid, AccessShareLock);
     107           0 :     aclresult = pg_class_aclcheck(relOid, GetUserId(), ACL_SELECT);
     108           0 :     if (aclresult != ACLCHECK_OK)
     109           0 :         aclcheck_error(aclresult, get_relkind_objtype(rel->rd_rel->relkind), get_rel_name(relOid));
     110             : 
     111             :     /* Check that the fork exists. */
     112           0 :     RelationOpenSmgr(rel);
     113           0 :     if (!smgrexists(rel->rd_smgr, forkNumber))
     114           0 :         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           0 :     nblocks = RelationGetNumberOfBlocksInFork(rel, forkNumber);
     121           0 :     if (PG_ARGISNULL(3))
     122           0 :         first_block = 0;
     123             :     else
     124             :     {
     125           0 :         first_block = PG_GETARG_INT64(3);
     126           0 :         if (first_block < 0 || first_block >= nblocks)
     127           0 :             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           0 :     if (PG_ARGISNULL(4))
     133           0 :         last_block = nblocks - 1;
     134             :     else
     135             :     {
     136           0 :         last_block = PG_GETARG_INT64(4);
     137           0 :         if (last_block < 0 || last_block >= nblocks)
     138           0 :             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           0 :     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           0 :         for (block = first_block; block <= last_block; ++block)
     160             :         {
     161           0 :             CHECK_FOR_INTERRUPTS();
     162           0 :             PrefetchBuffer(rel, forkNumber, block);
     163           0 :             ++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           0 :     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           0 :         for (block = first_block; block <= last_block; ++block)
     179             :         {
     180           0 :             CHECK_FOR_INTERRUPTS();
     181           0 :             smgrread(rel->rd_smgr, forkNumber, block, blockbuffer.data);
     182           0 :             ++blocks_done;
     183             :         }
     184             :     }
     185           0 :     else if (ptype == PREWARM_BUFFER)
     186             :     {
     187             :         /*
     188             :          * In buffer mode, we actually pull the data into shared_buffers.
     189             :          */
     190           0 :         for (block = first_block; block <= last_block; ++block)
     191             :         {
     192             :             Buffer      buf;
     193             : 
     194           0 :             CHECK_FOR_INTERRUPTS();
     195           0 :             buf = ReadBufferExtended(rel, forkNumber, block, RBM_NORMAL, NULL);
     196           0 :             ReleaseBuffer(buf);
     197           0 :             ++blocks_done;
     198             :         }
     199             :     }
     200             : 
     201             :     /* Close relation, release lock. */
     202           0 :     relation_close(rel, AccessShareLock);
     203             : 
     204           0 :     PG_RETURN_INT64(blocks_done);
     205             : }

Generated by: LCOV version 1.13