Skip to content

Commit

Permalink
Dependency control via event triggers
Browse files Browse the repository at this point in the history
This commit aims to implement dependency control of pg_shard with event
triggers. This commit is incomplete and can be considered as POC.
  • Loading branch information
onderkalaci committed Feb 20, 2015
1 parent f3bfdbb commit 926aae9
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 16 deletions.
10 changes: 10 additions & 0 deletions pg_shard--1.0.sql
Original file line number Diff line number Diff line change
Expand Up @@ -140,3 +140,13 @@ $sync_table_metadata_to_citus$ LANGUAGE 'plpgsql';

COMMENT ON FUNCTION sync_table_metadata_to_citus(text)
IS 'synchronize a distributed table''s pg_shard metadata to CitusDB';

CREATE FUNCTION control_drop_statements()
RETURNS event_trigger
AS 'MODULE_PATHNAME'
LANGUAGE C STRICT;

CREATE EVENT TRIGGER control_drop_statements_trigger
ON ddl_command_start
EXECUTE PROCEDURE control_drop_statements();

53 changes: 37 additions & 16 deletions pg_shard.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
#include "catalog/pg_type.h"
#include "catalog/objectaddress.h"
#include "commands/extension.h"
#include "commands/event_trigger.h"
#include "executor/execdesc.h"
#include "executor/executor.h"
#include "executor/instrument.h"
Expand Down Expand Up @@ -137,7 +138,6 @@ static void PgShardProcessUtility(Node *parsetree, const char *queryString,
ProcessUtilityContext context, ParamListInfo params,
DestReceiver *dest, char *completionTag);


/* declarations for dynamic loading */
PG_MODULE_MAGIC;

Expand All @@ -151,6 +151,9 @@ static ExecutorEnd_hook_type PreviousExecutorEndHook = NULL;
static ProcessUtility_hook_type PreviousProcessUtilityHook = NULL;


PG_FUNCTION_INFO_V1(control_drop_statements);


/*
* _PG_init is called when the module is loaded. In this function we save the
* previous utility hook, and then install our hook to pre-intercept calls to
Expand Down Expand Up @@ -2008,10 +2011,39 @@ PgShardProcessUtility(Node *parsetree, const char *queryString,
}
}
}
else if (statementType == T_DropStmt)

if (PreviousProcessUtilityHook != NULL)
{
PreviousProcessUtilityHook(parsetree, queryString, context,
params, dest, completionTag);
}
else
{
DropStmt *dropStatement = (DropStmt *) parsetree;
standard_ProcessUtility(parsetree, queryString, context,
params, dest, completionTag);
}
}


Datum
control_drop_statements(PG_FUNCTION_ARGS)
{

EventTriggerData *triggerData = NULL;
Node *parseTree = NULL;

/* error if event trigger manager did not call this function */
if (!CALLED_AS_EVENT_TRIGGER(fcinfo))
{
ereport(ERROR, (errmsg("trigger not fired by event trigger manager")));
}

triggerData = (EventTriggerData *) fcinfo->context;
parseTree = triggerData->parsetree;

if (nodeTag(parseTree) == T_DropStmt)
{
DropStmt *dropStatement = (DropStmt *) parseTree;
/* only apply hook for tables and extension */
if (dropStatement->removeType == OBJECT_TABLE)
{
Expand Down Expand Up @@ -2064,7 +2096,7 @@ PgShardProcessUtility(Node *parsetree, const char *queryString,
{
ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
errmsg("cannot drop %s because distributed"
" table(s) exists", PG_SHARD_EXTENSION_NAME),
" table(s) exists trigger", PG_SHARD_EXTENSION_NAME),
errhint("Try dropping the extension with"
" CASCADE modifier.")));
}
Expand All @@ -2073,16 +2105,5 @@ PgShardProcessUtility(Node *parsetree, const char *queryString,
}
}
}

if (PreviousProcessUtilityHook != NULL)
{
PreviousProcessUtilityHook(parsetree, queryString, context,
params, dest, completionTag);
}
else
{
standard_ProcessUtility(parsetree, queryString, context,
params, dest, completionTag);
}
PG_RETURN_NULL();
}

0 comments on commit 926aae9

Please sign in to comment.