LCOV - code coverage report
Current view: top level - src/test/modules/dummy_index_am - dummy_index_am.c (source / functions) Hit Total Coverage
Test: PostgreSQL 13beta1 Lines: 84 108 77.8 %
Date: 2020-05-29 00:07:09 Functions: 8 17 47.1 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*-------------------------------------------------------------------------
       2             :  *
       3             :  * dummy_index_am.c
       4             :  *      Index AM template main file.
       5             :  *
       6             :  * Portions Copyright (c) 1996-2020, PostgreSQL Global Development Group
       7             :  * Portions Copyright (c) 1994, Regents of the University of California
       8             :  *
       9             :  * IDENTIFICATION
      10             :  *    src/test/modules/dummy_index_am/dummy_index_am.c
      11             :  *
      12             :  *-------------------------------------------------------------------------
      13             :  */
      14             : #include "postgres.h"
      15             : 
      16             : #include "access/amapi.h"
      17             : #include "access/reloptions.h"
      18             : #include "catalog/index.h"
      19             : #include "commands/vacuum.h"
      20             : #include "nodes/pathnodes.h"
      21             : #include "utils/guc.h"
      22             : #include "utils/rel.h"
      23             : 
      24           2 : PG_MODULE_MAGIC;
      25             : 
      26             : void        _PG_init(void);
      27             : 
      28             : /* parse table for fillRelOptions */
      29             : relopt_parse_elt di_relopt_tab[6];
      30             : 
      31             : /* Kind of relation options for dummy index */
      32             : relopt_kind di_relopt_kind;
      33             : 
      34             : typedef enum DummyAmEnum
      35             : {
      36             :     DUMMY_AM_ENUM_ONE,
      37             :     DUMMY_AM_ENUM_TWO
      38             : }           DummyAmEnum;
      39             : 
      40             : /* Dummy index options */
      41             : typedef struct DummyIndexOptions
      42             : {
      43             :     int32       vl_len_;        /* varlena header (do not touch directly!) */
      44             :     int         option_int;
      45             :     double      option_real;
      46             :     bool        option_bool;
      47             :     DummyAmEnum option_enum;
      48             :     int         option_string_val_offset;
      49             :     int         option_string_null_offset;
      50             : }           DummyIndexOptions;
      51             : 
      52             : relopt_enum_elt_def dummyAmEnumValues[] =
      53             : {
      54             :     {"one", DUMMY_AM_ENUM_ONE},
      55             :     {"two", DUMMY_AM_ENUM_TWO},
      56             :     {(const char *) NULL}       /* list terminator */
      57             : };
      58             : 
      59             : /* Handler for index AM */
      60           4 : PG_FUNCTION_INFO_V1(dihandler);
      61             : 
      62             : /*
      63             :  * Validation function for string relation options.
      64             :  */
      65             : static void
      66          60 : validate_string_option(const char *value)
      67             : {
      68          60 :     ereport(NOTICE,
      69             :             (errmsg("new option value for string parameter %s",
      70             :                     value ? value : "NULL")));
      71          60 : }
      72             : 
      73             : /*
      74             :  * This function creates a full set of relation option types,
      75             :  * with various patterns.
      76             :  */
      77             : static void
      78           2 : create_reloptions_table(void)
      79             : {
      80           2 :     di_relopt_kind = add_reloption_kind();
      81             : 
      82           2 :     add_int_reloption(di_relopt_kind, "option_int",
      83             :                       "Integer option for dummy_index_am",
      84             :                       10, -10, 100, AccessExclusiveLock);
      85           2 :     di_relopt_tab[0].optname = "option_int";
      86           2 :     di_relopt_tab[0].opttype = RELOPT_TYPE_INT;
      87           2 :     di_relopt_tab[0].offset = offsetof(DummyIndexOptions, option_int);
      88             : 
      89           2 :     add_real_reloption(di_relopt_kind, "option_real",
      90             :                        "Real option for dummy_index_am",
      91             :                        3.1415, -10, 100, AccessExclusiveLock);
      92           2 :     di_relopt_tab[1].optname = "option_real";
      93           2 :     di_relopt_tab[1].opttype = RELOPT_TYPE_REAL;
      94           2 :     di_relopt_tab[1].offset = offsetof(DummyIndexOptions, option_real);
      95             : 
      96           2 :     add_bool_reloption(di_relopt_kind, "option_bool",
      97             :                        "Boolean option for dummy_index_am",
      98             :                        true, AccessExclusiveLock);
      99           2 :     di_relopt_tab[2].optname = "option_bool";
     100           2 :     di_relopt_tab[2].opttype = RELOPT_TYPE_BOOL;
     101           2 :     di_relopt_tab[2].offset = offsetof(DummyIndexOptions, option_bool);
     102             : 
     103           2 :     add_enum_reloption(di_relopt_kind, "option_enum",
     104             :                        "Enum option for dummy_index_am",
     105             :                        dummyAmEnumValues,
     106             :                        DUMMY_AM_ENUM_ONE,
     107             :                        "Valid values are \"one\" and \"two\".",
     108             :                        AccessExclusiveLock);
     109           2 :     di_relopt_tab[3].optname = "option_enum";
     110           2 :     di_relopt_tab[3].opttype = RELOPT_TYPE_ENUM;
     111           2 :     di_relopt_tab[3].offset = offsetof(DummyIndexOptions, option_enum);
     112             : 
     113           2 :     add_string_reloption(di_relopt_kind, "option_string_val",
     114             :                          "String option for dummy_index_am with non-NULL default",
     115             :                          "DefaultValue", &validate_string_option,
     116             :                          AccessExclusiveLock);
     117           2 :     di_relopt_tab[4].optname = "option_string_val";
     118           2 :     di_relopt_tab[4].opttype = RELOPT_TYPE_STRING;
     119           2 :     di_relopt_tab[4].offset = offsetof(DummyIndexOptions,
     120             :                                        option_string_val_offset);
     121             : 
     122             :     /*
     123             :      * String option for dummy_index_am with NULL default, and without
     124             :      * description.
     125             :      */
     126           2 :     add_string_reloption(di_relopt_kind, "option_string_null",
     127             :                          NULL,  /* description */
     128             :                          NULL, &validate_string_option,
     129             :                          AccessExclusiveLock);
     130           2 :     di_relopt_tab[5].optname = "option_string_null";
     131           2 :     di_relopt_tab[5].opttype = RELOPT_TYPE_STRING;
     132           2 :     di_relopt_tab[5].offset = offsetof(DummyIndexOptions,
     133             :                                        option_string_null_offset);
     134           2 : }
     135             : 
     136             : 
     137             : /*
     138             :  * Build a new index.
     139             :  */
     140             : static IndexBuildResult *
     141           4 : dibuild(Relation heap, Relation index, IndexInfo *indexInfo)
     142             : {
     143             :     IndexBuildResult *result;
     144             : 
     145           4 :     result = (IndexBuildResult *) palloc(sizeof(IndexBuildResult));
     146             : 
     147             :     /* let's pretend that no tuples were scanned */
     148           4 :     result->heap_tuples = 0;
     149             :     /* and no index tuples were created (that is true) */
     150           4 :     result->index_tuples = 0;
     151             : 
     152           4 :     return result;
     153             : }
     154             : 
     155             : /*
     156             :  * Build an empty index for the initialiation fork.
     157             :  */
     158             : static void
     159           0 : dibuildempty(Relation index)
     160             : {
     161             :     /* No need to build an init fork for a dummy index */
     162           0 : }
     163             : 
     164             : /*
     165             :  * Insert new tuple to index AM.
     166             :  */
     167             : static bool
     168           0 : diinsert(Relation index, Datum *values, bool *isnull,
     169             :          ItemPointer ht_ctid, Relation heapRel,
     170             :          IndexUniqueCheck checkUnique,
     171             :          IndexInfo *indexInfo)
     172             : {
     173             :     /* nothing to do */
     174           0 :     return false;
     175             : }
     176             : 
     177             : /*
     178             :  * Bulk deletion of all index entries pointing to a set of table tuples.
     179             :  */
     180             : static IndexBulkDeleteResult *
     181           0 : dibulkdelete(IndexVacuumInfo *info, IndexBulkDeleteResult *stats,
     182             :              IndexBulkDeleteCallback callback, void *callback_state)
     183             : {
     184             :     /*
     185             :      * There is nothing to delete.  Return NULL as there is nothing to pass to
     186             :      * amvacuumcleanup.
     187             :      */
     188           0 :     return NULL;
     189             : }
     190             : 
     191             : /*
     192             :  * Post-VACUUM cleanup for index AM.
     193             :  */
     194             : static IndexBulkDeleteResult *
     195           0 : divacuumcleanup(IndexVacuumInfo *info, IndexBulkDeleteResult *stats)
     196             : {
     197             :     /* Index has not been modified, so returning NULL is fine */
     198           0 :     return NULL;
     199             : }
     200             : 
     201             : /*
     202             :  * Estimate cost of index AM.
     203             :  */
     204             : static void
     205           0 : dicostestimate(PlannerInfo *root, IndexPath *path, double loop_count,
     206             :                Cost *indexStartupCost, Cost *indexTotalCost,
     207             :                Selectivity *indexSelectivity, double *indexCorrelation,
     208             :                double *indexPages)
     209             : {
     210             :     /* Tell planner to never use this index! */
     211           0 :     *indexStartupCost = 1.0e10;
     212           0 :     *indexTotalCost = 1.0e10;
     213             : 
     214             :     /* Do not care about the rest */
     215           0 :     *indexSelectivity = 1;
     216           0 :     *indexCorrelation = 0;
     217           0 :     *indexPages = 1;
     218           0 : }
     219             : 
     220             : /*
     221             :  * Parse relation options for index AM, returning a DummyIndexOptions
     222             :  * structure filled with option values.
     223             :  */
     224             : static bytea *
     225         138 : dioptions(Datum reloptions, bool validate)
     226             : {
     227         138 :     return (bytea *) build_reloptions(reloptions, validate,
     228             :                                       di_relopt_kind,
     229             :                                       sizeof(DummyIndexOptions),
     230             :                                       di_relopt_tab, lengthof(di_relopt_tab));
     231             : }
     232             : 
     233             : /*
     234             :  * Validator for index AM.
     235             :  */
     236             : static bool
     237           0 : divalidate(Oid opclassoid)
     238             : {
     239             :     /* Index is dummy so we are happy with any opclass */
     240           0 :     return true;
     241             : }
     242             : 
     243             : /*
     244             :  * Begin scan of index AM.
     245             :  */
     246             : static IndexScanDesc
     247           0 : dibeginscan(Relation r, int nkeys, int norderbys)
     248             : {
     249             :     IndexScanDesc scan;
     250             : 
     251             :     /* Let's pretend we are doing something */
     252           0 :     scan = RelationGetIndexScan(r, nkeys, norderbys);
     253           0 :     return scan;
     254             : }
     255             : 
     256             : /*
     257             :  * Rescan of index AM.
     258             :  */
     259             : static void
     260           0 : direscan(IndexScanDesc scan, ScanKey scankey, int nscankeys,
     261             :          ScanKey orderbys, int norderbys)
     262             : {
     263             :     /* nothing to do */
     264           0 : }
     265             : 
     266             : /*
     267             :  * End scan of index AM.
     268             :  */
     269             : static void
     270           0 : diendscan(IndexScanDesc scan)
     271             : {
     272             :     /* nothing to do */
     273           0 : }
     274             : 
     275             : /*
     276             :  * Index AM handler function: returns IndexAmRoutine with access method
     277             :  * parameters and callbacks.
     278             :  */
     279             : Datum
     280          66 : dihandler(PG_FUNCTION_ARGS)
     281             : {
     282          66 :     IndexAmRoutine *amroutine = makeNode(IndexAmRoutine);
     283             : 
     284          66 :     amroutine->amstrategies = 0;
     285          66 :     amroutine->amsupport = 1;
     286          66 :     amroutine->amcanorder = false;
     287          66 :     amroutine->amcanorderbyop = false;
     288          66 :     amroutine->amcanbackward = false;
     289          66 :     amroutine->amcanunique = false;
     290          66 :     amroutine->amcanmulticol = false;
     291          66 :     amroutine->amoptionalkey = false;
     292          66 :     amroutine->amsearcharray = false;
     293          66 :     amroutine->amsearchnulls = false;
     294          66 :     amroutine->amstorage = false;
     295          66 :     amroutine->amclusterable = false;
     296          66 :     amroutine->ampredlocks = false;
     297          66 :     amroutine->amcanparallel = false;
     298          66 :     amroutine->amcaninclude = false;
     299          66 :     amroutine->amusemaintenanceworkmem = false;
     300          66 :     amroutine->amparallelvacuumoptions = VACUUM_OPTION_NO_PARALLEL;
     301          66 :     amroutine->amkeytype = InvalidOid;
     302             : 
     303          66 :     amroutine->ambuild = dibuild;
     304          66 :     amroutine->ambuildempty = dibuildempty;
     305          66 :     amroutine->aminsert = diinsert;
     306          66 :     amroutine->ambulkdelete = dibulkdelete;
     307          66 :     amroutine->amvacuumcleanup = divacuumcleanup;
     308          66 :     amroutine->amcanreturn = NULL;
     309          66 :     amroutine->amcostestimate = dicostestimate;
     310          66 :     amroutine->amoptions = dioptions;
     311          66 :     amroutine->amproperty = NULL;
     312          66 :     amroutine->ambuildphasename = NULL;
     313          66 :     amroutine->amvalidate = divalidate;
     314          66 :     amroutine->ambeginscan = dibeginscan;
     315          66 :     amroutine->amrescan = direscan;
     316          66 :     amroutine->amgettuple = NULL;
     317          66 :     amroutine->amgetbitmap = NULL;
     318          66 :     amroutine->amendscan = diendscan;
     319          66 :     amroutine->ammarkpos = NULL;
     320          66 :     amroutine->amrestrpos = NULL;
     321          66 :     amroutine->amestimateparallelscan = NULL;
     322          66 :     amroutine->aminitparallelscan = NULL;
     323          66 :     amroutine->amparallelrescan = NULL;
     324             : 
     325          66 :     PG_RETURN_POINTER(amroutine);
     326             : }
     327             : 
     328             : void
     329           2 : _PG_init(void)
     330             : {
     331           2 :     create_reloptions_table();
     332           2 : }

Generated by: LCOV version 1.13