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

Commit 25c17f8

Browse files
committed
Cmd options parsed, include dir
1 parent 0da8c7a commit 25c17f8

File tree

4 files changed

+45
-11
lines changed

4 files changed

+45
-11
lines changed

Makefile

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,16 @@ DATA = pg_shardman--0.0.1.sql # script files to install with CREATE EXTENSION
44
MODULE_big = pg_shardman
55
OBJS = src/pg_shardman.o
66

7+
PG_CPPFLAGS += -Isrc/include
8+
79
# You can specify path to pg_config in PG_CONFIG var
810
ifndef PG_CONFIG
911
PG_CONFIG := pg_config
1012
endif
1113
PGXS := $(shell $(PG_CONFIG) --pgxs)
1214

1315
INCLUDEDIR := $(shell $(PG_CONFIG) --includedir)
14-
PG_CPPFLAGS = -I$(INCLUDEDIR) # add server's include directory for libpq-fe.h
16+
PG_CPPFLAGS += -I$(INCLUDEDIR) # add server's include directory for libpq-fe.h
1517
SHLIB_LINK += -lpq # add libpq
1618

1719
include $(PGXS)

pg_shardman--0.0.1.sql

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@ CREATE TRIGGER cmd_log_inserts
2727

2828
-- probably better to keep opts in an array field, but working with arrays from
2929
-- libpq is not very handy
30+
-- opts must be inserted sequentially, we order by them by id
3031
CREATE TABLE cmd_opts (
32+
id bigserial PRIMARY KEY,
3133
cmd_id bigint REFERENCES cmd_log(id),
3234
opt text NOT NULL
3335
);
@@ -48,7 +50,7 @@ DECLARE
4850
BEGIN
4951
INSERT INTO @extschema@.cmd_log VALUES (DEFAULT, 'add_node')
5052
RETURNING id INTO c_id;
51-
INSERT INTO @extschema@.cmd_opts VALUES (c_id, connstring);
53+
INSERT INTO @extschema@.cmd_opts VALUES (DEFAULT, c_id, connstring);
5254
END
5355
$$ LANGUAGE plpgsql;
5456

src/include/pg_shardman.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#ifndef PG_SHARDMAN_H
2+
#define PG_SHARDMAN_H
3+
4+
extern void _PG_init(void);
5+
extern void shardmaster_main(Datum main_arg);
6+
7+
#endif /* PG_SHARDMAN_H */

src/pg_shardman.c

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
#include "commands/extension.h"
2727
#include "libpq-fe.h"
2828

29+
#include "pg_shardman.h"
30+
2931

3032
/* ensure that extension won't load against incompatible version of Postgres */
3133
PG_MODULE_MAGIC;
@@ -35,11 +37,9 @@ typedef struct Cmd
3537
int64 id;
3638
char *cmd_type;
3739
char *status;
40+
char **opts; /* array of n options, opts[n] is NULL */
3841
} Cmd;
3942

40-
extern void _PG_init(void);
41-
extern void shardmaster_main(Datum main_arg);
42-
4343
static Cmd *next_cmd(void);
4444
static void update_cmd_status(int64 id, const char *new_status);
4545
static PGconn *listen_cmd_log_inserts(void);
@@ -132,10 +132,13 @@ shardmaster_main(Datum main_arg)
132132
/* main loop */
133133
while (!got_sigterm)
134134
{
135+
/* TODO: new mem ctxt for every command */
135136
while ((cmd = next_cmd()) != NULL)
136137
{
137138
update_cmd_status(cmd->id, "in progress");
138-
elog(LOG, "Working on command %ld, %s", cmd->id, cmd->cmd_type);
139+
elog(LOG, "Working on command %ld, %s, opts are", cmd->id, cmd->cmd_type);
140+
for (char **opts = cmd->opts; *opts; opts++)
141+
elog(LOG, "%s", *opts);
139142
update_cmd_status(cmd->id, "success");
140143
}
141144
wait_notify(conn);
@@ -239,24 +242,44 @@ next_cmd(void)
239242
" status = 'in progress') t2 using (id);";
240243
e = SPI_execute(cmd_sql, true, 0);
241244
if (e < 0)
242-
{
243245
elog(FATAL, "Stmt failed: %s", cmd_sql);
244-
}
245246

246247
if (SPI_processed > 0)
247248
{
248249
HeapTuple tuple = SPI_tuptable->vals[0];
249250
TupleDesc rowdesc = SPI_tuptable->tupdesc;
250251
bool isnull;
251-
const char *cmd_type = (SPI_getvalue(tuple, rowdesc,
252-
SPI_fnumber(rowdesc, "cmd_type")));
252+
uint64 i;
253253

254+
/* copy the command itself to callee context */
254255
MemoryContext spicxt = MemoryContextSwitchTo(oldcxt);
255256
cmd = palloc(sizeof(Cmd));
256257
cmd->id = DatumGetInt64(SPI_getbinval(tuple, rowdesc,
257258
SPI_fnumber(rowdesc, "id"),
258259
&isnull));
259-
cmd->cmd_type = pstrdup(cmd_type);
260+
cmd->cmd_type = SPI_getvalue(tuple, rowdesc,
261+
SPI_fnumber(rowdesc, "cmd_type"));
262+
MemoryContextSwitchTo(spicxt);
263+
264+
/* Now get options. cmd_sql will be freed by SPI_finish */
265+
cmd_sql = psprintf("select opt from shardman.cmd_opts where"
266+
" cmd_id = %ld order by id;", cmd->id);
267+
e = SPI_execute(cmd_sql, true, 0);
268+
if (e < 0)
269+
elog(FATAL, "Stmt failed: %s", cmd_sql);
270+
271+
MemoryContextSwitchTo(oldcxt);
272+
/* +1 for NULL in the end */
273+
cmd->opts = palloc((SPI_processed + 1) * sizeof(char*));
274+
for (i = 0; i < SPI_processed; i++)
275+
{
276+
tuple = SPI_tuptable->vals[i];
277+
rowdesc = SPI_tuptable->tupdesc;
278+
cmd->opts[i] = SPI_getvalue(tuple, rowdesc,
279+
SPI_fnumber(rowdesc, "opt"));
280+
}
281+
cmd->opts[i] = NULL;
282+
260283
MemoryContextSwitchTo(spicxt);
261284
}
262285

0 commit comments

Comments
 (0)