| 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-2025, 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 <math.h> |
| 21 |
|
| 22 |
#include "access/heapam.h" |
| 23 |
#include "catalog/indexing.h" |
| 24 |
#include "catalog/namespace.h" |
| 25 |
#include "nodes/makefuncs.h" |
| 26 |
#include "statistics/stat_utils.h" |
| 27 |
#include "utils/builtins.h" |
| 28 |
#include "utils/fmgroids.h" |
| 29 |
#include "utils/fmgrprotos.h" |
| 30 |
#include "utils/lsyscache.h" |
| 31 |
#include "utils/syscache.h" |
| 32 |
|
| 33 |
|
| 34 |
/* |
| 35 |
* Positional argument numbers, names, and types for |
| 36 |
* relation_statistics_update(). |
| 37 |
*/ |
| 38 |
|
| 39 |
enum relation_stats_argnum |
| 40 |
{ |
| 41 |
RELSCHEMA_ARG = 0, |
| 42 |
RELNAME_ARG, |
| 43 |
RELPAGES_ARG, |
| 44 |
RELTUPLES_ARG, |
| 45 |
RELALLVISIBLE_ARG, |
| 46 |
RELALLFROZEN_ARG, |
| 47 |
NUM_RELATION_STATS_ARGS |
| 48 |
}; |
| 49 |
|
| 50 |
static struct StatsArgInfo relarginfo[] = |
| 51 |
{ |
| 52 |
[RELSCHEMA_ARG] = {"schemaname", TEXTOID}, |
| 53 |
[RELNAME_ARG] = {"relname", TEXTOID}, |
| 54 |
[RELPAGES_ARG] = {"relpages", INT4OID}, |
| 55 |
[RELTUPLES_ARG] = {"reltuples", FLOAT4OID}, |
| 56 |
[RELALLVISIBLE_ARG] = {"relallvisible", INT4OID}, |
| 57 |
[RELALLFROZEN_ARG] = {"relallfrozen", INT4OID}, |
| 58 |
[NUM_RELATION_STATS_ARGS] = {0} |
| 59 |
}; |
| 60 |
|
| 61 |
static bool relation_statistics_update(FunctionCallInfo fcinfo); |
| 62 |
|
| 63 |
/* |
| 64 |
* Internal function for modifying statistics for a relation. |
| 65 |
*/ |
| 66 |
static bool |
| 67 |
relation_statistics_update(FunctionCallInfo fcinfo) |
| 68 |
{ |
| 69 |
bool result = true; |
| 70 |
char *nspname; |
| 71 |
char *relname; |
| 72 |
Oid reloid; |
| 73 |
Relation crel; |
| 74 |
BlockNumber relpages = 0; |
| 75 |
bool update_relpages = false; |
| 76 |
float reltuples = 0; |
| 77 |
bool update_reltuples = false; |
| 78 |
BlockNumber relallvisible = 0; |
| 79 |
bool update_relallvisible = false; |
| 80 |
BlockNumber relallfrozen = 0; |
| 81 |
bool update_relallfrozen = false; |
| 82 |
HeapTuple ctup; |
| 83 |
Form_pg_class pgcform; |
| 84 |
int replaces[4] = {0}; |
| 85 |
Datum values[4] = {0}; |
| 86 |
bool nulls[4] = {0}; |
| 87 |
int nreplaces = 0; |
| 88 |
Oid locked_table = InvalidOid; |
| 89 |
|
| 90 |
stats_check_required_arg(fcinfo, relarginfo, RELSCHEMA_ARG); |
| 91 |
stats_check_required_arg(fcinfo, relarginfo, RELNAME_ARG); |
| 92 |
|
| 93 |
nspname = TextDatumGetCString(PG_GETARG_DATUM(RELSCHEMA_ARG)); |
| 94 |
relname = TextDatumGetCString(PG_GETARG_DATUM(RELNAME_ARG)); |
| 95 |
|
| 96 |
if (RecoveryInProgress()) |
| 97 |
ereport(ERROR, |
| 98 |
(errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), |
| 99 |
errmsg("recovery is in progress"), |
| 100 |
errhint("Statistics cannot be modified during recovery."))); |
| 101 |
|
| 102 |
reloid = RangeVarGetRelidExtended(makeRangeVar(nspname, relname, -1), |
| 103 |
ShareUpdateExclusiveLock, 0, |
| 104 |
RangeVarCallbackForStats, &locked_table); |
| 105 |
|
| 106 |
if (!PG_ARGISNULL(RELPAGES_ARG)) |
| 107 |
{ |
| 108 |
relpages = PG_GETARG_UINT32(RELPAGES_ARG); |
| 109 |
update_relpages = true; |
| 110 |
} |
| 111 |
|
| 112 |
if (!PG_ARGISNULL(RELTUPLES_ARG)) |
| 113 |
{ |
| 114 |
reltuples = PG_GETARG_FLOAT4(RELTUPLES_ARG); |
| 115 |
if (isnan(reltuples) || isinf(reltuples)) |
| 116 |
{ |
| 117 |
ereport(WARNING, |
| 118 |
(errcode(ERRCODE_INVALID_PARAMETER_VALUE), |
| 119 |
errmsg("argument \"%s\" must be a finite value", "reltuples"))); |
| 120 |
result = false; |
| 121 |
} |
| 122 |
else if (reltuples < -1.0) |
| 123 |
{ |
| 124 |
ereport(WARNING, |
| 125 |
(errcode(ERRCODE_INVALID_PARAMETER_VALUE), |
| 126 |
errmsg("argument \"%s\" must not be less than -1.0", "reltuples"))); |
| 127 |
result = false; |
| 128 |
} |
| 129 |
else |
| 130 |
update_reltuples = true; |
| 131 |
} |
| 132 |
|
| 133 |
if (!PG_ARGISNULL(RELALLVISIBLE_ARG)) |
| 134 |
{ |
| 135 |
relallvisible = PG_GETARG_UINT32(RELALLVISIBLE_ARG); |
| 136 |
update_relallvisible = true; |
| 137 |
} |
| 138 |
|
| 139 |
if (!PG_ARGISNULL(RELALLFROZEN_ARG)) |
| 140 |
{ |
| 141 |
relallfrozen = PG_GETARG_UINT32(RELALLFROZEN_ARG); |
| 142 |
update_relallfrozen = true; |
| 143 |
} |
| 144 |
|
| 145 |
/* |
| 146 |
* Take RowExclusiveLock on pg_class, consistent with |
| 147 |
* vac_update_relstats(). |
| 148 |
*/ |
| 149 |
crel = table_open(RelationRelationId, RowExclusiveLock); |
| 150 |
|
| 151 |
ctup = SearchSysCache1(RELOID, ObjectIdGetDatum(reloid)); |
| 152 |
if (!HeapTupleIsValid(ctup)) |
| 153 |
elog(ERROR, "pg_class entry for relid %u not found", reloid); |
| 154 |
|
| 155 |
pgcform = (Form_pg_class) GETSTRUCT(ctup); |
| 156 |
|
| 157 |
if (update_relpages && relpages != pgcform->relpages) |
| 158 |
{ |
| 159 |
replaces[nreplaces] = Anum_pg_class_relpages; |
| 160 |
values[nreplaces] = UInt32GetDatum(relpages); |
| 161 |
nreplaces++; |
| 162 |
} |
| 163 |
|
| 164 |
if (update_reltuples && reltuples != pgcform->reltuples) |
| 165 |
{ |
| 166 |
replaces[nreplaces] = Anum_pg_class_reltuples; |
| 167 |
values[nreplaces] = Float4GetDatum(reltuples); |
| 168 |
nreplaces++; |
| 169 |
} |
| 170 |
|
| 171 |
if (update_relallvisible && relallvisible != pgcform->relallvisible) |
| 172 |
{ |
| 173 |
replaces[nreplaces] = Anum_pg_class_relallvisible; |
| 174 |
values[nreplaces] = UInt32GetDatum(relallvisible); |
| 175 |
nreplaces++; |
| 176 |
} |
| 177 |
|
| 178 |
if (update_relallfrozen && relallfrozen != pgcform->relallfrozen) |
| 179 |
{ |
| 180 |
replaces[nreplaces] = Anum_pg_class_relallfrozen; |
| 181 |
values[nreplaces] = UInt32GetDatum(relallfrozen); |
| 182 |
nreplaces++; |
| 183 |
} |
| 184 |
|
| 185 |
if (nreplaces > 0) |
| 186 |
{ |
| 187 |
TupleDesc tupdesc = RelationGetDescr(crel); |
| 188 |
HeapTuple newtup; |
| 189 |
|
| 190 |
newtup = heap_modify_tuple_by_cols(ctup, tupdesc, nreplaces, |
| 191 |
replaces, values, nulls); |
| 192 |
CatalogTupleUpdate(crel, &newtup->t_self, newtup); |
| 193 |
heap_freetuple(newtup); |
| 194 |
} |
| 195 |
|
| 196 |
ReleaseSysCache(ctup); |
| 197 |
|
| 198 |
/* release the lock, consistent with vac_update_relstats() */ |
| 199 |
table_close(crel, RowExclusiveLock); |
| 200 |
|
| 201 |
CommandCounterIncrement(); |
| 202 |
|
| 203 |
return result; |
| 204 |
} |
| 205 |
|
| 206 |
/* |
| 207 |
* Clear statistics for a given pg_class entry; that is, set back to initial |
| 208 |
* stats for a newly-created table. |
| 209 |
*/ |
| 210 |
Datum |
| 211 |
pg_clear_relation_stats(PG_FUNCTION_ARGS) |
| 212 |
{ |
| 213 |
LOCAL_FCINFO(newfcinfo, 6); |
| 214 |
|
| 215 |
InitFunctionCallInfoData(*newfcinfo, NULL, 6, InvalidOid, NULL, NULL); |
| 216 |
|
| 217 |
newfcinfo->args[0].value = PG_GETARG_DATUM(0); |
| 218 |
newfcinfo->args[0].isnull = PG_ARGISNULL(0); |
| 219 |
newfcinfo->args[1].value = PG_GETARG_DATUM(1); |
| 220 |
newfcinfo->args[1].isnull = PG_ARGISNULL(1); |
| 221 |
newfcinfo->args[2].value = UInt32GetDatum(0); |
| 222 |
newfcinfo->args[2].isnull = false; |
| 223 |
newfcinfo->args[3].value = Float4GetDatum(-1.0); |
| 224 |
newfcinfo->args[3].isnull = false; |
| 225 |
newfcinfo->args[4].value = UInt32GetDatum(0); |
| 226 |
newfcinfo->args[4].isnull = false; |
| 227 |
newfcinfo->args[5].value = UInt32GetDatum(0); |
| 228 |
newfcinfo->args[5].isnull = false; |
| 229 |
|
| 230 |
relation_statistics_update(newfcinfo); |
| 231 |
PG_RETURN_VOID(); |
| 232 |
} |
| 233 |
|
| 234 |
Datum |
| 235 |
pg_restore_relation_stats(PG_FUNCTION_ARGS) |
| 236 |
{ |
| 237 |
LOCAL_FCINFO(positional_fcinfo, NUM_RELATION_STATS_ARGS); |
| 238 |
bool result = true; |
| 239 |
|
| 240 |
InitFunctionCallInfoData(*positional_fcinfo, NULL, |
| 241 |
NUM_RELATION_STATS_ARGS, |
| 242 |
InvalidOid, NULL, NULL); |
| 243 |
|
| 244 |
if (!stats_fill_fcinfo_from_arg_pairs(fcinfo, positional_fcinfo, |
| 245 |
relarginfo)) |
| 246 |
result = false; |
| 247 |
|
| 248 |
if (!relation_statistics_update(positional_fcinfo)) |
| 249 |
result = false; |
| 250 |
|
| 251 |
PG_RETURN_BOOL(result); |
| 252 |
} |