Skip to content

Commit 42a1de3

Browse files
Add helper functions for dshash tables with string keys.
Presently, string keys are not well-supported for dshash tables. The dshash code always copies key_size bytes into new entries' keys, and dshash.h only provides compare and hash functions that forward to memcmp() and tag_hash(), both of which do not stop at the first NUL. This means that callers must pad string keys so that the data beyond the first NUL does not adversely affect the results of copying, comparing, and hashing the keys. To better support string keys in dshash tables, this commit does a couple things: * A new copy_function field is added to the dshash_parameters struct. This function pointer specifies how the key should be copied into new table entries. For example, we only want to copy up to the first NUL byte for string keys. A dshash_memcpy() helper function is provided and used for all existing in-tree dshash tables without string keys. * A set of helper functions for string keys are provided. These helper functions forward to strcmp(), strcpy(), and string_hash(), all of which ignore data beyond the first NUL. This commit also adjusts the DSM registry's dshash table to use the new helper functions for string keys. Reviewed-by: Andy Fan Discussion: https://postgr.es/m/20240119215941.GA1322079%40nathanxps13
1 parent 5fe08c0 commit 42a1de3

File tree

6 files changed

+83
-7
lines changed

6 files changed

+83
-7
lines changed

src/backend/lib/dshash.c

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,8 @@ static bool delete_item_from_bucket(dshash_table *hash_table,
188188
static inline dshash_hash hash_key(dshash_table *hash_table, const void *key);
189189
static inline bool equal_keys(dshash_table *hash_table,
190190
const void *a, const void *b);
191+
static inline void copy_key(dshash_table *hash_table, void *dest,
192+
const void *src);
191193

192194
#define PARTITION_LOCK(hash_table, i) \
193195
(&(hash_table)->control->partitions[(i)].lock)
@@ -583,6 +585,49 @@ dshash_memhash(const void *v, size_t size, void *arg)
583585
return tag_hash(v, size);
584586
}
585587

588+
/*
589+
* A copy function that forwards to memcpy.
590+
*/
591+
void
592+
dshash_memcpy(void *dest, const void *src, size_t size, void *arg)
593+
{
594+
(void) memcpy(dest, src, size);
595+
}
596+
597+
/*
598+
* A compare function that forwards to strcmp.
599+
*/
600+
int
601+
dshash_strcmp(const void *a, const void *b, size_t size, void *arg)
602+
{
603+
Assert(strlen((const char *) a) < size);
604+
Assert(strlen((const char *) b) < size);
605+
606+
return strcmp((const char *) a, (const char *) b);
607+
}
608+
609+
/*
610+
* A hash function that forwards to string_hash.
611+
*/
612+
dshash_hash
613+
dshash_strhash(const void *v, size_t size, void *arg)
614+
{
615+
Assert(strlen((const char *) v) < size);
616+
617+
return string_hash((const char *) v, size);
618+
}
619+
620+
/*
621+
* A copy function that forwards to strcpy.
622+
*/
623+
void
624+
dshash_strcpy(void *dest, const void *src, size_t size, void *arg)
625+
{
626+
Assert(strlen((const char *) src) < size);
627+
628+
(void) strcpy((char *) dest, (const char *) src);
629+
}
630+
586631
/*
587632
* Sequentially scan through dshash table and return all the elements one by
588633
* one, return NULL when all elements have been returned.
@@ -949,7 +994,7 @@ insert_into_bucket(dshash_table *hash_table,
949994
hash_table->params.entry_size +
950995
MAXALIGN(sizeof(dshash_table_item)));
951996
item = dsa_get_address(hash_table->area, item_pointer);
952-
memcpy(ENTRY_FROM_ITEM(item), key, hash_table->params.key_size);
997+
copy_key(hash_table, ENTRY_FROM_ITEM(item), key);
953998
insert_item_into_bucket(hash_table, item_pointer, item, bucket);
954999
return item;
9551000
}
@@ -1032,3 +1077,14 @@ equal_keys(dshash_table *hash_table, const void *a, const void *b)
10321077
hash_table->params.key_size,
10331078
hash_table->arg) == 0;
10341079
}
1080+
1081+
/*
1082+
* Copy a key.
1083+
*/
1084+
static inline void
1085+
copy_key(dshash_table *hash_table, void *dest, const void *src)
1086+
{
1087+
hash_table->params.copy_function(dest, src,
1088+
hash_table->params.key_size,
1089+
hash_table->arg);
1090+
}

src/backend/replication/logical/launcher.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ static const dshash_parameters dsh_params = {
8888
sizeof(LauncherLastStartTimesEntry),
8989
dshash_memcmp,
9090
dshash_memhash,
91+
dshash_memcpy,
9192
LWTRANCHE_LAUNCHER_HASH
9293
};
9394

