1
0
mirror of https://github.com/postgres/postgres.git synced 2025-11-22 12:22:45 +03:00

Extend PgStat_HashKey.objid from 4 to 8 bytes

This opens the possibility to define keys for more types of statistics
kinds in PgStat_HashKey, the first case being 8-byte query IDs for
statistics like pg_stat_statements.

This increases the size of PgStat_HashKey from 12 to 16 bytes, while
PgStatShared_HashEntry, entry stored in the dshash for pgstats, keeps
the same size due to alignment.

xl_xact_stats_item, that tracks the stats items to drop in commit WAL
records, is increased from 12 to 16 bytes.  Note that individual chunks
in commit WAL records should be multiples of sizeof(int), hence 8-byte
object IDs are stored as two uint32, based on a suggestion from Heikki
Linnakangas.

While on it, the field of PgStat_HashKey is renamed from "objoid" to
"objid", as for some stats kinds this field does not refer to OIDs but
just IDs, like for replication slot stats.

This commit bumps the following format variables:
- PGSTAT_FILE_FORMAT_ID, as PgStat_HashKey is written to the stats file
for non-serialized stats kinds in the dshash table.
- XLOG_PAGE_MAGIC for the changes in xl_xact_stats_item.
- Catalog version, for the SQL function pg_stat_have_stats().

Reviewed-by: Bertrand Drouvot
Discussion: https://postgr.es/m/ZsvTS9EW79Up8I62@paquier.xyz
This commit is contained in:
Michael Paquier
2024-09-18 12:44:15 +09:00
parent ac04aa84a7
commit b14e9ce7d5
17 changed files with 98 additions and 75 deletions

View File

