/Users/deen/code/yugabyte-db/src/yb/yql/pggate/pggate.h
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  |  |  | 
14  |  | #ifndef YB_YQL_PGGATE_PGGATE_H_  | 
15  |  | #define YB_YQL_PGGATE_PGGATE_H_  | 
16  |  |  | 
17  |  | #include <algorithm>  | 
18  |  | #include <functional>  | 
19  |  | #include <thread>  | 
20  |  | #include <unordered_map>  | 
21  |  |  | 
22  |  | #include <boost/preprocessor/cat.hpp>  | 
23  |  | #include <boost/preprocessor/seq/for_each.hpp>  | 
24  |  |  | 
25  |  | #include "yb/util/metrics.h"  | 
26  |  | #include "yb/util/mem_tracker.h"  | 
27  |  | #include "yb/common/ybc_util.h"  | 
28  |  |  | 
29  |  | #include "yb/client/callbacks.h"  | 
30  |  | #include "yb/client/async_initializer.h"  | 
31  |  | #include "yb/server/server_base_options.h"  | 
32  |  |  | 
33  |  | #include "yb/rpc/rpc_fwd.h"  | 
34  |  |  | 
35  |  | #include "yb/yql/pggate/pg_client.h"  | 
36  |  | #include "yb/yql/pggate/pg_env.h"  | 
37  |  | #include "yb/yql/pggate/pg_session.h"  | 
38  |  | #include "yb/yql/pggate/pg_statement.h"  | 
39  |  | #include "yb/yql/pggate/type_mapping.h"  | 
40  |  |  | 
41  |  | #include "yb/server/hybrid_clock.h"  | 
42  |  | #include "yb/yql/pggate/ybc_pggate.h"  | 
43  |  |  | 
44  |  | namespace yb { | 
45  |  | namespace pggate { | 
46  |  |  | 
47  |  | //--------------------------------------------------------------------------------------------------  | 
48  |  |  | 
49  |  | class PggateOptions : public yb::server::ServerBaseOptions { | 
50  |  |  public:  | 
51  |  |   static const uint16_t kDefaultPort = 5432;  | 
52  |  |   static const uint16_t kDefaultWebPort = 13000;  | 
53  |  |  | 
54  |  |   PggateOptions();  | 
55  | 1.65k  |   virtual ~PggateOptions() {} | 
56  |  | };  | 
57  |  |  | 
58  |  | struct PgApiContext { | 
59  |  |   struct MessengerHolder { | 
60  |  |     std::unique_ptr<rpc::SecureContext> security_context;  | 
61  |  |     std::unique_ptr<rpc::Messenger> messenger;  | 
62  |  |  | 
63  |  |     MessengerHolder(  | 
64  |  |         std::unique_ptr<rpc::SecureContext> security_context,  | 
65  |  |         std::unique_ptr<rpc::Messenger> messenger);  | 
66  |  |     MessengerHolder(MessengerHolder&&);  | 
67  |  |  | 
68  |  |     ~MessengerHolder();  | 
69  |  |   };  | 
70  |  |  | 
71  |  |   std::unique_ptr<MetricRegistry> metric_registry;  | 
72  |  |   scoped_refptr<MetricEntity> metric_entity;  | 
73  |  |   std::shared_ptr<MemTracker> mem_tracker;  | 
74  |  |   MessengerHolder messenger_holder;  | 
75  |  |   std::unique_ptr<rpc::ProxyCache> proxy_cache;  | 
76  |  |  | 
77  |  |   PgApiContext();  | 
78  |  |   PgApiContext(PgApiContext&&);  | 
79  |  |   ~PgApiContext();  | 
80  |  | };  | 
81  |  |  | 
82  |  | //--------------------------------------------------------------------------------------------------  | 
83  |  | // Implements support for CAPI.  | 
84  |  | class PgApiImpl { | 
85  |  |  public:  | 
86  |  |   PgApiImpl(PgApiContext context, const YBCPgTypeEntity *YBCDataTypeTable, int count,  | 
87  |  |             YBCPgCallbacks pg_callbacks);  | 
88  |  |   virtual ~PgApiImpl();  | 
89  |  |  | 
90  | 257  |   const YBCPgCallbacks* pg_callbacks() { | 
91  | 257  |     return &pg_callbacks_;  | 
92  | 257  |   }  | 
93  |  |  | 
94  |  |   //------------------------------------------------------------------------------------------------  | 
95  |  |   // Access function to Pggate attribute.  | 
96  | 1.65k  |   client::YBClient* client() { | 
97  | 1.65k  |     return async_client_init_.client();  | 
98  | 1.65k  |   }  | 
99  |  |  | 
100  |  |   void ResetCatalogReadTime();  | 
101  |  |  | 
102  |  |   // Initialize ENV within which PGSQL calls will be executed.  | 
103  |  |   CHECKED_STATUS CreateEnv(PgEnv **pg_env);  | 
104  |  |   CHECKED_STATUS DestroyEnv(PgEnv *pg_env);  | 
105  |  |  | 
106  |  |   // Initialize a session to process statements that come from the same client connection.  | 
107  |  |   // If database_name is empty, a session is created without connecting to any database.  | 
108  |  |   CHECKED_STATUS InitSession(const PgEnv *pg_env, const string& database_name);  | 
109  |  |  | 
110  |  |   // YB Memctx: Create, Destroy, and Reset must be "static" because a few contexts are created  | 
111  |  |   //            before YugaByte environments including PgGate are created and initialized.  | 
112  |  |   // Create YB Memctx. Each memctx will be associated with a Postgres's MemoryContext.  | 
113  |  |   static PgMemctx *CreateMemctx();  | 
114  |  |   // Destroy YB Memctx.  | 
115  |  |   static CHECKED_STATUS DestroyMemctx(PgMemctx *memctx);  | 
116  |  |   // Reset YB Memctx.  | 
117  |  |   static CHECKED_STATUS ResetMemctx(PgMemctx *memctx);  | 
118  |  |   // Cache statements in YB Memctx. When Memctx is destroyed, the statement is destructed.  | 
119  |  |   CHECKED_STATUS AddToCurrentPgMemctx(std::unique_ptr<PgStatement> stmt,  | 
120  |  |                                       PgStatement **handle);  | 
121  |  |   // Cache table descriptor in YB Memctx. When Memctx is destroyed, the descriptor is destructed.  | 
122  |  |   CHECKED_STATUS AddToCurrentPgMemctx(size_t table_desc_id,  | 
123  |  |                                       const PgTableDescPtr &table_desc);  | 
124  |  |   // Read table descriptor that was cached in YB Memctx.  | 
125  |  |   CHECKED_STATUS GetTabledescFromCurrentPgMemctx(size_t table_desc_id, PgTableDesc **handle);  | 
126  |  |  | 
127  |  |   // Invalidate the sessions table cache.  | 
128  |  |   CHECKED_STATUS InvalidateCache();  | 
129  |  |  | 
130  |  |   // Get the gflag TEST_ysql_disable_transparent_cache_refresh_retry.  | 
131  |  |   bool GetDisableTransparentCacheRefreshRetry();  | 
132  |  |  | 
133  |  |   Result<bool> IsInitDbDone();  | 
134  |  |  | 
135  |  |   Result<uint64_t> GetSharedCatalogVersion();  | 
136  |  |   Result<uint64_t> GetSharedAuthKey();  | 
137  |  |  | 
138  |  |   // Setup the table to store sequences data.  | 
139  |  |   CHECKED_STATUS CreateSequencesDataTable();  | 
140  |  |  | 
141  |  |   CHECKED_STATUS InsertSequenceTuple(int64_t db_oid,  | 
142  |  |                                      int64_t seq_oid,  | 
143  |  |                                      uint64_t ysql_catalog_version,  | 
144  |  |                                      int64_t last_val,  | 
145  |  |                                      bool is_called);  | 
146  |  |  | 
147  |  |   CHECKED_STATUS UpdateSequenceTupleConditionally(int64_t db_oid,  | 
148  |  |                                                   int64_t seq_oid,  | 
149  |  |                                                   uint64_t ysql_catalog_version,  | 
150  |  |                                                   int64_t last_val,  | 
151  |  |                                                   bool is_called,  | 
152  |  |                                                   int64_t expected_last_val,  | 
153  |  |                                                   bool expected_is_called,  | 
154  |  |                                                   bool *skipped);  | 
155  |  |  | 
156  |  |   CHECKED_STATUS UpdateSequenceTuple(int64_t db_oid,  | 
157  |  |                                      int64_t seq_oid,  | 
158  |  |                                      uint64_t ysql_catalog_version,  | 
159  |  |                                      int64_t last_val,  | 
160  |  |                                      bool is_called,  | 
161  |  |                                      bool* skipped);  | 
162  |  |  | 
163  |  |   CHECKED_STATUS ReadSequenceTuple(int64_t db_oid,  | 
164  |  |                                    int64_t seq_oid,  | 
165  |  |                                    uint64_t ysql_catalog_version,  | 
166  |  |                                    int64_t *last_val,  | 
167  |  |                                    bool *is_called);  | 
168  |  |  | 
169  |  |   CHECKED_STATUS DeleteSequenceTuple(int64_t db_oid, int64_t seq_oid);  | 
170  |  |  | 
171  |  |   void DeleteStatement(PgStatement *handle);  | 
172  |  |  | 
173  |  |   // Search for type_entity.  | 
174  |  |   const YBCPgTypeEntity *FindTypeEntity(int type_oid);  | 
175  |  |  | 
176  |  |   //------------------------------------------------------------------------------------------------  | 
177  |  |   // Connect database. Switch the connected database to the given "database_name".  | 
178  |  |   CHECKED_STATUS ConnectDatabase(const char *database_name);  | 
179  |  |  | 
180  |  |   // Determine whether the given database is colocated.  | 
181  |  |   CHECKED_STATUS IsDatabaseColocated(const PgOid database_oid, bool *colocated);  | 
182  |  |  | 
183  |  |   // Create database.  | 
184  |  |   CHECKED_STATUS NewCreateDatabase(const char *database_name,  | 
185  |  |                                    PgOid database_oid,  | 
186  |  |                                    PgOid source_database_oid,  | 
187  |  |                                    PgOid next_oid,  | 
188  |  |                                    const bool colocated,  | 
189  |  |                                    PgStatement **handle);  | 
190  |  |   CHECKED_STATUS ExecCreateDatabase(PgStatement *handle);  | 
191  |  |  | 
192  |  |   // Drop database.  | 
193  |  |   CHECKED_STATUS NewDropDatabase(const char *database_name,  | 
194  |  |                                  PgOid database_oid,  | 
195  |  |                                  PgStatement **handle);  | 
196  |  |   CHECKED_STATUS ExecDropDatabase(PgStatement *handle);  | 
197  |  |  | 
198  |  |   // Alter database.  | 
199  |  |   CHECKED_STATUS NewAlterDatabase(const char *database_name,  | 
200  |  |                                  PgOid database_oid,  | 
201  |  |                                  PgStatement **handle);  | 
202  |  |   CHECKED_STATUS AlterDatabaseRenameDatabase(PgStatement *handle, const char *newname);  | 
203  |  |   CHECKED_STATUS ExecAlterDatabase(PgStatement *handle);  | 
204  |  |  | 
205  |  |   // Reserve oids.  | 
206  |  |   CHECKED_STATUS ReserveOids(PgOid database_oid,  | 
207  |  |                              PgOid next_oid,  | 
208  |  |                              uint32_t count,  | 
209  |  |                              PgOid *begin_oid,  | 
210  |  |                              PgOid *end_oid);  | 
211  |  |  | 
212  |  |   CHECKED_STATUS GetCatalogMasterVersion(uint64_t *version);  | 
213  |  |  | 
214  |  |   // Load table.  | 
215  |  |   Result<PgTableDescPtr> LoadTable(const PgObjectId& table_id);  | 
216  |  |  | 
217  |  |   // Invalidate the cache entry corresponding to table_id from the PgSession table cache.  | 
218  |  |   void InvalidateTableCache(const PgObjectId& table_id);  | 
219  |  |  | 
220  |  |   //------------------------------------------------------------------------------------------------  | 
221  |  |   // Create and drop tablegroup.  | 
222  |  |  | 
223  |  |   CHECKED_STATUS NewCreateTablegroup(const char *database_name,  | 
224  |  |                                      const PgOid database_oid,  | 
225  |  |                                      const PgOid tablegroup_oid,  | 
226  |  |                                      const PgOid tablespace_oid,  | 
227  |  |                                      PgStatement **handle);  | 
228  |  |  | 
229  |  |   CHECKED_STATUS ExecCreateTablegroup(PgStatement *handle);  | 
230  |  |  | 
231  |  |   CHECKED_STATUS NewDropTablegroup(const PgOid database_oid,  | 
232  |  |                                    const PgOid tablegroup_oid,  | 
233  |  |                                    PgStatement **handle);  | 
234  |  |  | 
235  |  |   CHECKED_STATUS ExecDropTablegroup(PgStatement *handle);  | 
236  |  |  | 
237  |  |   //------------------------------------------------------------------------------------------------  | 
238  |  |   // Create, alter and drop table.  | 
239  |  |   CHECKED_STATUS NewCreateTable(const char *database_name,  | 
240  |  |                                 const char *schema_name,  | 
241  |  |                                 const char *table_name,  | 
242  |  |                                 const PgObjectId& table_id,  | 
243  |  |                                 bool is_shared_table,  | 
244  |  |                                 bool if_not_exist,  | 
245  |  |                                 bool add_primary_key,  | 
246  |  |                                 const bool colocated,  | 
247  |  |                                 const PgObjectId& tablegroup_oid,  | 
248  |  |                                 const PgObjectId& tablespace_oid,  | 
249  |  |                                 const PgObjectId& matview_pg_table_oid,  | 
250  |  |                                 PgStatement **handle);  | 
251  |  |  | 
252  |  |   CHECKED_STATUS CreateTableAddColumn(PgStatement *handle, const char *attr_name, int attr_num,  | 
253  |  |                                       const YBCPgTypeEntity *attr_type, bool is_hash,  | 
254  |  |                                       bool is_range, bool is_desc, bool is_nulls_first);  | 
255  |  |  | 
256  |  |   CHECKED_STATUS CreateTableSetNumTablets(PgStatement *handle, int32_t num_tablets);  | 
257  |  |  | 
258  |  |   CHECKED_STATUS AddSplitBoundary(PgStatement *handle, PgExpr **exprs, int expr_count);  | 
259  |  |  | 
260  |  |   CHECKED_STATUS ExecCreateTable(PgStatement *handle);  | 
261  |  |  | 
262  |  |   CHECKED_STATUS NewAlterTable(const PgObjectId& table_id,  | 
263  |  |                                PgStatement **handle);  | 
264  |  |  | 
265  |  |   CHECKED_STATUS AlterTableAddColumn(PgStatement *handle, const char *name,  | 
266  |  |                                      int order, const YBCPgTypeEntity *attr_type);  | 
267  |  |  | 
268  |  |   CHECKED_STATUS AlterTableRenameColumn(PgStatement *handle, const char *oldname,  | 
269  |  |                                         const char *newname);  | 
270  |  |  | 
271  |  |   CHECKED_STATUS AlterTableDropColumn(PgStatement *handle, const char *name);  | 
272  |  |  | 
273  |  |   CHECKED_STATUS AlterTableRenameTable(PgStatement *handle, const char *db_name,  | 
274  |  |                                        const char *newname);  | 
275  |  |  | 
276  |  |   CHECKED_STATUS ExecAlterTable(PgStatement *handle);  | 
277  |  |  | 
278  |  |   CHECKED_STATUS NewDropTable(const PgObjectId& table_id,  | 
279  |  |                               bool if_exist,  | 
280  |  |                               PgStatement **handle);  | 
281  |  |  | 
282  |  |   CHECKED_STATUS NewTruncateTable(const PgObjectId& table_id,  | 
283  |  |                                   PgStatement **handle);  | 
284  |  |  | 
285  |  |   CHECKED_STATUS ExecTruncateTable(PgStatement *handle);  | 
286  |  |  | 
287  |  |   CHECKED_STATUS GetTableDesc(const PgObjectId& table_id,  | 
288  |  |                               PgTableDesc **handle);  | 
289  |  |  | 
290  |  |   Result<YBCPgColumnInfo> GetColumnInfo(YBCPgTableDesc table_desc,  | 
291  |  |                                         int16_t attr_number);  | 
292  |  |  | 
293  |  |   CHECKED_STATUS DmlModifiesRow(PgStatement *handle, bool *modifies_row);  | 
294  |  |  | 
295  |  |   CHECKED_STATUS SetIsSysCatalogVersionChange(PgStatement *handle);  | 
296  |  |  | 
297  |  |   CHECKED_STATUS SetCatalogCacheVersion(PgStatement *handle, uint64_t catalog_cache_version);  | 
298  |  |  | 
299  |  |   //------------------------------------------------------------------------------------------------  | 
300  |  |   // Create and drop index.  | 
301  |  |   CHECKED_STATUS NewCreateIndex(const char *database_name,  | 
302  |  |                                 const char *schema_name,  | 
303  |  |                                 const char *index_name,  | 
304  |  |                                 const PgObjectId& index_id,  | 
305  |  |                                 const PgObjectId& table_id,  | 
306  |  |                                 bool is_shared_index,  | 
307  |  |                                 bool is_unique_index,  | 
308  |  |                                 const bool skip_index_backfill,  | 
309  |  |                                 bool if_not_exist,  | 
310  |  |                                 const PgObjectId& tablegroup_oid,  | 
311  |  |                                 const PgObjectId& tablespace_oid,  | 
312  |  |                                 PgStatement **handle);  | 
313  |  |  | 
314  |  |   CHECKED_STATUS CreateIndexAddColumn(PgStatement *handle, const char *attr_name, int attr_num,  | 
315  |  |                                       const YBCPgTypeEntity *attr_type, bool is_hash,  | 
316  |  |                                       bool is_range, bool is_desc, bool is_nulls_first);  | 
317  |  |  | 
318  |  |   CHECKED_STATUS CreateIndexSetNumTablets(PgStatement *handle, int32_t num_tablets);  | 
319  |  |  | 
320  |  |   CHECKED_STATUS CreateIndexAddSplitRow(PgStatement *handle, int num_cols,  | 
321  |  |                                         YBCPgTypeEntity **types, uint64_t *data);  | 
322  |  |  | 
323  |  |   CHECKED_STATUS ExecCreateIndex(PgStatement *handle);  | 
324  |  |  | 
325  |  |   CHECKED_STATUS NewDropIndex(const PgObjectId& index_id,  | 
326  |  |                               bool if_exist,  | 
327  |  |                               PgStatement **handle);  | 
328  |  |  | 
329  |  |   CHECKED_STATUS ExecPostponedDdlStmt(PgStatement *handle);  | 
330  |  |  | 
331  |  |   CHECKED_STATUS BackfillIndex(const PgObjectId& table_id);  | 
332  |  |  | 
333  |  |   //------------------------------------------------------------------------------------------------  | 
334  |  |   // All DML statements  | 
335  |  |   CHECKED_STATUS DmlAppendTarget(PgStatement *handle, PgExpr *expr);  | 
336  |  |  | 
337  |  |   CHECKED_STATUS DmlAppendQual(PgStatement *handle, PgExpr *expr);  | 
338  |  |  | 
339  |  |   CHECKED_STATUS DmlAppendColumnRef(PgStatement *handle, PgExpr *colref);  | 
340  |  |  | 
341  |  |   // Binding Columns: Bind column with a value (expression) in a statement.  | 
342  |  |   // + This API is used to identify the rows you want to operate on. If binding columns are not  | 
343  |  |   //   there, that means you want to operate on all rows (full scan). You can view this as a  | 
344  |  |   //   a definitions of an initial rowset or an optimization over full-scan.  | 
345  |  |   //  | 
346  |  |   // + There are some restrictions on when BindColumn() can be used.  | 
347  |  |   //   Case 1: INSERT INTO tab(x) VALUES(x_expr)  | 
348  |  |   //   - BindColumn() can be used for BOTH primary-key and regular columns.  | 
349  |  |   //   - This bind-column function is used to bind "x" with "x_expr", and "x_expr" that can contain  | 
350  |  |   //     bind-variables (placeholders) and constants whose values can be updated for each execution  | 
351  |  |   //     of the same allocated statement.  | 
352  |  |   //  | 
353  |  |   //   Case 2: SELECT / UPDATE / DELETE <WHERE key = "key_expr">  | 
354  |  |   //   - BindColumn() can only be used for primary-key columns.  | 
355  |  |   //   - This bind-column function is used to bind the primary column "key" with "key_expr" that can  | 
356  |  |   //     contain bind-variables (placeholders) and constants whose values can be updated for each  | 
357  |  |   //     execution of the same allocated statement.  | 
358  |  |   CHECKED_STATUS DmlBindColumn(YBCPgStatement handle, int attr_num, YBCPgExpr attr_value);  | 
359  |  |   CHECKED_STATUS DmlBindColumnCondBetween(YBCPgStatement handle, int attr_num, YBCPgExpr attr_value,  | 
360  |  |       YBCPgExpr attr_value_end);  | 
361  |  |   CHECKED_STATUS DmlBindColumnCondIn(YBCPgStatement handle, int attr_num, int n_attr_values,  | 
362  |  |       YBCPgExpr *attr_value);  | 
363  |  |  | 
364  |  |   CHECKED_STATUS DmlBindHashCode(PgStatement *handle, bool start_valid,  | 
365  |  |                                 bool start_inclusive, uint64_t start_hash_val,  | 
366  |  |                                 bool end_valid, bool end_inclusive,  | 
367  |  |                                 uint64_t end_hash_val);  | 
368  |  |  | 
369  |  |   // Binding Tables: Bind the whole table in a statement.  Do not use with BindColumn.  | 
370  |  |   CHECKED_STATUS DmlBindTable(YBCPgStatement handle);  | 
371  |  |  | 
372  |  |   // Utility method to get the info for column 'attr_num'.  | 
373  |  |   Result<YBCPgColumnInfo> DmlGetColumnInfo(YBCPgStatement handle, int attr_num);  | 
374  |  |  | 
375  |  |   // API for SET clause.  | 
376  |  |   CHECKED_STATUS DmlAssignColumn(YBCPgStatement handle, int attr_num, YBCPgExpr attr_value);  | 
377  |  |  | 
378  |  |   // This function is to fetch the targets in YBCPgDmlAppendTarget() from the rows that were defined  | 
379  |  |   // by YBCPgDmlBindColumn().  | 
380  |  |   CHECKED_STATUS DmlFetch(PgStatement *handle, int32_t natts, uint64_t *values, bool *isnulls,  | 
381  |  |                           PgSysColumns *syscols, bool *has_data);  | 
382  |  |  | 
383  |  |   // Utility method that checks stmt type and calls exec insert, update, or delete internally.  | 
384  |  |   CHECKED_STATUS DmlExecWriteOp(PgStatement *handle, int32_t *rows_affected_count);  | 
385  |  |  | 
386  |  |   // This function adds a primary column to be used in the construction of the tuple id (ybctid).  | 
387  |  |   CHECKED_STATUS DmlAddYBTupleIdColumn(PgStatement *handle, int attr_num, uint64_t datum,  | 
388  |  |                                        bool is_null, const YBCPgTypeEntity *type_entity);  | 
389  |  |  | 
390  |  |   using YBTupleIdProcessor = std::function<Status(const Slice&)>;  | 
391  |  |   CHECKED_STATUS ProcessYBTupleId(const YBCPgYBTupleIdDescriptor& descr,  | 
392  |  |                                   const YBTupleIdProcessor& processor);  | 
393  |  |  | 
394  |  |   // DB Operations: SET, WHERE, ORDER_BY, GROUP_BY, etc.  | 
395  |  |   // + The following operations are run by DocDB.  | 
396  |  |   //   - API for "set_clause" (not yet implemented).  | 
397  |  |   //  | 
398  |  |   // + The following operations are run by Postgres layer. An API might be added to move these  | 
399  |  |   //   operations to DocDB.  | 
400  |  |   //   - API for "where_expr"  | 
401  |  |   //   - API for "order_by_expr"  | 
402  |  |   //   - API for "group_by_expr"  | 
403  |  |  | 
404  |  |   // Buffer write operations.  | 
405  |  |   CHECKED_STATUS StartOperationsBuffering();  | 
406  |  |   CHECKED_STATUS StopOperationsBuffering();  | 
407  |  |   void ResetOperationsBuffering();  | 
408  |  |   CHECKED_STATUS FlushBufferedOperations();  | 
409  |  |  | 
410  |  |   //------------------------------------------------------------------------------------------------  | 
411  |  |   // Insert.  | 
412  |  |   CHECKED_STATUS NewInsert(const PgObjectId& table_id,  | 
413  |  |                            bool is_single_row_txn,  | 
414  |  |                            PgStatement **handle);  | 
415  |  |  | 
416  |  |   CHECKED_STATUS ExecInsert(PgStatement *handle);  | 
417  |  |  | 
418  |  |   CHECKED_STATUS InsertStmtSetUpsertMode(PgStatement *handle);  | 
419  |  |  | 
420  |  |   CHECKED_STATUS InsertStmtSetWriteTime(PgStatement *handle, const HybridTime write_time);  | 
421  |  |  | 
422  |  |   CHECKED_STATUS InsertStmtSetIsBackfill(PgStatement *handle, const bool is_backfill);  | 
423  |  |  | 
424  |  |   //------------------------------------------------------------------------------------------------  | 
425  |  |   // Update.  | 
426  |  |   CHECKED_STATUS NewUpdate(const PgObjectId& table_id,  | 
427  |  |                            bool is_single_row_txn,  | 
428  |  |                            PgStatement **handle);  | 
429  |  |  | 
430  |  |   CHECKED_STATUS ExecUpdate(PgStatement *handle);  | 
431  |  |  | 
432  |  |   //------------------------------------------------------------------------------------------------  | 
433  |  |   // Delete.  | 
434  |  |   CHECKED_STATUS NewDelete(const PgObjectId& table_id,  | 
435  |  |                            bool is_single_row_txn,  | 
436  |  |                            PgStatement **handle);  | 
437  |  |  | 
438  |  |   CHECKED_STATUS ExecDelete(PgStatement *handle);  | 
439  |  |  | 
440  |  |   CHECKED_STATUS DeleteStmtSetIsPersistNeeded(PgStatement *handle, const bool is_persist_needed);  | 
441  |  |  | 
442  |  |   //------------------------------------------------------------------------------------------------  | 
443  |  |   // Colocated Truncate.  | 
444  |  |   CHECKED_STATUS NewTruncateColocated(const PgObjectId& table_id,  | 
445  |  |                                       bool is_single_row_txn,  | 
446  |  |                                       PgStatement **handle);  | 
447  |  |  | 
448  |  |   CHECKED_STATUS ExecTruncateColocated(PgStatement *handle);  | 
449  |  |  | 
450  |  |   //------------------------------------------------------------------------------------------------  | 
451  |  |   // Select.  | 
452  |  |   CHECKED_STATUS NewSelect(const PgObjectId& table_id,  | 
453  |  |                            const PgObjectId& index_id,  | 
454  |  |                            const PgPrepareParameters *prepare_params,  | 
455  |  |                            PgStatement **handle);  | 
456  |  |  | 
457  |  |   CHECKED_STATUS SetForwardScan(PgStatement *handle, bool is_forward_scan);  | 
458  |  |  | 
459  |  |   CHECKED_STATUS ExecSelect(PgStatement *handle, const PgExecParameters *exec_params);  | 
460  |  |  | 
461  |  |   //------------------------------------------------------------------------------------------------  | 
462  |  |   // Analyze.  | 
463  |  |   CHECKED_STATUS NewSample(const PgObjectId& table_id,  | 
464  |  |                            const int targrows,  | 
465  |  |                            PgStatement **handle);  | 
466  |  |  | 
467  |  |   CHECKED_STATUS InitRandomState(PgStatement *handle, double rstate_w, uint64 rand_state);  | 
468  |  |  | 
469  |  |   CHECKED_STATUS SampleNextBlock(PgStatement *handle, bool *has_more);  | 
470  |  |  | 
471  |  |   CHECKED_STATUS ExecSample(PgStatement *handle);  | 
472  |  |  | 
473  |  |   CHECKED_STATUS GetEstimatedRowCount(PgStatement *handle, double *liverows, double *deadrows);  | 
474  |  |  | 
475  |  |   //------------------------------------------------------------------------------------------------  | 
476  |  |   // Transaction control.  | 
477  | 0  |   PgTxnManager* GetPgTxnManager() { return pg_txn_manager_.get(); } | 
478  |  |  | 
479  |  |   CHECKED_STATUS BeginTransaction();  | 
480  |  |   CHECKED_STATUS RecreateTransaction();  | 
481  |  |   CHECKED_STATUS RestartTransaction();  | 
482  |  |   CHECKED_STATUS ResetTransactionReadPoint();  | 
483  |  |   CHECKED_STATUS RestartReadPoint();  | 
484  |  |   CHECKED_STATUS CommitTransaction();  | 
485  |  |   CHECKED_STATUS AbortTransaction();  | 
486  |  |   CHECKED_STATUS SetTransactionIsolationLevel(int isolation);  | 
487  |  |   CHECKED_STATUS SetTransactionReadOnly(bool read_only);  | 
488  |  |   CHECKED_STATUS SetTransactionDeferrable(bool deferrable);  | 
489  |  |   CHECKED_STATUS EnableFollowerReads(bool enable_follower_reads, int32_t staleness_ms);  | 
490  |  |   CHECKED_STATUS EnterSeparateDdlTxnMode();  | 
491  |  |   CHECKED_STATUS ExitSeparateDdlTxnMode();  | 
492  |  |   void ClearSeparateDdlTxnMode();  | 
493  |  |   CHECKED_STATUS SetActiveSubTransaction(SubTransactionId id);  | 
494  |  |   CHECKED_STATUS RollbackSubTransaction(SubTransactionId id);  | 
495  |  |  | 
496  |  |   //------------------------------------------------------------------------------------------------  | 
497  |  |   // Expressions.  | 
498  |  |   // Column reference.  | 
499  |  |   CHECKED_STATUS NewColumnRef(  | 
500  |  |       PgStatement *handle, int attr_num, const PgTypeEntity *type_entity,  | 
501  |  |       bool collate_is_valid_non_c, const PgTypeAttrs *type_attrs, PgExpr **expr_handle);  | 
502  |  |  | 
503  |  |   // Constant expressions.  | 
504  |  |   CHECKED_STATUS NewConstant(  | 
505  |  |       YBCPgStatement stmt, const YBCPgTypeEntity *type_entity, bool collate_is_valid_non_c,  | 
506  |  |       const char *collation_sortkey, uint64_t datum, bool is_null, YBCPgExpr *expr_handle);  | 
507  |  |   CHECKED_STATUS NewConstantVirtual(  | 
508  |  |       YBCPgStatement stmt, const YBCPgTypeEntity *type_entity,  | 
509  |  |       YBCPgDatumKind datum_kind, YBCPgExpr *expr_handle);  | 
510  |  |   CHECKED_STATUS NewConstantOp(  | 
511  |  |       YBCPgStatement stmt, const YBCPgTypeEntity *type_entity, bool collate_is_valid_non_c,  | 
512  |  |       const char *collation_sortkey, uint64_t datum, bool is_null, YBCPgExpr *expr_handle,  | 
513  |  |       bool is_gt);  | 
514  |  |  | 
515  |  |   // TODO(neil) UpdateConstant should be merged into one.  | 
516  |  |   // Update constant.  | 
517  |  |   template<typename value_type>  | 
518  | 0  |   CHECKED_STATUS UpdateConstant(PgExpr *expr, value_type value, bool is_null) { | 
519  | 0  |     if (expr->opcode() != PgExpr::Opcode::PG_EXPR_CONSTANT) { | 
520  |  |       // Invalid handle.  | 
521  | 0  |       return STATUS(InvalidArgument, "Invalid expression handle for constant");  | 
522  | 0  |     }  | 
523  | 0  |     down_cast<PgConstant*>(expr)->UpdateConstant(value, is_null);  | 
524  | 0  |     return Status::OK();  | 
525  | 0  |   } Unexecuted instantiation: _ZN2yb6pggate9PgApiImpl14UpdateConstantIsEENS_6StatusEPNS0_6PgExprET_b Unexecuted instantiation: _ZN2yb6pggate9PgApiImpl14UpdateConstantIiEENS_6StatusEPNS0_6PgExprET_b Unexecuted instantiation: _ZN2yb6pggate9PgApiImpl14UpdateConstantIxEENS_6StatusEPNS0_6PgExprET_b Unexecuted instantiation: _ZN2yb6pggate9PgApiImpl14UpdateConstantIfEENS_6StatusEPNS0_6PgExprET_b Unexecuted instantiation: _ZN2yb6pggate9PgApiImpl14UpdateConstantIdEENS_6StatusEPNS0_6PgExprET_b  | 
526  |  |   CHECKED_STATUS UpdateConstant(PgExpr *expr, const char *value, bool is_null);  | 
527  |  |   CHECKED_STATUS UpdateConstant(PgExpr *expr, const void *value, int64_t bytes, bool is_null);  | 
528  |  |  | 
529  |  |   // Operators.  | 
530  |  |   CHECKED_STATUS NewOperator(  | 
531  |  |       PgStatement *stmt, const char *opname, const YBCPgTypeEntity *type_entity,  | 
532  |  |       bool collate_is_valid_non_c, PgExpr **op_handle);  | 
533  |  |   CHECKED_STATUS OperatorAppendArg(PgExpr *op_handle, PgExpr *arg);  | 
534  |  |  | 
535  |  |   // Foreign key reference caching.  | 
536  |  |   void DeleteForeignKeyReference(PgOid table_id, const Slice& ybctid);  | 
537  |  |   void AddForeignKeyReference(PgOid table_id, const Slice& ybctid);  | 
538  |  |   Result<bool> ForeignKeyReferenceExists(PgOid table_id, const Slice& ybctid, PgOid database_id);  | 
539  |  |   void AddForeignKeyReferenceIntent(PgOid table_id, const Slice& ybctid);  | 
540  |  |  | 
541  |  |   // Sets the specified timeout in the rpc service.  | 
542  |  |   void SetTimeout(int timeout_ms);  | 
543  |  |  | 
544  |  |   Result<client::TabletServersInfo> ListTabletServers();  | 
545  |  |  | 
546  |  |   //------------------------------------------------------------------------------------------------  | 
547  |  |   // System Validation.  | 
548  |  |   CHECKED_STATUS ValidatePlacement(const char *placement_info);  | 
549  |  |  | 
550  |  |  private:  | 
551  |  |   // Control variables.  | 
552  |  |   PggateOptions pggate_options_;  | 
553  |  |  | 
554  |  |   // Metrics.  | 
555  |  |   std::unique_ptr<MetricRegistry> metric_registry_;  | 
556  |  |   scoped_refptr<MetricEntity> metric_entity_;  | 
557  |  |  | 
558  |  |   // Memory tracker.  | 
559  |  |   std::shared_ptr<MemTracker> mem_tracker_;  | 
560  |  |  | 
561  |  |   PgApiContext::MessengerHolder messenger_holder_;  | 
562  |  |  | 
563  |  |   // YBClient is to communicate with either master or tserver.  | 
564  |  |   yb::client::AsyncClientInitialiser async_client_init_;  | 
565  |  |  | 
566  |  |   std::unique_ptr<rpc::ProxyCache> proxy_cache_;  | 
567  |  |  | 
568  |  |   // TODO Rename to client_ when YBClient is removed.  | 
569  |  |   PgClient pg_client_;  | 
570  |  |  | 
571  |  |   // TODO(neil) Map for environments (we should have just one ENV?). Environments should contain  | 
572  |  |   // all the custom flags the PostgreSQL sets. We ignore them all for now.  | 
573  |  |   PgEnv::SharedPtr pg_env_;  | 
574  |  |  | 
575  |  |   scoped_refptr<server::HybridClock> clock_;  | 
576  |  |  | 
577  |  |   // Local tablet-server shared memory segment handle.  | 
578  |  |   std::unique_ptr<tserver::TServerSharedObject> tserver_shared_object_;  | 
579  |  |  | 
580  |  |   YBCPgCallbacks pg_callbacks_;  | 
581  |  |  | 
582  |  |   scoped_refptr<PgTxnManager> pg_txn_manager_;  | 
583  |  |  | 
584  |  |   // Mapping table of YugaByte and PostgreSQL datatypes.  | 
585  |  |   std::unordered_map<int, const YBCPgTypeEntity *> type_map_;  | 
586  |  |  | 
587  |  |   scoped_refptr<PgSession> pg_session_;  | 
588  |  | };  | 
589  |  |  | 
590  |  | }  // namespace pggate  | 
591  |  | }  // namespace yb  | 
592  |  |  | 
593  |  | #endif // YB_YQL_PGGATE_PGGATE_H_  |