Skip to content

Commit a5b9998

Browse files
committed
Add unit_reset() function
1 parent 33f7d0b commit a5b9998

File tree

6 files changed

+63
-1
lines changed

6 files changed

+63
-1
lines changed

README.md

+9-1
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,8 @@ steps are repeated with the trailing 's' removed.
148148

149149
If the unit definition could be resolved, the result is stored in a
150150
backend-local hash table to speed up the next lookup. (The function
151-
`unit_is_hashed()` reports if a given unit name is already cached.)
151+
`unit_is_hashed()` reports if a given unit name is already cached. The function
152+
`unit_reset()` clears the hash table. Use it if unit definitions are changed.)
152153

153154
The `definition` column is only provided for information on how the unit was
154155
originally defined.
@@ -187,6 +188,11 @@ INSERT 0 1
187188
104.166666666667 legobricks
188189
```
189190

191+
*Note: If user-defined (or built-in) units are changed by updating the tables,
192+
call `unit_reset()` to clear the hash table that caches the lookup result.
193+
Otherwise, sessions that have already used the unit will continue the old
194+
definition.*
195+
190196
Input Syntax
191197
------------
192198

@@ -244,6 +250,8 @@ function stddev_pop(unit)
244250
function stddev_samp(unit)
245251
function stddev(unit)
246252
function sum(unit)
253+
function unit_is_hashed(cstring)
254+
function unit_reset()
247255
function value(unit)
248256
function variance(unit)
249257
function var_pop(unit)

expected/custom.out

+25
Original file line numberDiff line numberDiff line change
@@ -52,3 +52,28 @@ SELECT * FROM unit_units WHERE dump;
5252
legobrick | 9.6 mm | | | t
5353
(1 row)
5454

55+
UPDATE unit_units SET unit = '19.1 mm' WHERE name = 'legobrick'; -- Duplo size
56+
SELECT '2 legobricks'::unit AS old_size;
57+
old_size
58+
----------
59+
19.2 mm
60+
(1 row)
61+
62+
SELECT unit_reset();
63+
unit_reset
64+
------------
65+
66+
(1 row)
67+
68+
SELECT unit_is_hashed('legobricks');
69+
unit_is_hashed
70+
----------------
71+
f
72+
(1 row)
73+
74+
SELECT '2 legobricks'::unit AS new_size;
75+
new_size
76+
----------
77+
38.2 mm
78+
(1 row)
79+

sql/custom.sql

+6
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,9 @@ SELECT unit_is_hashed('legobricks');
1010
SELECT '1 m'::unit @ 'legobricks' AS one_meter;
1111
SELECT unit_is_hashed('legobricks');
1212
SELECT * FROM unit_units WHERE dump;
13+
14+
UPDATE unit_units SET unit = '19.1 mm' WHERE name = 'legobrick'; -- Duplo size
15+
SELECT '2 legobricks'::unit AS old_size;
16+
SELECT unit_reset();
17+
SELECT unit_is_hashed('legobricks');
18+
SELECT '2 legobricks'::unit AS new_size;

unit--2--3.sql.in

+5
Original file line numberDiff line numberDiff line change
@@ -59,3 +59,8 @@ CREATE FUNCTION unit_is_hashed(cstring)
5959
RETURNS bool
6060
AS '$libdir/unit'
6161
LANGUAGE C VOLATILE STRICT;
62+
63+
CREATE FUNCTION unit_reset()
64+
RETURNS void
65+
AS '$libdir/unit'
66+
LANGUAGE C VOLATILE STRICT;

unit--3.sql.in

+5
Original file line numberDiff line numberDiff line change
@@ -592,3 +592,8 @@ CREATE FUNCTION unit_is_hashed(cstring)
592592
RETURNS bool
593593
AS '$libdir/unit'
594594
LANGUAGE C VOLATILE STRICT;
595+
596+
CREATE FUNCTION unit_reset()
597+
RETURNS void
598+
AS '$libdir/unit'
599+
LANGUAGE C VOLATILE STRICT;

unit.c

+13
Original file line numberDiff line numberDiff line change
@@ -893,3 +893,16 @@ unit_is_hashed(PG_FUNCTION_ARGS)
893893

894894
PG_RETURN_BOOL (hash_search(unit_names, name, HASH_FIND, NULL) != NULL);
895895
}
896+
897+
PG_FUNCTION_INFO_V1(unit_reset);
898+
899+
Datum
900+
unit_reset(PG_FUNCTION_ARGS)
901+
{
902+
/* reinitialize hash tables */
903+
hash_destroy(unit_names);
904+
hash_destroy(unit_dimensions);
905+
unit_get_definitions();
906+
907+
PG_RETURN_VOID();
908+
}

0 commit comments

Comments
 (0)