PostgreSQL Source Code git master
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
stat_utils.h File Reference
#include "fmgr.h"
Include dependency graph for stat_utils.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  StatsArgInfo
 

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)
 
bool stats_check_arg_pair (FunctionCallInfo fcinfo, struct StatsArgInfo *arginfo, int argnum1, int argnum2)
 
void stats_lock_check_privileges (Oid reloid)
 
bool stats_fill_fcinfo_from_arg_pairs (FunctionCallInfo pairs_fcinfo, FunctionCallInfo positional_fcinfo, struct StatsArgInfo *arginfo)
 

Function Documentation

◆ stats_check_arg_array()

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

Definition at line 55 of file stat_utils.c.

58{
59 ArrayType *arr;
60
61 if (PG_ARGISNULL(argnum))
62 return true;
63
65
66 if (ARR_NDIM(arr) != 1)
67 {
69 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
70 errmsg("\"%s\" cannot be a multidimensional array",
71 arginfo[argnum].argname)));
72 return false;
73 }
74
75 if (array_contains_nulls(arr))
76 {
78 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
79 errmsg("\"%s\" array cannot contain NULL values",
80 arginfo[argnum].argname)));
81 return false;
82 }
83
84 return true;
85}
#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 WARNING
Definition: elog.h:36
#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, PG_GETARG_DATUM, and WARNING.

Referenced by attribute_statistics_update().

◆ stats_check_arg_pair()

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

Definition at line 96 of file stat_utils.c.

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

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

Referenced by attribute_statistics_update().

◆ stats_check_required_arg()

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

Definition at line 36 of file stat_utils.c.

39{
40 if (PG_ARGISNULL(argnum))
42 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
43 errmsg("\"%s\" cannot be NULL",
44 arginfo[argnum].argname)));
45}
#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 
)

Definition at line 263 of file stat_utils.c.

266{
267 Datum *args;
268 bool *argnulls;
269 Oid *types;
270 int nargs;
271 bool result = true;
272
273 /* clear positional args */
274 for (int i = 0; arginfo[i].argname != NULL; i++)
275 {
276 positional_fcinfo->args[i].value = (Datum) 0;
277 positional_fcinfo->args[i].isnull = true;
278 }
279
280 nargs = extract_variadic_args(pairs_fcinfo, 0, true,
281 &args, &types, &argnulls);
282
283 if (nargs % 2 != 0)
285 errmsg("variadic arguments must be name/value pairs"),
286 errhint("Provide an even number of variadic arguments that can be divided into pairs."));
287
288 /*
289 * For each argument name/value pair, find corresponding positional
290 * argument for the argument name, and assign the argument value to
291 * positional_fcinfo.
292 */
293 for (int i = 0; i < nargs; i += 2)
294 {
295 int argnum;
296 char *argname;
297
298 if (argnulls[i])
300 (errmsg("name at variadic position %d is NULL", i + 1)));
301
302 if (types[i] != TEXTOID)
304 (errmsg("name at variadic position %d has type \"%s\", expected type \"%s\"",
305 i + 1, format_type_be(types[i]),
306 format_type_be(TEXTOID))));
307
308 if (argnulls[i + 1])
309 continue;
310
311 argname = TextDatumGetCString(args[i]);
312
313 /*
314 * The 'version' argument is a special case, not handled by arginfo
315 * because it's not a valid positional argument.
316 *
317 * For now, 'version' is accepted but ignored. In the future it can be
318 * used to interpret older statistics properly.
319 */
320 if (pg_strcasecmp(argname, "version") == 0)
321 continue;
322
323 argnum = get_arg_by_name(argname, arginfo);
324
325 if (argnum < 0 || !stats_check_arg_type(argname, types[i + 1],
326 arginfo[argnum].argtype))
327 {
328 result = false;
329 continue;
330 }
331
332 positional_fcinfo->args[argnum].value = args[i + 1];
333 positional_fcinfo->args[argnum].isnull = false;
334 }
335
336 return result;
337}
#define TextDatumGetCString(d)
Definition: builtins.h:98
struct typedefs * types
Definition: ecpg.c:30
int errhint(const char *fmt,...)
Definition: elog.c:1317
char * format_type_be(Oid type_oid)
Definition: format_type.c:343
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
int pg_strcasecmp(const char *s1, const char *s2)
Definition: pgstrcasecmp.c:36
uintptr_t Datum
Definition: postgres.h:69
unsigned int Oid
Definition: postgres_ext.h:32
static int get_arg_by_name(const char *argname, struct StatsArgInfo *arginfo)
Definition: stat_utils.c:221
static bool stats_check_arg_type(const char *argname, Oid argtype, Oid expectedtype)
Definition: stat_utils.c:239
NullableDatum args[FLEXIBLE_ARRAY_MEMBER]
Definition: fmgr.h:95
Datum value
Definition: postgres.h:80
bool isnull
Definition: postgres.h:82
const char * argname
Definition: stat_utils.h:20

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 130 of file stat_utils.c.

