PostgreSQL Source Code  git master
stat_utils.c File Reference
#include "postgres.h"
#include "access/relation.h"
#include "catalog/pg_database.h"
#include "funcapi.h"
#include "miscadmin.h"
#include "statistics/stat_utils.h"
#include "utils/acl.h"
#include "utils/array.h"
#include "utils/builtins.h"
#include "utils/rel.h"
Include dependency graph for stat_utils.c:

Go to the source code of this file.

Functions

void stats_check_required_arg (FunctionCallInfo fcinfo, struct StatsArgInfo *arginfo, int argnum)
 
bool stats_check_arg_array (FunctionCallInfo fcinfo, struct StatsArgInfo *arginfo, int argnum, int elevel)
 
bool stats_check_arg_pair (FunctionCallInfo fcinfo, struct StatsArgInfo *arginfo, int argnum1, int argnum2, int elevel)
 
void stats_lock_check_privileges (Oid reloid)
 
static int get_arg_by_name (const char *argname, struct StatsArgInfo *arginfo, int elevel)
 
static bool stats_check_arg_type (const char *argname, Oid argtype, Oid expectedtype, int elevel)
 
bool stats_fill_fcinfo_from_arg_pairs (FunctionCallInfo pairs_fcinfo, FunctionCallInfo positional_fcinfo, struct StatsArgInfo *arginfo, int elevel)
 

Function Documentation

◆ get_arg_by_name()

static int get_arg_by_name ( const char *  argname,
struct StatsArgInfo arginfo,
int  elevel 
)
static

Definition at line 175 of file stat_utils.c.

176 {
177  int argnum;
178 
179  for (argnum = 0; arginfo[argnum].argname != NULL; argnum++)
180  if (pg_strcasecmp(argname, arginfo[argnum].argname) == 0)
181  return argnum;
182 
183  ereport(elevel,
184  (errmsg("unrecognized argument name: \"%s\"", argname)));
185 
186  return -1;
187 }
int errmsg(const char *fmt,...)
Definition: elog.c:1070
#define ereport(elevel,...)
Definition: elog.h:149
int pg_strcasecmp(const char *s1, const char *s2)
Definition: pgstrcasecmp.c:36
const char * argname
Definition: stat_utils.h:20

References StatsArgInfo::argname, ereport, errmsg(), and pg_strcasecmp().

Referenced by stats_fill_fcinfo_from_arg_pairs().

◆ stats_check_arg_array()

bool stats_check_arg_array ( FunctionCallInfo  fcinfo,
struct StatsArgInfo arginfo,
int  argnum,
int  elevel 
)

Definition at line 52 of file stat_utils.c.

55 {
56  ArrayType *arr;
57 
58  if (PG_ARGISNULL(argnum))
59  return true;
60 
61  arr = DatumGetArrayTypeP(PG_GETARG_DATUM(argnum));
62 
63  if (ARR_NDIM(arr) != 1)
64  {
65  ereport(elevel,
66  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
67  errmsg("\"%s\" cannot be a multidimensional array",
68  arginfo[argnum].argname)));
69  return false;
70  }
71 
72  if (array_contains_nulls(arr))
73  {
74  ereport(elevel,
75  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
76  errmsg("\"%s\" array cannot contain NULL values",
77  arginfo[argnum].argname)));
78  return false;
79  }
80 
81  return true;
82 }
#define ARR_NDIM(a)
Definition: array.h:290
#define DatumGetArrayTypeP(X)
Definition: array.h:261
bool array_contains_nulls(ArrayType *array)
Definition: arrayfuncs.c:3767
int errcode(int sqlerrcode)
Definition: elog.c:853
#define PG_ARGISNULL(n)
Definition: fmgr.h:209
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:268

References ARR_NDIM, array_contains_nulls(), DatumGetArrayTypeP, ereport, errcode(), errmsg(), PG_ARGISNULL, and PG_GETARG_DATUM.

Referenced by attribute_statistics_update().

◆ stats_check_arg_pair()

bool stats_check_arg_pair ( FunctionCallInfo  fcinfo,
struct StatsArgInfo arginfo,
int  argnum1,
int  argnum2,
int  elevel 
)

Definition at line 93 of file stat_utils.c.

96 {
97  if (PG_ARGISNULL(argnum1) && PG_ARGISNULL(argnum2))
98  return true;
99 
100  if (PG_ARGISNULL(argnum1) || PG_ARGISNULL(argnum2))
101  {
102  int nullarg = PG_ARGISNULL(argnum1) ? argnum1 : argnum2;
103  int otherarg = PG_ARGISNULL(argnum1) ? argnum2 : argnum1;
104 
105  ereport(elevel,
106  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
107  errmsg("\"%s\" must be specified when \"%s\" is specified",
108  arginfo[nullarg].argname,
109  arginfo[otherarg].argname)));
110 
111  return false;
112  }
113 
114  return true;
115 }

