/
pg_yb_utils.c
567 lines (507 loc) · 14.1 KB
/
pg_yb_utils.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
/*-------------------------------------------------------------------------
*
* pg_yb_utils.c
* Utilities for YugaByte/PostgreSQL integration that have to be defined on
* the PostgreSQL side.
*
* Copyright (c) YugaByte, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy
* of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
* IDENTIFICATION
* src/backend/utils/misc/pg_yb_utils.c
*
*-------------------------------------------------------------------------
*/
#include <sys/types.h>
#include <unistd.h>
#include "postgres.h"
#include "miscadmin.h"
#include "access/sysattr.h"
#include "utils/lsyscache.h"
#include "utils/rel.h"
#include "catalog/pg_database.h"
#include "utils/builtins.h"
#include "catalog/pg_type.h"
#include "catalog/catalog.h"
#include "commands/dbcommands.h"
#include "pg_yb_utils.h"
#include "catalog/ybctype.h"
#include "yb/yql/pggate/ybc_pggate.h"
#include "common/pg_yb_common.h"
#include "utils/resowner_private.h"
YBCPgSession ybc_pg_session = NULL;
uint64 yb_catalog_cache_version = YB_CATCACHE_VERSION_UNINITIALIZED;
/** These values are lazily initialized based on corresponding environment variables. */
int ybc_pg_double_write = -1;
int ybc_disable_pg_locking = -1;
YBCStatus ybc_commit_status = NULL;
bool
IsYugaByteEnabled()
{
/* We do not support Init/Bootstrap processing modes yet. */
return ybc_pg_session != NULL;
}
void
CheckIsYBSupportedRelation(Relation relation)
{
const char relkind = relation->rd_rel->relkind;
CheckIsYBSupportedRelationByKind(relkind);
}
void
CheckIsYBSupportedRelationByKind(char relkind)
{
if (!(relkind == RELKIND_RELATION || relkind == RELKIND_INDEX ||
relkind == RELKIND_VIEW || relkind == RELKIND_SEQUENCE))
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
errmsg("This feature is not supported in YugaByte.")));
}
bool
IsYBRelation(Relation relation)
{
if (!IsYugaByteEnabled()) return false;
const char relkind = relation->rd_rel->relkind;
CheckIsYBSupportedRelationByKind(relkind);
/* Currently only support regular tables and indexes.
* Temp tables and views are supported, but they are not YB relations. */
return (relkind == RELKIND_RELATION || relkind == RELKIND_INDEX)
&& relation->rd_rel->relpersistence != RELPERSISTENCE_TEMP;
}
bool
IsYBRelationById(Oid relid)
{
Relation relation = RelationIdGetRelation(relid);
bool is_supported = IsYBRelation(relation);
RelationClose(relation);
return is_supported;
}
bool
HasYBSecondaryIndices(Relation relation)
{
if (!relation->rd_rel->relhasindex)
return false;
bool has_indices = false;
List *indexlist = RelationGetIndexList(relation);
ListCell *lc;
foreach(lc, indexlist)
{
if (lfirst_oid(lc) == relation->rd_pkindex)
continue;
has_indices = true;
break;
}
list_free(indexlist);
return has_indices;
}
bool
YBNeedRetryAfterCacheRefresh(ErrorData *edata)
{
// TODO Inspect error code to distinguish retryable errors.
return true;
}
AttrNumber YBGetFirstLowInvalidAttributeNumber(Relation relation)
{
return IsYBRelation(relation)
? YBFirstLowInvalidAttributeNumber
: FirstLowInvalidHeapAttributeNumber;
}
AttrNumber YBGetFirstLowInvalidAttributeNumberFromOid(Oid relid)
{
Relation relation = RelationIdGetRelation(relid);
AttrNumber attr_num = YBGetFirstLowInvalidAttributeNumber(relation);
RelationClose(relation);
return attr_num;
}
bool
YBTransactionsEnabled()
{
static int cached_value = -1;
if (cached_value == -1)
{
cached_value = YBCIsEnvVarTrueWithDefault("YB_PG_TRANSACTIONS_ENABLED", true);
}
return IsYugaByteEnabled() && cached_value;
}
void
YBReportFeatureUnsupported(const char *msg)
{
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
errmsg("%s", msg)));
}
static bool
YBShouldReportErrorStatus()
{
static int cached_value = -1;
if (cached_value == -1)
{
cached_value = YBCIsEnvVarTrue("YB_PG_REPORT_ERROR_STATUS");
}
return cached_value;
}
void
HandleYBStatus(YBCStatus status)
{
if (!status)
return;
if (YBShouldReportErrorStatus()) {
YBC_LOG_ERROR("HandleYBStatus: %s", status->msg);
}
/* Copy the message to the current memory context and free the YBCStatus. */
size_t status_len = strlen(status->msg);
char* msg_buf = palloc(status_len + 1);
strncpy(msg_buf, status->msg, status_len + 1);
YBCFreeStatus(status);
/* TODO: consider creating PostgreSQL error codes for YB statuses. */
ereport(ERROR,
(errcode(ERRCODE_INTERNAL_ERROR),
errmsg("%s", msg_buf)));
}
void
HandleYBStmtStatus(YBCStatus status, YBCPgStatement ybc_stmt)
{
if (!status)
return;
if (ybc_stmt)
{
HandleYBStatus(YBCPgDeleteStatement(ybc_stmt));
}
HandleYBStatus(status);
}
void
HandleYBStmtStatusWithOwner(YBCStatus status,
YBCPgStatement ybc_stmt,
ResourceOwner owner)
{
if (!status)
return;
if (ybc_stmt)
{
HandleYBStatus(YBCPgDeleteStatement(ybc_stmt));
if (owner != NULL)
{
ResourceOwnerForgetYugaByteStmt(owner, ybc_stmt);
}
}
HandleYBStatus(status);
}
void
HandleYBTableDescStatus(YBCStatus status, YBCPgTableDesc table)
{
if (!status)
return;
if (table)
{
HandleYBStatus(YBCPgDeleteTableDesc(table));
}
HandleYBStatus(status);
}
void
YBInitPostgresBackend(
const char *program_name,
const char *db_name,
const char *user_name)
{
HandleYBStatus(YBCInit(program_name, palloc, cstring_to_text_with_len));
/*
* Enable "YB mode" for PostgreSQL so that we will initiate a connection
* to the YugaByte cluster right away from every backend process. We only
* do this if this env variable is set, so we can still run the regular
* PostgreSQL "make check".
*/
if (YBIsEnabledInPostgresEnvVar())
{
const YBCPgTypeEntity *type_table;
int count;
YBCGetTypeTable(&type_table, &count);
YBCInitPgGate(type_table, count);
if (ybc_pg_session != NULL) {
YBC_LOG_FATAL("Double initialization of ybc_pg_session");
}
/*
* For each process, we create one YBC session for PostgreSQL to use
* when accessing YugaByte storage.
*
* TODO: do we really need to DB name / username here?
*/
if (db_name != NULL)
{
HandleYBStatus(YBCPgCreateSession(
/* pg_env */ NULL, db_name, &ybc_pg_session));
}
else if (user_name != NULL)
{
HandleYBStatus(YBCPgCreateSession(
/* pg_env */ NULL, user_name, &ybc_pg_session));
}
}
}
void
YBOnPostgresBackendShutdown()
{
static bool shutdown_done = false;
if (shutdown_done)
{
return;
}
if (ybc_pg_session)
{
YBCPgDestroySession(ybc_pg_session);
ybc_pg_session = NULL;
}
YBCDestroyPgGate();
shutdown_done = true;
}
static void
YBCResetCommitStatus()
{
if (ybc_commit_status)
{
YBCFreeStatus(ybc_commit_status);
ybc_commit_status = NULL;
}
}
bool
YBCCommitTransaction()
{
if (!IsYugaByteEnabled())
return true;
YBCStatus status =
YBCPgTxnManager_CommitTransaction_Status(YBCGetPgTxnManager());
if (status != NULL) {
YBCResetCommitStatus();
ybc_commit_status = status;
return false;
}
return true;
}
void
YBCHandleCommitError()
{
YBCStatus status = ybc_commit_status;
if (status != NULL) {
char* msg = palloc(strlen(status->msg) + 1);
strcpy(msg, status->msg);
YBCResetCommitStatus();
ereport(ERROR,
(errcode(ERRCODE_T_R_SERIALIZATION_FAILURE),
errmsg("Error during commit: %s", msg)));
}
}
bool
YBIsPgLockingEnabled()
{
return !YBTransactionsEnabled();
}
static bool yb_preparing_templates = false;
void
YBSetPreparingTemplates() {
yb_preparing_templates = true;
}
bool
YBIsPreparingTemplates() {
return yb_preparing_templates;
}
const char*
YBPgTypeOidToStr(Oid type_id) {
switch (type_id) {
case BOOLOID: return "BOOL";
case BYTEAOID: return "BYTEA";
case CHAROID: return "CHAR";
case NAMEOID: return "NAME";
case INT8OID: return "INT8";
case INT2OID: return "INT2";
case INT2VECTOROID: return "INT2VECTOR";
case INT4OID: return "INT4";
case REGPROCOID: return "REGPROC";
case TEXTOID: return "TEXT";
case OIDOID: return "OID";
case TIDOID: return "TID";
case XIDOID: return "XID";
case CIDOID: return "CID";
case OIDVECTOROID: return "OIDVECTOR";
case JSONOID: return "JSON";
case XMLOID: return "XML";
case PGNODETREEOID: return "PGNODETREE";
case PGNDISTINCTOID: return "PGNDISTINCT";
case PGDEPENDENCIESOID: return "PGDEPENDENCIES";
case PGDDLCOMMANDOID: return "PGDDLCOMMAND";
case POINTOID: return "POINT";
case LSEGOID: return "LSEG";
case PATHOID: return "PATH";
case BOXOID: return "BOX";
case POLYGONOID: return "POLYGON";
case LINEOID: return "LINE";
case FLOAT4OID: return "FLOAT4";
case FLOAT8OID: return "FLOAT8";
case ABSTIMEOID: return "ABSTIME";
case RELTIMEOID: return "RELTIME";
case TINTERVALOID: return "TINTERVAL";
case UNKNOWNOID: return "UNKNOWN";
case CIRCLEOID: return "CIRCLE";
case CASHOID: return "CASH";
case MACADDROID: return "MACADDR";
case INETOID: return "INET";
case CIDROID: return "CIDR";
case MACADDR8OID: return "MACADDR8";
case INT2ARRAYOID: return "INT2ARRAY";
case INT4ARRAYOID: return "INT4ARRAY";
case TEXTARRAYOID: return "TEXTARRAY";
case OIDARRAYOID: return "OIDARRAY";
case FLOAT4ARRAYOID: return "FLOAT4ARRAY";
case ACLITEMOID: return "ACLITEM";
case CSTRINGARRAYOID: return "CSTRINGARRAY";
case BPCHAROID: return "BPCHAR";
case VARCHAROID: return "VARCHAR";
case DATEOID: return "DATE";
case TIMEOID: return "TIME";
case TIMESTAMPOID: return "TIMESTAMP";
case TIMESTAMPTZOID: return "TIMESTAMPTZ";
case INTERVALOID: return "INTERVAL";
case TIMETZOID: return "TIMETZ";
case BITOID: return "BIT";
case VARBITOID: return "VARBIT";
case NUMERICOID: return "NUMERIC";
case REFCURSOROID: return "REFCURSOR";
case REGPROCEDUREOID: return "REGPROCEDURE";
case REGOPEROID: return "REGOPER";
case REGOPERATOROID: return "REGOPERATOR";
case REGCLASSOID: return "REGCLASS";
case REGTYPEOID: return "REGTYPE";
case REGROLEOID: return "REGROLE";
case REGNAMESPACEOID: return "REGNAMESPACE";
case REGTYPEARRAYOID: return "REGTYPEARRAY";
case UUIDOID: return "UUID";
case LSNOID: return "LSN";
case TSVECTOROID: return "TSVECTOR";
case GTSVECTOROID: return "GTSVECTOR";
case TSQUERYOID: return "TSQUERY";
case REGCONFIGOID: return "REGCONFIG";
case REGDICTIONARYOID: return "REGDICTIONARY";
case JSONBOID: return "JSONB";
case INT4RANGEOID: return "INT4RANGE";
case RECORDOID: return "RECORD";
case RECORDARRAYOID: return "RECORDARRAY";
case CSTRINGOID: return "CSTRING";
case ANYOID: return "ANY";
case ANYARRAYOID: return "ANYARRAY";
case VOIDOID: return "VOID";
case TRIGGEROID: return "TRIGGER";
case EVTTRIGGEROID: return "EVTTRIGGER";
case LANGUAGE_HANDLEROID: return "LANGUAGE_HANDLER";
case INTERNALOID: return "INTERNAL";
case OPAQUEOID: return "OPAQUE";
case ANYELEMENTOID: return "ANYELEMENT";
case ANYNONARRAYOID: return "ANYNONARRAY";
case ANYENUMOID: return "ANYENUM";
case FDW_HANDLEROID: return "FDW_HANDLER";
case INDEX_AM_HANDLEROID: return "INDEX_AM_HANDLER";
case TSM_HANDLEROID: return "TSM_HANDLER";
case ANYRANGEOID: return "ANYRANGE";
default: return "user_defined_type";
}
}
void
YBReportIfYugaByteEnabled()
{
if (YBIsEnabledInPostgresEnvVar()) {
ereport(LOG, (errmsg(
"YugaByte is ENABLED in PostgreSQL. Transactions are %s.",
YBCIsEnvVarTrue("YB_PG_TRANSACTIONS_ENABLED") ?
"enabled" : "disabled")));
} else {
ereport(LOG, (errmsg("YugaByte is NOT ENABLED -- "
"this is a vanilla PostgreSQL server!")));
}
}
bool
YBShouldRestartAllChildrenIfOneCrashes() {
if (!YBIsEnabledInPostgresEnvVar()) {
ereport(LOG, (errmsg("YBShouldRestartAllChildrenIfOneCrashes returning 0, YBIsEnabledInPostgresEnvVar is false")));
return true;
}
const char* flag_file_path =
getenv("YB_PG_NO_RESTART_ALL_CHILDREN_ON_CRASH_FLAG_PATH");
// We will use PostgreSQL's default behavior (restarting all children if one of them crashes)
// if the flag env variable is not specified or the file pointed by it does not exist.
return !flag_file_path || access(flag_file_path, F_OK) == -1;
}
bool
YBShouldLogStackTraceOnError()
{
static int cached_value = -1;
if (cached_value != -1)
{
return cached_value;
}
cached_value = YBCIsEnvVarTrue("YB_PG_STACK_TRACE_ON_ERROR");
return cached_value;
}
const char*
YBPgErrorLevelToString(int elevel) {
switch (elevel)
{
case DEBUG5: return "DEBUG5";
case DEBUG4: return "DEBUG4";
case DEBUG3: return "DEBUG3";
case DEBUG2: return "DEBUG2";
case DEBUG1: return "DEBUG1";
case LOG: return "LOG";
case LOG_SERVER_ONLY: return "LOG_SERVER_ONLY";
case INFO: return "INFO";
case WARNING: return "WARNING";
case ERROR: return "ERROR";
case FATAL: return "FATAL";
case PANIC: return "PANIC";
default: return "UNKNOWN";
}
}
const char*
YBCGetDatabaseName(Oid relid)
{
/*
* Hardcode the names for system db since the cache might not
* be initialized during initdb (bootstrap mode).
* For shared rels (e.g. pg_database) we may not have a database id yet,
* so assuming template1 in that case since that's where shared tables are
* stored in YB.
* TODO Eventually YB should switch to using oid's everywhere so
* that dbname and schemaname should not be needed at all.
*/
if (MyDatabaseId == TemplateDbOid || IsSharedRelation(relid))
return "template1";
else
return get_database_name(MyDatabaseId);
}
const char*
YBCGetSchemaName(Oid schemaoid)
{
/*
* Hardcode the names for system namespaces since the cache might not
* be initialized during initdb (bootstrap mode).
* TODO Eventually YB should switch to using oid's everywhere so
* that dbname and schemaname should not be needed at all.
*/
if (IsSystemNamespace(schemaoid))
return "pg_catalog";
else if (IsToastNamespace(schemaoid))
return "pg_toast";
else
return get_namespace_name(schemaoid);
}
Oid
YBCGetDatabaseOid(Relation rel)
{
return rel->rd_rel->relisshared ? TemplateDbOid : MyDatabaseId;
}