YugabyteDB (2.13.0.0-b42, bfc6a6643e7399ac8a0e81d06a3ee6d6571b33ab)

Coverage Report

Created: 2022-03-09 17:30

/Users/deen/code/yugabyte-db/src/yb/yql/pggate/ybc_pggate.cc
Line
Count
Source (jump to first uncovered line)
1
// Copyright (c) YugaByte, Inc.
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
4
// in compliance with the License.  You may obtain a copy of the License at
5
//
6
// http://www.apache.org/licenses/LICENSE-2.0
7
//
8
// Unless required by applicable law or agreed to in writing, software distributed under the License
9
// is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
10
// or implied.  See the License for the specific language governing permissions and limitations
11
// under the License.
12
13
#include "yb/yql/pggate/ybc_pggate.h"
14
15
#include <algorithm>
16
#include <string>
17
18
#include <cds/init.h> // NOLINT
19
20
#include "yb/client/tablet_server.h"
21
22
#include "yb/common/common_flags.h"
23
#include "yb/common/ql_value.h"
24
#include "yb/common/ybc-internal.h"
25
26
#include "yb/util/atomic.h"
27
#include "yb/util/flag_tags.h"
28
#include "yb/util/thread.h"
29
#include "yb/util/yb_partition.h"
30
31
#include "yb/yql/pggate/pg_memctx.h"
32
#include "yb/yql/pggate/pg_value.h"
33
#include "yb/yql/pggate/pggate.h"
34
#include "yb/yql/pggate/pggate_flags.h"
35
#include "yb/yql/pggate/pggate_thread_local_vars.h"
36
#include "yb/yql/pggate/ybc_pg_typedefs.h"
37
38
DECLARE_bool(client_suppress_created_logs);
39
40
DEFINE_int32(ysql_client_read_write_timeout_ms, -1, "Timeout for YSQL's yb-client read/write "
41
             "operations. Falls back on max(client_read_write_timeout_ms, 600s) if set to -1." );
42
DEFINE_int32(pggate_num_connections_to_server, 1,
43
             "Number of underlying connections to each server from a PostgreSQL backend process. "
44
             "This overrides the value of --num_connections_to_server.");
45
DEFINE_test_flag(uint64, ysql_oid_prefetch_adjustment, 0,
46
                 "Amount to add when prefetch the next batch of OIDs. Never use this flag in "
47
                 "production environment. In unit test we use this flag to force allocation of "
48
                 "large Postgres OIDs.");
