mirror of
https://github.com/timescale/timescaledb.git
synced 2025-05-22 13:40:56 +08:00
Merged in move-internal (pull request #53)
This commit is contained in:
commit
a1d25906b5
@ -19,7 +19,7 @@ BEGIN
|
||||
|
||||
IF conn_exists IS NULL OR NOT conn_exists THEN
|
||||
--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 _iobeamdb_internal.register_dblink_precommit_connection(conn_name);
|
||||
END IF;
|
||||
@ -37,7 +37,7 @@ $BODY$
|
||||
DECLARE
|
||||
conn_name TEXT;
|
||||
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;
|
||||
PERFORM * FROM dblink(conn_name, sql_code) AS t(r TEXT);
|
||||
ELSE
|
||||
@ -56,7 +56,7 @@ DECLARE
|
||||
conn_name TEXT;
|
||||
return_value TEXT;
|
||||
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 t.r INTO return_value FROM dblink(conn_name, sql_code) AS t(r TEXT);
|
||||
ELSE
|
||||
@ -76,8 +76,9 @@ $BODY$
|
||||
DECLARE
|
||||
return_value TEXT;
|
||||
BEGIN
|
||||
IF 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);
|
||||
IF _iobeamdb_internal.get_meta_database_name() <> current_database() THEN
|
||||
SELECT t.r INTO return_value
|
||||
FROM dblink(_iobeamdb_internal.get_meta_server_name(), sql_code) AS t(r TEXT);
|
||||
ELSE
|
||||
EXECUTE sql_code INTO return_value;
|
||||
END IF;
|
||||
|
@ -37,13 +37,13 @@ BEGIN
|
||||
FROM pg_catalog.pg_index
|
||||
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;
|
||||
END IF;
|
||||
|
||||
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(
|
||||
hypertable_row.name,
|
||||
@ -71,7 +71,7 @@ BEGIN
|
||||
FROM pg_catalog.pg_index
|
||||
WHERE indexrelid = info.objid;
|
||||
|
||||
IF NOT is_main_table(table_oid) THEN
|
||||
IF NOT _iobeamdb_internal.is_main_table(table_oid) THEN
|
||||
RETURN;
|
||||
END IF;
|
||||
|
||||
@ -137,11 +137,11 @@ BEGIN
|
||||
LOOP
|
||||
|
||||
--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;
|
||||
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.
|
||||
found_action = FALSE;
|
||||
|
@ -26,7 +26,7 @@ $BODY$
|
||||
BEGIN
|
||||
EXECUTE format(
|
||||
$$
|
||||
SELECT insert_data(
|
||||
SELECT _iobeamdb_internal.insert_data(
|
||||
(SELECT name FROM _iobeamdb_catalog.hypertable
|
||||
WHERE main_schema_name = %1$L AND main_table_name = %2$L)
|
||||
, %3$L)
|
||||
|
@ -6,7 +6,7 @@ CREATE OR REPLACE FUNCTION _iobeamdb_internal.get_column_list(
|
||||
)
|
||||
RETURNS TEXT LANGUAGE SQL STABLE AS
|
||||
$BODY$
|
||||
SELECT array_to_string(get_quoted_column_names(hypertable_name), ', ')
|
||||
SELECT array_to_string(_iobeamdb_internal.get_quoted_column_names(hypertable_name), ', ')
|
||||
$BODY$;
|
||||
|
||||
-- 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
|
||||
-- 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,
|
||||
copy_table_oid REGCLASS
|
||||
)
|
||||
@ -160,8 +160,10 @@ BEGIN
|
||||
|
||||
SELECT *
|
||||
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
|
||||
SELECT c.name
|
||||
FROM _iobeamdb_catalog.hypertable_column c
|
||||
|
@ -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
|
||||
$BODY$
|
||||
DECLARE
|
||||
@ -13,7 +13,7 @@ END
|
||||
$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
|
||||
$BODY$
|
||||
DECLARE
|
||||
|
@ -1,7 +1,7 @@
|
||||
-- This file contains functions related to getting information about the
|
||||
-- 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,
|
||||
replica_id SMALLINT,
|
||||
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;
|
||||
$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.
|
||||
--
|
||||
-- hypertable_name - name of the hypertable.
|
||||
@ -51,27 +41,10 @@ $BODY$
|
||||
WHERE h.name = hypertable_name;
|
||||
$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
|
||||
--
|
||||
-- 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
|
||||
)
|
||||
RETURNS TEXT [] LANGUAGE SQL STABLE AS
|
||||
@ -84,75 +57,7 @@ SELECT ARRAY(
|
||||
);
|
||||
$BODY$;
|
||||
|
||||
-- Get a table of columns and their types for a hypertable
|
||||
--
|
||||
-- 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(
|
||||
CREATE OR REPLACE FUNCTION _iobeamdb_internal.get_partition_for_epoch(
|
||||
epoch _iobeamdb_catalog.partition_epoch,
|
||||
key_value TEXT
|
||||
)
|
||||
@ -174,6 +79,7 @@ BEGIN
|
||||
END
|
||||
$BODY$;
|
||||
|
||||
-- TODO Only used in tests -- okay to put in _iobeamdb_internal?
|
||||
CREATE OR REPLACE FUNCTION get_open_partition_for_key(
|
||||
hypertable_name NAME,
|
||||
key_value TEXT
|
||||
@ -182,13 +88,14 @@ CREATE OR REPLACE FUNCTION get_open_partition_for_key(
|
||||
$BODY$
|
||||
SELECT p.*
|
||||
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
|
||||
end_time IS NULL
|
||||
$BODY$;
|
||||
|
||||
-- Check if a given table OID is a main table for a hypertable
|
||||
CREATE OR REPLACE FUNCTION is_main_table(
|
||||
-- Check if a given table OID is a main table (i.e. the table a user
|
||||
-- targets for SQL operations) for a hypertable
|
||||
CREATE OR REPLACE FUNCTION _iobeamdb_internal.is_main_table(
|
||||
table_oid regclass
|
||||
)
|
||||
RETURNS bool LANGUAGE SQL STABLE AS
|
||||
@ -201,7 +108,7 @@ $BODY$;
|
||||
|
||||
|
||||
-- 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
|
||||
)
|
||||
RETURNS _iobeamdb_catalog.hypertable LANGUAGE SQL STABLE AS
|
||||
|
@ -5,9 +5,9 @@
|
||||
-- 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.
|
||||
CREATE OR REPLACE FUNCTION _iobeamdb_internal.extract_time_sql(
|
||||
CREATE OR REPLACE FUNCTION _iobeamdb_internal.extract_time_sql(
|
||||
identifier text,
|
||||
column_type REGTYPE
|
||||
column_type REGTYPE
|
||||
)
|
||||
RETURNS text LANGUAGE PLPGSQL STABLE AS
|
||||
$BODY$
|
||||
@ -23,9 +23,9 @@ END
|
||||
$BODY$;
|
||||
|
||||
-- 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,
|
||||
column_type REGTYPE
|
||||
column_type REGTYPE
|
||||
)
|
||||
RETURNS text LANGUAGE PLPGSQL STABLE AS
|
||||
$BODY$
|
||||
|
@ -21,6 +21,6 @@ echo "Connecting to $POSTGRES_HOST as user $POSTGRES_USER and with db $INSTALL_D
|
||||
|
||||
cd $DIR
|
||||
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
|
||||
cd $PWD
|
||||
|
Loading…
x
Reference in New Issue
Block a user