PostgreSQL Source Code  git master
stat_utils.c File Reference
#include "postgres.h"
#include "access/relation.h"
#include "catalog/pg_database.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)
 

Function Documentation

◆ stats_check_arg_array()

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

Definition at line 51 of file stat_utils.c.

54 {
55  ArrayType *arr;
56 
57  if (PG_ARGISNULL(argnum))
58  return true;
59 
60  arr = DatumGetArrayTypeP(PG_GETARG_DATUM(argnum));
61 
62  if (ARR_NDIM(arr) != 1)
63  {
64  ereport(elevel,
65  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
66  errmsg("\"%s\" cannot be a multidimensional array",
67  arginfo[argnum].argname)));
68  return false;
69  }
70 
71  if (array_contains_nulls(arr))
72  {
73  ereport(elevel,
74  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
75  errmsg("\"%s\" array cannot contain NULL values",
76  arginfo[argnum].argname)));
77  return false;
78  }
79 
80  return true;
81 }
#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
int errmsg(const char *fmt,...)
Definition: elog.c:1070
#define ereport(elevel,...)
Definition: elog.h:149
#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 92 of file stat_utils.c.

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

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

Referenced by attribute_statistics_update().

◆ stats_check_required_arg()

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

Definition at line 32 of file stat_utils.c.

35 {
36  if (PG_ARGISNULL(argnum))
37  ereport(ERROR,
38  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
39  errmsg("\"%s\" cannot be NULL",
40  arginfo[argnum].argname)));
41 }
#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_lock_check_privileges()

void stats_lock_check_privileges ( Oid  reloid)

Definition at line 126 of file stat_utils.c.

127 {
129  const char relkind = rel->rd_rel->relkind;
130 
131  /* All of the types that can be used with ANALYZE, plus indexes */
132  switch (relkind)
133  {
134  case RELKIND_RELATION:
135  case RELKIND_INDEX:
136  case RELKIND_MATVIEW:
137  case RELKIND_FOREIGN_TABLE:
138  case RELKIND_PARTITIONED_TABLE:
139  case RELKIND_PARTITIONED_INDEX:
140  break;
141  default:
142  ereport(ERROR,
143  (errcode(ERRCODE_WRONG_OBJECT_TYPE),
144  errmsg("cannot modify statistics for relation \"%s\"",
146  errdetail_relkind_not_supported(rel->rd_rel->relkind)));
147  }
148 
149  if (rel->rd_rel->relisshared)
150  ereport(ERROR,
151  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
152  errmsg("cannot modify statistics for shared relation")));
153 
154  if (!object_ownercheck(DatabaseRelationId, MyDatabaseId, GetUserId()))
155  {
157  GetUserId(),
158  ACL_MAINTAIN);
159 
160  if (aclresult != ACLCHECK_OK)
161  aclcheck_error(aclresult,
162  get_relkind_objtype(rel->rd_rel->relkind),
163  NameStr(rel->rd_rel->relname));
164  }
165 
166  relation_close(rel, NoLock);
167 }
AclResult
Definition: acl.h:182
@ ACLCHECK_OK
Definition: acl.h:183
void aclcheck_error(AclResult aclerr, ObjectType objtype, const char *objectname)
Definition: aclchk.c:2703
bool object_ownercheck(Oid classid, Oid objectid, Oid roleid)
Definition: aclchk.c:4145
AclResult pg_class_aclcheck(Oid table_oid, Oid roleid, AclMode mode)
Definition: aclchk.c:4094
#define NameStr(name)
Definition: c.h:737
Oid MyDatabaseId
Definition: globals.c:93
#define NoLock
Definition: lockdefs.h:34
#define ShareUpdateExclusiveLock
Definition: lockdefs.h:39
Oid GetUserId(void)
Definition: miscinit.c:514
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().