LCOV - differential code coverage report
Current view: top level - src/backend/utils/adt - misc.c (source / functions) Coverage Total Hit UIC GIC
Current: 4b3c9325757dfd8f2312e9f1a8decf4431f344ae vs e87b024bfb8dd55e3049b1b6e6ab4e810cf1abc8 Lines: 78.8 % 358 282 76 282
Current Date: 2026-11-28 16:48:44 +0900 Functions: 92.3 % 26 24 2 24
Baseline: lcov-20261128-baseline Branches: 61.8 % 217 134 83 134
Baseline Date: 2026-11-28 16:48:48 +0900 Line coverage date bins:
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
(1,7] days: 100.0 % 8 8 8
(7,30] days: 100.0 % 1 1 1
(30,360] days: 100.0 % 1 1 1
(360..) days: 78.2 % 348 272 76 272
Function coverage date bins:
(360..) days: 92.3 % 26 24 2 24
Branch coverage date bins:
(1,7] days: 100.0 % 2 2 2
(360..) days: 61.4 % 215 132 83 132

 Age         Owner                    Branch data    TLA  Line data    Source code
                                  1                 :                : /*-------------------------------------------------------------------------
                                  2                 :                :  *
                                  3                 :                :  * misc.c
                                  4                 :                :  *
                                  5                 :                :  *
                                  6                 :                :  * Portions Copyright (c) 1996-2026, 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                 :                : #include "utils/tuplestore.h"
                                 50                 :                : #include "utils/wait_event.h"
                                 51                 :                : 
                                 52                 :                : 
                                 53                 :                : /*
                                 54                 :                :  * structure to cache metadata needed in pg_input_is_valid_common
                                 55                 :                :  */
                                 56                 :                : typedef struct ValidIOData
                                 57                 :                : {
                                 58                 :                :     Oid         typoid;
                                 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
 3950 tgl@sss.pgh.pa.us          76                 :GIC          98 : count_nulls(FunctionCallInfo fcinfo,
                                 77                 :                :             int32 *nargs, int32 *nulls)
                                 78                 :                : {
                                 79                 :             98 :     int32       count = 0;
                                 80                 :                :     int         i;
                                 81                 :                : 
                                 82                 :                :     /* Did we get a VARIADIC array argument, or separate arguments? */
                                 83         [ +  + ]:             98 :     if (get_fn_expr_variadic(fcinfo->flinfo))
                                 84                 :                :     {
                                 85                 :                :         ArrayType  *arr;
                                 86                 :                :         int         ndims,
                                 87                 :                :                     nitems,
                                 88                 :                :                    *dims;
                                 89                 :                :         uint8      *bitmap;
                                 90                 :                : 
                                 91         [ -  + ]:             48 :         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         [ +  + ]:             48 :         if (PG_ARGISNULL(0))
                                 99                 :             10 :             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         [ -  + ]:             38 :         Assert(OidIsValid(get_base_element_type(get_fn_expr_argtype(fcinfo->flinfo, 0))));
                                109                 :                : 
                                110                 :                :         /* OK, safe to fetch the array value */
                                111                 :             38 :         arr = PG_GETARG_ARRAYTYPE_P(0);
                                112                 :                : 
                                113                 :                :         /* Count the array elements */
                                114                 :             38 :         ndims = ARR_NDIM(arr);
                                115                 :             38 :         dims = ARR_DIMS(arr);
                                116                 :             38 :         nitems = ArrayGetNItems(ndims, dims);
                                117                 :                : 
                                118                 :                :         /* Count those that are NULL */
                                119         [ +  + ]:             38 :         bitmap = ARR_NULLBITMAP(arr);
                                120         [ +  + ]:             38 :         if (bitmap)
                                121                 :                :         {
                                122                 :             18 :             int         bitmask = 1;
                                123                 :                : 
                                124         [ +  + ]:            858 :             for (i = 0; i < nitems; i++)
                                125                 :                :             {
                                126         [ +  + ]:            840 :                 if ((*bitmap & bitmask) == 0)
                                127                 :             18 :                     count++;
                                128                 :                : 
                                129                 :            840 :                 bitmask <<= 1;
                                130         [ +  + ]:            840 :                 if (bitmask == 0x100)
                                131                 :                :                 {
                                132                 :             96 :                     bitmap++;
                                133                 :             96 :                     bitmask = 1;
                                134                 :                :                 }
                                135                 :                :             }
                                136                 :                :         }
                                137                 :                : 
                                138                 :             38 :         *nargs = nitems;
                                139                 :             38 :         *nulls = count;
                                140                 :                :     }
                                141                 :                :     else
                                142                 :                :     {
                                143                 :                :         /* Separate arguments, so just count 'em */
                                144         [ +  + ]:            170 :         for (i = 0; i < PG_NARGS(); i++)
                                145                 :                :         {
                                146         [ +  + ]:            120 :             if (PG_ARGISNULL(i))
                                147                 :             70 :                 count++;
                                148                 :                :         }
                                149                 :                : 
                                150                 :             50 :         *nargs = PG_NARGS();
                                151                 :             50 :         *nulls = count;
                                152                 :                :     }
                                153                 :                : 
                                154                 :             88 :     return true;
                                155                 :                : }
                                156                 :                : 
                                157                 :                : /*
                                158                 :                :  * num_nulls()
                                159                 :                :  *  Count the number of NULL arguments
                                160                 :                :  */
                                161                 :                : Datum
                                162                 :             49 : pg_num_nulls(PG_FUNCTION_ARGS)
                                163                 :                : {
                                164                 :                :     int32       nargs,
                                165                 :                :                 nulls;
                                166                 :                : 
                                167         [ +  + ]:             49 :     if (!count_nulls(fcinfo, &nargs, &nulls))
                                168                 :              5 :         PG_RETURN_NULL();
                                169                 :                : 
                                170                 :             44 :     PG_RETURN_INT32(nulls);
                                171                 :                : }
                                172                 :                : 
                                173                 :                : /*
                                174                 :                :  * num_nonnulls()
                                175                 :                :  *  Count the number of non-NULL arguments
                                176                 :                :  */
                                177                 :                : Datum
                                178                 :             49 : pg_num_nonnulls(PG_FUNCTION_ARGS)
                                179                 :                : {
                                180                 :                :     int32       nargs,
                                181                 :                :                 nulls;
                                182                 :                : 
                                183         [ +  + ]:             49 :     if (!count_nulls(fcinfo, &nargs, &nulls))
                                184                 :              5 :         PG_RETURN_NULL();
                                185                 :                : 
                                186                 :             44 :     PG_RETURN_INT32(nargs - nulls);
                                187                 :                : }
                                188                 :                : 
                                189                 :                : /*
                                190                 :                :  * error_on_null()
                                191                 :                :  *  Check if the input is the NULL value
                                192                 :                :  */
                                193                 :                : Datum
  402 michael@paquier.xyz       194                 :             28 : pg_error_on_null(PG_FUNCTION_ARGS)
                                195                 :                : {
                                196         [ +  + ]:             28 :     if (PG_ARGISNULL(0))
                                197         [ +  - ]:              8 :         ereport(ERROR,
                                198                 :                :                 (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
                                199                 :                :                  errmsg("null value not allowed")));
                                200                 :                : 
                                201                 :             20 :     PG_RETURN_DATUM(PG_GETARG_DATUM(0));
                                202                 :                : }
                                203                 :                : 
                                204                 :                : /*
                                205                 :                :  * current_database()
                                206                 :                :  *  Expose the current database to the user
                                207                 :                :  */
                                208                 :                : Datum
 8866 bruce@momjian.us          209                 :           5187 : current_database(PG_FUNCTION_ARGS)
                                210                 :                : {
                                211                 :                :     Name        db;
                                212                 :                : 
                                213                 :           5187 :     db = (Name) palloc(NAMEDATALEN);
                                214                 :                : 
 8555 peter_e@gmx.net           215                 :           5187 :     namestrcpy(db, get_database_name(MyDatabaseId));
 8866 bruce@momjian.us          216                 :           5187 :     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
 6812 bruce@momjian.us          226                 :UIC           0 : current_query(PG_FUNCTION_ARGS)
                                227                 :                : {
                                228                 :                :     /* there is no easy way to access the more concise 'query_string' */
 6533                           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
 8126 bruce@momjian.us          238                 :GIC           4 : pg_tablespace_databases(PG_FUNCTION_ARGS)
                                239                 :                : {
 2448 tgl@sss.pgh.pa.us         240                 :              4 :     Oid         tablespaceOid = PG_GETARG_OID(0);
                                241                 :              4 :     ReturnSetInfo *rsinfo = (ReturnSetInfo *) fcinfo->resultinfo;
                                242                 :                :     char       *location;
                                243                 :                :     DIR        *dirdesc;
                                244                 :                :     struct dirent *de;
                                245                 :                : 
 1502 michael@paquier.xyz       246                 :              4 :     InitMaterializedSRF(fcinfo, MAT_SRF_USE_EXPECTED_DESC);
                                247                 :                : 
 2448 tgl@sss.pgh.pa.us         248         [ -  + ]:              4 :     if (tablespaceOid == GLOBALTABLESPACE_OID)
                                249                 :                :     {
 2448 tgl@sss.pgh.pa.us         250         [ #  # ]:UIC           0 :         ereport(WARNING,
                                251                 :                :                 (errmsg("global tablespace never has databases")));
                                252                 :                :         /* return empty tuplestore */
                                253                 :              0 :         return (Datum) 0;
                                254                 :                :     }
                                255                 :                : 
 2448 tgl@sss.pgh.pa.us         256         [ +  - ]:GIC           4 :     if (tablespaceOid == DEFAULTTABLESPACE_OID)
 1544 drowley@postgresql.o      257                 :              4 :         location = "base";
                                258                 :                :     else
  816 michael@paquier.xyz       259                 :UIC           0 :         location = psprintf("%s/%u/%s", PG_TBLSPC_DIR, tablespaceOid,
                                260                 :                :                             TABLESPACE_VERSION_DIRECTORY);
                                261                 :                : 
 2448 tgl@sss.pgh.pa.us         262                 :GIC           4 :     dirdesc = AllocateDir(location);
                                263                 :                : 
                                264         [ -  + ]:              4 :     if (!dirdesc)
                                265                 :                :     {
                                266                 :                :         /* the only expected error is ENOENT */
 2448 tgl@sss.pgh.pa.us         267         [ #  # ]:UIC           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                 :                : 
 2448 tgl@sss.pgh.pa.us         278         [ +  + ]:GIC          35 :     while ((de = ReadDir(dirdesc, location)) != NULL)
                                279                 :                :     {
 8126 bruce@momjian.us          280                 :             31 :         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 */
 8184 mail@joeconway.com        287         [ +  + ]:             31 :         if (!datOid)
                                288                 :             12 :             continue;
                                289                 :                : 
                                290                 :                :         /* if database subdir is empty, don't report tablespace as used */
                                291                 :                : 
 2448 tgl@sss.pgh.pa.us         292                 :             19 :         subdir = psprintf("%s/%s", location, de->d_name);
 3281                           293                 :             19 :         isempty = directory_is_empty(subdir);
 7832                           294                 :             19 :         pfree(subdir);
                                295                 :                : 
 3281                           296         [ -  + ]:             19 :         if (isempty)
 8152 tgl@sss.pgh.pa.us         297                 :UIC           0 :             continue;           /* indeed, nothing in it */
                                298                 :                : 
 2448 tgl@sss.pgh.pa.us         299                 :GIC          19 :         values[0] = ObjectIdGetDatum(datOid);
                                300                 :             19 :         nulls[0] = false;
                                301                 :                : 
 1727 michael@paquier.xyz       302                 :             19 :         tuplestore_putvalues(rsinfo->setResult, rsinfo->setDesc,
                                303                 :                :                              values, nulls);
                                304                 :                :     }
                                305                 :                : 
 2448 tgl@sss.pgh.pa.us         306                 :              4 :     FreeDir(dirdesc);
                                307                 :              4 :     return (Datum) 0;
                                308                 :                : }
                                309                 :                : 
                                310                 :                : 
                                311                 :                : /*
                                312                 :                :  * pg_tablespace_location - get location for a tablespace
                                313                 :                :  */
                                314                 :                : Datum
 5470 magnus@hagander.net       315                 :            163 : pg_tablespace_location(PG_FUNCTION_ARGS)
                                316                 :                : {
 5284 bruce@momjian.us          317                 :            163 :     Oid         tablespaceOid = PG_GETARG_OID(0);
                                318                 :                :     char       *tablespaceLoc;
                                319                 :                : 
                                320                 :                :     /* Get LOCATION string from its OID */
  381 alvherre@kurilemu.de      321                 :            163 :     tablespaceLoc = get_tablespace_location(tablespaceOid);
                                322                 :                : 
                                323                 :            163 :     PG_RETURN_TEXT_P(cstring_to_text(tablespaceLoc));
                                324                 :                : }
                                325                 :                : 
                                326                 :                : /*
                                327                 :                :  * pg_sleep - delay for N seconds
                                328                 :                :  */
                                329                 :                : Datum
 7626 tgl@sss.pgh.pa.us         330                 :            127 : pg_sleep(PG_FUNCTION_ARGS)
                                331                 :                : {
                                332                 :            127 :     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                 :                :      */
  429                           342   [ +  -  -  + ]:            127 :     if (isnan(secs) || secs <= 0.0)
  429 tgl@sss.pgh.pa.us         343                 :UIC           0 :         PG_RETURN_VOID();
  429 tgl@sss.pgh.pa.us         344                 :GIC         127 :     secs *= USECS_PER_SEC;      /* we assume overflow will produce +Inf */
                                345                 :            127 :     secs = ceil(secs);          /* round up any fractional microsecond */
                                346         [ +  - ]:            127 :     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                 :            127 :     endtime = GetCurrentTimestamp() + usecs;
                                361                 :                : 
                                362                 :                :     for (;;)
 7626                           363                 :            128 :     {
                                364                 :                :         TimestampTz delay;
                                365                 :                :         long        delay_ms;
                                366                 :                : 
                                367         [ +  + ]:            255 :         CHECK_FOR_INTERRUPTS();
                                368                 :                : 
  429                           369                 :            242 :         delay = endtime - GetCurrentTimestamp();
                                370         [ -  + ]:            242 :         if (delay >= 600 * USECS_PER_SEC)
 4914 tgl@sss.pgh.pa.us         371                 :UIC           0 :             delay_ms = 600000;
  429 tgl@sss.pgh.pa.us         372         [ +  + ]:GIC         242 :         else if (delay > 0)
                                373                 :            128 :             delay_ms = (long) ((delay + 999) / 1000);
                                374                 :                :         else
 7626                           375                 :            114 :             break;
                                376                 :                : 
 4336 andres@anarazel.de        377                 :            128 :         (void) WaitLatch(MyLatch,
                                378                 :                :                          WL_LATCH_SET | WL_TIMEOUT | WL_EXIT_ON_PM_DEATH,
                                379                 :                :                          delay_ms,
                                380                 :                :                          WAIT_EVENT_PG_SLEEP);
                                381                 :            128 :         ResetLatch(MyLatch);
                                382                 :                :     }
                                383                 :                : 
 7626 tgl@sss.pgh.pa.us         384                 :            114 :     PG_RETURN_VOID();
                                385                 :                : }
                                386                 :                : 
                                387                 :                : /* Function to return the list of grammar keywords */
                                388                 :                : Datum
 6722 tgl@sss.pgh.pa.us         389                 :UIC           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                 :                : 
 1438 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;
 6722 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                 :                : 
 2883                           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                 :                :         {
 6722                           423                 :              0 :             case UNRESERVED_KEYWORD:
                                424                 :              0 :                 values[1] = "U";
 2262                           425                 :              0 :                 values[3] = _("unreserved");
 6722                           426                 :              0 :                 break;
                                427                 :              0 :             case COL_NAME_KEYWORD:
                                428                 :              0 :                 values[1] = "C";
 2262                           429                 :              0 :                 values[3] = _("unreserved (cannot be function or type name)");
 6722                           430                 :              0 :                 break;
                                431                 :              0 :             case TYPE_FUNC_NAME_KEYWORD:
                                432                 :              0 :                 values[1] = "T";
 2262                           433                 :              0 :                 values[3] = _("reserved (can be function or type name)");
 6722                           434                 :              0 :                 break;
                                435                 :              0 :             case RESERVED_KEYWORD:
                                436                 :              0 :                 values[1] = "R";
 2262                           437                 :              0 :                 values[3] = _("reserved");
 6722                           438                 :              0 :                 break;
                                439                 :              0 :             default:            /* shouldn't be possible */
                                440                 :              0 :                 values[1] = NULL;
 2262                           441                 :              0 :                 values[3] = NULL;
 6722                           442                 :              0 :                 break;
                                443                 :                :         }
                                444                 :                : 
 2262                           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                 :                : 
 6722                           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
 2125 tgl@sss.pgh.pa.us         467                 :GIC         908 : pg_get_catalog_foreign_keys(PG_FUNCTION_ARGS)
                                468                 :                : {
                                469                 :                :     FuncCallContext *funcctx;
                                470                 :                :     FmgrInfo   *arrayinp;
                                471                 :                : 
                                472         [ +  + ]:            908 :     if (SRF_IS_FIRSTCALL())
                                473                 :                :     {
                                474                 :                :         MemoryContext oldcontext;
                                475                 :                :         TupleDesc   tupdesc;
                                476                 :                : 
                                477                 :              4 :         funcctx = SRF_FIRSTCALL_INIT();
                                478                 :              4 :         oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
                                479                 :                : 
 1438 michael@paquier.xyz       480         [ -  + ]:              4 :         if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE)
 1438 michael@paquier.xyz       481         [ #  # ]:UIC           0 :             elog(ERROR, "return type must be a row type");
 2125 tgl@sss.pgh.pa.us         482                 :GIC           4 :         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                 :                :          */
  353 michael@paquier.xyz       490                 :              4 :         arrayinp = palloc_object(FmgrInfo);
 2125 tgl@sss.pgh.pa.us         491                 :              4 :         fmgr_info(F_ARRAY_IN, arrayinp);
                                492                 :              4 :         funcctx->user_fctx = arrayinp;
                                493                 :                : 
                                494                 :              4 :         MemoryContextSwitchTo(oldcontext);
                                495                 :                :     }
                                496                 :                : 
                                497                 :            908 :     funcctx = SRF_PERCALL_SETUP();
                                498                 :            908 :     arrayinp = (FmgrInfo *) funcctx->user_fctx;
                                499                 :                : 
                                500         [ +  + ]:            908 :     if (funcctx->call_cntr < lengthof(sys_fk_relationships))
                                501                 :                :     {
                                502                 :            904 :         const SysFKRelationship *fkrel = &sys_fk_relationships[funcctx->call_cntr];
                                503                 :                :         Datum       values[6];
                                504                 :                :         bool        nulls[6];
                                505                 :                :         HeapTuple   tuple;
                                506                 :                : 
                                507                 :            904 :         memset(nulls, false, sizeof(nulls));
                                508                 :                : 
                                509                 :            904 :         values[0] = ObjectIdGetDatum(fkrel->fk_table);
                                510                 :            904 :         values[1] = FunctionCall3(arrayinp,
                                511                 :                :                                   CStringGetDatum(fkrel->fk_columns),
                                512                 :                :                                   ObjectIdGetDatum(TEXTOID),
                                513                 :                :                                   Int32GetDatum(-1));
                                514                 :            904 :         values[2] = ObjectIdGetDatum(fkrel->pk_table);
                                515                 :            904 :         values[3] = FunctionCall3(arrayinp,
                                516                 :                :                                   CStringGetDatum(fkrel->pk_columns),
                                517                 :                :                                   ObjectIdGetDatum(TEXTOID),
                                518                 :                :                                   Int32GetDatum(-1));
                                519                 :            904 :         values[4] = BoolGetDatum(fkrel->is_array);
                                520                 :            904 :         values[5] = BoolGetDatum(fkrel->is_opt);
                                521                 :                : 
                                522                 :            904 :         tuple = heap_form_tuple(funcctx->tuple_desc, values, nulls);
                                523                 :                : 
                                524                 :            904 :         SRF_RETURN_NEXT(funcctx, HeapTupleGetDatum(tuple));
                                525                 :                :     }
                                526                 :                : 
                                527                 :              4 :     SRF_RETURN_DONE(funcctx);
                                528                 :                : }
                                529                 :                : 
                                530                 :                : 
                                531                 :                : /*
                                532                 :                :  * Return the type of the argument.
                                533                 :                :  */
                                534                 :                : Datum
 6599                           535                 :            934 : pg_typeof(PG_FUNCTION_ARGS)
                                536                 :                : {
                                537                 :            934 :     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
  973                           554                 :             12 : pg_basetype(PG_FUNCTION_ARGS)
                                555                 :                : {
                                556                 :             12 :     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                 :             12 :     {
                                563                 :                :         HeapTuple   tup;
                                564                 :                :         Form_pg_type typTup;
                                565                 :                : 
                                566                 :             24 :         tup = SearchSysCache1(TYPEOID, ObjectIdGetDatum(typid));
                                567         [ +  + ]:             24 :         if (!HeapTupleIsValid(tup))
                                568                 :              4 :             PG_RETURN_NULL();   /* return NULL for bogus OID */
                                569                 :             20 :         typTup = (Form_pg_type) GETSTRUCT(tup);
                                570         [ +  + ]:             20 :         if (typTup->typtype != TYPTYPE_DOMAIN)
                                571                 :                :         {
                                572                 :                :             /* Not a domain, so done */
                                573                 :              8 :             ReleaseSysCache(tup);
                                574                 :              8 :             break;
                                575                 :                :         }
                                576                 :                : 
                                577                 :             12 :         typid = typTup->typbasetype;
                                578                 :             12 :         ReleaseSysCache(tup);
                                579                 :                :     }
                                580                 :                : 
                                581                 :              8 :     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
 5384 peter_e@gmx.net           590                 :             20 : pg_collation_for(PG_FUNCTION_ARGS)
                                591                 :                : {
                                592                 :                :     Oid         typeid;
                                593                 :                :     Oid         collid;
                                594                 :                : 
                                595                 :             20 :     typeid = get_fn_expr_argtype(fcinfo->flinfo, 0);
                                596         [ -  + ]:             20 :     if (!typeid)
 5384 peter_e@gmx.net           597                 :UIC           0 :         PG_RETURN_NULL();
 5384 peter_e@gmx.net           598   [ +  +  +  + ]:GIC          20 :     if (!type_is_collatable(typeid) && typeid != UNKNOWNOID)
                                599         [ +  - ]:              4 :         ereport(ERROR,
                                600                 :                :                 (errcode(ERRCODE_DATATYPE_MISMATCH),
                                601                 :                :                  errmsg("collations are not supported by type %s",
                                602                 :                :                         format_type_be(typeid))));
                                603                 :                : 
                                604                 :             16 :     collid = PG_GET_COLLATION();
                                605         [ +  + ]:             16 :     if (!collid)
                                606                 :              4 :         PG_RETURN_NULL();
                                607                 :             12 :     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
 4917 tgl@sss.pgh.pa.us         619                 :            596 : pg_relation_is_updatable(PG_FUNCTION_ARGS)
                                620                 :                : {
                                621                 :            596 :     Oid         reloid = PG_GETARG_OID(0);
                                622                 :            596 :     bool        include_triggers = PG_GETARG_BOOL(1);
                                623                 :                : 
 2564                           624                 :            596 :     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
 4917                           636                 :            444 : pg_column_is_updatable(PG_FUNCTION_ARGS)
                                637                 :                : {
                                638                 :            444 :     Oid         reloid = PG_GETARG_OID(0);
                                639                 :            444 :     AttrNumber  attnum = PG_GETARG_INT16(1);
    9 drowley@postgresql.o      640                 :            444 :     int         col = attnum - FirstLowInvalidHeapAttributeNumber;
 4917 tgl@sss.pgh.pa.us         641                 :            444 :     bool        include_triggers = PG_GETARG_BOOL(2);
                                642                 :                :     int         events;
                                643                 :                : 
                                644                 :                :     /* System columns are never updatable */
                                645         [ -  + ]:            444 :     if (attnum <= 0)
 4917 tgl@sss.pgh.pa.us         646                 :UIC           0 :         PG_RETURN_BOOL(false);
                                647                 :                : 
 2564 tgl@sss.pgh.pa.us         648                 :GIC         444 :     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                 :                : 
 4917                           654                 :            444 :     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
 1450                           667                 :            593 : pg_input_is_valid(PG_FUNCTION_ARGS)
                                668                 :                : {
                                669                 :            593 :     text       *txt = PG_GETARG_TEXT_PP(0);
                                670                 :            593 :     text       *typname = PG_GETARG_TEXT_PP(1);
                                671                 :            593 :     ErrorSaveContext escontext = {T_ErrorSaveContext};
                                672                 :                : 
                                673                 :            593 :     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
 1369 michael@paquier.xyz       687                 :            835 : pg_input_error_info(PG_FUNCTION_ARGS)
                                688                 :                : {
 1450 tgl@sss.pgh.pa.us         689                 :            835 :     text       *txt = PG_GETARG_TEXT_PP(0);
                                690                 :            835 :     text       *typname = PG_GETARG_TEXT_PP(1);
                                691                 :            835 :     ErrorSaveContext escontext = {T_ErrorSaveContext};
                                692                 :                :     TupleDesc   tupdesc;
                                693                 :                :     Datum       values[4];
                                694                 :                :     bool        isnull[4];
                                695                 :                : 
 1369 michael@paquier.xyz       696         [ -  + ]:            835 :     if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE)
 1369 michael@paquier.xyz       697         [ #  # ]:UIC           0 :         elog(ERROR, "return type must be a row type");
                                698                 :                : 
                                699                 :                :     /* Enable details_wanted */
 1450 tgl@sss.pgh.pa.us         700                 :GIC         835 :     escontext.details_wanted = true;
                                701                 :                : 
                                702         [ +  + ]:            835 :     if (pg_input_is_valid_common(fcinfo, txt, typname,
                                703                 :                :                                  &escontext))
 1369 michael@paquier.xyz       704                 :             19 :         memset(isnull, true, sizeof(isnull));
                                705                 :                :     else
                                706                 :                :     {
                                707                 :                :         char       *sqlstate;
                                708                 :                : 
                                709         [ -  + ]:            796 :         Assert(escontext.error_occurred);
                                710         [ -  + ]:            796 :         Assert(escontext.error_data != NULL);
                                711         [ -  + ]:            796 :         Assert(escontext.error_data->message != NULL);
                                712                 :                : 
                                713                 :            796 :         memset(isnull, false, sizeof(isnull));
                                714                 :                : 
                                715                 :            796 :         values[0] = CStringGetTextDatum(escontext.error_data->message);
                                716                 :                : 
                                717         [ +  + ]:            796 :         if (escontext.error_data->detail != NULL)
                                718                 :            356 :             values[1] = CStringGetTextDatum(escontext.error_data->detail);
                                719                 :                :         else
                                720                 :            440 :             isnull[1] = true;
                                721                 :                : 
                                722         [ -  + ]:            796 :         if (escontext.error_data->hint != NULL)
 1369 michael@paquier.xyz       723                 :UIC           0 :             values[2] = CStringGetTextDatum(escontext.error_data->hint);
                                724                 :                :         else
 1369 michael@paquier.xyz       725                 :GIC         796 :             isnull[2] = true;
                                726                 :                : 
                                727                 :            796 :         sqlstate = unpack_sql_state(escontext.error_data->sqlerrcode);
                                728                 :            796 :         values[3] = CStringGetTextDatum(sqlstate);
                                729                 :                :     }
                                730                 :                : 
                                731                 :            815 :     return HeapTupleGetDatum(heap_form_tuple(tupdesc, values, isnull));
                                732                 :                : }
                                733                 :                : 
                                734                 :                : /* Common subroutine for the above */
                                735                 :                : static bool
 1450 tgl@sss.pgh.pa.us         736                 :           1428 : pg_input_is_valid_common(FunctionCallInfo fcinfo,
                                737                 :                :                          text *txt, text *typname,
                                738                 :                :                          ErrorSaveContext *escontext)
                                739                 :                : {
                                740                 :           1428 :     char       *str = text_to_cstring(txt);
    2                           741                 :           1428 :     char       *typnamestr = text_to_cstring(typname);
                                742                 :                :     ValidIOData *my_extra;
                                743                 :                :     Oid         typoid;
                                744                 :                :     int32       typmod;
                                745                 :                :     Datum       converted;
                                746                 :                : 
                                747                 :                :     /*
                                748                 :                :      * We arrange to look up the needed I/O info just once per series of
                                749                 :                :      * calls, assuming the data type doesn't change underneath us.
                                750                 :                :      */
 1450                           751                 :           1428 :     my_extra = (ValidIOData *) fcinfo->flinfo->fn_extra;
                                752         [ +  + ]:           1428 :     if (my_extra == NULL)
                                753                 :                :     {
                                754                 :           2728 :         fcinfo->flinfo->fn_extra =
                                755                 :           1364 :             MemoryContextAlloc(fcinfo->flinfo->fn_mcxt,
                                756                 :                :                                sizeof(ValidIOData));
                                757                 :           1364 :         my_extra = (ValidIOData *) fcinfo->flinfo->fn_extra;
                                758                 :           1364 :         my_extra->typoid = InvalidOid;
                                759                 :                :     }
                                760                 :                : 
                                761                 :                :     /*
                                762                 :                :      * Parse type-name argument to obtain type OID and encoded typmod.  We
                                763                 :                :      * have to do this each time since (a) we don't know if the input string
                                764                 :                :      * is constant and (b) the results could be affected by search_path etc.
                                765                 :                :      */
    2                           766                 :           1428 :     (void) parseTypeString(typnamestr, &typoid, &typmod, NULL);
                                767                 :                : 
                                768                 :                :     /* But if we do get the same type OID, assume we can re-use this work: */
                                769         [ +  + ]:           1428 :     if (my_extra->typoid != typoid)
                                770                 :                :     {
                                771                 :           1372 :         my_extra->typoid = InvalidOid;   /* for safety if lookup fails */
                                772                 :           1372 :         getTypeInputInfo(typoid,
                                773                 :                :                          &my_extra->typiofunc,
                                774                 :                :                          &my_extra->typioparam);
                                775                 :           1372 :         fmgr_info_cxt(my_extra->typiofunc, &my_extra->inputproc,
                                776                 :           1372 :                       fcinfo->flinfo->fn_mcxt);
                                777                 :           1372 :         my_extra->typoid = typoid;
                                778                 :                :     }
                                779                 :                : 
                                780                 :                :     /* Now we can try to perform the conversion. */
 1450                           781                 :           1428 :     return InputFunctionCallSafe(&my_extra->inputproc,
                                782                 :                :                                  str,
                                783                 :                :                                  my_extra->typioparam,
                                784                 :                :                                  typmod,
                                785                 :                :                                  (Node *) escontext,
                                786                 :                :                                  &converted);
                                787                 :                : }
                                788                 :                : 
                                789                 :                : 
                                790                 :                : /*
                                791                 :                :  * Is character a valid identifier start?
                                792                 :                :  * Must match scan.l's {ident_start} character class.
                                793                 :                :  */
                                794                 :                : static bool
 3907 teodor@sigaev.ru          795                 :           1801 : is_ident_start(unsigned char c)
                                796                 :                : {
                                797                 :                :     /* Underscores and ASCII letters are OK */
                                798         [ -  + ]:           1801 :     if (c == '_')
 3907 teodor@sigaev.ru          799                 :UIC           0 :         return true;
 3907 teodor@sigaev.ru          800   [ +  +  -  +  :GIC        1801 :     if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'))
                                        +  +  +  + ]
                                801                 :           1692 :         return true;
                                802                 :                :     /* Any high-bit-set character is OK (might be part of a multibyte char) */
 3897 tgl@sss.pgh.pa.us         803         [ -  + ]:            109 :     if (IS_HIGHBIT_SET(c))
 3907 teodor@sigaev.ru          804                 :UIC           0 :         return true;
 3907 teodor@sigaev.ru          805                 :GIC         109 :     return false;
                                806                 :                : }
                                807                 :                : 
                                808                 :                : /*
                                809                 :                :  * Is character a valid identifier continuation?
                                810                 :                :  * Must match scan.l's {ident_cont} character class.
                                811                 :                :  */
                                812                 :                : static bool
                                813                 :           1692 : is_ident_cont(unsigned char c)
                                814                 :                : {
                                815                 :                :     /* Can be digit or dollar sign ... */
 3897 tgl@sss.pgh.pa.us         816   [ +  +  +  -  :           1692 :     if ((c >= '0' && c <= '9') || c == '$')
                                              -  + ]
 3907 teodor@sigaev.ru          817                 :UIC           0 :         return true;
                                818                 :                :     /* ... or an identifier start character */
 3907 teodor@sigaev.ru          819                 :GIC        1692 :     return is_ident_start(c);
                                820                 :                : }
                                821                 :                : 
                                822                 :                : /*
                                823                 :                :  * parse_ident - parse a SQL qualified identifier into separate identifiers.
                                824                 :                :  * When strict mode is active (second parameter), then any chars after
                                825                 :                :  * the last identifier are disallowed.
                                826                 :                :  */
                                827                 :                : Datum
                                828                 :             83 : parse_ident(PG_FUNCTION_ARGS)
                                829                 :                : {
 3897 tgl@sss.pgh.pa.us         830                 :             83 :     text       *qualname = PG_GETARG_TEXT_PP(0);
                                831                 :             83 :     bool        strict = PG_GETARG_BOOL(1);
                                832                 :             83 :     char       *qualname_str = text_to_cstring(qualname);
                                833                 :             83 :     ArrayBuildState *astate = NULL;
                                834                 :                :     char       *nextp;
 3907 teodor@sigaev.ru          835                 :             83 :     bool        after_dot = false;
                                836                 :                : 
                                837                 :                :     /*
                                838                 :                :      * The code below scribbles on qualname_str in some cases, so we should
                                839                 :                :      * reconvert qualname if we need to show the original string in error
                                840                 :                :      * messages.
                                841                 :                :      */
                                842                 :             83 :     nextp = qualname_str;
                                843                 :                : 
                                844                 :                :     /* skip leading whitespace */
 3475 tgl@sss.pgh.pa.us         845         [ +  + ]:            105 :     while (scanner_isspace(*nextp))
 3907 teodor@sigaev.ru          846                 :             22 :         nextp++;
                                847                 :                : 
                                848                 :                :     for (;;)
                                849                 :             75 :     {
                                850                 :                :         char       *curname;
 3897 tgl@sss.pgh.pa.us         851                 :            158 :         bool        missing_ident = true;
                                852                 :                : 
                                853         [ +  + ]:            158 :         if (*nextp == '"')
                                854                 :                :         {
                                855                 :                :             char       *endp;
                                856                 :                : 
 3907 teodor@sigaev.ru          857                 :             49 :             curname = nextp + 1;
                                858                 :                :             for (;;)
                                859                 :                :             {
 3897 tgl@sss.pgh.pa.us         860                 :             49 :                 endp = strchr(nextp + 1, '"');
 3907 teodor@sigaev.ru          861         [ -  + ]:             49 :                 if (endp == NULL)
 3907 teodor@sigaev.ru          862         [ #  # ]:UIC           0 :                     ereport(ERROR,
                                863                 :                :                             (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
                                864                 :                :                              errmsg("string is not a valid identifier: \"%s\"",
                                865                 :                :                                     text_to_cstring(qualname)),
                                866                 :                :                              errdetail("String has unclosed double quotes.")));
 3897 tgl@sss.pgh.pa.us         867         [ +  - ]:GIC          49 :                 if (endp[1] != '"')
 3907 teodor@sigaev.ru          868                 :             49 :                     break;
 3907 teodor@sigaev.ru          869                 :UIC           0 :                 memmove(endp, endp + 1, strlen(endp));
                                870                 :              0 :                 nextp = endp;
                                871                 :                :             }
 3907 teodor@sigaev.ru          872                 :GIC          49 :             nextp = endp + 1;
                                873                 :             49 :             *endp = '\0';
                                874                 :                : 
                                875         [ -  + ]:             49 :             if (endp - curname == 0)
 3907 teodor@sigaev.ru          876         [ #  # ]:UIC           0 :                 ereport(ERROR,
                                877                 :                :                         (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
                                878                 :                :                          errmsg("string is not a valid identifier: \"%s\"",
                                879                 :                :                                 text_to_cstring(qualname)),
                                880                 :                :                          errdetail("Quoted identifier must not be empty.")));
                                881                 :                : 
 3907 teodor@sigaev.ru          882                 :GIC          49 :             astate = accumArrayResult(astate, CStringGetTextDatum(curname),
                                883                 :                :                                       false, TEXTOID, CurrentMemoryContext);
                                884                 :             49 :             missing_ident = false;
                                885                 :                :         }
 3897 tgl@sss.pgh.pa.us         886         [ +  + ]:            109 :         else if (is_ident_start((unsigned char) *nextp))
                                887                 :                :         {
                                888                 :                :             char       *downname;
                                889                 :                :             int         len;
                                890                 :                :             text       *part;
                                891                 :                : 
                                892                 :             77 :             curname = nextp++;
                                893         [ +  + ]:           1692 :             while (is_ident_cont((unsigned char) *nextp))
                                894                 :           1615 :                 nextp++;
                                895                 :                : 
                                896                 :             77 :             len = nextp - curname;
                                897                 :                : 
                                898                 :                :             /*
                                899                 :                :              * We don't implicitly truncate identifiers. This is useful for
                                900                 :                :              * allowing the user to check for specific parts of the identifier
                                901                 :                :              * being too long. It's easy enough for the user to get the
                                902                 :                :              * truncated names by casting our output to name[].
                                903                 :                :              */
                                904                 :             77 :             downname = downcase_identifier(curname, len, false, false);
                                905                 :             77 :             part = cstring_to_text_with_len(downname, len);
                                906                 :             77 :             astate = accumArrayResult(astate, PointerGetDatum(part), false,
                                907                 :                :                                       TEXTOID, CurrentMemoryContext);
                                908                 :             77 :             missing_ident = false;
                                909                 :                :         }
                                910                 :                : 
 3907 teodor@sigaev.ru          911         [ +  + ]:            158 :         if (missing_ident)
                                912                 :                :         {
                                913                 :                :             /* Different error messages based on where we failed. */
                                914         [ +  + ]:             32 :             if (*nextp == '.')
                                915         [ +  - ]:             12 :                 ereport(ERROR,
                                916                 :                :                         (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
                                917                 :                :                          errmsg("string is not a valid identifier: \"%s\"",
                                918                 :                :                                 text_to_cstring(qualname)),
                                919                 :                :                          errdetail("No valid identifier before \".\".")));
                                920         [ +  + ]:             20 :             else if (after_dot)
                                921         [ +  - ]:              8 :                 ereport(ERROR,
                                922                 :                :                         (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
                                923                 :                :                          errmsg("string is not a valid identifier: \"%s\"",
                                924                 :                :                                 text_to_cstring(qualname)),
                                925                 :                :                          errdetail("No valid identifier after \".\".")));
                                926                 :                :             else
                                927         [ +  - ]:             12 :                 ereport(ERROR,
                                928                 :                :                         (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
                                929                 :                :                          errmsg("string is not a valid identifier: \"%s\"",
                                930                 :                :                                 text_to_cstring(qualname))));
                                931                 :                :         }
                                932                 :                : 
 3475 tgl@sss.pgh.pa.us         933         [ +  + ]:            158 :         while (scanner_isspace(*nextp))
 3907 teodor@sigaev.ru          934                 :             32 :             nextp++;
                                935                 :                : 
                                936         [ +  + ]:            126 :         if (*nextp == '.')
                                937                 :                :         {
                                938                 :             75 :             after_dot = true;
                                939                 :             75 :             nextp++;
 3475 tgl@sss.pgh.pa.us         940         [ +  + ]:             99 :             while (scanner_isspace(*nextp))
 3907 teodor@sigaev.ru          941                 :             24 :                 nextp++;
                                942                 :                :         }
                                943         [ +  + ]:             51 :         else if (*nextp == '\0')
                                944                 :                :         {
                                945                 :             30 :             break;
                                946                 :                :         }
                                947                 :                :         else
                                948                 :                :         {
                                949         [ +  + ]:             21 :             if (strict)
                                950         [ +  - ]:             16 :                 ereport(ERROR,
                                951                 :                :                         (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
                                952                 :                :                          errmsg("string is not a valid identifier: \"%s\"",
                                953                 :                :                                 text_to_cstring(qualname))));
                                954                 :              5 :             break;
                                955                 :                :         }
                                956                 :                :     }
                                957                 :                : 
                                958                 :             35 :     PG_RETURN_DATUM(makeArrayResult(astate, CurrentMemoryContext));
                                959                 :                : }
                                960                 :                : 
                                961                 :                : /*
                                962                 :                :  * pg_current_logfile
                                963                 :                :  *
                                964                 :                :  * Report current log file used by log collector by scanning current_logfiles.
                                965                 :                :  */
                                966                 :                : Datum
 3557 rhaas@postgresql.org      967                 :              6 : pg_current_logfile(PG_FUNCTION_ARGS)
                                968                 :                : {
                                969                 :                :     FILE       *fd;
                                970                 :                :     char        lbuffer[MAXPGPATH];
                                971                 :                :     char       *logfmt;
                                972                 :                : 
                                973                 :                :     /* The log format parameter is optional */
                                974   [ +  -  -  + ]:              6 :     if (PG_NARGS() == 0 || PG_ARGISNULL(0))
 3557 rhaas@postgresql.org      975                 :UIC           0 :         logfmt = NULL;
                                976                 :                :     else
                                977                 :                :     {
 3557 rhaas@postgresql.org      978                 :GIC           6 :         logfmt = text_to_cstring(PG_GETARG_TEXT_PP(0));
                                979                 :                : 
 1776 michael@paquier.xyz       980         [ +  + ]:              6 :         if (strcmp(logfmt, "stderr") != 0 &&
                                981         [ +  + ]:              4 :             strcmp(logfmt, "csvlog") != 0 &&
                                982         [ -  + ]:              2 :             strcmp(logfmt, "jsonlog") != 0)
 3557 rhaas@postgresql.org      983         [ #  # ]:UIC           0 :             ereport(ERROR,
                                984                 :                :                     (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
                                985                 :                :                      errmsg("log format \"%s\" is not supported", logfmt),
                                986                 :                :                      errhint("The supported log formats are \"stderr\", \"csvlog\", and \"jsonlog\".")));
                                987                 :                :     }
                                988                 :                : 
 3557 rhaas@postgresql.org      989                 :GIC           6 :     fd = AllocateFile(LOG_METAINFO_DATAFILE, "r");
                                990         [ -  + ]:              6 :     if (fd == NULL)
                                991                 :                :     {
 3557 rhaas@postgresql.org      992         [ #  # ]:UIC           0 :         if (errno != ENOENT)
                                993         [ #  # ]:              0 :             ereport(ERROR,
                                994                 :                :                     (errcode_for_file_access(),
                                995                 :                :                      errmsg("could not read file \"%s\": %m",
                                996                 :                :                             LOG_METAINFO_DATAFILE)));
                                997                 :              0 :         PG_RETURN_NULL();
                                998                 :                :     }
                                999                 :                : 
                               1000                 :                : #ifdef WIN32
                               1001                 :                :     /* syslogger.c writes CRLF line endings on Windows */
                               1002                 :                :     _setmode(_fileno(fd), _O_TEXT);
                               1003                 :                : #endif
                               1004                 :                : 
                               1005                 :                :     /*
                               1006                 :                :      * Read the file to gather current log filename(s) registered by the
                               1007                 :                :      * syslogger.
                               1008                 :                :      */
 3557 rhaas@postgresql.org     1009         [ +  - ]:GIC          12 :     while (fgets(lbuffer, sizeof(lbuffer), fd) != NULL)
                               1010                 :                :     {
                               1011                 :                :         char       *log_format;
                               1012                 :                :         char       *log_filepath;
                               1013                 :                :         char       *nlpos;
                               1014                 :                : 
                               1015                 :                :         /* Extract log format and log file path from the line. */
 2333 tgl@sss.pgh.pa.us        1016                 :             12 :         log_format = lbuffer;
 3557 rhaas@postgresql.org     1017                 :             12 :         log_filepath = strchr(lbuffer, ' ');
                               1018         [ -  + ]:             12 :         if (log_filepath == NULL)
                               1019                 :                :         {
                               1020                 :                :             /* Uh oh.  No space found, so file content is corrupted. */
 3557 rhaas@postgresql.org     1021         [ #  # ]:UIC           0 :             elog(ERROR,
                               1022                 :                :                  "missing space character in \"%s\"", LOG_METAINFO_DATAFILE);
                               1023                 :                :             break;
                               1024                 :                :         }
                               1025                 :                : 
 3557 rhaas@postgresql.org     1026                 :GIC          12 :         *log_filepath = '\0';
                               1027                 :             12 :         log_filepath++;
                               1028                 :             12 :         nlpos = strchr(log_filepath, '\n');
                               1029         [ -  + ]:             12 :         if (nlpos == NULL)
                               1030                 :                :         {
                               1031                 :                :             /* Uh oh.  No newline found, so file content is corrupted. */
 3557 rhaas@postgresql.org     1032         [ #  # ]:UIC           0 :             elog(ERROR,
                               1033                 :                :                  "missing newline character in \"%s\"", LOG_METAINFO_DATAFILE);
                               1034                 :                :             break;
                               1035                 :                :         }
 3557 rhaas@postgresql.org     1036                 :GIC          12 :         *nlpos = '\0';
                               1037                 :                : 
                               1038   [ +  -  +  + ]:             12 :         if (logfmt == NULL || strcmp(logfmt, log_format) == 0)
                               1039                 :                :         {
                               1040                 :              6 :             FreeFile(fd);
                               1041                 :              6 :             PG_RETURN_TEXT_P(cstring_to_text(log_filepath));
                               1042                 :                :         }
                               1043                 :                :     }
                               1044                 :                : 
                               1045                 :                :     /* Close the current log filename file. */
 3557 rhaas@postgresql.org     1046                 :UIC           0 :     FreeFile(fd);
                               1047                 :                : 
                               1048                 :              0 :     PG_RETURN_NULL();
                               1049                 :                : }
                               1050                 :                : 
                               1051                 :                : /*
                               1052                 :                :  * Report current log file used by log collector (1 argument version)
                               1053                 :                :  *
                               1054                 :                :  * note: this wrapper is necessary to pass the sanity check in opr_sanity,
                               1055                 :                :  * which checks that all built-in functions that share the implementing C
                               1056                 :                :  * function take the same number of arguments
                               1057                 :                :  */
                               1058                 :                : Datum
 3557 rhaas@postgresql.org     1059                 :GIC           6 : pg_current_logfile_1arg(PG_FUNCTION_ARGS)
                               1060                 :                : {
                               1061                 :              6 :     return pg_current_logfile(fcinfo);
                               1062                 :                : }
                               1063                 :                : 
                               1064                 :                : /*
                               1065                 :                :  * SQL wrapper around RelationGetReplicaIndex().
                               1066                 :                :  */
                               1067                 :                : Datum
 3537 peter_e@gmx.net          1068                 :            439 : pg_get_replica_identity_index(PG_FUNCTION_ARGS)
                               1069                 :                : {
                               1070                 :            439 :     Oid         reloid = PG_GETARG_OID(0);
                               1071                 :                :     Oid         idxoid;
                               1072                 :                :     Relation    rel;
                               1073                 :                : 
 2868 andres@anarazel.de       1074                 :            439 :     rel = table_open(reloid, AccessShareLock);
 3537 peter_e@gmx.net          1075                 :            439 :     idxoid = RelationGetReplicaIndex(rel);
 2868 andres@anarazel.de       1076                 :            439 :     table_close(rel, AccessShareLock);
                               1077                 :                : 
 3537 peter_e@gmx.net          1078         [ +  + ]:            439 :     if (OidIsValid(idxoid))
                               1079                 :            220 :         PG_RETURN_OID(idxoid);
                               1080                 :                :     else
                               1081                 :            219 :         PG_RETURN_NULL();
                               1082                 :                : }
                               1083                 :                : 
                               1084                 :                : /*
                               1085                 :                :  * Transition function for the ANY_VALUE aggregate
                               1086                 :                :  */
                               1087                 :                : Datum
 1375 peter@eisentraut.org     1088                 :             12 : any_value_transfn(PG_FUNCTION_ARGS)
                               1089                 :                : {
                               1090                 :             12 :     PG_RETURN_DATUM(PG_GETARG_DATUM(0));
                               1091                 :                : }
        

Generated by: LCOV version 2.0-1