Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 325f540

Browse files
committed
Add test_dsa module.
This covers basic calls within a single backend process, and also calling dsa_allocate() or dsa_get_address() while in a different resource owners. The latter case was fixed by the previous commit. Discussion: https://www.postgresql.org/message-id/11b70743-c5f3-3910-8e5b-dd6c115ff829%40gmail.com
1 parent a8b330f commit 325f540

File tree

9 files changed

+204
-0
lines changed

9 files changed

+204
-0
lines changed

src/test/modules/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ SUBDIRS = \
1717
test_copy_callbacks \
1818
test_custom_rmgrs \
1919
test_ddl_deparse \
20+
test_dsa \
2021
test_extensions \
2122
test_ginpostinglist \
2223
test_integerset \

src/test/modules/meson.build

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ subdir('test_bloomfilter')
1414
subdir('test_copy_callbacks')
1515
subdir('test_custom_rmgrs')
1616
subdir('test_ddl_deparse')
17+
subdir('test_dsa')
1718
subdir('test_extensions')
1819
subdir('test_ginpostinglist')
1920
subdir('test_integerset')

src/test/modules/test_dsa/Makefile

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# src/test/modules/test_dsa/Makefile
2+
3+
MODULE_big = test_dsa
4+
OBJS = \
5+
$(WIN32RES) \
6+
test_dsa.o
7+
PGFILEDESC = "test_dsa - test code for dynamic shared memory areas"
8+
9+
EXTENSION = test_dsa
10+
DATA = test_dsa--1.0.sql
11+
12+
REGRESS = test_dsa
13+
14+
ifdef USE_PGXS
15+
PG_CONFIG = pg_config
16+
PGXS := $(shell $(PG_CONFIG) --pgxs)
17+
include $(PGXS)
18+
else
19+
subdir = src/test/modules/test_dsa
20+
top_builddir = ../../../..
21+
include $(top_builddir)/src/Makefile.global
22+
include $(top_srcdir)/contrib/contrib-global.mk
23+
endif
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
CREATE EXTENSION test_dsa;
2+
SELECT test_dsa_basic();
3+
test_dsa_basic
4+
----------------
5+
6+
(1 row)
7+
8+
SELECT test_dsa_resowners();
9+
test_dsa_resowners
10+
--------------------
11+
12+
(1 row)
13+

