File tree Expand file tree Collapse file tree 3 files changed +47
-2
lines changed Expand file tree Collapse file tree 3 files changed +47
-2
lines changed Original file line number Diff line number Diff line change @@ -403,6 +403,38 @@ BEGIN
403
403
END
404
404
$$ LANGUAGE plpgsql;
405
405
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
+
406
438
CREATE FUNCTION gen_create_table_sql (relation text , connstring text ) RETURNS text
407
439
AS ' pg_shardman' LANGUAGE C;
408
440
Original file line number Diff line number Diff line change @@ -554,8 +554,8 @@ epoll_subscribe(int epfd, MoveMPartState *mmps)
554
554
}
555
555
556
556
/*
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.
559
559
*/
560
560
void
561
561
exec_move_mpart (MoveMPartState * mmps )
Original file line number Diff line number Diff line change 9
9
#include "funcapi.h"
10
10
#include "access/htup_details.h"
11
11
#include "catalog/pg_type.h"
12
+ #include "storage/lmgr.h"
12
13
#include "libpq-fe.h"
13
14
14
15
/*
@@ -268,3 +269,15 @@ pq_conninfo_parse(PG_FUNCTION_ARGS)
268
269
PQconninfoFree (opts );
269
270
PG_RETURN_DATUM (HeapTupleGetDatum (heap_form_tuple (tupdesc , values , nulls )));
270
271
}
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
+ }
You can’t perform that action at this time.
0 commit comments