|
| 1 | +/*------------------------------------------------------------------------- |
| 2 | + * |
| 3 | + * pg_logicalinspect.c |
| 4 | + * Functions to inspect contents of PostgreSQL logical snapshots |
| 5 | + * |
| 6 | + * Copyright (c) 2024, PostgreSQL Global Development Group |
| 7 | + * |
| 8 | + * IDENTIFICATION |
| 9 | + * contrib/pg_logicalinspect/pg_logicalinspect.c |
| 10 | + * |
| 11 | + *------------------------------------------------------------------------- |
| 12 | + */ |
| 13 | +#include "postgres.h" |
| 14 | + |
| 15 | +#include "funcapi.h" |
| 16 | +#include "replication/snapbuild_internal.h" |
| 17 | +#include "utils/array.h" |
| 18 | +#include "utils/builtins.h" |
| 19 | +#include "utils/pg_lsn.h" |
| 20 | + |
| 21 | +PG_MODULE_MAGIC; |
| 22 | + |
| 23 | +PG_FUNCTION_INFO_V1(pg_get_logical_snapshot_meta); |
| 24 | +PG_FUNCTION_INFO_V1(pg_get_logical_snapshot_info); |
| 25 | + |
| 26 | +/* Return the description of SnapBuildState */ |
| 27 | +static const char * |
| 28 | +get_snapbuild_state_desc(SnapBuildState state) |
| 29 | +{ |
| 30 | + const char *stateDesc = "unknown state"; |
| 31 | + |
| 32 | + switch (state) |
| 33 | + { |
| 34 | + case SNAPBUILD_START: |
| 35 | + stateDesc = "start"; |
| 36 | + break; |
| 37 | + case SNAPBUILD_BUILDING_SNAPSHOT: |
| 38 | + stateDesc = "building"; |
| 39 | + break; |
| 40 | + case SNAPBUILD_FULL_SNAPSHOT: |
| 41 | + stateDesc = "full"; |
| 42 | + break; |
| 43 | + case SNAPBUILD_CONSISTENT: |
| 44 | + stateDesc = "consistent"; |
| 45 | + break; |
| 46 | + } |
| 47 | + |
| 48 | + return stateDesc; |
| 49 | +} |
| 50 | + |
| 51 | +/* |
| 52 | + * Retrieve the logical snapshot file metadata. |
| 53 | + */ |
| 54 | +Datum |
| 55 | +pg_get_logical_snapshot_meta(PG_FUNCTION_ARGS) |
| 56 | +{ |
| 57 | +#define PG_GET_LOGICAL_SNAPSHOT_META_COLS 3 |
| 58 | + SnapBuildOnDisk ondisk; |
| 59 | + HeapTuple tuple; |
| 60 | + Datum values[PG_GET_LOGICAL_SNAPSHOT_META_COLS] = {0}; |
| 61 | + bool nulls[PG_GET_LOGICAL_SNAPSHOT_META_COLS] = {0}; |
| 62 | + TupleDesc tupdesc; |
| 63 | + char path[MAXPGPATH]; |
| 64 | + int i = 0; |
| 65 | + text *filename_t = PG_GETARG_TEXT_PP(0); |
| 66 | + |
| 67 | + /* Build a tuple descriptor for our result type */ |
| 68 | + if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE) |
| 69 | + elog(ERROR, "return type must be a row type"); |
| 70 | + |
| 71 | + sprintf(path, "%s/%s", |
| 72 | + PG_LOGICAL_SNAPSHOTS_DIR, |
| 73 | + text_to_cstring(filename_t)); |
| 74 | + |
| 75 | + /* Validate and restore the snapshot to 'ondisk' */ |
| 76 | + SnapBuildRestoreSnapshot(&ondisk, path, CurrentMemoryContext, false); |
| 77 | + |
| 78 | + values[i++] = UInt32GetDatum(ondisk.magic); |
| 79 | + values[i++] = Int64GetDatum((int64) ondisk.checksum); |
| 80 | + values[i++] = UInt32GetDatum(ondisk.version); |
| 81 | + |
| 82 | + Assert(i == PG_GET_LOGICAL_SNAPSHOT_META_COLS); |
| 83 | + |
| 84 | + tuple = heap_form_tuple(tupdesc, values, nulls); |
| 85 | + |
| 86 | + PG_RETURN_DATUM(HeapTupleGetDatum(tuple)); |
| 87 | + |
| 88 | +#undef PG_GET_LOGICAL_SNAPSHOT_META_COLS |
| 89 | +} |
| 90 | + |
| 91 | +Datum |
| 92 | +pg_get_logical_snapshot_info(PG_FUNCTION_ARGS) |
| 93 | +{ |
| 94 | +#define PG_GET_LOGICAL_SNAPSHOT_INFO_COLS 14 |
| 95 | + SnapBuildOnDisk ondisk; |
| 96 | + HeapTuple tuple; |
| 97 | + Datum values[PG_GET_LOGICAL_SNAPSHOT_INFO_COLS] = {0}; |
| 98 | + bool nulls[PG_GET_LOGICAL_SNAPSHOT_INFO_COLS] = {0}; |
| 99 | + TupleDesc tupdesc; |
| 100 | + char path[MAXPGPATH]; |
| 101 | + int i = 0; |
| 102 | + text *filename_t = PG_GETARG_TEXT_PP(0); |
| 103 | + |
| 104 | + /* Build a tuple descriptor for our result type */ |
| 105 | + if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE) |
| 106 | + elog(ERROR, "return type must be a row type"); |
| 107 | + |
| 108 | + sprintf(path, "%s/%s", |
| 109 | + PG_LOGICAL_SNAPSHOTS_DIR, |
| 110 | + text_to_cstring(filename_t)); |
| 111 | + |
| 112 | + /* Validate and restore the snapshot to 'ondisk' */ |
| 113 | + SnapBuildRestoreSnapshot(&ondisk, path, CurrentMemoryContext, false); |
| 114 | + |
| 115 | + values[i++] = CStringGetTextDatum(get_snapbuild_state_desc(ondisk.builder.state)); |
| 116 | + values[i++] = TransactionIdGetDatum(ondisk.builder.xmin); |
| 117 | + values[i++] = TransactionIdGetDatum(ondisk.builder.xmax); |
| 118 | + values[i++] = LSNGetDatum(ondisk.builder.start_decoding_at); |
| 119 | + values[i++] = LSNGetDatum(ondisk.builder.two_phase_at); |
| 120 | + values[i++] = TransactionIdGetDatum(ondisk.builder.initial_xmin_horizon); |
| 121 | + values[i++] = BoolGetDatum(ondisk.builder.building_full_snapshot); |
| 122 | + values[i++] = BoolGetDatum(ondisk.builder.in_slot_creation); |
| 123 | + values[i++] = LSNGetDatum(ondisk.builder.last_serialized_snapshot); |
| 124 | + values[i++] = TransactionIdGetDatum(ondisk.builder.next_phase_at); |
| 125 | + |
| 126 | + values[i++] = UInt32GetDatum(ondisk.builder.committed.xcnt); |
| 127 | + if (ondisk.builder.committed.xcnt > 0) |
| 128 | + { |
| 129 | + Datum *arrayelems; |
| 130 | + |
| 131 | + arrayelems = (Datum *) palloc(ondisk.builder.committed.xcnt * sizeof(Datum)); |
| 132 | + |
| 133 | + for (int j = 0; j < ondisk.builder.committed.xcnt; j++) |
| 134 | + arrayelems[j] = TransactionIdGetDatum(ondisk.builder.committed.xip[j]); |
| 135 | + |
| 136 | + values[i++] = PointerGetDatum(construct_array_builtin(arrayelems, |
| 137 | + ondisk.builder.committed.xcnt, |
| 138 | + XIDOID)); |
| 139 | + } |
| 140 | + else |
| 141 | + nulls[i++] = true; |
| 142 | + |
| 143 | + values[i++] = UInt32GetDatum(ondisk.builder.catchange.xcnt); |
| 144 | + if (ondisk.builder.catchange.xcnt > 0) |
| 145 | + { |
| 146 | + Datum *arrayelems; |
| 147 | + |
| 148 | + arrayelems = (Datum *) palloc(ondisk.builder.catchange.xcnt * sizeof(Datum)); |
| 149 | + |
| 150 | + for (int j = 0; j < ondisk.builder.catchange.xcnt; j++) |
| 151 | + arrayelems[j] = TransactionIdGetDatum(ondisk.builder.catchange.xip[j]); |
| 152 | + |
| 153 | + values[i++] = PointerGetDatum(construct_array_builtin(arrayelems, |
| 154 | + ondisk.builder.catchange.xcnt, |
| 155 | + XIDOID)); |
| 156 | + } |
| 157 | + else |
| 158 | + nulls[i++] = true; |
| 159 | + |
| 160 | + Assert(i == PG_GET_LOGICAL_SNAPSHOT_INFO_COLS); |
| 161 | + |
| 162 | + tuple = heap_form_tuple(tupdesc, values, nulls); |
| 163 | + |
| 164 | + PG_RETURN_DATUM(HeapTupleGetDatum(tuple)); |
| 165 | + |
| 166 | +#undef PG_GET_LOGICAL_SNAPSHOT_INFO_COLS |
| 167 | +} |
0 commit comments