YugabyteDB (2.13.1.0-b60, 21121d69985fbf76aa6958d8f04a9bfa936293b5)

Coverage Report

Created: 2022-03-22 16:43

/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
84.3M
YBCStatus ExtractValueFromResult(const Result<T>& result, T* value) {
71
84.3M
  if (
result.ok()84.3M
) {
72
84.3M
    *value = *result;
73
84.3M
    return YBCStatusOK();
74
84.3M
  }
75
18.4E
  return ToYBCStatus(result.status());
76
84.3M
}
ybc_pggate.cc:YBCStatusStruct* yb::pggate::(anonymous namespace)::ExtractValueFromResult<PgColumnInfo>(yb::Result<PgColumnInfo> const&, PgColumnInfo*)
Line
Count
Source
70
83.9M
YBCStatus ExtractValueFromResult(const Result<T>& result, T* value) {
71
83.9M
  if (
result.ok()83.9M
) {
72
83.9M
    *value = *result;
73
83.9M
    return YBCStatusOK();
74
83.9M
  }
75
18.4E
  return ToYBCStatus(result.status());
76
83.9M
}
ybc_pggate.cc:YBCStatusStruct* yb::pggate::(anonymous namespace)::ExtractValueFromResult<scoped_refptr<yb::pggate::PgTableDesc> >(yb::Result<scoped_refptr<yb::pggate::PgTableDesc> > const&, scoped_refptr<yb::pggate::PgTableDesc>*)
Line
Count
Source
70
5.38k
YBCStatus ExtractValueFromResult(const Result<T>& result, T* value) {
71
5.38k
  if (result.ok()) {
72
5.38k
    *value = *result;
73
5.38k
    return YBCStatusOK();
74
5.38k
  }
75
0
  return ToYBCStatus(result.status());
76
5.38k
}
ybc_pggate.cc:YBCStatusStruct* yb::pggate::(anonymous namespace)::ExtractValueFromResult<bool>(yb::Result<bool> const&, bool*)
Line
Count
Source
70
2
YBCStatus ExtractValueFromResult(const Result<T>& result, T* value) {
71
2
  if (result.ok()) {
72
2
    *value = *result;
73
2
    return YBCStatusOK();
74
2
  }
75
0
  return ToYBCStatus(result.status());
76
2
}
ybc_pggate.cc:YBCStatusStruct* yb::pggate::(anonymous namespace)::ExtractValueFromResult<unsigned long long>(yb::Result<unsigned long long> const&, unsigned long long*)
Line
Count
Source
70
444k
YBCStatus ExtractValueFromResult(const Result<T>& result, T* value) {
71
444k
  if (result.ok()) {
72
444k
    *value = *result;
73
444k
    return YBCStatusOK();
74
444k
  }
75
74
  return ToYBCStatus(result.status());
76
444k
}
77
78
YBCStatus ProcessYbctid(
79
    const YBCPgYBTupleIdDescriptor& source,
80
5.07M
    const std::function<Status(PgOid, const Slice&)>& processor) {
81
5.07M
  return ToYBCStatus(pgapi->ProcessYBTupleId(
82
5.07M
      source,
83
5.07M
      std::bind(processor, source.table_oid, std::placeholders::_1)));
84
5.07M
}
85
86
5.04M
Slice YbctidAsSlice(uint64_t ybctid) {
87
5.04M
  char* value = NULL;
88
5.04M
  int64_t bytes = 0;
89
5.04M
  pgapi->FindTypeEntity(kByteArrayOid)->datum_to_yb(ybctid, &value, &bytes);
90
5.04M
  return Slice(value, bytes);
91
5.04M
}
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
6.09k
                     PgApiContext* context) {
101
6.09k
  InitThreading();
102
103
6.09k
  CHECK
(pgapi == nullptr) << ": " << __PRETTY_FUNCTION__ << " can only be called once"1
;
104
105
6.09k
  YBCInitFlags();
106
107
6.09k
#ifndef NDEBUG
108
6.09k
  HybridTime::TEST_SetPrettyToString(true);
109
6.09k
#endif
110
111
6.09k
  pgapi_shutdown_done.exchange(false);
112
6.09k
  if (context) {
113
0
    pgapi = new pggate::PgApiImpl(std::move(*context), data_type_table, count, pg_callbacks);
114
6.09k
  } else {
115
6.09k
    pgapi = new pggate::PgApiImpl(PgApiContext(), data_type_table, count, pg_callbacks);
116
6.09k
  }
117
18.4E
  VLOG(1) << "PgGate open";
118
6.09k
}
119
120
extern "C" {
121
122
6.09k
void YBCInitPgGate(const YBCPgTypeEntity *data_type_table, int count, PgCallbacks pg_callbacks) {
123
6.09k
  YBCInitPgGateEx(data_type_table, count, pg_callbacks, nullptr);
124
6.09k
}
125
126
6.07k
void YBCDestroyPgGate() {
127
6.07k
  if (pgapi_shutdown_done.exchange(true)) {
128
0
    LOG(DFATAL) << __PRETTY_FUNCTION__ << " should only be called once";
129
6.07k
  } else {
130
6.07k
    pggate::PgApiImpl* local_pgapi = pgapi;
131
6.07k
    pgapi = nullptr; // YBCPgIsYugaByteEnabled() must return false from now on.
132
6.07k
    delete local_pgapi;
133
6.07k
    ClearGlobalPgMemctxMap();
134
6.07k
    VLOG
(1) << __PRETTY_FUNCTION__ << " finished"7
;
135
6.07k
  }
136
6.07k
}
137
138
2.25k
const YBCPgCallbacks *YBCGetPgCallbacks() {
139
2.25k
  return pgapi->pg_callbacks();
140
2.25k
}
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
6.09k
                           const char *database_name) {
152
6.09k
  const string db_name(database_name ? 
database_name6.08k
:
""8
);
153
6.09k
  return ToYBCStatus(pgapi->InitSession(pg_env, db_name));
154
6.09k
}
155
156
678k
YBCPgMemctx YBCPgCreateMemctx() {
157
678k
  return pgapi->CreateMemctx();
158
678k
}
159
160
668k
YBCStatus YBCPgDestroyMemctx(YBCPgMemctx memctx) {
161
668k
  return ToYBCStatus(pgapi->DestroyMemctx(memctx));
162
668k
}
163
164
2.35M
void YBCPgResetCatalogReadTime() {
165
2.35M
  return pgapi->ResetCatalogReadTime();
166
2.35M
}
167
168
3.95M
YBCStatus YBCPgResetMemctx(YBCPgMemctx memctx) {
169
3.95M
  return ToYBCStatus(pgapi->ResetMemctx(memctx));
170
3.95M
}
171
172
7.61M
void YBCPgDeleteStatement(YBCPgStatement handle) {
173
7.61M
  pgapi->DeleteStatement(handle);
174
7.61M
}
175
176
1.23k
YBCStatus YBCPgInvalidateCache() {
177
1.23k
  return ToYBCStatus(pgapi->InvalidateCache());
178
1.23k
}
179
180
63.5M
const YBCPgTypeEntity *YBCPgFindTypeEntity(int type_oid) {
181
63.5M
  return pgapi->FindTypeEntity(type_oid);
182
63.5M
}
183
184
63.5M
YBCPgDataType YBCPgGetType(const YBCPgTypeEntity *type_entity) {
185
63.5M
  if (type_entity) {
186
63.4M
    return type_entity->yb_type;
187
63.4M
  }
188
132k
  return YB_YQL_DATA_TYPE_UNKNOWN_DATA;
189
63.5M
}
190
191
9.60k
bool YBCPgAllowForPrimaryKey(const YBCPgTypeEntity *type_entity) {
192
9.60k
  if (type_entity) {
193
9.60k
    return type_entity->allow_for_primary_key;
194
9.60k
  }
195
0
  return false;
196
9.60k
}
197
198
//--------------------------------------------------------------------------------------------------
199
// DDL Statements.
200
//--------------------------------------------------------------------------------------------------
201
// Database Operations -----------------------------------------------------------------------------
202
203
8
YBCStatus YBCPgConnectDatabase(const char *database_name) {
204
8
  return ToYBCStatus(pgapi->ConnectDatabase(database_name));
205
8
}
206
207
5.72k
YBCStatus YBCPgIsDatabaseColocated(const YBCPgOid database_oid, bool *colocated) {
208
5.72k
  return ToYBCStatus(pgapi->IsDatabaseColocated(database_oid, colocated));
209
5.72k
}
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
134
                                 YBCPgStatement *handle) {
217
134
  return ToYBCStatus(pgapi->NewCreateDatabase(
218
134
      database_name, database_oid, source_database_oid, next_oid, colocated, handle));
219
134
}
220
221
134
YBCStatus YBCPgExecCreateDatabase(YBCPgStatement handle) {
222
134
  return ToYBCStatus(pgapi->ExecCreateDatabase(handle));
223
134
}
224
225
YBCStatus YBCPgNewDropDatabase(const char *database_name,
226
                               const YBCPgOid database_oid,
227
72
                               YBCPgStatement *handle) {
228
72
  return ToYBCStatus(pgapi->NewDropDatabase(database_name, database_oid, handle));
229
72
}
230
231
72
YBCStatus YBCPgExecDropDatabase(YBCPgStatement handle) {
232
72
  return ToYBCStatus(pgapi->ExecDropDatabase(handle));
233
72
}
234
235
YBCStatus YBCPgNewAlterDatabase(const char *database_name,
236
                               const YBCPgOid database_oid,
237
3
                               YBCPgStatement *handle) {
238
3
  return ToYBCStatus(pgapi->NewAlterDatabase(database_name, database_oid, handle));
239
3
}
240
241
3
YBCStatus YBCPgAlterDatabaseRenameDatabase(YBCPgStatement handle, const char *newname) {
242
3
  return ToYBCStatus(pgapi->AlterDatabaseRenameDatabase(handle, newname));
243
3
}
244
245
3
YBCStatus YBCPgExecAlterDatabase(YBCPgStatement handle) {
246
3
  return ToYBCStatus(pgapi->ExecAlterDatabase(handle));
247
3
}
248
249
YBCStatus YBCPgReserveOids(const YBCPgOid database_oid,
250
                           const YBCPgOid next_oid,
251
                           const uint32_t count,
252
                           YBCPgOid *begin_oid,
253
805
                           YBCPgOid *end_oid) {
254
805
  return ToYBCStatus(pgapi->ReserveOids(database_oid,
255
805
                                        next_oid + static_cast<YBCPgOid>(
256
805
                                          FLAGS_TEST_ysql_oid_prefetch_adjustment),
257
805
                                        count, begin_oid, end_oid));
258
805
}
259
260
22
YBCStatus YBCPgGetCatalogMasterVersion(uint64_t *version) {
261
22
  return ToYBCStatus(pgapi->GetCatalogMasterVersion(version));
262
22
}
263
264
9
YBCStatus YBCPgInvalidateTableCacheByTableId(const char *table_id) {
265
9
  if (table_id == NULL) {
266
0
    return ToYBCStatus(STATUS(InvalidArgument, "table_id is null"));
267
0
  }
268
9
  std::string table_id_str = table_id;
269
9
  const PgObjectId pg_object_id(table_id_str);
270
9
  pgapi->InvalidateTableCache(pg_object_id);
271
9
  return YBCStatusOK();
272
9
}
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
54
                                   YBCPgStatement *handle) {
281
54
  return ToYBCStatus(pgapi->NewCreateTablegroup(database_name,
282
54
                                                database_oid,
283
54
                                                tablegroup_oid,
284
54
                                                tablespace_oid,
285
54
                                                handle));
286
54
}
287
288
54
YBCStatus YBCPgExecCreateTablegroup(YBCPgStatement handle) {
289
54
  return ToYBCStatus(pgapi->ExecCreateTablegroup(handle));
290
54
}
291
292
YBCStatus YBCPgNewDropTablegroup(YBCPgOid database_oid,
293
                                 YBCPgOid tablegroup_oid,
294
39
                                 YBCPgStatement *handle) {
295
39
  return ToYBCStatus(pgapi->NewDropTablegroup(database_oid,
296
39
                                              tablegroup_oid,
297
39
                                              handle));
298
39
}
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
295
                                 bool is_called) {
310
295
  return ToYBCStatus(pgapi->InsertSequenceTuple(
311
295
      db_oid, seq_oid, ysql_catalog_version, last_val, is_called));
312
295
}
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
2.95k
                                              bool *skipped) {
322
2.95k
  return ToYBCStatus(
323
2.95k
      pgapi->UpdateSequenceTupleConditionally(db_oid, seq_oid, ysql_catalog_version,
324
2.95k
          last_val, is_called, expected_last_val, expected_is_called, skipped));
325
2.95k
}
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
29
                                 bool* skipped) {
333
29
  return ToYBCStatus(pgapi->UpdateSequenceTuple(
334
29
      db_oid, seq_oid, ysql_catalog_version, last_val, is_called, skipped));
335
29
}
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
3.23k
                               bool *is_called) {
342
3.23k
  return ToYBCStatus(pgapi->ReadSequenceTuple(
343
3.23k
      db_oid, seq_oid, ysql_catalog_version, last_val, is_called));
344
3.23k
}
345
346
282
YBCStatus YBCDeleteSequenceTuple(int64_t db_oid, int64_t seq_oid) {
347
282
  return ToYBCStatus(pgapi->DeleteSequenceTuple(db_oid, seq_oid));
348
282
}
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 colocation_id,
363
                              const YBCPgOid tablespace_oid,
