-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathstats.c
More file actions
551 lines (473 loc) · 13.8 KB
/
Copy pathstats.c
File metadata and controls
551 lines (473 loc) · 13.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
/*
* This file and its contents are licensed under the Apache License 2.0.
* Please see the included NOTICE for copyright information and
* LICENSE-APACHE for a copy of the license.
*/
#include <postgres.h>
#include <access/genam.h>
#include <access/htup_details.h>
#include <access/table.h>
#include <access/tableam.h>
#include <catalog/indexing.h>
#include <catalog/namespace.h>
#include <catalog/pg_class.h>
#include <catalog/pg_namespace.h>
#include <fmgr.h>
#include <storage/lmgr.h>
#include <utils/builtins.h>
#include <utils/snapmgr.h>
#include <utils/syscache.h>
#include "chunk.h"
#include "debug_point.h"
#include "extension.h"
#include "hypertable_cache.h"
#include "stats.h"
#include "ts_catalog/catalog.h"
#include "ts_catalog/compression_settings.h"
#include "ts_catalog/continuous_agg.h"
#include "utils.h"
typedef struct StatsContext
{
TelemetryStats *stats;
Snapshot snapshot;
} StatsContext;
static StatsRelType
classify_table(const Form_pg_class class, Cache *htcache, const Hypertable **ht,
const Chunk **chunk)
{
Assert(class->relkind == RELKIND_RELATION);
if (class->relispartition)
{
return RELTYPE_PARTITION;
}
/* Check if it is a hypertable */
*ht = ts_hypertable_cache_get_entry(htcache, class->oid, CACHE_FLAG_MISSING_OK);
if (*ht)
{
return RELTYPE_HYPERTABLE;
}
/* Check if it is a chunk */
*chunk = ts_chunk_get_by_relid(class->oid, false);
if (*chunk)
{
return RELTYPE_CHUNK;
}
/*
* The relation holding the compressed data of a chunk is not a chunk
* catalog entry on its own, so classify it directly here. Its stats are
* tracked separately via the compressed chunk size metadata.
*/
if (ts_relation_is_compressed_chunk_relation(class->oid))
{
return RELTYPE_COMPRESSION_CHUNK;
}
return RELTYPE_TABLE;
}
static StatsRelType
classify_partitioned_table(const Form_pg_class class)
{
Assert(class->relkind == RELKIND_PARTITIONED_TABLE);
/*
* If the partitioned table itself is a partition, then it is a partition
* in a multi-dimensional partitioned table. Treat it as a partition so
* that only "root" tables are counted as partitioned tables.
*/
if (class->relispartition)
{
return RELTYPE_PARTITION;
}
return RELTYPE_PARTITIONED_TABLE;
}
static StatsRelType
classify_foreign_table(Oid relid, const Chunk **chunk)
{
*chunk = ts_chunk_get_by_relid(relid, false);
if (*chunk)
{
return RELTYPE_CHUNK;
}
/*
* Currently don't care about non-chunk foreign tables, so classify as
* "other".
*/
return RELTYPE_OTHER;
}
static StatsRelType
classify_view(const Form_pg_class class, const ContinuousAgg **cagg)
{
const Catalog *catalog = ts_catalog_get();
if (class->relnamespace == catalog->extension_schema_id[TS_INTERNAL_SCHEMA])
{
return RELTYPE_OTHER;
}
*cagg = ts_continuous_agg_find_by_relid(class->oid);
if (*cagg)
{
return RELTYPE_CONTINUOUS_AGG;
}
return RELTYPE_VIEW;
}
static StatsRelType
classify_relation(const Form_pg_class class, Cache *htcache, const Hypertable **ht,
const Chunk **chunk, const ContinuousAgg **cagg)
{
*chunk = NULL;
*ht = NULL;
*cagg = NULL;
switch (class->relkind)
{
case RELKIND_RELATION:
return classify_table(class, htcache, ht, chunk);
case RELKIND_PARTITIONED_TABLE:
return classify_partitioned_table(class);
case RELKIND_FOREIGN_TABLE:
return classify_foreign_table(class->oid, chunk);
case RELKIND_MATVIEW:
return RELTYPE_MATVIEW;
case RELKIND_VIEW:
return classify_view(class, cagg);
default:
return RELTYPE_OTHER;
}
}
static void
add_storage(StorageStats *stats, Form_pg_class class)
{
RelationSize relsize;
relsize = ts_relation_size_impl(class->oid);
stats->relsize.total_size += relsize.total_size;
stats->relsize.heap_size += relsize.heap_size;
stats->relsize.toast_size += relsize.toast_size;
stats->relsize.index_size += relsize.index_size;
}
static void
process_relation(BaseStats *stats, Form_pg_class class)
{
stats->relcount++;
/*
* As of PG14, pg_class.reltuples is set to -1 when the row count is
* unknown. Make sure we only add the count when the information is
* available.
*/
if (class->reltuples > 0)
{
stats->reltuples += class->reltuples;
}
if (RELKIND_HAS_STORAGE(class->relkind))
{
add_storage((StorageStats *) stats, class);
}
}
static void
process_hypertable(HyperStats *hyp, Form_pg_class class, const Hypertable *ht)
{
process_relation(&hyp->storage.base, class);
if (TS_HYPERTABLE_HAS_COMPRESSION_ENABLED(ht))
{
hyp->compressed_hypertable_count++;
}
}
static void
process_continuous_agg(CaggStats *cs, Form_pg_class class, const ContinuousAgg *cagg)
{
const Hypertable *mat_ht = ts_hypertable_get_by_id(cagg->data.mat_hypertable_id);
Assert(cagg);
process_relation(&cs->hyp.storage.base, class);
if (TS_HYPERTABLE_HAS_COMPRESSION_ENABLED(mat_ht))
{
cs->hyp.compressed_hypertable_count++;
}
if (!cagg->data.materialized_only)
{
cs->uses_real_time_aggregation_count++;
}
cs->finalized++;
if (cagg->data.parent_mat_hypertable_id != INVALID_HYPERTABLE_ID)
{
cs->nested++;
}
}
static void
process_partition(HyperStats *stats, Form_pg_class class, bool ischunk)
{
stats->child_count++;
/*
* Note that reltuples should be correct even for compressed chunks, since
* we "freeze" those stats when a chunk is compressed, and for foreign
* table chunks, since we import those stats from data nodes.
*
* Also, as of PG14, the parent tables include the cumulative stats for
* all children, so no need to count the partitions separately since the
* sum will be in the root.
*/
if (ischunk && class->reltuples > 0)
{
stats->storage.base.reltuples += class->reltuples;
}
add_storage(&stats->storage, class);
}
/*
* Add a chunk's stats to the parent table.
*/
static void
add_chunk_stats(HyperStats *stats, Form_pg_class class, const Chunk *chunk,
const Form_compression_chunk_size fd_compr)
{
process_partition(stats, class, true);
if (ts_chunk_is_compressed(chunk))
{
stats->compressed_chunk_count++;
}
if (fd_compr)
{
stats->compressed_heap_size += fd_compr->compressed_heap_size;
stats->compressed_indexes_size += fd_compr->compressed_index_size;
stats->compressed_toast_size += fd_compr->compressed_toast_size;
stats->uncompressed_heap_size += fd_compr->uncompressed_heap_size;
stats->uncompressed_indexes_size += fd_compr->uncompressed_index_size;
stats->uncompressed_toast_size += fd_compr->uncompressed_toast_size;
stats->uncompressed_row_count += fd_compr->numrows_pre_compression;
stats->compressed_row_count += fd_compr->numrows_post_compression;
stats->compressed_row_frozen_immediately_count += fd_compr->numrows_frozen_immediately;
/* Also add compressed sizes to total number for entire table */
stats->storage.relsize.heap_size += fd_compr->compressed_heap_size;
stats->storage.relsize.toast_size += fd_compr->compressed_toast_size;
stats->storage.relsize.index_size += fd_compr->compressed_index_size;
}
}
static bool
get_chunk_compression_stats(StatsContext *statsctx, const Chunk *chunk,
Form_compression_chunk_size compr_stats)
{
TupleInfo *ti;
ScanIterator it;
bool found = false;
if (!ts_chunk_is_compressed(chunk))
{
return false;
}
it = ts_scan_iterator_create(COMPRESSION_CHUNK_SIZE, AccessShareLock, CurrentMemoryContext);
ts_scan_iterator_set_index(&it, COMPRESSION_CHUNK_SIZE, COMPRESSION_CHUNK_SIZE_PKEY);
it.ctx.snapshot = statsctx->snapshot;
ts_scan_iterator_scan_key_reset(&it);
ts_scan_iterator_scan_key_init(&it,
Anum_compression_chunk_size_pkey_chunk_id,
BTEqualStrategyNumber,
F_INT4EQ,
Int32GetDatum(chunk->fd.id));
ts_scan_iterator_start_or_restart_scan(&it);
ti = ts_scan_iterator_next(&it);
if (ti)
{
Form_compression_chunk_size fd;
bool should_free;
HeapTuple tuple = ts_scan_iterator_fetch_heap_tuple(&it, false, &should_free);
fd = (Form_compression_chunk_size) GETSTRUCT(tuple);
memcpy(compr_stats, fd, sizeof(*fd));
if (should_free)
{
heap_freetuple(tuple);
}
found = true;
}
ts_scan_iterator_close(&it);
return found;
}
/*
* Process a relation identified as being a chunk.
*
* The chunk could be part of a
*
* - Hypertable
* - Distributed hypertable
* - Distributed hypertable member
* - Materialized hypertable (cagg) chunk
* - Internal compression table for hypertable
* - Internal compression table for materialized hypertable (cagg)
*
* Note that we want to count regular chunks and compressed chunks as part of
* the same hypertable, although they are children of different tables
* internally. The same applies to chunks that belong to a continuous
* aggregate, although in that case there is actually a two-level indirection:
* The main cagg view is the user-facing relation we'd like to collect stats
* for, while its chunks are actually stored in a materialized hypertable,
* and, in a second tier, in a compressed hypertable.
*/
static void
process_chunk(StatsContext *statsctx, StatsRelType chunk_reltype, Form_pg_class class,
const Chunk *chunk)
{
TelemetryStats *stats = statsctx->stats;
FormData_compression_chunk_size comp_stats_data;
Form_compression_chunk_size compr_stats = NULL;
Assert(chunk);
/*
* Ignore compression chunks since we have a separate metadata table with
* stats for them
*/
if (chunk_reltype == RELTYPE_COMPRESSION_CHUNK)
{
return;
}
if (get_chunk_compression_stats(statsctx, chunk, &comp_stats_data))
{
compr_stats = &comp_stats_data;
}
switch (chunk_reltype)
{
case RELTYPE_CHUNK:
add_chunk_stats(&stats->hypertables, class, chunk, compr_stats);
break;
case RELTYPE_MATERIALIZED_CHUNK:
add_chunk_stats(&stats->continuous_aggs.hyp, class, chunk, compr_stats);
break;
default:
pg_unreachable();
break;
}
}
static bool
is_pg_schema(Oid namespaceid)
{
static Oid information_schema_oid = InvalidOid;
if (namespaceid == PG_CATALOG_NAMESPACE || namespaceid == PG_TOAST_NAMESPACE)
{
return true;
}
if (!OidIsValid(information_schema_oid))
{
information_schema_oid = get_namespace_oid("information_schema", false);
}
return namespaceid == information_schema_oid;
}
static bool
is_ts_schema(const Catalog *catalog, Oid namespaceid)
{
int i;
for (i = 0; i < _TS_MAX_SCHEMA; i++)
{
if (namespaceid != catalog->extension_schema_id[TS_INTERNAL_SCHEMA] &&
namespaceid == catalog->extension_schema_id[i])
{
return true;
}
}
return false;
}
static bool
should_ignore_relation(const Catalog *catalog, Form_pg_class class)
{
return (is_pg_schema(class->relnamespace) || isAnyTempNamespace(class->relnamespace) ||
is_ts_schema(catalog, class->relnamespace) || ts_is_catalog_table(class->oid));
}
/*
* Scan the entire pg_class catalog table for all relations. For each
* relation, classify it and gather stats based on the classification.
*/
void
ts_telemetry_stats_gather(TelemetryStats *stats)
{
const Catalog *catalog = ts_catalog_get();
Relation rel;
SysScanDesc scan;
Cache *htcache = ts_hypertable_cache_pin();
MemoryContext oldmcxt, relmcxt;
StatsContext statsctx = {
.stats = stats,
.snapshot = GetActiveSnapshot(),
};
MemSet(stats, 0, sizeof(*stats));
rel = table_open(RelationRelationId, AccessShareLock);
scan = systable_beginscan(rel, ClassOidIndexId, false, NULL, 0, NULL);
relmcxt = AllocSetContextCreate(CurrentMemoryContext, "RelationStats", ALLOCSET_DEFAULT_SIZES);
while (true)
{
HeapTuple tup;
Form_pg_class class;
StatsRelType reltype;
const Chunk *chunk = NULL;
const Hypertable *ht = NULL;
const ContinuousAgg *cagg = NULL;
tup = systable_getnext(scan);
if (!HeapTupleIsValid(tup))
{
break;
}
class = (Form_pg_class) GETSTRUCT(tup);
if (should_ignore_relation(catalog, class))
{
continue;
}
/* Lock the relation to ensure it does not disappear while we process
* it */
LockRelationOid(class->oid, AccessShareLock);
/* Now that the lock is acquired, ensure the relation still
* exists. Otherwise, ignore the relation and release the useless
* lock. */
if (!SearchSysCacheExists1(RELOID, ObjectIdGetDatum(class->oid)))
{
UnlockRelationOid(class->oid, AccessShareLock);
continue;
}
/*
* Use temporary per-relation memory context to not accumulate cruft
* during processing of pg_class.
*/
oldmcxt = MemoryContextSwitchTo(relmcxt);
MemoryContextReset(relmcxt);
reltype = classify_relation(class, htcache, &ht, &chunk, &cagg);
DEBUG_WAITPOINT("telemetry_classify_relation");
switch (reltype)
{
case RELTYPE_HYPERTABLE:
Assert(NULL != ht);
process_hypertable(&stats->hypertables, class, ht);
break;
case RELTYPE_TABLE:
process_relation(&stats->tables.base, class);
break;
case RELTYPE_PARTITIONED_TABLE:
process_relation(&stats->partitioned_tables.storage.base, class);
break;
case RELTYPE_COMPRESSION_CHUNK:
/*
* Ignore the compressed data relation. Its stats are tracked
* separately via the compressed chunk size metadata, which is
* accounted for when processing the owning chunk.
*/
break;
case RELTYPE_CHUNK:
case RELTYPE_MATERIALIZED_CHUNK:
Assert(NULL != chunk);
process_chunk(&statsctx, reltype, class, chunk);
break;
case RELTYPE_PARTITION:
process_partition(&stats->partitioned_tables, class, false);
break;
case RELTYPE_VIEW:
/* Filter internal cagg views */
if (class->relnamespace != catalog->extension_schema_id[TS_INTERNAL_SCHEMA])
{
process_relation(&stats->views, class);
}
break;
case RELTYPE_MATVIEW:
process_relation(&stats->materialized_views.base, class);
break;
case RELTYPE_CONTINUOUS_AGG:
Assert(NULL != cagg);
process_continuous_agg(&stats->continuous_aggs, class, cagg);
break;
/* No stats collected for types below */
case RELTYPE_MATERIALIZED_HYPERTABLE:
case RELTYPE_OTHER:
break;
}
UnlockRelationOid(class->oid, AccessShareLock);
MemoryContextSwitchTo(oldmcxt);
}
systable_endscan(scan);
table_close(rel, AccessShareLock);
ts_cache_release(&htcache);
MemoryContextDelete(relmcxt);
}