@@ -846,7 +846,7 @@ pgstat_reset_counters(void)
* GRANT system.
*/
void
pgstat_reset(PgStat_Kind kind, Oid dboid, Oid objoid)
pgstat_reset(PgStat_Kind kind, Oid dboid, uint64 objid)
{
const PgStat_KindInfo *kind_info = pgstat_get_kind_info(kind);
TimestampTz ts = GetCurrentTimestamp();
@@ -855,7 +855,7 @@ pgstat_reset(PgStat_Kind kind, Oid dboid, Oid objoid)
Assert(!pgstat_get_kind_info(kind)->fixed_amount);
/* reset the "single counter" */
pgstat_reset_entry(kind, dboid, objoid, ts);
pgstat_reset_entry(kind, dboid, objid, ts);
if (!kind_info->accessed_across_databases)
pgstat_reset_database_timestamp(dboid, ts);
@@ -926,7 +926,7 @@ pgstat_clear_snapshot(void)
}
void *
pgstat_fetch_entry(PgStat_Kind kind, Oid dboid, Oid objoid)
pgstat_fetch_entry(PgStat_Kind kind, Oid dboid, uint64 objid)
{
PgStat_HashKey key;
PgStat_EntryRef *entry_ref;
@@ -941,7 +941,7 @@ pgstat_fetch_entry(PgStat_Kind kind, Oid dboid, Oid objoid)
key.kind = kind;
key.dboid = dboid;
key.objoid = objoid;
key.objid = objid;
/* if we need to build a full snapshot, do so */
if (pgstat_fetch_consistency == PGSTAT_FETCH_CONSISTENCY_SNAPSHOT)
@@ -967,7 +967,7 @@ pgstat_fetch_entry(PgStat_Kind kind, Oid dboid, Oid objoid)
pgStatLocal.snapshot.mode = pgstat_fetch_consistency;
entry_ref = pgstat_get_entry_ref(kind, dboid, objoid, false, NULL);
entry_ref = pgstat_get_entry_ref(kind, dboid, objid, false, NULL);
if (entry_ref == NULL || entry_ref->shared_entry->dropped)
{
@@ -1036,13 +1036,13 @@ pgstat_get_stat_snapshot_timestamp(bool *have_snapshot)
}
bool
pgstat_have_entry(PgStat_Kind kind, Oid dboid, Oid objoid)
pgstat_have_entry(PgStat_Kind kind, Oid dboid, uint64 objid)
{
/* fixed-numbered stats always exist */
if (pgstat_get_kind_info(kind)->fixed_amount)
return true;
return pgstat_get_entry_ref(kind, dboid, objoid, false, NULL) != NULL;
return pgstat_get_entry_ref(kind, dboid, objid, false, NULL) != NULL;
}
/*
@@ -1257,7 +1257,7 @@ pgstat_build_snapshot_fixed(PgStat_Kind kind)
* created, false otherwise.
*/
PgStat_EntryRef *
pgstat_prep_pending_entry(PgStat_Kind kind, Oid dboid, Oid objoid, bool *created_entry)
pgstat_prep_pending_entry(PgStat_Kind kind, Oid dboid, uint64 objid, bool *created_entry)
{
PgStat_EntryRef *entry_ref;
@@ -1272,7 +1272,7 @@ pgstat_prep_pending_entry(PgStat_Kind kind, Oid dboid, Oid objoid, bool *created
ALLOCSET_SMALL_SIZES);
}
entry_ref = pgstat_get_entry_ref(kind, dboid, objoid,
entry_ref = pgstat_get_entry_ref(kind, dboid, objid,
true, created_entry);
if (entry_ref->pending == NULL)
@@ -1295,11 +1295,11 @@ pgstat_prep_pending_entry(PgStat_Kind kind, Oid dboid, Oid objoid, bool *created
* that it shouldn't be needed.
*/
PgStat_EntryRef *
pgstat_fetch_pending_entry(PgStat_Kind kind, Oid dboid, Oid objoid)
pgstat_fetch_pending_entry(PgStat_Kind kind, Oid dboid, uint64 objid)
{
PgStat_EntryRef *entry_ref;
entry_ref = pgstat_get_entry_ref(kind, dboid, objoid, false, NULL);
entry_ref = pgstat_get_entry_ref(kind, dboid, objid, false, NULL);
if (entry_ref == NULL || entry_ref->pending == NULL)
return NULL;
@@ -1648,8 +1648,9 @@ pgstat_write_statsfile(XLogRecPtr redo)
*/
if (!pgstat_is_kind_valid(ps->key.kind))
{
elog(WARNING, "found unknown stats entry %u/%u/%u",
ps->key.kind, ps->key.dboid, ps->key.objoid);
elog(WARNING, "found unknown stats entry %u/%u/%llu",
ps->key.kind, ps->key.dboid,
(unsigned long long) ps->key.objid);
continue;
}
@@ -1885,8 +1886,9 @@ pgstat_read_statsfile(XLogRecPtr redo)
if (!pgstat_is_kind_valid(key.kind))
{
elog(WARNING, "invalid stats kind for entry %u/%u/%u of type %c",
key.kind, key.dboid, key.objoid, t);
elog(WARNING, "invalid stats kind for entry %u/%u/%llu of type %c",
key.kind, key.dboid,
(unsigned long long) key.objid, t);
goto error;
}
}
@@ -1957,8 +1959,9 @@ pgstat_read_statsfile(XLogRecPtr redo)
if (found)
{
dshash_release_lock(pgStatLocal.shared_hash, p);
elog(WARNING, "found duplicate stats entry %u/%u/%u of type %c",
key.kind, key.dboid, key.objoid, t);
elog(WARNING, "found duplicate stats entry %u/%u/%llu of type %c",
key.kind, key.dboid,
(unsigned long long) key.objid, t);
goto error;
}
@@ -1969,8 +1972,9 @@ pgstat_read_statsfile(XLogRecPtr redo)
pgstat_get_entry_data(key.kind, header),
pgstat_get_entry_len(key.kind)))
{
elog(WARNING, "could not read data for entry %u/%u/%u of type %c",
key.kind, key.dboid, key.objoid, t);
elog(WARNING, "could not read data for entry %u/%u/%llu of type %c",
key.kind, key.dboid,
(unsigned long long) key.objid, t);
goto error;
}

View File

@@ -193,9 +193,9 @@ pgstat_replslot_to_serialized_name_cb(const PgStat_HashKey *key, const PgStatSha
* isn't allowed to change at this point, we can assume that a slot exists
* at the offset.
*/
if (!ReplicationSlotName(key->objoid, name))
elog(ERROR, "could not find name for replication slot index %u",
key->objoid);
if (!ReplicationSlotName(key->objid, name))
elog(ERROR, "could not find name for replication slot index %llu",
(unsigned long long) key->objid);
}
bool
@@ -209,7 +209,7 @@ pgstat_replslot_from_serialized_name_cb(const NameData *name, PgStat_HashKey *ke
key->kind = PGSTAT_KIND_REPLSLOT;
key->dboid = InvalidOid;
key->objoid = idx;
key->objid = idx;
return true;
}

View File

@@ -429,10 +429,10 @@ pgstat_get_entry_ref_cached(PgStat_HashKey key, PgStat_EntryRef **entry_ref_p)
* if the entry is newly created, false otherwise.
*/
PgStat_EntryRef *
pgstat_get_entry_ref(PgStat_Kind kind, Oid dboid, Oid objoid, bool create,
pgstat_get_entry_ref(PgStat_Kind kind, Oid dboid, uint64 objid, bool create,
bool *created_entry)
{
PgStat_HashKey key = {.kind = kind,.dboid = dboid,.objoid = objoid};
PgStat_HashKey key = {.kind = kind,.dboid = dboid,.objid = objid};
PgStatShared_HashEntry *shhashent;
PgStatShared_Common *shheader = NULL;
PgStat_EntryRef *entry_ref;
@@ -644,13 +644,13 @@ pgstat_unlock_entry(PgStat_EntryRef *entry_ref)
* Helper function to fetch and lock shared stats.
*/
PgStat_EntryRef *
pgstat_get_entry_ref_locked(PgStat_Kind kind, Oid dboid, Oid objoid,
pgstat_get_entry_ref_locked(PgStat_Kind kind, Oid dboid, uint64 objid,
bool nowait)
{
PgStat_EntryRef *entry_ref;
/* find shared table stats entry corresponding to the local entry */
entry_ref = pgstat_get_entry_ref(kind, dboid, objoid, true, NULL);
entry_ref = pgstat_get_entry_ref(kind, dboid, objid, true, NULL);
/* lock the shared entry to protect the content, skip if failed */
if (!pgstat_lock_entry(entry_ref, nowait))
@@ -820,9 +820,10 @@ pgstat_drop_entry_internal(PgStatShared_HashEntry *shent,
*/
if (shent->dropped)
elog(ERROR,
"trying to drop stats entry already dropped: kind=%s dboid=%u objoid=%u refcount=%u",
"trying to drop stats entry already dropped: kind=%s dboid=%u objid=%llu refcount=%u",
pgstat_get_kind_info(shent->key.kind)->name,
shent->key.dboid, shent->key.objoid,
shent->key.dboid,
(unsigned long long) shent->key.objid,
pg_atomic_read_u32(&shent->refcount));
shent->dropped = true;
@@ -905,9 +906,9 @@ pgstat_drop_database_and_contents(Oid dboid)
* pgstat_gc_entry_refs().
*/
bool
pgstat_drop_entry(PgStat_Kind kind, Oid dboid, Oid objoid)
pgstat_drop_entry(PgStat_Kind kind, Oid dboid, uint64 objid)
{
PgStat_HashKey key = {.kind = kind,.dboid = dboid,.objoid = objoid};
PgStat_HashKey key = {.kind = kind,.dboid = dboid,.objid = objid};
PgStatShared_HashEntry *shent;
bool freed = true;
@@ -980,13 +981,13 @@ shared_stat_reset_contents(PgStat_Kind kind, PgStatShared_Common *header,
* Reset one variable-numbered stats entry.
*/
void
pgstat_reset_entry(PgStat_Kind kind, Oid dboid, Oid objoid, TimestampTz ts)
pgstat_reset_entry(PgStat_Kind kind, Oid dboid, uint64 objid, TimestampTz ts)
{
PgStat_EntryRef *entry_ref;
Assert(!pgstat_get_kind_info(kind)->fixed_amount);
entry_ref = pgstat_get_entry_ref(kind, dboid, objoid, false, NULL);
entry_ref = pgstat_get_entry_ref(kind, dboid, objid, false, NULL);
if (!entry_ref || entry_ref->shared_entry->dropped)
return;

View File

@@ -77,6 +77,7 @@ AtEOXact_PgStat_DroppedStats(PgStat_SubXactStatus *xact_state, bool isCommit)
PgStat_PendingDroppedStatsItem *pending =
dclist_container(PgStat_PendingDroppedStatsItem, node, iter.cur);
xl_xact_stats_item *it = &pending->item;
uint64 objid = ((uint64) it->objid_hi) << 32 | it->objid_lo;
if (isCommit && !pending->is_create)
{
@@ -84,7 +85,7 @@ AtEOXact_PgStat_DroppedStats(PgStat_SubXactStatus *xact_state, bool isCommit)
* Transaction that dropped an object committed. Drop the stats
* too.
*/
if (!pgstat_drop_entry(it->kind, it->dboid, it->objoid))
if (!pgstat_drop_entry(it->kind, it->dboid, objid))
not_freed_count++;
}
else if (!isCommit && pending->is_create)
@@ -93,7 +94,7 @@ AtEOXact_PgStat_DroppedStats(PgStat_SubXactStatus *xact_state, bool isCommit)
* Transaction that created an object aborted. Drop the stats
* associated with the object.
*/
if (!pgstat_drop_entry(it->kind, it->dboid, it->objoid))
if (!pgstat_drop_entry(it->kind, it->dboid, objid))
not_freed_count++;
}
@@ -149,6 +150,7 @@ AtEOSubXact_PgStat_DroppedStats(PgStat_SubXactStatus *xact_state,
PgStat_PendingDroppedStatsItem *pending =
dclist_container(PgStat_PendingDroppedStatsItem, node, iter.cur);
xl_xact_stats_item *it = &pending->item;
uint64 objid = ((uint64) it->objid_hi) << 32 | it->objid_lo;
dclist_delete_from(&xact_state->pending_drops, &pending->node);
@@ -158,7 +160,7 @@ AtEOSubXact_PgStat_DroppedStats(PgStat_SubXactStatus *xact_state,
* Subtransaction creating a new stats object aborted. Drop the
* stats object.
*/
if (!pgstat_drop_entry(it->kind, it->dboid, it->objoid))
if (!pgstat_drop_entry(it->kind, it->dboid, objid))
not_freed_count++;
pfree(pending);
}
@@ -319,8 +321,9 @@ pgstat_execute_transactional_drops(int ndrops, struct xl_xact_stats_item *items,
for (int i = 0; i < ndrops; i++)
{
xl_xact_stats_item *it = &items[i];
uint64 objid = ((uint64) it->objid_hi) << 32 | it->objid_lo;
if (!pgstat_drop_entry(it->kind, it->dboid, it->objoid))
if (!pgstat_drop_entry(it->kind, it->dboid, objid))
not_freed_count++;
}
@@ -329,7 +332,7 @@ pgstat_execute_transactional_drops(int ndrops, struct xl_xact_stats_item *items,
}
static void
create_drop_transactional_internal(PgStat_Kind kind, Oid dboid, Oid objoid, bool is_create)
create_drop_transactional_internal(PgStat_Kind kind, Oid dboid, uint64 objid, bool is_create)
{
int nest_level = GetCurrentTransactionNestLevel();
PgStat_SubXactStatus *xact_state;
@@ -341,7 +344,8 @@ create_drop_transactional_internal(PgStat_Kind kind, Oid dboid, Oid objoid, bool
drop->is_create = is_create;
drop->item.kind = kind;
drop->item.dboid = dboid;
drop->item.objoid = objoid;
drop->item.objid_lo = (uint32) objid;
drop->item.objid_hi = (uint32) (objid >> 32);
dclist_push_tail(&xact_state->pending_drops, &drop->node);
}
@@ -354,18 +358,19 @@ create_drop_transactional_internal(PgStat_Kind kind, Oid dboid, Oid objoid, bool
* dropped.
*/
void
pgstat_create_transactional(PgStat_Kind kind, Oid dboid, Oid objoid)
pgstat_create_transactional(PgStat_Kind kind, Oid dboid, uint64 objid)
{
if (pgstat_get_entry_ref(kind, dboid, objoid, false, NULL))
if (pgstat_get_entry_ref(kind, dboid, objid, false, NULL))
{
ereport(WARNING,
errmsg("resetting existing statistics for kind %s, db=%u, oid=%u",
(pgstat_get_kind_info(kind))->name, dboid, objoid));
errmsg("resetting existing statistics for kind %s, db=%u, oid=%llu",
(pgstat_get_kind_info(kind))->name, dboid,
(unsigned long long) objid));
pgstat_reset(kind, dboid, objoid);
pgstat_reset(kind, dboid, objid);
}
create_drop_transactional_internal(kind, dboid, objoid, /* create */ true);
create_drop_transactional_internal(kind, dboid, objid, /* create */ true);
}
/*
@@ -376,7 +381,7 @@ pgstat_create_transactional(PgStat_Kind kind, Oid dboid, Oid objoid)
* alive.
*/
void
pgstat_drop_transactional(PgStat_Kind kind, Oid dboid, Oid objoid)
pgstat_drop_transactional(PgStat_Kind kind, Oid dboid, uint64 objid)
{
create_drop_transactional_internal(kind, dboid, objoid, /* create */ false);
create_drop_transactional_internal(kind, dboid, objid, /* create */ false);
}