PostgreSQL Source Code git master
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
relation_stats.c File Reference
#include "postgres.h"
#include "access/heapam.h"
#include "catalog/indexing.h"
#include "catalog/namespace.h"
#include "statistics/stat_utils.h"
#include "utils/builtins.h"
#include "utils/fmgroids.h"
#include "utils/fmgrprotos.h"
#include "utils/lsyscache.h"
#include "utils/syscache.h"
Include dependency graph for relation_stats.c:

Go to the source code of this file.

Enumerations

enum  relation_stats_argnum {
  RELSCHEMA_ARG = 0 , RELNAME_ARG , RELPAGES_ARG , RELTUPLES_ARG ,
  RELALLVISIBLE_ARG , RELALLFROZEN_ARG , NUM_RELATION_STATS_ARGS
}
 

Functions

static bool relation_statistics_update (FunctionCallInfo fcinfo)
 
Datum pg_clear_relation_stats (PG_FUNCTION_ARGS)
 
Datum pg_restore_relation_stats (PG_FUNCTION_ARGS)
 

Variables

static struct StatsArgInfo relarginfo []
 

Enumeration Type Documentation

◆ relation_stats_argnum

Enumerator
RELSCHEMA_ARG 
RELNAME_ARG 
RELPAGES_ARG 
RELTUPLES_ARG 
RELALLVISIBLE_ARG 
RELALLFROZEN_ARG 
NUM_RELATION_STATS_ARGS 

Definition at line 36 of file relation_stats.c.

37{
38 RELSCHEMA_ARG = 0,
45};
@ RELALLVISIBLE_ARG
@ RELSCHEMA_ARG
@ RELNAME_ARG
@ RELPAGES_ARG
@ RELALLFROZEN_ARG
@ RELTUPLES_ARG
@ NUM_RELATION_STATS_ARGS

Function Documentation

◆ pg_clear_relation_stats()

Datum pg_clear_relation_stats ( PG_FUNCTION_ARGS  )

Definition at line 200 of file relation_stats.c.

201{
202 LOCAL_FCINFO(newfcinfo, 6);
203
204 InitFunctionCallInfoData(*newfcinfo, NULL, 6, InvalidOid, NULL, NULL);
205
206 newfcinfo->args[0].value = PG_GETARG_DATUM(0);
207 newfcinfo->args[0].isnull = PG_ARGISNULL(0);
208 newfcinfo->args[1].value = PG_GETARG_DATUM(1);
209 newfcinfo->args[1].isnull = PG_ARGISNULL(1);
210 newfcinfo->args[2].value = UInt32GetDatum(0);
211 newfcinfo->args[2].isnull = false;
212 newfcinfo->args[3].value = Float4GetDatum(-1.0);
213 newfcinfo->args[3].isnull = false;
214 newfcinfo->args[4].value = UInt32GetDatum(0);
215 newfcinfo->args[4].isnull = false;
216 newfcinfo->args[5].value = UInt32GetDatum(0);
217 newfcinfo->args[5].isnull = false;
218
221}
#define PG_RETURN_VOID()
Definition: fmgr.h:349
#define InitFunctionCallInfoData(Fcinfo, Flinfo, Nargs, Collation, Context, Resultinfo)
Definition: fmgr.h:150
#define PG_ARGISNULL(n)
Definition: fmgr.h:209
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:268
#define LOCAL_FCINFO(name, nargs)
Definition: fmgr.h:110
static Datum Float4GetDatum(float4 X)
Definition: postgres.h:480
static Datum UInt32GetDatum(uint32 X)
Definition: postgres.h:237
#define InvalidOid
Definition: postgres_ext.h:35
static bool relation_statistics_update(FunctionCallInfo fcinfo)

References Float4GetDatum(), InitFunctionCallInfoData, InvalidOid, LOCAL_FCINFO, PG_ARGISNULL, PG_GETARG_DATUM, PG_RETURN_VOID, relation_statistics_update(), and UInt32GetDatum().

◆ pg_restore_relation_stats()

Datum pg_restore_relation_stats ( PG_FUNCTION_ARGS  )

Definition at line 224 of file relation_stats.c.