References ereport, errcode(), errmsg(), and PG_ARGISNULL.

Referenced by attribute_statistics_update().

◆ stats_check_arg_type()

static bool stats_check_arg_type ( const char *  argname,
Oid  argtype,
Oid  expectedtype,
int  elevel 
)
static

Definition at line 193 of file stat_utils.c.

194 {
195  if (argtype != expectedtype)
196  {
197  ereport(elevel,
198  (errmsg("argument \"%s\" has type \"%s\", expected type \"%s\"",
199  argname, format_type_be(argtype),
200  format_type_be(expectedtype))));
201  return false;
202  }
203 
204  return true;
205 }
char * format_type_be(Oid type_oid)
Definition: format_type.c:343

References ereport, errmsg(), and format_type_be().

Referenced by stats_fill_fcinfo_from_arg_pairs().

◆ stats_check_required_arg()

void stats_check_required_arg ( FunctionCallInfo  fcinfo,
struct StatsArgInfo arginfo,
int  argnum 
)

Definition at line 33 of file stat_utils.c.

36 {
37  if (PG_ARGISNULL(argnum))
38  ereport(ERROR,
39  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
40  errmsg("\"%s\" cannot be NULL",
41  arginfo[argnum].argname)));
42 }
#define ERROR
Definition: elog.h:39

References ereport, errcode(), errmsg(), ERROR, and PG_ARGISNULL.

Referenced by attribute_statistics_update(), pg_clear_attribute_stats(), and relation_statistics_update().

◆ stats_fill_fcinfo_from_arg_pairs()

bool stats_fill_fcinfo_from_arg_pairs ( FunctionCallInfo  pairs_fcinfo,
FunctionCallInfo  positional_fcinfo,
struct StatsArgInfo arginfo,
int  elevel 
)

Definition at line 217 of file stat_utils.c.

221 {
222  Datum *args;
223  bool *argnulls;
224  Oid *types;
225  int nargs;
226  bool result = true;
227 
228  /* clear positional args */
229  for (int i = 0; arginfo[i].argname != NULL; i++)
230  {
231  positional_fcinfo->args[i].value = (Datum) 0;
232  positional_fcinfo->args[i].isnull = true;
233  }
234 
235  nargs = extract_variadic_args(pairs_fcinfo, 0, true,
236  &args, &types, &argnulls);
237 
238  if (nargs % 2 != 0)
239  ereport(ERROR,
240  errmsg("variadic arguments must be name/value pairs"),
241  errhint("Provide an even number of variadic arguments that can be divided into pairs."));
242 
243  /*
244  * For each argument name/value pair, find corresponding positional
245  * argument for the argument name, and assign the argument value to
246  * postitional_fcinfo.
247  */
248  for (int i = 0; i < nargs; i += 2)
249  {
250  int argnum;
251  char *argname;
252 
253  if (argnulls[i])
254  ereport(ERROR,
255  (errmsg("name at variadic position %d is NULL", i + 1)));
256 
257  if (types[i] != TEXTOID)
258  ereport(ERROR,
259  (errmsg("name at variadic position %d has type \"%s\", expected type \"%s\"",
260  i + 1, format_type_be(types[i]),
261  format_type_be(TEXTOID))));
262 
263  if (argnulls[i + 1])
264  continue;
265 
266  argname = TextDatumGetCString(args[i]);
267 
268  /*
269  * The 'version' argument is a special case, not handled by arginfo
270  * because it's not a valid positional argument.
271  *
272  * For now, 'version' is accepted but ignored. In the future it can be
273  * used to interpret older statistics properly.
274  */
275  if (pg_strcasecmp(argname, "version") == 0)
276  continue;
277 
278  argnum = get_arg_by_name(argname, arginfo, elevel);
279 
280  if (argnum < 0 || !stats_check_arg_type(argname, types[i + 1],
281  arginfo[argnum].argtype,
282  elevel))
283  {
284  result = false;
285  continue;
286  }
287 
288  positional_fcinfo->args[argnum].value = args[i + 1];
289  positional_fcinfo->args[argnum].isnull = false;
290  }
291 
292  return result;
293 }
#define TextDatumGetCString(d)
Definition: builtins.h:98
struct typedefs * types
Definition: ecpg.c:30
int errhint(const char *fmt,...)
Definition: elog.c:1317
int extract_variadic_args(FunctionCallInfo fcinfo, int variadic_start, bool convert_unknown, Datum **args, Oid **types, bool **nulls)
Definition: funcapi.c:2005
int i
Definition: isn.c:72
uintptr_t Datum
Definition: postgres.h:64
unsigned int Oid
Definition: postgres_ext.h:31
static int get_arg_by_name(const char *argname, struct StatsArgInfo *arginfo, int elevel)
Definition: stat_utils.c:175
static bool stats_check_arg_type(const char *argname, Oid argtype, Oid expectedtype, int elevel)
Definition: stat_utils.c:193
NullableDatum args[FLEXIBLE_ARRAY_MEMBER]
Definition: fmgr.h:95
Datum value
Definition: postgres.h:75
bool isnull
Definition: postgres.h:77

