- /* Null terminate string if it is not already. */
- if (vec_end (em->string_table)[-1] != 0)
- vec_add1 (em->string_table, 0);
+ /* String table entries MUST be NULL terminated */
+ len = vec_len (em->string_table_tmp);
+ ASSERT (len > 0);
+ if (em->string_table_tmp[len - 1] != 0)
+ vec_add1 (em->string_table_tmp, 0);
+
+ /* See if we already have this string in the string table */
+ p = hash_get_mem (em->string_table_hash, em->string_table_tmp);
+
+ /* We already have the string, so give the caller its offset */
+ if (p)
+ {
+ elog_unlock (em);
+ return (p[0]);
+ }
+
+ /* We don't, so add it. */
+
+ offset = vec_len (em->string_table);
+ vec_append (em->string_table, em->string_table_tmp);
+
+ hash_set_mem (em->string_table_hash, em->string_table_tmp, offset);
+
+ /* We gave the key to the string table hash, so we can't reuse it! */
+ em->string_table_tmp = 0;
+ elog_unlock (em);