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

Commit 6f17dfb

Browse files
committed
Making tables read-only
1 parent ac29452 commit 6f17dfb

File tree

3 files changed

+47
-2
lines changed

3 files changed

+47
-2
lines changed

pg_shardman--0.0.1.sql

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -403,6 +403,38 @@ BEGIN
403403
END
404404
$$ LANGUAGE plpgsql;
405405

406+
-- Make table read-only
407+
CREATE FUNCTION readonly_table_on(relation regclass)
408+
RETURNS void AS $$
409+
BEGIN
410+
-- Grab exclusive lock on table to finish existing transactions
411+
PERFORM shardman.ae_lock_table(relation);
412+
-- Create go away trigger to prevent any new ones
413+
PERFORM shardman.readonly_table_off(relation);
414+
EXECUTE format(
415+
'CREATE TRIGGER shardman_readonly BEFORE INSERT OR UPDATE OR DELETE OR
416+
TRUNCATE ON %I FOR EACH STATEMENT EXECUTE PROCEDURE shardman.go_away();',
417+
relation);
418+
EXECUTE format(
419+
'ALTER TABLE %I ENABLE ALWAYS TRIGGER shardman_readonly;', relation);
420+
END
421+
$$ LANGUAGE plpgsql STRICT;
422+
CREATE FUNCTION go_away() RETURNS TRIGGER AS $$
423+
BEGIN
424+
RAISE EXCEPTION 'The "%" table is read only.', TG_TABLE_NAME
425+
USING HINT = 'Probably table copy is in progress';
426+
RETURN NULL;
427+
END;
428+
$$ LANGUAGE plpgsql;
429+
CREATE FUNCTION ae_lock_table(relation regclass) RETURNS void
430+
AS 'pg_shardman' LANGUAGE C;
431+
-- And make it writable again
432+
CREATE FUNCTION readonly_table_off(relation regclass)
433+
RETURNS void AS $$
434+
BEGIN
435+
EXECUTE format('DROP TRIGGER IF EXISTS shardman_readonly ON %s', relation);
436+
END $$ LANGUAGE plpgsql STRICT;
437+
406438
CREATE FUNCTION gen_create_table_sql(relation text, connstring text) RETURNS text
407439
AS 'pg_shardman' LANGUAGE C;
408440

src/shard.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -554,8 +554,8 @@ epoll_subscribe(int epfd, MoveMPartState *mmps)
554554
}
555555

556556
/*
557-
* Actually run MoveMPart state machine. Return value says when (if ever)
558-
* we want to be executed again.
557+
* Actually run MoveMPart state machine. On return, mmps values say when (if
558+
* ever) we want to be executed again.
559559
*/
560560
void
561561
exec_move_mpart(MoveMPartState *mmps)

src/udf.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include "funcapi.h"
1010
#include "access/htup_details.h"
1111
#include "catalog/pg_type.h"
12+
#include "storage/lmgr.h"
1213
#include "libpq-fe.h"
1314

1415
/*
@@ -268,3 +269,15 @@ pq_conninfo_parse(PG_FUNCTION_ARGS)
268269
PQconninfoFree(opts);
269270
PG_RETURN_DATUM(HeapTupleGetDatum(heap_form_tuple(tupdesc, values, nulls)));
270271
}
272+
273+
/*
274+
* Obtain AccessExclusiveLock on table
275+
*/
276+
PG_FUNCTION_INFO_V1(ae_lock_table);
277+
Datum
278+
ae_lock_table(PG_FUNCTION_ARGS)
279+
{
280+
Oid relid = PG_GETARG_OID(0);
281+
LockRelationOid(relid, AccessExclusiveLock);
282+
PG_RETURN_VOID();
283+
}

0 commit comments

Comments
 (0)