Merged in move-internal (pull request #53)

This commit is contained in:
RobAtticus NA 2017-01-24 17:40:01 +00:00
commit a1d25906b5
8 changed files with 33 additions and 123 deletions

View File

@ -19,7 +19,7 @@ BEGIN
IF conn_exists IS NULL OR NOT conn_exists THEN IF conn_exists IS NULL OR NOT conn_exists THEN
--tells c code to commit in precommit. --tells c code to commit in precommit.
PERFORM dblink_connect(conn_name, get_meta_server_name()); PERFORM dblink_connect(conn_name, _iobeamdb_internal.get_meta_server_name());
PERFORM dblink_exec(conn_name, 'BEGIN'); PERFORM dblink_exec(conn_name, 'BEGIN');
PERFORM _iobeamdb_internal.register_dblink_precommit_connection(conn_name); PERFORM _iobeamdb_internal.register_dblink_precommit_connection(conn_name);
END IF; END IF;
@ -37,7 +37,7 @@ $BODY$
DECLARE DECLARE
conn_name TEXT; conn_name TEXT;
BEGIN BEGIN
IF get_meta_database_name() <> current_database() THEN IF _iobeamdb_internal.get_meta_database_name() <> current_database() THEN
SELECT _iobeamdb_internal.meta_transaction_start() INTO conn_name; SELECT _iobeamdb_internal.meta_transaction_start() INTO conn_name;
PERFORM * FROM dblink(conn_name, sql_code) AS t(r TEXT); PERFORM * FROM dblink(conn_name, sql_code) AS t(r TEXT);
ELSE ELSE
@ -56,7 +56,7 @@ DECLARE
conn_name TEXT; conn_name TEXT;
return_value TEXT; return_value TEXT;
BEGIN BEGIN
IF get_meta_database_name() <> current_database() THEN IF _iobeamdb_internal.get_meta_database_name() <> current_database() THEN
SELECT _iobeamdb_internal.meta_transaction_start() INTO conn_name; SELECT _iobeamdb_internal.meta_transaction_start() INTO conn_name;
SELECT t.r INTO return_value FROM dblink(conn_name, sql_code) AS t(r TEXT); SELECT t.r INTO return_value FROM dblink(conn_name, sql_code) AS t(r TEXT);
ELSE ELSE
@ -76,8 +76,9 @@ $BODY$
DECLARE DECLARE
return_value TEXT; return_value TEXT;
BEGIN BEGIN
IF get_meta_database_name() <> current_database() THEN IF _iobeamdb_internal.get_meta_database_name() <> current_database() THEN
SELECT t.r INTO return_value FROM dblink(get_meta_server_name(), sql_code) AS t(r TEXT); SELECT t.r INTO return_value
FROM dblink(_iobeamdb_internal.get_meta_server_name(), sql_code) AS t(r TEXT);
ELSE ELSE
EXECUTE sql_code INTO return_value; EXECUTE sql_code INTO return_value;
END IF; END IF;

View File

@ -37,13 +37,13 @@ BEGIN
FROM pg_catalog.pg_index FROM pg_catalog.pg_index
WHERE indexrelid = info.objid; WHERE indexrelid = info.objid;
IF NOT is_main_table(table_oid) OR current_setting('io.ignore_ddl_in_trigger', true) = 'true' THEN IF NOT _iobeamdb_internal.is_main_table(table_oid) OR current_setting('io.ignore_ddl_in_trigger', true) = 'true' THEN
RETURN; RETURN;
END IF; END IF;
def = _iobeamdb_internal.get_general_index_definition(info.objid, table_oid); def = _iobeamdb_internal.get_general_index_definition(info.objid, table_oid);
hypertable_row := hypertable_from_main_table(table_oid); hypertable_row := _iobeamdb_internal.hypertable_from_main_table(table_oid);
PERFORM _iobeamdb_meta_api.add_index( PERFORM _iobeamdb_meta_api.add_index(
hypertable_row.name, hypertable_row.name,
@ -71,7 +71,7 @@ BEGIN
FROM pg_catalog.pg_index FROM pg_catalog.pg_index
WHERE indexrelid = info.objid; WHERE indexrelid = info.objid;
IF NOT is_main_table(table_oid) THEN IF NOT _iobeamdb_internal.is_main_table(table_oid) THEN
RETURN; RETURN;
END IF; END IF;
@ -137,11 +137,11 @@ BEGIN
LOOP LOOP
--exit if not hypertable or inside trigger --exit if not hypertable or inside trigger
IF NOT is_main_table(info.objid) OR current_setting('io.ignore_ddl_in_trigger', true) = 'true' THEN IF NOT _iobeamdb_internal.is_main_table(info.objid) OR current_setting('io.ignore_ddl_in_trigger', true) = 'true' THEN
RETURN; RETURN;
END IF; END IF;
hypertable_row := hypertable_from_main_table(info.objid); hypertable_row := _iobeamdb_internal.hypertable_from_main_table(info.objid);
--Try to find what was done. If you can't find it error out. --Try to find what was done. If you can't find it error out.
found_action = FALSE; found_action = FALSE;

View File

@ -26,7 +26,7 @@ $BODY$
BEGIN BEGIN
EXECUTE format( EXECUTE format(
$$ $$
SELECT insert_data( SELECT _iobeamdb_internal.insert_data(
(SELECT name FROM _iobeamdb_catalog.hypertable (SELECT name FROM _iobeamdb_catalog.hypertable
WHERE main_schema_name = %1$L AND main_table_name = %2$L) WHERE main_schema_name = %1$L AND main_table_name = %2$L)
, %3$L) , %3$L)

View File

@ -6,7 +6,7 @@ CREATE OR REPLACE FUNCTION _iobeamdb_internal.get_column_list(
) )
RETURNS TEXT LANGUAGE SQL STABLE AS RETURNS TEXT LANGUAGE SQL STABLE AS
$BODY$ $BODY$
SELECT array_to_string(get_quoted_column_names(hypertable_name), ', ') SELECT array_to_string(_iobeamdb_internal.get_quoted_column_names(hypertable_name), ', ')
$BODY$; $BODY$;
-- Gets the partition ID of a given epoch and data row. -- Gets the partition ID of a given epoch and data row.
@ -77,7 +77,7 @@ $BODY$;
-- --
-- hypertable_name - Name of the hypertable the data belongs to -- hypertable_name - Name of the hypertable the data belongs to
-- copy_table_oid -- OID of the table to fetch rows from -- copy_table_oid -- OID of the table to fetch rows from
CREATE OR REPLACE FUNCTION insert_data( CREATE OR REPLACE FUNCTION _iobeamdb_internal.insert_data(
hypertable_name NAME, hypertable_name NAME,
copy_table_oid REGCLASS copy_table_oid REGCLASS
) )
@ -160,8 +160,10 @@ BEGIN
SELECT * SELECT *
INTO distinct_table_oid INTO distinct_table_oid
FROM get_distinct_table_oid(hypertable_name, crn_record.replica_id, crn_record.database_name); FROM _iobeamdb_internal.get_distinct_table_oid(hypertable_name, crn_record.replica_id, crn_record.database_name);
-- Generate clauses to insert new distinct column values into the
-- correct distinct tables
FOR distinct_column IN FOR distinct_column IN
SELECT c.name SELECT c.name
FROM _iobeamdb_catalog.hypertable_column c FROM _iobeamdb_catalog.hypertable_column c

View File

@ -1,4 +1,4 @@
CREATE OR REPLACE FUNCTION get_meta_server_name() CREATE OR REPLACE FUNCTION _iobeamdb_internal.get_meta_server_name()
RETURNS TEXT LANGUAGE PLPGSQL VOLATILE AS RETURNS TEXT LANGUAGE PLPGSQL VOLATILE AS
$BODY$ $BODY$
DECLARE DECLARE
@ -13,7 +13,7 @@ END
$BODY$; $BODY$;
CREATE OR REPLACE FUNCTION get_meta_database_name() CREATE OR REPLACE FUNCTION _iobeamdb_internal.get_meta_database_name()
RETURNS TEXT LANGUAGE PLPGSQL VOLATILE AS RETURNS TEXT LANGUAGE PLPGSQL VOLATILE AS
$BODY$ $BODY$
DECLARE DECLARE

View File

@ -1,7 +1,7 @@
-- This file contains functions related to getting information about the -- This file contains functions related to getting information about the
-- schema of a hypertable, including columns, their types, etc. -- schema of a hypertable, including columns, their types, etc.
CREATE OR REPLACE FUNCTION get_distinct_table_oid( CREATE OR REPLACE FUNCTION _iobeamdb_internal.get_distinct_table_oid(
hypertable_name NAME, hypertable_name NAME,
replica_id SMALLINT, replica_id SMALLINT,
database_name NAME database_name NAME
@ -15,16 +15,6 @@ WHERE drn.hypertable_name = get_distinct_table_oid.hypertable_name AND
drn.database_name = get_distinct_table_oid.database_name; drn.database_name = get_distinct_table_oid.database_name;
$BODY$; $BODY$;
CREATE OR REPLACE FUNCTION get_distinct_local_table_oid(
hypertable_name NAME,
replica_id SMALLINT
)
RETURNS REGCLASS LANGUAGE SQL STABLE AS
$BODY$
SELECT get_distinct_table_oid(hypertable_name, replica_id, current_database())
$BODY$;
-- Get the name of the time column for a hypertable. -- Get the name of the time column for a hypertable.
-- --
-- hypertable_name - name of the hypertable. -- hypertable_name - name of the hypertable.
@ -51,27 +41,10 @@ $BODY$
WHERE h.name = hypertable_name; WHERE h.name = hypertable_name;
$BODY$; $BODY$;
-- Get the list of columns for a hypertable as an ARRAY
--
-- hypertable_name - Name of the hypertable
CREATE OR REPLACE FUNCTION get_column_names(
hypertable_name NAME
)
RETURNS NAME [] LANGUAGE SQL STABLE AS
$BODY$
SELECT ARRAY(
SELECT name
FROM _iobeamdb_catalog.hypertable_column c
WHERE c.hypertable_name = get_column_names.hypertable_name
ORDER BY attnum
);
$BODY$;
-- Get the list of columns (each quoted) from a hypertable as an ARRAY -- Get the list of columns (each quoted) from a hypertable as an ARRAY
-- --
-- hypertable_name -- Name of the hypertable -- hypertable_name -- Name of the hypertable
CREATE OR REPLACE FUNCTION get_quoted_column_names( CREATE OR REPLACE FUNCTION _iobeamdb_internal.get_quoted_column_names(
hypertable_name NAME hypertable_name NAME
) )
RETURNS TEXT [] LANGUAGE SQL STABLE AS RETURNS TEXT [] LANGUAGE SQL STABLE AS
@ -84,75 +57,7 @@ SELECT ARRAY(
); );
$BODY$; $BODY$;
-- Get a table of columns and their types for a hypertable CREATE OR REPLACE FUNCTION _iobeamdb_internal.get_partition_for_epoch(
--
-- hypertable_name - Name of the hypertable
-- column_names - Name of the columns to fetch types for
CREATE OR REPLACE FUNCTION get_column_names_and_types(
hypertable_name NAME,
column_names NAME []
)
RETURNS TABLE(column_name NAME, data_type REGTYPE) LANGUAGE PLPGSQL STABLE AS
$BODY$
DECLARE
rows_returned INT;
BEGIN
RETURN QUERY SELECT
c.name,
c.data_type
FROM _iobeamdb_catalog.hypertable_column c
INNER JOIN unnest(column_names) WITH ORDINALITY
AS x(column_name, ordering) ON c.name = x.column_name
WHERE c.hypertable_name = get_column_names_and_types.hypertable_name
ORDER BY x.ordering;
GET DIAGNOSTICS rows_returned = ROW_COUNT;
IF rows_returned != cardinality(column_names) THEN
DECLARE
missing_column NAME;
BEGIN
SELECT cn
INTO missing_column
FROM unnest(column_names) AS cn
WHERE NOT EXISTS(
SELECT 1
FROM _iobeamdb_catalog.hypertable_column c
WHERE c.hypertable_name = get_column_names_and_types.hypertable_name AND
c.name = cn
);
RAISE 'Missing column "%" in hypertable "%"', missing_column, hypertable_name
USING ERRCODE = 'IO002';
END;
END IF;
END
$BODY$;
-- Get the Postgres datatype of a column in a hypertable
--
-- hypertable_name - Name of the hypertable
-- column_name - Name of the column whose type is wanted
CREATE OR REPLACE FUNCTION get_column_type(
hypertable_name NAME,
column_name NAME
)
RETURNS REGTYPE LANGUAGE PLPGSQL STABLE AS
$BODY$
DECLARE
data_type REGTYPE;
BEGIN
SELECT c.data_type
INTO data_type
FROM _iobeamdb_catalog.hypertable_column c
WHERE c.name = get_column_type.column_name AND c.hypertable_name = get_column_type.hypertable_name;
IF NOT FOUND THEN
RAISE 'Missing column "%" in hypertable "%"', column_name, hypertable_name
USING ERRCODE = 'IO002';
END IF;
RETURN data_type;
END
$BODY$;
CREATE OR REPLACE FUNCTION get_partition_for_epoch(
epoch _iobeamdb_catalog.partition_epoch, epoch _iobeamdb_catalog.partition_epoch,
key_value TEXT key_value TEXT
) )
@ -174,6 +79,7 @@ BEGIN
END END
$BODY$; $BODY$;
-- TODO Only used in tests -- okay to put in _iobeamdb_internal?
CREATE OR REPLACE FUNCTION get_open_partition_for_key( CREATE OR REPLACE FUNCTION get_open_partition_for_key(
hypertable_name NAME, hypertable_name NAME,
key_value TEXT key_value TEXT
@ -182,13 +88,14 @@ CREATE OR REPLACE FUNCTION get_open_partition_for_key(
$BODY$ $BODY$
SELECT p.* SELECT p.*
FROM _iobeamdb_catalog.partition_epoch pe, FROM _iobeamdb_catalog.partition_epoch pe,
get_partition_for_epoch(pe, key_value) p _iobeamdb_internal.get_partition_for_epoch(pe, key_value) p
WHERE pe.hypertable_name = get_open_partition_for_key.hypertable_name AND WHERE pe.hypertable_name = get_open_partition_for_key.hypertable_name AND
end_time IS NULL end_time IS NULL
$BODY$; $BODY$;
-- Check if a given table OID is a main table for a hypertable -- Check if a given table OID is a main table (i.e. the table a user
CREATE OR REPLACE FUNCTION is_main_table( -- targets for SQL operations) for a hypertable
CREATE OR REPLACE FUNCTION _iobeamdb_internal.is_main_table(
table_oid regclass table_oid regclass
) )
RETURNS bool LANGUAGE SQL STABLE AS RETURNS bool LANGUAGE SQL STABLE AS
@ -201,7 +108,7 @@ $BODY$;
-- Get a hypertable given its main table OID -- Get a hypertable given its main table OID
CREATE OR REPLACE FUNCTION hypertable_from_main_table( CREATE OR REPLACE FUNCTION _iobeamdb_internal.hypertable_from_main_table(
table_oid regclass table_oid regclass
) )
RETURNS _iobeamdb_catalog.hypertable LANGUAGE SQL STABLE AS RETURNS _iobeamdb_catalog.hypertable LANGUAGE SQL STABLE AS

View File

@ -5,9 +5,9 @@
-- Converting from timestamps to internal representation is conversion to epoch (in microseconds). -- Converting from timestamps to internal representation is conversion to epoch (in microseconds).
-- Gets the sql code for extracting the internal (bigint) time value from the identifier with the given column_type. -- Gets the sql code for extracting the internal (bigint) time value from the identifier with the given column_type.
CREATE OR REPLACE FUNCTION _iobeamdb_internal.extract_time_sql( CREATE OR REPLACE FUNCTION _iobeamdb_internal.extract_time_sql(
identifier text, identifier text,
column_type REGTYPE column_type REGTYPE
) )
RETURNS text LANGUAGE PLPGSQL STABLE AS RETURNS text LANGUAGE PLPGSQL STABLE AS
$BODY$ $BODY$
@ -23,9 +23,9 @@ END
$BODY$; $BODY$;
-- Gets the sql code for representing the literal for the given time value (in the internal representation) as the column_type. -- Gets the sql code for representing the literal for the given time value (in the internal representation) as the column_type.
CREATE OR REPLACE FUNCTION _iobeamdb_internal.time_literal_sql( CREATE OR REPLACE FUNCTION _iobeamdb_internal.time_literal_sql(
time_value BIGINT, time_value BIGINT,
column_type REGTYPE column_type REGTYPE
) )
RETURNS text LANGUAGE PLPGSQL STABLE AS RETURNS text LANGUAGE PLPGSQL STABLE AS
$BODY$ $BODY$

View File

@ -21,6 +21,6 @@ echo "Connecting to $POSTGRES_HOST as user $POSTGRES_USER and with db $INSTALL_D
cd $DIR cd $DIR
psql -U $POSTGRES_USER -h $POSTGRES_HOST -d $INSTALL_DB_MAIN -v ON_ERROR_STOP=1 <<EOF psql -U $POSTGRES_USER -h $POSTGRES_HOST -d $INSTALL_DB_MAIN -v ON_ERROR_STOP=1 <<EOF
SELECT insert_data('33_testNs', '$1'); SELECT _iobeamdb_internal.insert_data('33_testNs', '$1');
EOF EOF
cd $PWD cd $PWD