src/backend/storage/ipc/dsm_registry.c

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,9 @@ typedef struct DSMRegistryEntry
5050
static const dshash_parameters dsh_params = {
5151
offsetof(DSMRegistryEntry, handle),
5252
sizeof(DSMRegistryEntry),
53-
dshash_memcmp,
54-
dshash_memhash,
53+
dshash_strcmp,
54+
dshash_strhash,
55+
dshash_strcpy,
5556
LWTRANCHE_DSM_REGISTRY_HASH
5657
};
5758

@@ -132,7 +133,6 @@ GetNamedDSMSegment(const char *name, size_t size,
132133
{
133134
DSMRegistryEntry *entry;
134135
MemoryContext oldcontext;
135-
char name_padded[offsetof(DSMRegistryEntry, handle)] = {0};
136136
void *ret;
137137

138138
Assert(found);
@@ -155,8 +155,7 @@ GetNamedDSMSegment(const char *name, size_t size,
155155
/* Connect to the registry. */
156156
init_dsm_registry();
157157

158-
strcpy(name_padded, name);
159-
entry = dshash_find_or_insert(dsm_registry_table, name_padded, found);
158+
entry = dshash_find_or_insert(dsm_registry_table, name, found);
160159
if (!(*found))
161160
{
162161
/* Initialize the segment. */

src/backend/utils/activity/pgstat_shmem.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ static const dshash_parameters dsh_params = {
6464
sizeof(PgStatShared_HashEntry),
6565
pgstat_cmp_hash_key,
6666
pgstat_hash_hash_key,
67+
dshash_memcpy,
6768
LWTRANCHE_PGSTATS_HASH
6869
};
6970

src/backend/utils/cache/typcache.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,7 @@ static const dshash_parameters srtr_record_table_params = {
259259
sizeof(SharedRecordTableEntry),
260260
shared_record_table_compare,
261261
shared_record_table_hash,
262+
dshash_memcpy,
262263
LWTRANCHE_PER_SESSION_RECORD_TYPE
263264
};
264265

@@ -268,6 +269,7 @@ static const dshash_parameters srtr_typmod_table_params = {
268269
sizeof(SharedTypmodTableEntry),
269270
dshash_memcmp,
270271
dshash_memhash,
272+
dshash_memcpy,
271273
LWTRANCHE_PER_SESSION_RECORD_TYPMOD
272274
};
273275

src/include/lib/dshash.h

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ typedef int (*dshash_compare_function) (const void *a, const void *b,
3737
typedef dshash_hash (*dshash_hash_function) (const void *v, size_t size,
3838
void *arg);
3939

40+
/* A function type for copying keys. */
41+
typedef void (*dshash_copy_function) (void *dest, const void *src, size_t size,
42+
void *arg);
43+
4044
/*
4145
* The set of parameters needed to create or attach to a hash table. The
4246
* members tranche_id and tranche_name do not need to be initialized when
@@ -55,6 +59,7 @@ typedef struct dshash_parameters
5559
size_t entry_size; /* Total size of entry */
5660
dshash_compare_function compare_function; /* Compare function */
5761
dshash_hash_function hash_function; /* Hash function */
62+
dshash_copy_function copy_function; /* Copy function */
5863
int tranche_id; /* The tranche ID to use for locks */
5964
} dshash_parameters;
6065

@@ -105,9 +110,21 @@ extern void *dshash_seq_next(dshash_seq_status *status);
105110
extern void dshash_seq_term(dshash_seq_status *status);
106111
extern void dshash_delete_current(dshash_seq_status *status);
107112

108-
/* Convenience hash and compare functions wrapping memcmp and tag_hash. */
113+
/*
114+
* Convenience hash, compare, and copy functions wrapping memcmp, tag_hash, and
115+
* memcpy.
116+
*/
109117
extern int dshash_memcmp(const void *a, const void *b, size_t size, void *arg);
110118
extern dshash_hash dshash_memhash(const void *v, size_t size, void *arg);
119+
extern void dshash_memcpy(void *dest, const void *src, size_t size, void *arg);
120+
121+
/*
122+
* Convenience hash, compare, and copy functions wrapping strcmp, string_hash,
123+
* and strcpy.
124+
*/
125+
extern int dshash_strcmp(const void *a, const void *b, size_t size, void *arg);
126+
extern dshash_hash dshash_strhash(const void *v, size_t size, void *arg);
127+
extern void dshash_strcpy(void *dest, const void *src, size_t size, void *arg);
111128

112129
/* Debugging support. */
113130
extern void dshash_dump(dshash_table *hash_table);

0 commit comments

Comments
 (0)