References StatsArgInfo::argname, generate_unaccent_rules::args, FunctionCallInfoBaseData::args, ereport, errhint(), errmsg(), ERROR, extract_variadic_args(), format_type_be(), get_arg_by_name(), i, NullableDatum::isnull, pg_strcasecmp(), stats_check_arg_type(), TextDatumGetCString, types, and NullableDatum::value.

Referenced by pg_restore_attribute_stats(), and pg_restore_relation_stats().

◆ stats_lock_check_privileges()

void stats_lock_check_privileges ( Oid  reloid)

Definition at line 127 of file stat_utils.c.

128 {
130  const char relkind = rel->rd_rel->relkind;
131 
132  /* All of the types that can be used with ANALYZE, plus indexes */
133  switch (relkind)
134  {
135  case RELKIND_RELATION:
136  case RELKIND_INDEX:
137  case RELKIND_MATVIEW:
138  case RELKIND_FOREIGN_TABLE:
139  case RELKIND_PARTITIONED_TABLE:
140  case RELKIND_PARTITIONED_INDEX:
141  break;
142  default:
143  ereport(ERROR,
144  (errcode(ERRCODE_WRONG_OBJECT_TYPE),
145  errmsg("cannot modify statistics for relation \"%s\"",
147  errdetail_relkind_not_supported(rel->rd_rel->relkind)));
148  }
149 
150  if (rel->rd_rel->relisshared)
151  ereport(ERROR,
152  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
153  errmsg("cannot modify statistics for shared relation")));
154 
155  if (!object_ownercheck(DatabaseRelationId, MyDatabaseId, GetUserId()))
156  {
158  GetUserId(),
159  ACL_MAINTAIN);
160 
161  if (aclresult != ACLCHECK_OK)
162  aclcheck_error(aclresult,
163  get_relkind_objtype(rel->rd_rel->relkind),
164  NameStr(rel->rd_rel->relname));
165  }
166 
167  relation_close(rel, NoLock);
168 }
AclResult
Definition: acl.h:182
@ ACLCHECK_OK
Definition: acl.h:183
void aclcheck_error(AclResult aclerr, ObjectType objtype, const char *objectname)
Definition: aclchk.c:2622
bool object_ownercheck(Oid classid, Oid objectid, Oid roleid)
Definition: aclchk.c:4064
AclResult pg_class_aclcheck(Oid table_oid, Oid roleid, AclMode mode)
Definition: aclchk.c:4013
#define NameStr(name)
Definition: c.h:700
Oid MyDatabaseId
Definition: globals.c:93
#define NoLock
Definition: lockdefs.h:34
#define ShareUpdateExclusiveLock
Definition: lockdefs.h:39
Oid GetUserId(void)
Definition: miscinit.c:524
ObjectType get_relkind_objtype(char relkind)
#define ACL_MAINTAIN
Definition: parsenodes.h:90
int errdetail_relkind_not_supported(char relkind)
Definition: pg_class.c:24
#define RelationGetRelid(relation)
Definition: rel.h:505
#define RelationGetRelationName(relation)
Definition: rel.h:539
void relation_close(Relation relation, LOCKMODE lockmode)
Definition: relation.c:205
Relation relation_open(Oid relationId, LOCKMODE lockmode)
Definition: relation.c:47
Form_pg_class rd_rel
Definition: rel.h:111

References ACL_MAINTAIN, aclcheck_error(), ACLCHECK_OK, ereport, errcode(), errdetail_relkind_not_supported(), errmsg(), ERROR, get_relkind_objtype(), GetUserId(), MyDatabaseId, NameStr, NoLock, object_ownercheck(), pg_class_aclcheck(), RelationData::rd_rel, relation_close(), relation_open(), RelationGetRelationName, RelationGetRelid, and ShareUpdateExclusiveLock.

Referenced by attribute_statistics_update(), pg_clear_attribute_stats(), and relation_statistics_update().