LCOV - differential code coverage report
Current view: top level - src/backend/utils/adt - misc.c (source / functions) Coverage Total Hit UNC UBC GNC CBC DUB DCB
Current: 806555e3000d0b0e0c536c1dc65548128d457d86 vs 1d325ad99cb2dec0e8b45ba36909ee0a497d2a57 Lines: 78.8 % 359 283 1 75 16 267 4 18
Current Date: 2025-12-17 08:58:58 +0900 Functions: 92.3 % 26 24 2 4 20
Baseline: lcov-20251217-005640-baseline Branches: 62.4 % 221 138 5 78 9 129
Baseline Date: 2025-12-16 12:57:12 -0800 Line coverage date bins:
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
(1,7] days: 100.0 % 1 1 1
(30,360] days: 93.8 % 16 15 1 15
(360..) days: 78.1 % 342 267 75 267
Function coverage date bins:
(30,360] days: 100.0 % 1 1 1
(360..) days: 92.0 % 25 23 2 3 20
Branch coverage date bins:
(30,360] days: 64.3 % 14 9 5 9
(360..) days: 62.3 % 207 129 78 129

 Age         Owner                    Branch data    TLA  Line data    Source code
                                  1                 :                : /*-------------------------------------------------------------------------
                                  2                 :                :  *
                                  3                 :                :  * misc.c
                                  4                 :                :  *
                                  5                 :                :  *
                                  6                 :                :  * Portions Copyright (c) 1996-2025, PostgreSQL Global Development Group
                                  7                 :                :  * Portions Copyright (c) 1994, Regents of the University of California
                                  8                 :                :  *
                                  9                 :                :  *
                                 10                 :                :  * IDENTIFICATION
                                 11                 :                :  *    src/backend/utils/adt/misc.c
                                 12                 :                :  *
                                 13                 :                :  *-------------------------------------------------------------------------
                                 14                 :                :  */
                                 15                 :                : #include "postgres.h"
                                 16                 :                : 
                                 17                 :                : #include <sys/file.h>
                                 18                 :                : #include <sys/stat.h>
                                 19                 :                : #include <dirent.h>
                                 20                 :                : #include <fcntl.h>
                                 21                 :                : #include <math.h>
                                 22                 :                : #include <unistd.h>
                                 23                 :                : 
                                 24                 :                : #include "access/htup_details.h"
                                 25                 :                : #include "access/sysattr.h"
                                 26                 :                : #include "access/table.h"
                                 27                 :                : #include "catalog/pg_tablespace.h"
                                 28                 :                : #include "catalog/pg_type.h"
                                 29                 :                : #include "catalog/system_fk_info.h"
                                 30                 :                : #include "commands/tablespace.h"
                                 31                 :                : #include "common/keywords.h"
                                 32                 :                : #include "funcapi.h"
                                 33                 :                : #include "miscadmin.h"
                                 34                 :                : #include "nodes/miscnodes.h"
                                 35                 :                : #include "parser/parse_type.h"
                                 36                 :                : #include "parser/scansup.h"
                                 37                 :                : #include "pgstat.h"
                                 38                 :                : #include "postmaster/syslogger.h"
                                 39                 :                : #include "rewrite/rewriteHandler.h"
                                 40                 :                : #include "storage/fd.h"
                                 41                 :                : #include "storage/latch.h"
                                 42                 :                : #include "tcop/tcopprot.h"
                                 43                 :                : #include "utils/builtins.h"
                                 44                 :                : #include "utils/fmgroids.h"
                                 45                 :                : #include "utils/lsyscache.h"
                                 46                 :                : #include "utils/ruleutils.h"
                                 47                 :                : #include "utils/syscache.h"
                                 48                 :                : #include "utils/timestamp.h"
                                 49                 :                : 
                                 50                 :                : 
                                 51                 :                : /*
                                 52                 :                :  * structure to cache metadata needed in pg_input_is_valid_common
                                 53                 :                :  */
                                 54                 :                : typedef struct ValidIOData
                                 55                 :                : {
                                 56                 :                :     Oid         typoid;
                                 57                 :                :     int32       typmod;
                                 58                 :                :     bool        typname_constant;
                                 59                 :                :     Oid         typiofunc;
                                 60                 :                :     Oid         typioparam;
                                 61                 :                :     FmgrInfo    inputproc;
                                 62                 :                : } ValidIOData;
                                 63                 :                : 
                                 64                 :                : static bool pg_input_is_valid_common(FunctionCallInfo fcinfo,
                                 65                 :                :                                      text *txt, text *typname,
                                 66                 :                :                                      ErrorSaveContext *escontext);
                                 67                 :                : 
                                 68                 :                : 
                                 69                 :                : /*
                                 70                 :                :  * Common subroutine for num_nulls() and num_nonnulls().
                                 71                 :                :  * Returns true if successful, false if function should return NULL.
                                 72                 :                :  * If successful, total argument count and number of nulls are
                                 73                 :                :  * returned into *nargs and *nulls.
                                 74                 :                :  */
                                 75                 :                : static bool
 3604 tgl@sss.pgh.pa.us          76                 :CBC          60 : count_nulls(FunctionCallInfo fcinfo,
                                 77                 :                :             int32 *nargs, int32 *nulls)
                                 78                 :                : {
                                 79                 :             60 :     int32       count = 0;
                                 80                 :                :     int         i;
                                 81                 :                : 
                                 82                 :                :     /* Did we get a VARIADIC array argument, or separate arguments? */
                                 83         [ +  + ]:             60 :     if (get_fn_expr_variadic(fcinfo->flinfo))
                                 84                 :                :     {
                                 85                 :                :         ArrayType  *arr;
                                 86                 :                :         int         ndims,
                                 87                 :                :                     nitems,
                                 88                 :                :                    *dims;
                                 89                 :                :         bits8      *bitmap;
                                 90                 :                : 
                                 91         [ -  + ]:             30 :         Assert(PG_NARGS() == 1);
                                 92                 :                : 
                                 93                 :                :         /*
                                 94                 :                :          * If we get a null as VARIADIC array argument, we can't say anything
                                 95                 :                :          * useful about the number of elements, so return NULL.  This behavior
                                 96                 :                :          * is consistent with other variadic functions - see concat_internal.
                                 97                 :                :          */
                                 98         [ +  + ]:             30 :         if (PG_ARGISNULL(0))
                                 99                 :              6 :             return false;
                                100                 :                : 
                                101                 :                :         /*
                                102                 :                :          * Non-null argument had better be an array.  We assume that any call
                                103                 :                :          * context that could let get_fn_expr_variadic return true will have
                                104                 :                :          * checked that a VARIADIC-labeled parameter actually is an array.  So
                                105                 :                :          * it should be okay to just Assert that it's an array rather than
                                106                 :                :          * doing a full-fledged error check.
                                107                 :                :          */
                                108         [ -  + ]:             24 :         Assert(OidIsValid(get_base_element_type(get_fn_expr_argtype(fcinfo->flinfo, 0))));
                                109                 :                : 
                                110                 :                :         /* OK, safe to fetch the array value */
                                111                 :             24 :         arr = PG_GETARG_ARRAYTYPE_P(0);
                                112                 :                : 
                                113                 :                :         /* Count the array elements */
                                114                 :             24 :         ndims = ARR_NDIM(arr);
                                115                 :             24 :         dims = ARR_DIMS(arr);
                                116                 :             24 :         nitems = ArrayGetNItems(ndims, dims);
                                117                 :                : 
                                118                 :                :         /* Count those that are NULL */
                                119         [ +  + ]:             24 :         bitmap = ARR_NULLBITMAP(arr);
                                120         [ +  + ]:             24 :         if (bitmap)
                                121                 :                :         {
                                122                 :             12 :             int         bitmask = 1;
                                123                 :                : 
                                124         [ +  + ]:            636 :             for (i = 0; i < nitems; i++)
                                125                 :                :             {
                                126         [ +  + ]:            624 :                 if ((*bitmap & bitmask) == 0)
                                127                 :             12 :                     count++;
                                128                 :                : 
                                129                 :            624 :                 bitmask <<= 1;
                                130         [ +  + ]:            624 :                 if (bitmask == 0x100)
                                131                 :                :                 {
                                132                 :             72 :                     bitmap++;
                                133                 :             72 :                     bitmask = 1;
                                134                 :                :                 }
                                135                 :                :             }
                                136                 :                :         }
                                137                 :                : 
                                138                 :             24 :         *nargs = nitems;
                                139                 :             24 :         *nulls = count;
                                140                 :                :     }
                                141                 :                :     else
                                142                 :                :     {
                                143                 :                :         /* Separate arguments, so just count 'em */
                                144         [ +  + ]:            102 :         for (i = 0; i < PG_NARGS(); i++)
                                145                 :                :         {
                                146         [ +  + ]:             72 :             if (PG_ARGISNULL(i))
                                147                 :             42 :                 count++;
                                148                 :                :         }
                                149                 :                : 
                                150                 :             30 :         *nargs = PG_NARGS();
                                151                 :             30 :         *nulls = count;
                                152                 :                :     }
                                153                 :                : 
                                154                 :             54 :     return true;
                                155                 :                : }
                                156                 :                : 
                                157                 :                : /*
                                158                 :                :  * num_nulls()
                                159                 :                :  *  Count the number of NULL arguments
                                160                 :                :  */
                                161                 :                : Datum
                                162                 :             30 : pg_num_nulls(PG_FUNCTION_ARGS)
                                163                 :                : {
                                164                 :                :     int32       nargs,
                                165                 :                :                 nulls;
                                166                 :                : 
                                167         [ +  + ]:             30 :     if (!count_nulls(fcinfo, &nargs, &nulls))
                                168                 :              3 :         PG_RETURN_NULL();
                                169                 :                : 
                                170                 :             27 :     PG_RETURN_INT32(nulls);
                                171                 :                : }
                                172                 :                : 
                                173                 :                : /*
                                174                 :                :  * num_nonnulls()
                                175                 :                :  *  Count the number of non-NULL arguments
                                176                 :                :  */
                                177                 :                : Datum
                                178                 :             30 : pg_num_nonnulls(PG_FUNCTION_ARGS)
                                179                 :                : {
                                180                 :                :     int32       nargs,
                                181                 :                :                 nulls;
                                182                 :                : 
                                183         [ +  + ]:             30 :     if (!count_nulls(fcinfo, &nargs, &nulls))
                                184                 :              3 :         PG_RETURN_NULL();
                                185                 :                : 
                                186                 :             27 :     PG_RETURN_INT32(nargs - nulls);
                                187                 :                : }
                                188                 :                : 
                                189                 :                : /*
                                190                 :                :  * error_on_null()
                                191                 :                :  *  Check if the input is the NULL value
                                192                 :                :  */
                                193                 :                : Datum
   56 michael@paquier.xyz       194                 :GNC          18 : pg_error_on_null(PG_FUNCTION_ARGS)
                                195                 :                : {
                                196         [ +  + ]:             18 :     if (PG_ARGISNULL(0))
                                197         [ +  - ]:              6 :         ereport(ERROR,
                                198                 :                :                 (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
                                199                 :                :                  errmsg("null value not allowed")));
                                200                 :                : 
                                201                 :             12 :     PG_RETURN_DATUM(PG_GETARG_DATUM(0));
                                202                 :                : }
                                203                 :                : 
                                204                 :                : /*
                                205                 :                :  * current_database()
                                206                 :                :  *  Expose the current database to the user
                                207                 :                :  */
                                208                 :                : Datum
 8520 bruce@momjian.us          209                 :CBC        3878 : current_database(PG_FUNCTION_ARGS)
                                210                 :                : {
                                211                 :                :     Name        db;
                                212                 :                : 
                                213                 :           3878 :     db = (Name) palloc(NAMEDATALEN);
                                214                 :                : 
 8209 peter_e@gmx.net           215                 :           3878 :     namestrcpy(db, get_database_name(MyDatabaseId));
 8520 bruce@momjian.us          216                 :           3878 :     PG_RETURN_NAME(db);
                                217                 :                : }
                                218                 :                : 
                                219                 :                : 
                                220                 :                : /*
                                221                 :                :  * current_query()
                                222                 :                :  *  Expose the current query to the user (useful in stored procedures)
                                223                 :                :  *  We might want to use ActivePortal->sourceText someday.
                                224                 :                :  */
                                225                 :                : Datum
 6466 bruce@momjian.us          226                 :UBC           0 : current_query(PG_FUNCTION_ARGS)
                                227                 :                : {
                                228                 :                :     /* there is no easy way to access the more concise 'query_string' */
 6187                           229         [ #  # ]:              0 :     if (debug_query_string)
                                230                 :              0 :         PG_RETURN_TEXT_P(cstring_to_text(debug_query_string));
                                231                 :                :     else
                                232                 :              0 :         PG_RETURN_NULL();
                                233                 :                : }
                                234                 :                : 
                                235                 :                : /* Function to find out which databases make use of a tablespace */
                                236                 :                : 
                                237                 :                : Datum
 7780 bruce@momjian.us          238                 :CBC           3 : pg_tablespace_databases(PG_FUNCTION_ARGS)
                                239                 :                : {
 2102 tgl@sss.pgh.pa.us         240                 :              3 :     Oid         tablespaceOid = PG_GETARG_OID(0);
                                241                 :              3 :     ReturnSetInfo *rsinfo = (ReturnSetInfo *) fcinfo->resultinfo;
                                242                 :                :     char       *location;
                                243                 :                :     DIR        *dirdesc;
                                244                 :                :     struct dirent *de;
                                245                 :                : 
 1156 michael@paquier.xyz       246                 :              3 :     InitMaterializedSRF(fcinfo, MAT_SRF_USE_EXPECTED_DESC);
                                247                 :                : 
 2102 tgl@sss.pgh.pa.us         248         [ -  + ]:              3 :     if (tablespaceOid == GLOBALTABLESPACE_OID)
                                249                 :                :     {
 2102 tgl@sss.pgh.pa.us         250         [ #  # ]:UBC           0 :         ereport(WARNING,
                                251                 :                :                 (errmsg("global tablespace never has databases")));
                                252                 :                :         /* return empty tuplestore */
                                253                 :              0 :         return (Datum) 0;
                                254                 :                :     }
                                255                 :                : 
 2102 tgl@sss.pgh.pa.us         256         [ +  - ]:CBC           3 :     if (tablespaceOid == DEFAULTTABLESPACE_OID)
 1198 drowley@postgresql.o      257                 :              3 :         location = "base";
                                258                 :                :     else
  470 michael@paquier.xyz       259                 :UBC           0 :         location = psprintf("%s/%u/%s", PG_TBLSPC_DIR, tablespaceOid,
                                260                 :                :                             TABLESPACE_VERSION_DIRECTORY);
                                261                 :                : 
 2102 tgl@sss.pgh.pa.us         262                 :CBC           3 :     dirdesc = AllocateDir(location);
                                263                 :                : 
                                264         [ -  + ]:              3 :     if (!dirdesc)
                                265                 :                :     {
                                266                 :                :         /* the only expected error is ENOENT */
 2102 tgl@sss.pgh.pa.us         267         [ #  # ]:UBC           0 :         if (errno != ENOENT)
                                268         [ #  # ]:              0 :             ereport(ERROR,
                                269                 :                :                     (errcode_for_file_access(),
                                270                 :                :                      errmsg("could not open directory \"%s\": %m",
                                271                 :                :                             location)));
                                272         [ #  # ]:              0 :         ereport(WARNING,
                                273                 :                :                 (errmsg("%u is not a tablespace OID", tablespaceOid)));
                                274                 :                :         /* return empty tuplestore */
                                275                 :              0 :         return (Datum) 0;
                                276                 :                :     }
                                277                 :                : 
 2102 tgl@sss.pgh.pa.us         278         [ +  + ]:CBC          27 :     while ((de = ReadDir(dirdesc, location)) != NULL)
                                279                 :                :     {
 7780 bruce@momjian.us          280                 :             24 :         Oid         datOid = atooid(de->d_name);
                                281                 :                :         char       *subdir;
                                282                 :                :         bool        isempty;
                                283                 :                :         Datum       values[1];
                                284                 :                :         bool        nulls[1];
                                285                 :                : 
                                286                 :                :         /* this test skips . and .., but is awfully weak */
 7838 mail@joeconway.com        287         [ +  + ]:             24 :         if (!datOid)
                                288                 :              9 :             continue;
                                289                 :                : 
                                290                 :                :         /* if database subdir is empty, don't report tablespace as used */
                                291                 :                : 
 2102 tgl@sss.pgh.pa.us         292                 :             15 :         subdir = psprintf("%s/%s", location, de->d_name);
 2935                           293                 :             15 :         isempty = directory_is_empty(subdir);
 7486                           294                 :             15 :         pfree(subdir);
                                295                 :                : 
 2935                           296         [ -  + ]:             15 :         if (isempty)
 7806 tgl@sss.pgh.pa.us         297                 :UBC           0 :             continue;           /* indeed, nothing in it */
                                298                 :                : 
 2102 tgl@sss.pgh.pa.us         299                 :CBC          15 :         values[0] = ObjectIdGetDatum(datOid);
                                300                 :             15 :         nulls[0] = false;
                                301                 :                : 
 1381 michael@paquier.xyz       302                 :             15 :         tuplestore_putvalues(rsinfo->setResult, rsinfo->setDesc,
                                303                 :                :                              values, nulls);
                                304                 :                :     }
                                305                 :                : 
 2102 tgl@sss.pgh.pa.us         306                 :              3 :     FreeDir(dirdesc);
                                307                 :              3 :     return (Datum) 0;
                                308                 :                : }
                                309                 :                : 
                                310                 :                : 
                                311                 :                : /*
                                312                 :                :  * pg_tablespace_location - get location for a tablespace
                                313                 :                :  */
                                314                 :                : Datum
 5124 magnus@hagander.net       315                 :            138 : pg_tablespace_location(PG_FUNCTION_ARGS)
                                316                 :                : {
 4938 bruce@momjian.us          317                 :            138 :     Oid         tablespaceOid = PG_GETARG_OID(0);
                                318                 :                :     char       *tablespaceLoc;
                                319                 :                : 
                                320                 :                :     /* Get LOCATION string from its OID */
   35 alvherre@kurilemu.de      321                 :GNC         138 :     tablespaceLoc = get_tablespace_location(tablespaceOid);
                                322                 :                : 
                                323                 :            138 :     PG_RETURN_TEXT_P(cstring_to_text(tablespaceLoc));
                                324                 :                : }
                                325                 :                : 
                                326                 :                : /*
                                327                 :                :  * pg_sleep - delay for N seconds
                                328                 :                :  */
                                329                 :                : Datum
 7280 tgl@sss.pgh.pa.us         330                 :CBC         117 : pg_sleep(PG_FUNCTION_ARGS)
                                331                 :                : {
                                332                 :            117 :     float8      secs = PG_GETARG_FLOAT8(0);
                                333                 :                :     int64       usecs;
                                334                 :                :     TimestampTz endtime;
                                335                 :                : 
                                336                 :                :     /*
                                337                 :                :      * Convert the delay to int64 microseconds, rounding up any fraction, and
                                338                 :                :      * silently limiting it to PG_INT64_MAX/2 microseconds (about 150K years)
                                339                 :                :      * to ensure the computation of endtime won't overflow.  Historically
                                340                 :                :      * we've treated NaN as "no wait", not an error, so keep that behavior.
                                341                 :                :      */
   83 tgl@sss.pgh.pa.us         342   [ +  -  -  + ]:GNC         117 :     if (isnan(secs) || secs <= 0.0)
   83 tgl@sss.pgh.pa.us         343                 :UNC           0 :         PG_RETURN_VOID();
   83 tgl@sss.pgh.pa.us         344                 :GNC         117 :     secs *= USECS_PER_SEC;      /* we assume overflow will produce +Inf */
                                345                 :            117 :     secs = ceil(secs);          /* round up any fractional microsecond */
                                346         [ +  - ]:            117 :     usecs = (int64) Min(secs, (float8) (PG_INT64_MAX / 2));
                                347                 :                : 
                                348                 :                :     /*
                                349                 :                :      * We sleep using WaitLatch, to ensure that we'll wake up promptly if an
                                350                 :                :      * important signal (such as SIGALRM or SIGINT) arrives.  Because
                                351                 :                :      * WaitLatch's upper limit of delay is INT_MAX milliseconds, and the user
                                352                 :                :      * might ask for more than that, we sleep for at most 10 minutes and then
                                353                 :                :      * loop.
                                354                 :                :      *
                                355                 :                :      * By computing the intended stop time initially, we avoid accumulation of
                                356                 :                :      * extra delay across multiple sleeps.  This also ensures we won't delay
                                357                 :                :      * less than the specified time when WaitLatch is terminated early by a
                                358                 :                :      * non-query-canceling signal such as SIGHUP.
                                359                 :                :      */
                                360                 :            117 :     endtime = GetCurrentTimestamp() + usecs;
                                361                 :                : 
                                362                 :                :     for (;;)
 7280 tgl@sss.pgh.pa.us         363                 :CBC         117 :     {
                                364                 :                :         TimestampTz delay;
                                365                 :                :         long        delay_ms;
                                366                 :                : 
                                367         [ +  + ]:            234 :         CHECK_FOR_INTERRUPTS();
                                368                 :                : 
   83 tgl@sss.pgh.pa.us         369                 :GNC         222 :         delay = endtime - GetCurrentTimestamp();
                                370         [ -  + ]:            222 :         if (delay >= 600 * USECS_PER_SEC)
 4568 tgl@sss.pgh.pa.us         371                 :UBC           0 :             delay_ms = 600000;
   83 tgl@sss.pgh.pa.us         372         [ +  + ]:GNC         222 :         else if (delay > 0)
                                373                 :            117 :             delay_ms = (long) ((delay + 999) / 1000);
                                374                 :                :         else
 7280 tgl@sss.pgh.pa.us         375                 :CBC         105 :             break;
                                376                 :                : 
 3990 andres@anarazel.de        377                 :            117 :         (void) WaitLatch(MyLatch,
                                378                 :                :                          WL_LATCH_SET | WL_TIMEOUT | WL_EXIT_ON_PM_DEATH,
                                379                 :                :                          delay_ms,
                                380                 :                :                          WAIT_EVENT_PG_SLEEP);
                                381                 :            117 :         ResetLatch(MyLatch);
                                382                 :                :     }
                                383                 :                : 
 7280 tgl@sss.pgh.pa.us         384                 :            105 :     PG_RETURN_VOID();
                                385                 :                : }
                                386                 :                : 
                                387                 :                : /* Function to return the list of grammar keywords */
                                388                 :                : Datum
 6376 tgl@sss.pgh.pa.us         389                 :UBC           0 : pg_get_keywords(PG_FUNCTION_ARGS)
                                390                 :                : {
                                391                 :                :     FuncCallContext *funcctx;
                                392                 :                : 
                                393         [ #  # ]:              0 :     if (SRF_IS_FIRSTCALL())
                                394                 :                :     {
                                395                 :                :         MemoryContext oldcontext;
                                396                 :                :         TupleDesc   tupdesc;
                                397                 :                : 
                                398                 :              0 :         funcctx = SRF_FIRSTCALL_INIT();
                                399                 :              0 :         oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
                                400                 :                : 
 1092 michael@paquier.xyz       401         [ #  # ]:              0 :         if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE)
                                402         [ #  # ]:              0 :             elog(ERROR, "return type must be a row type");
                                403                 :              0 :         funcctx->tuple_desc = tupdesc;
 6376 tgl@sss.pgh.pa.us         404                 :              0 :         funcctx->attinmeta = TupleDescGetAttInMetadata(tupdesc);
                                405                 :                : 
                                406                 :              0 :         MemoryContextSwitchTo(oldcontext);
                                407                 :                :     }
                                408                 :                : 
                                409                 :              0 :     funcctx = SRF_PERCALL_SETUP();
                                410                 :                : 
 2537                           411         [ #  # ]:              0 :     if (funcctx->call_cntr < ScanKeywords.num_keywords)
                                412                 :                :     {
                                413                 :                :         char       *values[5];
                                414                 :                :         HeapTuple   tuple;
                                415                 :                : 
                                416                 :                :         /* cast-away-const is ugly but alternatives aren't much better */
                                417                 :              0 :         values[0] = unconstify(char *,
                                418                 :                :                                GetScanKeyword(funcctx->call_cntr,
                                419                 :                :                                               &ScanKeywords));
                                420                 :                : 
                                421   [ #  #  #  #  :              0 :         switch (ScanKeywordCategories[funcctx->call_cntr])
                                                 # ]
                                422                 :                :         {
 6376                           423                 :              0 :             case UNRESERVED_KEYWORD:
                                424                 :              0 :                 values[1] = "U";
 1916                           425                 :              0 :                 values[3] = _("unreserved");
 6376                           426                 :              0 :                 break;
                                427                 :              0 :             case COL_NAME_KEYWORD:
                                428                 :              0 :                 values[1] = "C";
 1916                           429                 :              0 :                 values[3] = _("unreserved (cannot be function or type name)");
 6376                           430                 :              0 :                 break;
                                431                 :              0 :             case TYPE_FUNC_NAME_KEYWORD:
                                432                 :              0 :                 values[1] = "T";
 1916                           433                 :              0 :                 values[3] = _("reserved (can be function or type name)");
 6376                           434                 :              0 :                 break;
                                435                 :              0 :             case RESERVED_KEYWORD:
                                436                 :              0 :                 values[1] = "R";
 1916                           437                 :              0 :                 values[3] = _("reserved");
 6376                           438                 :              0 :                 break;
                                439                 :              0 :             default:            /* shouldn't be possible */
                                440                 :              0 :                 values[1] = NULL;
 1916                           441                 :              0 :                 values[3] = NULL;
 6376                           442                 :              0 :                 break;
                                443                 :                :         }
                                444                 :                : 
 1916                           445         [ #  # ]:              0 :         if (ScanKeywordBareLabel[funcctx->call_cntr])
                                446                 :                :         {
                                447                 :              0 :             values[2] = "true";
                                448                 :              0 :             values[4] = _("can be bare label");
                                449                 :                :         }
                                450                 :                :         else
                                451                 :                :         {
                                452                 :              0 :             values[2] = "false";
                                453                 :              0 :             values[4] = _("requires AS");
                                454                 :                :         }
                                455                 :                : 
 6376                           456                 :              0 :         tuple = BuildTupleFromCStrings(funcctx->attinmeta, values);
                                457                 :                : 
                                458                 :              0 :         SRF_RETURN_NEXT(funcctx, HeapTupleGetDatum(tuple));
                                459                 :                :     }
                                460                 :                : 
                                461                 :              0 :     SRF_RETURN_DONE(funcctx);
                                462                 :                : }
                                463                 :                : 
                                464                 :                : 
                                465                 :                : /* Function to return the list of catalog foreign key relationships */
                                466                 :                : Datum
 1779 tgl@sss.pgh.pa.us         467                 :CBC         660 : pg_get_catalog_foreign_keys(PG_FUNCTION_ARGS)
                                468                 :                : {
                                469                 :                :     FuncCallContext *funcctx;
                                470                 :                :     FmgrInfo   *arrayinp;
                                471                 :                : 
                                472         [ +  + ]:            660 :     if (SRF_IS_FIRSTCALL())
                                473                 :                :     {
                                474                 :                :         MemoryContext oldcontext;
                                475                 :                :         TupleDesc   tupdesc;
                                476                 :                : 
                                477                 :              3 :         funcctx = SRF_FIRSTCALL_INIT();
                                478                 :              3 :         oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
                                479                 :                : 
 1092 michael@paquier.xyz       480         [ -  + ]:              3 :         if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE)
 1092 michael@paquier.xyz       481         [ #  # ]:UBC           0 :             elog(ERROR, "return type must be a row type");
 1779 tgl@sss.pgh.pa.us         482                 :CBC           3 :         funcctx->tuple_desc = BlessTupleDesc(tupdesc);
                                483                 :                : 
                                484                 :                :         /*
                                485                 :                :          * We use array_in to convert the C strings in sys_fk_relationships[]
                                486                 :                :          * to text arrays.  But we cannot use DirectFunctionCallN to call
                                487                 :                :          * array_in, and it wouldn't be very efficient if we could.  Fill an
                                488                 :                :          * FmgrInfo to use for the call.
                                489                 :                :          */
    7 michael@paquier.xyz       490                 :GNC           3 :         arrayinp = palloc_object(FmgrInfo);
 1779 tgl@sss.pgh.pa.us         491                 :CBC           3 :         fmgr_info(F_ARRAY_IN, arrayinp);
                                492                 :              3 :         funcctx->user_fctx = arrayinp;
                                493                 :                : 
                                494                 :              3 :         MemoryContextSwitchTo(oldcontext);
                                495                 :                :     }
                                496                 :                : 
                                497                 :            660 :     funcctx = SRF_PERCALL_SETUP();
                                498                 :            660 :     arrayinp = (FmgrInfo *) funcctx->user_fctx;
                                499                 :                : 
                                500         [ +  + ]:            660 :     if (funcctx->call_cntr < lengthof(sys_fk_relationships))
                                501                 :                :     {
                                502                 :            657 :         const SysFKRelationship *fkrel = &sys_fk_relationships[funcctx->call_cntr];
                                503                 :                :         Datum       values[6];
                                504                 :                :         bool        nulls[6];
                                505                 :                :         HeapTuple   tuple;
                                506                 :                : 
                                507                 :            657 :         memset(nulls, false, sizeof(nulls));
                                508                 :                : 
                                509                 :            657 :         values[0] = ObjectIdGetDatum(fkrel->fk_table);
                                510                 :            657 :         values[1] = FunctionCall3(arrayinp,
                                511                 :                :                                   CStringGetDatum(fkrel->fk_columns),
                                512                 :                :                                   ObjectIdGetDatum(TEXTOID),
                                513                 :                :                                   Int32GetDatum(-1));
                                514                 :            657 :         values[2] = ObjectIdGetDatum(fkrel->pk_table);
                                515                 :            657 :         values[3] = FunctionCall3(arrayinp,
                                516                 :                :                                   CStringGetDatum(fkrel->pk_columns),
                                517                 :                :                                   ObjectIdGetDatum(TEXTOID),
                                518                 :                :                                   Int32GetDatum(-1));
                                519                 :            657 :         values[4] = BoolGetDatum(fkrel->is_array);
                                520                 :            657 :         values[5] = BoolGetDatum(fkrel->is_opt);
                                521                 :                : 
                                522                 :            657 :         tuple = heap_form_tuple(funcctx->tuple_desc, values, nulls);
                                523                 :                : 
                                524                 :            657 :         SRF_RETURN_NEXT(funcctx, HeapTupleGetDatum(tuple));
                                525                 :                :     }
                                526                 :                : 
                                527                 :              3 :     SRF_RETURN_DONE(funcctx);
                                528                 :                : }
                                529                 :                : 
                                530                 :                : 
                                531                 :                : /*
                                532                 :                :  * Return the type of the argument.
                                533                 :                :  */
                                534                 :                : Datum
 6253                           535                 :            841 : pg_typeof(PG_FUNCTION_ARGS)
                                536                 :                : {
                                537                 :            841 :     PG_RETURN_OID(get_fn_expr_argtype(fcinfo->flinfo, 0));
                                538                 :                : }
                                539                 :                : 
                                540                 :                : 
                                541                 :                : /*
                                542                 :                :  * Return the base type of the argument.
                                543                 :                :  *      If the given type is a domain, return its base type;
                                544                 :                :  *      otherwise return the type's own OID.
                                545                 :                :  *      Return NULL if the type OID doesn't exist or points to a
                                546                 :                :  *      non-existent base type.
                                547                 :                :  *
                                548                 :                :  * This is a SQL-callable version of getBaseType().  Unlike that function,
                                549                 :                :  * we don't want to fail for a bogus type OID; this is helpful to keep race
                                550                 :                :  * conditions from turning into query failures when scanning the catalogs.
                                551                 :                :  * Hence we need our own implementation.
                                552                 :                :  */
                                553                 :                : Datum
  627                           554                 :              9 : pg_basetype(PG_FUNCTION_ARGS)
                                555                 :                : {
                                556                 :              9 :     Oid         typid = PG_GETARG_OID(0);
                                557                 :                : 
                                558                 :                :     /*
                                559                 :                :      * We loop to find the bottom base type in a stack of domains.
                                560                 :                :      */
                                561                 :                :     for (;;)
                                562                 :              9 :     {
                                563                 :                :         HeapTuple   tup;
                                564                 :                :         Form_pg_type typTup;
                                565                 :                : 
                                566                 :             18 :         tup = SearchSysCache1(TYPEOID, ObjectIdGetDatum(typid));
                                567         [ +  + ]:             18 :         if (!HeapTupleIsValid(tup))
                                568                 :              3 :             PG_RETURN_NULL();   /* return NULL for bogus OID */
                                569                 :             15 :         typTup = (Form_pg_type) GETSTRUCT(tup);
                                570         [ +  + ]:             15 :         if (typTup->typtype != TYPTYPE_DOMAIN)
                                571                 :                :         {
                                572                 :                :             /* Not a domain, so done */
                                573                 :              6 :             ReleaseSysCache(tup);
                                574                 :              6 :             break;
                                575                 :                :         }
                                576                 :                : 
                                577                 :              9 :         typid = typTup->typbasetype;
                                578                 :              9 :         ReleaseSysCache(tup);
                                579                 :                :     }
                                580                 :                : 
                                581                 :              6 :     PG_RETURN_OID(typid);
                                582                 :                : }
                                583                 :                : 
                                584                 :                : 
                                585                 :                : /*
                                586                 :                :  * Implementation of the COLLATE FOR expression; returns the collation
                                587                 :                :  * of the argument.
                                588                 :                :  */
                                589                 :                : Datum
 5038 peter_e@gmx.net           590                 :             15 : pg_collation_for(PG_FUNCTION_ARGS)
                                591                 :                : {
                                592                 :                :     Oid         typeid;
                                593                 :                :     Oid         collid;
                                594                 :                : 
                                595                 :             15 :     typeid = get_fn_expr_argtype(fcinfo->flinfo, 0);
                                596         [ -  + ]:             15 :     if (!typeid)
 5038 peter_e@gmx.net           597                 :UBC           0 :         PG_RETURN_NULL();
 5038 peter_e@gmx.net           598   [ +  +  +  + ]:CBC          15 :     if (!type_is_collatable(typeid) && typeid != UNKNOWNOID)
                                599         [ +  - ]:              3 :         ereport(ERROR,
                                600                 :                :                 (errcode(ERRCODE_DATATYPE_MISMATCH),
                                601                 :                :                  errmsg("collations are not supported by type %s",
                                602                 :                :                         format_type_be(typeid))));
                                603                 :                : 
                                604                 :             12 :     collid = PG_GET_COLLATION();
                                605         [ +  + ]:             12 :     if (!collid)
                                606                 :              3 :         PG_RETURN_NULL();
                                607                 :              9 :     PG_RETURN_TEXT_P(cstring_to_text(generate_collation_name(collid)));
                                608                 :                : }
                                609                 :                : 
                                610                 :                : 
                                611                 :                : /*
                                612                 :                :  * pg_relation_is_updatable - determine which update events the specified
                                613                 :                :  * relation supports.
                                614                 :                :  *
                                615                 :                :  * This relies on relation_is_updatable() in rewriteHandler.c, which see
                                616                 :                :  * for additional information.
                                617                 :                :  */
                                618                 :                : Datum
 4571 tgl@sss.pgh.pa.us         619                 :            447 : pg_relation_is_updatable(PG_FUNCTION_ARGS)
                                620                 :                : {
                                621                 :            447 :     Oid         reloid = PG_GETARG_OID(0);
                                622                 :            447 :     bool        include_triggers = PG_GETARG_BOOL(1);
                                623                 :                : 
 2218                           624                 :            447 :     PG_RETURN_INT32(relation_is_updatable(reloid, NIL, include_triggers, NULL));
                                625                 :                : }
                                626                 :                : 
                                627                 :                : /*
                                628                 :                :  * pg_column_is_updatable - determine whether a column is updatable
                                629                 :                :  *
                                630                 :                :  * This function encapsulates the decision about just what
                                631                 :                :  * information_schema.columns.is_updatable actually means.  It's not clear
                                632                 :                :  * whether deletability of the column's relation should be required, so
                                633                 :                :  * we want that decision in C code where we could change it without initdb.
                                634                 :                :  */
                                635                 :                : Datum
 4571                           636                 :            333 : pg_column_is_updatable(PG_FUNCTION_ARGS)
                                637                 :                : {
                                638                 :            333 :     Oid         reloid = PG_GETARG_OID(0);
                                639                 :            333 :     AttrNumber  attnum = PG_GETARG_INT16(1);
 4443 rhaas@postgresql.org      640                 :            333 :     AttrNumber  col = attnum - FirstLowInvalidHeapAttributeNumber;
 4571 tgl@sss.pgh.pa.us         641                 :            333 :     bool        include_triggers = PG_GETARG_BOOL(2);
                                642                 :                :     int         events;
                                643                 :                : 
                                644                 :                :     /* System columns are never updatable */
                                645         [ -  + ]:            333 :     if (attnum <= 0)
 4571 tgl@sss.pgh.pa.us         646                 :UBC           0 :         PG_RETURN_BOOL(false);
                                647                 :                : 
 2218 tgl@sss.pgh.pa.us         648                 :CBC         333 :     events = relation_is_updatable(reloid, NIL, include_triggers,
                                649                 :                :                                    bms_make_singleton(col));
                                650                 :                : 
                                651                 :                :     /* We require both updatability and deletability of the relation */
                                652                 :                : #define REQ_EVENTS ((1 << CMD_UPDATE) | (1 << CMD_DELETE))
                                653                 :                : 
 4571                           654                 :            333 :     PG_RETURN_BOOL((events & REQ_EVENTS) == REQ_EVENTS);
                                655                 :                : }
                                656                 :                : 
                                657                 :                : 
                                658                 :                : /*
                                659                 :                :  * pg_input_is_valid - test whether string is valid input for datatype.
                                660                 :                :  *
                                661                 :                :  * Returns true if OK, false if not.
                                662                 :                :  *
                                663                 :                :  * This will only work usefully if the datatype's input function has been
                                664                 :                :  * updated to return "soft" errors via errsave/ereturn.
                                665                 :                :  */
                                666                 :                : Datum
 1104                           667                 :            442 : pg_input_is_valid(PG_FUNCTION_ARGS)
                                668                 :                : {
                                669                 :            442 :     text       *txt = PG_GETARG_TEXT_PP(0);
                                670                 :            442 :     text       *typname = PG_GETARG_TEXT_PP(1);
                                671                 :            442 :     ErrorSaveContext escontext = {T_ErrorSaveContext};
                                672                 :                : 
                                673                 :            442 :     PG_RETURN_BOOL(pg_input_is_valid_common(fcinfo, txt, typname,
                                674                 :                :                                             &escontext));
                                675                 :                : }
                                676                 :                : 
                                677                 :                : /*
                                678                 :                :  * pg_input_error_info - test whether string is valid input for datatype.
                                679                 :                :  *
                                680                 :                :  * Returns NULL if OK, else the primary message, detail message, hint message
                                681                 :                :  * and sql error code from the error.
                                682                 :                :  *
                                683                 :                :  * This will only work usefully if the datatype's input function has been
                                684                 :                :  * updated to return "soft" errors via errsave/ereturn.
                                685                 :                :  */
                                686                 :                : Datum
 1023 michael@paquier.xyz       687                 :            626 : pg_input_error_info(PG_FUNCTION_ARGS)
                                688                 :                : {
 1104 tgl@sss.pgh.pa.us         689                 :            626 :     text       *txt = PG_GETARG_TEXT_PP(0);
                                690                 :            626 :     text       *typname = PG_GETARG_TEXT_PP(1);
                                691                 :            626 :     ErrorSaveContext escontext = {T_ErrorSaveContext};
                                692                 :                :     TupleDesc   tupdesc;
                                693                 :                :     Datum       values[4];
                                694                 :                :     bool        isnull[4];
                                695                 :                : 
 1023 michael@paquier.xyz       696         [ -  + ]:            626 :     if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE)
 1023 michael@paquier.xyz       697         [ #  # ]:UBC           0 :         elog(ERROR, "return type must be a row type");
                                698                 :                : 
                                699                 :                :     /* Enable details_wanted */
 1104 tgl@sss.pgh.pa.us         700                 :CBC         626 :     escontext.details_wanted = true;
                                701                 :                : 
                                702         [ +  + ]:            626 :     if (pg_input_is_valid_common(fcinfo, txt, typname,
                                703                 :                :                                  &escontext))
 1023 michael@paquier.xyz       704                 :             16 :         memset(isnull, true, sizeof(isnull));
                                705                 :                :     else
                                706                 :                :     {
                                707                 :                :         char       *sqlstate;
                                708                 :                : 
                                709         [ -  + ]:            595 :         Assert(escontext.error_occurred);
                                710         [ -  + ]:            595 :         Assert(escontext.error_data != NULL);
                                711         [ -  + ]:            595 :         Assert(escontext.error_data->message != NULL);
                                712                 :                : 
                                713                 :            595 :         memset(isnull, false, sizeof(isnull));
                                714                 :                : 
                                715                 :            595 :         values[0] = CStringGetTextDatum(escontext.error_data->message);
                                716                 :                : 
                                717         [ +  + ]:            595 :         if (escontext.error_data->detail != NULL)
                                718                 :            268 :             values[1] = CStringGetTextDatum(escontext.error_data->detail);
                                719                 :                :         else
                                720                 :            327 :             isnull[1] = true;
                                721                 :                : 
                                722         [ -  + ]:            595 :         if (escontext.error_data->hint != NULL)
 1023 michael@paquier.xyz       723                 :UBC           0 :             values[2] = CStringGetTextDatum(escontext.error_data->hint);
                                724                 :                :         else
 1023 michael@paquier.xyz       725                 :CBC         595 :             isnull[2] = true;
                                726                 :                : 
                                727                 :            595 :         sqlstate = unpack_sql_state(escontext.error_data->sqlerrcode);
                                728                 :            595 :         values[3] = CStringGetTextDatum(sqlstate);
                                729                 :                :     }
                                730                 :                : 
                                731                 :            611 :     return HeapTupleGetDatum(heap_form_tuple(tupdesc, values, isnull));
                                732                 :                : }
                                733                 :                : 
                                734                 :                : /* Common subroutine for the above */
                                735                 :                : static bool
 1104 tgl@sss.pgh.pa.us         736                 :           1068 : pg_input_is_valid_common(FunctionCallInfo fcinfo,
                                737                 :                :                          text *txt, text *typname,
                                738                 :                :                          ErrorSaveContext *escontext)
                                739                 :                : {
                                740                 :           1068 :     char       *str = text_to_cstring(txt);
                                741                 :                :     ValidIOData *my_extra;
                                742                 :                :     Datum       converted;
                                743                 :                : 
                                744                 :                :     /*
                                745                 :                :      * We arrange to look up the needed I/O info just once per series of
                                746                 :                :      * calls, assuming the data type doesn't change underneath us.
                                747                 :                :      */
                                748                 :           1068 :     my_extra = (ValidIOData *) fcinfo->flinfo->fn_extra;
                                749         [ +  + ]:           1068 :     if (my_extra == NULL)
                                750                 :                :     {
                                751                 :           2024 :         fcinfo->flinfo->fn_extra =
                                752                 :           1012 :             MemoryContextAlloc(fcinfo->flinfo->fn_mcxt,
                                753                 :                :                                sizeof(ValidIOData));
                                754                 :           1012 :         my_extra = (ValidIOData *) fcinfo->flinfo->fn_extra;
                                755                 :           1012 :         my_extra->typoid = InvalidOid;
                                756                 :                :         /* Detect whether typname argument is constant. */
                                757                 :           1012 :         my_extra->typname_constant = get_fn_expr_arg_stable(fcinfo->flinfo, 1);
                                758                 :                :     }
                                759                 :                : 
                                760                 :                :     /*
                                761                 :                :      * If the typname argument is constant, we only need to parse it the first
                                762                 :                :      * time through.
                                763                 :                :      */
                                764   [ +  +  +  + ]:           1068 :     if (my_extra->typoid == InvalidOid || !my_extra->typname_constant)
                                765                 :                :     {
                                766                 :           1030 :         char       *typnamestr = text_to_cstring(typname);
                                767                 :                :         Oid         typoid;
                                768                 :                : 
                                769                 :                :         /* Parse type-name argument to obtain type OID and encoded typmod. */
 1086                           770                 :           1030 :         (void) parseTypeString(typnamestr, &typoid, &my_extra->typmod, NULL);
                                771                 :                : 
                                772                 :                :         /* Update type-specific info if typoid changed. */
 1104                           773         [ +  + ]:           1030 :         if (my_extra->typoid != typoid)
                                774                 :                :         {
                                775                 :           1020 :             getTypeInputInfo(typoid,
                                776                 :                :                              &my_extra->typiofunc,
                                777                 :                :                              &my_extra->typioparam);
                                778                 :           1020 :             fmgr_info_cxt(my_extra->typiofunc, &my_extra->inputproc,
                                779                 :           1020 :                           fcinfo->flinfo->fn_mcxt);
                                780                 :           1020 :             my_extra->typoid = typoid;
                                781                 :                :         }
                                782                 :                :     }
                                783                 :                : 
                                784                 :                :     /* Now we can try to perform the conversion. */
                                785                 :           1068 :     return InputFunctionCallSafe(&my_extra->inputproc,
                                786                 :                :                                  str,
                                787                 :                :                                  my_extra->typioparam,
                                788                 :                :                                  my_extra->typmod,
                                789                 :                :                                  (Node *) escontext,
                                790                 :                :                                  &converted);
                                791                 :                : }
                                792                 :                : 
                                793                 :                : 
                                794                 :                : /*
                                795                 :                :  * Is character a valid identifier start?
                                796                 :                :  * Must match scan.l's {ident_start} character class.
                                797                 :                :  */
                                798                 :                : static bool
 3561 teodor@sigaev.ru          799                 :           1101 : is_ident_start(unsigned char c)
                                800                 :                : {
                                801                 :                :     /* Underscores and ASCII letters are OK */
                                802         [ -  + ]:           1101 :     if (c == '_')
 3561 teodor@sigaev.ru          803                 :UBC           0 :         return true;
 3561 teodor@sigaev.ru          804   [ +  +  -  +  :CBC        1101 :     if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'))
                                        +  +  +  + ]
                                805                 :           1026 :         return true;
                                806                 :                :     /* Any high-bit-set character is OK (might be part of a multibyte char) */
 3551 tgl@sss.pgh.pa.us         807         [ -  + ]:             75 :     if (IS_HIGHBIT_SET(c))
 3561 teodor@sigaev.ru          808                 :UBC           0 :         return true;
 3561 teodor@sigaev.ru          809                 :CBC          75 :     return false;
                                810                 :                : }
                                811                 :                : 
                                812                 :                : /*
                                813                 :                :  * Is character a valid identifier continuation?
                                814                 :                :  * Must match scan.l's {ident_cont} character class.
                                815                 :                :  */
                                816                 :                : static bool
                                817                 :           1026 : is_ident_cont(unsigned char c)
                                818                 :                : {
                                819                 :                :     /* Can be digit or dollar sign ... */
 3551 tgl@sss.pgh.pa.us         820   [ +  +  +  -  :           1026 :     if ((c >= '0' && c <= '9') || c == '$')
                                              -  + ]
 3561 teodor@sigaev.ru          821                 :UBC           0 :         return true;
                                822                 :                :     /* ... or an identifier start character */
 3561 teodor@sigaev.ru          823                 :CBC        1026 :     return is_ident_start(c);
                                824                 :                : }
                                825                 :                : 
                                826                 :                : /*
                                827                 :                :  * parse_ident - parse a SQL qualified identifier into separate identifiers.
                                828                 :                :  * When strict mode is active (second parameter), then any chars after
                                829                 :                :  * the last identifier are disallowed.
                                830                 :                :  */
                                831                 :                : Datum
                                832                 :             57 : parse_ident(PG_FUNCTION_ARGS)
                                833                 :                : {
 3551 tgl@sss.pgh.pa.us         834                 :             57 :     text       *qualname = PG_GETARG_TEXT_PP(0);
                                835                 :             57 :     bool        strict = PG_GETARG_BOOL(1);
                                836                 :             57 :     char       *qualname_str = text_to_cstring(qualname);
                                837                 :             57 :     ArrayBuildState *astate = NULL;
                                838                 :                :     char       *nextp;
 3561 teodor@sigaev.ru          839                 :             57 :     bool        after_dot = false;
                                840                 :                : 
                                841                 :                :     /*
                                842                 :                :      * The code below scribbles on qualname_str in some cases, so we should
                                843                 :                :      * reconvert qualname if we need to show the original string in error
                                844                 :                :      * messages.
                                845                 :                :      */
                                846                 :             57 :     nextp = qualname_str;
                                847                 :                : 
                                848                 :                :     /* skip leading whitespace */
 3129 tgl@sss.pgh.pa.us         849         [ +  + ]:             72 :     while (scanner_isspace(*nextp))
 3561 teodor@sigaev.ru          850                 :             15 :         nextp++;
                                851                 :                : 
                                852                 :                :     for (;;)
                                853                 :             48 :     {
                                854                 :                :         char       *curname;
 3551 tgl@sss.pgh.pa.us         855                 :            105 :         bool        missing_ident = true;
                                856                 :                : 
                                857         [ +  + ]:            105 :         if (*nextp == '"')
                                858                 :                :         {
                                859                 :                :             char       *endp;
                                860                 :                : 
 3561 teodor@sigaev.ru          861                 :             30 :             curname = nextp + 1;
                                862                 :                :             for (;;)
                                863                 :                :             {
 3551 tgl@sss.pgh.pa.us         864                 :             30 :                 endp = strchr(nextp + 1, '"');
 3561 teodor@sigaev.ru          865         [ -  + ]:             30 :                 if (endp == NULL)
 3561 teodor@sigaev.ru          866         [ #  # ]:UBC           0 :                     ereport(ERROR,
                                867                 :                :                             (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
                                868                 :                :                              errmsg("string is not a valid identifier: \"%s\"",
                                869                 :                :                                     text_to_cstring(qualname)),
                                870                 :                :                              errdetail("String has unclosed double quotes.")));
 3551 tgl@sss.pgh.pa.us         871         [ +  - ]:CBC          30 :                 if (endp[1] != '"')
 3561 teodor@sigaev.ru          872                 :             30 :                     break;
 3561 teodor@sigaev.ru          873                 :UBC           0 :                 memmove(endp, endp + 1, strlen(endp));
                                874                 :              0 :                 nextp = endp;
                                875                 :                :             }
 3561 teodor@sigaev.ru          876                 :CBC          30 :             nextp = endp + 1;
                                877                 :             30 :             *endp = '\0';
                                878                 :                : 
                                879         [ -  + ]:             30 :             if (endp - curname == 0)
 3561 teodor@sigaev.ru          880         [ #  # ]:UBC           0 :                 ereport(ERROR,
                                881                 :                :                         (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
                                882                 :                :                          errmsg("string is not a valid identifier: \"%s\"",
                                883                 :                :                                 text_to_cstring(qualname)),
                                884                 :                :                          errdetail("Quoted identifier must not be empty.")));
                                885                 :                : 
 3561 teodor@sigaev.ru          886                 :CBC          30 :             astate = accumArrayResult(astate, CStringGetTextDatum(curname),
                                887                 :                :                                       false, TEXTOID, CurrentMemoryContext);
                                888                 :             30 :             missing_ident = false;
                                889                 :                :         }
 3551 tgl@sss.pgh.pa.us         890         [ +  + ]:             75 :         else if (is_ident_start((unsigned char) *nextp))
                                891                 :                :         {
                                892                 :                :             char       *downname;
                                893                 :                :             int         len;
                                894                 :                :             text       *part;
                                895                 :                : 
                                896                 :             51 :             curname = nextp++;
                                897         [ +  + ]:           1026 :             while (is_ident_cont((unsigned char) *nextp))
                                898                 :            975 :                 nextp++;
                                899                 :                : 
                                900                 :             51 :             len = nextp - curname;
                                901                 :                : 
                                902                 :                :             /*
                                903                 :                :              * We don't implicitly truncate identifiers. This is useful for
                                904                 :                :              * allowing the user to check for specific parts of the identifier
                                905                 :                :              * being too long. It's easy enough for the user to get the
                                906                 :                :              * truncated names by casting our output to name[].
                                907                 :                :              */
                                908                 :             51 :             downname = downcase_identifier(curname, len, false, false);
                                909                 :             51 :             part = cstring_to_text_with_len(downname, len);
                                910                 :             51 :             astate = accumArrayResult(astate, PointerGetDatum(part), false,
                                911                 :                :                                       TEXTOID, CurrentMemoryContext);
                                912                 :             51 :             missing_ident = false;
                                913                 :                :         }
                                914                 :                : 
 3561 teodor@sigaev.ru          915         [ +  + ]:            105 :         if (missing_ident)
                                916                 :                :         {
                                917                 :                :             /* Different error messages based on where we failed. */
                                918         [ +  + ]:             24 :             if (*nextp == '.')
                                919         [ +  - ]:              9 :                 ereport(ERROR,
                                920                 :                :                         (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
                                921                 :                :                          errmsg("string is not a valid identifier: \"%s\"",
                                922                 :                :                                 text_to_cstring(qualname)),
                                923                 :                :                          errdetail("No valid identifier before \".\".")));
                                924         [ +  + ]:             15 :             else if (after_dot)
                                925         [ +  - ]:              6 :                 ereport(ERROR,
                                926                 :                :                         (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
                                927                 :                :                          errmsg("string is not a valid identifier: \"%s\"",
                                928                 :                :                                 text_to_cstring(qualname)),
                                929                 :                :                          errdetail("No valid identifier after \".\".")));
                                930                 :                :             else
                                931         [ +  - ]:              9 :                 ereport(ERROR,
                                932                 :                :                         (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
                                933                 :                :                          errmsg("string is not a valid identifier: \"%s\"",
                                934                 :                :                                 text_to_cstring(qualname))));
                                935                 :                :         }
                                936                 :                : 
 3129 tgl@sss.pgh.pa.us         937         [ +  + ]:            102 :         while (scanner_isspace(*nextp))
 3561 teodor@sigaev.ru          938                 :             21 :             nextp++;
                                939                 :                : 
                                940         [ +  + ]:             81 :         if (*nextp == '.')
                                941                 :                :         {
                                942                 :             48 :             after_dot = true;
                                943                 :             48 :             nextp++;
 3129 tgl@sss.pgh.pa.us         944         [ +  + ]:             63 :             while (scanner_isspace(*nextp))
 3561 teodor@sigaev.ru          945                 :             15 :                 nextp++;
                                946                 :                :         }
                                947         [ +  + ]:             33 :         else if (*nextp == '\0')
                                948                 :                :         {
                                949                 :             18 :             break;
                                950                 :                :         }
                                951                 :                :         else
                                952                 :                :         {
                                953         [ +  + ]:             15 :             if (strict)
                                954         [ +  - ]:             12 :                 ereport(ERROR,
                                955                 :                :                         (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
                                956                 :                :                          errmsg("string is not a valid identifier: \"%s\"",
                                957                 :                :                                 text_to_cstring(qualname))));
                                958                 :              3 :             break;
                                959                 :                :         }
                                960                 :                :     }
                                961                 :                : 
                                962                 :             21 :     PG_RETURN_DATUM(makeArrayResult(astate, CurrentMemoryContext));
                                963                 :                : }
                                964                 :                : 
                                965                 :                : /*
                                966                 :                :  * pg_current_logfile
                                967                 :                :  *
                                968                 :                :  * Report current log file used by log collector by scanning current_logfiles.
                                969                 :                :  */
                                970                 :                : Datum
 3211 rhaas@postgresql.org      971                 :              6 : pg_current_logfile(PG_FUNCTION_ARGS)
                                972                 :                : {
                                973                 :                :     FILE       *fd;
                                974                 :                :     char        lbuffer[MAXPGPATH];
                                975                 :                :     char       *logfmt;
                                976                 :                : 
                                977                 :                :     /* The log format parameter is optional */
                                978   [ +  -  -  + ]:              6 :     if (PG_NARGS() == 0 || PG_ARGISNULL(0))
 3211 rhaas@postgresql.org      979                 :UBC           0 :         logfmt = NULL;
                                980                 :                :     else
                                981                 :                :     {
 3211 rhaas@postgresql.org      982                 :CBC           6 :         logfmt = text_to_cstring(PG_GETARG_TEXT_PP(0));
                                983                 :                : 
 1430 michael@paquier.xyz       984         [ +  + ]:              6 :         if (strcmp(logfmt, "stderr") != 0 &&
                                985         [ +  + ]:              4 :             strcmp(logfmt, "csvlog") != 0 &&
                                986         [ -  + ]:              2 :             strcmp(logfmt, "jsonlog") != 0)
 3211 rhaas@postgresql.org      987         [ #  # ]:UBC           0 :             ereport(ERROR,
                                988                 :                :                     (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
                                989                 :                :                      errmsg("log format \"%s\" is not supported", logfmt),
                                990                 :                :                      errhint("The supported log formats are \"stderr\", \"csvlog\", and \"jsonlog\".")));
                                991                 :                :     }
                                992                 :                : 
 3211 rhaas@postgresql.org      993                 :CBC           6 :     fd = AllocateFile(LOG_METAINFO_DATAFILE, "r");
                                994         [ -  + ]:              6 :     if (fd == NULL)
                                995                 :                :     {
 3211 rhaas@postgresql.org      996         [ #  # ]:UBC           0 :         if (errno != ENOENT)
                                997         [ #  # ]:              0 :             ereport(ERROR,
                                998                 :                :                     (errcode_for_file_access(),
                                999                 :                :                      errmsg("could not read file \"%s\": %m",
                               1000                 :                :                             LOG_METAINFO_DATAFILE)));
                               1001                 :              0 :         PG_RETURN_NULL();
                               1002                 :                :     }
                               1003                 :                : 
                               1004                 :                : #ifdef WIN32
                               1005                 :                :     /* syslogger.c writes CRLF line endings on Windows */
                               1006                 :                :     _setmode(_fileno(fd), _O_TEXT);
                               1007                 :                : #endif
                               1008                 :                : 
                               1009                 :                :     /*
                               1010                 :                :      * Read the file to gather current log filename(s) registered by the
                               1011                 :                :      * syslogger.
                               1012                 :                :      */
 3211 rhaas@postgresql.org     1013         [ +  - ]:CBC          12 :     while (fgets(lbuffer, sizeof(lbuffer), fd) != NULL)
                               1014                 :                :     {
                               1015                 :                :         char       *log_format;
                               1016                 :                :         char       *log_filepath;
                               1017                 :                :         char       *nlpos;
                               1018                 :                : 
                               1019                 :                :         /* Extract log format and log file path from the line. */
 1987 tgl@sss.pgh.pa.us        1020                 :             12 :         log_format = lbuffer;
 3211 rhaas@postgresql.org     1021                 :             12 :         log_filepath = strchr(lbuffer, ' ');
                               1022         [ -  + ]:             12 :         if (log_filepath == NULL)
                               1023                 :                :         {
                               1024                 :                :             /* Uh oh.  No space found, so file content is corrupted. */
 3211 rhaas@postgresql.org     1025         [ #  # ]:UBC           0 :             elog(ERROR,
                               1026                 :                :                  "missing space character in \"%s\"", LOG_METAINFO_DATAFILE);
                               1027                 :                :             break;
                               1028                 :                :         }
                               1029                 :                : 
 3211 rhaas@postgresql.org     1030                 :CBC          12 :         *log_filepath = '\0';
                               1031                 :             12 :         log_filepath++;
                               1032                 :             12 :         nlpos = strchr(log_filepath, '\n');
                               1033         [ -  + ]:             12 :         if (nlpos == NULL)
                               1034                 :                :         {
                               1035                 :                :             /* Uh oh.  No newline found, so file content is corrupted. */
 3211 rhaas@postgresql.org     1036         [ #  # ]:UBC           0 :             elog(ERROR,
                               1037                 :                :                  "missing newline character in \"%s\"", LOG_METAINFO_DATAFILE);
                               1038                 :                :             break;
                               1039                 :                :         }
 3211 rhaas@postgresql.org     1040                 :CBC          12 :         *nlpos = '\0';
                               1041                 :                : 
                               1042   [ +  -  +  + ]:             12 :         if (logfmt == NULL || strcmp(logfmt, log_format) == 0)
                               1043                 :                :         {
                               1044                 :              6 :             FreeFile(fd);
                               1045                 :              6 :             PG_RETURN_TEXT_P(cstring_to_text(log_filepath));
                               1046                 :                :         }
                               1047                 :                :     }
                               1048                 :                : 
                               1049                 :                :     /* Close the current log filename file. */
 3211 rhaas@postgresql.org     1050                 :UBC           0 :     FreeFile(fd);
                               1051                 :                : 
                               1052                 :              0 :     PG_RETURN_NULL();
                               1053                 :                : }
                               1054                 :                : 
                               1055                 :                : /*
                               1056                 :                :  * Report current log file used by log collector (1 argument version)
                               1057                 :                :  *
                               1058                 :                :  * note: this wrapper is necessary to pass the sanity check in opr_sanity,
                               1059                 :                :  * which checks that all built-in functions that share the implementing C
                               1060                 :                :  * function take the same number of arguments
                               1061                 :                :  */
                               1062                 :                : Datum
 3211 rhaas@postgresql.org     1063                 :CBC           6 : pg_current_logfile_1arg(PG_FUNCTION_ARGS)
                               1064                 :                : {
                               1065                 :              6 :     return pg_current_logfile(fcinfo);
                               1066                 :                : }
                               1067                 :                : 
                               1068                 :                : /*
                               1069                 :                :  * SQL wrapper around RelationGetReplicaIndex().
                               1070                 :                :  */
                               1071                 :                : Datum
 3191 peter_e@gmx.net          1072                 :            393 : pg_get_replica_identity_index(PG_FUNCTION_ARGS)
                               1073                 :                : {
                               1074                 :            393 :     Oid         reloid = PG_GETARG_OID(0);
                               1075                 :                :     Oid         idxoid;
                               1076                 :                :     Relation    rel;
                               1077                 :                : 
 2522 andres@anarazel.de       1078                 :            393 :     rel = table_open(reloid, AccessShareLock);
 3191 peter_e@gmx.net          1079                 :            393 :     idxoid = RelationGetReplicaIndex(rel);
 2522 andres@anarazel.de       1080                 :            393 :     table_close(rel, AccessShareLock);
                               1081                 :                : 
 3191 peter_e@gmx.net          1082         [ +  + ]:            393 :     if (OidIsValid(idxoid))
                               1083                 :            208 :         PG_RETURN_OID(idxoid);
                               1084                 :                :     else
                               1085                 :            185 :         PG_RETURN_NULL();
                               1086                 :                : }
                               1087                 :                : 
                               1088                 :                : /*
                               1089                 :                :  * Transition function for the ANY_VALUE aggregate
                               1090                 :                :  */
                               1091                 :                : Datum
 1029 peter@eisentraut.org     1092                 :              9 : any_value_transfn(PG_FUNCTION_ARGS)
                               1093                 :                : {
                               1094                 :              9 :     PG_RETURN_DATUM(PG_GETARG_DATUM(0));
                               1095                 :                : }
        

Generated by: LCOV version 2.4-beta