LCOV - code coverage report
Current view: top level - src/test/modules/delay_execution - delay_execution.c (source / functions) Hit Total Coverage
Test: PostgreSQL 14beta2 Lines: 14 18 77.8 %
Date: 2021-06-24 16:07:26 Functions: 3 4 75.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*-------------------------------------------------------------------------
       2             :  *
       3             :  * delay_execution.c
       4             :  *      Test module to allow delay between parsing and execution of a query.
       5             :  *
       6             :  * The delay is implemented by taking and immediately releasing a specified
       7             :  * advisory lock.  If another process has previously taken that lock, the
       8             :  * current process will be blocked until the lock is released; otherwise,
       9             :  * there's no effect.  This allows an isolationtester script to reliably
      10             :  * test behaviors where some specified action happens in another backend
      11             :  * between parsing and execution of any desired query.
      12             :  *
      13             :  * Copyright (c) 2020-2021, PostgreSQL Global Development Group
      14             :  *
      15             :  * IDENTIFICATION
      16             :  *    src/test/modules/delay_execution/delay_execution.c
      17             :  *
      18             :  *-------------------------------------------------------------------------
      19             :  */
      20             : 
      21             : #include "postgres.h"
      22             : 
      23             : #include <limits.h>
      24             : 
      25             : #include "optimizer/planner.h"
      26             : #include "utils/builtins.h"
      27             : #include "utils/guc.h"
      28             : #include "utils/inval.h"
      29             : 
      30             : 
      31           4 : PG_MODULE_MAGIC;
      32             : 
      33             : /* GUC: advisory lock ID to use.  Zero disables the feature. */
      34             : static int  post_planning_lock_id = 0;
      35             : 
      36             : /* Save previous planner hook user to be a good citizen */
      37             : static planner_hook_type prev_planner_hook = NULL;
      38             : 
      39             : /* Module load/unload functions */
      40             : void        _PG_init(void);
      41             : void        _PG_fini(void);
      42             : 
      43             : 
      44             : /* planner_hook function to provide the desired delay */
      45             : static PlannedStmt *
      46          30 : delay_execution_planner(Query *parse, const char *query_string,
      47             :                         int cursorOptions, ParamListInfo boundParams)
      48             : {
      49             :     PlannedStmt *result;
      50             : 
      51             :     /* Invoke the planner, possibly via a previous hook user */
      52          30 :     if (prev_planner_hook)
      53           0 :         result = prev_planner_hook(parse, query_string, cursorOptions,
      54             :                                    boundParams);
      55             :     else
      56          30 :         result = standard_planner(parse, query_string, cursorOptions,
      57             :                                   boundParams);
      58             : 
      59             :     /* If enabled, delay by taking and releasing the specified lock */
      60          30 :     if (post_planning_lock_id != 0)
      61             :     {
      62          30 :         DirectFunctionCall1(pg_advisory_lock_int8,
      63             :                             Int64GetDatum((int64) post_planning_lock_id));
      64          30 :         DirectFunctionCall1(pg_advisory_unlock_int8,
      65             :                             Int64GetDatum((int64) post_planning_lock_id));
      66             : 
      67             :         /*
      68             :          * Ensure that we notice any pending invalidations, since the advisory
      69             :          * lock functions don't do this.
      70             :          */
      71          30 :         AcceptInvalidationMessages();
      72             :     }
      73             : 
      74          30 :     return result;
      75             : }
      76             : 
      77             : /* Module load function */
      78             : void
      79           4 : _PG_init(void)
      80             : {
      81             :     /* Set up the GUC to control which lock is used */
      82           4 :     DefineCustomIntVariable("delay_execution.post_planning_lock_id",
      83             :                             "Sets the advisory lock ID to be locked/unlocked after planning.",
      84             :                             "Zero disables the delay.",
      85             :                             &post_planning_lock_id,
      86             :                             0,
      87             :                             0, INT_MAX,
      88             :                             PGC_USERSET,
      89             :                             0,
      90             :                             NULL,
      91             :                             NULL,
      92             :                             NULL);
      93             : 
      94             :     /* Install our hook */
      95           4 :     prev_planner_hook = planner_hook;
      96           4 :     planner_hook = delay_execution_planner;
      97           4 : }
      98             : 
      99             : /* Module unload function (pro forma, not used currently) */
     100             : void
     101           0 : _PG_fini(void)
     102             : {
     103           0 :     planner_hook = prev_planner_hook;
     104           0 : }

Generated by: LCOV version 1.13