PostgreSQL Source Code git master
Loading...
Searching...
No Matches
extended_stats_funcs.c
Go to the documentation of this file.
1/*-------------------------------------------------------------------------
2 *
3 * extended_stats_funcs.c
4 * Functions for manipulating extended statistics.
5 *
6 * This file includes the set of facilities required to support the direct
7 * manipulations of extended statistics objects.
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/extended_stats_funcs.c
14 *
15 *-------------------------------------------------------------------------
16 */
17#include "postgres.h"
18
19#include "access/heapam.h"
20#include "catalog/indexing.h"
21#include "catalog/namespace.h"
22#include "catalog/pg_database.h"
25#include "miscadmin.h"
26#include "nodes/makefuncs.h"
28#include "utils/acl.h"
29#include "utils/builtins.h"
30#include "utils/fmgroids.h"
31#include "utils/lsyscache.h"
32#include "utils/syscache.h"
33
34
35/*
36 * Index of the arguments for the SQL functions.
37 */
47
48/*
49 * The argument names and type OIDs of the arguments for the SQL
50 * functions.
51 */
52static struct StatsArgInfo extarginfo[] =
53{
54 [RELSCHEMA_ARG] = {"schemaname", TEXTOID},
55 [RELNAME_ARG] = {"relname", TEXTOID},
56 [STATSCHEMA_ARG] = {"statistics_schemaname", TEXTOID},
57 [STATNAME_ARG] = {"statistics_name", TEXTOID},
58 [INHERITED_ARG] = {"inherited", BOOLOID},
60};
61
63 const char *stxname);
65
66/*
67 * Fetch a pg_statistic_ext row by name and namespace OID.
68 */
69static HeapTuple
71{
72 ScanKeyData key[2];
73 SysScanDesc scan;
76
77 ScanKeyInit(&key[0],
82 ScanKeyInit(&key[1],
85 F_OIDEQ,
87
88 /*
89 * Try to find matching pg_statistic_ext row.
90 */
93 true,
94 NULL,
95 2,
96 key);
97
98 /* Lookup is based on a unique index, so we get either 0 or 1 tuple. */
99 tup = systable_getnext(scan);
100
103
104 systable_endscan(scan);
105
106 if (!OidIsValid(stxoid))
107 return NULL;
108
110}
111
112/*
113 * Remove an existing pg_statistic_ext_data row for a given pg_statistic_ext
114 * row and "inherited" pair.
115 */
116static bool
118{
121 bool result = false;
122
123 /* Is there already a pg_statistic tuple for this attribute? */
127
129 {
130 CatalogTupleDelete(sed, &oldtup->t_self);
132 result = true;
133 }
134
136
138
139 return result;
140}
141
142/*
143 * Delete statistics for the given statistics object.
144 */
145Datum
147{
148 char *relnspname;
149 char *relname;
150 char *nspname;
151 Oid nspoid;
152 Oid relid;
153 char *stxname;
154 bool inherited;
159
160 /* relation arguments */
165
166 /* extended statistics arguments */
173
174 if (RecoveryInProgress())
175 {
178 errmsg("recovery is in progress"),
179 errhint("Statistics cannot be modified during recovery."));
181 }
182
183 /*
184 * First open the relation where we expect to find the statistics. This
185 * is similar to relation and attribute statistics, so as ACL checks are
186 * done before any locks are taken, even before any attempts related to
187 * the extended stats object.
188 */
192
193 /* Now check if the namespace of the stats object exists. */
194 nspoid = get_namespace_oid(nspname, true);
195 if (nspoid == InvalidOid)
196 {
199 errmsg("could not find schema \"%s\"", nspname));
201 }
202
205
206 if (!HeapTupleIsValid(tup))
207 {
211 errmsg("could not find extended statistics object \"%s\".\"%s\"",
212 nspname, stxname));
214 }
215
217
218 /*
219 * This should be consistent, based on the lock taken on the table when we
220 * started.
221 */
222 if (stxform->stxrelid != relid)
223 {
227 errmsg("could not clear extended statistics object \"%s\".\"%s\": incorrect relation \"%s\".\"%s\" specified",
231 }
232
235
237
239}
#define TextDatumGetCString(d)
Definition builtins.h:98
#define OidIsValid(objectId)
Definition c.h:798
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 ereport(elevel,...)
Definition elog.h:150
extended_stats_argnum
@ INHERITED_ARG
@ RELSCHEMA_ARG
@ NUM_EXTENDED_STATS_ARGS
@ RELNAME_ARG
@ STATNAME_ARG
@ STATSCHEMA_ARG
Datum pg_clear_extended_stats(PG_FUNCTION_ARGS)
static bool delete_pg_statistic_ext_data(Oid stxoid, bool inherited)
static struct StatsArgInfo extarginfo[]
static HeapTuple get_pg_statistic_ext(Relation pg_stext, Oid nspoid, const char *stxname)
#define PG_RETURN_VOID()
Definition fmgr.h:350
#define PG_GETARG_DATUM(n)
Definition fmgr.h:268
#define PG_GETARG_BOOL(n)
Definition fmgr.h:274
#define PG_FUNCTION_ARGS
Definition fmgr.h:193
void systable_endscan(SysScanDesc sysscan)
Definition genam.c:603
HeapTuple systable_getnext(SysScanDesc sysscan)
Definition genam.c:514
SysScanDesc systable_beginscan(Relation heapRelation, Oid indexId, bool indexOK, Snapshot snapshot, int nkeys, ScanKey key)
Definition genam.c:388
void heap_freetuple(HeapTuple htup)
Definition heaptuple.c:1435
#define HeapTupleIsValid(tuple)
Definition htup.h:78
static void * GETSTRUCT(const HeapTupleData *tuple)
void CatalogTupleDelete(Relation heapRel, const ItemPointerData *tid)
Definition indexing.c:365
#define ShareUpdateExclusiveLock
Definition lockdefs.h:39
#define RowExclusiveLock
Definition lockdefs.h:38
char * get_namespace_name(Oid nspid)
Definition lsyscache.c:3516
RangeVar * makeRangeVar(char *schemaname, char *relname, int location)
Definition makefuncs.c:473
Oid get_namespace_oid(const char *nspname, bool missing_ok)
Definition namespace.c:3605
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_statistic_ext * Form_pg_statistic_ext
static Datum BoolGetDatum(bool X)
Definition postgres.h:112
static Datum ObjectIdGetDatum(Oid X)
Definition postgres.h:262
uint64_t Datum
Definition postgres.h:70
static Datum CStringGetDatum(const char *X)
Definition postgres.h:380
#define InvalidOid
unsigned int Oid
static int fb(int x)
void ScanKeyInit(ScanKey entry, AttrNumber attributeNumber, StrategyNumber strategy, RegProcedure procedure, Datum argument)
Definition scankey.c:76
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
#define BTEqualStrategyNumber
Definition stratnum.h:31
void ReleaseSysCache(HeapTuple tuple)
Definition syscache.c:264
HeapTuple SearchSysCache2(int cacheId, Datum key1, Datum key2)
Definition syscache.c:230
#define SearchSysCacheCopy1(cacheId, key1)
Definition syscache.h:91
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