Skip to content

Commit f6f7ac2

Browse files
authored
Merge pull request #6 from jsc0218/dev_gc
Dev gc
2 parents f5e6eab + dba4df7 commit f6f7ac2

9 files changed

Lines changed: 464 additions & 151 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,6 @@
44
*.bc
55
.cproject
66
.project
7+
.vscode
78
.*.swp
89
.*.swo

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,4 @@ PGXS := $(shell $(PG_CONFIG) --pgxs)
1414
include $(PGXS)
1515

1616
src/kv_storage.bc:
17-
$(COMPILE.cxx.bc) $(CCFLAGS) $(CPPFLAGS) -fPIC -c -o $@ src/kv_storage.cc
17+
$(COMPILE.cxx.bc) $(CCFLAGS) $(CPPFLAGS) -fPIC -c -o $@ src/kv_storage.cc

README.md

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,9 @@ We test this foreign data wrapper on Ubuntu Server 18.04 using PostgreSQL-11 tog
6767

6868
Before using this foreign data wrapper, you need to add it to ```shared_preload_libraries``` in your ```postgresql.conf```
6969

70+
```sh
7071
shared_preload_libraries = 'kv_fdw' # (change requires restart)
72+
```
7173

7274
and restart Postgres:
7375

@@ -82,33 +84,33 @@ A simple example is as follows.
8284

8385

8486
```
85-
CREATE DATABASE kvtest;
86-
\c kvtest
87+
CREATE DATABASE kvtest;
88+
\c kvtest
8789
88-
CREATE EXTENSION kv_fdw;
89-
CREATE SERVER kv_server FOREIGN DATA WRAPPER kv_fdw;
90+
CREATE EXTENSION kv_fdw;
91+
CREATE SERVER kv_server FOREIGN DATA WRAPPER kv_fdw;
9092
91-
CREATE FOREIGN TABLE test(key TEXT, value TEXT) SERVER kv_server;
93+
CREATE FOREIGN TABLE test(key TEXT, value TEXT) SERVER kv_server;
9294
93-
INSERT INTO test VALUES('YC', 'VidarDB');
94-
SELECT * FROM test;
95+
INSERT INTO test VALUES('YC', 'VidarDB');
96+
SELECT * FROM test;
9597
96-
INSERT INTO test VALUES('California', 'Waterloo');
97-
SELECT * FROM test;
98+
INSERT INTO test VALUES('California', 'Waterloo');
99+
SELECT * FROM test;
98100
99-
DELETE FROM test WHERE key='California';
100-
SELECT * FROM test;
101+
DELETE FROM test WHERE key='California';
102+
SELECT * FROM test;
101103
102-
UPDATE test SET value='VidarSQL';
103-
SELECT * FROM test;
104+
UPDATE test SET value='VidarSQL';
105+
SELECT * FROM test;
104106
105-
DROP FOREIGN TABLE test;
107+
DROP FOREIGN TABLE test;
106108
107-
DROP SERVER kv_server;
108-
DROP EXTENSION kv_fdw;
109+
DROP SERVER kv_server;
110+
DROP EXTENSION kv_fdw;
109111
110-
\c postgres
111-
DROP DATABASE kvtest;
112+
\c postgres
113+
DROP DATABASE kvtest;
112114
113115
```
114116

@@ -118,15 +120,15 @@ We have tested certain typical SQL statements and will add more test cases later
118120

119121

120122
```sh
121-
sudo service postgresql restart
123+
sudo service postgresql restart
122124

123-
cd PostgresForeignDataWrapper
125+
cd PostgresForeignDataWrapper
124126

125-
sudo -u postgres psql -U postgres -a -f test/sql/create.sql
127+
sudo -u postgres psql -U postgres -a -f test/sql/create.sql
126128

127-
sudo -u postgres psql -U postgres -d kvtest -a -f test/sql/test.sql
129+
sudo -u postgres psql -U postgres -d kvtest -a -f test/sql/test.sql
128130

129-
sudo -u postgres psql -U postgres -d kvtest -a -f test/sql/clear.sql
131+
sudo -u postgres psql -U postgres -d kvtest -a -f test/sql/clear.sql
130132
```
131133