225{
226 LOCAL_FCINFO(positional_fcinfo, NUM_RELATION_STATS_ARGS);
227 bool result = true;
228
229 InitFunctionCallInfoData(*positional_fcinfo, NULL,
231 InvalidOid, NULL, NULL);
232
233 if (!stats_fill_fcinfo_from_arg_pairs(fcinfo, positional_fcinfo,
234 relarginfo))
235 result = false;
236
237 if (!relation_statistics_update(positional_fcinfo))
238 result = false;
239
240 PG_RETURN_BOOL(result);
241}
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:359
static struct StatsArgInfo relarginfo[]
bool stats_fill_fcinfo_from_arg_pairs(FunctionCallInfo pairs_fcinfo, FunctionCallInfo positional_fcinfo, struct StatsArgInfo *arginfo)
Definition: stat_utils.c:285

References InitFunctionCallInfoData, InvalidOid, LOCAL_FCINFO, NUM_RELATION_STATS_ARGS, PG_RETURN_BOOL, relarginfo, relation_statistics_update(), and stats_fill_fcinfo_from_arg_pairs().

◆ relation_statistics_update()

static bool relation_statistics_update ( FunctionCallInfo  fcinfo)
static

Definition at line 64 of file relation_stats.c.

65{
66 bool result = true;
67 char *nspname;
68 char *relname;
69 Oid reloid;
70 Relation crel;
71 BlockNumber relpages = 0;
72 bool update_relpages = false;
73 float reltuples = 0;
74 bool update_reltuples = false;
75 BlockNumber relallvisible = 0;
76 bool update_relallvisible = false;
77 BlockNumber relallfrozen = 0;
78 bool update_relallfrozen = false;
79 HeapTuple ctup;
80 Form_pg_class pgcform;
81 int replaces[4] = {0};
82 Datum values[4] = {0};
83 bool nulls[4] = {0};
84 int nreplaces = 0;
85
88
91
92 reloid = stats_lookup_relid(nspname, relname);
93
96 (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
97 errmsg("recovery is in progress"),
98 errhint("Statistics cannot be modified during recovery.")));
99
101
103 {
104 relpages = PG_GETARG_UINT32(RELPAGES_ARG);
105 update_relpages = true;
106 }
107
109 {
110 reltuples = PG_GETARG_FLOAT4(RELTUPLES_ARG);
111 if (reltuples < -1.0)
112 {
114 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
115 errmsg("reltuples cannot be < -1.0")));
116 result = false;
117 }
118 else
119 update_reltuples = true;
120 }
121
123 {
124 relallvisible = PG_GETARG_UINT32(RELALLVISIBLE_ARG);
125 update_relallvisible = true;
126 }
127
129 {
130 relallfrozen = PG_GETARG_UINT32(RELALLFROZEN_ARG);
131 update_relallfrozen = true;
132 }
133
134 /*
135 * Take RowExclusiveLock on pg_class, consistent with
136 * vac_update_relstats().
137 */
138 crel = table_open(RelationRelationId, RowExclusiveLock);
139
140 ctup = SearchSysCache1(RELOID, ObjectIdGetDatum(reloid));
141 if (!HeapTupleIsValid(ctup))
142 elog(ERROR, "pg_class entry for relid %u not found", reloid);
143
144 pgcform = (Form_pg_class) GETSTRUCT(ctup);
145
146 if (update_relpages && relpages != pgcform->relpages)
147 {
148 replaces[nreplaces] = Anum_pg_class_relpages;
149 values[nreplaces] = UInt32GetDatum(relpages);
150 nreplaces++;
151 }
152
153 if (update_reltuples && reltuples != pgcform->reltuples)
154 {
155 replaces[nreplaces] = Anum_pg_class_reltuples;
156 values[nreplaces] = Float4GetDatum(reltuples);
157 nreplaces++;
158 }
159
160 if (update_relallvisible && relallvisible != pgcform->relallvisible)
161 {
162 replaces[nreplaces] = Anum_pg_class_relallvisible;
163 values[nreplaces] = UInt32GetDatum(relallvisible);
164 nreplaces++;
165 }
166
167 if (update_relallfrozen && relallfrozen != pgcform->relallfrozen)
168 {
169 replaces[nreplaces] = Anum_pg_class_relallfrozen;
170 values[nreplaces] = UInt32GetDatum(relallfrozen);
171 nreplaces++;
172 }
173
174 if (nreplaces > 0)
175 {
176 TupleDesc tupdesc = RelationGetDescr(crel);
177 HeapTuple newtup;
178
179 newtup = heap_modify_tuple_by_cols(ctup, tupdesc, nreplaces,
180 replaces, values, nulls);
181 CatalogTupleUpdate(crel, &newtup->t_self, newtup);
182 heap_freetuple(newtup);
183 }
184
185 ReleaseSysCache(ctup);
186
187 /* release the lock, consistent with vac_update_relstats() */
189
191
192 return result;
193}
uint32 BlockNumber
Definition: block.h:31
static Datum values[MAXATTR]
Definition: bootstrap.c:151
#define TextDatumGetCString(d)
Definition: builtins.h:98
int errhint(const char *fmt,...)
Definition: elog.c:1318
int errcode(int sqlerrcode)
Definition: elog.c:854
int errmsg(const char *fmt,...)
Definition: elog.c:1071
#define WARNING
Definition: elog.h:36
#define ERROR
Definition: elog.h:39
#define elog(elevel,...)
Definition: elog.h:225
#define ereport(elevel,...)
Definition: elog.h:149
#define PG_GETARG_UINT32(n)
Definition: fmgr.h:270
#define PG_GETARG_FLOAT4(n)
Definition: fmgr.h:281
HeapTuple heap_modify_tuple_by_cols(HeapTuple tuple, TupleDesc tupleDesc, int nCols, const int *replCols, const Datum *replValues, const bool *replIsnull)
Definition: heaptuple.c:1278
void heap_freetuple(HeapTuple htup)
Definition: heaptuple.c:1435
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
static void * GETSTRUCT(const HeapTupleData *tuple)
Definition: htup_details.h:728
void CatalogTupleUpdate(Relation heapRel, ItemPointer otid, HeapTuple tup)
Definition: indexing.c:313
#define RowExclusiveLock
Definition: lockdefs.h:38
NameData relname
Definition: pg_class.h:38
FormData_pg_class * Form_pg_class
Definition: pg_class.h:156
uintptr_t Datum
Definition: postgres.h:69
static Datum ObjectIdGetDatum(Oid X)
Definition: postgres.h:257
unsigned int Oid
Definition: postgres_ext.h:30
#define RelationGetDescr(relation)
Definition: rel.h:542
void stats_check_required_arg(FunctionCallInfo fcinfo, struct StatsArgInfo *arginfo, int argnum)
Definition: stat_utils.c:37
void stats_lock_check_privileges(Oid reloid)
Definition: stat_utils.c:131
Oid stats_lookup_relid(const char *nspname, const char *relname)
Definition: stat_utils.c:221
ItemPointerData t_self
Definition: htup.h:65
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:269
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Definition: syscache.c:221
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:126
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition: table.c:40
void CommandCounterIncrement(void)
Definition: xact.c:1100
bool RecoveryInProgress(void)
Definition: xlog.c:6522

