diff --git a/src/diskquota.c b/src/diskquota.c index aa706f40..00faba4b 100644 --- a/src/diskquota.c +++ b/src/diskquota.c @@ -91,7 +91,7 @@ static DiskQuotaWorkerEntry *volatile MyWorkerInfo = NULL; // how many database diskquota are monitoring on static int num_db = 0; -static DiskquotaLauncherShmemStruct *DiskquotaLauncherShmem; +DiskquotaLauncherShmemStruct *DiskquotaLauncherShmem = NULL; #define MIN_SLEEPTIME 100 /* milliseconds */ #define BGWORKER_LOG_TIME 3600000 /* milliseconds */ @@ -1130,7 +1130,7 @@ process_extension_ddl_message() ExtensionDDLMessage local_extension_ddl_message; LWLockAcquire(diskquota_locks.extension_ddl_message_lock, LW_SHARED); - memcpy(&local_extension_ddl_message, extension_ddl_message, sizeof(ExtensionDDLMessage)); + memcpy(&local_extension_ddl_message, extension_ddl_message, EXTENSION_DDL_MESSAGE_SIZE); LWLockRelease(diskquota_locks.extension_ddl_message_lock); /* create/drop extension message must be valid */ @@ -1146,7 +1146,7 @@ process_extension_ddl_message() /* Send createdrop extension diskquota result back to QD */ LWLockAcquire(diskquota_locks.extension_ddl_message_lock, LW_EXCLUSIVE); - memset(extension_ddl_message, 0, sizeof(ExtensionDDLMessage)); + memset(extension_ddl_message, 0, EXTENSION_DDL_MESSAGE_SIZE); extension_ddl_message->launcher_pid = MyProcPid; extension_ddl_message->result = (int)code; LWLockRelease(diskquota_locks.extension_ddl_message_lock); @@ -1732,8 +1732,8 @@ void init_launcher_shmem() { bool found; - DiskquotaLauncherShmem = (DiskquotaLauncherShmemStruct *)ShmemInitStruct("Diskquota launcher Data", - diskquota_launcher_shmem_size(), &found); + DiskquotaLauncherShmem = (DiskquotaLauncherShmemStruct *)DiskquotaShmemInitStruct( + "Diskquota launcher Data", diskquota_launcher_shmem_size(), &found); memset(DiskquotaLauncherShmem, 0, diskquota_launcher_shmem_size()); if (!found) { diff --git a/src/diskquota.h b/src/diskquota.h index c46adb21..a726d8cf 100644 --- a/src/diskquota.h +++ b/src/diskquota.h @@ -75,6 +75,13 @@ extern int diskquota_worker_timeout; #define DiskquotaGetRelstorage(classForm) (0) #endif /* GP_VERSION_NUM */ +#define EXTENSION_DDL_MESSAGE_SIZE sizeof(ExtensionDDLMessage) +#define ACTIVE_TABLES_MAP_ENTRY_SIZE sizeof(DiskQuotaActiveTableFileEntry) +#define RELATION_CACHE_ENTRY_SIZE sizeof(DiskQuotaRelationCacheEntry) +#define RELID_CACHE_ENTRY_SIZE sizeof(DiskQuotaRelidCacheEntry) +#define ALTERED_RELOID_CACHE_ENTRY_SIZE sizeof(Oid) +#define MONITORED_DBID_CACHE_ENTRY_SIZE sizeof(struct MonitorDBEntryStruct) + typedef enum { NAMESPACE_QUOTA = 0, @@ -316,6 +323,7 @@ extern HTAB *diskquota_hash_create(const char *tabname, long nelem, HASHC DiskquotaHashFunction hashFunction); extern HTAB *DiskquotaShmemInitHash(const char *name, long init_size, long max_size, HASHCTL *infoP, int hash_flags, DiskquotaHashFunction hash_function); +extern void *DiskquotaShmemInitStruct(const char *name, Size size, bool *foundPtr); extern void refresh_monitored_dbid_cache(void); extern HASHACTION check_hash_fullness(HTAB *hashp, int max_size, const char *warning_message, TimestampTz *last_overflow_report); diff --git a/src/diskquota_utility.c b/src/diskquota_utility.c index 43d0b0a1..514bbfa0 100644 --- a/src/diskquota_utility.c +++ b/src/diskquota_utility.c @@ -114,6 +114,11 @@ static float4 get_per_segment_ratio(Oid spcoid); static bool to_delete_quota(QuotaType type, int64 quota_limit_mb, float4 segratio); static void check_role(Oid roleoid, char *rolname, int64 quota_limit_mb); +#ifdef USE_ASSERT_CHECKING +extern DiskquotaLauncherShmemStruct *DiskquotaLauncherShmem; +extern void diskquota_shmem_size_sub(Size size); +#endif + /* ---- Help Functions to set quota limit. ---- */ /* * Initialize table diskquota.table_size. @@ -1642,6 +1647,11 @@ DiskquotaShmemInitHash(const char *name, /* table string name fo int hash_flags, /* info about infoP */ DiskquotaHashFunction hashFunction) { +#ifdef USE_ASSERT_CHECKING + if (!DiskquotaLauncherShmem || !DiskquotaLauncherShmem->isDynamicWorker) + diskquota_shmem_size_sub(hash_estimate_size(max_size, infoP->entrysize)); +#endif + #if GP_VERSION_NUM < 70000 if (hashFunction == DISKQUOTA_TAG_HASH) infoP->hash = tag_hash; @@ -1655,6 +1665,16 @@ DiskquotaShmemInitHash(const char *name, /* table string name fo #endif /* GP_VERSION_NUM */ } +void * +DiskquotaShmemInitStruct(const char *name, Size size, bool *foundPtr) +{ +#ifdef USE_ASSERT_CHECKING + if (!DiskquotaLauncherShmem || !DiskquotaLauncherShmem->isDynamicWorker) diskquota_shmem_size_sub(size); +#endif + + return ShmemInitStruct(name, size, foundPtr); +} + /* * Returns HASH_FIND if hash table is full and HASH_ENTER otherwise. * It can be used only under lock. diff --git a/src/gp_activetable.c b/src/gp_activetable.c index bf34e2ae..dba31b30 100644 --- a/src/gp_activetable.c +++ b/src/gp_activetable.c @@ -109,14 +109,14 @@ init_shm_worker_active_tables(void) HASHCTL ctl; memset(&ctl, 0, sizeof(ctl)); - ctl.keysize = sizeof(DiskQuotaActiveTableFileEntry); - ctl.entrysize = sizeof(DiskQuotaActiveTableFileEntry); + ctl.keysize = ACTIVE_TABLES_MAP_ENTRY_SIZE; + ctl.entrysize = ACTIVE_TABLES_MAP_ENTRY_SIZE; active_tables_map = DiskquotaShmemInitHash("active_tables", diskquota_max_active_tables, diskquota_max_active_tables, &ctl, HASH_ELEM, DISKQUOTA_TAG_HASH); memset(&ctl, 0, sizeof(ctl)); - ctl.keysize = sizeof(Oid); - ctl.entrysize = sizeof(Oid); + ctl.keysize = ALTERED_RELOID_CACHE_ENTRY_SIZE; + ctl.entrysize = ALTERED_RELOID_CACHE_ENTRY_SIZE; altered_reloid_cache = DiskquotaShmemInitHash("altered_reloid_cache", diskquota_max_active_tables, diskquota_max_active_tables, &ctl, HASH_ELEM, DISKQUOTA_OID_HASH); } @@ -322,7 +322,7 @@ report_active_table_helper(const RelFileNodeBackend *relFileNode) } found = false; - MemSet(&item, 0, sizeof(DiskQuotaActiveTableFileEntry)); + MemSet(&item, 0, ACTIVE_TABLES_MAP_ENTRY_SIZE); item.dbid = relFileNode->node.dbNode; item.relfilenode = relFileNode->node.relNode; item.tablespaceoid = relFileNode->node.spcNode; @@ -730,15 +730,15 @@ get_active_tables_oid(void) refresh_monitored_dbid_cache(); memset(&ctl, 0, sizeof(ctl)); - ctl.keysize = sizeof(DiskQuotaActiveTableFileEntry); - ctl.entrysize = sizeof(DiskQuotaActiveTableFileEntry); + ctl.keysize = ACTIVE_TABLES_MAP_ENTRY_SIZE; + ctl.entrysize = ACTIVE_TABLES_MAP_ENTRY_SIZE; ctl.hcxt = CurrentMemoryContext; local_active_table_file_map = diskquota_hash_create("local active table map with relfilenode info", 1024, &ctl, HASH_ELEM | HASH_CONTEXT, DISKQUOTA_TAG_HASH); memset(&ctl, 0, sizeof(ctl)); - ctl.keysize = sizeof(Oid); - ctl.entrysize = sizeof(Oid); + ctl.keysize = ALTERED_RELOID_CACHE_ENTRY_SIZE; + ctl.entrysize = ALTERED_RELOID_CACHE_ENTRY_SIZE; ctl.hcxt = CurrentMemoryContext; local_altered_reloid_cache = diskquota_hash_create("local_altered_reloid_cache", 1024, &ctl, HASH_ELEM | HASH_CONTEXT, DISKQUOTA_OID_HASH); diff --git a/src/monitored_db.c b/src/monitored_db.c index c2b29a1b..93049ffb 100644 --- a/src/monitored_db.c +++ b/src/monitored_db.c @@ -322,14 +322,14 @@ dump_monitored_dbid_cache(long *nitems) HASH_SEQ_STATUS seq; MonitorDBEntry curEntry; int count = *nitems = hash_get_num_entries(monitored_dbid_cache); - MonitorDBEntry entries = curEntry = (MonitorDBEntry)palloc(sizeof(struct MonitorDBEntryStruct) * count); + MonitorDBEntry entries = curEntry = (MonitorDBEntry)palloc(MONITORED_DBID_CACHE_ENTRY_SIZE * count); hash_seq_init(&seq, monitored_dbid_cache); MonitorDBEntry entry; while ((entry = hash_seq_search(&seq)) != NULL) { Assert(count > 0); - memcpy(curEntry, entry, sizeof(struct MonitorDBEntryStruct)); + memcpy(curEntry, entry, MONITORED_DBID_CACHE_ENTRY_SIZE); curEntry++; count--; } diff --git a/src/quotamodel.c b/src/quotamodel.c index ba0ab3ac..740e99c1 100644 --- a/src/quotamodel.c +++ b/src/quotamodel.c @@ -71,6 +71,14 @@ ? ((entry->key.id + 1) * SEGMENT_SIZE_ARRAY_LENGTH - 1) \ : SEGCOUNT) +#define TABLE_SIZE_MAP_LAST_OVERFLOW_REPORT_SIZE sizeof(TimestampTz) +#define LOCAL_DISK_QUOTA_REJECT_MAP_LAST_OVERFLOW_REPORT_SIZE sizeof(TimestampTz) +#define QUOTA_INFO_MAP_LAST_OVERFLOW_REPORT_SIZE sizeof(TimestampTz) +#define DISK_QUOTA_REJECT_MAP_ENTRY_SIZE sizeof(GlobalRejectMapEntry) +#define TABLE_SIZE_MAP_ENTRY_SIZE sizeof(TableSizeEntry) +#define LOCAL_DISK_QUOTA_REJECT_MAP_ENTRY_SIZE sizeof(LocalRejectMapEntry) +#define QUOTA_INFO_MAP_ENTRY_SIZE sizeof(QuotaInfoEntry) + typedef struct TableSizeEntry TableSizeEntry; typedef struct NamespaceSizeEntry NamespaceSizeEntry; typedef struct RoleSizeEntry RoleSizeEntry; @@ -210,6 +218,11 @@ static const char *local_disk_quota_reject_map_warning = static shmem_startup_hook_type prev_shmem_startup_hook = NULL; +#ifdef USE_ASSERT_CHECKING +static pg_atomic_uint64 *diskquota_shmem_size; +void diskquota_shmem_size_sub(Size size); +#endif + /* functions to maintain the quota maps */ static void update_size_for_quota(int64 size, QuotaType type, Oid *keys, int16 segid); static void update_limit_for_quota(int64 limit, float segratio, QuotaType type, Oid *keys); @@ -239,6 +252,8 @@ static bool get_table_size_entry_flag(TableSizeEntry *entry, TableSizeEntryFlag static void reset_table_size_entry_flag(TableSizeEntry *entry, TableSizeEntryFlag flag); static void set_table_size_entry_flag(TableSizeEntry *entry, TableSizeEntryFlag flag); +static Size diskquota_worker_shmem_size(void); + typedef struct { ArrayBuildState *tableids; @@ -441,6 +456,15 @@ disk_quota_shmem_startup(void) LWLockAcquire(AddinShmemInitLock, LW_EXCLUSIVE); +#ifdef USE_ASSERT_CHECKING + diskquota_shmem_size = ShmemInitStruct("diskquota_shmem_size", sizeof(pg_atomic_uint64), &found); + if (!found) + { + pg_atomic_init_u64(diskquota_shmem_size, DiskQuotaShmemSize()); + diskquota_shmem_size_sub(sizeof(pg_atomic_uint64)); // diskquota_shmem_size + } +#endif + init_lwlocks(); /* @@ -449,12 +473,13 @@ disk_quota_shmem_startup(void) * to store out-of-quota rejectmap. active_tables_map is used to store * active tables whose disk usage is changed. */ - extension_ddl_message = ShmemInitStruct("disk_quota_extension_ddl_message", sizeof(ExtensionDDLMessage), &found); - if (!found) memset((void *)extension_ddl_message, 0, sizeof(ExtensionDDLMessage)); + extension_ddl_message = + DiskquotaShmemInitStruct("disk_quota_extension_ddl_message", EXTENSION_DDL_MESSAGE_SIZE, &found); + if (!found) memset((void *)extension_ddl_message, 0, EXTENSION_DDL_MESSAGE_SIZE); memset(&hash_ctl, 0, sizeof(hash_ctl)); hash_ctl.keysize = sizeof(RejectMapEntry); - hash_ctl.entrysize = sizeof(GlobalRejectMapEntry); + hash_ctl.entrysize = DISK_QUOTA_REJECT_MAP_ENTRY_SIZE; disk_quota_reject_map = DiskquotaShmemInitHash("rejectmap whose quota limitation is reached", diskquota_max_local_reject_entries, MAX_DISK_QUOTA_REJECT_ENTRIES, &hash_ctl, HASH_ELEM, DISKQUOTA_TAG_HASH); @@ -465,13 +490,21 @@ disk_quota_shmem_startup(void) memset(&hash_ctl, 0, sizeof(hash_ctl)); hash_ctl.keysize = sizeof(Oid); - hash_ctl.entrysize = sizeof(struct MonitorDBEntryStruct); + hash_ctl.entrysize = MONITORED_DBID_CACHE_ENTRY_SIZE; monitored_dbid_cache = DiskquotaShmemInitHash("table oid cache which shoud tracking", diskquota_max_monitored_databases, diskquota_max_monitored_databases, &hash_ctl, HASH_ELEM, DISKQUOTA_OID_HASH); - init_launcher_shmem(); + if (IS_QUERY_DISPATCHER()) init_launcher_shmem(); LWLockRelease(AddinShmemInitLock); + +#ifdef USE_ASSERT_CHECKING + if (IS_QUERY_DISPATCHER()) + Assert(pg_atomic_read_u64(diskquota_shmem_size) == + diskquota_worker_shmem_size() * diskquota_max_monitored_databases); + else + Assert(pg_atomic_read_u64(diskquota_shmem_size) == 0); +#endif } /* @@ -512,16 +545,16 @@ init_lwlocks(void) } static Size -diskquota_worker_shmem_size() +diskquota_worker_shmem_size(void) { Size size; - size = hash_estimate_size(MAX_NUM_TABLE_SIZE_ENTRIES / diskquota_max_monitored_databases + 100, - sizeof(TableSizeEntry)); - size = add_size(size, hash_estimate_size(diskquota_max_local_reject_entries, sizeof(LocalRejectMapEntry))); - size = add_size(size, hash_estimate_size(MAX_QUOTA_MAP_ENTRIES, sizeof(QuotaInfoEntry))); - size = add_size(size, sizeof(TimestampTz)); // table_size_map_last_overflow_report - size = add_size(size, sizeof(TimestampTz)); // local_disk_quota_reject_map_last_overflow_report - size = add_size(size, sizeof(TimestampTz)); // quota_info_map_last_overflow_report + size = hash_estimate_size(MAX_NUM_TABLE_SIZE_ENTRIES, TABLE_SIZE_MAP_ENTRY_SIZE); + size = add_size(size, + hash_estimate_size(diskquota_max_local_reject_entries, LOCAL_DISK_QUOTA_REJECT_MAP_ENTRY_SIZE)); + size = add_size(size, hash_estimate_size(MAX_QUOTA_MAP_ENTRIES, QUOTA_INFO_MAP_ENTRY_SIZE)); + size = add_size(size, TABLE_SIZE_MAP_LAST_OVERFLOW_REPORT_SIZE); + size = add_size(size, LOCAL_DISK_QUOTA_REJECT_MAP_LAST_OVERFLOW_REPORT_SIZE); + size = add_size(size, QUOTA_INFO_MAP_LAST_OVERFLOW_REPORT_SIZE); return size; } @@ -533,18 +566,22 @@ static Size DiskQuotaShmemSize(void) { Size size; - size = sizeof(ExtensionDDLMessage); - size = add_size(size, hash_estimate_size(MAX_DISK_QUOTA_REJECT_ENTRIES, sizeof(GlobalRejectMapEntry))); - size = add_size(size, hash_estimate_size(diskquota_max_active_tables, sizeof(DiskQuotaActiveTableEntry))); - size = add_size(size, hash_estimate_size(diskquota_max_active_tables, sizeof(DiskQuotaRelationCacheEntry))); - size = add_size(size, hash_estimate_size(diskquota_max_active_tables, sizeof(DiskQuotaRelidCacheEntry))); - size = add_size(size, hash_estimate_size(diskquota_max_active_tables, sizeof(Oid))); - size = add_size(size, hash_estimate_size(diskquota_max_monitored_databases, - sizeof(struct MonitorDBEntryStruct))); // monitored_dbid_cache + size = EXTENSION_DDL_MESSAGE_SIZE; + +#ifdef USE_ASSERT_CHECKING + size = add_size(size, sizeof(pg_atomic_uint64)); // diskquota_shmem_size +#endif + + size = add_size(size, hash_estimate_size(MAX_DISK_QUOTA_REJECT_ENTRIES, DISK_QUOTA_REJECT_MAP_ENTRY_SIZE)); + size = add_size(size, hash_estimate_size(diskquota_max_active_tables, ACTIVE_TABLES_MAP_ENTRY_SIZE)); + size = add_size(size, hash_estimate_size(diskquota_max_active_tables, RELATION_CACHE_ENTRY_SIZE)); + size = add_size(size, hash_estimate_size(diskquota_max_active_tables, RELID_CACHE_ENTRY_SIZE)); + size = add_size(size, hash_estimate_size(diskquota_max_active_tables, ALTERED_RELOID_CACHE_ENTRY_SIZE)); + size = add_size(size, hash_estimate_size(diskquota_max_monitored_databases, MONITORED_DBID_CACHE_ENTRY_SIZE)); if (IS_QUERY_DISPATCHER()) { - size = add_size(size, diskquota_launcher_shmem_size()); + size = add_size(size, diskquota_launcher_shmem_size()); // DiskquotaLauncherShmem size = add_size(size, diskquota_worker_shmem_size() * diskquota_max_monitored_databases); } @@ -566,11 +603,12 @@ init_disk_quota_model(uint32 id) format_name("TableSizeEntrymap", id, &str); memset(&hash_ctl, 0, sizeof(hash_ctl)); hash_ctl.keysize = sizeof(TableSizeEntryKey); - hash_ctl.entrysize = sizeof(TableSizeEntry); + hash_ctl.entrysize = TABLE_SIZE_MAP_ENTRY_SIZE; table_size_map = DiskquotaShmemInitHash(str.data, INIT_NUM_TABLE_SIZE_ENTRIES, MAX_NUM_TABLE_SIZE_ENTRIES, &hash_ctl, HASH_ELEM, DISKQUOTA_TAG_HASH); format_name("TableSizeEntrymap_last_overflow_report", id, &str); - table_size_map_last_overflow_report = ShmemInitStruct(str.data, sizeof(TimestampTz), &found); + table_size_map_last_overflow_report = + DiskquotaShmemInitStruct(str.data, TABLE_SIZE_MAP_LAST_OVERFLOW_REPORT_SIZE, &found); if (!found) *table_size_map_last_overflow_report = 0; /* for localrejectmap */ @@ -578,24 +616,26 @@ init_disk_quota_model(uint32 id) format_name("localrejectmap", id, &str); memset(&hash_ctl, 0, sizeof(hash_ctl)); hash_ctl.keysize = sizeof(RejectMapEntry); - hash_ctl.entrysize = sizeof(LocalRejectMapEntry); + hash_ctl.entrysize = LOCAL_DISK_QUOTA_REJECT_MAP_ENTRY_SIZE; local_disk_quota_reject_map = DiskquotaShmemInitHash(str.data, diskquota_max_local_reject_entries, diskquota_max_local_reject_entries, &hash_ctl, HASH_ELEM, DISKQUOTA_TAG_HASH); format_name("localrejectmap_last_overflow_report", id, &str); - local_disk_quota_reject_map_last_overflow_report = ShmemInitStruct(str.data, sizeof(TimestampTz), &found); + local_disk_quota_reject_map_last_overflow_report = + DiskquotaShmemInitStruct(str.data, LOCAL_DISK_QUOTA_REJECT_MAP_LAST_OVERFLOW_REPORT_SIZE, &found); if (!found) *local_disk_quota_reject_map_last_overflow_report = 0; /* for quota_info_map */ format_name("QuotaInfoMap", id, &str); memset(&hash_ctl, 0, sizeof(hash_ctl)); - hash_ctl.entrysize = sizeof(QuotaInfoEntry); + hash_ctl.entrysize = QUOTA_INFO_MAP_ENTRY_SIZE; hash_ctl.keysize = sizeof(QuotaInfoEntryKey); quota_info_map = DiskquotaShmemInitHash(str.data, INIT_QUOTA_MAP_ENTRIES, MAX_QUOTA_MAP_ENTRIES, &hash_ctl, HASH_ELEM, DISKQUOTA_TAG_HASH); format_name("QuotaInfoMap_last_overflow_report", id, &str); - quota_info_map_last_overflow_report = ShmemInitStruct(str.data, sizeof(TimestampTz), &found); + quota_info_map_last_overflow_report = + DiskquotaShmemInitStruct(str.data, QUOTA_INFO_MAP_LAST_OVERFLOW_REPORT_SIZE, &found); if (!found) *quota_info_map_last_overflow_report = 0; pfree(str.data); @@ -630,7 +670,7 @@ vacuum_disk_quota_model(uint32 id) format_name("TableSizeEntrymap", id, &str); memset(&hash_ctl, 0, sizeof(hash_ctl)); hash_ctl.keysize = sizeof(TableSizeEntryKey); - hash_ctl.entrysize = sizeof(TableSizeEntry); + hash_ctl.entrysize = TABLE_SIZE_MAP_ENTRY_SIZE; table_size_map = DiskquotaShmemInitHash(str.data, INIT_NUM_TABLE_SIZE_ENTRIES, MAX_NUM_TABLE_SIZE_ENTRIES, &hash_ctl, HASH_ELEM, DISKQUOTA_TAG_HASH); hash_seq_init(&iter, table_size_map); @@ -640,13 +680,15 @@ vacuum_disk_quota_model(uint32 id) } format_name("TableSizeEntrymap_last_overflow_report", id, &str); - table_size_map_last_overflow_report = ShmemInitStruct(str.data, sizeof(TimestampTz), &found); + table_size_map_last_overflow_report = + DiskquotaShmemInitStruct(str.data, TABLE_SIZE_MAP_LAST_OVERFLOW_REPORT_SIZE, &found); if (!found) *table_size_map_last_overflow_report = 0; + /* localrejectmap */ format_name("localrejectmap", id, &str); memset(&hash_ctl, 0, sizeof(hash_ctl)); hash_ctl.keysize = sizeof(RejectMapEntry); - hash_ctl.entrysize = sizeof(LocalRejectMapEntry); + hash_ctl.entrysize = LOCAL_DISK_QUOTA_REJECT_MAP_ENTRY_SIZE; local_disk_quota_reject_map = DiskquotaShmemInitHash(str.data, diskquota_max_local_reject_entries, diskquota_max_local_reject_entries, &hash_ctl, HASH_ELEM, DISKQUOTA_TAG_HASH); @@ -656,13 +698,14 @@ vacuum_disk_quota_model(uint32 id) hash_search(local_disk_quota_reject_map, &localrejectentry->keyitem, HASH_REMOVE, NULL); } format_name("localrejectmap_last_overflow_report", id, &str); - local_disk_quota_reject_map_last_overflow_report = ShmemInitStruct(str.data, sizeof(TimestampTz), &found); + local_disk_quota_reject_map_last_overflow_report = + DiskquotaShmemInitStruct(str.data, LOCAL_DISK_QUOTA_REJECT_MAP_LAST_OVERFLOW_REPORT_SIZE, &found); if (!found) *local_disk_quota_reject_map_last_overflow_report = 0; /* quota_info_map */ format_name("QuotaInfoMap", id, &str); memset(&hash_ctl, 0, sizeof(hash_ctl)); - hash_ctl.entrysize = sizeof(QuotaInfoEntry); + hash_ctl.entrysize = QUOTA_INFO_MAP_ENTRY_SIZE; hash_ctl.keysize = sizeof(QuotaInfoEntryKey); quota_info_map = DiskquotaShmemInitHash(str.data, INIT_QUOTA_MAP_ENTRIES, MAX_QUOTA_MAP_ENTRIES, &hash_ctl, HASH_ELEM, DISKQUOTA_TAG_HASH); @@ -672,7 +715,8 @@ vacuum_disk_quota_model(uint32 id) hash_search(quota_info_map, &qentry->key, HASH_REMOVE, NULL); } format_name("QuotaInfoMap_last_overflow_report", id, &str); - quota_info_map_last_overflow_report = ShmemInitStruct(str.data, sizeof(TimestampTz), &found); + quota_info_map_last_overflow_report = + DiskquotaShmemInitStruct(str.data, QUOTA_INFO_MAP_LAST_OVERFLOW_REPORT_SIZE, &found); if (!found) *quota_info_map_last_overflow_report = 0; pfree(str.data); @@ -1919,7 +1963,7 @@ refresh_rejectmap(PG_FUNCTION_ARGS) */ memset(&hashctl, 0, sizeof(hashctl)); hashctl.keysize = sizeof(RejectMapEntry); - hashctl.entrysize = sizeof(GlobalRejectMapEntry); + hashctl.entrysize = DISK_QUOTA_REJECT_MAP_ENTRY_SIZE; hashctl.hcxt = CurrentMemoryContext; /* @@ -2169,7 +2213,7 @@ refresh_rejectmap(PG_FUNCTION_ARGS) check_hash_fullness(disk_quota_reject_map, MAX_DISK_QUOTA_REJECT_ENTRIES, disk_quota_reject_map_warning, &disk_quota_reject_map_last_overflow_report); new_entry = hash_search(disk_quota_reject_map, &rejectmapentry->keyitem, action, &found); - if (!found && new_entry) memcpy(new_entry, rejectmapentry, sizeof(GlobalRejectMapEntry)); + if (!found && new_entry) memcpy(new_entry, rejectmapentry, DISK_QUOTA_REJECT_MAP_ENTRY_SIZE); } LWLockRelease(diskquota_locks.reject_map_lock); @@ -2223,7 +2267,7 @@ show_rejectmap(PG_FUNCTION_ARGS) /* Create a local hash table and fill it with entries from shared memory. */ memset(&hashctl, 0, sizeof(hashctl)); hashctl.keysize = sizeof(RejectMapEntry); - hashctl.entrysize = sizeof(GlobalRejectMapEntry); + hashctl.entrysize = DISK_QUOTA_REJECT_MAP_ENTRY_SIZE; hashctl.hcxt = CurrentMemoryContext; rejectmap_ctx->rejectmap = diskquota_hash_create("rejectmap_ctx rejectmap", 1024, &hashctl, HASH_ELEM | HASH_CONTEXT, DISKQUOTA_TAG_HASH); @@ -2361,3 +2405,13 @@ set_table_size_entry_flag(TableSizeEntry *entry, TableSizeEntryFlag flag) { entry->flag |= flag; } + +#ifdef USE_ASSERT_CHECKING +void +diskquota_shmem_size_sub(Size size) +{ + Assert(pg_atomic_read_u64(diskquota_shmem_size) >= size); + + pg_atomic_sub_fetch_u64(diskquota_shmem_size, size); +} +#endif diff --git a/src/relation_cache.c b/src/relation_cache.c index b5624c42..f8af8429 100644 --- a/src/relation_cache.c +++ b/src/relation_cache.c @@ -54,13 +54,13 @@ init_shm_worker_relation_cache(void) memset(&ctl, 0, sizeof(ctl)); ctl.keysize = sizeof(Oid); - ctl.entrysize = sizeof(DiskQuotaRelationCacheEntry); + ctl.entrysize = RELATION_CACHE_ENTRY_SIZE; relation_cache = DiskquotaShmemInitHash("relation_cache", diskquota_max_active_tables, diskquota_max_active_tables, &ctl, HASH_ELEM, DISKQUOTA_OID_HASH); memset(&ctl, 0, sizeof(ctl)); ctl.keysize = sizeof(Oid); - ctl.entrysize = sizeof(DiskQuotaRelidCacheEntry); + ctl.entrysize = RELID_CACHE_ENTRY_SIZE; relid_cache = DiskquotaShmemInitHash("relid_cache", diskquota_max_active_tables, diskquota_max_active_tables, &ctl, HASH_ELEM, DISKQUOTA_OID_HASH); } @@ -198,7 +198,7 @@ update_relation_cache(Oid relid) LWLockRelease(diskquota_locks.relation_cache_lock); return; } - memcpy(relation_entry, &relation_entry_data, sizeof(DiskQuotaRelationCacheEntry)); + memcpy(relation_entry, &relation_entry_data, RELATION_CACHE_ENTRY_SIZE); action = check_hash_fullness(relid_cache, diskquota_max_active_tables, relid_cache_warning, &active_tables_map_last_overflow_report); @@ -208,7 +208,7 @@ update_relation_cache(Oid relid) LWLockRelease(diskquota_locks.relation_cache_lock); return; } - memcpy(relid_entry, &relid_entry_data, sizeof(DiskQuotaRelidCacheEntry)); + memcpy(relid_entry, &relid_entry_data, RELID_CACHE_ENTRY_SIZE); LWLockRelease(diskquota_locks.relation_cache_lock); prelid = get_primary_table_oid(relid, false); @@ -302,7 +302,7 @@ remove_committed_relation_from_cache(void) memset(&ctl, 0, sizeof(ctl)); ctl.keysize = sizeof(Oid); - ctl.entrysize = sizeof(DiskQuotaRelationCacheEntry); + ctl.entrysize = RELATION_CACHE_ENTRY_SIZE; ctl.hcxt = CurrentMemoryContext; local_relation_cache = diskquota_hash_create("local relation cache", 1024, &ctl, HASH_ELEM | HASH_CONTEXT, DISKQUOTA_OID_HASH); @@ -314,7 +314,7 @@ remove_committed_relation_from_cache(void) /* The session of db1 should not see the table inside db2. */ if (entry->rnode.node.dbNode != MyDatabaseId) continue; local_entry = hash_search(local_relation_cache, &entry->relid, HASH_ENTER, NULL); - memcpy(local_entry, entry, sizeof(DiskQuotaRelationCacheEntry)); + memcpy(local_entry, entry, RELATION_CACHE_ENTRY_SIZE); } LWLockRelease(diskquota_locks.relation_cache_lock); @@ -380,7 +380,7 @@ show_relation_cache(PG_FUNCTION_ARGS) /* Create a local hash table and fill it with entries from shared memory. */ memset(&hashctl, 0, sizeof(hashctl)); hashctl.keysize = sizeof(Oid); - hashctl.entrysize = sizeof(DiskQuotaRelationCacheEntry); + hashctl.entrysize = RELATION_CACHE_ENTRY_SIZE; hashctl.hcxt = CurrentMemoryContext; relation_cache_ctx->relation_cache = diskquota_hash_create("relation_cache_ctx->relation_cache", 1024, &hashctl, @@ -396,7 +396,7 @@ show_relation_cache(PG_FUNCTION_ARGS) hash_search(relation_cache_ctx->relation_cache, &entry->relid, HASH_ENTER_NULL, NULL); if (local_entry) { - memcpy(local_entry, entry, sizeof(DiskQuotaRelationCacheEntry)); + memcpy(local_entry, entry, RELATION_CACHE_ENTRY_SIZE); } } LWLockRelease(diskquota_locks.relation_cache_lock); @@ -559,7 +559,7 @@ get_relation_entry(Oid relid, DiskQuotaRelationCacheEntry *entry) tentry = hash_search(relation_cache, &relid, HASH_FIND, NULL); if (tentry) { - memcpy(entry, tentry, sizeof(DiskQuotaRelationCacheEntry)); + memcpy(entry, tentry, RELATION_CACHE_ENTRY_SIZE); LWLockRelease(diskquota_locks.relation_cache_lock); return; }