49
50
DECLARE_int32(num_connections_to_server);
51
52
DECLARE_int32(delay_alter_sequence_sec);
53
54
DECLARE_int32(client_read_write_timeout_ms);
55
56
namespace yb {
57
namespace pggate {
58
59
//--------------------------------------------------------------------------------------------------
60
// C++ Implementation.
61
// All C++ objects and structures in this module are listed in the following namespace.
62
//--------------------------------------------------------------------------------------------------
63
namespace {
64
65
// Using a raw pointer here to fully control object initialization and destruction.
66
pggate::PgApiImpl* pgapi;
67
std::atomic<bool> pgapi_shutdown_done;
68
69
template<class T>
70
27.5M
YBCStatus ExtractValueFromResult(const Result<T>& result, T* value) {
71
27.5M
  if (result.ok()) {
72
27.5M
    *value = *result;
73
27.5M
    return YBCStatusOK();
74
27.5M
  }
75
18.4E
  return ToYBCStatus(result.status());
76
18.4E
}
ybc_pggate.cc:_ZN2yb6pggate12_GLOBAL__N_122ExtractValueFromResultI12PgColumnInfoEEP15YBCStatusStructRKNS_6ResultIT_EEPS7_
Line
Count
Source
70
27.3M
YBCStatus ExtractValueFromResult(const Result<T>& result, T* value) {
71
27.3M
  if (result.ok()) {
72
27.3M
    *value = *result;
73
27.3M
    return YBCStatusOK();
74
27.3M
  }
75
18.4E
  return ToYBCStatus(result.status());
76
18.4E
}
ybc_pggate.cc:_ZN2yb6pggate12_GLOBAL__N_122ExtractValueFromResultI13scoped_refptrINS0_11PgTableDescEEEEP15YBCStatusStructRKNS_6ResultIT_EEPS9_
Line
Count
Source
70
1.83k
YBCStatus ExtractValueFromResult(const Result<T>& result, T* value) {
71
1.83k
  if (result.ok()) {
72
1.83k
    *value = *result;
73
1.83k
    return YBCStatusOK();
74
1.83k
  }
75
0
  return ToYBCStatus(result.status());
76
0
}
Unexecuted instantiation: ybc_pggate.cc:_ZN2yb6pggate12_GLOBAL__N_122ExtractValueFromResultIbEEP15YBCStatusStructRKNS_6ResultIT_EEPS6_
ybc_pggate.cc:_ZN2yb6pggate12_GLOBAL__N_122ExtractValueFromResultIyEEP15YBCStatusStructRKNS_6ResultIT_EEPS6_
Line
Count
Source
70
158k
YBCStatus ExtractValueFromResult(const Result<T>& result, T* value) {
71
158k
  if (result.ok()) {
72
158k
    *value = *result;
73
158k
    return YBCStatusOK();
74
158k
  }
75
10
  return ToYBCStatus(result.status());
76
10
}
77
78
YBCStatus ProcessYbctid(
79
    const YBCPgYBTupleIdDescriptor& source,
80
2.07M
    const std::function<Status(PgOid, const Slice&)>& processor) {
81
2.07M
  return ToYBCStatus(pgapi->ProcessYBTupleId(
82
2.07M
      source,
83
2.07M
      std::bind(processor, source.table_oid, std::placeholders::_1)));
84
2.07M
}
85
86
1.55M
Slice YbctidAsSlice(uint64_t ybctid) {
87
1.55M
  char* value = NULL;
88
1.55M
  int64_t bytes = 0;
89
1.55M
  pgapi->FindTypeEntity(kByteArrayOid)->datum_to_yb(ybctid, &value, &bytes);
90
1.55M
  return Slice(value, bytes);
91
1.55M
}
92
93
} // anonymous namespace
94
95
//--------------------------------------------------------------------------------------------------
96
// C API.
97
//--------------------------------------------------------------------------------------------------
98
99
void YBCInitPgGateEx(const YBCPgTypeEntity *data_type_table, int count, PgCallbacks pg_callbacks,
100
1.65k
                     PgApiContext* context) {
101
1.65k
  InitThreading();
102
103
2
  CHECK(pgapi == nullptr) << ": " << __PRETTY_FUNCTION__ << " can only be called once";
104
105
1.65k
  YBCInitFlags();
106
107
1.65k
#ifndef NDEBUG
108
1.65k
  HybridTime::TEST_SetPrettyToString(true);
109
1.65k
#endif
110
111
1.65k
  pgapi_shutdown_done.exchange(false);
112
1.65k
  if (context) {
113
0
    pgapi = new pggate::PgApiImpl(std::move(*context), data_type_table, count, pg_callbacks);
114
1.65k
  } else {
115
1.65k
    pgapi = new pggate::PgApiImpl(PgApiContext(), data_type_table, count, pg_callbacks);
116
1.65k
  }
117
0
  VLOG(1) << "PgGate open";
118
1.65k
}
119
120
extern "C" {
121
122
1.65k
void YBCInitPgGate(const YBCPgTypeEntity *data_type_table, int count, PgCallbacks pg_callbacks) {
123
1.65k
  YBCInitPgGateEx(data_type_table, count, pg_callbacks, nullptr);
124
1.65k
}
125
126
1.64k
void YBCDestroyPgGate() {
127
1.64k
  if (pgapi_shutdown_done.exchange(true)) {
128
0
    LOG(DFATAL) << __PRETTY_FUNCTION__ << " should only be called once";
129
1.64k
  } else {
130
1.64k
    pggate::PgApiImpl* local_pgapi = pgapi;
131
1.64k
    pgapi = nullptr; // YBCPgIsYugaByteEnabled() must return false from now on.
132
1.64k
    delete local_pgapi;
133
1.64k
    ClearGlobalPgMemctxMap();
134
0
    VLOG(1) << __PRETTY_FUNCTION__ << " finished";
135
1.64k
  }
136
1.64k
}
137
138
257
const YBCPgCallbacks *YBCGetPgCallbacks() {
139
257
  return pgapi->pg_callbacks();
140
257
}
141
142
0
YBCStatus YBCPgCreateEnv(YBCPgEnv *pg_env) {
143
0
  return ToYBCStatus(pgapi->CreateEnv(pg_env));
144
0
}
145
146
0
YBCStatus YBCPgDestroyEnv(YBCPgEnv pg_env) {
147
0
  return ToYBCStatus(pgapi->DestroyEnv(pg_env));
148
0
}
149
150
YBCStatus YBCPgInitSession(const YBCPgEnv pg_env,
151
1.65k
                           const char *database_name) {
152
1.65k
  const string db_name(database_name ? database_name : "");
153
1.65k
  return ToYBCStatus(pgapi->InitSession(pg_env, db_name));
154
1.65k
}
155
156
218k
YBCPgMemctx YBCPgCreateMemctx() {
157
218k
  return pgapi->CreateMemctx();
158
218k
}
159
160
215k
YBCStatus YBCPgDestroyMemctx(YBCPgMemctx memctx) {
161
215k
  return ToYBCStatus(pgapi->DestroyMemctx(memctx));
162
215k
}
163
164
759k
void YBCPgResetCatalogReadTime() {
165
759k
  return pgapi->ResetCatalogReadTime();
166
759k
}
167
168
1.17M
YBCStatus YBCPgResetMemctx(YBCPgMemctx memctx) {
169
1.17M
  return ToYBCStatus(pgapi->ResetMemctx(memctx));
170
1.17M
}
171
172
2.30M
void YBCPgDeleteStatement(YBCPgStatement handle) {
173
2.30M
  pgapi->DeleteStatement(handle);
174
2.30M
}
175
176
613
YBCStatus YBCPgInvalidateCache() {
177
613
  return ToYBCStatus(pgapi->InvalidateCache());
178
613
}
179
180
21.4M
const YBCPgTypeEntity *YBCPgFindTypeEntity(int type_oid) {
181
21.4M
  return pgapi->FindTypeEntity(type_oid);
182
21.4M
}
183
184
21.4M
YBCPgDataType YBCPgGetType(const YBCPgTypeEntity *type_entity) {
185
21.4M
  if (type_entity) {
186
21.3M
    return type_entity->yb_type;
187
21.3M
  }
188
121k
  return YB_YQL_DATA_TYPE_UNKNOWN_DATA;
189
121k
}
190
191
1.35k
bool YBCPgAllowForPrimaryKey(const YBCPgTypeEntity *type_entity) {
192
1.35k
  if (type_entity) {
193
1.35k
    return type_entity->allow_for_primary_key;
194
1.35k
  }
195
0
  return false;
196
0
}
197
198
//--------------------------------------------------------------------------------------------------
199
// DDL Statements.
200
//--------------------------------------------------------------------------------------------------
201
// Database Operations -----------------------------------------------------------------------------
202
203
0
YBCStatus YBCPgConnectDatabase(const char *database_name) {
204
0
  return ToYBCStatus(pgapi->ConnectDatabase(database_name));
205
0
}
206
207
1.61k
YBCStatus YBCPgIsDatabaseColocated(const YBCPgOid database_oid, bool *colocated) {
208
1.61k
  return ToYBCStatus(pgapi->IsDatabaseColocated(database_oid, colocated));
209
1.61k
}
210
211
YBCStatus YBCPgNewCreateDatabase(const char *database_name,
212
                                 const YBCPgOid database_oid,
213
                                 const YBCPgOid source_database_oid,
214
                                 const YBCPgOid next_oid,
215
                                 const bool colocated,
216
22
                                 YBCPgStatement *handle) {
217
22
  return ToYBCStatus(pgapi->NewCreateDatabase(
218
22
      database_name, database_oid, source_database_oid, next_oid, colocated, handle));
219
22
}
220
221
22
YBCStatus YBCPgExecCreateDatabase(YBCPgStatement handle) {
222
22
  return ToYBCStatus(pgapi->ExecCreateDatabase(handle));
223
22
}
224
225
YBCStatus YBCPgNewDropDatabase(const char *database_name,
226
                               const YBCPgOid database_oid,
227
21
                               YBCPgStatement *handle) {
228
21
  return ToYBCStatus(pgapi->NewDropDatabase(database_name, database_oid, handle));
229
21
}
230
231
21
YBCStatus YBCPgExecDropDatabase(YBCPgStatement handle) {
232
21
  return ToYBCStatus(pgapi->ExecDropDatabase(handle));
233
21
}
234
235
YBCStatus YBCPgNewAlterDatabase(const char *database_name,
236
                               const YBCPgOid database_oid,
237
0
                               YBCPgStatement *handle) {
238
0
  return ToYBCStatus(pgapi->NewAlterDatabase(database_name, database_oid, handle));
239
0
}
240
241
0
YBCStatus YBCPgAlterDatabaseRenameDatabase(YBCPgStatement handle, const char *newname) {
242
0
  return ToYBCStatus(pgapi->AlterDatabaseRenameDatabase(handle, newname));
243
0
}
244
245
0
YBCStatus YBCPgExecAlterDatabase(YBCPgStatement handle) {
246
0
  return ToYBCStatus(pgapi->ExecAlterDatabase(handle));
247
0
}
248
249
YBCStatus YBCPgReserveOids(const YBCPgOid database_oid,
250
                           const YBCPgOid next_oid,
251
                           const uint32_t count,
252
                           YBCPgOid *begin_oid,
253
380
                           YBCPgOid *end_oid) {
254
380
  return ToYBCStatus(pgapi->ReserveOids(database_oid,
255
380
                                        next_oid + static_cast<YBCPgOid>(
256
380
                                          FLAGS_TEST_ysql_oid_prefetch_adjustment),
257
380
                                        count, begin_oid, end_oid));
258
380
}
259
260
0
YBCStatus YBCPgGetCatalogMasterVersion(uint64_t *version) {
261
0
  return ToYBCStatus(pgapi->GetCatalogMasterVersion(version));
262
0
}
263
264
2
YBCStatus YBCPgInvalidateTableCacheByTableId(const char *table_id) {
265
2
  if (table_id == NULL) {
266
0
    return ToYBCStatus(STATUS(InvalidArgument, "table_id is null"));
267
0
  }
268
2
  std::string table_id_str = table_id;
269
2
  const PgObjectId pg_object_id(table_id_str);
270
2
  pgapi->InvalidateTableCache(pg_object_id);
271
2
  return YBCStatusOK();
272
2
}
273
274
// Tablegroup Operations ---------------------------------------------------------------------------
275
276
YBCStatus YBCPgNewCreateTablegroup(const char *database_name,
277
                                   YBCPgOid database_oid,
278
                                   YBCPgOid tablegroup_oid,
279
                                   YBCPgOid tablespace_oid,
280
1
                                   YBCPgStatement *handle) {
281
1
  return ToYBCStatus(pgapi->NewCreateTablegroup(database_name,
282
1
                                                database_oid,
283
1
                                                tablegroup_oid,
284
1
                                                tablespace_oid,
285
1
                                                handle));
286
1
}
287
288
1
YBCStatus YBCPgExecCreateTablegroup(YBCPgStatement handle) {
289
1
  return ToYBCStatus(pgapi->ExecCreateTablegroup(handle));
290
1
}
291
292
YBCStatus YBCPgNewDropTablegroup(YBCPgOid database_oid,
293
                                 YBCPgOid tablegroup_oid,
294
1
                                 YBCPgStatement *handle) {
295
1
  return ToYBCStatus(pgapi->NewDropTablegroup(database_oid,
296
1
                                              tablegroup_oid,
297
1
                                              handle));
298
1
}
299
0
YBCStatus YBCPgExecDropTablegroup(YBCPgStatement handle) {
300
0
  return ToYBCStatus(pgapi->ExecDropTablegroup(handle));
301
0
}
302
303
// Sequence Operations -----------------------------------------------------------------------------
304
305
YBCStatus YBCInsertSequenceTuple(int64_t db_oid,
306
                                 int64_t seq_oid,
307
                                 uint64_t ysql_catalog_version,
308
                                 int64_t last_val,
309
54
                                 bool is_called) {
310
54
  return ToYBCStatus(pgapi->InsertSequenceTuple(
311
54
      db_oid, seq_oid, ysql_catalog_version, last_val, is_called));
312
54
}
313
314
YBCStatus YBCUpdateSequenceTupleConditionally(int64_t db_oid,
315
                                              int64_t seq_oid,
316
                                              uint64_t ysql_catalog_version,
317
                                              int64_t last_val,
318
                                              bool is_called,
319
                                              int64_t expected_last_val,
320
                                              bool expected_is_called,
321
31
                                              bool *skipped) {
322
31
  return ToYBCStatus(
323
31
      pgapi->UpdateSequenceTupleConditionally(db_oid, seq_oid, ysql_catalog_version,
324
31
          last_val, is_called, expected_last_val, expected_is_called, skipped));
325
31
}
326
327
YBCStatus YBCUpdateSequenceTuple(int64_t db_oid,
328
                                 int64_t seq_oid,
329
                                 uint64_t ysql_catalog_version,
330
                                 int64_t last_val,
331
                                 bool is_called,
332
2
                                 bool* skipped) {
333
2
  return ToYBCStatus(pgapi->UpdateSequenceTuple(
334
2
      db_oid, seq_oid, ysql_catalog_version, last_val, is_called, skipped));
335
2
}
336
337
YBCStatus YBCReadSequenceTuple(int64_t db_oid,
338
                               int64_t seq_oid,
339
                               uint64_t ysql_catalog_version,
340
                               int64_t *last_val,
341
69
                               bool *is_called) {
342
69
  return ToYBCStatus(pgapi->ReadSequenceTuple(
343
69
      db_oid, seq_oid, ysql_catalog_version, last_val, is_called));
344
69
}
345
346
44
YBCStatus YBCDeleteSequenceTuple(int64_t db_oid, int64_t seq_oid) {
347
44
  return ToYBCStatus(pgapi->DeleteSequenceTuple(db_oid, seq_oid));
348
44
}
349
350
// Table Operations -------------------------------------------------------------------------------
351
352
YBCStatus YBCPgNewCreateTable(const char *database_name,
353
                              const char *schema_name,
354
                              const char *table_name,
355
                              const YBCPgOid database_oid,
356
                              const YBCPgOid table_oid,
357
                              bool is_shared_table,
358
                              bool if_not_exist,
359
                              bool add_primary_key,
360
                              const bool colocated,
361
                              const YBCPgOid tablegroup_oid,
362
                              const YBCPgOid tablespace_oid,
363
                              const YBCPgOid matview_pg_table_oid,
364
1.27k
                              YBCPgStatement *handle) {
365
1.27k
  const PgObjectId table_id(database_oid, table_oid);
366
1.27k
  const PgObjectId tablegroup_id(database_oid, tablegroup_oid);
367
1.27k
  const PgObjectId tablespace_id(database_oid, tablespace_oid);
368
1.27k
  const PgObjectId matview_pg_table_id(database_oid, matview_pg_table_oid);
369
1.27k
  return ToYBCStatus(pgapi->NewCreateTable(
370
1.27k
      database_name, schema_name, table_name, table_id, is_shared_table,
371
1.27k
      if_not_exist, add_primary_key, colocated, tablegroup_id, tablespace_id, matview_pg_table_id,
372
1.27k
      handle));
373
1.27k
}
374
375
YBCStatus YBCPgCreateTableAddColumn(YBCPgStatement handle, const char *attr_name, int attr_num,
376
                                    const YBCPgTypeEntity *attr_type, bool is_hash, bool is_range,
377
2.84k
                                    bool is_desc, bool is_nulls_first) {
378
2.84k
  return ToYBCStatus(pgapi->CreateTableAddColumn(handle, attr_name, attr_num, attr_type,
379
2.84k
                                                 is_hash, is_range, is_desc, is_nulls_first));
380
2.84k
}
381
382
162
YBCStatus YBCPgCreateTableSetNumTablets(YBCPgStatement handle, int32_t num_tablets) {
383
162
  return ToYBCStatus(pgapi->CreateTableSetNumTablets(handle, num_tablets));
384
162
}
385
386
15
YBCStatus YBCPgAddSplitBoundary(YBCPgStatement handle, YBCPgExpr *exprs, int expr_count) {
387
15
  return ToYBCStatus(pgapi->AddSplitBoundary(handle, exprs, expr_count));
388
15
}
389
390
1.26k
YBCStatus YBCPgExecCreateTable(YBCPgStatement handle) {
391
1.26k
  return ToYBCStatus(pgapi->ExecCreateTable(handle));
392
1.26k
}
393
394
YBCStatus YBCPgNewAlterTable(const YBCPgOid database_oid,
395
                             const YBCPgOid table_oid,
396
661
                             YBCPgStatement *handle) {
397
661
  const PgObjectId table_id(database_oid, table_oid);
398
661
  return ToYBCStatus(pgapi->NewAlterTable(table_id, handle));
399
661
}
400
401
YBCStatus YBCPgAlterTableAddColumn(YBCPgStatement handle, const char *name, int order,
402
69
                                   const YBCPgTypeEntity *attr_type) {
403
69
  return ToYBCStatus(pgapi->AlterTableAddColumn(handle, name, order, attr_type));
404
69
}
405
406
YBCStatus YBCPgAlterTableRenameColumn(YBCPgStatement handle, const char *oldname,
407
1
                                      const char *newname) {
408
1
  return ToYBCStatus(pgapi->AlterTableRenameColumn(handle, oldname, newname));
409
1
}
410
411
116
YBCStatus YBCPgAlterTableDropColumn(YBCPgStatement handle, const char *name) {
412
116
  return ToYBCStatus(pgapi->AlterTableDropColumn(handle, name));
413
116
}
414
415
YBCStatus YBCPgAlterTableRenameTable(YBCPgStatement handle, const char *db_name,
416
42
                                     const char *newname) {
417
42
  return ToYBCStatus(pgapi->AlterTableRenameTable(handle, db_name, newname));
418
42
}
419
420
155
YBCStatus YBCPgExecAlterTable(YBCPgStatement handle) {
421
155
  return ToYBCStatus(pgapi->ExecAlterTable(handle));
422
155
}
423
424
YBCStatus YBCPgNewDropTable(const YBCPgOid database_oid,
425
                            const YBCPgOid table_oid,
426
                            bool if_exist,
427
1.03k
                            YBCPgStatement *handle) {
428
1.03k
  const PgObjectId table_id(database_oid, table_oid);
429
1.03k
  return ToYBCStatus(pgapi->NewDropTable(table_id, if_exist, handle));
430
1.03k
}
431
432
YBCStatus YBCPgGetTableDesc(const YBCPgOid database_oid,
433
                            const YBCPgOid table_oid,
434
5.96M
                            YBCPgTableDesc *handle) {
435
5.96M
  const PgObjectId table_id(database_oid, table_oid);
436
5.96M
  return ToYBCStatus(pgapi->GetTableDesc(table_id, handle));
437
5.96M
}
438
439
YBCStatus YBCPgGetColumnInfo(YBCPgTableDesc table_desc,
440
                             int16_t attr_number,
441
26.2M
                             YBCPgColumnInfo *column_info) {
442
26.2M
  return ExtractValueFromResult(pgapi->GetColumnInfo(table_desc, attr_number), column_info);
443
26.2M
}
444
445
YBCStatus YBCPgSetCatalogCacheVersion(YBCPgStatement handle,
446
2.31M
                                      uint64_t catalog_cache_version) {
447
2.31M
  return ToYBCStatus(pgapi->SetCatalogCacheVersion(handle, catalog_cache_version));
448
2.31M
}
449
450
0
YBCStatus YBCPgDmlModifiesRow(YBCPgStatement handle, bool *modifies_row) {
451
0
  return ToYBCStatus(pgapi->DmlModifiesRow(handle, modifies_row));
452
0
}
453
454
0
YBCStatus YBCPgSetIsSysCatalogVersionChange(YBCPgStatement handle) {
455
0
  return ToYBCStatus(pgapi->SetIsSysCatalogVersionChange(handle));
456
0
}
457
458
YBCStatus YBCPgNewTruncateTable(const YBCPgOid database_oid,
459
                                const YBCPgOid table_oid,
460
31
                                YBCPgStatement *handle) {
461
31
  const PgObjectId table_id(database_oid, table_oid);
462
31
  return ToYBCStatus(pgapi->NewTruncateTable(table_id, handle));
463
31
}
464
465
31
YBCStatus YBCPgExecTruncateTable(YBCPgStatement handle) {
466
31
  return ToYBCStatus(pgapi->ExecTruncateTable(handle));
467
31
}
468
469
YBCStatus YBCPgIsTableColocated(const YBCPgOid database_oid,
470
                                const YBCPgOid table_oid,
471
1.83k
                                bool *colocated) {
472
1.83k
  const PgObjectId table_id(database_oid, table_oid);
473
1.83k
  PgTableDescPtr table_desc;
474
1.83k
  YBCStatus status = ExtractValueFromResult(pgapi->LoadTable(table_id), &table_desc);
475
1.83k
  if (status) {
476
0
    return status;
477
1.83k
  } else {
478
1.83k
    *colocated = table_desc->IsColocated();
479
1.83k
    return YBCStatusOK();
480
1.83k
  }
481
1.83k
}
482
483
YBCStatus YBCPgGetTableProperties(YBCPgTableDesc table_desc,
484
81
                                  YBCPgTableProperties *properties) {
485
81
  CHECK_NOTNULL(properties)->num_tablets = table_desc->GetPartitionCount();
486
81
  properties->num_hash_key_columns = table_desc->num_hash_key_columns();
487
81
  properties->is_colocated = table_desc->IsColocated();
488
81
  return YBCStatusOK();
489
81
}
490
491
YBCStatus YBCPgTableExists(const YBCPgOid database_oid,
492
                           const YBCPgOid table_oid,
493
3.32k
                           bool *exists) {
494
3.32k
  const PgObjectId table_id(database_oid, table_oid);
495
3.32k
  const auto result = pgapi->LoadTable(table_id);
496
497
3.32k
  if (result.ok()) {
498
3.32k
    *exists = true;
499
3.32k
    return YBCStatusOK();
500
1
  } else if (result.status().IsNotFound()) {
501
0
    *exists = false;
502
0
    return YBCStatusOK();
503
1
  } else {
504
1
    return ToYBCStatus(result.status());
505
1
  }
506
3.32k
}
507
508
// Index Operations -------------------------------------------------------------------------------
509
510
YBCStatus YBCPgNewCreateIndex(const char *database_name,
511
                              const char *schema_name,
512
                              const char *index_name,
513
                              const YBCPgOid database_oid,
514
                              const YBCPgOid index_oid,
515
                              const YBCPgOid table_oid,
516
                              bool is_shared_index,
517
                              bool is_unique_index,
518
                              const bool skip_index_backfill,
519
                              bool if_not_exist,
520
                              const YBCPgOid tablegroup_oid,
521
                              const YBCPgOid tablespace_oid,
522
151
                              YBCPgStatement *handle) {
523
151
  const PgObjectId index_id(database_oid, index_oid);
524
151
  const PgObjectId table_id(database_oid, table_oid);
525
151
  const PgObjectId tablegroup_id(database_oid, tablegroup_oid);
526
151
  const PgObjectId tablespace_id(database_oid, tablespace_oid);
527
151
  return ToYBCStatus(pgapi->NewCreateIndex(database_name, schema_name, index_name, index_id,
528
151
                                           table_id, is_shared_index, is_unique_index,
529
151
                                           skip_index_backfill, if_not_exist, tablegroup_id,
530
151
                                           tablespace_id, handle));
531
151
}
532
533
YBCStatus YBCPgCreateIndexAddColumn(YBCPgStatement handle, const char *attr_name, int attr_num,
534
                                    const YBCPgTypeEntity *attr_type, bool is_hash, bool is_range,
535
194
                                    bool is_desc, bool is_nulls_first) {
536
194
  return ToYBCStatus(pgapi->CreateIndexAddColumn(handle, attr_name, attr_num, attr_type,
537
194
                                                 is_hash, is_range, is_desc, is_nulls_first));
538
194
}
539
540
0
YBCStatus YBCPgCreateIndexSetNumTablets(YBCPgStatement handle, int32_t num_tablets) {
541
0
  return ToYBCStatus(pgapi->CreateIndexSetNumTablets(handle, num_tablets));
542
0
}
543
544
151
YBCStatus YBCPgExecCreateIndex(YBCPgStatement handle) {
545
151
  return ToYBCStatus(pgapi->ExecCreateIndex(handle));
546
151
}
547
548
YBCStatus YBCPgNewDropIndex(const YBCPgOid database_oid,
549
                            const YBCPgOid index_oid,
550
                            bool if_exist,
551
141
                            YBCPgStatement *handle) {
552
141
  const PgObjectId index_id(database_oid, index_oid);
553
141
  return ToYBCStatus(pgapi->NewDropIndex(index_id, if_exist, handle));
554
141
}
555
556
1.17k
YBCStatus YBCPgExecPostponedDdlStmt(YBCPgStatement handle) {
557
1.17k
  return ToYBCStatus(pgapi->ExecPostponedDdlStmt(handle));
558
1.17k
}
559
560
YBCStatus YBCPgBackfillIndex(
561
    const YBCPgOid database_oid,
562
89
    const YBCPgOid index_oid) {
563
89
  const PgObjectId index_id(database_oid, index_oid);
564
89
  return ToYBCStatus(pgapi->BackfillIndex(index_id));
565
89
}
566
567
//--------------------------------------------------------------------------------------------------
568
// DML Statements.
569
//--------------------------------------------------------------------------------------------------
570
571
7.54M
YBCStatus YBCPgDmlAppendTarget(YBCPgStatement handle, YBCPgExpr target) {
572
7.54M
  return ToYBCStatus(pgapi->DmlAppendTarget(handle, target));
573
7.54M
}
574
575
22
YBCStatus YbPgDmlAppendQual(YBCPgStatement handle, YBCPgExpr qual) {
576
22
  return ToYBCStatus(pgapi->DmlAppendQual(handle, qual));
577
22
}
578
579
3.94k
YBCStatus YbPgDmlAppendColumnRef(YBCPgStatement handle, YBCPgExpr colref) {
580
3.94k
  return ToYBCStatus(pgapi->DmlAppendColumnRef(handle, colref));
581
3.94k
}
582
583
9.75M
YBCStatus YBCPgDmlBindColumn(YBCPgStatement handle, int attr_num, YBCPgExpr attr_value) {
584
9.75M
  return ToYBCStatus(pgapi->DmlBindColumn(handle, attr_num, attr_value));
585
9.75M
}
586
587
YBCStatus YBCPgDmlBindColumnCondBetween(YBCPgStatement handle, int attr_num, YBCPgExpr attr_value,
588
134
    YBCPgExpr attr_value_end) {
589
134
  return ToYBCStatus(pgapi->DmlBindColumnCondBetween(handle, attr_num, attr_value, attr_value_end));
590
134
}
591
592
YBCStatus YBCPgDmlBindColumnCondIn(YBCPgStatement handle, int attr_num, int n_attr_values,
593
45.5k
    YBCPgExpr *attr_values) {
594
45.5k
  return ToYBCStatus(pgapi->DmlBindColumnCondIn(handle, attr_num, n_attr_values, attr_values));
595
45.5k
}
596
597
YBCStatus YBCPgDmlBindHashCodes(YBCPgStatement handle, bool start_valid,
598
                                 bool start_inclusive, uint64_t start_hash_val,
599
                                 bool end_valid, bool end_inclusive,
600
0
                                 uint64_t end_hash_val) {
601
0
  return ToYBCStatus(pgapi->DmlBindHashCode(handle, start_valid,
602
0
                      start_inclusive, start_hash_val, end_valid,
603
0
                      end_inclusive, end_hash_val));
604
0
}
605
606
19
YBCStatus YBCPgDmlBindTable(YBCPgStatement handle) {
607
19
  return ToYBCStatus(pgapi->DmlBindTable(handle));
608
19
}
609
610
1.16M
YBCStatus YBCPgDmlGetColumnInfo(YBCPgStatement handle, int attr_num, YBCPgColumnInfo* column_info) {
611
1.16M
  return ExtractValueFromResult(pgapi->DmlGetColumnInfo(handle, attr_num), column_info);
612
1.16M
}
613
614
YBCStatus YBCPgDmlAssignColumn(YBCPgStatement handle,
615
                               int attr_num,
616
259k
                               YBCPgExpr attr_value) {
617
259k
  return ToYBCStatus(pgapi->DmlAssignColumn(handle, attr_num, attr_value));
618
259k
}
619
620
YBCStatus YBCPgDmlFetch(YBCPgStatement handle, int32_t natts, uint64_t *values, bool *isnulls,
621
20.5M
                        YBCPgSysColumns *syscols, bool *has_data) {
622
20.5M
  return ToYBCStatus(pgapi->DmlFetch(handle, natts, values, isnulls, syscols, has_data));
623
20.5M
}
624
625
201k
YBCStatus YBCPgStartOperationsBuffering() {
626
201k
  return ToYBCStatus(pgapi->StartOperationsBuffering());
627
201k
}
628
629
174k
YBCStatus YBCPgStopOperationsBuffering() {
630
174k
  return ToYBCStatus(pgapi->StopOperationsBuffering());
631
174k
}
632
633
48.0k
void YBCPgResetOperationsBuffering() {
634
48.0k
  pgapi->ResetOperationsBuffering();
635
48.0k
}
636
637
185k
YBCStatus YBCPgFlushBufferedOperations() {
638
185k
  return ToYBCStatus(pgapi->FlushBufferedOperations());
639
185k
}
640
641
2.12M
YBCStatus YBCPgDmlExecWriteOp(YBCPgStatement handle, int32_t *rows_affected_count) {
642
2.12M
  return ToYBCStatus(pgapi->DmlExecWriteOp(handle, rows_affected_count));
643
2.12M
}
644
645
1.57M
YBCStatus YBCPgBuildYBTupleId(const YBCPgYBTupleIdDescriptor *source, uint64_t *ybctid) {
646
1.57M
  return ProcessYbctid(*source, [ybctid](const auto&, const auto& yid) {
647
1.57M
    const auto* type_entity = pgapi->FindTypeEntity(kByteArrayOid);
648
1.57M
    *ybctid = type_entity->yb_to_datum(yid.cdata(), yid.size(), nullptr /* type_attrs */);
649
1.57M
    return Status::OK();
650
1.57M
  });
651
1.57M
}
652
653
YBCStatus YBCPgNewSample(const YBCPgOid database_oid,
654
                         const YBCPgOid table_oid,
655
                         const int targrows,
656
79
                         YBCPgStatement *handle) {
657
79
  const PgObjectId table_id(database_oid, table_oid);
658
79
  return ToYBCStatus(pgapi->NewSample(table_id, targrows, handle));
659
79
}
660
661
79
YBCStatus YBCPgInitRandomState(YBCPgStatement handle, double rstate_w, uint64_t rand_state) {
662
79
  return ToYBCStatus(pgapi->InitRandomState(handle, rstate_w, rand_state));
663
79
}
664
665
270
YBCStatus YBCPgSampleNextBlock(YBCPgStatement handle, bool *has_more) {
666
270
  return ToYBCStatus(pgapi->SampleNextBlock(handle, has_more));
667
270
}
668
669
79
YBCStatus YBCPgExecSample(YBCPgStatement handle) {
670
79
  return ToYBCStatus(pgapi->ExecSample(handle));
671
79
}
672
673
79
YBCStatus YBCPgGetEstimatedRowCount(YBCPgStatement handle, double *liverows, double *deadrows) {
674
79
  return ToYBCStatus(pgapi->GetEstimatedRowCount(handle, liverows, deadrows));
675
79
}
676
677
// INSERT Operations -------------------------------------------------------------------------------
678
YBCStatus YBCPgNewInsert(const YBCPgOid database_oid,
679
                         const YBCPgOid table_oid,
680
                         const bool is_single_row_txn,
681
1.84M
                         YBCPgStatement *handle) {
682
1.84M
  const PgObjectId table_id(database_oid, table_oid);
683
1.84M
  return ToYBCStatus(pgapi->NewInsert(table_id, is_single_row_txn, handle));
684
1.84M
}
685
686
0
YBCStatus YBCPgExecInsert(YBCPgStatement handle) {
687
0
  return ToYBCStatus(pgapi->ExecInsert(handle));
688
0
}
689
690
228k
YBCStatus YBCPgInsertStmtSetUpsertMode(YBCPgStatement handle) {
691
228k
  return ToYBCStatus(pgapi->InsertStmtSetUpsertMode(handle));
692
228k
}
693
694
103k
YBCStatus YBCPgInsertStmtSetWriteTime(YBCPgStatement handle, const uint64_t write_time) {
695
103k
  HybridTime write_hybrid_time;
696
103k
  YBCStatus status = ToYBCStatus(write_hybrid_time.FromUint64(write_time));
697
103k
  if (status) {
698
0
    return status;
699
103k
  } else {
700
103k
    return ToYBCStatus(pgapi->InsertStmtSetWriteTime(handle, write_hybrid_time));
701
103k
  }
702
103k
}
703
704
103k
YBCStatus YBCPgInsertStmtSetIsBackfill(YBCPgStatement handle, const bool is_backfill) {
705
103k
  return ToYBCStatus(pgapi->InsertStmtSetIsBackfill(handle, is_backfill));
706
103k
}
707
708
// UPDATE Operations -------------------------------------------------------------------------------
709
YBCStatus YBCPgNewUpdate(const YBCPgOid database_oid,
710
                         const YBCPgOid table_oid,
711
                         bool is_single_row_txn,
712
166k
                         YBCPgStatement *handle) {
713
166k
  const PgObjectId table_id(database_oid, table_oid);
714
166k
  return ToYBCStatus(pgapi->NewUpdate(table_id, is_single_row_txn, handle));
715
166k
}
716
717
0
YBCStatus YBCPgExecUpdate(YBCPgStatement handle) {
718
0
  return ToYBCStatus(pgapi->ExecUpdate(handle));
719
0
}
720
721
// DELETE Operations -------------------------------------------------------------------------------
722
YBCStatus YBCPgNewDelete(const YBCPgOid database_oid,
723
                         const YBCPgOid table_oid,
724
                         bool is_single_row_txn,
725
116k
                         YBCPgStatement *handle) {
726
116k
  const PgObjectId table_id(database_oid, table_oid);
727
116k
  return ToYBCStatus(pgapi->NewDelete(table_id, is_single_row_txn, handle));
728
116k
}
729
730
0
YBCStatus YBCPgExecDelete(YBCPgStatement handle) {
731
0
  return ToYBCStatus(pgapi->ExecDelete(handle));
732
0
}
733
734
0
YBCStatus YBCPgDeleteStmtSetIsPersistNeeded(YBCPgStatement handle, const bool is_persist_needed) {
735
0
  return ToYBCStatus(pgapi->DeleteStmtSetIsPersistNeeded(handle, is_persist_needed));
736
0
}
737
738
// Colocated TRUNCATE Operations -------------------------------------------------------------------
739
YBCStatus YBCPgNewTruncateColocated(const YBCPgOid database_oid,
740
                                    const YBCPgOid table_oid,
741
                                    bool is_single_row_txn,
742
19
                                    YBCPgStatement *handle) {
743
19
  const PgObjectId table_id(database_oid, table_oid);
744
19
  return ToYBCStatus(pgapi->NewTruncateColocated(table_id, is_single_row_txn, handle));
745
19
}
746
747
0
YBCStatus YBCPgExecTruncateColocated(YBCPgStatement handle) {
748
0
  return ToYBCStatus(pgapi->ExecTruncateColocated(handle));
749
0
}
750
751
// SELECT Operations -------------------------------------------------------------------------------
752
YBCStatus YBCPgNewSelect(const YBCPgOid database_oid,
753
                         const YBCPgOid table_oid,
754
                         const YBCPgPrepareParameters *prepare_params,
755
561k
                         YBCPgStatement *handle) {
756
561k
  const PgObjectId table_id(database_oid, table_oid);
757
561k
  const PgObjectId index_id(database_oid,
758
526k
                            prepare_params ? prepare_params->index_oid : kInvalidOid);
759
561k
  return ToYBCStatus(pgapi->NewSelect(table_id, index_id, prepare_params, handle));
760
561k
}
761
762
526k
YBCStatus YBCPgSetForwardScan(YBCPgStatement handle, bool is_forward_scan) {
763
526k
  return ToYBCStatus(pgapi->SetForwardScan(handle, is_forward_scan));
764
526k
}
765
766
561k
YBCStatus YBCPgExecSelect(YBCPgStatement handle, const YBCPgExecParameters *exec_params) {
767
561k
  return ToYBCStatus(pgapi->ExecSelect(handle, exec_params));
768
561k
}
769
770
//--------------------------------------------------------------------------------------------------
771
// Expression Operations
772
//--------------------------------------------------------------------------------------------------
773
774
YBCStatus YBCPgNewColumnRef(
775
    YBCPgStatement stmt, int attr_num, const YBCPgTypeEntity *type_entity,
776
7.55M
    bool collate_is_valid_non_c, const YBCPgTypeAttrs *type_attrs, YBCPgExpr *expr_handle) {
777
7.55M
  return ToYBCStatus(pgapi->NewColumnRef(
778
7.55M
      stmt, attr_num, type_entity, collate_is_valid_non_c, type_attrs, expr_handle));
779
7.55M
}
780
781
YBCStatus YBCPgNewConstant(
782
    YBCPgStatement stmt, const YBCPgTypeEntity *type_entity, bool collate_is_valid_non_c,
783
11.3M
    const char *collation_sortkey, uint64_t datum, bool is_null, YBCPgExpr *expr_handle) {
784
11.3M
  return ToYBCStatus(pgapi->NewConstant(
785
11.3M
      stmt, type_entity, collate_is_valid_non_c, collation_sortkey, datum, is_null, expr_handle));
786
11.3M
}
787
788
YBCStatus YBCPgNewConstantVirtual(
789
    YBCPgStatement stmt, const YBCPgTypeEntity *type_entity,
790
8
    YBCPgDatumKind datum_kind, YBCPgExpr *expr_handle) {
791
8
  return ToYBCStatus(pgapi->NewConstantVirtual(stmt, type_entity, datum_kind, expr_handle));
792
8
}
793
794
YBCStatus YBCPgNewConstantOp(
795
    YBCPgStatement stmt, const YBCPgTypeEntity *type_entity, bool collate_is_valid_non_c,
796
    const char *collation_sortkey, uint64_t datum, bool is_null, YBCPgExpr *expr_handle,
797
0
    bool is_gt) {
798
0
  return ToYBCStatus(pgapi->NewConstantOp(
799
0
      stmt, type_entity, collate_is_valid_non_c, collation_sortkey, datum, is_null, expr_handle,
800
0
      is_gt));
801
0
}
802
803
// Overwriting the expression's result with any desired values.
804
0
YBCStatus YBCPgUpdateConstInt2(YBCPgExpr expr, int16_t value, bool is_null) {
805
0
  return ToYBCStatus(pgapi->UpdateConstant(expr, value, is_null));
806
0
}
807
808
0
YBCStatus YBCPgUpdateConstInt4(YBCPgExpr expr, int32_t value, bool is_null) {
809
0
  return ToYBCStatus(pgapi->UpdateConstant(expr, value, is_null));
810
0
}
811
812
0
YBCStatus YBCPgUpdateConstInt8(YBCPgExpr expr, int64_t value, bool is_null) {
813
0
  return ToYBCStatus(pgapi->UpdateConstant(expr, value, is_null));
814
0
}
815
816
0
YBCStatus YBCPgUpdateConstFloat4(YBCPgExpr expr, float value, bool is_null) {
817
0
  return ToYBCStatus(pgapi->UpdateConstant(expr, value, is_null));
818
0
}
819
820
0
YBCStatus YBCPgUpdateConstFloat8(YBCPgExpr expr, double value, bool is_null) {
821
0
  return ToYBCStatus(pgapi->UpdateConstant(expr, value, is_null));
822
0
}
823
824
0
YBCStatus YBCPgUpdateConstText(YBCPgExpr expr, const char *value, bool is_null) {
825
0
  return ToYBCStatus(pgapi->UpdateConstant(expr, value, is_null));
826
0
}
827
828
0
YBCStatus YBCPgUpdateConstChar(YBCPgExpr expr, const char *value,  int64_t bytes, bool is_null) {
829
0
  return ToYBCStatus(pgapi->UpdateConstant(expr, value, bytes, is_null));
830
0
}
831
832
YBCStatus YBCPgNewOperator(
833
    YBCPgStatement stmt, const char *opname, const YBCPgTypeEntity *type_entity,
834
5.98k
    bool collate_is_valid_non_c, YBCPgExpr *op_handle) {
835
5.98k
  return ToYBCStatus(pgapi->NewOperator(
836
5.98k
      stmt, opname, type_entity, collate_is_valid_non_c, op_handle));
837
5.98k
}
838
839
5.98k
YBCStatus YBCPgOperatorAppendArg(YBCPgExpr op_handle, YBCPgExpr arg) {
840
5.98k
  return ToYBCStatus(pgapi->OperatorAppendArg(op_handle, arg));
841
5.98k
}
842
843
YBCStatus YBCGetDocDBKeySize(uint64_t data, const YBCPgTypeEntity *typeentity,
844
0
                            bool is_null, size_t *type_size) {
845
846
0
  if (typeentity == nullptr
847
0
      || typeentity->yb_type == YB_YQL_DATA_TYPE_UNKNOWN_DATA
848
0
      || !typeentity->allow_for_primary_key) {
849
0
    return YBCStatusNotSupport("");
850
0
  }
851
852
0
  if (typeentity->datum_fixed_size > 0) {
853
0
    *type_size = typeentity->datum_fixed_size;
854
0
    return YBCStatusOK();
855
0
  }
856
857
0
  QLValue val;
858
0
  Status status = pggate::PgValueToPB(typeentity, data, is_null, &val);
859
0
  if (!status.IsOk()) {
860
0
    return ToYBCStatus(status);
861
0
  }
862
863
0
  string key_buf;
864
0
  AppendToKey(val.value(), &key_buf);
865
0
  *type_size = key_buf.size();
866
0
  return YBCStatusOK();
867
0
}
868
869
870
YBCStatus YBCAppendDatumToKey(uint64_t data, const YBCPgTypeEntity *typeentity,
871
                              bool is_null, char *key_ptr,
872
0
                              size_t *bytes_written) {
873
0
  QLValue val;
874
875
0
  Status status = pggate::PgValueToPB(typeentity, data, is_null, &val);
876
0
  if (!status.IsOk()) {
877
0
    return ToYBCStatus(status);
878
0
  }
879
880
0
  string key_buf;
881
0
  AppendToKey(val.value(), &key_buf);
882
0
  memcpy(key_ptr, key_buf.c_str(), key_buf.size());
883
0
  *bytes_written = key_buf.size();
884
0
  return YBCStatusOK();
885
0
}
886
887
0
uint16_t YBCCompoundHash(const char *key, size_t length) {
888
0
  return YBPartition::HashColumnCompoundValue(string(key, length));
889
0
}
890
891
//------------------------------------------------------------------------------------------------
892
// Transaction operation.
893
//------------------------------------------------------------------------------------------------
894
895
160k
YBCStatus YBCPgBeginTransaction() {
896
160k
  return ToYBCStatus(pgapi->BeginTransaction());
897
160k
}
898
899
20.2k
YBCStatus YBCPgRecreateTransaction() {
900
20.2k
  return ToYBCStatus(pgapi->RecreateTransaction());
901
20.2k
}
902
903
0
YBCStatus YBCPgRestartTransaction() {
904
0
  return ToYBCStatus(pgapi->RestartTransaction());
905
0
}
906
907
24.3k
YBCStatus YBCPgResetTransactionReadPoint() {
908
24.3k
  return ToYBCStatus(pgapi->ResetTransactionReadPoint());
909
24.3k
}
910
911
1
YBCStatus YBCPgRestartReadPoint() {
912
1
  return ToYBCStatus(pgapi->RestartReadPoint());
913
1
}
914
915
155k
YBCStatus YBCPgCommitTransaction() {
916
155k
  return ToYBCStatus(pgapi->CommitTransaction());
917
155k
}
918
919
11.6k
YBCStatus YBCPgAbortTransaction() {
920
11.6k
  return ToYBCStatus(pgapi->AbortTransaction());
921
11.6k
}
922
923
161k
YBCStatus YBCPgSetTransactionIsolationLevel(int isolation) {
924
161k
  return ToYBCStatus(pgapi->SetTransactionIsolationLevel(isolation));
925
161k
}
926
927
160k
YBCStatus YBCPgSetTransactionReadOnly(bool read_only) {
928
160k
  return ToYBCStatus(pgapi->SetTransactionReadOnly(read_only));
929
160k
}
930
931
160k
YBCStatus YBCPgEnableFollowerReads(bool enable_follower_reads, int32_t staleness_ms) {
932
160k
  return ToYBCStatus(pgapi->EnableFollowerReads(enable_follower_reads, staleness_ms));
933
160k
}
934
935
160k
YBCStatus YBCPgSetTransactionDeferrable(bool deferrable) {
936
160k
  return ToYBCStatus(pgapi->SetTransactionDeferrable(deferrable));
937
160k
}
938
939
6.33k
YBCStatus YBCPgEnterSeparateDdlTxnMode() {
940
6.33k
  return ToYBCStatus(pgapi->EnterSeparateDdlTxnMode());
941
6.33k
}
942
943
5.94k
YBCStatus YBCPgExitSeparateDdlTxnMode() {
944
5.94k
  return ToYBCStatus(pgapi->ExitSeparateDdlTxnMode());
945
5.94k
}
946
947
421
void YBCPgClearSeparateDdlTxnMode() {
948
421
  pgapi->ClearSeparateDdlTxnMode();
949
421
}
950
951
48.8k
YBCStatus YBCPgSetActiveSubTransaction(uint32_t id) {
952
48.8k
  return ToYBCStatus(pgapi->SetActiveSubTransaction(id));
953
48.8k
}
954
955
23.5k
YBCStatus YBCPgRollbackSubTransaction(uint32_t id) {
956
23.5k
  return ToYBCStatus(pgapi->RollbackSubTransaction(id));
957
23.5k
}
958
959
//------------------------------------------------------------------------------------------------
960
// System validation.
961
//------------------------------------------------------------------------------------------------
962
0
YBCStatus YBCPgValidatePlacement(const char *placement_info) {
963
0
  return ToYBCStatus(pgapi->ValidatePlacement(placement_info));
964
0
}
965
966
// Referential Integrity Caching
967
21.6k
YBCStatus YBCPgForeignKeyReferenceCacheDelete(const YBCPgYBTupleIdDescriptor *source) {
968
21.6k
  return ProcessYbctid(*source, [](auto table_id, const auto& ybctid){
969
21.6k
    pgapi->DeleteForeignKeyReference(table_id, ybctid);
970
21.6k
    return Status::OK();
971
21.6k
  });
972
21.6k
}
973
974
41.3k
void YBCPgDeleteFromForeignKeyReferenceCache(YBCPgOid table_oid, uint64_t ybctid) {
975
41.3k
  pgapi->DeleteForeignKeyReference(table_oid, YbctidAsSlice(ybctid));
976
41.3k
}
977
978
1.51M
void YBCPgAddIntoForeignKeyReferenceCache(YBCPgOid table_oid, uint64_t ybctid) {
979
1.51M
  pgapi->AddForeignKeyReference(table_oid, YbctidAsSlice(ybctid));
980
1.51M
}
981
982
232k
YBCStatus YBCForeignKeyReferenceExists(const YBCPgYBTupleIdDescriptor *source, bool* res) {
983
232k
  return ProcessYbctid(*source, [res, source](auto table_id, const auto& ybctid) -> Status {
984
232k
    *res = VERIFY_RESULT(pgapi->ForeignKeyReferenceExists(table_id, ybctid, source->database_oid));
985
232k
    return Status::OK();
986
232k
  });
987
232k
}
988
989
240k
YBCStatus YBCAddForeignKeyReferenceIntent(const YBCPgYBTupleIdDescriptor *source) {
990
240k
  return ProcessYbctid(*source, [](auto table_id, const auto& ybctid) {
991
240k
    pgapi->AddForeignKeyReferenceIntent(table_id, ybctid);
992
240k
    return Status::OK();
993
240k
  });
994
240k
}
995
996
3.23M
bool YBCIsInitDbModeEnvVarSet() {
997
3.23M
  static bool cached_value = false;
998
3.23M
  static bool cached = false;
999
1000
3.23M
  if (!cached) {
1001
1.65k
    const char* initdb_mode_env_var_value = getenv("YB_PG_INITDB_MODE");
1002
1.65k
    cached_value = initdb_mode_env_var_value && strcmp(initdb_mode_env_var_value, "1") == 0;
1003
1.65k
    cached = true;
1004
1.65k
  }
1005
1006
3.23M
  return cached_value;
1007
3.23M
}
1008
1009
1.65k
void YBCInitFlags() {
1010
1.65k
  if (YBCIsInitDbModeEnvVarSet()) {
1011
    // Suppress log spew during initdb.
1012
0
    FLAGS_client_suppress_created_logs = true;
1013
0
  }
1014
1015
1.65k
  SetAtomicFlag(GetAtomicFlag(&FLAGS_pggate_num_connections_to_server),
1016
1.65k
                &FLAGS_num_connections_to_server);
1017
1018
  // TODO(neil) Init a gflag for "YB_PG_TRANSACTIONS_ENABLED" here also.
1019
  // Mikhail agreed that this flag should just be initialized once at the beginning here.
1020
  // Currently, it is initialized for every CREATE statement.
1021
1.65k
}
1022
1023
0
YBCStatus YBCPgIsInitDbDone(bool* initdb_done) {
1024
0
  return ExtractValueFromResult(pgapi->IsInitDbDone(), initdb_done);
1025
0
}
1026
1027
55
bool YBCGetDisableTransparentCacheRefreshRetry() {
1028
55
  return pgapi->GetDisableTransparentCacheRefreshRetry();
1029
55
}
1030
1031
158k
YBCStatus YBCGetSharedCatalogVersion(uint64_t* catalog_version) {
1032
158k
  return ExtractValueFromResult(pgapi->GetSharedCatalogVersion(), catalog_version);
1033
158k
}
1034
1035
235
YBCStatus YBCGetSharedAuthKey(uint64_t* auth_key) {
1036
235
  return ExtractValueFromResult(pgapi->GetSharedAuthKey(), auth_key);
1037
235
}
1038
1039
345k
const YBCPgGFlagsAccessor* YBCGetGFlags() {
1040
345k
  static YBCPgGFlagsAccessor accessor = {
1041
345k
      .log_ysql_catalog_versions               = &FLAGS_log_ysql_catalog_versions,
1042
345k
      .ysql_disable_index_backfill             = &FLAGS_ysql_disable_index_backfill,
1043
345k
      .ysql_max_read_restart_attempts          = &FLAGS_ysql_max_read_restart_attempts,
1044
345k
      .ysql_max_write_restart_attempts         = &FLAGS_ysql_max_write_restart_attempts,
1045
345k
      .ysql_output_buffer_size                 = &FLAGS_ysql_output_buffer_size,
1046
345k
      .ysql_sequence_cache_minval              = &FLAGS_ysql_sequence_cache_minval,
1047
345k
      .ysql_session_max_batch_size             = &FLAGS_ysql_session_max_batch_size,
1048
345k
      .ysql_sleep_before_retry_on_txn_conflict = &FLAGS_ysql_sleep_before_retry_on_txn_conflict
1049
345k
  };
1050
345k
  return &accessor;
1051
345k
}
1052
1053
63.7M
bool YBCPgIsYugaByteEnabled() {
1054
63.7M
  return pgapi;
1055
63.7M
}
1056
1057
3.61k
void YBCSetTimeout(int timeout_ms, void* extra) {
1058
3.61k
  if (!pgapi) {
1059
3.61k
    return;
1060
3.61k
  }
1061
1
  const auto default_client_timeout_ms =
1062
1
      (FLAGS_ysql_client_read_write_timeout_ms < 0
1063
1
           ? std::max(FLAGS_client_read_write_timeout_ms, 600000)
1064
0
           : FLAGS_ysql_client_read_write_timeout_ms);
1065
  // We set the rpc timeouts as a min{STATEMENT_TIMEOUT,
1066
  // FLAGS(_ysql)?_client_read_write_timeout_ms}.
1067
1
  if (timeout_ms <= 0) {
1068
    // The timeout is not valid. Use the default GFLAG value.
1069
1
    return;
1070
1
  }
1071
0
  timeout_ms = std::min(timeout_ms, default_client_timeout_ms);
1072
1073
  // The statement timeout is lesser than default_client_timeout, hence the rpcs would
1074
  // need to use a shorter timeout.
1075
0
  pgapi->SetTimeout(timeout_ms);
1076
0
}
1077
1078
2
YBCStatus YBCGetTabletServerHosts(YBCServerDescriptor **servers, size_t *count) {
1079
2
  const auto result = pgapi->ListTabletServers();
1080
2
  if (!result.ok()) {
1081
0
    return ToYBCStatus(result.status());
1082
0
  }
1083
2
  const auto &servers_info = result.get();
1084
2
  *count = servers_info.size();
1085
2
  *servers = NULL;
1086
2
  if (!servers_info.empty()) {
1087
2
    *servers = static_cast<YBCServerDescriptor *>(
1088
2
        YBCPAlloc(sizeof(YBCServerDescriptor) * servers_info.size()));
1089
2
    YBCServerDescriptor *dest = *servers;
1090
6
    for (const auto &info : servers_info) {
1091
6
      new (dest) YBCServerDescriptor {
1092
6
        .host = YBCPAllocStdString(info.server.hostname),
1093
6
        .cloud = YBCPAllocStdString(info.cloud),
1094
6
        .region = YBCPAllocStdString(info.region),
1095
6
        .zone = YBCPAllocStdString(info.zone),
1096
6
        .public_ip = YBCPAllocStdString(info.public_ip),
1097
6
        .is_primary = info.is_primary,
1098
6
        .pg_port = info.pg_port
1099
6
      };
1100
6
      ++dest;
1101
6
    }
1102
2
  }
1103
2
  return YBCStatusOK();
1104
2
}
1105
1106
//------------------------------------------------------------------------------------------------
1107
// Thread-local variables.
1108
//------------------------------------------------------------------------------------------------
1109
1110
21.7M
void* YBCPgGetThreadLocalCurrentMemoryContext() {
1111
21.7M
  return PgGetThreadLocalCurrentMemoryContext();
1112
21.7M
}
1113
1114
83.5M
void* YBCPgSetThreadLocalCurrentMemoryContext(void *memctx) {
1115
83.5M
  return PgSetThreadLocalCurrentMemoryContext(memctx);
1116
83.5M
}
1117
1118
20.5M
void YBCPgResetCurrentMemCtxThreadLocalVars() {
1119
20.5M
  PgResetCurrentMemCtxThreadLocalVars();
1120
20.5M
}
1121
1122
445k
void* YBCPgGetThreadLocalStrTokPtr() {
1123
445k
  return PgGetThreadLocalStrTokPtr();
1124
445k
}
1125
1126
452k
void YBCPgSetThreadLocalStrTokPtr(char *new_pg_strtok_ptr) {
1127
452k
  PgSetThreadLocalStrTokPtr(new_pg_strtok_ptr);
1128
452k
}
1129
1130
105M
void* YBCPgSetThreadLocalJumpBuffer(void* new_buffer) {
1131
105M
  return PgSetThreadLocalJumpBuffer(new_buffer);
1132
105M
}
1133
1134
0
void* YBCPgGetThreadLocalJumpBuffer() {
1135
0
  return PgGetThreadLocalJumpBuffer();
1136
0
}
1137
1138
0
void YBCPgSetThreadLocalErrMsg(const void* new_msg) {
1139
0
  PgSetThreadLocalErrMsg(new_msg);
1140
0
}
1141
1142
0
const void* YBCPgGetThreadLocalErrMsg() {
1143
0
  return PgGetThreadLocalErrMsg();
1144
0
}
1145
1146
} // extern "C"
1147
1148
} // namespace pggate
1149
} // namespace yb