364
                              const YBCPgOid matview_pg_table_oid,
365
4.28k
                              YBCPgStatement *handle) {
366
4.28k
  const PgObjectId table_id(database_oid, table_oid);
367
4.28k
  const PgObjectId tablegroup_id(database_oid, tablegroup_oid);
368
4.28k
  const PgObjectId tablespace_id(database_oid, tablespace_oid);
369
4.28k
  const PgObjectId matview_pg_table_id(database_oid, matview_pg_table_oid);
370
4.28k
  return ToYBCStatus(pgapi->NewCreateTable(
371
4.28k
      database_name, schema_name, table_name, table_id, is_shared_table,
372
4.28k
      if_not_exist, add_primary_key, colocated, tablegroup_id, colocation_id, tablespace_id,
373
4.28k
      matview_pg_table_id, handle));
374
4.28k
}
375
376
YBCStatus YBCPgCreateTableAddColumn(YBCPgStatement handle, const char *attr_name, int attr_num,
377
                                    const YBCPgTypeEntity *attr_type, bool is_hash, bool is_range,
378
10.4k
                                    bool is_desc, bool is_nulls_first) {
379
10.4k
  return ToYBCStatus(pgapi->CreateTableAddColumn(handle, attr_name, attr_num, attr_type,
380
10.4k
                                                 is_hash, is_range, is_desc, is_nulls_first));
381
10.4k
}
382
383
340
YBCStatus YBCPgCreateTableSetNumTablets(YBCPgStatement handle, int32_t num_tablets) {
384
340
  return ToYBCStatus(pgapi->CreateTableSetNumTablets(handle, num_tablets));
385
340
}
386
387
181
YBCStatus YBCPgAddSplitBoundary(YBCPgStatement handle, YBCPgExpr *exprs, int expr_count) {
388
181
  return ToYBCStatus(pgapi->AddSplitBoundary(handle, exprs, expr_count));
389
181
}
390
391
4.19k
YBCStatus YBCPgExecCreateTable(YBCPgStatement handle) {
392
4.19k
  return ToYBCStatus(pgapi->ExecCreateTable(handle));
393
4.19k
}
394
395
YBCStatus YBCPgNewAlterTable(const YBCPgOid database_oid,
396
                             const YBCPgOid table_oid,
397
1.71k
                             YBCPgStatement *handle) {
398
1.71k
  const PgObjectId table_id(database_oid, table_oid);
399
1.71k
  return ToYBCStatus(pgapi->NewAlterTable(table_id, handle));
400
1.71k
}
401
402
YBCStatus YBCPgAlterTableAddColumn(YBCPgStatement handle, const char *name, int order,
403
238
                                   const YBCPgTypeEntity *attr_type) {
404
238
  return ToYBCStatus(pgapi->AlterTableAddColumn(handle, name, order, attr_type));
405
238
}
406
407
YBCStatus YBCPgAlterTableRenameColumn(YBCPgStatement handle, const char *oldname,
408
17
                                      const char *newname) {
409
17
  return ToYBCStatus(pgapi->AlterTableRenameColumn(handle, oldname, newname));
410
17
}
411
412
408
YBCStatus YBCPgAlterTableDropColumn(YBCPgStatement handle, const char *name) {
413
408
  return ToYBCStatus(pgapi->AlterTableDropColumn(handle, name));
414
408
}
415
416
YBCStatus YBCPgAlterTableRenameTable(YBCPgStatement handle, const char *db_name,
417
115
                                     const char *newname) {
418
115
  return ToYBCStatus(pgapi->AlterTableRenameTable(handle, db_name, newname));
419
115
}
420
421
522
YBCStatus YBCPgExecAlterTable(YBCPgStatement handle) {
422
522
  return ToYBCStatus(pgapi->ExecAlterTable(handle));
423
522
}
424
425
YBCStatus YBCPgNewDropTable(const YBCPgOid database_oid,
426
                            const YBCPgOid table_oid,
427
                            bool if_exist,
428
3.49k
                            YBCPgStatement *handle) {
429
3.49k
  const PgObjectId table_id(database_oid, table_oid);
430
3.49k
  return ToYBCStatus(pgapi->NewDropTable(table_id, if_exist, handle));
431
3.49k
}
432
433
YBCStatus YBCPgGetTableDesc(const YBCPgOid database_oid,
434
                            const YBCPgOid table_oid,
435
16.4M
                            YBCPgTableDesc *handle) {
436
16.4M
  const PgObjectId table_id(database_oid, table_oid);
437
16.4M
  return ToYBCStatus(pgapi->GetTableDesc(table_id, handle));
438
16.4M
}
439
440
YBCStatus YBCPgGetColumnInfo(YBCPgTableDesc table_desc,
441
                             int16_t attr_number,
442
77.7M
                             YBCPgColumnInfo *column_info) {
443
77.7M
  return ExtractValueFromResult(pgapi->GetColumnInfo(table_desc, attr_number), column_info);
444
77.7M
}
445
446
YBCStatus YBCPgSetCatalogCacheVersion(YBCPgStatement handle,
447
7.56M
                                      uint64_t catalog_cache_version) {
448
7.56M
  return ToYBCStatus(pgapi->SetCatalogCacheVersion(handle, catalog_cache_version));
449
7.56M
}
450
451
33.8k
YBCStatus YBCPgDmlModifiesRow(YBCPgStatement handle, bool *modifies_row) {
452
33.8k
  return ToYBCStatus(pgapi->DmlModifiesRow(handle, modifies_row));
453
33.8k
}
454
455
14
YBCStatus YBCPgSetIsSysCatalogVersionChange(YBCPgStatement handle) {
456
14
  return ToYBCStatus(pgapi->SetIsSysCatalogVersionChange(handle));
457
14
}
458
459
YBCStatus YBCPgNewTruncateTable(const YBCPgOid database_oid,
460
                                const YBCPgOid table_oid,
461
624
                                YBCPgStatement *handle) {
462
624
  const PgObjectId table_id(database_oid, table_oid);
463
624
  return ToYBCStatus(pgapi->NewTruncateTable(table_id, handle));
464
624
}
465
466
624
YBCStatus YBCPgExecTruncateTable(YBCPgStatement handle) {
467
624
  return ToYBCStatus(pgapi->ExecTruncateTable(handle));
468
624
}
469
470
YBCStatus YBCPgIsTableColocated(const YBCPgOid database_oid,
471
                                const YBCPgOid table_oid,
472
5.38k
                                bool *colocated) {
473
5.38k
  const PgObjectId table_id(database_oid, table_oid);
474
5.38k
  PgTableDescPtr table_desc;
475
5.38k
  YBCStatus status = ExtractValueFromResult(pgapi->LoadTable(table_id), &table_desc);
476
5.38k
  if (status) {
477
0
    return status;
478
5.38k
  } else {
479
5.38k
    *colocated = table_desc->IsColocated();
480
5.38k
    return YBCStatusOK();
481
5.38k
  }
482
5.38k
}
483
484
YBCStatus YBCPgGetSomeTableProperties(YBCPgTableDesc table_desc,
485
249
                                      YBCPgTableProperties *properties) {
486
249
  CHECK_NOTNULL(properties)->num_tablets = table_desc->GetPartitionCount();
487
249
  properties->num_hash_key_columns = table_desc->num_hash_key_columns();
488
249
  properties->is_colocated = table_desc->IsColocated();
489
249
  properties->tablegroup_oid = 0; /* Isn't set here. */
490
249
  properties->colocation_id = table_desc->GetColocationId();
491
249
  return YBCStatusOK();
492
249
}
493
494
YBCStatus YBCPgTableExists(const YBCPgOid database_oid,
495
                           const YBCPgOid table_oid,
496
12.0k
                           bool *exists) {
497
12.0k
  const PgObjectId table_id(database_oid, table_oid);
498
12.0k
  const auto result = pgapi->LoadTable(table_id);
499
500
12.0k
  if (result.ok()) {
501
11.9k
    *exists = true;
502
11.9k
    return YBCStatusOK();
503
11.9k
  } else 
if (87
result.status().IsNotFound()87
) {
504
64
    *exists = false;
505
64
    return YBCStatusOK();
506
64
  } else {
507
23
    return ToYBCStatus(result.status());
508
23
  }
509
12.0k
}
510
511
// Index Operations -------------------------------------------------------------------------------
512
513
YBCStatus YBCPgNewCreateIndex(const char *database_name,
514
                              const char *schema_name,
515
                              const char *index_name,
516
                              const YBCPgOid database_oid,
517
                              const YBCPgOid index_oid,
518
                              const YBCPgOid table_oid,
519
                              bool is_shared_index,
520
                              bool is_unique_index,
521
                              const bool skip_index_backfill,
522
                              bool if_not_exist,
523
                              const YBCPgOid tablegroup_oid,
524
                              const YBCPgOid colocation_id,
525
                              const YBCPgOid tablespace_oid,
526
868
                              YBCPgStatement *handle) {
527
868
  const PgObjectId index_id(database_oid, index_oid);
528
868
  const PgObjectId table_id(database_oid, table_oid);
529
868
  const PgObjectId tablegroup_id(database_oid, tablegroup_oid);
530
868
  const PgObjectId tablespace_id(database_oid, tablespace_oid);
531
868
  return ToYBCStatus(pgapi->NewCreateIndex(database_name, schema_name, index_name, index_id,
532
868
                                           table_id, is_shared_index, is_unique_index,
533
868
                                           skip_index_backfill, if_not_exist, tablegroup_id,
534
868
                                           colocation_id, tablespace_id, handle));
535
868
}
536
537
YBCStatus YBCPgCreateIndexAddColumn(YBCPgStatement handle, const char *attr_name, int attr_num,
538
                                    const YBCPgTypeEntity *attr_type, bool is_hash, bool is_range,
539
1.24k
                                    bool is_desc, bool is_nulls_first) {
540
1.24k
  return ToYBCStatus(pgapi->CreateIndexAddColumn(handle, attr_name, attr_num, attr_type,
541
1.24k
                                                 is_hash, is_range, is_desc, is_nulls_first));
542
1.24k
}
543
544
13
YBCStatus YBCPgCreateIndexSetNumTablets(YBCPgStatement handle, int32_t num_tablets) {
545
13
  return ToYBCStatus(pgapi->CreateIndexSetNumTablets(handle, num_tablets));
546
13
}
547
548
862
YBCStatus YBCPgExecCreateIndex(YBCPgStatement handle) {
549
862
  return ToYBCStatus(pgapi->ExecCreateIndex(handle));
550
862
}
551
552
YBCStatus YBCPgNewDropIndex(const YBCPgOid database_oid,
553
                            const YBCPgOid index_oid,
554
                            bool if_exist,
555
672
                            YBCPgStatement *handle) {
556
672
  const PgObjectId index_id(database_oid, index_oid);
557
672
  return ToYBCStatus(pgapi->NewDropIndex(index_id, if_exist, handle));
558
672
}
559
560
4.18k
YBCStatus YBCPgExecPostponedDdlStmt(YBCPgStatement handle) {
561
4.18k
  return ToYBCStatus(pgapi->ExecPostponedDdlStmt(handle));
562
4.18k
}
563
564
YBCStatus YBCPgBackfillIndex(
565
    const YBCPgOid database_oid,
566
540
    const YBCPgOid index_oid) {
567
540
  const PgObjectId index_id(database_oid, index_oid);
568
540
  return ToYBCStatus(pgapi->BackfillIndex(index_id));
569
540
}
570
571
//--------------------------------------------------------------------------------------------------
572
// DML Statements.
573
//--------------------------------------------------------------------------------------------------
574
575
22.3M
YBCStatus YBCPgDmlAppendTarget(YBCPgStatement handle, YBCPgExpr target) {
576
22.3M
  return ToYBCStatus(pgapi->DmlAppendTarget(handle, target));
577
22.3M
}
578
579
22
YBCStatus YbPgDmlAppendQual(YBCPgStatement handle, YBCPgExpr qual) {
580
22
  return ToYBCStatus(pgapi->DmlAppendQual(handle, qual));
581
22
}
582
583
11.9k
YBCStatus YbPgDmlAppendColumnRef(YBCPgStatement handle, YBCPgExpr colref) {
584
11.9k
  return ToYBCStatus(pgapi->DmlAppendColumnRef(handle, colref));
585
11.9k
}
586
587
31.1M
YBCStatus YBCPgDmlBindColumn(YBCPgStatement handle, int attr_num, YBCPgExpr attr_value) {
588
31.1M
  return ToYBCStatus(pgapi->DmlBindColumn(handle, attr_num, attr_value));
589
31.1M
}
590
591
YBCStatus YBCPgDmlAddRowUpperBound(YBCPgStatement handle,
592
                                    int n_col_values,
593
                                    YBCPgExpr *col_values,
594
90
                                    bool is_inclusive) {
595
90
    return ToYBCStatus(pgapi->DmlAddRowUpperBound(handle,
596
90
                                                    n_col_values,
597
90
                                                    col_values,
598
90
                                                    is_inclusive));
599
90
}
600
601
YBCStatus YBCPgDmlAddRowLowerBound(YBCPgStatement handle,
602
                                    int n_col_values,
603
                                    YBCPgExpr *col_values,
604
88
                                    bool is_inclusive) {
605
88
    return ToYBCStatus(pgapi->DmlAddRowLowerBound(handle,
606
88
                                                    n_col_values,
607
88
                                                    col_values,
608
88
                                                    is_inclusive));
609
88
}
610
611
YBCStatus YBCPgDmlBindColumnCondBetween(YBCPgStatement handle, int attr_num, YBCPgExpr attr_value,
612
306
    YBCPgExpr attr_value_end) {
613
306
  return ToYBCStatus(pgapi->DmlBindColumnCondBetween(handle, attr_num, attr_value, attr_value_end));
614
306
}
615
616
YBCStatus YBCPgDmlBindColumnCondIn(YBCPgStatement handle, int attr_num, int n_attr_values,
617
92.3k
    YBCPgExpr *attr_values) {
618
92.3k
  return ToYBCStatus(pgapi->DmlBindColumnCondIn(handle, attr_num, n_attr_values, attr_values));
619
92.3k
}
620
621
YBCStatus YBCPgDmlBindHashCodes(YBCPgStatement handle, bool start_valid,
622
                                 bool start_inclusive, uint64_t start_hash_val,
623
                                 bool end_valid, bool end_inclusive,
624
79
                                 uint64_t end_hash_val) {
625
79
  return ToYBCStatus(pgapi->DmlBindHashCode(handle, start_valid,
626
79
                      start_inclusive, start_hash_val, end_valid,
627
79
                      end_inclusive, end_hash_val));
628
79
}
629
630
90
YBCStatus YBCPgDmlBindTable(YBCPgStatement handle) {
631
90
  return ToYBCStatus(pgapi->DmlBindTable(handle));
632
90
}
633
634
6.19M
YBCStatus YBCPgDmlGetColumnInfo(YBCPgStatement handle, int attr_num, YBCPgColumnInfo* column_info) {
635
6.19M
  return ExtractValueFromResult(pgapi->DmlGetColumnInfo(handle, attr_num), column_info);
636
6.19M
}
637
638
YBCStatus YBCPgDmlAssignColumn(YBCPgStatement handle,
639
                               int attr_num,
640
983k
                               YBCPgExpr attr_value) {
641
983k
  return ToYBCStatus(pgapi->DmlAssignColumn(handle, attr_num, attr_value));
642
983k
}
643
644
YBCStatus YBCPgDmlFetch(YBCPgStatement handle, int32_t natts, uint64_t *values, bool *isnulls,
645
53.1M
                        YBCPgSysColumns *syscols, bool *has_data) {
646
53.1M
  return ToYBCStatus(pgapi->DmlFetch(handle, natts, values, isnulls, syscols, has_data));
647
53.1M
}
648
649
667k
YBCStatus YBCPgStartOperationsBuffering() {
650
667k
  return ToYBCStatus(pgapi->StartOperationsBuffering());
651
667k
}
652
653
610k
YBCStatus YBCPgStopOperationsBuffering() {
654
610k
  return ToYBCStatus(pgapi->StopOperationsBuffering());
655
610k
}
656
657
114k
void YBCPgResetOperationsBuffering() {
658
114k
  pgapi->ResetOperationsBuffering();
659
114k
}
660
661
595k
YBCStatus YBCPgFlushBufferedOperations() {
662
595k
  return ToYBCStatus(pgapi->FlushBufferedOperations());
663
595k
}
664
665
7.20M
YBCStatus YBCPgDmlExecWriteOp(YBCPgStatement handle, int32_t *rows_affected_count) {
666
7.20M
  return ToYBCStatus(pgapi->DmlExecWriteOp(handle, rows_affected_count));
667
7.20M
}
668
669
4.52M
YBCStatus YBCPgBuildYBTupleId(const YBCPgYBTupleIdDescriptor *source, uint64_t *ybctid) {
670
4.52M
  return ProcessYbctid(*source, [ybctid](const auto&, const auto& yid) {
671
4.52M
    const auto* type_entity = pgapi->FindTypeEntity(kByteArrayOid);
672
4.52M
    *ybctid = type_entity->yb_to_datum(yid.cdata(), yid.size(), nullptr /* type_attrs */);
673
4.52M
    return Status::OK();
674
4.52M
  });
675
4.52M
}
676
677
YBCStatus YBCPgNewSample(const YBCPgOid database_oid,
678
                         const YBCPgOid table_oid,
679
                         const int targrows,
680
178
                         YBCPgStatement *handle) {
681
178
  const PgObjectId table_id(database_oid, table_oid);
682
178
  return ToYBCStatus(pgapi->NewSample(table_id, targrows, handle));
683
178
}
684
685
178
YBCStatus YBCPgInitRandomState(YBCPgStatement handle, double rstate_w, uint64_t rand_state) {
686
178
  return ToYBCStatus(pgapi->InitRandomState(handle, rstate_w, rand_state));
687
178
}
688
689
939
YBCStatus YBCPgSampleNextBlock(YBCPgStatement handle, bool *has_more) {
690
939
  return ToYBCStatus(pgapi->SampleNextBlock(handle, has_more));
691
939
}
692
693
178
YBCStatus YBCPgExecSample(YBCPgStatement handle) {
694
178
  return ToYBCStatus(pgapi->ExecSample(handle));
695
178
}
696
697
178
YBCStatus YBCPgGetEstimatedRowCount(YBCPgStatement handle, double *liverows, double *deadrows) {
698
178
  return ToYBCStatus(pgapi->GetEstimatedRowCount(handle, liverows, deadrows));
699
178
}
700
701
// INSERT Operations -------------------------------------------------------------------------------
702
YBCStatus YBCPgNewInsert(const YBCPgOid database_oid,
703
                         const YBCPgOid table_oid,
704
                         const bool is_single_row_txn,
705
5.59M
                         YBCPgStatement *handle) {
706
5.59M
  const PgObjectId table_id(database_oid, table_oid);
707
5.59M
  return ToYBCStatus(pgapi->NewInsert(table_id, is_single_row_txn, handle));
708
5.59M
}
709
710
1.04k
YBCStatus YBCPgExecInsert(YBCPgStatement handle) {
711
1.04k
  return ToYBCStatus(pgapi->ExecInsert(handle));
712
1.04k
}
713
714
953k
YBCStatus YBCPgInsertStmtSetUpsertMode(YBCPgStatement handle) {
715
953k
  return ToYBCStatus(pgapi->InsertStmtSetUpsertMode(handle));
716
953k
}
717
718
371k
YBCStatus YBCPgInsertStmtSetWriteTime(YBCPgStatement handle, const uint64_t write_time) {
719
371k
  HybridTime write_hybrid_time;
720
371k
  YBCStatus status = ToYBCStatus(write_hybrid_time.FromUint64(write_time));
721
371k
  if (status) {
722
0
    return status;
723
371k
  } else {
724
371k
    return ToYBCStatus(pgapi->InsertStmtSetWriteTime(handle, write_hybrid_time));
725
371k
  }
726
371k
}
727
728
371k
YBCStatus YBCPgInsertStmtSetIsBackfill(YBCPgStatement handle, const bool is_backfill) {
729
371k
  return ToYBCStatus(pgapi->InsertStmtSetIsBackfill(handle, is_backfill));
730
371k
}
731
732
// UPDATE Operations -------------------------------------------------------------------------------
733
YBCStatus YBCPgNewUpdate(const YBCPgOid database_oid,
734
                         const YBCPgOid table_oid,
735
                         bool is_single_row_txn,
736
704k
                         YBCPgStatement *handle) {
737
704k
  const PgObjectId table_id(database_oid, table_oid);
738
704k
  return ToYBCStatus(pgapi->NewUpdate(table_id, is_single_row_txn, handle));
739
704k
}
740
741
8
YBCStatus YBCPgExecUpdate(YBCPgStatement handle) {
742
8
  return ToYBCStatus(pgapi->ExecUpdate(handle));
743
8
}
744
745
// DELETE Operations -------------------------------------------------------------------------------
746
YBCStatus YBCPgNewDelete(const YBCPgOid database_oid,
747
                         const YBCPgOid table_oid,
748
                         bool is_single_row_txn,
749
907k
                         YBCPgStatement *handle) {
750
907k
  const PgObjectId table_id(database_oid, table_oid);
751
907k
  return ToYBCStatus(pgapi->NewDelete(table_id, is_single_row_txn, handle));
752
907k
}
753
754
4
YBCStatus YBCPgExecDelete(YBCPgStatement handle) {
755
4
  return ToYBCStatus(pgapi->ExecDelete(handle));
756
4
}
757
758
5
YBCStatus YBCPgDeleteStmtSetIsPersistNeeded(YBCPgStatement handle, const bool is_persist_needed) {
759
5
  return ToYBCStatus(pgapi->DeleteStmtSetIsPersistNeeded(handle, is_persist_needed));
760
5
}
761
762
// Colocated TRUNCATE Operations -------------------------------------------------------------------
763
YBCStatus YBCPgNewTruncateColocated(const YBCPgOid database_oid,
764
                                    const YBCPgOid table_oid,
765
                                    bool is_single_row_txn,
766
90
                                    YBCPgStatement *handle) {
767
90
  const PgObjectId table_id(database_oid, table_oid);
768
90
  return ToYBCStatus(pgapi->NewTruncateColocated(table_id, is_single_row_txn, handle));
769
90
}
770
771
0
YBCStatus YBCPgExecTruncateColocated(YBCPgStatement handle) {
772
0
  return ToYBCStatus(pgapi->ExecTruncateColocated(handle));
773
0
}
774
775
// SELECT Operations -------------------------------------------------------------------------------
776
YBCStatus YBCPgNewSelect(const YBCPgOid database_oid,
777
                         const YBCPgOid table_oid,
778
                         const YBCPgPrepareParameters *prepare_params,
779
1.58M
                         YBCPgStatement *handle) {
780
1.58M
  const PgObjectId table_id(database_oid, table_oid);
781
1.58M
  const PgObjectId index_id(database_oid,
782
1.58M
                            prepare_params ? 
prepare_params->index_oid1.46M
:
kInvalidOid129k
);
783
1.58M
  return ToYBCStatus(pgapi->NewSelect(table_id, index_id, prepare_params, handle));
784
1.58M
}
785
786
1.46M
YBCStatus YBCPgSetForwardScan(YBCPgStatement handle, bool is_forward_scan) {
787
1.46M
  return ToYBCStatus(pgapi->SetForwardScan(handle, is_forward_scan));
788
1.46M
}
789
790
1.58M
YBCStatus YBCPgExecSelect(YBCPgStatement handle, const YBCPgExecParameters *exec_params) {
791
1.58M
  return ToYBCStatus(pgapi->ExecSelect(handle, exec_params));
792
1.58M
}
793
794
//--------------------------------------------------------------------------------------------------
795
// Expression Operations
796
//--------------------------------------------------------------------------------------------------
797
798
YBCStatus YBCPgNewColumnRef(
799
    YBCPgStatement stmt, int attr_num, const YBCPgTypeEntity *type_entity,
800
22.3M
    bool collate_is_valid_non_c, const YBCPgTypeAttrs *type_attrs, YBCPgExpr *expr_handle) {
801
22.3M
  return ToYBCStatus(pgapi->NewColumnRef(
802
22.3M
      stmt, attr_num, type_entity, collate_is_valid_non_c, type_attrs, expr_handle));
803
22.3M
}
804
805
YBCStatus YBCPgNewConstant(
806
    YBCPgStatement stmt, const YBCPgTypeEntity *type_entity, bool collate_is_valid_non_c,
807
34.8M
    const char *collation_sortkey, uint64_t datum, bool is_null, YBCPgExpr *expr_handle) {
808
34.8M
  return ToYBCStatus(pgapi->NewConstant(
809
34.8M
      stmt, type_entity, collate_is_valid_non_c, collation_sortkey, datum, is_null, expr_handle));
810
34.8M
}
811
812
YBCStatus YBCPgNewConstantVirtual(
813
    YBCPgStatement stmt, const YBCPgTypeEntity *type_entity,
814
50
    YBCPgDatumKind datum_kind, YBCPgExpr *expr_handle) {
815
50
  return ToYBCStatus(pgapi->NewConstantVirtual(stmt, type_entity, datum_kind, expr_handle));
816
50
}
817
818
YBCStatus YBCPgNewConstantOp(
819
    YBCPgStatement stmt, const YBCPgTypeEntity *type_entity, bool collate_is_valid_non_c,
820
    const char *collation_sortkey, uint64_t datum, bool is_null, YBCPgExpr *expr_handle,
821
6
    bool is_gt) {
822
6
  return ToYBCStatus(pgapi->NewConstantOp(
823
6
      stmt, type_entity, collate_is_valid_non_c, collation_sortkey, datum, is_null, expr_handle,
824
6
      is_gt));
825
6
}
826
827
// Overwriting the expression's result with any desired values.
828
43
YBCStatus YBCPgUpdateConstInt2(YBCPgExpr expr, int16_t value, bool is_null) {
829
43
  return ToYBCStatus(pgapi->UpdateConstant(expr, value, is_null));
830
43
}
831
832
111
YBCStatus YBCPgUpdateConstInt4(YBCPgExpr expr, int32_t value, bool is_null) {
833
111
  return ToYBCStatus(pgapi->UpdateConstant(expr, value, is_null));
834
111
}
835
836
1.02k
YBCStatus YBCPgUpdateConstInt8(YBCPgExpr expr, int64_t value, bool is_null) {
837
1.02k
  return ToYBCStatus(pgapi->UpdateConstant(expr, value, is_null));
838
1.02k
}
839
840
43
YBCStatus YBCPgUpdateConstFloat4(YBCPgExpr expr, float value, bool is_null) {
841
43
  return ToYBCStatus(pgapi->UpdateConstant(expr, value, is_null));
842
43
}
843
844
0
YBCStatus YBCPgUpdateConstFloat8(YBCPgExpr expr, double value, bool is_null) {
845
0
  return ToYBCStatus(pgapi->UpdateConstant(expr, value, is_null));
846
0
}
847
848
2.00k
YBCStatus YBCPgUpdateConstText(YBCPgExpr expr, const char *value, bool is_null) {
849
2.00k
  return ToYBCStatus(pgapi->UpdateConstant(expr, value, is_null));
850
2.00k
}
851
852
36
YBCStatus YBCPgUpdateConstChar(YBCPgExpr expr, const char *value,  int64_t bytes, bool is_null) {
853
36
  return ToYBCStatus(pgapi->UpdateConstant(expr, value, bytes, is_null));
854
36
}
855
856
YBCStatus YBCPgNewOperator(
857
    YBCPgStatement stmt, const char *opname, const YBCPgTypeEntity *type_entity,
858
22.8k
    bool collate_is_valid_non_c, YBCPgExpr *op_handle) {
859
22.8k
  return ToYBCStatus(pgapi->NewOperator(
860
22.8k
      stmt, opname, type_entity, collate_is_valid_non_c, op_handle));
861
22.8k
}
862
863
22.8k
YBCStatus YBCPgOperatorAppendArg(YBCPgExpr op_handle, YBCPgExpr arg) {
864
22.8k
  return ToYBCStatus(pgapi->OperatorAppendArg(op_handle, arg));
865
22.8k
}
866
867
YBCStatus YBCGetDocDBKeySize(uint64_t data, const YBCPgTypeEntity *typeentity,
868
431k
                            bool is_null, size_t *type_size) {
869
870
431k
  if (typeentity == nullptr
871
431k
      || typeentity->yb_type == YB_YQL_DATA_TYPE_UNKNOWN_DATA
872
431k
      || !typeentity->allow_for_primary_key) {
873
2
    return YBCStatusNotSupport("");
874
2
  }
875
876
431k
  if (typeentity->datum_fixed_size > 0) {
877
381k
    *type_size = typeentity->datum_fixed_size;
878
381k
    return YBCStatusOK();
879
381k
  }
880
881
50.2k
  QLValue val;
882
50.2k
  Status status = pggate::PgValueToPB(typeentity, data, is_null, &val);
883
50.2k
  if (!status.IsOk()) {
884
0
    return ToYBCStatus(status);
885
0
  }
886
887
50.2k
  string key_buf;
888
50.2k
  AppendToKey(val.value(), &key_buf);
889
50.2k
  *type_size = key_buf.size();
890
50.2k
  return YBCStatusOK();
891
50.2k
}
892
893
894
YBCStatus YBCAppendDatumToKey(uint64_t data, const YBCPgTypeEntity *typeentity,
895
                              bool is_null, char *key_ptr,
896
431k
                              size_t *bytes_written) {
897
431k
  QLValue val;
898
899
431k
  Status status = pggate::PgValueToPB(typeentity, data, is_null, &val);
900
431k
  if (!status.IsOk()) {
901
0
    return ToYBCStatus(status);
902
0
  }
903
904
431k
  string key_buf;
905
431k
  AppendToKey(val.value(), &key_buf);
906
431k
  memcpy(key_ptr, key_buf.c_str(), key_buf.size());
907
431k
  *bytes_written = key_buf.size();
908
431k
  return YBCStatusOK();
909
431k
}
910
911
155k
uint16_t YBCCompoundHash(const char *key, size_t length) {
912
155k
  return YBPartition::HashColumnCompoundValue(string(key, length));
913
155k
}
914
915
//------------------------------------------------------------------------------------------------
916
// Transaction operation.
917
//------------------------------------------------------------------------------------------------
918
919
415k
YBCStatus YBCPgBeginTransaction() {
920
415k
  return ToYBCStatus(pgapi->BeginTransaction());
921
415k
}
922
923
69.1k
YBCStatus YBCPgRecreateTransaction() {
924
69.1k
  return ToYBCStatus(pgapi->RecreateTransaction());
925
69.1k
}
926
927
510
YBCStatus YBCPgRestartTransaction() {
928
510
  return ToYBCStatus(pgapi->RestartTransaction());
929
510
}
930
931
28.6k
YBCStatus YBCPgResetTransactionReadPoint() {
932
28.6k
  return ToYBCStatus(pgapi->ResetTransactionReadPoint());
933
28.6k
}
934
935
163
YBCStatus YBCPgRestartReadPoint() {
936
163
  return ToYBCStatus(pgapi->RestartReadPoint());
937
163
}
938
939
381k
YBCStatus YBCPgCommitTransaction() {
940
381k
  return ToYBCStatus(pgapi->CommitTransaction());
941
381k
}
942
943
50.5k
YBCStatus YBCPgAbortTransaction() {
944
50.5k
  return ToYBCStatus(pgapi->AbortTransaction());
945
50.5k
}
946
947
437k
YBCStatus YBCPgSetTransactionIsolationLevel(int isolation) {
948
437k
  return ToYBCStatus(pgapi->SetTransactionIsolationLevel(isolation));
949
437k
}
950
951
414k
YBCStatus YBCPgSetTransactionReadOnly(bool read_only) {
952
414k
  return ToYBCStatus(pgapi->SetTransactionReadOnly(read_only));
953
414k
}
954
955
414k
YBCStatus YBCPgEnableFollowerReads(bool enable_follower_reads, int32_t staleness_ms) {
956
414k
  return ToYBCStatus(pgapi->EnableFollowerReads(enable_follower_reads, staleness_ms));
957
414k
}
958
959
414k
YBCStatus YBCPgSetTransactionDeferrable(bool deferrable) {
960
414k
  return ToYBCStatus(pgapi->SetTransactionDeferrable(deferrable));
961
414k
}
962
963
20.3k
YBCStatus YBCPgEnterSeparateDdlTxnMode() {
964
20.3k
  return ToYBCStatus(pgapi->EnterSeparateDdlTxnMode());
965
20.3k
}
966
967
18.6k
YBCStatus YBCPgExitSeparateDdlTxnMode() {
968
18.6k
  return ToYBCStatus(pgapi->ExitSeparateDdlTxnMode());
969
18.6k
}
970
971
1.71k
void YBCPgClearSeparateDdlTxnMode() {
972
1.71k
  pgapi->ClearSeparateDdlTxnMode();
973
1.71k
}
974
975
61.7k
YBCStatus YBCPgSetActiveSubTransaction(uint32_t id) {
976
61.7k
  return ToYBCStatus(pgapi->SetActiveSubTransaction(id));
977
61.7k
}
978
979
13.5k
YBCStatus YBCPgRollbackSubTransaction(uint32_t id) {
980
13.5k
  return ToYBCStatus(pgapi->RollbackSubTransaction(id));
981
13.5k
}
982
983
//------------------------------------------------------------------------------------------------
984
// System validation.
985
//------------------------------------------------------------------------------------------------
986
1
YBCStatus YBCPgValidatePlacement(const char *placement_info) {
987
1
  return ToYBCStatus(pgapi->ValidatePlacement(placement_info));
988
1
}
989
990
// Referential Integrity Caching
991
68.2k
YBCStatus YBCPgForeignKeyReferenceCacheDelete(const YBCPgYBTupleIdDescriptor *source) {
992
68.2k
  return ProcessYbctid(*source, [](auto table_id, const auto& ybctid){
993
68.2k
    pgapi->DeleteForeignKeyReference(table_id, ybctid);
994
68.2k
    return Status::OK();
995
68.2k
  });
996
68.2k
}
997
998
656k
void YBCPgDeleteFromForeignKeyReferenceCache(YBCPgOid table_oid, uint64_t ybctid) {
999
656k
  pgapi->DeleteForeignKeyReference(table_oid, YbctidAsSlice(ybctid));
1000
656k
}
1001
1002
4.38M
void YBCPgAddIntoForeignKeyReferenceCache(YBCPgOid table_oid, uint64_t ybctid) {
1003
4.38M
  pgapi->AddForeignKeyReference(table_oid, YbctidAsSlice(ybctid));
1004
4.38M
}
1005
1006
237k
YBCStatus YBCForeignKeyReferenceExists(const YBCPgYBTupleIdDescriptor *source, bool* res) {
1007
237k
  return ProcessYbctid(*source, [res, source](auto table_id, const auto& ybctid) -> Status {
1008
237k
    *res = 
VERIFY_RESULT237k
(237k
pgapi->ForeignKeyReferenceExists(table_id, ybctid, source->database_oid));
1009
0
    return Status::OK();
1010
237k
  });
1011
237k
}
1012
1013
245k
YBCStatus YBCAddForeignKeyReferenceIntent(const YBCPgYBTupleIdDescriptor *source) {
1014
245k
  return ProcessYbctid(*source, [](auto table_id, const auto& ybctid) {
1015
245k
    pgapi->AddForeignKeyReferenceIntent(table_id, ybctid);
1016
245k
    return Status::OK();
1017
245k
  });
1018
245k
}
1019
1020
23.3M
bool YBCIsInitDbModeEnvVarSet() {
1021
23.3M
  static bool cached_value = false;
1022
23.3M
  static bool cached = false;
1023
1024
23.3M
  if (!cached) {
1025
6.08k
    const char* initdb_mode_env_var_value = getenv("YB_PG_INITDB_MODE");
1026
6.08k
    cached_value = initdb_mode_env_var_value && 
strcmp(initdb_mode_env_var_value, "1") == 04
;
1027
6.08k
    cached = true;
1028
6.08k
  }
1029
1030
23.3M
  return cached_value;
1031
23.3M
}
1032
1033
6.09k
void YBCInitFlags() {
1034
6.09k
  if (YBCIsInitDbModeEnvVarSet()) {
1035
    // Suppress log spew during initdb.
1036
4
    FLAGS_client_suppress_created_logs = true;
1037
4
  }
1038
1039
6.09k
  SetAtomicFlag(GetAtomicFlag(&FLAGS_pggate_num_connections_to_server),
1040
6.09k
                &FLAGS_num_connections_to_server);
1041
1042
  // TODO(neil) Init a gflag for "YB_PG_TRANSACTIONS_ENABLED" here also.
1043
  // Mikhail agreed that this flag should just be initialized once at the beginning here.
1044
  // Currently, it is initialized for every CREATE statement.
1045
6.09k
}
1046
1047
2
YBCStatus YBCPgIsInitDbDone(bool* initdb_done) {
1048
2
  return ExtractValueFromResult(pgapi->IsInitDbDone(), initdb_done);
1049
2
}
1050
1051
147
bool YBCGetDisableTransparentCacheRefreshRetry() {
1052
147
  return pgapi->GetDisableTransparentCacheRefreshRetry();
1053
147
}
1054
1055
442k
YBCStatus YBCGetSharedCatalogVersion(uint64_t* catalog_version) {
1056
442k
  return ExtractValueFromResult(pgapi->GetSharedCatalogVersion(), catalog_version);
1057
442k
}
1058
1059
1.95k
YBCStatus YBCGetSharedAuthKey(uint64_t* auth_key) {
1060
1.95k
  return ExtractValueFromResult(pgapi->GetSharedAuthKey(), auth_key);
1061
1.95k
}
1062
1063
1.00M
const YBCPgGFlagsAccessor* YBCGetGFlags() {
1064
1.00M
  static YBCPgGFlagsAccessor accessor = {
1065
1.00M
      .log_ysql_catalog_versions               = &FLAGS_log_ysql_catalog_versions,
1066
1.00M
      .ysql_disable_index_backfill             = &FLAGS_ysql_disable_index_backfill,
1067
1.00M
      .ysql_max_read_restart_attempts          = &FLAGS_ysql_max_read_restart_attempts,
1068
1.00M
      .ysql_max_write_restart_attempts         = &FLAGS_ysql_max_write_restart_attempts,
1069
1.00M
      .ysql_output_buffer_size                 = &FLAGS_ysql_output_buffer_size,
1070
1.00M
      .ysql_sequence_cache_minval              = &FLAGS_ysql_sequence_cache_minval,
1071
1.00M
      .ysql_session_max_batch_size             = &FLAGS_ysql_session_max_batch_size,
1072
1.00M
      .ysql_sleep_before_retry_on_txn_conflict = &FLAGS_ysql_sleep_before_retry_on_txn_conflict
1073
1.00M
  };
1074
1.00M
  return &accessor;
1075
1.00M
}
1076
1077
173M
bool YBCPgIsYugaByteEnabled() {
1078
173M
  return pgapi;
1079
173M
}
1080
1081
8.05k
void YBCSetTimeout(int timeout_ms, void* extra) {
1082
8.05k
  if (!pgapi) {
1083
8.04k
    return;
1084
8.04k
  }
1085
17
  const auto default_client_timeout_ms =
1086
17
      (FLAGS_ysql_client_read_write_timeout_ms < 0
1087
17
           ? std::max(FLAGS_client_read_write_timeout_ms, 600000)
1088
17
           : 
FLAGS_ysql_client_read_write_timeout_ms0
);
1089
  // We set the rpc timeouts as a min{STATEMENT_TIMEOUT,
1090
  // FLAGS(_ysql)?_client_read_write_timeout_ms}.
1091
17
  if (timeout_ms <= 0) {
1092
    // The timeout is not valid. Use the default GFLAG value.
1093
15
    return;
1094
15
  }
1095
2
  timeout_ms = std::min(timeout_ms, default_client_timeout_ms);
1096
1097
  // The statement timeout is lesser than default_client_timeout, hence the rpcs would
1098
  // need to use a shorter timeout.
1099
2
  pgapi->SetTimeout(timeout_ms);
1100
2
}
1101
1102
4
YBCStatus YBCGetTabletServerHosts(YBCServerDescriptor **servers, size_t *count) {
1103
4
  const auto result = pgapi->ListTabletServers();
1104
4
  if (!result.ok()) {
1105
0
    return ToYBCStatus(result.status());
1106
0
  }
1107
4
  const auto &servers_info = result.get();
1108
4
  *count = servers_info.size();
1109
4
  *servers = NULL;
1110
4
  if (!servers_info.empty()) {
1111
4
    *servers = static_cast<YBCServerDescriptor *>(
1112
4
        YBCPAlloc(sizeof(YBCServerDescriptor) * servers_info.size()));
1113
4
    YBCServerDescriptor *dest = *servers;
1114
12
    for (const auto &info : servers_info) {
1115
12
      new (dest) YBCServerDescriptor {
1116
12
        .host = YBCPAllocStdString(info.server.hostname),
1117
12
        .cloud = YBCPAllocStdString(info.cloud),
1118
12
        .region = YBCPAllocStdString(info.region),
1119
12
        .zone = YBCPAllocStdString(info.zone),
1120
12
        .public_ip = YBCPAllocStdString(info.public_ip),
1121
12
        .is_primary = info.is_primary,
1122
12
        .pg_port = info.pg_port
1123
12
      };
1124
12
      ++dest;
1125
12
    }
1126
4
  }
1127
4
  return YBCStatusOK();
1128
4
}
1129
1130
//------------------------------------------------------------------------------------------------
1131
// Thread-local variables.
1132
//------------------------------------------------------------------------------------------------
1133
1134
55.4M
void* YBCPgGetThreadLocalCurrentMemoryContext() {
1135
55.4M
  return PgGetThreadLocalCurrentMemoryContext();
1136
55.4M
}
1137
1138
214M
void* YBCPgSetThreadLocalCurrentMemoryContext(void *memctx) {
1139
214M
  return PgSetThreadLocalCurrentMemoryContext(memctx);
1140
214M
}
1141
1142
52.8M
void YBCPgResetCurrentMemCtxThreadLocalVars() {
1143
52.8M
  PgResetCurrentMemCtxThreadLocalVars();
1144
52.8M
}
1145
1146
1.25M
void* YBCPgGetThreadLocalStrTokPtr() {
1147
1.25M
  return PgGetThreadLocalStrTokPtr();
1148
1.25M
}
1149
1150
1.26M
void YBCPgSetThreadLocalStrTokPtr(char *new_pg_strtok_ptr) {
1151
1.26M
  PgSetThreadLocalStrTokPtr(new_pg_strtok_ptr);
1152
1.26M
}
1153
1154
269M
void* YBCPgSetThreadLocalJumpBuffer(void* new_buffer) {
1155
269M
  return PgSetThreadLocalJumpBuffer(new_buffer);
1156
269M
}
1157
1158
0
void* YBCPgGetThreadLocalJumpBuffer() {
1159
0
  return PgGetThreadLocalJumpBuffer();
1160
0
}
1161
1162
0
void YBCPgSetThreadLocalErrMsg(const void* new_msg) {
1163
0
  PgSetThreadLocalErrMsg(new_msg);
1164
0
}
1165
1166
0
const void* YBCPgGetThreadLocalErrMsg() {
1167
0
  return PgGetThreadLocalErrMsg();
1168
0
}
1169
1170
} // extern "C"
1171
1172
} // namespace pggate
1173
} // namespace yb