References CatalogTupleUpdate(), CommandCounterIncrement(), elog, ereport, errcode(), errhint(), errmsg(), ERROR, Float4GetDatum(), GETSTRUCT(), heap_freetuple(), heap_modify_tuple_by_cols(), HeapTupleIsValid, ObjectIdGetDatum(), PG_ARGISNULL, PG_GETARG_DATUM, PG_GETARG_FLOAT4, PG_GETARG_UINT32, RecoveryInProgress(), RELALLFROZEN_ARG, RELALLVISIBLE_ARG, relarginfo, RelationGetDescr, ReleaseSysCache(), relname, RELNAME_ARG, RELPAGES_ARG, RELSCHEMA_ARG, RELTUPLES_ARG, RowExclusiveLock, SearchSysCache1(), stats_check_required_arg(), stats_lock_check_privileges(), stats_lookup_relid(), HeapTupleData::t_self, table_close(), table_open(), TextDatumGetCString, UInt32GetDatum(), values, and WARNING.

Referenced by pg_clear_relation_stats(), and pg_restore_relation_stats().

Variable Documentation

◆ relarginfo

struct StatsArgInfo relarginfo[]
static
Initial value:
=
{
[RELSCHEMA_ARG] = {"schemaname", TEXTOID},
[RELNAME_ARG] = {"relname", TEXTOID},
[RELPAGES_ARG] = {"relpages", INT4OID},
[RELTUPLES_ARG] = {"reltuples", FLOAT4OID},
[RELALLVISIBLE_ARG] = {"relallvisible", INT4OID},
[RELALLFROZEN_ARG] = {"relallfrozen", INT4OID},
}

Definition at line 47 of file relation_stats.c.

Referenced by pg_restore_relation_stats(), and relation_statistics_update().