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