PostgreSQL Source Code git master
Loading...
Searching...
No Matches
relation_stats.c
Go to the documentation of this file.
1/*-------------------------------------------------------------------------
2 * relation_stats.c
3 *
4 * PostgreSQL relation statistics manipulation
5 *
6 * Code supporting the direct import of relation statistics, similar to
7 * what is done by the ANALYZE command.
8 *
9 * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group
10 * Portions Copyright (c) 1994, Regents of the University of California
11 *
12 * IDENTIFICATION
13 * src/backend/statistics/relation_stats.c
14 *
15 *-------------------------------------------------------------------------
16 */
17
18#include "postgres.h"
19
20#include "access/heapam.h"
21#include "catalog/indexing.h"
22#include "catalog/namespace.h"
23#include "nodes/makefuncs.h"
25#include "utils/builtins.h"
26#include "utils/fmgroids.h"
27#include "utils/fmgrprotos.h"
28#include "utils/lsyscache.h"
29#include "utils/syscache.h"
30
31
32/*
33 * Positional argument numbers, names, and types for
34 * relation_statistics_update().
35 */
36
47
48static struct StatsArgInfo relarginfo[] =
49{
50 [RELSCHEMA_ARG] = {"schemaname", TEXTOID},
51 [RELNAME_ARG] = {"relname", TEXTOID},
52 [RELPAGES_ARG] = {"relpages", INT4OID},
53 [RELTUPLES_ARG] = {"reltuples", FLOAT4OID},
54 [RELALLVISIBLE_ARG] = {"relallvisible", INT4OID},
55 [RELALLFROZEN_ARG] = {"relallfrozen", INT4OID},
57};
58
60
61/*
62 * Internal function for modifying statistics for a relation.
63 */
64static bool
66{
67 bool result = true;
68 char *nspname;
69 char *relname;
70 Oid reloid;
72 BlockNumber relpages = 0;
73 bool update_relpages = false;
74 float reltuples = 0;
75 bool update_reltuples = false;
76 BlockNumber relallvisible = 0;
77 bool update_relallvisible = false;
78 BlockNumber relallfrozen = 0;
79 bool update_relallfrozen = false;
82 int replaces[4] = {0};
83 Datum values[4] = {0};
84 bool nulls[4] = {0};
85 int nreplaces = 0;
87
90
93
97 errmsg("recovery is in progress"),
98 errhint("Statistics cannot be modified during recovery.")));
99
100 reloid = RangeVarGetRelidExtended(makeRangeVar(nspname, relname, -1),
103
105 {
106 relpages = PG_GETARG_UINT32(RELPAGES_ARG);
107 update_relpages = true;
108 }
109
111 {
112 reltuples = PG_GETARG_FLOAT4(RELTUPLES_ARG);
113 if (reltuples < -1.0)
114 {
117 errmsg("argument \"%s\" must not be less than -1.0", "reltuples")));
118 result = false;
119 }
120 else
121 update_reltuples = true;
122 }
123
125 {
126 relallvisible = PG_GETARG_UINT32(RELALLVISIBLE_ARG);
128 }
129
131 {
132 relallfrozen = PG_GETARG_UINT32(RELALLFROZEN_ARG);
133 update_relallfrozen = true;
134 }
135
136 /*
137 * Take RowExclusiveLock on pg_class, consistent with
138 * vac_update_relstats().
139 */
141
144 elog(ERROR, "pg_class entry for relid %u not found", reloid);
145
147
148 if (update_relpages && relpages != pgcform->relpages)
149 {
151 values[nreplaces] = UInt32GetDatum(relpages);
152 nreplaces++;
153 }
154
155 if (update_reltuples && reltuples != pgcform->reltuples)
156 {
158 values[nreplaces] = Float4GetDatum(reltuples);
159 nreplaces++;
160 }
161
162 if (update_relallvisible && relallvisible != pgcform->relallvisible)
163 {
165 values[nreplaces] = UInt32GetDatum(relallvisible);
166 nreplaces++;
167 }
168
169 if (update_relallfrozen && relallfrozen != pgcform->relallfrozen)
170 {
172 values[nreplaces] = UInt32GetDatum(relallfrozen);
173 nreplaces++;
174 }
175
176 if (nreplaces > 0)
177 {
180
182 replaces, values, nulls);
185 }
186
188
189 /* release the lock, consistent with vac_update_relstats() */
191
193
194 return result;
195}
196
197/*
198 * Clear statistics for a given pg_class entry; that is, set back to initial
199 * stats for a newly-created table.
200 */
201Datum
203{
205
207
208 newfcinfo->args[0].value = PG_GETARG_DATUM(0);
209 newfcinfo->args[0].isnull = PG_ARGISNULL(0);
210 newfcinfo->args[1].value = PG_GETARG_DATUM(1);
211 newfcinfo->args[1].isnull = PG_ARGISNULL(1);
212 newfcinfo->args[2].value = UInt32GetDatum(0);
213 newfcinfo->args[2].isnull = false;
214 newfcinfo->args[3].value = Float4GetDatum(-1.0);
215 newfcinfo->args[3].isnull = false;
216 newfcinfo->args[4].value = UInt32GetDatum(0);
217 newfcinfo->args[4].isnull = false;
218 newfcinfo->args[5].value = UInt32GetDatum(0);
219 newfcinfo->args[5].isnull = false;
220
223}
224
225Datum
uint32 BlockNumber
Definition block.h:31
static Datum values[MAXATTR]
Definition bootstrap.c:155
#define TextDatumGetCString(d)
Definition builtins.h:98
int errhint(const char *fmt,...)
Definition elog.c:1330
int errcode(int sqlerrcode)
Definition elog.c:863
int errmsg(const char *fmt,...)
Definition elog.c:1080
#define WARNING
Definition elog.h:36
#define ERROR
Definition elog.h:39
#define elog(elevel,...)
Definition elog.h:226
#define ereport(elevel,...)
Definition elog.h:150
#define PG_RETURN_VOID()
Definition fmgr.h:350
#define PG_GETARG_UINT32(n)
Definition fmgr.h:270
#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
#define PG_GETARG_FLOAT4(n)
Definition fmgr.h:282
#define PG_FUNCTION_ARGS
Definition fmgr.h:193
#define PG_RETURN_BOOL(x)
Definition fmgr.h:360
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)
void CatalogTupleUpdate(Relation heapRel, const ItemPointerData *otid, HeapTuple tup)
Definition indexing.c:313
#define ShareUpdateExclusiveLock
Definition lockdefs.h:39
#define RowExclusiveLock
Definition lockdefs.h:38
RangeVar * makeRangeVar(char *schemaname, char *relname, int location)
Definition makefuncs.c:473
Oid RangeVarGetRelidExtended(const RangeVar *relation, LOCKMODE lockmode, uint32 flags, RangeVarGetRelidCallback callback, void *callback_arg)
Definition namespace.c:440
NameData relname
Definition pg_class.h:38
FormData_pg_class * Form_pg_class
Definition pg_class.h:156
static Datum Float4GetDatum(float4 X)
Definition postgres.h:478
static Datum ObjectIdGetDatum(Oid X)
Definition postgres.h:262
uint64_t Datum
Definition postgres.h:70
static Datum UInt32GetDatum(uint32 X)
Definition postgres.h:242
#define InvalidOid
unsigned int Oid
static int fb(int x)
#define RelationGetDescr(relation)
Definition rel.h:540
static struct StatsArgInfo relarginfo[]
relation_stats_argnum
@ RELALLVISIBLE_ARG
@ RELSCHEMA_ARG
@ RELNAME_ARG
@ RELPAGES_ARG
@ RELALLFROZEN_ARG
@ RELTUPLES_ARG
@ NUM_RELATION_STATS_ARGS
Datum pg_restore_relation_stats(PG_FUNCTION_ARGS)
static bool relation_statistics_update(FunctionCallInfo fcinfo)
Datum pg_clear_relation_stats(PG_FUNCTION_ARGS)
bool stats_fill_fcinfo_from_arg_pairs(FunctionCallInfo pairs_fcinfo, FunctionCallInfo positional_fcinfo, struct StatsArgInfo *arginfo)
Definition stat_utils.c:348
void RangeVarCallbackForStats(const RangeVar *relation, Oid relId, Oid oldRelId, void *arg)
Definition stat_utils.c:142
void stats_check_required_arg(FunctionCallInfo fcinfo, struct StatsArgInfo *arginfo, int argnum)
Definition stat_utils.c:51
void ReleaseSysCache(HeapTuple tuple)
Definition syscache.c:264
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Definition syscache.c:220
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:1101
bool RecoveryInProgress(void)
Definition xlog.c:6461