src/test/modules/test_dsa/meson.build

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Copyright (c) 2022-2023, PostgreSQL Global Development Group
2+
3+
test_dsa_sources = files(
4+
'test_dsa.c',
5+
)
6+
7+
if host_system == 'windows'
8+
test_dsa_sources += rc_lib_gen.process(win32ver_rc, extra_args: [
9+
'--NAME', 'test_dsa',
10+
'--FILEDESC', 'test_dsa - test code for dynamic shared memory areas',])
11+
endif
12+
13+
test_dsa = shared_module('test_dsa',
14+
test_dsa_sources,
15+
kwargs: pg_test_mod_args,
16+
)
17+
test_install_libs += test_dsa
18+
19+
test_install_data += files(
20+
'test_dsa.control',
21+
'test_dsa--1.0.sql',
22+
)
23+
24+
tests += {
25+
'name': 'test_dsa',
26+
'sd': meson.current_source_dir(),
27+
'bd': meson.current_build_dir(),
28+
'regress': {
29+
'sql': [
30+
'test_dsa',
31+
],
32+
},
33+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
CREATE EXTENSION test_dsa;
2+
3+
SELECT test_dsa_basic();
4+
SELECT test_dsa_resowners();
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/* src/test/modules/test_dsa/test_dsa--1.0.sql */
2+
3+
-- complain if script is sourced in psql, rather than via CREATE EXTENSION
4+
\echo Use "CREATE EXTENSION test_dsa" to load this file. \quit
5+
6+
CREATE FUNCTION test_dsa_basic()
7+
RETURNS pg_catalog.void
8+
AS 'MODULE_PATHNAME' LANGUAGE C;
9+
10+
CREATE FUNCTION test_dsa_resowners()
11+
RETURNS pg_catalog.void
12+
AS 'MODULE_PATHNAME' LANGUAGE C;

src/test/modules/test_dsa/test_dsa.c

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
/*--------------------------------------------------------------------------
2+
*
3+
* test_dsa.c
4+
* Test dynamic shared memory areas (DSAs)
5+
*
6+
* Copyright (c) 2022-2023, PostgreSQL Global Development Group
7+
*
8+
* IDENTIFICATION
9+
* src/test/modules/test_dsa/test_dsa.c
10+
*
11+
* -------------------------------------------------------------------------
12+
*/
13+
#include "postgres.h"
14+
15+
#include "fmgr.h"
16+
#include "utils/dsa.h"
17+
#include "storage/lwlock.h"
18+
#include "utils/resowner.h"
19+
20+
PG_MODULE_MAGIC;
21+
22+
/* Test basic DSA functionality */
23+
PG_FUNCTION_INFO_V1(test_dsa_basic);
24+
Datum
25+
test_dsa_basic(PG_FUNCTION_ARGS)
26+
{
27+
int tranche_id;
28+
dsa_area *a;
29+
dsa_pointer p[100];
30+
31+
/* XXX: this tranche is leaked */
32+
tranche_id = LWLockNewTrancheId();
33+
LWLockRegisterTranche(tranche_id, "test_dsa");
34+
35+
a = dsa_create(tranche_id);
36+
for (int i = 0; i < 100; i++)
37+
{
38+
p[i] = dsa_allocate(a, 1000);
39+
snprintf(dsa_get_address(a, p[i]), 1000, "foobar%d", i);
40+
}
41+
42+
for (int i = 0; i < 100; i++)
43+
{
44+
char buf[100];
45+
46+
snprintf(buf, 100, "foobar%d", i);
47+
if (strcmp(dsa_get_address(a, p[i]), buf) != 0)
48+
elog(ERROR, "no match");
49+
}
50+
51+
for (int i = 0; i < 100; i++)
52+
{
53+
dsa_free(a, p[i]);
54+
}
55+
56+
dsa_detach(a);
57+
58+
PG_RETURN_VOID();
59+
}
60+
61+
/* Test using DSA across different resource owners */
62+
PG_FUNCTION_INFO_V1(test_dsa_resowners);
63+
Datum
64+
test_dsa_resowners(PG_FUNCTION_ARGS)
65+
{
66+
int tranche_id;
67+
dsa_area *a;
68+
dsa_pointer p[10000];
69+
ResourceOwner oldowner;
70+
ResourceOwner childowner;
71+
72+
/* XXX: this tranche is leaked */
73+
tranche_id = LWLockNewTrancheId();
74+
LWLockRegisterTranche(tranche_id, "test_dsa");
75+
76+
/* Create DSA in parent resource owner */
77+
a = dsa_create(tranche_id);
78+
79+
/*
80+
* Switch to child resource owner, and do a bunch of allocations in the
81+
* DSA
82+
*/
83+
oldowner = CurrentResourceOwner;
84+
childowner = ResourceOwnerCreate(oldowner, "test_dsa temp owner");
85+
CurrentResourceOwner = childowner;
86+
87+
for (int i = 0; i < 10000; i++)
88+
{
89+
p[i] = dsa_allocate(a, 1000);
90+
snprintf(dsa_get_address(a, p[i]), 1000, "foobar%d", i);
91+
}
92+
93+
/* Also test freeing, by freeing some of the allocations. */
94+
for (int i = 0; i < 500; i++)
95+
dsa_free(a, p[i]);
96+
97+
/* Release the child resource owner */
98+
CurrentResourceOwner = oldowner;
99+
ResourceOwnerRelease(childowner,
100+
RESOURCE_RELEASE_BEFORE_LOCKS,
101+
true, false);
102+
ResourceOwnerRelease(childowner,
103+
RESOURCE_RELEASE_LOCKS,
104+
true, false);
105+
ResourceOwnerRelease(childowner,
106+
RESOURCE_RELEASE_AFTER_LOCKS,
107+
true, false);
108+
ResourceOwnerDelete(childowner);
109+
110+
dsa_detach(a);
111+
112+
PG_RETURN_VOID();
113+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
comment = 'Test code for dynamic shared memory areas'
2+
default_version = '1.0'
3+
module_pathname = '$libdir/test_dsa'
4+
relocatable = true

0 commit comments

Comments
 (0)