132134
# Debug
@@ -135,7 +137,7 @@ If you want to debug the source code, you may need to start PostgreSQL in the de
135137

136138

137139
```sh
138-
sudo service postgresql stop
140+
sudo service postgresql stop
139141

140-
sudo -u postgres /usr/lib/postgresql/11/bin/postgres -d 0 -D /var/lib/postgresql/11/main -c config_file=/etc/postgresql/11/main/postgresql.conf
142+
sudo -u postgres /usr/lib/postgresql/11/bin/postgres -d 0 -D /var/lib/postgresql/11/main -c config_file=/etc/postgresql/11/main/postgresql.conf
141143
```

src/kv_fdw.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ PG_FUNCTION_INFO_V1(kv_fdw_validator);
2626

2727
static SharedMem *ptr = NULL; // in client process
2828

29-
3029
static void GetForeignRelSize(PlannerInfo *root,
3130
RelOptInfo *baserel,
3231
Oid foreignTableId) {

src/kv_fdw.h

Lines changed: 35 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,45 @@
1212
#include "access/attnum.h"
1313
#include "utils/relcache.h"
1414

15+
/* Defines */
16+
#define KVKEYJUNK "__key_junk"
17+
18+
#define KVFDWNAME "kv_fdw"
19+
20+
#define BACKFILE "/KVSharedMem"
21+
22+
#define PERMISSION 0777
23+
24+
#define PATHMAXLENGTH 4096
25+
26+
#define BUFSIZE 65536
27+
28+
#define FILENAMELENGTH 20
29+
30+
#define RESPONSEFILE "/KVSharedResponse"
31+
32+
#define RESPONSEQUEUELENGTH 2
33+
34+
#define DATAAREASIZE sizeof(char)*BUFSIZE
1535

36+
37+
/* Shared memory for function requests:
38+
* mutex is used for the mutual exclusion of the request buffer;
39+
* full is used to tell whether the request buffer is full;
40+
* agent[2] are used to synchronize the creation of the worker process;
41+
* worker is used to notify the worker process after a request is submitted;
42+
* responseMutexes[RESPONSEQUEUELENGTH] are used for the mutual exclusion of the response buffer;
43+
* responseSync[RESPONSEQUEUELENGTH] are used to notify child processes after the response is ready.
44+
*/
1645
typedef struct SharedMem {
46+
sem_t mutex;
47+
sem_t full;
1748
sem_t agent[2];
18-
sem_t worker[2];
49+
sem_t worker;
50+
sem_t responseMutexes[RESPONSEQUEUELENGTH];
51+
sem_t responseSync[RESPONSEQUEUELENGTH];
1952
bool workerProcessCreated;
20-
char area[65536]; // assume ~64K for a tuple is enough
53+
char area[BUFSIZE]; // assume ~64K for a tuple is enough
2154
} SharedMem;
2255

2356
/* Holds the option values to be used when reading or writing files.
@@ -67,18 +100,6 @@ typedef enum FuncName {
67100
} FuncName;
68101

69102

70-
/* Defines */
71-
#define KVKEYJUNK "__key_junk"
72-
73-
#define KVFDWNAME "kv_fdw"
74-
75-
#define BACKFILE "/KVSharedMem"
76-
77-
#define PERMISSION 0777
78-
79-
#define PATHMAXLENGTH 4096
80-
81-
82103
/* Function declarations for extension loading and unloading */
83104
extern void _PG_init(void);
84105

src/kv_posix.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,3 +113,11 @@ void SemWait(sem_t *__sem, const char *fun) {
113113
ereport(ERROR, (errmsg("%s %s failed", fun, __func__)));
114114
}
115115
}
116+
117+
int SemTryWait(sem_t *__sem, const char *fun) {
118+
int ret = sem_trywait(__sem);
119+
if (ret == -1 && errno != EAGAIN) {
120+
ereport(ERROR, (errmsg("%s %s failed", fun, __func__)));
121+
}
122+
return ret;
123+
}

src/kv_posix.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,5 +73,6 @@ extern void SemPost(sem_t *__sem, const char *fun);
7373

7474
extern void SemWait(sem_t *__sem, const char *fun);
7575

76+
extern int SemTryWait(sem_t *__sem, const char *fun);
7677

7778
#endif

0 commit comments

Comments
 (0)