|
49 | 49 | #include <utils/inval.h> |
50 | 50 | #include <utils/lsyscache.h> |
51 | 51 | #include <utils/palloc.h> |
| 52 | +#include <utils/snapmgr.h> |
52 | 53 | #include <utils/syscache.h> |
53 | 54 | #include <utils/timestamp.h> |
54 | 55 | #include <utils/typcache.h> |
@@ -94,6 +95,9 @@ TS_FUNCTION_INFO_V1(ts_chunk_drop_single_chunk); |
94 | 95 | TS_FUNCTION_INFO_V1(ts_chunk_attach_osm_table_chunk); |
95 | 96 | TS_FUNCTION_INFO_V1(ts_chunk_drop_osm_chunk); |
96 | 97 | TS_FUNCTION_INFO_V1(ts_chunk_id_from_relid); |
| 98 | +TS_FUNCTION_INFO_V1(ts_chunk_id_by_name); |
| 99 | +TS_FUNCTION_INFO_V1(ts_compressed_chunk_parent_id); |
| 100 | +TS_FUNCTION_INFO_V1(ts_chunk_hypertable_id); |
97 | 101 | TS_FUNCTION_INFO_V1(ts_chunk_show); |
98 | 102 | TS_FUNCTION_INFO_V1(ts_chunk_create); |
99 | 103 | TS_FUNCTION_INFO_V1(ts_chunk_status); |
@@ -2896,6 +2900,139 @@ ts_chunk_id_from_relid(PG_FUNCTION_ARGS) |
2896 | 2900 | PG_RETURN_INT32(last_id); |
2897 | 2901 | } |
2898 | 2902 |
|
| 2903 | +/* |
| 2904 | + * Look up a chunk id by (schema, table) using the active snapshot. |
| 2905 | + * |
| 2906 | + * Reads the snapshot via GetActiveSnapshot() so a logical decoding plugin can |
| 2907 | + * install a HistoricSnapshot via PushActiveSnapshot() before invoking. Returns |
| 2908 | + * NULL when no matching chunk exists. |
| 2909 | + */ |
| 2910 | +Datum |
| 2911 | +ts_chunk_id_by_name(PG_FUNCTION_ARGS) |
| 2912 | +{ |
| 2913 | + Name schema = PG_GETARG_NAME(0); |
| 2914 | + Name table = PG_GETARG_NAME(1); |
| 2915 | + ScanIterator iterator = ts_scan_iterator_create(CHUNK, AccessShareLock, CurrentMemoryContext); |
| 2916 | + int32 chunk_id = INVALID_CHUNK_ID; |
| 2917 | + bool found = false; |
| 2918 | + |
| 2919 | + iterator.ctx.snapshot = GetActiveSnapshot(); |
| 2920 | + iterator.ctx.index = catalog_get_index(ts_catalog_get(), CHUNK, CHUNK_SCHEMA_NAME_INDEX); |
| 2921 | + |
| 2922 | + ts_scan_iterator_scan_key_init(&iterator, |
| 2923 | + Anum_chunk_schema_name_idx_schema_name, |
| 2924 | + BTEqualStrategyNumber, |
| 2925 | + F_NAMEEQ, |
| 2926 | + NameGetDatum(schema)); |
| 2927 | + ts_scan_iterator_scan_key_init(&iterator, |
| 2928 | + Anum_chunk_schema_name_idx_table_name, |
| 2929 | + BTEqualStrategyNumber, |
| 2930 | + F_NAMEEQ, |
| 2931 | + NameGetDatum(table)); |
| 2932 | + |
| 2933 | + ts_scanner_foreach(&iterator) |
| 2934 | + { |
| 2935 | + bool isnull; |
| 2936 | + Datum value = slot_getattr(iterator.tinfo->slot, Anum_chunk_id, &isnull); |
| 2937 | + |
| 2938 | + if (!isnull) |
| 2939 | + { |
| 2940 | + chunk_id = DatumGetInt32(value); |
| 2941 | + found = true; |
| 2942 | + } |
| 2943 | + break; |
| 2944 | + } |
| 2945 | + ts_scan_iterator_close(&iterator); |
| 2946 | + |
| 2947 | + if (!found) |
| 2948 | + PG_RETURN_NULL(); |
| 2949 | + |
| 2950 | + PG_RETURN_INT32(chunk_id); |
| 2951 | +} |
| 2952 | + |
| 2953 | +/* |
| 2954 | + * Look up the parent (uncompressed) chunk id from a compressed chunk id, using |
| 2955 | + * the active snapshot. See ts_chunk_id_by_name for snapshot semantics. |
| 2956 | + */ |
| 2957 | +Datum |
| 2958 | +ts_compressed_chunk_parent_id(PG_FUNCTION_ARGS) |
| 2959 | +{ |
| 2960 | + int32 compressed_chunk_id = PG_GETARG_INT32(0); |
| 2961 | + ScanIterator iterator = ts_scan_iterator_create(CHUNK, AccessShareLock, CurrentMemoryContext); |
| 2962 | + int32 chunk_id = INVALID_CHUNK_ID; |
| 2963 | + bool found = false; |
| 2964 | + |
| 2965 | + iterator.ctx.snapshot = GetActiveSnapshot(); |
| 2966 | + iterator.ctx.index = |
| 2967 | + catalog_get_index(ts_catalog_get(), CHUNK, CHUNK_COMPRESSED_CHUNK_ID_INDEX); |
| 2968 | + |
| 2969 | + ts_scan_iterator_scan_key_init(&iterator, |
| 2970 | + Anum_chunk_compressed_chunk_id_idx_compressed_chunk_id, |
| 2971 | + BTEqualStrategyNumber, |
| 2972 | + F_INT4EQ, |
| 2973 | + Int32GetDatum(compressed_chunk_id)); |
| 2974 | + |
| 2975 | + ts_scanner_foreach(&iterator) |
| 2976 | + { |
| 2977 | + bool isnull; |
| 2978 | + Datum value = slot_getattr(iterator.tinfo->slot, Anum_chunk_id, &isnull); |
| 2979 | + |
| 2980 | + if (!isnull) |
| 2981 | + { |
| 2982 | + chunk_id = DatumGetInt32(value); |
| 2983 | + found = true; |
| 2984 | + } |
| 2985 | + break; |
| 2986 | + } |
| 2987 | + ts_scan_iterator_close(&iterator); |
| 2988 | + |
| 2989 | + if (!found) |
| 2990 | + PG_RETURN_NULL(); |
| 2991 | + |
| 2992 | + PG_RETURN_INT32(chunk_id); |
| 2993 | +} |
| 2994 | + |
| 2995 | +/* |
| 2996 | + * Look up the owning hypertable id of a chunk by chunk id, using the active |
| 2997 | + * snapshot. See ts_chunk_id_by_name for snapshot semantics. |
| 2998 | + */ |
| 2999 | +Datum |
| 3000 | +ts_chunk_hypertable_id(PG_FUNCTION_ARGS) |
| 3001 | +{ |
| 3002 | + int32 chunk_id = PG_GETARG_INT32(0); |
| 3003 | + ScanIterator iterator = ts_scan_iterator_create(CHUNK, AccessShareLock, CurrentMemoryContext); |
| 3004 | + int32 hypertable_id = 0; |
| 3005 | + bool found = false; |
| 3006 | + |
| 3007 | + iterator.ctx.snapshot = GetActiveSnapshot(); |
| 3008 | + iterator.ctx.index = catalog_get_index(ts_catalog_get(), CHUNK, CHUNK_ID_INDEX); |
| 3009 | + |
| 3010 | + ts_scan_iterator_scan_key_init(&iterator, |
| 3011 | + Anum_chunk_idx_id, |
| 3012 | + BTEqualStrategyNumber, |
| 3013 | + F_INT4EQ, |
| 3014 | + Int32GetDatum(chunk_id)); |
| 3015 | + |
| 3016 | + ts_scanner_foreach(&iterator) |
| 3017 | + { |
| 3018 | + bool isnull; |
| 3019 | + Datum value = slot_getattr(iterator.tinfo->slot, Anum_chunk_hypertable_id, &isnull); |
| 3020 | + |
| 3021 | + if (!isnull) |
| 3022 | + { |
| 3023 | + hypertable_id = DatumGetInt32(value); |
| 3024 | + found = true; |
| 3025 | + } |
| 3026 | + break; |
| 3027 | + } |
| 3028 | + ts_scan_iterator_close(&iterator); |
| 3029 | + |
| 3030 | + if (!found) |
| 3031 | + PG_RETURN_NULL(); |
| 3032 | + |
| 3033 | + PG_RETURN_INT32(hypertable_id); |
| 3034 | +} |
| 3035 | + |
2899 | 3036 | bool |
2900 | 3037 | ts_chunk_exists_relid(Oid relid) |
2901 | 3038 | { |
|
0 commit comments