131{
132 Relation table;
133 Oid table_oid = reloid;
134 Oid index_oid = InvalidOid;
135 LOCKMODE index_lockmode = NoLock;
136
137 /*
138 * For indexes, we follow the locking behavior in do_analyze_rel() and
139 * check_lock_if_inplace_updateable_rel(), which is to lock the table
140 * first in ShareUpdateExclusive mode and then the index in AccessShare
141 * mode.
142 *
143 * Partitioned indexes are treated differently than normal indexes in
144 * check_lock_if_inplace_updateable_rel(), so we take a
145 * ShareUpdateExclusive lock on both the partitioned table and the
146 * partitioned index.
147 */
148 switch (get_rel_relkind(reloid))
149 {
150 case RELKIND_INDEX:
151 index_oid = reloid;
152 table_oid = IndexGetRelation(index_oid, false);
153 index_lockmode = AccessShareLock;
154 break;
155 case RELKIND_PARTITIONED_INDEX:
156 index_oid = reloid;
157 table_oid = IndexGetRelation(index_oid, false);
158 index_lockmode = ShareUpdateExclusiveLock;
159 break;
160 default:
161 break;
162 }
163
164 table = relation_open(table_oid, ShareUpdateExclusiveLock);
165
166 /* the relkinds that can be used with ANALYZE */
167 switch (table->rd_rel->relkind)
168 {
169 case RELKIND_RELATION:
170 case RELKIND_MATVIEW:
171 case RELKIND_FOREIGN_TABLE:
172 case RELKIND_PARTITIONED_TABLE:
173 break;
174 default:
176 (errcode(ERRCODE_WRONG_OBJECT_TYPE),
177 errmsg("cannot modify statistics for relation \"%s\"",
179 errdetail_relkind_not_supported(table->rd_rel->relkind)));
180 }
181
182 if (OidIsValid(index_oid))
183 {
185
186 Assert(index_lockmode != NoLock);
187 index = relation_open(index_oid, index_lockmode);
188
189 Assert(index->rd_index && index->rd_index->indrelid == table_oid);
190
191 /* retain lock on index */
193 }
194
195 if (table->rd_rel->relisshared)
197 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
198 errmsg("cannot modify statistics for shared relation")));
199
200 if (!object_ownercheck(DatabaseRelationId, MyDatabaseId, GetUserId()))
201 {
203 GetUserId(),
205
206 if (aclresult != ACLCHECK_OK)
207 aclcheck_error(aclresult,
208 get_relkind_objtype(table->rd_rel->relkind),
209 NameStr(table->rd_rel->relname));
210 }
211
212 /* retain lock on table */
213 relation_close(table, NoLock);
214}
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:4058
AclResult pg_class_aclcheck(Oid table_oid, Oid roleid, AclMode mode)
Definition: aclchk.c:4007
#define NameStr(name)
Definition: c.h:717
#define OidIsValid(objectId)
Definition: c.h:746
Oid MyDatabaseId
Definition: globals.c:93
Assert(PointerIsAligned(start, uint64))
Oid IndexGetRelation(Oid indexId, bool missing_ok)
Definition: index.c:3583
int LOCKMODE
Definition: lockdefs.h:26
#define NoLock
Definition: lockdefs.h:34
#define AccessShareLock
Definition: lockdefs.h:36
#define ShareUpdateExclusiveLock
Definition: lockdefs.h:39
char get_rel_relkind(Oid relid)
Definition: lsyscache.c:2042
Oid GetUserId(void)
Definition: miscinit.c:517
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 InvalidOid
Definition: postgres_ext.h:37
#define RelationGetRelid(relation)
Definition: rel.h:512
#define RelationGetRelationName(relation)
Definition: rel.h:546
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
Definition: type.h:96

References AccessShareLock, ACL_MAINTAIN, aclcheck_error(), ACLCHECK_OK, Assert(), ereport, errcode(), errdetail_relkind_not_supported(), errmsg(), ERROR, get_rel_relkind(), get_relkind_objtype(), GetUserId(), IndexGetRelation(), InvalidOid, MyDatabaseId, NameStr, NoLock, object_ownercheck(), OidIsValid, 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().