Исходный код СУБД RuDB — свободной СУБД с открытым исходным кодом на базе PostgreSQL
rudb
2 days ago bd039d1e92813358c5c5a1e346b49074baf513f2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
CREATE EXTENSION test_lwlock_tranches;
 
-- Test lock tranches created with RequestNamedLWLockTranche()
SELECT test_startup_lwlocks();
 
-- Test creating new lock tranches with LWLockNewTrancheId()
CREATE TEMP TABLE test_tranches(tranche_name text, tranche_id int);
INSERT INTO test_tranches VALUES
    ('test_tranche_a', test_lwlock_tranche_create('test_tranche_a')),
    ('test_tranche_b', test_lwlock_tranche_create('test_tranche_b'));
 
-- You can create multiple tranches with the same name. It's confusing, so
-- not recommended, but test it.
INSERT INTO test_tranches VALUES
    ('test_tranche_duplicate', test_lwlock_tranche_create('test_tranche_duplicate')),
    ('test_tranche_duplicate', test_lwlock_tranche_create('test_tranche_duplicate'));
 
-- Check that all tranches were assigned different tranche IDs, including
-- the ones with duplicate names
select count(distinct tranche_id) from  test_tranches;
 
-- Test GetLWLockIdentifier() on tranches created with LWLockNewTrancheId()
select tranche_name, test_lwlock_get_lwlock_identifier(tranche_id) as lookup_result
from test_tranches;
 
-- negative tests
SELECT test_lwlock_tranche_create(NULL);
SELECT test_lwlock_tranche_create(repeat('a', 64));
SELECT test_lwlock_tranche_lookup('bogus');
SELECT test_lwlock_tranche_lookup('test_tranche_a');
SELECT test_lwlock_initialize(65535);
 
-- Test what happens when you use up all the slots.  Other preload modules
-- may already own user-defined tranches, so consume the remaining capacity
-- dynamically instead of assuming that this module owns the first IDs.
DO $$
BEGIN
    LOOP
        BEGIN
            PERFORM test_lwlock_tranche_create('test_tranche_consume_all');
        EXCEPTION WHEN OTHERS THEN
            IF SQLERRM = 'maximum number of tranches already registered' THEN
                EXIT;
            END IF;
            RAISE;
        END;
    END LOOP;
END
$$;
 
select test_lwlock_tranche_create('out-of-tranches');