PostgreSQL Source Code  git master
pgstat_relation.c
Go to the documentation of this file.
1 /* -------------------------------------------------------------------------
2  *
3  * pgstat_relation.c
4  * Implementation of relation statistics.
5  *
6  * This file contains the implementation of function relation. It is kept
7  * separate from pgstat.c to enforce the line between the statistics access /
8  * storage implementation and the details about individual types of
9  * statistics.
10  *
11  * Copyright (c) 2001-2023, PostgreSQL Global Development Group
12  *
13  * IDENTIFICATION
14  * src/backend/utils/activity/pgstat_relation.c
15  * -------------------------------------------------------------------------
16  */
17 
18 #include "postgres.h"
19 
20 #include "access/twophase_rmgr.h"
21 #include "access/xact.h"
22 #include "catalog/partition.h"
23 #include "postmaster/autovacuum.h"
24 #include "utils/memutils.h"
25 #include "utils/pgstat_internal.h"
26 #include "utils/rel.h"
27 #include "utils/timestamp.h"
28 #include "catalog/catalog.h"
29 
30 
31 /* Record that's written to 2PC state file when pgstat state is persisted */
32 typedef struct TwoPhasePgStatRecord
33 {
34  PgStat_Counter tuples_inserted; /* tuples inserted in xact */
35  PgStat_Counter tuples_updated; /* tuples updated in xact */
36  PgStat_Counter tuples_deleted; /* tuples deleted in xact */
37  /* tuples i/u/d prior to truncate/drop */
41  Oid t_id; /* table's OID */
42  bool t_shared; /* is it a shared catalog? */
43  bool t_truncdropped; /* was the relation truncated/dropped? */
45 
46 
47 static PgStat_TableStatus *pgstat_prep_relation_pending(Oid rel_id, bool isshared);
48 static void add_tabstat_xact_level(PgStat_TableStatus *pgstat_info, int nest_level);
49 static void ensure_tabstat_xact_level(PgStat_TableStatus *pgstat_info);
50 static void save_truncdrop_counters(PgStat_TableXactStatus *trans, bool is_drop);
52 
53 
54 /*
55  * Copy stats between relations. This is used for things like REINDEX
56  * CONCURRENTLY.
57  */
58 void
60 {
61  PgStat_StatTabEntry *srcstats;
62  PgStatShared_Relation *dstshstats;
63  PgStat_EntryRef *dst_ref;
64 
65  srcstats = pgstat_fetch_stat_tabentry_ext(src->rd_rel->relisshared,
66  RelationGetRelid(src));
67  if (!srcstats)
68  return;
69 
71  dst->rd_rel->relisshared ? InvalidOid : MyDatabaseId,
72  RelationGetRelid(dst),
73  false);
74 
75  dstshstats = (PgStatShared_Relation *) dst_ref->shared_stats;
76  dstshstats->stats = *srcstats;
77 
78  pgstat_unlock_entry(dst_ref);
79 }
80 
81 /*
82  * Initialize a relcache entry to count access statistics. Called whenever a
83  * relation is opened.
84  *
85  * We assume that a relcache entry's pgstat_info field is zeroed by relcache.c
86  * when the relcache entry is made; thereafter it is long-lived data.
87  *
88  * This does not create a reference to a stats entry in shared memory, nor
89  * allocate memory for the pending stats. That happens in
90  * pgstat_assoc_relation().
91  */
92 void
94 {
95  char relkind = rel->rd_rel->relkind;
96 
97  /*
98  * We only count stats for relations with storage and partitioned tables
99  */
100  if (!RELKIND_HAS_STORAGE(relkind) && relkind != RELKIND_PARTITIONED_TABLE)
101  {
102  rel->pgstat_enabled = false;
103  rel->pgstat_info = NULL;
104  return;
105  }
106 
107  if (!pgstat_track_counts)
108  {
109  if (rel->pgstat_info)
111 
112  /* We're not counting at all */
113  rel->pgstat_enabled = false;
114  rel->pgstat_info = NULL;
115  return;
116  }
117 
118  rel->pgstat_enabled = true;
119 }
120 
121 /*
122  * Prepare for statistics for this relation to be collected.
123  *
124  * This ensures we have a reference to the stats entry before stats can be
125  * generated. That is important because a relation drop in another connection
126  * could otherwise lead to the stats entry being dropped, which then later
127  * would get recreated when flushing stats.
128  *
129  * This is separate from pgstat_init_relation() as it is not uncommon for
130  * relcache entries to be opened without ever getting stats reported.
131  */
132 void
134 {
135  Assert(rel->pgstat_enabled);
136  Assert(rel->pgstat_info == NULL);
137 
138  /* Else find or make the PgStat_TableStatus entry, and update link */
140  rel->rd_rel->relisshared);
141 
142  /* don't allow link a stats to multiple relcache entries */
143  Assert(rel->pgstat_info->relation == NULL);
144 
145  /* mark this relation as the owner */
146  rel->pgstat_info->relation = rel;
147 }
148 
149 /*
150  * Break the mutual link between a relcache entry and pending stats entry.
151  * This must be called whenever one end of the link is removed.
152  */
153 void
155 {
156  /* remove the link to stats info if any */
157  if (rel->pgstat_info == NULL)
158  return;
159 
160  /* link sanity check */
161  Assert(rel->pgstat_info->relation == rel);
162  rel->pgstat_info->relation = NULL;
163  rel->pgstat_info = NULL;
164 }
165 
166 /*
167  * Ensure that stats are dropped if transaction aborts.
168  */
169 void
171 {
173  rel->rd_rel->relisshared ? InvalidOid : MyDatabaseId,
174  RelationGetRelid(rel));
175 }
176 
177 /*
178  * Ensure that stats are dropped if transaction commits.
179  */
180 void
182 {
183  int nest_level = GetCurrentTransactionNestLevel();
184  PgStat_TableStatus *pgstat_info;
185 
187  rel->rd_rel->relisshared ? InvalidOid : MyDatabaseId,
188  RelationGetRelid(rel));
189 
191  return;
192 
193  /*
194  * Transactionally set counters to 0. That ensures that accesses to
195  * pg_stat_xact_all_tables inside the transaction show 0.
196  */
197  pgstat_info = rel->pgstat_info;
198  if (pgstat_info->trans &&
199  pgstat_info->trans->nest_level == nest_level)
200  {
201  save_truncdrop_counters(pgstat_info->trans, true);
202  pgstat_info->trans->tuples_inserted = 0;
203  pgstat_info->trans->tuples_updated = 0;
204  pgstat_info->trans->tuples_deleted = 0;
205  }
206 }
207 
208 /*
209  * Report that the table was just vacuumed.
210  */
211 void
212 pgstat_report_vacuum(Oid tableoid, bool shared,
213  PgStat_Counter livetuples, PgStat_Counter deadtuples)
214 {
215  PgStat_EntryRef *entry_ref;
216  PgStatShared_Relation *shtabentry;
217  PgStat_StatTabEntry *tabentry;
218  Oid dboid = (shared ? InvalidOid : MyDatabaseId);
219  TimestampTz ts;
220 
221  if (!pgstat_track_counts)
222  return;
223 
224  /* Store the data in the table's hash table entry. */
225  ts = GetCurrentTimestamp();
226 
227  /* block acquiring lock for the same reason as pgstat_report_autovac() */
229  dboid, tableoid, false);
230 
231  shtabentry = (PgStatShared_Relation *) entry_ref->shared_stats;
232  tabentry = &shtabentry->stats;
233 
234  tabentry->live_tuples = livetuples;
235  tabentry->dead_tuples = deadtuples;
236 
237  /*
238  * It is quite possible that a non-aggressive VACUUM ended up skipping
239  * various pages, however, we'll zero the insert counter here regardless.
240  * It's currently used only to track when we need to perform an "insert"
241  * autovacuum, which are mainly intended to freeze newly inserted tuples.
242  * Zeroing this may just mean we'll not try to vacuum the table again
243  * until enough tuples have been inserted to trigger another insert
244  * autovacuum. An anti-wraparound autovacuum will catch any persistent
245  * stragglers.
246  */
247  tabentry->ins_since_vacuum = 0;
248 
250  {
251  tabentry->last_autovacuum_time = ts;
252  tabentry->autovacuum_count++;
253  }
254  else
255  {
256  tabentry->last_vacuum_time = ts;
257  tabentry->vacuum_count++;
258  }
259 
260  pgstat_unlock_entry(entry_ref);
261 }
262 
263 /*
264  * Report that the table was just analyzed.
265  *
266  * Caller must provide new live- and dead-tuples estimates, as well as a
267  * flag indicating whether to reset the mod_since_analyze counter.
268  */
269 void
271  PgStat_Counter livetuples, PgStat_Counter deadtuples,
272  bool resetcounter)
273 {
274  PgStat_EntryRef *entry_ref;
275  PgStatShared_Relation *shtabentry;
276  PgStat_StatTabEntry *tabentry;
277  Oid dboid = (rel->rd_rel->relisshared ? InvalidOid : MyDatabaseId);
278 
279  if (!pgstat_track_counts)
280  return;
281 
282  /*
283  * Unlike VACUUM, ANALYZE might be running inside a transaction that has
284  * already inserted and/or deleted rows in the target table. ANALYZE will
285  * have counted such rows as live or dead respectively. Because we will
286  * report our counts of such rows at transaction end, we should subtract
287  * off these counts from the update we're making now, else they'll be
288  * double-counted after commit. (This approach also ensures that the
289  * shared stats entry ends up with the right numbers if we abort instead
290  * of committing.)
291  *
292  * Waste no time on partitioned tables, though.
293  */
294  if (pgstat_should_count_relation(rel) &&
295  rel->rd_rel->relkind != RELKIND_PARTITIONED_TABLE)
296  {
298 
299  for (trans = rel->pgstat_info->trans; trans; trans = trans->upper)
300  {
301  livetuples -= trans->tuples_inserted - trans->tuples_deleted;
302  deadtuples -= trans->tuples_updated + trans->tuples_deleted;
303  }
304  /* count stuff inserted by already-aborted subxacts, too */
305  deadtuples -= rel->pgstat_info->t_counts.t_delta_dead_tuples;
306  /* Since ANALYZE's counts are estimates, we could have underflowed */
307  livetuples = Max(livetuples, 0);
308  deadtuples = Max(deadtuples, 0);
309  }
310 
311  /* block acquiring lock for the same reason as pgstat_report_autovac() */
313  RelationGetRelid(rel),
314  false);
315  /* can't get dropped while accessed */
316  Assert(entry_ref != NULL && entry_ref->shared_stats != NULL);
317 
318  shtabentry = (PgStatShared_Relation *) entry_ref->shared_stats;
319  tabentry = &shtabentry->stats;
320 
321  tabentry->live_tuples = livetuples;
322  tabentry->dead_tuples = deadtuples;
323 
324  /*
325  * If commanded, reset mod_since_analyze to zero. This forgets any
326  * changes that were committed while the ANALYZE was in progress, but we
327  * have no good way to estimate how many of those there were.
328  */
329  if (resetcounter)
330  tabentry->mod_since_analyze = 0;
331 
333  {
335  tabentry->autoanalyze_count++;
336  }
337  else
338  {
340  tabentry->analyze_count++;
341  }
342 
343  pgstat_unlock_entry(entry_ref);
344 }
345 
346 /*
347  * count a tuple insertion of n tuples
348  */
349 void
351 {
353  {
354  PgStat_TableStatus *pgstat_info = rel->pgstat_info;
355 
356  ensure_tabstat_xact_level(pgstat_info);
357  pgstat_info->trans->tuples_inserted += n;
358  }
359 }
360 
361 /*
362  * count a tuple update
363  */
364 void
366 {
368  {
369  PgStat_TableStatus *pgstat_info = rel->pgstat_info;
370 
371  ensure_tabstat_xact_level(pgstat_info);
372  pgstat_info->trans->tuples_updated++;
373 
374  /* t_tuples_hot_updated is nontransactional, so just advance it */
375  if (hot)
376  pgstat_info->t_counts.t_tuples_hot_updated++;
377  }
378 }
379 
380 /*
381  * count a tuple deletion
382  */
383 void
385 {
387  {
388  PgStat_TableStatus *pgstat_info = rel->pgstat_info;
389 
390  ensure_tabstat_xact_level(pgstat_info);
391  pgstat_info->trans->tuples_deleted++;
392  }
393 }
394 
395 /*
396  * update tuple counters due to truncate
397  */
398 void
400 {
402  {
403  PgStat_TableStatus *pgstat_info = rel->pgstat_info;
404 
405  ensure_tabstat_xact_level(pgstat_info);
406  save_truncdrop_counters(pgstat_info->trans, false);
407  pgstat_info->trans->tuples_inserted = 0;
408  pgstat_info->trans->tuples_updated = 0;
409  pgstat_info->trans->tuples_deleted = 0;
410  }
411 }
412 
413 /*
414  * update dead-tuples count
415  *
416  * The semantics of this are that we are reporting the nontransactional
417  * recovery of "delta" dead tuples; so t_delta_dead_tuples decreases
418  * rather than increasing, and the change goes straight into the per-table
419  * counter, not into transactional state.
420  */
421 void
423 {
425  {
426  PgStat_TableStatus *pgstat_info = rel->pgstat_info;
427 
428  pgstat_info->t_counts.t_delta_dead_tuples -= delta;
429  }
430 }
431 
432 /*
433  * Support function for the SQL-callable pgstat* functions. Returns
434  * the collected statistics for one table or NULL. NULL doesn't mean
435  * that the table doesn't exist, just that there are no statistics, so the
436  * caller is better off to report ZERO instead.
437  */
440 {
441  return pgstat_fetch_stat_tabentry_ext(IsSharedRelation(relid), relid);
442 }
443 
444 /*
445  * More efficient version of pgstat_fetch_stat_tabentry(), allowing to specify
446  * whether the to-be-accessed table is a shared relation or not.
447  */
450 {
451  Oid dboid = (shared ? InvalidOid : MyDatabaseId);
452 
453  return (PgStat_StatTabEntry *)
455 }
456 
457 /*
458  * find any existing PgStat_TableStatus entry for rel
459  *
460  * Find any existing PgStat_TableStatus entry for rel_id in the current
461  * database. If not found, try finding from shared tables.
462  *
463  * If no entry found, return NULL, don't create a new one
464  */
467 {
468  PgStat_EntryRef *entry_ref;
469 
471  if (!entry_ref)
473 
474  if (entry_ref)
475  return entry_ref->pending;
476  return NULL;
477 }
478 
479 /*
480  * Perform relation stats specific end-of-transaction work. Helper for
481  * AtEOXact_PgStat.
482  *
483  * Transfer transactional insert/update counts into the base tabstat entries.
484  * We don't bother to free any of the transactional state, since it's all in
485  * TopTransactionContext and will go away anyway.
486  */
487 void
489 {
491 
492  for (trans = xact_state->first; trans != NULL; trans = trans->next)
493  {
494  PgStat_TableStatus *tabstat;
495 
496  Assert(trans->nest_level == 1);
497  Assert(trans->upper == NULL);
498  tabstat = trans->parent;
499  Assert(tabstat->trans == trans);
500  /* restore pre-truncate/drop stats (if any) in case of aborted xact */
501  if (!isCommit)
503  /* count attempted actions regardless of commit/abort */
504  tabstat->t_counts.t_tuples_inserted += trans->tuples_inserted;
505  tabstat->t_counts.t_tuples_updated += trans->tuples_updated;
506  tabstat->t_counts.t_tuples_deleted += trans->tuples_deleted;
507  if (isCommit)
508  {
509  tabstat->t_counts.t_truncdropped = trans->truncdropped;
510  if (trans->truncdropped)
511  {
512  /* forget live/dead stats seen by backend thus far */
513  tabstat->t_counts.t_delta_live_tuples = 0;
514  tabstat->t_counts.t_delta_dead_tuples = 0;
515  }
516  /* insert adds a live tuple, delete removes one */
517  tabstat->t_counts.t_delta_live_tuples +=
518  trans->tuples_inserted - trans->tuples_deleted;
519  /* update and delete each create a dead tuple */
520  tabstat->t_counts.t_delta_dead_tuples +=
521  trans->tuples_updated + trans->tuples_deleted;
522  /* insert, update, delete each count as one change event */
523  tabstat->t_counts.t_changed_tuples +=
524  trans->tuples_inserted + trans->tuples_updated +
525  trans->tuples_deleted;
526  }
527  else
528  {
529  /* inserted tuples are dead, deleted tuples are unaffected */
530  tabstat->t_counts.t_delta_dead_tuples +=
531  trans->tuples_inserted + trans->tuples_updated;
532  /* an aborted xact generates no changed_tuple events */
533  }
534  tabstat->trans = NULL;
535  }
536 }
537 
538 /*
539  * Perform relation stats specific end-of-sub-transaction work. Helper for
540  * AtEOSubXact_PgStat.
541  *
542  * Transfer transactional insert/update counts into the next higher
543  * subtransaction state.
544  */
545 void
546 AtEOSubXact_PgStat_Relations(PgStat_SubXactStatus *xact_state, bool isCommit, int nestDepth)
547 {
549  PgStat_TableXactStatus *next_trans;
550 
551  for (trans = xact_state->first; trans != NULL; trans = next_trans)
552  {
553  PgStat_TableStatus *tabstat;
554 
555  next_trans = trans->next;
556  Assert(trans->nest_level == nestDepth);
557  tabstat = trans->parent;
558  Assert(tabstat->trans == trans);
559 
560  if (isCommit)
561  {
562  if (trans->upper && trans->upper->nest_level == nestDepth - 1)
563  {
564  if (trans->truncdropped)
565  {
566  /* propagate the truncate/drop status one level up */
567  save_truncdrop_counters(trans->upper, false);
568  /* replace upper xact stats with ours */
569  trans->upper->tuples_inserted = trans->tuples_inserted;
570  trans->upper->tuples_updated = trans->tuples_updated;
571  trans->upper->tuples_deleted = trans->tuples_deleted;
572  }
573  else
574  {
575  trans->upper->tuples_inserted += trans->tuples_inserted;
576  trans->upper->tuples_updated += trans->tuples_updated;
577  trans->upper->tuples_deleted += trans->tuples_deleted;
578  }
579  tabstat->trans = trans->upper;
580  pfree(trans);
581  }
582  else
583  {
584  /*
585  * When there isn't an immediate parent state, we can just
586  * reuse the record instead of going through a palloc/pfree
587  * pushup (this works since it's all in TopTransactionContext
588  * anyway). We have to re-link it into the parent level,
589  * though, and that might mean pushing a new entry into the
590  * pgStatXactStack.
591  */
592  PgStat_SubXactStatus *upper_xact_state;
593 
594  upper_xact_state = pgstat_get_xact_stack_level(nestDepth - 1);
595  trans->next = upper_xact_state->first;
596  upper_xact_state->first = trans;
597  trans->nest_level = nestDepth - 1;
598  }
599  }
600  else
601  {
602  /*
603  * On abort, update top-level tabstat counts, then forget the
604  * subtransaction
605  */
606 
607  /* first restore values obliterated by truncate/drop */
609  /* count attempted actions regardless of commit/abort */
610  tabstat->t_counts.t_tuples_inserted += trans->tuples_inserted;
611  tabstat->t_counts.t_tuples_updated += trans->tuples_updated;
612  tabstat->t_counts.t_tuples_deleted += trans->tuples_deleted;
613  /* inserted tuples are dead, deleted tuples are unaffected */
614  tabstat->t_counts.t_delta_dead_tuples +=
615  trans->tuples_inserted + trans->tuples_updated;
616  tabstat->trans = trans->upper;
617  pfree(trans);
618  }
619  }
620 }
621 
622 /*
623  * Generate 2PC records for all the pending transaction-dependent relation
624  * stats.
625  */
626 void
628 {
630 
631  for (trans = xact_state->first; trans != NULL; trans = trans->next)
632  {
634  TwoPhasePgStatRecord record;
635 
636  Assert(trans->nest_level == 1);
637  Assert(trans->upper == NULL);
638  tabstat = trans->parent;
639  Assert(tabstat->trans == trans);
640 
641  record.tuples_inserted = trans->tuples_inserted;
642  record.tuples_updated = trans->tuples_updated;
643  record.tuples_deleted = trans->tuples_deleted;
644  record.inserted_pre_truncdrop = trans->inserted_pre_truncdrop;
645  record.updated_pre_truncdrop = trans->updated_pre_truncdrop;
646  record.deleted_pre_truncdrop = trans->deleted_pre_truncdrop;
647  record.t_id = tabstat->t_id;
648  record.t_shared = tabstat->t_shared;
649  record.t_truncdropped = trans->truncdropped;
650 
652  &record, sizeof(TwoPhasePgStatRecord));
653  }
654 }
655 
656 /*
657  * All we need do here is unlink the transaction stats state from the
658  * nontransactional state. The nontransactional action counts will be
659  * reported to the stats system immediately, while the effects on live and
660  * dead tuple counts are preserved in the 2PC state file.
661  *
662  * Note: AtEOXact_PgStat_Relations is not called during PREPARE.
663  */
664 void
666 {
668 
669  for (trans = xact_state->first; trans != NULL; trans = trans->next)
670  {
671  PgStat_TableStatus *tabstat;
672 
673  tabstat = trans->parent;
674  tabstat->trans = NULL;
675  }
676 }
677 
678 /*
679  * 2PC processing routine for COMMIT PREPARED case.
680  *
681  * Load the saved counts into our local pgstats state.
682  */
683 void
685  void *recdata, uint32 len)
686 {
687  TwoPhasePgStatRecord *rec = (TwoPhasePgStatRecord *) recdata;
688  PgStat_TableStatus *pgstat_info;
689 
690  /* Find or create a tabstat entry for the rel */
691  pgstat_info = pgstat_prep_relation_pending(rec->t_id, rec->t_shared);
692 
693  /* Same math as in AtEOXact_PgStat, commit case */
694  pgstat_info->t_counts.t_tuples_inserted += rec->tuples_inserted;
695  pgstat_info->t_counts.t_tuples_updated += rec->tuples_updated;
696  pgstat_info->t_counts.t_tuples_deleted += rec->tuples_deleted;
697  pgstat_info->t_counts.t_truncdropped = rec->t_truncdropped;
698  if (rec->t_truncdropped)
699  {
700  /* forget live/dead stats seen by backend thus far */
701  pgstat_info->t_counts.t_delta_live_tuples = 0;
702  pgstat_info->t_counts.t_delta_dead_tuples = 0;
703  }
704  pgstat_info->t_counts.t_delta_live_tuples +=
705  rec->tuples_inserted - rec->tuples_deleted;
706  pgstat_info->t_counts.t_delta_dead_tuples +=
707  rec->tuples_updated + rec->tuples_deleted;
708  pgstat_info->t_counts.t_changed_tuples +=
709  rec->tuples_inserted + rec->tuples_updated +
710  rec->tuples_deleted;
711 }
712 
713 /*
714  * 2PC processing routine for ROLLBACK PREPARED case.
715  *
716  * Load the saved counts into our local pgstats state, but treat them
717  * as aborted.
718  */
719 void
721  void *recdata, uint32 len)
722 {
723  TwoPhasePgStatRecord *rec = (TwoPhasePgStatRecord *) recdata;
724  PgStat_TableStatus *pgstat_info;
725 
726  /* Find or create a tabstat entry for the rel */
727  pgstat_info = pgstat_prep_relation_pending(rec->t_id, rec->t_shared);
728 
729  /* Same math as in AtEOXact_PgStat, abort case */
730  if (rec->t_truncdropped)
731  {
735  }
736  pgstat_info->t_counts.t_tuples_inserted += rec->tuples_inserted;
737  pgstat_info->t_counts.t_tuples_updated += rec->tuples_updated;
738  pgstat_info->t_counts.t_tuples_deleted += rec->tuples_deleted;
739  pgstat_info->t_counts.t_delta_dead_tuples +=
740  rec->tuples_inserted + rec->tuples_updated;
741 }
742 
743 /*
744  * Flush out pending stats for the entry
745  *
746  * If nowait is true, this function returns false if lock could not
747  * immediately acquired, otherwise true is returned.
748  *
749  * Some of the stats are copied to the corresponding pending database stats
750  * entry when successfully flushing.
751  */
752 bool
754 {
755  static const PgStat_TableCounts all_zeroes;
756  Oid dboid;
757  PgStat_TableStatus *lstats; /* pending stats entry */
758  PgStatShared_Relation *shtabstats;
759  PgStat_StatTabEntry *tabentry; /* table entry of shared stats */
760  PgStat_StatDBEntry *dbentry; /* pending database entry */
761 
762  dboid = entry_ref->shared_entry->key.dboid;
763  lstats = (PgStat_TableStatus *) entry_ref->pending;
764  shtabstats = (PgStatShared_Relation *) entry_ref->shared_stats;
765 
766  /*
767  * Ignore entries that didn't accumulate any actual counts, such as
768  * indexes that were opened by the planner but not used.
769  */
770  if (memcmp(&lstats->t_counts, &all_zeroes,
771  sizeof(PgStat_TableCounts)) == 0)
772  {
773  return true;
774  }
775 
776  if (!pgstat_lock_entry(entry_ref, nowait))
777  return false;
778 
779  /* add the values to the shared entry. */
780  tabentry = &shtabstats->stats;
781 
782  tabentry->numscans += lstats->t_counts.t_numscans;
783  if (lstats->t_counts.t_numscans)
784  {
786 
787  if (t > tabentry->lastscan)
788  tabentry->lastscan = t;
789  }
790  tabentry->tuples_returned += lstats->t_counts.t_tuples_returned;
791  tabentry->tuples_fetched += lstats->t_counts.t_tuples_fetched;
792  tabentry->tuples_inserted += lstats->t_counts.t_tuples_inserted;
793  tabentry->tuples_updated += lstats->t_counts.t_tuples_updated;
794  tabentry->tuples_deleted += lstats->t_counts.t_tuples_deleted;
795  tabentry->tuples_hot_updated += lstats->t_counts.t_tuples_hot_updated;
796 
797  /*
798  * If table was truncated/dropped, first reset the live/dead counters.
799  */
800  if (lstats->t_counts.t_truncdropped)
801  {
802  tabentry->live_tuples = 0;
803  tabentry->dead_tuples = 0;
804  tabentry->ins_since_vacuum = 0;
805  }
806 
807  tabentry->live_tuples += lstats->t_counts.t_delta_live_tuples;
808  tabentry->dead_tuples += lstats->t_counts.t_delta_dead_tuples;
809  tabentry->mod_since_analyze += lstats->t_counts.t_changed_tuples;
810  tabentry->ins_since_vacuum += lstats->t_counts.t_tuples_inserted;
811  tabentry->blocks_fetched += lstats->t_counts.t_blocks_fetched;
812  tabentry->blocks_hit += lstats->t_counts.t_blocks_hit;
813 
814  /* Clamp live_tuples in case of negative delta_live_tuples */
815  tabentry->live_tuples = Max(tabentry->live_tuples, 0);
816  /* Likewise for dead_tuples */
817  tabentry->dead_tuples = Max(tabentry->dead_tuples, 0);
818 
819  pgstat_unlock_entry(entry_ref);
820 
821  /* The entry was successfully flushed, add the same to database stats */
822  dbentry = pgstat_prep_database_pending(dboid);
823  dbentry->tuples_returned += lstats->t_counts.t_tuples_returned;
824  dbentry->tuples_fetched += lstats->t_counts.t_tuples_fetched;
825  dbentry->tuples_inserted += lstats->t_counts.t_tuples_inserted;
826  dbentry->tuples_updated += lstats->t_counts.t_tuples_updated;
827  dbentry->tuples_deleted += lstats->t_counts.t_tuples_deleted;
828  dbentry->blocks_fetched += lstats->t_counts.t_blocks_fetched;
829  dbentry->blocks_hit += lstats->t_counts.t_blocks_hit;
830 
831  return true;
832 }
833 
834 void
836 {
837  PgStat_TableStatus *pending = (PgStat_TableStatus *) entry_ref->pending;
838 
839  if (pending->relation)
841 }
842 
843 /*
844  * Find or create a PgStat_TableStatus entry for rel. New entry is created and
845  * initialized if not exists.
846  */
847 static PgStat_TableStatus *
848 pgstat_prep_relation_pending(Oid rel_id, bool isshared)
849 {
850  PgStat_EntryRef *entry_ref;
851  PgStat_TableStatus *pending;
852 
854  isshared ? InvalidOid : MyDatabaseId,
855  rel_id, NULL);
856  pending = entry_ref->pending;
857  pending->t_id = rel_id;
858  pending->t_shared = isshared;
859 
860  return pending;
861 }
862 
863 /*
864  * add a new (sub)transaction state record
865  */
866 static void
867 add_tabstat_xact_level(PgStat_TableStatus *pgstat_info, int nest_level)
868 {
869  PgStat_SubXactStatus *xact_state;
871 
872  /*
873  * If this is the first rel to be modified at the current nest level, we
874  * first have to push a transaction stack entry.
875  */
876  xact_state = pgstat_get_xact_stack_level(nest_level);
877 
878  /* Now make a per-table stack entry */
881  sizeof(PgStat_TableXactStatus));
882  trans->nest_level = nest_level;
883  trans->upper = pgstat_info->trans;
884  trans->parent = pgstat_info;
885  trans->next = xact_state->first;
886  xact_state->first = trans;
887  pgstat_info->trans = trans;
888 }
889 
890 /*
891  * Add a new (sub)transaction record if needed.
892  */
893 static void
895 {
896  int nest_level = GetCurrentTransactionNestLevel();
897 
898  if (pgstat_info->trans == NULL ||
899  pgstat_info->trans->nest_level != nest_level)
900  add_tabstat_xact_level(pgstat_info, nest_level);
901 }
902 
903 /*
904  * Whenever a table is truncated/dropped, we save its i/u/d counters so that
905  * they can be cleared, and if the (sub)xact that executed the truncate/drop
906  * later aborts, the counters can be restored to the saved (pre-truncate/drop)
907  * values.
908  *
909  * Note that for truncate we do this on the first truncate in any particular
910  * subxact level only.
911  */
912 static void
914 {
915  if (!trans->truncdropped || is_drop)
916  {
917  trans->inserted_pre_truncdrop = trans->tuples_inserted;
918  trans->updated_pre_truncdrop = trans->tuples_updated;
919  trans->deleted_pre_truncdrop = trans->tuples_deleted;
920  trans->truncdropped = true;
921  }
922 }
923 
924 /*
925  * restore counters when a truncate aborts
926  */
927 static void
929 {
930  if (trans->truncdropped)
931  {
932  trans->tuples_inserted = trans->inserted_pre_truncdrop;
933  trans->tuples_updated = trans->updated_pre_truncdrop;
934  trans->tuples_deleted = trans->deleted_pre_truncdrop;
935  }
936 }
bool IsAutoVacuumWorkerProcess(void)
Definition: autovacuum.c:3319
TimestampTz GetCurrentTimestamp(void)
Definition: timestamp.c:1585
unsigned short uint16
Definition: c.h:489
unsigned int uint32
Definition: c.h:490
#define Max(x, y)
Definition: c.h:982
uint32 TransactionId
Definition: c.h:636
#define PG_USED_FOR_ASSERTS_ONLY
Definition: c.h:166
bool IsSharedRelation(Oid relationId)
Definition: catalog.c:245
int64 TimestampTz
Definition: timestamp.h:39
Oid MyDatabaseId
Definition: globals.c:89
if(TABLE==NULL||TABLE_index==NULL)
Definition: isn.c:77
Assert(fmt[strlen(fmt) - 1] !='\n')
MemoryContext TopTransactionContext
Definition: mcxt.c:146
void pfree(void *pointer)
Definition: mcxt.c:1436
void * MemoryContextAllocZero(MemoryContext context, Size size)
Definition: mcxt.c:1048
const void size_t len
bool pgstat_track_counts
Definition: pgstat.c:186
void * pgstat_fetch_entry(PgStat_Kind kind, Oid dboid, Oid objoid)
Definition: pgstat.c:779
PgStat_EntryRef * pgstat_prep_pending_entry(PgStat_Kind kind, Oid dboid, Oid objoid, bool *created_entry)
Definition: pgstat.c:1063
PgStat_EntryRef * pgstat_fetch_pending_entry(PgStat_Kind kind, Oid dboid, Oid objoid)
Definition: pgstat.c:1101
@ PGSTAT_KIND_RELATION
Definition: pgstat.h:42
#define pgstat_should_count_relation(rel)
Definition: pgstat.h:520
int64 PgStat_Counter
Definition: pgstat.h:88
PgStat_StatDBEntry * pgstat_prep_database_pending(Oid dboid)
struct TwoPhasePgStatRecord TwoPhasePgStatRecord
static void ensure_tabstat_xact_level(PgStat_TableStatus *pgstat_info)
void pgstat_copy_relation_stats(Relation dst, Relation src)
void pgstat_unlink_relation(Relation rel)
void pgstat_count_heap_update(Relation rel, bool hot)
void pgstat_twophase_postcommit(TransactionId xid, uint16 info, void *recdata, uint32 len)
static PgStat_TableStatus * pgstat_prep_relation_pending(Oid rel_id, bool isshared)
PgStat_TableStatus * find_tabstat_entry(Oid rel_id)
void pgstat_assoc_relation(Relation rel)
void pgstat_report_analyze(Relation rel, PgStat_Counter livetuples, PgStat_Counter deadtuples, bool resetcounter)
static void save_truncdrop_counters(PgStat_TableXactStatus *trans, bool is_drop)
void AtPrepare_PgStat_Relations(PgStat_SubXactStatus *xact_state)
void pgstat_twophase_postabort(TransactionId xid, uint16 info, void *recdata, uint32 len)
static void add_tabstat_xact_level(PgStat_TableStatus *pgstat_info, int nest_level)
bool pgstat_relation_flush_cb(PgStat_EntryRef *entry_ref, bool nowait)
PgStat_StatTabEntry * pgstat_fetch_stat_tabentry_ext(bool shared, Oid reloid)
void pgstat_create_relation(Relation rel)
PgStat_StatTabEntry * pgstat_fetch_stat_tabentry(Oid relid)
void pgstat_update_heap_dead_tuples(Relation rel, int delta)
void pgstat_count_heap_delete(Relation rel)
void pgstat_count_heap_insert(Relation rel, PgStat_Counter n)
void AtEOXact_PgStat_Relations(PgStat_SubXactStatus *xact_state, bool isCommit)
void pgstat_report_vacuum(Oid tableoid, bool shared, PgStat_Counter livetuples, PgStat_Counter deadtuples)
void PostPrepare_PgStat_Relations(PgStat_SubXactStatus *xact_state)
void pgstat_count_truncate(Relation rel)
static void restore_truncdrop_counters(PgStat_TableXactStatus *trans)
void pgstat_drop_relation(Relation rel)
void AtEOSubXact_PgStat_Relations(PgStat_SubXactStatus *xact_state, bool isCommit, int nestDepth)
void pgstat_init_relation(Relation rel)
void pgstat_relation_delete_pending_cb(PgStat_EntryRef *entry_ref)
void pgstat_unlock_entry(PgStat_EntryRef *entry_ref)
Definition: pgstat_shmem.c:599
bool pgstat_lock_entry(PgStat_EntryRef *entry_ref, bool nowait)
Definition: pgstat_shmem.c:571
PgStat_EntryRef * pgstat_get_entry_ref_locked(PgStat_Kind kind, Oid dboid, Oid objoid, bool nowait)
Definition: pgstat_shmem.c:608
void pgstat_create_transactional(PgStat_Kind kind, Oid dboid, Oid objoid)
Definition: pgstat_xact.c:358
void pgstat_drop_transactional(PgStat_Kind kind, Oid dboid, Oid objoid)
Definition: pgstat_xact.c:380
PgStat_SubXactStatus * pgstat_get_xact_stack_level(int nest_level)
Definition: pgstat_xact.c:237
#define InvalidOid
Definition: postgres_ext.h:36
unsigned int Oid
Definition: postgres_ext.h:31
#define RelationGetRelid(relation)
Definition: rel.h:501
PgStat_StatTabEntry stats
PgStatShared_Common * shared_stats
PgStatShared_HashEntry * shared_entry
PgStat_Counter tuples_updated
Definition: pgstat.h:288
PgStat_Counter tuples_inserted
Definition: pgstat.h:287
PgStat_Counter tuples_returned
Definition: pgstat.h:285
PgStat_Counter blocks_hit
Definition: pgstat.h:284
PgStat_Counter blocks_fetched
Definition: pgstat.h:283
PgStat_Counter tuples_deleted
Definition: pgstat.h:289
PgStat_Counter tuples_fetched
Definition: pgstat.h:286
PgStat_Counter vacuum_count
Definition: pgstat.h:376
PgStat_Counter tuples_fetched
Definition: pgstat.h:360
PgStat_Counter ins_since_vacuum
Definition: pgstat.h:370
PgStat_Counter blocks_hit
Definition: pgstat.h:373
PgStat_Counter mod_since_analyze
Definition: pgstat.h:369
TimestampTz last_autovacuum_time
Definition: pgstat.h:377
PgStat_Counter analyze_count
Definition: pgstat.h:380
PgStat_Counter tuples_deleted
Definition: pgstat.h:364
PgStat_Counter tuples_hot_updated
Definition: pgstat.h:365
PgStat_Counter tuples_updated
Definition: pgstat.h:363
PgStat_Counter live_tuples
Definition: pgstat.h:367
PgStat_Counter numscans
Definition: pgstat.h:356
PgStat_Counter autovacuum_count
Definition: pgstat.h:378
TimestampTz last_analyze_time
Definition: pgstat.h:379
PgStat_Counter dead_tuples
Definition: pgstat.h:368
PgStat_Counter autoanalyze_count
Definition: pgstat.h:382
PgStat_Counter blocks_fetched
Definition: pgstat.h:372
PgStat_Counter tuples_returned
Definition: pgstat.h:359
TimestampTz last_autoanalyze_time
Definition: pgstat.h:381
TimestampTz lastscan
Definition: pgstat.h:357
PgStat_Counter tuples_inserted
Definition: pgstat.h:362
TimestampTz last_vacuum_time
Definition: pgstat.h:375
PgStat_TableXactStatus * first
PgStat_Counter t_delta_dead_tuples
Definition: pgstat.h:182
PgStat_Counter t_delta_live_tuples
Definition: pgstat.h:181
PgStat_Counter t_tuples_hot_updated
Definition: pgstat.h:178
PgStat_Counter t_tuples_inserted
Definition: pgstat.h:175
PgStat_Counter t_tuples_updated
Definition: pgstat.h:176
PgStat_Counter t_tuples_fetched
Definition: pgstat.h:173
PgStat_Counter t_tuples_deleted
Definition: pgstat.h:177
PgStat_Counter t_numscans
Definition: pgstat.h:170
PgStat_Counter t_blocks_hit
Definition: pgstat.h:186
bool t_truncdropped
Definition: pgstat.h:179
PgStat_Counter t_changed_tuples
Definition: pgstat.h:183
PgStat_Counter t_blocks_fetched
Definition: pgstat.h:185
PgStat_Counter t_tuples_returned
Definition: pgstat.h:172
PgStat_TableCounts t_counts
Definition: pgstat.h:209
struct PgStat_TableXactStatus * trans
Definition: pgstat.h:208
Relation relation
Definition: pgstat.h:210
PgStat_Counter tuples_inserted
Definition: pgstat.h:219
PgStat_Counter tuples_updated
Definition: pgstat.h:220
PgStat_Counter tuples_deleted
Definition: pgstat.h:221
bool pgstat_enabled
Definition: rel.h:249
Form_pg_class rd_rel
Definition: rel.h:110
struct PgStat_TableStatus * pgstat_info
Definition: rel.h:251
PgStat_Counter deleted_pre_truncdrop
PgStat_Counter inserted_pre_truncdrop
PgStat_Counter updated_pre_truncdrop
PgStat_Counter tuples_deleted
PgStat_Counter tuples_inserted
PgStat_Counter tuples_updated
void RegisterTwoPhaseRecord(TwoPhaseRmgrId rmid, uint16 info, const void *data, uint32 len)
Definition: twophase.c:1257
#define TWOPHASE_RM_PGSTAT_ID
Definition: twophase_rmgr.h:26
int GetCurrentTransactionNestLevel(void)
Definition: xact.c:914
TimestampTz GetCurrentTransactionStopTimestamp(void)
Definition: xact.c:876
static zic_t trans[TZ_MAX_LEAPS]
Definition: zic.c:402