PostgreSQL Source Code  git master
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
tab-complete.in.c
Go to the documentation of this file.
1 /*
2  * psql - the PostgreSQL interactive terminal
3  *
4  * Copyright (c) 2000-2024, PostgreSQL Global Development Group
5  *
6  * src/bin/psql/tab-complete.in.c
7  *
8  * Note: this will compile and work as-is if SWITCH_CONVERSION_APPLIED
9  * is not defined. However, the expected usage is that it's first run
10  * through gen_tabcomplete.pl, which will #define that symbol, fill in the
11  * tcpatterns[] array, and convert the else-if chain in match_previous_words()
12  * into a switch. See comments for match_previous_words() and the header
13  * comment in gen_tabcomplete.pl for more detail.
14  */
15 
16 /*----------------------------------------------------------------------
17  * This file implements a somewhat more sophisticated readline "TAB
18  * completion" in psql. It is not intended to be AI, to replace
19  * learning SQL, or to relieve you from thinking about what you're
20  * doing. Also it does not always give you all the syntactically legal
21  * completions, only those that are the most common or the ones that
22  * the programmer felt most like implementing.
23  *
24  * CAVEAT: Tab completion causes queries to be sent to the backend.
25  * The number of tuples returned gets limited, in most default
26  * installations to 1000, but if you still don't like this prospect,
27  * you can turn off tab completion in your ~/.inputrc (or else
28  * ${INPUTRC}) file so:
29  *
30  * $if psql
31  * set disable-completion on
32  * $endif
33  *
34  * See `man 3 readline' or `info readline' for the full details.
35  *
36  * BUGS:
37  * - Quotes, parentheses, and other funny characters are not handled
38  * all that gracefully.
39  *----------------------------------------------------------------------
40  */
41 
42 #include "postgres_fe.h"
43 
44 #include "input.h"
45 #include "tab-complete.h"
46 
47 /* If we don't have this, we might as well forget about the whole thing: */
48 #ifdef USE_READLINE
49 
50 #include <ctype.h>
51 #include <sys/stat.h>
52 
53 #include "catalog/pg_am_d.h"
54 #include "catalog/pg_class_d.h"
55 #include "common.h"
56 #include "common/keywords.h"
57 #include "libpq-fe.h"
58 #include "mb/pg_wchar.h"
59 #include "pqexpbuffer.h"
60 #include "settings.h"
61 #include "stringutils.h"
62 
63 /*
64  * Ancient versions of libedit provide filename_completion_function()
65  * instead of rl_filename_completion_function(). Likewise for
66  * [rl_]completion_matches().
67  */
68 #ifndef HAVE_RL_FILENAME_COMPLETION_FUNCTION
69 #define rl_filename_completion_function filename_completion_function
70 #endif
71 
72 #ifndef HAVE_RL_COMPLETION_MATCHES
73 #define rl_completion_matches completion_matches
74 #endif
75 
76 /*
77  * Currently we assume that rl_filename_dequoting_function exists if
78  * rl_filename_quoting_function does. If that proves not to be the case,
79  * we'd need to test for the former, or possibly both, in configure.
80  */
81 #ifdef HAVE_RL_FILENAME_QUOTING_FUNCTION
82 #define USE_FILENAME_QUOTING_FUNCTIONS 1
83 #endif
84 
85 /* word break characters */
86 #define WORD_BREAKS "\t\n@><=;|&() "
87 
88 /*
89  * Since readline doesn't let us pass any state through to the tab completion
90  * callback, we have to use this global variable to let get_previous_words()
91  * get at the previous lines of the current command. Ick.
92  */
94 
95 /*
96  * In some situations, the query to find out what names are available to
97  * complete with must vary depending on server version. We handle this by
98  * storing a list of queries, each tagged with the minimum server version
99  * it will work for. Each list must be stored in descending server version
100  * order, so that the first satisfactory query is the one to use.
101  *
102  * When the query string is otherwise constant, an array of VersionedQuery
103  * suffices. Terminate the array with an entry having min_server_version = 0.
104  * That entry's query string can be a query that works in all supported older
105  * server versions, or NULL to give up and do no completion.
106  */
107 typedef struct VersionedQuery
108 {
109  int min_server_version;
110  const char *query;
111 } VersionedQuery;
112 
113 /*
114  * This struct is used to define "schema queries", which are custom-built
115  * to obtain possibly-schema-qualified names of database objects. There is
116  * enough similarity in the structure that we don't want to repeat it each
117  * time. So we put the components of each query into this struct and
118  * assemble them with the common boilerplate in _complete_from_query().
119  *
120  * We also use this struct to define queries that use completion_ref_object,
121  * which is some object related to the one(s) we want to get the names of
122  * (for example, the table we want the indexes of). In that usage the
123  * objects we're completing might not have a schema of their own, but the
124  * reference object almost always does (passed in completion_ref_schema).
125  *
126  * As with VersionedQuery, we can use an array of these if the query details
127  * must vary across versions.
128  */
129 typedef struct SchemaQuery
130 {
131  /*
132  * If not zero, minimum server version this struct applies to. If not
133  * zero, there should be a following struct with a smaller minimum server
134  * version; use catname == NULL in the last entry if we should do nothing.
135  */
136  int min_server_version;
137 
138  /*
139  * Name of catalog or catalogs to be queried, with alias(es), eg.
140  * "pg_catalog.pg_class c". Note that "pg_namespace n" and/or
141  * "pg_namespace nr" will be added automatically when needed.
142  */
143  const char *catname;
144 
145  /*
146  * Selection condition --- only rows meeting this condition are candidates
147  * to display. If catname mentions multiple tables, include the necessary
148  * join condition here. For example, this might look like "c.relkind = "
149  * CppAsString2(RELKIND_RELATION). Write NULL (not an empty string) if
150  * not needed.
151  */
152  const char *selcondition;
153 
154  /*
155  * Visibility condition --- which rows are visible without schema
156  * qualification? For example, "pg_catalog.pg_table_is_visible(c.oid)".
157  * NULL if not needed.
158  */
159  const char *viscondition;
160 
161  /*
162  * Namespace --- name of field to join to pg_namespace.oid when there is
163  * schema qualification. For example, "c.relnamespace". NULL if we don't
164  * want to join to pg_namespace (then any schema part in the input word
165  * will be ignored).
166  */
167  const char *namespace;
168 
169  /*
170  * Result --- the base object name to return. For example, "c.relname".
171  */
172  const char *result;
173 
174  /*
175  * In some cases, it's difficult to keep the query from returning the same
176  * object multiple times. Specify use_distinct to filter out duplicates.
177  */
178  bool use_distinct;
179 
180  /*
181  * Additional literal strings (usually keywords) to be offered along with
182  * the query results. Provide a NULL-terminated array of constant
183  * strings, or NULL if none.
184  */
185  const char *const *keywords;
186 
187  /*
188  * If this query uses completion_ref_object/completion_ref_schema,
189  * populate the remaining fields, else leave them NULL. When using this
190  * capability, catname must include the catalog that defines the
191  * completion_ref_object, and selcondition must include the join condition
192  * that connects it to the result's catalog.
193  *
194  * refname is the field that should be equated to completion_ref_object,
195  * for example "cr.relname".
196  */
197  const char *refname;
198 
199  /*
200  * Visibility condition to use when completion_ref_schema is not set. For
201  * example, "pg_catalog.pg_table_is_visible(cr.oid)". NULL if not needed.
202  */
203  const char *refviscondition;
204 
205  /*
206  * Name of field to join to pg_namespace.oid when completion_ref_schema is
207  * set. For example, "cr.relnamespace". NULL if we don't want to
208  * consider completion_ref_schema.
209  */
210  const char *refnamespace;
211 } SchemaQuery;
212 
213 
214 /* Store maximum number of records we want from database queries
215  * (implemented via SELECT ... LIMIT xx).
216  */
217 static int completion_max_records;
218 
219 /*
220  * Communication variables set by psql_completion (mostly in COMPLETE_WITH_FOO
221  * macros) and then used by the completion callback functions. Ugly but there
222  * is no better way.
223  */
224 static char completion_last_char; /* last char of input word */
225 static const char *completion_charp; /* to pass a string */
226 static const char *const *completion_charpp; /* to pass a list of strings */
227 static const VersionedQuery *completion_vquery; /* to pass a VersionedQuery */
228 static const SchemaQuery *completion_squery; /* to pass a SchemaQuery */
229 static char *completion_ref_object; /* name of reference object */
230 static char *completion_ref_schema; /* schema name of reference object */
231 static bool completion_case_sensitive; /* completion is case sensitive */
232 static bool completion_verbatim; /* completion is verbatim */
233 static bool completion_force_quote; /* true to force-quote filenames */
234 
235 /*
236  * A few macros to ease typing. You can use these to complete the given
237  * string with
238  * 1) The result from a query you pass it. (Perhaps one of those below?)
239  * We support both simple and versioned queries.
240  * 2) The result from a schema query you pass it.
241  * We support both simple and versioned schema queries.
242  * 3) The items from a null-pointer-terminated list (with or without
243  * case-sensitive comparison); if the list is constant you can build it
244  * with COMPLETE_WITH() or COMPLETE_WITH_CS(). The QUERY_LIST and
245  * QUERY_PLUS forms combine such literal lists with a query result.
246  * 4) The list of attributes of the given table (possibly schema-qualified).
247  * 5) The list of arguments to the given function (possibly schema-qualified).
248  *
249  * The query is generally expected to return raw SQL identifiers; matching
250  * to what the user typed is done in a quoting-aware fashion. If what is
251  * returned is not SQL identifiers, use one of the VERBATIM forms, in which
252  * case the query results are matched to the user's text without double-quote
253  * processing (so if quoting is needed, you must provide it in the query
254  * results).
255  */
256 #define COMPLETE_WITH_QUERY(query) \
257  COMPLETE_WITH_QUERY_LIST(query, NULL)
258 
259 #define COMPLETE_WITH_QUERY_LIST(query, list) \
260 do { \
261  completion_charp = query; \
262  completion_charpp = list; \
263  completion_verbatim = false; \
264  matches = rl_completion_matches(text, complete_from_query); \
265 } while (0)
266 
267 #define COMPLETE_WITH_QUERY_PLUS(query, ...) \
268 do { \
269  static const char *const list[] = { __VA_ARGS__, NULL }; \
270  COMPLETE_WITH_QUERY_LIST(query, list); \
271 } while (0)
272 
273 #define COMPLETE_WITH_QUERY_VERBATIM(query) \
274  COMPLETE_WITH_QUERY_VERBATIM_LIST(query, NULL)
275 
276 #define COMPLETE_WITH_QUERY_VERBATIM_LIST(query, list) \
277 do { \
278  completion_charp = query; \
279  completion_charpp = list; \
280  completion_verbatim = true; \
281  matches = rl_completion_matches(text, complete_from_query); \
282 } while (0)
283 
284 #define COMPLETE_WITH_QUERY_VERBATIM_PLUS(query, ...) \
285 do { \
286  static const char *const list[] = { __VA_ARGS__, NULL }; \
287  COMPLETE_WITH_QUERY_VERBATIM_LIST(query, list); \
288 } while (0)
289 
290 #define COMPLETE_WITH_VERSIONED_QUERY(query) \
291  COMPLETE_WITH_VERSIONED_QUERY_LIST(query, NULL)
292 
293 #define COMPLETE_WITH_VERSIONED_QUERY_LIST(query, list) \
294 do { \
295  completion_vquery = query; \
296  completion_charpp = list; \
297  completion_verbatim = false; \
298  matches = rl_completion_matches(text, complete_from_versioned_query); \
299 } while (0)
300 
301 #define COMPLETE_WITH_VERSIONED_QUERY_PLUS(query, ...) \
302 do { \
303  static const char *const list[] = { __VA_ARGS__, NULL }; \
304  COMPLETE_WITH_VERSIONED_QUERY_LIST(query, list); \
305 } while (0)
306 
307 #define COMPLETE_WITH_SCHEMA_QUERY(query) \
308  COMPLETE_WITH_SCHEMA_QUERY_LIST(query, NULL)
309 
310 #define COMPLETE_WITH_SCHEMA_QUERY_LIST(query, list) \
311 do { \
312  completion_squery = &(query); \
313  completion_charpp = list; \
314  completion_verbatim = false; \
315  matches = rl_completion_matches(text, complete_from_schema_query); \
316 } while (0)
317 
318 #define COMPLETE_WITH_SCHEMA_QUERY_PLUS(query, ...) \
319 do { \
320  static const char *const list[] = { __VA_ARGS__, NULL }; \
321  COMPLETE_WITH_SCHEMA_QUERY_LIST(query, list); \
322 } while (0)
323 
324 #define COMPLETE_WITH_SCHEMA_QUERY_VERBATIM(query) \
325 do { \
326  completion_squery = &(query); \
327  completion_charpp = NULL; \
328  completion_verbatim = true; \
329  matches = rl_completion_matches(text, complete_from_schema_query); \
330 } while (0)
331 
332 #define COMPLETE_WITH_VERSIONED_SCHEMA_QUERY(query) \
333  COMPLETE_WITH_VERSIONED_SCHEMA_QUERY_LIST(query, NULL)
334 
335 #define COMPLETE_WITH_VERSIONED_SCHEMA_QUERY_LIST(query, list) \
336 do { \
337  completion_squery = query; \
338  completion_charpp = list; \
339  completion_verbatim = false; \
340  matches = rl_completion_matches(text, complete_from_versioned_schema_query); \
341 } while (0)
342 
343 #define COMPLETE_WITH_VERSIONED_SCHEMA_QUERY_PLUS(query, ...) \
344 do { \
345  static const char *const list[] = { __VA_ARGS__, NULL }; \
346  COMPLETE_WITH_VERSIONED_SCHEMA_QUERY_LIST(query, list); \
347 } while (0)
348 
349 /*
350  * Caution: COMPLETE_WITH_CONST is not for general-purpose use; you probably
351  * want COMPLETE_WITH() with one element, instead.
352  */
353 #define COMPLETE_WITH_CONST(cs, con) \
354 do { \
355  completion_case_sensitive = (cs); \
356  completion_charp = (con); \
357  matches = rl_completion_matches(text, complete_from_const); \
358 } while (0)
359 
360 #define COMPLETE_WITH_LIST_INT(cs, list) \
361 do { \
362  completion_case_sensitive = (cs); \
363  completion_charpp = (list); \
364  matches = rl_completion_matches(text, complete_from_list); \
365 } while (0)
366 
367 #define COMPLETE_WITH_LIST(list) COMPLETE_WITH_LIST_INT(false, list)
368 #define COMPLETE_WITH_LIST_CS(list) COMPLETE_WITH_LIST_INT(true, list)
369 
370 #define COMPLETE_WITH(...) \
371 do { \
372  static const char *const list[] = { __VA_ARGS__, NULL }; \
373  COMPLETE_WITH_LIST(list); \
374 } while (0)
375 
376 #define COMPLETE_WITH_CS(...) \
377 do { \
378  static const char *const list[] = { __VA_ARGS__, NULL }; \
379  COMPLETE_WITH_LIST_CS(list); \
380 } while (0)
381 
382 #define COMPLETE_WITH_ATTR(relation) \
383  COMPLETE_WITH_ATTR_LIST(relation, NULL)
384 
385 #define COMPLETE_WITH_ATTR_LIST(relation, list) \
386 do { \
387  set_completion_reference(relation); \
388  completion_squery = &(Query_for_list_of_attributes); \
389  completion_charpp = list; \
390  completion_verbatim = false; \
391  matches = rl_completion_matches(text, complete_from_schema_query); \
392 } while (0)
393 
394 #define COMPLETE_WITH_ATTR_PLUS(relation, ...) \
395 do { \
396  static const char *const list[] = { __VA_ARGS__, NULL }; \
397  COMPLETE_WITH_ATTR_LIST(relation, list); \
398 } while (0)
399 
400 /*
401  * libedit will typically include the literal's leading single quote in
402  * "text", while readline will not. Adapt our offered strings to fit.
403  * But include a quote if there's not one just before "text", to get the
404  * user off to the right start.
405  */
406 #define COMPLETE_WITH_ENUM_VALUE(type) \
407 do { \
408  set_completion_reference(type); \
409  if (text[0] == '\'' || \
410  start == 0 || rl_line_buffer[start - 1] != '\'') \
411  completion_squery = &(Query_for_list_of_enum_values_quoted); \
412  else \
413  completion_squery = &(Query_for_list_of_enum_values_unquoted); \
414  completion_charpp = NULL; \
415  completion_verbatim = true; \
416  matches = rl_completion_matches(text, complete_from_schema_query); \
417 } while (0)
418 
419 /*
420  * Timezone completion is mostly like enum label completion, but we work
421  * a little harder since this is a more common use-case.
422  */
423 #define COMPLETE_WITH_TIMEZONE_NAME() \
424 do { \
425  static const char *const list[] = { "DEFAULT", NULL }; \
426  if (text[0] == '\'') \
427  completion_charp = Query_for_list_of_timezone_names_quoted_in; \
428  else if (start == 0 || rl_line_buffer[start - 1] != '\'') \
429  completion_charp = Query_for_list_of_timezone_names_quoted_out; \
430  else \
431  completion_charp = Query_for_list_of_timezone_names_unquoted; \
432  completion_charpp = list; \
433  completion_verbatim = true; \
434  matches = rl_completion_matches(text, complete_from_query); \
435 } while (0)
436 
437 #define COMPLETE_WITH_FUNCTION_ARG(function) \
438 do { \
439  set_completion_reference(function); \
440  completion_squery = &(Query_for_list_of_arguments); \
441  completion_charpp = NULL; \
442  completion_verbatim = true; \
443  matches = rl_completion_matches(text, complete_from_schema_query); \
444 } while (0)
445 
446 /*
447  * Assembly instructions for schema queries
448  *
449  * Note that toast tables are not included in those queries to avoid
450  * unnecessary bloat in the completions generated.
451  */
452 
453 static const SchemaQuery Query_for_constraint_of_table = {
454  .catname = "pg_catalog.pg_constraint con, pg_catalog.pg_class c1",
455  .selcondition = "con.conrelid=c1.oid",
456  .result = "con.conname",
457  .refname = "c1.relname",
458  .refviscondition = "pg_catalog.pg_table_is_visible(c1.oid)",
459  .refnamespace = "c1.relnamespace",
460 };
461 
462 static const SchemaQuery Query_for_constraint_of_table_not_validated = {
463  .catname = "pg_catalog.pg_constraint con, pg_catalog.pg_class c1",
464  .selcondition = "con.conrelid=c1.oid and not con.convalidated",
465  .result = "con.conname",
466  .refname = "c1.relname",
467  .refviscondition = "pg_catalog.pg_table_is_visible(c1.oid)",
468  .refnamespace = "c1.relnamespace",
469 };
470 
471 static const SchemaQuery Query_for_constraint_of_type = {
472  .catname = "pg_catalog.pg_constraint con, pg_catalog.pg_type t",
473  .selcondition = "con.contypid=t.oid",
474  .result = "con.conname",
475  .refname = "t.typname",
476  .refviscondition = "pg_catalog.pg_type_is_visible(t.oid)",
477  .refnamespace = "t.typnamespace",
478 };
479 
480 static const SchemaQuery Query_for_index_of_table = {
481  .catname = "pg_catalog.pg_class c1, pg_catalog.pg_class c2, pg_catalog.pg_index i",
482  .selcondition = "c1.oid=i.indrelid and i.indexrelid=c2.oid",
483  .result = "c2.relname",
484  .refname = "c1.relname",
485  .refviscondition = "pg_catalog.pg_table_is_visible(c1.oid)",
486  .refnamespace = "c1.relnamespace",
487 };
488 
489 static const SchemaQuery Query_for_unique_index_of_table = {
490  .catname = "pg_catalog.pg_class c1, pg_catalog.pg_class c2, pg_catalog.pg_index i",
491  .selcondition = "c1.oid=i.indrelid and i.indexrelid=c2.oid and i.indisunique",
492  .result = "c2.relname",
493  .refname = "c1.relname",
494  .refviscondition = "pg_catalog.pg_table_is_visible(c1.oid)",
495  .refnamespace = "c1.relnamespace",
496 };
497 
498 static const SchemaQuery Query_for_list_of_aggregates[] = {
499  {
500  .min_server_version = 110000,
501  .catname = "pg_catalog.pg_proc p",
502  .selcondition = "p.prokind = 'a'",
503  .viscondition = "pg_catalog.pg_function_is_visible(p.oid)",
504  .namespace = "p.pronamespace",
505  .result = "p.proname",
506  },
507  {
508  .catname = "pg_catalog.pg_proc p",
509  .selcondition = "p.proisagg",
510  .viscondition = "pg_catalog.pg_function_is_visible(p.oid)",
511  .namespace = "p.pronamespace",
512  .result = "p.proname",
513  }
514 };
515 
516 static const SchemaQuery Query_for_list_of_arguments = {
517  .catname = "pg_catalog.pg_proc p",
518  .result = "pg_catalog.oidvectortypes(p.proargtypes)||')'",
519  .refname = "p.proname",
520  .refviscondition = "pg_catalog.pg_function_is_visible(p.oid)",
521  .refnamespace = "p.pronamespace",
522 };
523 
524 static const SchemaQuery Query_for_list_of_attributes = {
525  .catname = "pg_catalog.pg_attribute a, pg_catalog.pg_class c",
526  .selcondition = "c.oid = a.attrelid and a.attnum > 0 and not a.attisdropped",
527  .result = "a.attname",
528  .refname = "c.relname",
529  .refviscondition = "pg_catalog.pg_table_is_visible(c.oid)",
530  .refnamespace = "c.relnamespace",
531 };
532 
533 static const SchemaQuery Query_for_list_of_attribute_numbers = {
534  .catname = "pg_catalog.pg_attribute a, pg_catalog.pg_class c",
535  .selcondition = "c.oid = a.attrelid and a.attnum > 0 and not a.attisdropped",
536  .result = "a.attnum::pg_catalog.text",
537  .refname = "c.relname",
538  .refviscondition = "pg_catalog.pg_table_is_visible(c.oid)",
539  .refnamespace = "c.relnamespace",
540 };
541 
542 static const char *const Keywords_for_list_of_datatypes[] = {
543  "bigint",
544  "boolean",
545  "character",
546  "double precision",
547  "integer",
548  "real",
549  "smallint",
550 
551  /*
552  * Note: currently there's no value in offering the following multiword
553  * type names, because tab completion cannot succeed for them: we can't
554  * disambiguate until somewhere in the second word, at which point we
555  * won't have the first word as context. ("double precision" does work,
556  * as long as no other type name begins with "double".) Leave them out to
557  * encourage users to use the PG-specific aliases, which we can complete.
558  */
559 #ifdef NOT_USED
560  "bit varying",
561  "character varying",
562  "time with time zone",
563  "time without time zone",
564  "timestamp with time zone",
565  "timestamp without time zone",
566 #endif
567  NULL
568 };
569 
570 static const SchemaQuery Query_for_list_of_datatypes = {
571  .catname = "pg_catalog.pg_type t",
572  /* selcondition --- ignore table rowtypes and array types */
573  .selcondition = "(t.typrelid = 0 "
574  " OR (SELECT c.relkind = " CppAsString2(RELKIND_COMPOSITE_TYPE)
575  " FROM pg_catalog.pg_class c WHERE c.oid = t.typrelid)) "
576  "AND t.typname !~ '^_'",
577  .viscondition = "pg_catalog.pg_type_is_visible(t.oid)",
578  .namespace = "t.typnamespace",
579  .result = "t.typname",
580  .keywords = Keywords_for_list_of_datatypes,
581 };
582 
583 static const SchemaQuery Query_for_list_of_composite_datatypes = {
584  .catname = "pg_catalog.pg_type t",
585  /* selcondition --- only get composite types */
586  .selcondition = "(SELECT c.relkind = " CppAsString2(RELKIND_COMPOSITE_TYPE)
587  " FROM pg_catalog.pg_class c WHERE c.oid = t.typrelid) "
588  "AND t.typname !~ '^_'",
589  .viscondition = "pg_catalog.pg_type_is_visible(t.oid)",
590  .namespace = "t.typnamespace",
591  .result = "t.typname",
592 };
593 
594 static const SchemaQuery Query_for_list_of_domains = {
595  .catname = "pg_catalog.pg_type t",
596  .selcondition = "t.typtype = 'd'",
597  .viscondition = "pg_catalog.pg_type_is_visible(t.oid)",
598  .namespace = "t.typnamespace",
599  .result = "t.typname",
600 };
601 
602 static const SchemaQuery Query_for_list_of_enum_values_quoted = {
603  .catname = "pg_catalog.pg_enum e, pg_catalog.pg_type t",
604  .selcondition = "t.oid = e.enumtypid",
605  .result = "pg_catalog.quote_literal(enumlabel)",
606  .refname = "t.typname",
607  .refviscondition = "pg_catalog.pg_type_is_visible(t.oid)",
608  .refnamespace = "t.typnamespace",
609 };
610 
611 static const SchemaQuery Query_for_list_of_enum_values_unquoted = {
612  .catname = "pg_catalog.pg_enum e, pg_catalog.pg_type t",
613  .selcondition = "t.oid = e.enumtypid",
614  .result = "e.enumlabel",
615  .refname = "t.typname",
616  .refviscondition = "pg_catalog.pg_type_is_visible(t.oid)",
617  .refnamespace = "t.typnamespace",
618 };
619 
620 /* Note: this intentionally accepts aggregates as well as plain functions */
621 static const SchemaQuery Query_for_list_of_functions[] = {
622  {
623  .min_server_version = 110000,
624  .catname = "pg_catalog.pg_proc p",
625  .selcondition = "p.prokind != 'p'",
626  .viscondition = "pg_catalog.pg_function_is_visible(p.oid)",
627  .namespace = "p.pronamespace",
628  .result = "p.proname",
629  },
630  {
631  .catname = "pg_catalog.pg_proc p",
632  .viscondition = "pg_catalog.pg_function_is_visible(p.oid)",
633  .namespace = "p.pronamespace",
634  .result = "p.proname",
635  }
636 };
637 
638 static const SchemaQuery Query_for_list_of_procedures[] = {
639  {
640  .min_server_version = 110000,
641  .catname = "pg_catalog.pg_proc p",
642  .selcondition = "p.prokind = 'p'",
643  .viscondition = "pg_catalog.pg_function_is_visible(p.oid)",
644  .namespace = "p.pronamespace",
645  .result = "p.proname",
646  },
647  {
648  /* not supported in older versions */
649  .catname = NULL,
650  }
651 };
652 
653 static const SchemaQuery Query_for_list_of_routines = {
654  .catname = "pg_catalog.pg_proc p",
655  .viscondition = "pg_catalog.pg_function_is_visible(p.oid)",
656  .namespace = "p.pronamespace",
657  .result = "p.proname",
658 };
659 
660 static const SchemaQuery Query_for_list_of_sequences = {
661  .catname = "pg_catalog.pg_class c",
662  .selcondition = "c.relkind IN (" CppAsString2(RELKIND_SEQUENCE) ")",
663  .viscondition = "pg_catalog.pg_table_is_visible(c.oid)",
664  .namespace = "c.relnamespace",
665  .result = "c.relname",
666 };
667 
668 static const SchemaQuery Query_for_list_of_foreign_tables = {
669  .catname = "pg_catalog.pg_class c",
670  .selcondition = "c.relkind IN (" CppAsString2(RELKIND_FOREIGN_TABLE) ")",
671  .viscondition = "pg_catalog.pg_table_is_visible(c.oid)",
672  .namespace = "c.relnamespace",
673  .result = "c.relname",
674 };
675 
676 static const SchemaQuery Query_for_list_of_tables = {
677  .catname = "pg_catalog.pg_class c",
678  .selcondition =
679  "c.relkind IN (" CppAsString2(RELKIND_RELATION) ", "
680  CppAsString2(RELKIND_PARTITIONED_TABLE) ")",
681  .viscondition = "pg_catalog.pg_table_is_visible(c.oid)",
682  .namespace = "c.relnamespace",
683  .result = "c.relname",
684 };
685 
686 static const SchemaQuery Query_for_list_of_partitioned_tables = {
687  .catname = "pg_catalog.pg_class c",
688  .selcondition = "c.relkind IN (" CppAsString2(RELKIND_PARTITIONED_TABLE) ")",
689  .viscondition = "pg_catalog.pg_table_is_visible(c.oid)",
690  .namespace = "c.relnamespace",
691  .result = "c.relname",
692 };
693 
694 static const SchemaQuery Query_for_list_of_tables_for_constraint = {
695  .catname = "pg_catalog.pg_class c, pg_catalog.pg_constraint con",
696  .selcondition = "c.oid=con.conrelid and c.relkind IN ("
697  CppAsString2(RELKIND_RELATION) ", "
698  CppAsString2(RELKIND_PARTITIONED_TABLE) ")",
699  .viscondition = "pg_catalog.pg_table_is_visible(c.oid)",
700  .namespace = "c.relnamespace",
701  .result = "c.relname",
702  .use_distinct = true,
703  .refname = "con.conname",
704 };
705 
706 static const SchemaQuery Query_for_list_of_tables_for_policy = {
707  .catname = "pg_catalog.pg_class c, pg_catalog.pg_policy p",
708  .selcondition = "c.oid=p.polrelid",
709  .viscondition = "pg_catalog.pg_table_is_visible(c.oid)",
710  .namespace = "c.relnamespace",
711  .result = "c.relname",
712  .use_distinct = true,
713  .refname = "p.polname",
714 };
715 
716 static const SchemaQuery Query_for_list_of_tables_for_rule = {
717  .catname = "pg_catalog.pg_class c, pg_catalog.pg_rewrite r",
718  .selcondition = "c.oid=r.ev_class",
719  .viscondition = "pg_catalog.pg_table_is_visible(c.oid)",
720  .namespace = "c.relnamespace",
721  .result = "c.relname",
722  .use_distinct = true,
723  .refname = "r.rulename",
724 };
725 
726 static const SchemaQuery Query_for_list_of_tables_for_trigger = {
727  .catname = "pg_catalog.pg_class c, pg_catalog.pg_trigger t",
728  .selcondition = "c.oid=t.tgrelid",
729  .viscondition = "pg_catalog.pg_table_is_visible(c.oid)",
730  .namespace = "c.relnamespace",
731  .result = "c.relname",
732  .use_distinct = true,
733  .refname = "t.tgname",
734 };
735 
736 static const SchemaQuery Query_for_list_of_ts_configurations = {
737  .catname = "pg_catalog.pg_ts_config c",
738  .viscondition = "pg_catalog.pg_ts_config_is_visible(c.oid)",
739  .namespace = "c.cfgnamespace",
740  .result = "c.cfgname",
741 };
742 
743 static const SchemaQuery Query_for_list_of_ts_dictionaries = {
744  .catname = "pg_catalog.pg_ts_dict d",
745  .viscondition = "pg_catalog.pg_ts_dict_is_visible(d.oid)",
746  .namespace = "d.dictnamespace",
747  .result = "d.dictname",
748 };
749 
750 static const SchemaQuery Query_for_list_of_ts_parsers = {
751  .catname = "pg_catalog.pg_ts_parser p",
752  .viscondition = "pg_catalog.pg_ts_parser_is_visible(p.oid)",
753  .namespace = "p.prsnamespace",
754  .result = "p.prsname",
755 };
756 
757 static const SchemaQuery Query_for_list_of_ts_templates = {
758  .catname = "pg_catalog.pg_ts_template t",
759  .viscondition = "pg_catalog.pg_ts_template_is_visible(t.oid)",
760  .namespace = "t.tmplnamespace",
761  .result = "t.tmplname",
762 };
763 
764 static const SchemaQuery Query_for_list_of_views = {
765  .catname = "pg_catalog.pg_class c",
766  .selcondition = "c.relkind IN (" CppAsString2(RELKIND_VIEW) ")",
767  .viscondition = "pg_catalog.pg_table_is_visible(c.oid)",
768  .namespace = "c.relnamespace",
769  .result = "c.relname",
770 };
771 
772 static const SchemaQuery Query_for_list_of_matviews = {
773  .catname = "pg_catalog.pg_class c",
774  .selcondition = "c.relkind IN (" CppAsString2(RELKIND_MATVIEW) ")",
775  .viscondition = "pg_catalog.pg_table_is_visible(c.oid)",
776  .namespace = "c.relnamespace",
777  .result = "c.relname",
778 };
779 
780 static const SchemaQuery Query_for_list_of_indexes = {
781  .catname = "pg_catalog.pg_class c",
782  .selcondition =
783  "c.relkind IN (" CppAsString2(RELKIND_INDEX) ", "
784  CppAsString2(RELKIND_PARTITIONED_INDEX) ")",
785  .viscondition = "pg_catalog.pg_table_is_visible(c.oid)",
786  .namespace = "c.relnamespace",
787  .result = "c.relname",
788 };
789 
790 static const SchemaQuery Query_for_list_of_partitioned_indexes = {
791  .catname = "pg_catalog.pg_class c",
792  .selcondition = "c.relkind = " CppAsString2(RELKIND_PARTITIONED_INDEX),
793  .viscondition = "pg_catalog.pg_table_is_visible(c.oid)",
794  .namespace = "c.relnamespace",
795  .result = "c.relname",
796 };
797 
798 
799 /* All relations */
800 static const SchemaQuery Query_for_list_of_relations = {
801  .catname = "pg_catalog.pg_class c",
802  .viscondition = "pg_catalog.pg_table_is_visible(c.oid)",
803  .namespace = "c.relnamespace",
804  .result = "c.relname",
805 };
806 
807 /* partitioned relations */
808 static const SchemaQuery Query_for_list_of_partitioned_relations = {
809  .catname = "pg_catalog.pg_class c",
810  .selcondition = "c.relkind IN (" CppAsString2(RELKIND_PARTITIONED_TABLE)
811  ", " CppAsString2(RELKIND_PARTITIONED_INDEX) ")",
812  .viscondition = "pg_catalog.pg_table_is_visible(c.oid)",
813  .namespace = "c.relnamespace",
814  .result = "c.relname",
815 };
816 
817 static const SchemaQuery Query_for_list_of_operator_families = {
818  .catname = "pg_catalog.pg_opfamily c",
819  .viscondition = "pg_catalog.pg_opfamily_is_visible(c.oid)",
820  .namespace = "c.opfnamespace",
821  .result = "c.opfname",
822 };
823 
824 /* Relations supporting INSERT, UPDATE or DELETE */
825 static const SchemaQuery Query_for_list_of_updatables = {
826  .catname = "pg_catalog.pg_class c",
827  .selcondition =
828  "c.relkind IN (" CppAsString2(RELKIND_RELATION) ", "
829  CppAsString2(RELKIND_FOREIGN_TABLE) ", "
830  CppAsString2(RELKIND_VIEW) ", "
831  CppAsString2(RELKIND_PARTITIONED_TABLE) ")",
832  .viscondition = "pg_catalog.pg_table_is_visible(c.oid)",
833  .namespace = "c.relnamespace",
834  .result = "c.relname",
835 };
836 
837 /* Relations supporting MERGE */
838 static const SchemaQuery Query_for_list_of_mergetargets = {
839  .catname = "pg_catalog.pg_class c",
840  .selcondition =
841  "c.relkind IN (" CppAsString2(RELKIND_RELATION) ", "
842  CppAsString2(RELKIND_VIEW) ", "
843  CppAsString2(RELKIND_PARTITIONED_TABLE) ") ",
844  .viscondition = "pg_catalog.pg_table_is_visible(c.oid)",
845  .namespace = "c.relnamespace",
846  .result = "c.relname",
847 };
848 
849 /* Relations supporting SELECT */
850 static const SchemaQuery Query_for_list_of_selectables = {
851  .catname = "pg_catalog.pg_class c",
852  .selcondition =
853  "c.relkind IN (" CppAsString2(RELKIND_RELATION) ", "
854  CppAsString2(RELKIND_SEQUENCE) ", "
855  CppAsString2(RELKIND_VIEW) ", "
856  CppAsString2(RELKIND_MATVIEW) ", "
857  CppAsString2(RELKIND_FOREIGN_TABLE) ", "
858  CppAsString2(RELKIND_PARTITIONED_TABLE) ")",
859  .viscondition = "pg_catalog.pg_table_is_visible(c.oid)",
860  .namespace = "c.relnamespace",
861  .result = "c.relname",
862 };
863 
864 /* Relations supporting TRUNCATE */
865 static const SchemaQuery Query_for_list_of_truncatables = {
866  .catname = "pg_catalog.pg_class c",
867  .selcondition =
868  "c.relkind IN (" CppAsString2(RELKIND_RELATION) ", "
869  CppAsString2(RELKIND_FOREIGN_TABLE) ", "
870  CppAsString2(RELKIND_PARTITIONED_TABLE) ")",
871  .viscondition = "pg_catalog.pg_table_is_visible(c.oid)",
872  .namespace = "c.relnamespace",
873  .result = "c.relname",
874 };
875 
876 /* Relations supporting GRANT are currently same as those supporting SELECT */
877 #define Query_for_list_of_grantables Query_for_list_of_selectables
878 
879 /* Relations supporting ANALYZE */
880 static const SchemaQuery Query_for_list_of_analyzables = {
881  .catname = "pg_catalog.pg_class c",
882  .selcondition =
883  "c.relkind IN (" CppAsString2(RELKIND_RELATION) ", "
884  CppAsString2(RELKIND_PARTITIONED_TABLE) ", "
885  CppAsString2(RELKIND_MATVIEW) ", "
886  CppAsString2(RELKIND_FOREIGN_TABLE) ")",
887  .viscondition = "pg_catalog.pg_table_is_visible(c.oid)",
888  .namespace = "c.relnamespace",
889  .result = "c.relname",
890 };
891 
892 /* Relations supporting index creation */
893 static const SchemaQuery Query_for_list_of_indexables = {
894  .catname = "pg_catalog.pg_class c",
895  .selcondition =
896  "c.relkind IN (" CppAsString2(RELKIND_RELATION) ", "
897  CppAsString2(RELKIND_PARTITIONED_TABLE) ", "
898  CppAsString2(RELKIND_MATVIEW) ")",
899  .viscondition = "pg_catalog.pg_table_is_visible(c.oid)",
900  .namespace = "c.relnamespace",
901  .result = "c.relname",
902 };
903 
904 /*
905  * Relations supporting VACUUM are currently same as those supporting
906  * indexing.
907  */
908 #define Query_for_list_of_vacuumables Query_for_list_of_indexables
909 
910 /* Relations supporting CLUSTER */
911 static const SchemaQuery Query_for_list_of_clusterables = {
912  .catname = "pg_catalog.pg_class c",
913  .selcondition =
914  "c.relkind IN (" CppAsString2(RELKIND_RELATION) ", "
915  CppAsString2(RELKIND_PARTITIONED_TABLE) ", "
916  CppAsString2(RELKIND_MATVIEW) ")",
917  .viscondition = "pg_catalog.pg_table_is_visible(c.oid)",
918  .namespace = "c.relnamespace",
919  .result = "c.relname",
920 };
921 
922 static const SchemaQuery Query_for_list_of_constraints_with_schema = {
923  .catname = "pg_catalog.pg_constraint c",
924  .selcondition = "c.conrelid <> 0",
925  .namespace = "c.connamespace",
926  .result = "c.conname",
927 };
928 
929 static const SchemaQuery Query_for_list_of_statistics = {
930  .catname = "pg_catalog.pg_statistic_ext s",
931  .viscondition = "pg_catalog.pg_statistics_obj_is_visible(s.oid)",
932  .namespace = "s.stxnamespace",
933  .result = "s.stxname",
934 };
935 
936 static const SchemaQuery Query_for_list_of_collations = {
937  .catname = "pg_catalog.pg_collation c",
938  .selcondition = "c.collencoding IN (-1, pg_catalog.pg_char_to_encoding(pg_catalog.getdatabaseencoding()))",
939  .viscondition = "pg_catalog.pg_collation_is_visible(c.oid)",
940  .namespace = "c.collnamespace",
941  .result = "c.collname",
942 };
943 
944 static const SchemaQuery Query_for_partition_of_table = {
945  .catname = "pg_catalog.pg_class c1, pg_catalog.pg_class c2, pg_catalog.pg_inherits i",
946  .selcondition = "c1.oid=i.inhparent and i.inhrelid=c2.oid and c2.relispartition",
947  .viscondition = "pg_catalog.pg_table_is_visible(c2.oid)",
948  .namespace = "c2.relnamespace",
949  .result = "c2.relname",
950  .refname = "c1.relname",
951  .refviscondition = "pg_catalog.pg_table_is_visible(c1.oid)",
952  .refnamespace = "c1.relnamespace",
953 };
954 
955 static const SchemaQuery Query_for_rule_of_table = {
956  .catname = "pg_catalog.pg_rewrite r, pg_catalog.pg_class c1",
957  .selcondition = "r.ev_class=c1.oid",
958  .result = "r.rulename",
959  .refname = "c1.relname",
960  .refviscondition = "pg_catalog.pg_table_is_visible(c1.oid)",
961  .refnamespace = "c1.relnamespace",
962 };
963 
964 static const SchemaQuery Query_for_trigger_of_table = {
965  .catname = "pg_catalog.pg_trigger t, pg_catalog.pg_class c1",
966  .selcondition = "t.tgrelid=c1.oid and not t.tgisinternal",
967  .result = "t.tgname",
968  .refname = "c1.relname",
969  .refviscondition = "pg_catalog.pg_table_is_visible(c1.oid)",
970  .refnamespace = "c1.relnamespace",
971 };
972 
973 
974 /*
975  * Queries to get lists of names of various kinds of things, possibly
976  * restricted to names matching a partially entered name. Don't use
977  * this method where the user might wish to enter a schema-qualified
978  * name; make a SchemaQuery instead.
979  *
980  * In these queries, there must be a restriction clause of the form
981  * output LIKE '%s'
982  * where "output" is the same string that the query returns. The %s
983  * will be replaced by a LIKE pattern to match the already-typed text.
984  *
985  * There can be a second '%s', which will be replaced by a suitably-escaped
986  * version of the string provided in completion_ref_object. If there is a
987  * third '%s', it will be replaced by a suitably-escaped version of the string
988  * provided in completion_ref_schema. NOTE: using completion_ref_object
989  * that way is usually the wrong thing, and using completion_ref_schema
990  * that way is always the wrong thing. Make a SchemaQuery instead.
991  */
992 
993 #define Query_for_list_of_template_databases \
994 "SELECT d.datname "\
995 " FROM pg_catalog.pg_database d "\
996 " WHERE d.datname LIKE '%s' "\
997 " AND (d.datistemplate OR pg_catalog.pg_has_role(d.datdba, 'USAGE'))"
998 
999 #define Query_for_list_of_databases \
1000 "SELECT datname FROM pg_catalog.pg_database "\
1001 " WHERE datname LIKE '%s'"
1002 
1003 #define Query_for_list_of_tablespaces \
1004 "SELECT spcname FROM pg_catalog.pg_tablespace "\
1005 " WHERE spcname LIKE '%s'"
1006 
1007 #define Query_for_list_of_encodings \
1008 " SELECT DISTINCT pg_catalog.pg_encoding_to_char(conforencoding) "\
1009 " FROM pg_catalog.pg_conversion "\
1010 " WHERE pg_catalog.pg_encoding_to_char(conforencoding) LIKE pg_catalog.upper('%s')"
1011 
1012 #define Query_for_list_of_languages \
1013 "SELECT lanname "\
1014 " FROM pg_catalog.pg_language "\
1015 " WHERE lanname != 'internal' "\
1016 " AND lanname LIKE '%s'"
1017 
1018 #define Query_for_list_of_schemas \
1019 "SELECT nspname FROM pg_catalog.pg_namespace "\
1020 " WHERE nspname LIKE '%s'"
1021 
1022 /* Use COMPLETE_WITH_QUERY_VERBATIM with these queries for GUC names: */
1023 #define Query_for_list_of_alter_system_set_vars \
1024 "SELECT pg_catalog.lower(name) FROM pg_catalog.pg_settings "\
1025 " WHERE context != 'internal' "\
1026 " AND pg_catalog.lower(name) LIKE pg_catalog.lower('%s')"
1027 
1028 #define Query_for_list_of_set_vars \
1029 "SELECT pg_catalog.lower(name) FROM pg_catalog.pg_settings "\
1030 " WHERE context IN ('user', 'superuser') "\
1031 " AND pg_catalog.lower(name) LIKE pg_catalog.lower('%s')"
1032 
1033 #define Query_for_list_of_show_vars \
1034 "SELECT pg_catalog.lower(name) FROM pg_catalog.pg_settings "\
1035 " WHERE pg_catalog.lower(name) LIKE pg_catalog.lower('%s')"
1036 
1037 #define Query_for_list_of_roles \
1038 " SELECT rolname "\
1039 " FROM pg_catalog.pg_roles "\
1040 " WHERE rolname LIKE '%s'"
1041 
1042 /* add these to Query_for_list_of_roles in OWNER contexts */
1043 #define Keywords_for_list_of_owner_roles \
1044 "CURRENT_ROLE", "CURRENT_USER", "SESSION_USER"
1045 
1046 /* add these to Query_for_list_of_roles in GRANT contexts */
1047 #define Keywords_for_list_of_grant_roles \
1048 Keywords_for_list_of_owner_roles, "PUBLIC"
1049 
1050 #define Query_for_all_table_constraints \
1051 "SELECT conname "\
1052 " FROM pg_catalog.pg_constraint c "\
1053 " WHERE c.conrelid <> 0 "\
1054 " and conname LIKE '%s'"
1055 
1056 #define Query_for_list_of_fdws \
1057 " SELECT fdwname "\
1058 " FROM pg_catalog.pg_foreign_data_wrapper "\
1059 " WHERE fdwname LIKE '%s'"
1060 
1061 #define Query_for_list_of_servers \
1062 " SELECT srvname "\
1063 " FROM pg_catalog.pg_foreign_server "\
1064 " WHERE srvname LIKE '%s'"
1065 
1066 #define Query_for_list_of_user_mappings \
1067 " SELECT usename "\
1068 " FROM pg_catalog.pg_user_mappings "\
1069 " WHERE usename LIKE '%s'"
1070 
1071 #define Query_for_list_of_access_methods \
1072 " SELECT amname "\
1073 " FROM pg_catalog.pg_am "\
1074 " WHERE amname LIKE '%s'"
1075 
1076 #define Query_for_list_of_index_access_methods \
1077 " SELECT amname "\
1078 " FROM pg_catalog.pg_am "\
1079 " WHERE amname LIKE '%s' AND "\
1080 " amtype=" CppAsString2(AMTYPE_INDEX)
1081 
1082 #define Query_for_list_of_table_access_methods \
1083 " SELECT amname "\
1084 " FROM pg_catalog.pg_am "\
1085 " WHERE amname LIKE '%s' AND "\
1086 " amtype=" CppAsString2(AMTYPE_TABLE)
1087 
1088 #define Query_for_list_of_extensions \
1089 " SELECT extname "\
1090 " FROM pg_catalog.pg_extension "\
1091 " WHERE extname LIKE '%s'"
1092 
1093 #define Query_for_list_of_available_extensions \
1094 " SELECT name "\
1095 " FROM pg_catalog.pg_available_extensions "\
1096 " WHERE name LIKE '%s' AND installed_version IS NULL"
1097 
1098 #define Query_for_list_of_available_extension_versions \
1099 " SELECT version "\
1100 " FROM pg_catalog.pg_available_extension_versions "\
1101 " WHERE version LIKE '%s' AND name='%s'"
1102 
1103 #define Query_for_list_of_prepared_statements \
1104 " SELECT name "\
1105 " FROM pg_catalog.pg_prepared_statements "\
1106 " WHERE name LIKE '%s'"
1107 
1108 #define Query_for_list_of_event_triggers \
1109 " SELECT evtname "\
1110 " FROM pg_catalog.pg_event_trigger "\
1111 " WHERE evtname LIKE '%s'"
1112 
1113 #define Query_for_list_of_tablesample_methods \
1114 " SELECT proname "\
1115 " FROM pg_catalog.pg_proc "\
1116 " WHERE prorettype = 'pg_catalog.tsm_handler'::pg_catalog.regtype AND "\
1117 " proargtypes[0] = 'pg_catalog.internal'::pg_catalog.regtype AND "\
1118 " proname LIKE '%s'"
1119 
1120 #define Query_for_list_of_policies \
1121 " SELECT polname "\
1122 " FROM pg_catalog.pg_policy "\
1123 " WHERE polname LIKE '%s'"
1124 
1125 #define Query_for_values_of_enum_GUC \
1126 " SELECT val FROM ( "\
1127 " SELECT name, pg_catalog.unnest(enumvals) AS val "\
1128 " FROM pg_catalog.pg_settings "\
1129 " ) ss "\
1130 " WHERE val LIKE '%s'"\
1131 " and pg_catalog.lower(name)=pg_catalog.lower('%s')"
1132 
1133 #define Query_for_list_of_channels \
1134 " SELECT channel "\
1135 " FROM pg_catalog.pg_listening_channels() AS channel "\
1136 " WHERE channel LIKE '%s'"
1137 
1138 #define Query_for_list_of_cursors \
1139 " SELECT name "\
1140 " FROM pg_catalog.pg_cursors "\
1141 " WHERE name LIKE '%s'"
1142 
1143 #define Query_for_list_of_timezone_names_unquoted \
1144 " SELECT name "\
1145 " FROM pg_catalog.pg_timezone_names() "\
1146 " WHERE pg_catalog.lower(name) LIKE pg_catalog.lower('%s')"
1147 
1148 #define Query_for_list_of_timezone_names_quoted_out \
1149 "SELECT pg_catalog.quote_literal(name) AS name "\
1150 " FROM pg_catalog.pg_timezone_names() "\
1151 " WHERE pg_catalog.lower(name) LIKE pg_catalog.lower('%s')"
1152 
1153 #define Query_for_list_of_timezone_names_quoted_in \
1154 "SELECT pg_catalog.quote_literal(name) AS name "\
1155 " FROM pg_catalog.pg_timezone_names() "\
1156 " WHERE pg_catalog.quote_literal(pg_catalog.lower(name)) LIKE pg_catalog.lower('%s')"
1157 
1158 /* Privilege options shared between GRANT and REVOKE */
1159 #define Privilege_options_of_grant_and_revoke \
1160 "SELECT", "INSERT", "UPDATE", "DELETE", "TRUNCATE", "REFERENCES", "TRIGGER", \
1161 "CREATE", "CONNECT", "TEMPORARY", "EXECUTE", "USAGE", "SET", "ALTER SYSTEM", \
1162 "MAINTAIN", "ALL"
1163 
1164 /* ALTER PROCEDURE options */
1165 #define Alter_procedure_options \
1166 "DEPENDS ON EXTENSION", "EXTERNAL SECURITY", "NO DEPENDS ON EXTENSION", \
1167 "OWNER TO", "RENAME TO", "RESET", "SECURITY", "SET"
1168 
1169 /* ALTER ROUTINE options */
1170 #define Alter_routine_options \
1171 Alter_procedure_options, "COST", "IMMUTABLE", "LEAKPROOF", "NOT LEAKPROOF", \
1172 "PARALLEL", "ROWS", "STABLE", "VOLATILE"
1173 
1174 /* ALTER FUNCTION options */
1175 #define Alter_function_options \
1176 Alter_routine_options, "CALLED ON NULL INPUT", "RETURNS NULL ON NULL INPUT", \
1177 "STRICT", "SUPPORT"
1178 
1179 /*
1180  * These object types were introduced later than our support cutoff of
1181  * server version 9.2. We use the VersionedQuery infrastructure so that
1182  * we don't send certain-to-fail queries to older servers.
1183  */
1184 
1185 static const VersionedQuery Query_for_list_of_publications[] = {
1186  {100000,
1187  " SELECT pubname "
1188  " FROM pg_catalog.pg_publication "
1189  " WHERE pubname LIKE '%s'"
1190  },
1191  {0, NULL}
1192 };
1193 
1194 static const VersionedQuery Query_for_list_of_subscriptions[] = {
1195  {100000,
1196  " SELECT s.subname "
1197  " FROM pg_catalog.pg_subscription s, pg_catalog.pg_database d "
1198  " WHERE s.subname LIKE '%s' "
1199  " AND d.datname = pg_catalog.current_database() "
1200  " AND s.subdbid = d.oid"
1201  },
1202  {0, NULL}
1203 };
1204 
1205  /* Known command-starting keywords. */
1206 static const char *const sql_commands[] = {
1207  "ABORT", "ALTER", "ANALYZE", "BEGIN", "CALL", "CHECKPOINT", "CLOSE", "CLUSTER",
1208  "COMMENT", "COMMIT", "COPY", "CREATE", "DEALLOCATE", "DECLARE",
1209  "DELETE FROM", "DISCARD", "DO", "DROP", "END", "EXECUTE", "EXPLAIN",
1210  "FETCH", "GRANT", "IMPORT FOREIGN SCHEMA", "INSERT INTO", "LISTEN", "LOAD", "LOCK",
1211  "MERGE INTO", "MOVE", "NOTIFY", "PREPARE",
1212  "REASSIGN", "REFRESH MATERIALIZED VIEW", "REINDEX", "RELEASE",
1213  "RESET", "REVOKE", "ROLLBACK",
1214  "SAVEPOINT", "SECURITY LABEL", "SELECT", "SET", "SHOW", "START",
1215  "TABLE", "TRUNCATE", "UNLISTEN", "UPDATE", "VACUUM", "VALUES", "WITH",
1216  NULL
1217 };
1218 
1219 /*
1220  * This is a list of all "things" in Pgsql, which can show up after CREATE or
1221  * DROP; and there is also a query to get a list of them.
1222  */
1223 
1224 typedef struct
1225 {
1226  const char *name;
1227  /* Provide at most one of these three types of query: */
1228  const char *query; /* simple query, or NULL */
1229  const VersionedQuery *vquery; /* versioned query, or NULL */
1230  const SchemaQuery *squery; /* schema query, or NULL */
1231  const char *const *keywords; /* keywords to be offered as well */
1232  const bits32 flags; /* visibility flags, see below */
1233 } pgsql_thing_t;
1234 
1235 #define THING_NO_CREATE (1 << 0) /* should not show up after CREATE */
1236 #define THING_NO_DROP (1 << 1) /* should not show up after DROP */
1237 #define THING_NO_ALTER (1 << 2) /* should not show up after ALTER */
1238 #define THING_NO_SHOW (THING_NO_CREATE | THING_NO_DROP | THING_NO_ALTER)
1239 
1240 /* When we have DROP USER etc, also offer MAPPING FOR */
1241 static const char *const Keywords_for_user_thing[] = {
1242  "MAPPING FOR",
1243  NULL
1244 };
1245 
1246 static const pgsql_thing_t words_after_create[] = {
1247  {"ACCESS METHOD", NULL, NULL, NULL, NULL, THING_NO_ALTER},
1248  {"AGGREGATE", NULL, NULL, Query_for_list_of_aggregates},
1249  {"CAST", NULL, NULL, NULL}, /* Casts have complex structures for names, so
1250  * skip it */
1251  {"COLLATION", NULL, NULL, &Query_for_list_of_collations},
1252 
1253  /*
1254  * CREATE CONSTRAINT TRIGGER is not supported here because it is designed
1255  * to be used only by pg_dump.
1256  */
1257  {"CONFIGURATION", NULL, NULL, &Query_for_list_of_ts_configurations, NULL, THING_NO_SHOW},
1258  {"CONVERSION", "SELECT conname FROM pg_catalog.pg_conversion WHERE conname LIKE '%s'"},
1259  {"DATABASE", Query_for_list_of_databases},
1260  {"DEFAULT PRIVILEGES", NULL, NULL, NULL, NULL, THING_NO_CREATE | THING_NO_DROP},
1261  {"DICTIONARY", NULL, NULL, &Query_for_list_of_ts_dictionaries, NULL, THING_NO_SHOW},
1262  {"DOMAIN", NULL, NULL, &Query_for_list_of_domains},
1263  {"EVENT TRIGGER", NULL, NULL, NULL},
1264  {"EXTENSION", Query_for_list_of_extensions},
1265  {"FOREIGN DATA WRAPPER", NULL, NULL, NULL},
1266  {"FOREIGN TABLE", NULL, NULL, NULL},
1267  {"FUNCTION", NULL, NULL, Query_for_list_of_functions},
1268  {"GROUP", Query_for_list_of_roles},
1269  {"INDEX", NULL, NULL, &Query_for_list_of_indexes},
1270  {"LANGUAGE", Query_for_list_of_languages},
1271  {"LARGE OBJECT", NULL, NULL, NULL, NULL, THING_NO_CREATE | THING_NO_DROP},
1272  {"MATERIALIZED VIEW", NULL, NULL, &Query_for_list_of_matviews},
1273  {"OPERATOR", NULL, NULL, NULL}, /* Querying for this is probably not such
1274  * a good idea. */
1275  {"OR REPLACE", NULL, NULL, NULL, NULL, THING_NO_DROP | THING_NO_ALTER},
1276  {"OWNED", NULL, NULL, NULL, NULL, THING_NO_CREATE | THING_NO_ALTER}, /* for DROP OWNED BY ... */
1277  {"PARSER", NULL, NULL, &Query_for_list_of_ts_parsers, NULL, THING_NO_SHOW},
1278  {"POLICY", NULL, NULL, NULL},
1279  {"PROCEDURE", NULL, NULL, Query_for_list_of_procedures},
1280  {"PUBLICATION", NULL, Query_for_list_of_publications},
1281  {"ROLE", Query_for_list_of_roles},
1282  {"ROUTINE", NULL, NULL, &Query_for_list_of_routines, NULL, THING_NO_CREATE},
1283  {"RULE", "SELECT rulename FROM pg_catalog.pg_rules WHERE rulename LIKE '%s'"},
1284  {"SCHEMA", Query_for_list_of_schemas},
1285  {"SEQUENCE", NULL, NULL, &Query_for_list_of_sequences},
1286  {"SERVER", Query_for_list_of_servers},
1287  {"STATISTICS", NULL, NULL, &Query_for_list_of_statistics},
1288  {"SUBSCRIPTION", NULL, Query_for_list_of_subscriptions},
1289  {"SYSTEM", NULL, NULL, NULL, NULL, THING_NO_CREATE | THING_NO_DROP},
1290  {"TABLE", NULL, NULL, &Query_for_list_of_tables},
1291  {"TABLESPACE", Query_for_list_of_tablespaces},
1292  {"TEMP", NULL, NULL, NULL, NULL, THING_NO_DROP | THING_NO_ALTER}, /* for CREATE TEMP TABLE
1293  * ... */
1294  {"TEMPLATE", NULL, NULL, &Query_for_list_of_ts_templates, NULL, THING_NO_SHOW},
1295  {"TEMPORARY", NULL, NULL, NULL, NULL, THING_NO_DROP | THING_NO_ALTER}, /* for CREATE TEMPORARY
1296  * TABLE ... */
1297  {"TEXT SEARCH", NULL, NULL, NULL},
1298  {"TRANSFORM", NULL, NULL, NULL, NULL, THING_NO_ALTER},
1299  {"TRIGGER", "SELECT tgname FROM pg_catalog.pg_trigger WHERE tgname LIKE '%s' AND NOT tgisinternal"},
1300  {"TYPE", NULL, NULL, &Query_for_list_of_datatypes},
1301  {"UNIQUE", NULL, NULL, NULL, NULL, THING_NO_DROP | THING_NO_ALTER}, /* for CREATE UNIQUE
1302  * INDEX ... */
1303  {"UNLOGGED", NULL, NULL, NULL, NULL, THING_NO_DROP | THING_NO_ALTER}, /* for CREATE UNLOGGED
1304  * TABLE ... */
1305  {"USER", Query_for_list_of_roles, NULL, NULL, Keywords_for_user_thing},
1306  {"USER MAPPING FOR", NULL, NULL, NULL},
1307  {"VIEW", NULL, NULL, &Query_for_list_of_views},
1308  {NULL} /* end of list */
1309 };
1310 
1311 /*
1312  * The tcpatterns[] table provides the initial pattern-match rule for each
1313  * switch case in match_previous_words(). The contents of the table
1314  * are constructed by gen_tabcomplete.pl.
1315  */
1316 
1317 /* Basic match rules appearing in tcpatterns[].kind */
1318 enum TCPatternKind
1319 {
1320  Match,
1321  MatchCS,
1322  HeadMatch,
1323  HeadMatchCS,
1324  TailMatch,
1325  TailMatchCS,
1326 };
1327 
1328 /* Things besides string literals that can appear in tcpatterns[].words */
1329 #define MatchAny NULL
1330 #define MatchAnyExcept(pattern) ("!" pattern)
1331 #define MatchAnyN ""
1332 
1333 /* One entry in tcpatterns[] */
1334 typedef struct
1335 {
1336  int id; /* case label used in match_previous_words */
1337  enum TCPatternKind kind; /* match kind, see above */
1338  int nwords; /* length of words[] array */
1339  const char *const *words; /* array of match words */
1340 } TCPattern;
1341 
1342 /* Macro emitted by gen_tabcomplete.pl to fill a tcpatterns[] entry */
1343 #define TCPAT(id, kind, ...) \
1344  { (id), (kind), VA_ARGS_NARGS(__VA_ARGS__), \
1345  (const char * const []) { __VA_ARGS__ } }
1346 
1347 #ifdef SWITCH_CONVERSION_APPLIED
1348 
1349 static const TCPattern tcpatterns[] =
1350 {
1351  /* Insert tab-completion pattern data here. */
1352 };
1353 
1354 #endif /* SWITCH_CONVERSION_APPLIED */
1355 
1356 /* Storage parameters for CREATE TABLE and ALTER TABLE */
1357 static const char *const table_storage_parameters[] = {
1358  "autovacuum_analyze_scale_factor",
1359  "autovacuum_analyze_threshold",
1360  "autovacuum_enabled",
1361  "autovacuum_freeze_max_age",
1362  "autovacuum_freeze_min_age",
1363  "autovacuum_freeze_table_age",
1364  "autovacuum_multixact_freeze_max_age",
1365  "autovacuum_multixact_freeze_min_age",
1366  "autovacuum_multixact_freeze_table_age",
1367  "autovacuum_vacuum_cost_delay",
1368  "autovacuum_vacuum_cost_limit",
1369  "autovacuum_vacuum_insert_scale_factor",
1370  "autovacuum_vacuum_insert_threshold",
1371  "autovacuum_vacuum_scale_factor",
1372  "autovacuum_vacuum_threshold",
1373  "fillfactor",
1374  "log_autovacuum_min_duration",
1375  "parallel_workers",
1376  "toast.autovacuum_enabled",
1377  "toast.autovacuum_freeze_max_age",
1378  "toast.autovacuum_freeze_min_age",
1379  "toast.autovacuum_freeze_table_age",
1380  "toast.autovacuum_multixact_freeze_max_age",
1381  "toast.autovacuum_multixact_freeze_min_age",
1382  "toast.autovacuum_multixact_freeze_table_age",
1383  "toast.autovacuum_vacuum_cost_delay",
1384  "toast.autovacuum_vacuum_cost_limit",
1385  "toast.autovacuum_vacuum_insert_scale_factor",
1386  "toast.autovacuum_vacuum_insert_threshold",
1387  "toast.autovacuum_vacuum_scale_factor",
1388  "toast.autovacuum_vacuum_threshold",
1389  "toast.log_autovacuum_min_duration",
1390  "toast.vacuum_index_cleanup",
1391  "toast.vacuum_truncate",
1392  "toast_tuple_target",
1393  "user_catalog_table",
1394  "vacuum_index_cleanup",
1395  "vacuum_truncate",
1396  NULL
1397 };
1398 
1399 /* Optional parameters for CREATE VIEW and ALTER VIEW */
1400 static const char *const view_optional_parameters[] = {
1401  "check_option",
1402  "security_barrier",
1403  "security_invoker",
1404  NULL
1405 };
1406 
1407 /* Forward declaration of functions */
1408 static char **psql_completion(const char *text, int start, int end);
1409 static char **match_previous_words(int pattern_id,
1410  const char *text, int start, int end,
1411  char **previous_words,
1412  int previous_words_count);
1413 static char *create_command_generator(const char *text, int state);
1414 static char *drop_command_generator(const char *text, int state);
1415 static char *alter_command_generator(const char *text, int state);
1416 static char *complete_from_query(const char *text, int state);
1417 static char *complete_from_versioned_query(const char *text, int state);
1418 static char *complete_from_schema_query(const char *text, int state);
1419 static char *complete_from_versioned_schema_query(const char *text, int state);
1420 static char *_complete_from_query(const char *simple_query,
1421  const SchemaQuery *schema_query,
1422  const char *const *keywords,
1423  bool verbatim,
1424  const char *text, int state);
1425 static void set_completion_reference(const char *word);
1426 static void set_completion_reference_verbatim(const char *word);
1427 static char *complete_from_list(const char *text, int state);
1428 static char *complete_from_const(const char *text, int state);
1429 static void append_variable_names(char ***varnames, int *nvars,
1430  int *maxvars, const char *varname,
1431  const char *prefix, const char *suffix);
1432 static char **complete_from_variables(const char *text,
1433  const char *prefix, const char *suffix, bool need_value);
1434 static char *complete_from_files(const char *text, int state);
1435 
1436 static char *pg_strdup_keyword_case(const char *s, const char *ref);
1437 static char *escape_string(const char *text);
1438 static char *make_like_pattern(const char *word);
1439 static void parse_identifier(const char *ident,
1440  char **schemaname, char **objectname,
1441  bool *schemaquoted, bool *objectquoted);
1442 static char *requote_identifier(const char *schemaname, const char *objectname,
1443  bool quote_schema, bool quote_object);
1444 static bool identifier_needs_quotes(const char *ident);
1445 static PGresult *exec_query(const char *query);
1446 
1447 static char **get_previous_words(int point, char **buffer, int *nwords);
1448 
1449 static char *get_guctype(const char *varname);
1450 
1451 #ifdef USE_FILENAME_QUOTING_FUNCTIONS
1452 static char *quote_file_name(char *fname, int match_type, char *quote_pointer);
1453 static char *dequote_file_name(char *fname, int quote_char);
1454 #endif
1455 
1456 
1457 /*
1458  * Initialize the readline library for our purposes.
1459  */
1460 void
1461 initialize_readline(void)
1462 {
1463  rl_readline_name = (char *) pset.progname;
1464  rl_attempted_completion_function = psql_completion;
1465 
1466 #ifdef USE_FILENAME_QUOTING_FUNCTIONS
1467  rl_filename_quoting_function = quote_file_name;
1468  rl_filename_dequoting_function = dequote_file_name;
1469 #endif
1470 
1471  rl_basic_word_break_characters = WORD_BREAKS;
1472 
1473  /*
1474  * Ideally we'd include '"' in rl_completer_quote_characters too, which
1475  * should allow us to complete quoted identifiers that include spaces.
1476  * However, the library support for rl_completer_quote_characters is
1477  * presently too inconsistent to want to mess with that. (Note in
1478  * particular that libedit has this variable but completely ignores it.)
1479  */
1480  rl_completer_quote_characters = "'";
1481 
1482  /*
1483  * Set rl_filename_quote_characters to "all possible characters",
1484  * otherwise Readline will skip filename quoting if it thinks a filename
1485  * doesn't need quoting. Readline actually interprets this as bytes, so
1486  * there are no encoding considerations here.
1487  */
1488 #ifdef HAVE_RL_FILENAME_QUOTE_CHARACTERS
1489  {
1490  unsigned char *fqc = (unsigned char *) pg_malloc(256);
1491 
1492  for (int i = 0; i < 255; i++)
1493  fqc[i] = (unsigned char) (i + 1);
1494  fqc[255] = '\0';
1495  rl_filename_quote_characters = (const char *) fqc;
1496  }
1497 #endif
1498 
1499  completion_max_records = 1000;
1500 
1501  /*
1502  * There is a variable rl_completion_query_items for this but apparently
1503  * it's not defined everywhere.
1504  */
1505 }
1506 
1507 /*
1508  * Check if 'word' matches any of the '|'-separated strings in 'pattern',
1509  * using case-insensitive or case-sensitive comparisons.
1510  *
1511  * If pattern is NULL, it's a wild card that matches any word.
1512  * If pattern begins with '!', the result is negated, ie we check that 'word'
1513  * does *not* match any alternative appearing in the rest of 'pattern'.
1514  * Any alternative can contain '*' which is a wild card, i.e., it can match
1515  * any substring; however, we allow at most one '*' per alternative.
1516  *
1517  * For readability, callers should use the macros MatchAny and MatchAnyExcept
1518  * to invoke those two special cases for 'pattern'. (But '|' and '*' must
1519  * just be written directly in patterns.) There is also MatchAnyN, but that
1520  * is supported only in Matches/MatchesCS and is not handled here.
1521  */
1522 static bool
1523 word_matches(const char *pattern,
1524  const char *word,
1525  bool case_sensitive)
1526 {
1527  size_t wordlen;
1528 
1529 #define cimatch(s1, s2, n) \
1530  (case_sensitive ? strncmp(s1, s2, n) == 0 : pg_strncasecmp(s1, s2, n) == 0)
1531 
1532  /* NULL pattern matches anything. */
1533  if (pattern == NULL)
1534  return true;
1535 
1536  /* Handle negated patterns from the MatchAnyExcept macro. */
1537  if (*pattern == '!')
1538  return !word_matches(pattern + 1, word, case_sensitive);
1539 
1540  /* Else consider each alternative in the pattern. */
1541  wordlen = strlen(word);
1542  for (;;)
1543  {
1544  const char *star = NULL;
1545  const char *c;
1546 
1547  /* Find end of current alternative, and locate any wild card. */
1548  c = pattern;
1549  while (*c != '\0' && *c != '|')
1550  {
1551  if (*c == '*')
1552  star = c;
1553  c++;
1554  }
1555  /* Was there a wild card? */
1556  if (star)
1557  {
1558  /* Yes, wildcard match? */
1559  size_t beforelen = star - pattern,
1560  afterlen = c - star - 1;
1561 
1562  if (wordlen >= (beforelen + afterlen) &&
1563  cimatch(word, pattern, beforelen) &&
1564  cimatch(word + wordlen - afterlen, star + 1, afterlen))
1565  return true;
1566  }
1567  else
1568  {
1569  /* No, plain match? */
1570  if (wordlen == (c - pattern) &&
1571  cimatch(word, pattern, wordlen))
1572  return true;
1573  }
1574  /* Out of alternatives? */
1575  if (*c == '\0')
1576  break;
1577  /* Nope, try next alternative. */
1578  pattern = c + 1;
1579  }
1580 
1581  return false;
1582 }
1583 
1584 /*
1585  * Implementation of TailMatches and TailMatchesCS tests: do the last N words
1586  * in previous_words match the pattern arguments?
1587  *
1588  * The array indexing might look backwards, but remember that
1589  * previous_words[0] contains the *last* word on the line, not the first.
1590  */
1591 static bool
1592 TailMatchesArray(bool case_sensitive,
1593  int previous_words_count, char **previous_words,
1594  int narg, const char *const *args)
1595 {
1596  if (previous_words_count < narg)
1597  return false;
1598 
1599  for (int argno = 0; argno < narg; argno++)
1600  {
1601  const char *arg = args[argno];
1602 
1603  if (!word_matches(arg, previous_words[narg - argno - 1],
1604  case_sensitive))
1605  return false;
1606  }
1607 
1608  return true;
1609 }
1610 
1611 /*
1612  * As above, but the pattern is passed as a variadic argument list.
1613  */
1614 static bool
1615 TailMatchesImpl(bool case_sensitive,
1616  int previous_words_count, char **previous_words,
1617  int narg,...)
1618 {
1619  const char *argarray[64];
1620  va_list args;
1621 
1622  Assert(narg <= lengthof(argarray));
1623 
1624  if (previous_words_count < narg)
1625  return false;
1626 
1627  va_start(args, narg);
1628  for (int argno = 0; argno < narg; argno++)
1629  argarray[argno] = va_arg(args, const char *);
1630  va_end(args);
1631 
1632  return TailMatchesArray(case_sensitive,
1633  previous_words_count, previous_words,
1634  narg, argarray);
1635 }
1636 
1637 /*
1638  * Implementation of HeadMatches and HeadMatchesCS tests: do the first N
1639  * words in previous_words match the pattern arguments?
1640  */
1641 static bool
1642 HeadMatchesArray(bool case_sensitive,
1643  int previous_words_count, char **previous_words,
1644  int narg, const char *const *args)
1645 {
1646  if (previous_words_count < narg)
1647  return false;
1648 
1649  for (int argno = 0; argno < narg; argno++)
1650  {
1651  const char *arg = args[argno];
1652 
1653  if (!word_matches(arg, previous_words[previous_words_count - argno - 1],
1654  case_sensitive))
1655  return false;
1656  }
1657 
1658  return true;
1659 }
1660 
1661 /*
1662  * As above, but the pattern is passed as a variadic argument list.
1663  */
1664 static bool
1665 HeadMatchesImpl(bool case_sensitive,
1666  int previous_words_count, char **previous_words,
1667  int narg,...)
1668 {
1669  const char *argarray[64];
1670  va_list args;
1671 
1672  Assert(narg <= lengthof(argarray));
1673 
1674  if (previous_words_count < narg)
1675  return false;
1676 
1677  va_start(args, narg);
1678  for (int argno = 0; argno < narg; argno++)
1679  argarray[argno] = va_arg(args, const char *);
1680  va_end(args);
1681 
1682  return HeadMatchesArray(case_sensitive,
1683  previous_words_count, previous_words,
1684  narg, argarray);
1685 }
1686 
1687 /*
1688  * Implementation of Matches and MatchesCS tests: do all of the words
1689  * in previous_words match the pattern arguments?
1690  *
1691  * This supports an additional kind of wildcard: MatchAnyN (represented as "")
1692  * can match any number of words, including zero, in the middle of the list.
1693  */
1694 static bool
1695 MatchesArray(bool case_sensitive,
1696  int previous_words_count, char **previous_words,
1697  int narg, const char *const *args)
1698 {
1699  int match_any_pos = -1;
1700 
1701  /* Even with MatchAnyN, there must be at least N-1 words */
1702  if (previous_words_count < narg - 1)
1703  return false;
1704 
1705  /* Check for MatchAnyN */
1706  for (int argno = 0; argno < narg; argno++)
1707  {
1708  const char *arg = args[argno];
1709 
1710  if (arg != NULL && arg[0] == '\0')
1711  {
1712  match_any_pos = argno;
1713  break;
1714  }
1715  }
1716 
1717  if (match_any_pos < 0)
1718  {
1719  /* Standard case without MatchAnyN */
1720  if (previous_words_count != narg)
1721  return false;
1722 
1723  /* Either Head or Tail match will do for the rest */
1724  if (!HeadMatchesArray(case_sensitive,
1725  previous_words_count, previous_words,
1726  narg, args))
1727  return false;
1728  }
1729  else
1730  {
1731  /* Match against head */
1732  if (!HeadMatchesArray(case_sensitive,
1733  previous_words_count, previous_words,
1734  match_any_pos, args))
1735  return false;
1736 
1737  /* Match against tail */
1738  if (!TailMatchesArray(case_sensitive,
1739  previous_words_count, previous_words,
1740  narg - match_any_pos - 1,
1741  args + match_any_pos + 1))
1742  return false;
1743  }
1744 
1745  return true;
1746 }
1747 
1748 /*
1749  * As above, but the pattern is passed as a variadic argument list.
1750  */
1751 static bool
1752 MatchesImpl(bool case_sensitive,
1753  int previous_words_count, char **previous_words,
1754  int narg,...)
1755 {
1756  const char *argarray[64];
1757  va_list args;
1758 
1759  Assert(narg <= lengthof(argarray));
1760 
1761  /* Even with MatchAnyN, there must be at least N-1 words */
1762  if (previous_words_count < narg - 1)
1763  return false;
1764 
1765  va_start(args, narg);
1766  for (int argno = 0; argno < narg; argno++)
1767  argarray[argno] = va_arg(args, const char *);
1768  va_end(args);
1769 
1770  return MatchesArray(case_sensitive,
1771  previous_words_count, previous_words,
1772  narg, argarray);
1773 }
1774 
1775 /*
1776  * Check if the final character of 's' is 'c'.
1777  */
1778 static bool
1779 ends_with(const char *s, char c)
1780 {
1781  size_t length = strlen(s);
1782 
1783  return (length > 0 && s[length - 1] == c);
1784 }
1785 
1786 /*
1787  * The completion function.
1788  *
1789  * According to readline spec this gets passed the text entered so far and its
1790  * start and end positions in the readline buffer. The return value is some
1791  * partially obscure list format that can be generated by readline's
1792  * rl_completion_matches() function, so we don't have to worry about it.
1793  */
1794 static char **
1795 psql_completion(const char *text, int start, int end)
1796 {
1797  /* This is the variable we'll return. */
1798  char **matches = NULL;
1799 
1800  /* Workspace for parsed words. */
1801  char *words_buffer;
1802 
1803  /* This array will contain pointers to parsed words. */
1804  char **previous_words;
1805 
1806  /* The number of words found on the input line. */
1807  int previous_words_count;
1808 
1809  /*
1810  * For compactness, we use these macros to reference previous_words[].
1811  * Caution: do not access a previous_words[] entry without having checked
1812  * previous_words_count to be sure it's valid. In most cases below, that
1813  * check is implicit in a TailMatches() or similar macro, but in some
1814  * places we have to check it explicitly.
1815  */
1816 #define prev_wd (previous_words[0])
1817 #define prev2_wd (previous_words[1])
1818 #define prev3_wd (previous_words[2])
1819 #define prev4_wd (previous_words[3])
1820 #define prev5_wd (previous_words[4])
1821 #define prev6_wd (previous_words[5])
1822 #define prev7_wd (previous_words[6])
1823 #define prev8_wd (previous_words[7])
1824 #define prev9_wd (previous_words[8])
1825 
1826  /* Match the last N words before point, case-insensitively. */
1827 #define TailMatches(...) \
1828  TailMatchesImpl(false, previous_words_count, previous_words, \
1829  VA_ARGS_NARGS(__VA_ARGS__), __VA_ARGS__)
1830 
1831  /* Match the last N words before point, case-sensitively. */
1832 #define TailMatchesCS(...) \
1833  TailMatchesImpl(true, previous_words_count, previous_words, \
1834  VA_ARGS_NARGS(__VA_ARGS__), __VA_ARGS__)
1835 
1836  /* Match N words representing all of the line, case-insensitively. */
1837 #define Matches(...) \
1838  MatchesImpl(false, previous_words_count, previous_words, \
1839  VA_ARGS_NARGS(__VA_ARGS__), __VA_ARGS__)
1840 
1841  /* Match N words representing all of the line, case-sensitively. */
1842 #define MatchesCS(...) \
1843  MatchesImpl(true, previous_words_count, previous_words, \
1844  VA_ARGS_NARGS(__VA_ARGS__), __VA_ARGS__)
1845 
1846  /* Match the first N words on the line, case-insensitively. */
1847 #define HeadMatches(...) \
1848  HeadMatchesImpl(false, previous_words_count, previous_words, \
1849  VA_ARGS_NARGS(__VA_ARGS__), __VA_ARGS__)
1850 
1851  /* Match the first N words on the line, case-sensitively. */
1852 #define HeadMatchesCS(...) \
1853  HeadMatchesImpl(true, previous_words_count, previous_words, \
1854  VA_ARGS_NARGS(__VA_ARGS__), __VA_ARGS__)
1855 
1856  /* psql's backslash commands. */
1857  static const char *const backslash_commands[] = {
1858  "\\a",
1859  "\\bind", "\\bind_named",
1860  "\\connect", "\\conninfo", "\\C", "\\cd", "\\close", "\\copy",
1861  "\\copyright", "\\crosstabview",
1862  "\\d", "\\da", "\\dA", "\\dAc", "\\dAf", "\\dAo", "\\dAp",
1863  "\\db", "\\dc", "\\dconfig", "\\dC", "\\dd", "\\ddp", "\\dD",
1864  "\\des", "\\det", "\\deu", "\\dew", "\\dE", "\\df",
1865  "\\dF", "\\dFd", "\\dFp", "\\dFt", "\\dg", "\\di", "\\dl", "\\dL",
1866  "\\dm", "\\dn", "\\do", "\\dO", "\\dp", "\\dP", "\\dPi", "\\dPt",
1867  "\\drds", "\\drg", "\\dRs", "\\dRp", "\\ds",
1868  "\\dt", "\\dT", "\\dv", "\\du", "\\dx", "\\dX", "\\dy",
1869  "\\echo", "\\edit", "\\ef", "\\elif", "\\else", "\\encoding",
1870  "\\endif", "\\errverbose", "\\ev",
1871  "\\f",
1872  "\\g", "\\gdesc", "\\getenv", "\\gexec", "\\gset", "\\gx",
1873  "\\help", "\\html",
1874  "\\if", "\\include", "\\include_relative", "\\ir",
1875  "\\list", "\\lo_import", "\\lo_export", "\\lo_list", "\\lo_unlink",
1876  "\\out",
1877  "\\parse", "\\password", "\\print", "\\prompt", "\\pset",
1878  "\\qecho", "\\quit",
1879  "\\reset",
1880  "\\s", "\\set", "\\setenv", "\\sf", "\\sv",
1881  "\\t", "\\T", "\\timing",
1882  "\\unset",
1883  "\\x",
1884  "\\warn", "\\watch", "\\write",
1885  "\\z",
1886  "\\!", "\\?",
1887  NULL
1888  };
1889 
1890  /*
1891  * Temporary workaround for a bug in recent (2019) libedit: it incorrectly
1892  * de-escapes the input "text", causing us to fail to recognize backslash
1893  * commands. So get the string to look at from rl_line_buffer instead.
1894  */
1895  char *text_copy = pnstrdup(rl_line_buffer + start, end - start);
1896  text = text_copy;
1897 
1898  /* Remember last char of the given input word. */
1899  completion_last_char = (end > start) ? text[end - start - 1] : '\0';
1900 
1901  /* We usually want the append character to be a space. */
1902  rl_completion_append_character = ' ';
1903 
1904  /* Clear a few things. */
1905  completion_charp = NULL;
1906  completion_charpp = NULL;
1907  completion_vquery = NULL;
1908  completion_squery = NULL;
1909  completion_ref_object = NULL;
1910  completion_ref_schema = NULL;
1911 
1912  /*
1913  * Scan the input line to extract the words before our current position.
1914  * According to those we'll make some smart decisions on what the user is
1915  * probably intending to type.
1916  */
1917  previous_words = get_previous_words(start,
1918  &words_buffer,
1919  &previous_words_count);
1920 
1921  /* If current word is a backslash command, offer completions for that */
1922  if (text[0] == '\\')
1923  COMPLETE_WITH_LIST_CS(backslash_commands);
1924 
1925  /* If current word is a variable interpolation, handle that case */
1926  else if (text[0] == ':' && text[1] != ':')
1927  {
1928  if (text[1] == '\'')
1929  matches = complete_from_variables(text, ":'", "'", true);
1930  else if (text[1] == '"')
1931  matches = complete_from_variables(text, ":\"", "\"", true);
1932  else if (text[1] == '{' && text[2] == '?')
1933  matches = complete_from_variables(text, ":{?", "}", true);
1934  else
1935  matches = complete_from_variables(text, ":", "", true);
1936  }
1937 
1938  /* If no previous word, suggest one of the basic sql commands */
1939  else if (previous_words_count == 0)
1940  COMPLETE_WITH_LIST(sql_commands);
1941 
1942  /* Else try completions based on matching patterns of previous words */
1943  else
1944  {
1945 #ifdef SWITCH_CONVERSION_APPLIED
1946  /*
1947  * If we have transformed match_previous_words into a switch, iterate
1948  * through tcpatterns[] to see which pattern ids match.
1949  *
1950  * For now, we have to try the patterns in the order they are stored
1951  * (matching the order of switch cases in match_previous_words),
1952  * because some of the logic in match_previous_words assumes that
1953  * previous matches have been eliminated. This is fairly
1954  * unprincipled, and it is likely that there are undesirable as well
1955  * as desirable interactions hidden in the order of the pattern
1956  * checks. TODO: think about a better way to manage that.
1957  */
1958  for (int tindx = 0; tindx < lengthof(tcpatterns); tindx++)
1959  {
1960  const TCPattern *tcpat = tcpatterns + tindx;
1961  bool match = false;
1962 
1963  switch (tcpat->kind)
1964  {
1965  case Match:
1966  match = MatchesArray(false,
1967  previous_words_count,
1968  previous_words,
1969  tcpat->nwords, tcpat->words);
1970  break;
1971  case MatchCS:
1972  match = MatchesArray(true,
1973  previous_words_count,
1974  previous_words,
1975  tcpat->nwords, tcpat->words);
1976  break;
1977  case HeadMatch:
1978  match = HeadMatchesArray(false,
1979  previous_words_count,
1980  previous_words,
1981  tcpat->nwords, tcpat->words);
1982  break;
1983  case HeadMatchCS:
1984  match = HeadMatchesArray(true,
1985  previous_words_count,
1986  previous_words,
1987  tcpat->nwords, tcpat->words);
1988  break;
1989  case TailMatch:
1990  match = TailMatchesArray(false,
1991  previous_words_count,
1992  previous_words,
1993  tcpat->nwords, tcpat->words);
1994  break;
1995  case TailMatchCS:
1996  match = TailMatchesArray(true,
1997  previous_words_count,
1998  previous_words,
1999  tcpat->nwords, tcpat->words);
2000  break;
2001  }
2002  if (match)
2003  {
2004  matches = match_previous_words(tcpat->id, text, start, end,
2005  previous_words,
2006  previous_words_count);
2007  if (matches != NULL)
2008  break;
2009  }
2010  }
2011 #else /* !SWITCH_CONVERSION_APPLIED */
2012  /*
2013  * If gen_tabcomplete.pl hasn't been applied to this code, just let
2014  * match_previous_words scan through all its patterns.
2015  */
2016  matches = match_previous_words(0, text, start, end,
2017  previous_words,
2018  previous_words_count);
2019 #endif /* SWITCH_CONVERSION_APPLIED */
2020  }
2021 
2022  /*
2023  * Finally, we look through the list of "things", such as TABLE, INDEX and
2024  * check if that was the previous word. If so, execute the query to get a
2025  * list of them.
2026  */
2027  if (matches == NULL && previous_words_count > 0)
2028  {
2029  const pgsql_thing_t *wac;
2030 
2031  for (wac = words_after_create; wac->name != NULL; wac++)
2032  {
2033  if (pg_strcasecmp(prev_wd, wac->name) == 0)
2034  {
2035  if (wac->query)
2036  COMPLETE_WITH_QUERY_LIST(wac->query,
2037  wac->keywords);
2038  else if (wac->vquery)
2039  COMPLETE_WITH_VERSIONED_QUERY_LIST(wac->vquery,
2040  wac->keywords);
2041  else if (wac->squery)
2042  COMPLETE_WITH_VERSIONED_SCHEMA_QUERY_LIST(wac->squery,
2043  wac->keywords);
2044  break;
2045  }
2046  }
2047  }
2048 
2049  /*
2050  * If we still don't have anything to match we have to fabricate some sort
2051  * of default list. If we were to just return NULL, readline automatically
2052  * attempts filename completion, and that's usually no good.
2053  */
2054  if (matches == NULL)
2055  {
2056  COMPLETE_WITH_CONST(true, "");
2057  /* Also, prevent Readline from appending stuff to the non-match */
2058  rl_completion_append_character = '\0';
2059 #ifdef HAVE_RL_COMPLETION_SUPPRESS_QUOTE
2060  rl_completion_suppress_quote = 1;
2061 #endif
2062  }
2063 
2064  /* free storage */
2065  free(previous_words);
2066  free(words_buffer);
2067  free(text_copy);
2068  free(completion_ref_object);
2069  completion_ref_object = NULL;
2070  free(completion_ref_schema);
2071  completion_ref_schema = NULL;
2072 
2073  /* Return our Grand List O' Matches */
2074  return matches;
2075 }
2076 
2077 /*
2078  * Subroutine to try matches based on previous_words.
2079  *
2080  * This can operate in one of two modes. As presented, the body of the
2081  * function is a long if-else-if chain that sequentially tries each known
2082  * match rule. That works, but some C compilers have trouble with such a long
2083  * else-if chain, either taking extra time to compile or failing altogether.
2084  * Therefore, we prefer to transform the else-if chain into a switch, and then
2085  * each call of this function considers just one match rule (under control of
2086  * a loop in psql_completion()). Compilers tend to be more ready to deal
2087  * with many-arm switches than many-arm else-if chains.
2088  *
2089  * Each if-condition in this function must begin with a call of one of the
2090  * functions Matches, HeadMatches, TailMatches, MatchesCS, HeadMatchesCS, or
2091  * TailMatchesCS. The preprocessor gen_tabcomplete.pl strips out those
2092  * calls and converts them into entries in tcpatterns[], which are evaluated
2093  * by the calling loop in psql_completion(). Successful matches result in
2094  * calls to this function with the appropriate pattern_id, causing just the
2095  * corresponding switch case to be executed.
2096  *
2097  * If-conditions in this function can be more complex than a single *Matches
2098  * function call in one of two ways (but not both!). They can be OR's
2099  * of *Matches calls, such as
2100  * else if (Matches("ALTER", "VIEW", MatchAny, "ALTER", MatchAny) ||
2101  * Matches("ALTER", "VIEW", MatchAny, "ALTER", "COLUMN", MatchAny))
2102  * or they can be a *Matches call AND'ed with some other condition, e.g.
2103  * else if (Matches("CREATE", "PUBLICATION", MatchAny, "FOR", "TABLE", MatchAny) &&
2104  * !ends_with(prev_wd, ','))
2105  * The former case is transformed into multiple tcpatterns[] entries and
2106  * multiple case labels for the same bit of code. The latter case is
2107  * transformed into a case label and a contained if-statement.
2108  *
2109  * This is split out of psql_completion() primarily to separate code that
2110  * gen_tabcomplete.pl should process from code that it should not, although
2111  * doing so also helps to avoid extra indentation of this code.
2112  *
2113  * Returns a matches list, or NULL if no match.
2114  */
2115 static char **
2116 match_previous_words(int pattern_id,
2117  const char *text, int start, int end,
2118  char **previous_words, int previous_words_count)
2119 {
2120  /* This is the variable we'll return. */
2121  char **matches = NULL;
2122 
2123  /* Dummy statement, allowing all the match rules to look like "else if" */
2124  if (0)
2125  {
2126  /* skip */
2127  }
2128 
2129  /* gen_tabcomplete.pl begins special processing here */
2130  /* BEGIN GEN_TABCOMPLETE */
2131 
2132 /* CREATE */
2133  /* complete with something you can create */
2134  else if (TailMatches("CREATE"))
2135  {
2136  /* only some object types can be created as part of CREATE SCHEMA */
2137  if (HeadMatches("CREATE", "SCHEMA"))
2138  COMPLETE_WITH("TABLE", "VIEW", "INDEX", "SEQUENCE", "TRIGGER",
2139  /* for INDEX and TABLE/SEQUENCE, respectively */
2140  "UNIQUE", "UNLOGGED");
2141  else
2142  matches = rl_completion_matches(text, create_command_generator);
2143  }
2144  /* complete with something you can create or replace */
2145  else if (TailMatches("CREATE", "OR", "REPLACE"))
2146  COMPLETE_WITH("FUNCTION", "PROCEDURE", "LANGUAGE", "RULE", "VIEW",
2147  "AGGREGATE", "TRANSFORM", "TRIGGER");
2148 
2149 /* DROP, but not DROP embedded in other commands */
2150  /* complete with something you can drop */
2151  else if (Matches("DROP"))
2152  matches = rl_completion_matches(text, drop_command_generator);
2153 
2154 /* ALTER */
2155 
2156  /* ALTER TABLE */
2157  else if (Matches("ALTER", "TABLE"))
2158  COMPLETE_WITH_SCHEMA_QUERY_PLUS(Query_for_list_of_tables,
2159  "ALL IN TABLESPACE");
2160 
2161  /* ALTER something */
2162  else if (Matches("ALTER"))
2163  matches = rl_completion_matches(text, alter_command_generator);
2164  /* ALTER TABLE,INDEX,MATERIALIZED VIEW ALL IN TABLESPACE xxx */
2165  else if (TailMatches("ALL", "IN", "TABLESPACE", MatchAny))
2166  COMPLETE_WITH("SET TABLESPACE", "OWNED BY");
2167  /* ALTER TABLE,INDEX,MATERIALIZED VIEW ALL IN TABLESPACE xxx OWNED BY */
2168  else if (TailMatches("ALL", "IN", "TABLESPACE", MatchAny, "OWNED", "BY"))
2169  COMPLETE_WITH_QUERY(Query_for_list_of_roles);
2170  /* ALTER TABLE,INDEX,MATERIALIZED VIEW ALL IN TABLESPACE xxx OWNED BY xxx */
2171  else if (TailMatches("ALL", "IN", "TABLESPACE", MatchAny, "OWNED", "BY", MatchAny))
2172  COMPLETE_WITH("SET TABLESPACE");
2173  /* ALTER AGGREGATE,FUNCTION,PROCEDURE,ROUTINE <name> */
2174  else if (Matches("ALTER", "AGGREGATE|FUNCTION|PROCEDURE|ROUTINE", MatchAny))
2175  COMPLETE_WITH("(");
2176  /* ALTER AGGREGATE <name> (...) */
2177  else if (Matches("ALTER", "AGGREGATE", MatchAny, MatchAny))
2178  {
2179  if (ends_with(prev_wd, ')'))
2180  COMPLETE_WITH("OWNER TO", "RENAME TO", "SET SCHEMA");
2181  else
2182  COMPLETE_WITH_FUNCTION_ARG(prev2_wd);
2183  }
2184  /* ALTER FUNCTION <name> (...) */
2185  else if (Matches("ALTER", "FUNCTION", MatchAny, MatchAny))
2186  {
2187  if (ends_with(prev_wd, ')'))
2188  COMPLETE_WITH(Alter_function_options);
2189  else
2190  COMPLETE_WITH_FUNCTION_ARG(prev2_wd);
2191  }
2192  /* ALTER PROCEDURE <name> (...) */
2193  else if (Matches("ALTER", "PROCEDURE", MatchAny, MatchAny))
2194  {
2195  if (ends_with(prev_wd, ')'))
2196  COMPLETE_WITH(Alter_procedure_options);
2197  else
2198  COMPLETE_WITH_FUNCTION_ARG(prev2_wd);
2199  }
2200  /* ALTER ROUTINE <name> (...) */
2201  else if (Matches("ALTER", "ROUTINE", MatchAny, MatchAny))
2202  {
2203  if (ends_with(prev_wd, ')'))
2204  COMPLETE_WITH(Alter_routine_options);
2205  else
2206  COMPLETE_WITH_FUNCTION_ARG(prev2_wd);
2207  }
2208  /* ALTER FUNCTION|ROUTINE <name> (...) PARALLEL */
2209  else if (Matches("ALTER", "FUNCTION|ROUTINE", MatchAny, MatchAny, "PARALLEL"))
2210  COMPLETE_WITH("RESTRICTED", "SAFE", "UNSAFE");
2211  /* ALTER FUNCTION|PROCEDURE|ROUTINE <name> (...) [EXTERNAL] SECURITY */
2212  else if (Matches("ALTER", "FUNCTION|PROCEDURE|ROUTINE", MatchAny, MatchAny, "SECURITY") ||
2213  Matches("ALTER", "FUNCTION|PROCEDURE|ROUTINE", MatchAny, MatchAny, "EXTERNAL", "SECURITY"))
2214  COMPLETE_WITH("DEFINER", "INVOKER");
2215  /* ALTER FUNCTION|PROCEDURE|ROUTINE <name> (...) RESET */
2216  else if (Matches("ALTER", "FUNCTION|PROCEDURE|ROUTINE", MatchAny, MatchAny, "RESET"))
2217  COMPLETE_WITH_QUERY_VERBATIM_PLUS(Query_for_list_of_set_vars,
2218  "ALL");
2219  /* ALTER FUNCTION|PROCEDURE|ROUTINE <name> (...) SET */
2220  else if (Matches("ALTER", "FUNCTION|PROCEDURE|ROUTINE", MatchAny, MatchAny, "SET"))
2221  COMPLETE_WITH_QUERY_VERBATIM_PLUS(Query_for_list_of_set_vars,
2222  "SCHEMA");
2223 
2224  /* ALTER PUBLICATION <name> */
2225  else if (Matches("ALTER", "PUBLICATION", MatchAny))
2226  COMPLETE_WITH("ADD", "DROP", "OWNER TO", "RENAME TO", "SET");
2227  /* ALTER PUBLICATION <name> ADD */
2228  else if (Matches("ALTER", "PUBLICATION", MatchAny, "ADD"))
2229  COMPLETE_WITH("TABLES IN SCHEMA", "TABLE");
2230  else if (Matches("ALTER", "PUBLICATION", MatchAny, "ADD|SET", "TABLE"))
2231  COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tables);
2232  else if (HeadMatches("ALTER", "PUBLICATION", MatchAny, "ADD|SET", "TABLE") &&
2233  ends_with(prev_wd, ','))
2234  COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tables);
2235 
2236  /*
2237  * "ALTER PUBLICATION <name> SET TABLE <name> WHERE (" - complete with
2238  * table attributes
2239  *
2240  * "ALTER PUBLICATION <name> ADD TABLE <name> WHERE (" - complete with
2241  * table attributes
2242  */
2243  else if (Matches("ALTER", "PUBLICATION", MatchAny, MatchAnyN, "WHERE"))
2244  COMPLETE_WITH("(");
2245  else if (Matches("ALTER", "PUBLICATION", MatchAny, MatchAnyN, "WHERE", "("))
2246  COMPLETE_WITH_ATTR(prev3_wd);
2247  else if (HeadMatches("ALTER", "PUBLICATION", MatchAny, "ADD|SET", "TABLE") &&
2248  !TailMatches("WHERE", "(*)"))
2249  COMPLETE_WITH(",", "WHERE (");
2250  else if (HeadMatches("ALTER", "PUBLICATION", MatchAny, "ADD|SET", "TABLE"))
2251  COMPLETE_WITH(",");
2252  /* ALTER PUBLICATION <name> DROP */
2253  else if (Matches("ALTER", "PUBLICATION", MatchAny, "DROP"))
2254  COMPLETE_WITH("TABLES IN SCHEMA", "TABLE");
2255  /* ALTER PUBLICATION <name> SET */
2256  else if (Matches("ALTER", "PUBLICATION", MatchAny, "SET"))
2257  COMPLETE_WITH("(", "TABLES IN SCHEMA", "TABLE");
2258  else if (Matches("ALTER", "PUBLICATION", MatchAny, "ADD|DROP|SET", "TABLES", "IN", "SCHEMA"))
2259  COMPLETE_WITH_QUERY_PLUS(Query_for_list_of_schemas
2260  " AND nspname NOT LIKE E'pg\\\\_%%'",
2261  "CURRENT_SCHEMA");
2262  /* ALTER PUBLICATION <name> SET ( */
2263  else if (Matches("ALTER", "PUBLICATION", MatchAny, MatchAnyN, "SET", "("))
2264  COMPLETE_WITH("publish", "publish_generated_columns", "publish_via_partition_root");
2265  /* ALTER SUBSCRIPTION <name> */
2266  else if (Matches("ALTER", "SUBSCRIPTION", MatchAny))
2267  COMPLETE_WITH("CONNECTION", "ENABLE", "DISABLE", "OWNER TO",
2268  "RENAME TO", "REFRESH PUBLICATION", "SET", "SKIP (",
2269  "ADD PUBLICATION", "DROP PUBLICATION");
2270  /* ALTER SUBSCRIPTION <name> REFRESH PUBLICATION */
2271  else if (Matches("ALTER", "SUBSCRIPTION", MatchAny, MatchAnyN, "REFRESH", "PUBLICATION"))
2272  COMPLETE_WITH("WITH (");
2273  /* ALTER SUBSCRIPTION <name> REFRESH PUBLICATION WITH ( */
2274  else if (Matches("ALTER", "SUBSCRIPTION", MatchAny, MatchAnyN, "REFRESH", "PUBLICATION", "WITH", "("))
2275  COMPLETE_WITH("copy_data");
2276  /* ALTER SUBSCRIPTION <name> SET */
2277  else if (Matches("ALTER", "SUBSCRIPTION", MatchAny, "SET"))
2278  COMPLETE_WITH("(", "PUBLICATION");
2279  /* ALTER SUBSCRIPTION <name> SET ( */
2280  else if (Matches("ALTER", "SUBSCRIPTION", MatchAny, MatchAnyN, "SET", "("))
2281  COMPLETE_WITH("binary", "disable_on_error", "failover", "origin",
2282  "password_required", "run_as_owner", "slot_name",
2283  "streaming", "synchronous_commit", "two_phase");
2284  /* ALTER SUBSCRIPTION <name> SKIP ( */
2285  else if (Matches("ALTER", "SUBSCRIPTION", MatchAny, MatchAnyN, "SKIP", "("))
2286  COMPLETE_WITH("lsn");
2287  /* ALTER SUBSCRIPTION <name> SET PUBLICATION */
2288  else if (Matches("ALTER", "SUBSCRIPTION", MatchAny, MatchAnyN, "SET", "PUBLICATION"))
2289  {
2290  /* complete with nothing here as this refers to remote publications */
2291  }
2292  /* ALTER SUBSCRIPTION <name> ADD|DROP|SET PUBLICATION <name> */
2293  else if (Matches("ALTER", "SUBSCRIPTION", MatchAny, MatchAnyN,
2294  "ADD|DROP|SET", "PUBLICATION", MatchAny))
2295  COMPLETE_WITH("WITH (");
2296  /* ALTER SUBSCRIPTION <name> ADD|DROP|SET PUBLICATION <name> WITH ( */
2297  else if (Matches("ALTER", "SUBSCRIPTION", MatchAny, MatchAnyN,
2298  "ADD|DROP|SET", "PUBLICATION", MatchAny, "WITH", "("))
2299  COMPLETE_WITH("copy_data", "refresh");
2300 
2301  /* ALTER SCHEMA <name> */
2302  else if (Matches("ALTER", "SCHEMA", MatchAny))
2303  COMPLETE_WITH("OWNER TO", "RENAME TO");
2304 
2305  /* ALTER COLLATION <name> */
2306  else if (Matches("ALTER", "COLLATION", MatchAny))
2307  COMPLETE_WITH("OWNER TO", "REFRESH VERSION", "RENAME TO", "SET SCHEMA");
2308 
2309  /* ALTER CONVERSION <name> */
2310  else if (Matches("ALTER", "CONVERSION", MatchAny))
2311  COMPLETE_WITH("OWNER TO", "RENAME TO", "SET SCHEMA");
2312 
2313  /* ALTER DATABASE <name> */
2314  else if (Matches("ALTER", "DATABASE", MatchAny))
2315  COMPLETE_WITH("RESET", "SET", "OWNER TO", "REFRESH COLLATION VERSION", "RENAME TO",
2316  "IS_TEMPLATE", "ALLOW_CONNECTIONS",
2317  "CONNECTION LIMIT");
2318 
2319  /* ALTER DATABASE <name> SET TABLESPACE */
2320  else if (Matches("ALTER", "DATABASE", MatchAny, "SET", "TABLESPACE"))
2321  COMPLETE_WITH_QUERY(Query_for_list_of_tablespaces);
2322 
2323  /* ALTER EVENT TRIGGER */
2324  else if (Matches("ALTER", "EVENT", "TRIGGER"))
2325  COMPLETE_WITH_QUERY(Query_for_list_of_event_triggers);
2326 
2327  /* ALTER EVENT TRIGGER <name> */
2328  else if (Matches("ALTER", "EVENT", "TRIGGER", MatchAny))
2329  COMPLETE_WITH("DISABLE", "ENABLE", "OWNER TO", "RENAME TO");
2330 
2331  /* ALTER EVENT TRIGGER <name> ENABLE */
2332  else if (Matches("ALTER", "EVENT", "TRIGGER", MatchAny, "ENABLE"))
2333  COMPLETE_WITH("REPLICA", "ALWAYS");
2334 
2335  /* ALTER EXTENSION <name> */
2336  else if (Matches("ALTER", "EXTENSION", MatchAny))
2337  COMPLETE_WITH("ADD", "DROP", "UPDATE", "SET SCHEMA");
2338 
2339  /* ALTER EXTENSION <name> ADD|DROP */
2340  else if (Matches("ALTER", "EXTENSION", MatchAny, "ADD|DROP"))
2341  COMPLETE_WITH("ACCESS METHOD", "AGGREGATE", "CAST", "COLLATION",
2342  "CONVERSION", "DOMAIN", "EVENT TRIGGER", "FOREIGN",
2343  "FUNCTION", "MATERIALIZED VIEW", "OPERATOR",
2344  "LANGUAGE", "PROCEDURE", "ROUTINE", "SCHEMA",
2345  "SEQUENCE", "SERVER", "TABLE", "TEXT SEARCH",
2346  "TRANSFORM FOR", "TYPE", "VIEW");
2347 
2348  /* ALTER EXTENSION <name> ADD|DROP FOREIGN */
2349  else if (Matches("ALTER", "EXTENSION", MatchAny, "ADD|DROP", "FOREIGN"))
2350  COMPLETE_WITH("DATA WRAPPER", "TABLE");
2351 
2352  /* ALTER EXTENSION <name> ADD|DROP OPERATOR */
2353  else if (Matches("ALTER", "EXTENSION", MatchAny, "ADD|DROP", "OPERATOR"))
2354  COMPLETE_WITH("CLASS", "FAMILY");
2355 
2356  /* ALTER EXTENSION <name> ADD|DROP TEXT SEARCH */
2357  else if (Matches("ALTER", "EXTENSION", MatchAny, "ADD|DROP", "TEXT", "SEARCH"))
2358  COMPLETE_WITH("CONFIGURATION", "DICTIONARY", "PARSER", "TEMPLATE");
2359 
2360  /* ALTER EXTENSION <name> UPDATE */
2361  else if (Matches("ALTER", "EXTENSION", MatchAny, "UPDATE"))
2362  COMPLETE_WITH("TO");
2363 
2364  /* ALTER EXTENSION <name> UPDATE TO */
2365  else if (Matches("ALTER", "EXTENSION", MatchAny, "UPDATE", "TO"))
2366  {
2367  set_completion_reference(prev3_wd);
2368  COMPLETE_WITH_QUERY(Query_for_list_of_available_extension_versions);
2369  }
2370 
2371  /* ALTER FOREIGN */
2372  else if (Matches("ALTER", "FOREIGN"))
2373  COMPLETE_WITH("DATA WRAPPER", "TABLE");
2374 
2375  /* ALTER FOREIGN DATA WRAPPER <name> */
2376  else if (Matches("ALTER", "FOREIGN", "DATA", "WRAPPER", MatchAny))
2377  COMPLETE_WITH("HANDLER", "VALIDATOR", "NO",
2378  "OPTIONS", "OWNER TO", "RENAME TO");
2379  else if (Matches("ALTER", "FOREIGN", "DATA", "WRAPPER", MatchAny, "NO"))
2380  COMPLETE_WITH("HANDLER", "VALIDATOR");
2381 
2382  /* ALTER FOREIGN TABLE <name> */
2383  else if (Matches("ALTER", "FOREIGN", "TABLE", MatchAny))
2384  COMPLETE_WITH("ADD", "ALTER", "DISABLE TRIGGER", "DROP", "ENABLE",
2385  "INHERIT", "NO INHERIT", "OPTIONS", "OWNER TO",
2386  "RENAME", "SET", "VALIDATE CONSTRAINT");
2387 
2388  /* ALTER INDEX */
2389  else if (Matches("ALTER", "INDEX"))
2390  COMPLETE_WITH_SCHEMA_QUERY_PLUS(Query_for_list_of_indexes,
2391  "ALL IN TABLESPACE");
2392  /* ALTER INDEX <name> */
2393  else if (Matches("ALTER", "INDEX", MatchAny))
2394  COMPLETE_WITH("ALTER COLUMN", "OWNER TO", "RENAME TO", "SET",
2395  "RESET", "ATTACH PARTITION",
2396  "DEPENDS ON EXTENSION", "NO DEPENDS ON EXTENSION");
2397  else if (Matches("ALTER", "INDEX", MatchAny, "ATTACH"))
2398  COMPLETE_WITH("PARTITION");
2399  else if (Matches("ALTER", "INDEX", MatchAny, "ATTACH", "PARTITION"))
2400  COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_indexes);
2401  /* ALTER INDEX <name> ALTER */
2402  else if (Matches("ALTER", "INDEX", MatchAny, "ALTER"))
2403  COMPLETE_WITH("COLUMN");
2404  /* ALTER INDEX <name> ALTER COLUMN */
2405  else if (Matches("ALTER", "INDEX", MatchAny, "ALTER", "COLUMN"))
2406  {
2407  set_completion_reference(prev3_wd);
2408  COMPLETE_WITH_SCHEMA_QUERY_VERBATIM(Query_for_list_of_attribute_numbers);
2409  }
2410  /* ALTER INDEX <name> ALTER COLUMN <colnum> */
2411  else if (Matches("ALTER", "INDEX", MatchAny, "ALTER", "COLUMN", MatchAny))
2412  COMPLETE_WITH("SET STATISTICS");
2413  /* ALTER INDEX <name> ALTER COLUMN <colnum> SET */
2414  else if (Matches("ALTER", "INDEX", MatchAny, "ALTER", "COLUMN", MatchAny, "SET"))
2415  COMPLETE_WITH("STATISTICS");
2416  /* ALTER INDEX <name> ALTER COLUMN <colnum> SET STATISTICS */
2417  else if (Matches("ALTER", "INDEX", MatchAny, "ALTER", "COLUMN", MatchAny, "SET", "STATISTICS"))
2418  {
2419  /* Enforce no completion here, as an integer has to be specified */
2420  }
2421  /* ALTER INDEX <name> SET */
2422  else if (Matches("ALTER", "INDEX", MatchAny, "SET"))
2423  COMPLETE_WITH("(", "TABLESPACE");
2424  /* ALTER INDEX <name> RESET */
2425  else if (Matches("ALTER", "INDEX", MatchAny, "RESET"))
2426  COMPLETE_WITH("(");
2427  /* ALTER INDEX <foo> SET|RESET ( */
2428  else if (Matches("ALTER", "INDEX", MatchAny, "RESET", "("))
2429  COMPLETE_WITH("fillfactor",
2430  "deduplicate_items", /* BTREE */
2431  "fastupdate", "gin_pending_list_limit", /* GIN */
2432  "buffering", /* GiST */
2433  "pages_per_range", "autosummarize" /* BRIN */
2434  );
2435  else if (Matches("ALTER", "INDEX", MatchAny, "SET", "("))
2436  COMPLETE_WITH("fillfactor =",
2437  "deduplicate_items =", /* BTREE */
2438  "fastupdate =", "gin_pending_list_limit =", /* GIN */
2439  "buffering =", /* GiST */
2440  "pages_per_range =", "autosummarize =" /* BRIN */
2441  );
2442  else if (Matches("ALTER", "INDEX", MatchAny, "NO", "DEPENDS"))
2443  COMPLETE_WITH("ON EXTENSION");
2444  else if (Matches("ALTER", "INDEX", MatchAny, "DEPENDS"))
2445  COMPLETE_WITH("ON EXTENSION");
2446 
2447  /* ALTER LANGUAGE <name> */
2448  else if (Matches("ALTER", "LANGUAGE", MatchAny))
2449  COMPLETE_WITH("OWNER TO", "RENAME TO");
2450 
2451  /* ALTER LARGE OBJECT <oid> */
2452  else if (Matches("ALTER", "LARGE", "OBJECT", MatchAny))
2453  COMPLETE_WITH("OWNER TO");
2454 
2455  /* ALTER MATERIALIZED VIEW */
2456  else if (Matches("ALTER", "MATERIALIZED", "VIEW"))
2457  COMPLETE_WITH_SCHEMA_QUERY_PLUS(Query_for_list_of_matviews,
2458  "ALL IN TABLESPACE");
2459 
2460  /* ALTER USER,ROLE <name> */
2461  else if (Matches("ALTER", "USER|ROLE", MatchAny) &&
2462  !TailMatches("USER", "MAPPING"))
2463  COMPLETE_WITH("BYPASSRLS", "CONNECTION LIMIT", "CREATEDB", "CREATEROLE",
2464  "ENCRYPTED PASSWORD", "INHERIT", "LOGIN", "NOBYPASSRLS",
2465  "NOCREATEDB", "NOCREATEROLE", "NOINHERIT",
2466  "NOLOGIN", "NOREPLICATION", "NOSUPERUSER", "PASSWORD",
2467  "RENAME TO", "REPLICATION", "RESET", "SET", "SUPERUSER",
2468  "VALID UNTIL", "WITH");
2469 
2470  /* ALTER USER,ROLE <name> WITH */
2471  else if (Matches("ALTER", "USER|ROLE", MatchAny, "WITH"))
2472  /* Similar to the above, but don't complete "WITH" again. */
2473  COMPLETE_WITH("BYPASSRLS", "CONNECTION LIMIT", "CREATEDB", "CREATEROLE",
2474  "ENCRYPTED PASSWORD", "INHERIT", "LOGIN", "NOBYPASSRLS",
2475  "NOCREATEDB", "NOCREATEROLE", "NOINHERIT",
2476  "NOLOGIN", "NOREPLICATION", "NOSUPERUSER", "PASSWORD",
2477  "RENAME TO", "REPLICATION", "RESET", "SET", "SUPERUSER",
2478  "VALID UNTIL");
2479 
2480  /* ALTER DEFAULT PRIVILEGES */
2481  else if (Matches("ALTER", "DEFAULT", "PRIVILEGES"))
2482  COMPLETE_WITH("FOR", "GRANT", "IN SCHEMA", "REVOKE");
2483  /* ALTER DEFAULT PRIVILEGES FOR */
2484  else if (Matches("ALTER", "DEFAULT", "PRIVILEGES", "FOR"))
2485  COMPLETE_WITH("ROLE");
2486  /* ALTER DEFAULT PRIVILEGES IN */
2487  else if (Matches("ALTER", "DEFAULT", "PRIVILEGES", "IN"))
2488  COMPLETE_WITH("SCHEMA");
2489  /* ALTER DEFAULT PRIVILEGES FOR ROLE|USER ... */
2490  else if (Matches("ALTER", "DEFAULT", "PRIVILEGES", "FOR", "ROLE|USER",
2491  MatchAny))
2492  COMPLETE_WITH("GRANT", "REVOKE", "IN SCHEMA");
2493  /* ALTER DEFAULT PRIVILEGES IN SCHEMA ... */
2494  else if (Matches("ALTER", "DEFAULT", "PRIVILEGES", "IN", "SCHEMA",
2495  MatchAny))
2496  COMPLETE_WITH("GRANT", "REVOKE", "FOR ROLE");
2497  /* ALTER DEFAULT PRIVILEGES IN SCHEMA ... FOR */
2498  else if (Matches("ALTER", "DEFAULT", "PRIVILEGES", "IN", "SCHEMA",
2499  MatchAny, "FOR"))
2500  COMPLETE_WITH("ROLE");
2501  /* ALTER DEFAULT PRIVILEGES FOR ROLE|USER ... IN SCHEMA ... */
2502  /* ALTER DEFAULT PRIVILEGES IN SCHEMA ... FOR ROLE|USER ... */
2503  else if (Matches("ALTER", "DEFAULT", "PRIVILEGES", "FOR", "ROLE|USER",
2504  MatchAny, "IN", "SCHEMA", MatchAny) ||
2505  Matches("ALTER", "DEFAULT", "PRIVILEGES", "IN", "SCHEMA",
2506  MatchAny, "FOR", "ROLE|USER", MatchAny))
2507  COMPLETE_WITH("GRANT", "REVOKE");
2508  /* ALTER DOMAIN <name> */
2509  else if (Matches("ALTER", "DOMAIN", MatchAny))
2510  COMPLETE_WITH("ADD", "DROP", "OWNER TO", "RENAME", "SET",
2511  "VALIDATE CONSTRAINT");
2512  /* ALTER DOMAIN <sth> DROP */
2513  else if (Matches("ALTER", "DOMAIN", MatchAny, "DROP"))
2514  COMPLETE_WITH("CONSTRAINT", "DEFAULT", "NOT NULL");
2515  /* ALTER DOMAIN <sth> DROP|RENAME|VALIDATE CONSTRAINT */
2516  else if (Matches("ALTER", "DOMAIN", MatchAny, "DROP|RENAME|VALIDATE", "CONSTRAINT"))
2517  {
2518  set_completion_reference(prev3_wd);
2519  COMPLETE_WITH_SCHEMA_QUERY(Query_for_constraint_of_type);
2520  }
2521  /* ALTER DOMAIN <sth> RENAME */
2522  else if (Matches("ALTER", "DOMAIN", MatchAny, "RENAME"))
2523  COMPLETE_WITH("CONSTRAINT", "TO");
2524  /* ALTER DOMAIN <sth> RENAME CONSTRAINT <sth> */
2525  else if (Matches("ALTER", "DOMAIN", MatchAny, "RENAME", "CONSTRAINT", MatchAny))
2526  COMPLETE_WITH("TO");
2527 
2528  /* ALTER DOMAIN <sth> SET */
2529  else if (Matches("ALTER", "DOMAIN", MatchAny, "SET"))
2530  COMPLETE_WITH("DEFAULT", "NOT NULL", "SCHEMA");
2531  /* ALTER SEQUENCE <name> */
2532  else if (Matches("ALTER", "SEQUENCE", MatchAny))
2533  COMPLETE_WITH("AS", "INCREMENT", "MINVALUE", "MAXVALUE", "RESTART",
2534  "START", "NO", "CACHE", "CYCLE", "SET", "OWNED BY",
2535  "OWNER TO", "RENAME TO");
2536  /* ALTER SEQUENCE <name> AS */
2537  else if (TailMatches("ALTER", "SEQUENCE", MatchAny, "AS"))
2538  COMPLETE_WITH_CS("smallint", "integer", "bigint");
2539  /* ALTER SEQUENCE <name> NO */
2540  else if (Matches("ALTER", "SEQUENCE", MatchAny, "NO"))
2541  COMPLETE_WITH("MINVALUE", "MAXVALUE", "CYCLE");
2542  /* ALTER SEQUENCE <name> SET */
2543  else if (Matches("ALTER", "SEQUENCE", MatchAny, "SET"))
2544  COMPLETE_WITH("SCHEMA", "LOGGED", "UNLOGGED");
2545  /* ALTER SERVER <name> */
2546  else if (Matches("ALTER", "SERVER", MatchAny))
2547  COMPLETE_WITH("VERSION", "OPTIONS", "OWNER TO", "RENAME TO");
2548  /* ALTER SERVER <name> VERSION <version> */
2549  else if (Matches("ALTER", "SERVER", MatchAny, "VERSION", MatchAny))
2550  COMPLETE_WITH("OPTIONS");
2551  /* ALTER SYSTEM SET, RESET, RESET ALL */
2552  else if (Matches("ALTER", "SYSTEM"))
2553  COMPLETE_WITH("SET", "RESET");
2554  else if (Matches("ALTER", "SYSTEM", "SET|RESET"))
2555  COMPLETE_WITH_QUERY_VERBATIM_PLUS(Query_for_list_of_alter_system_set_vars,
2556  "ALL");
2557  else if (Matches("ALTER", "SYSTEM", "SET", MatchAny))
2558  COMPLETE_WITH("TO");
2559  /* ALTER VIEW <name> */
2560  else if (Matches("ALTER", "VIEW", MatchAny))
2561  COMPLETE_WITH("ALTER COLUMN", "OWNER TO", "RENAME", "RESET", "SET");
2562  /* ALTER VIEW xxx RENAME */
2563  else if (Matches("ALTER", "VIEW", MatchAny, "RENAME"))
2564  COMPLETE_WITH_ATTR_PLUS(prev2_wd, "COLUMN", "TO");
2565  else if (Matches("ALTER", "VIEW", MatchAny, "ALTER|RENAME", "COLUMN"))
2566  COMPLETE_WITH_ATTR(prev3_wd);
2567  /* ALTER VIEW xxx ALTER [ COLUMN ] yyy */
2568  else if (Matches("ALTER", "VIEW", MatchAny, "ALTER", MatchAny) ||
2569  Matches("ALTER", "VIEW", MatchAny, "ALTER", "COLUMN", MatchAny))
2570  COMPLETE_WITH("SET DEFAULT", "DROP DEFAULT");
2571  /* ALTER VIEW xxx RENAME yyy */
2572  else if (Matches("ALTER", "VIEW", MatchAny, "RENAME", MatchAnyExcept("TO")))
2573  COMPLETE_WITH("TO");
2574  /* ALTER VIEW xxx RENAME COLUMN yyy */
2575  else if (Matches("ALTER", "VIEW", MatchAny, "RENAME", "COLUMN", MatchAnyExcept("TO")))
2576  COMPLETE_WITH("TO");
2577  /* ALTER VIEW xxx RESET ( */
2578  else if (Matches("ALTER", "VIEW", MatchAny, "RESET"))
2579  COMPLETE_WITH("(");
2580  /* Complete ALTER VIEW xxx SET with "(" or "SCHEMA" */
2581  else if (Matches("ALTER", "VIEW", MatchAny, "SET"))
2582  COMPLETE_WITH("(", "SCHEMA");
2583  /* ALTER VIEW xxx SET|RESET ( yyy [= zzz] ) */
2584  else if (Matches("ALTER", "VIEW", MatchAny, "SET|RESET", "("))
2585  COMPLETE_WITH_LIST(view_optional_parameters);
2586  else if (Matches("ALTER", "VIEW", MatchAny, "SET", "(", MatchAny))
2587  COMPLETE_WITH("=");
2588  else if (Matches("ALTER", "VIEW", MatchAny, "SET", "(", "check_option", "="))
2589  COMPLETE_WITH("local", "cascaded");
2590  else if (Matches("ALTER", "VIEW", MatchAny, "SET", "(", "security_barrier|security_invoker", "="))
2591  COMPLETE_WITH("true", "false");
2592 
2593  /* ALTER MATERIALIZED VIEW <name> */
2594  else if (Matches("ALTER", "MATERIALIZED", "VIEW", MatchAny))
2595  COMPLETE_WITH("ALTER COLUMN", "CLUSTER ON", "DEPENDS ON EXTENSION",
2596  "NO DEPENDS ON EXTENSION", "OWNER TO", "RENAME",
2597  "RESET (", "SET");
2598  /* ALTER MATERIALIZED VIEW xxx RENAME */
2599  else if (Matches("ALTER", "MATERIALIZED", "VIEW", MatchAny, "RENAME"))
2600  COMPLETE_WITH_ATTR_PLUS(prev2_wd, "COLUMN", "TO");
2601  else if (Matches("ALTER", "MATERIALIZED", "VIEW", MatchAny, "ALTER|RENAME", "COLUMN"))
2602  COMPLETE_WITH_ATTR(prev3_wd);
2603  /* ALTER MATERIALIZED VIEW xxx RENAME yyy */
2604  else if (Matches("ALTER", "MATERIALIZED", "VIEW", MatchAny, "RENAME", MatchAnyExcept("TO")))
2605  COMPLETE_WITH("TO");
2606  /* ALTER MATERIALIZED VIEW xxx RENAME COLUMN yyy */
2607  else if (Matches("ALTER", "MATERIALIZED", "VIEW", MatchAny, "RENAME", "COLUMN", MatchAnyExcept("TO")))
2608  COMPLETE_WITH("TO");
2609  /* ALTER MATERIALIZED VIEW xxx SET */
2610  else if (Matches("ALTER", "MATERIALIZED", "VIEW", MatchAny, "SET"))
2611  COMPLETE_WITH("(", "ACCESS METHOD", "SCHEMA", "TABLESPACE", "WITHOUT CLUSTER");
2612  /* ALTER MATERIALIZED VIEW xxx SET ACCESS METHOD */
2613  else if (Matches("ALTER", "MATERIALIZED", "VIEW", MatchAny, "SET", "ACCESS", "METHOD"))
2614  COMPLETE_WITH_QUERY(Query_for_list_of_table_access_methods);
2615 
2616  /* ALTER POLICY <name> */
2617  else if (Matches("ALTER", "POLICY"))
2618  COMPLETE_WITH_QUERY(Query_for_list_of_policies);
2619  /* ALTER POLICY <name> ON */
2620  else if (Matches("ALTER", "POLICY", MatchAny))
2621  COMPLETE_WITH("ON");
2622  /* ALTER POLICY <name> ON <table> */
2623  else if (Matches("ALTER", "POLICY", MatchAny, "ON"))
2624  {
2625  set_completion_reference(prev2_wd);
2626  COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tables_for_policy);
2627  }
2628  /* ALTER POLICY <name> ON <table> - show options */
2629  else if (Matches("ALTER", "POLICY", MatchAny, "ON", MatchAny))
2630  COMPLETE_WITH("RENAME TO", "TO", "USING (", "WITH CHECK (");
2631  /* ALTER POLICY <name> ON <table> TO <role> */
2632  else if (Matches("ALTER", "POLICY", MatchAny, "ON", MatchAny, "TO"))
2633  COMPLETE_WITH_QUERY_PLUS(Query_for_list_of_roles,
2634  Keywords_for_list_of_grant_roles);
2635  /* ALTER POLICY <name> ON <table> USING ( */
2636  else if (Matches("ALTER", "POLICY", MatchAny, "ON", MatchAny, "USING"))
2637  COMPLETE_WITH("(");
2638  /* ALTER POLICY <name> ON <table> WITH CHECK ( */
2639  else if (Matches("ALTER", "POLICY", MatchAny, "ON", MatchAny, "WITH", "CHECK"))
2640  COMPLETE_WITH("(");
2641 
2642  /* ALTER RULE <name>, add ON */
2643  else if (Matches("ALTER", "RULE", MatchAny))
2644  COMPLETE_WITH("ON");
2645 
2646  /* If we have ALTER RULE <name> ON, then add the correct tablename */
2647  else if (Matches("ALTER", "RULE", MatchAny, "ON"))
2648  {
2649  set_completion_reference(prev2_wd);
2650  COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tables_for_rule);
2651  }
2652 
2653  /* ALTER RULE <name> ON <name> */
2654  else if (Matches("ALTER", "RULE", MatchAny, "ON", MatchAny))
2655  COMPLETE_WITH("RENAME TO");
2656 
2657  /* ALTER STATISTICS <name> */
2658  else if (Matches("ALTER", "STATISTICS", MatchAny))
2659  COMPLETE_WITH("OWNER TO", "RENAME TO", "SET SCHEMA", "SET STATISTICS");
2660  /* ALTER STATISTICS <name> SET */
2661  else if (Matches("ALTER", "STATISTICS", MatchAny, "SET"))
2662  COMPLETE_WITH("SCHEMA", "STATISTICS");
2663 
2664  /* ALTER TRIGGER <name>, add ON */
2665  else if (Matches("ALTER", "TRIGGER", MatchAny))
2666  COMPLETE_WITH("ON");
2667 
2668  else if (Matches("ALTER", "TRIGGER", MatchAny, "ON"))
2669  {
2670  set_completion_reference(prev2_wd);
2671  COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tables_for_trigger);
2672  }
2673 
2674  /* ALTER TRIGGER <name> ON <name> */
2675  else if (Matches("ALTER", "TRIGGER", MatchAny, "ON", MatchAny))
2676  COMPLETE_WITH("RENAME TO", "DEPENDS ON EXTENSION",
2677  "NO DEPENDS ON EXTENSION");
2678 
2679  /*
2680  * If we detect ALTER TABLE <name>, suggest sub commands
2681  */
2682  else if (Matches("ALTER", "TABLE", MatchAny))
2683  COMPLETE_WITH("ADD", "ALTER", "CLUSTER ON", "DISABLE", "DROP",
2684  "ENABLE", "INHERIT", "NO", "RENAME", "RESET",
2685  "OWNER TO", "SET", "VALIDATE CONSTRAINT",
2686  "REPLICA IDENTITY", "ATTACH PARTITION",
2687  "DETACH PARTITION", "FORCE ROW LEVEL SECURITY",
2688  "OF", "NOT OF");
2689  /* ALTER TABLE xxx ADD */
2690  else if (Matches("ALTER", "TABLE", MatchAny, "ADD"))
2691  {
2692  /* make sure to keep this list and the !Matches() below in sync */
2693  COMPLETE_WITH("COLUMN", "CONSTRAINT", "CHECK", "UNIQUE", "PRIMARY KEY",
2694  "EXCLUDE", "FOREIGN KEY");
2695  }
2696  /* ALTER TABLE xxx ADD [COLUMN] yyy */
2697  else if (Matches("ALTER", "TABLE", MatchAny, "ADD", "COLUMN", MatchAny) ||
2698  Matches("ALTER", "TABLE", MatchAny, "ADD", MatchAnyExcept("COLUMN|CONSTRAINT|CHECK|UNIQUE|PRIMARY|EXCLUDE|FOREIGN")))
2699  COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_datatypes);
2700  /* ALTER TABLE xxx ADD CONSTRAINT yyy */
2701  else if (Matches("ALTER", "TABLE", MatchAny, "ADD", "CONSTRAINT", MatchAny))
2702  COMPLETE_WITH("CHECK", "UNIQUE", "PRIMARY KEY", "EXCLUDE", "FOREIGN KEY");
2703  /* ALTER TABLE xxx ADD [CONSTRAINT yyy] (PRIMARY KEY|UNIQUE) */
2704  else if (Matches("ALTER", "TABLE", MatchAny, "ADD", "PRIMARY", "KEY") ||
2705  Matches("ALTER", "TABLE", MatchAny, "ADD", "UNIQUE") ||
2706  Matches("ALTER", "TABLE", MatchAny, "ADD", "CONSTRAINT", MatchAny, "PRIMARY", "KEY") ||
2707  Matches("ALTER", "TABLE", MatchAny, "ADD", "CONSTRAINT", MatchAny, "UNIQUE"))
2708  COMPLETE_WITH("(", "USING INDEX");
2709  /* ALTER TABLE xxx ADD PRIMARY KEY USING INDEX */
2710  else if (Matches("ALTER", "TABLE", MatchAny, "ADD", "PRIMARY", "KEY", "USING", "INDEX"))
2711  {
2712  set_completion_reference(prev6_wd);
2713  COMPLETE_WITH_SCHEMA_QUERY(Query_for_unique_index_of_table);
2714  }
2715  /* ALTER TABLE xxx ADD UNIQUE USING INDEX */
2716  else if (Matches("ALTER", "TABLE", MatchAny, "ADD", "UNIQUE", "USING", "INDEX"))
2717  {
2718  set_completion_reference(prev5_wd);
2719  COMPLETE_WITH_SCHEMA_QUERY(Query_for_unique_index_of_table);
2720  }
2721  /* ALTER TABLE xxx ADD CONSTRAINT yyy PRIMARY KEY USING INDEX */
2722  else if (Matches("ALTER", "TABLE", MatchAny, "ADD", "CONSTRAINT", MatchAny,
2723  "PRIMARY", "KEY", "USING", "INDEX"))
2724  {
2725  set_completion_reference(prev8_wd);
2726  COMPLETE_WITH_SCHEMA_QUERY(Query_for_unique_index_of_table);
2727  }
2728  /* ALTER TABLE xxx ADD CONSTRAINT yyy UNIQUE USING INDEX */
2729  else if (Matches("ALTER", "TABLE", MatchAny, "ADD", "CONSTRAINT", MatchAny,
2730  "UNIQUE", "USING", "INDEX"))
2731  {
2732  set_completion_reference(prev7_wd);
2733  COMPLETE_WITH_SCHEMA_QUERY(Query_for_unique_index_of_table);
2734  }
2735  /* ALTER TABLE xxx ENABLE */
2736  else if (Matches("ALTER", "TABLE", MatchAny, "ENABLE"))
2737  COMPLETE_WITH("ALWAYS", "REPLICA", "ROW LEVEL SECURITY", "RULE",
2738  "TRIGGER");
2739  else if (Matches("ALTER", "TABLE", MatchAny, "ENABLE", "REPLICA|ALWAYS"))
2740  COMPLETE_WITH("RULE", "TRIGGER");
2741  else if (Matches("ALTER", "TABLE", MatchAny, "ENABLE", "RULE"))
2742  {
2743  set_completion_reference(prev3_wd);
2744  COMPLETE_WITH_SCHEMA_QUERY(Query_for_rule_of_table);
2745  }
2746  else if (Matches("ALTER", "TABLE", MatchAny, "ENABLE", MatchAny, "RULE"))
2747  {
2748  set_completion_reference(prev4_wd);
2749  COMPLETE_WITH_SCHEMA_QUERY(Query_for_rule_of_table);
2750  }
2751  else if (Matches("ALTER", "TABLE", MatchAny, "ENABLE", "TRIGGER"))
2752  {
2753  set_completion_reference(prev3_wd);
2754  COMPLETE_WITH_SCHEMA_QUERY(Query_for_trigger_of_table);
2755  }
2756  else if (Matches("ALTER", "TABLE", MatchAny, "ENABLE", MatchAny, "TRIGGER"))
2757  {
2758  set_completion_reference(prev4_wd);
2759  COMPLETE_WITH_SCHEMA_QUERY(Query_for_trigger_of_table);
2760  }
2761  /* ALTER TABLE xxx INHERIT */
2762  else if (Matches("ALTER", "TABLE", MatchAny, "INHERIT"))
2763  COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tables);
2764  /* ALTER TABLE xxx NO */
2765  else if (Matches("ALTER", "TABLE", MatchAny, "NO"))
2766  COMPLETE_WITH("FORCE ROW LEVEL SECURITY", "INHERIT");
2767  /* ALTER TABLE xxx NO INHERIT */
2768  else if (Matches("ALTER", "TABLE", MatchAny, "NO", "INHERIT"))
2769  COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tables);
2770  /* ALTER TABLE xxx DISABLE */
2771  else if (Matches("ALTER", "TABLE", MatchAny, "DISABLE"))
2772  COMPLETE_WITH("ROW LEVEL SECURITY", "RULE", "TRIGGER");
2773  else if (Matches("ALTER", "TABLE", MatchAny, "DISABLE", "RULE"))
2774  {
2775  set_completion_reference(prev3_wd);
2776  COMPLETE_WITH_SCHEMA_QUERY(Query_for_rule_of_table);
2777  }
2778  else if (Matches("ALTER", "TABLE", MatchAny, "DISABLE", "TRIGGER"))
2779  {
2780  set_completion_reference(prev3_wd);
2781  COMPLETE_WITH_SCHEMA_QUERY(Query_for_trigger_of_table);
2782  }
2783 
2784  /* ALTER TABLE xxx ALTER */
2785  else if (Matches("ALTER", "TABLE", MatchAny, "ALTER"))
2786  COMPLETE_WITH_ATTR_PLUS(prev2_wd, "COLUMN", "CONSTRAINT");
2787 
2788  /* ALTER TABLE xxx RENAME */
2789  else if (Matches("ALTER", "TABLE", MatchAny, "RENAME"))
2790  COMPLETE_WITH_ATTR_PLUS(prev2_wd, "COLUMN", "CONSTRAINT", "TO");
2791  else if (Matches("ALTER", "TABLE", MatchAny, "ALTER|RENAME", "COLUMN"))
2792  COMPLETE_WITH_ATTR(prev3_wd);
2793 
2794  /* ALTER TABLE xxx RENAME yyy */
2795  else if (Matches("ALTER", "TABLE", MatchAny, "RENAME", MatchAnyExcept("CONSTRAINT|TO")))
2796  COMPLETE_WITH("TO");
2797 
2798  /* ALTER TABLE xxx RENAME COLUMN/CONSTRAINT yyy */
2799  else if (Matches("ALTER", "TABLE", MatchAny, "RENAME", "COLUMN|CONSTRAINT", MatchAnyExcept("TO")))
2800  COMPLETE_WITH("TO");
2801 
2802  /* If we have ALTER TABLE <sth> DROP, provide COLUMN or CONSTRAINT */
2803  else if (Matches("ALTER", "TABLE", MatchAny, "DROP"))
2804  COMPLETE_WITH("COLUMN", "CONSTRAINT");
2805  /* If we have ALTER TABLE <sth> DROP COLUMN, provide list of columns */
2806  else if (Matches("ALTER", "TABLE", MatchAny, "DROP", "COLUMN"))
2807  COMPLETE_WITH_ATTR(prev3_wd);
2808  /* ALTER TABLE <sth> ALTER|DROP|RENAME CONSTRAINT <constraint> */
2809  else if (Matches("ALTER", "TABLE", MatchAny, "ALTER|DROP|RENAME", "CONSTRAINT"))
2810  {
2811  set_completion_reference(prev3_wd);
2812  COMPLETE_WITH_SCHEMA_QUERY(Query_for_constraint_of_table);
2813  }
2814  /* ALTER TABLE <sth> VALIDATE CONSTRAINT <non-validated constraint> */
2815  else if (Matches("ALTER", "TABLE", MatchAny, "VALIDATE", "CONSTRAINT"))
2816  {
2817  set_completion_reference(prev3_wd);
2818  COMPLETE_WITH_SCHEMA_QUERY(Query_for_constraint_of_table_not_validated);
2819  }
2820  /* ALTER TABLE ALTER [COLUMN] <foo> */
2821  else if (Matches("ALTER", "TABLE", MatchAny, "ALTER", "COLUMN", MatchAny) ||
2822  Matches("ALTER", "TABLE", MatchAny, "ALTER", MatchAny))
2823  COMPLETE_WITH("TYPE", "SET", "RESET", "RESTART", "ADD", "DROP");
2824  /* ALTER TABLE ALTER [COLUMN] <foo> ADD */
2825  else if (Matches("ALTER", "TABLE", MatchAny, "ALTER", "COLUMN", MatchAny, "ADD") ||
2826  Matches("ALTER", "TABLE", MatchAny, "ALTER", MatchAny, "ADD"))
2827  COMPLETE_WITH("GENERATED");
2828  /* ALTER TABLE ALTER [COLUMN] <foo> ADD GENERATED */
2829  else if (Matches("ALTER", "TABLE", MatchAny, "ALTER", "COLUMN", MatchAny, "ADD", "GENERATED") ||
2830  Matches("ALTER", "TABLE", MatchAny, "ALTER", MatchAny, "ADD", "GENERATED"))
2831  COMPLETE_WITH("ALWAYS", "BY DEFAULT");
2832  /* ALTER TABLE ALTER [COLUMN] <foo> ADD GENERATED */
2833  else if (Matches("ALTER", "TABLE", MatchAny, "ALTER", "COLUMN", MatchAny, "ADD", "GENERATED", "ALWAYS") ||
2834  Matches("ALTER", "TABLE", MatchAny, "ALTER", MatchAny, "ADD", "GENERATED", "ALWAYS") ||
2835  Matches("ALTER", "TABLE", MatchAny, "ALTER", "COLUMN", MatchAny, "ADD", "GENERATED", "BY", "DEFAULT") ||
2836  Matches("ALTER", "TABLE", MatchAny, "ALTER", MatchAny, "ADD", "GENERATED", "BY", "DEFAULT"))
2837  COMPLETE_WITH("AS IDENTITY");
2838  /* ALTER TABLE ALTER [COLUMN] <foo> SET */
2839  else if (Matches("ALTER", "TABLE", MatchAny, "ALTER", "COLUMN", MatchAny, "SET") ||
2840  Matches("ALTER", "TABLE", MatchAny, "ALTER", MatchAny, "SET"))
2841  COMPLETE_WITH("(", "COMPRESSION", "DATA TYPE", "DEFAULT", "EXPRESSION", "GENERATED", "NOT NULL",
2842  "STATISTICS", "STORAGE",
2843  /* a subset of ALTER SEQUENCE options */
2844  "INCREMENT", "MINVALUE", "MAXVALUE", "START", "NO", "CACHE", "CYCLE");
2845  /* ALTER TABLE ALTER [COLUMN] <foo> SET ( */
2846  else if (Matches("ALTER", "TABLE", MatchAny, "ALTER", "COLUMN", MatchAny, "SET", "(") ||
2847  Matches("ALTER", "TABLE", MatchAny, "ALTER", MatchAny, "SET", "("))
2848  COMPLETE_WITH("n_distinct", "n_distinct_inherited");
2849  /* ALTER TABLE ALTER [COLUMN] <foo> SET COMPRESSION */
2850  else if (Matches("ALTER", "TABLE", MatchAny, "ALTER", "COLUMN", MatchAny, "SET", "COMPRESSION") ||
2851  Matches("ALTER", "TABLE", MatchAny, "ALTER", MatchAny, "SET", "COMPRESSION"))
2852  COMPLETE_WITH("DEFAULT", "PGLZ", "LZ4");
2853  /* ALTER TABLE ALTER [COLUMN] <foo> SET EXPRESSION */
2854  else if (Matches("ALTER", "TABLE", MatchAny, "ALTER", "COLUMN", MatchAny, "SET", "EXPRESSION") ||
2855  Matches("ALTER", "TABLE", MatchAny, "ALTER", MatchAny, "SET", "EXPRESSION"))
2856  COMPLETE_WITH("AS");
2857  /* ALTER TABLE ALTER [COLUMN] <foo> SET EXPRESSION AS */
2858  else if (Matches("ALTER", "TABLE", MatchAny, "ALTER", "COLUMN", MatchAny, "SET", "EXPRESSION", "AS") ||
2859  Matches("ALTER", "TABLE", MatchAny, "ALTER", MatchAny, "SET", "EXPRESSION", "AS"))
2860  COMPLETE_WITH("(");
2861  /* ALTER TABLE ALTER [COLUMN] <foo> SET GENERATED */
2862  else if (Matches("ALTER", "TABLE", MatchAny, "ALTER", "COLUMN", MatchAny, "SET", "GENERATED") ||
2863  Matches("ALTER", "TABLE", MatchAny, "ALTER", MatchAny, "SET", "GENERATED"))
2864  COMPLETE_WITH("ALWAYS", "BY DEFAULT");
2865  /* ALTER TABLE ALTER [COLUMN] <foo> SET NO */
2866  else if (Matches("ALTER", "TABLE", MatchAny, "ALTER", "COLUMN", MatchAny, "SET", "NO") ||
2867  Matches("ALTER", "TABLE", MatchAny, "ALTER", MatchAny, "SET", "NO"))
2868  COMPLETE_WITH("MINVALUE", "MAXVALUE", "CYCLE");
2869  /* ALTER TABLE ALTER [COLUMN] <foo> SET STORAGE */
2870  else if (Matches("ALTER", "TABLE", MatchAny, "ALTER", "COLUMN", MatchAny, "SET", "STORAGE") ||
2871  Matches("ALTER", "TABLE", MatchAny, "ALTER", MatchAny, "SET", "STORAGE"))
2872  COMPLETE_WITH("DEFAULT", "PLAIN", "EXTERNAL", "EXTENDED", "MAIN");
2873  /* ALTER TABLE ALTER [COLUMN] <foo> SET STATISTICS */
2874  else if (Matches("ALTER", "TABLE", MatchAny, "ALTER", "COLUMN", MatchAny, "SET", "STATISTICS") ||
2875  Matches("ALTER", "TABLE", MatchAny, "ALTER", MatchAny, "SET", "STATISTICS"))
2876  {
2877  /* Enforce no completion here, as an integer has to be specified */
2878  }
2879  /* ALTER TABLE ALTER [COLUMN] <foo> DROP */
2880  else if (Matches("ALTER", "TABLE", MatchAny, "ALTER", "COLUMN", MatchAny, "DROP") ||
2881  Matches("ALTER", "TABLE", MatchAny, "ALTER", MatchAny, "DROP"))
2882  COMPLETE_WITH("DEFAULT", "EXPRESSION", "IDENTITY", "NOT NULL");
2883  else if (Matches("ALTER", "TABLE", MatchAny, "CLUSTER"))
2884  COMPLETE_WITH("ON");
2885  else if (Matches("ALTER", "TABLE", MatchAny, "CLUSTER", "ON"))
2886  {
2887  set_completion_reference(prev3_wd);
2888  COMPLETE_WITH_SCHEMA_QUERY(Query_for_index_of_table);
2889  }
2890  /* If we have ALTER TABLE <sth> SET, provide list of attributes and '(' */
2891  else if (Matches("ALTER", "TABLE", MatchAny, "SET"))
2892  COMPLETE_WITH("(", "ACCESS METHOD", "LOGGED", "SCHEMA",
2893  "TABLESPACE", "UNLOGGED", "WITH", "WITHOUT");
2894 
2895  /*
2896  * If we have ALTER TABLE <sth> SET ACCESS METHOD provide a list of table
2897  * AMs.
2898  */
2899  else if (Matches("ALTER", "TABLE", MatchAny, "SET", "ACCESS", "METHOD"))
2900  COMPLETE_WITH_QUERY_PLUS(Query_for_list_of_table_access_methods,
2901  "DEFAULT");
2902 
2903  /*
2904  * If we have ALTER TABLE <sth> SET TABLESPACE provide a list of
2905  * tablespaces
2906  */
2907  else if (Matches("ALTER", "TABLE", MatchAny, "SET", "TABLESPACE"))
2908  COMPLETE_WITH_QUERY(Query_for_list_of_tablespaces);
2909  /* If we have ALTER TABLE <sth> SET WITHOUT provide CLUSTER or OIDS */
2910  else if (Matches("ALTER", "TABLE", MatchAny, "SET", "WITHOUT"))
2911  COMPLETE_WITH("CLUSTER", "OIDS");
2912  /* ALTER TABLE <foo> RESET */
2913  else if (Matches("ALTER", "TABLE", MatchAny, "RESET"))
2914  COMPLETE_WITH("(");
2915  /* ALTER TABLE <foo> SET|RESET ( */
2916  else if (Matches("ALTER", "TABLE", MatchAny, "SET|RESET", "("))
2917  COMPLETE_WITH_LIST(table_storage_parameters);
2918  else if (Matches("ALTER", "TABLE", MatchAny, "REPLICA", "IDENTITY", "USING", "INDEX"))
2919  {
2920  set_completion_reference(prev5_wd);
2921  COMPLETE_WITH_SCHEMA_QUERY(Query_for_index_of_table);
2922  }
2923  else if (Matches("ALTER", "TABLE", MatchAny, "REPLICA", "IDENTITY", "USING"))
2924  COMPLETE_WITH("INDEX");
2925  else if (Matches("ALTER", "TABLE", MatchAny, "REPLICA", "IDENTITY"))
2926  COMPLETE_WITH("FULL", "NOTHING", "DEFAULT", "USING");
2927  else if (Matches("ALTER", "TABLE", MatchAny, "REPLICA"))
2928  COMPLETE_WITH("IDENTITY");
2929 
2930  /*
2931  * If we have ALTER TABLE <foo> ATTACH PARTITION, provide a list of
2932  * tables.
2933  */
2934  else if (Matches("ALTER", "TABLE", MatchAny, "ATTACH", "PARTITION"))
2935  COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tables);
2936  /* Limited completion support for partition bound specification */
2937  else if (TailMatches("ATTACH", "PARTITION", MatchAny))
2938  COMPLETE_WITH("FOR VALUES", "DEFAULT");
2939  else if (TailMatches("FOR", "VALUES"))
2940  COMPLETE_WITH("FROM (", "IN (", "WITH (");
2941 
2942  /*
2943  * If we have ALTER TABLE <foo> DETACH PARTITION, provide a list of
2944  * partitions of <foo>.
2945  */
2946  else if (Matches("ALTER", "TABLE", MatchAny, "DETACH", "PARTITION"))
2947  {
2948  set_completion_reference(prev3_wd);
2949  COMPLETE_WITH_SCHEMA_QUERY(Query_for_partition_of_table);
2950  }
2951  else if (Matches("ALTER", "TABLE", MatchAny, "DETACH", "PARTITION", MatchAny))
2952  COMPLETE_WITH("CONCURRENTLY", "FINALIZE");
2953 
2954  /* ALTER TABLE <name> OF */
2955  else if (Matches("ALTER", "TABLE", MatchAny, "OF"))
2956  COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_composite_datatypes);
2957 
2958  /* ALTER TABLESPACE <foo> with RENAME TO, OWNER TO, SET, RESET */
2959  else if (Matches("ALTER", "TABLESPACE", MatchAny))
2960  COMPLETE_WITH("RENAME TO", "OWNER TO", "SET", "RESET");
2961  /* ALTER TABLESPACE <foo> SET|RESET */
2962  else if (Matches("ALTER", "TABLESPACE", MatchAny, "SET|RESET"))
2963  COMPLETE_WITH("(");
2964  /* ALTER TABLESPACE <foo> SET|RESET ( */
2965  else if (Matches("ALTER", "TABLESPACE", MatchAny, "SET|RESET", "("))
2966  COMPLETE_WITH("seq_page_cost", "random_page_cost",
2967  "effective_io_concurrency", "maintenance_io_concurrency");
2968 
2969  /* ALTER TEXT SEARCH */
2970  else if (Matches("ALTER", "TEXT", "SEARCH"))
2971  COMPLETE_WITH("CONFIGURATION", "DICTIONARY", "PARSER", "TEMPLATE");
2972  else if (Matches("ALTER", "TEXT", "SEARCH", "TEMPLATE|PARSER", MatchAny))
2973  COMPLETE_WITH("RENAME TO", "SET SCHEMA");
2974  else if (Matches("ALTER", "TEXT", "SEARCH", "DICTIONARY", MatchAny))
2975  COMPLETE_WITH("(", "OWNER TO", "RENAME TO", "SET SCHEMA");
2976  else if (Matches("ALTER", "TEXT", "SEARCH", "CONFIGURATION", MatchAny))
2977  COMPLETE_WITH("ADD MAPPING FOR", "ALTER MAPPING",
2978  "DROP MAPPING FOR",
2979  "OWNER TO", "RENAME TO", "SET SCHEMA");
2980 
2981  /* complete ALTER TYPE <foo> with actions */
2982  else if (Matches("ALTER", "TYPE", MatchAny))
2983  COMPLETE_WITH("ADD ATTRIBUTE", "ADD VALUE", "ALTER ATTRIBUTE",
2984  "DROP ATTRIBUTE",
2985  "OWNER TO", "RENAME", "SET SCHEMA", "SET (");
2986  /* complete ALTER TYPE <foo> ADD with actions */
2987  else if (Matches("ALTER", "TYPE", MatchAny, "ADD"))
2988  COMPLETE_WITH("ATTRIBUTE", "VALUE");
2989  /* ALTER TYPE <foo> RENAME */
2990  else if (Matches("ALTER", "TYPE", MatchAny, "RENAME"))
2991  COMPLETE_WITH("ATTRIBUTE", "TO", "VALUE");
2992  /* ALTER TYPE xxx RENAME (ATTRIBUTE|VALUE) yyy */
2993  else if (Matches("ALTER", "TYPE", MatchAny, "RENAME", "ATTRIBUTE|VALUE", MatchAny))
2994  COMPLETE_WITH("TO");
2995 
2996  /*
2997  * If we have ALTER TYPE <sth> ALTER/DROP/RENAME ATTRIBUTE, provide list
2998  * of attributes
2999  */
3000  else if (Matches("ALTER", "TYPE", MatchAny, "ALTER|DROP|RENAME", "ATTRIBUTE"))
3001  COMPLETE_WITH_ATTR(prev3_wd);
3002  /* ALTER TYPE ALTER ATTRIBUTE <foo> */
3003  else if (Matches("ALTER", "TYPE", MatchAny, "ALTER", "ATTRIBUTE", MatchAny))
3004  COMPLETE_WITH("TYPE");
3005  /* complete ALTER TYPE <sth> RENAME VALUE with list of enum values */
3006  else if (Matches("ALTER", "TYPE", MatchAny, "RENAME", "VALUE"))
3007  COMPLETE_WITH_ENUM_VALUE(prev3_wd);
3008  /* ALTER TYPE <foo> SET */
3009  else if (Matches("ALTER", "TYPE", MatchAny, "SET"))
3010  COMPLETE_WITH("(", "SCHEMA");
3011  /* complete ALTER TYPE <foo> SET ( with settable properties */
3012  else if (Matches("ALTER", "TYPE", MatchAny, "SET", "("))
3013  COMPLETE_WITH("ANALYZE", "RECEIVE", "SEND", "STORAGE", "SUBSCRIPT",
3014  "TYPMOD_IN", "TYPMOD_OUT");
3015 
3016  /* complete ALTER GROUP <foo> */
3017  else if (Matches("ALTER", "GROUP", MatchAny))
3018  COMPLETE_WITH("ADD USER", "DROP USER", "RENAME TO");
3019  /* complete ALTER GROUP <foo> ADD|DROP with USER */
3020  else if (Matches("ALTER", "GROUP", MatchAny, "ADD|DROP"))
3021  COMPLETE_WITH("USER");
3022  /* complete ALTER GROUP <foo> ADD|DROP USER with a user name */
3023  else if (Matches("ALTER", "GROUP", MatchAny, "ADD|DROP", "USER"))
3024  COMPLETE_WITH_QUERY(Query_for_list_of_roles);
3025 
3026 /*
3027  * ANALYZE [ ( option [, ...] ) ] [ table_and_columns [, ...] ]
3028  * ANALYZE [ VERBOSE ] [ table_and_columns [, ...] ]
3029  */
3030  else if (Matches("ANALYZE"))
3031  COMPLETE_WITH_SCHEMA_QUERY_PLUS(Query_for_list_of_analyzables,
3032  "VERBOSE");
3033  else if (HeadMatches("ANALYZE", "(*") &&
3034  !HeadMatches("ANALYZE", "(*)"))
3035  {
3036  /*
3037  * This fires if we're in an unfinished parenthesized option list.
3038  * get_previous_words treats a completed parenthesized option list as
3039  * one word, so the above test is correct.
3040  */
3041  if (ends_with(prev_wd, '(') || ends_with(prev_wd, ','))
3042  COMPLETE_WITH("VERBOSE", "SKIP_LOCKED", "BUFFER_USAGE_LIMIT");
3043  else if (TailMatches("VERBOSE|SKIP_LOCKED"))
3044  COMPLETE_WITH("ON", "OFF");
3045  }
3046  else if (Matches("ANALYZE", MatchAnyN, "("))
3047  /* "ANALYZE (" should be caught above, so assume we want columns */
3048  COMPLETE_WITH_ATTR(prev2_wd);
3049  else if (HeadMatches("ANALYZE"))
3050  COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_analyzables);
3051 
3052 /* BEGIN */
3053  else if (Matches("BEGIN"))
3054  COMPLETE_WITH("WORK", "TRANSACTION", "ISOLATION LEVEL", "READ", "DEFERRABLE", "NOT DEFERRABLE");
3055 /* END, ABORT */
3056  else if (Matches("END|ABORT"))
3057  COMPLETE_WITH("AND", "WORK", "TRANSACTION");
3058 /* COMMIT */
3059  else if (Matches("COMMIT"))
3060  COMPLETE_WITH("AND", "WORK", "TRANSACTION", "PREPARED");
3061 /* RELEASE SAVEPOINT */
3062  else if (Matches("RELEASE"))
3063  COMPLETE_WITH("SAVEPOINT");
3064 /* ROLLBACK */
3065  else if (Matches("ROLLBACK"))
3066  COMPLETE_WITH("AND", "WORK", "TRANSACTION", "TO SAVEPOINT", "PREPARED");
3067  else if (Matches("ABORT|END|COMMIT|ROLLBACK", "AND"))
3068  COMPLETE_WITH("CHAIN");
3069 /* CALL */
3070  else if (Matches("CALL"))
3071  COMPLETE_WITH_VERSIONED_SCHEMA_QUERY(Query_for_list_of_procedures);
3072  else if (Matches("CALL", MatchAny))
3073  COMPLETE_WITH("(");
3074 /* CLOSE */
3075  else if (Matches("CLOSE"))
3076  COMPLETE_WITH_QUERY_PLUS(Query_for_list_of_cursors,
3077  "ALL");
3078 /* CLUSTER */
3079  else if (Matches("CLUSTER"))
3080  COMPLETE_WITH_SCHEMA_QUERY_PLUS(Query_for_list_of_clusterables,
3081  "VERBOSE");
3082  else if (Matches("CLUSTER", "VERBOSE") ||
3083  Matches("CLUSTER", "(*)"))
3084  COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_clusterables);
3085  /* If we have CLUSTER <sth>, then add "USING" */
3086  else if (Matches("CLUSTER", MatchAnyExcept("VERBOSE|ON|(|(*)")))
3087  COMPLETE_WITH("USING");
3088  /* If we have CLUSTER VERBOSE <sth>, then add "USING" */
3089  else if (Matches("CLUSTER", "VERBOSE|(*)", MatchAny))
3090  COMPLETE_WITH("USING");
3091  /* If we have CLUSTER <sth> USING, then add the index as well */
3092  else if (Matches("CLUSTER", MatchAny, "USING") ||
3093  Matches("CLUSTER", "VERBOSE|(*)", MatchAny, "USING"))
3094  {
3095  set_completion_reference(prev2_wd);
3096  COMPLETE_WITH_SCHEMA_QUERY(Query_for_index_of_table);
3097  }
3098  else if (HeadMatches("CLUSTER", "(*") &&
3099  !HeadMatches("CLUSTER", "(*)"))
3100  {
3101  /*
3102  * This fires if we're in an unfinished parenthesized option list.
3103  * get_previous_words treats a completed parenthesized option list as
3104  * one word, so the above test is correct.
3105  */
3106  if (ends_with(prev_wd, '(') || ends_with(prev_wd, ','))
3107  COMPLETE_WITH("VERBOSE");
3108  }
3109 
3110 /* COMMENT */
3111  else if (Matches("COMMENT"))
3112  COMPLETE_WITH("ON");
3113  else if (Matches("COMMENT", "ON"))
3114  COMPLETE_WITH("ACCESS METHOD", "AGGREGATE", "CAST", "COLLATION",
3115  "COLUMN", "CONSTRAINT", "CONVERSION", "DATABASE",
3116  "DOMAIN", "EXTENSION", "EVENT TRIGGER",
3117  "FOREIGN DATA WRAPPER", "FOREIGN TABLE",
3118  "FUNCTION", "INDEX", "LANGUAGE", "LARGE OBJECT",
3119  "MATERIALIZED VIEW", "OPERATOR", "POLICY",
3120  "PROCEDURE", "PROCEDURAL LANGUAGE", "PUBLICATION", "ROLE",
3121  "ROUTINE", "RULE", "SCHEMA", "SEQUENCE", "SERVER",
3122  "STATISTICS", "SUBSCRIPTION", "TABLE",
3123  "TABLESPACE", "TEXT SEARCH", "TRANSFORM FOR",
3124  "TRIGGER", "TYPE", "VIEW");
3125  else if (Matches("COMMENT", "ON", "ACCESS", "METHOD"))
3126  COMPLETE_WITH_QUERY(Query_for_list_of_access_methods);
3127  else if (Matches("COMMENT", "ON", "CONSTRAINT"))
3128  COMPLETE_WITH_QUERY(Query_for_all_table_constraints);
3129  else if (Matches("COMMENT", "ON", "CONSTRAINT", MatchAny))
3130  COMPLETE_WITH("ON");
3131  else if (Matches("COMMENT", "ON", "CONSTRAINT", MatchAny, "ON"))
3132  {
3133  set_completion_reference(prev2_wd);
3134  COMPLETE_WITH_SCHEMA_QUERY_PLUS(Query_for_list_of_tables_for_constraint,
3135  "DOMAIN");
3136  }
3137  else if (Matches("COMMENT", "ON", "CONSTRAINT", MatchAny, "ON", "DOMAIN"))
3138  COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_domains);
3139  else if (Matches("COMMENT", "ON", "EVENT", "TRIGGER"))
3140  COMPLETE_WITH_QUERY(Query_for_list_of_event_triggers);
3141  else if (Matches("COMMENT", "ON", "FOREIGN"))
3142  COMPLETE_WITH("DATA WRAPPER", "TABLE");
3143  else if (Matches("COMMENT", "ON", "FOREIGN", "TABLE"))
3144  COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_foreign_tables);
3145  else if (Matches("COMMENT", "ON", "MATERIALIZED", "VIEW"))
3146  COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_matviews);
3147  else if (Matches("COMMENT", "ON", "POLICY"))
3148  COMPLETE_WITH_QUERY(Query_for_list_of_policies);
3149  else if (Matches("COMMENT", "ON", "POLICY", MatchAny))
3150  COMPLETE_WITH("ON");
3151  else if (Matches("COMMENT", "ON", "POLICY", MatchAny, "ON"))
3152  {
3153  set_completion_reference(prev2_wd);
3154  COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tables_for_policy);
3155  }
3156  else if (Matches("COMMENT", "ON", "PROCEDURAL", "LANGUAGE"))
3157  COMPLETE_WITH_QUERY(Query_for_list_of_languages);
3158  else if (Matches("COMMENT", "ON", "RULE", MatchAny))
3159  COMPLETE_WITH("ON");
3160  else if (Matches("COMMENT", "ON", "RULE", MatchAny, "ON"))
3161  {
3162  set_completion_reference(prev2_wd);
3163  COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tables_for_rule);
3164  }
3165  else if (Matches("COMMENT", "ON", "TEXT", "SEARCH"))
3166  COMPLETE_WITH("CONFIGURATION", "DICTIONARY", "PARSER", "TEMPLATE");
3167  else if (Matches("COMMENT", "ON", "TEXT", "SEARCH", "CONFIGURATION"))
3168  COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_ts_configurations);
3169  else if (Matches("COMMENT", "ON", "TEXT", "SEARCH", "DICTIONARY"))
3170  COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_ts_dictionaries);
3171  else if (Matches("COMMENT", "ON", "TEXT", "SEARCH", "PARSER"))
3172  COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_ts_parsers);
3173  else if (Matches("COMMENT", "ON", "TEXT", "SEARCH", "TEMPLATE"))
3174  COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_ts_templates);
3175  else if (Matches("COMMENT", "ON", "TRANSFORM", "FOR"))
3176  COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_datatypes);
3177  else if (Matches("COMMENT", "ON", "TRANSFORM", "FOR", MatchAny))
3178  COMPLETE_WITH("LANGUAGE");
3179  else if (Matches("COMMENT", "ON", "TRANSFORM", "FOR", MatchAny, "LANGUAGE"))
3180  {
3181  set_completion_reference(prev2_wd);
3182  COMPLETE_WITH_QUERY(Query_for_list_of_languages);
3183  }
3184  else if (Matches("COMMENT", "ON", "TRIGGER", MatchAny))
3185  COMPLETE_WITH("ON");
3186  else if (Matches("COMMENT", "ON", "TRIGGER", MatchAny, "ON"))
3187  {
3188  set_completion_reference(prev2_wd);
3189  COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tables_for_trigger);
3190  }
3191  else if (Matches("COMMENT", "ON", MatchAny, MatchAnyExcept("IS")) ||
3192  Matches("COMMENT", "ON", MatchAny, MatchAny, MatchAnyExcept("IS")) ||
3193  Matches("COMMENT", "ON", MatchAny, MatchAny, MatchAny, MatchAnyExcept("IS")) ||
3194  Matches("COMMENT", "ON", MatchAny, MatchAny, MatchAny, MatchAny, MatchAnyExcept("IS")))
3195  COMPLETE_WITH("IS");
3196 
3197 /* COPY */
3198 
3199  /*
3200  * If we have COPY, offer list of tables or "(" (Also cover the analogous
3201  * backslash command).
3202  */
3203  else if (Matches("COPY|\\copy"))
3204  COMPLETE_WITH_SCHEMA_QUERY_PLUS(Query_for_list_of_tables, "(");
3205  /* Complete COPY ( with legal query commands */
3206  else if (Matches("COPY|\\copy", "("))
3207  COMPLETE_WITH("SELECT", "TABLE", "VALUES", "INSERT INTO", "UPDATE", "DELETE FROM", "WITH");
3208  /* Complete COPY <sth> */
3209  else if (Matches("COPY|\\copy", MatchAny))
3210  COMPLETE_WITH("FROM", "TO");
3211  /* Complete COPY <sth> FROM|TO with filename */
3212  else if (Matches("COPY", MatchAny, "FROM|TO"))
3213  {
3214  completion_charp = "";
3215  completion_force_quote = true; /* COPY requires quoted filename */
3216  matches = rl_completion_matches(text, complete_from_files);
3217  }
3218  else if (Matches("\\copy", MatchAny, "FROM|TO"))
3219  {
3220  completion_charp = "";
3221  completion_force_quote = false;
3222  matches = rl_completion_matches(text, complete_from_files);
3223  }
3224 
3225  /* Complete COPY <sth> TO <sth> */
3226  else if (Matches("COPY|\\copy", MatchAny, "TO", MatchAny))
3227  COMPLETE_WITH("WITH (");
3228 
3229  /* Complete COPY <sth> FROM <sth> */
3230  else if (Matches("COPY|\\copy", MatchAny, "FROM", MatchAny))
3231  COMPLETE_WITH("WITH (", "WHERE");
3232 
3233  /* Complete COPY <sth> FROM|TO filename WITH ( */
3234  else if (Matches("COPY|\\copy", MatchAny, "FROM|TO", MatchAny, "WITH", "("))
3235  COMPLETE_WITH("FORMAT", "FREEZE", "DELIMITER", "NULL",
3236  "HEADER", "QUOTE", "ESCAPE", "FORCE_QUOTE",
3237  "FORCE_NOT_NULL", "FORCE_NULL", "ENCODING", "DEFAULT",
3238  "ON_ERROR", "LOG_VERBOSITY");
3239 
3240  /* Complete COPY <sth> FROM|TO filename WITH (FORMAT */
3241  else if (Matches("COPY|\\copy", MatchAny, "FROM|TO", MatchAny, "WITH", "(", "FORMAT"))
3242  COMPLETE_WITH("binary", "csv", "text");
3243 
3244  /* Complete COPY <sth> FROM filename WITH (ON_ERROR */
3245  else if (Matches("COPY|\\copy", MatchAny, "FROM|TO", MatchAny, "WITH", "(", "ON_ERROR"))
3246  COMPLETE_WITH("stop", "ignore");
3247 
3248  /* Complete COPY <sth> FROM filename WITH (LOG_VERBOSITY */
3249  else if (Matches("COPY|\\copy", MatchAny, "FROM|TO", MatchAny, "WITH", "(", "LOG_VERBOSITY"))
3250  COMPLETE_WITH("silent", "default", "verbose");
3251 
3252  /* Complete COPY <sth> FROM <sth> WITH (<options>) */
3253  else if (Matches("COPY|\\copy", MatchAny, "FROM", MatchAny, "WITH", MatchAny))
3254  COMPLETE_WITH("WHERE");
3255 
3256  /* CREATE ACCESS METHOD */
3257  /* Complete "CREATE ACCESS METHOD <name>" */
3258  else if (Matches("CREATE", "ACCESS", "METHOD", MatchAny))
3259  COMPLETE_WITH("TYPE");
3260  /* Complete "CREATE ACCESS METHOD <name> TYPE" */
3261  else if (Matches("CREATE", "ACCESS", "METHOD", MatchAny, "TYPE"))
3262  COMPLETE_WITH("INDEX", "TABLE");
3263  /* Complete "CREATE ACCESS METHOD <name> TYPE <type>" */
3264  else if (Matches("CREATE", "ACCESS", "METHOD", MatchAny, "TYPE", MatchAny))
3265  COMPLETE_WITH("HANDLER");
3266 
3267  /* CREATE COLLATION */
3268  else if (Matches("CREATE", "COLLATION", MatchAny))
3269  COMPLETE_WITH("(", "FROM");
3270  else if (Matches("CREATE", "COLLATION", MatchAny, "FROM"))
3271  COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_collations);
3272  else if (HeadMatches("CREATE", "COLLATION", MatchAny, "(*"))
3273  {
3274  if (TailMatches("(|*,"))
3275  COMPLETE_WITH("LOCALE =", "LC_COLLATE =", "LC_CTYPE =",
3276  "PROVIDER =", "DETERMINISTIC =");
3277  else if (TailMatches("PROVIDER", "="))
3278  COMPLETE_WITH("libc", "icu");
3279  else if (TailMatches("DETERMINISTIC", "="))
3280  COMPLETE_WITH("true", "false");
3281  }
3282 
3283  /* CREATE DATABASE */
3284  else if (Matches("CREATE", "DATABASE", MatchAny))
3285  COMPLETE_WITH("OWNER", "TEMPLATE", "ENCODING", "TABLESPACE",
3286  "IS_TEMPLATE", "STRATEGY",
3287  "ALLOW_CONNECTIONS", "CONNECTION LIMIT",
3288  "LC_COLLATE", "LC_CTYPE", "LOCALE", "OID",
3289  "LOCALE_PROVIDER", "ICU_LOCALE");
3290 
3291  else if (Matches("CREATE", "DATABASE", MatchAny, "TEMPLATE"))
3292  COMPLETE_WITH_QUERY(Query_for_list_of_template_databases);
3293  else if (Matches("CREATE", "DATABASE", MatchAny, "STRATEGY"))
3294  COMPLETE_WITH("WAL_LOG", "FILE_COPY");
3295 
3296  /* CREATE DOMAIN */
3297  else if (Matches("CREATE", "DOMAIN", MatchAny))
3298  COMPLETE_WITH("AS");
3299  else if (Matches("CREATE", "DOMAIN", MatchAny, "AS"))
3300  COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_datatypes);
3301  else if (Matches("CREATE", "DOMAIN", MatchAny, "AS", MatchAny))
3302  COMPLETE_WITH("COLLATE", "DEFAULT", "CONSTRAINT",
3303  "NOT NULL", "NULL", "CHECK (");
3304  else if (Matches("CREATE", "DOMAIN", MatchAny, "COLLATE"))
3305  COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_collations);
3306 
3307  /* CREATE EXTENSION */
3308  /* Complete with available extensions rather than installed ones. */
3309  else if (Matches("CREATE", "EXTENSION"))
3310  COMPLETE_WITH_QUERY(Query_for_list_of_available_extensions);
3311  /* CREATE EXTENSION <name> */
3312  else if (Matches("CREATE", "EXTENSION", MatchAny))
3313  COMPLETE_WITH("WITH SCHEMA", "CASCADE", "VERSION");
3314  /* CREATE EXTENSION <name> VERSION */
3315  else if (Matches("CREATE", "EXTENSION", MatchAny, "VERSION"))
3316  {
3317  set_completion_reference(prev2_wd);
3318  COMPLETE_WITH_QUERY(Query_for_list_of_available_extension_versions);
3319  }
3320 
3321  /* CREATE FOREIGN */
3322  else if (Matches("CREATE", "FOREIGN"))
3323  COMPLETE_WITH("DATA WRAPPER", "TABLE");
3324 
3325  /* CREATE FOREIGN DATA WRAPPER */
3326  else if (Matches("CREATE", "FOREIGN", "DATA", "WRAPPER", MatchAny))
3327  COMPLETE_WITH("HANDLER", "VALIDATOR", "OPTIONS");
3328 
3329  /* CREATE FOREIGN TABLE */
3330  else if (Matches("CREATE", "FOREIGN", "TABLE", MatchAny))
3331  COMPLETE_WITH("(", "PARTITION OF");
3332 
3333  /* CREATE INDEX --- is allowed inside CREATE SCHEMA, so use TailMatches */
3334  /* First off we complete CREATE UNIQUE with "INDEX" */
3335  else if (TailMatches("CREATE", "UNIQUE"))
3336  COMPLETE_WITH("INDEX");
3337 
3338  /*
3339  * If we have CREATE|UNIQUE INDEX, then add "ON", "CONCURRENTLY", and
3340  * existing indexes
3341  */
3342  else if (TailMatches("CREATE|UNIQUE", "INDEX"))
3343  COMPLETE_WITH_SCHEMA_QUERY_PLUS(Query_for_list_of_indexes,
3344  "ON", "CONCURRENTLY");
3345 
3346  /*
3347  * Complete ... INDEX|CONCURRENTLY [<name>] ON with a list of relations
3348  * that indexes can be created on
3349  */
3350  else if (TailMatches("INDEX|CONCURRENTLY", MatchAny, "ON") ||
3351  TailMatches("INDEX|CONCURRENTLY", "ON"))
3352  COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_indexables);
3353 
3354  /*
3355  * Complete CREATE|UNIQUE INDEX CONCURRENTLY with "ON" and existing
3356  * indexes
3357  */
3358  else if (TailMatches("CREATE|UNIQUE", "INDEX", "CONCURRENTLY"))
3359  COMPLETE_WITH_SCHEMA_QUERY_PLUS(Query_for_list_of_indexes,
3360  "ON");
3361  /* Complete CREATE|UNIQUE INDEX [CONCURRENTLY] <sth> with "ON" */
3362  else if (TailMatches("CREATE|UNIQUE", "INDEX", MatchAny) ||
3363  TailMatches("CREATE|UNIQUE", "INDEX", "CONCURRENTLY", MatchAny))
3364  COMPLETE_WITH("ON");
3365 
3366  /*
3367  * Complete INDEX <name> ON <table> with a list of table columns (which
3368  * should really be in parens)
3369  */
3370  else if (TailMatches("INDEX", MatchAny, "ON", MatchAny) ||
3371  TailMatches("INDEX|CONCURRENTLY", "ON", MatchAny))
3372  COMPLETE_WITH("(", "USING");
3373  else if (TailMatches("INDEX", MatchAny, "ON", MatchAny, "(") ||
3374  TailMatches("INDEX|CONCURRENTLY", "ON", MatchAny, "("))
3375  COMPLETE_WITH_ATTR(prev2_wd);
3376  /* same if you put in USING */
3377  else if (TailMatches("ON", MatchAny, "USING", MatchAny, "("))
3378  COMPLETE_WITH_ATTR(prev4_wd);
3379  /* Complete USING with an index method */
3380  else if (TailMatches("INDEX", MatchAny, MatchAny, "ON", MatchAny, "USING") ||
3381  TailMatches("INDEX", MatchAny, "ON", MatchAny, "USING") ||
3382  TailMatches("INDEX", "ON", MatchAny, "USING"))
3383  COMPLETE_WITH_QUERY(Query_for_list_of_index_access_methods);
3384  else if (TailMatches("ON", MatchAny, "USING", MatchAny) &&
3385  !TailMatches("POLICY", MatchAny, MatchAny, MatchAny, MatchAny, MatchAny) &&
3386  !TailMatches("FOR", MatchAny, MatchAny, MatchAny))
3387  COMPLETE_WITH("(");
3388 
3389  /* CREATE OR REPLACE */
3390  else if (Matches("CREATE", "OR"))
3391  COMPLETE_WITH("REPLACE");
3392 
3393  /* CREATE POLICY */
3394  /* Complete "CREATE POLICY <name> ON" */
3395  else if (Matches("CREATE", "POLICY", MatchAny))
3396  COMPLETE_WITH("ON");
3397  /* Complete "CREATE POLICY <name> ON <table>" */
3398  else if (Matches("CREATE", "POLICY", MatchAny, "ON"))
3399  COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tables);
3400  /* Complete "CREATE POLICY <name> ON <table> AS|FOR|TO|USING|WITH CHECK" */
3401  else if (Matches("CREATE", "POLICY", MatchAny, "ON", MatchAny))
3402  COMPLETE_WITH("AS", "FOR", "TO", "USING (", "WITH CHECK (");
3403  /* CREATE POLICY <name> ON <table> AS PERMISSIVE|RESTRICTIVE */
3404  else if (Matches("CREATE", "POLICY", MatchAny, "ON", MatchAny, "AS"))
3405  COMPLETE_WITH("PERMISSIVE", "RESTRICTIVE");
3406 
3407  /*
3408  * CREATE POLICY <name> ON <table> AS PERMISSIVE|RESTRICTIVE
3409  * FOR|TO|USING|WITH CHECK
3410  */
3411  else if (Matches("CREATE", "POLICY", MatchAny, "ON", MatchAny, "AS", MatchAny))
3412  COMPLETE_WITH("FOR", "TO", "USING", "WITH CHECK");
3413  /* CREATE POLICY <name> ON <table> FOR ALL|SELECT|INSERT|UPDATE|DELETE */
3414  else if (Matches("CREATE", "POLICY", MatchAny, "ON", MatchAny, "FOR"))
3415  COMPLETE_WITH("ALL", "SELECT", "INSERT", "UPDATE", "DELETE");
3416  /* Complete "CREATE POLICY <name> ON <table> FOR INSERT TO|WITH CHECK" */
3417  else if (Matches("CREATE", "POLICY", MatchAny, "ON", MatchAny, "FOR", "INSERT"))
3418  COMPLETE_WITH("TO", "WITH CHECK (");
3419  /* Complete "CREATE POLICY <name> ON <table> FOR SELECT|DELETE TO|USING" */
3420  else if (Matches("CREATE", "POLICY", MatchAny, "ON", MatchAny, "FOR", "SELECT|DELETE"))
3421  COMPLETE_WITH("TO", "USING (");
3422  /* CREATE POLICY <name> ON <table> FOR ALL|UPDATE TO|USING|WITH CHECK */
3423  else if (Matches("CREATE", "POLICY", MatchAny, "ON", MatchAny, "FOR", "ALL|UPDATE"))
3424  COMPLETE_WITH("TO", "USING (", "WITH CHECK (");
3425  /* Complete "CREATE POLICY <name> ON <table> TO <role>" */
3426  else if (Matches("CREATE", "POLICY", MatchAny, "ON", MatchAny, "TO"))
3427  COMPLETE_WITH_QUERY_PLUS(Query_for_list_of_roles,
3428  Keywords_for_list_of_grant_roles);
3429  /* Complete "CREATE POLICY <name> ON <table> USING (" */
3430  else if (Matches("CREATE", "POLICY", MatchAny, "ON", MatchAny, "USING"))
3431  COMPLETE_WITH("(");
3432 
3433  /*
3434  * CREATE POLICY <name> ON <table> AS PERMISSIVE|RESTRICTIVE FOR
3435  * ALL|SELECT|INSERT|UPDATE|DELETE
3436  */
3437  else if (Matches("CREATE", "POLICY", MatchAny, "ON", MatchAny, "AS", MatchAny, "FOR"))
3438  COMPLETE_WITH("ALL", "SELECT", "INSERT", "UPDATE", "DELETE");
3439 
3440  /*
3441  * Complete "CREATE POLICY <name> ON <table> AS PERMISSIVE|RESTRICTIVE FOR
3442  * INSERT TO|WITH CHECK"
3443  */
3444  else if (Matches("CREATE", "POLICY", MatchAny, "ON", MatchAny, "AS", MatchAny, "FOR", "INSERT"))
3445  COMPLETE_WITH("TO", "WITH CHECK (");
3446 
3447  /*
3448  * Complete "CREATE POLICY <name> ON <table> AS PERMISSIVE|RESTRICTIVE FOR
3449  * SELECT|DELETE TO|USING"
3450  */
3451  else if (Matches("CREATE", "POLICY", MatchAny, "ON", MatchAny, "AS", MatchAny, "FOR", "SELECT|DELETE"))
3452  COMPLETE_WITH("TO", "USING (");
3453 
3454  /*
3455  * CREATE POLICY <name> ON <table> AS PERMISSIVE|RESTRICTIVE FOR
3456  * ALL|UPDATE TO|USING|WITH CHECK
3457  */
3458  else if (Matches("CREATE", "POLICY", MatchAny, "ON", MatchAny, "AS", MatchAny, "FOR", "ALL|UPDATE"))
3459  COMPLETE_WITH("TO", "USING (", "WITH CHECK (");
3460 
3461  /*
3462  * Complete "CREATE POLICY <name> ON <table> AS PERMISSIVE|RESTRICTIVE TO
3463  * <role>"
3464  */
3465  else if (Matches("CREATE", "POLICY", MatchAny, "ON", MatchAny, "AS", MatchAny, "TO"))
3466  COMPLETE_WITH_QUERY_PLUS(Query_for_list_of_roles,
3467  Keywords_for_list_of_grant_roles);
3468 
3469  /*
3470  * Complete "CREATE POLICY <name> ON <table> AS PERMISSIVE|RESTRICTIVE
3471  * USING ("
3472  */
3473  else if (Matches("CREATE", "POLICY", MatchAny, "ON", MatchAny, "AS", MatchAny, "USING"))
3474  COMPLETE_WITH("(");
3475 
3476 
3477 /* CREATE PUBLICATION */
3478  else if (Matches("CREATE", "PUBLICATION", MatchAny))
3479  COMPLETE_WITH("FOR TABLE", "FOR ALL TABLES", "FOR TABLES IN SCHEMA", "WITH (");
3480  else if (Matches("CREATE", "PUBLICATION", MatchAny, "FOR"))
3481  COMPLETE_WITH("TABLE", "ALL TABLES", "TABLES IN SCHEMA");
3482  else if (Matches("CREATE", "PUBLICATION", MatchAny, "FOR", "ALL"))
3483  COMPLETE_WITH("TABLES");
3484  else if (Matches("CREATE", "PUBLICATION", MatchAny, "FOR", "ALL", "TABLES"))
3485  COMPLETE_WITH("WITH (");
3486  else if (Matches("CREATE", "PUBLICATION", MatchAny, "FOR", "TABLES"))
3487  COMPLETE_WITH("IN SCHEMA");
3488  else if (Matches("CREATE", "PUBLICATION", MatchAny, "FOR", "TABLE", MatchAny) && !ends_with(prev_wd, ','))
3489  COMPLETE_WITH("WHERE (", "WITH (");
3490  /* Complete "CREATE PUBLICATION <name> FOR TABLE" with "<table>, ..." */
3491  else if (Matches("CREATE", "PUBLICATION", MatchAny, "FOR", "TABLE"))
3492  COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tables);
3493 
3494  /*
3495  * "CREATE PUBLICATION <name> FOR TABLE <name> WHERE (" - complete with
3496  * table attributes
3497  */
3498  else if (Matches("CREATE", "PUBLICATION", MatchAny, MatchAnyN, "WHERE"))
3499  COMPLETE_WITH("(");
3500  else if (Matches("CREATE", "PUBLICATION", MatchAny, MatchAnyN, "WHERE", "("))
3501  COMPLETE_WITH_ATTR(prev3_wd);
3502  else if (Matches("CREATE", "PUBLICATION", MatchAny, MatchAnyN, "WHERE", "(*)"))
3503  COMPLETE_WITH(" WITH (");
3504 
3505  /*
3506  * Complete "CREATE PUBLICATION <name> FOR TABLES IN SCHEMA <schema>, ..."
3507  */
3508  else if (Matches("CREATE", "PUBLICATION", MatchAny, "FOR", "TABLES", "IN", "SCHEMA"))
3509  COMPLETE_WITH_QUERY_PLUS(Query_for_list_of_schemas
3510  " AND nspname NOT LIKE E'pg\\\\_%%'",
3511  "CURRENT_SCHEMA");
3512  else if (Matches("CREATE", "PUBLICATION", MatchAny, "FOR", "TABLES", "IN", "SCHEMA", MatchAny) && (!ends_with(prev_wd, ',')))
3513  COMPLETE_WITH("WITH (");
3514  /* Complete "CREATE PUBLICATION <name> [...] WITH" */
3515  else if (Matches("CREATE", "PUBLICATION", MatchAnyN, "WITH", "("))
3516  COMPLETE_WITH("publish", "publish_generated_columns", "publish_via_partition_root");
3517 
3518 /* CREATE RULE */
3519  /* Complete "CREATE [ OR REPLACE ] RULE <sth>" with "AS ON" */
3520  else if (Matches("CREATE", "RULE", MatchAny) ||
3521  Matches("CREATE", "OR", "REPLACE", "RULE", MatchAny))
3522  COMPLETE_WITH("AS ON");
3523  /* Complete "CREATE [ OR REPLACE ] RULE <sth> AS" with "ON" */
3524  else if (Matches("CREATE", "RULE", MatchAny, "AS") ||
3525  Matches("CREATE", "OR", "REPLACE", "RULE", MatchAny, "AS"))
3526  COMPLETE_WITH("ON");
3527 
3528  /*
3529  * Complete "CREATE [ OR REPLACE ] RULE <sth> AS ON" with
3530  * SELECT|UPDATE|INSERT|DELETE
3531  */
3532  else if (Matches("CREATE", "RULE", MatchAny, "AS", "ON") ||
3533  Matches("CREATE", "OR", "REPLACE", "RULE", MatchAny, "AS", "ON"))
3534  COMPLETE_WITH("SELECT", "UPDATE", "INSERT", "DELETE");
3535  /* Complete "AS ON SELECT|UPDATE|INSERT|DELETE" with a "TO" */
3536  else if (TailMatches("AS", "ON", "SELECT|UPDATE|INSERT|DELETE"))
3537  COMPLETE_WITH("TO");
3538  /* Complete "AS ON <sth> TO" with a table name */
3539  else if (TailMatches("AS", "ON", "SELECT|UPDATE|INSERT|DELETE", "TO"))
3540  COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tables);
3541 
3542 /* CREATE SCHEMA [ <name> ] [ AUTHORIZATION ] */
3543  else if (Matches("CREATE", "SCHEMA"))
3544  COMPLETE_WITH_QUERY_PLUS(Query_for_list_of_schemas,
3545  "AUTHORIZATION");
3546  else if (Matches("CREATE", "SCHEMA", "AUTHORIZATION") ||
3547  Matches("CREATE", "SCHEMA", MatchAny, "AUTHORIZATION"))
3548  COMPLETE_WITH_QUERY_PLUS(Query_for_list_of_roles,
3549  Keywords_for_list_of_owner_roles);
3550  else if (Matches("CREATE", "SCHEMA", "AUTHORIZATION", MatchAny) ||
3551  Matches("CREATE", "SCHEMA", MatchAny, "AUTHORIZATION", MatchAny))
3552  COMPLETE_WITH("CREATE", "GRANT");
3553  else if (Matches("CREATE", "SCHEMA", MatchAny))
3554  COMPLETE_WITH("AUTHORIZATION", "CREATE", "GRANT");
3555 
3556 /* CREATE SEQUENCE --- is allowed inside CREATE SCHEMA, so use TailMatches */
3557  else if (TailMatches("CREATE", "SEQUENCE", MatchAny) ||
3558  TailMatches("CREATE", "TEMP|TEMPORARY", "SEQUENCE", MatchAny))
3559  COMPLETE_WITH("AS", "INCREMENT BY", "MINVALUE", "MAXVALUE", "NO",
3560  "CACHE", "CYCLE", "OWNED BY", "START WITH");
3561  else if (TailMatches("CREATE", "SEQUENCE", MatchAny, "AS") ||
3562  TailMatches("CREATE", "TEMP|TEMPORARY", "SEQUENCE", MatchAny, "AS"))
3563  COMPLETE_WITH_CS("smallint", "integer", "bigint");
3564  else if (TailMatches("CREATE", "SEQUENCE", MatchAny, "NO") ||
3565  TailMatches("CREATE", "TEMP|TEMPORARY", "SEQUENCE", MatchAny, "NO"))
3566  COMPLETE_WITH("MINVALUE", "MAXVALUE", "CYCLE");
3567 
3568 /* CREATE SERVER <name> */
3569  else if (Matches("CREATE", "SERVER", MatchAny))
3570  COMPLETE_WITH("TYPE", "VERSION", "FOREIGN DATA WRAPPER");
3571 
3572 /* CREATE STATISTICS <name> */
3573  else if (Matches("CREATE", "STATISTICS", MatchAny))
3574  COMPLETE_WITH("(", "ON");
3575  else if (Matches("CREATE", "STATISTICS", MatchAny, "("))
3576  COMPLETE_WITH("ndistinct", "dependencies", "mcv");
3577  else if (Matches("CREATE", "STATISTICS", MatchAny, "(*)"))
3578  COMPLETE_WITH("ON");
3579  else if (Matches("CREATE", "STATISTICS", MatchAny, MatchAnyN, "FROM"))
3580  COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tables);
3581 
3582 /* CREATE TABLE --- is allowed inside CREATE SCHEMA, so use TailMatches */
3583  /* Complete "CREATE TEMP/TEMPORARY" with the possible temp objects */
3584  else if (TailMatches("CREATE", "TEMP|TEMPORARY"))
3585  COMPLETE_WITH("SEQUENCE", "TABLE", "VIEW");
3586  /* Complete "CREATE UNLOGGED" with TABLE or SEQUENCE */
3587  else if (TailMatches("CREATE", "UNLOGGED"))
3588  COMPLETE_WITH("TABLE", "SEQUENCE");
3589  /* Complete PARTITION BY with RANGE ( or LIST ( or ... */
3590  else if (TailMatches("PARTITION", "BY"))
3591  COMPLETE_WITH("RANGE (", "LIST (", "HASH (");
3592  /* If we have xxx PARTITION OF, provide a list of partitioned tables */
3593  else if (TailMatches("PARTITION", "OF"))
3594  COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_partitioned_tables);
3595  /* Limited completion support for partition bound specification */
3596  else if (TailMatches("PARTITION", "OF", MatchAny))
3597  COMPLETE_WITH("FOR VALUES", "DEFAULT");
3598  /* Complete CREATE TABLE <name> with '(', AS, OF or PARTITION OF */
3599  else if (TailMatches("CREATE", "TABLE", MatchAny) ||
3600  TailMatches("CREATE", "TEMP|TEMPORARY|UNLOGGED", "TABLE", MatchAny))
3601  COMPLETE_WITH("(", "AS", "OF", "PARTITION OF");
3602  /* Complete CREATE TABLE <name> OF with list of composite types */
3603  else if (TailMatches("CREATE", "TABLE", MatchAny, "OF") ||
3604  TailMatches("CREATE", "TEMP|TEMPORARY|UNLOGGED", "TABLE", MatchAny, "OF"))
3605  COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_composite_datatypes);
3606  /* Complete CREATE TABLE <name> [ (...) ] AS with list of keywords */
3607  else if (TailMatches("CREATE", "TABLE", MatchAny, "AS") ||
3608  TailMatches("CREATE", "TABLE", MatchAny, "(*)", "AS") ||
3609  TailMatches("CREATE", "TEMP|TEMPORARY|UNLOGGED", "TABLE", MatchAny, "AS") ||
3610  TailMatches("CREATE", "TEMP|TEMPORARY|UNLOGGED", "TABLE", MatchAny, "(*)", "AS"))
3611  COMPLETE_WITH("EXECUTE", "SELECT", "TABLE", "VALUES", "WITH");
3612  /* Complete CREATE TABLE name (...) with supported options */
3613  else if (TailMatches("CREATE", "TABLE", MatchAny, "(*)") ||
3614  TailMatches("CREATE", "UNLOGGED", "TABLE", MatchAny, "(*)"))
3615  COMPLETE_WITH("AS", "INHERITS (", "PARTITION BY", "USING", "TABLESPACE", "WITH (");
3616  else if (TailMatches("CREATE", "TEMP|TEMPORARY", "TABLE", MatchAny, "(*)"))
3617  COMPLETE_WITH("AS", "INHERITS (", "ON COMMIT", "PARTITION BY",
3618  "TABLESPACE", "WITH (");
3619  /* Complete CREATE TABLE (...) USING with table access methods */
3620  else if (TailMatches("CREATE", "TABLE", MatchAny, "(*)", "USING") ||
3621  TailMatches("CREATE", "TEMP|TEMPORARY|UNLOGGED", "TABLE", MatchAny, "(*)", "USING"))
3622  COMPLETE_WITH_QUERY(Query_for_list_of_table_access_methods);
3623  /* Complete CREATE TABLE (...) WITH with storage parameters */
3624  else if (TailMatches("CREATE", "TABLE", MatchAny, "(*)", "WITH", "(") ||
3625  TailMatches("CREATE", "TEMP|TEMPORARY|UNLOGGED", "TABLE", MatchAny, "(*)", "WITH", "("))
3626  COMPLETE_WITH_LIST(table_storage_parameters);
3627  /* Complete CREATE TABLE ON COMMIT with actions */
3628  else if (TailMatches("CREATE", "TEMP|TEMPORARY", "TABLE", MatchAny, "(*)", "ON", "COMMIT"))
3629  COMPLETE_WITH("DELETE ROWS", "DROP", "PRESERVE ROWS");
3630 
3631 /* CREATE TABLESPACE */
3632  else if (Matches("CREATE", "TABLESPACE", MatchAny))
3633  COMPLETE_WITH("OWNER", "LOCATION");
3634  /* Complete CREATE TABLESPACE name OWNER name with "LOCATION" */
3635  else if (Matches("CREATE", "TABLESPACE", MatchAny, "OWNER", MatchAny))
3636  COMPLETE_WITH("LOCATION");
3637 
3638 /* CREATE TEXT SEARCH */
3639  else if (Matches("CREATE", "TEXT", "SEARCH"))
3640  COMPLETE_WITH("CONFIGURATION", "DICTIONARY", "PARSER", "TEMPLATE");
3641  else if (Matches("CREATE", "TEXT", "SEARCH", "CONFIGURATION|DICTIONARY|PARSER|TEMPLATE", MatchAny))
3642  COMPLETE_WITH("(");
3643 
3644 /* CREATE TRANSFORM */
3645  else if (Matches("CREATE", "TRANSFORM") ||
3646  Matches("CREATE", "OR", "REPLACE", "TRANSFORM"))
3647  COMPLETE_WITH("FOR");
3648  else if (Matches("CREATE", "TRANSFORM", "FOR") ||
3649  Matches("CREATE", "OR", "REPLACE", "TRANSFORM", "FOR"))
3650  COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_datatypes);
3651  else if (Matches("CREATE", "TRANSFORM", "FOR", MatchAny) ||
3652  Matches("CREATE", "OR", "REPLACE", "TRANSFORM", "FOR", MatchAny))
3653  COMPLETE_WITH("LANGUAGE");
3654  else if (Matches("CREATE", "TRANSFORM", "FOR", MatchAny, "LANGUAGE") ||
3655  Matches("CREATE", "OR", "REPLACE", "TRANSFORM", "FOR", MatchAny, "LANGUAGE"))
3656  {
3657  set_completion_reference(prev2_wd);
3658  COMPLETE_WITH_QUERY(Query_for_list_of_languages);
3659  }
3660 
3661 /* CREATE SUBSCRIPTION */
3662  else if (Matches("CREATE", "SUBSCRIPTION", MatchAny))
3663  COMPLETE_WITH("CONNECTION");
3664  else if (Matches("CREATE", "SUBSCRIPTION", MatchAny, "CONNECTION", MatchAny))
3665  COMPLETE_WITH("PUBLICATION");
3666  else if (Matches("CREATE", "SUBSCRIPTION", MatchAny, "CONNECTION",
3667  MatchAny, "PUBLICATION"))
3668  {
3669  /* complete with nothing here as this refers to remote publications */
3670  }
3671  else if (Matches("CREATE", "SUBSCRIPTION", MatchAnyN, "PUBLICATION", MatchAny))
3672  COMPLETE_WITH("WITH (");
3673  /* Complete "CREATE SUBSCRIPTION <name> ... WITH ( <opt>" */
3674  else if (Matches("CREATE", "SUBSCRIPTION", MatchAnyN, "WITH", "("))
3675  COMPLETE_WITH("binary", "connect", "copy_data", "create_slot",
3676  "disable_on_error", "enabled", "failover", "origin",
3677  "password_required", "run_as_owner", "slot_name",
3678  "streaming", "synchronous_commit", "two_phase");
3679 
3680 /* CREATE TRIGGER --- is allowed inside CREATE SCHEMA, so use TailMatches */
3681 
3682  /*
3683  * Complete CREATE [ OR REPLACE ] TRIGGER <name> with BEFORE|AFTER|INSTEAD
3684  * OF.
3685  */
3686  else if (TailMatches("CREATE", "TRIGGER", MatchAny) ||
3687  TailMatches("CREATE", "OR", "REPLACE", "TRIGGER", MatchAny))
3688  COMPLETE_WITH("BEFORE", "AFTER", "INSTEAD OF");
3689 
3690  /*
3691  * Complete CREATE [ OR REPLACE ] TRIGGER <name> BEFORE,AFTER with an
3692  * event.
3693  */
3694  else if (TailMatches("CREATE", "TRIGGER", MatchAny, "BEFORE|AFTER") ||
3695  TailMatches("CREATE", "OR", "REPLACE", "TRIGGER", MatchAny, "BEFORE|AFTER"))
3696  COMPLETE_WITH("INSERT", "DELETE", "UPDATE", "TRUNCATE");
3697  /* Complete CREATE [ OR REPLACE ] TRIGGER <name> INSTEAD OF with an event */
3698  else if (TailMatches("CREATE", "TRIGGER", MatchAny, "INSTEAD", "OF") ||
3699  TailMatches("CREATE", "OR", "REPLACE", "TRIGGER", MatchAny, "INSTEAD", "OF"))
3700  COMPLETE_WITH("INSERT", "DELETE", "UPDATE");
3701 
3702  /*
3703  * Complete CREATE [ OR REPLACE ] TRIGGER <name> BEFORE,AFTER sth with
3704  * OR|ON.
3705  */
3706  else if (TailMatches("CREATE", "TRIGGER", MatchAny, "BEFORE|AFTER", MatchAny) ||
3707  TailMatches("CREATE", "OR", "REPLACE", "TRIGGER", MatchAny, "BEFORE|AFTER", MatchAny) ||
3708  TailMatches("CREATE", "TRIGGER", MatchAny, "INSTEAD", "OF", MatchAny) ||
3709  TailMatches("CREATE", "OR", "REPLACE", "TRIGGER", MatchAny, "INSTEAD", "OF", MatchAny))
3710  COMPLETE_WITH("ON", "OR");
3711 
3712  /*
3713  * Complete CREATE [ OR REPLACE ] TRIGGER <name> BEFORE,AFTER event ON
3714  * with a list of tables. EXECUTE FUNCTION is the recommended grammar
3715  * instead of EXECUTE PROCEDURE in version 11 and upwards.
3716  */
3717  else if (TailMatches("CREATE", "TRIGGER", MatchAny, "BEFORE|AFTER", MatchAny, "ON") ||
3718  TailMatches("CREATE", "OR", "REPLACE", "TRIGGER", MatchAny, "BEFORE|AFTER", MatchAny, "ON"))
3719  COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tables);
3720 
3721  /*
3722  * Complete CREATE [ OR REPLACE ] TRIGGER ... INSTEAD OF event ON with a
3723  * list of views.
3724  */
3725  else if (TailMatches("CREATE", "TRIGGER", MatchAny, "INSTEAD", "OF", MatchAny, "ON") ||
3726  TailMatches("CREATE", "OR", "REPLACE", "TRIGGER", MatchAny, "INSTEAD", "OF", MatchAny, "ON"))
3727  COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_views);
3728  else if (Matches("CREATE", "TRIGGER", MatchAnyN,
3729  "ON", MatchAny) ||
3730  Matches("CREATE", "OR", "REPLACE", "TRIGGER", MatchAnyN,
3731  "ON", MatchAny))
3732  {
3733  if (pset.sversion >= 110000)
3734  COMPLETE_WITH("NOT DEFERRABLE", "DEFERRABLE", "INITIALLY",
3735  "REFERENCING", "FOR", "WHEN (", "EXECUTE FUNCTION");
3736  else
3737  COMPLETE_WITH("NOT DEFERRABLE", "DEFERRABLE", "INITIALLY",
3738  "REFERENCING", "FOR", "WHEN (", "EXECUTE PROCEDURE");
3739  }
3740  else if (Matches("CREATE", "TRIGGER", MatchAnyN,
3741  "DEFERRABLE") ||
3742  Matches("CREATE", "OR", "REPLACE", "TRIGGER", MatchAnyN,
3743  "DEFERRABLE") ||
3744  Matches("CREATE", "TRIGGER", MatchAnyN,
3745  "INITIALLY", "IMMEDIATE|DEFERRED") ||
3746  Matches("CREATE", "OR", "REPLACE", "TRIGGER", MatchAnyN,
3747  "INITIALLY", "IMMEDIATE|DEFERRED"))
3748  {
3749  if (pset.sversion >= 110000)
3750  COMPLETE_WITH("REFERENCING", "FOR", "WHEN (", "EXECUTE FUNCTION");
3751  else
3752  COMPLETE_WITH("REFERENCING", "FOR", "WHEN (", "EXECUTE PROCEDURE");
3753  }
3754  else if (Matches("CREATE", "TRIGGER", MatchAnyN,
3755  "REFERENCING") ||
3756  Matches("CREATE", "OR", "REPLACE", "TRIGGER", MatchAnyN,
3757  "REFERENCING"))
3758  COMPLETE_WITH("OLD TABLE", "NEW TABLE");
3759  else if (Matches("CREATE", "TRIGGER", MatchAnyN,
3760  "OLD|NEW", "TABLE") ||
3761  Matches("CREATE", "OR", "REPLACE", "TRIGGER", MatchAnyN,
3762  "OLD|NEW", "TABLE"))
3763  COMPLETE_WITH("AS");
3764  else if (Matches("CREATE", "TRIGGER", MatchAnyN,
3765  "REFERENCING", "OLD", "TABLE", "AS", MatchAny) ||
3766  Matches("CREATE", "OR", "REPLACE", "TRIGGER", MatchAnyN,
3767  "REFERENCING", "OLD", "TABLE", "AS", MatchAny) ||
3768  Matches("CREATE", "TRIGGER", MatchAnyN,
3769  "REFERENCING", "OLD", "TABLE", MatchAny) ||
3770  Matches("CREATE", "OR", "REPLACE", "TRIGGER", MatchAnyN,
3771  "REFERENCING", "OLD", "TABLE", MatchAny))
3772  {
3773  if (pset.sversion >= 110000)
3774  COMPLETE_WITH("NEW TABLE", "FOR", "WHEN (", "EXECUTE FUNCTION");
3775  else
3776  COMPLETE_WITH("NEW TABLE", "FOR", "WHEN (", "EXECUTE PROCEDURE");
3777  }
3778  else if (Matches("CREATE", "TRIGGER", MatchAnyN,
3779  "REFERENCING", "NEW", "TABLE", "AS", MatchAny) ||
3780  Matches("CREATE", "OR", "REPLACE", "TRIGGER", MatchAnyN,
3781  "REFERENCING", "NEW", "TABLE", "AS", MatchAny) ||
3782  Matches("CREATE", "TRIGGER", MatchAnyN,
3783  "REFERENCING", "NEW", "TABLE", MatchAny) ||
3784  Matches("CREATE", "OR", "REPLACE", "TRIGGER", MatchAnyN,
3785  "REFERENCING", "NEW", "TABLE", MatchAny))
3786  {
3787  if (pset.sversion >= 110000)
3788  COMPLETE_WITH("OLD TABLE", "FOR", "WHEN (", "EXECUTE FUNCTION");
3789  else
3790  COMPLETE_WITH("OLD TABLE", "FOR", "WHEN (", "EXECUTE PROCEDURE");
3791  }
3792  else if (Matches("CREATE", "TRIGGER", MatchAnyN,
3793  "REFERENCING", "OLD|NEW", "TABLE", "AS", MatchAny, "OLD|NEW", "TABLE", "AS", MatchAny) ||
3794  Matches("CREATE", "OR", "REPLACE", "TRIGGER", MatchAnyN,
3795  "REFERENCING", "OLD|NEW", "TABLE", "AS", MatchAny, "OLD|NEW", "TABLE", "AS", MatchAny) ||
3796  Matches("CREATE", "TRIGGER", MatchAnyN,
3797  "REFERENCING", "OLD|NEW", "TABLE", MatchAny, "OLD|NEW", "TABLE", "AS", MatchAny) ||
3798  Matches("CREATE", "OR", "REPLACE", "TRIGGER", MatchAnyN,
3799  "REFERENCING", "OLD|NEW", "TABLE", MatchAny, "OLD|NEW", "TABLE", "AS", MatchAny) ||
3800  Matches("CREATE", "TRIGGER", MatchAnyN,
3801  "REFERENCING", "OLD|NEW", "TABLE", "AS", MatchAny, "OLD|NEW", "TABLE", MatchAny) ||
3802  Matches("CREATE", "OR", "REPLACE", "TRIGGER", MatchAnyN,
3803  "REFERENCING", "OLD|NEW", "TABLE", "AS", MatchAny, "OLD|NEW", "TABLE", MatchAny) ||
3804  Matches("CREATE", "TRIGGER", MatchAnyN,
3805  "REFERENCING", "OLD|NEW", "TABLE", MatchAny, "OLD|NEW", "TABLE", MatchAny) ||
3806  Matches("CREATE", "OR", "REPLACE", "TRIGGER", MatchAnyN,
3807  "REFERENCING", "OLD|NEW", "TABLE", MatchAny, "OLD|NEW", "TABLE", MatchAny))
3808  {
3809  if (pset.sversion >= 110000)
3810  COMPLETE_WITH("FOR", "WHEN (", "EXECUTE FUNCTION");
3811  else
3812  COMPLETE_WITH("FOR", "WHEN (", "EXECUTE PROCEDURE");
3813  }
3814  else if (Matches("CREATE", "TRIGGER", MatchAnyN,
3815  "FOR") ||
3816  Matches("CREATE", "OR", "REPLACE", "TRIGGER", MatchAnyN,
3817  "FOR"))
3818  COMPLETE_WITH("EACH", "ROW", "STATEMENT");
3819  else if (Matches("CREATE", "TRIGGER", MatchAnyN,
3820  "FOR", "EACH") ||
3821  Matches("CREATE", "OR", "REPLACE", "TRIGGER", MatchAnyN,
3822  "FOR", "EACH"))
3823  COMPLETE_WITH("ROW", "STATEMENT");
3824  else if (Matches("CREATE", "TRIGGER", MatchAnyN,
3825  "FOR", "EACH", "ROW|STATEMENT") ||
3826  Matches("CREATE", "OR", "REPLACE", "TRIGGER", MatchAnyN,
3827  "FOR", "EACH", "ROW|STATEMENT") ||
3828  Matches("CREATE", "TRIGGER", MatchAnyN,
3829  "FOR", "ROW|STATEMENT") ||
3830  Matches("CREATE", "OR", "REPLACE", "TRIGGER", MatchAnyN,
3831  "FOR", "ROW|STATEMENT"))
3832  {
3833  if (pset.sversion >= 110000)
3834  COMPLETE_WITH("WHEN (", "EXECUTE FUNCTION");
3835  else
3836  COMPLETE_WITH("WHEN (", "EXECUTE PROCEDURE");
3837  }
3838  else if (Matches("CREATE", "TRIGGER", MatchAnyN,
3839  "WHEN", "(*)") ||
3840  Matches("CREATE", "OR", "REPLACE", "TRIGGER", MatchAnyN,
3841  "WHEN", "(*)"))
3842  {
3843  if (pset.sversion >= 110000)
3844  COMPLETE_WITH("EXECUTE FUNCTION");
3845  else
3846  COMPLETE_WITH("EXECUTE PROCEDURE");
3847  }
3848 
3849  /*
3850  * Complete CREATE [ OR REPLACE ] TRIGGER ... EXECUTE with
3851  * PROCEDURE|FUNCTION.
3852  */
3853  else if (Matches("CREATE", "TRIGGER", MatchAnyN,
3854  "EXECUTE") ||
3855  Matches("CREATE", "OR", "REPLACE", "TRIGGER", MatchAnyN,
3856  "EXECUTE"))
3857  {
3858  if (pset.sversion >= 110000)
3859  COMPLETE_WITH("FUNCTION");
3860  else
3861  COMPLETE_WITH("PROCEDURE");
3862  }
3863  else if (Matches("CREATE", "TRIGGER", MatchAnyN,
3864  "EXECUTE", "FUNCTION|PROCEDURE") ||
3865  Matches("CREATE", "OR", "REPLACE", "TRIGGER", MatchAnyN,
3866  "EXECUTE", "FUNCTION|PROCEDURE"))
3867  COMPLETE_WITH_VERSIONED_SCHEMA_QUERY(Query_for_list_of_functions);
3868 
3869 /* CREATE ROLE,USER,GROUP <name> */
3870  else if (Matches("CREATE", "ROLE|GROUP|USER", MatchAny) &&
3871  !TailMatches("USER", "MAPPING"))
3872  COMPLETE_WITH("ADMIN", "BYPASSRLS", "CONNECTION LIMIT", "CREATEDB",
3873  "CREATEROLE", "ENCRYPTED PASSWORD", "IN", "INHERIT",
3874  "LOGIN", "NOBYPASSRLS",
3875  "NOCREATEDB", "NOCREATEROLE", "NOINHERIT",
3876  "NOLOGIN", "NOREPLICATION", "NOSUPERUSER", "PASSWORD",
3877  "REPLICATION", "ROLE", "SUPERUSER", "SYSID",
3878  "VALID UNTIL", "WITH");
3879 
3880 /* CREATE ROLE,USER,GROUP <name> WITH */
3881  else if (Matches("CREATE", "ROLE|GROUP|USER", MatchAny, "WITH"))
3882  /* Similar to the above, but don't complete "WITH" again. */
3883  COMPLETE_WITH("ADMIN", "BYPASSRLS", "CONNECTION LIMIT", "CREATEDB",
3884  "CREATEROLE", "ENCRYPTED PASSWORD", "IN", "INHERIT",
3885  "LOGIN", "NOBYPASSRLS",
3886  "NOCREATEDB", "NOCREATEROLE", "NOINHERIT",
3887  "NOLOGIN", "NOREPLICATION", "NOSUPERUSER", "PASSWORD",
3888  "REPLICATION", "ROLE", "SUPERUSER", "SYSID",
3889  "VALID UNTIL");
3890 
3891  /* complete CREATE ROLE,USER,GROUP <name> IN with ROLE,GROUP */
3892  else if (Matches("CREATE", "ROLE|USER|GROUP", MatchAny, "IN"))
3893  COMPLETE_WITH("GROUP", "ROLE");
3894 
3895 /* CREATE TYPE */
3896  else if (Matches("CREATE", "TYPE", MatchAny))
3897  COMPLETE_WITH("(", "AS");
3898  else if (Matches("CREATE", "TYPE", MatchAny, "AS"))
3899  COMPLETE_WITH("ENUM", "RANGE", "(");
3900  else if (HeadMatches("CREATE", "TYPE", MatchAny, "AS", "("))
3901  {
3902  if (TailMatches("(|*,", MatchAny))
3903  COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_datatypes);
3904  else if (TailMatches("(|*,", MatchAny, MatchAnyExcept("*)")))
3905  COMPLETE_WITH("COLLATE", ",", ")");
3906  }
3907  else if (Matches("CREATE", "TYPE", MatchAny, "AS", "ENUM|RANGE"))
3908  COMPLETE_WITH("(");
3909  else if (HeadMatches("CREATE", "TYPE", MatchAny, "("))
3910  {
3911  if (TailMatches("(|*,"))
3912  COMPLETE_WITH("INPUT", "OUTPUT", "RECEIVE", "SEND",
3913  "TYPMOD_IN", "TYPMOD_OUT", "ANALYZE", "SUBSCRIPT",
3914  "INTERNALLENGTH", "PASSEDBYVALUE", "ALIGNMENT",
3915  "STORAGE", "LIKE", "CATEGORY", "PREFERRED",
3916  "DEFAULT", "ELEMENT", "DELIMITER",
3917  "COLLATABLE");
3918  else if (TailMatches("(*|*,", MatchAnyExcept("*=")))
3919  COMPLETE_WITH("=");
3920  else if (TailMatches("=", MatchAnyExcept("*)")))
3921  COMPLETE_WITH(",", ")");
3922  }
3923  else if (HeadMatches("CREATE", "TYPE", MatchAny, "AS", "RANGE", "("))
3924  {
3925  if (TailMatches("(|*,"))
3926  COMPLETE_WITH("SUBTYPE", "SUBTYPE_OPCLASS", "COLLATION",
3927  "CANONICAL", "SUBTYPE_DIFF",
3928  "MULTIRANGE_TYPE_NAME");
3929  else if (TailMatches("(*|*,", MatchAnyExcept("*=")))
3930  COMPLETE_WITH("=");
3931  else if (TailMatches("=", MatchAnyExcept("*)")))
3932  COMPLETE_WITH(",", ")");
3933  }
3934 
3935 /* CREATE VIEW --- is allowed inside CREATE SCHEMA, so use TailMatches */
3936  /* Complete CREATE [ OR REPLACE ] VIEW <name> with AS or WITH */
3937  else if (TailMatches("CREATE", "VIEW", MatchAny) ||
3938  TailMatches("CREATE", "OR", "REPLACE", "VIEW", MatchAny))
3939  COMPLETE_WITH("AS", "WITH");
3940  /* Complete "CREATE [ OR REPLACE ] VIEW <sth> AS with "SELECT" */
3941  else if (TailMatches("CREATE", "VIEW", MatchAny, "AS") ||
3942  TailMatches("CREATE", "OR", "REPLACE", "VIEW", MatchAny, "AS"))
3943  COMPLETE_WITH("SELECT");
3944  /* CREATE [ OR REPLACE ] VIEW <name> WITH ( yyy [= zzz] ) */
3945  else if (TailMatches("CREATE", "VIEW", MatchAny, "WITH") ||
3946  TailMatches("CREATE", "OR", "REPLACE", "VIEW", MatchAny, "WITH"))
3947  COMPLETE_WITH("(");
3948  else if (TailMatches("CREATE", "VIEW", MatchAny, "WITH", "(") ||
3949  TailMatches("CREATE", "OR", "REPLACE", "VIEW", MatchAny, "WITH", "("))
3950  COMPLETE_WITH_LIST(view_optional_parameters);
3951  else if (TailMatches("CREATE", "VIEW", MatchAny, "WITH", "(", "check_option") ||
3952  TailMatches("CREATE", "OR", "REPLACE", "VIEW", MatchAny, "WITH", "(", "check_option"))
3953  COMPLETE_WITH("=");
3954  else if (TailMatches("CREATE", "VIEW", MatchAny, "WITH", "(", "check_option", "=") ||
3955  TailMatches("CREATE", "OR", "REPLACE", "VIEW", MatchAny, "WITH", "(", "check_option", "="))
3956  COMPLETE_WITH("local", "cascaded");
3957  /* CREATE [ OR REPLACE ] VIEW <name> WITH ( ... ) AS */
3958  else if (TailMatches("CREATE", "VIEW", MatchAny, "WITH", "(*)") ||
3959  TailMatches("CREATE", "OR", "REPLACE", "VIEW", MatchAny, "WITH", "(*)"))
3960  COMPLETE_WITH("AS");
3961  /* CREATE [ OR REPLACE ] VIEW <name> WITH ( ... ) AS SELECT */
3962  else if (TailMatches("CREATE", "VIEW", MatchAny, "WITH", "(*)", "AS") ||
3963  TailMatches("CREATE", "OR", "REPLACE", "VIEW", MatchAny, "WITH", "(*)", "AS"))
3964  COMPLETE_WITH("SELECT");
3965 
3966 /* CREATE MATERIALIZED VIEW */
3967  else if (Matches("CREATE", "MATERIALIZED"))
3968  COMPLETE_WITH("VIEW");
3969  /* Complete CREATE MATERIALIZED VIEW <name> with AS */
3970  else if (Matches("CREATE", "MATERIALIZED", "VIEW", MatchAny))
3971  COMPLETE_WITH("AS");
3972  /* Complete "CREATE MATERIALIZED VIEW <sth> AS with "SELECT" */
3973  else if (Matches("CREATE", "MATERIALIZED", "VIEW", MatchAny, "AS"))
3974  COMPLETE_WITH("SELECT");
3975 
3976 /* CREATE EVENT TRIGGER */
3977  else if (Matches("CREATE", "EVENT"))
3978  COMPLETE_WITH("TRIGGER");
3979  /* Complete CREATE EVENT TRIGGER <name> with ON */
3980  else if (Matches("CREATE", "EVENT", "TRIGGER", MatchAny))
3981  COMPLETE_WITH("ON");
3982  /* Complete CREATE EVENT TRIGGER <name> ON with event_type */
3983  else if (Matches("CREATE", "EVENT", "TRIGGER", MatchAny, "ON"))
3984  COMPLETE_WITH("ddl_command_start", "ddl_command_end", "login",
3985  "sql_drop", "table_rewrite");
3986 
3987  /*
3988  * Complete CREATE EVENT TRIGGER <name> ON <event_type>. EXECUTE FUNCTION
3989  * is the recommended grammar instead of EXECUTE PROCEDURE in version 11
3990  * and upwards.
3991  */
3992  else if (Matches("CREATE", "EVENT", "TRIGGER", MatchAny, "ON", MatchAny))
3993  {
3994  if (pset.sversion >= 110000)
3995  COMPLETE_WITH("WHEN TAG IN (", "EXECUTE FUNCTION");
3996  else
3997  COMPLETE_WITH("WHEN TAG IN (", "EXECUTE PROCEDURE");
3998  }
3999  else if (Matches("CREATE", "EVENT", "TRIGGER", MatchAnyN, "WHEN|AND", MatchAny, "IN", "(*)"))
4000  {
4001  if (pset.sversion >= 110000)
4002  COMPLETE_WITH("EXECUTE FUNCTION");
4003  else
4004  COMPLETE_WITH("EXECUTE PROCEDURE");
4005  }
4006  else if (Matches("CREATE", "EVENT", "TRIGGER", MatchAnyN, "EXECUTE", "FUNCTION|PROCEDURE"))
4007  COMPLETE_WITH_VERSIONED_SCHEMA_QUERY(Query_for_list_of_functions);
4008 
4009 /* DEALLOCATE */
4010  else if (Matches("DEALLOCATE"))
4011  COMPLETE_WITH_QUERY_PLUS(Query_for_list_of_prepared_statements,
4012  "ALL");
4013 
4014 /* DECLARE */
4015 
4016  /*
4017  * Complete DECLARE <name> with one of BINARY, ASENSITIVE, INSENSITIVE,
4018  * SCROLL, NO SCROLL, and CURSOR.
4019  */
4020  else if (Matches("DECLARE", MatchAny))
4021  COMPLETE_WITH("BINARY", "ASENSITIVE", "INSENSITIVE", "SCROLL", "NO SCROLL",
4022  "CURSOR");
4023 
4024  /*
4025  * Complete DECLARE ... <option> with other options. The PostgreSQL parser
4026  * allows DECLARE options to be specified in any order. But the
4027  * tab-completion follows the ordering of them that the SQL standard
4028  * provides, like the syntax of DECLARE command in the documentation
4029  * indicates.
4030  */
4031  else if (Matches("DECLARE", MatchAnyN, "BINARY"))
4032  COMPLETE_WITH("ASENSITIVE", "INSENSITIVE", "SCROLL", "NO SCROLL", "CURSOR");
4033  else if (Matches("DECLARE", MatchAnyN, "ASENSITIVE|INSENSITIVE"))
4034  COMPLETE_WITH("SCROLL", "NO SCROLL", "CURSOR");
4035  else if (Matches("DECLARE", MatchAnyN, "SCROLL"))
4036  COMPLETE_WITH("CURSOR");
4037  /* Complete DECLARE ... [options] NO with SCROLL */
4038  else if (Matches("DECLARE", MatchAnyN, "NO"))
4039  COMPLETE_WITH("SCROLL");
4040 
4041  /*
4042  * Complete DECLARE ... CURSOR with one of WITH HOLD, WITHOUT HOLD, and
4043  * FOR
4044  */
4045  else if (Matches("DECLARE", MatchAnyN, "CURSOR"))
4046  COMPLETE_WITH("WITH HOLD", "WITHOUT HOLD", "FOR");
4047  /* Complete DECLARE ... CURSOR WITH|WITHOUT with HOLD */
4048  else if (Matches("DECLARE", MatchAnyN, "CURSOR", "WITH|WITHOUT"))
4049  COMPLETE_WITH("HOLD");
4050  /* Complete DECLARE ... CURSOR WITH|WITHOUT HOLD with FOR */
4051  else if (Matches("DECLARE", MatchAnyN, "CURSOR", "WITH|WITHOUT", "HOLD"))
4052  COMPLETE_WITH("FOR");
4053 
4054 /* DELETE --- can be inside EXPLAIN, RULE, etc */
4055  /* Complete DELETE with "FROM" */
4056  else if (Matches("DELETE"))
4057  COMPLETE_WITH("FROM");
4058  /* Complete DELETE FROM with a list of tables */
4059  else if (TailMatches("DELETE", "FROM"))
4060  COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_updatables);
4061  /* Complete DELETE FROM <table> */
4062  else if (TailMatches("DELETE", "FROM", MatchAny))
4063  COMPLETE_WITH("USING", "WHERE");
4064  /* XXX: implement tab completion for DELETE ... USING */
4065 
4066 /* DISCARD */
4067  else if (Matches("DISCARD"))
4068  COMPLETE_WITH("ALL", "PLANS", "SEQUENCES", "TEMP");
4069 
4070 /* DO */
4071  else if (Matches("DO"))
4072  COMPLETE_WITH("LANGUAGE");
4073 
4074 /* DROP */
4075  /* Complete DROP object with CASCADE / RESTRICT */
4076  else if (Matches("DROP",
4077  "COLLATION|CONVERSION|DOMAIN|EXTENSION|LANGUAGE|PUBLICATION|SCHEMA|SEQUENCE|SERVER|SUBSCRIPTION|STATISTICS|TABLE|TYPE|VIEW",
4078  MatchAny) ||
4079  Matches("DROP", "ACCESS", "METHOD", MatchAny) ||
4080  Matches("DROP", "EVENT", "TRIGGER", MatchAny) ||
4081  Matches("DROP", "FOREIGN", "DATA", "WRAPPER", MatchAny) ||
4082  Matches("DROP", "FOREIGN", "TABLE", MatchAny) ||
4083  Matches("DROP", "TEXT", "SEARCH", "CONFIGURATION|DICTIONARY|PARSER|TEMPLATE", MatchAny))
4084  COMPLETE_WITH("CASCADE", "RESTRICT");
4085  else if (Matches("DROP", "AGGREGATE|FUNCTION|PROCEDURE|ROUTINE", MatchAny, MatchAny) &&
4086  ends_with(prev_wd, ')'))
4087  COMPLETE_WITH("CASCADE", "RESTRICT");
4088 
4089  /* help completing some of the variants */
4090  else if (Matches("DROP", "AGGREGATE|FUNCTION|PROCEDURE|ROUTINE", MatchAny))
4091  COMPLETE_WITH("(");
4092  else if (Matches("DROP", "AGGREGATE|FUNCTION|PROCEDURE|ROUTINE", MatchAny, "("))
4093  COMPLETE_WITH_FUNCTION_ARG(prev2_wd);
4094  else if (Matches("DROP", "FOREIGN"))
4095  COMPLETE_WITH("DATA WRAPPER", "TABLE");
4096  else if (Matches("DROP", "DATABASE", MatchAny))
4097  COMPLETE_WITH("WITH (");
4098  else if (HeadMatches("DROP", "DATABASE") && (ends_with(prev_wd, '(')))
4099  COMPLETE_WITH("FORCE");
4100 
4101  /* DROP INDEX */
4102  else if (Matches("DROP", "INDEX"))
4103  COMPLETE_WITH_SCHEMA_QUERY_PLUS(Query_for_list_of_indexes,
4104  "CONCURRENTLY");
4105  else if (Matches("DROP", "INDEX", "CONCURRENTLY"))
4106  COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_indexes);
4107  else if (Matches("DROP", "INDEX", MatchAny))
4108  COMPLETE_WITH("CASCADE", "RESTRICT");
4109  else if (Matches("DROP", "INDEX", "CONCURRENTLY", MatchAny))
4110  COMPLETE_WITH("CASCADE", "RESTRICT");
4111 
4112  /* DROP MATERIALIZED VIEW */
4113  else if (Matches("DROP", "MATERIALIZED"))
4114  COMPLETE_WITH("VIEW");
4115  else if (Matches("DROP", "MATERIALIZED", "VIEW"))
4116  COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_matviews);
4117  else if (Matches("DROP", "MATERIALIZED", "VIEW", MatchAny))
4118  COMPLETE_WITH("CASCADE", "RESTRICT");
4119 
4120  /* DROP OWNED BY */
4121  else if (Matches("DROP", "OWNED"))
4122  COMPLETE_WITH("BY");
4123  else if (Matches("DROP", "OWNED", "BY"))
4124  COMPLETE_WITH_QUERY(Query_for_list_of_roles);
4125  else if (Matches("DROP", "OWNED", "BY", MatchAny))
4126  COMPLETE_WITH("CASCADE", "RESTRICT");
4127 
4128  /* DROP TEXT SEARCH */
4129  else if (Matches("DROP", "TEXT", "SEARCH"))
4130  COMPLETE_WITH("CONFIGURATION", "DICTIONARY", "PARSER", "TEMPLATE");
4131 
4132  /* DROP TRIGGER */
4133  else if (Matches("DROP", "TRIGGER", MatchAny))
4134  COMPLETE_WITH("ON");
4135  else if (Matches("DROP", "TRIGGER", MatchAny, "ON"))
4136  {
4137  set_completion_reference(prev2_wd);
4138  COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tables_for_trigger);
4139  }
4140  else if (Matches("DROP", "TRIGGER", MatchAny, "ON", MatchAny))
4141  COMPLETE_WITH("CASCADE", "RESTRICT");
4142 
4143  /* DROP ACCESS METHOD */
4144  else if (Matches("DROP", "ACCESS"))
4145  COMPLETE_WITH("METHOD");
4146  else if (Matches("DROP", "ACCESS", "METHOD"))
4147  COMPLETE_WITH_QUERY(Query_for_list_of_access_methods);
4148 
4149  /* DROP EVENT TRIGGER */
4150  else if (Matches("DROP", "EVENT"))
4151  COMPLETE_WITH("TRIGGER");
4152  else if (Matches("DROP", "EVENT", "TRIGGER"))
4153  COMPLETE_WITH_QUERY(Query_for_list_of_event_triggers);
4154 
4155  /* DROP POLICY <name> */
4156  else if (Matches("DROP", "POLICY"))
4157  COMPLETE_WITH_QUERY(Query_for_list_of_policies);
4158  /* DROP POLICY <name> ON */
4159  else if (Matches("DROP", "POLICY", MatchAny))
4160  COMPLETE_WITH("ON");
4161  /* DROP POLICY <name> ON <table> */
4162  else if (Matches("DROP", "POLICY", MatchAny, "ON"))
4163  {
4164  set_completion_reference(prev2_wd);
4165  COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tables_for_policy);
4166  }
4167  else if (Matches("DROP", "POLICY", MatchAny, "ON", MatchAny))
4168  COMPLETE_WITH("CASCADE", "RESTRICT");
4169 
4170  /* DROP RULE */
4171  else if (Matches("DROP", "RULE", MatchAny))
4172  COMPLETE_WITH("ON");
4173  else if (Matches("DROP", "RULE", MatchAny, "ON"))
4174  {
4175  set_completion_reference(prev2_wd);
4176  COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tables_for_rule);
4177  }
4178  else if (Matches("DROP", "RULE", MatchAny, "ON", MatchAny))
4179  COMPLETE_WITH("CASCADE", "RESTRICT");
4180 
4181  /* DROP TRANSFORM */
4182  else if (Matches("DROP", "TRANSFORM"))
4183  COMPLETE_WITH("FOR");
4184  else if (Matches("DROP", "TRANSFORM", "FOR"))
4185  COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_datatypes);
4186  else if (Matches("DROP", "TRANSFORM", "FOR", MatchAny))
4187  COMPLETE_WITH("LANGUAGE");
4188  else if (Matches("DROP", "TRANSFORM", "FOR", MatchAny, "LANGUAGE"))
4189  {
4190  set_completion_reference(prev2_wd);
4191  COMPLETE_WITH_QUERY(Query_for_list_of_languages);
4192  }
4193  else if (Matches("DROP", "TRANSFORM", "FOR", MatchAny, "LANGUAGE", MatchAny))
4194  COMPLETE_WITH("CASCADE", "RESTRICT");
4195 
4196 /* EXECUTE */
4197  else if (Matches("EXECUTE"))
4198  COMPLETE_WITH_QUERY(Query_for_list_of_prepared_statements);
4199 
4200 /*
4201  * EXPLAIN [ ( option [, ...] ) ] statement
4202  * EXPLAIN [ ANALYZE ] [ VERBOSE ] statement
4203  */
4204  else if (Matches("EXPLAIN"))
4205  COMPLETE_WITH("SELECT", "INSERT INTO", "DELETE FROM", "UPDATE", "DECLARE",
4206  "MERGE INTO", "EXECUTE", "ANALYZE", "VERBOSE");
4207  else if (HeadMatches("EXPLAIN", "(*") &&
4208  !HeadMatches("EXPLAIN", "(*)"))
4209  {
4210  /*
4211  * This fires if we're in an unfinished parenthesized option list.
4212  * get_previous_words treats a completed parenthesized option list as
4213  * one word, so the above test is correct.
4214  */
4215  if (ends_with(prev_wd, '(') || ends_with(prev_wd, ','))
4216  COMPLETE_WITH("ANALYZE", "VERBOSE", "COSTS", "SETTINGS", "GENERIC_PLAN",
4217  "BUFFERS", "SERIALIZE", "WAL", "TIMING", "SUMMARY",
4218  "MEMORY", "FORMAT");
4219  else if (TailMatches("ANALYZE|VERBOSE|COSTS|SETTINGS|GENERIC_PLAN|BUFFERS|WAL|TIMING|SUMMARY|MEMORY"))
4220  COMPLETE_WITH("ON", "OFF");
4221  else if (TailMatches("SERIALIZE"))
4222  COMPLETE_WITH("TEXT", "NONE", "BINARY");
4223  else if (TailMatches("FORMAT"))
4224  COMPLETE_WITH("TEXT", "XML", "JSON", "YAML");
4225  }
4226  else if (Matches("EXPLAIN", "ANALYZE"))
4227  COMPLETE_WITH("SELECT", "INSERT INTO", "DELETE FROM", "UPDATE", "DECLARE",
4228  "MERGE INTO", "EXECUTE", "VERBOSE");
4229  else if (Matches("EXPLAIN", "(*)") ||
4230  Matches("EXPLAIN", "VERBOSE") ||
4231  Matches("EXPLAIN", "ANALYZE", "VERBOSE"))
4232  COMPLETE_WITH("SELECT", "INSERT INTO", "DELETE FROM", "UPDATE", "DECLARE",
4233  "MERGE INTO", "EXECUTE");
4234 
4235 /* FETCH && MOVE */
4236 
4237  /*
4238  * Complete FETCH with one of ABSOLUTE, BACKWARD, FORWARD, RELATIVE, ALL,
4239  * NEXT, PRIOR, FIRST, LAST, FROM, IN, and a list of cursors
4240  */
4241  else if (Matches("FETCH|MOVE"))
4242  COMPLETE_WITH_QUERY_PLUS(Query_for_list_of_cursors,
4243  "ABSOLUTE",
4244  "BACKWARD",
4245  "FORWARD",
4246  "RELATIVE",
4247  "ALL",
4248  "NEXT",
4249  "PRIOR",
4250  "FIRST",
4251  "LAST",
4252  "FROM",
4253  "IN");
4254 
4255  /*
4256  * Complete FETCH BACKWARD or FORWARD with one of ALL, FROM, IN, and a
4257  * list of cursors
4258  */
4259  else if (Matches("FETCH|MOVE", "BACKWARD|FORWARD"))
4260  COMPLETE_WITH_QUERY_PLUS(Query_for_list_of_cursors,
4261  "ALL",
4262  "FROM",
4263  "IN");
4264 
4265  /*
4266  * Complete FETCH <direction> with "FROM" or "IN". These are equivalent,
4267  * but we may as well tab-complete both: perhaps some users prefer one
4268  * variant or the other.
4269  */
4270  else if (Matches("FETCH|MOVE", "ABSOLUTE|BACKWARD|FORWARD|RELATIVE",
4271  MatchAnyExcept("FROM|IN")) ||
4272  Matches("FETCH|MOVE", "ALL|NEXT|PRIOR|FIRST|LAST"))
4273  COMPLETE_WITH_QUERY_PLUS(Query_for_list_of_cursors,
4274  "FROM",
4275  "IN");
4276  /* Complete FETCH <direction> "FROM" or "IN" with a list of cursors */
4277  else if (Matches("FETCH|MOVE", MatchAnyN, "FROM|IN"))
4278  COMPLETE_WITH_QUERY(Query_for_list_of_cursors);
4279 
4280 /* FOREIGN DATA WRAPPER */
4281  /* applies in ALTER/DROP FDW and in CREATE SERVER */
4282  else if (TailMatches("FOREIGN", "DATA", "WRAPPER") &&
4283  !TailMatches("CREATE", MatchAny, MatchAny, MatchAny))
4284  COMPLETE_WITH_QUERY(Query_for_list_of_fdws);
4285  /* applies in CREATE SERVER */
4286  else if (Matches("CREATE", "SERVER", MatchAnyN, "FOREIGN", "DATA", "WRAPPER", MatchAny))
4287  COMPLETE_WITH("OPTIONS");
4288 
4289 /* FOREIGN TABLE */
4290  else if (TailMatches("FOREIGN", "TABLE") &&
4291  !TailMatches("CREATE", MatchAny, MatchAny))
4292  COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_foreign_tables);
4293 
4294 /* FOREIGN SERVER */
4295  else if (TailMatches("FOREIGN", "SERVER"))
4296  COMPLETE_WITH_QUERY(Query_for_list_of_servers);
4297 
4298 /*
4299  * GRANT and REVOKE are allowed inside CREATE SCHEMA and
4300  * ALTER DEFAULT PRIVILEGES, so use TailMatches
4301  */
4302  /* Complete GRANT/REVOKE with a list of roles and privileges */
4303  else if (TailMatches("GRANT|REVOKE") ||
4304  TailMatches("REVOKE", "ADMIN|GRANT|INHERIT|SET", "OPTION", "FOR"))
4305  {
4306  /*
4307  * With ALTER DEFAULT PRIVILEGES, restrict completion to grantable
4308  * privileges (can't grant roles)
4309  */
4310  if (HeadMatches("ALTER", "DEFAULT", "PRIVILEGES"))
4311  {
4312  if (TailMatches("GRANT") ||
4313  TailMatches("REVOKE", "GRANT", "OPTION", "FOR"))
4314  COMPLETE_WITH("SELECT", "INSERT", "UPDATE",
4315  "DELETE", "TRUNCATE", "REFERENCES", "TRIGGER",
4316  "CREATE", "EXECUTE", "USAGE", "MAINTAIN", "ALL");
4317  else if (TailMatches("REVOKE"))
4318  COMPLETE_WITH("SELECT", "INSERT", "UPDATE",
4319  "DELETE", "TRUNCATE", "REFERENCES", "TRIGGER",
4320  "CREATE", "EXECUTE", "USAGE", "MAINTAIN", "ALL",
4321  "GRANT OPTION FOR");
4322  }
4323  else if (TailMatches("GRANT"))
4324  COMPLETE_WITH_QUERY_PLUS(Query_for_list_of_roles,
4325  Privilege_options_of_grant_and_revoke);
4326  else if (TailMatches("REVOKE"))
4327  COMPLETE_WITH_QUERY_PLUS(Query_for_list_of_roles,
4328  Privilege_options_of_grant_and_revoke,
4329  "GRANT OPTION FOR",
4330  "ADMIN OPTION FOR",
4331  "INHERIT OPTION FOR",
4332  "SET OPTION FOR");
4333  else if (TailMatches("REVOKE", "GRANT", "OPTION", "FOR"))
4334  COMPLETE_WITH(Privilege_options_of_grant_and_revoke);
4335  else if (TailMatches("REVOKE", "ADMIN|INHERIT|SET", "OPTION", "FOR"))
4336  COMPLETE_WITH_QUERY(Query_for_list_of_roles);
4337  }
4338 
4339  else if (TailMatches("GRANT|REVOKE", "ALTER") ||
4340  TailMatches("REVOKE", "GRANT", "OPTION", "FOR", "ALTER"))
4341  COMPLETE_WITH("SYSTEM");
4342 
4343  else if (TailMatches("REVOKE", "SET"))
4344  COMPLETE_WITH("ON PARAMETER", "OPTION FOR");
4345  else if (TailMatches("GRANT", "SET") ||
4346  TailMatches("REVOKE", "GRANT", "OPTION", "FOR", "SET") ||
4347  TailMatches("GRANT|REVOKE", "ALTER", "SYSTEM") ||
4348  TailMatches("REVOKE", "GRANT", "OPTION", "FOR", "ALTER", "SYSTEM"))
4349  COMPLETE_WITH("ON PARAMETER");
4350 
4351  else if (TailMatches("GRANT|REVOKE", MatchAny, "ON", "PARAMETER") ||
4352  TailMatches("GRANT|REVOKE", MatchAny, MatchAny, "ON", "PARAMETER") ||
4353  TailMatches("REVOKE", "GRANT", "OPTION", "FOR", MatchAny, "ON", "PARAMETER") ||
4354  TailMatches("REVOKE", "GRANT", "OPTION", "FOR", MatchAny, MatchAny, "ON", "PARAMETER"))
4355  COMPLETE_WITH_QUERY_VERBATIM(Query_for_list_of_alter_system_set_vars);
4356 
4357  else if (TailMatches("GRANT", MatchAny, "ON", "PARAMETER", MatchAny) ||
4358  TailMatches("GRANT", MatchAny, MatchAny, "ON", "PARAMETER", MatchAny))
4359  COMPLETE_WITH("TO");
4360 
4361  else if (TailMatches("REVOKE", MatchAny, "ON", "PARAMETER", MatchAny) ||
4362  TailMatches("REVOKE", MatchAny, MatchAny, "ON", "PARAMETER", MatchAny) ||
4363  TailMatches("REVOKE", "GRANT", "OPTION", "FOR", MatchAny, "ON", "PARAMETER", MatchAny) ||
4364  TailMatches("REVOKE", "GRANT", "OPTION", "FOR", MatchAny, MatchAny, "ON", "PARAMETER", MatchAny))
4365  COMPLETE_WITH("FROM");
4366 
4367  /*
4368  * Complete GRANT/REVOKE <privilege> with "ON", GRANT/REVOKE <role> with
4369  * TO/FROM
4370  */
4371  else if (TailMatches("GRANT|REVOKE", MatchAny) ||
4372  TailMatches("REVOKE", "GRANT", "OPTION", "FOR", MatchAny))
4373  {
4374  if (TailMatches("SELECT|INSERT|UPDATE|DELETE|TRUNCATE|REFERENCES|TRIGGER|CREATE|CONNECT|TEMPORARY|TEMP|EXECUTE|USAGE|MAINTAIN|ALL"))
4375  COMPLETE_WITH("ON");
4376  else if (TailMatches("GRANT", MatchAny))
4377  COMPLETE_WITH("TO");
4378  else
4379  COMPLETE_WITH("FROM");
4380  }
4381 
4382  /*
4383  * Complete GRANT/REVOKE <sth> ON with a list of appropriate relations.
4384  *
4385  * Note: GRANT/REVOKE can get quite complex; tab-completion as implemented
4386  * here will only work if the privilege list contains exactly one
4387  * privilege.
4388  */
4389  else if (TailMatches("GRANT|REVOKE", MatchAny, "ON") ||
4390  TailMatches("REVOKE", "GRANT", "OPTION", "FOR", MatchAny, "ON"))
4391  {
4392  /*
4393  * With ALTER DEFAULT PRIVILEGES, restrict completion to the kinds of
4394  * objects supported.
4395  */
4396  if (HeadMatches("ALTER", "DEFAULT", "PRIVILEGES"))
4397  COMPLETE_WITH("TABLES", "SEQUENCES", "FUNCTIONS", "PROCEDURES", "ROUTINES", "TYPES", "SCHEMAS");
4398  else
4399  COMPLETE_WITH_SCHEMA_QUERY_PLUS(Query_for_list_of_grantables,
4400  "ALL FUNCTIONS IN SCHEMA",
4401  "ALL PROCEDURES IN SCHEMA",
4402  "ALL ROUTINES IN SCHEMA",
4403  "ALL SEQUENCES IN SCHEMA",
4404  "ALL TABLES IN SCHEMA",
4405  "DATABASE",
4406  "DOMAIN",
4407  "FOREIGN DATA WRAPPER",
4408  "FOREIGN SERVER",
4409  "FUNCTION",
4410  "LANGUAGE",
4411  "LARGE OBJECT",
4412  "PARAMETER",
4413  "PROCEDURE",
4414  "ROUTINE",
4415  "SCHEMA",
4416  "SEQUENCE",
4417  "TABLE",
4418  "TABLESPACE",
4419  "TYPE");
4420  }
4421  else if (TailMatches("GRANT|REVOKE", MatchAny, "ON", "ALL") ||
4422  TailMatches("REVOKE", "GRANT", "OPTION", "FOR", MatchAny, "ON", "ALL"))
4423  COMPLETE_WITH("FUNCTIONS IN SCHEMA",
4424  "PROCEDURES IN SCHEMA",
4425  "ROUTINES IN SCHEMA",
4426  "SEQUENCES IN SCHEMA",
4427  "TABLES IN SCHEMA");
4428  else if (TailMatches("GRANT|REVOKE", MatchAny, "ON", "FOREIGN") ||
4429  TailMatches("REVOKE", "GRANT", "OPTION", "FOR", MatchAny, "ON", "FOREIGN"))
4430  COMPLETE_WITH("DATA WRAPPER", "SERVER");
4431 
4432  /*
4433  * Complete "GRANT/REVOKE * ON DATABASE/DOMAIN/..." with a list of
4434  * appropriate objects.
4435  *
4436  * Complete "GRANT/REVOKE * ON *" with "TO/FROM".
4437  */
4438  else if (TailMatches("GRANT|REVOKE", MatchAny, "ON", MatchAny) ||
4439  TailMatches("REVOKE", "GRANT", "OPTION", "FOR", MatchAny, "ON", MatchAny))
4440  {
4441  if (TailMatches("DATABASE"))
4442  COMPLETE_WITH_QUERY(Query_for_list_of_databases);
4443  else if (TailMatches("DOMAIN"))
4444  COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_domains);
4445  else if (TailMatches("FUNCTION"))
4446  COMPLETE_WITH_VERSIONED_SCHEMA_QUERY(Query_for_list_of_functions);
4447  else if (TailMatches("LANGUAGE"))
4448  COMPLETE_WITH_QUERY(Query_for_list_of_languages);
4449  else if (TailMatches("PROCEDURE"))
4450  COMPLETE_WITH_VERSIONED_SCHEMA_QUERY(Query_for_list_of_procedures);
4451  else if (TailMatches("ROUTINE"))
4452  COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_routines);
4453  else if (TailMatches("SCHEMA"))
4454  COMPLETE_WITH_QUERY(Query_for_list_of_schemas);
4455  else if (TailMatches("SEQUENCE"))
4456  COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_sequences);
4457  else if (TailMatches("TABLE"))
4458  COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_grantables);
4459  else if (TailMatches("TABLESPACE"))
4460  COMPLETE_WITH_QUERY(Query_for_list_of_tablespaces);
4461  else if (TailMatches("TYPE"))
4462  COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_datatypes);
4463  else if (TailMatches("GRANT", MatchAny, MatchAny, MatchAny))
4464  COMPLETE_WITH("TO");
4465  else
4466  COMPLETE_WITH("FROM");
4467  }
4468 
4469  /*
4470  * Complete "GRANT/REVOKE ... TO/FROM" with username, PUBLIC,
4471  * CURRENT_ROLE, CURRENT_USER, or SESSION_USER.
4472  */
4473  else if (Matches("GRANT", MatchAnyN, "TO") ||
4474  Matches("REVOKE", MatchAnyN, "FROM"))
4475  COMPLETE_WITH_QUERY_PLUS(Query_for_list_of_roles,
4476  Keywords_for_list_of_grant_roles);
4477 
4478  /*
4479  * Offer grant options after that.
4480  */
4481  else if (Matches("GRANT", MatchAnyN, "TO", MatchAny))
4482  COMPLETE_WITH("WITH ADMIN",
4483  "WITH INHERIT",
4484  "WITH SET",
4485  "WITH GRANT OPTION",
4486  "GRANTED BY");
4487  else if (Matches("GRANT", MatchAnyN, "TO", MatchAny, "WITH"))
4488  COMPLETE_WITH("ADMIN",
4489  "INHERIT",
4490  "SET",
4491  "GRANT OPTION");
4492  else if (Matches("GRANT", MatchAnyN, "TO", MatchAny, "WITH", "ADMIN|INHERIT|SET"))
4493  COMPLETE_WITH("OPTION", "TRUE", "FALSE");
4494  else if (Matches("GRANT", MatchAnyN, "TO", MatchAny, "WITH", MatchAny, "OPTION"))
4495  COMPLETE_WITH("GRANTED BY");
4496  else if (Matches("GRANT", MatchAnyN, "TO", MatchAny, "WITH", MatchAny, "OPTION", "GRANTED", "BY"))
4497  COMPLETE_WITH_QUERY_PLUS(Query_for_list_of_roles,
4498  Keywords_for_list_of_grant_roles);
4499  /* Complete "ALTER DEFAULT PRIVILEGES ... GRANT/REVOKE ... TO/FROM */
4500  else if (Matches("ALTER", "DEFAULT", "PRIVILEGES", MatchAnyN, "TO|FROM"))
4501  COMPLETE_WITH_QUERY_PLUS(Query_for_list_of_roles,
4502  Keywords_for_list_of_grant_roles);
4503  /* Offer WITH GRANT OPTION after that */
4504  else if (Matches("ALTER", "DEFAULT", "PRIVILEGES", MatchAnyN, "TO", MatchAny))
4505  COMPLETE_WITH("WITH GRANT OPTION");
4506  /* Complete "GRANT/REVOKE ... ON * *" with TO/FROM */
4507  else if (Matches("GRANT", MatchAnyN, "ON", MatchAny, MatchAny))
4508  COMPLETE_WITH("TO");
4509  else if (Matches("REVOKE", MatchAnyN, "ON", MatchAny, MatchAny))
4510  COMPLETE_WITH("FROM");
4511 
4512  /* Complete "GRANT/REVOKE * ON ALL * IN SCHEMA *" with TO/FROM */
4513  else if (TailMatches("GRANT|REVOKE", MatchAny, "ON", "ALL", MatchAny, "IN", "SCHEMA", MatchAny) ||
4514  TailMatches("REVOKE", "GRANT", "OPTION", "FOR", MatchAny, "ON", "ALL", MatchAny, "IN", "SCHEMA", MatchAny))
4515  {
4516  if (TailMatches("GRANT", MatchAny, MatchAny, MatchAny, MatchAny, MatchAny, MatchAny, MatchAny))
4517  COMPLETE_WITH("TO");
4518  else
4519  COMPLETE_WITH("FROM");
4520  }
4521 
4522  /* Complete "GRANT/REVOKE * ON FOREIGN DATA WRAPPER *" with TO/FROM */
4523  else if (TailMatches("GRANT|REVOKE", MatchAny, "ON", "FOREIGN", "DATA", "WRAPPER", MatchAny) ||
4524  TailMatches("REVOKE", "GRANT", "OPTION", "FOR", MatchAny, "ON", "FOREIGN", "DATA", "WRAPPER", MatchAny))
4525  {
4526  if (TailMatches("GRANT", MatchAny, MatchAny, MatchAny, MatchAny, MatchAny, MatchAny))
4527  COMPLETE_WITH("TO");
4528  else
4529  COMPLETE_WITH("FROM");
4530  }
4531 
4532  /* Complete "GRANT/REVOKE * ON FOREIGN SERVER *" with TO/FROM */
4533  else if (TailMatches("GRANT|REVOKE", MatchAny, "ON", "FOREIGN", "SERVER", MatchAny) ||
4534  TailMatches("REVOKE", "GRANT", "OPTION", "FOR", MatchAny, "ON", "FOREIGN", "SERVER", MatchAny))
4535  {
4536  if (TailMatches("GRANT", MatchAny, MatchAny, MatchAny, MatchAny, MatchAny))
4537  COMPLETE_WITH("TO");
4538  else
4539  COMPLETE_WITH("FROM");
4540  }
4541 
4542 /* GROUP BY */
4543  else if (TailMatches("FROM", MatchAny, "GROUP"))
4544  COMPLETE_WITH("BY");
4545 
4546 /* IMPORT FOREIGN SCHEMA */
4547  else if (Matches("IMPORT"))
4548  COMPLETE_WITH("FOREIGN SCHEMA");
4549  else if (Matches("IMPORT", "FOREIGN"))
4550  COMPLETE_WITH("SCHEMA");
4551  else if (Matches("IMPORT", "FOREIGN", "SCHEMA", MatchAny))
4552  COMPLETE_WITH("EXCEPT (", "FROM SERVER", "LIMIT TO (");
4553  else if (TailMatches("LIMIT", "TO", "(*)") ||
4554  TailMatches("EXCEPT", "(*)"))
4555  COMPLETE_WITH("FROM SERVER");
4556  else if (TailMatches("FROM", "SERVER", MatchAny))
4557  COMPLETE_WITH("INTO");
4558  else if (TailMatches("FROM", "SERVER", MatchAny, "INTO"))
4559  COMPLETE_WITH_QUERY(Query_for_list_of_schemas);
4560  else if (TailMatches("FROM", "SERVER", MatchAny, "INTO", MatchAny))
4561  COMPLETE_WITH("OPTIONS (");
4562 
4563 /* INSERT --- can be inside EXPLAIN, RULE, etc */
4564  /* Complete NOT MATCHED THEN INSERT */
4565  else if (TailMatches("NOT", "MATCHED", "THEN", "INSERT"))
4566  COMPLETE_WITH("VALUES", "(");
4567  /* Complete INSERT with "INTO" */
4568  else if (TailMatches("INSERT"))
4569  COMPLETE_WITH("INTO");
4570  /* Complete INSERT INTO with table names */
4571  else if (TailMatches("INSERT", "INTO"))
4572  COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_updatables);
4573  /* Complete "INSERT INTO <table> (" with attribute names */
4574  else if (TailMatches("INSERT", "INTO", MatchAny, "("))
4575  COMPLETE_WITH_ATTR(prev2_wd);
4576 
4577  /*
4578  * Complete INSERT INTO <table> with "(" or "VALUES" or "SELECT" or
4579  * "TABLE" or "DEFAULT VALUES" or "OVERRIDING"
4580  */
4581  else if (TailMatches("INSERT", "INTO", MatchAny))
4582  COMPLETE_WITH("(", "DEFAULT VALUES", "SELECT", "TABLE", "VALUES", "OVERRIDING");
4583 
4584  /*
4585  * Complete INSERT INTO <table> (attribs) with "VALUES" or "SELECT" or
4586  * "TABLE" or "OVERRIDING"
4587  */
4588  else if (TailMatches("INSERT", "INTO", MatchAny, MatchAny) &&
4589  ends_with(prev_wd, ')'))
4590  COMPLETE_WITH("SELECT", "TABLE", "VALUES", "OVERRIDING");
4591 
4592  /* Complete OVERRIDING */
4593  else if (TailMatches("OVERRIDING"))
4594  COMPLETE_WITH("SYSTEM VALUE", "USER VALUE");
4595 
4596  /* Complete after OVERRIDING clause */
4597  else if (TailMatches("OVERRIDING", MatchAny, "VALUE"))
4598  COMPLETE_WITH("SELECT", "TABLE", "VALUES");
4599 
4600  /* Insert an open parenthesis after "VALUES" */
4601  else if (TailMatches("VALUES") && !TailMatches("DEFAULT", "VALUES"))
4602  COMPLETE_WITH("(");
4603 
4604 /* LOCK */
4605  /* Complete LOCK [TABLE] [ONLY] with a list of tables */
4606  else if (Matches("LOCK"))
4607  COMPLETE_WITH_SCHEMA_QUERY_PLUS(Query_for_list_of_tables,
4608  "TABLE", "ONLY");
4609  else if (Matches("LOCK", "TABLE"))
4610  COMPLETE_WITH_SCHEMA_QUERY_PLUS(Query_for_list_of_tables,
4611  "ONLY");
4612  else if (Matches("LOCK", "TABLE", "ONLY") || Matches("LOCK", "ONLY"))
4613  COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tables);
4614  /* For the following, handle the case of a single table only for now */
4615 
4616  /* Complete LOCK [TABLE] [ONLY] <table> with IN or NOWAIT */
4617  else if (Matches("LOCK", MatchAnyExcept("TABLE|ONLY")) ||
4618  Matches("LOCK", "TABLE", MatchAnyExcept("ONLY")) ||
4619  Matches("LOCK", "ONLY", MatchAny) ||
4620  Matches("LOCK", "TABLE", "ONLY", MatchAny))
4621  COMPLETE_WITH("IN", "NOWAIT");
4622 
4623  /* Complete LOCK [TABLE] [ONLY] <table> IN with a lock mode */
4624  else if (Matches("LOCK", MatchAnyN, "IN"))
4625  COMPLETE_WITH("ACCESS SHARE MODE",
4626  "ROW SHARE MODE", "ROW EXCLUSIVE MODE",
4627  "SHARE UPDATE EXCLUSIVE MODE", "SHARE MODE",
4628  "SHARE ROW EXCLUSIVE MODE",
4629  "EXCLUSIVE MODE", "ACCESS EXCLUSIVE MODE");
4630 
4631  /*
4632  * Complete LOCK [TABLE][ONLY] <table> IN ACCESS|ROW with rest of lock
4633  * mode
4634  */
4635  else if (Matches("LOCK", MatchAnyN, "IN", "ACCESS|ROW"))
4636  COMPLETE_WITH("EXCLUSIVE MODE", "SHARE MODE");
4637 
4638  /* Complete LOCK [TABLE] [ONLY] <table> IN SHARE with rest of lock mode */
4639  else if (Matches("LOCK", MatchAnyN, "IN", "SHARE"))
4640  COMPLETE_WITH("MODE", "ROW EXCLUSIVE MODE",
4641  "UPDATE EXCLUSIVE MODE");
4642 
4643  /* Complete LOCK [TABLE] [ONLY] <table> [IN lockmode MODE] with "NOWAIT" */
4644  else if (Matches("LOCK", MatchAnyN, "MODE"))
4645  COMPLETE_WITH("NOWAIT");
4646 
4647 /* MERGE --- can be inside EXPLAIN */
4648  else if (TailMatches("MERGE"))
4649  COMPLETE_WITH("INTO");
4650  else if (TailMatches("MERGE", "INTO"))
4651  COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_mergetargets);
4652 
4653  /* Complete MERGE INTO <table> [[AS] <alias>] with USING */
4654  else if (TailMatches("MERGE", "INTO", MatchAny))
4655  COMPLETE_WITH("USING", "AS");
4656  else if (TailMatches("MERGE", "INTO", MatchAny, "AS", MatchAny) ||
4657  TailMatches("MERGE", "INTO", MatchAny, MatchAnyExcept("USING|AS")))
4658  COMPLETE_WITH("USING");
4659 
4660  /*
4661  * Complete MERGE INTO ... USING with a list of relations supporting
4662  * SELECT
4663  */
4664  else if (TailMatches("MERGE", "INTO", MatchAny, "USING") ||
4665  TailMatches("MERGE", "INTO", MatchAny, "AS", MatchAny, "USING") ||
4666  TailMatches("MERGE", "INTO", MatchAny, MatchAny, "USING"))
4667  COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_selectables);
4668 
4669  /*
4670  * Complete MERGE INTO <table> [[AS] <alias>] USING <relations> [[AS]
4671  * alias] with ON
4672  */
4673  else if (TailMatches("MERGE", "INTO", MatchAny, "USING", MatchAny) ||
4674  TailMatches("MERGE", "INTO", MatchAny, "AS", MatchAny, "USING", MatchAny) ||
4675  TailMatches("MERGE", "INTO", MatchAny, MatchAny, "USING", MatchAny))
4676  COMPLETE_WITH("AS", "ON");
4677  else if (TailMatches("MERGE", "INTO", MatchAny, "USING", MatchAny, "AS", MatchAny) ||
4678  TailMatches("MERGE", "INTO", MatchAny, "AS", MatchAny, "USING", MatchAny, "AS", MatchAny) ||
4679  TailMatches("MERGE", "INTO", MatchAny, MatchAny, "USING", MatchAny, "AS", MatchAny) ||
4680  TailMatches("MERGE", "INTO", MatchAny, "USING", MatchAny, MatchAnyExcept("ON|AS")) ||
4681  TailMatches("MERGE", "INTO", MatchAny, "AS", MatchAny, "USING", MatchAny, MatchAnyExcept("ON|AS")) ||
4682  TailMatches("MERGE", "INTO", MatchAny, MatchAny, "USING", MatchAny, MatchAnyExcept("ON|AS")))
4683  COMPLETE_WITH("ON");
4684 
4685  /* Complete MERGE INTO ... ON with target table attributes */
4686  else if (TailMatches("INTO", MatchAny, "USING", MatchAny, "ON"))
4687  COMPLETE_WITH_ATTR(prev4_wd);
4688  else if (TailMatches("INTO", MatchAny, "AS", MatchAny, "USING", MatchAny, "AS", MatchAny, "ON"))
4689  COMPLETE_WITH_ATTR(prev8_wd);
4690  else if (TailMatches("INTO", MatchAny, MatchAny, "USING", MatchAny, MatchAny, "ON"))
4691  COMPLETE_WITH_ATTR(prev6_wd);
4692 
4693  /*
4694  * Complete ... USING <relation> [[AS] alias] ON join condition
4695  * (consisting of one or three words typically used) with WHEN [NOT]
4696  * MATCHED
4697  */
4698  else if (TailMatches("USING", MatchAny, "ON", MatchAny) ||
4699  TailMatches("USING", MatchAny, "AS", MatchAny, "ON", MatchAny) ||
4700  TailMatches("USING", MatchAny, MatchAny, "ON", MatchAny) ||
4701  TailMatches("USING", MatchAny, "ON", MatchAny, MatchAnyExcept("WHEN"), MatchAnyExcept("WHEN")) ||
4702  TailMatches("USING", MatchAny, "AS", MatchAny, "ON", MatchAny, MatchAnyExcept("WHEN"), MatchAnyExcept("WHEN")) ||
4703  TailMatches("USING", MatchAny, MatchAny, "ON", MatchAny, MatchAnyExcept("WHEN"), MatchAnyExcept("WHEN")))
4704  COMPLETE_WITH("WHEN MATCHED", "WHEN NOT MATCHED");
4705  else if (TailMatches("USING", MatchAny, "ON", MatchAny, "WHEN") ||
4706  TailMatches("USING", MatchAny, "AS", MatchAny, "ON", MatchAny, "WHEN") ||
4707  TailMatches("USING", MatchAny, MatchAny, "ON", MatchAny, "WHEN") ||
4708  TailMatches("USING", MatchAny, "ON", MatchAny, MatchAny, MatchAny, "WHEN") ||
4709  TailMatches("USING", MatchAny, "AS", MatchAny, "ON", MatchAny, MatchAny, MatchAny, "WHEN") ||
4710  TailMatches("USING", MatchAny, MatchAny, "ON", MatchAny, MatchAny, MatchAny, "WHEN"))
4711  COMPLETE_WITH("MATCHED", "NOT MATCHED");
4712 
4713  /*
4714  * Complete ... WHEN MATCHED and WHEN NOT MATCHED BY SOURCE|TARGET with
4715  * THEN/AND
4716  */
4717  else if (TailMatches("WHEN", "MATCHED") ||
4718  TailMatches("WHEN", "NOT", "MATCHED", "BY", "SOURCE|TARGET"))
4719  COMPLETE_WITH("THEN", "AND");
4720 
4721  /* Complete ... WHEN NOT MATCHED with BY/THEN/AND */
4722  else if (TailMatches("WHEN", "NOT", "MATCHED"))
4723  COMPLETE_WITH("BY", "THEN", "AND");
4724 
4725  /* Complete ... WHEN NOT MATCHED BY with SOURCE/TARGET */
4726  else if (TailMatches("WHEN", "NOT", "MATCHED", "BY"))
4727  COMPLETE_WITH("SOURCE", "TARGET");
4728 
4729  /*
4730  * Complete ... WHEN MATCHED THEN and WHEN NOT MATCHED BY SOURCE THEN with
4731  * UPDATE SET/DELETE/DO NOTHING
4732  */
4733  else if (TailMatches("WHEN", "MATCHED", "THEN") ||
4734  TailMatches("WHEN", "NOT", "MATCHED", "BY", "SOURCE", "THEN"))
4735  COMPLETE_WITH("UPDATE SET", "DELETE", "DO NOTHING");
4736 
4737  /*
4738  * Complete ... WHEN NOT MATCHED [BY TARGET] THEN with INSERT/DO NOTHING
4739  */
4740  else if (TailMatches("WHEN", "NOT", "MATCHED", "THEN") ||
4741  TailMatches("WHEN", "NOT", "MATCHED", "BY", "TARGET", "THEN"))
4742  COMPLETE_WITH("INSERT", "DO NOTHING");
4743 
4744 /* NOTIFY --- can be inside EXPLAIN, RULE, etc */
4745  else if (TailMatches("NOTIFY"))
4746  COMPLETE_WITH_QUERY(Query_for_list_of_channels);
4747 
4748 /* OPTIONS */
4749  else if (TailMatches("OPTIONS"))
4750  COMPLETE_WITH("(");
4751 
4752 /* OWNER TO - complete with available roles */
4753  else if (TailMatches("OWNER", "TO"))
4754  COMPLETE_WITH_QUERY_PLUS(Query_for_list_of_roles,
4755  Keywords_for_list_of_owner_roles);
4756 
4757 /* ORDER BY */
4758  else if (TailMatches("FROM", MatchAny, "ORDER"))
4759  COMPLETE_WITH("BY");
4760  else if (TailMatches("FROM", MatchAny, "ORDER", "BY"))
4761  COMPLETE_WITH_ATTR(prev3_wd);
4762 
4763 /* PREPARE xx AS */
4764  else if (Matches("PREPARE", MatchAny, "AS"))
4765  COMPLETE_WITH("SELECT", "UPDATE", "INSERT INTO", "DELETE FROM");
4766 
4767 /*
4768  * PREPARE TRANSACTION is missing on purpose. It's intended for transaction
4769  * managers, not for manual use in interactive sessions.
4770  */
4771 
4772 /* REASSIGN OWNED BY xxx TO yyy */
4773  else if (Matches("REASSIGN"))
4774  COMPLETE_WITH("OWNED BY");
4775  else if (Matches("REASSIGN", "OWNED"))
4776  COMPLETE_WITH("BY");
4777  else if (Matches("REASSIGN", "OWNED", "BY"))
4778  COMPLETE_WITH_QUERY(Query_for_list_of_roles);
4779  else if (Matches("REASSIGN", "OWNED", "BY", MatchAny))
4780  COMPLETE_WITH("TO");
4781  else if (Matches("REASSIGN", "OWNED", "BY", MatchAny, "TO"))
4782  COMPLETE_WITH_QUERY(Query_for_list_of_roles);
4783 
4784 /* REFRESH MATERIALIZED VIEW */
4785  else if (Matches("REFRESH"))
4786  COMPLETE_WITH("MATERIALIZED VIEW");
4787  else if (Matches("REFRESH", "MATERIALIZED"))
4788  COMPLETE_WITH("VIEW");
4789  else if (Matches("REFRESH", "MATERIALIZED", "VIEW"))
4790  COMPLETE_WITH_SCHEMA_QUERY_PLUS(Query_for_list_of_matviews,
4791  "CONCURRENTLY");
4792  else if (Matches("REFRESH", "MATERIALIZED", "VIEW", "CONCURRENTLY"))
4793  COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_matviews);
4794  else if (Matches("REFRESH", "MATERIALIZED", "VIEW", MatchAny))
4795  COMPLETE_WITH("WITH");
4796  else if (Matches("REFRESH", "MATERIALIZED", "VIEW", "CONCURRENTLY", MatchAny))
4797  COMPLETE_WITH("WITH");
4798  else if (Matches("REFRESH", "MATERIALIZED", "VIEW", MatchAny, "WITH"))
4799  COMPLETE_WITH("NO DATA", "DATA");
4800  else if (Matches("REFRESH", "MATERIALIZED", "VIEW", "CONCURRENTLY", MatchAny, "WITH"))
4801  COMPLETE_WITH("NO DATA", "DATA");
4802  else if (Matches("REFRESH", "MATERIALIZED", "VIEW", MatchAny, "WITH", "NO"))
4803  COMPLETE_WITH("DATA");
4804  else if (Matches("REFRESH", "MATERIALIZED", "VIEW", "CONCURRENTLY", MatchAny, "WITH", "NO"))
4805  COMPLETE_WITH("DATA");
4806 
4807 /* REINDEX */
4808  else if (Matches("REINDEX") ||
4809  Matches("REINDEX", "(*)"))
4810  COMPLETE_WITH("TABLE", "INDEX", "SYSTEM", "SCHEMA", "DATABASE");
4811  else if (Matches("REINDEX", "TABLE") ||
4812  Matches("REINDEX", "(*)", "TABLE"))
4813  COMPLETE_WITH_SCHEMA_QUERY_PLUS(Query_for_list_of_indexables,
4814  "CONCURRENTLY");
4815  else if (Matches("REINDEX", "INDEX") ||
4816  Matches("REINDEX", "(*)", "INDEX"))
4817  COMPLETE_WITH_SCHEMA_QUERY_PLUS(Query_for_list_of_indexes,
4818  "CONCURRENTLY");
4819  else if (Matches("REINDEX", "SCHEMA") ||
4820  Matches("REINDEX", "(*)", "SCHEMA"))
4821  COMPLETE_WITH_QUERY_PLUS(Query_for_list_of_schemas,
4822  "CONCURRENTLY");
4823  else if (Matches("REINDEX", "SYSTEM|DATABASE") ||
4824  Matches("REINDEX", "(*)", "SYSTEM|DATABASE"))
4825  COMPLETE_WITH_QUERY_PLUS(Query_for_list_of_databases,
4826  "CONCURRENTLY");
4827  else if (Matches("REINDEX", "TABLE", "CONCURRENTLY") ||
4828  Matches("REINDEX", "(*)", "TABLE", "CONCURRENTLY"))
4829  COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_indexables);
4830  else if (Matches("REINDEX", "INDEX", "CONCURRENTLY") ||
4831  Matches("REINDEX", "(*)", "INDEX", "CONCURRENTLY"))
4832  COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_indexes);
4833  else if (Matches("REINDEX", "SCHEMA", "CONCURRENTLY") ||
4834  Matches("REINDEX", "(*)", "SCHEMA", "CONCURRENTLY"))
4835  COMPLETE_WITH_QUERY(Query_for_list_of_schemas);
4836  else if (Matches("REINDEX", "SYSTEM|DATABASE", "CONCURRENTLY") ||
4837  Matches("REINDEX", "(*)", "SYSTEM|DATABASE", "CONCURRENTLY"))
4838  COMPLETE_WITH_QUERY(Query_for_list_of_databases);
4839  else if (HeadMatches("REINDEX", "(*") &&
4840  !HeadMatches("REINDEX", "(*)"))
4841  {
4842  /*
4843  * This fires if we're in an unfinished parenthesized option list.
4844  * get_previous_words treats a completed parenthesized option list as
4845  * one word, so the above test is correct.
4846  */
4847  if (ends_with(prev_wd, '(') || ends_with(prev_wd, ','))
4848  COMPLETE_WITH("CONCURRENTLY", "TABLESPACE", "VERBOSE");
4849  else if (TailMatches("TABLESPACE"))
4850  COMPLETE_WITH_QUERY(Query_for_list_of_tablespaces);
4851  }
4852 
4853 /* SECURITY LABEL */
4854  else if (Matches("SECURITY"))
4855  COMPLETE_WITH("LABEL");
4856  else if (Matches("SECURITY", "LABEL"))
4857  COMPLETE_WITH("ON", "FOR");
4858  else if (Matches("SECURITY", "LABEL", "FOR", MatchAny))
4859  COMPLETE_WITH("ON");
4860  else if (Matches("SECURITY", "LABEL", "ON") ||
4861  Matches("SECURITY", "LABEL", "FOR", MatchAny, "ON"))
4862  COMPLETE_WITH("TABLE", "COLUMN", "AGGREGATE", "DATABASE", "DOMAIN",
4863  "EVENT TRIGGER", "FOREIGN TABLE", "FUNCTION",
4864  "LARGE OBJECT", "MATERIALIZED VIEW", "LANGUAGE",
4865  "PUBLICATION", "PROCEDURE", "ROLE", "ROUTINE", "SCHEMA",
4866  "SEQUENCE", "SUBSCRIPTION", "TABLESPACE", "TYPE", "VIEW");
4867  else if (Matches("SECURITY", "LABEL", "ON", MatchAny, MatchAny))
4868  COMPLETE_WITH("IS");
4869 
4870 /* SELECT */
4871  /* naah . . . */
4872 
4873 /* SET, RESET, SHOW */
4874  /* Complete with a variable name */
4875  else if (TailMatches("SET|RESET") && !TailMatches("UPDATE", MatchAny, "SET"))
4876  COMPLETE_WITH_QUERY_VERBATIM_PLUS(Query_for_list_of_set_vars,
4877  "CONSTRAINTS",
4878  "TRANSACTION",
4879  "SESSION",
4880  "ROLE",
4881  "TABLESPACE",
4882  "ALL");
4883  else if (Matches("SHOW"))
4884  COMPLETE_WITH_QUERY_VERBATIM_PLUS(Query_for_list_of_show_vars,
4885  "SESSION AUTHORIZATION",
4886  "ALL");
4887  else if (Matches("SHOW", "SESSION"))
4888  COMPLETE_WITH("AUTHORIZATION");
4889  /* Complete "SET TRANSACTION" */
4890  else if (Matches("SET", "TRANSACTION"))
4891  COMPLETE_WITH("SNAPSHOT", "ISOLATION LEVEL", "READ", "DEFERRABLE", "NOT DEFERRABLE");
4892  else if (Matches("BEGIN|START", "TRANSACTION") ||
4893  Matches("BEGIN", "WORK") ||
4894  Matches("BEGIN") ||
4895  Matches("SET", "SESSION", "CHARACTERISTICS", "AS", "TRANSACTION"))
4896  COMPLETE_WITH("ISOLATION LEVEL", "READ", "DEFERRABLE", "NOT DEFERRABLE");
4897  else if (Matches("SET|BEGIN|START", "TRANSACTION|WORK", "NOT") ||
4898  Matches("BEGIN", "NOT") ||
4899  Matches("SET", "SESSION", "CHARACTERISTICS", "AS", "TRANSACTION", "NOT"))
4900  COMPLETE_WITH("DEFERRABLE");
4901  else if (Matches("SET|BEGIN|START", "TRANSACTION|WORK", "ISOLATION") ||
4902  Matches("BEGIN", "ISOLATION") ||
4903  Matches("SET", "SESSION", "CHARACTERISTICS", "AS", "TRANSACTION", "ISOLATION"))
4904  COMPLETE_WITH("LEVEL");
4905  else if (Matches("SET|BEGIN|START", "TRANSACTION|WORK", "ISOLATION", "LEVEL") ||
4906  Matches("BEGIN", "ISOLATION", "LEVEL") ||
4907  Matches("SET", "SESSION", "CHARACTERISTICS", "AS", "TRANSACTION", "ISOLATION", "LEVEL"))
4908  COMPLETE_WITH("READ", "REPEATABLE READ", "SERIALIZABLE");
4909  else if (Matches("SET|BEGIN|START", "TRANSACTION|WORK", "ISOLATION", "LEVEL", "READ") ||
4910  Matches("BEGIN", "ISOLATION", "LEVEL", "READ") ||
4911  Matches("SET", "SESSION", "CHARACTERISTICS", "AS", "TRANSACTION", "ISOLATION", "LEVEL", "READ"))
4912  COMPLETE_WITH("UNCOMMITTED", "COMMITTED");
4913  else if (Matches("SET|BEGIN|START", "TRANSACTION|WORK", "ISOLATION", "LEVEL", "REPEATABLE") ||
4914  Matches("BEGIN", "ISOLATION", "LEVEL", "REPEATABLE") ||
4915  Matches("SET", "SESSION", "CHARACTERISTICS", "AS", "TRANSACTION", "ISOLATION", "LEVEL", "REPEATABLE"))
4916  COMPLETE_WITH("READ");
4917  else if (Matches("SET|BEGIN|START", "TRANSACTION|WORK", "READ") ||
4918  Matches("BEGIN", "READ") ||
4919  Matches("SET", "SESSION", "CHARACTERISTICS", "AS", "TRANSACTION", "READ"))
4920  COMPLETE_WITH("ONLY", "WRITE");
4921  /* SET CONSTRAINTS */
4922  else if (Matches("SET", "CONSTRAINTS"))
4923  COMPLETE_WITH_SCHEMA_QUERY_PLUS(Query_for_list_of_constraints_with_schema,
4924  "ALL");
4925  /* Complete SET CONSTRAINTS <foo> with DEFERRED|IMMEDIATE */
4926  else if (Matches("SET", "CONSTRAINTS", MatchAny))
4927  COMPLETE_WITH("DEFERRED", "IMMEDIATE");
4928  /* Complete SET ROLE */
4929  else if (Matches("SET", "ROLE"))
4930  COMPLETE_WITH_QUERY(Query_for_list_of_roles);
4931  /* Complete SET SESSION with AUTHORIZATION or CHARACTERISTICS... */
4932  else if (Matches("SET", "SESSION"))
4933  COMPLETE_WITH("AUTHORIZATION", "CHARACTERISTICS AS TRANSACTION");
4934  /* Complete SET SESSION AUTHORIZATION with username */
4935  else if (Matches("SET", "SESSION", "AUTHORIZATION"))
4936  COMPLETE_WITH_QUERY_PLUS(Query_for_list_of_roles,
4937  "DEFAULT");
4938  /* Complete RESET SESSION with AUTHORIZATION */
4939  else if (Matches("RESET", "SESSION"))
4940  COMPLETE_WITH("AUTHORIZATION");
4941  /* Complete SET <var> with "TO" */
4942  else if (Matches("SET", MatchAny))
4943  COMPLETE_WITH("TO");
4944 
4945  /*
4946  * Complete ALTER DATABASE|FUNCTION|PROCEDURE|ROLE|ROUTINE|USER ... SET
4947  * <name>
4948  */
4949  else if (Matches("ALTER", "DATABASE|FUNCTION|PROCEDURE|ROLE|ROUTINE|USER", MatchAnyN, "SET", MatchAnyExcept("SCHEMA")))
4950  COMPLETE_WITH("FROM CURRENT", "TO");
4951 
4952  /*
4953  * Suggest possible variable values in SET variable TO|=, along with the
4954  * preceding ALTER syntaxes.
4955  */
4956  else if (TailMatches("SET", MatchAny, "TO|=") &&
4957  !TailMatches("UPDATE", MatchAny, "SET", MatchAny, "TO|="))
4958  {
4959  /* special cased code for individual GUCs */
4960  if (TailMatches("DateStyle", "TO|="))
4961  COMPLETE_WITH("ISO", "SQL", "Postgres", "German",
4962  "YMD", "DMY", "MDY",
4963  "US", "European", "NonEuropean",
4964  "DEFAULT");
4965  else if (TailMatches("search_path", "TO|="))
4966  {
4967  /* Here, we want to allow pg_catalog, so use narrower exclusion */
4968  COMPLETE_WITH_QUERY_PLUS(Query_for_list_of_schemas
4969  " AND nspname NOT LIKE E'pg\\\\_toast%%'"
4970  " AND nspname NOT LIKE E'pg\\\\_temp%%'",
4971  "DEFAULT");
4972  }
4973  else if (TailMatches("TimeZone", "TO|="))
4974  COMPLETE_WITH_TIMEZONE_NAME();
4975  else
4976  {
4977  /* generic, type based, GUC support */
4978  char *guctype = get_guctype(prev2_wd);
4979 
4980  /*
4981  * Note: if we don't recognize the GUC name, it's important to not
4982  * offer any completions, as most likely we've misinterpreted the
4983  * context and this isn't a GUC-setting command at all.
4984  */
4985  if (guctype)
4986  {
4987  if (strcmp(guctype, "enum") == 0)
4988  {
4989  set_completion_reference_verbatim(prev2_wd);
4990  COMPLETE_WITH_QUERY_PLUS(Query_for_values_of_enum_GUC,
4991  "DEFAULT");
4992  }
4993  else if (strcmp(guctype, "bool") == 0)
4994  COMPLETE_WITH("on", "off", "true", "false", "yes", "no",
4995  "1", "0", "DEFAULT");
4996  else
4997  COMPLETE_WITH("DEFAULT");
4998 
4999  free(guctype);
5000  }
5001  }
5002  }
5003 
5004 /* START TRANSACTION */
5005  else if (Matches("START"))
5006  COMPLETE_WITH("TRANSACTION");
5007 
5008 /* TABLE, but not TABLE embedded in other commands */
5009  else if (Matches("TABLE"))
5010  COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_selectables);
5011 
5012 /* TABLESAMPLE */
5013  else if (TailMatches("TABLESAMPLE"))
5014  COMPLETE_WITH_QUERY(Query_for_list_of_tablesample_methods);
5015  else if (TailMatches("TABLESAMPLE", MatchAny))
5016  COMPLETE_WITH("(");
5017 
5018 /* TRUNCATE */
5019  else if (Matches("TRUNCATE"))
5020  COMPLETE_WITH_SCHEMA_QUERY_PLUS(Query_for_list_of_truncatables,
5021  "TABLE", "ONLY");
5022  else if (Matches("TRUNCATE", "TABLE"))
5023  COMPLETE_WITH_SCHEMA_QUERY_PLUS(Query_for_list_of_truncatables,
5024  "ONLY");
5025  else if (Matches("TRUNCATE", MatchAnyN, "ONLY"))
5026  COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_truncatables);
5027  else if (Matches("TRUNCATE", MatchAny) ||
5028  Matches("TRUNCATE", "TABLE|ONLY", MatchAny) ||
5029  Matches("TRUNCATE", "TABLE", "ONLY", MatchAny))
5030  COMPLETE_WITH("RESTART IDENTITY", "CONTINUE IDENTITY", "CASCADE", "RESTRICT");
5031  else if (Matches("TRUNCATE", MatchAnyN, "IDENTITY"))
5032  COMPLETE_WITH("CASCADE", "RESTRICT");
5033 
5034 /* UNLISTEN */
5035  else if (Matches("UNLISTEN"))
5036  COMPLETE_WITH_QUERY_PLUS(Query_for_list_of_channels, "*");
5037 
5038 /* UPDATE --- can be inside EXPLAIN, RULE, etc */
5039  /* If prev. word is UPDATE suggest a list of tables */
5040  else if (TailMatches("UPDATE"))
5041  COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_updatables);
5042  /* Complete UPDATE <table> with "SET" */
5043  else if (TailMatches("UPDATE", MatchAny))
5044  COMPLETE_WITH("SET");
5045  /* Complete UPDATE <table> SET with list of attributes */
5046  else if (TailMatches("UPDATE", MatchAny, "SET"))
5047  COMPLETE_WITH_ATTR(prev2_wd);
5048  /* UPDATE <table> SET <attr> = */
5049  else if (TailMatches("UPDATE", MatchAny, "SET", MatchAnyExcept("*=")))
5050  COMPLETE_WITH("=");
5051 
5052 /* USER MAPPING */
5053  else if (Matches("ALTER|CREATE|DROP", "USER", "MAPPING"))
5054  COMPLETE_WITH("FOR");
5055  else if (Matches("CREATE", "USER", "MAPPING", "FOR"))
5056  COMPLETE_WITH_QUERY_PLUS(Query_for_list_of_roles,
5057  "CURRENT_ROLE",
5058  "CURRENT_USER",
5059  "PUBLIC",
5060  "USER");
5061  else if (Matches("ALTER|DROP", "USER", "MAPPING", "FOR"))
5062  COMPLETE_WITH_QUERY(Query_for_list_of_user_mappings);
5063  else if (Matches("CREATE|ALTER|DROP", "USER", "MAPPING", "FOR", MatchAny))
5064  COMPLETE_WITH("SERVER");
5065  else if (Matches("CREATE|ALTER", "USER", "MAPPING", "FOR", MatchAny, "SERVER", MatchAny))
5066  COMPLETE_WITH("OPTIONS");
5067 
5068 /*
5069  * VACUUM [ ( option [, ...] ) ] [ table_and_columns [, ...] ]
5070  * VACUUM [ FULL ] [ FREEZE ] [ VERBOSE ] [ ANALYZE ] [ table_and_columns [, ...] ]
5071  */
5072  else if (Matches("VACUUM"))
5073  COMPLETE_WITH_SCHEMA_QUERY_PLUS(Query_for_list_of_vacuumables,
5074  "FULL",
5075  "FREEZE",
5076  "ANALYZE",
5077  "VERBOSE");
5078  else if (Matches("VACUUM", "FULL"))
5079  COMPLETE_WITH_SCHEMA_QUERY_PLUS(Query_for_list_of_vacuumables,
5080  "FREEZE",
5081  "ANALYZE",
5082  "VERBOSE");
5083  else if (Matches("VACUUM", "FREEZE") ||
5084  Matches("VACUUM", "FULL", "FREEZE"))
5085  COMPLETE_WITH_SCHEMA_QUERY_PLUS(Query_for_list_of_vacuumables,
5086  "VERBOSE",
5087  "ANALYZE");
5088  else if (Matches("VACUUM", "VERBOSE") ||
5089  Matches("VACUUM", "FULL|FREEZE", "VERBOSE") ||
5090  Matches("VACUUM", "FULL", "FREEZE", "VERBOSE"))
5091  COMPLETE_WITH_SCHEMA_QUERY_PLUS(Query_for_list_of_vacuumables,
5092  "ANALYZE");
5093  else if (HeadMatches("VACUUM", "(*") &&
5094  !HeadMatches("VACUUM", "(*)"))
5095  {
5096  /*
5097  * This fires if we're in an unfinished parenthesized option list.
5098  * get_previous_words treats a completed parenthesized option list as
5099  * one word, so the above test is correct.
5100  */
5101  if (ends_with(prev_wd, '(') || ends_with(prev_wd, ','))
5102  COMPLETE_WITH("FULL", "FREEZE", "ANALYZE", "VERBOSE",
5103  "DISABLE_PAGE_SKIPPING", "SKIP_LOCKED",
5104  "INDEX_CLEANUP", "PROCESS_MAIN", "PROCESS_TOAST",
5105  "TRUNCATE", "PARALLEL", "SKIP_DATABASE_STATS",
5106  "ONLY_DATABASE_STATS", "BUFFER_USAGE_LIMIT");
5107  else if (TailMatches("FULL|FREEZE|ANALYZE|VERBOSE|DISABLE_PAGE_SKIPPING|SKIP_LOCKED|PROCESS_MAIN|PROCESS_TOAST|TRUNCATE|SKIP_DATABASE_STATS|ONLY_DATABASE_STATS"))
5108  COMPLETE_WITH("ON", "OFF");
5109  else if (TailMatches("INDEX_CLEANUP"))
5110  COMPLETE_WITH("AUTO", "ON", "OFF");
5111  }
5112  else if (Matches("VACUUM", MatchAnyN, "("))
5113  /* "VACUUM (" should be caught above, so assume we want columns */
5114  COMPLETE_WITH_ATTR(prev2_wd);
5115  else if (HeadMatches("VACUUM"))
5116  COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_vacuumables);
5117 
5118 /* WITH [RECURSIVE] */
5119 
5120  /*
5121  * Only match when WITH is the first word, as WITH may appear in many
5122  * other contexts.
5123  */
5124  else if (Matches("WITH"))
5125  COMPLETE_WITH("RECURSIVE");
5126 
5127 /* WHERE */
5128  /* Simple case of the word before the where being the table name */
5129  else if (TailMatches(MatchAny, "WHERE"))
5130  COMPLETE_WITH_ATTR(prev2_wd);
5131 
5132 /* ... FROM ... */
5133 /* TODO: also include SRF ? */
5134  else if (TailMatches("FROM") && !Matches("COPY|\\copy", MatchAny, "FROM"))
5135  COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_selectables);
5136 
5137 /* ... JOIN ... */
5138  else if (TailMatches("JOIN"))
5139  COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_selectables);
5140 
5141 /* ... AT [ LOCAL | TIME ZONE ] ... */
5142  else if (TailMatches("AT"))
5143  COMPLETE_WITH("LOCAL", "TIME ZONE");
5144  else if (TailMatches("AT", "TIME", "ZONE"))
5145  COMPLETE_WITH_TIMEZONE_NAME();
5146 
5147 /* Backslash commands */
5148 /* TODO: \dc \dd \dl */
5149  else if (TailMatchesCS("\\?"))
5150  COMPLETE_WITH_CS("commands", "options", "variables");
5151  else if (TailMatchesCS("\\connect|\\c"))
5152  {
5154  COMPLETE_WITH_QUERY(Query_for_list_of_databases);
5155  }
5156  else if (TailMatchesCS("\\connect|\\c", MatchAny))
5157  {
5158  if (!recognized_connection_string(prev_wd))
5159  COMPLETE_WITH_QUERY(Query_for_list_of_roles);
5160  }
5161  else if (TailMatchesCS("\\da*"))
5162  COMPLETE_WITH_VERSIONED_SCHEMA_QUERY(Query_for_list_of_aggregates);
5163  else if (TailMatchesCS("\\dAc*", MatchAny) ||
5164  TailMatchesCS("\\dAf*", MatchAny))
5165  COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_datatypes);
5166  else if (TailMatchesCS("\\dAo*", MatchAny) ||
5167  TailMatchesCS("\\dAp*", MatchAny))
5168  COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_operator_families);
5169  else if (TailMatchesCS("\\dA*"))
5170  COMPLETE_WITH_QUERY(Query_for_list_of_access_methods);
5171  else if (TailMatchesCS("\\db*"))
5172  COMPLETE_WITH_QUERY(Query_for_list_of_tablespaces);
5173  else if (TailMatchesCS("\\dconfig*"))
5174  COMPLETE_WITH_QUERY_VERBATIM(Query_for_list_of_show_vars);
5175  else if (TailMatchesCS("\\dD*"))
5176  COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_domains);
5177  else if (TailMatchesCS("\\des*"))
5178  COMPLETE_WITH_QUERY(Query_for_list_of_servers);
5179  else if (TailMatchesCS("\\deu*"))
5180  COMPLETE_WITH_QUERY(Query_for_list_of_user_mappings);
5181  else if (TailMatchesCS("\\dew*"))
5182  COMPLETE_WITH_QUERY(Query_for_list_of_fdws);
5183  else if (TailMatchesCS("\\df*"))
5184  COMPLETE_WITH_VERSIONED_SCHEMA_QUERY(Query_for_list_of_functions);
5185  else if (HeadMatchesCS("\\df*"))
5186  COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_datatypes);
5187 
5188  else if (TailMatchesCS("\\dFd*"))
5189  COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_ts_dictionaries);
5190  else if (TailMatchesCS("\\dFp*"))
5191  COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_ts_parsers);
5192  else if (TailMatchesCS("\\dFt*"))
5193  COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_ts_templates);
5194  /* must be at end of \dF alternatives: */
5195  else if (TailMatchesCS("\\dF*"))
5196  COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_ts_configurations);
5197 
5198  else if (TailMatchesCS("\\di*"))
5199  COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_indexes);
5200  else if (TailMatchesCS("\\dL*"))
5201  COMPLETE_WITH_QUERY(Query_for_list_of_languages);
5202  else if (TailMatchesCS("\\dn*"))
5203  COMPLETE_WITH_QUERY(Query_for_list_of_schemas);
5204  /* no support for completing operators, but we can complete types: */
5205  else if (HeadMatchesCS("\\do*", MatchAny))
5206  COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_datatypes);
5207  else if (TailMatchesCS("\\dp") || TailMatchesCS("\\z"))
5208  COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_grantables);
5209  else if (TailMatchesCS("\\dPi*"))
5210  COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_partitioned_indexes);
5211  else if (TailMatchesCS("\\dPt*"))
5212  COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_partitioned_tables);
5213  else if (TailMatchesCS("\\dP*"))
5214  COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_partitioned_relations);
5215  else if (TailMatchesCS("\\dRp*"))
5216  COMPLETE_WITH_VERSIONED_QUERY(Query_for_list_of_publications);
5217  else if (TailMatchesCS("\\dRs*"))
5218  COMPLETE_WITH_VERSIONED_QUERY(Query_for_list_of_subscriptions);
5219  else if (TailMatchesCS("\\ds*"))
5220  COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_sequences);
5221  else if (TailMatchesCS("\\dt*"))
5222  COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tables);
5223  else if (TailMatchesCS("\\dT*"))
5224  COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_datatypes);
5225  else if (TailMatchesCS("\\du*") ||
5226  TailMatchesCS("\\dg*") ||
5227  TailMatchesCS("\\drg*"))
5228  COMPLETE_WITH_QUERY(Query_for_list_of_roles);
5229  else if (TailMatchesCS("\\dv*"))
5230  COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_views);
5231  else if (TailMatchesCS("\\dx*"))
5232  COMPLETE_WITH_QUERY(Query_for_list_of_extensions);
5233  else if (TailMatchesCS("\\dX*"))
5234  COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_statistics);
5235  else if (TailMatchesCS("\\dm*"))
5236  COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_matviews);
5237  else if (TailMatchesCS("\\dE*"))
5238  COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_foreign_tables);
5239  else if (TailMatchesCS("\\dy*"))
5240  COMPLETE_WITH_QUERY(Query_for_list_of_event_triggers);
5241 
5242  /* must be at end of \d alternatives: */
5243  else if (TailMatchesCS("\\d*"))
5244  COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_relations);
5245 
5246  else if (TailMatchesCS("\\ef"))
5247  COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_routines);
5248  else if (TailMatchesCS("\\ev"))
5249  COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_views);
5250 
5251  else if (TailMatchesCS("\\encoding"))
5252  COMPLETE_WITH_QUERY_VERBATIM(Query_for_list_of_encodings);
5253  else if (TailMatchesCS("\\h|\\help"))
5254  COMPLETE_WITH_LIST(sql_commands);
5255  else if (TailMatchesCS("\\h|\\help", MatchAny))
5256  {
5257  if (TailMatches("DROP"))
5258  matches = rl_completion_matches(text, drop_command_generator);
5259  else if (TailMatches("ALTER"))
5260  matches = rl_completion_matches(text, alter_command_generator);
5261 
5262  /*
5263  * CREATE is recognized by tail match elsewhere, so doesn't need to be
5264  * repeated here
5265  */
5266  }
5267  else if (TailMatchesCS("\\h|\\help", MatchAny, MatchAny))
5268  {
5269  if (TailMatches("CREATE|DROP", "ACCESS"))
5270  COMPLETE_WITH("METHOD");
5271  else if (TailMatches("ALTER", "DEFAULT"))
5272  COMPLETE_WITH("PRIVILEGES");
5273  else if (TailMatches("CREATE|ALTER|DROP", "EVENT"))
5274  COMPLETE_WITH("TRIGGER");
5275  else if (TailMatches("CREATE|ALTER|DROP", "FOREIGN"))
5276  COMPLETE_WITH("DATA WRAPPER", "TABLE");
5277  else if (TailMatches("ALTER", "LARGE"))
5278  COMPLETE_WITH("OBJECT");
5279  else if (TailMatches("CREATE|ALTER|DROP", "MATERIALIZED"))
5280  COMPLETE_WITH("VIEW");
5281  else if (TailMatches("CREATE|ALTER|DROP", "TEXT"))
5282  COMPLETE_WITH("SEARCH");
5283  else if (TailMatches("CREATE|ALTER|DROP", "USER"))
5284  COMPLETE_WITH("MAPPING FOR");
5285  }
5286  else if (TailMatchesCS("\\h|\\help", MatchAny, MatchAny, MatchAny))
5287  {
5288  if (TailMatches("CREATE|ALTER|DROP", "FOREIGN", "DATA"))
5289  COMPLETE_WITH("WRAPPER");
5290  else if (TailMatches("CREATE|ALTER|DROP", "TEXT", "SEARCH"))
5291  COMPLETE_WITH("CONFIGURATION", "DICTIONARY", "PARSER", "TEMPLATE");
5292  else if (TailMatches("CREATE|ALTER|DROP", "USER", "MAPPING"))
5293  COMPLETE_WITH("FOR");
5294  }
5295  else if (TailMatchesCS("\\l*") && !TailMatchesCS("\\lo*"))
5296  COMPLETE_WITH_QUERY(Query_for_list_of_databases);
5297  else if (TailMatchesCS("\\password"))
5298  COMPLETE_WITH_QUERY(Query_for_list_of_roles);
5299  else if (TailMatchesCS("\\pset"))
5300  COMPLETE_WITH_CS("border", "columns", "csv_fieldsep", "expanded",
5301  "fieldsep", "fieldsep_zero", "footer", "format",
5302  "linestyle", "null", "numericlocale",
5303  "pager", "pager_min_lines",
5304  "recordsep", "recordsep_zero",
5305  "tableattr", "title", "tuples_only",
5306  "unicode_border_linestyle",
5307  "unicode_column_linestyle",
5308  "unicode_header_linestyle",
5309  "xheader_width");
5310  else if (TailMatchesCS("\\pset", MatchAny))
5311  {
5312  if (TailMatchesCS("format"))
5313  COMPLETE_WITH_CS("aligned", "asciidoc", "csv", "html", "latex",
5314  "latex-longtable", "troff-ms", "unaligned",
5315  "wrapped");
5316  else if (TailMatchesCS("xheader_width"))
5317  COMPLETE_WITH_CS("full", "column", "page");
5318  else if (TailMatchesCS("linestyle"))
5319  COMPLETE_WITH_CS("ascii", "old-ascii", "unicode");
5320  else if (TailMatchesCS("pager"))
5321  COMPLETE_WITH_CS("on", "off", "always");
5322  else if (TailMatchesCS("unicode_border_linestyle|"
5323  "unicode_column_linestyle|"
5324  "unicode_header_linestyle"))
5325  COMPLETE_WITH_CS("single", "double");
5326  }
5327  else if (TailMatchesCS("\\unset"))
5328  matches = complete_from_variables(text, "", "", true);
5329  else if (TailMatchesCS("\\set"))
5330  matches = complete_from_variables(text, "", "", false);
5331  else if (TailMatchesCS("\\set", MatchAny))
5332  {
5333  if (TailMatchesCS("AUTOCOMMIT|ON_ERROR_STOP|QUIET|SHOW_ALL_RESULTS|"
5334  "SINGLELINE|SINGLESTEP"))
5335  COMPLETE_WITH_CS("on", "off");
5336  else if (TailMatchesCS("COMP_KEYWORD_CASE"))
5337  COMPLETE_WITH_CS("lower", "upper",
5338  "preserve-lower", "preserve-upper");
5339  else if (TailMatchesCS("ECHO"))
5340  COMPLETE_WITH_CS("errors", "queries", "all", "none");
5341  else if (TailMatchesCS("ECHO_HIDDEN"))
5342  COMPLETE_WITH_CS("noexec", "off", "on");
5343  else if (TailMatchesCS("HISTCONTROL"))
5344  COMPLETE_WITH_CS("ignorespace", "ignoredups",
5345  "ignoreboth", "none");
5346  else if (TailMatchesCS("ON_ERROR_ROLLBACK"))
5347  COMPLETE_WITH_CS("on", "off", "interactive");
5348  else if (TailMatchesCS("SHOW_CONTEXT"))
5349  COMPLETE_WITH_CS("never", "errors", "always");
5350  else if (TailMatchesCS("VERBOSITY"))
5351  COMPLETE_WITH_CS("default", "verbose", "terse", "sqlstate");
5352  }
5353  else if (TailMatchesCS("\\sf*"))
5354  COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_routines);
5355  else if (TailMatchesCS("\\sv*"))
5356  COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_views);
5357  else if (TailMatchesCS("\\cd|\\e|\\edit|\\g|\\gx|\\i|\\include|"
5358  "\\ir|\\include_relative|\\o|\\out|"
5359  "\\s|\\w|\\write|\\lo_import"))
5360  {
5361  completion_charp = "\\";
5362  completion_force_quote = false;
5363  matches = rl_completion_matches(text, complete_from_files);
5364  }
5365 
5366  /* gen_tabcomplete.pl ends special processing here */
5367  /* END GEN_TABCOMPLETE */
5368 
5369  return matches;
5370 }
5371 
5372 
5373 /*
5374  * GENERATOR FUNCTIONS
5375  *
5376  * These functions do all the actual work of completing the input. They get
5377  * passed the text so far and the count how many times they have been called
5378  * so far with the same text.
5379  * If you read the above carefully, you'll see that these don't get called
5380  * directly but through the readline interface.
5381  * The return value is expected to be the full completion of the text, going
5382  * through a list each time, or NULL if there are no more matches. The string
5383  * will be free()'d by readline, so you must run it through strdup() or
5384  * something of that sort.
5385  */
5386 
5387 /*
5388  * Common routine for create_command_generator and drop_command_generator.
5389  * Entries that have 'excluded' flags are not returned.
5390  */
5391 static char *
5392 create_or_drop_command_generator(const char *text, int state, bits32 excluded)
5393 {
5394  static int list_index,
5395  string_length;
5396  const char *name;
5397 
5398  /* If this is the first time for this completion, init some values */
5399  if (state == 0)
5400  {
5401  list_index = 0;
5402  string_length = strlen(text);
5403  }
5404 
5405  /* find something that matches */
5406  while ((name = words_after_create[list_index++].name))
5407  {
5408  if ((pg_strncasecmp(name, text, string_length) == 0) &&
5409  !(words_after_create[list_index - 1].flags & excluded))
5410  return pg_strdup_keyword_case(name, text);
5411  }
5412  /* if nothing matches, return NULL */
5413  return NULL;
5414 }
5415 
5416 /*
5417  * This one gives you one from a list of things you can put after CREATE
5418  * as defined above.
5419  */
5420 static char *
5421 create_command_generator(const char *text, int state)
5422 {
5423  return create_or_drop_command_generator(text, state, THING_NO_CREATE);
5424 }
5425 
5426 /*
5427  * This function gives you a list of things you can put after a DROP command.
5428  */
5429 static char *
5430 drop_command_generator(const char *text, int state)
5431 {
5432  return create_or_drop_command_generator(text, state, THING_NO_DROP);
5433 }
5434 
5435 /*
5436  * This function gives you a list of things you can put after an ALTER command.
5437  */
5438 static char *
5439 alter_command_generator(const char *text, int state)
5440 {
5441  return create_or_drop_command_generator(text, state, THING_NO_ALTER);
5442 }
5443 
5444 /*
5445  * These functions generate lists using server queries.
5446  * They are all wrappers for _complete_from_query.
5447  */
5448 
5449 static char *
5450 complete_from_query(const char *text, int state)
5451 {
5452  /* query is assumed to work for any server version */
5453  return _complete_from_query(completion_charp, NULL, completion_charpp,
5454  completion_verbatim, text, state);
5455 }
5456 
5457 static char *
5458 complete_from_versioned_query(const char *text, int state)
5459 {
5460  const VersionedQuery *vquery = completion_vquery;
5461 
5462  /* Find appropriate array element */
5463  while (pset.sversion < vquery->min_server_version)
5464  vquery++;
5465  /* Fail completion if server is too old */
5466  if (vquery->query == NULL)
5467  return NULL;
5468 
5469  return _complete_from_query(vquery->query, NULL, completion_charpp,
5470  completion_verbatim, text, state);
5471 }
5472 
5473 static char *
5474 complete_from_schema_query(const char *text, int state)
5475 {
5476  /* query is assumed to work for any server version */
5477  return _complete_from_query(NULL, completion_squery, completion_charpp,
5478  completion_verbatim, text, state);
5479 }
5480 
5481 static char *
5482 complete_from_versioned_schema_query(const char *text, int state)
5483 {
5484  const SchemaQuery *squery = completion_squery;
5485 
5486  /* Find appropriate array element */
5487  while (pset.sversion < squery->min_server_version)
5488  squery++;
5489  /* Fail completion if server is too old */
5490  if (squery->catname == NULL)
5491  return NULL;
5492 
5493  return _complete_from_query(NULL, squery, completion_charpp,
5494  completion_verbatim, text, state);
5495 }
5496 
5497 
5498 /*
5499  * This creates a list of matching things, according to a query described by
5500  * the initial arguments. The caller has already done any work needed to
5501  * select the appropriate query for the server's version.
5502  *
5503  * The query can be one of two kinds:
5504  *
5505  * 1. A simple query, which must contain a restriction clause of the form
5506  * output LIKE '%s'
5507  * where "output" is the same string that the query returns. The %s
5508  * will be replaced by a LIKE pattern to match the already-typed text.
5509  * There can be a second '%s', which will be replaced by a suitably-escaped
5510  * version of the string provided in completion_ref_object. If there is a
5511  * third '%s', it will be replaced by a suitably-escaped version of the string
5512  * provided in completion_ref_schema. Those strings should be set up
5513  * by calling set_completion_reference or set_completion_reference_verbatim.
5514  * Simple queries should return a single column of matches. If "verbatim"
5515  * is true, the matches are returned as-is; otherwise, they are taken to
5516  * be SQL identifiers and quoted if necessary.
5517  *
5518  * 2. A schema query used for completion of both schema and relation names.
5519  * This is represented by a SchemaQuery object; see that typedef for details.
5520  *
5521  * See top of file for examples of both kinds of query.
5522  *
5523  * In addition to the query itself, we accept a null-terminated array of
5524  * literal keywords, which will be returned if they match the input-so-far
5525  * (case insensitively). (These are in addition to keywords specified
5526  * within the schema_query, if any.)
5527  *
5528  * If "verbatim" is true, then we use the given text as-is to match the
5529  * query results; otherwise we parse it as a possibly-qualified identifier,
5530  * and reconstruct suitable quoting afterward.
5531  *
5532  * "text" and "state" are supplied by Readline. "text" is the word we are
5533  * trying to complete. "state" is zero on first call, nonzero later.
5534  *
5535  * readline will call this repeatedly with the same text and varying
5536  * state. On each call, we are supposed to return a malloc'd string
5537  * that is a candidate completion. Return NULL when done.
5538  */
5539 static char *
5540 _complete_from_query(const char *simple_query,
5541  const SchemaQuery *schema_query,
5542  const char *const *keywords,
5543  bool verbatim,
5544  const char *text, int state)
5545 {
5546  static int list_index,
5547  num_schema_only,
5548  num_query_other,
5549  num_keywords;
5550  static PGresult *result = NULL;
5551  static bool non_empty_object;
5552  static bool schemaquoted;
5553  static bool objectquoted;
5554 
5555  /*
5556  * If this is the first time for this completion, we fetch a list of our
5557  * "things" from the backend.
5558  */
5559  if (state == 0)
5560  {
5561  PQExpBufferData query_buffer;
5562  char *schemaname;
5563  char *objectname;
5564  char *e_object_like;
5565  char *e_schemaname;
5566  char *e_ref_object;
5567  char *e_ref_schema;
5568 
5569  /* Reset static state, ensuring no memory leaks */
5570  list_index = 0;
5571  num_schema_only = 0;
5572  num_query_other = 0;
5573  num_keywords = 0;
5574  PQclear(result);
5575  result = NULL;
5576 
5577  /* Parse text, splitting into schema and object name if needed */
5578  if (verbatim)
5579  {
5580  objectname = pg_strdup(text);
5581  schemaname = NULL;
5582  }
5583  else
5584  {
5585  parse_identifier(text,
5586  &schemaname, &objectname,
5587  &schemaquoted, &objectquoted);
5588  }
5589 
5590  /* Remember whether the user has typed anything in the object part */
5591  non_empty_object = (*objectname != '\0');
5592 
5593  /*
5594  * Convert objectname to a LIKE prefix pattern (e.g. 'foo%'), and set
5595  * up suitably-escaped copies of all the strings we need.
5596  */
5597  e_object_like = make_like_pattern(objectname);
5598 
5599  if (schemaname)
5600  e_schemaname = escape_string(schemaname);
5601  else
5602  e_schemaname = NULL;
5603 
5604  if (completion_ref_object)
5605  e_ref_object = escape_string(completion_ref_object);
5606  else
5607  e_ref_object = NULL;
5608 
5609  if (completion_ref_schema)
5610  e_ref_schema = escape_string(completion_ref_schema);
5611  else
5612  e_ref_schema = NULL;
5613 
5614  initPQExpBuffer(&query_buffer);
5615 
5616  if (schema_query)
5617  {
5618  Assert(simple_query == NULL);
5619 
5620  /*
5621  * We issue different queries depending on whether the input is
5622  * already qualified or not. schema_query gives us the pieces to
5623  * assemble.
5624  */
5625  if (schemaname == NULL || schema_query->namespace == NULL)
5626  {
5627  /* Get unqualified names matching the input-so-far */
5628  appendPQExpBufferStr(&query_buffer, "SELECT ");
5629  if (schema_query->use_distinct)
5630  appendPQExpBufferStr(&query_buffer, "DISTINCT ");
5631  appendPQExpBuffer(&query_buffer,
5632  "%s, NULL::pg_catalog.text FROM %s",
5633  schema_query->result,
5634  schema_query->catname);
5635  if (schema_query->refnamespace && completion_ref_schema)
5636  appendPQExpBufferStr(&query_buffer,
5637  ", pg_catalog.pg_namespace nr");
5638  appendPQExpBufferStr(&query_buffer, " WHERE ");
5639  if (schema_query->selcondition)
5640  appendPQExpBuffer(&query_buffer, "%s AND ",
5641  schema_query->selcondition);
5642  appendPQExpBuffer(&query_buffer, "(%s) LIKE '%s'",
5643  schema_query->result,
5644  e_object_like);
5645  if (schema_query->viscondition)
5646  appendPQExpBuffer(&query_buffer, " AND %s",
5647  schema_query->viscondition);
5648  if (schema_query->refname)
5649  {
5650  Assert(completion_ref_object);
5651  appendPQExpBuffer(&query_buffer, " AND %s = '%s'",
5652  schema_query->refname, e_ref_object);
5653  if (schema_query->refnamespace && completion_ref_schema)
5654  appendPQExpBuffer(&query_buffer,
5655  " AND %s = nr.oid AND nr.nspname = '%s'",
5656  schema_query->refnamespace,
5657  e_ref_schema);
5658  else if (schema_query->refviscondition)
5659  appendPQExpBuffer(&query_buffer,
5660  " AND %s",
5661  schema_query->refviscondition);
5662  }
5663 
5664  /*
5665  * When fetching relation names, suppress system catalogs
5666  * unless the input-so-far begins with "pg_". This is a
5667  * compromise between not offering system catalogs for
5668  * completion at all, and having them swamp the result when
5669  * the input is just "p".
5670  */
5671  if (strcmp(schema_query->catname,
5672  "pg_catalog.pg_class c") == 0 &&
5673  strncmp(objectname, "pg_", 3) != 0)
5674  {
5675  appendPQExpBufferStr(&query_buffer,
5676  " AND c.relnamespace <> (SELECT oid FROM"
5677  " pg_catalog.pg_namespace WHERE nspname = 'pg_catalog')");
5678  }
5679 
5680  /*
5681  * If the target object type can be schema-qualified, add in
5682  * schema names matching the input-so-far.
5683  */
5684  if (schema_query->namespace)
5685  {
5686  appendPQExpBuffer(&query_buffer, "\nUNION ALL\n"
5687  "SELECT NULL::pg_catalog.text, n.nspname "
5688  "FROM pg_catalog.pg_namespace n "
5689  "WHERE n.nspname LIKE '%s'",
5690  e_object_like);
5691 
5692  /*
5693  * Likewise, suppress system schemas unless the
5694  * input-so-far begins with "pg_".
5695  */
5696  if (strncmp(objectname, "pg_", 3) != 0)
5697  appendPQExpBufferStr(&query_buffer,
5698  " AND n.nspname NOT LIKE E'pg\\\\_%'");
5699 
5700  /*
5701  * Since we're matching these schema names to the object
5702  * name, handle their quoting using the object name's
5703  * quoting state.
5704  */
5705  schemaquoted = objectquoted;
5706  }
5707  }
5708  else
5709  {
5710  /* Input is qualified, so produce only qualified names */
5711  appendPQExpBufferStr(&query_buffer, "SELECT ");
5712  if (schema_query->use_distinct)
5713  appendPQExpBufferStr(&query_buffer, "DISTINCT ");
5714  appendPQExpBuffer(&query_buffer, "%s, n.nspname "
5715  "FROM %s, pg_catalog.pg_namespace n",
5716  schema_query->result,
5717  schema_query->catname);
5718  if (schema_query->refnamespace && completion_ref_schema)
5719  appendPQExpBufferStr(&query_buffer,
5720  ", pg_catalog.pg_namespace nr");
5721  appendPQExpBuffer(&query_buffer, " WHERE %s = n.oid AND ",
5722  schema_query->namespace);
5723  if (schema_query->selcondition)
5724  appendPQExpBuffer(&query_buffer, "%s AND ",
5725  schema_query->selcondition);
5726  appendPQExpBuffer(&query_buffer, "(%s) LIKE '%s' AND ",
5727  schema_query->result,
5728  e_object_like);
5729  appendPQExpBuffer(&query_buffer, "n.nspname = '%s'",
5730  e_schemaname);
5731  if (schema_query->refname)
5732  {
5733  Assert(completion_ref_object);
5734  appendPQExpBuffer(&query_buffer, " AND %s = '%s'",
5735  schema_query->refname, e_ref_object);
5736  if (schema_query->refnamespace && completion_ref_schema)
5737  appendPQExpBuffer(&query_buffer,
5738  " AND %s = nr.oid AND nr.nspname = '%s'",
5739  schema_query->refnamespace,
5740  e_ref_schema);
5741  else if (schema_query->refviscondition)
5742  appendPQExpBuffer(&query_buffer,
5743  " AND %s",
5744  schema_query->refviscondition);
5745  }
5746  }
5747  }
5748  else
5749  {
5750  Assert(simple_query);
5751  /* simple_query is an sprintf-style format string */
5752  appendPQExpBuffer(&query_buffer, simple_query,
5753  e_object_like,
5754  e_ref_object, e_ref_schema);
5755  }
5756 
5757  /* Limit the number of records in the result */
5758  appendPQExpBuffer(&query_buffer, "\nLIMIT %d",
5759  completion_max_records);
5760 
5761  /* Finally, we can issue the query */
5762  result = exec_query(query_buffer.data);
5763 
5764  /* Clean up */
5765  termPQExpBuffer(&query_buffer);
5766  free(schemaname);
5767  free(objectname);
5768  free(e_object_like);
5769  free(e_schemaname);
5770  free(e_ref_object);
5771  free(e_ref_schema);
5772  }
5773 
5774  /* Return the next result, if any, but not if the query failed */
5775  if (result && PQresultStatus(result) == PGRES_TUPLES_OK)
5776  {
5777  int nskip;
5778 
5779  while (list_index < PQntuples(result))
5780  {
5781  const char *item = NULL;
5782  const char *nsp = NULL;
5783 
5784  if (!PQgetisnull(result, list_index, 0))
5785  item = PQgetvalue(result, list_index, 0);
5786  if (PQnfields(result) > 1 &&
5787  !PQgetisnull(result, list_index, 1))
5788  nsp = PQgetvalue(result, list_index, 1);
5789  list_index++;
5790 
5791  /* In verbatim mode, we return all the items as-is */
5792  if (verbatim)
5793  {
5794  num_query_other++;
5795  return pg_strdup(item);
5796  }
5797 
5798  /*
5799  * In normal mode, a name requiring quoting will be returned only
5800  * if the input was empty or quoted. Otherwise the user might see
5801  * completion inserting a quote she didn't type, which is
5802  * surprising. This restriction also dodges some odd behaviors of
5803  * some versions of readline/libedit.
5804  */
5805  if (non_empty_object)
5806  {
5807  if (item && !objectquoted && identifier_needs_quotes(item))
5808  continue;
5809  if (nsp && !schemaquoted && identifier_needs_quotes(nsp))
5810  continue;
5811  }
5812 
5813  /* Count schema-only results for hack below */
5814  if (item == NULL && nsp != NULL)
5815  num_schema_only++;
5816  else
5817  num_query_other++;
5818 
5819  return requote_identifier(nsp, item, schemaquoted, objectquoted);
5820  }
5821 
5822  /*
5823  * When the query result is exhausted, check for hard-wired keywords.
5824  * These will only be returned if they match the input-so-far,
5825  * ignoring case.
5826  */
5827  nskip = list_index - PQntuples(result);
5828  if (schema_query && schema_query->keywords)
5829  {
5830  const char *const *itemp = schema_query->keywords;
5831 
5832  while (*itemp)
5833  {
5834  const char *item = *itemp++;
5835 
5836  if (nskip-- > 0)
5837  continue;
5838  list_index++;
5839  if (pg_strncasecmp(text, item, strlen(text)) == 0)
5840  {
5841  num_keywords++;
5842  return pg_strdup_keyword_case(item, text);
5843  }
5844  }
5845  }
5846  if (keywords)
5847  {
5848  const char *const *itemp = keywords;
5849 
5850  while (*itemp)
5851  {
5852  const char *item = *itemp++;
5853 
5854  if (nskip-- > 0)
5855  continue;
5856  list_index++;
5857  if (pg_strncasecmp(text, item, strlen(text)) == 0)
5858  {
5859  num_keywords++;
5860  return pg_strdup_keyword_case(item, text);
5861  }
5862  }
5863  }
5864  }
5865 
5866  /*
5867  * Hack: if we returned only bare schema names, don't let Readline add a
5868  * space afterwards. Otherwise the schema will stop being part of the
5869  * completion subject text, which is not what we want.
5870  */
5871  if (num_schema_only > 0 && num_query_other == 0 && num_keywords == 0)
5872  rl_completion_append_character = '\0';
5873 
5874  /* No more matches, so free the result structure and return null */
5875  PQclear(result);
5876  result = NULL;
5877  return NULL;
5878 }
5879 
5880 
5881 /*
5882  * Set up completion_ref_object and completion_ref_schema
5883  * by parsing the given word. These variables can then be
5884  * used in a query passed to _complete_from_query.
5885  */
5886 static void
5887 set_completion_reference(const char *word)
5888 {
5889  bool schemaquoted,
5890  objectquoted;
5891 
5892  parse_identifier(word,
5893  &completion_ref_schema, &completion_ref_object,
5894  &schemaquoted, &objectquoted);
5895 }
5896 
5897 /*
5898  * Set up completion_ref_object when it should just be
5899  * the given word verbatim.
5900  */
5901 static void
5902 set_completion_reference_verbatim(const char *word)
5903 {
5904  completion_ref_schema = NULL;
5905  completion_ref_object = pg_strdup(word);
5906 }
5907 
5908 
5909 /*
5910  * This function returns in order one of a fixed, NULL pointer terminated list
5911  * of strings (if matching). This can be used if there are only a fixed number
5912  * SQL words that can appear at certain spot.
5913  */
5914 static char *
5915 complete_from_list(const char *text, int state)
5916 {
5917  static int string_length,
5918  list_index,
5919  matches;
5920  static bool casesensitive;
5921  const char *item;
5922 
5923  /* need to have a list */
5924  Assert(completion_charpp != NULL);
5925 
5926  /* Initialization */
5927  if (state == 0)
5928  {
5929  list_index = 0;
5930  string_length = strlen(text);
5931  casesensitive = completion_case_sensitive;
5932  matches = 0;
5933  }
5934 
5935  while ((item = completion_charpp[list_index++]))
5936  {
5937  /* First pass is case sensitive */
5938  if (casesensitive && strncmp(text, item, string_length) == 0)
5939  {
5940  matches++;
5941  return pg_strdup(item);
5942  }
5943 
5944  /* Second pass is case insensitive, don't bother counting matches */
5945  if (!casesensitive && pg_strncasecmp(text, item, string_length) == 0)
5946  {
5947  if (completion_case_sensitive)
5948  return pg_strdup(item);
5949  else
5950 
5951  /*
5952  * If case insensitive matching was requested initially,
5953  * adjust the case according to setting.
5954  */
5955  return pg_strdup_keyword_case(item, text);
5956  }
5957  }
5958 
5959  /*
5960  * No matches found. If we're not case insensitive already, lets switch to
5961  * being case insensitive and try again
5962  */
5963  if (casesensitive && matches == 0)
5964  {
5965  casesensitive = false;
5966  list_index = 0;
5967  state++;
5968  return complete_from_list(text, state);
5969  }
5970 
5971  /* If no more matches, return null. */
5972  return NULL;
5973 }
5974 
5975 
5976 /*
5977  * This function returns one fixed string the first time even if it doesn't
5978  * match what's there, and nothing the second time. The string
5979  * to be used must be in completion_charp.
5980  *
5981  * If the given string is "", this has the effect of preventing readline
5982  * from doing any completion. (Without this, readline tries to do filename
5983  * completion which is seldom the right thing.)
5984  *
5985  * If the given string is not empty, readline will replace whatever the
5986  * user typed with that string. This behavior might be useful if it's
5987  * completely certain that we know what must appear at a certain spot,
5988  * so that it's okay to overwrite misspellings. In practice, given the
5989  * relatively lame parsing technology used in this file, the level of
5990  * certainty is seldom that high, so that you probably don't want to
5991  * use this. Use complete_from_list with a one-element list instead;
5992  * that won't try to auto-correct "misspellings".
5993  */
5994 static char *
5995 complete_from_const(const char *text, int state)
5996 {
5997  Assert(completion_charp != NULL);
5998  if (state == 0)
5999  {
6000  if (completion_case_sensitive)
6001  return pg_strdup(completion_charp);
6002  else
6003 
6004  /*
6005  * If case insensitive matching was requested initially, adjust
6006  * the case according to setting.
6007  */
6008  return pg_strdup_keyword_case(completion_charp, text);
6009  }
6010  else
6011  return NULL;
6012 }
6013 
6014 
6015 /*
6016  * This function appends the variable name with prefix and suffix to
6017  * the variable names array.
6018  */
6019 static void
6020 append_variable_names(char ***varnames, int *nvars,
6021  int *maxvars, const char *varname,
6022  const char *prefix, const char *suffix)
6023 {
6024  if (*nvars >= *maxvars)
6025  {
6026  *maxvars *= 2;
6027  *varnames = (char **) pg_realloc(*varnames,
6028  ((*maxvars) + 1) * sizeof(char *));
6029  }
6030 
6031  (*varnames)[(*nvars)++] = psprintf("%s%s%s", prefix, varname, suffix);
6032 }
6033 
6034 
6035 /*
6036  * This function supports completion with the name of a psql variable.
6037  * The variable names can be prefixed and suffixed with additional text
6038  * to support quoting usages. If need_value is true, only variables
6039  * that are currently set are included; otherwise, special variables
6040  * (those that have hooks) are included even if currently unset.
6041  */
6042 static char **
6043 complete_from_variables(const char *text, const char *prefix, const char *suffix,
6044  bool need_value)
6045 {
6046  char **matches;
6047  char **varnames;
6048  int nvars = 0;
6049  int maxvars = 100;
6050  int i;
6051  struct _variable *ptr;
6052 
6053  varnames = (char **) pg_malloc((maxvars + 1) * sizeof(char *));
6054 
6055  for (ptr = pset.vars->next; ptr; ptr = ptr->next)
6056  {
6057  if (need_value && !(ptr->value))
6058  continue;
6059  append_variable_names(&varnames, &nvars, &maxvars, ptr->name,
6060  prefix, suffix);
6061  }
6062 
6063  varnames[nvars] = NULL;
6064  COMPLETE_WITH_LIST_CS((const char *const *) varnames);
6065 
6066  for (i = 0; i < nvars; i++)
6067  free(varnames[i]);
6068  free(varnames);
6069 
6070  return matches;
6071 }
6072 
6073 
6074 /*
6075  * This function wraps rl_filename_completion_function() to strip quotes from
6076  * the input before searching for matches and to quote any matches for which
6077  * the consuming command will require it.
6078  *
6079  * Caller must set completion_charp to a zero- or one-character string
6080  * containing the escape character. This is necessary since \copy has no
6081  * escape character, but every other backslash command recognizes "\" as an
6082  * escape character.
6083  *
6084  * Caller must also set completion_force_quote to indicate whether to force
6085  * quotes around the result. (The SQL COPY command requires that.)
6086  */
6087 static char *
6088 complete_from_files(const char *text, int state)
6089 {
6090 #ifdef USE_FILENAME_QUOTING_FUNCTIONS
6091 
6092  /*
6093  * If we're using a version of Readline that supports filename quoting
6094  * hooks, rely on those, and invoke rl_filename_completion_function()
6095  * without messing with its arguments. Readline does stuff internally
6096  * that does not work well at all if we try to handle dequoting here.
6097  * Instead, Readline will call quote_file_name() and dequote_file_name()
6098  * (see below) at appropriate times.
6099  *
6100  * ... or at least, mostly it will. There are some paths involving
6101  * unmatched file names in which Readline never calls quote_file_name(),
6102  * and if left to its own devices it will incorrectly append a quote
6103  * anyway. Set rl_completion_suppress_quote to prevent that. If we do
6104  * get to quote_file_name(), we'll clear this again. (Yes, this seems
6105  * like it's working around Readline bugs.)
6106  */
6107 #ifdef HAVE_RL_COMPLETION_SUPPRESS_QUOTE
6108  rl_completion_suppress_quote = 1;
6109 #endif
6110 
6111  /* If user typed a quote, force quoting (never remove user's quote) */
6112  if (*text == '\'')
6113  completion_force_quote = true;
6114 
6115  return rl_filename_completion_function(text, state);
6116 #else
6117 
6118  /*
6119  * Otherwise, we have to do the best we can.
6120  */
6121  static const char *unquoted_text;
6122  char *unquoted_match;
6123  char *ret = NULL;
6124 
6125  /* If user typed a quote, force quoting (never remove user's quote) */
6126  if (*text == '\'')
6127  completion_force_quote = true;
6128 
6129  if (state == 0)
6130  {
6131  /* Initialization: stash the unquoted input. */
6132  unquoted_text = strtokx(text, "", NULL, "'", *completion_charp,
6133  false, true, pset.encoding);
6134  /* expect a NULL return for the empty string only */
6135  if (!unquoted_text)
6136  {
6137  Assert(*text == '\0');
6138  unquoted_text = text;
6139  }
6140  }
6141 
6142  unquoted_match = rl_filename_completion_function(unquoted_text, state);
6143  if (unquoted_match)
6144  {
6145  struct stat statbuf;
6146  bool is_dir = (stat(unquoted_match, &statbuf) == 0 &&
6147  S_ISDIR(statbuf.st_mode) != 0);
6148 
6149  /* Re-quote the result, if needed. */
6150  ret = quote_if_needed(unquoted_match, " \t\r\n\"`",
6151  '\'', *completion_charp,
6152  completion_force_quote,
6153  pset.encoding);
6154  if (ret)
6155  free(unquoted_match);
6156  else
6157  ret = unquoted_match;
6158 
6159  /*
6160  * If it's a directory, replace trailing quote with a slash; this is
6161  * usually more convenient. (If we didn't quote, leave this to
6162  * libedit.)
6163  */
6164  if (*ret == '\'' && is_dir)
6165  {
6166  char *retend = ret + strlen(ret) - 1;
6167 
6168  Assert(*retend == '\'');
6169  *retend = '/';
6170  /* Prevent libedit from adding a space, too */
6171  rl_completion_append_character = '\0';
6172  }
6173  }
6174 
6175  return ret;
6176 #endif /* USE_FILENAME_QUOTING_FUNCTIONS */
6177 }
6178 
6179 
6180 /* HELPER FUNCTIONS */
6181 
6182 
6183 /*
6184  * Make a pg_strdup copy of s and convert the case according to
6185  * COMP_KEYWORD_CASE setting, using ref as the text that was already entered.
6186  */
6187 static char *
6188 pg_strdup_keyword_case(const char *s, const char *ref)
6189 {
6190  char *ret,
6191  *p;
6192  unsigned char first = ref[0];
6193 
6194  ret = pg_strdup(s);
6195 
6198  pset.comp_case == PSQL_COMP_CASE_PRESERVE_UPPER) && islower(first)) ||
6199  (pset.comp_case == PSQL_COMP_CASE_PRESERVE_LOWER && !isalpha(first)))
6200  {
6201  for (p = ret; *p; p++)
6202  *p = pg_tolower((unsigned char) *p);
6203  }
6204  else
6205  {
6206  for (p = ret; *p; p++)
6207  *p = pg_toupper((unsigned char) *p);
6208  }
6209 
6210  return ret;
6211 }
6212 
6213 
6214 /*
6215  * escape_string - Escape argument for use as string literal.
6216  *
6217  * The returned value has to be freed.
6218  */
6219 static char *
6220 escape_string(const char *text)
6221 {
6222  size_t text_length;
6223  char *result;
6224 
6225  text_length = strlen(text);
6226 
6227  result = pg_malloc(text_length * 2 + 1);
6228  PQescapeStringConn(pset.db, result, text, text_length, NULL);
6229 
6230  return result;
6231 }
6232 
6233 
6234 /*
6235  * make_like_pattern - Convert argument to a LIKE prefix pattern.
6236  *
6237  * We escape _ and % in the given text by backslashing, append a % to
6238  * represent "any subsequent characters", and then pass the string through
6239  * escape_string() so it's ready to insert in a query. The result needs
6240  * to be freed.
6241  */
6242 static char *
6243 make_like_pattern(const char *word)
6244 {
6245  char *result;
6246  char *buffer = pg_malloc(strlen(word) * 2 + 2);
6247  char *bptr = buffer;
6248 
6249  while (*word)
6250  {
6251  if (*word == '_' || *word == '%')
6252  *bptr++ = '\\';
6253  if (IS_HIGHBIT_SET(*word))
6254  {
6255  /*
6256  * Transfer multibyte characters without further processing, to
6257  * avoid getting confused in unsafe client encodings.
6258  */
6259  int chlen = PQmblenBounded(word, pset.encoding);
6260 
6261  while (chlen-- > 0)
6262  *bptr++ = *word++;
6263  }
6264  else
6265  *bptr++ = *word++;
6266  }
6267  *bptr++ = '%';
6268  *bptr = '\0';
6269 
6270  result = escape_string(buffer);
6271  free(buffer);
6272  return result;
6273 }
6274 
6275 
6276 /*
6277  * parse_identifier - Parse a possibly-schema-qualified SQL identifier.
6278  *
6279  * This involves splitting off the schema name if present, de-quoting,
6280  * and downcasing any unquoted text. We are a bit laxer than the backend
6281  * in that we allow just portions of a name to be quoted --- that's because
6282  * psql metacommands have traditionally behaved that way.
6283  *
6284  * Outputs are a malloc'd schema name (NULL if none), malloc'd object name,
6285  * and booleans telling whether any part of the schema and object name was
6286  * double-quoted.
6287  */
6288 static void
6289 parse_identifier(const char *ident,
6290  char **schemaname, char **objectname,
6291  bool *schemaquoted, bool *objectquoted)
6292 {
6293  size_t buflen = strlen(ident) + 1;
6294  bool enc_is_single_byte = (pg_encoding_max_length(pset.encoding) == 1);
6295  char *sname;
6296  char *oname;
6297  char *optr;
6298  bool inquotes;
6299 
6300  /* Initialize, making a certainly-large-enough output buffer */
6301  sname = NULL;
6302  oname = pg_malloc(buflen);
6303  *schemaquoted = *objectquoted = false;
6304  /* Scan */
6305  optr = oname;
6306  inquotes = false;
6307  while (*ident)
6308  {
6309  unsigned char ch = (unsigned char) *ident++;
6310 
6311  if (ch == '"')
6312  {
6313  if (inquotes && *ident == '"')
6314  {
6315  /* two quote marks within a quoted identifier = emit quote */
6316  *optr++ = '"';
6317  ident++;
6318  }
6319  else
6320  {
6321  inquotes = !inquotes;
6322  *objectquoted = true;
6323  }
6324  }
6325  else if (ch == '.' && !inquotes)
6326  {
6327  /* Found a schema name, transfer it to sname / *schemaquoted */
6328  *optr = '\0';
6329  free(sname); /* drop any catalog name */
6330  sname = oname;
6331  oname = pg_malloc(buflen);
6332  optr = oname;
6333  *schemaquoted = *objectquoted;
6334  *objectquoted = false;
6335  }
6336  else if (!enc_is_single_byte && IS_HIGHBIT_SET(ch))
6337  {
6338  /*
6339  * Transfer multibyte characters without further processing. They
6340  * wouldn't be affected by our downcasing rule anyway, and this
6341  * avoids possibly doing the wrong thing in unsafe client
6342  * encodings.
6343  */
6344  int chlen = PQmblenBounded(ident - 1, pset.encoding);
6345 
6346  *optr++ = (char) ch;
6347  while (--chlen > 0)
6348  *optr++ = *ident++;
6349  }
6350  else
6351  {
6352  if (!inquotes)
6353  {
6354  /*
6355  * This downcasing transformation should match the backend's
6356  * downcase_identifier() as best we can. We do not know the
6357  * backend's locale, though, so it's necessarily approximate.
6358  * We assume that psql is operating in the same locale and
6359  * encoding as the backend.
6360  */
6361  if (ch >= 'A' && ch <= 'Z')
6362  ch += 'a' - 'A';
6363  else if (enc_is_single_byte && IS_HIGHBIT_SET(ch) && isupper(ch))
6364  ch = tolower(ch);
6365  }
6366  *optr++ = (char) ch;
6367  }
6368  }
6369 
6370  *optr = '\0';
6371  *schemaname = sname;
6372  *objectname = oname;
6373 }
6374 
6375 
6376 /*
6377  * requote_identifier - Reconstruct a possibly-schema-qualified SQL identifier.
6378  *
6379  * Build a malloc'd string containing the identifier, with quoting applied
6380  * as necessary. This is more or less the inverse of parse_identifier;
6381  * in particular, if an input component was quoted, we'll quote the output
6382  * even when that isn't strictly required.
6383  *
6384  * Unlike parse_identifier, we handle the case where a schema and no
6385  * object name is provided, producing just "schema.".
6386  */
6387 static char *
6388 requote_identifier(const char *schemaname, const char *objectname,
6389  bool quote_schema, bool quote_object)
6390 {
6391  char *result;
6392  size_t buflen = 1; /* count the trailing \0 */
6393  char *ptr;
6394 
6395  /*
6396  * We could use PQescapeIdentifier for some of this, but not all, and it
6397  * adds more notational cruft than it seems worth.
6398  */
6399  if (schemaname)
6400  {
6401  buflen += strlen(schemaname) + 1; /* +1 for the dot */
6402  if (!quote_schema)
6403  quote_schema = identifier_needs_quotes(schemaname);
6404  if (quote_schema)
6405  {
6406  buflen += 2; /* account for quote marks */
6407  for (const char *p = schemaname; *p; p++)
6408  {
6409  if (*p == '"')
6410  buflen++;
6411  }
6412  }
6413  }
6414  if (objectname)
6415  {
6416  buflen += strlen(objectname);
6417  if (!quote_object)
6418  quote_object = identifier_needs_quotes(objectname);
6419  if (quote_object)
6420  {
6421  buflen += 2; /* account for quote marks */
6422  for (const char *p = objectname; *p; p++)
6423  {
6424  if (*p == '"')
6425  buflen++;
6426  }
6427  }
6428  }
6429  result = pg_malloc(buflen);
6430  ptr = result;
6431  if (schemaname)
6432  {
6433  if (quote_schema)
6434  *ptr++ = '"';
6435  for (const char *p = schemaname; *p; p++)
6436  {
6437  *ptr++ = *p;
6438  if (*p == '"')
6439  *ptr++ = '"';
6440  }
6441  if (quote_schema)
6442  *ptr++ = '"';
6443  *ptr++ = '.';
6444  }
6445  if (objectname)
6446  {
6447  if (quote_object)
6448  *ptr++ = '"';
6449  for (const char *p = objectname; *p; p++)
6450  {
6451  *ptr++ = *p;
6452  if (*p == '"')
6453  *ptr++ = '"';
6454  }
6455  if (quote_object)
6456  *ptr++ = '"';
6457  }
6458  *ptr = '\0';
6459  return result;
6460 }
6461 
6462 
6463 /*
6464  * Detect whether an identifier must be double-quoted.
6465  *
6466  * Note we'll quote anything that's not ASCII; the backend's quote_ident()
6467  * does the same. Perhaps this could be relaxed in future.
6468  */
6469 static bool
6470 identifier_needs_quotes(const char *ident)
6471 {
6472  int kwnum;
6473 
6474  /* Check syntax. */
6475  if (!((ident[0] >= 'a' && ident[0] <= 'z') || ident[0] == '_'))
6476  return true;
6477  if (strspn(ident, "abcdefghijklmnopqrstuvwxyz0123456789_$") != strlen(ident))
6478  return true;
6479 
6480  /*
6481  * Check for keyword. We quote keywords except for unreserved ones.
6482  *
6483  * It is possible that our keyword list doesn't quite agree with the
6484  * server's, but this should be close enough for tab-completion purposes.
6485  *
6486  * Note: ScanKeywordLookup() does case-insensitive comparison, but that's
6487  * fine, since we already know we have all-lower-case.
6488  */
6490 
6491  if (kwnum >= 0 && ScanKeywordCategories[kwnum] != UNRESERVED_KEYWORD)
6492  return true;
6493 
6494  return false;
6495 }
6496 
6497 
6498 /*
6499  * Execute a query, returning NULL if there was any error.
6500  * This should be the preferred way of talking to the database in this file.
6501  */
6502 static PGresult *
6503 exec_query(const char *query)
6504 {
6505  PGresult *result;
6506 
6507  if (query == NULL || !pset.db || PQstatus(pset.db) != CONNECTION_OK)
6508  return NULL;
6509 
6510  result = PQexec(pset.db, query);
6511 
6512  if (PQresultStatus(result) != PGRES_TUPLES_OK)
6513  {
6514  /*
6515  * Printing an error while the user is typing would be quite annoying,
6516  * so we don't. This does complicate debugging of this code; but you
6517  * can look in the server log instead.
6518  */
6519 #ifdef NOT_USED
6520  pg_log_error("tab completion query failed: %s\nQuery was:\n%s",
6521  PQerrorMessage(pset.db), query);
6522 #endif
6523  PQclear(result);
6524  result = NULL;
6525  }
6526 
6527  return result;
6528 }
6529 
6530 
6531 /*
6532  * Parse all the word(s) before point.
6533  *
6534  * Returns a malloc'd array of character pointers that point into the malloc'd
6535  * data array returned to *buffer; caller must free() both of these when done.
6536  * *nwords receives the number of words found, ie, the valid length of the
6537  * return array.
6538  *
6539  * Words are returned right to left, that is, previous_words[0] gets the last
6540  * word before point, previous_words[1] the next-to-last, etc.
6541  */
6542 static char **
6543 get_previous_words(int point, char **buffer, int *nwords)
6544 {
6545  char **previous_words;
6546  char *buf;
6547  char *outptr;
6548  int words_found = 0;
6549  int i;
6550 
6551  /*
6552  * If we have anything in tab_completion_query_buf, paste it together with
6553  * rl_line_buffer to construct the full query. Otherwise we can just use
6554  * rl_line_buffer as the input string.
6555  */
6557  {
6559  buf = pg_malloc(point + i + 2);
6560  memcpy(buf, tab_completion_query_buf->data, i);
6561  buf[i++] = '\n';
6562  memcpy(buf + i, rl_line_buffer, point);
6563  i += point;
6564  buf[i] = '\0';
6565  /* Readjust point to reference appropriate offset in buf */
6566  point = i;
6567  }
6568  else
6569  buf = rl_line_buffer;
6570 
6571  /*
6572  * Allocate an array of string pointers and a buffer to hold the strings
6573  * themselves. The worst case is that the line contains only
6574  * non-whitespace WORD_BREAKS characters, making each one a separate word.
6575  * This is usually much more space than we need, but it's cheaper than
6576  * doing a separate malloc() for each word.
6577  */
6578  previous_words = (char **) pg_malloc(point * sizeof(char *));
6579  *buffer = outptr = (char *) pg_malloc(point * 2);
6580 
6581  /*
6582  * First we look for a non-word char before the current point. (This is
6583  * probably useless, if readline is on the same page as we are about what
6584  * is a word, but if so it's cheap.)
6585  */
6586  for (i = point - 1; i >= 0; i--)
6587  {
6588  if (strchr(WORD_BREAKS, buf[i]))
6589  break;
6590  }
6591  point = i;
6592 
6593  /*
6594  * Now parse words, working backwards, until we hit start of line. The
6595  * backwards scan has some interesting but intentional properties
6596  * concerning parenthesis handling.
6597  */
6598  while (point >= 0)
6599  {
6600  int start,
6601  end;
6602  bool inquotes = false;
6603  int parentheses = 0;
6604 
6605  /* now find the first non-space which then constitutes the end */
6606  end = -1;
6607  for (i = point; i >= 0; i--)
6608  {
6609  if (!isspace((unsigned char) buf[i]))
6610  {
6611  end = i;
6612  break;
6613  }
6614  }
6615  /* if no end found, we're done */
6616  if (end < 0)
6617  break;
6618 
6619  /*
6620  * Otherwise we now look for the start. The start is either the last
6621  * character before any word-break character going backwards from the
6622  * end, or it's simply character 0. We also handle open quotes and
6623  * parentheses.
6624  */
6625  for (start = end; start > 0; start--)
6626  {
6627  if (buf[start] == '"')
6628  inquotes = !inquotes;
6629  if (!inquotes)
6630  {
6631  if (buf[start] == ')')
6632  parentheses++;
6633  else if (buf[start] == '(')
6634  {
6635  if (--parentheses <= 0)
6636  break;
6637  }
6638  else if (parentheses == 0 &&
6639  strchr(WORD_BREAKS, buf[start - 1]))
6640  break;
6641  }
6642  }
6643 
6644  /* Return the word located at start to end inclusive */
6645  previous_words[words_found++] = outptr;
6646  i = end - start + 1;
6647  memcpy(outptr, &buf[start], i);
6648  outptr += i;
6649  *outptr++ = '\0';
6650 
6651  /* Continue searching */
6652  point = start - 1;
6653  }
6654 
6655  /* Release parsing input workspace, if we made one above */
6656  if (buf != rl_line_buffer)
6657  free(buf);
6658 
6659  *nwords = words_found;
6660  return previous_words;
6661 }
6662 
6663 /*
6664  * Look up the type for the GUC variable with the passed name.
6665  *
6666  * Returns NULL if the variable is unknown. Otherwise the returned string,
6667  * containing the type, has to be freed.
6668  */
6669 static char *
6670 get_guctype(const char *varname)
6671 {
6672  PQExpBufferData query_buffer;
6673  char *e_varname;
6674  PGresult *result;
6675  char *guctype = NULL;
6676 
6677  e_varname = escape_string(varname);
6678 
6679  initPQExpBuffer(&query_buffer);
6680  appendPQExpBuffer(&query_buffer,
6681  "SELECT vartype FROM pg_catalog.pg_settings "
6682  "WHERE pg_catalog.lower(name) = pg_catalog.lower('%s')",
6683  e_varname);
6684 
6685  result = exec_query(query_buffer.data);
6686  termPQExpBuffer(&query_buffer);
6687  free(e_varname);
6688 
6689  if (PQresultStatus(result) == PGRES_TUPLES_OK && PQntuples(result) > 0)
6690  guctype = pg_strdup(PQgetvalue(result, 0, 0));
6691 
6692  PQclear(result);
6693 
6694  return guctype;
6695 }
6696 
6697 #ifdef USE_FILENAME_QUOTING_FUNCTIONS
6698 
6699 /*
6700  * Quote a filename according to SQL rules, returning a malloc'd string.
6701  * completion_charp must point to escape character or '\0', and
6702  * completion_force_quote must be set correctly, as per comments for
6703  * complete_from_files().
6704  */
6705 static char *
6706 quote_file_name(char *fname, int match_type, char *quote_pointer)
6707 {
6708  char *s;
6709  struct stat statbuf;
6710 
6711  /* Quote if needed. */
6712  s = quote_if_needed(fname, " \t\r\n\"`",
6713  '\'', *completion_charp,
6714  completion_force_quote,
6715  pset.encoding);
6716  if (!s)
6717  s = pg_strdup(fname);
6718 
6719  /*
6720  * However, some of the time we have to strip the trailing quote from what
6721  * we send back. Never strip the trailing quote if the user already typed
6722  * one; otherwise, suppress the trailing quote if we have multiple/no
6723  * matches (because we don't want to add a quote if the input is seemingly
6724  * unfinished), or if the input was already quoted (because Readline will
6725  * do arguably-buggy things otherwise), or if the file does not exist, or
6726  * if it's a directory.
6727  */
6728  if (*s == '\'' &&
6729  completion_last_char != '\'' &&
6730  (match_type != SINGLE_MATCH ||
6731  (quote_pointer && *quote_pointer == '\'') ||
6732  stat(fname, &statbuf) != 0 ||
6733  S_ISDIR(statbuf.st_mode)))
6734  {
6735  char *send = s + strlen(s) - 1;
6736 
6737  Assert(*send == '\'');
6738  *send = '\0';
6739  }
6740 
6741  /*
6742  * And now we can let Readline do its thing with possibly adding a quote
6743  * on its own accord. (This covers some additional cases beyond those
6744  * dealt with above.)
6745  */
6746 #ifdef HAVE_RL_COMPLETION_SUPPRESS_QUOTE
6747  rl_completion_suppress_quote = 0;
6748 #endif
6749 
6750  /*
6751  * If user typed a leading quote character other than single quote (i.e.,
6752  * double quote), zap it, so that we replace it with the correct single
6753  * quote.
6754  */
6755  if (quote_pointer && *quote_pointer != '\'')
6756  *quote_pointer = '\0';
6757 
6758  return s;
6759 }
6760 
6761 /*
6762  * Dequote a filename, if it's quoted.
6763  * completion_charp must point to escape character or '\0', as per
6764  * comments for complete_from_files().
6765  */
6766 static char *
6767 dequote_file_name(char *fname, int quote_char)
6768 {
6769  char *unquoted_fname;
6770 
6771  /*
6772  * If quote_char is set, it's not included in "fname". We have to add it
6773  * or strtokx will not interpret the string correctly (notably, it won't
6774  * recognize escapes).
6775  */
6776  if (quote_char == '\'')
6777  {
6778  char *workspace = (char *) pg_malloc(strlen(fname) + 2);
6779 
6780  workspace[0] = quote_char;
6781  strcpy(workspace + 1, fname);
6782  unquoted_fname = strtokx(workspace, "", NULL, "'", *completion_charp,
6783  false, true, pset.encoding);
6784  free(workspace);
6785  }
6786  else
6787  unquoted_fname = strtokx(fname, "", NULL, "'", *completion_charp,
6788  false, true, pset.encoding);
6789 
6790  /* expect a NULL return for the empty string only */
6791  if (!unquoted_fname)
6792  {
6793  Assert(*fname == '\0');
6794  unquoted_fname = fname;
6795  }
6796 
6797  /* readline expects a malloc'd result that it is to free */
6798  return pg_strdup(unquoted_fname);
6799 }
6800 
6801 #endif /* USE_FILENAME_QUOTING_FUNCTIONS */
6802 
6803 #endif /* USE_READLINE */
bool recognized_connection_string(const char *connstr)
Definition: common.c:2317
struct varlena text
Definition: c.h:705
#define IS_HIGHBIT_SET(ch)
Definition: c.h:1160
#define Assert(condition)
Definition: c.h:863
uint32 bits32
Definition: c.h:526
#define CppAsString2(x)
Definition: c.h:342
#define lengthof(array)
Definition: c.h:793
const uint8 ScanKeywordCategories[SCANKEYWORDS_NUM_KEYWORDS]
Definition: keywords.c:29
char * PQerrorMessage(const PGconn *conn)
Definition: fe-connect.c:7200
ConnStatusType PQstatus(const PGconn *conn)
Definition: fe-connect.c:7137
size_t PQescapeStringConn(PGconn *conn, char *to, const char *from, size_t length, int *error)
Definition: fe-exec.c:4145
ExecStatusType PQresultStatus(const PGresult *res)
Definition: fe-exec.c:3411
int PQntuples(const PGresult *res)
Definition: fe-exec.c:3481
PGresult * PQexec(PGconn *conn, const char *query)
Definition: fe-exec.c:2262
char * PQgetvalue(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3876
int PQgetisnull(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3901
int PQnfields(const PGresult *res)
Definition: fe-exec.c:3489
int PQmblenBounded(const char *s, int encoding)
Definition: fe-misc.c:1234
void * pg_realloc(void *ptr, size_t size)
Definition: fe_memutils.c:65
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
void * pg_malloc(size_t size)
Definition: fe_memutils.c:47
return str start
#define free(a)
Definition: header.h:65
#define ident
Definition: indent_codes.h:47
int i
Definition: isn.c:72
PGDLLIMPORT const ScanKeywordList ScanKeywords
#define UNRESERVED_KEYWORD
Definition: keywords.h:20
int ScanKeywordLookup(const char *str, const ScanKeywordList *keywords)
Definition: kwlookup.c:38
@ CONNECTION_OK
Definition: libpq-fe.h:81
@ PGRES_TUPLES_OK
Definition: libpq-fe.h:123
va_end(args)
va_start(args, fmt)
#define pg_log_error(...)
Definition: logging.h:106
char * pnstrdup(const char *in, Size len)
Definition: mcxt.c:1707
void * arg
static char * buf
Definition: pg_test_fsync.c:72
int pg_strcasecmp(const char *s1, const char *s2)
Definition: pgstrcasecmp.c:36
unsigned char pg_toupper(unsigned char ch)
Definition: pgstrcasecmp.c:105
unsigned char pg_tolower(unsigned char ch)
Definition: pgstrcasecmp.c:122
int pg_strncasecmp(const char *s1, const char *s2, size_t n)
Definition: pgstrcasecmp.c:69
void initPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:90
void appendPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:265
void appendPQExpBufferStr(PQExpBuffer str, const char *data)
Definition: pqexpbuffer.c:367
void termPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:129
char * c
char * psprintf(const char *fmt,...)
Definition: psprintf.c:43
static void word(struct vars *v, int dir, struct state *lp, struct state *rp)
Definition: regcomp.c:1476
PsqlSettings pset
Definition: startup.c:32
@ PSQL_COMP_CASE_PRESERVE_LOWER
Definition: settings.h:60
@ PSQL_COMP_CASE_LOWER
Definition: settings.h:62
@ PSQL_COMP_CASE_PRESERVE_UPPER
Definition: settings.h:59
char * quote_if_needed(const char *source, const char *entails_quote, char quote, char escape, bool force_quote, int encoding)
Definition: stringutils.c:292
char * strtokx(const char *s, const char *whitespace, const char *delim, const char *quote, char escape, bool e_strings, bool del_quotes, int encoding)
Definition: stringutils.c:52
VariableSpace vars
Definition: settings.h:133
PSQL_COMP_CASE comp_case
Definition: settings.h:160
int encoding
Definition: settings.h:92
PGconn * db
Definition: settings.h:91
const char * progname
Definition: settings.h:124
struct _variable * next
Definition: variables.h:68
char * name
Definition: variables.h:64
char * value
Definition: variables.h:65
Definition: regguts.h:323
Definition: c.h:692
void initialize_readline(void)
PQExpBuffer tab_completion_query_buf
static int32 text_length(Datum str)
Definition: varlena.c:711
const char * name
int pg_encoding_max_length(int encoding)
Definition: wchar.c:2127
#define stat
Definition: win32_port.h:284
#define S_ISDIR(m)
Definition: win32_port.h:325
#define send(s, buf, len, flags)
Definition: win32_port.h:515