|
| 1 | +/* |
| 2 | + * This file and its contents are licensed under the Apache License 2.0. |
| 3 | + * Please see the included NOTICE for copyright information and |
| 4 | + * LICENSE-APACHE for a copy of the license. |
| 5 | + */ |
| 6 | + |
| 7 | +#include <postgres.h> |
| 8 | +#include <access/table.h> |
| 9 | +#include <utils/rel.h> |
| 10 | + |
| 11 | +#include "scan_iterator.h" |
| 12 | +#include "ts_catalog/catalog.h" |
| 13 | +#include "ts_catalog/continuous_aggs_tenant_tracking.h" |
| 14 | + |
| 15 | +static void |
| 16 | +init_scan_by_hypertable_id(ScanIterator *iterator, const int32 hypertable_id) |
| 17 | +{ |
| 18 | + iterator->ctx.index = catalog_get_index(ts_catalog_get(), |
| 19 | + CONTINUOUS_AGGS_TENANT_TRACKING, |
| 20 | + CONTINUOUS_AGGS_TENANT_TRACKING_IDX); |
| 21 | + |
| 22 | + ts_scan_iterator_scan_key_init(iterator, |
| 23 | + Anum_continuous_aggs_tenant_tracking_idx_hypertable_id, |
| 24 | + BTEqualStrategyNumber, |
| 25 | + F_INT4EQ, |
| 26 | + Int32GetDatum(hypertable_id)); |
| 27 | +} |
| 28 | + |
| 29 | +/* |
| 30 | + * Insert one tenant tracking row. tenant_id is copied into a bytea holding the |
| 31 | + * exact tenant key bytes. |
| 32 | + */ |
| 33 | +TSDLLEXPORT void |
| 34 | +ts_cagg_tenant_tracking_insert(int32 hypertable_id, const char *tenant_id, int tenant_id_len, |
| 35 | + int64 min_timestamp, int64 max_timestamp, int32 seqnum) |
| 36 | +{ |
| 37 | + Catalog *catalog = ts_catalog_get(); |
| 38 | + Relation rel = table_open(catalog_get_table_id(catalog, CONTINUOUS_AGGS_TENANT_TRACKING), |
| 39 | + RowExclusiveLock); |
| 40 | + TupleDesc desc = RelationGetDescr(rel); |
| 41 | + Datum values[Natts_continuous_aggs_tenant_tracking]; |
| 42 | + bool nulls[Natts_continuous_aggs_tenant_tracking] = { false }; |
| 43 | + CatalogSecurityContext sec_ctx; |
| 44 | + bytea *tenant_bytea = (bytea *) palloc(VARHDRSZ + tenant_id_len); |
| 45 | + |
| 46 | + SET_VARSIZE(tenant_bytea, VARHDRSZ + tenant_id_len); |
| 47 | + memcpy(VARDATA(tenant_bytea), tenant_id, tenant_id_len); |
| 48 | + |
| 49 | + values[AttrNumberGetAttrOffset(Anum_continuous_aggs_tenant_tracking_hypertable_id)] = |
| 50 | + Int32GetDatum(hypertable_id); |
| 51 | + values[AttrNumberGetAttrOffset(Anum_continuous_aggs_tenant_tracking_tenant_id)] = |
| 52 | + PointerGetDatum(tenant_bytea); |
| 53 | + values[AttrNumberGetAttrOffset(Anum_continuous_aggs_tenant_tracking_min_timestamp)] = |
| 54 | + Int64GetDatum(min_timestamp); |
| 55 | + values[AttrNumberGetAttrOffset(Anum_continuous_aggs_tenant_tracking_max_timestamp)] = |
| 56 | + Int64GetDatum(max_timestamp); |
| 57 | + values[AttrNumberGetAttrOffset(Anum_continuous_aggs_tenant_tracking_seqnum)] = |
| 58 | + Int32GetDatum(seqnum); |
| 59 | + |
| 60 | + ts_catalog_database_info_become_owner(ts_catalog_database_info_get(), &sec_ctx); |
| 61 | + ts_catalog_insert_values(rel, desc, values, nulls); |
| 62 | + ts_catalog_restore_user(&sec_ctx); |
| 63 | + |
| 64 | + table_close(rel, NoLock); |
| 65 | +} |
| 66 | + |
| 67 | +/* |
| 68 | + * Insert the "invalid" marker row <null, null, null, seqnum>, signalling that |
| 69 | + * tenant tracking for this seqnum is incomplete and the refresh must fall back |
| 70 | + * to the full invalidation log. |
| 71 | + */ |
| 72 | +TSDLLEXPORT void |
| 73 | +ts_cagg_tenant_tracking_insert_invalid_marker(int32 hypertable_id, int32 seqnum) |
| 74 | +{ |
| 75 | + Catalog *catalog = ts_catalog_get(); |
| 76 | + Relation rel = table_open(catalog_get_table_id(catalog, CONTINUOUS_AGGS_TENANT_TRACKING), |
| 77 | + RowExclusiveLock); |
| 78 | + TupleDesc desc = RelationGetDescr(rel); |
| 79 | + Datum values[Natts_continuous_aggs_tenant_tracking] = { 0 }; |
| 80 | + bool nulls[Natts_continuous_aggs_tenant_tracking] = { false }; |
| 81 | + CatalogSecurityContext sec_ctx; |
| 82 | + |
| 83 | + values[AttrNumberGetAttrOffset(Anum_continuous_aggs_tenant_tracking_hypertable_id)] = |
| 84 | + Int32GetDatum(hypertable_id); |
| 85 | + nulls[AttrNumberGetAttrOffset(Anum_continuous_aggs_tenant_tracking_tenant_id)] = true; |
| 86 | + nulls[AttrNumberGetAttrOffset(Anum_continuous_aggs_tenant_tracking_min_timestamp)] = true; |
| 87 | + nulls[AttrNumberGetAttrOffset(Anum_continuous_aggs_tenant_tracking_max_timestamp)] = true; |
| 88 | + values[AttrNumberGetAttrOffset(Anum_continuous_aggs_tenant_tracking_seqnum)] = |
| 89 | + Int32GetDatum(seqnum); |
| 90 | + |
| 91 | + ts_catalog_database_info_become_owner(ts_catalog_database_info_get(), &sec_ctx); |
| 92 | + ts_catalog_insert_values(rel, desc, values, nulls); |
| 93 | + ts_catalog_restore_user(&sec_ctx); |
| 94 | + |
| 95 | + table_close(rel, NoLock); |
| 96 | +} |
| 97 | + |
| 98 | +/* Delete all tracking rows for a hypertable. */ |
| 99 | +TSDLLEXPORT void |
| 100 | +ts_cagg_tenant_tracking_delete_by_hypertable_id(int32 hypertable_id) |
| 101 | +{ |
| 102 | + ScanIterator iterator = ts_scan_iterator_create(CONTINUOUS_AGGS_TENANT_TRACKING, |
| 103 | + RowExclusiveLock, |
| 104 | + CurrentMemoryContext); |
| 105 | + |
| 106 | + init_scan_by_hypertable_id(&iterator, hypertable_id); |
| 107 | + |
| 108 | + ts_scanner_foreach(&iterator) |
| 109 | + { |
| 110 | + TupleInfo *ti = ts_scan_iterator_tuple_info(&iterator); |
| 111 | + |
| 112 | + ts_catalog_delete_tid(ti->scanrel, ts_scanner_get_tuple_tid(ti)); |
| 113 | + } |
| 114 | + ts_scan_iterator_close(&iterator); |
| 115 | +} |
0 commit comments