From d532e198a308c7369d63f1fc26bdcce01e8ac35f Mon Sep 17 00:00:00 2001 From: Robins Tharakan Date: Mon, 7 Aug 2017 13:59:51 +0000 Subject: [PATCH 001/149] Initial Commit. This branch is to cater to Amazon Redshift Server relatd changes that affect the psql client --- src/bin/psql/startup.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/bin/psql/startup.c b/src/bin/psql/startup.c index 7f767976a5b76..41d456ab007e2 100644 --- a/src/bin/psql/startup.c +++ b/src/bin/psql/startup.c @@ -768,7 +768,7 @@ process_psqlrc_file(char *filename) static void showVersion(void) { - puts("psql (PostgreSQL) " PG_VERSION); + puts("psql (Amazon Redshift) " PG_VERSION); } From de88b815c2649a2d1ffa0768bd20fb72464ce753 Mon Sep 17 00:00:00 2001 From: Robins Tharakan Date: Wed, 9 Aug 2017 08:52:33 +0000 Subject: [PATCH 002/149] Adding an SQL commands list from Redshift to keep a track of support level of various Redshift SQL commands --- src/bin/psql/README | 73 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 src/bin/psql/README diff --git a/src/bin/psql/README b/src/bin/psql/README new file mode 100644 index 0000000000000..6011d2d5727e9 --- /dev/null +++ b/src/bin/psql/README @@ -0,0 +1,73 @@ +Redshift Support - Status Report + +This document lists the current status of Redshift Support + +Legend +Pending Review +Supported +Partially Supported +Not Supported + +SQL Commands +ABORT +ALTER DATABASE +ALTER DEFAULT PRIVILEGES +ALTER GROUP +ALTER SCHEMA +ALTER TABLE +ALTER TABLE APPEND +ALTER USER +ANALYZE +ANALYZE COMPRESSION +BEGIN +CANCEL +CLOSE +COMMENT +COMMIT +COPY +CREATE DATABASE +CREATE EXTERNAL SCHEMA +CREATE EXTERNAL TABLE +CREATE FUNCTION +CREATE GROUP +CREATE LIBRARY +CREATE SCHEMA +CREATE TABLE +CREATE TABLE AS +CREATE USER +CREATE VIEW +DEALLOCATE +DECLARE +DELETE +DROP DATABASE +DROP FUNCTION +DROP GROUP +DROP LIBRARY +DROP SCHEMA +DROP TABLE +DROP USER +DROP VIEW +END +EXECUTE +EXPLAIN +FETCH +GRANT +INSERT +LOCK +PREPARE +RESET +REVOKE +ROLLBACK +SELECT +SELECT INTO +SET +SET SESSION AUTHORIZATION +SET SESSION CHARACTERISTICS +SHOW +START TRANSACTION +TRUNCATE +UNLOAD +UPDATE +VACUUM + + From 6480a095355a911c052e13a23c5459d0227f5a8d Mon Sep 17 00:00:00 2001 From: Robins Date: Wed, 9 Aug 2017 20:20:11 +1000 Subject: [PATCH 003/149] Update the relevant READMEs to confirm that this is not an officially supported / endorsed branch --- README | 2 ++ README.git | 2 ++ src/bin/psql/README | 22 ++++++++++------------ 3 files changed, 14 insertions(+), 12 deletions(-) diff --git a/README b/README index 12de3f1d73cc9..a564b7decd415 100644 --- a/README +++ b/README @@ -1,3 +1,5 @@ +NOTE: This branch is maintained as a personal effort and is not sponsored / supported by my Employer or any other third-party. This is not officially supported / endorsed by Amazon in any way. + PostgreSQL Database Management System ===================================== diff --git a/README.git b/README.git index 52cb7223dfb12..9fe75023653be 100644 --- a/README.git +++ b/README.git @@ -1,3 +1,5 @@ +NOTE: This branch is maintained as a personal effort and is not sponsored / supported by my Employer or any other third-party. This is not officially supported / endorsed by Amazon in any way. + (This file does not appear in release tarballs.) In a release or snapshot tarball of PostgreSQL, a documentation file named diff --git a/src/bin/psql/README b/src/bin/psql/README index 6011d2d5727e9..d3448d161437c 100644 --- a/src/bin/psql/README +++ b/src/bin/psql/README @@ -1,19 +1,17 @@ -Redshift Support - Status Report +Redshift Support -This document lists the current status of Redshift Support +NOTE: This branch is maintained as a personal effort and is not sponsored / supported by my Employer or any other third-party. This is not officially supported / endorsed by Amazon in any way. -Legend -Pending Review -Supported -Partially Supported -Not Supported +This document lists the current status of Redshift Support. Unmarked items are probably Pending Review and not necessarily Unsupported. SQL Commands -ABORT -ALTER DATABASE -ALTER DEFAULT PRIVILEGES -ALTER GROUP -ALTER SCHEMA +============ + +ABORT - Supported +ALTER DATABASE - Supported +ALTER DEFAULT PRIVILEGES - Supported +ALTER GROUP - Supported +ALTER SCHEMA - Supported ALTER TABLE ALTER TABLE APPEND ALTER USER From 7962b17bd1363dd053b11262fba4f01875f1f37f Mon Sep 17 00:00:00 2001 From: Robins Date: Wed, 9 Aug 2017 21:52:58 +1000 Subject: [PATCH 004/149] Still working on generating a list of supported features. More evaluations entered in --- src/bin/psql/README | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/src/bin/psql/README b/src/bin/psql/README index d3448d161437c..ab1bca7cd7109 100644 --- a/src/bin/psql/README +++ b/src/bin/psql/README @@ -12,9 +12,17 @@ ALTER DATABASE - Supported ALTER DEFAULT PRIVILEGES - Supported ALTER GROUP - Supported ALTER SCHEMA - Supported -ALTER TABLE -ALTER TABLE APPEND -ALTER USER +ALTER TABLE - Supported except +- ALTER TABLE ADD COLUMN ENCODING - Unsupported +- ALTER TABLE SET LOCATION 's3://bucket/folder' - Unsupported +- ALTER TABLE SET FILE FORMAT format - Unsupported +- ALTER TABLE SET TABLE PROPERTIES ( 'numRows'='row_count' ) - Unsupported +- ALTER TABLE PARTITION ( partition_column=partition_value [, ...] ) SET LOCATION 's3://bucket/folder' - Unsupported +- ALTER TABLE ADD PARTITION ( partition_column=partition_value [, ...] ) LOCATION 's3://bucket/folder' - Unsupported +- ALTER TABLE DROP PARTITION ( partition_column=partition_value [, ...] ) - Unsupported + +ALTER TABLE APPEND - Unsupported +ALTER USER - ANALYZE ANALYZE COMPRESSION BEGIN From db6753888b9617b520142517fcea9d5caef3914c Mon Sep 17 00:00:00 2001 From: Robins Date: Wed, 9 Aug 2017 21:53:43 +1000 Subject: [PATCH 005/149] Moving src/bin/psql/README to markdown --- src/bin/psql/{README => README.md} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename src/bin/psql/{README => README.md} (100%) diff --git a/src/bin/psql/README b/src/bin/psql/README.md similarity index 100% rename from src/bin/psql/README rename to src/bin/psql/README.md From b126260b68c616da861c730d2f0d64e87535dc03 Mon Sep 17 00:00:00 2001 From: Robins Date: Wed, 9 Aug 2017 22:01:48 +1000 Subject: [PATCH 006/149] Move src/bin/psql/README list of supported SQL to table. Still WIP --- src/bin/psql/README.md | 36 +++++++++++++++++------------------- 1 file changed, 17 insertions(+), 19 deletions(-) diff --git a/src/bin/psql/README.md b/src/bin/psql/README.md index ab1bca7cd7109..9ddeefb764dd9 100644 --- a/src/bin/psql/README.md +++ b/src/bin/psql/README.md @@ -4,25 +4,23 @@ NOTE: This branch is maintained as a personal effort and is not sponsored / supp This document lists the current status of Redshift Support. Unmarked items are probably Pending Review and not necessarily Unsupported. -SQL Commands -============ - -ABORT - Supported -ALTER DATABASE - Supported -ALTER DEFAULT PRIVILEGES - Supported -ALTER GROUP - Supported -ALTER SCHEMA - Supported -ALTER TABLE - Supported except -- ALTER TABLE ADD COLUMN ENCODING - Unsupported -- ALTER TABLE SET LOCATION 's3://bucket/folder' - Unsupported -- ALTER TABLE SET FILE FORMAT format - Unsupported -- ALTER TABLE SET TABLE PROPERTIES ( 'numRows'='row_count' ) - Unsupported -- ALTER TABLE PARTITION ( partition_column=partition_value [, ...] ) SET LOCATION 's3://bucket/folder' - Unsupported -- ALTER TABLE ADD PARTITION ( partition_column=partition_value [, ...] ) LOCATION 's3://bucket/folder' - Unsupported -- ALTER TABLE DROP PARTITION ( partition_column=partition_value [, ...] ) - Unsupported - -ALTER TABLE APPEND - Unsupported -ALTER USER - +SQL Commands | Support +:----------- |:-------: +ABORT | Supported +ALTER DATABASE | Supported +ALTER DEFAULT PRIVILEGES | Supported +ALTER GROUP | Supported +ALTER SCHEMA | Supported +ALTER TABLE | Partial Support +- ALTER TABLE ADD COLUMN ENCODING | Unsupported +- ALTER TABLE SET LOCATION 's3://bucket/folder' | Unsupported +- ALTER TABLE SET FILE FORMAT format | Unsupported +- ALTER TABLE SET TABLE PROPERTIES ( 'numRows'='row_count' ) | Unsupported +- ALTER TABLE PARTITION ( partition_column=partition_value [, ...] ) SET LOCATION 's3://bucket/folder' | Unsupported +- ALTER TABLE ADD PARTITION ( partition_column=partition_value [, ...] ) LOCATION 's3://bucket/folder' | Unsupported +- ALTER TABLE DROP PARTITION ( partition_column=partition_value [, ...] ) | Unsupported +ALTER TABLE APPEND | Unsupported +ALTER USER | ANALYZE ANALYZE COMPRESSION BEGIN From 393fbcbd6f3dd5de1065d4f0bcadd36dc6f645d8 Mon Sep 17 00:00:00 2001 From: Robins Date: Wed, 9 Aug 2017 22:03:38 +1000 Subject: [PATCH 007/149] Update SQL supported status for all Redshift SQL Commands --- src/bin/psql/README.md | 119 ++++++++++++++++++++--------------------- 1 file changed, 59 insertions(+), 60 deletions(-) diff --git a/src/bin/psql/README.md b/src/bin/psql/README.md index 9ddeefb764dd9..46ac165c2aef6 100644 --- a/src/bin/psql/README.md +++ b/src/bin/psql/README.md @@ -12,66 +12,65 @@ ALTER DEFAULT PRIVILEGES | Supported ALTER GROUP | Supported ALTER SCHEMA | Supported ALTER TABLE | Partial Support -- ALTER TABLE ADD COLUMN ENCODING | Unsupported -- ALTER TABLE SET LOCATION 's3://bucket/folder' | Unsupported -- ALTER TABLE SET FILE FORMAT format | Unsupported -- ALTER TABLE SET TABLE PROPERTIES ( 'numRows'='row_count' ) | Unsupported -- ALTER TABLE PARTITION ( partition_column=partition_value [, ...] ) SET LOCATION 's3://bucket/folder' | Unsupported -- ALTER TABLE ADD PARTITION ( partition_column=partition_value [, ...] ) LOCATION 's3://bucket/folder' | Unsupported -- ALTER TABLE DROP PARTITION ( partition_column=partition_value [, ...] ) | Unsupported +ALTER TABLE ADD COLUMN ENCODING | Unsupported +ALTER TABLE SET LOCATION 's3://bucket/folder' | Unsupported +ALTER TABLE SET FILE FORMAT format | Unsupported +ALTER TABLE SET TABLE PROPERTIES ( 'numRows'='row_count' ) | Unsupported +ALTER TABLE PARTITION ( partition_column=partition_value [, ...] ) SET LOCATION 's3://bucket/folder' | Unsupported +ALTER TABLE ADD PARTITION ( partition_column=partition_value [, ...] ) LOCATION 's3://bucket/folder' | Unsupported +ALTER TABLE DROP PARTITION ( partition_column=partition_value [, ...] ) | Unsupported ALTER TABLE APPEND | Unsupported ALTER USER | -ANALYZE -ANALYZE COMPRESSION -BEGIN -CANCEL -CLOSE -COMMENT -COMMIT -COPY -CREATE DATABASE -CREATE EXTERNAL SCHEMA -CREATE EXTERNAL TABLE -CREATE FUNCTION -CREATE GROUP -CREATE LIBRARY -CREATE SCHEMA -CREATE TABLE -CREATE TABLE AS -CREATE USER -CREATE VIEW -DEALLOCATE -DECLARE -DELETE -DROP DATABASE -DROP FUNCTION -DROP GROUP -DROP LIBRARY -DROP SCHEMA -DROP TABLE -DROP USER -DROP VIEW -END -EXECUTE -EXPLAIN -FETCH -GRANT -INSERT -LOCK -PREPARE -RESET -REVOKE -ROLLBACK -SELECT -SELECT INTO -SET -SET SESSION AUTHORIZATION -SET SESSION CHARACTERISTICS -SHOW -START TRANSACTION -TRUNCATE -UNLOAD -UPDATE -VACUUM - +ANALYZE | Pending Review +ANALYZE COMPRESSION | Pending Review +BEGIN | Pending Review +CANCEL | Pending Review +CLOSE | Pending Review +COMMENT | Pending Review +COMMIT | Pending Review +COPY | Pending Review +CREATE DATABASE | Pending Review +CREATE EXTERNAL SCHEMA | Pending Review +CREATE EXTERNAL TABLE | Pending Review +CREATE FUNCTION | Pending Review +CREATE GROUP | Pending Review +CREATE LIBRARY | Pending Review +CREATE SCHEMA | Pending Review +CREATE TABLE | Pending Review +CREATE TABLE AS | Pending Review +CREATE USER | Pending Review +CREATE VIEW | Pending Review +DEALLOCATE | Pending Review +DECLARE | Pending Review +DELETE | Pending Review +DROP DATABASE | Pending Review +DROP FUNCTION | Pending Review +DROP GROUP | Pending Review +DROP LIBRARY | Pending Review +DROP SCHEMA | Pending Review +DROP TABLE | Pending Review +DROP USER | Pending Review +DROP VIEW | Pending Review +END | Pending Review +EXECUTE | Pending Review +EXPLAIN | Pending Review +FETCH | Pending Review +GRANT | Pending Review +INSERT | Pending Review +LOCK | Pending Review +PREPARE | Pending Review +RESET | Pending Review +REVOKE | Pending Review +ROLLBACK | Pending Review +SELECT | Pending Review +SELECT INTO | Pending Review +SET | Pending Review +SET SESSION AUTHORIZATION | Pending Review +SET SESSION CHARACTERISTICS | Pending Review +SHOW | Pending Review +START TRANSACTION | Pending Review +TRUNCATE | Pending Review +UNLOAD | Pending Review +UPDATE | Pending Review +VACUUM | Pending Review From 964aa42a41fa9ed41dba91dc24e84e9393969af9 Mon Sep 17 00:00:00 2001 From: Robins Date: Wed, 9 Aug 2017 22:14:09 +1000 Subject: [PATCH 008/149] Update SQL supported status for more SQL Commands --- src/bin/psql/README.md | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/src/bin/psql/README.md b/src/bin/psql/README.md index 46ac165c2aef6..079d012cd6060 100644 --- a/src/bin/psql/README.md +++ b/src/bin/psql/README.md @@ -1,17 +1,17 @@ -Redshift Support +psql Auto-Complete support for Amazon Redshift NOTE: This branch is maintained as a personal effort and is not sponsored / supported by my Employer or any other third-party. This is not officially supported / endorsed by Amazon in any way. -This document lists the current status of Redshift Support. Unmarked items are probably Pending Review and not necessarily Unsupported. +This document lists the current state of psql Auto-Complete support for Redshift commands. -SQL Commands | Support +SQL Commands | Redshift Support :----------- |:-------: ABORT | Supported ALTER DATABASE | Supported ALTER DEFAULT PRIVILEGES | Supported ALTER GROUP | Supported ALTER SCHEMA | Supported -ALTER TABLE | Partial Support +ALTER TABLE | **Partial Support** ALTER TABLE ADD COLUMN ENCODING | Unsupported ALTER TABLE SET LOCATION 's3://bucket/folder' | Unsupported ALTER TABLE SET FILE FORMAT format | Unsupported @@ -20,8 +20,11 @@ ALTER TABLE PARTITION ( partition_column=partition_value [, ...] ) SET LOCATION ALTER TABLE ADD PARTITION ( partition_column=partition_value [, ...] ) LOCATION 's3://bucket/folder' | Unsupported ALTER TABLE DROP PARTITION ( partition_column=partition_value [, ...] ) | Unsupported ALTER TABLE APPEND | Unsupported -ALTER USER | -ANALYZE | Pending Review +ALTER USER | Supported +ANALYZE | **Partial Support** +ANALYZE VERBOSE | Unsupported +ANALYSE PREDICATE COLUMNS | Unsupported +ANALYZE ALL COLUMNS | Unsupported ANALYZE COMPRESSION | Pending Review BEGIN | Pending Review CANCEL | Pending Review From d2a37692aec4c1ef5c762f69628322e7e334af64 Mon Sep 17 00:00:00 2001 From: Robins Date: Wed, 9 Aug 2017 22:24:59 +1000 Subject: [PATCH 009/149] Update SQL supported status for more SQL Commands --- src/bin/psql/README.md | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/src/bin/psql/README.md b/src/bin/psql/README.md index 079d012cd6060..c064e343992a6 100644 --- a/src/bin/psql/README.md +++ b/src/bin/psql/README.md @@ -11,7 +11,7 @@ ALTER DATABASE | Supported ALTER DEFAULT PRIVILEGES | Supported ALTER GROUP | Supported ALTER SCHEMA | Supported -ALTER TABLE | **Partial Support** +ALTER TABLE | **Partial-Support** ALTER TABLE ADD COLUMN ENCODING | Unsupported ALTER TABLE SET LOCATION 's3://bucket/folder' | Unsupported ALTER TABLE SET FILE FORMAT format | Unsupported @@ -21,18 +21,19 @@ ALTER TABLE ADD PARTITION ( partition_column=partition_value [, ...] ) LOCATION ALTER TABLE DROP PARTITION ( partition_column=partition_value [, ...] ) | Unsupported ALTER TABLE APPEND | Unsupported ALTER USER | Supported -ANALYZE | **Partial Support** +ANALYZE | **Partial-Support** ANALYZE VERBOSE | Unsupported ANALYSE PREDICATE COLUMNS | Unsupported ANALYZE ALL COLUMNS | Unsupported -ANALYZE COMPRESSION | Pending Review -BEGIN | Pending Review -CANCEL | Pending Review -CLOSE | Pending Review -COMMENT | Pending Review -COMMIT | Pending Review +ANALYZE COMPRESSION | Unsupported +BEGIN | Supported +CANCEL | Unsupported +CLOSE | Unsupported +COMMENT | Supported +COMMIT | Supported COPY | Pending Review -CREATE DATABASE | Pending Review +CREATE DATABASE | **Partial-Support** +CREATE DATABASE CONNECTION LIMIT UNLIMITED | Unsupported CREATE EXTERNAL SCHEMA | Pending Review CREATE EXTERNAL TABLE | Pending Review CREATE FUNCTION | Pending Review From e3f0dfb37331a1b1286adfb9017e1c5ce6d6ea2f Mon Sep 17 00:00:00 2001 From: Robins Date: Wed, 9 Aug 2017 22:54:39 +1000 Subject: [PATCH 010/149] Update SQL supported status for more SQL Commands --- src/bin/psql/README.md | 49 ++++++++++++++++++++++++++---------------- 1 file changed, 30 insertions(+), 19 deletions(-) diff --git a/src/bin/psql/README.md b/src/bin/psql/README.md index c064e343992a6..743f331cf6170 100644 --- a/src/bin/psql/README.md +++ b/src/bin/psql/README.md @@ -20,7 +20,8 @@ ALTER TABLE PARTITION ( partition_column=partition_value [, ...] ) SET LOCATION ALTER TABLE ADD PARTITION ( partition_column=partition_value [, ...] ) LOCATION 's3://bucket/folder' | Unsupported ALTER TABLE DROP PARTITION ( partition_column=partition_value [, ...] ) | Unsupported ALTER TABLE APPEND | Unsupported -ALTER USER | Supported +ALTER USER | **Partial-Support** +ALTER USER [NO]CREATEUSER | Unsupported ANALYZE | **Partial-Support** ANALYZE VERBOSE | Unsupported ANALYSE PREDICATE COLUMNS | Unsupported @@ -34,26 +35,36 @@ COMMIT | Supported COPY | Pending Review CREATE DATABASE | **Partial-Support** CREATE DATABASE CONNECTION LIMIT UNLIMITED | Unsupported -CREATE EXTERNAL SCHEMA | Pending Review -CREATE EXTERNAL TABLE | Pending Review -CREATE FUNCTION | Pending Review -CREATE GROUP | Pending Review -CREATE LIBRARY | Pending Review -CREATE SCHEMA | Pending Review +CREATE EXTERNAL SCHEMA | Unsupported +CREATE EXTERNAL TABLE | Unsupported +CREATE FUNCTION | **Partial-Support** +CREATE OR REPLACE FUNCTION | Unsupported +CREATE GROUP | Supported +CREATE LIBRARY | Unsupported +CREATE SCHEMA | **Partial-Support** +CREATE SCHEMA AUTHORIZATION | Unsupported +CREATE SCHEMA IF NOT EXISTS | Unsupported CREATE TABLE | Pending Review CREATE TABLE AS | Pending Review -CREATE USER | Pending Review -CREATE VIEW | Pending Review -DEALLOCATE | Pending Review -DECLARE | Pending Review -DELETE | Pending Review -DROP DATABASE | Pending Review -DROP FUNCTION | Pending Review -DROP GROUP | Pending Review -DROP LIBRARY | Pending Review -DROP SCHEMA | Pending Review -DROP TABLE | Pending Review -DROP USER | Pending Review +CREATE USER | **Partial-Support** +CREATE USER [NO]CREATEUSER | Unsupported +CREATE VIEW | **Partial-Support** +CREATE OR REPLACE VIEW | Unsupported +DEALLOCATE | **Partial-Support** +DEALLOCATE PREPARE | Unsupported +DECLARE | Supported +DELETE | **Partial-Support** +DELETE FROM USING WHERE | Unsupported +DROP DATABASE | Supported +DROP FUNCTION | Supported +DROP GROUP | Supported +DROP LIBRARY | Unsupported +DROP SCHEMA | **Partial-Support** +DROP SCHEMA IF EXISTS | Unsupported +DROP TABLE | **Partial-Support** +DROP TABLE | Unsupported +DROP USER | **Partial-Support** +DROP USER IF EXISTS | Unsupported DROP VIEW | Pending Review END | Pending Review EXECUTE | Pending Review From 03a421c109a39a02b831c5bc2d3e99875beb92ce Mon Sep 17 00:00:00 2001 From: Robins Date: Wed, 9 Aug 2017 22:56:05 +1000 Subject: [PATCH 011/149] Update SQL supported status for more SQL Commands --- src/bin/psql/README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/bin/psql/README.md b/src/bin/psql/README.md index 743f331cf6170..784d66685036c 100644 --- a/src/bin/psql/README.md +++ b/src/bin/psql/README.md @@ -65,7 +65,8 @@ DROP TABLE | **Partial-Support** DROP TABLE | Unsupported DROP USER | **Partial-Support** DROP USER IF EXISTS | Unsupported -DROP VIEW | Pending Review +DROP VIEW | **Partial-Support** +DROP VIEW IF EXISTS | Unsupported END | Pending Review EXECUTE | Pending Review EXPLAIN | Pending Review From a90ddc032358247a7944e7375c493997dd6bedf0 Mon Sep 17 00:00:00 2001 From: Robins Date: Wed, 9 Aug 2017 22:57:44 +1000 Subject: [PATCH 012/149] Update SQL supported status for more SQL Commands --- src/bin/psql/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/bin/psql/README.md b/src/bin/psql/README.md index 784d66685036c..19733ca21d7b9 100644 --- a/src/bin/psql/README.md +++ b/src/bin/psql/README.md @@ -67,7 +67,7 @@ DROP USER | **Partial-Support** DROP USER IF EXISTS | Unsupported DROP VIEW | **Partial-Support** DROP VIEW IF EXISTS | Unsupported -END | Pending Review +END | Supported EXECUTE | Pending Review EXPLAIN | Pending Review FETCH | Pending Review From a651a8c729ecb83037c6d440985ff0555a45508c Mon Sep 17 00:00:00 2001 From: Robins Date: Thu, 10 Aug 2017 04:54:41 +1000 Subject: [PATCH 013/149] Update SQL supported status for more SQL Commands --- src/bin/psql/README.md | 68 +++++++++++++++++++++++++++++------------- 1 file changed, 48 insertions(+), 20 deletions(-) diff --git a/src/bin/psql/README.md b/src/bin/psql/README.md index 19733ca21d7b9..513a4726eeea1 100644 --- a/src/bin/psql/README.md +++ b/src/bin/psql/README.md @@ -32,7 +32,7 @@ CANCEL | Unsupported CLOSE | Unsupported COMMENT | Supported COMMIT | Supported -COPY | Pending Review +COPY | Unsupported CREATE DATABASE | **Partial-Support** CREATE DATABASE CONNECTION LIMIT UNLIMITED | Unsupported CREATE EXTERNAL SCHEMA | Unsupported @@ -44,7 +44,14 @@ CREATE LIBRARY | Unsupported CREATE SCHEMA | **Partial-Support** CREATE SCHEMA AUTHORIZATION | Unsupported CREATE SCHEMA IF NOT EXISTS | Unsupported -CREATE TABLE | Pending Review +CREATE TABLE | **Partial-Support** +CREATE LOCAL TABLE | Unsupported +CREATE TABLE IF | Unsupported +CREATE TABLE BACKUP | Unsupported +CREATE TABLE DISTSTYLE | Unsupported +CREATE TABLE DISTKEY | Unsupported +CREATE TABLE SORTKEY | Unsupported +CREATE TABLE | Unsupported CREATE TABLE AS | Pending Review CREATE USER | **Partial-Support** CREATE USER [NO]CREATEUSER | Unsupported @@ -68,25 +75,46 @@ DROP USER IF EXISTS | Unsupported DROP VIEW | **Partial-Support** DROP VIEW IF EXISTS | Unsupported END | Supported -EXECUTE | Pending Review -EXPLAIN | Pending Review -FETCH | Pending Review -GRANT | Pending Review +EXECUTE | Supported +EXPLAIN | Supported +FETCH | **Partial-Support** +FETCH NEXT | Unsupported +FETCH ALL | Unsupported +GRANT | **Partial-Support** +GRANT ALL PRIVILEGES | Unsupported +GRANT ALL ON TABLE ALL TABLES IN SCHEMA | Unsupported +GRANT ALL ON TABLE TO GROUP | Unsupported +GRANT ALL ON TABLE ALL TABLES IN SCHEMA | Unsupported +GRANT CREATE ALL | Unsupported +GRANT CREATE ON | Unsupported +GRANT | Unsupported INSERT | Pending Review -LOCK | Pending Review -PREPARE | Pending Review -RESET | Pending Review -REVOKE | Pending Review -ROLLBACK | Pending Review +LOCK | Supported +PREPARE | **Partial-Support** +PREPARE AS | Unsupported +RESET | **Partial-Support** +RESET ALL | Unsupported +REVOKE | **Partial-Support** +REVOKE GRANT | Unsupported +REVOKE SELECT ALL PRIVILEGES | Unsupported +REVOKE | Unsupported +ROLLBACK | Supported SELECT | Pending Review -SELECT INTO | Pending Review -SET | Pending Review -SET SESSION AUTHORIZATION | Pending Review -SET SESSION CHARACTERISTICS | Pending Review -SHOW | Pending Review -START TRANSACTION | Pending Review -TRUNCATE | Pending Review +SELECT INTO | Unsupported +SET | **Partial-Support** +SET ALL | Unsupported +SET SEED TO | Unsupported +SET SESSION AUTHORIZATION | Unsupported +SET SESSION CHARACTERISTICS | Supported +SHOW | **Partial-Support** +SHOW ALL | Unsupported +START TRANSACTION | Supported +TRUNCATE | **Partial-Support** +TRUNCATE TABLE | Unsupported UNLOAD | Pending Review UPDATE | Pending Review -VACUUM | Pending Review - +VACUUM | **Partial-Support** +VACUUM SORT | Unsupported +VACUUM DELETE | Unsupported +VACUUM REINDEX | Unsupported +VACUUM TO | Unsupported \ No newline at end of file From 5f4fc3163053b64af94598f19efef1790d2af385 Mon Sep 17 00:00:00 2001 From: Robins Date: Thu, 10 Aug 2017 05:04:28 +1000 Subject: [PATCH 014/149] Update all Redshift SQL Commands with a first round of thorough review done --- src/bin/psql/README.md | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/src/bin/psql/README.md b/src/bin/psql/README.md index 513a4726eeea1..73e4597e83fff 100644 --- a/src/bin/psql/README.md +++ b/src/bin/psql/README.md @@ -52,7 +52,7 @@ CREATE TABLE DISTSTYLE | Unsupported CREATE TABLE DISTKEY | Unsupported CREATE TABLE SORTKEY | Unsupported CREATE TABLE | Unsupported -CREATE TABLE AS | Pending Review +CREATE TABLE AS | Unsupported CREATE USER | **Partial-Support** CREATE USER [NO]CREATEUSER | Unsupported CREATE VIEW | **Partial-Support** @@ -88,7 +88,8 @@ GRANT ALL ON TABLE ALL TABLES IN SCHEMA | Unsupported GRANT CREATE ALL | Unsupported GRANT CREATE ON | Unsupported GRANT | Unsupported -INSERT | Pending Review +INSERT | **Partial-Support** +INSERT INTO DEFAULT VALUES | Unsupported LOCK | Supported PREPARE | **Partial-Support** PREPARE AS | Unsupported @@ -99,7 +100,11 @@ REVOKE GRANT | Unsupported REVOKE SELECT ALL PRIVILEGES | Unsupported REVOKE | Unsupported ROLLBACK | Supported -SELECT | Pending Review +SELECT | **Partial-Support** +SELECT TOP | Unsupported +SELECT ALL | Unsupported +SELECT DISTINCT | Unsupported +SELECT | Unsupported SELECT INTO | Unsupported SET | **Partial-Support** SET ALL | Unsupported @@ -111,8 +116,10 @@ SHOW ALL | Unsupported START TRANSACTION | Supported TRUNCATE | **Partial-Support** TRUNCATE TABLE | Unsupported -UNLOAD | Pending Review -UPDATE | Pending Review +UNLOAD | Unsupported +UPDATE | Unsupported +UPDATE SET FROM | Unsupported +UPDATE SET WHERE | Unsupported VACUUM | **Partial-Support** VACUUM SORT | Unsupported VACUUM DELETE | Unsupported From cb391e304d3a04bd6fb574f4d257e49e43828f53 Mon Sep 17 00:00:00 2001 From: Robins Tharakan Date: Wed, 9 Aug 2017 19:20:07 +0000 Subject: [PATCH 015/149] Add support for ALTER USER [NO]CREATEUSER --- src/bin/psql/tab-complete.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/bin/psql/tab-complete.c b/src/bin/psql/tab-complete.c index e34922dd731cd..27b1ee8b4b823 100644 --- a/src/bin/psql/tab-complete.c +++ b/src/bin/psql/tab-complete.c @@ -1677,9 +1677,9 @@ psql_completion(const char *text, int start, int end) !TailMatches2("USER", "MAPPING")) { static const char *const list_ALTERUSER[] = - {"BYPASSRLS", "CONNECTION LIMIT", "CREATEDB", "CREATEROLE", + {"BYPASSRLS", "CONNECTION LIMIT", "CREATEDB", "CREATEROLE", "CREATEUSER", "ENCRYPTED PASSWORD", "INHERIT", "LOGIN", "NOBYPASSRLS", - "NOCREATEDB", "NOCREATEROLE", "NOINHERIT", + "NOCREATEDB", "NOCREATEROLE", "NOCREATEUSER", "NOINHERIT", "NOLOGIN", "NOREPLICATION", "NOSUPERUSER", "PASSWORD", "RENAME TO", "REPLICATION", "RESET", "SET", "SUPERUSER", "VALID UNTIL", "WITH", NULL}; From 5b2fb0158f594714a32be9b26a5782f5edb7d3cc Mon Sep 17 00:00:00 2001 From: Robins Tharakan Date: Wed, 9 Aug 2017 19:22:01 +0000 Subject: [PATCH 016/149] Update src/bin/psql/README.md with recent support for ALTER USER [NO]CREATEUSER --- src/bin/psql/README.md | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/bin/psql/README.md b/src/bin/psql/README.md index 73e4597e83fff..caae5a04c19a7 100644 --- a/src/bin/psql/README.md +++ b/src/bin/psql/README.md @@ -20,8 +20,7 @@ ALTER TABLE PARTITION ( partition_column=partition_value [, ...] ) SET LOCATION ALTER TABLE ADD PARTITION ( partition_column=partition_value [, ...] ) LOCATION 's3://bucket/folder' | Unsupported ALTER TABLE DROP PARTITION ( partition_column=partition_value [, ...] ) | Unsupported ALTER TABLE APPEND | Unsupported -ALTER USER | **Partial-Support** -ALTER USER [NO]CREATEUSER | Unsupported +ALTER USER | Supported ANALYZE | **Partial-Support** ANALYZE VERBOSE | Unsupported ANALYSE PREDICATE COLUMNS | Unsupported @@ -124,4 +123,4 @@ VACUUM | **Partial-Support** VACUUM SORT | Unsupported VACUUM DELETE | Unsupported VACUUM REINDEX | Unsupported -VACUUM TO | Unsupported \ No newline at end of file +VACUUM TO | Unsupported From 81994d9ba57922a8d69e8e79c4fec5cbc683fdcc Mon Sep 17 00:00:00 2001 From: Robins Date: Thu, 10 Aug 2017 05:26:44 +1000 Subject: [PATCH 017/149] Add a legend to Redshift support matrix --- src/bin/psql/README.md | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/bin/psql/README.md b/src/bin/psql/README.md index 73e4597e83fff..8b344170169df 100644 --- a/src/bin/psql/README.md +++ b/src/bin/psql/README.md @@ -1,8 +1,14 @@ -psql Auto-Complete support for Amazon Redshift +This document shows the current state of psql's auto-complete support for Amazon Redshift NOTE: This branch is maintained as a personal effort and is not sponsored / supported by my Employer or any other third-party. This is not officially supported / endorsed by Amazon in any way. -This document lists the current state of psql Auto-Complete support for Redshift commands. +Support Legend | Description +-------------- | ----------- +Supported | All possible combinations are supported by psql's auto-complete feature +Unsupported | SQL Command not supported +Partial-Support | Only some possible combinations are supported by psql's auto-complete feature + + SQL Commands | Redshift Support :----------- |:-------: From ae70b6c03b1831d04e5f0d7c187b71a5ee22c2aa Mon Sep 17 00:00:00 2001 From: Robins Tharakan Date: Wed, 9 Aug 2017 19:27:38 +0000 Subject: [PATCH 018/149] Highlight that recently added ALTER USER support is custom to this branch --- src/bin/psql/README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/bin/psql/README.md b/src/bin/psql/README.md index caae5a04c19a7..50070857cce50 100644 --- a/src/bin/psql/README.md +++ b/src/bin/psql/README.md @@ -20,7 +20,8 @@ ALTER TABLE PARTITION ( partition_column=partition_value [, ...] ) SET LOCATION ALTER TABLE ADD PARTITION ( partition_column=partition_value [, ...] ) LOCATION 's3://bucket/folder' | Unsupported ALTER TABLE DROP PARTITION ( partition_column=partition_value [, ...] ) | Unsupported ALTER TABLE APPEND | Unsupported -ALTER USER | Supported +ALTER USER | Yes +ALTER USER [NO]CREATEROLE | Yes ANALYZE | **Partial-Support** ANALYZE VERBOSE | Unsupported ANALYSE PREDICATE COLUMNS | Unsupported From 79d36ce4ef56dc6440f980209060fb5fde4ce13b Mon Sep 17 00:00:00 2001 From: Robins Date: Thu, 10 Aug 2017 05:31:16 +1000 Subject: [PATCH 019/149] Add a legend to Redshift support matrix --- src/bin/psql/README.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/bin/psql/README.md b/src/bin/psql/README.md index d0ac8df8ae40e..310c74cea4519 100644 --- a/src/bin/psql/README.md +++ b/src/bin/psql/README.md @@ -2,6 +2,7 @@ This document shows the current state of psql's auto-complete support for Amazon NOTE: This branch is maintained as a personal effort and is not sponsored / supported by my Employer or any other third-party. This is not officially supported / endorsed by Amazon in any way. + Support Legend | Description -------------- | ----------- Supported | All possible combinations are supported by psql's auto-complete feature @@ -10,6 +11,7 @@ Partial-Support | Only some possible combinations are supported by psql's auto-c + SQL Commands | Redshift Support :----------- |:-------: ABORT | Supported @@ -27,7 +29,7 @@ ALTER TABLE ADD PARTITION ( partition_column=partition_value [, ...] ) LOCATION ALTER TABLE DROP PARTITION ( partition_column=partition_value [, ...] ) | Unsupported ALTER TABLE APPEND | Unsupported ALTER USER | Yes -ALTER USER [NO]CREATEROLE | Yes +ALTER USER [NO]CREATEUSER | Yes ANALYZE | **Partial-Support** ANALYZE VERBOSE | Unsupported ANALYSE PREDICATE COLUMNS | Unsupported From d981486c62ed0d8fceee2b9f95176d47e3296216 Mon Sep 17 00:00:00 2001 From: Robins Date: Thu, 10 Aug 2017 05:36:59 +1000 Subject: [PATCH 020/149] Update legend to correct describe states --- src/bin/psql/README.md | 102 ++++++++++++++++++++--------------------- 1 file changed, 51 insertions(+), 51 deletions(-) diff --git a/src/bin/psql/README.md b/src/bin/psql/README.md index 310c74cea4519..92cf078aacd2d 100644 --- a/src/bin/psql/README.md +++ b/src/bin/psql/README.md @@ -5,21 +5,21 @@ NOTE: This branch is maintained as a personal effort and is not sponsored / supp Support Legend | Description -------------- | ----------- -Supported | All possible combinations are supported by psql's auto-complete feature -Unsupported | SQL Command not supported -Partial-Support | Only some possible combinations are supported by psql's auto-complete feature - +Native | All combinations supported by Native psql +Unsupported | Not supported (yet) +Partial | Only some combinations supported +**Yes** | Feature supported (partly) by this branch SQL Commands | Redshift Support :----------- |:-------: -ABORT | Supported -ALTER DATABASE | Supported -ALTER DEFAULT PRIVILEGES | Supported -ALTER GROUP | Supported -ALTER SCHEMA | Supported -ALTER TABLE | **Partial-Support** +ABORT | Native +ALTER DATABASE | Native +ALTER DEFAULT PRIVILEGES | Native +ALTER GROUP | Native +ALTER SCHEMA | Native +ALTER TABLE | Partial ALTER TABLE ADD COLUMN ENCODING | Unsupported ALTER TABLE SET LOCATION 's3://bucket/folder' | Unsupported ALTER TABLE SET FILE FORMAT format | Unsupported @@ -28,31 +28,31 @@ ALTER TABLE PARTITION ( partition_column=partition_value [, ...] ) SET LOCATION ALTER TABLE ADD PARTITION ( partition_column=partition_value [, ...] ) LOCATION 's3://bucket/folder' | Unsupported ALTER TABLE DROP PARTITION ( partition_column=partition_value [, ...] ) | Unsupported ALTER TABLE APPEND | Unsupported -ALTER USER | Yes -ALTER USER [NO]CREATEUSER | Yes -ANALYZE | **Partial-Support** +ALTER USER | **Yes** +ALTER USER [NO]CREATEUSER | **Yes** +ANALYZE | Partial ANALYZE VERBOSE | Unsupported ANALYSE PREDICATE COLUMNS | Unsupported ANALYZE ALL COLUMNS | Unsupported ANALYZE COMPRESSION | Unsupported -BEGIN | Supported +BEGIN | Native CANCEL | Unsupported CLOSE | Unsupported -COMMENT | Supported -COMMIT | Supported +COMMENT | Native +COMMIT | Native COPY | Unsupported -CREATE DATABASE | **Partial-Support** +CREATE DATABASE | Partial CREATE DATABASE CONNECTION LIMIT UNLIMITED | Unsupported CREATE EXTERNAL SCHEMA | Unsupported CREATE EXTERNAL TABLE | Unsupported -CREATE FUNCTION | **Partial-Support** +CREATE FUNCTION | Partial CREATE OR REPLACE FUNCTION | Unsupported -CREATE GROUP | Supported +CREATE GROUP | Native CREATE LIBRARY | Unsupported -CREATE SCHEMA | **Partial-Support** +CREATE SCHEMA | Partial CREATE SCHEMA AUTHORIZATION | Unsupported CREATE SCHEMA IF NOT EXISTS | Unsupported -CREATE TABLE | **Partial-Support** +CREATE TABLE | Partial CREATE LOCAL TABLE | Unsupported CREATE TABLE IF | Unsupported CREATE TABLE BACKUP | Unsupported @@ -61,34 +61,34 @@ CREATE TABLE DISTKEY | Unsupported CREATE TABLE SORTKEY | Unsupported CREATE TABLE | Unsupported CREATE TABLE AS | Unsupported -CREATE USER | **Partial-Support** +CREATE USER | Partial CREATE USER [NO]CREATEUSER | Unsupported -CREATE VIEW | **Partial-Support** +CREATE VIEW | Partial CREATE OR REPLACE VIEW | Unsupported -DEALLOCATE | **Partial-Support** +DEALLOCATE | Partial DEALLOCATE PREPARE | Unsupported -DECLARE | Supported -DELETE | **Partial-Support** +DECLARE | Native +DELETE | Partial DELETE FROM USING WHERE | Unsupported -DROP DATABASE | Supported -DROP FUNCTION | Supported -DROP GROUP | Supported +DROP DATABASE | Native +DROP FUNCTION | Native +DROP GROUP | Native DROP LIBRARY | Unsupported -DROP SCHEMA | **Partial-Support** +DROP SCHEMA | Partial DROP SCHEMA IF EXISTS | Unsupported -DROP TABLE | **Partial-Support** +DROP TABLE | Partial DROP TABLE | Unsupported -DROP USER | **Partial-Support** +DROP USER | Partial DROP USER IF EXISTS | Unsupported -DROP VIEW | **Partial-Support** +DROP VIEW | Partial DROP VIEW IF EXISTS | Unsupported -END | Supported -EXECUTE | Supported -EXPLAIN | Supported -FETCH | **Partial-Support** +END | Native +EXECUTE | Native +EXPLAIN | Native +FETCH | Partial FETCH NEXT | Unsupported FETCH ALL | Unsupported -GRANT | **Partial-Support** +GRANT | Partial GRANT ALL PRIVILEGES | Unsupported GRANT ALL ON TABLE ALL TABLES IN SCHEMA | Unsupported GRANT ALL ON TABLE TO GROUP | Unsupported @@ -96,39 +96,39 @@ GRANT ALL ON TABLE ALL TABLES IN SCHEMA | Unsupported GRANT CREATE ALL | Unsupported GRANT CREATE ON | Unsupported GRANT | Unsupported -INSERT | **Partial-Support** +INSERT | Partial INSERT INTO DEFAULT VALUES | Unsupported -LOCK | Supported -PREPARE | **Partial-Support** +LOCK | Native +PREPARE | Partial PREPARE AS | Unsupported -RESET | **Partial-Support** +RESET | Partial RESET ALL | Unsupported -REVOKE | **Partial-Support** +REVOKE | Partial REVOKE GRANT | Unsupported REVOKE SELECT ALL PRIVILEGES | Unsupported REVOKE | Unsupported -ROLLBACK | Supported -SELECT | **Partial-Support** +ROLLBACK | Native +SELECT | Partial SELECT TOP | Unsupported SELECT ALL | Unsupported SELECT DISTINCT | Unsupported SELECT | Unsupported SELECT INTO | Unsupported -SET | **Partial-Support** +SET | Partial SET ALL | Unsupported SET SEED TO | Unsupported SET SESSION AUTHORIZATION | Unsupported -SET SESSION CHARACTERISTICS | Supported -SHOW | **Partial-Support** +SET SESSION CHARACTERISTICS | Native +SHOW | Partial SHOW ALL | Unsupported -START TRANSACTION | Supported -TRUNCATE | **Partial-Support** +START TRANSACTION | Native +TRUNCATE | Partial TRUNCATE TABLE | Unsupported UNLOAD | Unsupported UPDATE | Unsupported UPDATE SET FROM | Unsupported UPDATE SET WHERE | Unsupported -VACUUM | **Partial-Support** +VACUUM | Partial VACUUM SORT | Unsupported VACUUM DELETE | Unsupported VACUUM REINDEX | Unsupported From 6bcc574bb4bef25c60fd0587456771c58f7de964 Mon Sep 17 00:00:00 2001 From: Robins Date: Thu, 10 Aug 2017 05:40:26 +1000 Subject: [PATCH 021/149] Update legend to correctly describe states --- src/bin/psql/README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/bin/psql/README.md b/src/bin/psql/README.md index 92cf078aacd2d..a16d433cf0758 100644 --- a/src/bin/psql/README.md +++ b/src/bin/psql/README.md @@ -4,11 +4,11 @@ NOTE: This branch is maintained as a personal effort and is not sponsored / supp Support Legend | Description --------------- | ----------- +:-------------:| ----------- Native | All combinations supported by Native psql Unsupported | Not supported (yet) Partial | Only some combinations supported -**Yes** | Feature supported (partly) by this branch +**Yes** | Supported by this branch (not Native) From bba3d0d6b50dc537deb6949ad3c3d307d9951bca Mon Sep 17 00:00:00 2001 From: Robins Date: Thu, 10 Aug 2017 05:43:00 +1000 Subject: [PATCH 022/149] Added a Supported state to Legend --- src/bin/psql/README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/bin/psql/README.md b/src/bin/psql/README.md index a16d433cf0758..1b1c220c078e3 100644 --- a/src/bin/psql/README.md +++ b/src/bin/psql/README.md @@ -9,7 +9,7 @@ Native | All combinations supported by Native psql Unsupported | Not supported (yet) Partial | Only some combinations supported **Yes** | Supported by this branch (not Native) - +Supported | All combinations (now) supported by this branch SQL Commands | Redshift Support @@ -28,7 +28,7 @@ ALTER TABLE PARTITION ( partition_column=partition_value [, ...] ) SET LOCATION ALTER TABLE ADD PARTITION ( partition_column=partition_value [, ...] ) LOCATION 's3://bucket/folder' | Unsupported ALTER TABLE DROP PARTITION ( partition_column=partition_value [, ...] ) | Unsupported ALTER TABLE APPEND | Unsupported -ALTER USER | **Yes** +ALTER USER | Supported ALTER USER [NO]CREATEUSER | **Yes** ANALYZE | Partial ANALYZE VERBOSE | Unsupported From c98b753345166ab63188c0c5799ab39b57fdba73 Mon Sep 17 00:00:00 2001 From: Robins Date: Thu, 10 Aug 2017 05:48:52 +1000 Subject: [PATCH 023/149] Update table to show sub-items clearly --- src/bin/psql/README.md | 124 ++++++++++++++++++++--------------------- 1 file changed, 62 insertions(+), 62 deletions(-) diff --git a/src/bin/psql/README.md b/src/bin/psql/README.md index 1b1c220c078e3..86cad3c209e4f 100644 --- a/src/bin/psql/README.md +++ b/src/bin/psql/README.md @@ -20,21 +20,21 @@ ALTER DEFAULT PRIVILEGES | Native ALTER GROUP | Native ALTER SCHEMA | Native ALTER TABLE | Partial -ALTER TABLE ADD COLUMN ENCODING | Unsupported -ALTER TABLE SET LOCATION 's3://bucket/folder' | Unsupported -ALTER TABLE SET FILE FORMAT format | Unsupported -ALTER TABLE SET TABLE PROPERTIES ( 'numRows'='row_count' ) | Unsupported -ALTER TABLE PARTITION ( partition_column=partition_value [, ...] ) SET LOCATION 's3://bucket/folder' | Unsupported -ALTER TABLE ADD PARTITION ( partition_column=partition_value [, ...] ) LOCATION 's3://bucket/folder' | Unsupported -ALTER TABLE DROP PARTITION ( partition_column=partition_value [, ...] ) | Unsupported -ALTER TABLE APPEND | Unsupported +* ALTER TABLE ADD COLUMN ENCODING | Unsupported +* ALTER TABLE SET LOCATION 's3://bucket/folder' | Unsupported +* ALTER TABLE SET FILE FORMAT format | Unsupported +* ALTER TABLE SET TABLE PROPERTIES ( 'numRows'='row_count' ) | Unsupported +* ALTER TABLE PARTITION ( partition_column=partition_value [, ...] ) SET LOCATION 's3://bucket/folder' | Unsupported +* ALTER TABLE ADD PARTITION ( partition_column=partition_value [, ...] ) LOCATION 's3://bucket/folder' | Unsupported +* ALTER TABLE DROP PARTITION ( partition_column=partition_value [, ...] ) | Unsupported +* ALTER TABLE APPEND | Unsupported ALTER USER | Supported -ALTER USER [NO]CREATEUSER | **Yes** +* ALTER USER [NO]CREATEUSER | **Yes** ANALYZE | Partial -ANALYZE VERBOSE | Unsupported -ANALYSE PREDICATE COLUMNS | Unsupported -ANALYZE ALL COLUMNS | Unsupported -ANALYZE COMPRESSION | Unsupported +* ANALYZE VERBOSE | Unsupported +* ANALYSE PREDICATE COLUMNS | Unsupported +* ANALYZE ALL COLUMNS | Unsupported +* ANALYZE COMPRESSION | Unsupported BEGIN | Native CANCEL | Unsupported CLOSE | Unsupported @@ -42,94 +42,94 @@ COMMENT | Native COMMIT | Native COPY | Unsupported CREATE DATABASE | Partial -CREATE DATABASE CONNECTION LIMIT UNLIMITED | Unsupported +* CREATE DATABASE CONNECTION LIMIT UNLIMITED | Unsupported CREATE EXTERNAL SCHEMA | Unsupported CREATE EXTERNAL TABLE | Unsupported CREATE FUNCTION | Partial -CREATE OR REPLACE FUNCTION | Unsupported +* CREATE OR REPLACE FUNCTION | Unsupported CREATE GROUP | Native CREATE LIBRARY | Unsupported CREATE SCHEMA | Partial -CREATE SCHEMA AUTHORIZATION | Unsupported -CREATE SCHEMA IF NOT EXISTS | Unsupported +* CREATE SCHEMA AUTHORIZATION | Unsupported +* CREATE SCHEMA IF NOT EXISTS | Unsupported CREATE TABLE | Partial -CREATE LOCAL TABLE | Unsupported -CREATE TABLE IF | Unsupported -CREATE TABLE BACKUP | Unsupported -CREATE TABLE DISTSTYLE | Unsupported -CREATE TABLE DISTKEY | Unsupported -CREATE TABLE SORTKEY | Unsupported -CREATE TABLE | Unsupported +* CREATE LOCAL TABLE | Unsupported +* CREATE TABLE IF | Unsupported +* CREATE TABLE BACKUP | Unsupported +* CREATE TABLE DISTSTYLE | Unsupported +* CREATE TABLE DISTKEY | Unsupported +* CREATE TABLE SORTKEY | Unsupported +* CREATE TABLE | Unsupported CREATE TABLE AS | Unsupported CREATE USER | Partial -CREATE USER [NO]CREATEUSER | Unsupported +* CREATE USER [NO]CREATEUSER | Unsupported CREATE VIEW | Partial -CREATE OR REPLACE VIEW | Unsupported +* CREATE OR REPLACE VIEW | Unsupported DEALLOCATE | Partial -DEALLOCATE PREPARE | Unsupported +* DEALLOCATE PREPARE | Unsupported DECLARE | Native DELETE | Partial -DELETE FROM USING WHERE | Unsupported +* DELETE FROM USING WHERE | Unsupported DROP DATABASE | Native DROP FUNCTION | Native DROP GROUP | Native DROP LIBRARY | Unsupported DROP SCHEMA | Partial -DROP SCHEMA IF EXISTS | Unsupported +* DROP SCHEMA IF EXISTS | Unsupported DROP TABLE | Partial -DROP TABLE | Unsupported +* DROP TABLE | Unsupported DROP USER | Partial -DROP USER IF EXISTS | Unsupported +* DROP USER IF EXISTS | Unsupported DROP VIEW | Partial -DROP VIEW IF EXISTS | Unsupported +* DROP VIEW IF EXISTS | Unsupported END | Native EXECUTE | Native EXPLAIN | Native FETCH | Partial -FETCH NEXT | Unsupported -FETCH ALL | Unsupported +* FETCH NEXT | Unsupported +* FETCH ALL | Unsupported GRANT | Partial -GRANT ALL PRIVILEGES | Unsupported -GRANT ALL ON TABLE ALL TABLES IN SCHEMA | Unsupported -GRANT ALL ON TABLE TO GROUP | Unsupported -GRANT ALL ON TABLE ALL TABLES IN SCHEMA | Unsupported -GRANT CREATE ALL | Unsupported -GRANT CREATE ON | Unsupported +* GRANT ALL PRIVILEGES | Unsupported +* GRANT ALL ON TABLE ALL TABLES IN SCHEMA | Unsupported +* GRANT ALL ON TABLE TO GROUP | Unsupported +* GRANT ALL ON TABLE ALL TABLES IN SCHEMA | Unsupported +* GRANT CREATE ALL | Unsupported +* GRANT CREATE ON | Unsupported GRANT | Unsupported INSERT | Partial -INSERT INTO DEFAULT VALUES | Unsupported +* INSERT INTO DEFAULT VALUES | Unsupported LOCK | Native PREPARE | Partial -PREPARE AS | Unsupported +* PREPARE AS | Unsupported RESET | Partial -RESET ALL | Unsupported +* RESET ALL | Unsupported REVOKE | Partial -REVOKE GRANT | Unsupported -REVOKE SELECT ALL PRIVILEGES | Unsupported -REVOKE | Unsupported +* REVOKE GRANT | Unsupported +* REVOKE SELECT ALL PRIVILEGES | Unsupported +* REVOKE | Unsupported ROLLBACK | Native SELECT | Partial -SELECT TOP | Unsupported -SELECT ALL | Unsupported -SELECT DISTINCT | Unsupported -SELECT | Unsupported +* SELECT TOP | Unsupported +* SELECT ALL | Unsupported +* SELECT DISTINCT | Unsupported +* SELECT | Unsupported SELECT INTO | Unsupported SET | Partial -SET ALL | Unsupported -SET SEED TO | Unsupported -SET SESSION AUTHORIZATION | Unsupported -SET SESSION CHARACTERISTICS | Native +* SET ALL | Unsupported +* SET SEED TO | Unsupported +* SET SESSION AUTHORIZATION | Unsupported +* SET SESSION CHARACTERISTICS | Native SHOW | Partial -SHOW ALL | Unsupported +* SHOW ALL | Unsupported START TRANSACTION | Native TRUNCATE | Partial -TRUNCATE TABLE | Unsupported +* TRUNCATE TABLE | Unsupported UNLOAD | Unsupported UPDATE | Unsupported -UPDATE SET FROM | Unsupported -UPDATE SET WHERE | Unsupported +* UPDATE SET FROM | Unsupported +* UPDATE SET WHERE | Unsupported VACUUM | Partial -VACUUM SORT | Unsupported -VACUUM DELETE | Unsupported -VACUUM REINDEX | Unsupported -VACUUM TO | Unsupported +* VACUUM SORT | Unsupported +* VACUUM DELETE | Unsupported +* VACUUM REINDEX | Unsupported +* VACUUM TO | Unsupported From 3619d661e685ad382a135bee30c21f56efe2d624 Mon Sep 17 00:00:00 2001 From: Robins Date: Thu, 10 Aug 2017 05:55:43 +1000 Subject: [PATCH 024/149] Revert recent commit, bullets within tables don't show too well on Github md parser it seems --- src/bin/psql/README.md | 124 ++++++++++++++++++++--------------------- 1 file changed, 62 insertions(+), 62 deletions(-) diff --git a/src/bin/psql/README.md b/src/bin/psql/README.md index 86cad3c209e4f..1b1c220c078e3 100644 --- a/src/bin/psql/README.md +++ b/src/bin/psql/README.md @@ -20,21 +20,21 @@ ALTER DEFAULT PRIVILEGES | Native ALTER GROUP | Native ALTER SCHEMA | Native ALTER TABLE | Partial -* ALTER TABLE ADD COLUMN ENCODING | Unsupported -* ALTER TABLE SET LOCATION 's3://bucket/folder' | Unsupported -* ALTER TABLE SET FILE FORMAT format | Unsupported -* ALTER TABLE SET TABLE PROPERTIES ( 'numRows'='row_count' ) | Unsupported -* ALTER TABLE PARTITION ( partition_column=partition_value [, ...] ) SET LOCATION 's3://bucket/folder' | Unsupported -* ALTER TABLE ADD PARTITION ( partition_column=partition_value [, ...] ) LOCATION 's3://bucket/folder' | Unsupported -* ALTER TABLE DROP PARTITION ( partition_column=partition_value [, ...] ) | Unsupported -* ALTER TABLE APPEND | Unsupported +ALTER TABLE ADD COLUMN ENCODING | Unsupported +ALTER TABLE SET LOCATION 's3://bucket/folder' | Unsupported +ALTER TABLE SET FILE FORMAT format | Unsupported +ALTER TABLE SET TABLE PROPERTIES ( 'numRows'='row_count' ) | Unsupported +ALTER TABLE PARTITION ( partition_column=partition_value [, ...] ) SET LOCATION 's3://bucket/folder' | Unsupported +ALTER TABLE ADD PARTITION ( partition_column=partition_value [, ...] ) LOCATION 's3://bucket/folder' | Unsupported +ALTER TABLE DROP PARTITION ( partition_column=partition_value [, ...] ) | Unsupported +ALTER TABLE APPEND | Unsupported ALTER USER | Supported -* ALTER USER [NO]CREATEUSER | **Yes** +ALTER USER [NO]CREATEUSER | **Yes** ANALYZE | Partial -* ANALYZE VERBOSE | Unsupported -* ANALYSE PREDICATE COLUMNS | Unsupported -* ANALYZE ALL COLUMNS | Unsupported -* ANALYZE COMPRESSION | Unsupported +ANALYZE VERBOSE | Unsupported +ANALYSE PREDICATE COLUMNS | Unsupported +ANALYZE ALL COLUMNS | Unsupported +ANALYZE COMPRESSION | Unsupported BEGIN | Native CANCEL | Unsupported CLOSE | Unsupported @@ -42,94 +42,94 @@ COMMENT | Native COMMIT | Native COPY | Unsupported CREATE DATABASE | Partial -* CREATE DATABASE CONNECTION LIMIT UNLIMITED | Unsupported +CREATE DATABASE CONNECTION LIMIT UNLIMITED | Unsupported CREATE EXTERNAL SCHEMA | Unsupported CREATE EXTERNAL TABLE | Unsupported CREATE FUNCTION | Partial -* CREATE OR REPLACE FUNCTION | Unsupported +CREATE OR REPLACE FUNCTION | Unsupported CREATE GROUP | Native CREATE LIBRARY | Unsupported CREATE SCHEMA | Partial -* CREATE SCHEMA AUTHORIZATION | Unsupported -* CREATE SCHEMA IF NOT EXISTS | Unsupported +CREATE SCHEMA AUTHORIZATION | Unsupported +CREATE SCHEMA IF NOT EXISTS | Unsupported CREATE TABLE | Partial -* CREATE LOCAL TABLE | Unsupported -* CREATE TABLE IF | Unsupported -* CREATE TABLE BACKUP | Unsupported -* CREATE TABLE DISTSTYLE | Unsupported -* CREATE TABLE DISTKEY | Unsupported -* CREATE TABLE SORTKEY | Unsupported -* CREATE TABLE | Unsupported +CREATE LOCAL TABLE | Unsupported +CREATE TABLE IF | Unsupported +CREATE TABLE BACKUP | Unsupported +CREATE TABLE DISTSTYLE | Unsupported +CREATE TABLE DISTKEY | Unsupported +CREATE TABLE SORTKEY | Unsupported +CREATE TABLE | Unsupported CREATE TABLE AS | Unsupported CREATE USER | Partial -* CREATE USER [NO]CREATEUSER | Unsupported +CREATE USER [NO]CREATEUSER | Unsupported CREATE VIEW | Partial -* CREATE OR REPLACE VIEW | Unsupported +CREATE OR REPLACE VIEW | Unsupported DEALLOCATE | Partial -* DEALLOCATE PREPARE | Unsupported +DEALLOCATE PREPARE | Unsupported DECLARE | Native DELETE | Partial -* DELETE FROM USING WHERE | Unsupported +DELETE FROM USING WHERE | Unsupported DROP DATABASE | Native DROP FUNCTION | Native DROP GROUP | Native DROP LIBRARY | Unsupported DROP SCHEMA | Partial -* DROP SCHEMA IF EXISTS | Unsupported +DROP SCHEMA IF EXISTS | Unsupported DROP TABLE | Partial -* DROP TABLE | Unsupported +DROP TABLE | Unsupported DROP USER | Partial -* DROP USER IF EXISTS | Unsupported +DROP USER IF EXISTS | Unsupported DROP VIEW | Partial -* DROP VIEW IF EXISTS | Unsupported +DROP VIEW IF EXISTS | Unsupported END | Native EXECUTE | Native EXPLAIN | Native FETCH | Partial -* FETCH NEXT | Unsupported -* FETCH ALL | Unsupported +FETCH NEXT | Unsupported +FETCH ALL | Unsupported GRANT | Partial -* GRANT ALL PRIVILEGES | Unsupported -* GRANT ALL ON TABLE ALL TABLES IN SCHEMA | Unsupported -* GRANT ALL ON TABLE TO GROUP | Unsupported -* GRANT ALL ON TABLE ALL TABLES IN SCHEMA | Unsupported -* GRANT CREATE ALL | Unsupported -* GRANT CREATE ON | Unsupported +GRANT ALL PRIVILEGES | Unsupported +GRANT ALL ON TABLE ALL TABLES IN SCHEMA | Unsupported +GRANT ALL ON TABLE TO GROUP | Unsupported +GRANT ALL ON TABLE ALL TABLES IN SCHEMA | Unsupported +GRANT CREATE ALL | Unsupported +GRANT CREATE ON | Unsupported GRANT | Unsupported INSERT | Partial -* INSERT INTO DEFAULT VALUES | Unsupported +INSERT INTO DEFAULT VALUES | Unsupported LOCK | Native PREPARE | Partial -* PREPARE AS | Unsupported +PREPARE AS | Unsupported RESET | Partial -* RESET ALL | Unsupported +RESET ALL | Unsupported REVOKE | Partial -* REVOKE GRANT | Unsupported -* REVOKE SELECT ALL PRIVILEGES | Unsupported -* REVOKE | Unsupported +REVOKE GRANT | Unsupported +REVOKE SELECT ALL PRIVILEGES | Unsupported +REVOKE | Unsupported ROLLBACK | Native SELECT | Partial -* SELECT TOP | Unsupported -* SELECT ALL | Unsupported -* SELECT DISTINCT | Unsupported -* SELECT | Unsupported +SELECT TOP | Unsupported +SELECT ALL | Unsupported +SELECT DISTINCT | Unsupported +SELECT | Unsupported SELECT INTO | Unsupported SET | Partial -* SET ALL | Unsupported -* SET SEED TO | Unsupported -* SET SESSION AUTHORIZATION | Unsupported -* SET SESSION CHARACTERISTICS | Native +SET ALL | Unsupported +SET SEED TO | Unsupported +SET SESSION AUTHORIZATION | Unsupported +SET SESSION CHARACTERISTICS | Native SHOW | Partial -* SHOW ALL | Unsupported +SHOW ALL | Unsupported START TRANSACTION | Native TRUNCATE | Partial -* TRUNCATE TABLE | Unsupported +TRUNCATE TABLE | Unsupported UNLOAD | Unsupported UPDATE | Unsupported -* UPDATE SET FROM | Unsupported -* UPDATE SET WHERE | Unsupported +UPDATE SET FROM | Unsupported +UPDATE SET WHERE | Unsupported VACUUM | Partial -* VACUUM SORT | Unsupported -* VACUUM DELETE | Unsupported -* VACUUM REINDEX | Unsupported -* VACUUM TO | Unsupported +VACUUM SORT | Unsupported +VACUUM DELETE | Unsupported +VACUUM REINDEX | Unsupported +VACUUM TO | Unsupported From 3e9915b66a88492cda5eb950b8ba88ef66c426f7 Mon Sep 17 00:00:00 2001 From: Robins Date: Thu, 10 Aug 2017 06:02:21 +1000 Subject: [PATCH 025/149] Add table for Parameters support --- src/bin/psql/README.md | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/src/bin/psql/README.md b/src/bin/psql/README.md index 1b1c220c078e3..07d567b319a05 100644 --- a/src/bin/psql/README.md +++ b/src/bin/psql/README.md @@ -12,8 +12,23 @@ Partial | Only some combinations supported Supported | All combinations (now) supported by this branch -SQL Commands | Redshift Support -:----------- |:-------: + +Parameters | Supported State +:-------------:| ----------- +analyze_threshold_percent | Unsupported +datestyle | Native +enable_user_activity_logging | Unsupported +extra_float_digits | Native +query_group | Unsupported +require_ssl | Unsupported +search_path | Native +statement_timeout | Native +wlm_json_configuration | Unsupported + + + +SQL Commands | Supported State +:----------- |:--------------: ABORT | Native ALTER DATABASE | Native ALTER DEFAULT PRIVILEGES | Native @@ -133,3 +148,6 @@ VACUUM SORT | Unsupported VACUUM DELETE | Unsupported VACUUM REINDEX | Unsupported VACUUM TO | Unsupported + + + From 7c1c41e1c66a0f7b70333844d07d07414911fa6e Mon Sep 17 00:00:00 2001 From: Robins Tharakan Date: Wed, 9 Aug 2017 20:41:55 +0000 Subject: [PATCH 026/149] Add full support for CREATE DATABASE --- src/bin/psql/tab-complete.c | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/src/bin/psql/tab-complete.c b/src/bin/psql/tab-complete.c index 27b1ee8b4b823..76bc916b78492 100644 --- a/src/bin/psql/tab-complete.c +++ b/src/bin/psql/tab-complete.c @@ -2254,11 +2254,37 @@ psql_completion(const char *text, int start, int end) /* CREATE DATABASE */ else if (Matches3("CREATE", "DATABASE", MatchAny)) - COMPLETE_WITH_LIST9("OWNER", "TEMPLATE", "ENCODING", "TABLESPACE", + COMPLETE_WITH_LIST10("WITH", "OWNER", "TEMPLATE", "ENCODING", "TABLESPACE", "IS_TEMPLATE", "ALLOW_CONNECTIONS", "CONNECTION LIMIT", "LC_COLLATE", "LC_CTYPE"); + /* CREATE DATABASE WITH */ + else if (Matches4("CREATE", "DATABASE", MatchAny, "WITH")) + COMPLETE_WITH_LIST9("OWNER", "TEMPLATE", "ENCODING", "TABLESPACE", + "IS_TEMPLATE", + "ALLOW_CONNECTIONS", "CONNECTION LIMIT", + "LC_COLLATE", "LC_CTYPE"); + + /* CREATE DATABASE WITH OWNER */ + else if (Matches5("CREATE", "DATABASE", MatchAny, "WITH", "OWNER")) + COMPLETE_WITH_LIST8("TEMPLATE", "ENCODING", "TABLESPACE", + "IS_TEMPLATE", + "ALLOW_CONNECTIONS", "CONNECTION LIMIT", + "LC_COLLATE", "LC_CTYPE"); + + /* CREATE DATABASE OWNER */ + else if (Matches4("CREATE", "DATABASE", MatchAny, "OWNER")) + COMPLETE_WITH_LIST8("TEMPLATE", "ENCODING", "TABLESPACE", + "IS_TEMPLATE", + "ALLOW_CONNECTIONS", "CONNECTION LIMIT", + "LC_COLLATE", "LC_CTYPE"); + + + /* CONNECTION LIMIT -- is allowed inside CREATE DATABASE, so use TailMatches */ + else if (TailMatches2("CONNECTION", "LIMIT")) + COMPLETE_WITH_CONST("UNLIMITED"); + else if (Matches4("CREATE", "DATABASE", MatchAny, "TEMPLATE")) COMPLETE_WITH_QUERY(Query_for_list_of_template_databases); From 6e180fa0c7ffe3f2148324bc7e260b25716e4e60 Mon Sep 17 00:00:00 2001 From: Robins Date: Thu, 10 Aug 2017 06:42:49 +1000 Subject: [PATCH 027/149] Update state for CREATE DATABASE --- src/bin/psql/README.md | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/bin/psql/README.md b/src/bin/psql/README.md index 07d567b319a05..85207ed3d00b2 100644 --- a/src/bin/psql/README.md +++ b/src/bin/psql/README.md @@ -14,7 +14,7 @@ Supported | All combinations (now) supported by this branch Parameters | Supported State -:-------------:| ----------- +:---------:|:--------------: analyze_threshold_percent | Unsupported datestyle | Native enable_user_activity_logging | Unsupported @@ -56,8 +56,11 @@ CLOSE | Unsupported COMMENT | Native COMMIT | Native COPY | Unsupported -CREATE DATABASE | Partial -CREATE DATABASE CONNECTION LIMIT UNLIMITED | Unsupported +CREATE DATABASE | Supported +CREATE DATABASE CONNECTION LIMIT UNLIMITED | **Yes** +CREATE DATABASE WITH | **Yes** +CREATE DATABASE WITH OWNER | **Yes** +CREATE DATABASE OWNER | **Yes** CREATE EXTERNAL SCHEMA | Unsupported CREATE EXTERNAL TABLE | Unsupported CREATE FUNCTION | Partial From 04b2a2c5b0a496213469304088b700721b995801 Mon Sep 17 00:00:00 2001 From: Robins Tharakan Date: Thu, 10 Aug 2017 12:42:05 +0000 Subject: [PATCH 028/149] Highlight a few tab-complete sections. Add support for CREATE OR REPLACE FUNCTION --- src/bin/psql/tab-complete.c | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/bin/psql/tab-complete.c b/src/bin/psql/tab-complete.c index 76bc916b78492..e698b6daae937 100644 --- a/src/bin/psql/tab-complete.c +++ b/src/bin/psql/tab-complete.c @@ -1029,6 +1029,7 @@ static const pgsql_thing_t words_after_create[] = { {"MATERIALIZED VIEW", NULL, &Query_for_list_of_matviews}, {"OPERATOR", NULL, NULL}, /* Querying for this is probably not such a * good idea. */ + {"OR REPLACE FUNCTION", NULL, &Query_for_list_of_functions}, {"OWNED", NULL, NULL, THING_NO_CREATE | THING_NO_ALTER}, /* for DROP OWNED BY ... */ {"PARSER", Query_for_list_of_ts_parsers, NULL, THING_NO_SHOW}, {"POLICY", NULL, NULL}, @@ -2241,7 +2242,7 @@ psql_completion(const char *text, int start, int end) COMPLETE_WITH_LIST5("HEADER", "QUOTE", "ESCAPE", "FORCE QUOTE", "FORCE NOT NULL"); - /* CREATE ACCESS METHOD */ +/* CREATE ACCESS METHOD */ /* Complete "CREATE ACCESS METHOD " */ else if (Matches4("CREATE", "ACCESS", "METHOD", MatchAny)) COMPLETE_WITH_CONST("TYPE"); @@ -2252,7 +2253,7 @@ psql_completion(const char *text, int start, int end) else if (Matches6("CREATE", "ACCESS", "METHOD", MatchAny, "TYPE", MatchAny)) COMPLETE_WITH_CONST("HANDLER"); - /* CREATE DATABASE */ +/* CREATE DATABASE */ else if (Matches3("CREATE", "DATABASE", MatchAny)) COMPLETE_WITH_LIST10("WITH", "OWNER", "TEMPLATE", "ENCODING", "TABLESPACE", "IS_TEMPLATE", @@ -2288,7 +2289,7 @@ psql_completion(const char *text, int start, int end) else if (Matches4("CREATE", "DATABASE", MatchAny, "TEMPLATE")) COMPLETE_WITH_QUERY(Query_for_list_of_template_databases); - /* CREATE EXTENSION */ +/* CREATE EXTENSION */ /* Complete with available extensions rather than installed ones. */ else if (Matches2("CREATE", "EXTENSION")) COMPLETE_WITH_QUERY(Query_for_list_of_available_extensions); @@ -2302,7 +2303,7 @@ psql_completion(const char *text, int start, int end) COMPLETE_WITH_QUERY(Query_for_list_of_available_extension_versions); } - /* CREATE FOREIGN */ +/* CREATE FOREIGN */ else if (Matches2("CREATE", "FOREIGN")) COMPLETE_WITH_LIST2("DATA WRAPPER", "TABLE"); @@ -2363,7 +2364,7 @@ psql_completion(const char *text, int start, int end) !TailMatches4("FOR", MatchAny, MatchAny, MatchAny)) COMPLETE_WITH_CONST("("); - /* CREATE POLICY */ +/* CREATE POLICY */ /* Complete "CREATE POLICY ON" */ else if (Matches3("CREATE", "POLICY", MatchAny)) COMPLETE_WITH_CONST("ON"); From 14679e09c387a92bc61d75dd5b3747a61838785f Mon Sep 17 00:00:00 2001 From: Robins Date: Thu, 10 Aug 2017 22:43:08 +1000 Subject: [PATCH 029/149] Update psql Support Matrix for CREATE OR REPLACE FUNCTION --- src/bin/psql/README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/bin/psql/README.md b/src/bin/psql/README.md index 85207ed3d00b2..cfc502f368451 100644 --- a/src/bin/psql/README.md +++ b/src/bin/psql/README.md @@ -63,8 +63,8 @@ CREATE DATABASE WITH OWNER | **Yes** CREATE DATABASE OWNER | **Yes** CREATE EXTERNAL SCHEMA | Unsupported CREATE EXTERNAL TABLE | Unsupported -CREATE FUNCTION | Partial -CREATE OR REPLACE FUNCTION | Unsupported +CREATE FUNCTION | Supported +CREATE OR REPLACE FUNCTION | **Yes** CREATE GROUP | Native CREATE LIBRARY | Unsupported CREATE SCHEMA | Partial From c3898bc843daa1cc7dbcd2f50779da8c4e25d5f4 Mon Sep 17 00:00:00 2001 From: Robins Tharakan Date: Thu, 10 Aug 2017 12:57:51 +0000 Subject: [PATCH 030/149] Add support for CREATE USER [NO]CREATEUSER and CREATE SCHEMA AUTHORIZTION and CREATE SCHEMA IF NOT EXISTS --- src/bin/psql/tab-complete.c | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/src/bin/psql/tab-complete.c b/src/bin/psql/tab-complete.c index e698b6daae937..d7e91da9e27ff 100644 --- a/src/bin/psql/tab-complete.c +++ b/src/bin/psql/tab-complete.c @@ -2475,6 +2475,15 @@ psql_completion(const char *text, int start, int end) else if (TailMatches4("AS", "ON", "SELECT|UPDATE|INSERT|DELETE", "TO")) COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tables, NULL); +/* CREATE SCHEMA */ + else if (Matches2("CREATE", "SCHEMA")) + COMPLETE_WITH_LIST2("AUTHORIZATION", "IF NOT EXISTS"); + else if (Matches3("CREATE", "SCHEMA", MatchAny)) + COMPLETE_WITH_CONST("AUTHORIZATION"); + else if (Matches4("CREATE", "SCHEMA", "IF NOT EXISTS", MatchAny)) + COMPLETE_WITH_CONST("AUTHORIZATION"); + + /* CREATE SEQUENCE --- is allowed inside CREATE SCHEMA, so use TailMatches */ else if (TailMatches3("CREATE", "SEQUENCE", MatchAny) || TailMatches4("CREATE", "TEMP|TEMPORARY", "SEQUENCE", MatchAny)) @@ -2615,9 +2624,9 @@ psql_completion(const char *text, int start, int end) !TailMatches2("USER", "MAPPING")) { static const char *const list_CREATEROLE[] = - {"ADMIN", "BYPASSRLS", "CONNECTION LIMIT", "CREATEDB", "CREATEROLE", + {"ADMIN", "BYPASSRLS", "CONNECTION LIMIT", "CREATEDB", "CREATEROLE", "CREATEUSER", "ENCRYPTED PASSWORD", "IN", "INHERIT", "LOGIN", "NOBYPASSRLS", - "NOCREATEDB", "NOCREATEROLE", "NOINHERIT", + "NOCREATEDB", "NOCREATEROLE", "NOCREATEUSER", "NOINHERIT", "NOLOGIN", "NOREPLICATION", "NOSUPERUSER", "PASSWORD", "REPLICATION", "ROLE", "SUPERUSER", "SYSID", "VALID UNTIL", "WITH", NULL}; From c8d74496d56cc47f9b8d2b38494aa5d3045e57b4 Mon Sep 17 00:00:00 2001 From: Robins Date: Thu, 10 Aug 2017 22:58:38 +1000 Subject: [PATCH 031/149] Update psql Support Matrix for CREATE USER and CREATE SCHEMA --- src/bin/psql/README.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/bin/psql/README.md b/src/bin/psql/README.md index cfc502f368451..c25e99c753c24 100644 --- a/src/bin/psql/README.md +++ b/src/bin/psql/README.md @@ -67,9 +67,9 @@ CREATE FUNCTION | Supported CREATE OR REPLACE FUNCTION | **Yes** CREATE GROUP | Native CREATE LIBRARY | Unsupported -CREATE SCHEMA | Partial -CREATE SCHEMA AUTHORIZATION | Unsupported -CREATE SCHEMA IF NOT EXISTS | Unsupported +CREATE SCHEMA | Supported +CREATE SCHEMA AUTHORIZATION | **Yes** +CREATE SCHEMA IF NOT EXISTS | **Yes** CREATE TABLE | Partial CREATE LOCAL TABLE | Unsupported CREATE TABLE IF | Unsupported @@ -79,8 +79,8 @@ CREATE TABLE DISTKEY | Unsupported CREATE TABLE SORTKEY | Unsupported CREATE TABLE | Unsupported CREATE TABLE AS | Unsupported -CREATE USER | Partial -CREATE USER [NO]CREATEUSER | Unsupported +CREATE USER | Supported +CREATE USER [NO]CREATEUSER | **Yes** CREATE VIEW | Partial CREATE OR REPLACE VIEW | Unsupported DEALLOCATE | Partial From 09351bf3c9df60d2c962ced1181b2e2d8a32fb45 Mon Sep 17 00:00:00 2001 From: Robins Tharakan Date: Thu, 10 Aug 2017 18:45:08 +0000 Subject: [PATCH 032/149] WIP towards adding some more support for missing SQL commands --- src/bin/psql/tab-complete.c | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/src/bin/psql/tab-complete.c b/src/bin/psql/tab-complete.c index d7e91da9e27ff..10cd36f5a7acd 100644 --- a/src/bin/psql/tab-complete.c +++ b/src/bin/psql/tab-complete.c @@ -423,6 +423,21 @@ static const SchemaQuery Query_for_list_of_indexes = { NULL }; +static const SchemaQuery Query_for_list_of_libraries = { + /* catname */ + "pg_catalog.pg_class c", + /* selcondition */ + "c.relkind IN (" CppAsString2(RELKIND_INDEX) ")", + /* viscondition */ + "pg_catalog.pg_table_is_visible(c.oid)", + /* namespace */ + "c.relnamespace", + /* result */ + "pg_catalog.quote_ident(c.relname)", + /* qualresult */ + NULL +}; + static const SchemaQuery Query_for_list_of_sequences = { /* catname */ "pg_catalog.pg_class c", @@ -1026,10 +1041,13 @@ static const pgsql_thing_t words_after_create[] = { {"INDEX", NULL, &Query_for_list_of_indexes}, {"LANGUAGE", Query_for_list_of_languages}, {"LARGE OBJECT", NULL, NULL, THING_NO_CREATE | THING_NO_DROP}, + {"LIBRARY", NULL, NULL, THING_NO_ALTER}, {"MATERIALIZED VIEW", NULL, &Query_for_list_of_matviews}, {"OPERATOR", NULL, NULL}, /* Querying for this is probably not such a * good idea. */ {"OR REPLACE FUNCTION", NULL, &Query_for_list_of_functions}, + {"OR REPLACE LIBRARY", NULL, &Query_for_list_of_libraries}, + {"OR REPLACE VIEW", NULL, &Query_for_list_of_views}, {"OWNED", NULL, NULL, THING_NO_CREATE | THING_NO_ALTER}, /* for DROP OWNED BY ... */ {"PARSER", Query_for_list_of_ts_parsers, NULL, THING_NO_SHOW}, {"POLICY", NULL, NULL}, @@ -2683,6 +2701,8 @@ psql_completion(const char *text, int start, int end) /* DEALLOCATE */ else if (Matches1("DEALLOCATE")) + COMPLETE_WITH_QUERY(Query_for_list_of_prepared_statements, "UNION SELECT 'PREPARE'"); + else if (Matches1("DEALLOCATE", "PREPARE")) COMPLETE_WITH_QUERY(Query_for_list_of_prepared_statements); /* DECLARE */ @@ -2702,6 +2722,8 @@ psql_completion(const char *text, int start, int end) /* Complete DELETE FROM */ else if (TailMatches3("DELETE", "FROM", MatchAny)) COMPLETE_WITH_LIST2("USING", "WHERE"); + else if (TailMatches4("DELETE", "FROM", MatchAny, "USING")) + COMPLETE_WITH_LIST("WHERE"); /* XXX: implement tab completion for DELETE ... USING */ /* DISCARD */ From 0a27668064e9cd762509ebc71d50be3e865b15d2 Mon Sep 17 00:00:00 2001 From: Robins Date: Fri, 11 Aug 2017 05:08:41 +1000 Subject: [PATCH 033/149] Still WIP on adding a few commands. Not working --- src/bin/psql/tab-complete.c | 24 +++++++++++++++++++++++- src/include/catalog/pg_class.h | 1 + 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/src/bin/psql/tab-complete.c b/src/bin/psql/tab-complete.c index 10cd36f5a7acd..c95d43acc47b8 100644 --- a/src/bin/psql/tab-complete.c +++ b/src/bin/psql/tab-complete.c @@ -427,7 +427,7 @@ static const SchemaQuery Query_for_list_of_libraries = { /* catname */ "pg_catalog.pg_class c", /* selcondition */ - "c.relkind IN (" CppAsString2(RELKIND_INDEX) ")", + "c.relkind IN (" CppAsString2(RELKIND_LIBRARY) ")", /* viscondition */ "pg_catalog.pg_table_is_visible(c.oid)", /* namespace */ @@ -2382,6 +2382,24 @@ psql_completion(const char *text, int start, int end) !TailMatches4("FOR", MatchAny, MatchAny, MatchAny)) COMPLETE_WITH_CONST("("); +/* CREATE LIBRARY */ + else if (Matches2("CREATE", "LIBRARY")) + COMPLETE_WITH_QUERY(Query_for_list_of_libraries, NULL); + else if (Matches4("CREATE", "OR", "REPLACE", "LIBRARY")) + COMPLETE_WITH_QUERY(Query_for_list_of_libraries, NULL); + else if (Matches5("CREATE", "OR", "REPLACE", "LIBRARY", MatchAny)) + COMPLETE_WITH_CONST("LANGUAGE plpythonu FROM"); + else if (Matches1("CREATE") && TailMatches5("LIBRARY", "LANGUAGE", "plpythonu", "FROM", MatchAny)) + COMPLETE_WITH_CONST("AUTHORIZATION"); + else if (Matches1("CREATE") && TailMatches6("LIBRARY", "LANGUAGE", "plpythonu", "FROM", MatchAny, "AUTHORIZATION")) + COMPLETE_WITH_CONST("REGION"); + else if (Matches1("CREATE") && TailMatches6("LIBRARY", "LANGUAGE", "plpythonu", "FROM", MatchAny, "AUTHORIZATION", "REGION")) + COMPLETE_WITH_CONST("AS"); + else if (Matches1("CREATE") && TailMatches6("LIBRARY", "LANGUAGE", "plpythonu", "FROM", MatchAny, "AUTHORIZATION", "REGION")) + COMPLETE_WITH_CONST("AS"); + + // XXX See if you can add Regions here!? + /* CREATE POLICY */ /* Complete "CREATE POLICY ON" */ else if (Matches3("CREATE", "POLICY", MatchAny)) @@ -2767,6 +2785,10 @@ psql_completion(const char *text, int start, int end) else if (Matches4("DROP", "INDEX", "CONCURRENTLY", MatchAny)) COMPLETE_WITH_LIST2("CASCADE", "RESTRICT"); + /* DROP LIBRARY */ + else if (Matches2("DROP", "LIBRARY")) + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_libraries, NULL); + /* DROP MATERIALIZED VIEW */ else if (Matches2("DROP", "MATERIALIZED")) COMPLETE_WITH_CONST("VIEW"); diff --git a/src/include/catalog/pg_class.h b/src/include/catalog/pg_class.h index b256657bda08f..5a55612911940 100644 --- a/src/include/catalog/pg_class.h +++ b/src/include/catalog/pg_class.h @@ -159,6 +159,7 @@ DESCR(""); #define RELKIND_RELATION 'r' /* ordinary table */ #define RELKIND_INDEX 'i' /* secondary index */ +#define RELKIND_LIBRARY 'l' /* library */ #define RELKIND_SEQUENCE 'S' /* sequence object */ #define RELKIND_TOASTVALUE 't' /* for out-of-line values */ #define RELKIND_VIEW 'v' /* view */ From ab7d1fe392cc1b8e48f5658f48d694517f02032c Mon Sep 17 00:00:00 2001 From: Robins Date: Fri, 11 Aug 2017 11:00:02 +1000 Subject: [PATCH 034/149] Correct Query_list_for_libraries. Still WIP --- src/bin/psql/tab-complete.c | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/src/bin/psql/tab-complete.c b/src/bin/psql/tab-complete.c index c95d43acc47b8..c6561571e183c 100644 --- a/src/bin/psql/tab-complete.c +++ b/src/bin/psql/tab-complete.c @@ -424,18 +424,18 @@ static const SchemaQuery Query_for_list_of_indexes = { }; static const SchemaQuery Query_for_list_of_libraries = { - /* catname */ - "pg_catalog.pg_class c", - /* selcondition */ - "c.relkind IN (" CppAsString2(RELKIND_LIBRARY) ")", - /* viscondition */ - "pg_catalog.pg_table_is_visible(c.oid)", - /* namespace */ - "c.relnamespace", - /* result */ - "pg_catalog.quote_ident(c.relname)", - /* qualresult */ - NULL + /* catname */ + "pg_catalog.pg_library l", + /* selcondition */ + NULL, + /* viscondition */ + NULL, + /* namespace */ + "c.relnamespace", + /* result */ + "pg_catalog.quote_ident(l.name)", + /* qualresult */ + NULL }; static const SchemaQuery Query_for_list_of_sequences = { From 848bc87c3976e7734b7d42fbe1b1d1fc3644795e Mon Sep 17 00:00:00 2001 From: Robins Tharakan Date: Fri, 11 Aug 2017 23:58:25 +1000 Subject: [PATCH 035/149] Add AWS Regions. Still WIP --- src/bin/psql/tab-complete.c | 34 +++++++++++++++++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) diff --git a/src/bin/psql/tab-complete.c b/src/bin/psql/tab-complete.c index c6561571e183c..cff0c59e09ef6 100644 --- a/src/bin/psql/tab-complete.c +++ b/src/bin/psql/tab-complete.c @@ -685,6 +685,22 @@ static const SchemaQuery Query_for_list_of_statistics = { " AND (pg_catalog.quote_ident(nspname)='%s' "\ " OR '\"' || nspname || '\"' ='%s') " +#define Query_for_list_of_aws_regions \ +"SELECT 'ap-northeast-1' UNION ALL "\ +"SELECT 'ap-northeast-2' UNION ALL "\ +"SELECT 'ap-south-1' UNION ALL "\ +"SELECT 'ap-southeast-1' UNION ALL "\ +"SELECT 'ap-southeast-2' UNION ALL "\ +"SELECT 'ca-central-1' UNION ALL "\ +"SELECT 'eu-central-1' UNION ALL "\ +"SELECT 'eu-west-1' UNION ALL "\ +"SELECT 'eu-west-2' UNION ALL "\ +"SELECT 'sa-east-1' UNION ALL "\ +"SELECT 'us-east-1' UNION ALL "\ +"SELECT 'us-east-2' UNION ALL "\ +"SELECT 'us-west-1' UNION ALL "\ +"SELECT 'us-west-2' " + #define Query_for_list_of_enum_values \ "SELECT pg_catalog.quote_literal(enumlabel) "\ " FROM pg_catalog.pg_enum e, pg_catalog.pg_type t "\ @@ -2260,7 +2276,23 @@ psql_completion(const char *text, int start, int end) COMPLETE_WITH_LIST5("HEADER", "QUOTE", "ESCAPE", "FORCE QUOTE", "FORCE NOT NULL"); -/* CREATE ACCESS METHOD */ +/* + TailMatches2("ACCESS_KEY_ID", MatchAny) || + TailMatches4("IAM_ROLE", MatchAny, "SECRET_ACCESS_KEY", MatchAny) || + TailMatches6("IAM_ROLE", MatchAny, "SECRET_ACCESS_KEY", MatchAny, "SESSION_TOKEN", MatchAny) || + TailMatches6("IAM_ROLE", MatchAny, "SECRET_ACCESS_KEY", MatchAny, "token", MatchAny) || + TailMatches8("IAM_ROLE", MatchAny, "SECRET_ACCESS_KEY", MatchAny, "token", MatchAny, "MASTER_SYMMETRIC_KEY", MatchAny) || + TailMatches2("CREDENTIALS", MatchAny) || + TailMatches3("WITH", "CREDENTIALS", MatchAny) || + TailMatches4("WITH", "CREDENTIALS", "AS", MatchAny) || + +*/ + /* COPY FROM REGION */ + else if (HeadMatches2("COPY", MatchAny) && TailMatches1("REGION")) + COMPLETE_WITH_QUERY(Query_for_list_of_aws_regions); + + + /* CREATE ACCESS METHOD */ /* Complete "CREATE ACCESS METHOD " */ else if (Matches4("CREATE", "ACCESS", "METHOD", MatchAny)) COMPLETE_WITH_CONST("TYPE"); From 87c03df7c16b7fe7d6cf20941d53535704558988 Mon Sep 17 00:00:00 2001 From: Robins Tharakan Date: Sat, 12 Aug 2017 15:11:06 +1000 Subject: [PATCH 036/149] Lots of minor fixes to recent changes, lots of small learnings. sSeems to compile but not tested recent features yet --- src/bin/psql/tab-complete.c | 43 +++++++++++++++---------------------- 1 file changed, 17 insertions(+), 26 deletions(-) diff --git a/src/bin/psql/tab-complete.c b/src/bin/psql/tab-complete.c index cff0c59e09ef6..4368660fda256 100644 --- a/src/bin/psql/tab-complete.c +++ b/src/bin/psql/tab-complete.c @@ -423,21 +423,6 @@ static const SchemaQuery Query_for_list_of_indexes = { NULL }; -static const SchemaQuery Query_for_list_of_libraries = { - /* catname */ - "pg_catalog.pg_library l", - /* selcondition */ - NULL, - /* viscondition */ - NULL, - /* namespace */ - "c.relnamespace", - /* result */ - "pg_catalog.quote_ident(l.name)", - /* qualresult */ - NULL -}; - static const SchemaQuery Query_for_list_of_sequences = { /* catname */ "pg_catalog.pg_class c", @@ -908,6 +893,11 @@ static const SchemaQuery Query_for_list_of_statistics = { " FROM pg_catalog.pg_am "\ " WHERE substring(pg_catalog.quote_ident(amname),1,%d)='%s'" +#define Query_for_list_of_libraries \ +" SELECT pg_catalog.quote_ident(name) "\ +" FROM pg_catalog.pg_library "\ +" WHERE substring(pg_catalog.quote_ident(name),1,%d)='%s'" + #define Query_for_list_of_publications \ " SELECT pg_catalog.quote_ident(pubname) "\ " FROM pg_catalog.pg_publication "\ @@ -1062,7 +1052,7 @@ static const pgsql_thing_t words_after_create[] = { {"OPERATOR", NULL, NULL}, /* Querying for this is probably not such a * good idea. */ {"OR REPLACE FUNCTION", NULL, &Query_for_list_of_functions}, - {"OR REPLACE LIBRARY", NULL, &Query_for_list_of_libraries}, + {"OR REPLACE LIBRARY", Query_for_list_of_libraries}, {"OR REPLACE VIEW", NULL, &Query_for_list_of_views}, {"OWNED", NULL, NULL, THING_NO_CREATE | THING_NO_ALTER}, /* for DROP OWNED BY ... */ {"PARSER", Query_for_list_of_ts_parsers, NULL, THING_NO_SHOW}, @@ -2416,19 +2406,19 @@ psql_completion(const char *text, int start, int end) /* CREATE LIBRARY */ else if (Matches2("CREATE", "LIBRARY")) - COMPLETE_WITH_QUERY(Query_for_list_of_libraries, NULL); + COMPLETE_WITH_QUERY(Query_for_list_of_libraries); else if (Matches4("CREATE", "OR", "REPLACE", "LIBRARY")) - COMPLETE_WITH_QUERY(Query_for_list_of_libraries, NULL); + COMPLETE_WITH_QUERY(Query_for_list_of_libraries); else if (Matches5("CREATE", "OR", "REPLACE", "LIBRARY", MatchAny)) COMPLETE_WITH_CONST("LANGUAGE plpythonu FROM"); else if (Matches1("CREATE") && TailMatches5("LIBRARY", "LANGUAGE", "plpythonu", "FROM", MatchAny)) COMPLETE_WITH_CONST("AUTHORIZATION"); else if (Matches1("CREATE") && TailMatches6("LIBRARY", "LANGUAGE", "plpythonu", "FROM", MatchAny, "AUTHORIZATION")) COMPLETE_WITH_CONST("REGION"); - else if (Matches1("CREATE") && TailMatches6("LIBRARY", "LANGUAGE", "plpythonu", "FROM", MatchAny, "AUTHORIZATION", "REGION")) - COMPLETE_WITH_CONST("AS"); - else if (Matches1("CREATE") && TailMatches6("LIBRARY", "LANGUAGE", "plpythonu", "FROM", MatchAny, "AUTHORIZATION", "REGION")) - COMPLETE_WITH_CONST("AS"); + else if (Matches1("CREATE") && TailMatches7("LIBRARY", "LANGUAGE", "plpythonu", "FROM", MatchAny, "AUTHORIZATION", "REGION")) + COMPLETE_WITH_QUERY(Query_for_list_of_aws_regions); + else if (Matches1("CREATE") && TailMatches8("LIBRARY", "LANGUAGE", "plpythonu", "FROM", MatchAny, "AUTHORIZATION", "REGION", "AS")) + COMPLETE_WITH_QUERY(Query_for_list_of_aws_regions); // XXX See if you can add Regions here!? @@ -2751,8 +2741,9 @@ psql_completion(const char *text, int start, int end) /* DEALLOCATE */ else if (Matches1("DEALLOCATE")) - COMPLETE_WITH_QUERY(Query_for_list_of_prepared_statements, "UNION SELECT 'PREPARE'"); - else if (Matches1("DEALLOCATE", "PREPARE")) +// COMPLETE_WITH_QUERY2(Query_for_list_of_prepared_statements, "UNION SELECT 'PREPARE'"); + COMPLETE_WITH_QUERY(Query_for_list_of_prepared_statements); + else if (Matches2("DEALLOCATE", "PREPARE")) COMPLETE_WITH_QUERY(Query_for_list_of_prepared_statements); /* DECLARE */ @@ -2773,7 +2764,7 @@ psql_completion(const char *text, int start, int end) else if (TailMatches3("DELETE", "FROM", MatchAny)) COMPLETE_WITH_LIST2("USING", "WHERE"); else if (TailMatches4("DELETE", "FROM", MatchAny, "USING")) - COMPLETE_WITH_LIST("WHERE"); + COMPLETE_WITH_CONST("WHERE"); /* XXX: implement tab completion for DELETE ... USING */ /* DISCARD */ @@ -2819,7 +2810,7 @@ psql_completion(const char *text, int start, int end) /* DROP LIBRARY */ else if (Matches2("DROP", "LIBRARY")) - COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_libraries, NULL); + COMPLETE_WITH_QUERY(Query_for_list_of_libraries); /* DROP MATERIALIZED VIEW */ else if (Matches2("DROP", "MATERIALIZED")) From 3567e50cc3a0685493a9bc30961fb284978be0fe Mon Sep 17 00:00:00 2001 From: Robins Tharakan Date: Sat, 12 Aug 2017 15:53:36 +1000 Subject: [PATCH 037/149] Update psql/README with recently updated SQL Commands that are now working --- src/bin/psql/README.md | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/bin/psql/README.md b/src/bin/psql/README.md index c25e99c753c24..ac5d73032befc 100644 --- a/src/bin/psql/README.md +++ b/src/bin/psql/README.md @@ -56,6 +56,7 @@ CLOSE | Unsupported COMMENT | Native COMMIT | Native COPY | Unsupported +COPY .. REGION | **Yes** CREATE DATABASE | Supported CREATE DATABASE CONNECTION LIMIT UNLIMITED | **Yes** CREATE DATABASE WITH | **Yes** @@ -66,7 +67,8 @@ CREATE EXTERNAL TABLE | Unsupported CREATE FUNCTION | Supported CREATE OR REPLACE FUNCTION | **Yes** CREATE GROUP | Native -CREATE LIBRARY | Unsupported +CREATE LIBRARY | **Yes** +CREATE OR REPLACE LIBRARY | **Yes** CREATE SCHEMA | Supported CREATE SCHEMA AUTHORIZATION | **Yes** CREATE SCHEMA IF NOT EXISTS | **Yes** @@ -82,7 +84,7 @@ CREATE TABLE AS | Unsupported CREATE USER | Supported CREATE USER [NO]CREATEUSER | **Yes** CREATE VIEW | Partial -CREATE OR REPLACE VIEW | Unsupported +CREATE OR REPLACE VIEW | **Yes** DEALLOCATE | Partial DEALLOCATE PREPARE | Unsupported DECLARE | Native @@ -91,7 +93,7 @@ DELETE FROM USING WHERE | Unsupported DROP DATABASE | Native DROP FUNCTION | Native DROP GROUP | Native -DROP LIBRARY | Unsupported +DROP LIBRARY | **Yes** DROP SCHEMA | Partial DROP SCHEMA IF EXISTS | Unsupported DROP TABLE | Partial From 3539a71de58cc95c653d8cab0b189d0472f90b77 Mon Sep 17 00:00:00 2001 From: Robins Tharakan Date: Wed, 16 Aug 2017 18:38:10 +1000 Subject: [PATCH 038/149] Update psql/README.md with last merged with postgresql master --- src/bin/psql/README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/bin/psql/README.md b/src/bin/psql/README.md index ac5d73032befc..40140042e2555 100644 --- a/src/bin/psql/README.md +++ b/src/bin/psql/README.md @@ -1,3 +1,5 @@ +Last merged with postgres master: 15th Aug 2017 + This document shows the current state of psql's auto-complete support for Amazon Redshift NOTE: This branch is maintained as a personal effort and is not sponsored / supported by my Employer or any other third-party. This is not officially supported / endorsed by Amazon in any way. From 514a044e59c59e3fe32f66c7c47f404055534505 Mon Sep 17 00:00:00 2001 From: Robins Tharakan Date: Thu, 17 Aug 2017 06:50:21 +1000 Subject: [PATCH 039/149] Add Redshift column compressions (Encodings). All ALTER TABLE ADD COLUMN working. Some corner-cases need fine tuning --- src/bin/psql/tab-complete.c | 76 +++++++++++++++++++++++++++++++++++-- 1 file changed, 72 insertions(+), 4 deletions(-) diff --git a/src/bin/psql/tab-complete.c b/src/bin/psql/tab-complete.c index 94c12045cd04b..fec2bf9b4b9f8 100644 --- a/src/bin/psql/tab-complete.c +++ b/src/bin/psql/tab-complete.c @@ -686,6 +686,20 @@ static const SchemaQuery Query_for_list_of_statistics = { "SELECT 'us-west-1' UNION ALL "\ "SELECT 'us-west-2' " +#define Query_for_list_of_column_compressions \ +" SELECT 'BYTEDICT' UNION ALL "\ +" SELECT 'DELTA' UNION ALL "\ +" SELECT 'DELTA32K' UNION ALL "\ +" SELECT 'LZO' UNION ALL "\ +" SELECT 'MOSTLY8' UNION ALL "\ +" SELECT 'MOSTLY16' UNION ALL "\ +" SELECT 'MOSTLY32' UNION ALL "\ +" SELECT 'RAW' UNION ALL "\ +" SELECT 'RUNLENGTH' UNION ALL "\ +" SELECT 'TEXT255' UNION ALL "\ +" SELECT 'TEXT32K' UNION ALL "\ +" SELECT 'ZST' " + #define Query_for_list_of_enum_values \ "SELECT pg_catalog.quote_literal(enumlabel) "\ " FROM pg_catalog.pg_enum e, pg_catalog.pg_type t "\ @@ -1426,10 +1440,37 @@ psql_completion(const char *text, int start, int end) #define HeadMatches3(p1, p2, p3) \ (previous_words_count >= 3 && \ - word_matches(p1, previous_words[previous_words_count - 1]) && \ - word_matches(p2, previous_words[previous_words_count - 2]) && \ - word_matches(p3, previous_words[previous_words_count - 3])) - + word_matches(p1, previous_words[previous_words_count - 1]) && \ + word_matches(p2, previous_words[previous_words_count - 2]) && \ + word_matches(p3, previous_words[previous_words_count - 3])) + +#define HeadMatches5(p1, p2, p3, p4, p5) \ +(previous_words_count >= 5 && \ + word_matches(p1, previous_words[previous_words_count - 1]) && \ + word_matches(p2, previous_words[previous_words_count - 2]) && \ + word_matches(p3, previous_words[previous_words_count - 3]) && \ + word_matches(p4, previous_words[previous_words_count - 4]) && \ + word_matches(p5, previous_words[previous_words_count - 5])) + +#define HeadMatches6(p1, p2, p3, p4, p5, p6) \ +(previous_words_count >= 6 && \ + word_matches(p1, previous_words[previous_words_count - 1]) && \ + word_matches(p2, previous_words[previous_words_count - 2]) && \ + word_matches(p3, previous_words[previous_words_count - 3]) && \ + word_matches(p4, previous_words[previous_words_count - 4]) && \ + word_matches(p5, previous_words[previous_words_count - 5]) && \ + word_matches(p6, previous_words[previous_words_count - 6])) + +#define HeadMatches7(p1, p2, p3, p4, p5, p6, p7) \ +(previous_words_count >= 7 && \ + word_matches(p1, previous_words[previous_words_count - 1]) && \ + word_matches(p2, previous_words[previous_words_count - 2]) && \ + word_matches(p3, previous_words[previous_words_count - 3]) && \ + word_matches(p4, previous_words[previous_words_count - 4]) && \ + word_matches(p5, previous_words[previous_words_count - 5]) && \ + word_matches(p6, previous_words[previous_words_count - 6]) && \ + word_matches(p7, previous_words[previous_words_count - 7])) + /* Known command-starting keywords. */ static const char *const sql_commands[] = { "ABORT", "ALTER", "ANALYZE", "BEGIN", "CHECKPOINT", "CLOSE", "CLUSTER", @@ -1889,6 +1930,33 @@ psql_completion(const char *text, int start, int end) COMPLETE_WITH_LIST(list_ALTER2); } + + else if (Matches4("ALTER", "TABLE", MatchAny, "ADD")) + COMPLETE_WITH_CONST("COLUMN"); + + else if ( + (HeadMatches6("ALTER", "TABLE", MatchAny, "ADD", MatchAny, MatchAny)) + ) + { +// if (HeadMatches7("ALTER", "TABLE", MatchAny, "ADD", "COLUMN", MatchAny, MatchAny)) + if (TailMatches2("DEFAULT", MatchAny)) + COMPLETE_WITH_LIST3("ENCODE", "NOT NULL", "NULL"); + else if (TailMatches1("ENCODE")) + COMPLETE_WITH_QUERY(Query_for_list_of_column_compressions); + else if (TailMatches2("ENCODE", MatchAny)) + COMPLETE_WITH_LIST3("DEFAULT", "NOT NULL", "NULL"); + else if (TailMatches1("NULL") || TailMatches2("NOT", "NULL")) + COMPLETE_WITH_LIST2("ENCODE", "DEFAULT"); + else + COMPLETE_WITH_LIST4("DEFAULT", "ENCODE", "NOT NULL", "NULL"); + } + + else if ( + (HeadMatches5("ALTER", "TABLE", MatchAny, "ADD", MatchAny)) || + (HeadMatches6("ALTER", "TABLE", MatchAny, "ADD", "COLUMN", MatchAny))) + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_datatypes, NULL); + + /* ALTER TABLE xxx ENABLE */ else if (Matches4("ALTER", "TABLE", MatchAny, "ENABLE")) COMPLETE_WITH_LIST5("ALWAYS", "REPLICA", "ROW LEVEL SECURITY", "RULE", From 592e5b58a865a582c12fd730d68f55888ec912fa Mon Sep 17 00:00:00 2001 From: Robins Tharakan Date: Thu, 17 Aug 2017 13:14:08 +1000 Subject: [PATCH 040/149] Final changes to ALTER TABLE ADD COLUMN. Working --- src/bin/psql/tab-complete.c | 79 +++++++++++++++++++++---------------- 1 file changed, 45 insertions(+), 34 deletions(-) diff --git a/src/bin/psql/tab-complete.c b/src/bin/psql/tab-complete.c index fec2bf9b4b9f8..7c5809cbff730 100644 --- a/src/bin/psql/tab-complete.c +++ b/src/bin/psql/tab-complete.c @@ -1444,32 +1444,39 @@ psql_completion(const char *text, int start, int end) word_matches(p2, previous_words[previous_words_count - 2]) && \ word_matches(p3, previous_words[previous_words_count - 3])) -#define HeadMatches5(p1, p2, p3, p4, p5) \ -(previous_words_count >= 5 && \ - word_matches(p1, previous_words[previous_words_count - 1]) && \ - word_matches(p2, previous_words[previous_words_count - 2]) && \ - word_matches(p3, previous_words[previous_words_count - 3]) && \ - word_matches(p4, previous_words[previous_words_count - 4]) && \ - word_matches(p5, previous_words[previous_words_count - 5])) +#define HeadMatches4(p1, p2, p3, p4) \ + (previous_words_count >= 4 && \ + word_matches(p1, previous_words[previous_words_count - 1]) && \ + word_matches(p2, previous_words[previous_words_count - 2]) && \ + word_matches(p3, previous_words[previous_words_count - 3]) && \ + word_matches(p4, previous_words[previous_words_count - 4])) +#define HeadMatches5(p1, p2, p3, p4, p5) \ + (previous_words_count >= 5 && \ + word_matches(p1, previous_words[previous_words_count - 1]) && \ + word_matches(p2, previous_words[previous_words_count - 2]) && \ + word_matches(p3, previous_words[previous_words_count - 3]) && \ + word_matches(p4, previous_words[previous_words_count - 4]) && \ + word_matches(p5, previous_words[previous_words_count - 5])) + #define HeadMatches6(p1, p2, p3, p4, p5, p6) \ -(previous_words_count >= 6 && \ - word_matches(p1, previous_words[previous_words_count - 1]) && \ - word_matches(p2, previous_words[previous_words_count - 2]) && \ - word_matches(p3, previous_words[previous_words_count - 3]) && \ - word_matches(p4, previous_words[previous_words_count - 4]) && \ - word_matches(p5, previous_words[previous_words_count - 5]) && \ - word_matches(p6, previous_words[previous_words_count - 6])) + (previous_words_count >= 6 && \ + word_matches(p1, previous_words[previous_words_count - 1]) && \ + word_matches(p2, previous_words[previous_words_count - 2]) && \ + word_matches(p3, previous_words[previous_words_count - 3]) && \ + word_matches(p4, previous_words[previous_words_count - 4]) && \ + word_matches(p5, previous_words[previous_words_count - 5]) && \ + word_matches(p6, previous_words[previous_words_count - 6])) #define HeadMatches7(p1, p2, p3, p4, p5, p6, p7) \ -(previous_words_count >= 7 && \ - word_matches(p1, previous_words[previous_words_count - 1]) && \ - word_matches(p2, previous_words[previous_words_count - 2]) && \ - word_matches(p3, previous_words[previous_words_count - 3]) && \ - word_matches(p4, previous_words[previous_words_count - 4]) && \ - word_matches(p5, previous_words[previous_words_count - 5]) && \ - word_matches(p6, previous_words[previous_words_count - 6]) && \ - word_matches(p7, previous_words[previous_words_count - 7])) + (previous_words_count >= 7 && \ + word_matches(p1, previous_words[previous_words_count - 1]) && \ + word_matches(p2, previous_words[previous_words_count - 2]) && \ + word_matches(p3, previous_words[previous_words_count - 3]) && \ + word_matches(p4, previous_words[previous_words_count - 4]) && \ + word_matches(p5, previous_words[previous_words_count - 5]) && \ + word_matches(p6, previous_words[previous_words_count - 6]) && \ + word_matches(p7, previous_words[previous_words_count - 7])) /* Known command-starting keywords. */ static const char *const sql_commands[] = { @@ -1931,31 +1938,35 @@ psql_completion(const char *text, int start, int end) COMPLETE_WITH_LIST(list_ALTER2); } - else if (Matches4("ALTER", "TABLE", MatchAny, "ADD")) + /* ALTER TABLE xxx ADD */ + else if (Matches4("ALTER", "TABLE", MatchAny, "ADD")) COMPLETE_WITH_CONST("COLUMN"); - else if ( - (HeadMatches6("ALTER", "TABLE", MatchAny, "ADD", MatchAny, MatchAny)) - ) + else if (HeadMatches6("ALTER", "TABLE", MatchAny, "ADD", MatchAny, MatchAny)) { -// if (HeadMatches7("ALTER", "TABLE", MatchAny, "ADD", "COLUMN", MatchAny, MatchAny)) + if (TailMatches2("COLUMN", MatchAny)) + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_datatypes, NULL); + else + { if (TailMatches2("DEFAULT", MatchAny)) COMPLETE_WITH_LIST3("ENCODE", "NOT NULL", "NULL"); else if (TailMatches1("ENCODE")) COMPLETE_WITH_QUERY(Query_for_list_of_column_compressions); else if (TailMatches2("ENCODE", MatchAny)) COMPLETE_WITH_LIST3("DEFAULT", "NOT NULL", "NULL"); - else if (TailMatches1("NULL") || TailMatches2("NOT", "NULL")) + else if (TailMatches1("NULL")) COMPLETE_WITH_LIST2("ENCODE", "DEFAULT"); + else if (TailMatches1("NOT")) + COMPLETE_WITH_CONST("NULL"); else - COMPLETE_WITH_LIST4("DEFAULT", "ENCODE", "NOT NULL", "NULL"); + if (!TailMatches1("DEFAULT")) + COMPLETE_WITH_LIST4("DEFAULT", "ENCODE", "NOT NULL", "NULL"); + } } - else if ( - (HeadMatches5("ALTER", "TABLE", MatchAny, "ADD", MatchAny)) || - (HeadMatches6("ALTER", "TABLE", MatchAny, "ADD", "COLUMN", MatchAny))) - COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_datatypes, NULL); - + else if (HeadMatches5("ALTER", "TABLE", MatchAny, "ADD", MatchAny) && + (!TailMatches1("COLUMN"))) + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_datatypes, NULL); /* ALTER TABLE xxx ENABLE */ else if (Matches4("ALTER", "TABLE", MatchAny, "ENABLE")) From b249ddc518799ccdd861c42b41fa61f68e631a1e Mon Sep 17 00:00:00 2001 From: Robins Tharakan Date: Thu, 17 Aug 2017 13:20:55 +1000 Subject: [PATCH 041/149] Remove some recently added whitespace to match (as much as possible) with master. Update psql/README --- src/bin/psql/README.md | 3 +- src/bin/psql/tab-complete.c | 59 ++++++++++++++++++------------------- 2 files changed, 31 insertions(+), 31 deletions(-) diff --git a/src/bin/psql/README.md b/src/bin/psql/README.md index 40140042e2555..7cf630fcadd79 100644 --- a/src/bin/psql/README.md +++ b/src/bin/psql/README.md @@ -37,7 +37,8 @@ ALTER DEFAULT PRIVILEGES | Native ALTER GROUP | Native ALTER SCHEMA | Native ALTER TABLE | Partial -ALTER TABLE ADD COLUMN ENCODING | Unsupported +ALTER TABLE ADD COLUMN | **Yes** +ALTER TABLE ADD ENCODING | **Yes** ALTER TABLE SET LOCATION 's3://bucket/folder' | Unsupported ALTER TABLE SET FILE FORMAT format | Unsupported ALTER TABLE SET TABLE PROPERTIES ( 'numRows'='row_count' ) | Unsupported diff --git a/src/bin/psql/tab-complete.c b/src/bin/psql/tab-complete.c index 7c5809cbff730..2cad0010132cc 100644 --- a/src/bin/psql/tab-complete.c +++ b/src/bin/psql/tab-complete.c @@ -1440,17 +1440,17 @@ psql_completion(const char *text, int start, int end) #define HeadMatches3(p1, p2, p3) \ (previous_words_count >= 3 && \ - word_matches(p1, previous_words[previous_words_count - 1]) && \ - word_matches(p2, previous_words[previous_words_count - 2]) && \ - word_matches(p3, previous_words[previous_words_count - 3])) - + word_matches(p1, previous_words[previous_words_count - 1]) && \ + word_matches(p2, previous_words[previous_words_count - 2]) && \ + word_matches(p3, previous_words[previous_words_count - 3])) + #define HeadMatches4(p1, p2, p3, p4) \ (previous_words_count >= 4 && \ word_matches(p1, previous_words[previous_words_count - 1]) && \ word_matches(p2, previous_words[previous_words_count - 2]) && \ word_matches(p3, previous_words[previous_words_count - 3]) && \ word_matches(p4, previous_words[previous_words_count - 4])) - + #define HeadMatches5(p1, p2, p3, p4, p5) \ (previous_words_count >= 5 && \ word_matches(p1, previous_words[previous_words_count - 1]) && \ @@ -1477,7 +1477,7 @@ psql_completion(const char *text, int start, int end) word_matches(p5, previous_words[previous_words_count - 5]) && \ word_matches(p6, previous_words[previous_words_count - 6]) && \ word_matches(p7, previous_words[previous_words_count - 7])) - + /* Known command-starting keywords. */ static const char *const sql_commands[] = { "ABORT", "ALTER", "ANALYZE", "BEGIN", "CHECKPOINT", "CLOSE", "CLUSTER", @@ -1967,7 +1967,7 @@ psql_completion(const char *text, int start, int end) else if (HeadMatches5("ALTER", "TABLE", MatchAny, "ADD", MatchAny) && (!TailMatches1("COLUMN"))) COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_datatypes, NULL); - + /* ALTER TABLE xxx ENABLE */ else if (Matches4("ALTER", "TABLE", MatchAny, "ENABLE")) COMPLETE_WITH_LIST5("ALWAYS", "REPLICA", "ROW LEVEL SECURITY", "RULE", @@ -2372,34 +2372,33 @@ psql_completion(const char *text, int start, int end) else if (Matches6("CREATE", "ACCESS", "METHOD", MatchAny, "TYPE", MatchAny)) COMPLETE_WITH_CONST("HANDLER"); -/* CREATE DATABASE */ + /* CREATE DATABASE */ else if (Matches3("CREATE", "DATABASE", MatchAny)) COMPLETE_WITH_LIST10("WITH", "OWNER", "TEMPLATE", "ENCODING", "TABLESPACE", "IS_TEMPLATE", "ALLOW_CONNECTIONS", "CONNECTION LIMIT", "LC_COLLATE", "LC_CTYPE"); - /* CREATE DATABASE WITH */ - else if (Matches4("CREATE", "DATABASE", MatchAny, "WITH")) - COMPLETE_WITH_LIST9("OWNER", "TEMPLATE", "ENCODING", "TABLESPACE", - "IS_TEMPLATE", - "ALLOW_CONNECTIONS", "CONNECTION LIMIT", - "LC_COLLATE", "LC_CTYPE"); - - /* CREATE DATABASE WITH OWNER */ - else if (Matches5("CREATE", "DATABASE", MatchAny, "WITH", "OWNER")) - COMPLETE_WITH_LIST8("TEMPLATE", "ENCODING", "TABLESPACE", - "IS_TEMPLATE", - "ALLOW_CONNECTIONS", "CONNECTION LIMIT", - "LC_COLLATE", "LC_CTYPE"); + /* CREATE DATABASE WITH */ + else if (Matches4("CREATE", "DATABASE", MatchAny, "WITH")) + COMPLETE_WITH_LIST9("OWNER", "TEMPLATE", "ENCODING", "TABLESPACE", + "IS_TEMPLATE", + "ALLOW_CONNECTIONS", "CONNECTION LIMIT", + "LC_COLLATE", "LC_CTYPE"); - /* CREATE DATABASE OWNER */ - else if (Matches4("CREATE", "DATABASE", MatchAny, "OWNER")) - COMPLETE_WITH_LIST8("TEMPLATE", "ENCODING", "TABLESPACE", - "IS_TEMPLATE", - "ALLOW_CONNECTIONS", "CONNECTION LIMIT", - "LC_COLLATE", "LC_CTYPE"); + /* CREATE DATABASE WITH OWNER */ + else if (Matches5("CREATE", "DATABASE", MatchAny, "WITH", "OWNER")) + COMPLETE_WITH_LIST8("TEMPLATE", "ENCODING", "TABLESPACE", + "IS_TEMPLATE", + "ALLOW_CONNECTIONS", "CONNECTION LIMIT", + "LC_COLLATE", "LC_CTYPE"); + /* CREATE DATABASE OWNER */ + else if (Matches4("CREATE", "DATABASE", MatchAny, "OWNER")) + COMPLETE_WITH_LIST8("TEMPLATE", "ENCODING", "TABLESPACE", + "IS_TEMPLATE", + "ALLOW_CONNECTIONS", "CONNECTION LIMIT", + "LC_COLLATE", "LC_CTYPE"); /* CONNECTION LIMIT -- is allowed inside CREATE DATABASE, so use TailMatches */ else if (TailMatches2("CONNECTION", "LIMIT")) @@ -2408,7 +2407,7 @@ psql_completion(const char *text, int start, int end) else if (Matches4("CREATE", "DATABASE", MatchAny, "TEMPLATE")) COMPLETE_WITH_QUERY(Query_for_list_of_template_databases); -/* CREATE EXTENSION */ + /* CREATE EXTENSION */ /* Complete with available extensions rather than installed ones. */ else if (Matches2("CREATE", "EXTENSION")) COMPLETE_WITH_QUERY(Query_for_list_of_available_extensions); @@ -2422,7 +2421,7 @@ psql_completion(const char *text, int start, int end) COMPLETE_WITH_QUERY(Query_for_list_of_available_extension_versions); } -/* CREATE FOREIGN */ + /* CREATE FOREIGN */ else if (Matches2("CREATE", "FOREIGN")) COMPLETE_WITH_LIST2("DATA WRAPPER", "TABLE"); @@ -2501,7 +2500,7 @@ psql_completion(const char *text, int start, int end) // XXX See if you can add Regions here!? -/* CREATE POLICY */ + /* CREATE POLICY */ /* Complete "CREATE POLICY ON" */ else if (Matches3("CREATE", "POLICY", MatchAny)) COMPLETE_WITH_CONST("ON"); From e29a00f7da2ca5ff3b1dea6eaf7066c3abf13105 Mon Sep 17 00:00:00 2001 From: Robins Tharakan Date: Thu, 17 Aug 2017 16:38:22 +1000 Subject: [PATCH 042/149] Adding CREATE TABLE. Basics done. Column types etc. pending --- src/bin/psql/README.md | 2 +- src/bin/psql/tab-complete.c | 65 ++++++++++++++++++++++++++++++++++--- 2 files changed, 62 insertions(+), 5 deletions(-) diff --git a/src/bin/psql/README.md b/src/bin/psql/README.md index 7cf630fcadd79..62f616280d5bb 100644 --- a/src/bin/psql/README.md +++ b/src/bin/psql/README.md @@ -76,7 +76,7 @@ CREATE SCHEMA | Supported CREATE SCHEMA AUTHORIZATION | **Yes** CREATE SCHEMA IF NOT EXISTS | **Yes** CREATE TABLE | Partial -CREATE LOCAL TABLE | Unsupported +CREATE LOCAL TABLE | **Yes** CREATE TABLE IF | Unsupported CREATE TABLE BACKUP | Unsupported CREATE TABLE DISTSTYLE | Unsupported diff --git a/src/bin/psql/tab-complete.c b/src/bin/psql/tab-complete.c index 2cad0010132cc..15879bca72267 100644 --- a/src/bin/psql/tab-complete.c +++ b/src/bin/psql/tab-complete.c @@ -1062,6 +1062,7 @@ static const pgsql_thing_t words_after_create[] = { {"LANGUAGE", Query_for_list_of_languages}, {"LARGE OBJECT", NULL, NULL, THING_NO_CREATE | THING_NO_DROP}, {"LIBRARY", NULL, NULL, THING_NO_ALTER}, + {"LOCAL", NULL, NULL, THING_NO_DROP | THING_NO_ALTER}, /* CREATE LOCAL TABLE */ {"MATERIALIZED VIEW", NULL, &Query_for_list_of_matviews}, {"OPERATOR", NULL, NULL}, /* Querying for this is probably not such a * good idea. */ @@ -1478,7 +1479,30 @@ psql_completion(const char *text, int start, int end) word_matches(p6, previous_words[previous_words_count - 6]) && \ word_matches(p7, previous_words[previous_words_count - 7])) - /* Known command-starting keywords. */ +#define HeadMatches8(p1, p2, p3, p4, p5, p6, p7, p8) \ +(previous_words_count >= 8 && \ + word_matches(p1, previous_words[previous_words_count - 1]) && \ + word_matches(p2, previous_words[previous_words_count - 2]) && \ + word_matches(p3, previous_words[previous_words_count - 3]) && \ + word_matches(p4, previous_words[previous_words_count - 4]) && \ + word_matches(p5, previous_words[previous_words_count - 5]) && \ + word_matches(p6, previous_words[previous_words_count - 6]) && \ + word_matches(p7, previous_words[previous_words_count - 7]) && \ + word_matches(p8, previous_words[previous_words_count - 8])) + +#define HeadMatches9(p1, p2, p3, p4, p5, p6, p7, p8, p9) \ +(previous_words_count >= 9 && \ + word_matches(p1, previous_words[previous_words_count - 1]) && \ + word_matches(p2, previous_words[previous_words_count - 2]) && \ + word_matches(p3, previous_words[previous_words_count - 3]) && \ + word_matches(p4, previous_words[previous_words_count - 4]) && \ + word_matches(p5, previous_words[previous_words_count - 5]) && \ + word_matches(p6, previous_words[previous_words_count - 6]) && \ + word_matches(p7, previous_words[previous_words_count - 7]) && \ + word_matches(p8, previous_words[previous_words_count - 8]) && \ + word_matches(p9, previous_words[previous_words_count - 9])) + + /* Known command-starting keywords. */ static const char *const sql_commands[] = { "ABORT", "ALTER", "ANALYZE", "BEGIN", "CHECKPOINT", "CLOSE", "CLUSTER", "COMMENT", "COMMIT", "COPY", "CREATE", "DEALLOCATE", "DECLARE", @@ -2646,13 +2670,46 @@ psql_completion(const char *text, int start, int end) TailMatches1("FROM")) COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tables, NULL); -/* CREATE TABLE --- is allowed inside CREATE SCHEMA, so use TailMatches */ /* Complete "CREATE TEMP/TEMPORARY" with the possible temp objects */ - else if (TailMatches2("CREATE", "TEMP|TEMPORARY")) - COMPLETE_WITH_LIST3("SEQUENCE", "TABLE", "VIEW"); + else if (HeadMatches2("CREATE", "TEMP|TEMPORARY")) + COMPLETE_WITH_LIST3("SEQUENCE", "TABLE", "VIEW"); + /* Complete "CREATE UNLOGGED" with TABLE or MATVIEW */ else if (TailMatches2("CREATE", "UNLOGGED")) COMPLETE_WITH_LIST2("TABLE", "MATERIALIZED VIEW"); + + /* CREATE TABLE is allowed inside CREATE SCHEMA in Native Postgres so although + we ought to use TailMatches, using HeadMatches since this allows supporting + far more options */ + else if (Matches2("CREATE", "TABLE") || + Matches3("CREATE", "TEMP|TEMPORARY", "TABLE") || + Matches4("CREATE", "LOCAL", "TEMP|TEMPORARY", "TABLE")) + COMPLETE_WITH_CONST("IF NOT EXISTS"); + + else if (TailMatches3("CREATE", "TABLE", MatchAny)) + COMPLETE_WITH_CONST("("); + + else if (HeadMatches7("CREATE", "TABLE", MatchAny, MatchAny, MatchAny, MatchAny, MatchAny) /*|| + HeadMatches3("CREATE", "TEMP|TEMPORARY", "TABLE") || + HeadMatches4("CREATE", "LOCAL", "TEMP|TEMPORARY", "TABLE")*/) + { + //if (TailMatches3(MatchAnyExcept("("), MatchAny, ")")) + if (TailMatches1("BACKUP")) + COMPLETE_WITH_LIST2("YES", "NO"); + else if (TailMatches2("BACKUP", "YES|NO")) + COMPLETE_WITH_LIST5("DISTSTYLE", "DISTKEY (", "COMPOUND", "INTERLEAVED", "SORTKEY ("); + else if (TailMatches1("DISTSTYLE")) + COMPLETE_WITH_LIST3("EVEN", "KEY", "ALL"); + else if (TailMatches2("DISTSTYLE", "EVEN|KEY|ALL")) + COMPLETE_WITH_LIST3("COMPOUND", "INTERLEAVED", "SORTKEY ("); + else if (TailMatches4("DISTKEY", "(", MatchAny, ")")) + COMPLETE_WITH_LIST3("COMPOUND", "INTERLEAVED", "SORTKEY ("); + else if (TailMatches1("COMPOUND|INTERLEAVED")) + COMPLETE_WITH_CONST("SORTKEY ("); + else + COMPLETE_WITH_LIST6("BACKUP", "DISTSTYLE", "DISTKEY (", "COMPOUND", "INTERLEAVED", "SORTKEY ("); + } + /* Complete PARTITION BY with RANGE ( or LIST ( or ... */ else if (TailMatches2("PARTITION", "BY")) COMPLETE_WITH_LIST2("RANGE (", "LIST ("); From 2ea59b3fd2a9728dfcb843ceb16a366924534652 Mon Sep 17 00:00:00 2001 From: Robins Tharakan Date: Thu, 17 Aug 2017 17:18:10 +1000 Subject: [PATCH 043/149] Minor bugfixes, alterations to the recent patches. column-types are still pending --- src/bin/psql/tab-complete.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/bin/psql/tab-complete.c b/src/bin/psql/tab-complete.c index 15879bca72267..c604d82d71869 100644 --- a/src/bin/psql/tab-complete.c +++ b/src/bin/psql/tab-complete.c @@ -2686,10 +2686,10 @@ psql_completion(const char *text, int start, int end) Matches4("CREATE", "LOCAL", "TEMP|TEMPORARY", "TABLE")) COMPLETE_WITH_CONST("IF NOT EXISTS"); - else if (TailMatches3("CREATE", "TABLE", MatchAny)) + else if (Matches3("CREATE", "TABLE", MatchAny)) COMPLETE_WITH_CONST("("); - else if (HeadMatches7("CREATE", "TABLE", MatchAny, MatchAny, MatchAny, MatchAny, MatchAny) /*|| + else if (HeadMatches2("CREATE", "TABLE") /*|| HeadMatches3("CREATE", "TEMP|TEMPORARY", "TABLE") || HeadMatches4("CREATE", "LOCAL", "TEMP|TEMPORARY", "TABLE")*/) { @@ -2702,7 +2702,7 @@ psql_completion(const char *text, int start, int end) COMPLETE_WITH_LIST3("EVEN", "KEY", "ALL"); else if (TailMatches2("DISTSTYLE", "EVEN|KEY|ALL")) COMPLETE_WITH_LIST3("COMPOUND", "INTERLEAVED", "SORTKEY ("); - else if (TailMatches4("DISTKEY", "(", MatchAny, ")")) + else if (TailMatches2("DISTKEY", MatchAny)) COMPLETE_WITH_LIST3("COMPOUND", "INTERLEAVED", "SORTKEY ("); else if (TailMatches1("COMPOUND|INTERLEAVED")) COMPLETE_WITH_CONST("SORTKEY ("); From bc7a58fb8fe1f469ddac3d1f2333b8cc340614d7 Mon Sep 17 00:00:00 2001 From: Robins Tharakan Date: Thu, 17 Aug 2017 17:22:59 +1000 Subject: [PATCH 044/149] Update psql/README per recent updates --- src/bin/psql/README.md | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/bin/psql/README.md b/src/bin/psql/README.md index 62f616280d5bb..d73811cbd3184 100644 --- a/src/bin/psql/README.md +++ b/src/bin/psql/README.md @@ -77,11 +77,13 @@ CREATE SCHEMA AUTHORIZATION | **Yes** CREATE SCHEMA IF NOT EXISTS | **Yes** CREATE TABLE | Partial CREATE LOCAL TABLE | **Yes** -CREATE TABLE IF | Unsupported -CREATE TABLE BACKUP | Unsupported -CREATE TABLE DISTSTYLE | Unsupported -CREATE TABLE DISTKEY | Unsupported -CREATE TABLE SORTKEY | Unsupported +CREATE TABLE IF | **Yes** +CREATE TABLE BACKUP | **Yes** +CREATE TABLE DISTSTYLE | **Yes** +CREATE TABLE DISTKEY | **Yes** +CREATE TABLE INTERLEAVED SORTKEY | **Yes** +CREATE TABLE COMPOUND SORTKEY | **Yes** +CREATE TABLE SORTKEY | **Yes** CREATE TABLE | Unsupported CREATE TABLE AS | Unsupported CREATE USER | Supported From 49d65a293da96d43d2fd3ebf457de96c25c0c2e0 Mon Sep 17 00:00:00 2001 From: Robins Tharakan Date: Fri, 25 Aug 2017 06:23:49 +1000 Subject: [PATCH 045/149] DROP TABLE now fully supported --- src/bin/psql/README.md | 26 +++++++++---------- src/bin/psql/tab-complete.c | 52 +++++++++++++++++++++++++++++++++++++ 2 files changed, 65 insertions(+), 13 deletions(-) diff --git a/src/bin/psql/README.md b/src/bin/psql/README.md index d73811cbd3184..515dbfb0b18ba 100644 --- a/src/bin/psql/README.md +++ b/src/bin/psql/README.md @@ -8,10 +8,10 @@ NOTE: This branch is maintained as a personal effort and is not sponsored / supp Support Legend | Description :-------------:| ----------- Native | All combinations supported by Native psql -Unsupported | Not supported (yet) +Unsupported | Not (yet)supported Partial | Only some combinations supported -**Yes** | Supported by this branch (not Native) -Supported | All combinations (now) supported by this branch +**Yes** | Supported by (only) this branch (not Native) +Complete | All combinations supported @@ -46,7 +46,7 @@ ALTER TABLE PARTITION ( partition_column=partition_value [, ...] ) SET LOCATION ALTER TABLE ADD PARTITION ( partition_column=partition_value [, ...] ) LOCATION 's3://bucket/folder' | Unsupported ALTER TABLE DROP PARTITION ( partition_column=partition_value [, ...] ) | Unsupported ALTER TABLE APPEND | Unsupported -ALTER USER | Supported +ALTER USER | Complete ALTER USER [NO]CREATEUSER | **Yes** ANALYZE | Partial ANALYZE VERBOSE | Unsupported @@ -60,19 +60,19 @@ COMMENT | Native COMMIT | Native COPY | Unsupported COPY .. REGION | **Yes** -CREATE DATABASE | Supported +CREATE DATABASE | Complete CREATE DATABASE CONNECTION LIMIT UNLIMITED | **Yes** CREATE DATABASE WITH | **Yes** CREATE DATABASE WITH OWNER | **Yes** CREATE DATABASE OWNER | **Yes** CREATE EXTERNAL SCHEMA | Unsupported CREATE EXTERNAL TABLE | Unsupported -CREATE FUNCTION | Supported +CREATE FUNCTION | Complete CREATE OR REPLACE FUNCTION | **Yes** CREATE GROUP | Native CREATE LIBRARY | **Yes** CREATE OR REPLACE LIBRARY | **Yes** -CREATE SCHEMA | Supported +CREATE SCHEMA | Complete CREATE SCHEMA AUTHORIZATION | **Yes** CREATE SCHEMA IF NOT EXISTS | **Yes** CREATE TABLE | Partial @@ -86,7 +86,7 @@ CREATE TABLE COMPOUND SORTKEY | **Yes** CREATE TABLE SORTKEY | **Yes** CREATE TABLE | Unsupported CREATE TABLE AS | Unsupported -CREATE USER | Supported +CREATE USER | Complete CREATE USER [NO]CREATEUSER | **Yes** CREATE VIEW | Partial CREATE OR REPLACE VIEW | **Yes** @@ -100,13 +100,13 @@ DROP FUNCTION | Native DROP GROUP | Native DROP LIBRARY | **Yes** DROP SCHEMA | Partial -DROP SCHEMA IF EXISTS | Unsupported -DROP TABLE | Partial -DROP TABLE | Unsupported +DROP SCHEMA IF EXISTS | **Yes** +DROP TABLE | Complete +DROP TABLE IF EXISTS | **Yes** DROP USER | Partial -DROP USER IF EXISTS | Unsupported +DROP USER IF EXISTS | **Yes** DROP VIEW | Partial -DROP VIEW IF EXISTS | Unsupported +DROP VIEW IF EXISTS | **Yes** END | Native EXECUTE | Native EXPLAIN | Native diff --git a/src/bin/psql/tab-complete.c b/src/bin/psql/tab-complete.c index c604d82d71869..c6b020d4d97a0 100644 --- a/src/bin/psql/tab-complete.c +++ b/src/bin/psql/tab-complete.c @@ -601,6 +601,21 @@ static const SchemaQuery Query_for_list_of_views = { NULL }; +static const SchemaQuery Query_for_list_of_users = { + /* catname */ + "pg_catalog.pg_user c", + /* selcondition */ + "c.relkind IN (" CppAsString2(RELKIND_VIEW) ")", + /* viscondition */ + NULL, + /* namespace */ + "c.relnamespace", + /* result */ + "pg_catalog.quote_ident(c.relname)", + /* qualresult */ + NULL +}; + static const SchemaQuery Query_for_list_of_matviews = { /* catname */ "pg_catalog.pg_class c", @@ -2959,6 +2974,13 @@ psql_completion(const char *text, int start, int end) else if (Matches3("DROP", "OWNED", "BY")) COMPLETE_WITH_QUERY(Query_for_list_of_roles); + /* DROP SCHEMA */ + else if (Matches2("DROP", "SCHEMA")) + COMPLETE_WITH_QUERY(Query_for_list_of_schemas + " UNION ALL SELECT 'IF EXISTS'"); + else if ((HeadMatches3("DROP", "SCHEMA", MatchAny)) && TailMatches1(MatchAnyExcept("EXISTS"))) + COMPLETE_WITH_LIST2("CASCADE", "RESTRICT"); + /* DROP TEXT SEARCH */ else if (Matches3("DROP", "TEXT", "SEARCH")) COMPLETE_WITH_LIST4("CONFIGURATION", "DICTIONARY", "PARSER", "TEMPLATE"); @@ -3010,6 +3032,36 @@ psql_completion(const char *text, int start, int end) else if (Matches5("DROP", "RULE", MatchAny, "ON", MatchAny)) COMPLETE_WITH_LIST2("CASCADE", "RESTRICT"); + /* DROP TABLE */ + else if (Matches2("DROP", "TABLE")) + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tables, + " UNION ALL SELECT 'IF EXISTS'"); + else if ((Matches3("DROP", "TABLE", MatchAny))) + COMPLETE_WITH_LIST2("CASCADE", "RESTRICT"); + else if (Matches4("DROP", "TABLE", MatchAny, MatchAny) + && TailMatches1(MatchAnyExcept("CASCADE|RESTRICT"))) + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tables, NULL); + else if ((HeadMatches5("DROP", "TABLE", MatchAny, MatchAny, MatchAny)) + && TailMatches1(MatchAnyExcept("EXISTS|CASCADE|RESTRICT"))) + COMPLETE_WITH_LIST2("CASCADE", "RESTRICT"); + + /* DROP USER */ + else if (Matches2("DROP", "USER")) + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_users, + " UNION ALL SELECT 'IF EXISTS'"); + else if (HeadMatches3("DROP", "USER", MatchAny)) + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_users, NULL); + + /* DROP VIEW */ + else if (Matches2("DROP", "VIEW")) + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_views, + " UNION ALL SELECT 'IF EXISTS'"); + else if (HeadMatches3("DROP", "VIEW", MatchAny)) + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_views, NULL); + else if ((HeadMatches3("DROP", "VIEW", MatchAny)) && TailMatches1(MatchAnyExcept("EXISTS"))) + COMPLETE_WITH_LIST2("CASCADE", "RESTRICT"); + + /* EXECUTE */ else if (Matches1("EXECUTE")) COMPLETE_WITH_QUERY(Query_for_list_of_prepared_statements); From 2e8bc42e8ef6a935760234a753ff9f26813f929e Mon Sep 17 00:00:00 2001 From: Robins Tharakan Date: Fri, 25 Aug 2017 06:55:10 +1000 Subject: [PATCH 046/149] Now DROP VIEW / DROP SCHEMA / DROP USER also working --- src/bin/psql/README.md | 6 +++--- src/bin/psql/tab-complete.c | 30 +++++++++++++++++++----------- 2 files changed, 22 insertions(+), 14 deletions(-) diff --git a/src/bin/psql/README.md b/src/bin/psql/README.md index 515dbfb0b18ba..80fd3cd42c89f 100644 --- a/src/bin/psql/README.md +++ b/src/bin/psql/README.md @@ -99,13 +99,13 @@ DROP DATABASE | Native DROP FUNCTION | Native DROP GROUP | Native DROP LIBRARY | **Yes** -DROP SCHEMA | Partial +DROP SCHEMA | Complete DROP SCHEMA IF EXISTS | **Yes** DROP TABLE | Complete DROP TABLE IF EXISTS | **Yes** -DROP USER | Partial +DROP USER | Complete DROP USER IF EXISTS | **Yes** -DROP VIEW | Partial +DROP VIEW | Complete DROP VIEW IF EXISTS | **Yes** END | Native EXECUTE | Native diff --git a/src/bin/psql/tab-complete.c b/src/bin/psql/tab-complete.c index c6b020d4d97a0..4520162756e93 100644 --- a/src/bin/psql/tab-complete.c +++ b/src/bin/psql/tab-complete.c @@ -2977,8 +2977,14 @@ psql_completion(const char *text, int start, int end) /* DROP SCHEMA */ else if (Matches2("DROP", "SCHEMA")) COMPLETE_WITH_QUERY(Query_for_list_of_schemas - " UNION ALL SELECT 'IF EXISTS'"); - else if ((HeadMatches3("DROP", "SCHEMA", MatchAny)) && TailMatches1(MatchAnyExcept("EXISTS"))) + " UNION ALL SELECT 'IF EXISTS'"); + else if ((Matches3("DROP", "SCHEMA", MatchAny))) + COMPLETE_WITH_LIST2("CASCADE", "RESTRICT"); + else if (Matches4("DROP", "SCHEMA", MatchAny, MatchAny) + && TailMatches1(MatchAnyExcept("CASCADE|RESTRICT"))) + COMPLETE_WITH_QUERY(Query_for_list_of_schemas); + else if ((HeadMatches5("DROP", "SCHEMA", MatchAny, MatchAny, MatchAny)) + && TailMatches1(MatchAnyExcept("EXISTS|CASCADE|RESTRICT"))) COMPLETE_WITH_LIST2("CASCADE", "RESTRICT"); /* DROP TEXT SEARCH */ @@ -3035,7 +3041,7 @@ psql_completion(const char *text, int start, int end) /* DROP TABLE */ else if (Matches2("DROP", "TABLE")) COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tables, - " UNION ALL SELECT 'IF EXISTS'"); + " UNION ALL SELECT 'IF EXISTS'"); else if ((Matches3("DROP", "TABLE", MatchAny))) COMPLETE_WITH_LIST2("CASCADE", "RESTRICT"); else if (Matches4("DROP", "TABLE", MatchAny, MatchAny) @@ -3046,19 +3052,21 @@ psql_completion(const char *text, int start, int end) COMPLETE_WITH_LIST2("CASCADE", "RESTRICT"); /* DROP USER */ - else if (Matches2("DROP", "USER")) + else if (HeadMatches2("DROP", "USER")) COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_users, - " UNION ALL SELECT 'IF EXISTS'"); - else if (HeadMatches3("DROP", "USER", MatchAny)) - COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_users, NULL); - + " UNION ALL SELECT 'IF EXISTS'"); + /* DROP VIEW */ else if (Matches2("DROP", "VIEW")) COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_views, - " UNION ALL SELECT 'IF EXISTS'"); - else if (HeadMatches3("DROP", "VIEW", MatchAny)) + " UNION ALL SELECT 'IF EXISTS'"); + else if ((Matches3("DROP", "VIEW", MatchAny))) + COMPLETE_WITH_LIST2("CASCADE", "RESTRICT"); + else if (Matches4("DROP", "VIEW", MatchAny, MatchAny) + && TailMatches1(MatchAnyExcept("CASCADE|RESTRICT"))) COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_views, NULL); - else if ((HeadMatches3("DROP", "VIEW", MatchAny)) && TailMatches1(MatchAnyExcept("EXISTS"))) + else if ((HeadMatches5("DROP", "VIEW", MatchAny, MatchAny, MatchAny)) + && TailMatches1(MatchAnyExcept("EXISTS|CASCADE|RESTRICT"))) COMPLETE_WITH_LIST2("CASCADE", "RESTRICT"); From 46da62bf211c68881051eb375b78f496d9786aab Mon Sep 17 00:00:00 2001 From: Robins Tharakan Date: Fri, 25 Aug 2017 14:55:18 +1000 Subject: [PATCH 047/149] DROP USER now shows list of users --- src/bin/psql/tab-complete.c | 28 ++++++++++------------------ 1 file changed, 10 insertions(+), 18 deletions(-) diff --git a/src/bin/psql/tab-complete.c b/src/bin/psql/tab-complete.c index 4520162756e93..f55baf0956c5c 100644 --- a/src/bin/psql/tab-complete.c +++ b/src/bin/psql/tab-complete.c @@ -601,21 +601,6 @@ static const SchemaQuery Query_for_list_of_views = { NULL }; -static const SchemaQuery Query_for_list_of_users = { - /* catname */ - "pg_catalog.pg_user c", - /* selcondition */ - "c.relkind IN (" CppAsString2(RELKIND_VIEW) ")", - /* viscondition */ - NULL, - /* namespace */ - "c.relnamespace", - /* result */ - "pg_catalog.quote_ident(c.relname)", - /* qualresult */ - NULL -}; - static const SchemaQuery Query_for_list_of_matviews = { /* catname */ "pg_catalog.pg_class c", @@ -795,6 +780,11 @@ static const SchemaQuery Query_for_list_of_statistics = { " FROM pg_catalog.pg_roles "\ " WHERE substring(pg_catalog.quote_ident(rolname),1,%d)='%s'" +#define Query_for_list_of_users \ +" SELECT pg_catalog.quote_ident(usename) "\ +" FROM pg_catalog.pg_user "\ +" WHERE substring(pg_catalog.quote_ident(usename),1,%d)='%s'" + #define Query_for_list_of_grant_roles \ " SELECT pg_catalog.quote_ident(rolname) "\ " FROM pg_catalog.pg_roles "\ @@ -3053,9 +3043,11 @@ psql_completion(const char *text, int start, int end) /* DROP USER */ else if (HeadMatches2("DROP", "USER")) - COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_users, - " UNION ALL SELECT 'IF EXISTS'"); - + COMPLETE_WITH_QUERY(Query_for_list_of_users + " UNION ALL SELECT 'IF EXISTS'"); + else if (HeadMatches4("DROP", "USER", "IF", "EXISTS")) + COMPLETE_WITH_QUERY(Query_for_list_of_users); + /* DROP VIEW */ else if (Matches2("DROP", "VIEW")) COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_views, From 341a8071b20861b7cdd5b383de86b57094469555 Mon Sep 17 00:00:00 2001 From: Robins Tharakan Date: Fri, 25 Aug 2017 16:25:13 +1000 Subject: [PATCH 048/149] Finally, fully tested FETCH works per Redshift --- src/bin/psql/README.md | 6 +++--- src/bin/psql/tab-complete.c | 38 +++++++++++++++++++++---------------- 2 files changed, 25 insertions(+), 19 deletions(-) diff --git a/src/bin/psql/README.md b/src/bin/psql/README.md index 80fd3cd42c89f..bae07ec7d1f7b 100644 --- a/src/bin/psql/README.md +++ b/src/bin/psql/README.md @@ -110,9 +110,9 @@ DROP VIEW IF EXISTS | **Yes** END | Native EXECUTE | Native EXPLAIN | Native -FETCH | Partial -FETCH NEXT | Unsupported -FETCH ALL | Unsupported +FETCH | Complete +FETCH NEXT | **Yes** +FETCH ALL | **Yes** GRANT | Partial GRANT ALL PRIVILEGES | Unsupported GRANT ALL ON TABLE ALL TABLES IN SCHEMA | Unsupported diff --git a/src/bin/psql/tab-complete.c b/src/bin/psql/tab-complete.c index f55baf0956c5c..688c0c2f79dfa 100644 --- a/src/bin/psql/tab-complete.c +++ b/src/bin/psql/tab-complete.c @@ -700,6 +700,9 @@ static const SchemaQuery Query_for_list_of_statistics = { " SELECT 'TEXT32K' UNION ALL "\ " SELECT 'ZST' " +#define Query_for_list_of_cursors \ +" SELECT trim(name) FROM stv_active_cursors" + #define Query_for_list_of_enum_values \ "SELECT pg_catalog.quote_literal(enumlabel) "\ " FROM pg_catalog.pg_enum e, pg_catalog.pg_type t "\ @@ -3081,21 +3084,18 @@ psql_completion(const char *text, int start, int end) Matches3("EXPLAIN", "ANALYZE", "VERBOSE")) COMPLETE_WITH_LIST5("SELECT", "INSERT", "DELETE", "UPDATE", "DECLARE"); -/* FETCH && MOVE */ - /* Complete FETCH with one of FORWARD, BACKWARD, RELATIVE */ - else if (Matches1("FETCH|MOVE")) - COMPLETE_WITH_LIST4("ABSOLUTE", "BACKWARD", "FORWARD", "RELATIVE"); - /* Complete FETCH with one of ALL, NEXT, PRIOR */ - else if (Matches2("FETCH|MOVE", MatchAny)) - COMPLETE_WITH_LIST3("ALL", "NEXT", "PRIOR"); - - /* - * Complete FETCH with "FROM" or "IN". These are equivalent, - * but we may as well tab-complete both: perhaps some users prefer one - * variant or the other. - */ - else if (Matches3("FETCH|MOVE", MatchAny, MatchAny)) - COMPLETE_WITH_LIST2("FROM", "IN"); +/* FETCH */ + /* Experiment with removing Postgres compliant and keeping only Redshift */ + else if (Matches1("FETCH")) + COMPLETE_WITH_LIST4("ALL", "FORWARD", "FROM", "NEXT"); + else if (Matches2("FETCH", "FORWARD")) + COMPLETE_WITH_LIST2("ALL", "FROM"); + else if (Matches2("FETCH", "NEXT|ALL")) + COMPLETE_WITH_CONST("FROM"); + else if (Matches3("FETCH", "FORWARD", MatchAnyExcept("FROM"))) + COMPLETE_WITH_CONST("FROM"); + else if (HeadMatches1("FETCH") && TailMatches1("FROM")) + COMPLETE_WITH_QUERY(Query_for_list_of_cursors); /* FOREIGN DATA WRAPPER */ /* applies in ALTER/DROP FDW and in CREATE SERVER */ @@ -3382,6 +3382,8 @@ psql_completion(const char *text, int start, int end) COMPLETE_WITH_ATTR(prev3_wd, ""); /* PREPARE xx AS */ + else if (Matches2("PREPARE", MatchAny)) + COMPLETE_WITH_CONST("AS"); else if (Matches3("PREPARE", MatchAny, "AS")) COMPLETE_WITH_LIST4("SELECT", "UPDATE", "INSERT", "DELETE FROM"); @@ -3517,7 +3519,11 @@ psql_completion(const char *text, int start, int end) /* Complete RESET SESSION with AUTHORIZATION */ else if (Matches2("RESET", "SESSION")) COMPLETE_WITH_CONST("AUTHORIZATION"); - /* Complete SET with "TO" */ + /* RESET parameter / ALL */ + else if (Matches1("RESET")) + COMPLETE_WITH_QUERY(Query_for_list_of_show_vars + " UNION 'ALL'"); +/* Complete SET with "TO" */ else if (Matches2("SET", MatchAny)) COMPLETE_WITH_CONST("TO"); /* Complete ALTER DATABASE|FUNCTION|ROLE|USER ... SET */ From 3cacb3e90416563c11e87077b2fe92d4a238a329 Mon Sep 17 00:00:00 2001 From: Robins Tharakan Date: Sat, 26 Aug 2017 08:21:49 +1000 Subject: [PATCH 049/149] Add some basics for SELECT --- src/bin/psql/tab-complete.c | 44 ++++++++++++++++++++++++++++++++----- 1 file changed, 39 insertions(+), 5 deletions(-) diff --git a/src/bin/psql/tab-complete.c b/src/bin/psql/tab-complete.c index 688c0c2f79dfa..661c93d3f7159 100644 --- a/src/bin/psql/tab-complete.c +++ b/src/bin/psql/tab-complete.c @@ -775,7 +775,7 @@ static const SchemaQuery Query_for_list_of_statistics = { "SELECT name FROM "\ " (SELECT pg_catalog.lower(name) AS name FROM pg_catalog.pg_settings "\ " UNION ALL SELECT 'session authorization' "\ -" UNION ALL SELECT 'all') ss "\ +" UNION ALL SELECT 'ALL') ss "\ " WHERE substring(name,1,%d)='%s'" #define Query_for_list_of_roles \ @@ -3145,7 +3145,8 @@ psql_completion(const char *text, int start, int end) " UNION SELECT 'TEMPORARY'" " UNION SELECT 'EXECUTE'" " UNION SELECT 'USAGE'" - " UNION SELECT 'ALL'"); + " UNION SELECT 'ALL'" + " UNION SELECT 'GRANT OPTION FOR'"); } /* @@ -3461,7 +3462,41 @@ psql_completion(const char *text, int start, int end) COMPLETE_WITH_CONST("IS"); /* SELECT */ - /* naah . . . */ + else if (Matches1("SELECT")) + COMPLETE_WITH_LIST4("TOP", "ALL", "DISTINCT", "*"); + else if (Matches2("SELECT", "*")) + COMPLETE_WITH_LIST7("FROM", "WHERE", "GROUP BY", "HAVING", "ORDER BY", "LIMIT", "OFFSET"); + else if (Matches2("SELECT", MatchAny)) + COMPLETE_WITH_LIST7("FROM", "WHERE", "GROUP BY", "HAVING", "ORDER BY", "LIMIT", "OFFSET"); +/* SELECT FROM */ + else if (HeadMatches1("SELECT") && TailMatches1("FROM")) + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tables, NULL); + else if (HeadMatches1("SELECT") && TailMatches2("FROM", MatchAny)) + COMPLETE_WITH_LIST7("WHERE", "GROUP BY", "HAVING", "UNION", "ORDER BY", "LIMIT", "OFFSET"); + /* SELECT WHERE */ + else if (HeadMatches1("SELECT") && TailMatches3("FROM", MatchAny, "WHERE")) + COMPLETE_WITH_ATTR(prev2_wd, ""); + else if ( + (HeadMatches1("SELECT") && TailMatches3("WHERE", MatchAny, MatchAnyExcept("HAVING|BY|LIMIT|OFFSET"))) || + (HeadMatches1("SELECT") && TailMatches4("WHERE", MatchAny, MatchAnyExcept("HAVING|BY|LIMIT|OFFSET"), MatchAnyExcept("HAVING|BY|LIMIT|OFFSET"))) + ) + COMPLETE_WITH_LIST5("GROUP BY", "HAVING", "ORDER BY", "LIMIT", "OFFSET"); + /* SELECT GROUP BY */ + else if (HeadMatches1("SELECT") && TailMatches3("GROUP", "BY", MatchAny)) + COMPLETE_WITH_LIST5("HAVING", "UNION", "ORDER BY", "LIMIT", "OFFSET"); + /* SELECT ORDER BY */ + else if (HeadMatches1("SELECT") && TailMatches3("ORDER", "BY", MatchAny)) + COMPLETE_WITH_LIST2("ASC", "DESC"); + else if ((HeadMatches1("SELECT") && TailMatches3("ORDER", "BY", MatchAny)) || + (HeadMatches1("SELECT") && TailMatches4("ORDER", "BY", MatchAny, "ASC|DESC"))) + COMPLETE_WITH_LIST2("LIMIT", "OFFSET"); + /* SELECT LIMIT */ + else if (HeadMatches1("SELECT") && TailMatches1("LIMIT")) + COMPLETE_WITH_CONST("ALL"); + /* SELECT OFFSET */ + else if (HeadMatches1("SELECT") && TailMatches2("LIMIT", MatchAny)) + COMPLETE_WITH_CONST("OFFSET"); + /* SET, RESET, SHOW */ /* Complete with a variable name */ @@ -3521,8 +3556,7 @@ psql_completion(const char *text, int start, int end) COMPLETE_WITH_CONST("AUTHORIZATION"); /* RESET parameter / ALL */ else if (Matches1("RESET")) - COMPLETE_WITH_QUERY(Query_for_list_of_show_vars - " UNION 'ALL'"); + COMPLETE_WITH_QUERY(Query_for_list_of_show_vars); /* Complete SET with "TO" */ else if (Matches2("SET", MatchAny)) COMPLETE_WITH_CONST("TO"); From fb8f0ef101490d3835ec309a4207f0ccf312a8b5 Mon Sep 17 00:00:00 2001 From: Robins Tharakan Date: Tue, 29 Aug 2017 14:54:24 +1000 Subject: [PATCH 050/149] Add support for VACUUM / SET / SELECT (some options) --- src/bin/psql/README.md | 27 ++++++++++++--------------- src/bin/psql/tab-complete.c | 27 ++++++++++++++++++++++----- 2 files changed, 34 insertions(+), 20 deletions(-) diff --git a/src/bin/psql/README.md b/src/bin/psql/README.md index bae07ec7d1f7b..d2f35c5ee32dd 100644 --- a/src/bin/psql/README.md +++ b/src/bin/psql/README.md @@ -124,21 +124,18 @@ GRANT | Unsupported INSERT | Partial INSERT INTO DEFAULT VALUES | Unsupported LOCK | Native -PREPARE | Partial -PREPARE AS | Unsupported -RESET | Partial -RESET ALL | Unsupported +PREPARE | Native +RESET | Native REVOKE | Partial -REVOKE GRANT | Unsupported -REVOKE SELECT ALL PRIVILEGES | Unsupported +REVOKE GRANT | **Yes** REVOKE | Unsupported ROLLBACK | Native SELECT | Partial -SELECT TOP | Unsupported -SELECT ALL | Unsupported -SELECT DISTINCT | Unsupported +SELECT TOP | **Yes** +SELECT ALL | **Yes** +SELECT DISTINCT | **Yes** SELECT | Unsupported -SELECT INTO | Unsupported +SELECT INTO | **WIP** SET | Partial SET ALL | Unsupported SET SEED TO | Unsupported @@ -153,11 +150,11 @@ UNLOAD | Unsupported UPDATE | Unsupported UPDATE SET FROM | Unsupported UPDATE SET WHERE | Unsupported -VACUUM | Partial -VACUUM SORT | Unsupported -VACUUM DELETE | Unsupported -VACUUM REINDEX | Unsupported -VACUUM TO | Unsupported +VACUUM | Complete +VACUUM SORT | **Yes** +VACUUM DELETE | **Yes** +VACUUM REINDEX | **Yes** +VACUUM TO | **Yes** diff --git a/src/bin/psql/tab-complete.c b/src/bin/psql/tab-complete.c index 661c93d3f7159..9ce4e32ee3054 100644 --- a/src/bin/psql/tab-complete.c +++ b/src/bin/psql/tab-complete.c @@ -3464,11 +3464,18 @@ psql_completion(const char *text, int start, int end) /* SELECT */ else if (Matches1("SELECT")) COMPLETE_WITH_LIST4("TOP", "ALL", "DISTINCT", "*"); +/* remove if MatchAny below works for * else if (Matches2("SELECT", "*")) COMPLETE_WITH_LIST7("FROM", "WHERE", "GROUP BY", "HAVING", "ORDER BY", "LIMIT", "OFFSET"); +*/ else if (Matches2("SELECT", MatchAny)) + COMPLETE_WITH_LIST8("FROM", "INTO", "WHERE", "GROUP BY", "HAVING", "ORDER BY", "LIMIT", "OFFSET"); + else if (HeadMatches1("SELECT") && TailMatches1("INTO")) + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tables, + "UNION SELECT 'TEMP' UNION SELECT 'TEMPORARY' UNION SELECT 'TABLE'"); + else if (HeadMatches1("SELECT") && TailMatches2("INTO", MatchAny)) COMPLETE_WITH_LIST7("FROM", "WHERE", "GROUP BY", "HAVING", "ORDER BY", "LIMIT", "OFFSET"); -/* SELECT FROM */ + /* SELECT FROM */ else if (HeadMatches1("SELECT") && TailMatches1("FROM")) COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tables, NULL); else if (HeadMatches1("SELECT") && TailMatches2("FROM", MatchAny)) @@ -3501,7 +3508,10 @@ psql_completion(const char *text, int start, int end) /* SET, RESET, SHOW */ /* Complete with a variable name */ else if (TailMatches1("SET|RESET") && !TailMatches3("UPDATE", MatchAny, "SET")) - COMPLETE_WITH_QUERY(Query_for_list_of_set_vars); + COMPLETE_WITH_QUERY(Query_for_list_of_set_vars + " UNION SELECT 'SESSION'" + " UNION SELECT 'LOCAL'" + " UNION SELECT 'SEED'"); else if (Matches1("SHOW")) COMPLETE_WITH_QUERY(Query_for_list_of_show_vars); /* Complete "SET TRANSACTION" */ @@ -3663,7 +3673,10 @@ psql_completion(const char *text, int start, int end) */ else if (Matches1("VACUUM")) COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tm, - " UNION SELECT 'FULL'" + " UNION SELECT 'FULL'" + " UNION SELECT 'SORT ONLY'" + " UNION SELECT 'DELETE ONLY'" + " UNION SELECT 'REINDEX'" " UNION SELECT 'FREEZE'" " UNION SELECT 'ANALYZE'" " UNION SELECT 'VERBOSE'"); @@ -3683,8 +3696,12 @@ psql_completion(const char *text, int start, int end) else if (Matches2("VACUUM", "ANALYZE")) COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tm, " UNION SELECT 'VERBOSE'"); - else if (HeadMatches1("VACUUM")) - COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tm, NULL); + else if (Matches3("VACUUM", "FULL|REINDEX", MatchAny) || + Matches4("VACUUM", "SORT|DELETE", "ONLY", MatchAny)) + COMPLETE_WITH_CONST("TO"); + else if (Matches5("VACUUM", "FULL|REINDEX", MatchAny, "TO", MatchAny) || + Matches6("VACUUM", "SORT|DELETE", "ONLY", MatchAny, "TO", MatchAny)) + COMPLETE_WITH_CONST("PERCENT"); /* WITH [RECURSIVE] */ From 60b35ddb997927c90d0b993f0f552d3dd84e0e52 Mon Sep 17 00:00:00 2001 From: Robins Tharakan Date: Tue, 29 Aug 2017 15:03:34 +1000 Subject: [PATCH 051/149] Add UPDATE Support --- src/bin/psql/tab-complete.c | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/bin/psql/tab-complete.c b/src/bin/psql/tab-complete.c index 9ce4e32ee3054..0a91a6f4c90c4 100644 --- a/src/bin/psql/tab-complete.c +++ b/src/bin/psql/tab-complete.c @@ -3651,6 +3651,18 @@ psql_completion(const char *text, int start, int end) /* UPDATE
SET = */ else if (TailMatches4("UPDATE", MatchAny, "SET", MatchAny)) COMPLETE_WITH_CONST("="); + /* UPDATE
SET = FROM / WHERE */ + else if (TailMatches6("UPDATE", MatchAny, "SET", MatchAny, "=", MatchAny)) + COMPLETE_WITH_LIST2("FROM", "WHERE"); + /* UPDATE
SET = FROM */ + else if (TailMatches7("UPDATE", MatchAny, "SET", MatchAny, "=", MatchAny, "FROM")) + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tables, NULL); + /* UPDATE
SET = FROM WHERE */ + else if (TailMatches8("UPDATE", MatchAny, "SET", MatchAny, "=", MatchAny, "FROM", MatchAny)) + COMPLETE_WITH_CONST("WHERE"); + /* UPDATE
SET = FROM WHERE */ + else if (TailMatches9("UPDATE", MatchAny, "SET", MatchAny, "=", MatchAny, "FROM", MatchAny, "WHERE")) + COMPLETE_WITH_ATTR(prev2_wd, ""); /* USER MAPPING */ else if (Matches3("ALTER|CREATE|DROP", "USER", "MAPPING")) From 3b12d224d1eedfbabb5b7ef070e0d45da8f2baf9 Mon Sep 17 00:00:00 2001 From: Robins Tharakan Date: Tue, 29 Aug 2017 15:04:08 +1000 Subject: [PATCH 052/149] Update psql/Readme about recent UPDATE support --- src/bin/psql/README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/bin/psql/README.md b/src/bin/psql/README.md index d2f35c5ee32dd..edf397616fa50 100644 --- a/src/bin/psql/README.md +++ b/src/bin/psql/README.md @@ -147,9 +147,9 @@ START TRANSACTION | Native TRUNCATE | Partial TRUNCATE TABLE | Unsupported UNLOAD | Unsupported -UPDATE | Unsupported -UPDATE SET FROM | Unsupported -UPDATE SET WHERE | Unsupported +UPDATE | Complete +UPDATE SET FROM | **Yes** +UPDATE SET WHERE | **Yes** VACUUM | Complete VACUUM SORT | **Yes** VACUUM DELETE | **Yes** From feff0894b4832045169acfc9ac7888cbd4504c56 Mon Sep 17 00:00:00 2001 From: Robins Tharakan Date: Tue, 29 Aug 2017 15:30:34 +1000 Subject: [PATCH 053/149] Begun working on UNLOAD. Still WIP --- src/bin/psql/tab-complete.c | 45 +++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/src/bin/psql/tab-complete.c b/src/bin/psql/tab-complete.c index 0a91a6f4c90c4..dcda8a0e8e241 100644 --- a/src/bin/psql/tab-complete.c +++ b/src/bin/psql/tab-complete.c @@ -314,6 +314,18 @@ do { \ COMPLETE_WITH_LIST(list); \ } while (0) +#define COMPLETE_WITH_LIST11(s1, s2, s3, s4, s5, s6, s7, s8, s9, s10, s11) \ +do { \ + static const char *const list[] = { s1, s2, s3, s4, s5, s6, s7, s8, s9, s10, s11, NULL }; \ + COMPLETE_WITH_LIST(list); \ +} while (0) + +#define COMPLETE_WITH_LIST12(s1, s2, s3, s4, s5, s6, s7, s8, s9, s10, s11, s12) \ +do { \ + static const char *const list[] = { s1, s2, s3, s4, s5, s6, s7, s8, s9, s10, s11, s12, NULL }; \ + COMPLETE_WITH_LIST(list); \ +} while (0) + /* * Likewise for COMPLETE_WITH_LIST_CS. */ @@ -3638,6 +3650,39 @@ psql_completion(const char *text, int start, int end) else if (Matches1("UNLISTEN")) COMPLETE_WITH_QUERY("SELECT pg_catalog.quote_ident(channel) FROM pg_catalog.pg_listening_channels() AS channel WHERE substring(pg_catalog.quote_ident(channel),1,%d)='%s' UNION SELECT '*'"); +/* UNLISTEN */ + else if (Matches1("UNLOAD")) + COMPLETE_WITH_CONST("("); + else if (HeadMatches1("UNLOAD") && TailMatches1("TO")) + COMPLETE_WITH_CONST("TO"); + else if (HeadMatches1("UNLOAD") && TailMatches1("TO", MatchAny)) + COMPLETE_WITH_CONST("IAM_ROLE", "ACCESS_KEY_ID", "SECRET_ACCESS_KEY", "SESSION_TOKEN", "WITH", "CREDENTIALS"); + else if (HeadMatches1("UNLOAD") && TailMatches1("ACCESS_KEY_ID", MatchAny)) + COMPLETE_WITH_CONST("SECRET_ACCESS_KEY"); + else if (HeadMatches1("UNLOAD") && TailMatches1("SECRET_ACCESS_KEY", MatchAny)) + COMPLETE_WITH_CONST("SESSION_TOKEN"); + else if (HeadMatches1("UNLOAD") && TailMatches1("TO", MatchAny, "WITH")) + COMPLETE_WITH_CONST("CREDENTIALS"); + else if (HeadMatches1("UNLOAD") && TailMatches1("CREDENTIALS")) + COMPLETE_WITH_CONST("AS"); + else if (HeadMatches1("UNLOAD") && ( + (TailMatches2("IAM_ROLE", MatchAny)) || + (TailMatches2("SECRET_ACCESS_KEY", MatchAny)) || + (TailMatches2("SESSION_TOKEN", MatchAny)) || + (TailMatches2("CREDENTIALS|AS", MatchAny)) + ) + COMPLETE_WITH_LIST12("MANIFEST", "DELIMITER", "FIXEDWIDTH", "ENCRYPTED", "BZIP2", "GZIP", "ADDQUOTES", "NULL", "ESCAPE", "ALLOWOVERWRITE", "PARALLEL", "MAXFILESIZE"); + else if (HeadMatches1("UNLOAD") && TailMatches1("MANIFEST")) + COMPLETE_WITH_LIST11("DELIMITER", "FIXEDWIDTH", "ENCRYPTED", "BZIP2", "GZIP", "ADDQUOTES", "NULL", "ESCAPE", "ALLOWOVERWRITE", "PARALLEL", "MAXFILESIZE"); + else if (HeadMatches1("UNLOAD") && TailMatches1("ENCRYPTED")) + COMPLETE_WITH_LIST11("MANIFEST", "DELIMITER", "FIXEDWIDTH", "BZIP2", "GZIP", "ADDQUOTES", "NULL", "ESCAPE", "ALLOWOVERWRITE", "PARALLEL", "MAXFILESIZE"); + else if (HeadMatches1("UNLOAD") && TailMatches1("BZIP2")) + else if (HeadMatches1("UNLOAD") && TailMatches1("GZIP")) + else if (HeadMatches1("UNLOAD") && TailMatches1("ADDQUOTES")) + else if (HeadMatches1("UNLOAD") && TailMatches1("ESCAPE")) + else if (HeadMatches1("UNLOAD") && TailMatches1("ALLOWOVERWRITE")) + + /* UPDATE --- can be inside EXPLAIN, RULE, etc */ /* If prev. word is UPDATE suggest a list of tables */ else if (TailMatches1("UPDATE")) From 37bde8bf622bf4f7dc5052f5c26ad2f58e28ac62 Mon Sep 17 00:00:00 2001 From: Robins Tharakan Date: Tue, 29 Aug 2017 17:14:45 +1000 Subject: [PATCH 054/149] Add UNLOAD support --- src/bin/psql/tab-complete.c | 59 +++++++++++++++++++++++++++++++------ 1 file changed, 50 insertions(+), 9 deletions(-) diff --git a/src/bin/psql/tab-complete.c b/src/bin/psql/tab-complete.c index dcda8a0e8e241..cd039b85810a5 100644 --- a/src/bin/psql/tab-complete.c +++ b/src/bin/psql/tab-complete.c @@ -1532,7 +1532,7 @@ psql_completion(const char *text, int start, int end) "REASSIGN", "REFRESH MATERIALIZED VIEW", "REINDEX", "RELEASE", "RESET", "REVOKE", "ROLLBACK", "SAVEPOINT", "SECURITY LABEL", "SELECT", "SET", "SHOW", "START", - "TABLE", "TRUNCATE", "UNLISTEN", "UPDATE", "VACUUM", "VALUES", "WITH", + "TABLE", "TRUNCATE", "UNLISTEN", "UNLOAD", "UPDATE", "VACUUM", "VALUES", "WITH", NULL }; @@ -3653,15 +3653,15 @@ psql_completion(const char *text, int start, int end) /* UNLISTEN */ else if (Matches1("UNLOAD")) COMPLETE_WITH_CONST("("); - else if (HeadMatches1("UNLOAD") && TailMatches1("TO")) + else if (Matches2("UNLOAD", MatchAny)) COMPLETE_WITH_CONST("TO"); - else if (HeadMatches1("UNLOAD") && TailMatches1("TO", MatchAny)) - COMPLETE_WITH_CONST("IAM_ROLE", "ACCESS_KEY_ID", "SECRET_ACCESS_KEY", "SESSION_TOKEN", "WITH", "CREDENTIALS"); - else if (HeadMatches1("UNLOAD") && TailMatches1("ACCESS_KEY_ID", MatchAny)) + else if (HeadMatches1("UNLOAD") && TailMatches2("TO", MatchAnyExcept("TO"))) + COMPLETE_WITH_LIST6("IAM_ROLE", "ACCESS_KEY_ID", "SECRET_ACCESS_KEY", "SESSION_TOKEN", "WITH", "CREDENTIALS"); + else if (HeadMatches1("UNLOAD") && TailMatches2("ACCESS_KEY_ID", MatchAny)) COMPLETE_WITH_CONST("SECRET_ACCESS_KEY"); - else if (HeadMatches1("UNLOAD") && TailMatches1("SECRET_ACCESS_KEY", MatchAny)) + else if (HeadMatches1("UNLOAD") && TailMatches2("SECRET_ACCESS_KEY", MatchAny)) COMPLETE_WITH_CONST("SESSION_TOKEN"); - else if (HeadMatches1("UNLOAD") && TailMatches1("TO", MatchAny, "WITH")) + else if (HeadMatches1("UNLOAD") && TailMatches3("TO", MatchAny, "WITH")) COMPLETE_WITH_CONST("CREDENTIALS"); else if (HeadMatches1("UNLOAD") && TailMatches1("CREDENTIALS")) COMPLETE_WITH_CONST("AS"); @@ -3670,18 +3670,59 @@ psql_completion(const char *text, int start, int end) (TailMatches2("SECRET_ACCESS_KEY", MatchAny)) || (TailMatches2("SESSION_TOKEN", MatchAny)) || (TailMatches2("CREDENTIALS|AS", MatchAny)) - ) + )) COMPLETE_WITH_LIST12("MANIFEST", "DELIMITER", "FIXEDWIDTH", "ENCRYPTED", "BZIP2", "GZIP", "ADDQUOTES", "NULL", "ESCAPE", "ALLOWOVERWRITE", "PARALLEL", "MAXFILESIZE"); else if (HeadMatches1("UNLOAD") && TailMatches1("MANIFEST")) COMPLETE_WITH_LIST11("DELIMITER", "FIXEDWIDTH", "ENCRYPTED", "BZIP2", "GZIP", "ADDQUOTES", "NULL", "ESCAPE", "ALLOWOVERWRITE", "PARALLEL", "MAXFILESIZE"); else if (HeadMatches1("UNLOAD") && TailMatches1("ENCRYPTED")) COMPLETE_WITH_LIST11("MANIFEST", "DELIMITER", "FIXEDWIDTH", "BZIP2", "GZIP", "ADDQUOTES", "NULL", "ESCAPE", "ALLOWOVERWRITE", "PARALLEL", "MAXFILESIZE"); else if (HeadMatches1("UNLOAD") && TailMatches1("BZIP2")) + COMPLETE_WITH_LIST11("MANIFEST", "DELIMITER", "FIXEDWIDTH", "ENCRYPTED", "GZIP", "ADDQUOTES", "NULL", "ESCAPE", "ALLOWOVERWRITE", "PARALLEL", "MAXFILESIZE"); else if (HeadMatches1("UNLOAD") && TailMatches1("GZIP")) + COMPLETE_WITH_LIST11("MANIFEST", "DELIMITER", "FIXEDWIDTH", "ENCRYPTED", "BZIP2", "ADDQUOTES", "NULL", "ESCAPE", "ALLOWOVERWRITE", "PARALLEL", "MAXFILESIZE"); else if (HeadMatches1("UNLOAD") && TailMatches1("ADDQUOTES")) + COMPLETE_WITH_LIST11("MANIFEST", "DELIMITER", "FIXEDWIDTH", "ENCRYPTED", "BZIP2", "GZIP", "NULL", "ESCAPE", "ALLOWOVERWRITE", "PARALLEL", "MAXFILESIZE"); else if (HeadMatches1("UNLOAD") && TailMatches1("ESCAPE")) + COMPLETE_WITH_LIST11("MANIFEST", "DELIMITER", "FIXEDWIDTH", "ENCRYPTED", "BZIP2", "GZIP", "ADDQUOTES", "NULL", "ALLOWOVERWRITE", "PARALLEL", "MAXFILESIZE"); else if (HeadMatches1("UNLOAD") && TailMatches1("ALLOWOVERWRITE")) - + COMPLETE_WITH_LIST11("MANIFEST", "DELIMITER", "FIXEDWIDTH", "ENCRYPTED", "BZIP2", "GZIP", "ADDQUOTES", "NULL", "ESCAPE", "ALLOWOVERWRITE", "PARALLEL"); + else if (HeadMatches1("UNLOAD") && TailMatches1("DELIMITER|FIXEDWIDTH|NULL")) + COMPLETE_WITH_CONST("AS '"); + else if (HeadMatches1("UNLOAD") && TailMatches1("MAXFILESIZE")) + COMPLETE_WITH_CONST("AS"); + else if (HeadMatches1("UNLOAD") && TailMatches1("PARALLEL")) + COMPLETE_WITH_LIST4("ON", "OFF", "TRUE", "FALSE"); + else if (HeadMatches1("UNLOAD") && ( + TailMatches2("MAXFILESIZE", MatchAnyExcept("AS")) || + TailMatches3("MAXFILESIZE", "AS", MatchAny) + )) + COMPLETE_WITH_LIST2("MB", "GB"); + else if (HeadMatches1("UNLOAD") && ( + TailMatches2("DELIMITER", MatchAny) || + TailMatches3("DELIMITER", "AS", MatchAny) + )) + COMPLETE_WITH_LIST11("MANIFEST", "FIXEDWIDTH", "ENCRYPTED", "BZIP2", "GZIP", "ADDQUOTES", "NULL", "ESCAPE", "ALLOWOVERWRITE", "PARALLEL", "MAXFILESIZE"); + else if (HeadMatches1("UNLOAD") && ( + TailMatches2("FIXEDWIDTH", MatchAny) || + TailMatches3("FIXEDWIDTH", "AS", MatchAny) + )) + COMPLETE_WITH_LIST11("MANIFEST", "DELIMITER", "ENCRYPTED", "BZIP2", "GZIP", "ADDQUOTES", "NULL", "ESCAPE", "ALLOWOVERWRITE", "PARALLEL", "MAXFILESIZE"); + else if (HeadMatches1("UNLOAD") && ( + TailMatches2("NULL", MatchAny) || + TailMatches3("NULL", "AS", MatchAny) + )) + COMPLETE_WITH_LIST11("MANIFEST", "DELIMITER", "FIXEDWIDTH", "ENCRYPTED", "BZIP2", "GZIP", "ADDQUOTES", "ESCAPE", "ALLOWOVERWRITE", "PARALLEL", "MAXFILESIZE"); + else if (HeadMatches1("UNLOAD") && ( + TailMatches2("PARALLEL", MatchAny) || + TailMatches3("PARALLEL", "AS", MatchAny) + )) + COMPLETE_WITH_LIST11("MANIFEST", "DELIMITER", "FIXEDWIDTH", "ENCRYPTED", "BZIP2", "GZIP", "ADDQUOTES", "NULL", "ESCAPE", "ALLOWOVERWRITE", "MAXFILESIZE"); + else if (HeadMatches1("UNLOAD") && ( + TailMatches3("MAXFILESIZE", MatchAnyExcept("AS"), "MB|GB") || + TailMatches4("MAXFILESIZE", "AS", MatchAnyExcept("AS"), "MB|GB") + )) + COMPLETE_WITH_LIST12("MANIFEST", "DELIMITER", "FIXEDWIDTH", "ENCRYPTED", "BZIP2", "GZIP", "ADDQUOTES", "NULL", "ESCAPE", "ALLOWOVERWRITE", "PARALLEL", "MAXFILESIZE"); + /* UPDATE --- can be inside EXPLAIN, RULE, etc */ /* If prev. word is UPDATE suggest a list of tables */ From 1715f3c97383b2009ba358ebf0b3cf96b92ff6f6 Mon Sep 17 00:00:00 2001 From: Robins Tharakan Date: Tue, 29 Aug 2017 17:15:50 +1000 Subject: [PATCH 055/149] Update psql/README with recently added UNLOAD support --- src/bin/psql/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/bin/psql/README.md b/src/bin/psql/README.md index edf397616fa50..7365ec4ad04f3 100644 --- a/src/bin/psql/README.md +++ b/src/bin/psql/README.md @@ -146,7 +146,7 @@ SHOW ALL | Unsupported START TRANSACTION | Native TRUNCATE | Partial TRUNCATE TABLE | Unsupported -UNLOAD | Unsupported +UNLOAD | **Yes** UPDATE | Complete UPDATE SET FROM | **Yes** UPDATE SET WHERE | **Yes** From 0001175667dc9ecee1bebc75bcb3257ef370c681 Mon Sep 17 00:00:00 2001 From: Robins Tharakan Date: Tue, 29 Aug 2017 19:59:21 +1000 Subject: [PATCH 056/149] Adding support for DEALLOCATE / DELETE FROM USING WHERE / TRUNCATE TABLE --- src/bin/psql/README.md | 22 +++++++++------------- src/bin/psql/tab-complete.c | 10 ++++++++-- 2 files changed, 17 insertions(+), 15 deletions(-) diff --git a/src/bin/psql/README.md b/src/bin/psql/README.md index 7365ec4ad04f3..2fcbc91e6e22f 100644 --- a/src/bin/psql/README.md +++ b/src/bin/psql/README.md @@ -90,11 +90,11 @@ CREATE USER | Complete CREATE USER [NO]CREATEUSER | **Yes** CREATE VIEW | Partial CREATE OR REPLACE VIEW | **Yes** -DEALLOCATE | Partial -DEALLOCATE PREPARE | Unsupported +DEALLOCATE | Complete +DEALLOCATE PREPARE | **Yes** DECLARE | Native -DELETE | Partial -DELETE FROM USING WHERE | Unsupported +DELETE | Complete +DELETE FROM USING WHERE | **Yes** DROP DATABASE | Native DROP FUNCTION | Native DROP GROUP | Native @@ -136,16 +136,12 @@ SELECT ALL | **Yes** SELECT DISTINCT | **Yes** SELECT | Unsupported SELECT INTO | **WIP** -SET | Partial -SET ALL | Unsupported -SET SEED TO | Unsupported -SET SESSION AUTHORIZATION | Unsupported -SET SESSION CHARACTERISTICS | Native -SHOW | Partial -SHOW ALL | Unsupported +SET | Complete +SET SEED TO | **Yes** +SHOW | Native START TRANSACTION | Native -TRUNCATE | Partial -TRUNCATE TABLE | Unsupported +TRUNCATE | Complete +TRUNCATE TABLE | **Yes** UNLOAD | **Yes** UPDATE | Complete UPDATE SET FROM | **Yes** diff --git a/src/bin/psql/tab-complete.c b/src/bin/psql/tab-complete.c index cd039b85810a5..c88754c4a0226 100644 --- a/src/bin/psql/tab-complete.c +++ b/src/bin/psql/tab-complete.c @@ -2896,8 +2896,7 @@ psql_completion(const char *text, int start, int end) /* DEALLOCATE */ else if (Matches1("DEALLOCATE")) -// COMPLETE_WITH_QUERY2(Query_for_list_of_prepared_statements, "UNION SELECT 'PREPARE'"); - COMPLETE_WITH_QUERY(Query_for_list_of_prepared_statements); + COMPLETE_WITH_QUERY(Query_for_list_of_prepared_statements " UNION SELECT 'PREPARE'"); else if (Matches2("DEALLOCATE", "PREPARE")) COMPLETE_WITH_QUERY(Query_for_list_of_prepared_statements); @@ -2919,7 +2918,12 @@ psql_completion(const char *text, int start, int end) else if (TailMatches3("DELETE", "FROM", MatchAny)) COMPLETE_WITH_LIST2("USING", "WHERE"); else if (TailMatches4("DELETE", "FROM", MatchAny, "USING")) + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tables, NULL); + else if (TailMatches5("DELETE", "FROM", MatchAny, "USING", MatchAny)) COMPLETE_WITH_CONST("WHERE"); + else if ((TailMatches5("DELETE", MatchAny, "USING", MatchAny, "WHERE")) || + (TailMatches6("DELETE", "FROM", MatchAny, "USING", MatchAny, "WHERE"))) + COMPLETE_WITH_ATTR(prev2_wd, ""); /* XXX: implement tab completion for DELETE ... USING */ /* DISCARD */ @@ -3644,6 +3648,8 @@ psql_completion(const char *text, int start, int end) /* TRUNCATE */ else if (Matches1("TRUNCATE")) + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tables, " UNION SELECT 'TABLE'"); + else if (Matches2("TRUNCATE", "TABLE")) COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tables, NULL); /* UNLISTEN */ From 2cfde5a2a4925d99cff1dcc6ac4dbafbd610fa87 Mon Sep 17 00:00:00 2001 From: Robins Tharakan Date: Tue, 29 Aug 2017 20:14:45 +1000 Subject: [PATCH 057/149] Update psql/README. The angle brackets were invisble in HTML. Conveted to curved braces. --- src/bin/psql/README.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/bin/psql/README.md b/src/bin/psql/README.md index 2fcbc91e6e22f..c9a05649bbc9a 100644 --- a/src/bin/psql/README.md +++ b/src/bin/psql/README.md @@ -84,7 +84,7 @@ CREATE TABLE DISTKEY | **Yes** CREATE TABLE INTERLEAVED SORTKEY | **Yes** CREATE TABLE COMPOUND SORTKEY | **Yes** CREATE TABLE SORTKEY | **Yes** -CREATE TABLE | Unsupported +CREATE TABLE (similarly many variants) | Unsupported CREATE TABLE AS | Unsupported CREATE USER | Complete CREATE USER [NO]CREATEUSER | **Yes** @@ -120,7 +120,7 @@ GRANT ALL ON TABLE TO GROUP | Unsupported GRANT ALL ON TABLE ALL TABLES IN SCHEMA | Unsupported GRANT CREATE ALL | Unsupported GRANT CREATE ON | Unsupported -GRANT | Unsupported +GRANT (similarly many variants) | Unsupported INSERT | Partial INSERT INTO DEFAULT VALUES | Unsupported LOCK | Native @@ -128,13 +128,13 @@ PREPARE | Native RESET | Native REVOKE | Partial REVOKE GRANT | **Yes** -REVOKE | Unsupported +REVOKE (similarly many variants) | Unsupported ROLLBACK | Native SELECT | Partial SELECT TOP | **Yes** SELECT ALL | **Yes** SELECT DISTINCT | **Yes** -SELECT | Unsupported +SELECT (similarly many variants) | Unsupported SELECT INTO | **WIP** SET | Complete SET SEED TO | **Yes** From f1195a06ed44a9f94f6a703438164fe7f7061405 Mon Sep 17 00:00:00 2001 From: Robins Tharakan Date: Tue, 29 Aug 2017 20:23:07 +1000 Subject: [PATCH 058/149] INSERT INTO DEFAULT VALUES is Natively supported --- src/bin/psql/README.md | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/bin/psql/README.md b/src/bin/psql/README.md index c9a05649bbc9a..70b89b77ed85b 100644 --- a/src/bin/psql/README.md +++ b/src/bin/psql/README.md @@ -121,8 +121,7 @@ GRANT ALL ON TABLE ALL TABLES IN SCHEMA | Unsupported GRANT CREATE ALL | Unsupported GRANT CREATE ON | Unsupported GRANT (similarly many variants) | Unsupported -INSERT | Partial -INSERT INTO DEFAULT VALUES | Unsupported +INSERT | Native LOCK | Native PREPARE | Native RESET | Native From 5c6d02f18a60d7d421f037735b2ec769a4227e10 Mon Sep 17 00:00:00 2001 From: Robins Tharakan Date: Tue, 29 Aug 2017 21:29:08 +1000 Subject: [PATCH 059/149] Add support for CREATE EXTERNAL SCHEMA --- src/bin/psql/README.md | 2 +- src/bin/psql/tab-complete.c | 69 +++++++++++++++++++++++++++++-------- 2 files changed, 56 insertions(+), 15 deletions(-) diff --git a/src/bin/psql/README.md b/src/bin/psql/README.md index 70b89b77ed85b..0ff7738257e3c 100644 --- a/src/bin/psql/README.md +++ b/src/bin/psql/README.md @@ -65,7 +65,7 @@ CREATE DATABASE CONNECTION LIMIT UNLIMITED | **Yes** CREATE DATABASE WITH | **Yes** CREATE DATABASE WITH OWNER | **Yes** CREATE DATABASE OWNER | **Yes** -CREATE EXTERNAL SCHEMA | Unsupported +CREATE EXTERNAL SCHEMA | **Yes** CREATE EXTERNAL TABLE | Unsupported CREATE FUNCTION | Complete CREATE OR REPLACE FUNCTION | **Yes** diff --git a/src/bin/psql/tab-complete.c b/src/bin/psql/tab-complete.c index c88754c4a0226..098838b8a24bb 100644 --- a/src/bin/psql/tab-complete.c +++ b/src/bin/psql/tab-complete.c @@ -683,20 +683,20 @@ static const SchemaQuery Query_for_list_of_statistics = { " OR '\"' || nspname || '\"' ='%s') " #define Query_for_list_of_aws_regions \ -"SELECT 'ap-northeast-1' UNION ALL "\ -"SELECT 'ap-northeast-2' UNION ALL "\ -"SELECT 'ap-south-1' UNION ALL "\ -"SELECT 'ap-southeast-1' UNION ALL "\ -"SELECT 'ap-southeast-2' UNION ALL "\ -"SELECT 'ca-central-1' UNION ALL "\ -"SELECT 'eu-central-1' UNION ALL "\ -"SELECT 'eu-west-1' UNION ALL "\ -"SELECT 'eu-west-2' UNION ALL "\ -"SELECT 'sa-east-1' UNION ALL "\ -"SELECT 'us-east-1' UNION ALL "\ -"SELECT 'us-east-2' UNION ALL "\ -"SELECT 'us-west-1' UNION ALL "\ -"SELECT 'us-west-2' " +"SELECT '''ap-northeast-1''' UNION ALL "\ +"SELECT '''ap-northeast-2''' UNION ALL "\ +"SELECT '''ap-south-1''' UNION ALL "\ +"SELECT '''ap-southeast-1''' UNION ALL "\ +"SELECT '''ap-southeast-2''' UNION ALL "\ +"SELECT '''ca-central-1''' UNION ALL "\ +"SELECT '''eu-central-1''' UNION ALL "\ +"SELECT '''eu-west-1''' UNION ALL "\ +"SELECT '''eu-west-2''' UNION ALL "\ +"SELECT '''sa-east-1''' UNION ALL "\ +"SELECT '''us-east-1''' UNION ALL "\ +"SELECT '''us-east-2''' UNION ALL "\ +"SELECT '''us-west-1''' UNION ALL "\ +"SELECT '''us-west-2''' " #define Query_for_list_of_column_compressions \ " SELECT 'BYTEDICT' UNION ALL "\ @@ -764,6 +764,14 @@ static const SchemaQuery Query_for_list_of_statistics = { "SELECT pg_catalog.quote_ident(nspname) FROM pg_catalog.pg_namespace "\ " WHERE substring(pg_catalog.quote_ident(nspname),1,%d)='%s'" +#define Query_for_list_of_external_schemas \ +"SELECT pg_catalog.quote_ident(schemaname) FROM SVV_EXTERNAL_SCHEMAS "\ +" WHERE substring(pg_catalog.quote_ident(schemaname),1,%d)='%s'" + +#define Query_for_list_of_external_tables \ +"SELECT pg_catalog.quote_ident(tablename) FROM SVV_EXTERNAL_TABLES "\ +" WHERE substring(pg_catalog.quote_ident(tablename),1,%d)='%s'" + #define Query_for_list_of_alter_system_set_vars \ "SELECT name FROM "\ " (SELECT pg_catalog.lower(name) AS name FROM pg_catalog.pg_settings "\ @@ -1074,6 +1082,8 @@ static const pgsql_thing_t words_after_create[] = { {"DOMAIN", NULL, &Query_for_list_of_domains}, {"EVENT TRIGGER", NULL, NULL}, {"EXTENSION", Query_for_list_of_extensions}, + {"EXTERNAL SCHEMA", Query_for_list_of_external_schemas}, + {"EXTERNAL TABLE", Query_for_list_of_external_tables}, {"FOREIGN DATA WRAPPER", NULL, NULL}, {"FOREIGN TABLE", NULL, NULL}, {"FUNCTION", NULL, &Query_for_list_of_functions}, @@ -2465,6 +2475,37 @@ psql_completion(const char *text, int start, int end) COMPLETE_WITH_QUERY(Query_for_list_of_available_extension_versions); } + /* CREATE EXTERNAL SCHEMA */ + else if (Matches2("CREATE", "EXTERNAL")) + COMPLETE_WITH_LIST2("SCHEMA", "TABLE"); + else if (Matches3("CREATE", "EXTERNAL", "SCHEMA")) + COMPLETE_WITH_QUERY(Query_for_list_of_external_schemas + " UNION ALL SELECT 'IF NOT EXISTS'"); + else if ( + (Matches7("CREATE", "EXTERNAL", "SCHEMA", "IF", "NOT", "EXISTS", MatchAnyExcept("FROM"))) || + (Matches4("CREATE", "EXTERNAL", "SCHEMA", MatchAnyExcept("FROM"))) + ) + COMPLETE_WITH_CONST("FROM"); + else if (HeadMatches3("CREATE", "EXTERNAL", "SCHEMA") && TailMatches1("FROM")) + COMPLETE_WITH_LIST2("DATA CATALOG", "HIVE METASTORE"); + else if (HeadMatches3("CREATE", "EXTERNAL", "SCHEMA") && + (TailMatches2("DATA", "CATALOG") || TailMatches2("HIVE", "METASTORE")) + ) + COMPLETE_WITH_CONST("DATABASE"); + else if (HeadMatches3("CREATE", "EXTERNAL", "SCHEMA") && TailMatches2("DATABASE", MatchAny)) + COMPLETE_WITH_LIST4("REGION", "URI", "IAM_ROLE", "CREATE EXTERNAL DATABASE IF NOT EXISTS"); + else if (HeadMatches3("CREATE", "EXTERNAL", "SCHEMA") && TailMatches1("REGION")) + COMPLETE_WITH_QUERY(Query_for_list_of_aws_regions); + else if (HeadMatches3("CREATE", "EXTERNAL", "SCHEMA") && TailMatches2("REGION", MatchAny)) + COMPLETE_WITH_LIST3("URI", "IAM_ROLE", "CREATE EXTERNAL DATABASE IF NOT EXISTS"); + else if (HeadMatches3("CREATE", "EXTERNAL", "SCHEMA") && TailMatches2("URI", MatchAny)) + COMPLETE_WITH_LIST3("PORT", "IAM_ROLE", "CREATE EXTERNAL DATABASE IF NOT EXISTS"); + else if (HeadMatches3("CREATE", "EXTERNAL", "SCHEMA") && TailMatches2("PORT", MatchAny)) + COMPLETE_WITH_LIST2("IAM_ROLE", "CREATE EXTERNAL DATABASE IF NOT EXISTS"); + else if (HeadMatches3("CREATE", "EXTERNAL", "SCHEMA") && TailMatches2("IAM_ROLE", MatchAny)) + COMPLETE_WITH_CONST("CREATE EXTERNAL DATABASE IF NOT EXISTS"); + + /* CREATE FOREIGN */ else if (Matches2("CREATE", "FOREIGN")) COMPLETE_WITH_LIST2("DATA WRAPPER", "TABLE"); From 4896cda8cfeecb706abe09dcd65d9424f64a7b7f Mon Sep 17 00:00:00 2001 From: Robins Tharakan Date: Tue, 29 Aug 2017 21:54:33 +1000 Subject: [PATCH 060/149] Add support for CREATE EXTERNAL TABLE --- src/bin/psql/tab-complete.c | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/bin/psql/tab-complete.c b/src/bin/psql/tab-complete.c index 098838b8a24bb..8046fd31bcce5 100644 --- a/src/bin/psql/tab-complete.c +++ b/src/bin/psql/tab-complete.c @@ -2505,6 +2505,24 @@ psql_completion(const char *text, int start, int end) else if (HeadMatches3("CREATE", "EXTERNAL", "SCHEMA") && TailMatches2("IAM_ROLE", MatchAny)) COMPLETE_WITH_CONST("CREATE EXTERNAL DATABASE IF NOT EXISTS"); + /* CREATE EXTERNAL TABLE */ + else if (Matches3("CREATE", "EXTERNAL", "TABLE")) + COMPLETE_WITH_QUERY(Query_for_list_of_external_schemas); + else if ((HeadMatches3("CREATE", "EXTERNAL", "TABLE")) && TailMatches1("PARTITIONED")) + COMPLETE_WITH_CONST("BY ("); + else if ((HeadMatches3("CREATE", "EXTERNAL", "TABLE")) && TailMatches1("ROW")) + COMPLETE_WITH_LIST2("FORMAT", "DELIMITED"); + else if ((HeadMatches3("CREATE", "EXTERNAL", "TABLE")) && TailMatches3("ROW", "FORMAT", "DELIMITED")) + COMPLETE_WITH_LIST2("FIELDS TERMINATED BY", "LINES TERMINATED BY"); + else if ((HeadMatches3("CREATE", "EXTERNAL", "TABLE")) && TailMatches3("ROW", "FORMAT", "DELIMITED")) + COMPLETE_WITH_LIST2("FIELDS TERMINATED BY", "LINES TERMINATED BY"); + else if ((HeadMatches3("CREATE", "EXTERNAL", "TABLE")) && TailMatches2("STORED", "AS")) + COMPLETE_WITH_LIST5("PARQUET", "RCFILE", "SEQUENCEFILE", "TEXTFILE", "ORC"); + else if ((HeadMatches3("CREATE", "EXTERNAL", "TABLE")) && TailMatches1("PROPERTIES")) + COMPLETE_WITH_CONST("('numRows'='"); + else if (HeadMatches4("CREATE", "EXTERNAL", "TABLE", MatchAny)) + COMPLETE_WITH_LIST5("PARTITIONED BY (", "ROW FORMAT DELIMITED", "STORED AS", "LOCATION", "TABLE PROPERTIES ('numRows'='"); + /* CREATE FOREIGN */ else if (Matches2("CREATE", "FOREIGN")) From 896e3063b93ffe38b765806436716761eb9d6e84 Mon Sep 17 00:00:00 2001 From: Robins Tharakan Date: Wed, 30 Aug 2017 04:18:17 +1000 Subject: [PATCH 061/149] Update psql/README with recent CREATE EXTERNAL TABLE support --- src/bin/psql/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/bin/psql/README.md b/src/bin/psql/README.md index 0ff7738257e3c..e7abcc32c1604 100644 --- a/src/bin/psql/README.md +++ b/src/bin/psql/README.md @@ -66,7 +66,7 @@ CREATE DATABASE WITH | **Yes** CREATE DATABASE WITH OWNER | **Yes** CREATE DATABASE OWNER | **Yes** CREATE EXTERNAL SCHEMA | **Yes** -CREATE EXTERNAL TABLE | Unsupported +CREATE EXTERNAL TABLE | **Yes** CREATE FUNCTION | Complete CREATE OR REPLACE FUNCTION | **Yes** CREATE GROUP | Native From 3f9054c67318983273f14f0f294bc9ae04c201bb Mon Sep 17 00:00:00 2001 From: Robins Tharakan Date: Wed, 30 Aug 2017 04:52:42 +1000 Subject: [PATCH 062/149] Add support for CANCEL / CLOSE. Update psql/README --- src/bin/psql/README.md | 8 ++++---- src/bin/psql/tab-complete.c | 12 +++++++++++- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/src/bin/psql/README.md b/src/bin/psql/README.md index e7abcc32c1604..2e7b9b10477b8 100644 --- a/src/bin/psql/README.md +++ b/src/bin/psql/README.md @@ -38,7 +38,7 @@ ALTER GROUP | Native ALTER SCHEMA | Native ALTER TABLE | Partial ALTER TABLE ADD COLUMN | **Yes** -ALTER TABLE ADD ENCODING | **Yes** +ALTER TABLE ADD ENCODING (Column-Compressions)| **Yes** ALTER TABLE SET LOCATION 's3://bucket/folder' | Unsupported ALTER TABLE SET FILE FORMAT format | Unsupported ALTER TABLE SET TABLE PROPERTIES ( 'numRows'='row_count' ) | Unsupported @@ -54,12 +54,12 @@ ANALYSE PREDICATE COLUMNS | Unsupported ANALYZE ALL COLUMNS | Unsupported ANALYZE COMPRESSION | Unsupported BEGIN | Native -CANCEL | Unsupported -CLOSE | Unsupported +CANCEL | **Yes** +CLOSE | **Yes** COMMENT | Native COMMIT | Native COPY | Unsupported -COPY .. REGION | **Yes** +COPY .. REGION (aws-regions) | **Yes** CREATE DATABASE | Complete CREATE DATABASE CONNECTION LIMIT UNLIMITED | **Yes** CREATE DATABASE WITH | **Yes** diff --git a/src/bin/psql/tab-complete.c b/src/bin/psql/tab-complete.c index 8046fd31bcce5..a75192d225d37 100644 --- a/src/bin/psql/tab-complete.c +++ b/src/bin/psql/tab-complete.c @@ -779,6 +779,9 @@ static const SchemaQuery Query_for_list_of_statistics = { " UNION ALL SELECT 'all') ss "\ " WHERE substring(name,1,%d)='%s'" +#define Query_for_list_of_pids \ +"SELECT process || ' (user->' || user_name || '; database->' || db_name || ')' FROM stv_sessions ORDER BY db_name, user_name" + #define Query_for_list_of_set_vars \ "SELECT name FROM "\ " (SELECT pg_catalog.lower(name) AS name FROM pg_catalog.pg_settings "\ @@ -1534,7 +1537,7 @@ psql_completion(const char *text, int start, int end) /* Known command-starting keywords. */ static const char *const sql_commands[] = { - "ABORT", "ALTER", "ANALYZE", "BEGIN", "CHECKPOINT", "CLOSE", "CLUSTER", + "ABORT", "ALTER", "ANALYZE", "BEGIN", "CANCEL", "CHECKPOINT", "CLOSE", "CLUSTER", "COMMENT", "COMMIT", "COPY", "CREATE", "DEALLOCATE", "DECLARE", "DELETE FROM", "DISCARD", "DO", "DROP", "END", "EXECUTE", "EXPLAIN", "FETCH", "GRANT", "IMPORT", "INSERT", "LISTEN", "LOAD", "LOCK", @@ -2318,6 +2321,13 @@ psql_completion(const char *text, int start, int end) completion_info_charp = prev2_wd; COMPLETE_WITH_QUERY(Query_for_index_of_table); } +/* CANCEL */ + else if (Matches1("CANCEL")) + COMPLETE_WITH_QUERY(Query_for_list_of_pids); + +/* CLOSE */ + else if (Matches1("CLOSE")) + COMPLETE_WITH_QUERY(Query_for_list_of_cursors); /* COMMENT */ else if (Matches1("COMMENT")) From ead8a3d63284432ceac6cca93cac1158d031187b Mon Sep 17 00:00:00 2001 From: Robins Tharakan Date: Wed, 30 Aug 2017 12:00:00 +1000 Subject: [PATCH 063/149] Revert the recent unfinished work done in ALTER TABLE --- src/bin/psql/tab-complete.c | 62 +++++++++++++++++++++++++++++++++++-- 1 file changed, 59 insertions(+), 3 deletions(-) diff --git a/src/bin/psql/tab-complete.c b/src/bin/psql/tab-complete.c index a75192d225d37..6418717a9a472 100644 --- a/src/bin/psql/tab-complete.c +++ b/src/bin/psql/tab-complete.c @@ -48,6 +48,7 @@ #include "common.h" #include "settings.h" #include "stringutils.h" +#include "fe_utils/string_utils.h" #ifdef HAVE_RL_FILENAME_COMPLETION_FUNCTION #define filename_completion_function rl_filename_completion_function @@ -1632,11 +1633,66 @@ psql_completion(const char *text, int start, int end) matches = completion_matches(text, drop_command_generator); /* ALTER */ - - /* ALTER TABLE */ +/* + / * ALTER TABLE * / else if (Matches2("ALTER", "TABLE")) COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tables, - "UNION SELECT 'ALL IN TABLESPACE'"); + " UNION SELECT 'ALL IN TABLESPACE'" + " UNION SELECT 'ADD'" + " UNION SELECT 'SET'" + " UNION SELECT 'PARTITION'" + " UNION SELECT 'DROP'" + " UNION SELECT 'APPEND'" + " UNION SELECT 'OWNER'" + " UNION SELECT 'RENAME'"); + else if (Matches4("ALTER", "TABLE", MatchAny, "ADD")) + COMPLETE_WITH_QUERY(Query_for_list_of_tables_for_constraint + " UNION SELECT 'CONSTRAINT'" + " UNION SELECT 'UNIQUE'" + " UNION SELECT 'PRIMARY KEY'" + " UNION SELECT 'FOREIGN KEY'" + " UNION SELECT 'REFERENCES'" + " UNION SELECT 'PARTITION'" + ); + else if (Matches4("ALTER", "TABLE", MatchAny, "OWNER")) + COMPLETE_WITH_CONST("TO"); + else if (Matches5("ALTER", "TABLE", MatchAny, "OWNER", "TO")) + COMPLETE_WITH_QUERY(Query_for_list_of_users); + else if (Matches4("ALTER", "TABLE", MatchAny, "RENAME")) + COMPLETE_WITH_CONST("TO"); + else if (Matches5("ALTER", "TABLE", MatchAny, "RENAME", "TO")) + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tables, NULL); + else if (Matches5("ALTER", "TABLE", MatchAny, "RENAME", "COLUMN")) + COMPLETE_WITH_ATTR(prev3_wd, ""); + else if (Matches4("ALTER", "TABLE", MatchAny, "ADD")) + COMPLETE_WITH_CONST("COLUMN"); + else if (Matches5("ALTER", "TABLE", MatchAny, "ADD", "COLUMN")) + COMPLETE_WITH_ATTR(prev3_wd, ""); + else if (Matches6("ALTER", "TABLE", MatchAny, "ADD", "COLUMN", MatchAny)) + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_datatypes, NULL); + else if ( + (Matches6("ALTER", "TABLE", MatchAny, "ADD", MatchAny, MatchAny)) || + (Matches7("ALTER", "TABLE", MatchAny, "ADD", "COLUMN", MatchAny, MatchAny)) + ) + COMPLETE_WITH_LIST4("DEFAULT", "ENCODE", "NOT NULL", "NULL"); + else if (Matches2("ALTER", "TABLE") && Matches1("ENCODE")) + COMPLETE_WITH_QUERY(Query_for_list_of_column_compressions); + else if (Matches2("ALTER", "TABLE") && Matches1("DROP")) + COMPLETE_WITH_LIST2("CONSTRAINT", "COLUMN"); + else if (Matches4("ALTER", "TABLE", MatchAny, "DROP")) + COMPLETE_WITH_ATTR(prev2_wd, + " UNION SELECT 'CONSTRAINT'" + " UNION SELECT 'COLUMN'" + ); + else if (Matches5("ALTER", "TABLE", MatchAny, "DROP", "COLUMN")) + COMPLETE_WITH_ATTR(prev3_wd, ""); + else if ( + Matches5("ALTER", "TABLE", MatchAny, "DROP", MatchAnyExcept("CONSTRAINT|COLUMN")) || + Matches6("ALTER", "TABLE", MatchAny, "DROP", "CONSTRAINT|COLUMN", MatchAny) + ) + COMPLETE_WITH_LIST2("RESTRICT", "CASCADE"); + +*/ /* ALTER something */ else if (Matches1("ALTER")) From 6724b8d29f57a3d7d3aa311491b9dc03630fee43 Mon Sep 17 00:00:00 2001 From: Robins Tharakan Date: Wed, 30 Aug 2017 14:06:22 +1000 Subject: [PATCH 064/149] Employ IS_REDSHIFT that checks whether server is a different engine. Add a engine variable to store the Engine. Currently only supports 'postgres' and 'redshift' --- src/bin/psql/command.c | 6 ++++++ src/bin/psql/settings.h | 1 + src/bin/psql/tab-complete.c | 11 +++++++++-- 3 files changed, 16 insertions(+), 2 deletions(-) diff --git a/src/bin/psql/command.c b/src/bin/psql/command.c index 96f3a402a4097..284892737b67b 100644 --- a/src/bin/psql/command.c +++ b/src/bin/psql/command.c @@ -3342,6 +3342,12 @@ SyncVariables(void) pset.popt.topt.encoding = pset.encoding; pset.sversion = PQserverVersion(pset.db); + if (pset.sversion == 80002) { + pset.sengine = "redshift"; + } else { + pset.sengine = "postgres"; + } + SetVariable(pset.vars, "DBNAME", PQdb(pset.db)); SetVariable(pset.vars, "USER", PQuser(pset.db)); SetVariable(pset.vars, "HOST", PQhost(pset.db)); diff --git a/src/bin/psql/settings.h b/src/bin/psql/settings.h index b78f151acd887..5b5f6f4861cb1 100644 --- a/src/bin/psql/settings.h +++ b/src/bin/psql/settings.h @@ -104,6 +104,7 @@ typedef struct _psqlSettings * loop */ bool cur_cmd_interactive; int sversion; /* backend server version */ + const char *sengine; /* backend server engine variant (if not postgres) */ const char *progname; /* in case you renamed psql */ char *inputfile; /* file being currently processed, if any */ uint64 lineno; /* also for error reporting */ diff --git a/src/bin/psql/tab-complete.c b/src/bin/psql/tab-complete.c index 6418717a9a472..0049992e0e0d8 100644 --- a/src/bin/psql/tab-complete.c +++ b/src/bin/psql/tab-complete.c @@ -714,7 +714,9 @@ static const SchemaQuery Query_for_list_of_statistics = { " SELECT 'ZST' " #define Query_for_list_of_cursors \ -" SELECT trim(name) FROM stv_active_cursors" +((strncmp(pset.sengine, "redshift", 8) == 0) ? \ + " SELECT trim(name) FROM stv_active_cursors" : \ + " SELECT trim(name) FROM pg_cursors" ) #define Query_for_list_of_enum_values \ "SELECT pg_catalog.quote_literal(enumlabel) "\ @@ -1588,6 +1590,11 @@ psql_completion(const char *text, int start, int end) rl_completion_append_character = ' '; #endif +/* Flag to check if server is a Redshift Engine. + Any change here should probably be replicated elsewhere since + #define for various SQLs (in this script) employ there own string-compare */ +#define IS_REDSHIFT strncmp(pset.sengine, "redshift", 8) == 0 + /* Clear a few things. */ completion_charp = NULL; completion_charpp = NULL; @@ -2477,7 +2484,7 @@ psql_completion(const char *text, int start, int end) */ /* COPY FROM REGION */ - else if (HeadMatches2("COPY", MatchAny) && TailMatches1("REGION")) + else if (IS_REDSHIFT && HeadMatches2("COPY", MatchAny) && TailMatches1("REGION")) COMPLETE_WITH_QUERY(Query_for_list_of_aws_regions); From aad69a950d246a6fc97b7a3c5d7cee3ac0ff4378 Mon Sep 17 00:00:00 2001 From: Robins Tharakan Date: Wed, 30 Aug 2017 16:41:23 +1000 Subject: [PATCH 065/149] Employ IS_REDSHIFT at more places. Reduce diff (from mainline) as much as possible --- src/bin/psql/tab-complete.c | 187 ++++++++++++------------------------ 1 file changed, 64 insertions(+), 123 deletions(-) diff --git a/src/bin/psql/tab-complete.c b/src/bin/psql/tab-complete.c index 0049992e0e0d8..ac45454434458 100644 --- a/src/bin/psql/tab-complete.c +++ b/src/bin/psql/tab-complete.c @@ -48,7 +48,6 @@ #include "common.h" #include "settings.h" #include "stringutils.h" -#include "fe_utils/string_utils.h" #ifdef HAVE_RL_FILENAME_COMPLETION_FUNCTION #define filename_completion_function rl_filename_completion_function @@ -715,8 +714,8 @@ static const SchemaQuery Query_for_list_of_statistics = { #define Query_for_list_of_cursors \ ((strncmp(pset.sengine, "redshift", 8) == 0) ? \ - " SELECT trim(name) FROM stv_active_cursors" : \ - " SELECT trim(name) FROM pg_cursors" ) + " SELECT trim(name) FROM stv_active_cursors ORDER BY 1" : \ + " SELECT trim(name) FROM pg_cursors ORDER BY 1" ) #define Query_for_list_of_enum_values \ "SELECT pg_catalog.quote_literal(enumlabel) "\ @@ -783,7 +782,9 @@ static const SchemaQuery Query_for_list_of_statistics = { " WHERE substring(name,1,%d)='%s'" #define Query_for_list_of_pids \ -"SELECT process || ' (user->' || user_name || '; database->' || db_name || ')' FROM stv_sessions ORDER BY db_name, user_name" +((strncmp(pset.sengine, "redshift", 8) == 0) ? \ + "SELECT procpid || ' (user->' || usename || '; database->' || datname || ')' FROM pg_stat_activity ORDER BY datname, usename" : \ + "SELECT process || ' (user->' || usename || '; database->' || datname || ')' FROM pg_stat_activity ORDER BY datname, usename") #define Query_for_list_of_set_vars \ "SELECT name FROM "\ @@ -801,7 +802,7 @@ static const SchemaQuery Query_for_list_of_statistics = { "SELECT name FROM "\ " (SELECT pg_catalog.lower(name) AS name FROM pg_catalog.pg_settings "\ " UNION ALL SELECT 'session authorization' "\ -" UNION ALL SELECT 'ALL') ss "\ +" UNION ALL SELECT 'all') ss "\ " WHERE substring(name,1,%d)='%s'" #define Query_for_list_of_roles \ @@ -1640,66 +1641,11 @@ psql_completion(const char *text, int start, int end) matches = completion_matches(text, drop_command_generator); /* ALTER */ -/* - / * ALTER TABLE * / + + /* ALTER TABLE */ else if (Matches2("ALTER", "TABLE")) COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tables, - " UNION SELECT 'ALL IN TABLESPACE'" - " UNION SELECT 'ADD'" - " UNION SELECT 'SET'" - " UNION SELECT 'PARTITION'" - " UNION SELECT 'DROP'" - " UNION SELECT 'APPEND'" - " UNION SELECT 'OWNER'" - " UNION SELECT 'RENAME'"); - else if (Matches4("ALTER", "TABLE", MatchAny, "ADD")) - COMPLETE_WITH_QUERY(Query_for_list_of_tables_for_constraint - " UNION SELECT 'CONSTRAINT'" - " UNION SELECT 'UNIQUE'" - " UNION SELECT 'PRIMARY KEY'" - " UNION SELECT 'FOREIGN KEY'" - " UNION SELECT 'REFERENCES'" - " UNION SELECT 'PARTITION'" - ); - else if (Matches4("ALTER", "TABLE", MatchAny, "OWNER")) - COMPLETE_WITH_CONST("TO"); - else if (Matches5("ALTER", "TABLE", MatchAny, "OWNER", "TO")) - COMPLETE_WITH_QUERY(Query_for_list_of_users); - else if (Matches4("ALTER", "TABLE", MatchAny, "RENAME")) - COMPLETE_WITH_CONST("TO"); - else if (Matches5("ALTER", "TABLE", MatchAny, "RENAME", "TO")) - COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tables, NULL); - else if (Matches5("ALTER", "TABLE", MatchAny, "RENAME", "COLUMN")) - COMPLETE_WITH_ATTR(prev3_wd, ""); - else if (Matches4("ALTER", "TABLE", MatchAny, "ADD")) - COMPLETE_WITH_CONST("COLUMN"); - else if (Matches5("ALTER", "TABLE", MatchAny, "ADD", "COLUMN")) - COMPLETE_WITH_ATTR(prev3_wd, ""); - else if (Matches6("ALTER", "TABLE", MatchAny, "ADD", "COLUMN", MatchAny)) - COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_datatypes, NULL); - else if ( - (Matches6("ALTER", "TABLE", MatchAny, "ADD", MatchAny, MatchAny)) || - (Matches7("ALTER", "TABLE", MatchAny, "ADD", "COLUMN", MatchAny, MatchAny)) - ) - COMPLETE_WITH_LIST4("DEFAULT", "ENCODE", "NOT NULL", "NULL"); - else if (Matches2("ALTER", "TABLE") && Matches1("ENCODE")) - COMPLETE_WITH_QUERY(Query_for_list_of_column_compressions); - else if (Matches2("ALTER", "TABLE") && Matches1("DROP")) - COMPLETE_WITH_LIST2("CONSTRAINT", "COLUMN"); - else if (Matches4("ALTER", "TABLE", MatchAny, "DROP")) - COMPLETE_WITH_ATTR(prev2_wd, - " UNION SELECT 'CONSTRAINT'" - " UNION SELECT 'COLUMN'" - ); - else if (Matches5("ALTER", "TABLE", MatchAny, "DROP", "COLUMN")) - COMPLETE_WITH_ATTR(prev3_wd, ""); - else if ( - Matches5("ALTER", "TABLE", MatchAny, "DROP", MatchAnyExcept("CONSTRAINT|COLUMN")) || - Matches6("ALTER", "TABLE", MatchAny, "DROP", "CONSTRAINT|COLUMN", MatchAny) - ) - COMPLETE_WITH_LIST2("RESTRICT", "CASCADE"); - -*/ + "UNION SELECT 'ALL IN TABLESPACE'"); /* ALTER something */ else if (Matches1("ALTER")) @@ -2059,10 +2005,10 @@ psql_completion(const char *text, int start, int end) } /* ALTER TABLE xxx ADD */ - else if (Matches4("ALTER", "TABLE", MatchAny, "ADD")) + else if (IS_REDSHIFT && Matches4("ALTER", "TABLE", MatchAny, "ADD")) COMPLETE_WITH_CONST("COLUMN"); - else if (HeadMatches6("ALTER", "TABLE", MatchAny, "ADD", MatchAny, MatchAny)) + else if (IS_REDSHIFT && HeadMatches6("ALTER", "TABLE", MatchAny, "ADD", MatchAny, MatchAny)) { if (TailMatches2("COLUMN", MatchAny)) COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_datatypes, NULL); @@ -2084,7 +2030,7 @@ psql_completion(const char *text, int start, int end) } } - else if (HeadMatches5("ALTER", "TABLE", MatchAny, "ADD", MatchAny) && + else if (IS_REDSHIFT && HeadMatches5("ALTER", "TABLE", MatchAny, "ADD", MatchAny) && (!TailMatches1("COLUMN"))) COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_datatypes, NULL); @@ -2385,7 +2331,7 @@ psql_completion(const char *text, int start, int end) COMPLETE_WITH_QUERY(Query_for_index_of_table); } /* CANCEL */ - else if (Matches1("CANCEL")) + else if (IS_REDSHIFT && Matches1("CANCEL")) COMPLETE_WITH_QUERY(Query_for_list_of_pids); /* CLOSE */ @@ -2472,17 +2418,6 @@ psql_completion(const char *text, int start, int end) COMPLETE_WITH_LIST5("HEADER", "QUOTE", "ESCAPE", "FORCE QUOTE", "FORCE NOT NULL"); -/* - TailMatches2("ACCESS_KEY_ID", MatchAny) || - TailMatches4("IAM_ROLE", MatchAny, "SECRET_ACCESS_KEY", MatchAny) || - TailMatches6("IAM_ROLE", MatchAny, "SECRET_ACCESS_KEY", MatchAny, "SESSION_TOKEN", MatchAny) || - TailMatches6("IAM_ROLE", MatchAny, "SECRET_ACCESS_KEY", MatchAny, "token", MatchAny) || - TailMatches8("IAM_ROLE", MatchAny, "SECRET_ACCESS_KEY", MatchAny, "token", MatchAny, "MASTER_SYMMETRIC_KEY", MatchAny) || - TailMatches2("CREDENTIALS", MatchAny) || - TailMatches3("WITH", "CREDENTIALS", MatchAny) || - TailMatches4("WITH", "CREDENTIALS", "AS", MatchAny) || - -*/ /* COPY FROM REGION */ else if (IS_REDSHIFT && HeadMatches2("COPY", MatchAny) && TailMatches1("REGION")) COMPLETE_WITH_QUERY(Query_for_list_of_aws_regions); @@ -2548,54 +2483,60 @@ psql_completion(const char *text, int start, int end) COMPLETE_WITH_QUERY(Query_for_list_of_available_extension_versions); } - /* CREATE EXTERNAL SCHEMA */ - else if (Matches2("CREATE", "EXTERNAL")) + else if (IS_REDSHIFT && (Matches2("CREATE", "EXTERNAL"))) COMPLETE_WITH_LIST2("SCHEMA", "TABLE"); - else if (Matches3("CREATE", "EXTERNAL", "SCHEMA")) - COMPLETE_WITH_QUERY(Query_for_list_of_external_schemas - " UNION ALL SELECT 'IF NOT EXISTS'"); - else if ( - (Matches7("CREATE", "EXTERNAL", "SCHEMA", "IF", "NOT", "EXISTS", MatchAnyExcept("FROM"))) || - (Matches4("CREATE", "EXTERNAL", "SCHEMA", MatchAnyExcept("FROM"))) - ) - COMPLETE_WITH_CONST("FROM"); - else if (HeadMatches3("CREATE", "EXTERNAL", "SCHEMA") && TailMatches1("FROM")) - COMPLETE_WITH_LIST2("DATA CATALOG", "HIVE METASTORE"); - else if (HeadMatches3("CREATE", "EXTERNAL", "SCHEMA") && - (TailMatches2("DATA", "CATALOG") || TailMatches2("HIVE", "METASTORE")) - ) - COMPLETE_WITH_CONST("DATABASE"); - else if (HeadMatches3("CREATE", "EXTERNAL", "SCHEMA") && TailMatches2("DATABASE", MatchAny)) - COMPLETE_WITH_LIST4("REGION", "URI", "IAM_ROLE", "CREATE EXTERNAL DATABASE IF NOT EXISTS"); - else if (HeadMatches3("CREATE", "EXTERNAL", "SCHEMA") && TailMatches1("REGION")) - COMPLETE_WITH_QUERY(Query_for_list_of_aws_regions); - else if (HeadMatches3("CREATE", "EXTERNAL", "SCHEMA") && TailMatches2("REGION", MatchAny)) - COMPLETE_WITH_LIST3("URI", "IAM_ROLE", "CREATE EXTERNAL DATABASE IF NOT EXISTS"); - else if (HeadMatches3("CREATE", "EXTERNAL", "SCHEMA") && TailMatches2("URI", MatchAny)) - COMPLETE_WITH_LIST3("PORT", "IAM_ROLE", "CREATE EXTERNAL DATABASE IF NOT EXISTS"); - else if (HeadMatches3("CREATE", "EXTERNAL", "SCHEMA") && TailMatches2("PORT", MatchAny)) - COMPLETE_WITH_LIST2("IAM_ROLE", "CREATE EXTERNAL DATABASE IF NOT EXISTS"); - else if (HeadMatches3("CREATE", "EXTERNAL", "SCHEMA") && TailMatches2("IAM_ROLE", MatchAny)) - COMPLETE_WITH_CONST("CREATE EXTERNAL DATABASE IF NOT EXISTS"); - /* CREATE EXTERNAL TABLE */ - else if (Matches3("CREATE", "EXTERNAL", "TABLE")) - COMPLETE_WITH_QUERY(Query_for_list_of_external_schemas); - else if ((HeadMatches3("CREATE", "EXTERNAL", "TABLE")) && TailMatches1("PARTITIONED")) - COMPLETE_WITH_CONST("BY ("); - else if ((HeadMatches3("CREATE", "EXTERNAL", "TABLE")) && TailMatches1("ROW")) - COMPLETE_WITH_LIST2("FORMAT", "DELIMITED"); - else if ((HeadMatches3("CREATE", "EXTERNAL", "TABLE")) && TailMatches3("ROW", "FORMAT", "DELIMITED")) - COMPLETE_WITH_LIST2("FIELDS TERMINATED BY", "LINES TERMINATED BY"); - else if ((HeadMatches3("CREATE", "EXTERNAL", "TABLE")) && TailMatches3("ROW", "FORMAT", "DELIMITED")) - COMPLETE_WITH_LIST2("FIELDS TERMINATED BY", "LINES TERMINATED BY"); - else if ((HeadMatches3("CREATE", "EXTERNAL", "TABLE")) && TailMatches2("STORED", "AS")) - COMPLETE_WITH_LIST5("PARQUET", "RCFILE", "SEQUENCEFILE", "TEXTFILE", "ORC"); - else if ((HeadMatches3("CREATE", "EXTERNAL", "TABLE")) && TailMatches1("PROPERTIES")) - COMPLETE_WITH_CONST("('numRows'='"); - else if (HeadMatches4("CREATE", "EXTERNAL", "TABLE", MatchAny)) - COMPLETE_WITH_LIST5("PARTITIONED BY (", "ROW FORMAT DELIMITED", "STORED AS", "LOCATION", "TABLE PROPERTIES ('numRows'='"); + /* CREATE EXTERNAL SCHEMA */ + else if (IS_REDSHIFT && (Matches3("CREATE", "EXTERNAL", "SCHEMA"))) { + if (Matches2("CREATE", "EXTERNAL")) + COMPLETE_WITH_LIST2("SCHEMA", "TABLE"); + else if (Matches3("CREATE", "EXTERNAL", "SCHEMA")) + COMPLETE_WITH_QUERY(Query_for_list_of_external_schemas + " UNION ALL SELECT 'IF NOT EXISTS'"); + else if ( + (Matches7("CREATE", "EXTERNAL", "SCHEMA", "IF", "NOT", "EXISTS", MatchAnyExcept("FROM"))) || + (Matches4("CREATE", "EXTERNAL", "SCHEMA", MatchAnyExcept("FROM"))) + ) + COMPLETE_WITH_CONST("FROM"); + else if (HeadMatches3("CREATE", "EXTERNAL", "SCHEMA") && TailMatches1("FROM")) + COMPLETE_WITH_LIST2("DATA CATALOG", "HIVE METASTORE"); + else if (HeadMatches3("CREATE", "EXTERNAL", "SCHEMA") && + (TailMatches2("DATA", "CATALOG") || TailMatches2("HIVE", "METASTORE")) + ) + COMPLETE_WITH_CONST("DATABASE"); + else if (HeadMatches3("CREATE", "EXTERNAL", "SCHEMA") && TailMatches2("DATABASE", MatchAny)) + COMPLETE_WITH_LIST4("REGION", "URI", "IAM_ROLE", "CREATE EXTERNAL DATABASE IF NOT EXISTS"); + else if (HeadMatches3("CREATE", "EXTERNAL", "SCHEMA") && TailMatches1("REGION")) + COMPLETE_WITH_QUERY(Query_for_list_of_aws_regions); + else if (HeadMatches3("CREATE", "EXTERNAL", "SCHEMA") && TailMatches2("REGION", MatchAny)) + COMPLETE_WITH_LIST3("URI", "IAM_ROLE", "CREATE EXTERNAL DATABASE IF NOT EXISTS"); + else if (HeadMatches3("CREATE", "EXTERNAL", "SCHEMA") && TailMatches2("URI", MatchAny)) + COMPLETE_WITH_LIST3("PORT", "IAM_ROLE", "CREATE EXTERNAL DATABASE IF NOT EXISTS"); + else if (HeadMatches3("CREATE", "EXTERNAL", "SCHEMA") && TailMatches2("PORT", MatchAny)) + COMPLETE_WITH_LIST2("IAM_ROLE", "CREATE EXTERNAL DATABASE IF NOT EXISTS"); + else if (HeadMatches3("CREATE", "EXTERNAL", "SCHEMA") && TailMatches2("IAM_ROLE", MatchAny)) + COMPLETE_WITH_CONST("CREATE EXTERNAL DATABASE IF NOT EXISTS"); + } + /* CREATE EXTERNAL TABLE */ + else if (IS_REDSHIFT && (Matches3("CREATE", "EXTERNAL", "TABLE"))) { + if (Matches3("CREATE", "EXTERNAL", "TABLE")) + COMPLETE_WITH_QUERY(Query_for_list_of_external_schemas); + else if ((HeadMatches3("CREATE", "EXTERNAL", "TABLE")) && TailMatches1("PARTITIONED")) + COMPLETE_WITH_CONST("BY ("); + else if ((HeadMatches3("CREATE", "EXTERNAL", "TABLE")) && TailMatches1("ROW")) + COMPLETE_WITH_LIST2("FORMAT", "DELIMITED"); + else if ((HeadMatches3("CREATE", "EXTERNAL", "TABLE")) && TailMatches3("ROW", "FORMAT", "DELIMITED")) + COMPLETE_WITH_LIST2("FIELDS TERMINATED BY", "LINES TERMINATED BY"); + else if ((HeadMatches3("CREATE", "EXTERNAL", "TABLE")) && TailMatches3("ROW", "FORMAT", "DELIMITED")) + COMPLETE_WITH_LIST2("FIELDS TERMINATED BY", "LINES TERMINATED BY"); + else if ((HeadMatches3("CREATE", "EXTERNAL", "TABLE")) && TailMatches2("STORED", "AS")) + COMPLETE_WITH_LIST5("PARQUET", "RCFILE", "SEQUENCEFILE", "TEXTFILE", "ORC"); + else if ((HeadMatches3("CREATE", "EXTERNAL", "TABLE")) && TailMatches1("PROPERTIES")) + COMPLETE_WITH_CONST("('numRows'='"); + else if (HeadMatches4("CREATE", "EXTERNAL", "TABLE", MatchAny)) + COMPLETE_WITH_LIST5("PARTITIONED BY (", "ROW FORMAT DELIMITED", "STORED AS", "LOCATION", "TABLE PROPERTIES ('numRows'='"); + } /* CREATE FOREIGN */ else if (Matches2("CREATE", "FOREIGN")) From 1a95e7809ee2f47a5233dfd5571b7892c99419f9 Mon Sep 17 00:00:00 2001 From: Robins Tharakan Date: Thu, 31 Aug 2017 11:28:50 +1000 Subject: [PATCH 066/149] First commit in trying to add an ENCODING COLUMN to \d tablename. Still WIP --- src/bin/psql/describe.c | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/src/bin/psql/describe.c b/src/bin/psql/describe.c index f6049cc9e5c13..9fbfda44189b7 100644 --- a/src/bin/psql/describe.c +++ b/src/bin/psql/describe.c @@ -1500,6 +1500,20 @@ describeOneTableDetails(const char *schemaname, } else if (pset.sversion >= 80200) { + if (IS_REDSHIFT) { + printfPQExpBuffer(&buf, + "SELECT c.relchecks, c.relkind, c.relhasindex, c.relhasrules, " + "c.relhastriggers, false, false, c.relhasoids, " + "%s, c.reltablespace\n" + "FROM pg_catalog.pg_class c\n " + "LEFT JOIN pg_catalog.pg_class tc ON (c.reltoastrelid = tc.oid)\n" + "WHERE c.oid = '%s';", + (verbose ? + "pg_catalog.array_to_string(c.reloptions || " + "array(select 'toast.' || x from pg_catalog.unnest(tc.reloptions) x), ', ')\n" + : "''"), + oid); + } else { printfPQExpBuffer(&buf, "SELECT relchecks, relkind, relhasindex, relhasrules, " "reltriggers <> 0, false, false, relhasoids, " @@ -1508,6 +1522,7 @@ describeOneTableDetails(const char *schemaname, (verbose ? "pg_catalog.array_to_string(reloptions, E', ')" : "''"), oid); + } } else if (pset.sversion >= 80000) { @@ -1617,6 +1632,12 @@ describeOneTableDetails(const char *schemaname, " pg_catalog.pg_options_to_table(attfdwoptions)), ', ') || ')' END AS attfdwoptions"); else appendPQExpBufferStr(&buf, ",\n NULL AS attfdwoptions"); + if (IS_REDSHIFT) + appendPQExpBufferStr(&buf, "\n (SELECT encoding FROM pg_catalog.pg_table_def df\n" + " WHERE df.tablename = %s AND df.schemaname = %s) AS attcompression"); + else + appendPQExpBufferStr(&buf, "\n NULL AS attcompression"); + if (verbose) { appendPQExpBufferStr(&buf, ",\n a.attstorage"); @@ -1716,6 +1737,9 @@ describeOneTableDetails(const char *schemaname, headers[1] = gettext_noop("Type"); cols = 2; + if (IS_REDSHIFT && (tableinfo.relkind == RELKIND_RELATION)) + headers[cols++] = gettext_noop("ENCODING"); + if (tableinfo.relkind == RELKIND_RELATION || tableinfo.relkind == RELKIND_VIEW || tableinfo.relkind == RELKIND_MATVIEW || @@ -1800,6 +1824,10 @@ describeOneTableDetails(const char *schemaname, printTableAddCell(&cont, strcmp(PQgetvalue(res, i, 3), "t") == 0 ? "not null" : "", false, false); + /* Column Compression Options (For Engines that support Column Compressions) */ + if (IS_REDSHIFT && tableinfo.relkind == RELKIND_RELATION) + printTableAddCell(&cont, PQgetvalue(res, i, 9), false, false); + identity = PQgetvalue(res, i, 6); if (!identity[0]) @@ -1828,7 +1856,7 @@ describeOneTableDetails(const char *schemaname, /* Storage and Description */ if (verbose) { - int firstvcol = 9; + int firstvcol = 10; char *storage = PQgetvalue(res, i, firstvcol); /* these strings are literal in our syntax, so not translated. */ From f3f2ff442370d78b7b2cfaebefb489d2aa8c57c6 Mon Sep 17 00:00:00 2001 From: Robins Tharakan Date: Thu, 31 Aug 2017 12:33:19 +1000 Subject: [PATCH 067/149] Support for showing ENCODING (Column Compression) now working for \d and \d+ for Local Tables --- src/bin/psql/describe.c | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/src/bin/psql/describe.c b/src/bin/psql/describe.c index 9fbfda44189b7..058f49a4f2b2b 100644 --- a/src/bin/psql/describe.c +++ b/src/bin/psql/describe.c @@ -26,6 +26,8 @@ #include "settings.h" #include "variables.h" +/* Flag to check if server is a Redshift Engine */ +#define IS_REDSHIFT strncmp(pset.sengine, "redshift", 8) == 0 static bool describeOneTableDetails(const char *schemaname, const char *relationname, @@ -1633,10 +1635,10 @@ describeOneTableDetails(const char *schemaname, else appendPQExpBufferStr(&buf, ",\n NULL AS attfdwoptions"); if (IS_REDSHIFT) - appendPQExpBufferStr(&buf, "\n (SELECT encoding FROM pg_catalog.pg_table_def df\n" - " WHERE df.tablename = %s AND df.schemaname = %s) AS attcompression"); + appendPQExpBufferStr(&buf, ",\n (SELECT format_encoding(attencodingtype) FROM pg_catalog.pg_attribute att \n" + " WHERE att.attrelid = a.attrelid AND att.attnum = a.attnum) AS attcompression"); else - appendPQExpBufferStr(&buf, "\n NULL AS attcompression"); + appendPQExpBufferStr(&buf, ",\n NULL AS attcompression"); if (verbose) { @@ -1738,7 +1740,7 @@ describeOneTableDetails(const char *schemaname, cols = 2; if (IS_REDSHIFT && (tableinfo.relkind == RELKIND_RELATION)) - headers[cols++] = gettext_noop("ENCODING"); + headers[cols++] = gettext_noop("Encoding"); if (tableinfo.relkind == RELKIND_RELATION || tableinfo.relkind == RELKIND_VIEW || @@ -1820,14 +1822,14 @@ describeOneTableDetails(const char *schemaname, char *identity; char *default_str = ""; + /* Column-Compression Type (For engines that support Column-Compression) */ + if (IS_REDSHIFT && tableinfo.relkind == RELKIND_RELATION) + printTableAddCell(&cont, PQgetvalue(res, i, 9), false, false); + printTableAddCell(&cont, PQgetvalue(res, i, 5), false, false); printTableAddCell(&cont, strcmp(PQgetvalue(res, i, 3), "t") == 0 ? "not null" : "", false, false); - /* Column Compression Options (For Engines that support Column Compressions) */ - if (IS_REDSHIFT && tableinfo.relkind == RELKIND_RELATION) - printTableAddCell(&cont, PQgetvalue(res, i, 9), false, false); - identity = PQgetvalue(res, i, 6); if (!identity[0]) From b7b4b17d5b9bdc4a04955bad59cfb377db81b711 Mon Sep 17 00:00:00 2001 From: Robins Tharakan Date: Thu, 31 Aug 2017 14:23:45 +1000 Subject: [PATCH 068/149] Add more Redshift specific columns (DISTKEY / SORTKEY / PRELOAD / ENCRYPTION) to \d and \d+. Explicitly, this shows up only when connected to a Redshift server --- src/bin/psql/describe.c | 54 +++++++++++++++++++++++------------------ 1 file changed, 31 insertions(+), 23 deletions(-) diff --git a/src/bin/psql/describe.c b/src/bin/psql/describe.c index 058f49a4f2b2b..a771ed40d56e0 100644 --- a/src/bin/psql/describe.c +++ b/src/bin/psql/describe.c @@ -1381,7 +1381,7 @@ describeOneTableDetails(const char *schemaname, bool printTableInitialized = false; int i; char *view_def = NULL; - char *headers[11]; + char *headers[16]; char **seq_values = NULL; char **ptr; PQExpBufferData title; @@ -1502,20 +1502,6 @@ describeOneTableDetails(const char *schemaname, } else if (pset.sversion >= 80200) { - if (IS_REDSHIFT) { - printfPQExpBuffer(&buf, - "SELECT c.relchecks, c.relkind, c.relhasindex, c.relhasrules, " - "c.relhastriggers, false, false, c.relhasoids, " - "%s, c.reltablespace\n" - "FROM pg_catalog.pg_class c\n " - "LEFT JOIN pg_catalog.pg_class tc ON (c.reltoastrelid = tc.oid)\n" - "WHERE c.oid = '%s';", - (verbose ? - "pg_catalog.array_to_string(c.reloptions || " - "array(select 'toast.' || x from pg_catalog.unnest(tc.reloptions) x), ', ')\n" - : "''"), - oid); - } else { printfPQExpBuffer(&buf, "SELECT relchecks, relkind, relhasindex, relhasrules, " "reltriggers <> 0, false, false, relhasoids, " @@ -1524,7 +1510,6 @@ describeOneTableDetails(const char *schemaname, (verbose ? "pg_catalog.array_to_string(reloptions, E', ')" : "''"), oid); - } } else if (pset.sversion >= 80000) { @@ -1634,11 +1619,24 @@ describeOneTableDetails(const char *schemaname, " pg_catalog.pg_options_to_table(attfdwoptions)), ', ') || ')' END AS attfdwoptions"); else appendPQExpBufferStr(&buf, ",\n NULL AS attfdwoptions"); - if (IS_REDSHIFT) + if (IS_REDSHIFT) { appendPQExpBufferStr(&buf, ",\n (SELECT format_encoding(attencodingtype) FROM pg_catalog.pg_attribute att \n" - " WHERE att.attrelid = a.attrelid AND att.attnum = a.attnum) AS attcompression"); - else + " WHERE att.attrelid = a.attrelid AND att.attnum = a.attnum) AS attcompression"); + appendPQExpBufferStr(&buf, ",\n (SELECT attisdistkey FROM pg_catalog.pg_attribute att \n" + " WHERE att.attrelid = a.attrelid AND att.attnum = a.attnum) AS attisdistkey"); + appendPQExpBufferStr(&buf, ",\n (SELECT ABS(attsortkeyord) FROM pg_catalog.pg_attribute att \n" + " WHERE att.attrelid = a.attrelid AND att.attnum = a.attnum) AS attsortkeyord"); + appendPQExpBufferStr(&buf, ",\n (SELECT attispreloaded FROM pg_catalog.pg_attribute att \n" + " WHERE att.attrelid = a.attrelid AND att.attnum = a.attnum) AS attispreloaded"); + appendPQExpBufferStr(&buf, ",\n (SELECT CASE WHEN attencrypttype = 0 THEN 'none' ELSE attencrypttype::TEXT END FROM pg_catalog.pg_attribute att \n" + " WHERE att.attrelid = a.attrelid AND att.attnum = a.attnum) AS attencrypttype"); + } else { appendPQExpBufferStr(&buf, ",\n NULL AS attcompression"); + appendPQExpBufferStr(&buf, ",\n NULL AS attisdistkey"); + appendPQExpBufferStr(&buf, ",\n NULL AS attsortkeyord"); + appendPQExpBufferStr(&buf, ",\n NULL AS attispreloaded"); + appendPQExpBufferStr(&buf, ",\n NULL AS attencrypttype"); + } if (verbose) { @@ -1739,8 +1737,13 @@ describeOneTableDetails(const char *schemaname, headers[1] = gettext_noop("Type"); cols = 2; - if (IS_REDSHIFT && (tableinfo.relkind == RELKIND_RELATION)) + if (IS_REDSHIFT && tableinfo.relkind == RELKIND_RELATION) { headers[cols++] = gettext_noop("Encoding"); + headers[cols++] = gettext_noop("DistKey"); + headers[cols++] = gettext_noop("SortKey"); + headers[cols++] = gettext_noop("Preload"); + headers[cols++] = gettext_noop("Encryption"); + } if (tableinfo.relkind == RELKIND_RELATION || tableinfo.relkind == RELKIND_VIEW || @@ -1823,8 +1826,13 @@ describeOneTableDetails(const char *schemaname, char *default_str = ""; /* Column-Compression Type (For engines that support Column-Compression) */ - if (IS_REDSHIFT && tableinfo.relkind == RELKIND_RELATION) - printTableAddCell(&cont, PQgetvalue(res, i, 9), false, false); + if (IS_REDSHIFT && tableinfo.relkind == RELKIND_RELATION) { + printTableAddCell(&cont, PQgetvalue(res, i, 9), false, false); + printTableAddCell(&cont, PQgetvalue(res, i, 10), false, false); + printTableAddCell(&cont, PQgetvalue(res, i, 11), false, false); + printTableAddCell(&cont, PQgetvalue(res, i, 12), false, false); + printTableAddCell(&cont, PQgetvalue(res, i, 13), false, false); + } printTableAddCell(&cont, PQgetvalue(res, i, 5), false, false); @@ -1858,7 +1866,7 @@ describeOneTableDetails(const char *schemaname, /* Storage and Description */ if (verbose) { - int firstvcol = 10; + int firstvcol = 14; char *storage = PQgetvalue(res, i, firstvcol); /* these strings are literal in our syntax, so not translated. */ From b5b1fbf42556815669bd9517fa45843f9fb748c1 Mon Sep 17 00:00:00 2001 From: Robins Tharakan Date: Thu, 31 Aug 2017 17:13:11 +1000 Subject: [PATCH 069/149] Minor patch for CREATE TABLE AS. Update psql/README --- src/bin/psql/README.md | 11 +++-------- src/bin/psql/tab-complete.c | 2 +- 2 files changed, 4 insertions(+), 9 deletions(-) diff --git a/src/bin/psql/README.md b/src/bin/psql/README.md index 2e7b9b10477b8..77f9fecf8a243 100644 --- a/src/bin/psql/README.md +++ b/src/bin/psql/README.md @@ -58,8 +58,9 @@ CANCEL | **Yes** CLOSE | **Yes** COMMENT | Native COMMIT | Native -COPY | Unsupported +COPY | Partial COPY .. REGION (aws-regions) | **Yes** +COPY (Many Variants) | Unsupported CREATE DATABASE | Complete CREATE DATABASE CONNECTION LIMIT UNLIMITED | **Yes** CREATE DATABASE WITH | **Yes** @@ -85,7 +86,7 @@ CREATE TABLE INTERLEAVED SORTKEY | **Yes** CREATE TABLE COMPOUND SORTKEY | **Yes** CREATE TABLE SORTKEY | **Yes** CREATE TABLE (similarly many variants) | Unsupported -CREATE TABLE AS | Unsupported +CREATE TABLE AS | **Yes** CREATE USER | Complete CREATE USER [NO]CREATEUSER | **Yes** CREATE VIEW | Partial @@ -114,12 +115,6 @@ FETCH | Complete FETCH NEXT | **Yes** FETCH ALL | **Yes** GRANT | Partial -GRANT ALL PRIVILEGES | Unsupported -GRANT ALL ON TABLE ALL TABLES IN SCHEMA | Unsupported -GRANT ALL ON TABLE TO GROUP | Unsupported -GRANT ALL ON TABLE ALL TABLES IN SCHEMA | Unsupported -GRANT CREATE ALL | Unsupported -GRANT CREATE ON | Unsupported GRANT (similarly many variants) | Unsupported INSERT | Native LOCK | Native diff --git a/src/bin/psql/tab-complete.c b/src/bin/psql/tab-complete.c index ac45454434458..428a2ca20889a 100644 --- a/src/bin/psql/tab-complete.c +++ b/src/bin/psql/tab-complete.c @@ -2777,7 +2777,7 @@ psql_completion(const char *text, int start, int end) else if (Matches2("CREATE", "TABLE") || Matches3("CREATE", "TEMP|TEMPORARY", "TABLE") || Matches4("CREATE", "LOCAL", "TEMP|TEMPORARY", "TABLE")) - COMPLETE_WITH_CONST("IF NOT EXISTS"); + COMPLETE_WITH_LIST2("IF NOT EXISTS", "AS"); else if (Matches3("CREATE", "TABLE", MatchAny)) COMPLETE_WITH_CONST("("); From 6fbf96977db00b892e16e793e59225c5af1abe7d Mon Sep 17 00:00:00 2001 From: Robins Tharakan Date: Thu, 31 Aug 2017 17:18:11 +1000 Subject: [PATCH 070/149] Add support ANALYSE options per Redshift --- src/bin/psql/README.md | 10 +++++----- src/bin/psql/tab-complete.c | 9 +++++++-- 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/src/bin/psql/README.md b/src/bin/psql/README.md index 77f9fecf8a243..09e042af73ff2 100644 --- a/src/bin/psql/README.md +++ b/src/bin/psql/README.md @@ -48,11 +48,11 @@ ALTER TABLE DROP PARTITION ( partition_column=partition_value [, ...] ) | Unsupp ALTER TABLE APPEND | Unsupported ALTER USER | Complete ALTER USER [NO]CREATEUSER | **Yes** -ANALYZE | Partial -ANALYZE VERBOSE | Unsupported -ANALYSE PREDICATE COLUMNS | Unsupported -ANALYZE ALL COLUMNS | Unsupported -ANALYZE COMPRESSION | Unsupported +ANALYZE | Complete +ANALYZE VERBOSE | **Yes** +ANALYSE PREDICATE COLUMNS | **Yes** +ANALYZE ALL COLUMNS | **Yes** +ANALYZE COMPRESSION | **Yes** BEGIN | Native CANCEL | **Yes** CLOSE | **Yes** diff --git a/src/bin/psql/tab-complete.c b/src/bin/psql/tab-complete.c index 428a2ca20889a..3f0bbae3531d3 100644 --- a/src/bin/psql/tab-complete.c +++ b/src/bin/psql/tab-complete.c @@ -3891,8 +3891,13 @@ psql_completion(const char *text, int start, int end) /* ANALYZE */ /* Complete with list of tables */ - else if (Matches1("ANALYZE")) - COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tmf, NULL); + else if (Matches1("ANALYZE|ANALYSE")) + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tmf, + " UNION SELECT 'VERBOSE'" + " UNION SELECT 'PREDICATE COLUMNS'" + " UNION SELECT 'ALL COLUMNS'" + " UNION SELECT 'COMPRESSION'" + ); /* WHERE */ /* Simple case of the word before the where being the table name */ From 8368a79af31d6689e16d656b3182d837cd077169 Mon Sep 17 00:00:00 2001 From: Robins Tharakan Date: Thu, 31 Aug 2017 17:32:12 +1000 Subject: [PATCH 071/149] Add the last set of changes to mostly support Redshift related SQL Commands. Going forward changes in this area are going to be to support Redshift SQL commands in deep compliance --- src/bin/psql/README.md | 16 ++++++++-------- src/bin/psql/tab-complete.c | 8 ++++---- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/src/bin/psql/README.md b/src/bin/psql/README.md index 09e042af73ff2..34a99aed6fb46 100644 --- a/src/bin/psql/README.md +++ b/src/bin/psql/README.md @@ -38,14 +38,14 @@ ALTER GROUP | Native ALTER SCHEMA | Native ALTER TABLE | Partial ALTER TABLE ADD COLUMN | **Yes** -ALTER TABLE ADD ENCODING (Column-Compressions)| **Yes** -ALTER TABLE SET LOCATION 's3://bucket/folder' | Unsupported -ALTER TABLE SET FILE FORMAT format | Unsupported -ALTER TABLE SET TABLE PROPERTIES ( 'numRows'='row_count' ) | Unsupported -ALTER TABLE PARTITION ( partition_column=partition_value [, ...] ) SET LOCATION 's3://bucket/folder' | Unsupported -ALTER TABLE ADD PARTITION ( partition_column=partition_value [, ...] ) LOCATION 's3://bucket/folder' | Unsupported -ALTER TABLE DROP PARTITION ( partition_column=partition_value [, ...] ) | Unsupported -ALTER TABLE APPEND | Unsupported +ALTER TABLE SET LOCATION | **Yes** +ALTER TABLE SET FILE FORMAT | **Yes** +ALTER TABLE SET TABLE PROPERTIES | **Yes** +ALTER TABLE PARTITION | Unsupported +ALTER TABLE ADD PARTITION | **Yes** +ALTER TABLE DROP PARTITION | **Yes** +ALTER TABLE (Similarly Many Variants) | Unsupported +ALTER TABLE APPEND | **Yes** ALTER USER | Complete ALTER USER [NO]CREATEUSER | **Yes** ANALYZE | Complete diff --git a/src/bin/psql/tab-complete.c b/src/bin/psql/tab-complete.c index 3f0bbae3531d3..2b0c8a66cc84b 100644 --- a/src/bin/psql/tab-complete.c +++ b/src/bin/psql/tab-complete.c @@ -1996,7 +1996,7 @@ psql_completion(const char *text, int start, int end) else if (Matches3("ALTER", "TABLE", MatchAny)) { static const char *const list_ALTER2[] = - {"ADD", "ALTER", "CLUSTER ON", "DISABLE", "DROP", "ENABLE", "INHERIT", + {"ADD", "ALTER", "APPEND FROM", "CLUSTER ON", "DISABLE", "DROP", "ENABLE", "INHERIT", "NO INHERIT", "RENAME", "RESET", "OWNER TO", "SET", "VALIDATE CONSTRAINT", "REPLICA IDENTITY", "ATTACH PARTITION", "DETACH PARTITION", NULL}; @@ -2006,7 +2006,7 @@ psql_completion(const char *text, int start, int end) /* ALTER TABLE xxx ADD */ else if (IS_REDSHIFT && Matches4("ALTER", "TABLE", MatchAny, "ADD")) - COMPLETE_WITH_CONST("COLUMN"); + COMPLETE_WITH_LIST2("COLUMN", "PARTITION"); else if (IS_REDSHIFT && HeadMatches6("ALTER", "TABLE", MatchAny, "ADD", MatchAny, MatchAny)) { @@ -2100,7 +2100,7 @@ psql_completion(const char *text, int start, int end) /* If we have ALTER TABLE DROP, provide COLUMN or CONSTRAINT */ else if (Matches4("ALTER", "TABLE", MatchAny, "DROP")) - COMPLETE_WITH_LIST2("COLUMN", "CONSTRAINT"); + COMPLETE_WITH_LIST3("COLUMN", "CONSTRAINT", "PARTITION"); /* If we have ALTER TABLE DROP COLUMN, provide list of columns */ else if (Matches5("ALTER", "TABLE", MatchAny, "DROP", "COLUMN")) COMPLETE_WITH_ATTR(prev3_wd, ""); @@ -2143,7 +2143,7 @@ psql_completion(const char *text, int start, int end) } /* If we have ALTER TABLE SET, provide list of attributes and '(' */ else if (Matches4("ALTER", "TABLE", MatchAny, "SET")) - COMPLETE_WITH_LIST7("(", "LOGGED", "SCHEMA", "TABLESPACE", "UNLOGGED", + COMPLETE_WITH_LIST10("(", "LOGGED", "LOCATION", "FILE FORMAT", "TABLE PROPERTIES", "SCHEMA", "TABLESPACE", "UNLOGGED", "WITH", "WITHOUT"); /* From 9fe75e3816391dae945eb0046f6c0212944709ed Mon Sep 17 00:00:00 2001 From: Robins Tharakan Date: Thu, 31 Aug 2017 17:32:59 +1000 Subject: [PATCH 072/149] Minor update psql/README --- src/bin/psql/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/bin/psql/README.md b/src/bin/psql/README.md index 34a99aed6fb46..b729152179068 100644 --- a/src/bin/psql/README.md +++ b/src/bin/psql/README.md @@ -41,7 +41,7 @@ ALTER TABLE ADD COLUMN | **Yes** ALTER TABLE SET LOCATION | **Yes** ALTER TABLE SET FILE FORMAT | **Yes** ALTER TABLE SET TABLE PROPERTIES | **Yes** -ALTER TABLE PARTITION | Unsupported +ALTER TABLE PARTITION | **Yes** ALTER TABLE ADD PARTITION | **Yes** ALTER TABLE DROP PARTITION | **Yes** ALTER TABLE (Similarly Many Variants) | Unsupported From 07dcd8064801d30c2d2730da9c6cba2c84f65e95 Mon Sep 17 00:00:00 2001 From: Robins Date: Thu, 31 Aug 2017 17:39:33 +1000 Subject: [PATCH 073/149] Remove 'Last merged with Master' line. It's going to be outdated, by design, if managed humanly. Remove 'Last merged with Master' line. It's going to be outdated, by design, if managed humanly. --- src/bin/psql/README.md | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/bin/psql/README.md b/src/bin/psql/README.md index b729152179068..3d38adeb5d31e 100644 --- a/src/bin/psql/README.md +++ b/src/bin/psql/README.md @@ -1,5 +1,3 @@ -Last merged with postgres master: 15th Aug 2017 - This document shows the current state of psql's auto-complete support for Amazon Redshift NOTE: This branch is maintained as a personal effort and is not sponsored / supported by my Employer or any other third-party. This is not officially supported / endorsed by Amazon in any way. From 8cd00e64228ce2a3099cb8a4472f579e71b5a163 Mon Sep 17 00:00:00 2001 From: Robins Tharakan Date: Fri, 1 Sep 2017 23:48:38 +1000 Subject: [PATCH 074/149] WIP towards adding SORTKEY details to Describe Table --- src/bin/psql/describe.c | 86 ++++++++++++++++++++++++++++++++++++++--- 1 file changed, 81 insertions(+), 5 deletions(-) diff --git a/src/bin/psql/describe.c b/src/bin/psql/describe.c index a771ed40d56e0..f27459cac5d7c 100644 --- a/src/bin/psql/describe.c +++ b/src/bin/psql/describe.c @@ -1403,6 +1403,7 @@ describeOneTableDetails(const char *schemaname, char *reloftype; char relpersistence; char relreplident; + char hassortkey; } tableinfo; bool show_column_details = false; bool retval; @@ -1513,12 +1514,26 @@ describeOneTableDetails(const char *schemaname, } else if (pset.sversion >= 80000) { + + if (IS_REDSHIFT) { + printfPQExpBuffer(&buf, + "SELECT relchecks, relkind, relhasindex, relhasrules, " + "reltriggers <> 0, false, false, relhasoids, " + "%s, reltablespace, (SELECT EXISTS (SELECT 1 FROM pg_attribute WHERE attrelid = oid AND attsortkeyord <> 0))) as relhassortkey \n" + "FROM pg_catalog.pg_class WHERE oid = '%s';", + (verbose ? + "pg_catalog.array_to_string(reloptions, E', ')" : "''"), + oid); + } + else + { printfPQExpBuffer(&buf, "SELECT relchecks, relkind, relhasindex, relhasrules, " "reltriggers <> 0, false, false, relhasoids, " "'', reltablespace\n" "FROM pg_catalog.pg_class WHERE oid = '%s';", oid); + } } else { @@ -1554,6 +1569,9 @@ describeOneTableDetails(const char *schemaname, pg_strdup(PQgetvalue(res, 0, 8)) : NULL; tableinfo.tablespace = (pset.sversion >= 80000) ? atooid(PQgetvalue(res, 0, 9)) : 0; +/* tableinfo.hassortkey = (IS_REDSHIFT)? + (strcmp(PQgetvalue(res, 0, 11), "t") == 0) : '0'; + */ tableinfo.reloftype = (pset.sversion >= 90000 && strcmp(PQgetvalue(res, 0, 10), "") != 0) ? pg_strdup(PQgetvalue(res, 0, 10)) : NULL; @@ -2124,8 +2142,9 @@ describeOneTableDetails(const char *schemaname, int tuples = 0; /* print indexes */ - if (tableinfo.hasindex) + if (tableinfo.hasindex || tableinfo.hassortkey) { + if (tableinfo.hasindex) { printfPQExpBuffer(&buf, "SELECT c2.relname, i.indisprimary, i.indisunique, i.indisclustered, "); if (pset.sversion >= 80200) @@ -2146,16 +2165,64 @@ describeOneTableDetails(const char *schemaname, else appendPQExpBufferStr(&buf, ", false AS indisreplident"); if (pset.sversion >= 80000) - appendPQExpBufferStr(&buf, ", c2.reltablespace"); + appendPQExpBufferStr(&buf, ", c2.reltablespace,"); + + /* Redshift specific Columns */ + appendPQExpBufferStr(&buf, "FALSE as indisinterleaved, "); + appendPQExpBufferStr(&buf, "FALSE as indiscompound "); + appendPQExpBufferStr(&buf, "\nFROM pg_catalog.pg_class c, pg_catalog.pg_class c2, pg_catalog.pg_index i\n"); if (pset.sversion >= 90000) appendPQExpBufferStr(&buf, " LEFT JOIN pg_catalog.pg_constraint con ON (conrelid = i.indrelid AND conindid = i.indexrelid AND contype IN ('p','u','x'))\n"); appendPQExpBuffer(&buf, - "WHERE c.oid = '%s' AND c.oid = i.indrelid AND i.indexrelid = c2.oid\n" - "ORDER BY i.indisprimary DESC, i.indisunique DESC, c2.relname;", - oid); + "WHERE c.oid = '%s' AND c.oid = i.indrelid AND i.indexrelid = c2.oid\n", oid); + } + + if (tableinfo.hasindex && tableinfo.hassortkey) { + appendPQExpBufferStr(&buf, " UNION ALL "); + } + + if (tableinfo.hassortkey) { + + appendPQExpBufferStr(&buf, + " SELECT "); + appendPQExpBuffer(&buf, + " (SELECT CASE WHEN (SELECT MIN(attsortkeyord) FROM pg_attribute WHERE attrelid = '%s') <0 ", oid); + appendPQExpBufferStr(&buf, + " THEN 'INTERLEAVED SORTKEY' ELSE 'COMPOUND SORTKEY' END) AS relname, " + " FALSE AS indisprimary, FALSE AS indisunique, FALSE AS indisclustered, TRUE AS indisvalid, " + " (WITH x AS " + " ( SELECT row_number() OVER () AS r, trim(attname) AS nm " + " FROM pg_attribute "); + appendPQExpBuffer(&buf, + " WHERE attrelid = '%s' AND attsortkeyord <> 0 ORDER BY ABS(attsortkeyord)) ", oid); + appendPQExpBufferStr(&buf, + " SELECT " + " MAX(CASE WHEN r = 1 THEN nm ELSE '' END) || " + " MAX(CASE WHEN r = 2 THEN (',' || nm) ELSE '' END) || " + " MAX(CASE WHEN r = 3 THEN (',' || nm) ELSE '' END) || " + " MAX(CASE WHEN r = 4 THEN (',' || nm) ELSE '' END) || " + " MAX(CASE WHEN r = 5 THEN (',' || nm) ELSE '' END) || " + " MAX(CASE WHEN r = 6 THEN (',' || nm) ELSE '' END) || " + " MAX(CASE WHEN r = 7 THEN (',' || nm) ELSE '' END) || " + " MAX(CASE WHEN r = 8 THEN (',' || nm) ELSE '' END) || " + " MAX(CASE WHEN r = 9 THEN (',' || nm) ELSE '' END) || " + " MAX(CASE WHEN r =10 THEN (',' || nm) ELSE '' END) AS t " + " FROM x), " + " NULL AS constraintdef, NULL AS contype, FALSE AS condeferrable, FALSE AS condeferred, " + " FALSE AS indisreplident, NULL AS reltablespace, "); + appendPQExpBuffer(&buf, + " ((SELECT MIN(attsortkeyord) FROM pg_attribute WHERE attrelid = '%s') < 0)::BOOLEAN AS indisinterleaved, ", oid); + appendPQExpBuffer(&buf, + " ((SELECT MIN(attsortkeyord) FROM pg_attribute WHERE attrelid = '%s') > 0)::BOOLEAN AS indiscompound ", oid); + + appendPQExpBuffer(&buf, + " FROM (SELECT '%s' AS oid) c ", oid); + + + appendPQExpBufferStr(&buf, "ORDER BY indisprimary DESC, indisunique DESC, relname;"); result = PSQLexec(buf.data); if (!result) goto error_return; @@ -2225,6 +2292,15 @@ describeOneTableDetails(const char *schemaname, add_tablespace_footer(&cont, RELKIND_INDEX, atooid(PQgetvalue(result, i, 11)), false); +/* + / * Print SORTKEY details (Redshift specific) * / + if (IS_REDSHIFT) + add_tablespace_footer(&cont, RELKIND_INDEX, + atooid(PQgetvalue(result, i, 11)), + false); + + 12 indisinterleaved +*/ } } PQclear(result); From d219525eb60ba10ffb86c5efd94d2b457bd129ae Mon Sep 17 00:00:00 2001 From: Robins Tharakan Date: Sat, 2 Sep 2017 07:22:59 +1000 Subject: [PATCH 075/149] INTERLEAVED / COMPOUND SORTKEY showing up properly on Redshift tables (COMMPOUND SORTKEY upto 10 columns only) --- src/bin/psql/describe.c | 56 +++++++++++++++++++++-------------------- 1 file changed, 29 insertions(+), 27 deletions(-) diff --git a/src/bin/psql/describe.c b/src/bin/psql/describe.c index f27459cac5d7c..71f2b8829350b 100644 --- a/src/bin/psql/describe.c +++ b/src/bin/psql/describe.c @@ -1519,7 +1519,7 @@ describeOneTableDetails(const char *schemaname, printfPQExpBuffer(&buf, "SELECT relchecks, relkind, relhasindex, relhasrules, " "reltriggers <> 0, false, false, relhasoids, " - "%s, reltablespace, (SELECT EXISTS (SELECT 1 FROM pg_attribute WHERE attrelid = oid AND attsortkeyord <> 0))) as relhassortkey \n" + "%s, reltablespace, (SELECT EXISTS (SELECT 1 FROM pg_attribute WHERE attrelid = oid AND attsortkeyord <> 0)) as relhassortkey \n" "FROM pg_catalog.pg_class WHERE oid = '%s';", (verbose ? "pg_catalog.array_to_string(reloptions, E', ')" : "''"), @@ -1569,9 +1569,8 @@ describeOneTableDetails(const char *schemaname, pg_strdup(PQgetvalue(res, 0, 8)) : NULL; tableinfo.tablespace = (pset.sversion >= 80000) ? atooid(PQgetvalue(res, 0, 9)) : 0; -/* tableinfo.hassortkey = (IS_REDSHIFT)? - (strcmp(PQgetvalue(res, 0, 11), "t") == 0) : '0'; - */ + tableinfo.hassortkey = (IS_REDSHIFT)? + (strcmp(PQgetvalue(res, 0, 10), "t") == 0) : '0'; tableinfo.reloftype = (pset.sversion >= 90000 && strcmp(PQgetvalue(res, 0, 10), "") != 0) ? pg_strdup(PQgetvalue(res, 0, 10)) : NULL; @@ -2142,7 +2141,7 @@ describeOneTableDetails(const char *schemaname, int tuples = 0; /* print indexes */ - if (tableinfo.hasindex || tableinfo.hassortkey) + if ((tableinfo.hasindex) || (tableinfo.hassortkey)) { if (tableinfo.hasindex) { printfPQExpBuffer(&buf, @@ -2187,12 +2186,8 @@ describeOneTableDetails(const char *schemaname, if (tableinfo.hassortkey) { appendPQExpBufferStr(&buf, - " SELECT "); - appendPQExpBuffer(&buf, - " (SELECT CASE WHEN (SELECT MIN(attsortkeyord) FROM pg_attribute WHERE attrelid = '%s') <0 ", oid); - appendPQExpBufferStr(&buf, - " THEN 'INTERLEAVED SORTKEY' ELSE 'COMPOUND SORTKEY' END) AS relname, " - " FALSE AS indisprimary, FALSE AS indisunique, FALSE AS indisclustered, TRUE AS indisvalid, " + " SELECT " + " NULL AS relname, FALSE AS indisprimary, FALSE AS indisunique, FALSE AS indisclustered, TRUE AS indisvalid, " " (WITH x AS " " ( SELECT row_number() OVER () AS r, trim(attname) AS nm " " FROM pg_attribute "); @@ -2200,7 +2195,7 @@ describeOneTableDetails(const char *schemaname, " WHERE attrelid = '%s' AND attsortkeyord <> 0 ORDER BY ABS(attsortkeyord)) ", oid); appendPQExpBufferStr(&buf, " SELECT " - " MAX(CASE WHEN r = 1 THEN nm ELSE '' END) || " + "'(' || MAX(CASE WHEN r = 1 THEN nm ELSE '' END) || " " MAX(CASE WHEN r = 2 THEN (',' || nm) ELSE '' END) || " " MAX(CASE WHEN r = 3 THEN (',' || nm) ELSE '' END) || " " MAX(CASE WHEN r = 4 THEN (',' || nm) ELSE '' END) || " @@ -2209,18 +2204,18 @@ describeOneTableDetails(const char *schemaname, " MAX(CASE WHEN r = 7 THEN (',' || nm) ELSE '' END) || " " MAX(CASE WHEN r = 8 THEN (',' || nm) ELSE '' END) || " " MAX(CASE WHEN r = 9 THEN (',' || nm) ELSE '' END) || " - " MAX(CASE WHEN r =10 THEN (',' || nm) ELSE '' END) AS t " + " MAX(CASE WHEN r =10 THEN (',' || nm) ELSE '' END) || ')' AS t " " FROM x), " " NULL AS constraintdef, NULL AS contype, FALSE AS condeferrable, FALSE AS condeferred, " " FALSE AS indisreplident, NULL AS reltablespace, "); appendPQExpBuffer(&buf, " ((SELECT MIN(attsortkeyord) FROM pg_attribute WHERE attrelid = '%s') < 0)::BOOLEAN AS indisinterleaved, ", oid); appendPQExpBuffer(&buf, - " ((SELECT MIN(attsortkeyord) FROM pg_attribute WHERE attrelid = '%s') > 0)::BOOLEAN AS indiscompound ", oid); + " ((SELECT MIN(attsortkeyord) FROM pg_attribute WHERE attrelid = '%s' AND attsortkeyord > 0) > 0)::BOOLEAN AS indiscompound ", oid); appendPQExpBuffer(&buf, " FROM (SELECT '%s' AS oid) c ", oid); - + } appendPQExpBufferStr(&buf, "ORDER BY indisprimary DESC, indisunique DESC, relname;"); result = PSQLexec(buf.data); @@ -2235,8 +2230,11 @@ describeOneTableDetails(const char *schemaname, for (i = 0; i < tuples; i++) { /* untranslated index name */ - printfPQExpBuffer(&buf, " \"%s\"", - PQgetvalue(result, i, 0)); + if (!IS_REDSHIFT) + printfPQExpBuffer(&buf, " \"%s\"", + PQgetvalue(result, i, 0)); + else + resetPQExpBuffer(&buf); /* If exclusion constraint, print the constraintdef */ if (strcmp(PQgetvalue(result, i, 7), "x") == 0) @@ -2250,7 +2248,7 @@ describeOneTableDetails(const char *schemaname, const char *usingpos; /* Label as primary key or unique (but not both) */ - if (strcmp(PQgetvalue(result, i, 1), "t") == 0) + if (strcmp(PQgetvalue(result, i, 1), "t") == 0) appendPQExpBufferStr(&buf, " PRIMARY KEY,"); else if (strcmp(PQgetvalue(result, i, 2), "t") == 0) { @@ -2259,6 +2257,19 @@ describeOneTableDetails(const char *schemaname, else appendPQExpBufferStr(&buf, " UNIQUE,"); } + else if (IS_REDSHIFT) + { + if (strcmp(PQgetvalue(result, i, 12), "t") == 0) + appendPQExpBufferStr(&buf, " INTERLEAVED SORTKEY"); + else if (strcmp(PQgetvalue(result, i, 13), "t") == 0) + appendPQExpBufferStr(&buf, " COMPOUND SORTKEY"); + else + { + //psql_error("\nUnable to ascertain Redshift SORTKEY type (%d)\n", *PQgetvalue(result, i, 12)); + psql_error("\nUnable to ascertain Redshift SORTKEY type\n"); + goto error_return; + } + } /* Everything after "USING" is echoed verbatim */ indexdef = PQgetvalue(result, i, 5); @@ -2292,15 +2303,6 @@ describeOneTableDetails(const char *schemaname, add_tablespace_footer(&cont, RELKIND_INDEX, atooid(PQgetvalue(result, i, 11)), false); -/* - / * Print SORTKEY details (Redshift specific) * / - if (IS_REDSHIFT) - add_tablespace_footer(&cont, RELKIND_INDEX, - atooid(PQgetvalue(result, i, 11)), - false); - - 12 indisinterleaved -*/ } } PQclear(result); From 7ba71cfa1250024c1066f1bb6136abb83d55b1d7 Mon Sep 17 00:00:00 2001 From: Robins Tharakan Date: Mon, 4 Sep 2017 03:45:44 +1000 Subject: [PATCH 076/149] First commit towards IAM. Just a help addition. Still WIP --- src/bin/psql/help.c | 1 + 1 file changed, 1 insertion(+) diff --git a/src/bin/psql/help.c b/src/bin/psql/help.c index b3dbb5946eac0..c1097c2a97c19 100644 --- a/src/bin/psql/help.c +++ b/src/bin/psql/help.c @@ -128,6 +128,7 @@ usage(unsigned short int pager) env = getenv("PGHOST"); fprintf(output, _(" -h, --host=HOSTNAME database server host or socket directory (default: \"%s\")\n"), env ? env : _("local socket")); + fprintf(output, _(" -I, --aws-iam-redshift use temporary database credentials from AWS IAM Service\n")); /* Display default port */ env = getenv("PGPORT"); fprintf(output, _(" -p, --port=PORT database server port (default: \"%s\")\n"), From f7f72812a35fce5f287a2fb479c667f0d2fbea55 Mon Sep 17 00:00:00 2001 From: Robins Tharakan Date: Sun, 3 Sep 2017 18:15:04 +0000 Subject: [PATCH 077/149] Update --version to say both PostgreSQL and Amazon Redshift --- src/bin/psql/startup.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/bin/psql/startup.c b/src/bin/psql/startup.c index 41d456ab007e2..8b91414494ca5 100644 --- a/src/bin/psql/startup.c +++ b/src/bin/psql/startup.c @@ -768,7 +768,7 @@ process_psqlrc_file(char *filename) static void showVersion(void) { - puts("psql (Amazon Redshift) " PG_VERSION); + puts("psql (Supports PostgreSQL & Amazon Redshift) " PG_VERSION); } From 0394d820b2bf69568cc4ab86dbaddfcec3928a8c Mon Sep 17 00:00:00 2001 From: Robins Tharakan Date: Mon, 4 Sep 2017 21:41:49 +1000 Subject: [PATCH 078/149] Still working towards groundwork of IAM support. Not working --- src/bin/psql/command.c | 7 +++++++ src/bin/psql/settings.h | 8 ++++++++ src/bin/psql/startup.c | 44 ++++++++++++++++++++++++++++++++++------- 3 files changed, 52 insertions(+), 7 deletions(-) diff --git a/src/bin/psql/command.c b/src/bin/psql/command.c index 284892737b67b..66c16831fbe0a 100644 --- a/src/bin/psql/command.c +++ b/src/bin/psql/command.c @@ -3076,6 +3076,13 @@ do_connect(enum trivalue reuse_previous_specification, else connstr.data = NULL; + /* + * If the user asked psql to request username / password from AWS IAM + */ + if (pset.aws_iam_redshift) { + + } + /* * If the user asked to be prompted for a password, ask for one now. If * not, use the password from the old connection, provided the username diff --git a/src/bin/psql/settings.h b/src/bin/psql/settings.h index 5b5f6f4861cb1..589f3bb63d7c9 100644 --- a/src/bin/psql/settings.h +++ b/src/bin/psql/settings.h @@ -70,6 +70,12 @@ typedef enum hctl_ignoreboth = hctl_ignorespace | hctl_ignoredups } HistControl; +enum credential_source +{ + DEFAULT + AWS_IAM_REDSHIFT +} + enum trivalue { TRI_DEFAULT, @@ -100,6 +106,8 @@ typedef struct _psqlSettings bool notty; /* stdin or stdout is not a tty (as determined * on startup) */ enum trivalue getPassword; /* prompt the user for a username and password */ + enum credential_sources credential_source; /* source of database credentials */ + bool aws_iam_redshift; /* request username / password from AWS IAM */ FILE *cur_cmd_source; /* describe the status of the current main * loop */ bool cur_cmd_interactive; diff --git a/src/bin/psql/startup.c b/src/bin/psql/startup.c index 41d456ab007e2..1215651b27821 100644 --- a/src/bin/psql/startup.c +++ b/src/bin/psql/startup.c @@ -157,7 +157,8 @@ main(int argc, char *argv[]) pset.notty = (!isatty(fileno(stdin)) || !isatty(fileno(stdout))); pset.getPassword = TRI_DEFAULT; - + pset.credential_source = DEFAULT; + EstablishVariableSpace(); SetVariable(pset.vars, "VERSION", PG_VERSION_STR); @@ -206,8 +207,21 @@ main(int argc, char *argv[]) if (pset.getPassword == TRI_YES) { - simple_prompt(password_prompt, password, sizeof(password), false); - have_password = true; + if (pset.credential_source == AWS_IAM_REDSHIFT) + { + request_password_from_external_source(); + have_password = true; + } + else if (pset.credential_source == DEFAULT) + { + simple_prompt(password_prompt, password, sizeof(password), false); + have_password = true; + } + else + { + fprintf(stderr, _("getPassword enabled but Source unclear. Exiting.\n")); + exit(EXIT_FAILURE); + } } /* loop until we have a password if requested by backend */ @@ -245,10 +259,21 @@ main(int argc, char *argv[]) !have_password && pset.getPassword != TRI_NO) { - PQfinish(pset.db); - simple_prompt(password_prompt, password, sizeof(password), false); - have_password = true; - new_pass = true; + if (pset.credential_source == AWS_IAM_REDSHIFT) + { + PQfinish(pset.db); + request_password_from_external_source(); +// simple_prompt(password_prompt, password, sizeof(password), false); + have_password = true; + new_pass = true; + } + else + { + PQfinish(pset.db); + simple_prompt(password_prompt, password, sizeof(password), false); + have_password = true; + new_pass = true; + } } } while (new_pass); @@ -417,6 +442,7 @@ parse_psql_options(int argc, char *argv[], struct adhoc_opts *options) { {"echo-all", no_argument, NULL, 'a'}, {"no-align", no_argument, NULL, 'A'}, + {"aws-iam-redshift", no_argument, NULL, 'I'}, {"command", required_argument, NULL, 'c'}, {"dbname", required_argument, NULL, 'd'}, {"echo-queries", no_argument, NULL, 'e'}, @@ -506,6 +532,10 @@ parse_psql_options(int argc, char *argv[], struct adhoc_opts *options) case 'H': pset.popt.topt.format = PRINT_HTML; break; + case 'I': + pset.credential_source = DEFAULT; + pset.getPassword = TRI_YES; + break; case 'l': options->list_dbs = true; break; From 0becae081a82609e02ea4364a4bf1fa7ac28df00 Mon Sep 17 00:00:00 2001 From: Robins Tharakan Date: Mon, 4 Sep 2017 23:37:04 +1000 Subject: [PATCH 079/149] Compiling but now aws needs setting up in test env. Still heavily WIP --- src/bin/psql/command.c | 17 +++++++++-------- src/bin/psql/settings.h | 7 +++---- src/bin/psql/startup.c | 35 +++++++++++++++++++++++++++++++++-- 3 files changed, 45 insertions(+), 14 deletions(-) diff --git a/src/bin/psql/command.c b/src/bin/psql/command.c index 66c16831fbe0a..bb4b019b6cb3f 100644 --- a/src/bin/psql/command.c +++ b/src/bin/psql/command.c @@ -3076,13 +3076,6 @@ do_connect(enum trivalue reuse_previous_specification, else connstr.data = NULL; - /* - * If the user asked psql to request username / password from AWS IAM - */ - if (pset.aws_iam_redshift) { - - } - /* * If the user asked to be prompted for a password, ask for one now. If * not, use the password from the old connection, provided the username @@ -3095,7 +3088,15 @@ do_connect(enum trivalue reuse_previous_specification, */ if (pset.getPassword == TRI_YES) { - password = prompt_for_password(user); + if (pset.credential_source == AWS_IAM_REDSHIFT) + request_password_from_external_source(); + else if (pset.credential_source == DEFAULT) + password = prompt_for_password(user); + else + { + fprintf(stderr, _("getPassword enabled but Source unclear. Exiting.\n")); + exit(EXIT_FAILURE); + } } else if (o_conn && keep_password) { diff --git a/src/bin/psql/settings.h b/src/bin/psql/settings.h index 589f3bb63d7c9..c55cd68b39cd1 100644 --- a/src/bin/psql/settings.h +++ b/src/bin/psql/settings.h @@ -70,11 +70,11 @@ typedef enum hctl_ignoreboth = hctl_ignorespace | hctl_ignoredups } HistControl; -enum credential_source +enum credential_sources { - DEFAULT + DEFAULT, AWS_IAM_REDSHIFT -} +}; enum trivalue { @@ -107,7 +107,6 @@ typedef struct _psqlSettings * on startup) */ enum trivalue getPassword; /* prompt the user for a username and password */ enum credential_sources credential_source; /* source of database credentials */ - bool aws_iam_redshift; /* request username / password from AWS IAM */ FILE *cur_cmd_source; /* describe the status of the current main * loop */ bool cur_cmd_interactive; diff --git a/src/bin/psql/startup.c b/src/bin/psql/startup.c index 1215651b27821..213e31e817693 100644 --- a/src/bin/psql/startup.c +++ b/src/bin/psql/startup.c @@ -89,6 +89,37 @@ static void EstablishVariableSpace(void); #define NOPAGER 0 + + +/* + * request_password_from_external_source + * + * Generalized function to fetch usernames and passwords from external source. + * For now works only in Linux. + */ + void + request_password_from_external_source() + { + FILE *fp; + char path[1035]; + + /* Open the command for reading. */ + fp = popen("aws redshift get-cluster-credentials --db-user redshift2 --cluster-identifier redshift2", "r"); + if (fp == NULL) { + printf("Failed to run command\n" ); + exit(1); + } + + /* Read the output a line at a time - output it. */ + while (fgets(path, sizeof(path)-1, fp) != NULL) { + printf("%s", path); + } + + /* close */ + pclose(fp); + } + + /* * * main @@ -484,7 +515,7 @@ parse_psql_options(int argc, char *argv[], struct adhoc_opts *options) memset(options, 0, sizeof *options); - while ((c = getopt_long(argc, argv, "aAbc:d:eEf:F:h:HlL:no:p:P:qR:sStT:U:v:VwWxXz?01", + while ((c = getopt_long(argc, argv, "aAbc:d:eEf:F:h:HIlL:no:p:P:qR:sStT:U:v:VwWxXz?01", long_options, &optindex)) != -1) { switch (c) @@ -533,7 +564,7 @@ parse_psql_options(int argc, char *argv[], struct adhoc_opts *options) pset.popt.topt.format = PRINT_HTML; break; case 'I': - pset.credential_source = DEFAULT; + pset.credential_source = AWS_IAM_REDSHIFT; pset.getPassword = TRI_YES; break; case 'l': From a98a1d434f2871ce2f98c58b61257de60b1cab13 Mon Sep 17 00:00:00 2001 From: Robins Tharakan Date: Mon, 4 Sep 2017 20:04:19 +0000 Subject: [PATCH 080/149] Revert "Update --version to say both PostgreSQL and Amazon Redshift" This reverts commit f7f72812a35fce5f287a2fb479c667f0d2fbea55. --- src/bin/psql/startup.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/bin/psql/startup.c b/src/bin/psql/startup.c index 8b91414494ca5..41d456ab007e2 100644 --- a/src/bin/psql/startup.c +++ b/src/bin/psql/startup.c @@ -768,7 +768,7 @@ process_psqlrc_file(char *filename) static void showVersion(void) { - puts("psql (Supports PostgreSQL & Amazon Redshift) " PG_VERSION); + puts("psql (Amazon Redshift) " PG_VERSION); } From 3221b5db1a8bd579dfbb2873978c5b847ff26f9a Mon Sep 17 00:00:00 2001 From: Robins Tharakan Date: Tue, 5 Sep 2017 20:17:19 +1000 Subject: [PATCH 081/149] Sample commit to see if an external script would do the job (in linux). The Script is called such keeping in mind this is a 'External AUTHentication script for Linux'. This is from a foreign contributer, but would update this commit once I have explicit permission to include the reference / contribution. --- src/bin/psql/leauth.sh | 76 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100755 src/bin/psql/leauth.sh diff --git a/src/bin/psql/leauth.sh b/src/bin/psql/leauth.sh new file mode 100755 index 0000000000000..7aca7b79849a7 --- /dev/null +++ b/src/bin/psql/leauth.sh @@ -0,0 +1,76 @@ +#!/bin/bash + +# AWS CLI requires Python 2.6.5, this script also requires Python 2.6+ or newer +if which aws > /dev/null 2>&1; +then + AWSCLI_VERSION=`python -c "import awscli; print(awscli.__version__)"` + if [[ "$AWSCLI_VERSION" < "1.11.132" ]] ; + then + echo "Please update your version of the AWS CLI." + exit 0 + fi +else + echo "Please install and configure the AWS CLI." + echo "http://docs.aws.amazon.com/cli/latest/userguide/installing.html" + exit 0 +fi + +ARGS="" + +while [[ $# -gt 1 ]] +do +key="$1" +case "$key" in + -U|--username) + USERNAME="$2" + shift + ;; + -i|--clusterid) + CLUSTER_ID="$2" + shift + ;; + -r|--region) + REGION="$2" + shift + ;; + -h|--host) + HOST="$2" + shift + ;; + *) + ARGS="$ARGS $1" + ;; +esac +shift +done + +ARGS="$ARGS $@" + + +if [ "$HOST" != "" ] && [ "$CLUSTER_ID" != "" ] ; +then + echo "Specify either Host or Cluster Identifier, not both." + exit 0 +fi + +if [ "$CLUSTER_ID" != "" ] ; +then + if [ "$REGION" == "" ] ; + then + CLUSTER_INFO=`aws redshift describe-clusters --cluster-identifier "$CLUSTER_ID"` || exit 0 + CREDS=`aws redshift get-cluster-credentials --db-user "$USERNAME" --cluster-identifier "$CLUSTER_ID"` + else + CLUSTER_INFO=`aws redshift describe-clusters --cluster-identifier "$CLUSTER_ID" --region="$REGION"` || exit 0 + CREDS=`aws redshift get-cluster-credentials --db-user "$USERNAME" --cluster-identifier "$CLUSTER_ID" --region="$REGION"` + fi + + CLUSTER_ENDPOINT=`echo "$CLUSTER_INFO" | python -c "import sys, json;print(json.load(sys.stdin)['Clusters'][0]['Endpoint']['Address'])"` + DBPWD=`echo "$CREDS" | python -c "import sys, json; print(json.load(sys.stdin)['DbPassword'])"` + DBUSER=`echo "$CREDS" | python -c "import sys, json; print(json.load(sys.stdin)['DbUser'])"` + + ARGS="$ARGS -U $DBUSER -h $CLUSTER_ENDPOINT" +else + ARGS="$ARGS -U $USERNAME -h $HOST" +fi + +PGPASSWORD="$DBPWD" psql ${ARGS} From 706db1763ea8e6b7a6fb1f9eaca8ba5a4a0a3ad6 Mon Sep 17 00:00:00 2001 From: Robins Tharakan Date: Tue, 12 Sep 2017 20:57:59 +1000 Subject: [PATCH 082/149] Move Redshift specific README to README_redshift --- src/bin/psql/{README.md => README_redshift.md} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename src/bin/psql/{README.md => README_redshift.md} (100%) diff --git a/src/bin/psql/README.md b/src/bin/psql/README_redshift.md similarity index 100% rename from src/bin/psql/README.md rename to src/bin/psql/README_redshift.md From dac6de05e26e5097ec8b2120235d0f924af81943 Mon Sep 17 00:00:00 2001 From: Robins Tharakan Date: Tue, 12 Sep 2017 21:01:47 +1000 Subject: [PATCH 083/149] Add a generic README.md to refer to status page for multiple Postgres forks --- src/bin/psql/README.md | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 src/bin/psql/README.md diff --git a/src/bin/psql/README.md b/src/bin/psql/README.md new file mode 100644 index 0000000000000..ccc7d10b096af --- /dev/null +++ b/src/bin/psql/README.md @@ -0,0 +1,8 @@ +This documents the current state of psql's auto-complete support for various Postgres Variants. + +NOTE: This branch is maintained as a personal effort and is not sponsored / supported by my Employer or any other third-party. This is not officially supported / endorsed by any Commercial interest in any way. + +Postgres Forks | Support Level | Description +:-------------:| ------------- | ----------- +Amazon Redshift | | Amazon Redshift was spin off of PostgreSQL (version 8.0.2) +PipelineDB | | Compatible with PostgreSQL (version 9.5) From 4a93625ed18cac9d27515d96fccefe29fd27d446 Mon Sep 17 00:00:00 2001 From: Robins Date: Tue, 12 Sep 2017 21:07:25 +1000 Subject: [PATCH 084/149] Update README.md with Status Page URLs for Redshift / PipelineDB (for psql) --- src/bin/psql/README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/bin/psql/README.md b/src/bin/psql/README.md index ccc7d10b096af..8ab3434277199 100644 --- a/src/bin/psql/README.md +++ b/src/bin/psql/README.md @@ -3,6 +3,6 @@ This documents the current state of psql's auto-complete support for various Pos NOTE: This branch is maintained as a personal effort and is not sponsored / supported by my Employer or any other third-party. This is not officially supported / endorsed by any Commercial interest in any way. Postgres Forks | Support Level | Description -:-------------:| ------------- | ----------- -Amazon Redshift | | Amazon Redshift was spin off of PostgreSQL (version 8.0.2) -PipelineDB | | Compatible with PostgreSQL (version 9.5) +:-------------:| :-----------: | :---------- +Amazon Redshift | [Status](https://github.com/robins/postgres/blob/redshift/src/bin/psql/README_redshift.md) | Amazon Redshift was spin off of PostgreSQL (version 8.0.2) +PipelineDB | [Status](https://github.com/robins/postgres/blob/redshift/src/bin/psql/README_pipelinedb.md) | Compatible with PostgreSQL (version 9.5) From 93c230b9a7150b0adb8cbdc873ceb10122dabd3b Mon Sep 17 00:00:00 2001 From: Robins Tharakan Date: Tue, 12 Sep 2017 21:10:19 +1000 Subject: [PATCH 085/149] Initial Commit for README_PipelineDB.md. Still WIP --- src/bin/psql/README_pipelinedb.md | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 src/bin/psql/README_pipelinedb.md diff --git a/src/bin/psql/README_pipelinedb.md b/src/bin/psql/README_pipelinedb.md new file mode 100644 index 0000000000000..17003630baff1 --- /dev/null +++ b/src/bin/psql/README_pipelinedb.md @@ -0,0 +1,20 @@ +This document shows the current state of psql's auto-complete support for PipelineDB. + +Reference: http://docs.pipelinedb.com/ + +Support Legend | Description +:-------------:| ----------- +Native | All combinations supported by Native psql +Unsupported | Not (yet)supported +Partial | Only some combinations supported +**Yes** | Supported by (only) this branch (not Native) +Complete | All combinations supported + + +SQL Commands | Supported State +:----------- |:--------------: +ABORT | Native +ALTER TABLE | Partial +ALTER TABLE ADD COLUMN | **Yes** +ALTER TABLE (Similarly Many Variants) | Unsupported +ALTER USER | Complete From 4d0d91a6cd892870fb31fa20024bc76b2503072a Mon Sep 17 00:00:00 2001 From: Robins Tharakan Date: Tue, 12 Sep 2017 21:26:37 +1000 Subject: [PATCH 086/149] Add SQL Commands from PipelineDB Docs --- src/bin/psql/README_pipelinedb.md | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/bin/psql/README_pipelinedb.md b/src/bin/psql/README_pipelinedb.md index 17003630baff1..b4133ae10d9a9 100644 --- a/src/bin/psql/README_pipelinedb.md +++ b/src/bin/psql/README_pipelinedb.md @@ -13,8 +13,10 @@ Complete | All combinations supported SQL Commands | Supported State :----------- |:--------------: -ABORT | Native -ALTER TABLE | Partial -ALTER TABLE ADD COLUMN | **Yes** -ALTER TABLE (Similarly Many Variants) | Unsupported -ALTER USER | Complete +ALTER STREAM | Unsupported +CREATE CONTINUOUS TRANSFORM | Unsupported +CREATE CONTINUOUS VIEW | Unsupported +CREATE STREAM | Unsupported +DROP CONTINUOUS TRANSFORM | Unsupported +DROP CONTINUOUS VIEW | Unsupported +DROP STREAM | Unsupported From 0f4868a81c2daa86b15b186427f36209053213ad Mon Sep 17 00:00:00 2001 From: Robins Tharakan Date: Wed, 13 Sep 2017 06:07:06 +1000 Subject: [PATCH 087/149] Now psql recognizes PipelineDB (and Redshift) on login and sets an internal variable accordingly (in the future). Also basic command support for STREAM / CONTINUOUS VIEW / CONTINUOUS TRANSFORM --- src/bin/psql/command.c | 96 ++++++++++++++++++++++++++++++++++++- src/bin/psql/startup.c | 2 +- src/bin/psql/tab-complete.c | 10 ++++ 3 files changed, 106 insertions(+), 2 deletions(-) diff --git a/src/bin/psql/command.c b/src/bin/psql/command.c index ff4c1764e003a..2963b7073f75d 100644 --- a/src/bin/psql/command.c +++ b/src/bin/psql/command.c @@ -162,6 +162,10 @@ static void printSSLInfo(void); static bool printPsetInfo(const char *param, struct printQueryOpt *popt); static char *pset_value_string(const char *param, struct printQueryOpt *popt); +static PGresult *exec_query(const char *query); +static char *escape_string(const char *text); +static char *get_guctype(const char *varname); + #ifdef WIN32 static void checkWin32Codepage(void); #endif @@ -3220,7 +3224,15 @@ SyncVariables(void) if (pset.sversion == 80002) { pset.sengine = "redshift"; } else { - pset.sengine = "postgres"; + /* Check whether a given GUC exists */ + char *guctype = get_guctype("continuous_queries_enabled"); + if (guctype != NULL) + pset.sengine = "pipelinedb"; + else + pset.sengine = "postgres"; + + if (guctype) + free(guctype); } SetVariable(pset.vars, "DBNAME", PQdb(pset.db)); @@ -3228,6 +3240,7 @@ SyncVariables(void) SetVariable(pset.vars, "HOST", PQhost(pset.db)); SetVariable(pset.vars, "PORT", PQport(pset.db)); SetVariable(pset.vars, "ENCODING", pg_encoding_to_char(pset.encoding)); + SetVariable(pset.vars, "ENGINE", PQdb(pset.db)); /* this bit should match connection_warnings(): */ /* Try to get full text form of version, might include "devel" etc */ @@ -3248,6 +3261,87 @@ SyncVariables(void) PQsetErrorContextVisibility(pset.db, pset.show_context); } +// XXX: Ideally the following 3 functions should be included from common definitions +/* + * Execute a query and report any errors. This should be the preferred way of + * talking to the database in this file. + */ + static PGresult * + exec_query(const char *query) + { + PGresult *result; + + if (query == NULL || !pset.db || PQstatus(pset.db) != CONNECTION_OK) + return NULL; + + result = PQexec(pset.db, query); + + if (PQresultStatus(result) != PGRES_TUPLES_OK) + { + #ifdef NOT_USED + psql_error("tab completion query failed: %s\nQuery was:\n%s\n", + PQerrorMessage(pset.db), query); + #endif + PQclear(result); + result = NULL; + } + + return result; + } + +/* + * escape_string - Escape argument for use as string literal. + * + * The returned value has to be freed. + */ + static char * + escape_string(const char *text) + { + size_t text_length; + char *result; + + text_length = strlen(text); + + result = pg_malloc(text_length * 2 + 1); + PQescapeStringConn(pset.db, result, text, text_length, NULL); + + return result; + } + +/* + * Look up the type for the GUC variable with the passed name. + * + * Returns NULL if the variable is unknown. Otherwise the returned string, + * containing the type, has to be freed. + */ + static char * + get_guctype(const char *varname) + { + PQExpBufferData query_buffer; + char *e_varname; + PGresult *result; + char *guctype = NULL; + + e_varname = escape_string(varname); + + initPQExpBuffer(&query_buffer); + appendPQExpBuffer(&query_buffer, + "SELECT vartype FROM pg_catalog.pg_settings " + "WHERE pg_catalog.lower(name) = pg_catalog.lower('%s')", + e_varname); + + result = exec_query(query_buffer.data); + termPQExpBuffer(&query_buffer); + free(e_varname); + + if (PQresultStatus(result) == PGRES_TUPLES_OK && PQntuples(result) > 0) + guctype = pg_strdup(PQgetvalue(result, 0, 0)); + + PQclear(result); + + return guctype; + } + /* * UnsyncVariables * diff --git a/src/bin/psql/startup.c b/src/bin/psql/startup.c index 844976ea8c994..b3158c3d44fa6 100644 --- a/src/bin/psql/startup.c +++ b/src/bin/psql/startup.c @@ -771,7 +771,7 @@ process_psqlrc_file(char *filename) static void showVersion(void) { - puts("psql (Amazon Redshift) " PG_VERSION); + puts("psql (PostgreSQL / Amazon Redshift / PipelineDB) " PG_VERSION); } diff --git a/src/bin/psql/tab-complete.c b/src/bin/psql/tab-complete.c index ee068dbdd8c29..c426cfc834a9e 100644 --- a/src/bin/psql/tab-complete.c +++ b/src/bin/psql/tab-complete.c @@ -1082,6 +1082,8 @@ static const pgsql_thing_t words_after_create[] = { * to be used only by pg_dump. */ {"CONFIGURATION", Query_for_list_of_ts_configurations, NULL, THING_NO_SHOW}, + {"CONTINUOUS TRANSFORM", NULL, NULL, THING_NO_ALTER}, + {"CONTINUOUS VIEW", NULL, NULL, THING_NO_ALTER}, {"CONVERSION", "SELECT pg_catalog.quote_ident(conname) FROM pg_catalog.pg_conversion WHERE substring(pg_catalog.quote_ident(conname),1,%d)='%s'"}, {"DATABASE", Query_for_list_of_databases}, {"DEFAULT PRIVILEGES", NULL, NULL, THING_NO_CREATE | THING_NO_DROP}, @@ -1116,6 +1118,7 @@ static const pgsql_thing_t words_after_create[] = { {"SEQUENCE", NULL, &Query_for_list_of_sequences}, {"SERVER", Query_for_list_of_servers}, {"STATISTICS", NULL, &Query_for_list_of_statistics}, + {"STREAM", NULL, NULL}, {"SUBSCRIPTION", Query_for_list_of_subscriptions}, {"SYSTEM", NULL, NULL, THING_NO_CREATE | THING_NO_DROP}, {"TABLE", NULL, &Query_for_list_of_tables}, @@ -1596,6 +1599,9 @@ psql_completion(const char *text, int start, int end) #define for various SQLs (in this script) employ there own string-compare */ #define IS_REDSHIFT strncmp(pset.sengine, "redshift", 8) == 0 +/* Flag to check if server is a PipelineDB Engine*/ +#define IS_PIPELINEDB strncmp(pset.sengine, "pipelinedb", 10) == 0 + /* Clear a few things. */ completion_charp = NULL; completion_charpp = NULL; @@ -2970,6 +2976,10 @@ psql_completion(const char *text, int start, int end) else if (Matches5("CREATE", "EVENT", "TRIGGER", MatchAny, "ON")) COMPLETE_WITH_LIST3("ddl_command_start", "ddl_command_end", "sql_drop"); +/* CREATE CONTINUOUS TRANSFORM / VIEW */ + else if ((IS_PIPELINEDB) && (Matches2("CREATE|DROP", "CONTINUOUS"))) + COMPLETE_WITH_LIST2("TRANSFORM", "VIEW"); + /* DEALLOCATE */ else if (Matches1("DEALLOCATE")) COMPLETE_WITH_QUERY(Query_for_list_of_prepared_statements " UNION SELECT 'PREPARE'"); From 41ace6290dad2d874574920ada275234e477a028 Mon Sep 17 00:00:00 2001 From: Robins Tharakan Date: Wed, 13 Sep 2017 06:09:39 +1000 Subject: [PATCH 088/149] Update README_pipelinedb with recently added basic support --- src/bin/psql/README_pipelinedb.md | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/bin/psql/README_pipelinedb.md b/src/bin/psql/README_pipelinedb.md index b4133ae10d9a9..fbaccfe0c49e9 100644 --- a/src/bin/psql/README_pipelinedb.md +++ b/src/bin/psql/README_pipelinedb.md @@ -13,10 +13,10 @@ Complete | All combinations supported SQL Commands | Supported State :----------- |:--------------: -ALTER STREAM | Unsupported -CREATE CONTINUOUS TRANSFORM | Unsupported -CREATE CONTINUOUS VIEW | Unsupported -CREATE STREAM | Unsupported -DROP CONTINUOUS TRANSFORM | Unsupported -DROP CONTINUOUS VIEW | Unsupported -DROP STREAM | Unsupported +ALTER STREAM | Partial +CREATE CONTINUOUS TRANSFORM | Partial +CREATE CONTINUOUS VIEW | Partial +CREATE STREAM | Partial +DROP CONTINUOUS TRANSFORM | Partial +DROP CONTINUOUS VIEW | Partial +DROP STREAM | Partial From 2fe7f121272254ed8d700c91063f03d46588a64f Mon Sep 17 00:00:00 2001 From: Robins Tharakan Date: Wed, 13 Sep 2017 06:12:12 +1000 Subject: [PATCH 089/149] Add reference to the parent psql/README to both fork pages --- src/bin/psql/README_pipelinedb.md | 2 ++ src/bin/psql/README_redshift.md | 1 + 2 files changed, 3 insertions(+) diff --git a/src/bin/psql/README_pipelinedb.md b/src/bin/psql/README_pipelinedb.md index fbaccfe0c49e9..3f668b68d6c2c 100644 --- a/src/bin/psql/README_pipelinedb.md +++ b/src/bin/psql/README_pipelinedb.md @@ -2,6 +2,8 @@ This document shows the current state of psql's auto-complete support for Pipeli Reference: http://docs.pipelinedb.com/ +See Support level of all Postgres forks: Here(https://github.com/robins/postgres/blob/redshift/src/bin/psql/README.md) + Support Legend | Description :-------------:| ----------- Native | All combinations supported by Native psql diff --git a/src/bin/psql/README_redshift.md b/src/bin/psql/README_redshift.md index 3d38adeb5d31e..ca19dc5c7a34c 100644 --- a/src/bin/psql/README_redshift.md +++ b/src/bin/psql/README_redshift.md @@ -2,6 +2,7 @@ This document shows the current state of psql's auto-complete support for Amazon NOTE: This branch is maintained as a personal effort and is not sponsored / supported by my Employer or any other third-party. This is not officially supported / endorsed by Amazon in any way. +See Support level of all Postgres forks: Here(https://github.com/robins/postgres/blob/redshift/src/bin/psql/README.md) Support Legend | Description :-------------:| ----------- From f408b5a8c0f58fb8559bc544104664ee274b0c8a Mon Sep 17 00:00:00 2001 From: Robins Tharakan Date: Wed, 13 Sep 2017 06:13:12 +1000 Subject: [PATCH 090/149] Move the Legend to the end of the doc --- src/bin/psql/README_pipelinedb.md | 17 ++++++++--------- src/bin/psql/README_redshift.md | 18 +++++++----------- 2 files changed, 15 insertions(+), 20 deletions(-) diff --git a/src/bin/psql/README_pipelinedb.md b/src/bin/psql/README_pipelinedb.md index 3f668b68d6c2c..c1f7321754650 100644 --- a/src/bin/psql/README_pipelinedb.md +++ b/src/bin/psql/README_pipelinedb.md @@ -4,15 +4,6 @@ Reference: http://docs.pipelinedb.com/ See Support level of all Postgres forks: Here(https://github.com/robins/postgres/blob/redshift/src/bin/psql/README.md) -Support Legend | Description -:-------------:| ----------- -Native | All combinations supported by Native psql -Unsupported | Not (yet)supported -Partial | Only some combinations supported -**Yes** | Supported by (only) this branch (not Native) -Complete | All combinations supported - - SQL Commands | Supported State :----------- |:--------------: ALTER STREAM | Partial @@ -22,3 +13,11 @@ CREATE STREAM | Partial DROP CONTINUOUS TRANSFORM | Partial DROP CONTINUOUS VIEW | Partial DROP STREAM | Partial + +Support Legend | Description +:-------------:| ----------- +Native | All combinations supported by Native psql +Unsupported | Not (yet)supported +Partial | Only some combinations supported +**Yes** | Supported by (only) this branch (not Native) +Complete | All combinations supported \ No newline at end of file diff --git a/src/bin/psql/README_redshift.md b/src/bin/psql/README_redshift.md index ca19dc5c7a34c..6fa362c973eed 100644 --- a/src/bin/psql/README_redshift.md +++ b/src/bin/psql/README_redshift.md @@ -4,16 +4,6 @@ NOTE: This branch is maintained as a personal effort and is not sponsored / supp See Support level of all Postgres forks: Here(https://github.com/robins/postgres/blob/redshift/src/bin/psql/README.md) -Support Legend | Description -:-------------:| ----------- -Native | All combinations supported by Native psql -Unsupported | Not (yet)supported -Partial | Only some combinations supported -**Yes** | Supported by (only) this branch (not Native) -Complete | All combinations supported - - - Parameters | Supported State :---------:|:--------------: analyze_threshold_percent | Unsupported @@ -146,4 +136,10 @@ VACUUM REINDEX | **Yes** VACUUM TO | **Yes** - +Support Legend | Description +:-------------:| ----------- +Native | All combinations supported by Native psql +Unsupported | Not (yet)supported +Partial | Only some combinations supported +**Yes** | Supported by (only) this branch (not Native) +Complete | All combinations supported \ No newline at end of file From 6a749b7f0dc56fa3d798e45b0234d712de05f054 Mon Sep 17 00:00:00 2001 From: Robins Tharakan Date: Wed, 13 Sep 2017 07:03:04 +1000 Subject: [PATCH 091/149] Minor typo correction in the previous commit --- src/bin/psql/README_pipelinedb.md | 2 +- src/bin/psql/README_redshift.md | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/bin/psql/README_pipelinedb.md b/src/bin/psql/README_pipelinedb.md index c1f7321754650..ea29e352ab062 100644 --- a/src/bin/psql/README_pipelinedb.md +++ b/src/bin/psql/README_pipelinedb.md @@ -2,7 +2,7 @@ This document shows the current state of psql's auto-complete support for Pipeli Reference: http://docs.pipelinedb.com/ -See Support level of all Postgres forks: Here(https://github.com/robins/postgres/blob/redshift/src/bin/psql/README.md) +See Support level of all Postgres forks: [Here](https://github.com/robins/postgres/blob/redshift/src/bin/psql/README.md) SQL Commands | Supported State :----------- |:--------------: diff --git a/src/bin/psql/README_redshift.md b/src/bin/psql/README_redshift.md index 6fa362c973eed..69808160025a5 100644 --- a/src/bin/psql/README_redshift.md +++ b/src/bin/psql/README_redshift.md @@ -2,7 +2,7 @@ This document shows the current state of psql's auto-complete support for Amazon NOTE: This branch is maintained as a personal effort and is not sponsored / supported by my Employer or any other third-party. This is not officially supported / endorsed by Amazon in any way. -See Support level of all Postgres forks: Here(https://github.com/robins/postgres/blob/redshift/src/bin/psql/README.md) +See Support level of all Postgres forks: [Here](https://github.com/robins/postgres/blob/redshift/src/bin/psql/README.md) Parameters | Supported State :---------:|:--------------: From 6e118210933c9c0e0636c373412ea0c32b0d2a73 Mon Sep 17 00:00:00 2001 From: Robins Tharakan Date: Wed, 13 Sep 2017 17:12:20 +1000 Subject: [PATCH 092/149] First (failed) attempt at putting the 'engine' on psql login prompt --- src/bin/psql/command.c | 33 +++++++++++++++++++++------------ src/bin/psql/describe.c | 9 +++++++-- src/bin/psql/settings.h | 1 - src/bin/psql/tab-complete.c | 14 +++++++------- 4 files changed, 35 insertions(+), 22 deletions(-) diff --git a/src/bin/psql/command.c b/src/bin/psql/command.c index 2963b7073f75d..db3629c8999b0 100644 --- a/src/bin/psql/command.c +++ b/src/bin/psql/command.c @@ -3115,9 +3115,12 @@ connection_warnings(bool in_startup) if (pset.sversion != client_ver) { const char *server_version; + const char *server_engine; /* Try to get full text form, might include "devel" etc */ server_version = PQparameterStatus(pset.db, "server_version"); + server_engine = PQparameterStatus(pset.db, "server_engine"); + /* Otherwise fall back on pset.sversion */ if (!server_version) { @@ -3126,8 +3129,8 @@ connection_warnings(bool in_startup) server_version = sverbuf; } - printf(_("%s (%s, server %s)\n"), - pset.progname, PG_VERSION, server_version); + printf(_("%s (%s, server %s, engine %s)\n"), + pset.progname, PG_VERSION, server_version, server_engine); } /* For version match, only print psql banner on startup. */ else if (in_startup) @@ -3215,26 +3218,31 @@ SyncVariables(void) { char vbuf[32]; const char *server_version; + const char *server_engine; + const char *guctype; /* get stuff from connection */ pset.encoding = PQclientEncoding(pset.db); pset.popt.topt.encoding = pset.encoding; pset.sversion = PQserverVersion(pset.db); - if (pset.sversion == 80002) { - pset.sengine = "redshift"; - } else { - /* Check whether a given GUC exists */ - char *guctype = get_guctype("continuous_queries_enabled"); + /* Check whether a given GUC exists */ + guctype = get_guctype("continuous_queries_enabled"); + if (guctype != NULL) + server_engine = "pipelinedb"; + else + { + guctype = get_guctype("wlm_query_slot_count"); + if (guctype != NULL) - pset.sengine = "pipelinedb"; + server_engine = "redshift"; else - pset.sengine = "postgres"; - - if (guctype) - free(guctype); + server_engine = "postgres"; } + if (guctype) + free(guctype); + SetVariable(pset.vars, "DBNAME", PQdb(pset.db)); SetVariable(pset.vars, "USER", PQuser(pset.db)); SetVariable(pset.vars, "HOST", PQhost(pset.db)); @@ -3252,6 +3260,7 @@ SyncVariables(void) server_version = vbuf; } SetVariable(pset.vars, "SERVER_VERSION_NAME", server_version); + SetVariable(pset.vars, "SERVER_ENGINE", server_engine); snprintf(vbuf, sizeof(vbuf), "%d", pset.sversion); SetVariable(pset.vars, "SERVER_VERSION_NUM", vbuf); diff --git a/src/bin/psql/describe.c b/src/bin/psql/describe.c index 46cddfb7f88e3..935de5ad1690b 100644 --- a/src/bin/psql/describe.c +++ b/src/bin/psql/describe.c @@ -26,8 +26,13 @@ #include "settings.h" #include "variables.h" -/* Flag to check if server is a Redshift Engine */ -#define IS_REDSHIFT strncmp(pset.sengine, "redshift", 8) == 0 +/* Flag to check if server is a Redshift Engine * / +const char *server_engine; +server_engine = PQparameterStatus(pset.db, "server_engine"); +#define IS_REDSHIFT ((strncmp(server_engine, "redshift", 8) == 0) ? 1 : 0) +#define IS_PIPELINEDB ((strncmp(server_engine, "pipelinedb", 10) == 0) ? 1 : 0) +*/ +#define IS_REDSHIFT 1 static bool describeOneTableDetails(const char *schemaname, const char *relationname, diff --git a/src/bin/psql/settings.h b/src/bin/psql/settings.h index 28fe2f241d55f..96338c31976a0 100644 --- a/src/bin/psql/settings.h +++ b/src/bin/psql/settings.h @@ -105,7 +105,6 @@ typedef struct _psqlSettings * loop */ bool cur_cmd_interactive; int sversion; /* backend server version */ - const char *sengine; /* backend server engine variant (if not postgres) */ const char *progname; /* in case you renamed psql */ char *inputfile; /* file being currently processed, if any */ uint64 lineno; /* also for error reporting */ diff --git a/src/bin/psql/tab-complete.c b/src/bin/psql/tab-complete.c index c426cfc834a9e..b173593289b6c 100644 --- a/src/bin/psql/tab-complete.c +++ b/src/bin/psql/tab-complete.c @@ -713,7 +713,7 @@ static const SchemaQuery Query_for_list_of_statistics = { " SELECT 'ZST' " #define Query_for_list_of_cursors \ -((strncmp(pset.sengine, "redshift", 8) == 0) ? \ +((strncmp(server_engine, "redshift", 8) == 0) ? \ " SELECT trim(name) FROM stv_active_cursors ORDER BY 1" : \ " SELECT trim(name) FROM pg_cursors ORDER BY 1" ) @@ -782,7 +782,7 @@ static const SchemaQuery Query_for_list_of_statistics = { " WHERE substring(name,1,%d)='%s'" #define Query_for_list_of_pids \ -((strncmp(pset.sengine, "redshift", 8) == 0) ? \ +((strncmp(server_engine, "redshift", 8) == 0) ? \ "SELECT procpid || ' (user->' || usename || '; database->' || datname || ')' FROM pg_stat_activity ORDER BY datname, usename" : \ "SELECT process || ' (user->' || usename || '; database->' || datname || ')' FROM pg_stat_activity ORDER BY datname, usename") @@ -1594,13 +1594,13 @@ psql_completion(const char *text, int start, int end) rl_completion_append_character = ' '; #endif -/* Flag to check if server is a Redshift Engine. +/* Flag to set server_engine appropriately Any change here should probably be replicated elsewhere since #define for various SQLs (in this script) employ there own string-compare */ -#define IS_REDSHIFT strncmp(pset.sengine, "redshift", 8) == 0 - -/* Flag to check if server is a PipelineDB Engine*/ -#define IS_PIPELINEDB strncmp(pset.sengine, "pipelinedb", 10) == 0 + const char *server_engine; + server_engine = PQparameterStatus(pset.db, "server_engine"); + #define IS_REDSHIFT ((strncmp(server_engine, "redshift", 8) == 0) ? 1 : 0) + #define IS_PIPELINEDB ((strncmp(server_engine, "pipelinedb", 10) == 0) ? 1 : 0) /* Clear a few things. */ completion_charp = NULL; From 5a0c5c821eb924d6165904e0a32a8d6396117fa9 Mon Sep 17 00:00:00 2001 From: Robins Tharakan Date: Wed, 13 Sep 2017 19:47:20 +1000 Subject: [PATCH 093/149] psql now supports Server Engine (type) as an internal variable. Currently it works with PostgreSQL / Amazon Redshift / PipelineDB. Also the psql prompt throws up client / server details like this 'psql (client-version:11devel, server-version:9.6.1, engine:postgres)'. --- src/bin/psql/command.c | 11 +++++------ src/bin/psql/describe.c | 10 +++------- src/bin/psql/settings.h | 1 + src/bin/psql/tab-complete.c | 12 +++++------- 4 files changed, 14 insertions(+), 20 deletions(-) diff --git a/src/bin/psql/command.c b/src/bin/psql/command.c index db3629c8999b0..1d21777ff253a 100644 --- a/src/bin/psql/command.c +++ b/src/bin/psql/command.c @@ -3115,11 +3115,9 @@ connection_warnings(bool in_startup) if (pset.sversion != client_ver) { const char *server_version; - const char *server_engine; /* Try to get full text form, might include "devel" etc */ server_version = PQparameterStatus(pset.db, "server_version"); - server_engine = PQparameterStatus(pset.db, "server_engine"); /* Otherwise fall back on pset.sversion */ if (!server_version) @@ -3129,8 +3127,8 @@ connection_warnings(bool in_startup) server_version = sverbuf; } - printf(_("%s (%s, server %s, engine %s)\n"), - pset.progname, PG_VERSION, server_version, server_engine); + printf(_("%s (client-version:%s, server-version:%s, engine:%s)\n"), + pset.progname, PG_VERSION, server_version, pset.sengine); } /* For version match, only print psql banner on startup. */ else if (in_startup) @@ -3219,7 +3217,7 @@ SyncVariables(void) char vbuf[32]; const char *server_version; const char *server_engine; - const char *guctype; + char *guctype; /* get stuff from connection */ pset.encoding = PQclientEncoding(pset.db); @@ -3240,6 +3238,8 @@ SyncVariables(void) server_engine = "postgres"; } + pset.sengine = server_engine; + if (guctype) free(guctype); @@ -3248,7 +3248,6 @@ SyncVariables(void) SetVariable(pset.vars, "HOST", PQhost(pset.db)); SetVariable(pset.vars, "PORT", PQport(pset.db)); SetVariable(pset.vars, "ENCODING", pg_encoding_to_char(pset.encoding)); - SetVariable(pset.vars, "ENGINE", PQdb(pset.db)); /* this bit should match connection_warnings(): */ /* Try to get full text form of version, might include "devel" etc */ diff --git a/src/bin/psql/describe.c b/src/bin/psql/describe.c index 935de5ad1690b..61b3a88934331 100644 --- a/src/bin/psql/describe.c +++ b/src/bin/psql/describe.c @@ -26,13 +26,9 @@ #include "settings.h" #include "variables.h" -/* Flag to check if server is a Redshift Engine * / -const char *server_engine; -server_engine = PQparameterStatus(pset.db, "server_engine"); -#define IS_REDSHIFT ((strncmp(server_engine, "redshift", 8) == 0) ? 1 : 0) -#define IS_PIPELINEDB ((strncmp(server_engine, "pipelinedb", 10) == 0) ? 1 : 0) -*/ -#define IS_REDSHIFT 1 +/* Flag to check if server is a Redshift Engine */ +#define IS_REDSHIFT (strncmp(pset.sengine, "redshift", 8) == 0) +#define IS_PIPELINEDB (strncmp(pset.sengine, "pipelinedb", 10) == 0) static bool describeOneTableDetails(const char *schemaname, const char *relationname, diff --git a/src/bin/psql/settings.h b/src/bin/psql/settings.h index 96338c31976a0..404267ce11847 100644 --- a/src/bin/psql/settings.h +++ b/src/bin/psql/settings.h @@ -105,6 +105,7 @@ typedef struct _psqlSettings * loop */ bool cur_cmd_interactive; int sversion; /* backend server version */ + const char *sengine; /* backend server engine */ const char *progname; /* in case you renamed psql */ char *inputfile; /* file being currently processed, if any */ uint64 lineno; /* also for error reporting */ diff --git a/src/bin/psql/tab-complete.c b/src/bin/psql/tab-complete.c index b173593289b6c..9e1b9f953f0a3 100644 --- a/src/bin/psql/tab-complete.c +++ b/src/bin/psql/tab-complete.c @@ -713,7 +713,7 @@ static const SchemaQuery Query_for_list_of_statistics = { " SELECT 'ZST' " #define Query_for_list_of_cursors \ -((strncmp(server_engine, "redshift", 8) == 0) ? \ +((strncmp(pset.sengine, "redshift", 8) == 0) ? \ " SELECT trim(name) FROM stv_active_cursors ORDER BY 1" : \ " SELECT trim(name) FROM pg_cursors ORDER BY 1" ) @@ -782,7 +782,7 @@ static const SchemaQuery Query_for_list_of_statistics = { " WHERE substring(name,1,%d)='%s'" #define Query_for_list_of_pids \ -((strncmp(server_engine, "redshift", 8) == 0) ? \ +((strncmp(pset.sengine, "redshift", 8) == 0) ? \ "SELECT procpid || ' (user->' || usename || '; database->' || datname || ')' FROM pg_stat_activity ORDER BY datname, usename" : \ "SELECT process || ' (user->' || usename || '; database->' || datname || ')' FROM pg_stat_activity ORDER BY datname, usename") @@ -1594,13 +1594,11 @@ psql_completion(const char *text, int start, int end) rl_completion_append_character = ' '; #endif -/* Flag to set server_engine appropriately +/* Macro to easily identify which Engine (type) are we speaking to Any change here should probably be replicated elsewhere since #define for various SQLs (in this script) employ there own string-compare */ - const char *server_engine; - server_engine = PQparameterStatus(pset.db, "server_engine"); - #define IS_REDSHIFT ((strncmp(server_engine, "redshift", 8) == 0) ? 1 : 0) - #define IS_PIPELINEDB ((strncmp(server_engine, "pipelinedb", 10) == 0) ? 1 : 0) + #define IS_REDSHIFT (strncmp(pset.sengine, "redshift", 8) == 0) + #define IS_PIPELINEDB (strncmp(pset.sengine, "pipelinedb", 10) == 0) /* Clear a few things. */ completion_charp = NULL; From 0e400aad934359df18d2af3fb89ad25937a9a294 Mon Sep 17 00:00:00 2001 From: Robins Tharakan Date: Wed, 13 Sep 2017 21:22:09 +1000 Subject: [PATCH 094/149] Minor. Bold NOTE --- src/bin/psql/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/bin/psql/README.md b/src/bin/psql/README.md index 8ab3434277199..92103eb638dc9 100644 --- a/src/bin/psql/README.md +++ b/src/bin/psql/README.md @@ -1,6 +1,6 @@ This documents the current state of psql's auto-complete support for various Postgres Variants. -NOTE: This branch is maintained as a personal effort and is not sponsored / supported by my Employer or any other third-party. This is not officially supported / endorsed by any Commercial interest in any way. +**NOTE**: This branch is maintained as a personal effort and is not sponsored / supported by my Employer or any other third-party. This is not officially supported / endorsed by any Commercial interest in any way. Postgres Forks | Support Level | Description :-------------:| :-----------: | :---------- From 95485cead473dd08b4f93d606bd95ff193ee66cd Mon Sep 17 00:00:00 2001 From: Robins Tharakan Date: Thu, 14 Sep 2017 22:34:37 +1000 Subject: [PATCH 095/149] Still WIP. Midway between getting this to work --- src/bin/psql/input.c | 9 ++++++++- src/bin/psql/nls.mk | 2 +- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/src/bin/psql/input.c b/src/bin/psql/input.c index 62f5f7738391e..0e8291d143e42 100644 --- a/src/bin/psql/input.c +++ b/src/bin/psql/input.c @@ -16,6 +16,7 @@ #include "input.h" #include "settings.h" #include "tab-complete.h" +#include "tab-complete-redshift.h" #include "common.h" #ifndef WIN32 @@ -354,7 +355,13 @@ initializeInput(int flags) useReadline = true; /* these two things must be done in this order: */ - initialize_readline(); + + if (strncmp(pset.sengine, "redshift", 8) == 0) + initialize_readline_redshift(); + else if (strncmp(pset.sengine, "pipelinedb", 10) == 0) + ;// initialize_readline_pipelinedb(); + else + initialize_readline(); rl_initialize(); useHistory = true; diff --git a/src/bin/psql/nls.mk b/src/bin/psql/nls.mk index e355f219b98f8..c78a25a06ef36 100644 --- a/src/bin/psql/nls.mk +++ b/src/bin/psql/nls.mk @@ -4,7 +4,7 @@ AVAIL_LANGUAGES = cs de es fr he it ja ko pl pt_BR ru sv zh_CN zh_TW GETTEXT_FILES = command.c common.c copy.c crosstabview.c help.c input.c large_obj.c \ mainloop.c psqlscanslash.c startup.c \ describe.c sql_help.h sql_help.c \ - tab-complete.c variables.c \ + tab-complete.c tab-complete-redshift.c variables.c \ ../../fe_utils/print.c ../../fe_utils/psqlscan.c \ ../../common/exec.c ../../common/fe_memutils.c ../../common/username.c \ ../../common/wait_error.c From a0c8e5a9cd12a98bf48839222901f5fd71e4b815 Mon Sep 17 00:00:00 2001 From: Robins Tharakan Date: Thu, 14 Sep 2017 22:35:38 +1000 Subject: [PATCH 096/149] First attempt at spinning off another tab-complete for redshift. Not working yet --- src/bin/psql/tab-complete-redshift.c | 4945 ++++++++++++++++++++++++++ src/bin/psql/tab-complete-redshift.h | 17 + 2 files changed, 4962 insertions(+) create mode 100644 src/bin/psql/tab-complete-redshift.c create mode 100644 src/bin/psql/tab-complete-redshift.h diff --git a/src/bin/psql/tab-complete-redshift.c b/src/bin/psql/tab-complete-redshift.c new file mode 100644 index 0000000000000..42fba9e3b4988 --- /dev/null +++ b/src/bin/psql/tab-complete-redshift.c @@ -0,0 +1,4945 @@ +/* + * psql - the PostgreSQL interactive terminal + * + * Copyright (c) 2000-2017, PostgreSQL Global Development Group + * + * src/bin/psql/tab-complete-redshift.c + */ + +/*---------------------------------------------------------------------- + * This file implements a somewhat more sophisticated readline "TAB + * completion" in psql. It is not intended to be AI, to replace + * learning SQL, or to relieve you from thinking about what you're + * doing. Also it does not always give you all the syntactically legal + * completions, only those that are the most common or the ones that + * the programmer felt most like implementing. + * + * CAVEAT: Tab completion causes queries to be sent to the backend. + * The number of tuples returned gets limited, in most default + * installations to 1000, but if you still don't like this prospect, + * you can turn off tab completion in your ~/.inputrc (or else + * ${INPUTRC}) file so: + * + * $if psql + * set disable-completion on + * $endif + * + * See `man 3 readline' or `info readline' for the full details. + * + * BUGS: + * - Quotes, parentheses, and other funny characters are not handled + * all that gracefully. + *---------------------------------------------------------------------- + */ + +#include "postgres_fe.h" +#include "tab-complete-redshift.h" +#include "input.h" + +/* If we don't have this, we might as well forget about the whole thing: */ +#ifdef USE_READLINE + +#include + +#include "catalog/pg_class.h" + +#include "libpq-fe.h" +#include "pqexpbuffer.h" +#include "common.h" +#include "settings.h" +#include "stringutils.h" + +#ifdef HAVE_RL_FILENAME_COMPLETION_FUNCTION +#define filename_completion_function rl_filename_completion_function +#else +/* missing in some header files */ +extern char *filename_completion_function(); +#endif + +#ifdef HAVE_RL_COMPLETION_MATCHES +#define completion_matches rl_completion_matches +#endif + +/* word break characters */ +#define WORD_BREAKS "\t\n@$><=;|&{() " + +/* + * Since readline doesn't let us pass any state through to the tab completion + * callback, we have to use this global variable to let get_previous_words() + * get at the previous lines of the current command. Ick. + */ +PQExpBuffer tab_completion_query_buf = NULL; + +/* + * This struct is used to define "schema queries", which are custom-built + * to obtain possibly-schema-qualified names of database objects. There is + * enough similarity in the structure that we don't want to repeat it each + * time. So we put the components of each query into this struct and + * assemble them with the common boilerplate in _complete_from_query(). + */ +typedef struct SchemaQuery +{ + /* + * Name of catalog or catalogs to be queried, with alias, eg. + * "pg_catalog.pg_class c". Note that "pg_namespace n" will be added. + */ + const char *catname; + + /* + * Selection condition --- only rows meeting this condition are candidates + * to display. If catname mentions multiple tables, include the necessary + * join condition here. For example, this might look like "c.relkind = " + * CppAsString2(RELKIND_RELATION). Write NULL (not an empty string) if + * not needed. + */ + const char *selcondition; + + /* + * Visibility condition --- which rows are visible without schema + * qualification? For example, "pg_catalog.pg_table_is_visible(c.oid)". + */ + const char *viscondition; + + /* + * Namespace --- name of field to join to pg_namespace.oid. For example, + * "c.relnamespace". + */ + const char *namespace; + + /* + * Result --- the appropriately-quoted name to return, in the case of an + * unqualified name. For example, "pg_catalog.quote_ident(c.relname)". + */ + const char *result; + + /* + * In some cases a different result must be used for qualified names. + * Enter that here, or write NULL if result can be used. + */ + const char *qualresult; +} SchemaQuery; + + +/* Store maximum number of records we want from database queries + * (implemented via SELECT ... LIMIT xx). + */ +static int completion_max_records; + +/* + * Communication variables set by COMPLETE_WITH_FOO macros and then used by + * the completion callback functions. Ugly but there is no better way. + */ +static const char *completion_charp; /* to pass a string */ +static const char *const *completion_charpp; /* to pass a list of strings */ +static const char *completion_info_charp; /* to pass a second string */ +static const char *completion_info_charp2; /* to pass a third string */ +static const SchemaQuery *completion_squery; /* to pass a SchemaQuery */ +static bool completion_case_sensitive; /* completion is case sensitive */ + +/* + * A few macros to ease typing. You can use these to complete the given + * string with + * 1) The results from a query you pass it. (Perhaps one of those below?) + * 2) The results from a schema query you pass it. + * 3) The items from a null-pointer-terminated list (with or without + * case-sensitive comparison; see also COMPLETE_WITH_LISTn, below). + * 4) A string constant. + * 5) The list of attributes of the given table (possibly schema-qualified). + * 6/ The list of arguments to the given function (possibly schema-qualified). + */ +#define COMPLETE_WITH_QUERY(query) \ +do { \ + completion_charp = query; \ + matches = completion_matches(text, complete_from_query); \ +} while (0) + +#define COMPLETE_WITH_SCHEMA_QUERY(query, addon) \ +do { \ + completion_squery = &(query); \ + completion_charp = addon; \ + matches = completion_matches(text, complete_from_schema_query); \ +} while (0) + +#define COMPLETE_WITH_LIST_CS(list) \ +do { \ + completion_charpp = list; \ + completion_case_sensitive = true; \ + matches = completion_matches(text, complete_from_list); \ +} while (0) + +#define COMPLETE_WITH_LIST(list) \ +do { \ + completion_charpp = list; \ + completion_case_sensitive = false; \ + matches = completion_matches(text, complete_from_list); \ +} while (0) + +#define COMPLETE_WITH_CONST(string) \ +do { \ + completion_charp = string; \ + completion_case_sensitive = false; \ + matches = completion_matches(text, complete_from_const); \ +} while (0) + +#define COMPLETE_WITH_ATTR(relation, addon) \ +do { \ + char *_completion_schema; \ + char *_completion_table; \ +\ + _completion_schema = strtokx(relation, " \t\n\r", ".", "\"", 0, \ + false, false, pset.encoding); \ + (void) strtokx(NULL, " \t\n\r", ".", "\"", 0, \ + false, false, pset.encoding); \ + _completion_table = strtokx(NULL, " \t\n\r", ".", "\"", 0, \ + false, false, pset.encoding); \ + if (_completion_table == NULL) \ + { \ + completion_charp = Query_for_list_of_attributes addon; \ + completion_info_charp = relation; \ + } \ + else \ + { \ + completion_charp = Query_for_list_of_attributes_with_schema addon; \ + completion_info_charp = _completion_table; \ + completion_info_charp2 = _completion_schema; \ + } \ + matches = completion_matches(text, complete_from_query); \ +} while (0) + +#define COMPLETE_WITH_ENUM_VALUE(type) \ +do { \ + char *_completion_schema; \ + char *_completion_type; \ +\ + _completion_schema = strtokx(type, " \t\n\r", ".", "\"", 0, \ + false, false, pset.encoding); \ + (void) strtokx(NULL, " \t\n\r", ".", "\"", 0, \ + false, false, pset.encoding); \ + _completion_type = strtokx(NULL, " \t\n\r", ".", "\"", 0, \ + false, false, pset.encoding); \ + if (_completion_type == NULL)\ + { \ + completion_charp = Query_for_list_of_enum_values; \ + completion_info_charp = type; \ + } \ + else \ + { \ + completion_charp = Query_for_list_of_enum_values_with_schema; \ + completion_info_charp = _completion_type; \ + completion_info_charp2 = _completion_schema; \ + } \ + matches = completion_matches(text, complete_from_query); \ +} while (0) + +#define COMPLETE_WITH_FUNCTION_ARG(function) \ +do { \ + char *_completion_schema; \ + char *_completion_function; \ +\ + _completion_schema = strtokx(function, " \t\n\r", ".", "\"", 0, \ + false, false, pset.encoding); \ + (void) strtokx(NULL, " \t\n\r", ".", "\"", 0, \ + false, false, pset.encoding); \ + _completion_function = strtokx(NULL, " \t\n\r", ".", "\"", 0, \ + false, false, pset.encoding); \ + if (_completion_function == NULL) \ + { \ + completion_charp = Query_for_list_of_arguments; \ + completion_info_charp = function; \ + } \ + else \ + { \ + completion_charp = Query_for_list_of_arguments_with_schema; \ + completion_info_charp = _completion_function; \ + completion_info_charp2 = _completion_schema; \ + } \ + matches = completion_matches(text, complete_from_query); \ +} while (0) + +/* + * These macros simplify use of COMPLETE_WITH_LIST for short, fixed lists. + * There is no COMPLETE_WITH_LIST1; use COMPLETE_WITH_CONST for that case. + */ +#define COMPLETE_WITH_LIST2(s1, s2) \ +do { \ + static const char *const list[] = { s1, s2, NULL }; \ + COMPLETE_WITH_LIST(list); \ +} while (0) + +#define COMPLETE_WITH_LIST3(s1, s2, s3) \ +do { \ + static const char *const list[] = { s1, s2, s3, NULL }; \ + COMPLETE_WITH_LIST(list); \ +} while (0) + +#define COMPLETE_WITH_LIST4(s1, s2, s3, s4) \ +do { \ + static const char *const list[] = { s1, s2, s3, s4, NULL }; \ + COMPLETE_WITH_LIST(list); \ +} while (0) + +#define COMPLETE_WITH_LIST5(s1, s2, s3, s4, s5) \ +do { \ + static const char *const list[] = { s1, s2, s3, s4, s5, NULL }; \ + COMPLETE_WITH_LIST(list); \ +} while (0) + +#define COMPLETE_WITH_LIST6(s1, s2, s3, s4, s5, s6) \ +do { \ + static const char *const list[] = { s1, s2, s3, s4, s5, s6, NULL }; \ + COMPLETE_WITH_LIST(list); \ +} while (0) + +#define COMPLETE_WITH_LIST7(s1, s2, s3, s4, s5, s6, s7) \ +do { \ + static const char *const list[] = { s1, s2, s3, s4, s5, s6, s7, NULL }; \ + COMPLETE_WITH_LIST(list); \ +} while (0) + +#define COMPLETE_WITH_LIST8(s1, s2, s3, s4, s5, s6, s7, s8) \ +do { \ + static const char *const list[] = { s1, s2, s3, s4, s5, s6, s7, s8, NULL }; \ + COMPLETE_WITH_LIST(list); \ +} while (0) + +#define COMPLETE_WITH_LIST9(s1, s2, s3, s4, s5, s6, s7, s8, s9) \ +do { \ + static const char *const list[] = { s1, s2, s3, s4, s5, s6, s7, s8, s9, NULL }; \ + COMPLETE_WITH_LIST(list); \ +} while (0) + +#define COMPLETE_WITH_LIST10(s1, s2, s3, s4, s5, s6, s7, s8, s9, s10) \ +do { \ + static const char *const list[] = { s1, s2, s3, s4, s5, s6, s7, s8, s9, s10, NULL }; \ + COMPLETE_WITH_LIST(list); \ +} while (0) + +#define COMPLETE_WITH_LIST11(s1, s2, s3, s4, s5, s6, s7, s8, s9, s10, s11) \ +do { \ + static const char *const list[] = { s1, s2, s3, s4, s5, s6, s7, s8, s9, s10, s11, NULL }; \ + COMPLETE_WITH_LIST(list); \ +} while (0) + +#define COMPLETE_WITH_LIST12(s1, s2, s3, s4, s5, s6, s7, s8, s9, s10, s11, s12) \ +do { \ + static const char *const list[] = { s1, s2, s3, s4, s5, s6, s7, s8, s9, s10, s11, s12, NULL }; \ + COMPLETE_WITH_LIST(list); \ +} while (0) + +/* + * Likewise for COMPLETE_WITH_LIST_CS. + */ +#define COMPLETE_WITH_LIST_CS2(s1, s2) \ +do { \ + static const char *const list[] = { s1, s2, NULL }; \ + COMPLETE_WITH_LIST_CS(list); \ +} while (0) + +#define COMPLETE_WITH_LIST_CS3(s1, s2, s3) \ +do { \ + static const char *const list[] = { s1, s2, s3, NULL }; \ + COMPLETE_WITH_LIST_CS(list); \ +} while (0) + +#define COMPLETE_WITH_LIST_CS4(s1, s2, s3, s4) \ +do { \ + static const char *const list[] = { s1, s2, s3, s4, NULL }; \ + COMPLETE_WITH_LIST_CS(list); \ +} while (0) + +#define COMPLETE_WITH_LIST_CS5(s1, s2, s3, s4, s5) \ +do { \ + static const char *const list[] = { s1, s2, s3, s4, s5, NULL }; \ + COMPLETE_WITH_LIST_CS(list); \ +} while (0) + +/* + * Assembly instructions for schema queries + */ + +static const SchemaQuery Query_for_list_of_aggregates = { + /* catname */ + "pg_catalog.pg_proc p", + /* selcondition */ + "p.proisagg", + /* viscondition */ + "pg_catalog.pg_function_is_visible(p.oid)", + /* namespace */ + "p.pronamespace", + /* result */ + "pg_catalog.quote_ident(p.proname)", + /* qualresult */ + NULL +}; + +static const SchemaQuery Query_for_list_of_datatypes = { + /* catname */ + "pg_catalog.pg_type t", + /* selcondition --- ignore table rowtypes and array types */ + "(t.typrelid = 0 " + " OR (SELECT c.relkind = " CppAsString2(RELKIND_COMPOSITE_TYPE) + " FROM pg_catalog.pg_class c WHERE c.oid = t.typrelid)) " + "AND t.typname !~ '^_'", + /* viscondition */ + "pg_catalog.pg_type_is_visible(t.oid)", + /* namespace */ + "t.typnamespace", + /* result */ + "pg_catalog.format_type(t.oid, NULL)", + /* qualresult */ + "pg_catalog.quote_ident(t.typname)" +}; + +static const SchemaQuery Query_for_list_of_domains = { + /* catname */ + "pg_catalog.pg_type t", + /* selcondition */ + "t.typtype = 'd'", + /* viscondition */ + "pg_catalog.pg_type_is_visible(t.oid)", + /* namespace */ + "t.typnamespace", + /* result */ + "pg_catalog.quote_ident(t.typname)", + /* qualresult */ + NULL +}; + +static const SchemaQuery Query_for_list_of_functions = { + /* catname */ + "pg_catalog.pg_proc p", + /* selcondition */ + NULL, + /* viscondition */ + "pg_catalog.pg_function_is_visible(p.oid)", + /* namespace */ + "p.pronamespace", + /* result */ + "pg_catalog.quote_ident(p.proname)", + /* qualresult */ + NULL +}; + +static const SchemaQuery Query_for_list_of_indexes = { + /* catname */ + "pg_catalog.pg_class c", + /* selcondition */ + "c.relkind IN (" CppAsString2(RELKIND_INDEX) ")", + /* viscondition */ + "pg_catalog.pg_table_is_visible(c.oid)", + /* namespace */ + "c.relnamespace", + /* result */ + "pg_catalog.quote_ident(c.relname)", + /* qualresult */ + NULL +}; + +static const SchemaQuery Query_for_list_of_sequences = { + /* catname */ + "pg_catalog.pg_class c", + /* selcondition */ + "c.relkind IN (" CppAsString2(RELKIND_SEQUENCE) ")", + /* viscondition */ + "pg_catalog.pg_table_is_visible(c.oid)", + /* namespace */ + "c.relnamespace", + /* result */ + "pg_catalog.quote_ident(c.relname)", + /* qualresult */ + NULL +}; + +static const SchemaQuery Query_for_list_of_foreign_tables = { + /* catname */ + "pg_catalog.pg_class c", + /* selcondition */ + "c.relkind IN (" CppAsString2(RELKIND_FOREIGN_TABLE) ")", + /* viscondition */ + "pg_catalog.pg_table_is_visible(c.oid)", + /* namespace */ + "c.relnamespace", + /* result */ + "pg_catalog.quote_ident(c.relname)", + /* qualresult */ + NULL +}; + +static const SchemaQuery Query_for_list_of_tables = { + /* catname */ + "pg_catalog.pg_class c", + /* selcondition */ + "c.relkind IN (" CppAsString2(RELKIND_RELATION) ", " + CppAsString2(RELKIND_PARTITIONED_TABLE) ")", + /* viscondition */ + "pg_catalog.pg_table_is_visible(c.oid)", + /* namespace */ + "c.relnamespace", + /* result */ + "pg_catalog.quote_ident(c.relname)", + /* qualresult */ + NULL +}; + +static const SchemaQuery Query_for_list_of_partitioned_tables = { + /* catname */ + "pg_catalog.pg_class c", + /* selcondition */ + "c.relkind IN (" CppAsString2(RELKIND_PARTITIONED_TABLE) ")", + /* viscondition */ + "pg_catalog.pg_table_is_visible(c.oid)", + /* namespace */ + "c.relnamespace", + /* result */ + "pg_catalog.quote_ident(c.relname)", + /* qualresult */ + NULL +}; + +static const SchemaQuery Query_for_list_of_constraints_with_schema = { + /* catname */ + "pg_catalog.pg_constraint c", + /* selcondition */ + "c.conrelid <> 0", + /* viscondition */ + "true", /* there is no pg_constraint_is_visible */ + /* namespace */ + "c.connamespace", + /* result */ + "pg_catalog.quote_ident(c.conname)", + /* qualresult */ + NULL +}; + +/* Relations supporting INSERT, UPDATE or DELETE */ +static const SchemaQuery Query_for_list_of_updatables = { + /* catname */ + "pg_catalog.pg_class c", + /* selcondition */ + "c.relkind IN (" CppAsString2(RELKIND_RELATION) ", " + CppAsString2(RELKIND_FOREIGN_TABLE) ", " + CppAsString2(RELKIND_VIEW) ", " + CppAsString2(RELKIND_PARTITIONED_TABLE) ")", + /* viscondition */ + "pg_catalog.pg_table_is_visible(c.oid)", + /* namespace */ + "c.relnamespace", + /* result */ + "pg_catalog.quote_ident(c.relname)", + /* qualresult */ + NULL +}; + +static const SchemaQuery Query_for_list_of_relations = { + /* catname */ + "pg_catalog.pg_class c", + /* selcondition */ + NULL, + /* viscondition */ + "pg_catalog.pg_table_is_visible(c.oid)", + /* namespace */ + "c.relnamespace", + /* result */ + "pg_catalog.quote_ident(c.relname)", + /* qualresult */ + NULL +}; + +static const SchemaQuery Query_for_list_of_tsvmf = { + /* catname */ + "pg_catalog.pg_class c", + /* selcondition */ + "c.relkind IN (" CppAsString2(RELKIND_RELATION) ", " + CppAsString2(RELKIND_SEQUENCE) ", " + CppAsString2(RELKIND_VIEW) ", " + CppAsString2(RELKIND_MATVIEW) ", " + CppAsString2(RELKIND_FOREIGN_TABLE) ", " + CppAsString2(RELKIND_PARTITIONED_TABLE) ")", + /* viscondition */ + "pg_catalog.pg_table_is_visible(c.oid)", + /* namespace */ + "c.relnamespace", + /* result */ + "pg_catalog.quote_ident(c.relname)", + /* qualresult */ + NULL +}; + +static const SchemaQuery Query_for_list_of_tmf = { + /* catname */ + "pg_catalog.pg_class c", + /* selcondition */ + "c.relkind IN (" CppAsString2(RELKIND_RELATION) ", " + CppAsString2(RELKIND_MATVIEW) ", " + CppAsString2(RELKIND_FOREIGN_TABLE) ")", + /* viscondition */ + "pg_catalog.pg_table_is_visible(c.oid)", + /* namespace */ + "c.relnamespace", + /* result */ + "pg_catalog.quote_ident(c.relname)", + /* qualresult */ + NULL +}; + +static const SchemaQuery Query_for_list_of_tm = { + /* catname */ + "pg_catalog.pg_class c", + /* selcondition */ + "c.relkind IN (" CppAsString2(RELKIND_RELATION) ", " + CppAsString2(RELKIND_MATVIEW) ")", + /* viscondition */ + "pg_catalog.pg_table_is_visible(c.oid)", + /* namespace */ + "c.relnamespace", + /* result */ + "pg_catalog.quote_ident(c.relname)", + /* qualresult */ + NULL +}; + +static const SchemaQuery Query_for_list_of_views = { + /* catname */ + "pg_catalog.pg_class c", + /* selcondition */ + "c.relkind IN (" CppAsString2(RELKIND_VIEW) ")", + /* viscondition */ + "pg_catalog.pg_table_is_visible(c.oid)", + /* namespace */ + "c.relnamespace", + /* result */ + "pg_catalog.quote_ident(c.relname)", + /* qualresult */ + NULL +}; + +static const SchemaQuery Query_for_list_of_matviews = { + /* catname */ + "pg_catalog.pg_class c", + /* selcondition */ + "c.relkind IN (" CppAsString2(RELKIND_MATVIEW) ")", + /* viscondition */ + "pg_catalog.pg_table_is_visible(c.oid)", + /* namespace */ + "c.relnamespace", + /* result */ + "pg_catalog.quote_ident(c.relname)", + /* qualresult */ + NULL +}; + +static const SchemaQuery Query_for_list_of_statistics = { + /* catname */ + "pg_catalog.pg_statistic_ext s", + /* selcondition */ + NULL, + /* viscondition */ + "pg_catalog.pg_statistics_obj_is_visible(s.oid)", + /* namespace */ + "s.stxnamespace", + /* result */ + "pg_catalog.quote_ident(s.stxname)", + /* qualresult */ + NULL +}; + + +/* + * Queries to get lists of names of various kinds of things, possibly + * restricted to names matching a partially entered name. In these queries, + * the first %s will be replaced by the text entered so far (suitably escaped + * to become a SQL literal string). %d will be replaced by the length of the + * string (in unescaped form). A second and third %s, if present, will be + * replaced by a suitably-escaped version of the string provided in + * completion_info_charp. A fourth and fifth %s are similarly replaced by + * completion_info_charp2. + * + * Beware that the allowed sequences of %s and %d are determined by + * _complete_from_query(). + */ + +#define Query_for_list_of_attributes \ +"SELECT pg_catalog.quote_ident(attname) "\ +" FROM pg_catalog.pg_attribute a, pg_catalog.pg_class c "\ +" WHERE c.oid = a.attrelid "\ +" AND a.attnum > 0 "\ +" AND NOT a.attisdropped "\ +" AND substring(pg_catalog.quote_ident(attname),1,%d)='%s' "\ +" AND (pg_catalog.quote_ident(relname)='%s' "\ +" OR '\"' || relname || '\"'='%s') "\ +" AND pg_catalog.pg_table_is_visible(c.oid)" + +#define Query_for_list_of_attributes_with_schema \ +"SELECT pg_catalog.quote_ident(attname) "\ +" FROM pg_catalog.pg_attribute a, pg_catalog.pg_class c, pg_catalog.pg_namespace n "\ +" WHERE c.oid = a.attrelid "\ +" AND n.oid = c.relnamespace "\ +" AND a.attnum > 0 "\ +" AND NOT a.attisdropped "\ +" AND substring(pg_catalog.quote_ident(attname),1,%d)='%s' "\ +" AND (pg_catalog.quote_ident(relname)='%s' "\ +" OR '\"' || relname || '\"' ='%s') "\ +" AND (pg_catalog.quote_ident(nspname)='%s' "\ +" OR '\"' || nspname || '\"' ='%s') " + +#define Query_for_list_of_aws_regions \ +"SELECT '''ap-northeast-1''' UNION ALL "\ +"SELECT '''ap-northeast-2''' UNION ALL "\ +"SELECT '''ap-south-1''' UNION ALL "\ +"SELECT '''ap-southeast-1''' UNION ALL "\ +"SELECT '''ap-southeast-2''' UNION ALL "\ +"SELECT '''ca-central-1''' UNION ALL "\ +"SELECT '''eu-central-1''' UNION ALL "\ +"SELECT '''eu-west-1''' UNION ALL "\ +"SELECT '''eu-west-2''' UNION ALL "\ +"SELECT '''sa-east-1''' UNION ALL "\ +"SELECT '''us-east-1''' UNION ALL "\ +"SELECT '''us-east-2''' UNION ALL "\ +"SELECT '''us-west-1''' UNION ALL "\ +"SELECT '''us-west-2''' " + +#define Query_for_list_of_column_compressions \ +" SELECT 'BYTEDICT' UNION ALL "\ +" SELECT 'DELTA' UNION ALL "\ +" SELECT 'DELTA32K' UNION ALL "\ +" SELECT 'LZO' UNION ALL "\ +" SELECT 'MOSTLY8' UNION ALL "\ +" SELECT 'MOSTLY16' UNION ALL "\ +" SELECT 'MOSTLY32' UNION ALL "\ +" SELECT 'RAW' UNION ALL "\ +" SELECT 'RUNLENGTH' UNION ALL "\ +" SELECT 'TEXT255' UNION ALL "\ +" SELECT 'TEXT32K' UNION ALL "\ +" SELECT 'ZST' " + +#define Query_for_list_of_cursors \ +((strncmp(pset.sengine, "redshift", 8) == 0) ? \ + " SELECT trim(name) FROM stv_active_cursors ORDER BY 1" : \ + " SELECT trim(name) FROM pg_cursors ORDER BY 1" ) + +#define Query_for_list_of_enum_values \ +"SELECT pg_catalog.quote_literal(enumlabel) "\ +" FROM pg_catalog.pg_enum e, pg_catalog.pg_type t "\ +" WHERE t.oid = e.enumtypid "\ +" AND substring(pg_catalog.quote_literal(enumlabel),1,%d)='%s' "\ +" AND (pg_catalog.quote_ident(typname)='%s' "\ +" OR '\"' || typname || '\"'='%s') "\ +" AND pg_catalog.pg_type_is_visible(t.oid)" + +#define Query_for_list_of_enum_values_with_schema \ +"SELECT pg_catalog.quote_literal(enumlabel) "\ +" FROM pg_catalog.pg_enum e, pg_catalog.pg_type t, pg_catalog.pg_namespace n "\ +" WHERE t.oid = e.enumtypid "\ +" AND n.oid = t.typnamespace "\ +" AND substring(pg_catalog.quote_literal(enumlabel),1,%d)='%s' "\ +" AND (pg_catalog.quote_ident(typname)='%s' "\ +" OR '\"' || typname || '\"'='%s') "\ +" AND (pg_catalog.quote_ident(nspname)='%s' "\ +" OR '\"' || nspname || '\"' ='%s') " + +#define Query_for_list_of_template_databases \ +"SELECT pg_catalog.quote_ident(d.datname) "\ +" FROM pg_catalog.pg_database d "\ +" WHERE substring(pg_catalog.quote_ident(d.datname),1,%d)='%s' "\ +" AND (d.datistemplate OR pg_catalog.pg_has_role(d.datdba, 'USAGE'))" + +#define Query_for_list_of_databases \ +"SELECT pg_catalog.quote_ident(datname) FROM pg_catalog.pg_database "\ +" WHERE substring(pg_catalog.quote_ident(datname),1,%d)='%s'" + +#define Query_for_list_of_tablespaces \ +"SELECT pg_catalog.quote_ident(spcname) FROM pg_catalog.pg_tablespace "\ +" WHERE substring(pg_catalog.quote_ident(spcname),1,%d)='%s'" + +#define Query_for_list_of_encodings \ +" SELECT DISTINCT pg_catalog.pg_encoding_to_char(conforencoding) "\ +" FROM pg_catalog.pg_conversion "\ +" WHERE substring(pg_catalog.pg_encoding_to_char(conforencoding),1,%d)=UPPER('%s')" + +#define Query_for_list_of_languages \ +"SELECT pg_catalog.quote_ident(lanname) "\ +" FROM pg_catalog.pg_language "\ +" WHERE lanname != 'internal' "\ +" AND substring(pg_catalog.quote_ident(lanname),1,%d)='%s'" + +#define Query_for_list_of_schemas \ +"SELECT pg_catalog.quote_ident(nspname) FROM pg_catalog.pg_namespace "\ +" WHERE substring(pg_catalog.quote_ident(nspname),1,%d)='%s'" + +#define Query_for_list_of_external_schemas \ +"SELECT pg_catalog.quote_ident(schemaname) FROM SVV_EXTERNAL_SCHEMAS "\ +" WHERE substring(pg_catalog.quote_ident(schemaname),1,%d)='%s'" + +#define Query_for_list_of_external_tables \ +"SELECT pg_catalog.quote_ident(tablename) FROM SVV_EXTERNAL_TABLES "\ +" WHERE substring(pg_catalog.quote_ident(tablename),1,%d)='%s'" + +#define Query_for_list_of_alter_system_set_vars \ +"SELECT name FROM "\ +" (SELECT pg_catalog.lower(name) AS name FROM pg_catalog.pg_settings "\ +" WHERE context != 'internal' "\ +" UNION ALL SELECT 'all') ss "\ +" WHERE substring(name,1,%d)='%s'" + +#define Query_for_list_of_pids \ +((strncmp(pset.sengine, "redshift", 8) == 0) ? \ + "SELECT procpid || ' (user->' || usename || '; database->' || datname || ')' FROM pg_stat_activity ORDER BY datname, usename" : \ + "SELECT process || ' (user->' || usename || '; database->' || datname || ')' FROM pg_stat_activity ORDER BY datname, usename") + +#define Query_for_list_of_set_vars \ +"SELECT name FROM "\ +" (SELECT pg_catalog.lower(name) AS name FROM pg_catalog.pg_settings "\ +" WHERE context IN ('user', 'superuser') "\ +" UNION ALL SELECT 'constraints' "\ +" UNION ALL SELECT 'transaction' "\ +" UNION ALL SELECT 'session' "\ +" UNION ALL SELECT 'role' "\ +" UNION ALL SELECT 'tablespace' "\ +" UNION ALL SELECT 'all') ss "\ +" WHERE substring(name,1,%d)='%s'" + +#define Query_for_list_of_show_vars \ +"SELECT name FROM "\ +" (SELECT pg_catalog.lower(name) AS name FROM pg_catalog.pg_settings "\ +" UNION ALL SELECT 'session authorization' "\ +" UNION ALL SELECT 'all') ss "\ +" WHERE substring(name,1,%d)='%s'" + +#define Query_for_list_of_roles \ +" SELECT pg_catalog.quote_ident(rolname) "\ +" FROM pg_catalog.pg_roles "\ +" WHERE substring(pg_catalog.quote_ident(rolname),1,%d)='%s'" + +#define Query_for_list_of_users \ +" SELECT pg_catalog.quote_ident(usename) "\ +" FROM pg_catalog.pg_user "\ +" WHERE substring(pg_catalog.quote_ident(usename),1,%d)='%s'" + +#define Query_for_list_of_grant_roles \ +" SELECT pg_catalog.quote_ident(rolname) "\ +" FROM pg_catalog.pg_roles "\ +" WHERE substring(pg_catalog.quote_ident(rolname),1,%d)='%s'"\ +" UNION ALL SELECT 'PUBLIC'"\ +" UNION ALL SELECT 'CURRENT_USER'"\ +" UNION ALL SELECT 'SESSION_USER'" + +/* the silly-looking length condition is just to eat up the current word */ +#define Query_for_table_owning_index \ +"SELECT pg_catalog.quote_ident(c1.relname) "\ +" FROM pg_catalog.pg_class c1, pg_catalog.pg_class c2, pg_catalog.pg_index i"\ +" WHERE c1.oid=i.indrelid and i.indexrelid=c2.oid"\ +" and (%d = pg_catalog.length('%s'))"\ +" and pg_catalog.quote_ident(c2.relname)='%s'"\ +" and pg_catalog.pg_table_is_visible(c2.oid)" + +/* the silly-looking length condition is just to eat up the current word */ +#define Query_for_index_of_table \ +"SELECT pg_catalog.quote_ident(c2.relname) "\ +" FROM pg_catalog.pg_class c1, pg_catalog.pg_class c2, pg_catalog.pg_index i"\ +" WHERE c1.oid=i.indrelid and i.indexrelid=c2.oid"\ +" and (%d = pg_catalog.length('%s'))"\ +" and pg_catalog.quote_ident(c1.relname)='%s'"\ +" and pg_catalog.pg_table_is_visible(c2.oid)" + +/* the silly-looking length condition is just to eat up the current word */ +#define Query_for_constraint_of_table \ +"SELECT pg_catalog.quote_ident(conname) "\ +" FROM pg_catalog.pg_class c1, pg_catalog.pg_constraint con "\ +" WHERE c1.oid=conrelid and (%d = pg_catalog.length('%s'))"\ +" and pg_catalog.quote_ident(c1.relname)='%s'"\ +" and pg_catalog.pg_table_is_visible(c1.oid)" + +#define Query_for_all_table_constraints \ +"SELECT pg_catalog.quote_ident(conname) "\ +" FROM pg_catalog.pg_constraint c "\ +" WHERE c.conrelid <> 0 " + +/* the silly-looking length condition is just to eat up the current word */ +#define Query_for_constraint_of_type \ +"SELECT pg_catalog.quote_ident(conname) "\ +" FROM pg_catalog.pg_type t, pg_catalog.pg_constraint con "\ +" WHERE t.oid=contypid and (%d = pg_catalog.length('%s'))"\ +" and pg_catalog.quote_ident(t.typname)='%s'"\ +" and pg_catalog.pg_type_is_visible(t.oid)" + +/* the silly-looking length condition is just to eat up the current word */ +#define Query_for_list_of_tables_for_constraint \ +"SELECT pg_catalog.quote_ident(relname) "\ +" FROM pg_catalog.pg_class"\ +" WHERE (%d = pg_catalog.length('%s'))"\ +" AND oid IN "\ +" (SELECT conrelid FROM pg_catalog.pg_constraint "\ +" WHERE pg_catalog.quote_ident(conname)='%s')" + +/* the silly-looking length condition is just to eat up the current word */ +#define Query_for_rule_of_table \ +"SELECT pg_catalog.quote_ident(rulename) "\ +" FROM pg_catalog.pg_class c1, pg_catalog.pg_rewrite "\ +" WHERE c1.oid=ev_class and (%d = pg_catalog.length('%s'))"\ +" and pg_catalog.quote_ident(c1.relname)='%s'"\ +" and pg_catalog.pg_table_is_visible(c1.oid)" + +/* the silly-looking length condition is just to eat up the current word */ +#define Query_for_list_of_tables_for_rule \ +"SELECT pg_catalog.quote_ident(relname) "\ +" FROM pg_catalog.pg_class"\ +" WHERE (%d = pg_catalog.length('%s'))"\ +" AND oid IN "\ +" (SELECT ev_class FROM pg_catalog.pg_rewrite "\ +" WHERE pg_catalog.quote_ident(rulename)='%s')" + +/* the silly-looking length condition is just to eat up the current word */ +#define Query_for_trigger_of_table \ +"SELECT pg_catalog.quote_ident(tgname) "\ +" FROM pg_catalog.pg_class c1, pg_catalog.pg_trigger "\ +" WHERE c1.oid=tgrelid and (%d = pg_catalog.length('%s'))"\ +" and pg_catalog.quote_ident(c1.relname)='%s'"\ +" and pg_catalog.pg_table_is_visible(c1.oid)"\ +" and not tgisinternal" + +/* the silly-looking length condition is just to eat up the current word */ +#define Query_for_list_of_tables_for_trigger \ +"SELECT pg_catalog.quote_ident(relname) "\ +" FROM pg_catalog.pg_class"\ +" WHERE (%d = pg_catalog.length('%s'))"\ +" AND oid IN "\ +" (SELECT tgrelid FROM pg_catalog.pg_trigger "\ +" WHERE pg_catalog.quote_ident(tgname)='%s')" + +#define Query_for_list_of_ts_configurations \ +"SELECT pg_catalog.quote_ident(cfgname) FROM pg_catalog.pg_ts_config "\ +" WHERE substring(pg_catalog.quote_ident(cfgname),1,%d)='%s'" + +#define Query_for_list_of_ts_dictionaries \ +"SELECT pg_catalog.quote_ident(dictname) FROM pg_catalog.pg_ts_dict "\ +" WHERE substring(pg_catalog.quote_ident(dictname),1,%d)='%s'" + +#define Query_for_list_of_ts_parsers \ +"SELECT pg_catalog.quote_ident(prsname) FROM pg_catalog.pg_ts_parser "\ +" WHERE substring(pg_catalog.quote_ident(prsname),1,%d)='%s'" + +#define Query_for_list_of_ts_templates \ +"SELECT pg_catalog.quote_ident(tmplname) FROM pg_catalog.pg_ts_template "\ +" WHERE substring(pg_catalog.quote_ident(tmplname),1,%d)='%s'" + +#define Query_for_list_of_fdws \ +" SELECT pg_catalog.quote_ident(fdwname) "\ +" FROM pg_catalog.pg_foreign_data_wrapper "\ +" WHERE substring(pg_catalog.quote_ident(fdwname),1,%d)='%s'" + +#define Query_for_list_of_servers \ +" SELECT pg_catalog.quote_ident(srvname) "\ +" FROM pg_catalog.pg_foreign_server "\ +" WHERE substring(pg_catalog.quote_ident(srvname),1,%d)='%s'" + +#define Query_for_list_of_user_mappings \ +" SELECT pg_catalog.quote_ident(usename) "\ +" FROM pg_catalog.pg_user_mappings "\ +" WHERE substring(pg_catalog.quote_ident(usename),1,%d)='%s'" + +#define Query_for_list_of_access_methods \ +" SELECT pg_catalog.quote_ident(amname) "\ +" FROM pg_catalog.pg_am "\ +" WHERE substring(pg_catalog.quote_ident(amname),1,%d)='%s'" + +#define Query_for_list_of_libraries \ +" SELECT pg_catalog.quote_ident(name) "\ +" FROM pg_catalog.pg_library "\ +" WHERE substring(pg_catalog.quote_ident(name),1,%d)='%s'" + +#define Query_for_list_of_publications \ +" SELECT pg_catalog.quote_ident(pubname) "\ +" FROM pg_catalog.pg_publication "\ +" WHERE substring(pg_catalog.quote_ident(pubname),1,%d)='%s'" + +#define Query_for_list_of_subscriptions \ +" SELECT pg_catalog.quote_ident(s.subname) "\ +" FROM pg_catalog.pg_subscription s, pg_catalog.pg_database d "\ +" WHERE substring(pg_catalog.quote_ident(s.subname),1,%d)='%s' "\ +" AND d.datname = pg_catalog.current_database() "\ +" AND s.subdbid = d.oid" + +/* the silly-looking length condition is just to eat up the current word */ +#define Query_for_list_of_arguments \ +"SELECT pg_catalog.oidvectortypes(proargtypes)||')' "\ +" FROM pg_catalog.pg_proc "\ +" WHERE (%d = pg_catalog.length('%s'))"\ +" AND (pg_catalog.quote_ident(proname)='%s'"\ +" OR '\"' || proname || '\"'='%s') "\ +" AND (pg_catalog.pg_function_is_visible(pg_proc.oid))" + +/* the silly-looking length condition is just to eat up the current word */ +#define Query_for_list_of_arguments_with_schema \ +"SELECT pg_catalog.oidvectortypes(proargtypes)||')' "\ +" FROM pg_catalog.pg_proc p, pg_catalog.pg_namespace n "\ +" WHERE (%d = pg_catalog.length('%s'))"\ +" AND n.oid = p.pronamespace "\ +" AND (pg_catalog.quote_ident(proname)='%s' "\ +" OR '\"' || proname || '\"' ='%s') "\ +" AND (pg_catalog.quote_ident(nspname)='%s' "\ +" OR '\"' || nspname || '\"' ='%s') " + +#define Query_for_list_of_extensions \ +" SELECT pg_catalog.quote_ident(extname) "\ +" FROM pg_catalog.pg_extension "\ +" WHERE substring(pg_catalog.quote_ident(extname),1,%d)='%s'" + +#define Query_for_list_of_available_extensions \ +" SELECT pg_catalog.quote_ident(name) "\ +" FROM pg_catalog.pg_available_extensions "\ +" WHERE substring(pg_catalog.quote_ident(name),1,%d)='%s' AND installed_version IS NULL" + +/* the silly-looking length condition is just to eat up the current word */ +#define Query_for_list_of_available_extension_versions \ +" SELECT pg_catalog.quote_ident(version) "\ +" FROM pg_catalog.pg_available_extension_versions "\ +" WHERE (%d = pg_catalog.length('%s'))"\ +" AND pg_catalog.quote_ident(name)='%s'" + +/* the silly-looking length condition is just to eat up the current word */ +#define Query_for_list_of_available_extension_versions_with_TO \ +" SELECT 'TO ' || pg_catalog.quote_ident(version) "\ +" FROM pg_catalog.pg_available_extension_versions "\ +" WHERE (%d = pg_catalog.length('%s'))"\ +" AND pg_catalog.quote_ident(name)='%s'" + +#define Query_for_list_of_prepared_statements \ +" SELECT pg_catalog.quote_ident(name) "\ +" FROM pg_catalog.pg_prepared_statements "\ +" WHERE substring(pg_catalog.quote_ident(name),1,%d)='%s'" + +#define Query_for_list_of_event_triggers \ +" SELECT pg_catalog.quote_ident(evtname) "\ +" FROM pg_catalog.pg_event_trigger "\ +" WHERE substring(pg_catalog.quote_ident(evtname),1,%d)='%s'" + +#define Query_for_list_of_tablesample_methods \ +" SELECT pg_catalog.quote_ident(proname) "\ +" FROM pg_catalog.pg_proc "\ +" WHERE prorettype = 'pg_catalog.tsm_handler'::pg_catalog.regtype AND "\ +" proargtypes[0] = 'pg_catalog.internal'::pg_catalog.regtype AND "\ +" substring(pg_catalog.quote_ident(proname),1,%d)='%s'" + +#define Query_for_list_of_policies \ +" SELECT pg_catalog.quote_ident(polname) "\ +" FROM pg_catalog.pg_policy "\ +" WHERE substring(pg_catalog.quote_ident(polname),1,%d)='%s'" + +#define Query_for_list_of_tables_for_policy \ +"SELECT pg_catalog.quote_ident(relname) "\ +" FROM pg_catalog.pg_class"\ +" WHERE (%d = pg_catalog.length('%s'))"\ +" AND oid IN "\ +" (SELECT polrelid FROM pg_catalog.pg_policy "\ +" WHERE pg_catalog.quote_ident(polname)='%s')" + +#define Query_for_enum \ +" SELECT name FROM ( "\ +" SELECT pg_catalog.quote_ident(pg_catalog.unnest(enumvals)) AS name "\ +" FROM pg_catalog.pg_settings "\ +" WHERE pg_catalog.lower(name)=pg_catalog.lower('%s') "\ +" UNION ALL " \ +" SELECT 'DEFAULT' ) ss "\ +" WHERE pg_catalog.substring(name,1,%%d)='%%s'" + +/* the silly-looking length condition is just to eat up the current word */ +#define Query_for_partition_of_table \ +"SELECT pg_catalog.quote_ident(c2.relname) "\ +" FROM pg_catalog.pg_class c1, pg_catalog.pg_class c2, pg_catalog.pg_inherits i"\ +" WHERE c1.oid=i.inhparent and i.inhrelid=c2.oid"\ +" and (%d = pg_catalog.length('%s'))"\ +" and pg_catalog.quote_ident(c1.relname)='%s'"\ +" and pg_catalog.pg_table_is_visible(c2.oid)"\ +" and c2.relispartition = 'true'" + +/* + * This is a list of all "things" in Pgsql, which can show up after CREATE or + * DROP; and there is also a query to get a list of them. + */ + +typedef struct +{ + const char *name; + const char *query; /* simple query, or NULL */ + const SchemaQuery *squery; /* schema query, or NULL */ + const bits32 flags; /* visibility flags, see below */ +} pgsql_thing_t; + +#define THING_NO_CREATE (1 << 0) /* should not show up after CREATE */ +#define THING_NO_DROP (1 << 1) /* should not show up after DROP */ +#define THING_NO_ALTER (1 << 2) /* should not show up after ALTER */ +#define THING_NO_SHOW (THING_NO_CREATE | THING_NO_DROP | THING_NO_ALTER) + +static const pgsql_thing_t words_after_create[] = { + {"ACCESS METHOD", NULL, NULL, THING_NO_ALTER}, + {"AGGREGATE", NULL, &Query_for_list_of_aggregates}, + {"CAST", NULL, NULL}, /* Casts have complex structures for names, so + * skip it */ + {"COLLATION", "SELECT pg_catalog.quote_ident(collname) FROM pg_catalog.pg_collation WHERE collencoding IN (-1, pg_catalog.pg_char_to_encoding(pg_catalog.getdatabaseencoding())) AND substring(pg_catalog.quote_ident(collname),1,%d)='%s'"}, + + /* + * CREATE CONSTRAINT TRIGGER is not supported here because it is designed + * to be used only by pg_dump. + */ + {"CONFIGURATION", Query_for_list_of_ts_configurations, NULL, THING_NO_SHOW}, + {"CONTINUOUS TRANSFORM", NULL, NULL, THING_NO_ALTER}, + {"CONTINUOUS VIEW", NULL, NULL, THING_NO_ALTER}, + {"CONVERSION", "SELECT pg_catalog.quote_ident(conname) FROM pg_catalog.pg_conversion WHERE substring(pg_catalog.quote_ident(conname),1,%d)='%s'"}, + {"DATABASE", Query_for_list_of_databases}, + {"DEFAULT PRIVILEGES", NULL, NULL, THING_NO_CREATE | THING_NO_DROP}, + {"DICTIONARY", Query_for_list_of_ts_dictionaries, NULL, THING_NO_SHOW}, + {"DOMAIN", NULL, &Query_for_list_of_domains}, + {"EVENT TRIGGER", NULL, NULL}, + {"EXTENSION", Query_for_list_of_extensions}, + {"EXTERNAL SCHEMA", Query_for_list_of_external_schemas}, + {"EXTERNAL TABLE", Query_for_list_of_external_tables}, + {"FOREIGN DATA WRAPPER", NULL, NULL}, + {"FOREIGN TABLE", NULL, NULL}, + {"FUNCTION", NULL, &Query_for_list_of_functions}, + {"GROUP", Query_for_list_of_roles}, + {"INDEX", NULL, &Query_for_list_of_indexes}, + {"LANGUAGE", Query_for_list_of_languages}, + {"LARGE OBJECT", NULL, NULL, THING_NO_CREATE | THING_NO_DROP}, + {"LIBRARY", NULL, NULL, THING_NO_ALTER}, + {"LOCAL", NULL, NULL, THING_NO_DROP | THING_NO_ALTER}, /* CREATE LOCAL TABLE */ + {"MATERIALIZED VIEW", NULL, &Query_for_list_of_matviews}, + {"OPERATOR", NULL, NULL}, /* Querying for this is probably not such a + * good idea. */ + {"OR REPLACE FUNCTION", NULL, &Query_for_list_of_functions}, + {"OR REPLACE LIBRARY", Query_for_list_of_libraries}, + {"OR REPLACE VIEW", NULL, &Query_for_list_of_views}, + {"OWNED", NULL, NULL, THING_NO_CREATE | THING_NO_ALTER}, /* for DROP OWNED BY ... */ + {"PARSER", Query_for_list_of_ts_parsers, NULL, THING_NO_SHOW}, + {"POLICY", NULL, NULL}, + {"PUBLICATION", Query_for_list_of_publications}, + {"ROLE", Query_for_list_of_roles}, + {"RULE", "SELECT pg_catalog.quote_ident(rulename) FROM pg_catalog.pg_rules WHERE substring(pg_catalog.quote_ident(rulename),1,%d)='%s'"}, + {"SCHEMA", Query_for_list_of_schemas}, + {"SEQUENCE", NULL, &Query_for_list_of_sequences}, + {"SERVER", Query_for_list_of_servers}, + {"STATISTICS", NULL, &Query_for_list_of_statistics}, + {"STREAM", NULL, NULL}, + {"SUBSCRIPTION", Query_for_list_of_subscriptions}, + {"SYSTEM", NULL, NULL, THING_NO_CREATE | THING_NO_DROP}, + {"TABLE", NULL, &Query_for_list_of_tables}, + {"TABLESPACE", Query_for_list_of_tablespaces}, + {"TEMP", NULL, NULL, THING_NO_DROP | THING_NO_ALTER}, /* for CREATE TEMP TABLE + * ... */ + {"TEMPLATE", Query_for_list_of_ts_templates, NULL, THING_NO_SHOW}, + {"TEMPORARY", NULL, NULL, THING_NO_DROP | THING_NO_ALTER}, /* for CREATE TEMPORARY + * TABLE ... */ + {"TEXT SEARCH", NULL, NULL}, + {"TRANSFORM", NULL, NULL}, + {"TRIGGER", "SELECT pg_catalog.quote_ident(tgname) FROM pg_catalog.pg_trigger WHERE substring(pg_catalog.quote_ident(tgname),1,%d)='%s' AND NOT tgisinternal"}, + {"TYPE", NULL, &Query_for_list_of_datatypes}, + {"UNIQUE", NULL, NULL, THING_NO_DROP | THING_NO_ALTER}, /* for CREATE UNIQUE + * INDEX ... */ + {"UNLOGGED", NULL, NULL, THING_NO_DROP | THING_NO_ALTER}, /* for CREATE UNLOGGED + * TABLE ... */ + {"USER", Query_for_list_of_roles " UNION SELECT 'MAPPING FOR'"}, + {"USER MAPPING FOR", NULL, NULL}, + {"VIEW", NULL, &Query_for_list_of_views}, + {NULL} /* end of list */ +}; + + +/* Forward declaration of functions */ +static char **psql_completion(const char *text, int start, int end); +static char *create_command_generator(const char *text, int state); +static char *drop_command_generator(const char *text, int state); +static char *alter_command_generator(const char *text, int state); +static char *complete_from_query(const char *text, int state); +static char *complete_from_schema_query(const char *text, int state); +static char *_complete_from_query(int is_schema_query, + const char *text, int state); +static char *complete_from_list(const char *text, int state); +static char *complete_from_const(const char *text, int state); +static void append_variable_names(char ***varnames, int *nvars, + int *maxvars, const char *varname, + const char *prefix, const char *suffix); +static char **complete_from_variables(const char *text, + const char *prefix, const char *suffix, bool need_value); +static char *complete_from_files(const char *text, int state); + +static char *pg_strdup_keyword_case(const char *s, const char *ref); +static char *escape_string(const char *text); +static PGresult *exec_query(const char *query); + +static char **get_previous_words(int point, char **buffer, int *nwords); + +static char *get_guctype(const char *varname); + +#ifdef NOT_USED +static char *quote_file_name(char *text, int match_type, char *quote_pointer); +static char *dequote_file_name(char *text, char quote_char); +#endif + + +/* + * Initialize the readline library for our purposes. + */ +void +initialize_readline_redshift(void) +{ + rl_readline_name = (char *) pset.progname; + rl_attempted_completion_function = psql_completion; + + rl_basic_word_break_characters = WORD_BREAKS; + + completion_max_records = 1000; + + /* + * There is a variable rl_completion_query_items for this but apparently + * it's not defined everywhere. + */ +} + +/* + * Check if 'word' matches any of the '|'-separated strings in 'pattern', + * using case-insensitive or case-sensitive comparisons. + * + * If pattern is NULL, it's a wild card that matches any word. + * If pattern begins with '!', the result is negated, ie we check that 'word' + * does *not* match any alternative appearing in the rest of 'pattern'. + * Any alternative can end with '*' which is a wild card, i.e., it means + * match any word that matches the characters so far. (We do not currently + * support '*' elsewhere than the end of an alternative.) + * + * For readability, callers should use the macros MatchAny and MatchAnyExcept + * to invoke those two special cases for 'pattern'. (But '|' and '*' must + * just be written directly in patterns.) + */ +#define MatchAny NULL +#define MatchAnyExcept(pattern) ("!" pattern) + +static bool +word_matches_internal(const char *pattern, + const char *word, + bool case_sensitive) +{ + size_t wordlen, + patternlen; + + /* NULL pattern matches anything. */ + if (pattern == NULL) + return true; + + /* Handle negated patterns from the MatchAnyExcept macro. */ + if (*pattern == '!') + return !word_matches_internal(pattern + 1, word, case_sensitive); + + /* Else consider each alternative in the pattern. */ + wordlen = strlen(word); + for (;;) + { + const char *c; + + /* Find end of current alternative. */ + c = pattern; + while (*c != '\0' && *c != '|') + c++; + /* Was there a wild card? (Assumes first alternative is not empty) */ + if (c[-1] == '*') + { + /* Yes, wildcard match? */ + patternlen = c - pattern - 1; + if (wordlen >= patternlen && + (case_sensitive ? + strncmp(word, pattern, patternlen) == 0 : + pg_strncasecmp(word, pattern, patternlen) == 0)) + return true; + } + else + { + /* No, plain match? */ + patternlen = c - pattern; + if (wordlen == patternlen && + (case_sensitive ? + strncmp(word, pattern, wordlen) == 0 : + pg_strncasecmp(word, pattern, wordlen) == 0)) + return true; + } + /* Out of alternatives? */ + if (*c == '\0') + break; + /* Nope, try next alternative. */ + pattern = c + 1; + } + + return false; +} + +/* + * There are enough matching calls below that it seems worth having these two + * interface routines rather than including a third parameter in every call. + * + * word_matches --- match case-insensitively. + */ +static bool +word_matches(const char *pattern, const char *word) +{ + return word_matches_internal(pattern, word, false); +} + +/* + * word_matches_cs --- match case-sensitively. + */ +static bool +word_matches_cs(const char *pattern, const char *word) +{ + return word_matches_internal(pattern, word, true); +} + +/* + * Check if the final character of 's' is 'c'. + */ +static bool +ends_with(const char *s, char c) +{ + size_t length = strlen(s); + + return (length > 0 && s[length - 1] == c); +} + +/* + * The completion function. + * + * According to readline spec this gets passed the text entered so far and its + * start and end positions in the readline buffer. The return value is some + * partially obscure list format that can be generated by readline's + * completion_matches() function, so we don't have to worry about it. + */ +static char ** +psql_completion(const char *text, int start, int end) +{ + /* This is the variable we'll return. */ + char **matches = NULL; + + /* Workspace for parsed words. */ + char *words_buffer; + + /* This array will contain pointers to parsed words. */ + char **previous_words; + + /* The number of words found on the input line. */ + int previous_words_count; + + /* + * For compactness, we use these macros to reference previous_words[]. + * Caution: do not access a previous_words[] entry without having checked + * previous_words_count to be sure it's valid. In most cases below, that + * check is implicit in a TailMatches() or similar macro, but in some + * places we have to check it explicitly. + */ +#define prev_wd (previous_words[0]) +#define prev2_wd (previous_words[1]) +#define prev3_wd (previous_words[2]) +#define prev4_wd (previous_words[3]) +#define prev5_wd (previous_words[4]) +#define prev6_wd (previous_words[5]) +#define prev7_wd (previous_words[6]) +#define prev8_wd (previous_words[7]) +#define prev9_wd (previous_words[8]) + + /* Macros for matching the last N words before point, case-insensitively. */ +#define TailMatches1(p1) \ + (previous_words_count >= 1 && \ + word_matches(p1, prev_wd)) + +#define TailMatches2(p2, p1) \ + (previous_words_count >= 2 && \ + word_matches(p1, prev_wd) && \ + word_matches(p2, prev2_wd)) + +#define TailMatches3(p3, p2, p1) \ + (previous_words_count >= 3 && \ + word_matches(p1, prev_wd) && \ + word_matches(p2, prev2_wd) && \ + word_matches(p3, prev3_wd)) + +#define TailMatches4(p4, p3, p2, p1) \ + (previous_words_count >= 4 && \ + word_matches(p1, prev_wd) && \ + word_matches(p2, prev2_wd) && \ + word_matches(p3, prev3_wd) && \ + word_matches(p4, prev4_wd)) + +#define TailMatches5(p5, p4, p3, p2, p1) \ + (previous_words_count >= 5 && \ + word_matches(p1, prev_wd) && \ + word_matches(p2, prev2_wd) && \ + word_matches(p3, prev3_wd) && \ + word_matches(p4, prev4_wd) && \ + word_matches(p5, prev5_wd)) + +#define TailMatches6(p6, p5, p4, p3, p2, p1) \ + (previous_words_count >= 6 && \ + word_matches(p1, prev_wd) && \ + word_matches(p2, prev2_wd) && \ + word_matches(p3, prev3_wd) && \ + word_matches(p4, prev4_wd) && \ + word_matches(p5, prev5_wd) && \ + word_matches(p6, prev6_wd)) + +#define TailMatches7(p7, p6, p5, p4, p3, p2, p1) \ + (previous_words_count >= 7 && \ + word_matches(p1, prev_wd) && \ + word_matches(p2, prev2_wd) && \ + word_matches(p3, prev3_wd) && \ + word_matches(p4, prev4_wd) && \ + word_matches(p5, prev5_wd) && \ + word_matches(p6, prev6_wd) && \ + word_matches(p7, prev7_wd)) + +#define TailMatches8(p8, p7, p6, p5, p4, p3, p2, p1) \ + (previous_words_count >= 8 && \ + word_matches(p1, prev_wd) && \ + word_matches(p2, prev2_wd) && \ + word_matches(p3, prev3_wd) && \ + word_matches(p4, prev4_wd) && \ + word_matches(p5, prev5_wd) && \ + word_matches(p6, prev6_wd) && \ + word_matches(p7, prev7_wd) && \ + word_matches(p8, prev8_wd)) + +#define TailMatches9(p9, p8, p7, p6, p5, p4, p3, p2, p1) \ + (previous_words_count >= 9 && \ + word_matches(p1, prev_wd) && \ + word_matches(p2, prev2_wd) && \ + word_matches(p3, prev3_wd) && \ + word_matches(p4, prev4_wd) && \ + word_matches(p5, prev5_wd) && \ + word_matches(p6, prev6_wd) && \ + word_matches(p7, prev7_wd) && \ + word_matches(p8, prev8_wd) && \ + word_matches(p9, prev9_wd)) + + /* Macros for matching the last N words before point, case-sensitively. */ +#define TailMatchesCS1(p1) \ + (previous_words_count >= 1 && \ + word_matches_cs(p1, prev_wd)) +#define TailMatchesCS2(p2, p1) \ + (previous_words_count >= 2 && \ + word_matches_cs(p1, prev_wd) && \ + word_matches_cs(p2, prev2_wd)) +#define TailMatchesCS3(p3, p2, p1) \ + (previous_words_count >= 3 && \ + word_matches_cs(p1, prev_wd) && \ + word_matches_cs(p2, prev2_wd) && \ + word_matches_cs(p3, prev3_wd)) +#define TailMatchesCS4(p4, p3, p2, p1) \ + (previous_words_count >= 4 && \ + word_matches_cs(p1, prev_wd) && \ + word_matches_cs(p2, prev2_wd) && \ + word_matches_cs(p3, prev3_wd) && \ + word_matches_cs(p4, prev4_wd)) + + /* + * Macros for matching N words beginning at the start of the line, + * case-insensitively. + */ +#define Matches1(p1) \ + (previous_words_count == 1 && \ + TailMatches1(p1)) +#define Matches2(p1, p2) \ + (previous_words_count == 2 && \ + TailMatches2(p1, p2)) +#define Matches3(p1, p2, p3) \ + (previous_words_count == 3 && \ + TailMatches3(p1, p2, p3)) +#define Matches4(p1, p2, p3, p4) \ + (previous_words_count == 4 && \ + TailMatches4(p1, p2, p3, p4)) +#define Matches5(p1, p2, p3, p4, p5) \ + (previous_words_count == 5 && \ + TailMatches5(p1, p2, p3, p4, p5)) +#define Matches6(p1, p2, p3, p4, p5, p6) \ + (previous_words_count == 6 && \ + TailMatches6(p1, p2, p3, p4, p5, p6)) +#define Matches7(p1, p2, p3, p4, p5, p6, p7) \ + (previous_words_count == 7 && \ + TailMatches7(p1, p2, p3, p4, p5, p6, p7)) +#define Matches8(p1, p2, p3, p4, p5, p6, p7, p8) \ + (previous_words_count == 8 && \ + TailMatches8(p1, p2, p3, p4, p5, p6, p7, p8)) +#define Matches9(p1, p2, p3, p4, p5, p6, p7, p8, p9) \ + (previous_words_count == 9 && \ + TailMatches9(p1, p2, p3, p4, p5, p6, p7, p8, p9)) + + /* + * Macros for matching N words at the start of the line, regardless of + * what is after them, case-insensitively. + */ +#define HeadMatches1(p1) \ + (previous_words_count >= 1 && \ + word_matches(p1, previous_words[previous_words_count - 1])) + +#define HeadMatches2(p1, p2) \ + (previous_words_count >= 2 && \ + word_matches(p1, previous_words[previous_words_count - 1]) && \ + word_matches(p2, previous_words[previous_words_count - 2])) + +#define HeadMatches3(p1, p2, p3) \ + (previous_words_count >= 3 && \ + word_matches(p1, previous_words[previous_words_count - 1]) && \ + word_matches(p2, previous_words[previous_words_count - 2]) && \ + word_matches(p3, previous_words[previous_words_count - 3])) + +#define HeadMatches4(p1, p2, p3, p4) \ + (previous_words_count >= 4 && \ + word_matches(p1, previous_words[previous_words_count - 1]) && \ + word_matches(p2, previous_words[previous_words_count - 2]) && \ + word_matches(p3, previous_words[previous_words_count - 3]) && \ + word_matches(p4, previous_words[previous_words_count - 4])) + +#define HeadMatches5(p1, p2, p3, p4, p5) \ + (previous_words_count >= 5 && \ + word_matches(p1, previous_words[previous_words_count - 1]) && \ + word_matches(p2, previous_words[previous_words_count - 2]) && \ + word_matches(p3, previous_words[previous_words_count - 3]) && \ + word_matches(p4, previous_words[previous_words_count - 4]) && \ + word_matches(p5, previous_words[previous_words_count - 5])) + +#define HeadMatches6(p1, p2, p3, p4, p5, p6) \ + (previous_words_count >= 6 && \ + word_matches(p1, previous_words[previous_words_count - 1]) && \ + word_matches(p2, previous_words[previous_words_count - 2]) && \ + word_matches(p3, previous_words[previous_words_count - 3]) && \ + word_matches(p4, previous_words[previous_words_count - 4]) && \ + word_matches(p5, previous_words[previous_words_count - 5]) && \ + word_matches(p6, previous_words[previous_words_count - 6])) + +#define HeadMatches7(p1, p2, p3, p4, p5, p6, p7) \ + (previous_words_count >= 7 && \ + word_matches(p1, previous_words[previous_words_count - 1]) && \ + word_matches(p2, previous_words[previous_words_count - 2]) && \ + word_matches(p3, previous_words[previous_words_count - 3]) && \ + word_matches(p4, previous_words[previous_words_count - 4]) && \ + word_matches(p5, previous_words[previous_words_count - 5]) && \ + word_matches(p6, previous_words[previous_words_count - 6]) && \ + word_matches(p7, previous_words[previous_words_count - 7])) + +#define HeadMatches8(p1, p2, p3, p4, p5, p6, p7, p8) \ +(previous_words_count >= 8 && \ + word_matches(p1, previous_words[previous_words_count - 1]) && \ + word_matches(p2, previous_words[previous_words_count - 2]) && \ + word_matches(p3, previous_words[previous_words_count - 3]) && \ + word_matches(p4, previous_words[previous_words_count - 4]) && \ + word_matches(p5, previous_words[previous_words_count - 5]) && \ + word_matches(p6, previous_words[previous_words_count - 6]) && \ + word_matches(p7, previous_words[previous_words_count - 7]) && \ + word_matches(p8, previous_words[previous_words_count - 8])) + +#define HeadMatches9(p1, p2, p3, p4, p5, p6, p7, p8, p9) \ +(previous_words_count >= 9 && \ + word_matches(p1, previous_words[previous_words_count - 1]) && \ + word_matches(p2, previous_words[previous_words_count - 2]) && \ + word_matches(p3, previous_words[previous_words_count - 3]) && \ + word_matches(p4, previous_words[previous_words_count - 4]) && \ + word_matches(p5, previous_words[previous_words_count - 5]) && \ + word_matches(p6, previous_words[previous_words_count - 6]) && \ + word_matches(p7, previous_words[previous_words_count - 7]) && \ + word_matches(p8, previous_words[previous_words_count - 8]) && \ + word_matches(p9, previous_words[previous_words_count - 9])) + + /* Known command-starting keywords. */ + static const char *const sql_commands[] = { + "ABORT", "ALTER", "ANALYZE", "BEGIN", "CANCEL", "CHECKPOINT", "CLOSE", "CLUSTER", + "COMMENT", "COMMIT", "COPY", "CREATE", "DEALLOCATE", "DECLARE", + "DELETE FROM", "DISCARD", "DO", "DROP", "END", "EXECUTE", "EXPLAIN", + "FETCH", "GRANT", "IMPORT", "INSERT", "LISTEN", "LOAD", "LOCK", + "MOVE", "NOTIFY", "PREPARE", + "REASSIGN", "REFRESH MATERIALIZED VIEW", "REINDEX", "RELEASE", + "RESET", "REVOKE", "ROLLBACK", + "SAVEPOINT", "SECURITY LABEL", "SELECT", "SET", "SHOW", "START", + "TABLE", "TRUNCATE", "UNLISTEN", "UNLOAD", "UPDATE", "VACUUM", "VALUES", "WITH", + NULL + }; + + /* psql's backslash commands. */ + static const char *const backslash_commands[] = { + "\\a", + "\\connect", "\\conninfo", "\\C", "\\cd", "\\copy", + "\\copyright", "\\crosstabview", + "\\d", "\\da", "\\dA", "\\db", "\\dc", "\\dC", "\\dd", "\\ddp", "\\dD", + "\\des", "\\det", "\\deu", "\\dew", "\\dE", "\\df", + "\\dF", "\\dFd", "\\dFp", "\\dFt", "\\dg", "\\di", "\\dl", "\\dL", + "\\dm", "\\dn", "\\do", "\\dO", "\\dp", + "\\drds", "\\dRs", "\\dRp", "\\ds", "\\dS", + "\\dt", "\\dT", "\\dv", "\\du", "\\dx", "\\dy", + "\\e", "\\echo", "\\ef", "\\elif", "\\else", "\\encoding", + "\\endif", "\\errverbose", "\\ev", + "\\f", + "\\g", "\\gdesc", "\\gexec", "\\gset", "\\gx", + "\\h", "\\help", "\\H", + "\\i", "\\if", "\\ir", + "\\l", "\\lo_import", "\\lo_export", "\\lo_list", "\\lo_unlink", + "\\o", + "\\p", "\\password", "\\prompt", "\\pset", + "\\q", "\\qecho", + "\\r", + "\\s", "\\set", "\\setenv", "\\sf", "\\sv", + "\\t", "\\T", "\\timing", + "\\unset", + "\\x", + "\\w", "\\watch", + "\\z", + "\\!", "\\?", + NULL + }; + + (void) end; /* "end" is not used */ + +#ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER + rl_completion_append_character = ' '; +#endif + +/* Macro to easily identify which Engine (type) are we speaking to + Any change here should probably be replicated elsewhere since + #define for various SQLs (in this script) employ there own string-compare */ + #define IS_REDSHIFT (strncmp(pset.sengine, "redshift", 8) == 0) + #define IS_PIPELINEDB (strncmp(pset.sengine, "pipelinedb", 10) == 0) + + /* Clear a few things. */ + completion_charp = NULL; + completion_charpp = NULL; + completion_info_charp = NULL; + completion_info_charp2 = NULL; + + /* + * Scan the input line to extract the words before our current position. + * According to those we'll make some smart decisions on what the user is + * probably intending to type. + */ + previous_words = get_previous_words(start, + &words_buffer, + &previous_words_count); + + /* If current word is a backslash command, offer completions for that */ + if (text[0] == '\\') + COMPLETE_WITH_LIST_CS(backslash_commands); + + /* If current word is a variable interpolation, handle that case */ + else if (text[0] == ':' && text[1] != ':') + { + if (text[1] == '\'') + matches = complete_from_variables(text, ":'", "'", true); + else if (text[1] == '"') + matches = complete_from_variables(text, ":\"", "\"", true); + else + matches = complete_from_variables(text, ":", "", true); + } + + /* If no previous word, suggest one of the basic sql commands */ + else if (previous_words_count == 0) + COMPLETE_WITH_LIST(sql_commands); + +/* CREATE */ + /* complete with something you can create */ + else if (TailMatches1("CREATE")) + matches = completion_matches(text, create_command_generator); + +/* DROP, but not DROP embedded in other commands */ + /* complete with something you can drop */ + else if (Matches1("DROP")) + matches = completion_matches(text, drop_command_generator); + +/* ALTER */ + + /* ALTER TABLE */ + else if (Matches2("ALTER", "TABLE")) + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tables, + "UNION SELECT 'ALL IN TABLESPACE'"); + + /* ALTER something */ + else if (Matches1("ALTER")) + matches = completion_matches(text, alter_command_generator); + /* ALTER TABLE,INDEX,MATERIALIZED VIEW ALL IN TABLESPACE xxx */ + else if (TailMatches4("ALL", "IN", "TABLESPACE", MatchAny)) + COMPLETE_WITH_LIST2("SET TABLESPACE", "OWNED BY"); + /* ALTER TABLE,INDEX,MATERIALIZED VIEW ALL IN TABLESPACE xxx OWNED BY */ + else if (TailMatches6("ALL", "IN", "TABLESPACE", MatchAny, "OWNED", "BY")) + COMPLETE_WITH_QUERY(Query_for_list_of_roles); + /* ALTER TABLE,INDEX,MATERIALIZED VIEW ALL IN TABLESPACE xxx OWNED BY xxx */ + else if (TailMatches7("ALL", "IN", "TABLESPACE", MatchAny, "OWNED", "BY", MatchAny)) + COMPLETE_WITH_CONST("SET TABLESPACE"); + /* ALTER AGGREGATE,FUNCTION */ + else if (Matches3("ALTER", "AGGREGATE|FUNCTION", MatchAny)) + COMPLETE_WITH_CONST("("); + /* ALTER AGGREGATE,FUNCTION (...) */ + else if (Matches4("ALTER", "AGGREGATE|FUNCTION", MatchAny, MatchAny)) + { + if (ends_with(prev_wd, ')')) + COMPLETE_WITH_LIST3("OWNER TO", "RENAME TO", "SET SCHEMA"); + else + COMPLETE_WITH_FUNCTION_ARG(prev2_wd); + } + /* ALTER PUBLICATION */ + else if (Matches3("ALTER", "PUBLICATION", MatchAny)) + COMPLETE_WITH_LIST5("ADD TABLE", "DROP TABLE", "OWNER TO", "RENAME TO", "SET"); + /* ALTER PUBLICATION SET */ + else if (Matches4("ALTER", "PUBLICATION", MatchAny, "SET")) + COMPLETE_WITH_LIST2("(", "TABLE"); + /* ALTER PUBLICATION SET ( */ + else if (HeadMatches3("ALTER", "PUBLICATION", MatchAny) && TailMatches2("SET", "(")) + COMPLETE_WITH_CONST("publish"); + /* ALTER SUBSCRIPTION */ + else if (Matches3("ALTER", "SUBSCRIPTION", MatchAny)) + COMPLETE_WITH_LIST7("CONNECTION", "ENABLE", "DISABLE", "OWNER TO", + "RENAME TO", "REFRESH PUBLICATION", "SET"); + /* ALTER SUBSCRIPTION REFRESH PUBLICATION */ + else if (HeadMatches3("ALTER", "SUBSCRIPTION", MatchAny) && + TailMatches2("REFRESH", "PUBLICATION")) + COMPLETE_WITH_CONST("WITH ("); + /* ALTER SUBSCRIPTION REFRESH PUBLICATION WITH ( */ + else if (HeadMatches3("ALTER", "SUBSCRIPTION", MatchAny) && + TailMatches4("REFRESH", "PUBLICATION", "WITH", "(")) + COMPLETE_WITH_CONST("copy_data"); + /* ALTER SUBSCRIPTION SET */ + else if (Matches4("ALTER", "SUBSCRIPTION", MatchAny, "SET")) + COMPLETE_WITH_LIST2("(", "PUBLICATION"); + /* ALTER SUBSCRIPTION SET ( */ + else if (HeadMatches3("ALTER", "SUBSCRIPTION", MatchAny) && TailMatches2("SET", "(")) + COMPLETE_WITH_LIST2("slot_name", "synchronous_commit"); + /* ALTER SUBSCRIPTION SET PUBLICATION */ + else if (HeadMatches3("ALTER", "SUBSCRIPTION", MatchAny) && TailMatches2("SET", "PUBLICATION")) + { + /* complete with nothing here as this refers to remote publications */ + } + /* ALTER SUBSCRIPTION SET PUBLICATION */ + else if (HeadMatches3("ALTER", "SUBSCRIPTION", MatchAny) && + TailMatches3("SET", "PUBLICATION", MatchAny)) + COMPLETE_WITH_CONST("WITH ("); + /* ALTER SUBSCRIPTION SET PUBLICATION WITH ( */ + else if (HeadMatches3("ALTER", "SUBSCRIPTION", MatchAny) && + TailMatches5("SET", "PUBLICATION", MatchAny, "WITH", "(")) + COMPLETE_WITH_LIST2("copy_data", "refresh"); + /* ALTER SCHEMA */ + else if (Matches3("ALTER", "SCHEMA", MatchAny)) + COMPLETE_WITH_LIST2("OWNER TO", "RENAME TO"); + + /* ALTER COLLATION */ + else if (Matches3("ALTER", "COLLATION", MatchAny)) + COMPLETE_WITH_LIST3("OWNER TO", "RENAME TO", "SET SCHEMA"); + + /* ALTER CONVERSION */ + else if (Matches3("ALTER", "CONVERSION", MatchAny)) + COMPLETE_WITH_LIST3("OWNER TO", "RENAME TO", "SET SCHEMA"); + + /* ALTER DATABASE */ + else if (Matches3("ALTER", "DATABASE", MatchAny)) + COMPLETE_WITH_LIST7("RESET", "SET", "OWNER TO", "RENAME TO", + "IS_TEMPLATE", "ALLOW_CONNECTIONS", + "CONNECTION LIMIT"); + + /* ALTER EVENT TRIGGER */ + else if (Matches3("ALTER", "EVENT", "TRIGGER")) + COMPLETE_WITH_QUERY(Query_for_list_of_event_triggers); + + /* ALTER EVENT TRIGGER */ + else if (Matches4("ALTER", "EVENT", "TRIGGER", MatchAny)) + COMPLETE_WITH_LIST4("DISABLE", "ENABLE", "OWNER TO", "RENAME TO"); + + /* ALTER EVENT TRIGGER ENABLE */ + else if (Matches5("ALTER", "EVENT", "TRIGGER", MatchAny, "ENABLE")) + COMPLETE_WITH_LIST2("REPLICA", "ALWAYS"); + + /* ALTER EXTENSION */ + else if (Matches3("ALTER", "EXTENSION", MatchAny)) + COMPLETE_WITH_LIST4("ADD", "DROP", "UPDATE", "SET SCHEMA"); + + /* ALTER EXTENSION UPDATE */ + else if (Matches4("ALTER", "EXTENSION", MatchAny, "UPDATE")) + { + completion_info_charp = prev2_wd; + COMPLETE_WITH_QUERY(Query_for_list_of_available_extension_versions_with_TO); + } + + /* ALTER EXTENSION UPDATE TO */ + else if (Matches5("ALTER", "EXTENSION", MatchAny, "UPDATE", "TO")) + { + completion_info_charp = prev3_wd; + COMPLETE_WITH_QUERY(Query_for_list_of_available_extension_versions); + } + + /* ALTER FOREIGN */ + else if (Matches2("ALTER", "FOREIGN")) + COMPLETE_WITH_LIST2("DATA WRAPPER", "TABLE"); + + /* ALTER FOREIGN DATA WRAPPER */ + else if (Matches5("ALTER", "FOREIGN", "DATA", "WRAPPER", MatchAny)) + COMPLETE_WITH_LIST5("HANDLER", "VALIDATOR", "OPTIONS", "OWNER TO", "RENAME TO"); + + /* ALTER FOREIGN TABLE */ + else if (Matches4("ALTER", "FOREIGN", "TABLE", MatchAny)) + { + static const char *const list_ALTER_FOREIGN_TABLE[] = + {"ADD", "ALTER", "DISABLE TRIGGER", "DROP", "ENABLE", "INHERIT", + "NO INHERIT", "OPTIONS", "OWNER TO", "RENAME", "SET", + "VALIDATE CONSTRAINT", NULL}; + + COMPLETE_WITH_LIST(list_ALTER_FOREIGN_TABLE); + } + + /* ALTER INDEX */ + else if (Matches2("ALTER", "INDEX")) + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_indexes, + "UNION SELECT 'ALL IN TABLESPACE'"); + /* ALTER INDEX */ + else if (Matches3("ALTER", "INDEX", MatchAny)) + COMPLETE_WITH_LIST5("ALTER COLUMN", "OWNER TO", "RENAME TO", "SET", "RESET"); + /* ALTER INDEX ALTER COLUMN */ + else if (Matches6("ALTER", "INDEX", MatchAny, "ALTER", "COLUMN", MatchAny)) + COMPLETE_WITH_CONST("SET STATISTICS"); + /* ALTER INDEX SET */ + else if (Matches4("ALTER", "INDEX", MatchAny, "SET")) + COMPLETE_WITH_LIST2("(", "TABLESPACE"); + /* ALTER INDEX RESET */ + else if (Matches4("ALTER", "INDEX", MatchAny, "RESET")) + COMPLETE_WITH_CONST("("); + /* ALTER INDEX SET|RESET ( */ + else if (Matches5("ALTER", "INDEX", MatchAny, "RESET", "(")) + COMPLETE_WITH_LIST3("fillfactor", "fastupdate", + "gin_pending_list_limit"); + else if (Matches5("ALTER", "INDEX", MatchAny, "SET", "(")) + COMPLETE_WITH_LIST3("fillfactor =", "fastupdate =", + "gin_pending_list_limit ="); + + /* ALTER LANGUAGE */ + else if (Matches3("ALTER", "LANGUAGE", MatchAny)) + COMPLETE_WITH_LIST2("OWNER_TO", "RENAME TO"); + + /* ALTER LARGE OBJECT */ + else if (Matches4("ALTER", "LARGE", "OBJECT", MatchAny)) + COMPLETE_WITH_CONST("OWNER TO"); + + /* ALTER MATERIALIZED VIEW */ + else if (Matches3("ALTER", "MATERIALIZED", "VIEW")) + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_matviews, + "UNION SELECT 'ALL IN TABLESPACE'"); + + /* ALTER USER,ROLE */ + else if (Matches3("ALTER", "USER|ROLE", MatchAny) && + !TailMatches2("USER", "MAPPING")) + { + static const char *const list_ALTERUSER[] = + {"BYPASSRLS", "CONNECTION LIMIT", "CREATEDB", "CREATEROLE", "CREATEUSER", + "ENCRYPTED PASSWORD", "INHERIT", "LOGIN", "NOBYPASSRLS", + "NOCREATEDB", "NOCREATEROLE", "NOCREATEUSER", "NOINHERIT", + "NOLOGIN", "NOREPLICATION", "NOSUPERUSER", "PASSWORD", "RENAME TO", + "REPLICATION", "RESET", "SET", "SUPERUSER", + "VALID UNTIL", "WITH", NULL}; + + COMPLETE_WITH_LIST(list_ALTERUSER); + } + + /* ALTER USER,ROLE WITH */ + else if (Matches4("ALTER", "USER|ROLE", MatchAny, "WITH")) + { + /* Similar to the above, but don't complete "WITH" again. */ + static const char *const list_ALTERUSER_WITH[] = + {"BYPASSRLS", "CONNECTION LIMIT", "CREATEDB", "CREATEROLE", + "ENCRYPTED PASSWORD", "INHERIT", "LOGIN", "NOBYPASSRLS", + "NOCREATEDB", "NOCREATEROLE", "NOINHERIT", + "NOLOGIN", "NOREPLICATION", "NOSUPERUSER", "PASSWORD", "RENAME TO", + "REPLICATION", "RESET", "SET", "SUPERUSER", + "VALID UNTIL", NULL}; + + COMPLETE_WITH_LIST(list_ALTERUSER_WITH); + } + + /* ALTER DEFAULT PRIVILEGES */ + else if (Matches3("ALTER", "DEFAULT", "PRIVILEGES")) + COMPLETE_WITH_LIST2("FOR ROLE", "IN SCHEMA"); + /* ALTER DEFAULT PRIVILEGES FOR */ + else if (Matches4("ALTER", "DEFAULT", "PRIVILEGES", "FOR")) + COMPLETE_WITH_CONST("ROLE"); + /* ALTER DEFAULT PRIVILEGES IN */ + else if (Matches4("ALTER", "DEFAULT", "PRIVILEGES", "IN")) + COMPLETE_WITH_CONST("SCHEMA"); + /* ALTER DEFAULT PRIVILEGES FOR ROLE|USER ... */ + else if (Matches6("ALTER", "DEFAULT", "PRIVILEGES", "FOR", "ROLE|USER", + MatchAny)) + COMPLETE_WITH_LIST3("GRANT", "REVOKE", "IN SCHEMA"); + /* ALTER DEFAULT PRIVILEGES IN SCHEMA ... */ + else if (Matches6("ALTER", "DEFAULT", "PRIVILEGES", "IN", "SCHEMA", + MatchAny)) + COMPLETE_WITH_LIST3("GRANT", "REVOKE", "FOR ROLE"); + /* ALTER DEFAULT PRIVILEGES IN SCHEMA ... FOR */ + else if (Matches7("ALTER", "DEFAULT", "PRIVILEGES", "IN", "SCHEMA", + MatchAny, "FOR")) + COMPLETE_WITH_CONST("ROLE"); + /* ALTER DEFAULT PRIVILEGES FOR ROLE|USER ... IN SCHEMA ... */ + /* ALTER DEFAULT PRIVILEGES IN SCHEMA ... FOR ROLE|USER ... */ + else if (Matches9("ALTER", "DEFAULT", "PRIVILEGES", "FOR", "ROLE|USER", + MatchAny, "IN", "SCHEMA", MatchAny) || + Matches9("ALTER", "DEFAULT", "PRIVILEGES", "IN", "SCHEMA", + MatchAny, "FOR", "ROLE|USER", MatchAny)) + COMPLETE_WITH_LIST2("GRANT", "REVOKE"); + /* ALTER DOMAIN */ + else if (Matches3("ALTER", "DOMAIN", MatchAny)) + COMPLETE_WITH_LIST6("ADD", "DROP", "OWNER TO", "RENAME", "SET", + "VALIDATE CONSTRAINT"); + /* ALTER DOMAIN DROP */ + else if (Matches4("ALTER", "DOMAIN", MatchAny, "DROP")) + COMPLETE_WITH_LIST3("CONSTRAINT", "DEFAULT", "NOT NULL"); + /* ALTER DOMAIN DROP|RENAME|VALIDATE CONSTRAINT */ + else if (Matches5("ALTER", "DOMAIN", MatchAny, "DROP|RENAME|VALIDATE", "CONSTRAINT")) + { + completion_info_charp = prev3_wd; + COMPLETE_WITH_QUERY(Query_for_constraint_of_type); + } + /* ALTER DOMAIN RENAME */ + else if (Matches4("ALTER", "DOMAIN", MatchAny, "RENAME")) + COMPLETE_WITH_LIST2("CONSTRAINT", "TO"); + /* ALTER DOMAIN RENAME CONSTRAINT */ + else if (Matches6("ALTER", "DOMAIN", MatchAny, "RENAME", "CONSTRAINT", MatchAny)) + COMPLETE_WITH_CONST("TO"); + + /* ALTER DOMAIN SET */ + else if (Matches4("ALTER", "DOMAIN", MatchAny, "SET")) + COMPLETE_WITH_LIST3("DEFAULT", "NOT NULL", "SCHEMA"); + /* ALTER SEQUENCE */ + else if (Matches3("ALTER", "SEQUENCE", MatchAny)) + { + static const char *const list_ALTERSEQUENCE[] = + {"INCREMENT", "MINVALUE", "MAXVALUE", "RESTART", "NO", "CACHE", "CYCLE", + "SET SCHEMA", "OWNED BY", "OWNER TO", "RENAME TO", NULL}; + + COMPLETE_WITH_LIST(list_ALTERSEQUENCE); + } + /* ALTER SEQUENCE NO */ + else if (Matches4("ALTER", "SEQUENCE", MatchAny, "NO")) + COMPLETE_WITH_LIST3("MINVALUE", "MAXVALUE", "CYCLE"); + /* ALTER SERVER */ + else if (Matches3("ALTER", "SERVER", MatchAny)) + COMPLETE_WITH_LIST4("VERSION", "OPTIONS", "OWNER TO", "RENAME TO"); + /* ALTER SERVER VERSION */ + else if (Matches5("ALTER", "SERVER", MatchAny, "VERSION", MatchAny)) + COMPLETE_WITH_CONST("OPTIONS"); + /* ALTER SYSTEM SET, RESET, RESET ALL */ + else if (Matches2("ALTER", "SYSTEM")) + COMPLETE_WITH_LIST2("SET", "RESET"); + else if (Matches3("ALTER", "SYSTEM", "SET|RESET")) + COMPLETE_WITH_QUERY(Query_for_list_of_alter_system_set_vars); + else if (Matches4("ALTER", "SYSTEM", "SET", MatchAny)) + COMPLETE_WITH_CONST("TO"); + /* ALTER VIEW */ + else if (Matches3("ALTER", "VIEW", MatchAny)) + COMPLETE_WITH_LIST4("ALTER COLUMN", "OWNER TO", "RENAME TO", + "SET SCHEMA"); + /* ALTER MATERIALIZED VIEW */ + else if (Matches4("ALTER", "MATERIALIZED", "VIEW", MatchAny)) + COMPLETE_WITH_LIST4("ALTER COLUMN", "OWNER TO", "RENAME TO", + "SET SCHEMA"); + + /* ALTER POLICY */ + else if (Matches2("ALTER", "POLICY")) + COMPLETE_WITH_QUERY(Query_for_list_of_policies); + /* ALTER POLICY ON */ + else if (Matches3("ALTER", "POLICY", MatchAny)) + COMPLETE_WITH_CONST("ON"); + /* ALTER POLICY ON
*/ + else if (Matches4("ALTER", "POLICY", MatchAny, "ON")) + { + completion_info_charp = prev2_wd; + COMPLETE_WITH_QUERY(Query_for_list_of_tables_for_policy); + } + /* ALTER POLICY ON
- show options */ + else if (Matches5("ALTER", "POLICY", MatchAny, "ON", MatchAny)) + COMPLETE_WITH_LIST4("RENAME TO", "TO", "USING (", "WITH CHECK ("); + /* ALTER POLICY ON
TO */ + else if (Matches6("ALTER", "POLICY", MatchAny, "ON", MatchAny, "TO")) + COMPLETE_WITH_QUERY(Query_for_list_of_grant_roles); + /* ALTER POLICY ON
USING ( */ + else if (Matches6("ALTER", "POLICY", MatchAny, "ON", MatchAny, "USING")) + COMPLETE_WITH_CONST("("); + /* ALTER POLICY ON
WITH CHECK ( */ + else if (Matches7("ALTER", "POLICY", MatchAny, "ON", MatchAny, "WITH", "CHECK")) + COMPLETE_WITH_CONST("("); + + /* ALTER RULE , add ON */ + else if (Matches3("ALTER", "RULE", MatchAny)) + COMPLETE_WITH_CONST("ON"); + + /* If we have ALTER RULE ON, then add the correct tablename */ + else if (Matches4("ALTER", "RULE", MatchAny, "ON")) + { + completion_info_charp = prev2_wd; + COMPLETE_WITH_QUERY(Query_for_list_of_tables_for_rule); + } + + /* ALTER RULE ON */ + else if (Matches5("ALTER", "RULE", MatchAny, "ON", MatchAny)) + COMPLETE_WITH_CONST("RENAME TO"); + + /* ALTER STATISTICS */ + else if (Matches3("ALTER", "STATISTICS", MatchAny)) + COMPLETE_WITH_LIST3("OWNER TO", "RENAME TO", "SET SCHEMA"); + + /* ALTER TRIGGER , add ON */ + else if (Matches3("ALTER", "TRIGGER", MatchAny)) + COMPLETE_WITH_CONST("ON"); + + else if (Matches4("ALTER", "TRIGGER", MatchAny, MatchAny)) + { + completion_info_charp = prev2_wd; + COMPLETE_WITH_QUERY(Query_for_list_of_tables_for_trigger); + } + + /* + * If we have ALTER TRIGGER ON, then add the correct tablename + */ + else if (Matches4("ALTER", "TRIGGER", MatchAny, "ON")) + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tables, NULL); + + /* ALTER TRIGGER ON */ + else if (Matches5("ALTER", "TRIGGER", MatchAny, "ON", MatchAny)) + COMPLETE_WITH_CONST("RENAME TO"); + + /* + * If we detect ALTER TABLE , suggest sub commands + */ + else if (Matches3("ALTER", "TABLE", MatchAny)) + { + static const char *const list_ALTER2[] = + {"ADD", "ALTER", "APPEND FROM", "CLUSTER ON", "DISABLE", "DROP", "ENABLE", "INHERIT", + "NO INHERIT", "RENAME", "RESET", "OWNER TO", "SET", + "VALIDATE CONSTRAINT", "REPLICA IDENTITY", "ATTACH PARTITION", + "DETACH PARTITION", NULL}; + + COMPLETE_WITH_LIST(list_ALTER2); + } + + /* ALTER TABLE xxx ADD */ + else if (IS_REDSHIFT && Matches4("ALTER", "TABLE", MatchAny, "ADD")) + COMPLETE_WITH_LIST2("COLUMN", "PARTITION"); + + else if (IS_REDSHIFT && HeadMatches6("ALTER", "TABLE", MatchAny, "ADD", MatchAny, MatchAny)) + { + if (TailMatches2("COLUMN", MatchAny)) + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_datatypes, NULL); + else + { + if (TailMatches2("DEFAULT", MatchAny)) + COMPLETE_WITH_LIST3("ENCODE", "NOT NULL", "NULL"); + else if (TailMatches1("ENCODE")) + COMPLETE_WITH_QUERY(Query_for_list_of_column_compressions); + else if (TailMatches2("ENCODE", MatchAny)) + COMPLETE_WITH_LIST3("DEFAULT", "NOT NULL", "NULL"); + else if (TailMatches1("NULL")) + COMPLETE_WITH_LIST2("ENCODE", "DEFAULT"); + else if (TailMatches1("NOT")) + COMPLETE_WITH_CONST("NULL"); + else + if (!TailMatches1("DEFAULT")) + COMPLETE_WITH_LIST4("DEFAULT", "ENCODE", "NOT NULL", "NULL"); + } + } + + else if (IS_REDSHIFT && HeadMatches5("ALTER", "TABLE", MatchAny, "ADD", MatchAny) && + (!TailMatches1("COLUMN"))) + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_datatypes, NULL); + + /* ALTER TABLE xxx ENABLE */ + else if (Matches4("ALTER", "TABLE", MatchAny, "ENABLE")) + COMPLETE_WITH_LIST5("ALWAYS", "REPLICA", "ROW LEVEL SECURITY", "RULE", + "TRIGGER"); + else if (Matches5("ALTER", "TABLE", MatchAny, "ENABLE", "REPLICA|ALWAYS")) + COMPLETE_WITH_LIST2("RULE", "TRIGGER"); + else if (Matches5("ALTER", "TABLE", MatchAny, "ENABLE", "RULE")) + { + completion_info_charp = prev3_wd; + COMPLETE_WITH_QUERY(Query_for_rule_of_table); + } + else if (Matches6("ALTER", "TABLE", MatchAny, "ENABLE", MatchAny, "RULE")) + { + completion_info_charp = prev4_wd; + COMPLETE_WITH_QUERY(Query_for_rule_of_table); + } + else if (Matches5("ALTER", "TABLE", MatchAny, "ENABLE", "TRIGGER")) + { + completion_info_charp = prev3_wd; + COMPLETE_WITH_QUERY(Query_for_trigger_of_table); + } + else if (Matches6("ALTER", "TABLE", MatchAny, "ENABLE", MatchAny, "TRIGGER")) + { + completion_info_charp = prev4_wd; + COMPLETE_WITH_QUERY(Query_for_trigger_of_table); + } + /* ALTER TABLE xxx INHERIT */ + else if (Matches4("ALTER", "TABLE", MatchAny, "INHERIT")) + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tables, ""); + /* ALTER TABLE xxx NO INHERIT */ + else if (Matches5("ALTER", "TABLE", MatchAny, "NO", "INHERIT")) + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tables, ""); + /* ALTER TABLE xxx DISABLE */ + else if (Matches4("ALTER", "TABLE", MatchAny, "DISABLE")) + COMPLETE_WITH_LIST3("ROW LEVEL SECURITY", "RULE", "TRIGGER"); + else if (Matches5("ALTER", "TABLE", MatchAny, "DISABLE", "RULE")) + { + completion_info_charp = prev3_wd; + COMPLETE_WITH_QUERY(Query_for_rule_of_table); + } + else if (Matches5("ALTER", "TABLE", MatchAny, "DISABLE", "TRIGGER")) + { + completion_info_charp = prev3_wd; + COMPLETE_WITH_QUERY(Query_for_trigger_of_table); + } + + /* ALTER TABLE xxx ALTER */ + else if (Matches4("ALTER", "TABLE", MatchAny, "ALTER")) + COMPLETE_WITH_ATTR(prev2_wd, " UNION SELECT 'COLUMN' UNION SELECT 'CONSTRAINT'"); + + /* ALTER TABLE xxx RENAME */ + else if (Matches4("ALTER", "TABLE", MatchAny, "RENAME")) + COMPLETE_WITH_ATTR(prev2_wd, " UNION SELECT 'COLUMN' UNION SELECT 'CONSTRAINT' UNION SELECT 'TO'"); + else if (Matches5("ALTER", "TABLE", MatchAny, "ALTER|RENAME", "COLUMN")) + COMPLETE_WITH_ATTR(prev3_wd, ""); + + /* ALTER TABLE xxx RENAME yyy */ + else if (Matches5("ALTER", "TABLE", MatchAny, "RENAME", MatchAnyExcept("CONSTRAINT|TO"))) + COMPLETE_WITH_CONST("TO"); + + /* ALTER TABLE xxx RENAME COLUMN/CONSTRAINT yyy */ + else if (Matches6("ALTER", "TABLE", MatchAny, "RENAME", "COLUMN|CONSTRAINT", MatchAnyExcept("TO"))) + COMPLETE_WITH_CONST("TO"); + + /* If we have ALTER TABLE DROP, provide COLUMN or CONSTRAINT */ + else if (Matches4("ALTER", "TABLE", MatchAny, "DROP")) + COMPLETE_WITH_LIST3("COLUMN", "CONSTRAINT", "PARTITION"); + /* If we have ALTER TABLE DROP COLUMN, provide list of columns */ + else if (Matches5("ALTER", "TABLE", MatchAny, "DROP", "COLUMN")) + COMPLETE_WITH_ATTR(prev3_wd, ""); + + /* + * If we have ALTER TABLE ALTER|DROP|RENAME|VALIDATE CONSTRAINT, + * provide list of constraints + */ + else if (Matches5("ALTER", "TABLE", MatchAny, "ALTER|DROP|RENAME|VALIDATE", "CONSTRAINT")) + { + completion_info_charp = prev3_wd; + COMPLETE_WITH_QUERY(Query_for_constraint_of_table); + } + /* ALTER TABLE ALTER [COLUMN] */ + else if (Matches6("ALTER", "TABLE", MatchAny, "ALTER", "COLUMN", MatchAny) || + Matches5("ALTER", "TABLE", MatchAny, "ALTER", MatchAny)) + COMPLETE_WITH_LIST6("TYPE", "SET", "RESET", "RESTART", "ADD", "DROP"); + /* ALTER TABLE ALTER [COLUMN] SET */ + else if (Matches7("ALTER", "TABLE", MatchAny, "ALTER", "COLUMN", MatchAny, "SET") || + Matches6("ALTER", "TABLE", MatchAny, "ALTER", MatchAny, "SET")) + COMPLETE_WITH_LIST5("(", "DEFAULT", "NOT NULL", "STATISTICS", "STORAGE"); + /* ALTER TABLE ALTER [COLUMN] SET ( */ + else if (Matches8("ALTER", "TABLE", MatchAny, "ALTER", "COLUMN", MatchAny, "SET", "(") || + Matches7("ALTER", "TABLE", MatchAny, "ALTER", MatchAny, "SET", "(")) + COMPLETE_WITH_LIST2("n_distinct", "n_distinct_inherited"); + /* ALTER TABLE ALTER [COLUMN] SET STORAGE */ + else if (Matches8("ALTER", "TABLE", MatchAny, "ALTER", "COLUMN", MatchAny, "SET", "STORAGE") || + Matches7("ALTER", "TABLE", MatchAny, "ALTER", MatchAny, "SET", "STORAGE")) + COMPLETE_WITH_LIST4("PLAIN", "EXTERNAL", "EXTENDED", "MAIN"); + /* ALTER TABLE ALTER [COLUMN] DROP */ + else if (Matches7("ALTER", "TABLE", MatchAny, "ALTER", "COLUMN", MatchAny, "DROP") || + Matches6("ALTER", "TABLE", MatchAny, "ALTER", MatchAny, "DROP")) + COMPLETE_WITH_LIST3("DEFAULT", "IDENTITY", "NOT NULL"); + else if (Matches4("ALTER", "TABLE", MatchAny, "CLUSTER")) + COMPLETE_WITH_CONST("ON"); + else if (Matches5("ALTER", "TABLE", MatchAny, "CLUSTER", "ON")) + { + completion_info_charp = prev3_wd; + COMPLETE_WITH_QUERY(Query_for_index_of_table); + } + /* If we have ALTER TABLE SET, provide list of attributes and '(' */ + else if (Matches4("ALTER", "TABLE", MatchAny, "SET")) + COMPLETE_WITH_LIST10("(", "LOGGED", "LOCATION", "FILE FORMAT", "TABLE PROPERTIES", "SCHEMA", "TABLESPACE", "UNLOGGED", + "WITH", "WITHOUT"); + + /* + * If we have ALTER TABLE SET TABLESPACE provide a list of + * tablespaces + */ + else if (Matches5("ALTER", "TABLE", MatchAny, "SET", "TABLESPACE")) + COMPLETE_WITH_QUERY(Query_for_list_of_tablespaces); + /* If we have ALTER TABLE SET WITH provide OIDS */ + else if (Matches5("ALTER", "TABLE", MatchAny, "SET", "WITH")) + COMPLETE_WITH_CONST("OIDS"); + /* If we have ALTER TABLE SET WITHOUT provide CLUSTER or OIDS */ + else if (Matches5("ALTER", "TABLE", MatchAny, "SET", "WITHOUT")) + COMPLETE_WITH_LIST2("CLUSTER", "OIDS"); + /* ALTER TABLE RESET */ + else if (Matches4("ALTER", "TABLE", MatchAny, "RESET")) + COMPLETE_WITH_CONST("("); + /* ALTER TABLE SET|RESET ( */ + else if (Matches5("ALTER", "TABLE", MatchAny, "SET|RESET", "(")) + { + static const char *const list_TABLEOPTIONS[] = + { + "autovacuum_analyze_scale_factor", + "autovacuum_analyze_threshold", + "autovacuum_enabled", + "autovacuum_freeze_max_age", + "autovacuum_freeze_min_age", + "autovacuum_freeze_table_age", + "autovacuum_multixact_freeze_max_age", + "autovacuum_multixact_freeze_min_age", + "autovacuum_multixact_freeze_table_age", + "autovacuum_vacuum_cost_delay", + "autovacuum_vacuum_cost_limit", + "autovacuum_vacuum_scale_factor", + "autovacuum_vacuum_threshold", + "fillfactor", + "parallel_workers", + "log_autovacuum_min_duration", + "toast.autovacuum_enabled", + "toast.autovacuum_freeze_max_age", + "toast.autovacuum_freeze_min_age", + "toast.autovacuum_freeze_table_age", + "toast.autovacuum_multixact_freeze_max_age", + "toast.autovacuum_multixact_freeze_min_age", + "toast.autovacuum_multixact_freeze_table_age", + "toast.autovacuum_vacuum_cost_delay", + "toast.autovacuum_vacuum_cost_limit", + "toast.autovacuum_vacuum_scale_factor", + "toast.autovacuum_vacuum_threshold", + "toast.log_autovacuum_min_duration", + "user_catalog_table", + NULL + }; + + COMPLETE_WITH_LIST(list_TABLEOPTIONS); + } + else if (Matches7("ALTER", "TABLE", MatchAny, "REPLICA", "IDENTITY", "USING", "INDEX")) + { + completion_info_charp = prev5_wd; + COMPLETE_WITH_QUERY(Query_for_index_of_table); + } + else if (Matches6("ALTER", "TABLE", MatchAny, "REPLICA", "IDENTITY", "USING")) + COMPLETE_WITH_CONST("INDEX"); + else if (Matches5("ALTER", "TABLE", MatchAny, "REPLICA", "IDENTITY")) + COMPLETE_WITH_LIST4("FULL", "NOTHING", "DEFAULT", "USING"); + else if (Matches4("ALTER", "TABLE", MatchAny, "REPLICA")) + COMPLETE_WITH_CONST("IDENTITY"); + + /* + * If we have ALTER TABLE ATTACH PARTITION, provide a list of + * tables. + */ + else if (Matches5("ALTER", "TABLE", MatchAny, "ATTACH", "PARTITION")) + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tables, ""); + /* Limited completion support for partition bound specification */ + else if (TailMatches3("ATTACH", "PARTITION", MatchAny)) + COMPLETE_WITH_LIST2("FOR VALUES", "DEFAULT"); + else if (TailMatches2("FOR", "VALUES")) + COMPLETE_WITH_LIST2("FROM (", "IN ("); + + /* + * If we have ALTER TABLE DETACH PARTITION, provide a list of + * partitions of . + */ + else if (Matches5("ALTER", "TABLE", MatchAny, "DETACH", "PARTITION")) + { + completion_info_charp = prev3_wd; + COMPLETE_WITH_QUERY(Query_for_partition_of_table); + } + + /* ALTER TABLESPACE with RENAME TO, OWNER TO, SET, RESET */ + else if (Matches3("ALTER", "TABLESPACE", MatchAny)) + COMPLETE_WITH_LIST4("RENAME TO", "OWNER TO", "SET", "RESET"); + /* ALTER TABLESPACE SET|RESET */ + else if (Matches4("ALTER", "TABLESPACE", MatchAny, "SET|RESET")) + COMPLETE_WITH_CONST("("); + /* ALTER TABLESPACE SET|RESET ( */ + else if (Matches5("ALTER", "TABLESPACE", MatchAny, "SET|RESET", "(")) + COMPLETE_WITH_LIST3("seq_page_cost", "random_page_cost", + "effective_io_concurrency"); + + /* ALTER TEXT SEARCH */ + else if (Matches3("ALTER", "TEXT", "SEARCH")) + COMPLETE_WITH_LIST4("CONFIGURATION", "DICTIONARY", "PARSER", "TEMPLATE"); + else if (Matches5("ALTER", "TEXT", "SEARCH", "TEMPLATE|PARSER", MatchAny)) + COMPLETE_WITH_LIST2("RENAME TO", "SET SCHEMA"); + else if (Matches5("ALTER", "TEXT", "SEARCH", "DICTIONARY", MatchAny)) + COMPLETE_WITH_LIST3("OWNER TO", "RENAME TO", "SET SCHEMA"); + else if (Matches5("ALTER", "TEXT", "SEARCH", "CONFIGURATION", MatchAny)) + COMPLETE_WITH_LIST6("ADD MAPPING FOR", "ALTER MAPPING", + "DROP MAPPING FOR", + "OWNER TO", "RENAME TO", "SET SCHEMA"); + + /* complete ALTER TYPE with actions */ + else if (Matches3("ALTER", "TYPE", MatchAny)) + COMPLETE_WITH_LIST7("ADD ATTRIBUTE", "ADD VALUE", "ALTER ATTRIBUTE", + "DROP ATTRIBUTE", + "OWNER TO", "RENAME", "SET SCHEMA"); + /* complete ALTER TYPE ADD with actions */ + else if (Matches4("ALTER", "TYPE", MatchAny, "ADD")) + COMPLETE_WITH_LIST2("ATTRIBUTE", "VALUE"); + /* ALTER TYPE RENAME */ + else if (Matches4("ALTER", "TYPE", MatchAny, "RENAME")) + COMPLETE_WITH_LIST3("ATTRIBUTE", "TO", "VALUE"); + /* ALTER TYPE xxx RENAME (ATTRIBUTE|VALUE) yyy */ + else if (Matches6("ALTER", "TYPE", MatchAny, "RENAME", "ATTRIBUTE|VALUE", MatchAny)) + COMPLETE_WITH_CONST("TO"); + + /* + * If we have ALTER TYPE ALTER/DROP/RENAME ATTRIBUTE, provide list + * of attributes + */ + else if (Matches5("ALTER", "TYPE", MatchAny, "ALTER|DROP|RENAME", "ATTRIBUTE")) + COMPLETE_WITH_ATTR(prev3_wd, ""); + /* ALTER TYPE ALTER ATTRIBUTE */ + else if (Matches6("ALTER", "TYPE", MatchAny, "ALTER", "ATTRIBUTE", MatchAny)) + COMPLETE_WITH_CONST("TYPE"); + /* complete ALTER GROUP */ + else if (Matches3("ALTER", "GROUP", MatchAny)) + COMPLETE_WITH_LIST3("ADD USER", "DROP USER", "RENAME TO"); + /* complete ALTER GROUP ADD|DROP with USER */ + else if (Matches4("ALTER", "GROUP", MatchAny, "ADD|DROP")) + COMPLETE_WITH_CONST("USER"); + /* complete ALTER GROUP ADD|DROP USER with a user name */ + else if (Matches5("ALTER", "GROUP", MatchAny, "ADD|DROP", "USER")) + COMPLETE_WITH_QUERY(Query_for_list_of_roles); + + /* + * If we have ALTER TYPE RENAME VALUE, provide list of enum values + */ + else if (Matches5("ALTER", "TYPE", MatchAny, "RENAME", "VALUE")) + COMPLETE_WITH_ENUM_VALUE(prev3_wd); + +/* BEGIN */ + else if (Matches1("BEGIN")) + COMPLETE_WITH_LIST6("WORK", "TRANSACTION", "ISOLATION LEVEL", "READ", "DEFERRABLE", "NOT DEFERRABLE"); +/* END, ABORT */ + else if (Matches1("END|ABORT")) + COMPLETE_WITH_LIST2("WORK", "TRANSACTION"); +/* COMMIT */ + else if (Matches1("COMMIT")) + COMPLETE_WITH_LIST3("WORK", "TRANSACTION", "PREPARED"); +/* RELEASE SAVEPOINT */ + else if (Matches1("RELEASE")) + COMPLETE_WITH_CONST("SAVEPOINT"); +/* ROLLBACK */ + else if (Matches1("ROLLBACK")) + COMPLETE_WITH_LIST4("WORK", "TRANSACTION", "TO SAVEPOINT", "PREPARED"); +/* CLUSTER */ + else if (Matches1("CLUSTER")) + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tm, "UNION SELECT 'VERBOSE'"); + else if (Matches2("CLUSTER", "VERBOSE")) + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tm, NULL); + /* If we have CLUSTER , then add "USING" */ + else if (Matches2("CLUSTER", MatchAnyExcept("VERBOSE|ON"))) + COMPLETE_WITH_CONST("USING"); + /* If we have CLUSTER VERBOSE , then add "USING" */ + else if (Matches3("CLUSTER", "VERBOSE", MatchAny)) + COMPLETE_WITH_CONST("USING"); + /* If we have CLUSTER USING, then add the index as well */ + else if (Matches3("CLUSTER", MatchAny, "USING") || + Matches4("CLUSTER", "VERBOSE", MatchAny, "USING")) + { + completion_info_charp = prev2_wd; + COMPLETE_WITH_QUERY(Query_for_index_of_table); + } +/* CANCEL */ + else if (IS_REDSHIFT && Matches1("CANCEL")) + COMPLETE_WITH_QUERY(Query_for_list_of_pids); + +/* CLOSE */ + else if (Matches1("CLOSE")) + COMPLETE_WITH_QUERY(Query_for_list_of_cursors); + +/* COMMENT */ + else if (Matches1("COMMENT")) + COMPLETE_WITH_CONST("ON"); + else if (Matches2("COMMENT", "ON")) + { + static const char *const list_COMMENT[] = + {"ACCESS METHOD", "CAST", "COLLATION", "CONVERSION", "DATABASE", + "EVENT TRIGGER", "EXTENSION", + "FOREIGN DATA WRAPPER", "FOREIGN TABLE", + "SERVER", "INDEX", "LANGUAGE", "POLICY", "PUBLICATION", "RULE", + "SCHEMA", "SEQUENCE", "STATISTICS", "SUBSCRIPTION", + "TABLE", "TYPE", "VIEW", "MATERIALIZED VIEW", "COLUMN", "AGGREGATE", "FUNCTION", + "OPERATOR", "TRIGGER", "CONSTRAINT", "DOMAIN", "LARGE OBJECT", + "TABLESPACE", "TEXT SEARCH", "ROLE", NULL}; + + COMPLETE_WITH_LIST(list_COMMENT); + } + else if (Matches4("COMMENT", "ON", "ACCESS", "METHOD")) + COMPLETE_WITH_QUERY(Query_for_list_of_access_methods); + else if (Matches3("COMMENT", "ON", "FOREIGN")) + COMPLETE_WITH_LIST2("DATA WRAPPER", "TABLE"); + else if (Matches4("COMMENT", "ON", "TEXT", "SEARCH")) + COMPLETE_WITH_LIST4("CONFIGURATION", "DICTIONARY", "PARSER", "TEMPLATE"); + else if (Matches3("COMMENT", "ON", "CONSTRAINT")) + COMPLETE_WITH_QUERY(Query_for_all_table_constraints); + else if (Matches4("COMMENT", "ON", "CONSTRAINT", MatchAny)) + COMPLETE_WITH_CONST("ON"); + else if (Matches5("COMMENT", "ON", "CONSTRAINT", MatchAny, "ON")) + { + completion_info_charp = prev2_wd; + COMPLETE_WITH_QUERY(Query_for_list_of_tables_for_constraint); + } + else if (Matches4("COMMENT", "ON", "MATERIALIZED", "VIEW")) + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_matviews, NULL); + else if (Matches4("COMMENT", "ON", "EVENT", "TRIGGER")) + COMPLETE_WITH_QUERY(Query_for_list_of_event_triggers); + else if (Matches4("COMMENT", "ON", MatchAny, MatchAnyExcept("IS")) || + Matches5("COMMENT", "ON", MatchAny, MatchAny, MatchAnyExcept("IS")) || + Matches6("COMMENT", "ON", MatchAny, MatchAny, MatchAny, MatchAnyExcept("IS"))) + COMPLETE_WITH_CONST("IS"); + +/* COPY */ + + /* + * If we have COPY, offer list of tables or "(" (Also cover the analogous + * backslash command). + */ + else if (Matches1("COPY|\\copy")) + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tables, + " UNION ALL SELECT '('"); + /* If we have COPY BINARY, complete with list of tables */ + else if (Matches2("COPY", "BINARY")) + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tables, NULL); + /* If we have COPY (, complete it with legal commands */ + else if (Matches2("COPY|\\copy", "(")) + COMPLETE_WITH_LIST7("SELECT", "TABLE", "VALUES", "INSERT", "UPDATE", "DELETE", "WITH"); + /* If we have COPY [BINARY] , complete it with "TO" or "FROM" */ + else if (Matches2("COPY|\\copy", MatchAny) || + Matches3("COPY", "BINARY", MatchAny)) + COMPLETE_WITH_LIST2("FROM", "TO"); + /* If we have COPY [BINARY] FROM|TO, complete with filename */ + else if (Matches3("COPY|\\copy", MatchAny, "FROM|TO") || + Matches4("COPY", "BINARY", MatchAny, "FROM|TO")) + { + completion_charp = ""; + matches = completion_matches(text, complete_from_files); + } + + /* Handle COPY [BINARY] FROM|TO filename */ + else if (Matches4("COPY|\\copy", MatchAny, "FROM|TO", MatchAny) || + Matches5("COPY", "BINARY", MatchAny, "FROM|TO", MatchAny)) + COMPLETE_WITH_LIST6("BINARY", "OIDS", "DELIMITER", "NULL", "CSV", + "ENCODING"); + + /* Handle COPY [BINARY] FROM|TO filename CSV */ + else if (Matches5("COPY|\\copy", MatchAny, "FROM|TO", MatchAny, "CSV") || + Matches6("COPY", "BINARY", MatchAny, "FROM|TO", MatchAny, "CSV")) + COMPLETE_WITH_LIST5("HEADER", "QUOTE", "ESCAPE", "FORCE QUOTE", + "FORCE NOT NULL"); + + /* COPY FROM REGION */ + else if (IS_REDSHIFT && HeadMatches2("COPY", MatchAny) && TailMatches1("REGION")) + COMPLETE_WITH_QUERY(Query_for_list_of_aws_regions); + + + /* CREATE ACCESS METHOD */ + /* Complete "CREATE ACCESS METHOD " */ + else if (Matches4("CREATE", "ACCESS", "METHOD", MatchAny)) + COMPLETE_WITH_CONST("TYPE"); + /* Complete "CREATE ACCESS METHOD TYPE" */ + else if (Matches5("CREATE", "ACCESS", "METHOD", MatchAny, "TYPE")) + COMPLETE_WITH_CONST("INDEX"); + /* Complete "CREATE ACCESS METHOD TYPE " */ + else if (Matches6("CREATE", "ACCESS", "METHOD", MatchAny, "TYPE", MatchAny)) + COMPLETE_WITH_CONST("HANDLER"); + + /* CREATE DATABASE */ + else if (Matches3("CREATE", "DATABASE", MatchAny)) + COMPLETE_WITH_LIST10("WITH", "OWNER", "TEMPLATE", "ENCODING", "TABLESPACE", + "IS_TEMPLATE", + "ALLOW_CONNECTIONS", "CONNECTION LIMIT", + "LC_COLLATE", "LC_CTYPE"); + + /* CREATE DATABASE WITH */ + else if (Matches4("CREATE", "DATABASE", MatchAny, "WITH")) + COMPLETE_WITH_LIST9("OWNER", "TEMPLATE", "ENCODING", "TABLESPACE", + "IS_TEMPLATE", + "ALLOW_CONNECTIONS", "CONNECTION LIMIT", + "LC_COLLATE", "LC_CTYPE"); + + /* CREATE DATABASE WITH OWNER */ + else if (Matches5("CREATE", "DATABASE", MatchAny, "WITH", "OWNER")) + COMPLETE_WITH_LIST8("TEMPLATE", "ENCODING", "TABLESPACE", + "IS_TEMPLATE", + "ALLOW_CONNECTIONS", "CONNECTION LIMIT", + "LC_COLLATE", "LC_CTYPE"); + + /* CREATE DATABASE OWNER */ + else if (Matches4("CREATE", "DATABASE", MatchAny, "OWNER")) + COMPLETE_WITH_LIST8("TEMPLATE", "ENCODING", "TABLESPACE", + "IS_TEMPLATE", + "ALLOW_CONNECTIONS", "CONNECTION LIMIT", + "LC_COLLATE", "LC_CTYPE"); + + /* CONNECTION LIMIT -- is allowed inside CREATE DATABASE, so use TailMatches */ + else if (TailMatches2("CONNECTION", "LIMIT")) + COMPLETE_WITH_CONST("UNLIMITED"); + + else if (Matches4("CREATE", "DATABASE", MatchAny, "TEMPLATE")) + COMPLETE_WITH_QUERY(Query_for_list_of_template_databases); + + /* CREATE EXTENSION */ + /* Complete with available extensions rather than installed ones. */ + else if (Matches2("CREATE", "EXTENSION")) + COMPLETE_WITH_QUERY(Query_for_list_of_available_extensions); + /* CREATE EXTENSION */ + else if (Matches3("CREATE", "EXTENSION", MatchAny)) + COMPLETE_WITH_LIST3("WITH SCHEMA", "CASCADE", "VERSION"); + /* CREATE EXTENSION VERSION */ + else if (Matches4("CREATE", "EXTENSION", MatchAny, "VERSION")) + { + completion_info_charp = prev2_wd; + COMPLETE_WITH_QUERY(Query_for_list_of_available_extension_versions); + } + + else if (IS_REDSHIFT && (Matches2("CREATE", "EXTERNAL"))) + COMPLETE_WITH_LIST2("SCHEMA", "TABLE"); + + /* CREATE EXTERNAL SCHEMA */ + else if (IS_REDSHIFT && (Matches3("CREATE", "EXTERNAL", "SCHEMA"))) { + if (Matches2("CREATE", "EXTERNAL")) + COMPLETE_WITH_LIST2("SCHEMA", "TABLE"); + else if (Matches3("CREATE", "EXTERNAL", "SCHEMA")) + COMPLETE_WITH_QUERY(Query_for_list_of_external_schemas + " UNION ALL SELECT 'IF NOT EXISTS'"); + else if ( + (Matches7("CREATE", "EXTERNAL", "SCHEMA", "IF", "NOT", "EXISTS", MatchAnyExcept("FROM"))) || + (Matches4("CREATE", "EXTERNAL", "SCHEMA", MatchAnyExcept("FROM"))) + ) + COMPLETE_WITH_CONST("FROM"); + else if (HeadMatches3("CREATE", "EXTERNAL", "SCHEMA") && TailMatches1("FROM")) + COMPLETE_WITH_LIST2("DATA CATALOG", "HIVE METASTORE"); + else if (HeadMatches3("CREATE", "EXTERNAL", "SCHEMA") && + (TailMatches2("DATA", "CATALOG") || TailMatches2("HIVE", "METASTORE")) + ) + COMPLETE_WITH_CONST("DATABASE"); + else if (HeadMatches3("CREATE", "EXTERNAL", "SCHEMA") && TailMatches2("DATABASE", MatchAny)) + COMPLETE_WITH_LIST4("REGION", "URI", "IAM_ROLE", "CREATE EXTERNAL DATABASE IF NOT EXISTS"); + else if (HeadMatches3("CREATE", "EXTERNAL", "SCHEMA") && TailMatches1("REGION")) + COMPLETE_WITH_QUERY(Query_for_list_of_aws_regions); + else if (HeadMatches3("CREATE", "EXTERNAL", "SCHEMA") && TailMatches2("REGION", MatchAny)) + COMPLETE_WITH_LIST3("URI", "IAM_ROLE", "CREATE EXTERNAL DATABASE IF NOT EXISTS"); + else if (HeadMatches3("CREATE", "EXTERNAL", "SCHEMA") && TailMatches2("URI", MatchAny)) + COMPLETE_WITH_LIST3("PORT", "IAM_ROLE", "CREATE EXTERNAL DATABASE IF NOT EXISTS"); + else if (HeadMatches3("CREATE", "EXTERNAL", "SCHEMA") && TailMatches2("PORT", MatchAny)) + COMPLETE_WITH_LIST2("IAM_ROLE", "CREATE EXTERNAL DATABASE IF NOT EXISTS"); + else if (HeadMatches3("CREATE", "EXTERNAL", "SCHEMA") && TailMatches2("IAM_ROLE", MatchAny)) + COMPLETE_WITH_CONST("CREATE EXTERNAL DATABASE IF NOT EXISTS"); + } + + /* CREATE EXTERNAL TABLE */ + else if (IS_REDSHIFT && (Matches3("CREATE", "EXTERNAL", "TABLE"))) { + if (Matches3("CREATE", "EXTERNAL", "TABLE")) + COMPLETE_WITH_QUERY(Query_for_list_of_external_schemas); + else if ((HeadMatches3("CREATE", "EXTERNAL", "TABLE")) && TailMatches1("PARTITIONED")) + COMPLETE_WITH_CONST("BY ("); + else if ((HeadMatches3("CREATE", "EXTERNAL", "TABLE")) && TailMatches1("ROW")) + COMPLETE_WITH_LIST2("FORMAT", "DELIMITED"); + else if ((HeadMatches3("CREATE", "EXTERNAL", "TABLE")) && TailMatches3("ROW", "FORMAT", "DELIMITED")) + COMPLETE_WITH_LIST2("FIELDS TERMINATED BY", "LINES TERMINATED BY"); + else if ((HeadMatches3("CREATE", "EXTERNAL", "TABLE")) && TailMatches3("ROW", "FORMAT", "DELIMITED")) + COMPLETE_WITH_LIST2("FIELDS TERMINATED BY", "LINES TERMINATED BY"); + else if ((HeadMatches3("CREATE", "EXTERNAL", "TABLE")) && TailMatches2("STORED", "AS")) + COMPLETE_WITH_LIST5("PARQUET", "RCFILE", "SEQUENCEFILE", "TEXTFILE", "ORC"); + else if ((HeadMatches3("CREATE", "EXTERNAL", "TABLE")) && TailMatches1("PROPERTIES")) + COMPLETE_WITH_CONST("('numRows'='"); + else if (HeadMatches4("CREATE", "EXTERNAL", "TABLE", MatchAny)) + COMPLETE_WITH_LIST5("PARTITIONED BY (", "ROW FORMAT DELIMITED", "STORED AS", "LOCATION", "TABLE PROPERTIES ('numRows'='"); + } + + /* CREATE FOREIGN */ + else if (Matches2("CREATE", "FOREIGN")) + COMPLETE_WITH_LIST2("DATA WRAPPER", "TABLE"); + + /* CREATE FOREIGN DATA WRAPPER */ + else if (Matches5("CREATE", "FOREIGN", "DATA", "WRAPPER", MatchAny)) + COMPLETE_WITH_LIST3("HANDLER", "VALIDATOR", "OPTIONS"); + + /* CREATE INDEX --- is allowed inside CREATE SCHEMA, so use TailMatches */ + /* First off we complete CREATE UNIQUE with "INDEX" */ + else if (TailMatches2("CREATE", "UNIQUE")) + COMPLETE_WITH_CONST("INDEX"); + + /* + * If we have CREATE|UNIQUE INDEX, then add "ON", "CONCURRENTLY", and + * existing indexes + */ + else if (TailMatches2("CREATE|UNIQUE", "INDEX")) + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_indexes, + " UNION SELECT 'ON'" + " UNION SELECT 'CONCURRENTLY'"); + /* Complete ... INDEX|CONCURRENTLY [] ON with a list of tables */ + else if (TailMatches3("INDEX|CONCURRENTLY", MatchAny, "ON") || + TailMatches2("INDEX|CONCURRENTLY", "ON")) + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tm, NULL); + + /* + * Complete CREATE|UNIQUE INDEX CONCURRENTLY with "ON" and existing + * indexes + */ + else if (TailMatches3("CREATE|UNIQUE", "INDEX", "CONCURRENTLY")) + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_indexes, + " UNION SELECT 'ON'"); + /* Complete CREATE|UNIQUE INDEX [CONCURRENTLY] with "ON" */ + else if (TailMatches3("CREATE|UNIQUE", "INDEX", MatchAny) || + TailMatches4("CREATE|UNIQUE", "INDEX", "CONCURRENTLY", MatchAny)) + COMPLETE_WITH_CONST("ON"); + + /* + * Complete INDEX ON
with a list of table columns (which + * should really be in parens) + */ + else if (TailMatches4("INDEX", MatchAny, "ON", MatchAny) || + TailMatches3("INDEX|CONCURRENTLY", "ON", MatchAny)) + COMPLETE_WITH_LIST2("(", "USING"); + else if (TailMatches5("INDEX", MatchAny, "ON", MatchAny, "(") || + TailMatches4("INDEX|CONCURRENTLY", "ON", MatchAny, "(")) + COMPLETE_WITH_ATTR(prev2_wd, ""); + /* same if you put in USING */ + else if (TailMatches5("ON", MatchAny, "USING", MatchAny, "(")) + COMPLETE_WITH_ATTR(prev4_wd, ""); + /* Complete USING with an index method */ + else if (TailMatches6("INDEX", MatchAny, MatchAny, "ON", MatchAny, "USING") || + TailMatches5("INDEX", MatchAny, "ON", MatchAny, "USING") || + TailMatches4("INDEX", "ON", MatchAny, "USING")) + COMPLETE_WITH_QUERY(Query_for_list_of_access_methods); + else if (TailMatches4("ON", MatchAny, "USING", MatchAny) && + !TailMatches6("POLICY", MatchAny, MatchAny, MatchAny, MatchAny, MatchAny) && + !TailMatches4("FOR", MatchAny, MatchAny, MatchAny)) + COMPLETE_WITH_CONST("("); + +/* CREATE LIBRARY */ + else if (Matches2("CREATE", "LIBRARY")) + COMPLETE_WITH_QUERY(Query_for_list_of_libraries); + else if (Matches4("CREATE", "OR", "REPLACE", "LIBRARY")) + COMPLETE_WITH_QUERY(Query_for_list_of_libraries); + else if (Matches5("CREATE", "OR", "REPLACE", "LIBRARY", MatchAny)) + COMPLETE_WITH_CONST("LANGUAGE plpythonu FROM"); + else if (Matches1("CREATE") && TailMatches5("LIBRARY", "LANGUAGE", "plpythonu", "FROM", MatchAny)) + COMPLETE_WITH_CONST("AUTHORIZATION"); + else if (Matches1("CREATE") && TailMatches6("LIBRARY", "LANGUAGE", "plpythonu", "FROM", MatchAny, "AUTHORIZATION")) + COMPLETE_WITH_CONST("REGION"); + else if (Matches1("CREATE") && TailMatches7("LIBRARY", "LANGUAGE", "plpythonu", "FROM", MatchAny, "AUTHORIZATION", "REGION")) + COMPLETE_WITH_QUERY(Query_for_list_of_aws_regions); + else if (Matches1("CREATE") && TailMatches8("LIBRARY", "LANGUAGE", "plpythonu", "FROM", MatchAny, "AUTHORIZATION", "REGION", "AS")) + COMPLETE_WITH_QUERY(Query_for_list_of_aws_regions); + + // XXX See if you can add Regions here!? + + /* CREATE POLICY */ + /* Complete "CREATE POLICY ON" */ + else if (Matches3("CREATE", "POLICY", MatchAny)) + COMPLETE_WITH_CONST("ON"); + /* Complete "CREATE POLICY ON
" */ + else if (Matches4("CREATE", "POLICY", MatchAny, "ON")) + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tables, NULL); + /* Complete "CREATE POLICY ON
AS|FOR|TO|USING|WITH CHECK" */ + else if (Matches5("CREATE", "POLICY", MatchAny, "ON", MatchAny)) + COMPLETE_WITH_LIST5("AS", "FOR", "TO", "USING (", "WITH CHECK ("); + /* CREATE POLICY ON
AS PERMISSIVE|RESTRICTIVE */ + else if (Matches6("CREATE", "POLICY", MatchAny, "ON", MatchAny, "AS")) + COMPLETE_WITH_LIST2("PERMISSIVE", "RESTRICTIVE"); + + /* + * CREATE POLICY ON
AS PERMISSIVE|RESTRICTIVE + * FOR|TO|USING|WITH CHECK + */ + else if (Matches7("CREATE", "POLICY", MatchAny, "ON", MatchAny, "AS", MatchAny)) + COMPLETE_WITH_LIST4("FOR", "TO", "USING", "WITH CHECK"); + /* CREATE POLICY ON
FOR ALL|SELECT|INSERT|UPDATE|DELETE */ + else if (Matches6("CREATE", "POLICY", MatchAny, "ON", MatchAny, "FOR")) + COMPLETE_WITH_LIST5("ALL", "SELECT", "INSERT", "UPDATE", "DELETE"); + /* Complete "CREATE POLICY ON
FOR INSERT TO|WITH CHECK" */ + else if (Matches7("CREATE", "POLICY", MatchAny, "ON", MatchAny, "FOR", "INSERT")) + COMPLETE_WITH_LIST2("TO", "WITH CHECK ("); + /* Complete "CREATE POLICY ON
FOR SELECT|DELETE TO|USING" */ + else if (Matches7("CREATE", "POLICY", MatchAny, "ON", MatchAny, "FOR", "SELECT|DELETE")) + COMPLETE_WITH_LIST2("TO", "USING ("); + /* CREATE POLICY ON
FOR ALL|UPDATE TO|USING|WITH CHECK */ + else if (Matches7("CREATE", "POLICY", MatchAny, "ON", MatchAny, "FOR", "ALL|UPDATE")) + COMPLETE_WITH_LIST3("TO", "USING (", "WITH CHECK ("); + /* Complete "CREATE POLICY ON
TO " */ + else if (Matches6("CREATE", "POLICY", MatchAny, "ON", MatchAny, "TO")) + COMPLETE_WITH_QUERY(Query_for_list_of_grant_roles); + /* Complete "CREATE POLICY ON
USING (" */ + else if (Matches6("CREATE", "POLICY", MatchAny, "ON", MatchAny, "USING")) + COMPLETE_WITH_CONST("("); + + /* + * CREATE POLICY ON
AS PERMISSIVE|RESTRICTIVE FOR + * ALL|SELECT|INSERT|UPDATE|DELETE + */ + else if (Matches8("CREATE", "POLICY", MatchAny, "ON", MatchAny, "AS", MatchAny, "FOR")) + COMPLETE_WITH_LIST5("ALL", "SELECT", "INSERT", "UPDATE", "DELETE"); + + /* + * Complete "CREATE POLICY ON
AS PERMISSIVE|RESTRICTIVE FOR + * INSERT TO|WITH CHECK" + */ + else if (Matches9("CREATE", "POLICY", MatchAny, "ON", MatchAny, "AS", MatchAny, "FOR", "INSERT")) + COMPLETE_WITH_LIST2("TO", "WITH CHECK ("); + + /* + * Complete "CREATE POLICY ON
AS PERMISSIVE|RESTRICTIVE FOR + * SELECT|DELETE TO|USING" + */ + else if (Matches9("CREATE", "POLICY", MatchAny, "ON", MatchAny, "AS", MatchAny, "FOR", "SELECT|DELETE")) + COMPLETE_WITH_LIST2("TO", "USING ("); + + /* + * CREATE POLICY ON
AS PERMISSIVE|RESTRICTIVE FOR + * ALL|UPDATE TO|USING|WITH CHECK + */ + else if (Matches9("CREATE", "POLICY", MatchAny, "ON", MatchAny, "AS", MatchAny, "FOR", "ALL|UPDATE")) + COMPLETE_WITH_LIST3("TO", "USING (", "WITH CHECK ("); + + /* + * Complete "CREATE POLICY ON
AS PERMISSIVE|RESTRICTIVE TO + * " + */ + else if (Matches8("CREATE", "POLICY", MatchAny, "ON", MatchAny, "AS", MatchAny, "TO")) + COMPLETE_WITH_QUERY(Query_for_list_of_grant_roles); + + /* + * Complete "CREATE POLICY ON
AS PERMISSIVE|RESTRICTIVE + * USING (" + */ + else if (Matches8("CREATE", "POLICY", MatchAny, "ON", MatchAny, "AS", MatchAny, "USING")) + COMPLETE_WITH_CONST("("); + + +/* CREATE PUBLICATION */ + else if (Matches3("CREATE", "PUBLICATION", MatchAny)) + COMPLETE_WITH_LIST3("FOR TABLE", "FOR ALL TABLES", "WITH ("); + else if (Matches4("CREATE", "PUBLICATION", MatchAny, "FOR")) + COMPLETE_WITH_LIST2("TABLE", "ALL TABLES"); + /* Complete "CREATE PUBLICATION FOR TABLE
" */ + else if (Matches4("CREATE", "PUBLICATION", MatchAny, "FOR TABLE")) + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tables, NULL); + /* Complete "CREATE PUBLICATION [...] WITH" */ + else if (HeadMatches2("CREATE", "PUBLICATION") && TailMatches2("WITH", "(")) + COMPLETE_WITH_CONST("publish"); + +/* CREATE RULE */ + /* Complete "CREATE RULE " with "AS ON" */ + else if (Matches3("CREATE", "RULE", MatchAny)) + COMPLETE_WITH_CONST("AS ON"); + /* Complete "CREATE RULE AS" with "ON" */ + else if (Matches4("CREATE", "RULE", MatchAny, "AS")) + COMPLETE_WITH_CONST("ON"); + /* Complete "CREATE RULE AS ON" with SELECT|UPDATE|INSERT|DELETE */ + else if (Matches5("CREATE", "RULE", MatchAny, "AS", "ON")) + COMPLETE_WITH_LIST4("SELECT", "UPDATE", "INSERT", "DELETE"); + /* Complete "AS ON SELECT|UPDATE|INSERT|DELETE" with a "TO" */ + else if (TailMatches3("AS", "ON", "SELECT|UPDATE|INSERT|DELETE")) + COMPLETE_WITH_CONST("TO"); + /* Complete "AS ON TO" with a table name */ + else if (TailMatches4("AS", "ON", "SELECT|UPDATE|INSERT|DELETE", "TO")) + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tables, NULL); + +/* CREATE SCHEMA */ + else if (Matches2("CREATE", "SCHEMA")) + COMPLETE_WITH_LIST2("AUTHORIZATION", "IF NOT EXISTS"); + else if (Matches3("CREATE", "SCHEMA", MatchAny)) + COMPLETE_WITH_CONST("AUTHORIZATION"); + else if (Matches4("CREATE", "SCHEMA", "IF NOT EXISTS", MatchAny)) + COMPLETE_WITH_CONST("AUTHORIZATION"); + + +/* CREATE SEQUENCE --- is allowed inside CREATE SCHEMA, so use TailMatches */ + else if (TailMatches3("CREATE", "SEQUENCE", MatchAny) || + TailMatches4("CREATE", "TEMP|TEMPORARY", "SEQUENCE", MatchAny)) + COMPLETE_WITH_LIST8("INCREMENT BY", "MINVALUE", "MAXVALUE", "NO", "CACHE", + "CYCLE", "OWNED BY", "START WITH"); + else if (TailMatches4("CREATE", "SEQUENCE", MatchAny, "NO") || + TailMatches5("CREATE", "TEMP|TEMPORARY", "SEQUENCE", MatchAny, "NO")) + COMPLETE_WITH_LIST3("MINVALUE", "MAXVALUE", "CYCLE"); + +/* CREATE SERVER */ + else if (Matches3("CREATE", "SERVER", MatchAny)) + COMPLETE_WITH_LIST3("TYPE", "VERSION", "FOREIGN DATA WRAPPER"); + +/* CREATE STATISTICS */ + else if (Matches3("CREATE", "STATISTICS", MatchAny)) + COMPLETE_WITH_LIST2("(", "ON"); + else if (Matches4("CREATE", "STATISTICS", MatchAny, "(")) + COMPLETE_WITH_LIST2("ndistinct", "dependencies"); + else if (HeadMatches3("CREATE", "STATISTICS", MatchAny) && + previous_words[0][0] == '(' && + previous_words[0][strlen(previous_words[0]) - 1] == ')') + COMPLETE_WITH_CONST("ON"); + else if (HeadMatches3("CREATE", "STATISTICS", MatchAny) && + TailMatches1("FROM")) + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tables, NULL); + + /* Complete "CREATE TEMP/TEMPORARY" with the possible temp objects */ + else if (HeadMatches2("CREATE", "TEMP|TEMPORARY")) + COMPLETE_WITH_LIST3("SEQUENCE", "TABLE", "VIEW"); + + /* Complete "CREATE UNLOGGED" with TABLE or MATVIEW */ + else if (TailMatches2("CREATE", "UNLOGGED")) + COMPLETE_WITH_LIST2("TABLE", "MATERIALIZED VIEW"); + + /* CREATE TABLE is allowed inside CREATE SCHEMA in Native Postgres so although + we ought to use TailMatches, using HeadMatches since this allows supporting + far more options */ + else if (Matches2("CREATE", "TABLE") || + Matches3("CREATE", "TEMP|TEMPORARY", "TABLE") || + Matches4("CREATE", "LOCAL", "TEMP|TEMPORARY", "TABLE")) + COMPLETE_WITH_LIST2("IF NOT EXISTS", "AS"); + + else if (Matches3("CREATE", "TABLE", MatchAny)) + COMPLETE_WITH_CONST("("); + + else if (HeadMatches2("CREATE", "TABLE") /*|| + HeadMatches3("CREATE", "TEMP|TEMPORARY", "TABLE") || + HeadMatches4("CREATE", "LOCAL", "TEMP|TEMPORARY", "TABLE")*/) + { + //if (TailMatches3(MatchAnyExcept("("), MatchAny, ")")) + if (TailMatches1("BACKUP")) + COMPLETE_WITH_LIST2("YES", "NO"); + else if (TailMatches2("BACKUP", "YES|NO")) + COMPLETE_WITH_LIST5("DISTSTYLE", "DISTKEY (", "COMPOUND", "INTERLEAVED", "SORTKEY ("); + else if (TailMatches1("DISTSTYLE")) + COMPLETE_WITH_LIST3("EVEN", "KEY", "ALL"); + else if (TailMatches2("DISTSTYLE", "EVEN|KEY|ALL")) + COMPLETE_WITH_LIST3("COMPOUND", "INTERLEAVED", "SORTKEY ("); + else if (TailMatches2("DISTKEY", MatchAny)) + COMPLETE_WITH_LIST3("COMPOUND", "INTERLEAVED", "SORTKEY ("); + else if (TailMatches1("COMPOUND|INTERLEAVED")) + COMPLETE_WITH_CONST("SORTKEY ("); + else + COMPLETE_WITH_LIST6("BACKUP", "DISTSTYLE", "DISTKEY (", "COMPOUND", "INTERLEAVED", "SORTKEY ("); + } + + /* Complete PARTITION BY with RANGE ( or LIST ( or ... */ + else if (TailMatches2("PARTITION", "BY")) + COMPLETE_WITH_LIST2("RANGE (", "LIST ("); + /* If we have xxx PARTITION OF, provide a list of partitioned tables */ + else if (TailMatches2("PARTITION", "OF")) + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_partitioned_tables, ""); + /* Limited completion support for partition bound specification */ + else if (TailMatches3("PARTITION", "OF", MatchAny)) + COMPLETE_WITH_LIST2("FOR VALUES", "DEFAULT"); + +/* CREATE TABLESPACE */ + else if (Matches3("CREATE", "TABLESPACE", MatchAny)) + COMPLETE_WITH_LIST2("OWNER", "LOCATION"); + /* Complete CREATE TABLESPACE name OWNER name with "LOCATION" */ + else if (Matches5("CREATE", "TABLESPACE", MatchAny, "OWNER", MatchAny)) + COMPLETE_WITH_CONST("LOCATION"); + +/* CREATE TEXT SEARCH */ + else if (Matches3("CREATE", "TEXT", "SEARCH")) + COMPLETE_WITH_LIST4("CONFIGURATION", "DICTIONARY", "PARSER", "TEMPLATE"); + else if (Matches5("CREATE", "TEXT", "SEARCH", "CONFIGURATION", MatchAny)) + COMPLETE_WITH_CONST("("); + +/* CREATE SUBSCRIPTION */ + else if (Matches3("CREATE", "SUBSCRIPTION", MatchAny)) + COMPLETE_WITH_CONST("CONNECTION"); + else if (Matches5("CREATE", "SUBSCRIPTION", MatchAny, "CONNECTION", MatchAny)) + COMPLETE_WITH_CONST("PUBLICATION"); + else if (Matches6("CREATE", "SUBSCRIPTION", MatchAny, "CONNECTION", + MatchAny, "PUBLICATION")) + { + /* complete with nothing here as this refers to remote publications */ + } + else if (HeadMatches2("CREATE", "SUBSCRIPTION") && TailMatches2("PUBLICATION", MatchAny)) + COMPLETE_WITH_CONST("WITH ("); + /* Complete "CREATE SUBSCRIPTION ... WITH ( " */ + else if (HeadMatches2("CREATE", "SUBSCRIPTION") && TailMatches2("WITH", "(")) + COMPLETE_WITH_LIST6("copy_data", "connect", "create_slot", "enabled", + "slot_name", "synchronous_commit"); + +/* CREATE TRIGGER --- is allowed inside CREATE SCHEMA, so use TailMatches */ + /* complete CREATE TRIGGER with BEFORE,AFTER,INSTEAD OF */ + else if (TailMatches3("CREATE", "TRIGGER", MatchAny)) + COMPLETE_WITH_LIST3("BEFORE", "AFTER", "INSTEAD OF"); + /* complete CREATE TRIGGER BEFORE,AFTER with an event */ + else if (TailMatches4("CREATE", "TRIGGER", MatchAny, "BEFORE|AFTER")) + COMPLETE_WITH_LIST4("INSERT", "DELETE", "UPDATE", "TRUNCATE"); + /* complete CREATE TRIGGER INSTEAD OF with an event */ + else if (TailMatches5("CREATE", "TRIGGER", MatchAny, "INSTEAD", "OF")) + COMPLETE_WITH_LIST3("INSERT", "DELETE", "UPDATE"); + /* complete CREATE TRIGGER BEFORE,AFTER sth with OR,ON */ + else if (TailMatches5("CREATE", "TRIGGER", MatchAny, "BEFORE|AFTER", MatchAny) || + TailMatches6("CREATE", "TRIGGER", MatchAny, "INSTEAD", "OF", MatchAny)) + COMPLETE_WITH_LIST2("ON", "OR"); + + /* + * complete CREATE TRIGGER BEFORE,AFTER event ON with a list of + * tables + */ + else if (TailMatches6("CREATE", "TRIGGER", MatchAny, "BEFORE|AFTER", MatchAny, "ON")) + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tables, NULL); + /* complete CREATE TRIGGER ... INSTEAD OF event ON with a list of views */ + else if (TailMatches7("CREATE", "TRIGGER", MatchAny, "INSTEAD", "OF", MatchAny, "ON")) + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_views, NULL); + else if (HeadMatches2("CREATE", "TRIGGER") && TailMatches2("ON", MatchAny)) + COMPLETE_WITH_LIST7("NOT DEFERRABLE", "DEFERRABLE", "INITIALLY", + "REFERENCING", "FOR", "WHEN (", "EXECUTE PROCEDURE"); + else if (HeadMatches2("CREATE", "TRIGGER") && + (TailMatches1("DEFERRABLE") || TailMatches2("INITIALLY", "IMMEDIATE|DEFERRED"))) + COMPLETE_WITH_LIST4("REFERENCING", "FOR", "WHEN (", "EXECUTE PROCEDURE"); + else if (HeadMatches2("CREATE", "TRIGGER") && TailMatches1("REFERENCING")) + COMPLETE_WITH_LIST2("OLD TABLE", "NEW TABLE"); + else if (HeadMatches2("CREATE", "TRIGGER") && TailMatches2("OLD|NEW", "TABLE")) + COMPLETE_WITH_CONST("AS"); + else if (HeadMatches2("CREATE", "TRIGGER") && + (TailMatches5("REFERENCING", "OLD", "TABLE", "AS", MatchAny) || + TailMatches4("REFERENCING", "OLD", "TABLE", MatchAny))) + COMPLETE_WITH_LIST4("NEW TABLE", "FOR", "WHEN (", "EXECUTE PROCEDURE"); + else if (HeadMatches2("CREATE", "TRIGGER") && + (TailMatches5("REFERENCING", "NEW", "TABLE", "AS", MatchAny) || + TailMatches4("REFERENCING", "NEW", "TABLE", MatchAny))) + COMPLETE_WITH_LIST4("OLD TABLE", "FOR", "WHEN (", "EXECUTE PROCEDURE"); + else if (HeadMatches2("CREATE", "TRIGGER") && + (TailMatches9("REFERENCING", "OLD|NEW", "TABLE", "AS", MatchAny, "OLD|NEW", "TABLE", "AS", MatchAny) || + TailMatches8("REFERENCING", "OLD|NEW", "TABLE", MatchAny, "OLD|NEW", "TABLE", "AS", MatchAny) || + TailMatches8("REFERENCING", "OLD|NEW", "TABLE", "AS", MatchAny, "OLD|NEW", "TABLE", MatchAny) || + TailMatches7("REFERENCING", "OLD|NEW", "TABLE", MatchAny, "OLD|NEW", "TABLE", MatchAny))) + COMPLETE_WITH_LIST3("FOR", "WHEN (", "EXECUTE PROCEDURE"); + else if (HeadMatches2("CREATE", "TRIGGER") && TailMatches1("FOR")) + COMPLETE_WITH_LIST3("EACH", "ROW", "STATEMENT"); + else if (HeadMatches2("CREATE", "TRIGGER") && TailMatches2("FOR", "EACH")) + COMPLETE_WITH_LIST2("ROW", "STATEMENT"); + else if (HeadMatches2("CREATE", "TRIGGER") && + (TailMatches3("FOR", "EACH", "ROW|STATEMENT") || + TailMatches2("FOR", "ROW|STATEMENT"))) + COMPLETE_WITH_LIST2("WHEN (", "EXECUTE PROCEDURE"); + /* complete CREATE TRIGGER ... EXECUTE with PROCEDURE */ + else if (HeadMatches2("CREATE", "TRIGGER") && TailMatches1("EXECUTE")) + COMPLETE_WITH_CONST("PROCEDURE"); + else if (HeadMatches2("CREATE", "TRIGGER") && TailMatches2("EXECUTE", "PROCEDURE")) + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_functions, NULL); + +/* CREATE ROLE,USER,GROUP */ + else if (Matches3("CREATE", "ROLE|GROUP|USER", MatchAny) && + !TailMatches2("USER", "MAPPING")) + { + static const char *const list_CREATEROLE[] = + {"ADMIN", "BYPASSRLS", "CONNECTION LIMIT", "CREATEDB", "CREATEROLE", "CREATEUSER", + "ENCRYPTED PASSWORD", "IN", "INHERIT", "LOGIN", "NOBYPASSRLS", + "NOCREATEDB", "NOCREATEROLE", "NOCREATEUSER", "NOINHERIT", + "NOLOGIN", "NOREPLICATION", "NOSUPERUSER", "PASSWORD", + "REPLICATION", "ROLE", "SUPERUSER", "SYSID", + "VALID UNTIL", "WITH", NULL}; + + COMPLETE_WITH_LIST(list_CREATEROLE); + } + +/* CREATE ROLE,USER,GROUP WITH */ + else if (Matches4("CREATE", "ROLE|GROUP|USER", MatchAny, "WITH")) + { + /* Similar to the above, but don't complete "WITH" again. */ + static const char *const list_CREATEROLE_WITH[] = + {"ADMIN", "BYPASSRLS", "CONNECTION LIMIT", "CREATEDB", "CREATEROLE", + "ENCRYPTED PASSWORD", "IN", "INHERIT", "LOGIN", "NOBYPASSRLS", + "NOCREATEDB", "NOCREATEROLE", "NOINHERIT", + "NOLOGIN", "NOREPLICATION", "NOSUPERUSER", "PASSWORD", + "REPLICATION", "ROLE", "SUPERUSER", "SYSID", + "VALID UNTIL", NULL}; + + COMPLETE_WITH_LIST(list_CREATEROLE_WITH); + } + + /* complete CREATE ROLE,USER,GROUP IN with ROLE,GROUP */ + else if (Matches4("CREATE", "ROLE|USER|GROUP", MatchAny, "IN")) + COMPLETE_WITH_LIST2("GROUP", "ROLE"); + +/* CREATE VIEW --- is allowed inside CREATE SCHEMA, so use TailMatches */ + /* Complete CREATE VIEW with AS */ + else if (TailMatches3("CREATE", "VIEW", MatchAny)) + COMPLETE_WITH_CONST("AS"); + /* Complete "CREATE VIEW AS with "SELECT" */ + else if (TailMatches4("CREATE", "VIEW", MatchAny, "AS")) + COMPLETE_WITH_CONST("SELECT"); + +/* CREATE MATERIALIZED VIEW */ + else if (Matches2("CREATE", "MATERIALIZED")) + COMPLETE_WITH_CONST("VIEW"); + /* Complete CREATE MATERIALIZED VIEW with AS */ + else if (Matches4("CREATE", "MATERIALIZED", "VIEW", MatchAny)) + COMPLETE_WITH_CONST("AS"); + /* Complete "CREATE MATERIALIZED VIEW AS with "SELECT" */ + else if (Matches5("CREATE", "MATERIALIZED", "VIEW", MatchAny, "AS")) + COMPLETE_WITH_CONST("SELECT"); + +/* CREATE EVENT TRIGGER */ + else if (Matches2("CREATE", "EVENT")) + COMPLETE_WITH_CONST("TRIGGER"); + /* Complete CREATE EVENT TRIGGER with ON */ + else if (Matches4("CREATE", "EVENT", "TRIGGER", MatchAny)) + COMPLETE_WITH_CONST("ON"); + /* Complete CREATE EVENT TRIGGER ON with event_type */ + else if (Matches5("CREATE", "EVENT", "TRIGGER", MatchAny, "ON")) + COMPLETE_WITH_LIST3("ddl_command_start", "ddl_command_end", "sql_drop"); + +/* CREATE CONTINUOUS TRANSFORM / VIEW */ + else if ((IS_PIPELINEDB) && (Matches2("CREATE|DROP", "CONTINUOUS"))) + COMPLETE_WITH_LIST2("TRANSFORM", "VIEW"); + +/* DEALLOCATE */ + else if (Matches1("DEALLOCATE")) + COMPLETE_WITH_QUERY(Query_for_list_of_prepared_statements " UNION SELECT 'PREPARE'"); + else if (Matches2("DEALLOCATE", "PREPARE")) + COMPLETE_WITH_QUERY(Query_for_list_of_prepared_statements); + +/* DECLARE */ + else if (Matches2("DECLARE", MatchAny)) + COMPLETE_WITH_LIST5("BINARY", "INSENSITIVE", "SCROLL", "NO SCROLL", + "CURSOR"); + else if (HeadMatches1("DECLARE") && TailMatches1("CURSOR")) + COMPLETE_WITH_LIST3("WITH HOLD", "WITHOUT HOLD", "FOR"); + +/* DELETE --- can be inside EXPLAIN, RULE, etc */ + /* ... despite which, only complete DELETE with FROM at start of line */ + else if (Matches1("DELETE")) + COMPLETE_WITH_CONST("FROM"); + /* Complete DELETE FROM with a list of tables */ + else if (TailMatches2("DELETE", "FROM")) + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_updatables, NULL); + /* Complete DELETE FROM
*/ + else if (TailMatches3("DELETE", "FROM", MatchAny)) + COMPLETE_WITH_LIST2("USING", "WHERE"); + else if (TailMatches4("DELETE", "FROM", MatchAny, "USING")) + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tables, NULL); + else if (TailMatches5("DELETE", "FROM", MatchAny, "USING", MatchAny)) + COMPLETE_WITH_CONST("WHERE"); + else if ((TailMatches5("DELETE", MatchAny, "USING", MatchAny, "WHERE")) || + (TailMatches6("DELETE", "FROM", MatchAny, "USING", MatchAny, "WHERE"))) + COMPLETE_WITH_ATTR(prev2_wd, ""); + /* XXX: implement tab completion for DELETE ... USING */ + +/* DISCARD */ + else if (Matches1("DISCARD")) + COMPLETE_WITH_LIST4("ALL", "PLANS", "SEQUENCES", "TEMP"); + +/* DO */ + else if (Matches1("DO")) + COMPLETE_WITH_CONST("LANGUAGE"); + +/* DROP */ + /* Complete DROP object with CASCADE / RESTRICT */ + else if (Matches3("DROP", + "COLLATION|CONVERSION|DOMAIN|EXTENSION|LANGUAGE|PUBLICATION|SCHEMA|SEQUENCE|SERVER|SUBSCRIPTION|STATISTICS|TABLE|TYPE|VIEW", + MatchAny) || + Matches4("DROP", "ACCESS", "METHOD", MatchAny) || + (Matches4("DROP", "AGGREGATE|FUNCTION", MatchAny, MatchAny) && + ends_with(prev_wd, ')')) || + Matches4("DROP", "EVENT", "TRIGGER", MatchAny) || + Matches5("DROP", "FOREIGN", "DATA", "WRAPPER", MatchAny) || + Matches4("DROP", "FOREIGN", "TABLE", MatchAny) || + Matches5("DROP", "TEXT", "SEARCH", "CONFIGURATION|DICTIONARY|PARSER|TEMPLATE", MatchAny)) + COMPLETE_WITH_LIST2("CASCADE", "RESTRICT"); + + /* help completing some of the variants */ + else if (Matches3("DROP", "AGGREGATE|FUNCTION", MatchAny)) + COMPLETE_WITH_CONST("("); + else if (Matches4("DROP", "AGGREGATE|FUNCTION", MatchAny, "(")) + COMPLETE_WITH_FUNCTION_ARG(prev2_wd); + else if (Matches2("DROP", "FOREIGN")) + COMPLETE_WITH_LIST2("DATA WRAPPER", "TABLE"); + + /* DROP INDEX */ + else if (Matches2("DROP", "INDEX")) + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_indexes, + " UNION SELECT 'CONCURRENTLY'"); + else if (Matches3("DROP", "INDEX", "CONCURRENTLY")) + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_indexes, NULL); + else if (Matches3("DROP", "INDEX", MatchAny)) + COMPLETE_WITH_LIST2("CASCADE", "RESTRICT"); + else if (Matches4("DROP", "INDEX", "CONCURRENTLY", MatchAny)) + COMPLETE_WITH_LIST2("CASCADE", "RESTRICT"); + + /* DROP LIBRARY */ + else if (Matches2("DROP", "LIBRARY")) + COMPLETE_WITH_QUERY(Query_for_list_of_libraries); + + /* DROP MATERIALIZED VIEW */ + else if (Matches2("DROP", "MATERIALIZED")) + COMPLETE_WITH_CONST("VIEW"); + else if (Matches3("DROP", "MATERIALIZED", "VIEW")) + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_matviews, NULL); + + /* DROP OWNED BY */ + else if (Matches2("DROP", "OWNED")) + COMPLETE_WITH_CONST("BY"); + else if (Matches3("DROP", "OWNED", "BY")) + COMPLETE_WITH_QUERY(Query_for_list_of_roles); + + /* DROP SCHEMA */ + else if (Matches2("DROP", "SCHEMA")) + COMPLETE_WITH_QUERY(Query_for_list_of_schemas + " UNION ALL SELECT 'IF EXISTS'"); + else if ((Matches3("DROP", "SCHEMA", MatchAny))) + COMPLETE_WITH_LIST2("CASCADE", "RESTRICT"); + else if (Matches4("DROP", "SCHEMA", MatchAny, MatchAny) + && TailMatches1(MatchAnyExcept("CASCADE|RESTRICT"))) + COMPLETE_WITH_QUERY(Query_for_list_of_schemas); + else if ((HeadMatches5("DROP", "SCHEMA", MatchAny, MatchAny, MatchAny)) + && TailMatches1(MatchAnyExcept("EXISTS|CASCADE|RESTRICT"))) + COMPLETE_WITH_LIST2("CASCADE", "RESTRICT"); + + /* DROP TEXT SEARCH */ + else if (Matches3("DROP", "TEXT", "SEARCH")) + COMPLETE_WITH_LIST4("CONFIGURATION", "DICTIONARY", "PARSER", "TEMPLATE"); + + /* DROP TRIGGER */ + else if (Matches3("DROP", "TRIGGER", MatchAny)) + COMPLETE_WITH_CONST("ON"); + else if (Matches4("DROP", "TRIGGER", MatchAny, "ON")) + { + completion_info_charp = prev2_wd; + COMPLETE_WITH_QUERY(Query_for_list_of_tables_for_trigger); + } + else if (Matches5("DROP", "TRIGGER", MatchAny, "ON", MatchAny)) + COMPLETE_WITH_LIST2("CASCADE", "RESTRICT"); + + /* DROP ACCESS METHOD */ + else if (Matches2("DROP", "ACCESS")) + COMPLETE_WITH_CONST("METHOD"); + else if (Matches3("DROP", "ACCESS", "METHOD")) + COMPLETE_WITH_QUERY(Query_for_list_of_access_methods); + + /* DROP EVENT TRIGGER */ + else if (Matches2("DROP", "EVENT")) + COMPLETE_WITH_CONST("TRIGGER"); + else if (Matches3("DROP", "EVENT", "TRIGGER")) + COMPLETE_WITH_QUERY(Query_for_list_of_event_triggers); + + /* DROP POLICY */ + else if (Matches2("DROP", "POLICY")) + COMPLETE_WITH_QUERY(Query_for_list_of_policies); + /* DROP POLICY ON */ + else if (Matches3("DROP", "POLICY", MatchAny)) + COMPLETE_WITH_CONST("ON"); + /* DROP POLICY ON
*/ + else if (Matches4("DROP", "POLICY", MatchAny, "ON")) + { + completion_info_charp = prev2_wd; + COMPLETE_WITH_QUERY(Query_for_list_of_tables_for_policy); + } + + /* DROP RULE */ + else if (Matches3("DROP", "RULE", MatchAny)) + COMPLETE_WITH_CONST("ON"); + else if (Matches4("DROP", "RULE", MatchAny, "ON")) + { + completion_info_charp = prev2_wd; + COMPLETE_WITH_QUERY(Query_for_list_of_tables_for_rule); + } + else if (Matches5("DROP", "RULE", MatchAny, "ON", MatchAny)) + COMPLETE_WITH_LIST2("CASCADE", "RESTRICT"); + + /* DROP TABLE */ + else if (Matches2("DROP", "TABLE")) + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tables, + " UNION ALL SELECT 'IF EXISTS'"); + else if ((Matches3("DROP", "TABLE", MatchAny))) + COMPLETE_WITH_LIST2("CASCADE", "RESTRICT"); + else if (Matches4("DROP", "TABLE", MatchAny, MatchAny) + && TailMatches1(MatchAnyExcept("CASCADE|RESTRICT"))) + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tables, NULL); + else if ((HeadMatches5("DROP", "TABLE", MatchAny, MatchAny, MatchAny)) + && TailMatches1(MatchAnyExcept("EXISTS|CASCADE|RESTRICT"))) + COMPLETE_WITH_LIST2("CASCADE", "RESTRICT"); + + /* DROP USER */ + else if (HeadMatches2("DROP", "USER")) + COMPLETE_WITH_QUERY(Query_for_list_of_users + " UNION ALL SELECT 'IF EXISTS'"); + else if (HeadMatches4("DROP", "USER", "IF", "EXISTS")) + COMPLETE_WITH_QUERY(Query_for_list_of_users); + + /* DROP VIEW */ + else if (Matches2("DROP", "VIEW")) + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_views, + " UNION ALL SELECT 'IF EXISTS'"); + else if ((Matches3("DROP", "VIEW", MatchAny))) + COMPLETE_WITH_LIST2("CASCADE", "RESTRICT"); + else if (Matches4("DROP", "VIEW", MatchAny, MatchAny) + && TailMatches1(MatchAnyExcept("CASCADE|RESTRICT"))) + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_views, NULL); + else if ((HeadMatches5("DROP", "VIEW", MatchAny, MatchAny, MatchAny)) + && TailMatches1(MatchAnyExcept("EXISTS|CASCADE|RESTRICT"))) + COMPLETE_WITH_LIST2("CASCADE", "RESTRICT"); + + +/* EXECUTE */ + else if (Matches1("EXECUTE")) + COMPLETE_WITH_QUERY(Query_for_list_of_prepared_statements); + +/* EXPLAIN */ + + /* + * Complete EXPLAIN [ANALYZE] [VERBOSE] with list of EXPLAIN-able commands + */ + else if (Matches1("EXPLAIN")) + COMPLETE_WITH_LIST7("SELECT", "INSERT", "DELETE", "UPDATE", "DECLARE", + "ANALYZE", "VERBOSE"); + else if (Matches2("EXPLAIN", "ANALYZE")) + COMPLETE_WITH_LIST6("SELECT", "INSERT", "DELETE", "UPDATE", "DECLARE", + "VERBOSE"); + else if (Matches2("EXPLAIN", "VERBOSE") || + Matches3("EXPLAIN", "ANALYZE", "VERBOSE")) + COMPLETE_WITH_LIST5("SELECT", "INSERT", "DELETE", "UPDATE", "DECLARE"); + +/* FETCH */ + /* Experiment with removing Postgres compliant and keeping only Redshift */ + else if (Matches1("FETCH")) + COMPLETE_WITH_LIST4("ALL", "FORWARD", "FROM", "NEXT"); + else if (Matches2("FETCH", "FORWARD")) + COMPLETE_WITH_LIST2("ALL", "FROM"); + else if (Matches2("FETCH", "NEXT|ALL")) + COMPLETE_WITH_CONST("FROM"); + else if (Matches3("FETCH", "FORWARD", MatchAnyExcept("FROM"))) + COMPLETE_WITH_CONST("FROM"); + else if (HeadMatches1("FETCH") && TailMatches1("FROM")) + COMPLETE_WITH_QUERY(Query_for_list_of_cursors); + +/* FOREIGN DATA WRAPPER */ + /* applies in ALTER/DROP FDW and in CREATE SERVER */ + else if (TailMatches3("FOREIGN", "DATA", "WRAPPER") && + !TailMatches4("CREATE", MatchAny, MatchAny, MatchAny)) + COMPLETE_WITH_QUERY(Query_for_list_of_fdws); + /* applies in CREATE SERVER */ + else if (TailMatches4("FOREIGN", "DATA", "WRAPPER", MatchAny) && + HeadMatches2("CREATE", "SERVER")) + COMPLETE_WITH_CONST("OPTIONS"); + +/* FOREIGN TABLE */ + else if (TailMatches2("FOREIGN", "TABLE") && + !TailMatches3("CREATE", MatchAny, MatchAny)) + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_foreign_tables, NULL); + +/* FOREIGN SERVER */ + else if (TailMatches2("FOREIGN", "SERVER")) + COMPLETE_WITH_QUERY(Query_for_list_of_servers); + +/* + * GRANT and REVOKE are allowed inside CREATE SCHEMA and + * ALTER DEFAULT PRIVILEGES, so use TailMatches + */ + /* Complete GRANT/REVOKE with a list of roles and privileges */ + else if (TailMatches1("GRANT|REVOKE")) + { + /* + * With ALTER DEFAULT PRIVILEGES, restrict completion to grantable + * privileges (can't grant roles) + */ + if (HeadMatches3("ALTER", "DEFAULT", "PRIVILEGES")) + COMPLETE_WITH_LIST10("SELECT", "INSERT", "UPDATE", + "DELETE", "TRUNCATE", "REFERENCES", "TRIGGER", + "EXECUTE", "USAGE", "ALL"); + else + COMPLETE_WITH_QUERY(Query_for_list_of_roles + " UNION SELECT 'SELECT'" + " UNION SELECT 'INSERT'" + " UNION SELECT 'UPDATE'" + " UNION SELECT 'DELETE'" + " UNION SELECT 'TRUNCATE'" + " UNION SELECT 'REFERENCES'" + " UNION SELECT 'TRIGGER'" + " UNION SELECT 'CREATE'" + " UNION SELECT 'CONNECT'" + " UNION SELECT 'TEMPORARY'" + " UNION SELECT 'EXECUTE'" + " UNION SELECT 'USAGE'" + " UNION SELECT 'ALL'" + " UNION SELECT 'GRANT OPTION FOR'"); + } + + /* + * Complete GRANT/REVOKE with "ON", GRANT/REVOKE with + * TO/FROM + */ + else if (TailMatches2("GRANT|REVOKE", MatchAny)) + { + if (TailMatches1("SELECT|INSERT|UPDATE|DELETE|TRUNCATE|REFERENCES|TRIGGER|CREATE|CONNECT|TEMPORARY|TEMP|EXECUTE|USAGE|ALL")) + COMPLETE_WITH_CONST("ON"); + else if (TailMatches2("GRANT", MatchAny)) + COMPLETE_WITH_CONST("TO"); + else + COMPLETE_WITH_CONST("FROM"); + } + + /* + * Complete GRANT/REVOKE ON with a list of tables, views, and + * sequences. + * + * Keywords like DATABASE, FUNCTION, LANGUAGE and SCHEMA added to query + * result via UNION; seems to work intuitively. + * + * Note: GRANT/REVOKE can get quite complex; tab-completion as implemented + * here will only work if the privilege list contains exactly one + * privilege. + */ + else if (TailMatches3("GRANT|REVOKE", MatchAny, "ON")) + { + /* + * With ALTER DEFAULT PRIVILEGES, restrict completion to the kinds of + * objects supported. + */ + if (HeadMatches3("ALTER", "DEFAULT", "PRIVILEGES")) + COMPLETE_WITH_LIST5("TABLES", "SEQUENCES", "FUNCTIONS", "TYPES", "SCHEMAS"); + else + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tsvmf, + " UNION SELECT 'ALL FUNCTIONS IN SCHEMA'" + " UNION SELECT 'ALL SEQUENCES IN SCHEMA'" + " UNION SELECT 'ALL TABLES IN SCHEMA'" + " UNION SELECT 'DATABASE'" + " UNION SELECT 'DOMAIN'" + " UNION SELECT 'FOREIGN DATA WRAPPER'" + " UNION SELECT 'FOREIGN SERVER'" + " UNION SELECT 'FUNCTION'" + " UNION SELECT 'LANGUAGE'" + " UNION SELECT 'LARGE OBJECT'" + " UNION SELECT 'SCHEMA'" + " UNION SELECT 'SEQUENCE'" + " UNION SELECT 'TABLE'" + " UNION SELECT 'TABLESPACE'" + " UNION SELECT 'TYPE'"); + } + else if (TailMatches4("GRANT|REVOKE", MatchAny, "ON", "ALL")) + COMPLETE_WITH_LIST3("FUNCTIONS IN SCHEMA", "SEQUENCES IN SCHEMA", + "TABLES IN SCHEMA"); + else if (TailMatches4("GRANT|REVOKE", MatchAny, "ON", "FOREIGN")) + COMPLETE_WITH_LIST2("DATA WRAPPER", "SERVER"); + + /* + * Complete "GRANT/REVOKE * ON DATABASE/DOMAIN/..." with a list of + * appropriate objects. + * + * Complete "GRANT/REVOKE * ON *" with "TO/FROM". + */ + else if (TailMatches4("GRANT|REVOKE", MatchAny, "ON", MatchAny)) + { + if (TailMatches1("DATABASE")) + COMPLETE_WITH_QUERY(Query_for_list_of_databases); + else if (TailMatches1("DOMAIN")) + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_domains, NULL); + else if (TailMatches1("FUNCTION")) + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_functions, NULL); + else if (TailMatches1("LANGUAGE")) + COMPLETE_WITH_QUERY(Query_for_list_of_languages); + else if (TailMatches1("SCHEMA")) + COMPLETE_WITH_QUERY(Query_for_list_of_schemas); + else if (TailMatches1("SEQUENCE")) + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_sequences, NULL); + else if (TailMatches1("TABLE")) + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tsvmf, NULL); + else if (TailMatches1("TABLESPACE")) + COMPLETE_WITH_QUERY(Query_for_list_of_tablespaces); + else if (TailMatches1("TYPE")) + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_datatypes, NULL); + else if (TailMatches4("GRANT", MatchAny, MatchAny, MatchAny)) + COMPLETE_WITH_CONST("TO"); + else + COMPLETE_WITH_CONST("FROM"); + } + + /* + * Complete "GRANT/REVOKE ... TO/FROM" with username, PUBLIC, + * CURRENT_USER, or SESSION_USER. + */ + else if ((HeadMatches1("GRANT") && TailMatches1("TO")) || + (HeadMatches1("REVOKE") && TailMatches1("FROM"))) + COMPLETE_WITH_QUERY(Query_for_list_of_grant_roles); + /* Complete "ALTER DEFAULT PRIVILEGES ... GRANT/REVOKE ... TO/FROM */ + else if (HeadMatches3("ALTER", "DEFAULT", "PRIVILEGES") && TailMatches1("TO|FROM")) + COMPLETE_WITH_QUERY(Query_for_list_of_grant_roles); + /* Complete "GRANT/REVOKE ... ON * *" with TO/FROM */ + else if (HeadMatches1("GRANT") && TailMatches3("ON", MatchAny, MatchAny)) + COMPLETE_WITH_CONST("TO"); + else if (HeadMatches1("REVOKE") && TailMatches3("ON", MatchAny, MatchAny)) + COMPLETE_WITH_CONST("FROM"); + + /* Complete "GRANT/REVOKE * ON ALL * IN SCHEMA *" with TO/FROM */ + else if (TailMatches8("GRANT|REVOKE", MatchAny, "ON", "ALL", MatchAny, "IN", "SCHEMA", MatchAny)) + { + if (TailMatches8("GRANT", MatchAny, MatchAny, MatchAny, MatchAny, MatchAny, MatchAny, MatchAny)) + COMPLETE_WITH_CONST("TO"); + else + COMPLETE_WITH_CONST("FROM"); + } + + /* Complete "GRANT/REVOKE * ON FOREIGN DATA WRAPPER *" with TO/FROM */ + else if (TailMatches7("GRANT|REVOKE", MatchAny, "ON", "FOREIGN", "DATA", "WRAPPER", MatchAny)) + { + if (TailMatches7("GRANT", MatchAny, MatchAny, MatchAny, MatchAny, MatchAny, MatchAny)) + COMPLETE_WITH_CONST("TO"); + else + COMPLETE_WITH_CONST("FROM"); + } + + /* Complete "GRANT/REVOKE * ON FOREIGN SERVER *" with TO/FROM */ + else if (TailMatches6("GRANT|REVOKE", MatchAny, "ON", "FOREIGN", "SERVER", MatchAny)) + { + if (TailMatches6("GRANT", MatchAny, MatchAny, MatchAny, MatchAny, MatchAny)) + COMPLETE_WITH_CONST("TO"); + else + COMPLETE_WITH_CONST("FROM"); + } + +/* GROUP BY */ + else if (TailMatches3("FROM", MatchAny, "GROUP")) + COMPLETE_WITH_CONST("BY"); + +/* IMPORT FOREIGN SCHEMA */ + else if (Matches1("IMPORT")) + COMPLETE_WITH_CONST("FOREIGN SCHEMA"); + else if (Matches2("IMPORT", "FOREIGN")) + COMPLETE_WITH_CONST("SCHEMA"); + +/* INSERT --- can be inside EXPLAIN, RULE, etc */ + /* Complete INSERT with "INTO" */ + else if (TailMatches1("INSERT")) + COMPLETE_WITH_CONST("INTO"); + /* Complete INSERT INTO with table names */ + else if (TailMatches2("INSERT", "INTO")) + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_updatables, NULL); + /* Complete "INSERT INTO
(" with attribute names */ + else if (TailMatches4("INSERT", "INTO", MatchAny, "(")) + COMPLETE_WITH_ATTR(prev2_wd, ""); + + /* + * Complete INSERT INTO
with "(" or "VALUES" or "SELECT" or + * "TABLE" or "DEFAULT VALUES" or "OVERRIDING" + */ + else if (TailMatches3("INSERT", "INTO", MatchAny)) + COMPLETE_WITH_LIST6("(", "DEFAULT VALUES", "SELECT", "TABLE", "VALUES", "OVERRIDING"); + + /* + * Complete INSERT INTO
(attribs) with "VALUES" or "SELECT" or + * "TABLE" or "OVERRIDING" + */ + else if (TailMatches4("INSERT", "INTO", MatchAny, MatchAny) && + ends_with(prev_wd, ')')) + COMPLETE_WITH_LIST4("SELECT", "TABLE", "VALUES", "OVERRIDING"); + + /* Complete OVERRIDING */ + else if (TailMatches1("OVERRIDING")) + COMPLETE_WITH_LIST2("SYSTEM VALUE", "USER VALUE"); + + /* Complete after OVERRIDING clause */ + else if (TailMatches3("OVERRIDING", MatchAny, "VALUE")) + COMPLETE_WITH_LIST3("SELECT", "TABLE", "VALUES"); + + /* Insert an open parenthesis after "VALUES" */ + else if (TailMatches1("VALUES") && !TailMatches2("DEFAULT", "VALUES")) + COMPLETE_WITH_CONST("("); + +/* LOCK */ + /* Complete LOCK [TABLE] with a list of tables */ + else if (Matches1("LOCK")) + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tables, + " UNION SELECT 'TABLE'"); + else if (Matches2("LOCK", "TABLE")) + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tables, ""); + + /* For the following, handle the case of a single table only for now */ + + /* Complete LOCK [TABLE]
with "IN" */ + else if (Matches2("LOCK", MatchAnyExcept("TABLE")) || + Matches3("LOCK", "TABLE", MatchAny)) + COMPLETE_WITH_CONST("IN"); + + /* Complete LOCK [TABLE]
IN with a lock mode */ + else if (Matches3("LOCK", MatchAny, "IN") || + Matches4("LOCK", "TABLE", MatchAny, "IN")) + COMPLETE_WITH_LIST8("ACCESS SHARE MODE", + "ROW SHARE MODE", "ROW EXCLUSIVE MODE", + "SHARE UPDATE EXCLUSIVE MODE", "SHARE MODE", + "SHARE ROW EXCLUSIVE MODE", + "EXCLUSIVE MODE", "ACCESS EXCLUSIVE MODE"); + + /* Complete LOCK [TABLE]
IN ACCESS|ROW with rest of lock mode */ + else if (Matches4("LOCK", MatchAny, "IN", "ACCESS|ROW") || + Matches5("LOCK", "TABLE", MatchAny, "IN", "ACCESS|ROW")) + COMPLETE_WITH_LIST2("EXCLUSIVE MODE", "SHARE MODE"); + + /* Complete LOCK [TABLE]
IN SHARE with rest of lock mode */ + else if (Matches4("LOCK", MatchAny, "IN", "SHARE") || + Matches5("LOCK", "TABLE", MatchAny, "IN", "SHARE")) + COMPLETE_WITH_LIST3("MODE", "ROW EXCLUSIVE MODE", + "UPDATE EXCLUSIVE MODE"); + +/* NOTIFY --- can be inside EXPLAIN, RULE, etc */ + else if (TailMatches1("NOTIFY")) + COMPLETE_WITH_QUERY("SELECT pg_catalog.quote_ident(channel) FROM pg_catalog.pg_listening_channels() AS channel WHERE substring(pg_catalog.quote_ident(channel),1,%d)='%s'"); + +/* OPTIONS */ + else if (TailMatches1("OPTIONS")) + COMPLETE_WITH_CONST("("); + +/* OWNER TO - complete with available roles */ + else if (TailMatches2("OWNER", "TO")) + COMPLETE_WITH_QUERY(Query_for_list_of_roles); + +/* ORDER BY */ + else if (TailMatches3("FROM", MatchAny, "ORDER")) + COMPLETE_WITH_CONST("BY"); + else if (TailMatches4("FROM", MatchAny, "ORDER", "BY")) + COMPLETE_WITH_ATTR(prev3_wd, ""); + +/* PREPARE xx AS */ + else if (Matches2("PREPARE", MatchAny)) + COMPLETE_WITH_CONST("AS"); + else if (Matches3("PREPARE", MatchAny, "AS")) + COMPLETE_WITH_LIST4("SELECT", "UPDATE", "INSERT", "DELETE FROM"); + +/* + * PREPARE TRANSACTION is missing on purpose. It's intended for transaction + * managers, not for manual use in interactive sessions. + */ + +/* REASSIGN OWNED BY xxx TO yyy */ + else if (Matches1("REASSIGN")) + COMPLETE_WITH_CONST("OWNED BY"); + else if (Matches2("REASSIGN", "OWNED")) + COMPLETE_WITH_CONST("BY"); + else if (Matches3("REASSIGN", "OWNED", "BY")) + COMPLETE_WITH_QUERY(Query_for_list_of_roles); + else if (Matches4("REASSIGN", "OWNED", "BY", MatchAny)) + COMPLETE_WITH_CONST("TO"); + else if (Matches5("REASSIGN", "OWNED", "BY", MatchAny, "TO")) + COMPLETE_WITH_QUERY(Query_for_list_of_roles); + +/* REFRESH MATERIALIZED VIEW */ + else if (Matches1("REFRESH")) + COMPLETE_WITH_CONST("MATERIALIZED VIEW"); + else if (Matches2("REFRESH", "MATERIALIZED")) + COMPLETE_WITH_CONST("VIEW"); + else if (Matches3("REFRESH", "MATERIALIZED", "VIEW")) + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_matviews, + " UNION SELECT 'CONCURRENTLY'"); + else if (Matches4("REFRESH", "MATERIALIZED", "VIEW", "CONCURRENTLY")) + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_matviews, NULL); + else if (Matches4("REFRESH", "MATERIALIZED", "VIEW", MatchAny)) + COMPLETE_WITH_CONST("WITH"); + else if (Matches5("REFRESH", "MATERIALIZED", "VIEW", "CONCURRENTLY", MatchAny)) + COMPLETE_WITH_CONST("WITH"); + else if (Matches5("REFRESH", "MATERIALIZED", "VIEW", MatchAny, "WITH")) + COMPLETE_WITH_LIST2("NO DATA", "DATA"); + else if (Matches6("REFRESH", "MATERIALIZED", "VIEW", "CONCURRENTLY", MatchAny, "WITH")) + COMPLETE_WITH_LIST2("NO DATA", "DATA"); + else if (Matches6("REFRESH", "MATERIALIZED", "VIEW", MatchAny, "WITH", "NO")) + COMPLETE_WITH_CONST("DATA"); + else if (Matches7("REFRESH", "MATERIALIZED", "VIEW", "CONCURRENTLY", MatchAny, "WITH", "NO")) + COMPLETE_WITH_CONST("DATA"); + +/* REINDEX */ + else if (Matches1("REINDEX")) + COMPLETE_WITH_LIST5("TABLE", "INDEX", "SYSTEM", "SCHEMA", "DATABASE"); + else if (Matches2("REINDEX", "TABLE")) + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tm, NULL); + else if (Matches2("REINDEX", "INDEX")) + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_indexes, NULL); + else if (Matches2("REINDEX", "SCHEMA")) + COMPLETE_WITH_QUERY(Query_for_list_of_schemas); + else if (Matches2("REINDEX", "SYSTEM|DATABASE")) + COMPLETE_WITH_QUERY(Query_for_list_of_databases); + +/* SECURITY LABEL */ + else if (Matches1("SECURITY")) + COMPLETE_WITH_CONST("LABEL"); + else if (Matches2("SECURITY", "LABEL")) + COMPLETE_WITH_LIST2("ON", "FOR"); + else if (Matches4("SECURITY", "LABEL", "FOR", MatchAny)) + COMPLETE_WITH_CONST("ON"); + else if (Matches3("SECURITY", "LABEL", "ON") || + Matches5("SECURITY", "LABEL", "FOR", MatchAny, "ON")) + { + static const char *const list_SECURITY_LABEL[] = + {"TABLE", "COLUMN", "AGGREGATE", "DATABASE", "DOMAIN", + "EVENT TRIGGER", "FOREIGN TABLE", "FUNCTION", "LARGE OBJECT", + "MATERIALIZED VIEW", "LANGUAGE", "PUBLICATION", "ROLE", "SCHEMA", + "SEQUENCE", "SUBSCRIPTION", "TABLESPACE", "TYPE", "VIEW", NULL}; + + COMPLETE_WITH_LIST(list_SECURITY_LABEL); + } + else if (Matches5("SECURITY", "LABEL", "ON", MatchAny, MatchAny)) + COMPLETE_WITH_CONST("IS"); + +/* SELECT */ + else if (Matches1("SELECT")) + COMPLETE_WITH_LIST4("TOP", "ALL", "DISTINCT", "*"); +/* remove if MatchAny below works for * + else if (Matches2("SELECT", "*")) + COMPLETE_WITH_LIST7("FROM", "WHERE", "GROUP BY", "HAVING", "ORDER BY", "LIMIT", "OFFSET"); +*/ + else if (Matches2("SELECT", MatchAny)) + COMPLETE_WITH_LIST8("FROM", "INTO", "WHERE", "GROUP BY", "HAVING", "ORDER BY", "LIMIT", "OFFSET"); + else if (HeadMatches1("SELECT") && TailMatches1("INTO")) + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tables, + "UNION SELECT 'TEMP' UNION SELECT 'TEMPORARY' UNION SELECT 'TABLE'"); + else if (HeadMatches1("SELECT") && TailMatches2("INTO", MatchAny)) + COMPLETE_WITH_LIST7("FROM", "WHERE", "GROUP BY", "HAVING", "ORDER BY", "LIMIT", "OFFSET"); + /* SELECT FROM */ + else if (HeadMatches1("SELECT") && TailMatches1("FROM")) + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tables, NULL); + else if (HeadMatches1("SELECT") && TailMatches2("FROM", MatchAny)) + COMPLETE_WITH_LIST7("WHERE", "GROUP BY", "HAVING", "UNION", "ORDER BY", "LIMIT", "OFFSET"); + /* SELECT WHERE */ + else if (HeadMatches1("SELECT") && TailMatches3("FROM", MatchAny, "WHERE")) + COMPLETE_WITH_ATTR(prev2_wd, ""); + else if ( + (HeadMatches1("SELECT") && TailMatches3("WHERE", MatchAny, MatchAnyExcept("HAVING|BY|LIMIT|OFFSET"))) || + (HeadMatches1("SELECT") && TailMatches4("WHERE", MatchAny, MatchAnyExcept("HAVING|BY|LIMIT|OFFSET"), MatchAnyExcept("HAVING|BY|LIMIT|OFFSET"))) + ) + COMPLETE_WITH_LIST5("GROUP BY", "HAVING", "ORDER BY", "LIMIT", "OFFSET"); + /* SELECT GROUP BY */ + else if (HeadMatches1("SELECT") && TailMatches3("GROUP", "BY", MatchAny)) + COMPLETE_WITH_LIST5("HAVING", "UNION", "ORDER BY", "LIMIT", "OFFSET"); + /* SELECT ORDER BY */ + else if (HeadMatches1("SELECT") && TailMatches3("ORDER", "BY", MatchAny)) + COMPLETE_WITH_LIST2("ASC", "DESC"); + else if ((HeadMatches1("SELECT") && TailMatches3("ORDER", "BY", MatchAny)) || + (HeadMatches1("SELECT") && TailMatches4("ORDER", "BY", MatchAny, "ASC|DESC"))) + COMPLETE_WITH_LIST2("LIMIT", "OFFSET"); + /* SELECT LIMIT */ + else if (HeadMatches1("SELECT") && TailMatches1("LIMIT")) + COMPLETE_WITH_CONST("ALL"); + /* SELECT OFFSET */ + else if (HeadMatches1("SELECT") && TailMatches2("LIMIT", MatchAny)) + COMPLETE_WITH_CONST("OFFSET"); + + +/* SET, RESET, SHOW */ + /* Complete with a variable name */ + else if (TailMatches1("SET|RESET") && !TailMatches3("UPDATE", MatchAny, "SET")) + COMPLETE_WITH_QUERY(Query_for_list_of_set_vars + " UNION SELECT 'SESSION'" + " UNION SELECT 'LOCAL'" + " UNION SELECT 'SEED'"); + else if (Matches1("SHOW")) + COMPLETE_WITH_QUERY(Query_for_list_of_show_vars); + /* Complete "SET TRANSACTION" */ + else if (Matches2("SET", "TRANSACTION")) + COMPLETE_WITH_LIST5("SNAPSHOT", "ISOLATION LEVEL", "READ", "DEFERRABLE", "NOT DEFERRABLE"); + else if (Matches2("BEGIN|START", "TRANSACTION") || + Matches2("BEGIN", "WORK") || + Matches1("BEGIN") || + Matches5("SET", "SESSION", "CHARACTERISTICS", "AS", "TRANSACTION")) + COMPLETE_WITH_LIST4("ISOLATION LEVEL", "READ", "DEFERRABLE", "NOT DEFERRABLE"); + else if (Matches3("SET|BEGIN|START", "TRANSACTION|WORK", "NOT") || + Matches2("BEGIN", "NOT") || + Matches6("SET", "SESSION", "CHARACTERISTICS", "AS", "TRANSACTION", "NOT")) + COMPLETE_WITH_CONST("DEFERRABLE"); + else if (Matches3("SET|BEGIN|START", "TRANSACTION|WORK", "ISOLATION") || + Matches2("BEGIN", "ISOLATION") || + Matches6("SET", "SESSION", "CHARACTERISTICS", "AS", "TRANSACTION", "ISOLATION")) + COMPLETE_WITH_CONST("LEVEL"); + else if (Matches4("SET|BEGIN|START", "TRANSACTION|WORK", "ISOLATION", "LEVEL") || + Matches3("BEGIN", "ISOLATION", "LEVEL") || + Matches7("SET", "SESSION", "CHARACTERISTICS", "AS", "TRANSACTION", "ISOLATION", "LEVEL")) + COMPLETE_WITH_LIST3("READ", "REPEATABLE READ", "SERIALIZABLE"); + else if (Matches5("SET|BEGIN|START", "TRANSACTION|WORK", "ISOLATION", "LEVEL", "READ") || + Matches4("BEGIN", "ISOLATION", "LEVEL", "READ") || + Matches8("SET", "SESSION", "CHARACTERISTICS", "AS", "TRANSACTION", "ISOLATION", "LEVEL", "READ")) + COMPLETE_WITH_LIST2("UNCOMMITTED", "COMMITTED"); + else if (Matches5("SET|BEGIN|START", "TRANSACTION|WORK", "ISOLATION", "LEVEL", "REPEATABLE") || + Matches4("BEGIN", "ISOLATION", "LEVEL", "REPEATABLE") || + Matches8("SET", "SESSION", "CHARACTERISTICS", "AS", "TRANSACTION", "ISOLATION", "LEVEL", "REPEATABLE")) + COMPLETE_WITH_CONST("READ"); + else if (Matches3("SET|BEGIN|START", "TRANSACTION|WORK", "READ") || + Matches2("BEGIN", "READ") || + Matches6("SET", "SESSION", "CHARACTERISTICS", "AS", "TRANSACTION", "READ")) + COMPLETE_WITH_LIST2("ONLY", "WRITE"); + /* SET CONSTRAINTS */ + else if (Matches2("SET", "CONSTRAINTS")) + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_constraints_with_schema, "UNION SELECT 'ALL'"); + /* Complete SET CONSTRAINTS with DEFERRED|IMMEDIATE */ + else if (Matches3("SET", "CONSTRAINTS", MatchAny)) + COMPLETE_WITH_LIST2("DEFERRED", "IMMEDIATE"); + /* Complete SET ROLE */ + else if (Matches2("SET", "ROLE")) + COMPLETE_WITH_QUERY(Query_for_list_of_roles); + /* Complete SET SESSION with AUTHORIZATION or CHARACTERISTICS... */ + else if (Matches2("SET", "SESSION")) + COMPLETE_WITH_LIST2("AUTHORIZATION", "CHARACTERISTICS AS TRANSACTION"); + /* Complete SET SESSION AUTHORIZATION with username */ + else if (Matches3("SET", "SESSION", "AUTHORIZATION")) + COMPLETE_WITH_QUERY(Query_for_list_of_roles " UNION SELECT 'DEFAULT'"); + /* Complete RESET SESSION with AUTHORIZATION */ + else if (Matches2("RESET", "SESSION")) + COMPLETE_WITH_CONST("AUTHORIZATION"); + /* RESET parameter / ALL */ + else if (Matches1("RESET")) + COMPLETE_WITH_QUERY(Query_for_list_of_show_vars); +/* Complete SET with "TO" */ + else if (Matches2("SET", MatchAny)) + COMPLETE_WITH_CONST("TO"); + /* Complete ALTER DATABASE|FUNCTION|ROLE|USER ... SET */ + else if (HeadMatches2("ALTER", "DATABASE|FUNCTION|ROLE|USER") && + TailMatches2("SET", MatchAny)) + COMPLETE_WITH_LIST2("FROM CURRENT", "TO"); + /* Suggest possible variable values */ + else if (TailMatches3("SET", MatchAny, "TO|=")) + { + /* special cased code for individual GUCs */ + if (TailMatches2("DateStyle", "TO|=")) + { + static const char *const my_list[] = + {"ISO", "SQL", "Postgres", "German", + "YMD", "DMY", "MDY", + "US", "European", "NonEuropean", + "DEFAULT", NULL}; + + COMPLETE_WITH_LIST(my_list); + } + else if (TailMatches2("search_path", "TO|=")) + COMPLETE_WITH_QUERY(Query_for_list_of_schemas + " AND nspname not like 'pg\\_toast%%' " + " AND nspname not like 'pg\\_temp%%' " + " UNION SELECT 'DEFAULT' "); + else + { + /* generic, type based, GUC support */ + char *guctype = get_guctype(prev2_wd); + + if (guctype && strcmp(guctype, "enum") == 0) + { + char querybuf[1024]; + + snprintf(querybuf, sizeof(querybuf), Query_for_enum, prev2_wd); + COMPLETE_WITH_QUERY(querybuf); + } + else if (guctype && strcmp(guctype, "bool") == 0) + COMPLETE_WITH_LIST9("on", "off", "true", "false", "yes", "no", + "1", "0", "DEFAULT"); + else + COMPLETE_WITH_CONST("DEFAULT"); + + if (guctype) + free(guctype); + } + } + +/* START TRANSACTION */ + else if (Matches1("START")) + COMPLETE_WITH_CONST("TRANSACTION"); + +/* TABLE, but not TABLE embedded in other commands */ + else if (Matches1("TABLE")) + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_relations, NULL); + +/* TABLESAMPLE */ + else if (TailMatches1("TABLESAMPLE")) + COMPLETE_WITH_QUERY(Query_for_list_of_tablesample_methods); + else if (TailMatches2("TABLESAMPLE", MatchAny)) + COMPLETE_WITH_CONST("("); + +/* TRUNCATE */ + else if (Matches1("TRUNCATE")) + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tables, " UNION SELECT 'TABLE'"); + else if (Matches2("TRUNCATE", "TABLE")) + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tables, NULL); + +/* UNLISTEN */ + else if (Matches1("UNLISTEN")) + COMPLETE_WITH_QUERY("SELECT pg_catalog.quote_ident(channel) FROM pg_catalog.pg_listening_channels() AS channel WHERE substring(pg_catalog.quote_ident(channel),1,%d)='%s' UNION SELECT '*'"); + +/* UNLISTEN */ + else if (Matches1("UNLOAD")) + COMPLETE_WITH_CONST("("); + else if (Matches2("UNLOAD", MatchAny)) + COMPLETE_WITH_CONST("TO"); + else if (HeadMatches1("UNLOAD") && TailMatches2("TO", MatchAnyExcept("TO"))) + COMPLETE_WITH_LIST6("IAM_ROLE", "ACCESS_KEY_ID", "SECRET_ACCESS_KEY", "SESSION_TOKEN", "WITH", "CREDENTIALS"); + else if (HeadMatches1("UNLOAD") && TailMatches2("ACCESS_KEY_ID", MatchAny)) + COMPLETE_WITH_CONST("SECRET_ACCESS_KEY"); + else if (HeadMatches1("UNLOAD") && TailMatches2("SECRET_ACCESS_KEY", MatchAny)) + COMPLETE_WITH_CONST("SESSION_TOKEN"); + else if (HeadMatches1("UNLOAD") && TailMatches3("TO", MatchAny, "WITH")) + COMPLETE_WITH_CONST("CREDENTIALS"); + else if (HeadMatches1("UNLOAD") && TailMatches1("CREDENTIALS")) + COMPLETE_WITH_CONST("AS"); + else if (HeadMatches1("UNLOAD") && ( + (TailMatches2("IAM_ROLE", MatchAny)) || + (TailMatches2("SECRET_ACCESS_KEY", MatchAny)) || + (TailMatches2("SESSION_TOKEN", MatchAny)) || + (TailMatches2("CREDENTIALS|AS", MatchAny)) + )) + COMPLETE_WITH_LIST12("MANIFEST", "DELIMITER", "FIXEDWIDTH", "ENCRYPTED", "BZIP2", "GZIP", "ADDQUOTES", "NULL", "ESCAPE", "ALLOWOVERWRITE", "PARALLEL", "MAXFILESIZE"); + else if (HeadMatches1("UNLOAD") && TailMatches1("MANIFEST")) + COMPLETE_WITH_LIST11("DELIMITER", "FIXEDWIDTH", "ENCRYPTED", "BZIP2", "GZIP", "ADDQUOTES", "NULL", "ESCAPE", "ALLOWOVERWRITE", "PARALLEL", "MAXFILESIZE"); + else if (HeadMatches1("UNLOAD") && TailMatches1("ENCRYPTED")) + COMPLETE_WITH_LIST11("MANIFEST", "DELIMITER", "FIXEDWIDTH", "BZIP2", "GZIP", "ADDQUOTES", "NULL", "ESCAPE", "ALLOWOVERWRITE", "PARALLEL", "MAXFILESIZE"); + else if (HeadMatches1("UNLOAD") && TailMatches1("BZIP2")) + COMPLETE_WITH_LIST11("MANIFEST", "DELIMITER", "FIXEDWIDTH", "ENCRYPTED", "GZIP", "ADDQUOTES", "NULL", "ESCAPE", "ALLOWOVERWRITE", "PARALLEL", "MAXFILESIZE"); + else if (HeadMatches1("UNLOAD") && TailMatches1("GZIP")) + COMPLETE_WITH_LIST11("MANIFEST", "DELIMITER", "FIXEDWIDTH", "ENCRYPTED", "BZIP2", "ADDQUOTES", "NULL", "ESCAPE", "ALLOWOVERWRITE", "PARALLEL", "MAXFILESIZE"); + else if (HeadMatches1("UNLOAD") && TailMatches1("ADDQUOTES")) + COMPLETE_WITH_LIST11("MANIFEST", "DELIMITER", "FIXEDWIDTH", "ENCRYPTED", "BZIP2", "GZIP", "NULL", "ESCAPE", "ALLOWOVERWRITE", "PARALLEL", "MAXFILESIZE"); + else if (HeadMatches1("UNLOAD") && TailMatches1("ESCAPE")) + COMPLETE_WITH_LIST11("MANIFEST", "DELIMITER", "FIXEDWIDTH", "ENCRYPTED", "BZIP2", "GZIP", "ADDQUOTES", "NULL", "ALLOWOVERWRITE", "PARALLEL", "MAXFILESIZE"); + else if (HeadMatches1("UNLOAD") && TailMatches1("ALLOWOVERWRITE")) + COMPLETE_WITH_LIST11("MANIFEST", "DELIMITER", "FIXEDWIDTH", "ENCRYPTED", "BZIP2", "GZIP", "ADDQUOTES", "NULL", "ESCAPE", "ALLOWOVERWRITE", "PARALLEL"); + else if (HeadMatches1("UNLOAD") && TailMatches1("DELIMITER|FIXEDWIDTH|NULL")) + COMPLETE_WITH_CONST("AS '"); + else if (HeadMatches1("UNLOAD") && TailMatches1("MAXFILESIZE")) + COMPLETE_WITH_CONST("AS"); + else if (HeadMatches1("UNLOAD") && TailMatches1("PARALLEL")) + COMPLETE_WITH_LIST4("ON", "OFF", "TRUE", "FALSE"); + else if (HeadMatches1("UNLOAD") && ( + TailMatches2("MAXFILESIZE", MatchAnyExcept("AS")) || + TailMatches3("MAXFILESIZE", "AS", MatchAny) + )) + COMPLETE_WITH_LIST2("MB", "GB"); + else if (HeadMatches1("UNLOAD") && ( + TailMatches2("DELIMITER", MatchAny) || + TailMatches3("DELIMITER", "AS", MatchAny) + )) + COMPLETE_WITH_LIST11("MANIFEST", "FIXEDWIDTH", "ENCRYPTED", "BZIP2", "GZIP", "ADDQUOTES", "NULL", "ESCAPE", "ALLOWOVERWRITE", "PARALLEL", "MAXFILESIZE"); + else if (HeadMatches1("UNLOAD") && ( + TailMatches2("FIXEDWIDTH", MatchAny) || + TailMatches3("FIXEDWIDTH", "AS", MatchAny) + )) + COMPLETE_WITH_LIST11("MANIFEST", "DELIMITER", "ENCRYPTED", "BZIP2", "GZIP", "ADDQUOTES", "NULL", "ESCAPE", "ALLOWOVERWRITE", "PARALLEL", "MAXFILESIZE"); + else if (HeadMatches1("UNLOAD") && ( + TailMatches2("NULL", MatchAny) || + TailMatches3("NULL", "AS", MatchAny) + )) + COMPLETE_WITH_LIST11("MANIFEST", "DELIMITER", "FIXEDWIDTH", "ENCRYPTED", "BZIP2", "GZIP", "ADDQUOTES", "ESCAPE", "ALLOWOVERWRITE", "PARALLEL", "MAXFILESIZE"); + else if (HeadMatches1("UNLOAD") && ( + TailMatches2("PARALLEL", MatchAny) || + TailMatches3("PARALLEL", "AS", MatchAny) + )) + COMPLETE_WITH_LIST11("MANIFEST", "DELIMITER", "FIXEDWIDTH", "ENCRYPTED", "BZIP2", "GZIP", "ADDQUOTES", "NULL", "ESCAPE", "ALLOWOVERWRITE", "MAXFILESIZE"); + else if (HeadMatches1("UNLOAD") && ( + TailMatches3("MAXFILESIZE", MatchAnyExcept("AS"), "MB|GB") || + TailMatches4("MAXFILESIZE", "AS", MatchAnyExcept("AS"), "MB|GB") + )) + COMPLETE_WITH_LIST12("MANIFEST", "DELIMITER", "FIXEDWIDTH", "ENCRYPTED", "BZIP2", "GZIP", "ADDQUOTES", "NULL", "ESCAPE", "ALLOWOVERWRITE", "PARALLEL", "MAXFILESIZE"); + + +/* UPDATE --- can be inside EXPLAIN, RULE, etc */ + /* If prev. word is UPDATE suggest a list of tables */ + else if (TailMatches1("UPDATE")) + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_updatables, NULL); + /* Complete UPDATE
with "SET" */ + else if (TailMatches2("UPDATE", MatchAny)) + COMPLETE_WITH_CONST("SET"); + /* Complete UPDATE
SET with list of attributes */ + else if (TailMatches3("UPDATE", MatchAny, "SET")) + COMPLETE_WITH_ATTR(prev2_wd, ""); + /* UPDATE
SET = */ + else if (TailMatches4("UPDATE", MatchAny, "SET", MatchAny)) + COMPLETE_WITH_CONST("="); + /* UPDATE
SET = FROM / WHERE */ + else if (TailMatches6("UPDATE", MatchAny, "SET", MatchAny, "=", MatchAny)) + COMPLETE_WITH_LIST2("FROM", "WHERE"); + /* UPDATE
SET = FROM */ + else if (TailMatches7("UPDATE", MatchAny, "SET", MatchAny, "=", MatchAny, "FROM")) + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tables, NULL); + /* UPDATE
SET = FROM WHERE */ + else if (TailMatches8("UPDATE", MatchAny, "SET", MatchAny, "=", MatchAny, "FROM", MatchAny)) + COMPLETE_WITH_CONST("WHERE"); + /* UPDATE
SET = FROM WHERE */ + else if (TailMatches9("UPDATE", MatchAny, "SET", MatchAny, "=", MatchAny, "FROM", MatchAny, "WHERE")) + COMPLETE_WITH_ATTR(prev2_wd, ""); + +/* USER MAPPING */ + else if (Matches3("ALTER|CREATE|DROP", "USER", "MAPPING")) + COMPLETE_WITH_CONST("FOR"); + else if (Matches4("CREATE", "USER", "MAPPING", "FOR")) + COMPLETE_WITH_QUERY(Query_for_list_of_roles + " UNION SELECT 'CURRENT_USER'" + " UNION SELECT 'PUBLIC'" + " UNION SELECT 'USER'"); + else if (Matches4("ALTER|DROP", "USER", "MAPPING", "FOR")) + COMPLETE_WITH_QUERY(Query_for_list_of_user_mappings); + else if (Matches5("CREATE|ALTER|DROP", "USER", "MAPPING", "FOR", MatchAny)) + COMPLETE_WITH_CONST("SERVER"); + else if (Matches7("CREATE|ALTER", "USER", "MAPPING", "FOR", MatchAny, "SERVER", MatchAny)) + COMPLETE_WITH_CONST("OPTIONS"); + +/* + * VACUUM [ FULL | FREEZE ] [ VERBOSE ] [ table ] + * VACUUM [ FULL | FREEZE ] [ VERBOSE ] ANALYZE [ table [ (column [, ...] ) ] ] + */ + else if (Matches1("VACUUM")) + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tm, + " UNION SELECT 'FULL'" + " UNION SELECT 'SORT ONLY'" + " UNION SELECT 'DELETE ONLY'" + " UNION SELECT 'REINDEX'" + " UNION SELECT 'FREEZE'" + " UNION SELECT 'ANALYZE'" + " UNION SELECT 'VERBOSE'"); + else if (Matches2("VACUUM", "FULL|FREEZE")) + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tm, + " UNION SELECT 'ANALYZE'" + " UNION SELECT 'VERBOSE'"); + else if (Matches3("VACUUM", "FULL|FREEZE", "ANALYZE")) + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tm, + " UNION SELECT 'VERBOSE'"); + else if (Matches3("VACUUM", "FULL|FREEZE", "VERBOSE")) + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tm, + " UNION SELECT 'ANALYZE'"); + else if (Matches2("VACUUM", "VERBOSE")) + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tm, + " UNION SELECT 'ANALYZE'"); + else if (Matches2("VACUUM", "ANALYZE")) + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tm, + " UNION SELECT 'VERBOSE'"); + else if (Matches3("VACUUM", "FULL|REINDEX", MatchAny) || + Matches4("VACUUM", "SORT|DELETE", "ONLY", MatchAny)) + COMPLETE_WITH_CONST("TO"); + else if (Matches5("VACUUM", "FULL|REINDEX", MatchAny, "TO", MatchAny) || + Matches6("VACUUM", "SORT|DELETE", "ONLY", MatchAny, "TO", MatchAny)) + COMPLETE_WITH_CONST("PERCENT"); + +/* WITH [RECURSIVE] */ + + /* + * Only match when WITH is the first word, as WITH may appear in many + * other contexts. + */ + else if (Matches1("WITH")) + COMPLETE_WITH_CONST("RECURSIVE"); + +/* ANALYZE */ + /* Complete with list of tables */ + else if (Matches1("ANALYZE|ANALYSE")) + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tmf, + " UNION SELECT 'VERBOSE'" + " UNION SELECT 'PREDICATE COLUMNS'" + " UNION SELECT 'ALL COLUMNS'" + " UNION SELECT 'COMPRESSION'" + ); + +/* WHERE */ + /* Simple case of the word before the where being the table name */ + else if (TailMatches2(MatchAny, "WHERE")) + COMPLETE_WITH_ATTR(prev2_wd, ""); + +/* ... FROM ... */ +/* TODO: also include SRF ? */ + else if (TailMatches1("FROM") && !Matches3("COPY|\\copy", MatchAny, "FROM")) + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tsvmf, NULL); + +/* ... JOIN ... */ + else if (TailMatches1("JOIN")) + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tsvmf, NULL); + +/* Backslash commands */ +/* TODO: \dc \dd \dl */ + else if (TailMatchesCS1("\\?")) + COMPLETE_WITH_LIST_CS3("commands", "options", "variables"); + else if (TailMatchesCS1("\\connect|\\c")) + { + if (!recognized_connection_string(text)) + COMPLETE_WITH_QUERY(Query_for_list_of_databases); + } + else if (TailMatchesCS2("\\connect|\\c", MatchAny)) + { + if (!recognized_connection_string(prev_wd)) + COMPLETE_WITH_QUERY(Query_for_list_of_roles); + } + else if (TailMatchesCS1("\\da*")) + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_aggregates, NULL); + else if (TailMatchesCS1("\\dA*")) + COMPLETE_WITH_QUERY(Query_for_list_of_access_methods); + else if (TailMatchesCS1("\\db*")) + COMPLETE_WITH_QUERY(Query_for_list_of_tablespaces); + else if (TailMatchesCS1("\\dD*")) + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_domains, NULL); + else if (TailMatchesCS1("\\des*")) + COMPLETE_WITH_QUERY(Query_for_list_of_servers); + else if (TailMatchesCS1("\\deu*")) + COMPLETE_WITH_QUERY(Query_for_list_of_user_mappings); + else if (TailMatchesCS1("\\dew*")) + COMPLETE_WITH_QUERY(Query_for_list_of_fdws); + else if (TailMatchesCS1("\\df*")) + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_functions, NULL); + + else if (TailMatchesCS1("\\dFd*")) + COMPLETE_WITH_QUERY(Query_for_list_of_ts_dictionaries); + else if (TailMatchesCS1("\\dFp*")) + COMPLETE_WITH_QUERY(Query_for_list_of_ts_parsers); + else if (TailMatchesCS1("\\dFt*")) + COMPLETE_WITH_QUERY(Query_for_list_of_ts_templates); + /* must be at end of \dF alternatives: */ + else if (TailMatchesCS1("\\dF*")) + COMPLETE_WITH_QUERY(Query_for_list_of_ts_configurations); + + else if (TailMatchesCS1("\\di*")) + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_indexes, NULL); + else if (TailMatchesCS1("\\dL*")) + COMPLETE_WITH_QUERY(Query_for_list_of_languages); + else if (TailMatchesCS1("\\dn*")) + COMPLETE_WITH_QUERY(Query_for_list_of_schemas); + else if (TailMatchesCS1("\\dp") || TailMatchesCS1("\\z")) + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tsvmf, NULL); + else if (TailMatchesCS1("\\ds*")) + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_sequences, NULL); + else if (TailMatchesCS1("\\dt*")) + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tables, NULL); + else if (TailMatchesCS1("\\dT*")) + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_datatypes, NULL); + else if (TailMatchesCS1("\\du*") || TailMatchesCS1("\\dg*")) + COMPLETE_WITH_QUERY(Query_for_list_of_roles); + else if (TailMatchesCS1("\\dv*")) + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_views, NULL); + else if (TailMatchesCS1("\\dx*")) + COMPLETE_WITH_QUERY(Query_for_list_of_extensions); + else if (TailMatchesCS1("\\dm*")) + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_matviews, NULL); + else if (TailMatchesCS1("\\dE*")) + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_foreign_tables, NULL); + else if (TailMatchesCS1("\\dy*")) + COMPLETE_WITH_QUERY(Query_for_list_of_event_triggers); + + /* must be at end of \d alternatives: */ + else if (TailMatchesCS1("\\d*")) + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_relations, NULL); + + else if (TailMatchesCS1("\\ef")) + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_functions, NULL); + else if (TailMatchesCS1("\\ev")) + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_views, NULL); + + else if (TailMatchesCS1("\\encoding")) + COMPLETE_WITH_QUERY(Query_for_list_of_encodings); + else if (TailMatchesCS1("\\h|\\help")) + COMPLETE_WITH_LIST(sql_commands); + else if (TailMatchesCS2("\\h|\\help", MatchAny)) + { + if (TailMatches1("DROP")) + matches = completion_matches(text, drop_command_generator); + else if (TailMatches1("ALTER")) + matches = completion_matches(text, alter_command_generator); + + /* + * CREATE is recognized by tail match elsewhere, so doesn't need to be + * repeated here + */ + } + else if (TailMatchesCS3("\\h|\\help", MatchAny, MatchAny)) + { + if (TailMatches2("CREATE|DROP", "ACCESS")) + COMPLETE_WITH_CONST("METHOD"); + else if (TailMatches2("ALTER", "DEFAULT")) + COMPLETE_WITH_CONST("PRIVILEGES"); + else if (TailMatches2("CREATE|ALTER|DROP", "EVENT")) + COMPLETE_WITH_CONST("TRIGGER"); + else if (TailMatches2("CREATE|ALTER|DROP", "FOREIGN")) + COMPLETE_WITH_LIST2("DATA WRAPPER", "TABLE"); + else if (TailMatches2("ALTER", "LARGE")) + COMPLETE_WITH_CONST("OBJECT"); + else if (TailMatches2("CREATE|ALTER|DROP", "MATERIALIZED")) + COMPLETE_WITH_CONST("VIEW"); + else if (TailMatches2("CREATE|ALTER|DROP", "TEXT")) + COMPLETE_WITH_CONST("SEARCH"); + else if (TailMatches2("CREATE|ALTER|DROP", "USER")) + COMPLETE_WITH_CONST("MAPPING FOR"); + } + else if (TailMatchesCS4("\\h|\\help", MatchAny, MatchAny, MatchAny)) + { + if (TailMatches3("CREATE|ALTER|DROP", "FOREIGN", "DATA")) + COMPLETE_WITH_CONST("WRAPPER"); + else if (TailMatches3("CREATE|ALTER|DROP", "TEXT", "SEARCH")) + COMPLETE_WITH_LIST4("CONFIGURATION", "DICTIONARY", "PARSER", "TEMPLATE"); + else if (TailMatches3("CREATE|ALTER|DROP", "USER", "MAPPING")) + COMPLETE_WITH_CONST("FOR"); + } + else if (TailMatchesCS1("\\l*") && !TailMatchesCS1("\\lo*")) + COMPLETE_WITH_QUERY(Query_for_list_of_databases); + else if (TailMatchesCS1("\\password")) + COMPLETE_WITH_QUERY(Query_for_list_of_roles); + else if (TailMatchesCS1("\\pset")) + { + static const char *const my_list[] = + {"border", "columns", "expanded", "fieldsep", "fieldsep_zero", + "footer", "format", "linestyle", "null", "numericlocale", + "pager", "pager_min_lines", "recordsep", "recordsep_zero", + "tableattr", "title", "tuples_only", "unicode_border_linestyle", + "unicode_column_linestyle", "unicode_header_linestyle", NULL}; + + COMPLETE_WITH_LIST_CS(my_list); + } + else if (TailMatchesCS2("\\pset", MatchAny)) + { + if (TailMatchesCS1("format")) + { + static const char *const my_list[] = + {"unaligned", "aligned", "wrapped", "html", "asciidoc", + "latex", "latex-longtable", "troff-ms", NULL}; + + COMPLETE_WITH_LIST_CS(my_list); + } + else if (TailMatchesCS1("linestyle")) + COMPLETE_WITH_LIST_CS3("ascii", "old-ascii", "unicode"); + else if (TailMatchesCS1("pager")) + COMPLETE_WITH_LIST_CS3("on", "off", "always"); + else if (TailMatchesCS1("unicode_border_linestyle|" + "unicode_column_linestyle|" + "unicode_header_linestyle")) + COMPLETE_WITH_LIST_CS2("single", "double"); + } + else if (TailMatchesCS1("\\unset")) + matches = complete_from_variables(text, "", "", true); + else if (TailMatchesCS1("\\set")) + matches = complete_from_variables(text, "", "", false); + else if (TailMatchesCS2("\\set", MatchAny)) + { + if (TailMatchesCS1("AUTOCOMMIT|ON_ERROR_STOP|QUIET|" + "SINGLELINE|SINGLESTEP")) + COMPLETE_WITH_LIST_CS2("on", "off"); + else if (TailMatchesCS1("COMP_KEYWORD_CASE")) + COMPLETE_WITH_LIST_CS4("lower", "upper", + "preserve-lower", "preserve-upper"); + else if (TailMatchesCS1("ECHO")) + COMPLETE_WITH_LIST_CS4("errors", "queries", "all", "none"); + else if (TailMatchesCS1("ECHO_HIDDEN")) + COMPLETE_WITH_LIST_CS3("noexec", "off", "on"); + else if (TailMatchesCS1("HISTCONTROL")) + COMPLETE_WITH_LIST_CS4("ignorespace", "ignoredups", + "ignoreboth", "none"); + else if (TailMatchesCS1("ON_ERROR_ROLLBACK")) + COMPLETE_WITH_LIST_CS3("on", "off", "interactive"); + else if (TailMatchesCS1("SHOW_CONTEXT")) + COMPLETE_WITH_LIST_CS3("never", "errors", "always"); + else if (TailMatchesCS1("VERBOSITY")) + COMPLETE_WITH_LIST_CS3("default", "verbose", "terse"); + } + else if (TailMatchesCS1("\\sf*")) + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_functions, NULL); + else if (TailMatchesCS1("\\sv*")) + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_views, NULL); + else if (TailMatchesCS1("\\cd|\\e|\\edit|\\g|\\i|\\include|" + "\\ir|\\include_relative|\\o|\\out|" + "\\s|\\w|\\write|\\lo_import")) + { + completion_charp = "\\"; + matches = completion_matches(text, complete_from_files); + } + + /* + * Finally, we look through the list of "things", such as TABLE, INDEX and + * check if that was the previous word. If so, execute the query to get a + * list of them. + */ + else + { + int i; + + for (i = 0; words_after_create[i].name; i++) + { + if (pg_strcasecmp(prev_wd, words_after_create[i].name) == 0) + { + if (words_after_create[i].query) + COMPLETE_WITH_QUERY(words_after_create[i].query); + else if (words_after_create[i].squery) + COMPLETE_WITH_SCHEMA_QUERY(*words_after_create[i].squery, + NULL); + break; + } + } + } + + /* + * If we still don't have anything to match we have to fabricate some sort + * of default list. If we were to just return NULL, readline automatically + * attempts filename completion, and that's usually no good. + */ + if (matches == NULL) + { + COMPLETE_WITH_CONST(""); +#ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER + rl_completion_append_character = '\0'; +#endif + } + + /* free storage */ + free(previous_words); + free(words_buffer); + + /* Return our Grand List O' Matches */ + return matches; +} + + +/* + * GENERATOR FUNCTIONS + * + * These functions do all the actual work of completing the input. They get + * passed the text so far and the count how many times they have been called + * so far with the same text. + * If you read the above carefully, you'll see that these don't get called + * directly but through the readline interface. + * The return value is expected to be the full completion of the text, going + * through a list each time, or NULL if there are no more matches. The string + * will be free()'d by readline, so you must run it through strdup() or + * something of that sort. + */ + +/* + * Common routine for create_command_generator and drop_command_generator. + * Entries that have 'excluded' flags are not returned. + */ +static char * +create_or_drop_command_generator(const char *text, int state, bits32 excluded) +{ + static int list_index, + string_length; + const char *name; + + /* If this is the first time for this completion, init some values */ + if (state == 0) + { + list_index = 0; + string_length = strlen(text); + } + + /* find something that matches */ + while ((name = words_after_create[list_index++].name)) + { + if ((pg_strncasecmp(name, text, string_length) == 0) && + !(words_after_create[list_index - 1].flags & excluded)) + return pg_strdup_keyword_case(name, text); + } + /* if nothing matches, return NULL */ + return NULL; +} + +/* + * This one gives you one from a list of things you can put after CREATE + * as defined above. + */ +static char * +create_command_generator(const char *text, int state) +{ + return create_or_drop_command_generator(text, state, THING_NO_CREATE); +} + +/* + * This function gives you a list of things you can put after a DROP command. + */ +static char * +drop_command_generator(const char *text, int state) +{ + return create_or_drop_command_generator(text, state, THING_NO_DROP); +} + +/* + * This function gives you a list of things you can put after an ALTER command. + */ +static char * +alter_command_generator(const char *text, int state) +{ + return create_or_drop_command_generator(text, state, THING_NO_ALTER); +} + +/* The following two functions are wrappers for _complete_from_query */ + +static char * +complete_from_query(const char *text, int state) +{ + return _complete_from_query(0, text, state); +} + +static char * +complete_from_schema_query(const char *text, int state) +{ + return _complete_from_query(1, text, state); +} + + +/* + * This creates a list of matching things, according to a query pointed to + * by completion_charp. + * The query can be one of two kinds: + * + * 1. A simple query which must contain a %d and a %s, which will be replaced + * by the string length of the text and the text itself. The query may also + * have up to four more %s in it; the first two such will be replaced by the + * value of completion_info_charp, the next two by the value of + * completion_info_charp2. + * + * 2. A schema query used for completion of both schema and relation names. + * These are more complex and must contain in the following order: + * %d %s %d %s %d %s %s %d %s + * where %d is the string length of the text and %s the text itself. + * + * It is assumed that strings should be escaped to become SQL literals + * (that is, what is in the query is actually ... '%s' ...) + * + * See top of file for examples of both kinds of query. + */ +static char * +_complete_from_query(int is_schema_query, const char *text, int state) +{ + static int list_index, + byte_length; + static PGresult *result = NULL; + + /* + * If this is the first time for this completion, we fetch a list of our + * "things" from the backend. + */ + if (state == 0) + { + PQExpBufferData query_buffer; + char *e_text; + char *e_info_charp; + char *e_info_charp2; + const char *pstr = text; + int char_length = 0; + + list_index = 0; + byte_length = strlen(text); + + /* + * Count length as number of characters (not bytes), for passing to + * substring + */ + while (*pstr) + { + char_length++; + pstr += PQmblen(pstr, pset.encoding); + } + + /* Free any prior result */ + PQclear(result); + result = NULL; + + /* Set up suitably-escaped copies of textual inputs */ + e_text = escape_string(text); + + if (completion_info_charp) + e_info_charp = escape_string(completion_info_charp); + else + e_info_charp = NULL; + + if (completion_info_charp2) + e_info_charp2 = escape_string(completion_info_charp2); + else + e_info_charp2 = NULL; + + initPQExpBuffer(&query_buffer); + + if (is_schema_query) + { + /* completion_squery gives us the pieces to assemble */ + const char *qualresult = completion_squery->qualresult; + + if (qualresult == NULL) + qualresult = completion_squery->result; + + /* Get unqualified names matching the input-so-far */ + appendPQExpBuffer(&query_buffer, "SELECT %s FROM %s WHERE ", + completion_squery->result, + completion_squery->catname); + if (completion_squery->selcondition) + appendPQExpBuffer(&query_buffer, "%s AND ", + completion_squery->selcondition); + appendPQExpBuffer(&query_buffer, "substring(%s,1,%d)='%s'", + completion_squery->result, + char_length, e_text); + appendPQExpBuffer(&query_buffer, " AND %s", + completion_squery->viscondition); + + /* + * When fetching relation names, suppress system catalogs unless + * the input-so-far begins with "pg_". This is a compromise + * between not offering system catalogs for completion at all, and + * having them swamp the result when the input is just "p". + */ + if (strcmp(completion_squery->catname, + "pg_catalog.pg_class c") == 0 && + strncmp(text, "pg_", 3) !=0) + { + appendPQExpBufferStr(&query_buffer, + " AND c.relnamespace <> (SELECT oid FROM" + " pg_catalog.pg_namespace WHERE nspname = 'pg_catalog')"); + } + + /* + * Add in matching schema names, but only if there is more than + * one potential match among schema names. + */ + appendPQExpBuffer(&query_buffer, "\nUNION\n" + "SELECT pg_catalog.quote_ident(n.nspname) || '.' " + "FROM pg_catalog.pg_namespace n " + "WHERE substring(pg_catalog.quote_ident(n.nspname) || '.',1,%d)='%s'", + char_length, e_text); + appendPQExpBuffer(&query_buffer, + " AND (SELECT pg_catalog.count(*)" + " FROM pg_catalog.pg_namespace" + " WHERE substring(pg_catalog.quote_ident(nspname) || '.',1,%d) =" + " substring('%s',1,pg_catalog.length(pg_catalog.quote_ident(nspname))+1)) > 1", + char_length, e_text); + + /* + * Add in matching qualified names, but only if there is exactly + * one schema matching the input-so-far. + */ + appendPQExpBuffer(&query_buffer, "\nUNION\n" + "SELECT pg_catalog.quote_ident(n.nspname) || '.' || %s " + "FROM %s, pg_catalog.pg_namespace n " + "WHERE %s = n.oid AND ", + qualresult, + completion_squery->catname, + completion_squery->namespace); + if (completion_squery->selcondition) + appendPQExpBuffer(&query_buffer, "%s AND ", + completion_squery->selcondition); + appendPQExpBuffer(&query_buffer, "substring(pg_catalog.quote_ident(n.nspname) || '.' || %s,1,%d)='%s'", + qualresult, + char_length, e_text); + + /* + * This condition exploits the single-matching-schema rule to + * speed up the query + */ + appendPQExpBuffer(&query_buffer, + " AND substring(pg_catalog.quote_ident(n.nspname) || '.',1,%d) =" + " substring('%s',1,pg_catalog.length(pg_catalog.quote_ident(n.nspname))+1)", + char_length, e_text); + appendPQExpBuffer(&query_buffer, + " AND (SELECT pg_catalog.count(*)" + " FROM pg_catalog.pg_namespace" + " WHERE substring(pg_catalog.quote_ident(nspname) || '.',1,%d) =" + " substring('%s',1,pg_catalog.length(pg_catalog.quote_ident(nspname))+1)) = 1", + char_length, e_text); + + /* If an addon query was provided, use it */ + if (completion_charp) + appendPQExpBuffer(&query_buffer, "\n%s", completion_charp); + } + else + { + /* completion_charp is an sprintf-style format string */ + appendPQExpBuffer(&query_buffer, completion_charp, + char_length, e_text, + e_info_charp, e_info_charp, + e_info_charp2, e_info_charp2); + } + + /* Limit the number of records in the result */ + appendPQExpBuffer(&query_buffer, "\nLIMIT %d", + completion_max_records); + + result = exec_query(query_buffer.data); + + termPQExpBuffer(&query_buffer); + free(e_text); + if (e_info_charp) + free(e_info_charp); + if (e_info_charp2) + free(e_info_charp2); + } + + /* Find something that matches */ + if (result && PQresultStatus(result) == PGRES_TUPLES_OK) + { + const char *item; + + while (list_index < PQntuples(result) && + (item = PQgetvalue(result, list_index++, 0))) + if (pg_strncasecmp(text, item, byte_length) == 0) + return pg_strdup(item); + } + + /* If nothing matches, free the db structure and return null */ + PQclear(result); + result = NULL; + return NULL; +} + + +/* + * This function returns in order one of a fixed, NULL pointer terminated list + * of strings (if matching). This can be used if there are only a fixed number + * SQL words that can appear at certain spot. + */ +static char * +complete_from_list(const char *text, int state) +{ + static int string_length, + list_index, + matches; + static bool casesensitive; + const char *item; + + /* need to have a list */ + Assert(completion_charpp != NULL); + + /* Initialization */ + if (state == 0) + { + list_index = 0; + string_length = strlen(text); + casesensitive = completion_case_sensitive; + matches = 0; + } + + while ((item = completion_charpp[list_index++])) + { + /* First pass is case sensitive */ + if (casesensitive && strncmp(text, item, string_length) == 0) + { + matches++; + return pg_strdup(item); + } + + /* Second pass is case insensitive, don't bother counting matches */ + if (!casesensitive && pg_strncasecmp(text, item, string_length) == 0) + { + if (completion_case_sensitive) + return pg_strdup(item); + else + + /* + * If case insensitive matching was requested initially, + * adjust the case according to setting. + */ + return pg_strdup_keyword_case(item, text); + } + } + + /* + * No matches found. If we're not case insensitive already, lets switch to + * being case insensitive and try again + */ + if (casesensitive && matches == 0) + { + casesensitive = false; + list_index = 0; + state++; + return complete_from_list(text, state); + } + + /* If no more matches, return null. */ + return NULL; +} + + +/* + * This function returns one fixed string the first time even if it doesn't + * match what's there, and nothing the second time. This should be used if + * there is only one possibility that can appear at a certain spot, so + * misspellings will be overwritten. The string to be passed must be in + * completion_charp. + */ +static char * +complete_from_const(const char *text, int state) +{ + Assert(completion_charp != NULL); + if (state == 0) + { + if (completion_case_sensitive) + return pg_strdup(completion_charp); + else + + /* + * If case insensitive matching was requested initially, adjust + * the case according to setting. + */ + return pg_strdup_keyword_case(completion_charp, text); + } + else + return NULL; +} + + +/* + * This function appends the variable name with prefix and suffix to + * the variable names array. + */ +static void +append_variable_names(char ***varnames, int *nvars, + int *maxvars, const char *varname, + const char *prefix, const char *suffix) +{ + if (*nvars >= *maxvars) + { + *maxvars *= 2; + *varnames = (char **) pg_realloc(*varnames, + ((*maxvars) + 1) * sizeof(char *)); + } + + (*varnames)[(*nvars)++] = psprintf("%s%s%s", prefix, varname, suffix); +} + + +/* + * This function supports completion with the name of a psql variable. + * The variable names can be prefixed and suffixed with additional text + * to support quoting usages. If need_value is true, only variables + * that are currently set are included; otherwise, special variables + * (those that have hooks) are included even if currently unset. + */ +static char ** +complete_from_variables(const char *text, const char *prefix, const char *suffix, + bool need_value) +{ + char **matches; + char **varnames; + int nvars = 0; + int maxvars = 100; + int i; + struct _variable *ptr; + + varnames = (char **) pg_malloc((maxvars + 1) * sizeof(char *)); + + for (ptr = pset.vars->next; ptr; ptr = ptr->next) + { + if (need_value && !(ptr->value)) + continue; + append_variable_names(&varnames, &nvars, &maxvars, ptr->name, + prefix, suffix); + } + + varnames[nvars] = NULL; + COMPLETE_WITH_LIST_CS((const char *const *) varnames); + + for (i = 0; i < nvars; i++) + free(varnames[i]); + free(varnames); + + return matches; +} + + +/* + * This function wraps rl_filename_completion_function() to strip quotes from + * the input before searching for matches and to quote any matches for which + * the consuming command will require it. + */ +static char * +complete_from_files(const char *text, int state) +{ + static const char *unquoted_text; + char *unquoted_match; + char *ret = NULL; + + if (state == 0) + { + /* Initialization: stash the unquoted input. */ + unquoted_text = strtokx(text, "", NULL, "'", *completion_charp, + false, true, pset.encoding); + /* expect a NULL return for the empty string only */ + if (!unquoted_text) + { + Assert(*text == '\0'); + unquoted_text = text; + } + } + + unquoted_match = filename_completion_function(unquoted_text, state); + if (unquoted_match) + { + /* + * Caller sets completion_charp to a zero- or one-character string + * containing the escape character. This is necessary since \copy has + * no escape character, but every other backslash command recognizes + * "\" as an escape character. Since we have only two callers, don't + * bother providing a macro to simplify this. + */ + ret = quote_if_needed(unquoted_match, " \t\r\n\"`", + '\'', *completion_charp, pset.encoding); + if (ret) + free(unquoted_match); + else + ret = unquoted_match; + } + + return ret; +} + + +/* HELPER FUNCTIONS */ + + +/* + * Make a pg_strdup copy of s and convert the case according to + * COMP_KEYWORD_CASE setting, using ref as the text that was already entered. + */ +static char * +pg_strdup_keyword_case(const char *s, const char *ref) +{ + char *ret, + *p; + unsigned char first = ref[0]; + + ret = pg_strdup(s); + + if (pset.comp_case == PSQL_COMP_CASE_LOWER || + ((pset.comp_case == PSQL_COMP_CASE_PRESERVE_LOWER || + pset.comp_case == PSQL_COMP_CASE_PRESERVE_UPPER) && islower(first)) || + (pset.comp_case == PSQL_COMP_CASE_PRESERVE_LOWER && !isalpha(first))) + { + for (p = ret; *p; p++) + *p = pg_tolower((unsigned char) *p); + } + else + { + for (p = ret; *p; p++) + *p = pg_toupper((unsigned char) *p); + } + + return ret; +} + + +/* + * escape_string - Escape argument for use as string literal. + * + * The returned value has to be freed. + */ +static char * +escape_string(const char *text) +{ + size_t text_length; + char *result; + + text_length = strlen(text); + + result = pg_malloc(text_length * 2 + 1); + PQescapeStringConn(pset.db, result, text, text_length, NULL); + + return result; +} + + +/* + * Execute a query and report any errors. This should be the preferred way of + * talking to the database in this file. + */ +static PGresult * +exec_query(const char *query) +{ + PGresult *result; + + if (query == NULL || !pset.db || PQstatus(pset.db) != CONNECTION_OK) + return NULL; + + result = PQexec(pset.db, query); + + if (PQresultStatus(result) != PGRES_TUPLES_OK) + { +#ifdef NOT_USED + psql_error("tab completion query failed: %s\nQuery was:\n%s\n", + PQerrorMessage(pset.db), query); +#endif + PQclear(result); + result = NULL; + } + + return result; +} + + +/* + * Parse all the word(s) before point. + * + * Returns a malloc'd array of character pointers that point into the malloc'd + * data array returned to *buffer; caller must free() both of these when done. + * *nwords receives the number of words found, ie, the valid length of the + * return array. + * + * Words are returned right to left, that is, previous_words[0] gets the last + * word before point, previous_words[1] the next-to-last, etc. + */ +static char ** +get_previous_words(int point, char **buffer, int *nwords) +{ + char **previous_words; + char *buf; + char *outptr; + int words_found = 0; + int i; + + /* + * If we have anything in tab_completion_query_buf, paste it together with + * rl_line_buffer to construct the full query. Otherwise we can just use + * rl_line_buffer as the input string. + */ + if (tab_completion_query_buf && tab_completion_query_buf->len > 0) + { + i = tab_completion_query_buf->len; + buf = pg_malloc(point + i + 2); + memcpy(buf, tab_completion_query_buf->data, i); + buf[i++] = '\n'; + memcpy(buf + i, rl_line_buffer, point); + i += point; + buf[i] = '\0'; + /* Readjust point to reference appropriate offset in buf */ + point = i; + } + else + buf = rl_line_buffer; + + /* + * Allocate an array of string pointers and a buffer to hold the strings + * themselves. The worst case is that the line contains only + * non-whitespace WORD_BREAKS characters, making each one a separate word. + * This is usually much more space than we need, but it's cheaper than + * doing a separate malloc() for each word. + */ + previous_words = (char **) pg_malloc(point * sizeof(char *)); + *buffer = outptr = (char *) pg_malloc(point * 2); + + /* + * First we look for a non-word char before the current point. (This is + * probably useless, if readline is on the same page as we are about what + * is a word, but if so it's cheap.) + */ + for (i = point - 1; i >= 0; i--) + { + if (strchr(WORD_BREAKS, buf[i])) + break; + } + point = i; + + /* + * Now parse words, working backwards, until we hit start of line. The + * backwards scan has some interesting but intentional properties + * concerning parenthesis handling. + */ + while (point >= 0) + { + int start, + end; + bool inquotes = false; + int parentheses = 0; + + /* now find the first non-space which then constitutes the end */ + end = -1; + for (i = point; i >= 0; i--) + { + if (!isspace((unsigned char) buf[i])) + { + end = i; + break; + } + } + /* if no end found, we're done */ + if (end < 0) + break; + + /* + * Otherwise we now look for the start. The start is either the last + * character before any word-break character going backwards from the + * end, or it's simply character 0. We also handle open quotes and + * parentheses. + */ + for (start = end; start > 0; start--) + { + if (buf[start] == '"') + inquotes = !inquotes; + if (!inquotes) + { + if (buf[start] == ')') + parentheses++; + else if (buf[start] == '(') + { + if (--parentheses <= 0) + break; + } + else if (parentheses == 0 && + strchr(WORD_BREAKS, buf[start - 1])) + break; + } + } + + /* Return the word located at start to end inclusive */ + previous_words[words_found++] = outptr; + i = end - start + 1; + memcpy(outptr, &buf[start], i); + outptr += i; + *outptr++ = '\0'; + + /* Continue searching */ + point = start - 1; + } + + /* Release parsing input workspace, if we made one above */ + if (buf != rl_line_buffer) + free(buf); + + *nwords = words_found; + return previous_words; +} + +/* + * Look up the type for the GUC variable with the passed name. + * + * Returns NULL if the variable is unknown. Otherwise the returned string, + * containing the type, has to be freed. + */ +static char * +get_guctype(const char *varname) +{ + PQExpBufferData query_buffer; + char *e_varname; + PGresult *result; + char *guctype = NULL; + + e_varname = escape_string(varname); + + initPQExpBuffer(&query_buffer); + appendPQExpBuffer(&query_buffer, + "SELECT vartype FROM pg_catalog.pg_settings " + "WHERE pg_catalog.lower(name) = pg_catalog.lower('%s')", + e_varname); + + result = exec_query(query_buffer.data); + termPQExpBuffer(&query_buffer); + free(e_varname); + + if (PQresultStatus(result) == PGRES_TUPLES_OK && PQntuples(result) > 0) + guctype = pg_strdup(PQgetvalue(result, 0, 0)); + + PQclear(result); + + return guctype; +} + +#ifdef NOT_USED + +/* + * Surround a string with single quotes. This works for both SQL and + * psql internal. Currently disabled because it is reported not to + * cooperate with certain versions of readline. + */ +static char * +quote_file_name(char *text, int match_type, char *quote_pointer) +{ + char *s; + size_t length; + + (void) quote_pointer; /* not used */ + + length = strlen(text) +(match_type == SINGLE_MATCH ? 3 : 2); + s = pg_malloc(length); + s[0] = '\''; + strcpy(s + 1, text); + if (match_type == SINGLE_MATCH) + s[length - 2] = '\''; + s[length - 1] = '\0'; + return s; +} + +static char * +dequote_file_name(char *text, char quote_char) +{ + char *s; + size_t length; + + if (!quote_char) + return pg_strdup(text); + + length = strlen(text); + s = pg_malloc(length - 2 + 1); + strlcpy(s, text +1, length - 2 + 1); + + return s; +} +#endif /* NOT_USED */ + +#endif /* USE_READLINE */ diff --git a/src/bin/psql/tab-complete-redshift.h b/src/bin/psql/tab-complete-redshift.h new file mode 100644 index 0000000000000..8ce07cd9c7dfe --- /dev/null +++ b/src/bin/psql/tab-complete-redshift.h @@ -0,0 +1,17 @@ +/* + * psql - the PostgreSQL interactive terminal + * + * Copyright (c) 2000-2017, PostgreSQL Global Development Group + * + * src/bin/psql/tab-complete-redshift.h + */ +#ifndef TAB_COMPLETE_H +#define TAB_COMPLETE_H + +#include "pqexpbuffer.h" + +extern PQExpBuffer tab_completion_query_buf; + +extern void initialize_readline_redshift(void); + +#endif /* TAB_COMPLETE_REDSHIFT_H */ From d57aaed3ed79d2fa6b46bfef7b09b00b5ca401f7 Mon Sep 17 00:00:00 2001 From: Robins Tharakan Date: Fri, 15 Sep 2017 07:14:10 +0000 Subject: [PATCH 097/149] Now a separate Redshift tab-completion script is compiling + working --- src/bin/psql/Makefile | 2 +- src/bin/psql/tab-complete-redshift.c | 7 ------- src/bin/psql/tab-complete-redshift.h | 4 ++-- 3 files changed, 3 insertions(+), 10 deletions(-) diff --git a/src/bin/psql/Makefile b/src/bin/psql/Makefile index cabfe15f97f75..b074df4cc1d21 100644 --- a/src/bin/psql/Makefile +++ b/src/bin/psql/Makefile @@ -24,7 +24,7 @@ override LDFLAGS := -L$(top_builddir)/src/fe_utils -lpgfeutils $(libpq_pgport) $ OBJS= command.o common.o conditional.o copy.o crosstabview.o \ describe.o help.o input.o large_obj.o mainloop.o \ prompt.o psqlscanslash.o sql_help.o startup.o stringutils.o \ - tab-complete.o variables.o \ + tab-complete.o tab-complete-redshift.o variables.o \ $(WIN32RES) diff --git a/src/bin/psql/tab-complete-redshift.c b/src/bin/psql/tab-complete-redshift.c index 42fba9e3b4988..28461459d2c61 100644 --- a/src/bin/psql/tab-complete-redshift.c +++ b/src/bin/psql/tab-complete-redshift.c @@ -63,13 +63,6 @@ extern char *filename_completion_function(); /* word break characters */ #define WORD_BREAKS "\t\n@$><=;|&{() " -/* - * Since readline doesn't let us pass any state through to the tab completion - * callback, we have to use this global variable to let get_previous_words() - * get at the previous lines of the current command. Ick. - */ -PQExpBuffer tab_completion_query_buf = NULL; - /* * This struct is used to define "schema queries", which are custom-built * to obtain possibly-schema-qualified names of database objects. There is diff --git a/src/bin/psql/tab-complete-redshift.h b/src/bin/psql/tab-complete-redshift.h index 8ce07cd9c7dfe..3d2c7ce2279ca 100644 --- a/src/bin/psql/tab-complete-redshift.h +++ b/src/bin/psql/tab-complete-redshift.h @@ -5,8 +5,8 @@ * * src/bin/psql/tab-complete-redshift.h */ -#ifndef TAB_COMPLETE_H -#define TAB_COMPLETE_H +#ifndef TAB_COMPLETE_REDSHIFT_H +#define TAB_COMPLETE_REDSHIFT_H #include "pqexpbuffer.h" From 425556f17d75101600d834d410eb44ed2c3533b3 Mon Sep 17 00:00:00 2001 From: Robins Tharakan Date: Fri, 15 Sep 2017 19:30:28 +1000 Subject: [PATCH 098/149] Separate tab-complete script for PipelineDB now working --- src/bin/psql/Makefile | 2 +- src/bin/psql/input.c | 4 ++-- src/bin/psql/nls.mk | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/bin/psql/Makefile b/src/bin/psql/Makefile index b074df4cc1d21..99ab5e46c8ee0 100644 --- a/src/bin/psql/Makefile +++ b/src/bin/psql/Makefile @@ -24,7 +24,7 @@ override LDFLAGS := -L$(top_builddir)/src/fe_utils -lpgfeutils $(libpq_pgport) $ OBJS= command.o common.o conditional.o copy.o crosstabview.o \ describe.o help.o input.o large_obj.o mainloop.o \ prompt.o psqlscanslash.o sql_help.o startup.o stringutils.o \ - tab-complete.o tab-complete-redshift.o variables.o \ + tab-complete.o tab-complete-pipelinedb.o tab-complete-redshift.o variables.o \ $(WIN32RES) diff --git a/src/bin/psql/input.c b/src/bin/psql/input.c index 0e8291d143e42..256f470aea2c9 100644 --- a/src/bin/psql/input.c +++ b/src/bin/psql/input.c @@ -17,6 +17,7 @@ #include "settings.h" #include "tab-complete.h" #include "tab-complete-redshift.h" +#include "tab-complete-pipelinedb.h" #include "common.h" #ifndef WIN32 @@ -35,7 +36,6 @@ static char *psql_history; static int history_lines_added; - /* * Preserve newlines in saved queries by mapping '\n' to NL_IN_HISTORY * @@ -359,7 +359,7 @@ initializeInput(int flags) if (strncmp(pset.sengine, "redshift", 8) == 0) initialize_readline_redshift(); else if (strncmp(pset.sengine, "pipelinedb", 10) == 0) - ;// initialize_readline_pipelinedb(); + initialize_readline_pipelinedb(); else initialize_readline(); rl_initialize(); diff --git a/src/bin/psql/nls.mk b/src/bin/psql/nls.mk index c78a25a06ef36..00548e124439f 100644 --- a/src/bin/psql/nls.mk +++ b/src/bin/psql/nls.mk @@ -4,7 +4,7 @@ AVAIL_LANGUAGES = cs de es fr he it ja ko pl pt_BR ru sv zh_CN zh_TW GETTEXT_FILES = command.c common.c copy.c crosstabview.c help.c input.c large_obj.c \ mainloop.c psqlscanslash.c startup.c \ describe.c sql_help.h sql_help.c \ - tab-complete.c tab-complete-redshift.c variables.c \ + tab-complete.c tab-complete-pipelinedb.c tab-complete-redshift.c variables.c \ ../../fe_utils/print.c ../../fe_utils/psqlscan.c \ ../../common/exec.c ../../common/fe_memutils.c ../../common/username.c \ ../../common/wait_error.c From c95b804086776edf0b3d91ce9153cc680c581b68 Mon Sep 17 00:00:00 2001 From: Robins Tharakan Date: Fri, 15 Sep 2017 19:31:40 +1000 Subject: [PATCH 099/149] Missed adding pipelinedb scripts ealier :( --- src/bin/psql/tab-complete-pipelinedb.c | 4933 ++++++++++++++++++++++++ src/bin/psql/tab-complete-pipelinedb.h | 17 + 2 files changed, 4950 insertions(+) create mode 100644 src/bin/psql/tab-complete-pipelinedb.c create mode 100644 src/bin/psql/tab-complete-pipelinedb.h diff --git a/src/bin/psql/tab-complete-pipelinedb.c b/src/bin/psql/tab-complete-pipelinedb.c new file mode 100644 index 0000000000000..21903f5f88632 --- /dev/null +++ b/src/bin/psql/tab-complete-pipelinedb.c @@ -0,0 +1,4933 @@ +/* + * psql - the PostgreSQL interactive terminal + * + * Copyright (c) 2000-2017, PostgreSQL Global Development Group + * + * src/bin/psql/tab-complete-pipelinedb.c + */ + +/*---------------------------------------------------------------------- + * This file implements a somewhat more sophisticated readline "TAB + * completion" in psql. It is not intended to be AI, to replace + * learning SQL, or to relieve you from thinking about what you're + * doing. Also it does not always give you all the syntactically legal + * completions, only those that are the most common or the ones that + * the programmer felt most like implementing. + * + * CAVEAT: Tab completion causes queries to be sent to the backend. + * The number of tuples returned gets limited, in most default + * installations to 1000, but if you still don't like this prospect, + * you can turn off tab completion in your ~/.inputrc (or else + * ${INPUTRC}) file so: + * + * $if psql + * set disable-completion on + * $endif + * + * See `man 3 readline' or `info readline' for the full details. + * + * BUGS: + * - Quotes, parentheses, and other funny characters are not handled + * all that gracefully. + *---------------------------------------------------------------------- + */ + +#include "postgres_fe.h" +#include "tab-complete-pipelinedb.h" +#include "input.h" + +/* If we don't have this, we might as well forget about the whole thing: */ +#ifdef USE_READLINE + +#include + +#include "catalog/pg_class.h" + +#include "libpq-fe.h" +#include "pqexpbuffer.h" +#include "common.h" +#include "settings.h" +#include "stringutils.h" + +#ifdef HAVE_RL_FILENAME_COMPLETION_FUNCTION +#define filename_completion_function rl_filename_completion_function +#else +/* missing in some header files */ +extern char *filename_completion_function(); +#endif + +#ifdef HAVE_RL_COMPLETION_MATCHES +#define completion_matches rl_completion_matches +#endif + +/* word break characters */ +#define WORD_BREAKS "\t\n@$><=;|&{() " + +/* + * This struct is used to define "schema queries", which are custom-built + * to obtain possibly-schema-qualified names of database objects. There is + * enough similarity in the structure that we don't want to repeat it each + * time. So we put the components of each query into this struct and + * assemble them with the common boilerplate in _complete_from_query(). + */ +typedef struct SchemaQuery +{ + /* + * Name of catalog or catalogs to be queried, with alias, eg. + * "pg_catalog.pg_class c". Note that "pg_namespace n" will be added. + */ + const char *catname; + + /* + * Selection condition --- only rows meeting this condition are candidates + * to display. If catname mentions multiple tables, include the necessary + * join condition here. For example, this might look like "c.relkind = " + * CppAsString2(RELKIND_RELATION). Write NULL (not an empty string) if + * not needed. + */ + const char *selcondition; + + /* + * Visibility condition --- which rows are visible without schema + * qualification? For example, "pg_catalog.pg_table_is_visible(c.oid)". + */ + const char *viscondition; + + /* + * Namespace --- name of field to join to pg_namespace.oid. For example, + * "c.relnamespace". + */ + const char *namespace; + + /* + * Result --- the appropriately-quoted name to return, in the case of an + * unqualified name. For example, "pg_catalog.quote_ident(c.relname)". + */ + const char *result; + + /* + * In some cases a different result must be used for qualified names. + * Enter that here, or write NULL if result can be used. + */ + const char *qualresult; +} SchemaQuery; + + +/* Store maximum number of records we want from database queries + * (implemented via SELECT ... LIMIT xx). + */ +static int completion_max_records; + +/* + * Communication variables set by COMPLETE_WITH_FOO macros and then used by + * the completion callback functions. Ugly but there is no better way. + */ +static const char *completion_charp; /* to pass a string */ +static const char *const *completion_charpp; /* to pass a list of strings */ +static const char *completion_info_charp; /* to pass a second string */ +static const char *completion_info_charp2; /* to pass a third string */ +static const SchemaQuery *completion_squery; /* to pass a SchemaQuery */ +static bool completion_case_sensitive; /* completion is case sensitive */ + +/* + * A few macros to ease typing. You can use these to complete the given + * string with + * 1) The results from a query you pass it. (Perhaps one of those below?) + * 2) The results from a schema query you pass it. + * 3) The items from a null-pointer-terminated list (with or without + * case-sensitive comparison; see also COMPLETE_WITH_LISTn, below). + * 4) A string constant. + * 5) The list of attributes of the given table (possibly schema-qualified). + * 6/ The list of arguments to the given function (possibly schema-qualified). + */ +#define COMPLETE_WITH_QUERY(query) \ +do { \ + completion_charp = query; \ + matches = completion_matches(text, complete_from_query); \ +} while (0) + +#define COMPLETE_WITH_SCHEMA_QUERY(query, addon) \ +do { \ + completion_squery = &(query); \ + completion_charp = addon; \ + matches = completion_matches(text, complete_from_schema_query); \ +} while (0) + +#define COMPLETE_WITH_LIST_CS(list) \ +do { \ + completion_charpp = list; \ + completion_case_sensitive = true; \ + matches = completion_matches(text, complete_from_list); \ +} while (0) + +#define COMPLETE_WITH_LIST(list) \ +do { \ + completion_charpp = list; \ + completion_case_sensitive = false; \ + matches = completion_matches(text, complete_from_list); \ +} while (0) + +#define COMPLETE_WITH_CONST(string) \ +do { \ + completion_charp = string; \ + completion_case_sensitive = false; \ + matches = completion_matches(text, complete_from_const); \ +} while (0) + +#define COMPLETE_WITH_ATTR(relation, addon) \ +do { \ + char *_completion_schema; \ + char *_completion_table; \ +\ + _completion_schema = strtokx(relation, " \t\n\r", ".", "\"", 0, \ + false, false, pset.encoding); \ + (void) strtokx(NULL, " \t\n\r", ".", "\"", 0, \ + false, false, pset.encoding); \ + _completion_table = strtokx(NULL, " \t\n\r", ".", "\"", 0, \ + false, false, pset.encoding); \ + if (_completion_table == NULL) \ + { \ + completion_charp = Query_for_list_of_attributes addon; \ + completion_info_charp = relation; \ + } \ + else \ + { \ + completion_charp = Query_for_list_of_attributes_with_schema addon; \ + completion_info_charp = _completion_table; \ + completion_info_charp2 = _completion_schema; \ + } \ + matches = completion_matches(text, complete_from_query); \ +} while (0) + +#define COMPLETE_WITH_ENUM_VALUE(type) \ +do { \ + char *_completion_schema; \ + char *_completion_type; \ +\ + _completion_schema = strtokx(type, " \t\n\r", ".", "\"", 0, \ + false, false, pset.encoding); \ + (void) strtokx(NULL, " \t\n\r", ".", "\"", 0, \ + false, false, pset.encoding); \ + _completion_type = strtokx(NULL, " \t\n\r", ".", "\"", 0, \ + false, false, pset.encoding); \ + if (_completion_type == NULL)\ + { \ + completion_charp = Query_for_list_of_enum_values; \ + completion_info_charp = type; \ + } \ + else \ + { \ + completion_charp = Query_for_list_of_enum_values_with_schema; \ + completion_info_charp = _completion_type; \ + completion_info_charp2 = _completion_schema; \ + } \ + matches = completion_matches(text, complete_from_query); \ +} while (0) + +#define COMPLETE_WITH_FUNCTION_ARG(function) \ +do { \ + char *_completion_schema; \ + char *_completion_function; \ +\ + _completion_schema = strtokx(function, " \t\n\r", ".", "\"", 0, \ + false, false, pset.encoding); \ + (void) strtokx(NULL, " \t\n\r", ".", "\"", 0, \ + false, false, pset.encoding); \ + _completion_function = strtokx(NULL, " \t\n\r", ".", "\"", 0, \ + false, false, pset.encoding); \ + if (_completion_function == NULL) \ + { \ + completion_charp = Query_for_list_of_arguments; \ + completion_info_charp = function; \ + } \ + else \ + { \ + completion_charp = Query_for_list_of_arguments_with_schema; \ + completion_info_charp = _completion_function; \ + completion_info_charp2 = _completion_schema; \ + } \ + matches = completion_matches(text, complete_from_query); \ +} while (0) + +/* + * These macros simplify use of COMPLETE_WITH_LIST for short, fixed lists. + * There is no COMPLETE_WITH_LIST1; use COMPLETE_WITH_CONST for that case. + */ +#define COMPLETE_WITH_LIST2(s1, s2) \ +do { \ + static const char *const list[] = { s1, s2, NULL }; \ + COMPLETE_WITH_LIST(list); \ +} while (0) + +#define COMPLETE_WITH_LIST3(s1, s2, s3) \ +do { \ + static const char *const list[] = { s1, s2, s3, NULL }; \ + COMPLETE_WITH_LIST(list); \ +} while (0) + +#define COMPLETE_WITH_LIST4(s1, s2, s3, s4) \ +do { \ + static const char *const list[] = { s1, s2, s3, s4, NULL }; \ + COMPLETE_WITH_LIST(list); \ +} while (0) + +#define COMPLETE_WITH_LIST5(s1, s2, s3, s4, s5) \ +do { \ + static const char *const list[] = { s1, s2, s3, s4, s5, NULL }; \ + COMPLETE_WITH_LIST(list); \ +} while (0) + +#define COMPLETE_WITH_LIST6(s1, s2, s3, s4, s5, s6) \ +do { \ + static const char *const list[] = { s1, s2, s3, s4, s5, s6, NULL }; \ + COMPLETE_WITH_LIST(list); \ +} while (0) + +#define COMPLETE_WITH_LIST7(s1, s2, s3, s4, s5, s6, s7) \ +do { \ + static const char *const list[] = { s1, s2, s3, s4, s5, s6, s7, NULL }; \ + COMPLETE_WITH_LIST(list); \ +} while (0) + +#define COMPLETE_WITH_LIST8(s1, s2, s3, s4, s5, s6, s7, s8) \ +do { \ + static const char *const list[] = { s1, s2, s3, s4, s5, s6, s7, s8, NULL }; \ + COMPLETE_WITH_LIST(list); \ +} while (0) + +#define COMPLETE_WITH_LIST9(s1, s2, s3, s4, s5, s6, s7, s8, s9) \ +do { \ + static const char *const list[] = { s1, s2, s3, s4, s5, s6, s7, s8, s9, NULL }; \ + COMPLETE_WITH_LIST(list); \ +} while (0) + +#define COMPLETE_WITH_LIST10(s1, s2, s3, s4, s5, s6, s7, s8, s9, s10) \ +do { \ + static const char *const list[] = { s1, s2, s3, s4, s5, s6, s7, s8, s9, s10, NULL }; \ + COMPLETE_WITH_LIST(list); \ +} while (0) + +#define COMPLETE_WITH_LIST11(s1, s2, s3, s4, s5, s6, s7, s8, s9, s10, s11) \ +do { \ + static const char *const list[] = { s1, s2, s3, s4, s5, s6, s7, s8, s9, s10, s11, NULL }; \ + COMPLETE_WITH_LIST(list); \ +} while (0) + +#define COMPLETE_WITH_LIST12(s1, s2, s3, s4, s5, s6, s7, s8, s9, s10, s11, s12) \ +do { \ + static const char *const list[] = { s1, s2, s3, s4, s5, s6, s7, s8, s9, s10, s11, s12, NULL }; \ + COMPLETE_WITH_LIST(list); \ +} while (0) + +/* + * Likewise for COMPLETE_WITH_LIST_CS. + */ +#define COMPLETE_WITH_LIST_CS2(s1, s2) \ +do { \ + static const char *const list[] = { s1, s2, NULL }; \ + COMPLETE_WITH_LIST_CS(list); \ +} while (0) + +#define COMPLETE_WITH_LIST_CS3(s1, s2, s3) \ +do { \ + static const char *const list[] = { s1, s2, s3, NULL }; \ + COMPLETE_WITH_LIST_CS(list); \ +} while (0) + +#define COMPLETE_WITH_LIST_CS4(s1, s2, s3, s4) \ +do { \ + static const char *const list[] = { s1, s2, s3, s4, NULL }; \ + COMPLETE_WITH_LIST_CS(list); \ +} while (0) + +#define COMPLETE_WITH_LIST_CS5(s1, s2, s3, s4, s5) \ +do { \ + static const char *const list[] = { s1, s2, s3, s4, s5, NULL }; \ + COMPLETE_WITH_LIST_CS(list); \ +} while (0) + +/* + * Assembly instructions for schema queries + */ + +static const SchemaQuery Query_for_list_of_aggregates = { + /* catname */ + "pg_catalog.pg_proc p", + /* selcondition */ + "p.proisagg", + /* viscondition */ + "pg_catalog.pg_function_is_visible(p.oid)", + /* namespace */ + "p.pronamespace", + /* result */ + "pg_catalog.quote_ident(p.proname)", + /* qualresult */ + NULL +}; + +static const SchemaQuery Query_for_list_of_datatypes = { + /* catname */ + "pg_catalog.pg_type t", + /* selcondition --- ignore table rowtypes and array types */ + "(t.typrelid = 0 " + " OR (SELECT c.relkind = " CppAsString2(RELKIND_COMPOSITE_TYPE) + " FROM pg_catalog.pg_class c WHERE c.oid = t.typrelid)) " + "AND t.typname !~ '^_'", + /* viscondition */ + "pg_catalog.pg_type_is_visible(t.oid)", + /* namespace */ + "t.typnamespace", + /* result */ + "pg_catalog.format_type(t.oid, NULL)", + /* qualresult */ + "pg_catalog.quote_ident(t.typname)" +}; + +static const SchemaQuery Query_for_list_of_domains = { + /* catname */ + "pg_catalog.pg_type t", + /* selcondition */ + "t.typtype = 'd'", + /* viscondition */ + "pg_catalog.pg_type_is_visible(t.oid)", + /* namespace */ + "t.typnamespace", + /* result */ + "pg_catalog.quote_ident(t.typname)", + /* qualresult */ + NULL +}; + +static const SchemaQuery Query_for_list_of_functions = { + /* catname */ + "pg_catalog.pg_proc p", + /* selcondition */ + NULL, + /* viscondition */ + "pg_catalog.pg_function_is_visible(p.oid)", + /* namespace */ + "p.pronamespace", + /* result */ + "pg_catalog.quote_ident(p.proname)", + /* qualresult */ + NULL +}; + +static const SchemaQuery Query_for_list_of_indexes = { + /* catname */ + "pg_catalog.pg_class c", + /* selcondition */ + "c.relkind IN (" CppAsString2(RELKIND_INDEX) ")", + /* viscondition */ + "pg_catalog.pg_table_is_visible(c.oid)", + /* namespace */ + "c.relnamespace", + /* result */ + "pg_catalog.quote_ident(c.relname)", + /* qualresult */ + NULL +}; + +static const SchemaQuery Query_for_list_of_sequences = { + /* catname */ + "pg_catalog.pg_class c", + /* selcondition */ + "c.relkind IN (" CppAsString2(RELKIND_SEQUENCE) ")", + /* viscondition */ + "pg_catalog.pg_table_is_visible(c.oid)", + /* namespace */ + "c.relnamespace", + /* result */ + "pg_catalog.quote_ident(c.relname)", + /* qualresult */ + NULL +}; + +static const SchemaQuery Query_for_list_of_foreign_tables = { + /* catname */ + "pg_catalog.pg_class c", + /* selcondition */ + "c.relkind IN (" CppAsString2(RELKIND_FOREIGN_TABLE) ")", + /* viscondition */ + "pg_catalog.pg_table_is_visible(c.oid)", + /* namespace */ + "c.relnamespace", + /* result */ + "pg_catalog.quote_ident(c.relname)", + /* qualresult */ + NULL +}; + +static const SchemaQuery Query_for_list_of_tables = { + /* catname */ + "pg_catalog.pg_class c", + /* selcondition */ + "c.relkind IN (" CppAsString2(RELKIND_RELATION) ", " + CppAsString2(RELKIND_PARTITIONED_TABLE) ")", + /* viscondition */ + "pg_catalog.pg_table_is_visible(c.oid)", + /* namespace */ + "c.relnamespace", + /* result */ + "pg_catalog.quote_ident(c.relname)", + /* qualresult */ + NULL +}; + +static const SchemaQuery Query_for_list_of_partitioned_tables = { + /* catname */ + "pg_catalog.pg_class c", + /* selcondition */ + "c.relkind IN (" CppAsString2(RELKIND_PARTITIONED_TABLE) ")", + /* viscondition */ + "pg_catalog.pg_table_is_visible(c.oid)", + /* namespace */ + "c.relnamespace", + /* result */ + "pg_catalog.quote_ident(c.relname)", + /* qualresult */ + NULL +}; + +static const SchemaQuery Query_for_list_of_constraints_with_schema = { + /* catname */ + "pg_catalog.pg_constraint c", + /* selcondition */ + "c.conrelid <> 0", + /* viscondition */ + "true", /* there is no pg_constraint_is_visible */ + /* namespace */ + "c.connamespace", + /* result */ + "pg_catalog.quote_ident(c.conname)", + /* qualresult */ + NULL +}; + +/* Relations supporting INSERT, UPDATE or DELETE */ +static const SchemaQuery Query_for_list_of_updatables = { + /* catname */ + "pg_catalog.pg_class c", + /* selcondition */ + "c.relkind IN (" CppAsString2(RELKIND_RELATION) ", " + CppAsString2(RELKIND_FOREIGN_TABLE) ", " + CppAsString2(RELKIND_VIEW) ", " + CppAsString2(RELKIND_PARTITIONED_TABLE) ")", + /* viscondition */ + "pg_catalog.pg_table_is_visible(c.oid)", + /* namespace */ + "c.relnamespace", + /* result */ + "pg_catalog.quote_ident(c.relname)", + /* qualresult */ + NULL +}; + +static const SchemaQuery Query_for_list_of_relations = { + /* catname */ + "pg_catalog.pg_class c", + /* selcondition */ + NULL, + /* viscondition */ + "pg_catalog.pg_table_is_visible(c.oid)", + /* namespace */ + "c.relnamespace", + /* result */ + "pg_catalog.quote_ident(c.relname)", + /* qualresult */ + NULL +}; + +static const SchemaQuery Query_for_list_of_tsvmf = { + /* catname */ + "pg_catalog.pg_class c", + /* selcondition */ + "c.relkind IN (" CppAsString2(RELKIND_RELATION) ", " + CppAsString2(RELKIND_SEQUENCE) ", " + CppAsString2(RELKIND_VIEW) ", " + CppAsString2(RELKIND_MATVIEW) ", " + CppAsString2(RELKIND_FOREIGN_TABLE) ", " + CppAsString2(RELKIND_PARTITIONED_TABLE) ")", + /* viscondition */ + "pg_catalog.pg_table_is_visible(c.oid)", + /* namespace */ + "c.relnamespace", + /* result */ + "pg_catalog.quote_ident(c.relname)", + /* qualresult */ + NULL +}; + +static const SchemaQuery Query_for_list_of_tmf = { + /* catname */ + "pg_catalog.pg_class c", + /* selcondition */ + "c.relkind IN (" CppAsString2(RELKIND_RELATION) ", " + CppAsString2(RELKIND_MATVIEW) ", " + CppAsString2(RELKIND_FOREIGN_TABLE) ")", + /* viscondition */ + "pg_catalog.pg_table_is_visible(c.oid)", + /* namespace */ + "c.relnamespace", + /* result */ + "pg_catalog.quote_ident(c.relname)", + /* qualresult */ + NULL +}; + +static const SchemaQuery Query_for_list_of_tm = { + /* catname */ + "pg_catalog.pg_class c", + /* selcondition */ + "c.relkind IN (" CppAsString2(RELKIND_RELATION) ", " + CppAsString2(RELKIND_MATVIEW) ")", + /* viscondition */ + "pg_catalog.pg_table_is_visible(c.oid)", + /* namespace */ + "c.relnamespace", + /* result */ + "pg_catalog.quote_ident(c.relname)", + /* qualresult */ + NULL +}; + +static const SchemaQuery Query_for_list_of_views = { + /* catname */ + "pg_catalog.pg_class c", + /* selcondition */ + "c.relkind IN (" CppAsString2(RELKIND_VIEW) ")", + /* viscondition */ + "pg_catalog.pg_table_is_visible(c.oid)", + /* namespace */ + "c.relnamespace", + /* result */ + "pg_catalog.quote_ident(c.relname)", + /* qualresult */ + NULL +}; + +static const SchemaQuery Query_for_list_of_matviews = { + /* catname */ + "pg_catalog.pg_class c", + /* selcondition */ + "c.relkind IN (" CppAsString2(RELKIND_MATVIEW) ")", + /* viscondition */ + "pg_catalog.pg_table_is_visible(c.oid)", + /* namespace */ + "c.relnamespace", + /* result */ + "pg_catalog.quote_ident(c.relname)", + /* qualresult */ + NULL +}; + +static const SchemaQuery Query_for_list_of_statistics = { + /* catname */ + "pg_catalog.pg_statistic_ext s", + /* selcondition */ + NULL, + /* viscondition */ + "pg_catalog.pg_statistics_obj_is_visible(s.oid)", + /* namespace */ + "s.stxnamespace", + /* result */ + "pg_catalog.quote_ident(s.stxname)", + /* qualresult */ + NULL +}; + + +/* + * Queries to get lists of names of various kinds of things, possibly + * restricted to names matching a partially entered name. In these queries, + * the first %s will be replaced by the text entered so far (suitably escaped + * to become a SQL literal string). %d will be replaced by the length of the + * string (in unescaped form). A second and third %s, if present, will be + * replaced by a suitably-escaped version of the string provided in + * completion_info_charp. A fourth and fifth %s are similarly replaced by + * completion_info_charp2. + * + * Beware that the allowed sequences of %s and %d are determined by + * _complete_from_query(). + */ + +#define Query_for_list_of_attributes \ +"SELECT pg_catalog.quote_ident(attname) "\ +" FROM pg_catalog.pg_attribute a, pg_catalog.pg_class c "\ +" WHERE c.oid = a.attrelid "\ +" AND a.attnum > 0 "\ +" AND NOT a.attisdropped "\ +" AND substring(pg_catalog.quote_ident(attname),1,%d)='%s' "\ +" AND (pg_catalog.quote_ident(relname)='%s' "\ +" OR '\"' || relname || '\"'='%s') "\ +" AND pg_catalog.pg_table_is_visible(c.oid)" + +#define Query_for_list_of_attributes_with_schema \ +"SELECT pg_catalog.quote_ident(attname) "\ +" FROM pg_catalog.pg_attribute a, pg_catalog.pg_class c, pg_catalog.pg_namespace n "\ +" WHERE c.oid = a.attrelid "\ +" AND n.oid = c.relnamespace "\ +" AND a.attnum > 0 "\ +" AND NOT a.attisdropped "\ +" AND substring(pg_catalog.quote_ident(attname),1,%d)='%s' "\ +" AND (pg_catalog.quote_ident(relname)='%s' "\ +" OR '\"' || relname || '\"' ='%s') "\ +" AND (pg_catalog.quote_ident(nspname)='%s' "\ +" OR '\"' || nspname || '\"' ='%s') " + +#define Query_for_list_of_aws_regions \ +"SELECT '''ap-northeast-1''' UNION ALL "\ +"SELECT '''ap-northeast-2''' UNION ALL "\ +"SELECT '''ap-south-1''' UNION ALL "\ +"SELECT '''ap-southeast-1''' UNION ALL "\ +"SELECT '''ap-southeast-2''' UNION ALL "\ +"SELECT '''ca-central-1''' UNION ALL "\ +"SELECT '''eu-central-1''' UNION ALL "\ +"SELECT '''eu-west-1''' UNION ALL "\ +"SELECT '''eu-west-2''' UNION ALL "\ +"SELECT '''sa-east-1''' UNION ALL "\ +"SELECT '''us-east-1''' UNION ALL "\ +"SELECT '''us-east-2''' UNION ALL "\ +"SELECT '''us-west-1''' UNION ALL "\ +"SELECT '''us-west-2''' " + +#define Query_for_list_of_column_compressions \ +" SELECT 'BYTEDICT' UNION ALL "\ +" SELECT 'DELTA' UNION ALL "\ +" SELECT 'DELTA32K' UNION ALL "\ +" SELECT 'LZO' UNION ALL "\ +" SELECT 'MOSTLY8' UNION ALL "\ +" SELECT 'MOSTLY16' UNION ALL "\ +" SELECT 'MOSTLY32' UNION ALL "\ +" SELECT 'RAW' UNION ALL "\ +" SELECT 'RUNLENGTH' UNION ALL "\ +" SELECT 'TEXT255' UNION ALL "\ +" SELECT 'TEXT32K' UNION ALL "\ +" SELECT 'ZST' " + +#define Query_for_list_of_cursors \ +" SELECT trim(name) FROM pg_cursors ORDER BY 1" + +#define Query_for_list_of_enum_values \ +"SELECT pg_catalog.quote_literal(enumlabel) "\ +" FROM pg_catalog.pg_enum e, pg_catalog.pg_type t "\ +" WHERE t.oid = e.enumtypid "\ +" AND substring(pg_catalog.quote_literal(enumlabel),1,%d)='%s' "\ +" AND (pg_catalog.quote_ident(typname)='%s' "\ +" OR '\"' || typname || '\"'='%s') "\ +" AND pg_catalog.pg_type_is_visible(t.oid)" + +#define Query_for_list_of_enum_values_with_schema \ +"SELECT pg_catalog.quote_literal(enumlabel) "\ +" FROM pg_catalog.pg_enum e, pg_catalog.pg_type t, pg_catalog.pg_namespace n "\ +" WHERE t.oid = e.enumtypid "\ +" AND n.oid = t.typnamespace "\ +" AND substring(pg_catalog.quote_literal(enumlabel),1,%d)='%s' "\ +" AND (pg_catalog.quote_ident(typname)='%s' "\ +" OR '\"' || typname || '\"'='%s') "\ +" AND (pg_catalog.quote_ident(nspname)='%s' "\ +" OR '\"' || nspname || '\"' ='%s') " + +#define Query_for_list_of_template_databases \ +"SELECT pg_catalog.quote_ident(d.datname) "\ +" FROM pg_catalog.pg_database d "\ +" WHERE substring(pg_catalog.quote_ident(d.datname),1,%d)='%s' "\ +" AND (d.datistemplate OR pg_catalog.pg_has_role(d.datdba, 'USAGE'))" + +#define Query_for_list_of_databases \ +"SELECT pg_catalog.quote_ident(datname) FROM pg_catalog.pg_database "\ +" WHERE substring(pg_catalog.quote_ident(datname),1,%d)='%s'" + +#define Query_for_list_of_tablespaces \ +"SELECT pg_catalog.quote_ident(spcname) FROM pg_catalog.pg_tablespace "\ +" WHERE substring(pg_catalog.quote_ident(spcname),1,%d)='%s'" + +#define Query_for_list_of_encodings \ +" SELECT DISTINCT pg_catalog.pg_encoding_to_char(conforencoding) "\ +" FROM pg_catalog.pg_conversion "\ +" WHERE substring(pg_catalog.pg_encoding_to_char(conforencoding),1,%d)=UPPER('%s')" + +#define Query_for_list_of_languages \ +"SELECT pg_catalog.quote_ident(lanname) "\ +" FROM pg_catalog.pg_language "\ +" WHERE lanname != 'internal' "\ +" AND substring(pg_catalog.quote_ident(lanname),1,%d)='%s'" + +#define Query_for_list_of_schemas \ +"SELECT pg_catalog.quote_ident(nspname) FROM pg_catalog.pg_namespace "\ +" WHERE substring(pg_catalog.quote_ident(nspname),1,%d)='%s'" + +#define Query_for_list_of_external_schemas \ +"SELECT pg_catalog.quote_ident(schemaname) FROM SVV_EXTERNAL_SCHEMAS "\ +" WHERE substring(pg_catalog.quote_ident(schemaname),1,%d)='%s'" + +#define Query_for_list_of_external_tables \ +"SELECT pg_catalog.quote_ident(tablename) FROM SVV_EXTERNAL_TABLES "\ +" WHERE substring(pg_catalog.quote_ident(tablename),1,%d)='%s'" + +#define Query_for_list_of_alter_system_set_vars \ +"SELECT name FROM "\ +" (SELECT pg_catalog.lower(name) AS name FROM pg_catalog.pg_settings "\ +" WHERE context != 'internal' "\ +" UNION ALL SELECT 'all') ss "\ +" WHERE substring(name,1,%d)='%s'" + +#define Query_for_list_of_pids \ +"SELECT process || ' (user->' || usename || '; database->' || datname || ')' FROM pg_stat_activity ORDER BY datname, usename" + +#define Query_for_list_of_set_vars \ +"SELECT name FROM "\ +" (SELECT pg_catalog.lower(name) AS name FROM pg_catalog.pg_settings "\ +" WHERE context IN ('user', 'superuser') "\ +" UNION ALL SELECT 'constraints' "\ +" UNION ALL SELECT 'transaction' "\ +" UNION ALL SELECT 'session' "\ +" UNION ALL SELECT 'role' "\ +" UNION ALL SELECT 'tablespace' "\ +" UNION ALL SELECT 'all') ss "\ +" WHERE substring(name,1,%d)='%s'" + +#define Query_for_list_of_show_vars \ +"SELECT name FROM "\ +" (SELECT pg_catalog.lower(name) AS name FROM pg_catalog.pg_settings "\ +" UNION ALL SELECT 'session authorization' "\ +" UNION ALL SELECT 'all') ss "\ +" WHERE substring(name,1,%d)='%s'" + +#define Query_for_list_of_roles \ +" SELECT pg_catalog.quote_ident(rolname) "\ +" FROM pg_catalog.pg_roles "\ +" WHERE substring(pg_catalog.quote_ident(rolname),1,%d)='%s'" + +#define Query_for_list_of_users \ +" SELECT pg_catalog.quote_ident(usename) "\ +" FROM pg_catalog.pg_user "\ +" WHERE substring(pg_catalog.quote_ident(usename),1,%d)='%s'" + +#define Query_for_list_of_grant_roles \ +" SELECT pg_catalog.quote_ident(rolname) "\ +" FROM pg_catalog.pg_roles "\ +" WHERE substring(pg_catalog.quote_ident(rolname),1,%d)='%s'"\ +" UNION ALL SELECT 'PUBLIC'"\ +" UNION ALL SELECT 'CURRENT_USER'"\ +" UNION ALL SELECT 'SESSION_USER'" + +/* the silly-looking length condition is just to eat up the current word */ +#define Query_for_table_owning_index \ +"SELECT pg_catalog.quote_ident(c1.relname) "\ +" FROM pg_catalog.pg_class c1, pg_catalog.pg_class c2, pg_catalog.pg_index i"\ +" WHERE c1.oid=i.indrelid and i.indexrelid=c2.oid"\ +" and (%d = pg_catalog.length('%s'))"\ +" and pg_catalog.quote_ident(c2.relname)='%s'"\ +" and pg_catalog.pg_table_is_visible(c2.oid)" + +/* the silly-looking length condition is just to eat up the current word */ +#define Query_for_index_of_table \ +"SELECT pg_catalog.quote_ident(c2.relname) "\ +" FROM pg_catalog.pg_class c1, pg_catalog.pg_class c2, pg_catalog.pg_index i"\ +" WHERE c1.oid=i.indrelid and i.indexrelid=c2.oid"\ +" and (%d = pg_catalog.length('%s'))"\ +" and pg_catalog.quote_ident(c1.relname)='%s'"\ +" and pg_catalog.pg_table_is_visible(c2.oid)" + +/* the silly-looking length condition is just to eat up the current word */ +#define Query_for_constraint_of_table \ +"SELECT pg_catalog.quote_ident(conname) "\ +" FROM pg_catalog.pg_class c1, pg_catalog.pg_constraint con "\ +" WHERE c1.oid=conrelid and (%d = pg_catalog.length('%s'))"\ +" and pg_catalog.quote_ident(c1.relname)='%s'"\ +" and pg_catalog.pg_table_is_visible(c1.oid)" + +#define Query_for_all_table_constraints \ +"SELECT pg_catalog.quote_ident(conname) "\ +" FROM pg_catalog.pg_constraint c "\ +" WHERE c.conrelid <> 0 " + +/* the silly-looking length condition is just to eat up the current word */ +#define Query_for_constraint_of_type \ +"SELECT pg_catalog.quote_ident(conname) "\ +" FROM pg_catalog.pg_type t, pg_catalog.pg_constraint con "\ +" WHERE t.oid=contypid and (%d = pg_catalog.length('%s'))"\ +" and pg_catalog.quote_ident(t.typname)='%s'"\ +" and pg_catalog.pg_type_is_visible(t.oid)" + +/* the silly-looking length condition is just to eat up the current word */ +#define Query_for_list_of_tables_for_constraint \ +"SELECT pg_catalog.quote_ident(relname) "\ +" FROM pg_catalog.pg_class"\ +" WHERE (%d = pg_catalog.length('%s'))"\ +" AND oid IN "\ +" (SELECT conrelid FROM pg_catalog.pg_constraint "\ +" WHERE pg_catalog.quote_ident(conname)='%s')" + +/* the silly-looking length condition is just to eat up the current word */ +#define Query_for_rule_of_table \ +"SELECT pg_catalog.quote_ident(rulename) "\ +" FROM pg_catalog.pg_class c1, pg_catalog.pg_rewrite "\ +" WHERE c1.oid=ev_class and (%d = pg_catalog.length('%s'))"\ +" and pg_catalog.quote_ident(c1.relname)='%s'"\ +" and pg_catalog.pg_table_is_visible(c1.oid)" + +/* the silly-looking length condition is just to eat up the current word */ +#define Query_for_list_of_tables_for_rule \ +"SELECT pg_catalog.quote_ident(relname) "\ +" FROM pg_catalog.pg_class"\ +" WHERE (%d = pg_catalog.length('%s'))"\ +" AND oid IN "\ +" (SELECT ev_class FROM pg_catalog.pg_rewrite "\ +" WHERE pg_catalog.quote_ident(rulename)='%s')" + +/* the silly-looking length condition is just to eat up the current word */ +#define Query_for_trigger_of_table \ +"SELECT pg_catalog.quote_ident(tgname) "\ +" FROM pg_catalog.pg_class c1, pg_catalog.pg_trigger "\ +" WHERE c1.oid=tgrelid and (%d = pg_catalog.length('%s'))"\ +" and pg_catalog.quote_ident(c1.relname)='%s'"\ +" and pg_catalog.pg_table_is_visible(c1.oid)"\ +" and not tgisinternal" + +/* the silly-looking length condition is just to eat up the current word */ +#define Query_for_list_of_tables_for_trigger \ +"SELECT pg_catalog.quote_ident(relname) "\ +" FROM pg_catalog.pg_class"\ +" WHERE (%d = pg_catalog.length('%s'))"\ +" AND oid IN "\ +" (SELECT tgrelid FROM pg_catalog.pg_trigger "\ +" WHERE pg_catalog.quote_ident(tgname)='%s')" + +#define Query_for_list_of_ts_configurations \ +"SELECT pg_catalog.quote_ident(cfgname) FROM pg_catalog.pg_ts_config "\ +" WHERE substring(pg_catalog.quote_ident(cfgname),1,%d)='%s'" + +#define Query_for_list_of_ts_dictionaries \ +"SELECT pg_catalog.quote_ident(dictname) FROM pg_catalog.pg_ts_dict "\ +" WHERE substring(pg_catalog.quote_ident(dictname),1,%d)='%s'" + +#define Query_for_list_of_ts_parsers \ +"SELECT pg_catalog.quote_ident(prsname) FROM pg_catalog.pg_ts_parser "\ +" WHERE substring(pg_catalog.quote_ident(prsname),1,%d)='%s'" + +#define Query_for_list_of_ts_templates \ +"SELECT pg_catalog.quote_ident(tmplname) FROM pg_catalog.pg_ts_template "\ +" WHERE substring(pg_catalog.quote_ident(tmplname),1,%d)='%s'" + +#define Query_for_list_of_fdws \ +" SELECT pg_catalog.quote_ident(fdwname) "\ +" FROM pg_catalog.pg_foreign_data_wrapper "\ +" WHERE substring(pg_catalog.quote_ident(fdwname),1,%d)='%s'" + +#define Query_for_list_of_servers \ +" SELECT pg_catalog.quote_ident(srvname) "\ +" FROM pg_catalog.pg_foreign_server "\ +" WHERE substring(pg_catalog.quote_ident(srvname),1,%d)='%s'" + +#define Query_for_list_of_user_mappings \ +" SELECT pg_catalog.quote_ident(usename) "\ +" FROM pg_catalog.pg_user_mappings "\ +" WHERE substring(pg_catalog.quote_ident(usename),1,%d)='%s'" + +#define Query_for_list_of_access_methods \ +" SELECT pg_catalog.quote_ident(amname) "\ +" FROM pg_catalog.pg_am "\ +" WHERE substring(pg_catalog.quote_ident(amname),1,%d)='%s'" + +#define Query_for_list_of_libraries \ +" SELECT pg_catalog.quote_ident(name) "\ +" FROM pg_catalog.pg_library "\ +" WHERE substring(pg_catalog.quote_ident(name),1,%d)='%s'" + +#define Query_for_list_of_publications \ +" SELECT pg_catalog.quote_ident(pubname) "\ +" FROM pg_catalog.pg_publication "\ +" WHERE substring(pg_catalog.quote_ident(pubname),1,%d)='%s'" + +#define Query_for_list_of_subscriptions \ +" SELECT pg_catalog.quote_ident(s.subname) "\ +" FROM pg_catalog.pg_subscription s, pg_catalog.pg_database d "\ +" WHERE substring(pg_catalog.quote_ident(s.subname),1,%d)='%s' "\ +" AND d.datname = pg_catalog.current_database() "\ +" AND s.subdbid = d.oid" + +/* the silly-looking length condition is just to eat up the current word */ +#define Query_for_list_of_arguments \ +"SELECT pg_catalog.oidvectortypes(proargtypes)||')' "\ +" FROM pg_catalog.pg_proc "\ +" WHERE (%d = pg_catalog.length('%s'))"\ +" AND (pg_catalog.quote_ident(proname)='%s'"\ +" OR '\"' || proname || '\"'='%s') "\ +" AND (pg_catalog.pg_function_is_visible(pg_proc.oid))" + +/* the silly-looking length condition is just to eat up the current word */ +#define Query_for_list_of_arguments_with_schema \ +"SELECT pg_catalog.oidvectortypes(proargtypes)||')' "\ +" FROM pg_catalog.pg_proc p, pg_catalog.pg_namespace n "\ +" WHERE (%d = pg_catalog.length('%s'))"\ +" AND n.oid = p.pronamespace "\ +" AND (pg_catalog.quote_ident(proname)='%s' "\ +" OR '\"' || proname || '\"' ='%s') "\ +" AND (pg_catalog.quote_ident(nspname)='%s' "\ +" OR '\"' || nspname || '\"' ='%s') " + +#define Query_for_list_of_extensions \ +" SELECT pg_catalog.quote_ident(extname) "\ +" FROM pg_catalog.pg_extension "\ +" WHERE substring(pg_catalog.quote_ident(extname),1,%d)='%s'" + +#define Query_for_list_of_available_extensions \ +" SELECT pg_catalog.quote_ident(name) "\ +" FROM pg_catalog.pg_available_extensions "\ +" WHERE substring(pg_catalog.quote_ident(name),1,%d)='%s' AND installed_version IS NULL" + +/* the silly-looking length condition is just to eat up the current word */ +#define Query_for_list_of_available_extension_versions \ +" SELECT pg_catalog.quote_ident(version) "\ +" FROM pg_catalog.pg_available_extension_versions "\ +" WHERE (%d = pg_catalog.length('%s'))"\ +" AND pg_catalog.quote_ident(name)='%s'" + +/* the silly-looking length condition is just to eat up the current word */ +#define Query_for_list_of_available_extension_versions_with_TO \ +" SELECT 'TO ' || pg_catalog.quote_ident(version) "\ +" FROM pg_catalog.pg_available_extension_versions "\ +" WHERE (%d = pg_catalog.length('%s'))"\ +" AND pg_catalog.quote_ident(name)='%s'" + +#define Query_for_list_of_prepared_statements \ +" SELECT pg_catalog.quote_ident(name) "\ +" FROM pg_catalog.pg_prepared_statements "\ +" WHERE substring(pg_catalog.quote_ident(name),1,%d)='%s'" + +#define Query_for_list_of_event_triggers \ +" SELECT pg_catalog.quote_ident(evtname) "\ +" FROM pg_catalog.pg_event_trigger "\ +" WHERE substring(pg_catalog.quote_ident(evtname),1,%d)='%s'" + +#define Query_for_list_of_tablesample_methods \ +" SELECT pg_catalog.quote_ident(proname) "\ +" FROM pg_catalog.pg_proc "\ +" WHERE prorettype = 'pg_catalog.tsm_handler'::pg_catalog.regtype AND "\ +" proargtypes[0] = 'pg_catalog.internal'::pg_catalog.regtype AND "\ +" substring(pg_catalog.quote_ident(proname),1,%d)='%s'" + +#define Query_for_list_of_policies \ +" SELECT pg_catalog.quote_ident(polname) "\ +" FROM pg_catalog.pg_policy "\ +" WHERE substring(pg_catalog.quote_ident(polname),1,%d)='%s'" + +#define Query_for_list_of_tables_for_policy \ +"SELECT pg_catalog.quote_ident(relname) "\ +" FROM pg_catalog.pg_class"\ +" WHERE (%d = pg_catalog.length('%s'))"\ +" AND oid IN "\ +" (SELECT polrelid FROM pg_catalog.pg_policy "\ +" WHERE pg_catalog.quote_ident(polname)='%s')" + +#define Query_for_enum \ +" SELECT name FROM ( "\ +" SELECT pg_catalog.quote_ident(pg_catalog.unnest(enumvals)) AS name "\ +" FROM pg_catalog.pg_settings "\ +" WHERE pg_catalog.lower(name)=pg_catalog.lower('%s') "\ +" UNION ALL " \ +" SELECT 'DEFAULT' ) ss "\ +" WHERE pg_catalog.substring(name,1,%%d)='%%s'" + +/* the silly-looking length condition is just to eat up the current word */ +#define Query_for_partition_of_table \ +"SELECT pg_catalog.quote_ident(c2.relname) "\ +" FROM pg_catalog.pg_class c1, pg_catalog.pg_class c2, pg_catalog.pg_inherits i"\ +" WHERE c1.oid=i.inhparent and i.inhrelid=c2.oid"\ +" and (%d = pg_catalog.length('%s'))"\ +" and pg_catalog.quote_ident(c1.relname)='%s'"\ +" and pg_catalog.pg_table_is_visible(c2.oid)"\ +" and c2.relispartition = 'true'" + +/* + * This is a list of all "things" in Pgsql, which can show up after CREATE or + * DROP; and there is also a query to get a list of them. + */ + +typedef struct +{ + const char *name; + const char *query; /* simple query, or NULL */ + const SchemaQuery *squery; /* schema query, or NULL */ + const bits32 flags; /* visibility flags, see below */ +} pgsql_thing_t; + +#define THING_NO_CREATE (1 << 0) /* should not show up after CREATE */ +#define THING_NO_DROP (1 << 1) /* should not show up after DROP */ +#define THING_NO_ALTER (1 << 2) /* should not show up after ALTER */ +#define THING_NO_SHOW (THING_NO_CREATE | THING_NO_DROP | THING_NO_ALTER) + +static const pgsql_thing_t words_after_create[] = { + {"ACCESS METHOD", NULL, NULL, THING_NO_ALTER}, + {"AGGREGATE", NULL, &Query_for_list_of_aggregates}, + {"CAST", NULL, NULL}, /* Casts have complex structures for names, so + * skip it */ + {"COLLATION", "SELECT pg_catalog.quote_ident(collname) FROM pg_catalog.pg_collation WHERE collencoding IN (-1, pg_catalog.pg_char_to_encoding(pg_catalog.getdatabaseencoding())) AND substring(pg_catalog.quote_ident(collname),1,%d)='%s'"}, + + /* + * CREATE CONSTRAINT TRIGGER is not supported here because it is designed + * to be used only by pg_dump. + */ + {"CONFIGURATION", Query_for_list_of_ts_configurations, NULL, THING_NO_SHOW}, + {"CONTINUOUS TRANSFORM", NULL, NULL, THING_NO_ALTER}, + {"CONTINUOUS VIEW", NULL, NULL, THING_NO_ALTER}, + {"CONVERSION", "SELECT pg_catalog.quote_ident(conname) FROM pg_catalog.pg_conversion WHERE substring(pg_catalog.quote_ident(conname),1,%d)='%s'"}, + {"DATABASE", Query_for_list_of_databases}, + {"DEFAULT PRIVILEGES", NULL, NULL, THING_NO_CREATE | THING_NO_DROP}, + {"DICTIONARY", Query_for_list_of_ts_dictionaries, NULL, THING_NO_SHOW}, + {"DOMAIN", NULL, &Query_for_list_of_domains}, + {"EVENT TRIGGER", NULL, NULL}, + {"EXTENSION", Query_for_list_of_extensions}, + {"EXTERNAL SCHEMA", Query_for_list_of_external_schemas}, + {"EXTERNAL TABLE", Query_for_list_of_external_tables}, + {"FOREIGN DATA WRAPPER", NULL, NULL}, + {"FOREIGN TABLE", NULL, NULL}, + {"FUNCTION", NULL, &Query_for_list_of_functions}, + {"GROUP", Query_for_list_of_roles}, + {"INDEX", NULL, &Query_for_list_of_indexes}, + {"LANGUAGE", Query_for_list_of_languages}, + {"LARGE OBJECT", NULL, NULL, THING_NO_CREATE | THING_NO_DROP}, + {"LIBRARY", NULL, NULL, THING_NO_ALTER}, + {"LOCAL", NULL, NULL, THING_NO_DROP | THING_NO_ALTER}, /* CREATE LOCAL TABLE */ + {"MATERIALIZED VIEW", NULL, &Query_for_list_of_matviews}, + {"OPERATOR", NULL, NULL}, /* Querying for this is probably not such a + * good idea. */ + {"OR REPLACE FUNCTION", NULL, &Query_for_list_of_functions}, + {"OR REPLACE LIBRARY", Query_for_list_of_libraries}, + {"OR REPLACE VIEW", NULL, &Query_for_list_of_views}, + {"OWNED", NULL, NULL, THING_NO_CREATE | THING_NO_ALTER}, /* for DROP OWNED BY ... */ + {"PARSER", Query_for_list_of_ts_parsers, NULL, THING_NO_SHOW}, + {"POLICY", NULL, NULL}, + {"PUBLICATION", Query_for_list_of_publications}, + {"ROLE", Query_for_list_of_roles}, + {"RULE", "SELECT pg_catalog.quote_ident(rulename) FROM pg_catalog.pg_rules WHERE substring(pg_catalog.quote_ident(rulename),1,%d)='%s'"}, + {"SCHEMA", Query_for_list_of_schemas}, + {"SEQUENCE", NULL, &Query_for_list_of_sequences}, + {"SERVER", Query_for_list_of_servers}, + {"STATISTICS", NULL, &Query_for_list_of_statistics}, + {"STREAM", NULL, NULL}, + {"SUBSCRIPTION", Query_for_list_of_subscriptions}, + {"SYSTEM", NULL, NULL, THING_NO_CREATE | THING_NO_DROP}, + {"TABLE", NULL, &Query_for_list_of_tables}, + {"TABLESPACE", Query_for_list_of_tablespaces}, + {"TEMP", NULL, NULL, THING_NO_DROP | THING_NO_ALTER}, /* for CREATE TEMP TABLE + * ... */ + {"TEMPLATE", Query_for_list_of_ts_templates, NULL, THING_NO_SHOW}, + {"TEMPORARY", NULL, NULL, THING_NO_DROP | THING_NO_ALTER}, /* for CREATE TEMPORARY + * TABLE ... */ + {"TEXT SEARCH", NULL, NULL}, + {"TRANSFORM", NULL, NULL}, + {"TRIGGER", "SELECT pg_catalog.quote_ident(tgname) FROM pg_catalog.pg_trigger WHERE substring(pg_catalog.quote_ident(tgname),1,%d)='%s' AND NOT tgisinternal"}, + {"TYPE", NULL, &Query_for_list_of_datatypes}, + {"UNIQUE", NULL, NULL, THING_NO_DROP | THING_NO_ALTER}, /* for CREATE UNIQUE + * INDEX ... */ + {"UNLOGGED", NULL, NULL, THING_NO_DROP | THING_NO_ALTER}, /* for CREATE UNLOGGED + * TABLE ... */ + {"USER", Query_for_list_of_roles " UNION SELECT 'MAPPING FOR'"}, + {"USER MAPPING FOR", NULL, NULL}, + {"VIEW", NULL, &Query_for_list_of_views}, + {NULL} /* end of list */ +}; + + +/* Forward declaration of functions */ +static char **psql_completion(const char *text, int start, int end); +static char *create_command_generator(const char *text, int state); +static char *drop_command_generator(const char *text, int state); +static char *alter_command_generator(const char *text, int state); +static char *complete_from_query(const char *text, int state); +static char *complete_from_schema_query(const char *text, int state); +static char *_complete_from_query(int is_schema_query, + const char *text, int state); +static char *complete_from_list(const char *text, int state); +static char *complete_from_const(const char *text, int state); +static void append_variable_names(char ***varnames, int *nvars, + int *maxvars, const char *varname, + const char *prefix, const char *suffix); +static char **complete_from_variables(const char *text, + const char *prefix, const char *suffix, bool need_value); +static char *complete_from_files(const char *text, int state); + +static char *pg_strdup_keyword_case(const char *s, const char *ref); +static char *escape_string(const char *text); +static PGresult *exec_query(const char *query); + +static char **get_previous_words(int point, char **buffer, int *nwords); + +static char *get_guctype(const char *varname); + +#ifdef NOT_USED +static char *quote_file_name(char *text, int match_type, char *quote_pointer); +static char *dequote_file_name(char *text, char quote_char); +#endif + + +/* + * Initialize the readline library for our purposes. + */ +void +initialize_readline_pipelinedb(void) +{ + rl_readline_name = (char *) pset.progname; + rl_attempted_completion_function = psql_completion; + + rl_basic_word_break_characters = WORD_BREAKS; + + completion_max_records = 1000; + + /* + * There is a variable rl_completion_query_items for this but apparently + * it's not defined everywhere. + */ +} + +/* + * Check if 'word' matches any of the '|'-separated strings in 'pattern', + * using case-insensitive or case-sensitive comparisons. + * + * If pattern is NULL, it's a wild card that matches any word. + * If pattern begins with '!', the result is negated, ie we check that 'word' + * does *not* match any alternative appearing in the rest of 'pattern'. + * Any alternative can end with '*' which is a wild card, i.e., it means + * match any word that matches the characters so far. (We do not currently + * support '*' elsewhere than the end of an alternative.) + * + * For readability, callers should use the macros MatchAny and MatchAnyExcept + * to invoke those two special cases for 'pattern'. (But '|' and '*' must + * just be written directly in patterns.) + */ +#define MatchAny NULL +#define MatchAnyExcept(pattern) ("!" pattern) + +static bool +word_matches_internal(const char *pattern, + const char *word, + bool case_sensitive) +{ + size_t wordlen, + patternlen; + + /* NULL pattern matches anything. */ + if (pattern == NULL) + return true; + + /* Handle negated patterns from the MatchAnyExcept macro. */ + if (*pattern == '!') + return !word_matches_internal(pattern + 1, word, case_sensitive); + + /* Else consider each alternative in the pattern. */ + wordlen = strlen(word); + for (;;) + { + const char *c; + + /* Find end of current alternative. */ + c = pattern; + while (*c != '\0' && *c != '|') + c++; + /* Was there a wild card? (Assumes first alternative is not empty) */ + if (c[-1] == '*') + { + /* Yes, wildcard match? */ + patternlen = c - pattern - 1; + if (wordlen >= patternlen && + (case_sensitive ? + strncmp(word, pattern, patternlen) == 0 : + pg_strncasecmp(word, pattern, patternlen) == 0)) + return true; + } + else + { + /* No, plain match? */ + patternlen = c - pattern; + if (wordlen == patternlen && + (case_sensitive ? + strncmp(word, pattern, wordlen) == 0 : + pg_strncasecmp(word, pattern, wordlen) == 0)) + return true; + } + /* Out of alternatives? */ + if (*c == '\0') + break; + /* Nope, try next alternative. */ + pattern = c + 1; + } + + return false; +} + +/* + * There are enough matching calls below that it seems worth having these two + * interface routines rather than including a third parameter in every call. + * + * word_matches --- match case-insensitively. + */ +static bool +word_matches(const char *pattern, const char *word) +{ + return word_matches_internal(pattern, word, false); +} + +/* + * word_matches_cs --- match case-sensitively. + */ +static bool +word_matches_cs(const char *pattern, const char *word) +{ + return word_matches_internal(pattern, word, true); +} + +/* + * Check if the final character of 's' is 'c'. + */ +static bool +ends_with(const char *s, char c) +{ + size_t length = strlen(s); + + return (length > 0 && s[length - 1] == c); +} + +/* + * The completion function. + * + * According to readline spec this gets passed the text entered so far and its + * start and end positions in the readline buffer. The return value is some + * partially obscure list format that can be generated by readline's + * completion_matches() function, so we don't have to worry about it. + */ +static char ** +psql_completion(const char *text, int start, int end) +{ + /* This is the variable we'll return. */ + char **matches = NULL; + + /* Workspace for parsed words. */ + char *words_buffer; + + /* This array will contain pointers to parsed words. */ + char **previous_words; + + /* The number of words found on the input line. */ + int previous_words_count; + + /* + * For compactness, we use these macros to reference previous_words[]. + * Caution: do not access a previous_words[] entry without having checked + * previous_words_count to be sure it's valid. In most cases below, that + * check is implicit in a TailMatches() or similar macro, but in some + * places we have to check it explicitly. + */ +#define prev_wd (previous_words[0]) +#define prev2_wd (previous_words[1]) +#define prev3_wd (previous_words[2]) +#define prev4_wd (previous_words[3]) +#define prev5_wd (previous_words[4]) +#define prev6_wd (previous_words[5]) +#define prev7_wd (previous_words[6]) +#define prev8_wd (previous_words[7]) +#define prev9_wd (previous_words[8]) + + /* Macros for matching the last N words before point, case-insensitively. */ +#define TailMatches1(p1) \ + (previous_words_count >= 1 && \ + word_matches(p1, prev_wd)) + +#define TailMatches2(p2, p1) \ + (previous_words_count >= 2 && \ + word_matches(p1, prev_wd) && \ + word_matches(p2, prev2_wd)) + +#define TailMatches3(p3, p2, p1) \ + (previous_words_count >= 3 && \ + word_matches(p1, prev_wd) && \ + word_matches(p2, prev2_wd) && \ + word_matches(p3, prev3_wd)) + +#define TailMatches4(p4, p3, p2, p1) \ + (previous_words_count >= 4 && \ + word_matches(p1, prev_wd) && \ + word_matches(p2, prev2_wd) && \ + word_matches(p3, prev3_wd) && \ + word_matches(p4, prev4_wd)) + +#define TailMatches5(p5, p4, p3, p2, p1) \ + (previous_words_count >= 5 && \ + word_matches(p1, prev_wd) && \ + word_matches(p2, prev2_wd) && \ + word_matches(p3, prev3_wd) && \ + word_matches(p4, prev4_wd) && \ + word_matches(p5, prev5_wd)) + +#define TailMatches6(p6, p5, p4, p3, p2, p1) \ + (previous_words_count >= 6 && \ + word_matches(p1, prev_wd) && \ + word_matches(p2, prev2_wd) && \ + word_matches(p3, prev3_wd) && \ + word_matches(p4, prev4_wd) && \ + word_matches(p5, prev5_wd) && \ + word_matches(p6, prev6_wd)) + +#define TailMatches7(p7, p6, p5, p4, p3, p2, p1) \ + (previous_words_count >= 7 && \ + word_matches(p1, prev_wd) && \ + word_matches(p2, prev2_wd) && \ + word_matches(p3, prev3_wd) && \ + word_matches(p4, prev4_wd) && \ + word_matches(p5, prev5_wd) && \ + word_matches(p6, prev6_wd) && \ + word_matches(p7, prev7_wd)) + +#define TailMatches8(p8, p7, p6, p5, p4, p3, p2, p1) \ + (previous_words_count >= 8 && \ + word_matches(p1, prev_wd) && \ + word_matches(p2, prev2_wd) && \ + word_matches(p3, prev3_wd) && \ + word_matches(p4, prev4_wd) && \ + word_matches(p5, prev5_wd) && \ + word_matches(p6, prev6_wd) && \ + word_matches(p7, prev7_wd) && \ + word_matches(p8, prev8_wd)) + +#define TailMatches9(p9, p8, p7, p6, p5, p4, p3, p2, p1) \ + (previous_words_count >= 9 && \ + word_matches(p1, prev_wd) && \ + word_matches(p2, prev2_wd) && \ + word_matches(p3, prev3_wd) && \ + word_matches(p4, prev4_wd) && \ + word_matches(p5, prev5_wd) && \ + word_matches(p6, prev6_wd) && \ + word_matches(p7, prev7_wd) && \ + word_matches(p8, prev8_wd) && \ + word_matches(p9, prev9_wd)) + + /* Macros for matching the last N words before point, case-sensitively. */ +#define TailMatchesCS1(p1) \ + (previous_words_count >= 1 && \ + word_matches_cs(p1, prev_wd)) +#define TailMatchesCS2(p2, p1) \ + (previous_words_count >= 2 && \ + word_matches_cs(p1, prev_wd) && \ + word_matches_cs(p2, prev2_wd)) +#define TailMatchesCS3(p3, p2, p1) \ + (previous_words_count >= 3 && \ + word_matches_cs(p1, prev_wd) && \ + word_matches_cs(p2, prev2_wd) && \ + word_matches_cs(p3, prev3_wd)) +#define TailMatchesCS4(p4, p3, p2, p1) \ + (previous_words_count >= 4 && \ + word_matches_cs(p1, prev_wd) && \ + word_matches_cs(p2, prev2_wd) && \ + word_matches_cs(p3, prev3_wd) && \ + word_matches_cs(p4, prev4_wd)) + + /* + * Macros for matching N words beginning at the start of the line, + * case-insensitively. + */ +#define Matches1(p1) \ + (previous_words_count == 1 && \ + TailMatches1(p1)) +#define Matches2(p1, p2) \ + (previous_words_count == 2 && \ + TailMatches2(p1, p2)) +#define Matches3(p1, p2, p3) \ + (previous_words_count == 3 && \ + TailMatches3(p1, p2, p3)) +#define Matches4(p1, p2, p3, p4) \ + (previous_words_count == 4 && \ + TailMatches4(p1, p2, p3, p4)) +#define Matches5(p1, p2, p3, p4, p5) \ + (previous_words_count == 5 && \ + TailMatches5(p1, p2, p3, p4, p5)) +#define Matches6(p1, p2, p3, p4, p5, p6) \ + (previous_words_count == 6 && \ + TailMatches6(p1, p2, p3, p4, p5, p6)) +#define Matches7(p1, p2, p3, p4, p5, p6, p7) \ + (previous_words_count == 7 && \ + TailMatches7(p1, p2, p3, p4, p5, p6, p7)) +#define Matches8(p1, p2, p3, p4, p5, p6, p7, p8) \ + (previous_words_count == 8 && \ + TailMatches8(p1, p2, p3, p4, p5, p6, p7, p8)) +#define Matches9(p1, p2, p3, p4, p5, p6, p7, p8, p9) \ + (previous_words_count == 9 && \ + TailMatches9(p1, p2, p3, p4, p5, p6, p7, p8, p9)) + + /* + * Macros for matching N words at the start of the line, regardless of + * what is after them, case-insensitively. + */ +#define HeadMatches1(p1) \ + (previous_words_count >= 1 && \ + word_matches(p1, previous_words[previous_words_count - 1])) + +#define HeadMatches2(p1, p2) \ + (previous_words_count >= 2 && \ + word_matches(p1, previous_words[previous_words_count - 1]) && \ + word_matches(p2, previous_words[previous_words_count - 2])) + +#define HeadMatches3(p1, p2, p3) \ + (previous_words_count >= 3 && \ + word_matches(p1, previous_words[previous_words_count - 1]) && \ + word_matches(p2, previous_words[previous_words_count - 2]) && \ + word_matches(p3, previous_words[previous_words_count - 3])) + +#define HeadMatches4(p1, p2, p3, p4) \ + (previous_words_count >= 4 && \ + word_matches(p1, previous_words[previous_words_count - 1]) && \ + word_matches(p2, previous_words[previous_words_count - 2]) && \ + word_matches(p3, previous_words[previous_words_count - 3]) && \ + word_matches(p4, previous_words[previous_words_count - 4])) + +#define HeadMatches5(p1, p2, p3, p4, p5) \ + (previous_words_count >= 5 && \ + word_matches(p1, previous_words[previous_words_count - 1]) && \ + word_matches(p2, previous_words[previous_words_count - 2]) && \ + word_matches(p3, previous_words[previous_words_count - 3]) && \ + word_matches(p4, previous_words[previous_words_count - 4]) && \ + word_matches(p5, previous_words[previous_words_count - 5])) + +#define HeadMatches6(p1, p2, p3, p4, p5, p6) \ + (previous_words_count >= 6 && \ + word_matches(p1, previous_words[previous_words_count - 1]) && \ + word_matches(p2, previous_words[previous_words_count - 2]) && \ + word_matches(p3, previous_words[previous_words_count - 3]) && \ + word_matches(p4, previous_words[previous_words_count - 4]) && \ + word_matches(p5, previous_words[previous_words_count - 5]) && \ + word_matches(p6, previous_words[previous_words_count - 6])) + +#define HeadMatches7(p1, p2, p3, p4, p5, p6, p7) \ + (previous_words_count >= 7 && \ + word_matches(p1, previous_words[previous_words_count - 1]) && \ + word_matches(p2, previous_words[previous_words_count - 2]) && \ + word_matches(p3, previous_words[previous_words_count - 3]) && \ + word_matches(p4, previous_words[previous_words_count - 4]) && \ + word_matches(p5, previous_words[previous_words_count - 5]) && \ + word_matches(p6, previous_words[previous_words_count - 6]) && \ + word_matches(p7, previous_words[previous_words_count - 7])) + +#define HeadMatches8(p1, p2, p3, p4, p5, p6, p7, p8) \ +(previous_words_count >= 8 && \ + word_matches(p1, previous_words[previous_words_count - 1]) && \ + word_matches(p2, previous_words[previous_words_count - 2]) && \ + word_matches(p3, previous_words[previous_words_count - 3]) && \ + word_matches(p4, previous_words[previous_words_count - 4]) && \ + word_matches(p5, previous_words[previous_words_count - 5]) && \ + word_matches(p6, previous_words[previous_words_count - 6]) && \ + word_matches(p7, previous_words[previous_words_count - 7]) && \ + word_matches(p8, previous_words[previous_words_count - 8])) + +#define HeadMatches9(p1, p2, p3, p4, p5, p6, p7, p8, p9) \ +(previous_words_count >= 9 && \ + word_matches(p1, previous_words[previous_words_count - 1]) && \ + word_matches(p2, previous_words[previous_words_count - 2]) && \ + word_matches(p3, previous_words[previous_words_count - 3]) && \ + word_matches(p4, previous_words[previous_words_count - 4]) && \ + word_matches(p5, previous_words[previous_words_count - 5]) && \ + word_matches(p6, previous_words[previous_words_count - 6]) && \ + word_matches(p7, previous_words[previous_words_count - 7]) && \ + word_matches(p8, previous_words[previous_words_count - 8]) && \ + word_matches(p9, previous_words[previous_words_count - 9])) + + /* Known command-starting keywords. */ + static const char *const sql_commands[] = { + "ABORT", "ALTER", "ANALYZE", "BEGIN", "CANCEL", "CHECKPOINT", "CLOSE", "CLUSTER", + "COMMENT", "COMMIT", "COPY", "CREATE", "DEALLOCATE", "DECLARE", + "DELETE FROM", "DISCARD", "DO", "DROP", "END", "EXECUTE", "EXPLAIN", + "FETCH", "GRANT", "IMPORT", "INSERT", "LISTEN", "LOAD", "LOCK", + "MOVE", "NOTIFY", "PREPARE", + "REASSIGN", "REFRESH MATERIALIZED VIEW", "REINDEX", "RELEASE", + "RESET", "REVOKE", "ROLLBACK", + "SAVEPOINT", "SECURITY LABEL", "SELECT", "SET", "SHOW", "START", + "TABLE", "TRUNCATE", "UNLISTEN", "UNLOAD", "UPDATE", "VACUUM", "VALUES", "WITH", + NULL + }; + + /* psql's backslash commands. */ + static const char *const backslash_commands[] = { + "\\a", + "\\connect", "\\conninfo", "\\C", "\\cd", "\\copy", + "\\copyright", "\\crosstabview", + "\\d", "\\da", "\\dA", "\\db", "\\dc", "\\dC", "\\dd", "\\ddp", "\\dD", + "\\des", "\\det", "\\deu", "\\dew", "\\dE", "\\df", + "\\dF", "\\dFd", "\\dFp", "\\dFt", "\\dg", "\\di", "\\dl", "\\dL", + "\\dm", "\\dn", "\\do", "\\dO", "\\dp", + "\\drds", "\\dRs", "\\dRp", "\\ds", "\\dS", + "\\dt", "\\dT", "\\dv", "\\du", "\\dx", "\\dy", + "\\e", "\\echo", "\\ef", "\\elif", "\\else", "\\encoding", + "\\endif", "\\errverbose", "\\ev", + "\\f", + "\\g", "\\gdesc", "\\gexec", "\\gset", "\\gx", + "\\h", "\\help", "\\H", + "\\i", "\\if", "\\ir", + "\\l", "\\lo_import", "\\lo_export", "\\lo_list", "\\lo_unlink", + "\\o", + "\\p", "\\password", "\\prompt", "\\pset", + "\\q", "\\qecho", + "\\r", + "\\s", "\\set", "\\setenv", "\\sf", "\\sv", + "\\t", "\\T", "\\timing", + "\\unset", + "\\x", + "\\w", "\\watch", + "\\z", + "\\!", "\\?", + NULL + }; + + (void) end; /* "end" is not used */ + +#ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER + rl_completion_append_character = ' '; +#endif + +/* Macro to easily identify which Engine (type) are we speaking to + Any change here should probably be replicated elsewhere since + #define for various SQLs (in this script) employ there own string-compare */ + #define IS_PIPELINEDB (strncmp(pset.sengine, "pipelinedb", 10) == 0) + + /* Clear a few things. */ + completion_charp = NULL; + completion_charpp = NULL; + completion_info_charp = NULL; + completion_info_charp2 = NULL; + + /* + * Scan the input line to extract the words before our current position. + * According to those we'll make some smart decisions on what the user is + * probably intending to type. + */ + previous_words = get_previous_words(start, + &words_buffer, + &previous_words_count); + + /* If current word is a backslash command, offer completions for that */ + if (text[0] == '\\') + COMPLETE_WITH_LIST_CS(backslash_commands); + + /* If current word is a variable interpolation, handle that case */ + else if (text[0] == ':' && text[1] != ':') + { + if (text[1] == '\'') + matches = complete_from_variables(text, ":'", "'", true); + else if (text[1] == '"') + matches = complete_from_variables(text, ":\"", "\"", true); + else + matches = complete_from_variables(text, ":", "", true); + } + + /* If no previous word, suggest one of the basic sql commands */ + else if (previous_words_count == 0) + COMPLETE_WITH_LIST(sql_commands); + +/* CREATE */ + /* complete with something you can create */ + else if (TailMatches1("CREATE")) + matches = completion_matches(text, create_command_generator); + +/* DROP, but not DROP embedded in other commands */ + /* complete with something you can drop */ + else if (Matches1("DROP")) + matches = completion_matches(text, drop_command_generator); + +/* ALTER */ + + /* ALTER TABLE */ + else if (Matches2("ALTER", "TABLE")) + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tables, + "UNION SELECT 'ALL IN TABLESPACE'"); + + /* ALTER something */ + else if (Matches1("ALTER")) + matches = completion_matches(text, alter_command_generator); + /* ALTER TABLE,INDEX,MATERIALIZED VIEW ALL IN TABLESPACE xxx */ + else if (TailMatches4("ALL", "IN", "TABLESPACE", MatchAny)) + COMPLETE_WITH_LIST2("SET TABLESPACE", "OWNED BY"); + /* ALTER TABLE,INDEX,MATERIALIZED VIEW ALL IN TABLESPACE xxx OWNED BY */ + else if (TailMatches6("ALL", "IN", "TABLESPACE", MatchAny, "OWNED", "BY")) + COMPLETE_WITH_QUERY(Query_for_list_of_roles); + /* ALTER TABLE,INDEX,MATERIALIZED VIEW ALL IN TABLESPACE xxx OWNED BY xxx */ + else if (TailMatches7("ALL", "IN", "TABLESPACE", MatchAny, "OWNED", "BY", MatchAny)) + COMPLETE_WITH_CONST("SET TABLESPACE"); + /* ALTER AGGREGATE,FUNCTION */ + else if (Matches3("ALTER", "AGGREGATE|FUNCTION", MatchAny)) + COMPLETE_WITH_CONST("("); + /* ALTER AGGREGATE,FUNCTION (...) */ + else if (Matches4("ALTER", "AGGREGATE|FUNCTION", MatchAny, MatchAny)) + { + if (ends_with(prev_wd, ')')) + COMPLETE_WITH_LIST3("OWNER TO", "RENAME TO", "SET SCHEMA"); + else + COMPLETE_WITH_FUNCTION_ARG(prev2_wd); + } + /* ALTER PUBLICATION */ + else if (Matches3("ALTER", "PUBLICATION", MatchAny)) + COMPLETE_WITH_LIST5("ADD TABLE", "DROP TABLE", "OWNER TO", "RENAME TO", "SET"); + /* ALTER PUBLICATION SET */ + else if (Matches4("ALTER", "PUBLICATION", MatchAny, "SET")) + COMPLETE_WITH_LIST2("(", "TABLE"); + /* ALTER PUBLICATION SET ( */ + else if (HeadMatches3("ALTER", "PUBLICATION", MatchAny) && TailMatches2("SET", "(")) + COMPLETE_WITH_CONST("publish"); + /* ALTER SUBSCRIPTION */ + else if (Matches3("ALTER", "SUBSCRIPTION", MatchAny)) + COMPLETE_WITH_LIST7("CONNECTION", "ENABLE", "DISABLE", "OWNER TO", + "RENAME TO", "REFRESH PUBLICATION", "SET"); + /* ALTER SUBSCRIPTION REFRESH PUBLICATION */ + else if (HeadMatches3("ALTER", "SUBSCRIPTION", MatchAny) && + TailMatches2("REFRESH", "PUBLICATION")) + COMPLETE_WITH_CONST("WITH ("); + /* ALTER SUBSCRIPTION REFRESH PUBLICATION WITH ( */ + else if (HeadMatches3("ALTER", "SUBSCRIPTION", MatchAny) && + TailMatches4("REFRESH", "PUBLICATION", "WITH", "(")) + COMPLETE_WITH_CONST("copy_data"); + /* ALTER SUBSCRIPTION SET */ + else if (Matches4("ALTER", "SUBSCRIPTION", MatchAny, "SET")) + COMPLETE_WITH_LIST2("(", "PUBLICATION"); + /* ALTER SUBSCRIPTION SET ( */ + else if (HeadMatches3("ALTER", "SUBSCRIPTION", MatchAny) && TailMatches2("SET", "(")) + COMPLETE_WITH_LIST2("slot_name", "synchronous_commit"); + /* ALTER SUBSCRIPTION SET PUBLICATION */ + else if (HeadMatches3("ALTER", "SUBSCRIPTION", MatchAny) && TailMatches2("SET", "PUBLICATION")) + { + /* complete with nothing here as this refers to remote publications */ + } + /* ALTER SUBSCRIPTION SET PUBLICATION */ + else if (HeadMatches3("ALTER", "SUBSCRIPTION", MatchAny) && + TailMatches3("SET", "PUBLICATION", MatchAny)) + COMPLETE_WITH_CONST("WITH ("); + /* ALTER SUBSCRIPTION SET PUBLICATION WITH ( */ + else if (HeadMatches3("ALTER", "SUBSCRIPTION", MatchAny) && + TailMatches5("SET", "PUBLICATION", MatchAny, "WITH", "(")) + COMPLETE_WITH_LIST2("copy_data", "refresh"); + /* ALTER SCHEMA */ + else if (Matches3("ALTER", "SCHEMA", MatchAny)) + COMPLETE_WITH_LIST2("OWNER TO", "RENAME TO"); + + /* ALTER COLLATION */ + else if (Matches3("ALTER", "COLLATION", MatchAny)) + COMPLETE_WITH_LIST3("OWNER TO", "RENAME TO", "SET SCHEMA"); + + /* ALTER CONVERSION */ + else if (Matches3("ALTER", "CONVERSION", MatchAny)) + COMPLETE_WITH_LIST3("OWNER TO", "RENAME TO", "SET SCHEMA"); + + /* ALTER DATABASE */ + else if (Matches3("ALTER", "DATABASE", MatchAny)) + COMPLETE_WITH_LIST7("RESET", "SET", "OWNER TO", "RENAME TO", + "IS_TEMPLATE", "ALLOW_CONNECTIONS", + "CONNECTION LIMIT"); + + /* ALTER EVENT TRIGGER */ + else if (Matches3("ALTER", "EVENT", "TRIGGER")) + COMPLETE_WITH_QUERY(Query_for_list_of_event_triggers); + + /* ALTER EVENT TRIGGER */ + else if (Matches4("ALTER", "EVENT", "TRIGGER", MatchAny)) + COMPLETE_WITH_LIST4("DISABLE", "ENABLE", "OWNER TO", "RENAME TO"); + + /* ALTER EVENT TRIGGER ENABLE */ + else if (Matches5("ALTER", "EVENT", "TRIGGER", MatchAny, "ENABLE")) + COMPLETE_WITH_LIST2("REPLICA", "ALWAYS"); + + /* ALTER EXTENSION */ + else if (Matches3("ALTER", "EXTENSION", MatchAny)) + COMPLETE_WITH_LIST4("ADD", "DROP", "UPDATE", "SET SCHEMA"); + + /* ALTER EXTENSION UPDATE */ + else if (Matches4("ALTER", "EXTENSION", MatchAny, "UPDATE")) + { + completion_info_charp = prev2_wd; + COMPLETE_WITH_QUERY(Query_for_list_of_available_extension_versions_with_TO); + } + + /* ALTER EXTENSION UPDATE TO */ + else if (Matches5("ALTER", "EXTENSION", MatchAny, "UPDATE", "TO")) + { + completion_info_charp = prev3_wd; + COMPLETE_WITH_QUERY(Query_for_list_of_available_extension_versions); + } + + /* ALTER FOREIGN */ + else if (Matches2("ALTER", "FOREIGN")) + COMPLETE_WITH_LIST2("DATA WRAPPER", "TABLE"); + + /* ALTER FOREIGN DATA WRAPPER */ + else if (Matches5("ALTER", "FOREIGN", "DATA", "WRAPPER", MatchAny)) + COMPLETE_WITH_LIST5("HANDLER", "VALIDATOR", "OPTIONS", "OWNER TO", "RENAME TO"); + + /* ALTER FOREIGN TABLE */ + else if (Matches4("ALTER", "FOREIGN", "TABLE", MatchAny)) + { + static const char *const list_ALTER_FOREIGN_TABLE[] = + {"ADD", "ALTER", "DISABLE TRIGGER", "DROP", "ENABLE", "INHERIT", + "NO INHERIT", "OPTIONS", "OWNER TO", "RENAME", "SET", + "VALIDATE CONSTRAINT", NULL}; + + COMPLETE_WITH_LIST(list_ALTER_FOREIGN_TABLE); + } + + /* ALTER INDEX */ + else if (Matches2("ALTER", "INDEX")) + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_indexes, + "UNION SELECT 'ALL IN TABLESPACE'"); + /* ALTER INDEX */ + else if (Matches3("ALTER", "INDEX", MatchAny)) + COMPLETE_WITH_LIST5("ALTER COLUMN", "OWNER TO", "RENAME TO", "SET", "RESET"); + /* ALTER INDEX ALTER COLUMN */ + else if (Matches6("ALTER", "INDEX", MatchAny, "ALTER", "COLUMN", MatchAny)) + COMPLETE_WITH_CONST("SET STATISTICS"); + /* ALTER INDEX SET */ + else if (Matches4("ALTER", "INDEX", MatchAny, "SET")) + COMPLETE_WITH_LIST2("(", "TABLESPACE"); + /* ALTER INDEX RESET */ + else if (Matches4("ALTER", "INDEX", MatchAny, "RESET")) + COMPLETE_WITH_CONST("("); + /* ALTER INDEX SET|RESET ( */ + else if (Matches5("ALTER", "INDEX", MatchAny, "RESET", "(")) + COMPLETE_WITH_LIST3("fillfactor", "fastupdate", + "gin_pending_list_limit"); + else if (Matches5("ALTER", "INDEX", MatchAny, "SET", "(")) + COMPLETE_WITH_LIST3("fillfactor =", "fastupdate =", + "gin_pending_list_limit ="); + + /* ALTER LANGUAGE */ + else if (Matches3("ALTER", "LANGUAGE", MatchAny)) + COMPLETE_WITH_LIST2("OWNER_TO", "RENAME TO"); + + /* ALTER LARGE OBJECT */ + else if (Matches4("ALTER", "LARGE", "OBJECT", MatchAny)) + COMPLETE_WITH_CONST("OWNER TO"); + + /* ALTER MATERIALIZED VIEW */ + else if (Matches3("ALTER", "MATERIALIZED", "VIEW")) + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_matviews, + "UNION SELECT 'ALL IN TABLESPACE'"); + + /* ALTER USER,ROLE */ + else if (Matches3("ALTER", "USER|ROLE", MatchAny) && + !TailMatches2("USER", "MAPPING")) + { + static const char *const list_ALTERUSER[] = + {"BYPASSRLS", "CONNECTION LIMIT", "CREATEDB", "CREATEROLE", "CREATEUSER", + "ENCRYPTED PASSWORD", "INHERIT", "LOGIN", "NOBYPASSRLS", + "NOCREATEDB", "NOCREATEROLE", "NOCREATEUSER", "NOINHERIT", + "NOLOGIN", "NOREPLICATION", "NOSUPERUSER", "PASSWORD", "RENAME TO", + "REPLICATION", "RESET", "SET", "SUPERUSER", + "VALID UNTIL", "WITH", NULL}; + + COMPLETE_WITH_LIST(list_ALTERUSER); + } + + /* ALTER USER,ROLE WITH */ + else if (Matches4("ALTER", "USER|ROLE", MatchAny, "WITH")) + { + /* Similar to the above, but don't complete "WITH" again. */ + static const char *const list_ALTERUSER_WITH[] = + {"BYPASSRLS", "CONNECTION LIMIT", "CREATEDB", "CREATEROLE", + "ENCRYPTED PASSWORD", "INHERIT", "LOGIN", "NOBYPASSRLS", + "NOCREATEDB", "NOCREATEROLE", "NOINHERIT", + "NOLOGIN", "NOREPLICATION", "NOSUPERUSER", "PASSWORD", "RENAME TO", + "REPLICATION", "RESET", "SET", "SUPERUSER", + "VALID UNTIL", NULL}; + + COMPLETE_WITH_LIST(list_ALTERUSER_WITH); + } + + /* ALTER DEFAULT PRIVILEGES */ + else if (Matches3("ALTER", "DEFAULT", "PRIVILEGES")) + COMPLETE_WITH_LIST2("FOR ROLE", "IN SCHEMA"); + /* ALTER DEFAULT PRIVILEGES FOR */ + else if (Matches4("ALTER", "DEFAULT", "PRIVILEGES", "FOR")) + COMPLETE_WITH_CONST("ROLE"); + /* ALTER DEFAULT PRIVILEGES IN */ + else if (Matches4("ALTER", "DEFAULT", "PRIVILEGES", "IN")) + COMPLETE_WITH_CONST("SCHEMA"); + /* ALTER DEFAULT PRIVILEGES FOR ROLE|USER ... */ + else if (Matches6("ALTER", "DEFAULT", "PRIVILEGES", "FOR", "ROLE|USER", + MatchAny)) + COMPLETE_WITH_LIST3("GRANT", "REVOKE", "IN SCHEMA"); + /* ALTER DEFAULT PRIVILEGES IN SCHEMA ... */ + else if (Matches6("ALTER", "DEFAULT", "PRIVILEGES", "IN", "SCHEMA", + MatchAny)) + COMPLETE_WITH_LIST3("GRANT", "REVOKE", "FOR ROLE"); + /* ALTER DEFAULT PRIVILEGES IN SCHEMA ... FOR */ + else if (Matches7("ALTER", "DEFAULT", "PRIVILEGES", "IN", "SCHEMA", + MatchAny, "FOR")) + COMPLETE_WITH_CONST("ROLE"); + /* ALTER DEFAULT PRIVILEGES FOR ROLE|USER ... IN SCHEMA ... */ + /* ALTER DEFAULT PRIVILEGES IN SCHEMA ... FOR ROLE|USER ... */ + else if (Matches9("ALTER", "DEFAULT", "PRIVILEGES", "FOR", "ROLE|USER", + MatchAny, "IN", "SCHEMA", MatchAny) || + Matches9("ALTER", "DEFAULT", "PRIVILEGES", "IN", "SCHEMA", + MatchAny, "FOR", "ROLE|USER", MatchAny)) + COMPLETE_WITH_LIST2("GRANT", "REVOKE"); + /* ALTER DOMAIN */ + else if (Matches3("ALTER", "DOMAIN", MatchAny)) + COMPLETE_WITH_LIST6("ADD", "DROP", "OWNER TO", "RENAME", "SET", + "VALIDATE CONSTRAINT"); + /* ALTER DOMAIN DROP */ + else if (Matches4("ALTER", "DOMAIN", MatchAny, "DROP")) + COMPLETE_WITH_LIST3("CONSTRAINT", "DEFAULT", "NOT NULL"); + /* ALTER DOMAIN DROP|RENAME|VALIDATE CONSTRAINT */ + else if (Matches5("ALTER", "DOMAIN", MatchAny, "DROP|RENAME|VALIDATE", "CONSTRAINT")) + { + completion_info_charp = prev3_wd; + COMPLETE_WITH_QUERY(Query_for_constraint_of_type); + } + /* ALTER DOMAIN RENAME */ + else if (Matches4("ALTER", "DOMAIN", MatchAny, "RENAME")) + COMPLETE_WITH_LIST2("CONSTRAINT", "TO"); + /* ALTER DOMAIN RENAME CONSTRAINT */ + else if (Matches6("ALTER", "DOMAIN", MatchAny, "RENAME", "CONSTRAINT", MatchAny)) + COMPLETE_WITH_CONST("TO"); + + /* ALTER DOMAIN SET */ + else if (Matches4("ALTER", "DOMAIN", MatchAny, "SET")) + COMPLETE_WITH_LIST3("DEFAULT", "NOT NULL", "SCHEMA"); + /* ALTER SEQUENCE */ + else if (Matches3("ALTER", "SEQUENCE", MatchAny)) + { + static const char *const list_ALTERSEQUENCE[] = + {"INCREMENT", "MINVALUE", "MAXVALUE", "RESTART", "NO", "CACHE", "CYCLE", + "SET SCHEMA", "OWNED BY", "OWNER TO", "RENAME TO", NULL}; + + COMPLETE_WITH_LIST(list_ALTERSEQUENCE); + } + /* ALTER SEQUENCE NO */ + else if (Matches4("ALTER", "SEQUENCE", MatchAny, "NO")) + COMPLETE_WITH_LIST3("MINVALUE", "MAXVALUE", "CYCLE"); + /* ALTER SERVER */ + else if (Matches3("ALTER", "SERVER", MatchAny)) + COMPLETE_WITH_LIST4("VERSION", "OPTIONS", "OWNER TO", "RENAME TO"); + /* ALTER SERVER VERSION */ + else if (Matches5("ALTER", "SERVER", MatchAny, "VERSION", MatchAny)) + COMPLETE_WITH_CONST("OPTIONS"); + /* ALTER SYSTEM SET, RESET, RESET ALL */ + else if (Matches2("ALTER", "SYSTEM")) + COMPLETE_WITH_LIST2("SET", "RESET"); + else if (Matches3("ALTER", "SYSTEM", "SET|RESET")) + COMPLETE_WITH_QUERY(Query_for_list_of_alter_system_set_vars); + else if (Matches4("ALTER", "SYSTEM", "SET", MatchAny)) + COMPLETE_WITH_CONST("TO"); + /* ALTER VIEW */ + else if (Matches3("ALTER", "VIEW", MatchAny)) + COMPLETE_WITH_LIST4("ALTER COLUMN", "OWNER TO", "RENAME TO", + "SET SCHEMA"); + /* ALTER MATERIALIZED VIEW */ + else if (Matches4("ALTER", "MATERIALIZED", "VIEW", MatchAny)) + COMPLETE_WITH_LIST4("ALTER COLUMN", "OWNER TO", "RENAME TO", + "SET SCHEMA"); + + /* ALTER POLICY */ + else if (Matches2("ALTER", "POLICY")) + COMPLETE_WITH_QUERY(Query_for_list_of_policies); + /* ALTER POLICY ON */ + else if (Matches3("ALTER", "POLICY", MatchAny)) + COMPLETE_WITH_CONST("ON"); + /* ALTER POLICY ON
*/ + else if (Matches4("ALTER", "POLICY", MatchAny, "ON")) + { + completion_info_charp = prev2_wd; + COMPLETE_WITH_QUERY(Query_for_list_of_tables_for_policy); + } + /* ALTER POLICY ON
- show options */ + else if (Matches5("ALTER", "POLICY", MatchAny, "ON", MatchAny)) + COMPLETE_WITH_LIST4("RENAME TO", "TO", "USING (", "WITH CHECK ("); + /* ALTER POLICY ON
TO */ + else if (Matches6("ALTER", "POLICY", MatchAny, "ON", MatchAny, "TO")) + COMPLETE_WITH_QUERY(Query_for_list_of_grant_roles); + /* ALTER POLICY ON
USING ( */ + else if (Matches6("ALTER", "POLICY", MatchAny, "ON", MatchAny, "USING")) + COMPLETE_WITH_CONST("("); + /* ALTER POLICY ON
WITH CHECK ( */ + else if (Matches7("ALTER", "POLICY", MatchAny, "ON", MatchAny, "WITH", "CHECK")) + COMPLETE_WITH_CONST("("); + + /* ALTER RULE , add ON */ + else if (Matches3("ALTER", "RULE", MatchAny)) + COMPLETE_WITH_CONST("ON"); + + /* If we have ALTER RULE ON, then add the correct tablename */ + else if (Matches4("ALTER", "RULE", MatchAny, "ON")) + { + completion_info_charp = prev2_wd; + COMPLETE_WITH_QUERY(Query_for_list_of_tables_for_rule); + } + + /* ALTER RULE ON */ + else if (Matches5("ALTER", "RULE", MatchAny, "ON", MatchAny)) + COMPLETE_WITH_CONST("RENAME TO"); + + /* ALTER STATISTICS */ + else if (Matches3("ALTER", "STATISTICS", MatchAny)) + COMPLETE_WITH_LIST3("OWNER TO", "RENAME TO", "SET SCHEMA"); + + /* ALTER TRIGGER , add ON */ + else if (Matches3("ALTER", "TRIGGER", MatchAny)) + COMPLETE_WITH_CONST("ON"); + + else if (Matches4("ALTER", "TRIGGER", MatchAny, MatchAny)) + { + completion_info_charp = prev2_wd; + COMPLETE_WITH_QUERY(Query_for_list_of_tables_for_trigger); + } + + /* + * If we have ALTER TRIGGER ON, then add the correct tablename + */ + else if (Matches4("ALTER", "TRIGGER", MatchAny, "ON")) + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tables, NULL); + + /* ALTER TRIGGER ON */ + else if (Matches5("ALTER", "TRIGGER", MatchAny, "ON", MatchAny)) + COMPLETE_WITH_CONST("RENAME TO"); + + /* + * If we detect ALTER TABLE , suggest sub commands + */ + else if (Matches3("ALTER", "TABLE", MatchAny)) + { + static const char *const list_ALTER2[] = + {"ADD", "ALTER", "APPEND FROM", "CLUSTER ON", "DISABLE", "DROP", "ENABLE", "INHERIT", + "NO INHERIT", "RENAME", "RESET", "OWNER TO", "SET", + "VALIDATE CONSTRAINT", "REPLICA IDENTITY", "ATTACH PARTITION", + "DETACH PARTITION", NULL}; + + COMPLETE_WITH_LIST(list_ALTER2); + } + + /* ALTER TABLE xxx ADD */ + else if (IS_REDSHIFT && Matches4("ALTER", "TABLE", MatchAny, "ADD")) + COMPLETE_WITH_LIST2("COLUMN", "PARTITION"); + + else if (IS_REDSHIFT && HeadMatches6("ALTER", "TABLE", MatchAny, "ADD", MatchAny, MatchAny)) + { + if (TailMatches2("COLUMN", MatchAny)) + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_datatypes, NULL); + else + { + if (TailMatches2("DEFAULT", MatchAny)) + COMPLETE_WITH_LIST3("ENCODE", "NOT NULL", "NULL"); + else if (TailMatches1("ENCODE")) + COMPLETE_WITH_QUERY(Query_for_list_of_column_compressions); + else if (TailMatches2("ENCODE", MatchAny)) + COMPLETE_WITH_LIST3("DEFAULT", "NOT NULL", "NULL"); + else if (TailMatches1("NULL")) + COMPLETE_WITH_LIST2("ENCODE", "DEFAULT"); + else if (TailMatches1("NOT")) + COMPLETE_WITH_CONST("NULL"); + else + if (!TailMatches1("DEFAULT")) + COMPLETE_WITH_LIST4("DEFAULT", "ENCODE", "NOT NULL", "NULL"); + } + } + + else if (IS_REDSHIFT && HeadMatches5("ALTER", "TABLE", MatchAny, "ADD", MatchAny) && + (!TailMatches1("COLUMN"))) + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_datatypes, NULL); + + /* ALTER TABLE xxx ENABLE */ + else if (Matches4("ALTER", "TABLE", MatchAny, "ENABLE")) + COMPLETE_WITH_LIST5("ALWAYS", "REPLICA", "ROW LEVEL SECURITY", "RULE", + "TRIGGER"); + else if (Matches5("ALTER", "TABLE", MatchAny, "ENABLE", "REPLICA|ALWAYS")) + COMPLETE_WITH_LIST2("RULE", "TRIGGER"); + else if (Matches5("ALTER", "TABLE", MatchAny, "ENABLE", "RULE")) + { + completion_info_charp = prev3_wd; + COMPLETE_WITH_QUERY(Query_for_rule_of_table); + } + else if (Matches6("ALTER", "TABLE", MatchAny, "ENABLE", MatchAny, "RULE")) + { + completion_info_charp = prev4_wd; + COMPLETE_WITH_QUERY(Query_for_rule_of_table); + } + else if (Matches5("ALTER", "TABLE", MatchAny, "ENABLE", "TRIGGER")) + { + completion_info_charp = prev3_wd; + COMPLETE_WITH_QUERY(Query_for_trigger_of_table); + } + else if (Matches6("ALTER", "TABLE", MatchAny, "ENABLE", MatchAny, "TRIGGER")) + { + completion_info_charp = prev4_wd; + COMPLETE_WITH_QUERY(Query_for_trigger_of_table); + } + /* ALTER TABLE xxx INHERIT */ + else if (Matches4("ALTER", "TABLE", MatchAny, "INHERIT")) + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tables, ""); + /* ALTER TABLE xxx NO INHERIT */ + else if (Matches5("ALTER", "TABLE", MatchAny, "NO", "INHERIT")) + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tables, ""); + /* ALTER TABLE xxx DISABLE */ + else if (Matches4("ALTER", "TABLE", MatchAny, "DISABLE")) + COMPLETE_WITH_LIST3("ROW LEVEL SECURITY", "RULE", "TRIGGER"); + else if (Matches5("ALTER", "TABLE", MatchAny, "DISABLE", "RULE")) + { + completion_info_charp = prev3_wd; + COMPLETE_WITH_QUERY(Query_for_rule_of_table); + } + else if (Matches5("ALTER", "TABLE", MatchAny, "DISABLE", "TRIGGER")) + { + completion_info_charp = prev3_wd; + COMPLETE_WITH_QUERY(Query_for_trigger_of_table); + } + + /* ALTER TABLE xxx ALTER */ + else if (Matches4("ALTER", "TABLE", MatchAny, "ALTER")) + COMPLETE_WITH_ATTR(prev2_wd, " UNION SELECT 'COLUMN' UNION SELECT 'CONSTRAINT'"); + + /* ALTER TABLE xxx RENAME */ + else if (Matches4("ALTER", "TABLE", MatchAny, "RENAME")) + COMPLETE_WITH_ATTR(prev2_wd, " UNION SELECT 'COLUMN' UNION SELECT 'CONSTRAINT' UNION SELECT 'TO'"); + else if (Matches5("ALTER", "TABLE", MatchAny, "ALTER|RENAME", "COLUMN")) + COMPLETE_WITH_ATTR(prev3_wd, ""); + + /* ALTER TABLE xxx RENAME yyy */ + else if (Matches5("ALTER", "TABLE", MatchAny, "RENAME", MatchAnyExcept("CONSTRAINT|TO"))) + COMPLETE_WITH_CONST("TO"); + + /* ALTER TABLE xxx RENAME COLUMN/CONSTRAINT yyy */ + else if (Matches6("ALTER", "TABLE", MatchAny, "RENAME", "COLUMN|CONSTRAINT", MatchAnyExcept("TO"))) + COMPLETE_WITH_CONST("TO"); + + /* If we have ALTER TABLE DROP, provide COLUMN or CONSTRAINT */ + else if (Matches4("ALTER", "TABLE", MatchAny, "DROP")) + COMPLETE_WITH_LIST3("COLUMN", "CONSTRAINT", "PARTITION"); + /* If we have ALTER TABLE DROP COLUMN, provide list of columns */ + else if (Matches5("ALTER", "TABLE", MatchAny, "DROP", "COLUMN")) + COMPLETE_WITH_ATTR(prev3_wd, ""); + + /* + * If we have ALTER TABLE ALTER|DROP|RENAME|VALIDATE CONSTRAINT, + * provide list of constraints + */ + else if (Matches5("ALTER", "TABLE", MatchAny, "ALTER|DROP|RENAME|VALIDATE", "CONSTRAINT")) + { + completion_info_charp = prev3_wd; + COMPLETE_WITH_QUERY(Query_for_constraint_of_table); + } + /* ALTER TABLE ALTER [COLUMN] */ + else if (Matches6("ALTER", "TABLE", MatchAny, "ALTER", "COLUMN", MatchAny) || + Matches5("ALTER", "TABLE", MatchAny, "ALTER", MatchAny)) + COMPLETE_WITH_LIST6("TYPE", "SET", "RESET", "RESTART", "ADD", "DROP"); + /* ALTER TABLE ALTER [COLUMN] SET */ + else if (Matches7("ALTER", "TABLE", MatchAny, "ALTER", "COLUMN", MatchAny, "SET") || + Matches6("ALTER", "TABLE", MatchAny, "ALTER", MatchAny, "SET")) + COMPLETE_WITH_LIST5("(", "DEFAULT", "NOT NULL", "STATISTICS", "STORAGE"); + /* ALTER TABLE ALTER [COLUMN] SET ( */ + else if (Matches8("ALTER", "TABLE", MatchAny, "ALTER", "COLUMN", MatchAny, "SET", "(") || + Matches7("ALTER", "TABLE", MatchAny, "ALTER", MatchAny, "SET", "(")) + COMPLETE_WITH_LIST2("n_distinct", "n_distinct_inherited"); + /* ALTER TABLE ALTER [COLUMN] SET STORAGE */ + else if (Matches8("ALTER", "TABLE", MatchAny, "ALTER", "COLUMN", MatchAny, "SET", "STORAGE") || + Matches7("ALTER", "TABLE", MatchAny, "ALTER", MatchAny, "SET", "STORAGE")) + COMPLETE_WITH_LIST4("PLAIN", "EXTERNAL", "EXTENDED", "MAIN"); + /* ALTER TABLE ALTER [COLUMN] DROP */ + else if (Matches7("ALTER", "TABLE", MatchAny, "ALTER", "COLUMN", MatchAny, "DROP") || + Matches6("ALTER", "TABLE", MatchAny, "ALTER", MatchAny, "DROP")) + COMPLETE_WITH_LIST3("DEFAULT", "IDENTITY", "NOT NULL"); + else if (Matches4("ALTER", "TABLE", MatchAny, "CLUSTER")) + COMPLETE_WITH_CONST("ON"); + else if (Matches5("ALTER", "TABLE", MatchAny, "CLUSTER", "ON")) + { + completion_info_charp = prev3_wd; + COMPLETE_WITH_QUERY(Query_for_index_of_table); + } + /* If we have ALTER TABLE SET, provide list of attributes and '(' */ + else if (Matches4("ALTER", "TABLE", MatchAny, "SET")) + COMPLETE_WITH_LIST10("(", "LOGGED", "LOCATION", "FILE FORMAT", "TABLE PROPERTIES", "SCHEMA", "TABLESPACE", "UNLOGGED", + "WITH", "WITHOUT"); + + /* + * If we have ALTER TABLE SET TABLESPACE provide a list of + * tablespaces + */ + else if (Matches5("ALTER", "TABLE", MatchAny, "SET", "TABLESPACE")) + COMPLETE_WITH_QUERY(Query_for_list_of_tablespaces); + /* If we have ALTER TABLE SET WITH provide OIDS */ + else if (Matches5("ALTER", "TABLE", MatchAny, "SET", "WITH")) + COMPLETE_WITH_CONST("OIDS"); + /* If we have ALTER TABLE SET WITHOUT provide CLUSTER or OIDS */ + else if (Matches5("ALTER", "TABLE", MatchAny, "SET", "WITHOUT")) + COMPLETE_WITH_LIST2("CLUSTER", "OIDS"); + /* ALTER TABLE RESET */ + else if (Matches4("ALTER", "TABLE", MatchAny, "RESET")) + COMPLETE_WITH_CONST("("); + /* ALTER TABLE SET|RESET ( */ + else if (Matches5("ALTER", "TABLE", MatchAny, "SET|RESET", "(")) + { + static const char *const list_TABLEOPTIONS[] = + { + "autovacuum_analyze_scale_factor", + "autovacuum_analyze_threshold", + "autovacuum_enabled", + "autovacuum_freeze_max_age", + "autovacuum_freeze_min_age", + "autovacuum_freeze_table_age", + "autovacuum_multixact_freeze_max_age", + "autovacuum_multixact_freeze_min_age", + "autovacuum_multixact_freeze_table_age", + "autovacuum_vacuum_cost_delay", + "autovacuum_vacuum_cost_limit", + "autovacuum_vacuum_scale_factor", + "autovacuum_vacuum_threshold", + "fillfactor", + "parallel_workers", + "log_autovacuum_min_duration", + "toast.autovacuum_enabled", + "toast.autovacuum_freeze_max_age", + "toast.autovacuum_freeze_min_age", + "toast.autovacuum_freeze_table_age", + "toast.autovacuum_multixact_freeze_max_age", + "toast.autovacuum_multixact_freeze_min_age", + "toast.autovacuum_multixact_freeze_table_age", + "toast.autovacuum_vacuum_cost_delay", + "toast.autovacuum_vacuum_cost_limit", + "toast.autovacuum_vacuum_scale_factor", + "toast.autovacuum_vacuum_threshold", + "toast.log_autovacuum_min_duration", + "user_catalog_table", + NULL + }; + + COMPLETE_WITH_LIST(list_TABLEOPTIONS); + } + else if (Matches7("ALTER", "TABLE", MatchAny, "REPLICA", "IDENTITY", "USING", "INDEX")) + { + completion_info_charp = prev5_wd; + COMPLETE_WITH_QUERY(Query_for_index_of_table); + } + else if (Matches6("ALTER", "TABLE", MatchAny, "REPLICA", "IDENTITY", "USING")) + COMPLETE_WITH_CONST("INDEX"); + else if (Matches5("ALTER", "TABLE", MatchAny, "REPLICA", "IDENTITY")) + COMPLETE_WITH_LIST4("FULL", "NOTHING", "DEFAULT", "USING"); + else if (Matches4("ALTER", "TABLE", MatchAny, "REPLICA")) + COMPLETE_WITH_CONST("IDENTITY"); + + /* + * If we have ALTER TABLE ATTACH PARTITION, provide a list of + * tables. + */ + else if (Matches5("ALTER", "TABLE", MatchAny, "ATTACH", "PARTITION")) + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tables, ""); + /* Limited completion support for partition bound specification */ + else if (TailMatches3("ATTACH", "PARTITION", MatchAny)) + COMPLETE_WITH_LIST2("FOR VALUES", "DEFAULT"); + else if (TailMatches2("FOR", "VALUES")) + COMPLETE_WITH_LIST2("FROM (", "IN ("); + + /* + * If we have ALTER TABLE DETACH PARTITION, provide a list of + * partitions of . + */ + else if (Matches5("ALTER", "TABLE", MatchAny, "DETACH", "PARTITION")) + { + completion_info_charp = prev3_wd; + COMPLETE_WITH_QUERY(Query_for_partition_of_table); + } + + /* ALTER TABLESPACE with RENAME TO, OWNER TO, SET, RESET */ + else if (Matches3("ALTER", "TABLESPACE", MatchAny)) + COMPLETE_WITH_LIST4("RENAME TO", "OWNER TO", "SET", "RESET"); + /* ALTER TABLESPACE SET|RESET */ + else if (Matches4("ALTER", "TABLESPACE", MatchAny, "SET|RESET")) + COMPLETE_WITH_CONST("("); + /* ALTER TABLESPACE SET|RESET ( */ + else if (Matches5("ALTER", "TABLESPACE", MatchAny, "SET|RESET", "(")) + COMPLETE_WITH_LIST3("seq_page_cost", "random_page_cost", + "effective_io_concurrency"); + + /* ALTER TEXT SEARCH */ + else if (Matches3("ALTER", "TEXT", "SEARCH")) + COMPLETE_WITH_LIST4("CONFIGURATION", "DICTIONARY", "PARSER", "TEMPLATE"); + else if (Matches5("ALTER", "TEXT", "SEARCH", "TEMPLATE|PARSER", MatchAny)) + COMPLETE_WITH_LIST2("RENAME TO", "SET SCHEMA"); + else if (Matches5("ALTER", "TEXT", "SEARCH", "DICTIONARY", MatchAny)) + COMPLETE_WITH_LIST3("OWNER TO", "RENAME TO", "SET SCHEMA"); + else if (Matches5("ALTER", "TEXT", "SEARCH", "CONFIGURATION", MatchAny)) + COMPLETE_WITH_LIST6("ADD MAPPING FOR", "ALTER MAPPING", + "DROP MAPPING FOR", + "OWNER TO", "RENAME TO", "SET SCHEMA"); + + /* complete ALTER TYPE with actions */ + else if (Matches3("ALTER", "TYPE", MatchAny)) + COMPLETE_WITH_LIST7("ADD ATTRIBUTE", "ADD VALUE", "ALTER ATTRIBUTE", + "DROP ATTRIBUTE", + "OWNER TO", "RENAME", "SET SCHEMA"); + /* complete ALTER TYPE ADD with actions */ + else if (Matches4("ALTER", "TYPE", MatchAny, "ADD")) + COMPLETE_WITH_LIST2("ATTRIBUTE", "VALUE"); + /* ALTER TYPE RENAME */ + else if (Matches4("ALTER", "TYPE", MatchAny, "RENAME")) + COMPLETE_WITH_LIST3("ATTRIBUTE", "TO", "VALUE"); + /* ALTER TYPE xxx RENAME (ATTRIBUTE|VALUE) yyy */ + else if (Matches6("ALTER", "TYPE", MatchAny, "RENAME", "ATTRIBUTE|VALUE", MatchAny)) + COMPLETE_WITH_CONST("TO"); + + /* + * If we have ALTER TYPE ALTER/DROP/RENAME ATTRIBUTE, provide list + * of attributes + */ + else if (Matches5("ALTER", "TYPE", MatchAny, "ALTER|DROP|RENAME", "ATTRIBUTE")) + COMPLETE_WITH_ATTR(prev3_wd, ""); + /* ALTER TYPE ALTER ATTRIBUTE */ + else if (Matches6("ALTER", "TYPE", MatchAny, "ALTER", "ATTRIBUTE", MatchAny)) + COMPLETE_WITH_CONST("TYPE"); + /* complete ALTER GROUP */ + else if (Matches3("ALTER", "GROUP", MatchAny)) + COMPLETE_WITH_LIST3("ADD USER", "DROP USER", "RENAME TO"); + /* complete ALTER GROUP ADD|DROP with USER */ + else if (Matches4("ALTER", "GROUP", MatchAny, "ADD|DROP")) + COMPLETE_WITH_CONST("USER"); + /* complete ALTER GROUP ADD|DROP USER with a user name */ + else if (Matches5("ALTER", "GROUP", MatchAny, "ADD|DROP", "USER")) + COMPLETE_WITH_QUERY(Query_for_list_of_roles); + + /* + * If we have ALTER TYPE RENAME VALUE, provide list of enum values + */ + else if (Matches5("ALTER", "TYPE", MatchAny, "RENAME", "VALUE")) + COMPLETE_WITH_ENUM_VALUE(prev3_wd); + +/* BEGIN */ + else if (Matches1("BEGIN")) + COMPLETE_WITH_LIST6("WORK", "TRANSACTION", "ISOLATION LEVEL", "READ", "DEFERRABLE", "NOT DEFERRABLE"); +/* END, ABORT */ + else if (Matches1("END|ABORT")) + COMPLETE_WITH_LIST2("WORK", "TRANSACTION"); +/* COMMIT */ + else if (Matches1("COMMIT")) + COMPLETE_WITH_LIST3("WORK", "TRANSACTION", "PREPARED"); +/* RELEASE SAVEPOINT */ + else if (Matches1("RELEASE")) + COMPLETE_WITH_CONST("SAVEPOINT"); +/* ROLLBACK */ + else if (Matches1("ROLLBACK")) + COMPLETE_WITH_LIST4("WORK", "TRANSACTION", "TO SAVEPOINT", "PREPARED"); +/* CLUSTER */ + else if (Matches1("CLUSTER")) + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tm, "UNION SELECT 'VERBOSE'"); + else if (Matches2("CLUSTER", "VERBOSE")) + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tm, NULL); + /* If we have CLUSTER , then add "USING" */ + else if (Matches2("CLUSTER", MatchAnyExcept("VERBOSE|ON"))) + COMPLETE_WITH_CONST("USING"); + /* If we have CLUSTER VERBOSE , then add "USING" */ + else if (Matches3("CLUSTER", "VERBOSE", MatchAny)) + COMPLETE_WITH_CONST("USING"); + /* If we have CLUSTER USING, then add the index as well */ + else if (Matches3("CLUSTER", MatchAny, "USING") || + Matches4("CLUSTER", "VERBOSE", MatchAny, "USING")) + { + completion_info_charp = prev2_wd; + COMPLETE_WITH_QUERY(Query_for_index_of_table); + } +/* CANCEL */ + else if (IS_REDSHIFT && Matches1("CANCEL")) + COMPLETE_WITH_QUERY(Query_for_list_of_pids); + +/* CLOSE */ + else if (Matches1("CLOSE")) + COMPLETE_WITH_QUERY(Query_for_list_of_cursors); + +/* COMMENT */ + else if (Matches1("COMMENT")) + COMPLETE_WITH_CONST("ON"); + else if (Matches2("COMMENT", "ON")) + { + static const char *const list_COMMENT[] = + {"ACCESS METHOD", "CAST", "COLLATION", "CONVERSION", "DATABASE", + "EVENT TRIGGER", "EXTENSION", + "FOREIGN DATA WRAPPER", "FOREIGN TABLE", + "SERVER", "INDEX", "LANGUAGE", "POLICY", "PUBLICATION", "RULE", + "SCHEMA", "SEQUENCE", "STATISTICS", "SUBSCRIPTION", + "TABLE", "TYPE", "VIEW", "MATERIALIZED VIEW", "COLUMN", "AGGREGATE", "FUNCTION", + "OPERATOR", "TRIGGER", "CONSTRAINT", "DOMAIN", "LARGE OBJECT", + "TABLESPACE", "TEXT SEARCH", "ROLE", NULL}; + + COMPLETE_WITH_LIST(list_COMMENT); + } + else if (Matches4("COMMENT", "ON", "ACCESS", "METHOD")) + COMPLETE_WITH_QUERY(Query_for_list_of_access_methods); + else if (Matches3("COMMENT", "ON", "FOREIGN")) + COMPLETE_WITH_LIST2("DATA WRAPPER", "TABLE"); + else if (Matches4("COMMENT", "ON", "TEXT", "SEARCH")) + COMPLETE_WITH_LIST4("CONFIGURATION", "DICTIONARY", "PARSER", "TEMPLATE"); + else if (Matches3("COMMENT", "ON", "CONSTRAINT")) + COMPLETE_WITH_QUERY(Query_for_all_table_constraints); + else if (Matches4("COMMENT", "ON", "CONSTRAINT", MatchAny)) + COMPLETE_WITH_CONST("ON"); + else if (Matches5("COMMENT", "ON", "CONSTRAINT", MatchAny, "ON")) + { + completion_info_charp = prev2_wd; + COMPLETE_WITH_QUERY(Query_for_list_of_tables_for_constraint); + } + else if (Matches4("COMMENT", "ON", "MATERIALIZED", "VIEW")) + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_matviews, NULL); + else if (Matches4("COMMENT", "ON", "EVENT", "TRIGGER")) + COMPLETE_WITH_QUERY(Query_for_list_of_event_triggers); + else if (Matches4("COMMENT", "ON", MatchAny, MatchAnyExcept("IS")) || + Matches5("COMMENT", "ON", MatchAny, MatchAny, MatchAnyExcept("IS")) || + Matches6("COMMENT", "ON", MatchAny, MatchAny, MatchAny, MatchAnyExcept("IS"))) + COMPLETE_WITH_CONST("IS"); + +/* COPY */ + + /* + * If we have COPY, offer list of tables or "(" (Also cover the analogous + * backslash command). + */ + else if (Matches1("COPY|\\copy")) + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tables, + " UNION ALL SELECT '('"); + /* If we have COPY BINARY, complete with list of tables */ + else if (Matches2("COPY", "BINARY")) + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tables, NULL); + /* If we have COPY (, complete it with legal commands */ + else if (Matches2("COPY|\\copy", "(")) + COMPLETE_WITH_LIST7("SELECT", "TABLE", "VALUES", "INSERT", "UPDATE", "DELETE", "WITH"); + /* If we have COPY [BINARY] , complete it with "TO" or "FROM" */ + else if (Matches2("COPY|\\copy", MatchAny) || + Matches3("COPY", "BINARY", MatchAny)) + COMPLETE_WITH_LIST2("FROM", "TO"); + /* If we have COPY [BINARY] FROM|TO, complete with filename */ + else if (Matches3("COPY|\\copy", MatchAny, "FROM|TO") || + Matches4("COPY", "BINARY", MatchAny, "FROM|TO")) + { + completion_charp = ""; + matches = completion_matches(text, complete_from_files); + } + + /* Handle COPY [BINARY] FROM|TO filename */ + else if (Matches4("COPY|\\copy", MatchAny, "FROM|TO", MatchAny) || + Matches5("COPY", "BINARY", MatchAny, "FROM|TO", MatchAny)) + COMPLETE_WITH_LIST6("BINARY", "OIDS", "DELIMITER", "NULL", "CSV", + "ENCODING"); + + /* Handle COPY [BINARY] FROM|TO filename CSV */ + else if (Matches5("COPY|\\copy", MatchAny, "FROM|TO", MatchAny, "CSV") || + Matches6("COPY", "BINARY", MatchAny, "FROM|TO", MatchAny, "CSV")) + COMPLETE_WITH_LIST5("HEADER", "QUOTE", "ESCAPE", "FORCE QUOTE", + "FORCE NOT NULL"); + + /* COPY FROM REGION */ + else if (IS_REDSHIFT && HeadMatches2("COPY", MatchAny) && TailMatches1("REGION")) + COMPLETE_WITH_QUERY(Query_for_list_of_aws_regions); + + + /* CREATE ACCESS METHOD */ + /* Complete "CREATE ACCESS METHOD " */ + else if (Matches4("CREATE", "ACCESS", "METHOD", MatchAny)) + COMPLETE_WITH_CONST("TYPE"); + /* Complete "CREATE ACCESS METHOD TYPE" */ + else if (Matches5("CREATE", "ACCESS", "METHOD", MatchAny, "TYPE")) + COMPLETE_WITH_CONST("INDEX"); + /* Complete "CREATE ACCESS METHOD TYPE " */ + else if (Matches6("CREATE", "ACCESS", "METHOD", MatchAny, "TYPE", MatchAny)) + COMPLETE_WITH_CONST("HANDLER"); + + /* CREATE DATABASE */ + else if (Matches3("CREATE", "DATABASE", MatchAny)) + COMPLETE_WITH_LIST10("WITH", "OWNER", "TEMPLATE", "ENCODING", "TABLESPACE", + "IS_TEMPLATE", + "ALLOW_CONNECTIONS", "CONNECTION LIMIT", + "LC_COLLATE", "LC_CTYPE"); + + /* CREATE DATABASE WITH */ + else if (Matches4("CREATE", "DATABASE", MatchAny, "WITH")) + COMPLETE_WITH_LIST9("OWNER", "TEMPLATE", "ENCODING", "TABLESPACE", + "IS_TEMPLATE", + "ALLOW_CONNECTIONS", "CONNECTION LIMIT", + "LC_COLLATE", "LC_CTYPE"); + + /* CREATE DATABASE WITH OWNER */ + else if (Matches5("CREATE", "DATABASE", MatchAny, "WITH", "OWNER")) + COMPLETE_WITH_LIST8("TEMPLATE", "ENCODING", "TABLESPACE", + "IS_TEMPLATE", + "ALLOW_CONNECTIONS", "CONNECTION LIMIT", + "LC_COLLATE", "LC_CTYPE"); + + /* CREATE DATABASE OWNER */ + else if (Matches4("CREATE", "DATABASE", MatchAny, "OWNER")) + COMPLETE_WITH_LIST8("TEMPLATE", "ENCODING", "TABLESPACE", + "IS_TEMPLATE", + "ALLOW_CONNECTIONS", "CONNECTION LIMIT", + "LC_COLLATE", "LC_CTYPE"); + + /* CONNECTION LIMIT -- is allowed inside CREATE DATABASE, so use TailMatches */ + else if (TailMatches2("CONNECTION", "LIMIT")) + COMPLETE_WITH_CONST("UNLIMITED"); + + else if (Matches4("CREATE", "DATABASE", MatchAny, "TEMPLATE")) + COMPLETE_WITH_QUERY(Query_for_list_of_template_databases); + + /* CREATE EXTENSION */ + /* Complete with available extensions rather than installed ones. */ + else if (Matches2("CREATE", "EXTENSION")) + COMPLETE_WITH_QUERY(Query_for_list_of_available_extensions); + /* CREATE EXTENSION */ + else if (Matches3("CREATE", "EXTENSION", MatchAny)) + COMPLETE_WITH_LIST3("WITH SCHEMA", "CASCADE", "VERSION"); + /* CREATE EXTENSION VERSION */ + else if (Matches4("CREATE", "EXTENSION", MatchAny, "VERSION")) + { + completion_info_charp = prev2_wd; + COMPLETE_WITH_QUERY(Query_for_list_of_available_extension_versions); + } + + else if (IS_REDSHIFT && (Matches2("CREATE", "EXTERNAL"))) + COMPLETE_WITH_LIST2("SCHEMA", "TABLE"); + + /* CREATE EXTERNAL SCHEMA */ + else if (IS_REDSHIFT && (Matches3("CREATE", "EXTERNAL", "SCHEMA"))) { + if (Matches2("CREATE", "EXTERNAL")) + COMPLETE_WITH_LIST2("SCHEMA", "TABLE"); + else if (Matches3("CREATE", "EXTERNAL", "SCHEMA")) + COMPLETE_WITH_QUERY(Query_for_list_of_external_schemas + " UNION ALL SELECT 'IF NOT EXISTS'"); + else if ( + (Matches7("CREATE", "EXTERNAL", "SCHEMA", "IF", "NOT", "EXISTS", MatchAnyExcept("FROM"))) || + (Matches4("CREATE", "EXTERNAL", "SCHEMA", MatchAnyExcept("FROM"))) + ) + COMPLETE_WITH_CONST("FROM"); + else if (HeadMatches3("CREATE", "EXTERNAL", "SCHEMA") && TailMatches1("FROM")) + COMPLETE_WITH_LIST2("DATA CATALOG", "HIVE METASTORE"); + else if (HeadMatches3("CREATE", "EXTERNAL", "SCHEMA") && + (TailMatches2("DATA", "CATALOG") || TailMatches2("HIVE", "METASTORE")) + ) + COMPLETE_WITH_CONST("DATABASE"); + else if (HeadMatches3("CREATE", "EXTERNAL", "SCHEMA") && TailMatches2("DATABASE", MatchAny)) + COMPLETE_WITH_LIST4("REGION", "URI", "IAM_ROLE", "CREATE EXTERNAL DATABASE IF NOT EXISTS"); + else if (HeadMatches3("CREATE", "EXTERNAL", "SCHEMA") && TailMatches1("REGION")) + COMPLETE_WITH_QUERY(Query_for_list_of_aws_regions); + else if (HeadMatches3("CREATE", "EXTERNAL", "SCHEMA") && TailMatches2("REGION", MatchAny)) + COMPLETE_WITH_LIST3("URI", "IAM_ROLE", "CREATE EXTERNAL DATABASE IF NOT EXISTS"); + else if (HeadMatches3("CREATE", "EXTERNAL", "SCHEMA") && TailMatches2("URI", MatchAny)) + COMPLETE_WITH_LIST3("PORT", "IAM_ROLE", "CREATE EXTERNAL DATABASE IF NOT EXISTS"); + else if (HeadMatches3("CREATE", "EXTERNAL", "SCHEMA") && TailMatches2("PORT", MatchAny)) + COMPLETE_WITH_LIST2("IAM_ROLE", "CREATE EXTERNAL DATABASE IF NOT EXISTS"); + else if (HeadMatches3("CREATE", "EXTERNAL", "SCHEMA") && TailMatches2("IAM_ROLE", MatchAny)) + COMPLETE_WITH_CONST("CREATE EXTERNAL DATABASE IF NOT EXISTS"); + } + + /* CREATE EXTERNAL TABLE */ + else if (IS_REDSHIFT && (Matches3("CREATE", "EXTERNAL", "TABLE"))) { + if (Matches3("CREATE", "EXTERNAL", "TABLE")) + COMPLETE_WITH_QUERY(Query_for_list_of_external_schemas); + else if ((HeadMatches3("CREATE", "EXTERNAL", "TABLE")) && TailMatches1("PARTITIONED")) + COMPLETE_WITH_CONST("BY ("); + else if ((HeadMatches3("CREATE", "EXTERNAL", "TABLE")) && TailMatches1("ROW")) + COMPLETE_WITH_LIST2("FORMAT", "DELIMITED"); + else if ((HeadMatches3("CREATE", "EXTERNAL", "TABLE")) && TailMatches3("ROW", "FORMAT", "DELIMITED")) + COMPLETE_WITH_LIST2("FIELDS TERMINATED BY", "LINES TERMINATED BY"); + else if ((HeadMatches3("CREATE", "EXTERNAL", "TABLE")) && TailMatches3("ROW", "FORMAT", "DELIMITED")) + COMPLETE_WITH_LIST2("FIELDS TERMINATED BY", "LINES TERMINATED BY"); + else if ((HeadMatches3("CREATE", "EXTERNAL", "TABLE")) && TailMatches2("STORED", "AS")) + COMPLETE_WITH_LIST5("PARQUET", "RCFILE", "SEQUENCEFILE", "TEXTFILE", "ORC"); + else if ((HeadMatches3("CREATE", "EXTERNAL", "TABLE")) && TailMatches1("PROPERTIES")) + COMPLETE_WITH_CONST("('numRows'='"); + else if (HeadMatches4("CREATE", "EXTERNAL", "TABLE", MatchAny)) + COMPLETE_WITH_LIST5("PARTITIONED BY (", "ROW FORMAT DELIMITED", "STORED AS", "LOCATION", "TABLE PROPERTIES ('numRows'='"); + } + + /* CREATE FOREIGN */ + else if (Matches2("CREATE", "FOREIGN")) + COMPLETE_WITH_LIST2("DATA WRAPPER", "TABLE"); + + /* CREATE FOREIGN DATA WRAPPER */ + else if (Matches5("CREATE", "FOREIGN", "DATA", "WRAPPER", MatchAny)) + COMPLETE_WITH_LIST3("HANDLER", "VALIDATOR", "OPTIONS"); + + /* CREATE INDEX --- is allowed inside CREATE SCHEMA, so use TailMatches */ + /* First off we complete CREATE UNIQUE with "INDEX" */ + else if (TailMatches2("CREATE", "UNIQUE")) + COMPLETE_WITH_CONST("INDEX"); + + /* + * If we have CREATE|UNIQUE INDEX, then add "ON", "CONCURRENTLY", and + * existing indexes + */ + else if (TailMatches2("CREATE|UNIQUE", "INDEX")) + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_indexes, + " UNION SELECT 'ON'" + " UNION SELECT 'CONCURRENTLY'"); + /* Complete ... INDEX|CONCURRENTLY [] ON with a list of tables */ + else if (TailMatches3("INDEX|CONCURRENTLY", MatchAny, "ON") || + TailMatches2("INDEX|CONCURRENTLY", "ON")) + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tm, NULL); + + /* + * Complete CREATE|UNIQUE INDEX CONCURRENTLY with "ON" and existing + * indexes + */ + else if (TailMatches3("CREATE|UNIQUE", "INDEX", "CONCURRENTLY")) + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_indexes, + " UNION SELECT 'ON'"); + /* Complete CREATE|UNIQUE INDEX [CONCURRENTLY] with "ON" */ + else if (TailMatches3("CREATE|UNIQUE", "INDEX", MatchAny) || + TailMatches4("CREATE|UNIQUE", "INDEX", "CONCURRENTLY", MatchAny)) + COMPLETE_WITH_CONST("ON"); + + /* + * Complete INDEX ON
with a list of table columns (which + * should really be in parens) + */ + else if (TailMatches4("INDEX", MatchAny, "ON", MatchAny) || + TailMatches3("INDEX|CONCURRENTLY", "ON", MatchAny)) + COMPLETE_WITH_LIST2("(", "USING"); + else if (TailMatches5("INDEX", MatchAny, "ON", MatchAny, "(") || + TailMatches4("INDEX|CONCURRENTLY", "ON", MatchAny, "(")) + COMPLETE_WITH_ATTR(prev2_wd, ""); + /* same if you put in USING */ + else if (TailMatches5("ON", MatchAny, "USING", MatchAny, "(")) + COMPLETE_WITH_ATTR(prev4_wd, ""); + /* Complete USING with an index method */ + else if (TailMatches6("INDEX", MatchAny, MatchAny, "ON", MatchAny, "USING") || + TailMatches5("INDEX", MatchAny, "ON", MatchAny, "USING") || + TailMatches4("INDEX", "ON", MatchAny, "USING")) + COMPLETE_WITH_QUERY(Query_for_list_of_access_methods); + else if (TailMatches4("ON", MatchAny, "USING", MatchAny) && + !TailMatches6("POLICY", MatchAny, MatchAny, MatchAny, MatchAny, MatchAny) && + !TailMatches4("FOR", MatchAny, MatchAny, MatchAny)) + COMPLETE_WITH_CONST("("); + +/* CREATE LIBRARY */ + else if (Matches2("CREATE", "LIBRARY")) + COMPLETE_WITH_QUERY(Query_for_list_of_libraries); + else if (Matches4("CREATE", "OR", "REPLACE", "LIBRARY")) + COMPLETE_WITH_QUERY(Query_for_list_of_libraries); + else if (Matches5("CREATE", "OR", "REPLACE", "LIBRARY", MatchAny)) + COMPLETE_WITH_CONST("LANGUAGE plpythonu FROM"); + else if (Matches1("CREATE") && TailMatches5("LIBRARY", "LANGUAGE", "plpythonu", "FROM", MatchAny)) + COMPLETE_WITH_CONST("AUTHORIZATION"); + else if (Matches1("CREATE") && TailMatches6("LIBRARY", "LANGUAGE", "plpythonu", "FROM", MatchAny, "AUTHORIZATION")) + COMPLETE_WITH_CONST("REGION"); + else if (Matches1("CREATE") && TailMatches7("LIBRARY", "LANGUAGE", "plpythonu", "FROM", MatchAny, "AUTHORIZATION", "REGION")) + COMPLETE_WITH_QUERY(Query_for_list_of_aws_regions); + else if (Matches1("CREATE") && TailMatches8("LIBRARY", "LANGUAGE", "plpythonu", "FROM", MatchAny, "AUTHORIZATION", "REGION", "AS")) + COMPLETE_WITH_QUERY(Query_for_list_of_aws_regions); + + // XXX See if you can add Regions here!? + + /* CREATE POLICY */ + /* Complete "CREATE POLICY ON" */ + else if (Matches3("CREATE", "POLICY", MatchAny)) + COMPLETE_WITH_CONST("ON"); + /* Complete "CREATE POLICY ON
" */ + else if (Matches4("CREATE", "POLICY", MatchAny, "ON")) + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tables, NULL); + /* Complete "CREATE POLICY ON
AS|FOR|TO|USING|WITH CHECK" */ + else if (Matches5("CREATE", "POLICY", MatchAny, "ON", MatchAny)) + COMPLETE_WITH_LIST5("AS", "FOR", "TO", "USING (", "WITH CHECK ("); + /* CREATE POLICY ON
AS PERMISSIVE|RESTRICTIVE */ + else if (Matches6("CREATE", "POLICY", MatchAny, "ON", MatchAny, "AS")) + COMPLETE_WITH_LIST2("PERMISSIVE", "RESTRICTIVE"); + + /* + * CREATE POLICY ON
AS PERMISSIVE|RESTRICTIVE + * FOR|TO|USING|WITH CHECK + */ + else if (Matches7("CREATE", "POLICY", MatchAny, "ON", MatchAny, "AS", MatchAny)) + COMPLETE_WITH_LIST4("FOR", "TO", "USING", "WITH CHECK"); + /* CREATE POLICY ON
FOR ALL|SELECT|INSERT|UPDATE|DELETE */ + else if (Matches6("CREATE", "POLICY", MatchAny, "ON", MatchAny, "FOR")) + COMPLETE_WITH_LIST5("ALL", "SELECT", "INSERT", "UPDATE", "DELETE"); + /* Complete "CREATE POLICY ON
FOR INSERT TO|WITH CHECK" */ + else if (Matches7("CREATE", "POLICY", MatchAny, "ON", MatchAny, "FOR", "INSERT")) + COMPLETE_WITH_LIST2("TO", "WITH CHECK ("); + /* Complete "CREATE POLICY ON
FOR SELECT|DELETE TO|USING" */ + else if (Matches7("CREATE", "POLICY", MatchAny, "ON", MatchAny, "FOR", "SELECT|DELETE")) + COMPLETE_WITH_LIST2("TO", "USING ("); + /* CREATE POLICY ON
FOR ALL|UPDATE TO|USING|WITH CHECK */ + else if (Matches7("CREATE", "POLICY", MatchAny, "ON", MatchAny, "FOR", "ALL|UPDATE")) + COMPLETE_WITH_LIST3("TO", "USING (", "WITH CHECK ("); + /* Complete "CREATE POLICY ON
TO " */ + else if (Matches6("CREATE", "POLICY", MatchAny, "ON", MatchAny, "TO")) + COMPLETE_WITH_QUERY(Query_for_list_of_grant_roles); + /* Complete "CREATE POLICY ON
USING (" */ + else if (Matches6("CREATE", "POLICY", MatchAny, "ON", MatchAny, "USING")) + COMPLETE_WITH_CONST("("); + + /* + * CREATE POLICY ON
AS PERMISSIVE|RESTRICTIVE FOR + * ALL|SELECT|INSERT|UPDATE|DELETE + */ + else if (Matches8("CREATE", "POLICY", MatchAny, "ON", MatchAny, "AS", MatchAny, "FOR")) + COMPLETE_WITH_LIST5("ALL", "SELECT", "INSERT", "UPDATE", "DELETE"); + + /* + * Complete "CREATE POLICY ON
AS PERMISSIVE|RESTRICTIVE FOR + * INSERT TO|WITH CHECK" + */ + else if (Matches9("CREATE", "POLICY", MatchAny, "ON", MatchAny, "AS", MatchAny, "FOR", "INSERT")) + COMPLETE_WITH_LIST2("TO", "WITH CHECK ("); + + /* + * Complete "CREATE POLICY ON
AS PERMISSIVE|RESTRICTIVE FOR + * SELECT|DELETE TO|USING" + */ + else if (Matches9("CREATE", "POLICY", MatchAny, "ON", MatchAny, "AS", MatchAny, "FOR", "SELECT|DELETE")) + COMPLETE_WITH_LIST2("TO", "USING ("); + + /* + * CREATE POLICY ON
AS PERMISSIVE|RESTRICTIVE FOR + * ALL|UPDATE TO|USING|WITH CHECK + */ + else if (Matches9("CREATE", "POLICY", MatchAny, "ON", MatchAny, "AS", MatchAny, "FOR", "ALL|UPDATE")) + COMPLETE_WITH_LIST3("TO", "USING (", "WITH CHECK ("); + + /* + * Complete "CREATE POLICY ON
AS PERMISSIVE|RESTRICTIVE TO + * " + */ + else if (Matches8("CREATE", "POLICY", MatchAny, "ON", MatchAny, "AS", MatchAny, "TO")) + COMPLETE_WITH_QUERY(Query_for_list_of_grant_roles); + + /* + * Complete "CREATE POLICY ON
AS PERMISSIVE|RESTRICTIVE + * USING (" + */ + else if (Matches8("CREATE", "POLICY", MatchAny, "ON", MatchAny, "AS", MatchAny, "USING")) + COMPLETE_WITH_CONST("("); + + +/* CREATE PUBLICATION */ + else if (Matches3("CREATE", "PUBLICATION", MatchAny)) + COMPLETE_WITH_LIST3("FOR TABLE", "FOR ALL TABLES", "WITH ("); + else if (Matches4("CREATE", "PUBLICATION", MatchAny, "FOR")) + COMPLETE_WITH_LIST2("TABLE", "ALL TABLES"); + /* Complete "CREATE PUBLICATION FOR TABLE
" */ + else if (Matches4("CREATE", "PUBLICATION", MatchAny, "FOR TABLE")) + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tables, NULL); + /* Complete "CREATE PUBLICATION [...] WITH" */ + else if (HeadMatches2("CREATE", "PUBLICATION") && TailMatches2("WITH", "(")) + COMPLETE_WITH_CONST("publish"); + +/* CREATE RULE */ + /* Complete "CREATE RULE " with "AS ON" */ + else if (Matches3("CREATE", "RULE", MatchAny)) + COMPLETE_WITH_CONST("AS ON"); + /* Complete "CREATE RULE AS" with "ON" */ + else if (Matches4("CREATE", "RULE", MatchAny, "AS")) + COMPLETE_WITH_CONST("ON"); + /* Complete "CREATE RULE AS ON" with SELECT|UPDATE|INSERT|DELETE */ + else if (Matches5("CREATE", "RULE", MatchAny, "AS", "ON")) + COMPLETE_WITH_LIST4("SELECT", "UPDATE", "INSERT", "DELETE"); + /* Complete "AS ON SELECT|UPDATE|INSERT|DELETE" with a "TO" */ + else if (TailMatches3("AS", "ON", "SELECT|UPDATE|INSERT|DELETE")) + COMPLETE_WITH_CONST("TO"); + /* Complete "AS ON TO" with a table name */ + else if (TailMatches4("AS", "ON", "SELECT|UPDATE|INSERT|DELETE", "TO")) + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tables, NULL); + +/* CREATE SCHEMA */ + else if (Matches2("CREATE", "SCHEMA")) + COMPLETE_WITH_LIST2("AUTHORIZATION", "IF NOT EXISTS"); + else if (Matches3("CREATE", "SCHEMA", MatchAny)) + COMPLETE_WITH_CONST("AUTHORIZATION"); + else if (Matches4("CREATE", "SCHEMA", "IF NOT EXISTS", MatchAny)) + COMPLETE_WITH_CONST("AUTHORIZATION"); + + +/* CREATE SEQUENCE --- is allowed inside CREATE SCHEMA, so use TailMatches */ + else if (TailMatches3("CREATE", "SEQUENCE", MatchAny) || + TailMatches4("CREATE", "TEMP|TEMPORARY", "SEQUENCE", MatchAny)) + COMPLETE_WITH_LIST8("INCREMENT BY", "MINVALUE", "MAXVALUE", "NO", "CACHE", + "CYCLE", "OWNED BY", "START WITH"); + else if (TailMatches4("CREATE", "SEQUENCE", MatchAny, "NO") || + TailMatches5("CREATE", "TEMP|TEMPORARY", "SEQUENCE", MatchAny, "NO")) + COMPLETE_WITH_LIST3("MINVALUE", "MAXVALUE", "CYCLE"); + +/* CREATE SERVER */ + else if (Matches3("CREATE", "SERVER", MatchAny)) + COMPLETE_WITH_LIST3("TYPE", "VERSION", "FOREIGN DATA WRAPPER"); + +/* CREATE STATISTICS */ + else if (Matches3("CREATE", "STATISTICS", MatchAny)) + COMPLETE_WITH_LIST2("(", "ON"); + else if (Matches4("CREATE", "STATISTICS", MatchAny, "(")) + COMPLETE_WITH_LIST2("ndistinct", "dependencies"); + else if (HeadMatches3("CREATE", "STATISTICS", MatchAny) && + previous_words[0][0] == '(' && + previous_words[0][strlen(previous_words[0]) - 1] == ')') + COMPLETE_WITH_CONST("ON"); + else if (HeadMatches3("CREATE", "STATISTICS", MatchAny) && + TailMatches1("FROM")) + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tables, NULL); + + /* Complete "CREATE TEMP/TEMPORARY" with the possible temp objects */ + else if (HeadMatches2("CREATE", "TEMP|TEMPORARY")) + COMPLETE_WITH_LIST3("SEQUENCE", "TABLE", "VIEW"); + + /* Complete "CREATE UNLOGGED" with TABLE or MATVIEW */ + else if (TailMatches2("CREATE", "UNLOGGED")) + COMPLETE_WITH_LIST2("TABLE", "MATERIALIZED VIEW"); + + /* CREATE TABLE is allowed inside CREATE SCHEMA in Native Postgres so although + we ought to use TailMatches, using HeadMatches since this allows supporting + far more options */ + else if (Matches2("CREATE", "TABLE") || + Matches3("CREATE", "TEMP|TEMPORARY", "TABLE") || + Matches4("CREATE", "LOCAL", "TEMP|TEMPORARY", "TABLE")) + COMPLETE_WITH_LIST2("IF NOT EXISTS", "AS"); + + else if (Matches3("CREATE", "TABLE", MatchAny)) + COMPLETE_WITH_CONST("("); + + else if (HeadMatches2("CREATE", "TABLE") /*|| + HeadMatches3("CREATE", "TEMP|TEMPORARY", "TABLE") || + HeadMatches4("CREATE", "LOCAL", "TEMP|TEMPORARY", "TABLE")*/) + { + //if (TailMatches3(MatchAnyExcept("("), MatchAny, ")")) + if (TailMatches1("BACKUP")) + COMPLETE_WITH_LIST2("YES", "NO"); + else if (TailMatches2("BACKUP", "YES|NO")) + COMPLETE_WITH_LIST5("DISTSTYLE", "DISTKEY (", "COMPOUND", "INTERLEAVED", "SORTKEY ("); + else if (TailMatches1("DISTSTYLE")) + COMPLETE_WITH_LIST3("EVEN", "KEY", "ALL"); + else if (TailMatches2("DISTSTYLE", "EVEN|KEY|ALL")) + COMPLETE_WITH_LIST3("COMPOUND", "INTERLEAVED", "SORTKEY ("); + else if (TailMatches2("DISTKEY", MatchAny)) + COMPLETE_WITH_LIST3("COMPOUND", "INTERLEAVED", "SORTKEY ("); + else if (TailMatches1("COMPOUND|INTERLEAVED")) + COMPLETE_WITH_CONST("SORTKEY ("); + else + COMPLETE_WITH_LIST6("BACKUP", "DISTSTYLE", "DISTKEY (", "COMPOUND", "INTERLEAVED", "SORTKEY ("); + } + + /* Complete PARTITION BY with RANGE ( or LIST ( or ... */ + else if (TailMatches2("PARTITION", "BY")) + COMPLETE_WITH_LIST2("RANGE (", "LIST ("); + /* If we have xxx PARTITION OF, provide a list of partitioned tables */ + else if (TailMatches2("PARTITION", "OF")) + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_partitioned_tables, ""); + /* Limited completion support for partition bound specification */ + else if (TailMatches3("PARTITION", "OF", MatchAny)) + COMPLETE_WITH_LIST2("FOR VALUES", "DEFAULT"); + +/* CREATE TABLESPACE */ + else if (Matches3("CREATE", "TABLESPACE", MatchAny)) + COMPLETE_WITH_LIST2("OWNER", "LOCATION"); + /* Complete CREATE TABLESPACE name OWNER name with "LOCATION" */ + else if (Matches5("CREATE", "TABLESPACE", MatchAny, "OWNER", MatchAny)) + COMPLETE_WITH_CONST("LOCATION"); + +/* CREATE TEXT SEARCH */ + else if (Matches3("CREATE", "TEXT", "SEARCH")) + COMPLETE_WITH_LIST4("CONFIGURATION", "DICTIONARY", "PARSER", "TEMPLATE"); + else if (Matches5("CREATE", "TEXT", "SEARCH", "CONFIGURATION", MatchAny)) + COMPLETE_WITH_CONST("("); + +/* CREATE SUBSCRIPTION */ + else if (Matches3("CREATE", "SUBSCRIPTION", MatchAny)) + COMPLETE_WITH_CONST("CONNECTION"); + else if (Matches5("CREATE", "SUBSCRIPTION", MatchAny, "CONNECTION", MatchAny)) + COMPLETE_WITH_CONST("PUBLICATION"); + else if (Matches6("CREATE", "SUBSCRIPTION", MatchAny, "CONNECTION", + MatchAny, "PUBLICATION")) + { + /* complete with nothing here as this refers to remote publications */ + } + else if (HeadMatches2("CREATE", "SUBSCRIPTION") && TailMatches2("PUBLICATION", MatchAny)) + COMPLETE_WITH_CONST("WITH ("); + /* Complete "CREATE SUBSCRIPTION ... WITH ( " */ + else if (HeadMatches2("CREATE", "SUBSCRIPTION") && TailMatches2("WITH", "(")) + COMPLETE_WITH_LIST6("copy_data", "connect", "create_slot", "enabled", + "slot_name", "synchronous_commit"); + +/* CREATE TRIGGER --- is allowed inside CREATE SCHEMA, so use TailMatches */ + /* complete CREATE TRIGGER with BEFORE,AFTER,INSTEAD OF */ + else if (TailMatches3("CREATE", "TRIGGER", MatchAny)) + COMPLETE_WITH_LIST3("BEFORE", "AFTER", "INSTEAD OF"); + /* complete CREATE TRIGGER BEFORE,AFTER with an event */ + else if (TailMatches4("CREATE", "TRIGGER", MatchAny, "BEFORE|AFTER")) + COMPLETE_WITH_LIST4("INSERT", "DELETE", "UPDATE", "TRUNCATE"); + /* complete CREATE TRIGGER INSTEAD OF with an event */ + else if (TailMatches5("CREATE", "TRIGGER", MatchAny, "INSTEAD", "OF")) + COMPLETE_WITH_LIST3("INSERT", "DELETE", "UPDATE"); + /* complete CREATE TRIGGER BEFORE,AFTER sth with OR,ON */ + else if (TailMatches5("CREATE", "TRIGGER", MatchAny, "BEFORE|AFTER", MatchAny) || + TailMatches6("CREATE", "TRIGGER", MatchAny, "INSTEAD", "OF", MatchAny)) + COMPLETE_WITH_LIST2("ON", "OR"); + + /* + * complete CREATE TRIGGER BEFORE,AFTER event ON with a list of + * tables + */ + else if (TailMatches6("CREATE", "TRIGGER", MatchAny, "BEFORE|AFTER", MatchAny, "ON")) + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tables, NULL); + /* complete CREATE TRIGGER ... INSTEAD OF event ON with a list of views */ + else if (TailMatches7("CREATE", "TRIGGER", MatchAny, "INSTEAD", "OF", MatchAny, "ON")) + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_views, NULL); + else if (HeadMatches2("CREATE", "TRIGGER") && TailMatches2("ON", MatchAny)) + COMPLETE_WITH_LIST7("NOT DEFERRABLE", "DEFERRABLE", "INITIALLY", + "REFERENCING", "FOR", "WHEN (", "EXECUTE PROCEDURE"); + else if (HeadMatches2("CREATE", "TRIGGER") && + (TailMatches1("DEFERRABLE") || TailMatches2("INITIALLY", "IMMEDIATE|DEFERRED"))) + COMPLETE_WITH_LIST4("REFERENCING", "FOR", "WHEN (", "EXECUTE PROCEDURE"); + else if (HeadMatches2("CREATE", "TRIGGER") && TailMatches1("REFERENCING")) + COMPLETE_WITH_LIST2("OLD TABLE", "NEW TABLE"); + else if (HeadMatches2("CREATE", "TRIGGER") && TailMatches2("OLD|NEW", "TABLE")) + COMPLETE_WITH_CONST("AS"); + else if (HeadMatches2("CREATE", "TRIGGER") && + (TailMatches5("REFERENCING", "OLD", "TABLE", "AS", MatchAny) || + TailMatches4("REFERENCING", "OLD", "TABLE", MatchAny))) + COMPLETE_WITH_LIST4("NEW TABLE", "FOR", "WHEN (", "EXECUTE PROCEDURE"); + else if (HeadMatches2("CREATE", "TRIGGER") && + (TailMatches5("REFERENCING", "NEW", "TABLE", "AS", MatchAny) || + TailMatches4("REFERENCING", "NEW", "TABLE", MatchAny))) + COMPLETE_WITH_LIST4("OLD TABLE", "FOR", "WHEN (", "EXECUTE PROCEDURE"); + else if (HeadMatches2("CREATE", "TRIGGER") && + (TailMatches9("REFERENCING", "OLD|NEW", "TABLE", "AS", MatchAny, "OLD|NEW", "TABLE", "AS", MatchAny) || + TailMatches8("REFERENCING", "OLD|NEW", "TABLE", MatchAny, "OLD|NEW", "TABLE", "AS", MatchAny) || + TailMatches8("REFERENCING", "OLD|NEW", "TABLE", "AS", MatchAny, "OLD|NEW", "TABLE", MatchAny) || + TailMatches7("REFERENCING", "OLD|NEW", "TABLE", MatchAny, "OLD|NEW", "TABLE", MatchAny))) + COMPLETE_WITH_LIST3("FOR", "WHEN (", "EXECUTE PROCEDURE"); + else if (HeadMatches2("CREATE", "TRIGGER") && TailMatches1("FOR")) + COMPLETE_WITH_LIST3("EACH", "ROW", "STATEMENT"); + else if (HeadMatches2("CREATE", "TRIGGER") && TailMatches2("FOR", "EACH")) + COMPLETE_WITH_LIST2("ROW", "STATEMENT"); + else if (HeadMatches2("CREATE", "TRIGGER") && + (TailMatches3("FOR", "EACH", "ROW|STATEMENT") || + TailMatches2("FOR", "ROW|STATEMENT"))) + COMPLETE_WITH_LIST2("WHEN (", "EXECUTE PROCEDURE"); + /* complete CREATE TRIGGER ... EXECUTE with PROCEDURE */ + else if (HeadMatches2("CREATE", "TRIGGER") && TailMatches1("EXECUTE")) + COMPLETE_WITH_CONST("PROCEDURE"); + else if (HeadMatches2("CREATE", "TRIGGER") && TailMatches2("EXECUTE", "PROCEDURE")) + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_functions, NULL); + +/* CREATE ROLE,USER,GROUP */ + else if (Matches3("CREATE", "ROLE|GROUP|USER", MatchAny) && + !TailMatches2("USER", "MAPPING")) + { + static const char *const list_CREATEROLE[] = + {"ADMIN", "BYPASSRLS", "CONNECTION LIMIT", "CREATEDB", "CREATEROLE", "CREATEUSER", + "ENCRYPTED PASSWORD", "IN", "INHERIT", "LOGIN", "NOBYPASSRLS", + "NOCREATEDB", "NOCREATEROLE", "NOCREATEUSER", "NOINHERIT", + "NOLOGIN", "NOREPLICATION", "NOSUPERUSER", "PASSWORD", + "REPLICATION", "ROLE", "SUPERUSER", "SYSID", + "VALID UNTIL", "WITH", NULL}; + + COMPLETE_WITH_LIST(list_CREATEROLE); + } + +/* CREATE ROLE,USER,GROUP WITH */ + else if (Matches4("CREATE", "ROLE|GROUP|USER", MatchAny, "WITH")) + { + /* Similar to the above, but don't complete "WITH" again. */ + static const char *const list_CREATEROLE_WITH[] = + {"ADMIN", "BYPASSRLS", "CONNECTION LIMIT", "CREATEDB", "CREATEROLE", + "ENCRYPTED PASSWORD", "IN", "INHERIT", "LOGIN", "NOBYPASSRLS", + "NOCREATEDB", "NOCREATEROLE", "NOINHERIT", + "NOLOGIN", "NOREPLICATION", "NOSUPERUSER", "PASSWORD", + "REPLICATION", "ROLE", "SUPERUSER", "SYSID", + "VALID UNTIL", NULL}; + + COMPLETE_WITH_LIST(list_CREATEROLE_WITH); + } + + /* complete CREATE ROLE,USER,GROUP IN with ROLE,GROUP */ + else if (Matches4("CREATE", "ROLE|USER|GROUP", MatchAny, "IN")) + COMPLETE_WITH_LIST2("GROUP", "ROLE"); + +/* CREATE VIEW --- is allowed inside CREATE SCHEMA, so use TailMatches */ + /* Complete CREATE VIEW with AS */ + else if (TailMatches3("CREATE", "VIEW", MatchAny)) + COMPLETE_WITH_CONST("AS"); + /* Complete "CREATE VIEW AS with "SELECT" */ + else if (TailMatches4("CREATE", "VIEW", MatchAny, "AS")) + COMPLETE_WITH_CONST("SELECT"); + +/* CREATE MATERIALIZED VIEW */ + else if (Matches2("CREATE", "MATERIALIZED")) + COMPLETE_WITH_CONST("VIEW"); + /* Complete CREATE MATERIALIZED VIEW with AS */ + else if (Matches4("CREATE", "MATERIALIZED", "VIEW", MatchAny)) + COMPLETE_WITH_CONST("AS"); + /* Complete "CREATE MATERIALIZED VIEW AS with "SELECT" */ + else if (Matches5("CREATE", "MATERIALIZED", "VIEW", MatchAny, "AS")) + COMPLETE_WITH_CONST("SELECT"); + +/* CREATE EVENT TRIGGER */ + else if (Matches2("CREATE", "EVENT")) + COMPLETE_WITH_CONST("TRIGGER"); + /* Complete CREATE EVENT TRIGGER with ON */ + else if (Matches4("CREATE", "EVENT", "TRIGGER", MatchAny)) + COMPLETE_WITH_CONST("ON"); + /* Complete CREATE EVENT TRIGGER ON with event_type */ + else if (Matches5("CREATE", "EVENT", "TRIGGER", MatchAny, "ON")) + COMPLETE_WITH_LIST3("ddl_command_start", "ddl_command_end", "sql_drop"); + +/* CREATE CONTINUOUS TRANSFORM / VIEW */ + else if ((IS_PIPELINEDB) && (Matches2("CREATE|DROP", "CONTINUOUS"))) + COMPLETE_WITH_LIST2("TRANSFORM", "VIEW"); + +/* DEALLOCATE */ + else if (Matches1("DEALLOCATE")) + COMPLETE_WITH_QUERY(Query_for_list_of_prepared_statements " UNION SELECT 'PREPARE'"); + else if (Matches2("DEALLOCATE", "PREPARE")) + COMPLETE_WITH_QUERY(Query_for_list_of_prepared_statements); + +/* DECLARE */ + else if (Matches2("DECLARE", MatchAny)) + COMPLETE_WITH_LIST5("BINARY", "INSENSITIVE", "SCROLL", "NO SCROLL", + "CURSOR"); + else if (HeadMatches1("DECLARE") && TailMatches1("CURSOR")) + COMPLETE_WITH_LIST3("WITH HOLD", "WITHOUT HOLD", "FOR"); + +/* DELETE --- can be inside EXPLAIN, RULE, etc */ + /* ... despite which, only complete DELETE with FROM at start of line */ + else if (Matches1("DELETE")) + COMPLETE_WITH_CONST("FROM"); + /* Complete DELETE FROM with a list of tables */ + else if (TailMatches2("DELETE", "FROM")) + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_updatables, NULL); + /* Complete DELETE FROM
*/ + else if (TailMatches3("DELETE", "FROM", MatchAny)) + COMPLETE_WITH_LIST2("USING", "WHERE"); + else if (TailMatches4("DELETE", "FROM", MatchAny, "USING")) + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tables, NULL); + else if (TailMatches5("DELETE", "FROM", MatchAny, "USING", MatchAny)) + COMPLETE_WITH_CONST("WHERE"); + else if ((TailMatches5("DELETE", MatchAny, "USING", MatchAny, "WHERE")) || + (TailMatches6("DELETE", "FROM", MatchAny, "USING", MatchAny, "WHERE"))) + COMPLETE_WITH_ATTR(prev2_wd, ""); + /* XXX: implement tab completion for DELETE ... USING */ + +/* DISCARD */ + else if (Matches1("DISCARD")) + COMPLETE_WITH_LIST4("ALL", "PLANS", "SEQUENCES", "TEMP"); + +/* DO */ + else if (Matches1("DO")) + COMPLETE_WITH_CONST("LANGUAGE"); + +/* DROP */ + /* Complete DROP object with CASCADE / RESTRICT */ + else if (Matches3("DROP", + "COLLATION|CONVERSION|DOMAIN|EXTENSION|LANGUAGE|PUBLICATION|SCHEMA|SEQUENCE|SERVER|SUBSCRIPTION|STATISTICS|TABLE|TYPE|VIEW", + MatchAny) || + Matches4("DROP", "ACCESS", "METHOD", MatchAny) || + (Matches4("DROP", "AGGREGATE|FUNCTION", MatchAny, MatchAny) && + ends_with(prev_wd, ')')) || + Matches4("DROP", "EVENT", "TRIGGER", MatchAny) || + Matches5("DROP", "FOREIGN", "DATA", "WRAPPER", MatchAny) || + Matches4("DROP", "FOREIGN", "TABLE", MatchAny) || + Matches5("DROP", "TEXT", "SEARCH", "CONFIGURATION|DICTIONARY|PARSER|TEMPLATE", MatchAny)) + COMPLETE_WITH_LIST2("CASCADE", "RESTRICT"); + + /* help completing some of the variants */ + else if (Matches3("DROP", "AGGREGATE|FUNCTION", MatchAny)) + COMPLETE_WITH_CONST("("); + else if (Matches4("DROP", "AGGREGATE|FUNCTION", MatchAny, "(")) + COMPLETE_WITH_FUNCTION_ARG(prev2_wd); + else if (Matches2("DROP", "FOREIGN")) + COMPLETE_WITH_LIST2("DATA WRAPPER", "TABLE"); + + /* DROP INDEX */ + else if (Matches2("DROP", "INDEX")) + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_indexes, + " UNION SELECT 'CONCURRENTLY'"); + else if (Matches3("DROP", "INDEX", "CONCURRENTLY")) + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_indexes, NULL); + else if (Matches3("DROP", "INDEX", MatchAny)) + COMPLETE_WITH_LIST2("CASCADE", "RESTRICT"); + else if (Matches4("DROP", "INDEX", "CONCURRENTLY", MatchAny)) + COMPLETE_WITH_LIST2("CASCADE", "RESTRICT"); + + /* DROP LIBRARY */ + else if (Matches2("DROP", "LIBRARY")) + COMPLETE_WITH_QUERY(Query_for_list_of_libraries); + + /* DROP MATERIALIZED VIEW */ + else if (Matches2("DROP", "MATERIALIZED")) + COMPLETE_WITH_CONST("VIEW"); + else if (Matches3("DROP", "MATERIALIZED", "VIEW")) + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_matviews, NULL); + + /* DROP OWNED BY */ + else if (Matches2("DROP", "OWNED")) + COMPLETE_WITH_CONST("BY"); + else if (Matches3("DROP", "OWNED", "BY")) + COMPLETE_WITH_QUERY(Query_for_list_of_roles); + + /* DROP SCHEMA */ + else if (Matches2("DROP", "SCHEMA")) + COMPLETE_WITH_QUERY(Query_for_list_of_schemas + " UNION ALL SELECT 'IF EXISTS'"); + else if ((Matches3("DROP", "SCHEMA", MatchAny))) + COMPLETE_WITH_LIST2("CASCADE", "RESTRICT"); + else if (Matches4("DROP", "SCHEMA", MatchAny, MatchAny) + && TailMatches1(MatchAnyExcept("CASCADE|RESTRICT"))) + COMPLETE_WITH_QUERY(Query_for_list_of_schemas); + else if ((HeadMatches5("DROP", "SCHEMA", MatchAny, MatchAny, MatchAny)) + && TailMatches1(MatchAnyExcept("EXISTS|CASCADE|RESTRICT"))) + COMPLETE_WITH_LIST2("CASCADE", "RESTRICT"); + + /* DROP TEXT SEARCH */ + else if (Matches3("DROP", "TEXT", "SEARCH")) + COMPLETE_WITH_LIST4("CONFIGURATION", "DICTIONARY", "PARSER", "TEMPLATE"); + + /* DROP TRIGGER */ + else if (Matches3("DROP", "TRIGGER", MatchAny)) + COMPLETE_WITH_CONST("ON"); + else if (Matches4("DROP", "TRIGGER", MatchAny, "ON")) + { + completion_info_charp = prev2_wd; + COMPLETE_WITH_QUERY(Query_for_list_of_tables_for_trigger); + } + else if (Matches5("DROP", "TRIGGER", MatchAny, "ON", MatchAny)) + COMPLETE_WITH_LIST2("CASCADE", "RESTRICT"); + + /* DROP ACCESS METHOD */ + else if (Matches2("DROP", "ACCESS")) + COMPLETE_WITH_CONST("METHOD"); + else if (Matches3("DROP", "ACCESS", "METHOD")) + COMPLETE_WITH_QUERY(Query_for_list_of_access_methods); + + /* DROP EVENT TRIGGER */ + else if (Matches2("DROP", "EVENT")) + COMPLETE_WITH_CONST("TRIGGER"); + else if (Matches3("DROP", "EVENT", "TRIGGER")) + COMPLETE_WITH_QUERY(Query_for_list_of_event_triggers); + + /* DROP POLICY */ + else if (Matches2("DROP", "POLICY")) + COMPLETE_WITH_QUERY(Query_for_list_of_policies); + /* DROP POLICY ON */ + else if (Matches3("DROP", "POLICY", MatchAny)) + COMPLETE_WITH_CONST("ON"); + /* DROP POLICY ON
*/ + else if (Matches4("DROP", "POLICY", MatchAny, "ON")) + { + completion_info_charp = prev2_wd; + COMPLETE_WITH_QUERY(Query_for_list_of_tables_for_policy); + } + + /* DROP RULE */ + else if (Matches3("DROP", "RULE", MatchAny)) + COMPLETE_WITH_CONST("ON"); + else if (Matches4("DROP", "RULE", MatchAny, "ON")) + { + completion_info_charp = prev2_wd; + COMPLETE_WITH_QUERY(Query_for_list_of_tables_for_rule); + } + else if (Matches5("DROP", "RULE", MatchAny, "ON", MatchAny)) + COMPLETE_WITH_LIST2("CASCADE", "RESTRICT"); + + /* DROP TABLE */ + else if (Matches2("DROP", "TABLE")) + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tables, + " UNION ALL SELECT 'IF EXISTS'"); + else if ((Matches3("DROP", "TABLE", MatchAny))) + COMPLETE_WITH_LIST2("CASCADE", "RESTRICT"); + else if (Matches4("DROP", "TABLE", MatchAny, MatchAny) + && TailMatches1(MatchAnyExcept("CASCADE|RESTRICT"))) + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tables, NULL); + else if ((HeadMatches5("DROP", "TABLE", MatchAny, MatchAny, MatchAny)) + && TailMatches1(MatchAnyExcept("EXISTS|CASCADE|RESTRICT"))) + COMPLETE_WITH_LIST2("CASCADE", "RESTRICT"); + + /* DROP USER */ + else if (HeadMatches2("DROP", "USER")) + COMPLETE_WITH_QUERY(Query_for_list_of_users + " UNION ALL SELECT 'IF EXISTS'"); + else if (HeadMatches4("DROP", "USER", "IF", "EXISTS")) + COMPLETE_WITH_QUERY(Query_for_list_of_users); + + /* DROP VIEW */ + else if (Matches2("DROP", "VIEW")) + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_views, + " UNION ALL SELECT 'IF EXISTS'"); + else if ((Matches3("DROP", "VIEW", MatchAny))) + COMPLETE_WITH_LIST2("CASCADE", "RESTRICT"); + else if (Matches4("DROP", "VIEW", MatchAny, MatchAny) + && TailMatches1(MatchAnyExcept("CASCADE|RESTRICT"))) + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_views, NULL); + else if ((HeadMatches5("DROP", "VIEW", MatchAny, MatchAny, MatchAny)) + && TailMatches1(MatchAnyExcept("EXISTS|CASCADE|RESTRICT"))) + COMPLETE_WITH_LIST2("CASCADE", "RESTRICT"); + + +/* EXECUTE */ + else if (Matches1("EXECUTE")) + COMPLETE_WITH_QUERY(Query_for_list_of_prepared_statements); + +/* EXPLAIN */ + + /* + * Complete EXPLAIN [ANALYZE] [VERBOSE] with list of EXPLAIN-able commands + */ + else if (Matches1("EXPLAIN")) + COMPLETE_WITH_LIST7("SELECT", "INSERT", "DELETE", "UPDATE", "DECLARE", + "ANALYZE", "VERBOSE"); + else if (Matches2("EXPLAIN", "ANALYZE")) + COMPLETE_WITH_LIST6("SELECT", "INSERT", "DELETE", "UPDATE", "DECLARE", + "VERBOSE"); + else if (Matches2("EXPLAIN", "VERBOSE") || + Matches3("EXPLAIN", "ANALYZE", "VERBOSE")) + COMPLETE_WITH_LIST5("SELECT", "INSERT", "DELETE", "UPDATE", "DECLARE"); + +/* FETCH */ + /* Experiment with removing Postgres compliant and keeping only Redshift */ + else if (Matches1("FETCH")) + COMPLETE_WITH_LIST4("ALL", "FORWARD", "FROM", "NEXT"); + else if (Matches2("FETCH", "FORWARD")) + COMPLETE_WITH_LIST2("ALL", "FROM"); + else if (Matches2("FETCH", "NEXT|ALL")) + COMPLETE_WITH_CONST("FROM"); + else if (Matches3("FETCH", "FORWARD", MatchAnyExcept("FROM"))) + COMPLETE_WITH_CONST("FROM"); + else if (HeadMatches1("FETCH") && TailMatches1("FROM")) + COMPLETE_WITH_QUERY(Query_for_list_of_cursors); + +/* FOREIGN DATA WRAPPER */ + /* applies in ALTER/DROP FDW and in CREATE SERVER */ + else if (TailMatches3("FOREIGN", "DATA", "WRAPPER") && + !TailMatches4("CREATE", MatchAny, MatchAny, MatchAny)) + COMPLETE_WITH_QUERY(Query_for_list_of_fdws); + /* applies in CREATE SERVER */ + else if (TailMatches4("FOREIGN", "DATA", "WRAPPER", MatchAny) && + HeadMatches2("CREATE", "SERVER")) + COMPLETE_WITH_CONST("OPTIONS"); + +/* FOREIGN TABLE */ + else if (TailMatches2("FOREIGN", "TABLE") && + !TailMatches3("CREATE", MatchAny, MatchAny)) + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_foreign_tables, NULL); + +/* FOREIGN SERVER */ + else if (TailMatches2("FOREIGN", "SERVER")) + COMPLETE_WITH_QUERY(Query_for_list_of_servers); + +/* + * GRANT and REVOKE are allowed inside CREATE SCHEMA and + * ALTER DEFAULT PRIVILEGES, so use TailMatches + */ + /* Complete GRANT/REVOKE with a list of roles and privileges */ + else if (TailMatches1("GRANT|REVOKE")) + { + /* + * With ALTER DEFAULT PRIVILEGES, restrict completion to grantable + * privileges (can't grant roles) + */ + if (HeadMatches3("ALTER", "DEFAULT", "PRIVILEGES")) + COMPLETE_WITH_LIST10("SELECT", "INSERT", "UPDATE", + "DELETE", "TRUNCATE", "REFERENCES", "TRIGGER", + "EXECUTE", "USAGE", "ALL"); + else + COMPLETE_WITH_QUERY(Query_for_list_of_roles + " UNION SELECT 'SELECT'" + " UNION SELECT 'INSERT'" + " UNION SELECT 'UPDATE'" + " UNION SELECT 'DELETE'" + " UNION SELECT 'TRUNCATE'" + " UNION SELECT 'REFERENCES'" + " UNION SELECT 'TRIGGER'" + " UNION SELECT 'CREATE'" + " UNION SELECT 'CONNECT'" + " UNION SELECT 'TEMPORARY'" + " UNION SELECT 'EXECUTE'" + " UNION SELECT 'USAGE'" + " UNION SELECT 'ALL'" + " UNION SELECT 'GRANT OPTION FOR'"); + } + + /* + * Complete GRANT/REVOKE with "ON", GRANT/REVOKE with + * TO/FROM + */ + else if (TailMatches2("GRANT|REVOKE", MatchAny)) + { + if (TailMatches1("SELECT|INSERT|UPDATE|DELETE|TRUNCATE|REFERENCES|TRIGGER|CREATE|CONNECT|TEMPORARY|TEMP|EXECUTE|USAGE|ALL")) + COMPLETE_WITH_CONST("ON"); + else if (TailMatches2("GRANT", MatchAny)) + COMPLETE_WITH_CONST("TO"); + else + COMPLETE_WITH_CONST("FROM"); + } + + /* + * Complete GRANT/REVOKE ON with a list of tables, views, and + * sequences. + * + * Keywords like DATABASE, FUNCTION, LANGUAGE and SCHEMA added to query + * result via UNION; seems to work intuitively. + * + * Note: GRANT/REVOKE can get quite complex; tab-completion as implemented + * here will only work if the privilege list contains exactly one + * privilege. + */ + else if (TailMatches3("GRANT|REVOKE", MatchAny, "ON")) + { + /* + * With ALTER DEFAULT PRIVILEGES, restrict completion to the kinds of + * objects supported. + */ + if (HeadMatches3("ALTER", "DEFAULT", "PRIVILEGES")) + COMPLETE_WITH_LIST5("TABLES", "SEQUENCES", "FUNCTIONS", "TYPES", "SCHEMAS"); + else + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tsvmf, + " UNION SELECT 'ALL FUNCTIONS IN SCHEMA'" + " UNION SELECT 'ALL SEQUENCES IN SCHEMA'" + " UNION SELECT 'ALL TABLES IN SCHEMA'" + " UNION SELECT 'DATABASE'" + " UNION SELECT 'DOMAIN'" + " UNION SELECT 'FOREIGN DATA WRAPPER'" + " UNION SELECT 'FOREIGN SERVER'" + " UNION SELECT 'FUNCTION'" + " UNION SELECT 'LANGUAGE'" + " UNION SELECT 'LARGE OBJECT'" + " UNION SELECT 'SCHEMA'" + " UNION SELECT 'SEQUENCE'" + " UNION SELECT 'TABLE'" + " UNION SELECT 'TABLESPACE'" + " UNION SELECT 'TYPE'"); + } + else if (TailMatches4("GRANT|REVOKE", MatchAny, "ON", "ALL")) + COMPLETE_WITH_LIST3("FUNCTIONS IN SCHEMA", "SEQUENCES IN SCHEMA", + "TABLES IN SCHEMA"); + else if (TailMatches4("GRANT|REVOKE", MatchAny, "ON", "FOREIGN")) + COMPLETE_WITH_LIST2("DATA WRAPPER", "SERVER"); + + /* + * Complete "GRANT/REVOKE * ON DATABASE/DOMAIN/..." with a list of + * appropriate objects. + * + * Complete "GRANT/REVOKE * ON *" with "TO/FROM". + */ + else if (TailMatches4("GRANT|REVOKE", MatchAny, "ON", MatchAny)) + { + if (TailMatches1("DATABASE")) + COMPLETE_WITH_QUERY(Query_for_list_of_databases); + else if (TailMatches1("DOMAIN")) + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_domains, NULL); + else if (TailMatches1("FUNCTION")) + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_functions, NULL); + else if (TailMatches1("LANGUAGE")) + COMPLETE_WITH_QUERY(Query_for_list_of_languages); + else if (TailMatches1("SCHEMA")) + COMPLETE_WITH_QUERY(Query_for_list_of_schemas); + else if (TailMatches1("SEQUENCE")) + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_sequences, NULL); + else if (TailMatches1("TABLE")) + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tsvmf, NULL); + else if (TailMatches1("TABLESPACE")) + COMPLETE_WITH_QUERY(Query_for_list_of_tablespaces); + else if (TailMatches1("TYPE")) + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_datatypes, NULL); + else if (TailMatches4("GRANT", MatchAny, MatchAny, MatchAny)) + COMPLETE_WITH_CONST("TO"); + else + COMPLETE_WITH_CONST("FROM"); + } + + /* + * Complete "GRANT/REVOKE ... TO/FROM" with username, PUBLIC, + * CURRENT_USER, or SESSION_USER. + */ + else if ((HeadMatches1("GRANT") && TailMatches1("TO")) || + (HeadMatches1("REVOKE") && TailMatches1("FROM"))) + COMPLETE_WITH_QUERY(Query_for_list_of_grant_roles); + /* Complete "ALTER DEFAULT PRIVILEGES ... GRANT/REVOKE ... TO/FROM */ + else if (HeadMatches3("ALTER", "DEFAULT", "PRIVILEGES") && TailMatches1("TO|FROM")) + COMPLETE_WITH_QUERY(Query_for_list_of_grant_roles); + /* Complete "GRANT/REVOKE ... ON * *" with TO/FROM */ + else if (HeadMatches1("GRANT") && TailMatches3("ON", MatchAny, MatchAny)) + COMPLETE_WITH_CONST("TO"); + else if (HeadMatches1("REVOKE") && TailMatches3("ON", MatchAny, MatchAny)) + COMPLETE_WITH_CONST("FROM"); + + /* Complete "GRANT/REVOKE * ON ALL * IN SCHEMA *" with TO/FROM */ + else if (TailMatches8("GRANT|REVOKE", MatchAny, "ON", "ALL", MatchAny, "IN", "SCHEMA", MatchAny)) + { + if (TailMatches8("GRANT", MatchAny, MatchAny, MatchAny, MatchAny, MatchAny, MatchAny, MatchAny)) + COMPLETE_WITH_CONST("TO"); + else + COMPLETE_WITH_CONST("FROM"); + } + + /* Complete "GRANT/REVOKE * ON FOREIGN DATA WRAPPER *" with TO/FROM */ + else if (TailMatches7("GRANT|REVOKE", MatchAny, "ON", "FOREIGN", "DATA", "WRAPPER", MatchAny)) + { + if (TailMatches7("GRANT", MatchAny, MatchAny, MatchAny, MatchAny, MatchAny, MatchAny)) + COMPLETE_WITH_CONST("TO"); + else + COMPLETE_WITH_CONST("FROM"); + } + + /* Complete "GRANT/REVOKE * ON FOREIGN SERVER *" with TO/FROM */ + else if (TailMatches6("GRANT|REVOKE", MatchAny, "ON", "FOREIGN", "SERVER", MatchAny)) + { + if (TailMatches6("GRANT", MatchAny, MatchAny, MatchAny, MatchAny, MatchAny)) + COMPLETE_WITH_CONST("TO"); + else + COMPLETE_WITH_CONST("FROM"); + } + +/* GROUP BY */ + else if (TailMatches3("FROM", MatchAny, "GROUP")) + COMPLETE_WITH_CONST("BY"); + +/* IMPORT FOREIGN SCHEMA */ + else if (Matches1("IMPORT")) + COMPLETE_WITH_CONST("FOREIGN SCHEMA"); + else if (Matches2("IMPORT", "FOREIGN")) + COMPLETE_WITH_CONST("SCHEMA"); + +/* INSERT --- can be inside EXPLAIN, RULE, etc */ + /* Complete INSERT with "INTO" */ + else if (TailMatches1("INSERT")) + COMPLETE_WITH_CONST("INTO"); + /* Complete INSERT INTO with table names */ + else if (TailMatches2("INSERT", "INTO")) + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_updatables, NULL); + /* Complete "INSERT INTO
(" with attribute names */ + else if (TailMatches4("INSERT", "INTO", MatchAny, "(")) + COMPLETE_WITH_ATTR(prev2_wd, ""); + + /* + * Complete INSERT INTO
with "(" or "VALUES" or "SELECT" or + * "TABLE" or "DEFAULT VALUES" or "OVERRIDING" + */ + else if (TailMatches3("INSERT", "INTO", MatchAny)) + COMPLETE_WITH_LIST6("(", "DEFAULT VALUES", "SELECT", "TABLE", "VALUES", "OVERRIDING"); + + /* + * Complete INSERT INTO
(attribs) with "VALUES" or "SELECT" or + * "TABLE" or "OVERRIDING" + */ + else if (TailMatches4("INSERT", "INTO", MatchAny, MatchAny) && + ends_with(prev_wd, ')')) + COMPLETE_WITH_LIST4("SELECT", "TABLE", "VALUES", "OVERRIDING"); + + /* Complete OVERRIDING */ + else if (TailMatches1("OVERRIDING")) + COMPLETE_WITH_LIST2("SYSTEM VALUE", "USER VALUE"); + + /* Complete after OVERRIDING clause */ + else if (TailMatches3("OVERRIDING", MatchAny, "VALUE")) + COMPLETE_WITH_LIST3("SELECT", "TABLE", "VALUES"); + + /* Insert an open parenthesis after "VALUES" */ + else if (TailMatches1("VALUES") && !TailMatches2("DEFAULT", "VALUES")) + COMPLETE_WITH_CONST("("); + +/* LOCK */ + /* Complete LOCK [TABLE] with a list of tables */ + else if (Matches1("LOCK")) + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tables, + " UNION SELECT 'TABLE'"); + else if (Matches2("LOCK", "TABLE")) + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tables, ""); + + /* For the following, handle the case of a single table only for now */ + + /* Complete LOCK [TABLE]
with "IN" */ + else if (Matches2("LOCK", MatchAnyExcept("TABLE")) || + Matches3("LOCK", "TABLE", MatchAny)) + COMPLETE_WITH_CONST("IN"); + + /* Complete LOCK [TABLE]
IN with a lock mode */ + else if (Matches3("LOCK", MatchAny, "IN") || + Matches4("LOCK", "TABLE", MatchAny, "IN")) + COMPLETE_WITH_LIST8("ACCESS SHARE MODE", + "ROW SHARE MODE", "ROW EXCLUSIVE MODE", + "SHARE UPDATE EXCLUSIVE MODE", "SHARE MODE", + "SHARE ROW EXCLUSIVE MODE", + "EXCLUSIVE MODE", "ACCESS EXCLUSIVE MODE"); + + /* Complete LOCK [TABLE]
IN ACCESS|ROW with rest of lock mode */ + else if (Matches4("LOCK", MatchAny, "IN", "ACCESS|ROW") || + Matches5("LOCK", "TABLE", MatchAny, "IN", "ACCESS|ROW")) + COMPLETE_WITH_LIST2("EXCLUSIVE MODE", "SHARE MODE"); + + /* Complete LOCK [TABLE]
IN SHARE with rest of lock mode */ + else if (Matches4("LOCK", MatchAny, "IN", "SHARE") || + Matches5("LOCK", "TABLE", MatchAny, "IN", "SHARE")) + COMPLETE_WITH_LIST3("MODE", "ROW EXCLUSIVE MODE", + "UPDATE EXCLUSIVE MODE"); + +/* NOTIFY --- can be inside EXPLAIN, RULE, etc */ + else if (TailMatches1("NOTIFY")) + COMPLETE_WITH_QUERY("SELECT pg_catalog.quote_ident(channel) FROM pg_catalog.pg_listening_channels() AS channel WHERE substring(pg_catalog.quote_ident(channel),1,%d)='%s'"); + +/* OPTIONS */ + else if (TailMatches1("OPTIONS")) + COMPLETE_WITH_CONST("("); + +/* OWNER TO - complete with available roles */ + else if (TailMatches2("OWNER", "TO")) + COMPLETE_WITH_QUERY(Query_for_list_of_roles); + +/* ORDER BY */ + else if (TailMatches3("FROM", MatchAny, "ORDER")) + COMPLETE_WITH_CONST("BY"); + else if (TailMatches4("FROM", MatchAny, "ORDER", "BY")) + COMPLETE_WITH_ATTR(prev3_wd, ""); + +/* PREPARE xx AS */ + else if (Matches2("PREPARE", MatchAny)) + COMPLETE_WITH_CONST("AS"); + else if (Matches3("PREPARE", MatchAny, "AS")) + COMPLETE_WITH_LIST4("SELECT", "UPDATE", "INSERT", "DELETE FROM"); + +/* + * PREPARE TRANSACTION is missing on purpose. It's intended for transaction + * managers, not for manual use in interactive sessions. + */ + +/* REASSIGN OWNED BY xxx TO yyy */ + else if (Matches1("REASSIGN")) + COMPLETE_WITH_CONST("OWNED BY"); + else if (Matches2("REASSIGN", "OWNED")) + COMPLETE_WITH_CONST("BY"); + else if (Matches3("REASSIGN", "OWNED", "BY")) + COMPLETE_WITH_QUERY(Query_for_list_of_roles); + else if (Matches4("REASSIGN", "OWNED", "BY", MatchAny)) + COMPLETE_WITH_CONST("TO"); + else if (Matches5("REASSIGN", "OWNED", "BY", MatchAny, "TO")) + COMPLETE_WITH_QUERY(Query_for_list_of_roles); + +/* REFRESH MATERIALIZED VIEW */ + else if (Matches1("REFRESH")) + COMPLETE_WITH_CONST("MATERIALIZED VIEW"); + else if (Matches2("REFRESH", "MATERIALIZED")) + COMPLETE_WITH_CONST("VIEW"); + else if (Matches3("REFRESH", "MATERIALIZED", "VIEW")) + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_matviews, + " UNION SELECT 'CONCURRENTLY'"); + else if (Matches4("REFRESH", "MATERIALIZED", "VIEW", "CONCURRENTLY")) + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_matviews, NULL); + else if (Matches4("REFRESH", "MATERIALIZED", "VIEW", MatchAny)) + COMPLETE_WITH_CONST("WITH"); + else if (Matches5("REFRESH", "MATERIALIZED", "VIEW", "CONCURRENTLY", MatchAny)) + COMPLETE_WITH_CONST("WITH"); + else if (Matches5("REFRESH", "MATERIALIZED", "VIEW", MatchAny, "WITH")) + COMPLETE_WITH_LIST2("NO DATA", "DATA"); + else if (Matches6("REFRESH", "MATERIALIZED", "VIEW", "CONCURRENTLY", MatchAny, "WITH")) + COMPLETE_WITH_LIST2("NO DATA", "DATA"); + else if (Matches6("REFRESH", "MATERIALIZED", "VIEW", MatchAny, "WITH", "NO")) + COMPLETE_WITH_CONST("DATA"); + else if (Matches7("REFRESH", "MATERIALIZED", "VIEW", "CONCURRENTLY", MatchAny, "WITH", "NO")) + COMPLETE_WITH_CONST("DATA"); + +/* REINDEX */ + else if (Matches1("REINDEX")) + COMPLETE_WITH_LIST5("TABLE", "INDEX", "SYSTEM", "SCHEMA", "DATABASE"); + else if (Matches2("REINDEX", "TABLE")) + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tm, NULL); + else if (Matches2("REINDEX", "INDEX")) + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_indexes, NULL); + else if (Matches2("REINDEX", "SCHEMA")) + COMPLETE_WITH_QUERY(Query_for_list_of_schemas); + else if (Matches2("REINDEX", "SYSTEM|DATABASE")) + COMPLETE_WITH_QUERY(Query_for_list_of_databases); + +/* SECURITY LABEL */ + else if (Matches1("SECURITY")) + COMPLETE_WITH_CONST("LABEL"); + else if (Matches2("SECURITY", "LABEL")) + COMPLETE_WITH_LIST2("ON", "FOR"); + else if (Matches4("SECURITY", "LABEL", "FOR", MatchAny)) + COMPLETE_WITH_CONST("ON"); + else if (Matches3("SECURITY", "LABEL", "ON") || + Matches5("SECURITY", "LABEL", "FOR", MatchAny, "ON")) + { + static const char *const list_SECURITY_LABEL[] = + {"TABLE", "COLUMN", "AGGREGATE", "DATABASE", "DOMAIN", + "EVENT TRIGGER", "FOREIGN TABLE", "FUNCTION", "LARGE OBJECT", + "MATERIALIZED VIEW", "LANGUAGE", "PUBLICATION", "ROLE", "SCHEMA", + "SEQUENCE", "SUBSCRIPTION", "TABLESPACE", "TYPE", "VIEW", NULL}; + + COMPLETE_WITH_LIST(list_SECURITY_LABEL); + } + else if (Matches5("SECURITY", "LABEL", "ON", MatchAny, MatchAny)) + COMPLETE_WITH_CONST("IS"); + +/* SELECT */ + else if (Matches1("SELECT")) + COMPLETE_WITH_LIST4("TOP", "ALL", "DISTINCT", "*"); +/* remove if MatchAny below works for * + else if (Matches2("SELECT", "*")) + COMPLETE_WITH_LIST7("FROM", "WHERE", "GROUP BY", "HAVING", "ORDER BY", "LIMIT", "OFFSET"); +*/ + else if (Matches2("SELECT", MatchAny)) + COMPLETE_WITH_LIST8("FROM", "INTO", "WHERE", "GROUP BY", "HAVING", "ORDER BY", "LIMIT", "OFFSET"); + else if (HeadMatches1("SELECT") && TailMatches1("INTO")) + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tables, + "UNION SELECT 'TEMP' UNION SELECT 'TEMPORARY' UNION SELECT 'TABLE'"); + else if (HeadMatches1("SELECT") && TailMatches2("INTO", MatchAny)) + COMPLETE_WITH_LIST7("FROM", "WHERE", "GROUP BY", "HAVING", "ORDER BY", "LIMIT", "OFFSET"); + /* SELECT FROM */ + else if (HeadMatches1("SELECT") && TailMatches1("FROM")) + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tables, NULL); + else if (HeadMatches1("SELECT") && TailMatches2("FROM", MatchAny)) + COMPLETE_WITH_LIST7("WHERE", "GROUP BY", "HAVING", "UNION", "ORDER BY", "LIMIT", "OFFSET"); + /* SELECT WHERE */ + else if (HeadMatches1("SELECT") && TailMatches3("FROM", MatchAny, "WHERE")) + COMPLETE_WITH_ATTR(prev2_wd, ""); + else if ( + (HeadMatches1("SELECT") && TailMatches3("WHERE", MatchAny, MatchAnyExcept("HAVING|BY|LIMIT|OFFSET"))) || + (HeadMatches1("SELECT") && TailMatches4("WHERE", MatchAny, MatchAnyExcept("HAVING|BY|LIMIT|OFFSET"), MatchAnyExcept("HAVING|BY|LIMIT|OFFSET"))) + ) + COMPLETE_WITH_LIST5("GROUP BY", "HAVING", "ORDER BY", "LIMIT", "OFFSET"); + /* SELECT GROUP BY */ + else if (HeadMatches1("SELECT") && TailMatches3("GROUP", "BY", MatchAny)) + COMPLETE_WITH_LIST5("HAVING", "UNION", "ORDER BY", "LIMIT", "OFFSET"); + /* SELECT ORDER BY */ + else if (HeadMatches1("SELECT") && TailMatches3("ORDER", "BY", MatchAny)) + COMPLETE_WITH_LIST2("ASC", "DESC"); + else if ((HeadMatches1("SELECT") && TailMatches3("ORDER", "BY", MatchAny)) || + (HeadMatches1("SELECT") && TailMatches4("ORDER", "BY", MatchAny, "ASC|DESC"))) + COMPLETE_WITH_LIST2("LIMIT", "OFFSET"); + /* SELECT LIMIT */ + else if (HeadMatches1("SELECT") && TailMatches1("LIMIT")) + COMPLETE_WITH_CONST("ALL"); + /* SELECT OFFSET */ + else if (HeadMatches1("SELECT") && TailMatches2("LIMIT", MatchAny)) + COMPLETE_WITH_CONST("OFFSET"); + + +/* SET, RESET, SHOW */ + /* Complete with a variable name */ + else if (TailMatches1("SET|RESET") && !TailMatches3("UPDATE", MatchAny, "SET")) + COMPLETE_WITH_QUERY(Query_for_list_of_set_vars + " UNION SELECT 'SESSION'" + " UNION SELECT 'LOCAL'" + " UNION SELECT 'SEED'"); + else if (Matches1("SHOW")) + COMPLETE_WITH_QUERY(Query_for_list_of_show_vars); + /* Complete "SET TRANSACTION" */ + else if (Matches2("SET", "TRANSACTION")) + COMPLETE_WITH_LIST5("SNAPSHOT", "ISOLATION LEVEL", "READ", "DEFERRABLE", "NOT DEFERRABLE"); + else if (Matches2("BEGIN|START", "TRANSACTION") || + Matches2("BEGIN", "WORK") || + Matches1("BEGIN") || + Matches5("SET", "SESSION", "CHARACTERISTICS", "AS", "TRANSACTION")) + COMPLETE_WITH_LIST4("ISOLATION LEVEL", "READ", "DEFERRABLE", "NOT DEFERRABLE"); + else if (Matches3("SET|BEGIN|START", "TRANSACTION|WORK", "NOT") || + Matches2("BEGIN", "NOT") || + Matches6("SET", "SESSION", "CHARACTERISTICS", "AS", "TRANSACTION", "NOT")) + COMPLETE_WITH_CONST("DEFERRABLE"); + else if (Matches3("SET|BEGIN|START", "TRANSACTION|WORK", "ISOLATION") || + Matches2("BEGIN", "ISOLATION") || + Matches6("SET", "SESSION", "CHARACTERISTICS", "AS", "TRANSACTION", "ISOLATION")) + COMPLETE_WITH_CONST("LEVEL"); + else if (Matches4("SET|BEGIN|START", "TRANSACTION|WORK", "ISOLATION", "LEVEL") || + Matches3("BEGIN", "ISOLATION", "LEVEL") || + Matches7("SET", "SESSION", "CHARACTERISTICS", "AS", "TRANSACTION", "ISOLATION", "LEVEL")) + COMPLETE_WITH_LIST3("READ", "REPEATABLE READ", "SERIALIZABLE"); + else if (Matches5("SET|BEGIN|START", "TRANSACTION|WORK", "ISOLATION", "LEVEL", "READ") || + Matches4("BEGIN", "ISOLATION", "LEVEL", "READ") || + Matches8("SET", "SESSION", "CHARACTERISTICS", "AS", "TRANSACTION", "ISOLATION", "LEVEL", "READ")) + COMPLETE_WITH_LIST2("UNCOMMITTED", "COMMITTED"); + else if (Matches5("SET|BEGIN|START", "TRANSACTION|WORK", "ISOLATION", "LEVEL", "REPEATABLE") || + Matches4("BEGIN", "ISOLATION", "LEVEL", "REPEATABLE") || + Matches8("SET", "SESSION", "CHARACTERISTICS", "AS", "TRANSACTION", "ISOLATION", "LEVEL", "REPEATABLE")) + COMPLETE_WITH_CONST("READ"); + else if (Matches3("SET|BEGIN|START", "TRANSACTION|WORK", "READ") || + Matches2("BEGIN", "READ") || + Matches6("SET", "SESSION", "CHARACTERISTICS", "AS", "TRANSACTION", "READ")) + COMPLETE_WITH_LIST2("ONLY", "WRITE"); + /* SET CONSTRAINTS */ + else if (Matches2("SET", "CONSTRAINTS")) + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_constraints_with_schema, "UNION SELECT 'ALL'"); + /* Complete SET CONSTRAINTS with DEFERRED|IMMEDIATE */ + else if (Matches3("SET", "CONSTRAINTS", MatchAny)) + COMPLETE_WITH_LIST2("DEFERRED", "IMMEDIATE"); + /* Complete SET ROLE */ + else if (Matches2("SET", "ROLE")) + COMPLETE_WITH_QUERY(Query_for_list_of_roles); + /* Complete SET SESSION with AUTHORIZATION or CHARACTERISTICS... */ + else if (Matches2("SET", "SESSION")) + COMPLETE_WITH_LIST2("AUTHORIZATION", "CHARACTERISTICS AS TRANSACTION"); + /* Complete SET SESSION AUTHORIZATION with username */ + else if (Matches3("SET", "SESSION", "AUTHORIZATION")) + COMPLETE_WITH_QUERY(Query_for_list_of_roles " UNION SELECT 'DEFAULT'"); + /* Complete RESET SESSION with AUTHORIZATION */ + else if (Matches2("RESET", "SESSION")) + COMPLETE_WITH_CONST("AUTHORIZATION"); + /* RESET parameter / ALL */ + else if (Matches1("RESET")) + COMPLETE_WITH_QUERY(Query_for_list_of_show_vars); +/* Complete SET with "TO" */ + else if (Matches2("SET", MatchAny)) + COMPLETE_WITH_CONST("TO"); + /* Complete ALTER DATABASE|FUNCTION|ROLE|USER ... SET */ + else if (HeadMatches2("ALTER", "DATABASE|FUNCTION|ROLE|USER") && + TailMatches2("SET", MatchAny)) + COMPLETE_WITH_LIST2("FROM CURRENT", "TO"); + /* Suggest possible variable values */ + else if (TailMatches3("SET", MatchAny, "TO|=")) + { + /* special cased code for individual GUCs */ + if (TailMatches2("DateStyle", "TO|=")) + { + static const char *const my_list[] = + {"ISO", "SQL", "Postgres", "German", + "YMD", "DMY", "MDY", + "US", "European", "NonEuropean", + "DEFAULT", NULL}; + + COMPLETE_WITH_LIST(my_list); + } + else if (TailMatches2("search_path", "TO|=")) + COMPLETE_WITH_QUERY(Query_for_list_of_schemas + " AND nspname not like 'pg\\_toast%%' " + " AND nspname not like 'pg\\_temp%%' " + " UNION SELECT 'DEFAULT' "); + else + { + /* generic, type based, GUC support */ + char *guctype = get_guctype(prev2_wd); + + if (guctype && strcmp(guctype, "enum") == 0) + { + char querybuf[1024]; + + snprintf(querybuf, sizeof(querybuf), Query_for_enum, prev2_wd); + COMPLETE_WITH_QUERY(querybuf); + } + else if (guctype && strcmp(guctype, "bool") == 0) + COMPLETE_WITH_LIST9("on", "off", "true", "false", "yes", "no", + "1", "0", "DEFAULT"); + else + COMPLETE_WITH_CONST("DEFAULT"); + + if (guctype) + free(guctype); + } + } + +/* START TRANSACTION */ + else if (Matches1("START")) + COMPLETE_WITH_CONST("TRANSACTION"); + +/* TABLE, but not TABLE embedded in other commands */ + else if (Matches1("TABLE")) + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_relations, NULL); + +/* TABLESAMPLE */ + else if (TailMatches1("TABLESAMPLE")) + COMPLETE_WITH_QUERY(Query_for_list_of_tablesample_methods); + else if (TailMatches2("TABLESAMPLE", MatchAny)) + COMPLETE_WITH_CONST("("); + +/* TRUNCATE */ + else if (Matches1("TRUNCATE")) + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tables, " UNION SELECT 'TABLE'"); + else if (Matches2("TRUNCATE", "TABLE")) + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tables, NULL); + +/* UNLISTEN */ + else if (Matches1("UNLISTEN")) + COMPLETE_WITH_QUERY("SELECT pg_catalog.quote_ident(channel) FROM pg_catalog.pg_listening_channels() AS channel WHERE substring(pg_catalog.quote_ident(channel),1,%d)='%s' UNION SELECT '*'"); + +/* UNLISTEN */ + else if (Matches1("UNLOAD")) + COMPLETE_WITH_CONST("("); + else if (Matches2("UNLOAD", MatchAny)) + COMPLETE_WITH_CONST("TO"); + else if (HeadMatches1("UNLOAD") && TailMatches2("TO", MatchAnyExcept("TO"))) + COMPLETE_WITH_LIST6("IAM_ROLE", "ACCESS_KEY_ID", "SECRET_ACCESS_KEY", "SESSION_TOKEN", "WITH", "CREDENTIALS"); + else if (HeadMatches1("UNLOAD") && TailMatches2("ACCESS_KEY_ID", MatchAny)) + COMPLETE_WITH_CONST("SECRET_ACCESS_KEY"); + else if (HeadMatches1("UNLOAD") && TailMatches2("SECRET_ACCESS_KEY", MatchAny)) + COMPLETE_WITH_CONST("SESSION_TOKEN"); + else if (HeadMatches1("UNLOAD") && TailMatches3("TO", MatchAny, "WITH")) + COMPLETE_WITH_CONST("CREDENTIALS"); + else if (HeadMatches1("UNLOAD") && TailMatches1("CREDENTIALS")) + COMPLETE_WITH_CONST("AS"); + else if (HeadMatches1("UNLOAD") && ( + (TailMatches2("IAM_ROLE", MatchAny)) || + (TailMatches2("SECRET_ACCESS_KEY", MatchAny)) || + (TailMatches2("SESSION_TOKEN", MatchAny)) || + (TailMatches2("CREDENTIALS|AS", MatchAny)) + )) + COMPLETE_WITH_LIST12("MANIFEST", "DELIMITER", "FIXEDWIDTH", "ENCRYPTED", "BZIP2", "GZIP", "ADDQUOTES", "NULL", "ESCAPE", "ALLOWOVERWRITE", "PARALLEL", "MAXFILESIZE"); + else if (HeadMatches1("UNLOAD") && TailMatches1("MANIFEST")) + COMPLETE_WITH_LIST11("DELIMITER", "FIXEDWIDTH", "ENCRYPTED", "BZIP2", "GZIP", "ADDQUOTES", "NULL", "ESCAPE", "ALLOWOVERWRITE", "PARALLEL", "MAXFILESIZE"); + else if (HeadMatches1("UNLOAD") && TailMatches1("ENCRYPTED")) + COMPLETE_WITH_LIST11("MANIFEST", "DELIMITER", "FIXEDWIDTH", "BZIP2", "GZIP", "ADDQUOTES", "NULL", "ESCAPE", "ALLOWOVERWRITE", "PARALLEL", "MAXFILESIZE"); + else if (HeadMatches1("UNLOAD") && TailMatches1("BZIP2")) + COMPLETE_WITH_LIST11("MANIFEST", "DELIMITER", "FIXEDWIDTH", "ENCRYPTED", "GZIP", "ADDQUOTES", "NULL", "ESCAPE", "ALLOWOVERWRITE", "PARALLEL", "MAXFILESIZE"); + else if (HeadMatches1("UNLOAD") && TailMatches1("GZIP")) + COMPLETE_WITH_LIST11("MANIFEST", "DELIMITER", "FIXEDWIDTH", "ENCRYPTED", "BZIP2", "ADDQUOTES", "NULL", "ESCAPE", "ALLOWOVERWRITE", "PARALLEL", "MAXFILESIZE"); + else if (HeadMatches1("UNLOAD") && TailMatches1("ADDQUOTES")) + COMPLETE_WITH_LIST11("MANIFEST", "DELIMITER", "FIXEDWIDTH", "ENCRYPTED", "BZIP2", "GZIP", "NULL", "ESCAPE", "ALLOWOVERWRITE", "PARALLEL", "MAXFILESIZE"); + else if (HeadMatches1("UNLOAD") && TailMatches1("ESCAPE")) + COMPLETE_WITH_LIST11("MANIFEST", "DELIMITER", "FIXEDWIDTH", "ENCRYPTED", "BZIP2", "GZIP", "ADDQUOTES", "NULL", "ALLOWOVERWRITE", "PARALLEL", "MAXFILESIZE"); + else if (HeadMatches1("UNLOAD") && TailMatches1("ALLOWOVERWRITE")) + COMPLETE_WITH_LIST11("MANIFEST", "DELIMITER", "FIXEDWIDTH", "ENCRYPTED", "BZIP2", "GZIP", "ADDQUOTES", "NULL", "ESCAPE", "ALLOWOVERWRITE", "PARALLEL"); + else if (HeadMatches1("UNLOAD") && TailMatches1("DELIMITER|FIXEDWIDTH|NULL")) + COMPLETE_WITH_CONST("AS '"); + else if (HeadMatches1("UNLOAD") && TailMatches1("MAXFILESIZE")) + COMPLETE_WITH_CONST("AS"); + else if (HeadMatches1("UNLOAD") && TailMatches1("PARALLEL")) + COMPLETE_WITH_LIST4("ON", "OFF", "TRUE", "FALSE"); + else if (HeadMatches1("UNLOAD") && ( + TailMatches2("MAXFILESIZE", MatchAnyExcept("AS")) || + TailMatches3("MAXFILESIZE", "AS", MatchAny) + )) + COMPLETE_WITH_LIST2("MB", "GB"); + else if (HeadMatches1("UNLOAD") && ( + TailMatches2("DELIMITER", MatchAny) || + TailMatches3("DELIMITER", "AS", MatchAny) + )) + COMPLETE_WITH_LIST11("MANIFEST", "FIXEDWIDTH", "ENCRYPTED", "BZIP2", "GZIP", "ADDQUOTES", "NULL", "ESCAPE", "ALLOWOVERWRITE", "PARALLEL", "MAXFILESIZE"); + else if (HeadMatches1("UNLOAD") && ( + TailMatches2("FIXEDWIDTH", MatchAny) || + TailMatches3("FIXEDWIDTH", "AS", MatchAny) + )) + COMPLETE_WITH_LIST11("MANIFEST", "DELIMITER", "ENCRYPTED", "BZIP2", "GZIP", "ADDQUOTES", "NULL", "ESCAPE", "ALLOWOVERWRITE", "PARALLEL", "MAXFILESIZE"); + else if (HeadMatches1("UNLOAD") && ( + TailMatches2("NULL", MatchAny) || + TailMatches3("NULL", "AS", MatchAny) + )) + COMPLETE_WITH_LIST11("MANIFEST", "DELIMITER", "FIXEDWIDTH", "ENCRYPTED", "BZIP2", "GZIP", "ADDQUOTES", "ESCAPE", "ALLOWOVERWRITE", "PARALLEL", "MAXFILESIZE"); + else if (HeadMatches1("UNLOAD") && ( + TailMatches2("PARALLEL", MatchAny) || + TailMatches3("PARALLEL", "AS", MatchAny) + )) + COMPLETE_WITH_LIST11("MANIFEST", "DELIMITER", "FIXEDWIDTH", "ENCRYPTED", "BZIP2", "GZIP", "ADDQUOTES", "NULL", "ESCAPE", "ALLOWOVERWRITE", "MAXFILESIZE"); + else if (HeadMatches1("UNLOAD") && ( + TailMatches3("MAXFILESIZE", MatchAnyExcept("AS"), "MB|GB") || + TailMatches4("MAXFILESIZE", "AS", MatchAnyExcept("AS"), "MB|GB") + )) + COMPLETE_WITH_LIST12("MANIFEST", "DELIMITER", "FIXEDWIDTH", "ENCRYPTED", "BZIP2", "GZIP", "ADDQUOTES", "NULL", "ESCAPE", "ALLOWOVERWRITE", "PARALLEL", "MAXFILESIZE"); + + +/* UPDATE --- can be inside EXPLAIN, RULE, etc */ + /* If prev. word is UPDATE suggest a list of tables */ + else if (TailMatches1("UPDATE")) + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_updatables, NULL); + /* Complete UPDATE
with "SET" */ + else if (TailMatches2("UPDATE", MatchAny)) + COMPLETE_WITH_CONST("SET"); + /* Complete UPDATE
SET with list of attributes */ + else if (TailMatches3("UPDATE", MatchAny, "SET")) + COMPLETE_WITH_ATTR(prev2_wd, ""); + /* UPDATE
SET = */ + else if (TailMatches4("UPDATE", MatchAny, "SET", MatchAny)) + COMPLETE_WITH_CONST("="); + /* UPDATE
SET = FROM / WHERE */ + else if (TailMatches6("UPDATE", MatchAny, "SET", MatchAny, "=", MatchAny)) + COMPLETE_WITH_LIST2("FROM", "WHERE"); + /* UPDATE
SET = FROM */ + else if (TailMatches7("UPDATE", MatchAny, "SET", MatchAny, "=", MatchAny, "FROM")) + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tables, NULL); + /* UPDATE
SET = FROM WHERE */ + else if (TailMatches8("UPDATE", MatchAny, "SET", MatchAny, "=", MatchAny, "FROM", MatchAny)) + COMPLETE_WITH_CONST("WHERE"); + /* UPDATE
SET = FROM WHERE */ + else if (TailMatches9("UPDATE", MatchAny, "SET", MatchAny, "=", MatchAny, "FROM", MatchAny, "WHERE")) + COMPLETE_WITH_ATTR(prev2_wd, ""); + +/* USER MAPPING */ + else if (Matches3("ALTER|CREATE|DROP", "USER", "MAPPING")) + COMPLETE_WITH_CONST("FOR"); + else if (Matches4("CREATE", "USER", "MAPPING", "FOR")) + COMPLETE_WITH_QUERY(Query_for_list_of_roles + " UNION SELECT 'CURRENT_USER'" + " UNION SELECT 'PUBLIC'" + " UNION SELECT 'USER'"); + else if (Matches4("ALTER|DROP", "USER", "MAPPING", "FOR")) + COMPLETE_WITH_QUERY(Query_for_list_of_user_mappings); + else if (Matches5("CREATE|ALTER|DROP", "USER", "MAPPING", "FOR", MatchAny)) + COMPLETE_WITH_CONST("SERVER"); + else if (Matches7("CREATE|ALTER", "USER", "MAPPING", "FOR", MatchAny, "SERVER", MatchAny)) + COMPLETE_WITH_CONST("OPTIONS"); + +/* + * VACUUM [ FULL | FREEZE ] [ VERBOSE ] [ table ] + * VACUUM [ FULL | FREEZE ] [ VERBOSE ] ANALYZE [ table [ (column [, ...] ) ] ] + */ + else if (Matches1("VACUUM")) + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tm, + " UNION SELECT 'FULL'" + " UNION SELECT 'SORT ONLY'" + " UNION SELECT 'DELETE ONLY'" + " UNION SELECT 'REINDEX'" + " UNION SELECT 'FREEZE'" + " UNION SELECT 'ANALYZE'" + " UNION SELECT 'VERBOSE'"); + else if (Matches2("VACUUM", "FULL|FREEZE")) + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tm, + " UNION SELECT 'ANALYZE'" + " UNION SELECT 'VERBOSE'"); + else if (Matches3("VACUUM", "FULL|FREEZE", "ANALYZE")) + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tm, + " UNION SELECT 'VERBOSE'"); + else if (Matches3("VACUUM", "FULL|FREEZE", "VERBOSE")) + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tm, + " UNION SELECT 'ANALYZE'"); + else if (Matches2("VACUUM", "VERBOSE")) + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tm, + " UNION SELECT 'ANALYZE'"); + else if (Matches2("VACUUM", "ANALYZE")) + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tm, + " UNION SELECT 'VERBOSE'"); + else if (Matches3("VACUUM", "FULL|REINDEX", MatchAny) || + Matches4("VACUUM", "SORT|DELETE", "ONLY", MatchAny)) + COMPLETE_WITH_CONST("TO"); + else if (Matches5("VACUUM", "FULL|REINDEX", MatchAny, "TO", MatchAny) || + Matches6("VACUUM", "SORT|DELETE", "ONLY", MatchAny, "TO", MatchAny)) + COMPLETE_WITH_CONST("PERCENT"); + +/* WITH [RECURSIVE] */ + + /* + * Only match when WITH is the first word, as WITH may appear in many + * other contexts. + */ + else if (Matches1("WITH")) + COMPLETE_WITH_CONST("RECURSIVE"); + +/* ANALYZE */ + /* Complete with list of tables */ + else if (Matches1("ANALYZE|ANALYSE")) + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tmf, + " UNION SELECT 'VERBOSE'" + " UNION SELECT 'PREDICATE COLUMNS'" + " UNION SELECT 'ALL COLUMNS'" + " UNION SELECT 'COMPRESSION'" + ); + +/* WHERE */ + /* Simple case of the word before the where being the table name */ + else if (TailMatches2(MatchAny, "WHERE")) + COMPLETE_WITH_ATTR(prev2_wd, ""); + +/* ... FROM ... */ +/* TODO: also include SRF ? */ + else if (TailMatches1("FROM") && !Matches3("COPY|\\copy", MatchAny, "FROM")) + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tsvmf, NULL); + +/* ... JOIN ... */ + else if (TailMatches1("JOIN")) + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tsvmf, NULL); + +/* Backslash commands */ +/* TODO: \dc \dd \dl */ + else if (TailMatchesCS1("\\?")) + COMPLETE_WITH_LIST_CS3("commands", "options", "variables"); + else if (TailMatchesCS1("\\connect|\\c")) + { + if (!recognized_connection_string(text)) + COMPLETE_WITH_QUERY(Query_for_list_of_databases); + } + else if (TailMatchesCS2("\\connect|\\c", MatchAny)) + { + if (!recognized_connection_string(prev_wd)) + COMPLETE_WITH_QUERY(Query_for_list_of_roles); + } + else if (TailMatchesCS1("\\da*")) + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_aggregates, NULL); + else if (TailMatchesCS1("\\dA*")) + COMPLETE_WITH_QUERY(Query_for_list_of_access_methods); + else if (TailMatchesCS1("\\db*")) + COMPLETE_WITH_QUERY(Query_for_list_of_tablespaces); + else if (TailMatchesCS1("\\dD*")) + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_domains, NULL); + else if (TailMatchesCS1("\\des*")) + COMPLETE_WITH_QUERY(Query_for_list_of_servers); + else if (TailMatchesCS1("\\deu*")) + COMPLETE_WITH_QUERY(Query_for_list_of_user_mappings); + else if (TailMatchesCS1("\\dew*")) + COMPLETE_WITH_QUERY(Query_for_list_of_fdws); + else if (TailMatchesCS1("\\df*")) + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_functions, NULL); + + else if (TailMatchesCS1("\\dFd*")) + COMPLETE_WITH_QUERY(Query_for_list_of_ts_dictionaries); + else if (TailMatchesCS1("\\dFp*")) + COMPLETE_WITH_QUERY(Query_for_list_of_ts_parsers); + else if (TailMatchesCS1("\\dFt*")) + COMPLETE_WITH_QUERY(Query_for_list_of_ts_templates); + /* must be at end of \dF alternatives: */ + else if (TailMatchesCS1("\\dF*")) + COMPLETE_WITH_QUERY(Query_for_list_of_ts_configurations); + + else if (TailMatchesCS1("\\di*")) + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_indexes, NULL); + else if (TailMatchesCS1("\\dL*")) + COMPLETE_WITH_QUERY(Query_for_list_of_languages); + else if (TailMatchesCS1("\\dn*")) + COMPLETE_WITH_QUERY(Query_for_list_of_schemas); + else if (TailMatchesCS1("\\dp") || TailMatchesCS1("\\z")) + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tsvmf, NULL); + else if (TailMatchesCS1("\\ds*")) + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_sequences, NULL); + else if (TailMatchesCS1("\\dt*")) + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tables, NULL); + else if (TailMatchesCS1("\\dT*")) + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_datatypes, NULL); + else if (TailMatchesCS1("\\du*") || TailMatchesCS1("\\dg*")) + COMPLETE_WITH_QUERY(Query_for_list_of_roles); + else if (TailMatchesCS1("\\dv*")) + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_views, NULL); + else if (TailMatchesCS1("\\dx*")) + COMPLETE_WITH_QUERY(Query_for_list_of_extensions); + else if (TailMatchesCS1("\\dm*")) + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_matviews, NULL); + else if (TailMatchesCS1("\\dE*")) + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_foreign_tables, NULL); + else if (TailMatchesCS1("\\dy*")) + COMPLETE_WITH_QUERY(Query_for_list_of_event_triggers); + + /* must be at end of \d alternatives: */ + else if (TailMatchesCS1("\\d*")) + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_relations, NULL); + + else if (TailMatchesCS1("\\ef")) + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_functions, NULL); + else if (TailMatchesCS1("\\ev")) + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_views, NULL); + + else if (TailMatchesCS1("\\encoding")) + COMPLETE_WITH_QUERY(Query_for_list_of_encodings); + else if (TailMatchesCS1("\\h|\\help")) + COMPLETE_WITH_LIST(sql_commands); + else if (TailMatchesCS2("\\h|\\help", MatchAny)) + { + if (TailMatches1("DROP")) + matches = completion_matches(text, drop_command_generator); + else if (TailMatches1("ALTER")) + matches = completion_matches(text, alter_command_generator); + + /* + * CREATE is recognized by tail match elsewhere, so doesn't need to be + * repeated here + */ + } + else if (TailMatchesCS3("\\h|\\help", MatchAny, MatchAny)) + { + if (TailMatches2("CREATE|DROP", "ACCESS")) + COMPLETE_WITH_CONST("METHOD"); + else if (TailMatches2("ALTER", "DEFAULT")) + COMPLETE_WITH_CONST("PRIVILEGES"); + else if (TailMatches2("CREATE|ALTER|DROP", "EVENT")) + COMPLETE_WITH_CONST("TRIGGER"); + else if (TailMatches2("CREATE|ALTER|DROP", "FOREIGN")) + COMPLETE_WITH_LIST2("DATA WRAPPER", "TABLE"); + else if (TailMatches2("ALTER", "LARGE")) + COMPLETE_WITH_CONST("OBJECT"); + else if (TailMatches2("CREATE|ALTER|DROP", "MATERIALIZED")) + COMPLETE_WITH_CONST("VIEW"); + else if (TailMatches2("CREATE|ALTER|DROP", "TEXT")) + COMPLETE_WITH_CONST("SEARCH"); + else if (TailMatches2("CREATE|ALTER|DROP", "USER")) + COMPLETE_WITH_CONST("MAPPING FOR"); + } + else if (TailMatchesCS4("\\h|\\help", MatchAny, MatchAny, MatchAny)) + { + if (TailMatches3("CREATE|ALTER|DROP", "FOREIGN", "DATA")) + COMPLETE_WITH_CONST("WRAPPER"); + else if (TailMatches3("CREATE|ALTER|DROP", "TEXT", "SEARCH")) + COMPLETE_WITH_LIST4("CONFIGURATION", "DICTIONARY", "PARSER", "TEMPLATE"); + else if (TailMatches3("CREATE|ALTER|DROP", "USER", "MAPPING")) + COMPLETE_WITH_CONST("FOR"); + } + else if (TailMatchesCS1("\\l*") && !TailMatchesCS1("\\lo*")) + COMPLETE_WITH_QUERY(Query_for_list_of_databases); + else if (TailMatchesCS1("\\password")) + COMPLETE_WITH_QUERY(Query_for_list_of_roles); + else if (TailMatchesCS1("\\pset")) + { + static const char *const my_list[] = + {"border", "columns", "expanded", "fieldsep", "fieldsep_zero", + "footer", "format", "linestyle", "null", "numericlocale", + "pager", "pager_min_lines", "recordsep", "recordsep_zero", + "tableattr", "title", "tuples_only", "unicode_border_linestyle", + "unicode_column_linestyle", "unicode_header_linestyle", NULL}; + + COMPLETE_WITH_LIST_CS(my_list); + } + else if (TailMatchesCS2("\\pset", MatchAny)) + { + if (TailMatchesCS1("format")) + { + static const char *const my_list[] = + {"unaligned", "aligned", "wrapped", "html", "asciidoc", + "latex", "latex-longtable", "troff-ms", NULL}; + + COMPLETE_WITH_LIST_CS(my_list); + } + else if (TailMatchesCS1("linestyle")) + COMPLETE_WITH_LIST_CS3("ascii", "old-ascii", "unicode"); + else if (TailMatchesCS1("pager")) + COMPLETE_WITH_LIST_CS3("on", "off", "always"); + else if (TailMatchesCS1("unicode_border_linestyle|" + "unicode_column_linestyle|" + "unicode_header_linestyle")) + COMPLETE_WITH_LIST_CS2("single", "double"); + } + else if (TailMatchesCS1("\\unset")) + matches = complete_from_variables(text, "", "", true); + else if (TailMatchesCS1("\\set")) + matches = complete_from_variables(text, "", "", false); + else if (TailMatchesCS2("\\set", MatchAny)) + { + if (TailMatchesCS1("AUTOCOMMIT|ON_ERROR_STOP|QUIET|" + "SINGLELINE|SINGLESTEP")) + COMPLETE_WITH_LIST_CS2("on", "off"); + else if (TailMatchesCS1("COMP_KEYWORD_CASE")) + COMPLETE_WITH_LIST_CS4("lower", "upper", + "preserve-lower", "preserve-upper"); + else if (TailMatchesCS1("ECHO")) + COMPLETE_WITH_LIST_CS4("errors", "queries", "all", "none"); + else if (TailMatchesCS1("ECHO_HIDDEN")) + COMPLETE_WITH_LIST_CS3("noexec", "off", "on"); + else if (TailMatchesCS1("HISTCONTROL")) + COMPLETE_WITH_LIST_CS4("ignorespace", "ignoredups", + "ignoreboth", "none"); + else if (TailMatchesCS1("ON_ERROR_ROLLBACK")) + COMPLETE_WITH_LIST_CS3("on", "off", "interactive"); + else if (TailMatchesCS1("SHOW_CONTEXT")) + COMPLETE_WITH_LIST_CS3("never", "errors", "always"); + else if (TailMatchesCS1("VERBOSITY")) + COMPLETE_WITH_LIST_CS3("default", "verbose", "terse"); + } + else if (TailMatchesCS1("\\sf*")) + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_functions, NULL); + else if (TailMatchesCS1("\\sv*")) + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_views, NULL); + else if (TailMatchesCS1("\\cd|\\e|\\edit|\\g|\\i|\\include|" + "\\ir|\\include_relative|\\o|\\out|" + "\\s|\\w|\\write|\\lo_import")) + { + completion_charp = "\\"; + matches = completion_matches(text, complete_from_files); + } + + /* + * Finally, we look through the list of "things", such as TABLE, INDEX and + * check if that was the previous word. If so, execute the query to get a + * list of them. + */ + else + { + int i; + + for (i = 0; words_after_create[i].name; i++) + { + if (pg_strcasecmp(prev_wd, words_after_create[i].name) == 0) + { + if (words_after_create[i].query) + COMPLETE_WITH_QUERY(words_after_create[i].query); + else if (words_after_create[i].squery) + COMPLETE_WITH_SCHEMA_QUERY(*words_after_create[i].squery, + NULL); + break; + } + } + } + + /* + * If we still don't have anything to match we have to fabricate some sort + * of default list. If we were to just return NULL, readline automatically + * attempts filename completion, and that's usually no good. + */ + if (matches == NULL) + { + COMPLETE_WITH_CONST(""); +#ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER + rl_completion_append_character = '\0'; +#endif + } + + /* free storage */ + free(previous_words); + free(words_buffer); + + /* Return our Grand List O' Matches */ + return matches; +} + + +/* + * GENERATOR FUNCTIONS + * + * These functions do all the actual work of completing the input. They get + * passed the text so far and the count how many times they have been called + * so far with the same text. + * If you read the above carefully, you'll see that these don't get called + * directly but through the readline interface. + * The return value is expected to be the full completion of the text, going + * through a list each time, or NULL if there are no more matches. The string + * will be free()'d by readline, so you must run it through strdup() or + * something of that sort. + */ + +/* + * Common routine for create_command_generator and drop_command_generator. + * Entries that have 'excluded' flags are not returned. + */ +static char * +create_or_drop_command_generator(const char *text, int state, bits32 excluded) +{ + static int list_index, + string_length; + const char *name; + + /* If this is the first time for this completion, init some values */ + if (state == 0) + { + list_index = 0; + string_length = strlen(text); + } + + /* find something that matches */ + while ((name = words_after_create[list_index++].name)) + { + if ((pg_strncasecmp(name, text, string_length) == 0) && + !(words_after_create[list_index - 1].flags & excluded)) + return pg_strdup_keyword_case(name, text); + } + /* if nothing matches, return NULL */ + return NULL; +} + +/* + * This one gives you one from a list of things you can put after CREATE + * as defined above. + */ +static char * +create_command_generator(const char *text, int state) +{ + return create_or_drop_command_generator(text, state, THING_NO_CREATE); +} + +/* + * This function gives you a list of things you can put after a DROP command. + */ +static char * +drop_command_generator(const char *text, int state) +{ + return create_or_drop_command_generator(text, state, THING_NO_DROP); +} + +/* + * This function gives you a list of things you can put after an ALTER command. + */ +static char * +alter_command_generator(const char *text, int state) +{ + return create_or_drop_command_generator(text, state, THING_NO_ALTER); +} + +/* The following two functions are wrappers for _complete_from_query */ + +static char * +complete_from_query(const char *text, int state) +{ + return _complete_from_query(0, text, state); +} + +static char * +complete_from_schema_query(const char *text, int state) +{ + return _complete_from_query(1, text, state); +} + + +/* + * This creates a list of matching things, according to a query pointed to + * by completion_charp. + * The query can be one of two kinds: + * + * 1. A simple query which must contain a %d and a %s, which will be replaced + * by the string length of the text and the text itself. The query may also + * have up to four more %s in it; the first two such will be replaced by the + * value of completion_info_charp, the next two by the value of + * completion_info_charp2. + * + * 2. A schema query used for completion of both schema and relation names. + * These are more complex and must contain in the following order: + * %d %s %d %s %d %s %s %d %s + * where %d is the string length of the text and %s the text itself. + * + * It is assumed that strings should be escaped to become SQL literals + * (that is, what is in the query is actually ... '%s' ...) + * + * See top of file for examples of both kinds of query. + */ +static char * +_complete_from_query(int is_schema_query, const char *text, int state) +{ + static int list_index, + byte_length; + static PGresult *result = NULL; + + /* + * If this is the first time for this completion, we fetch a list of our + * "things" from the backend. + */ + if (state == 0) + { + PQExpBufferData query_buffer; + char *e_text; + char *e_info_charp; + char *e_info_charp2; + const char *pstr = text; + int char_length = 0; + + list_index = 0; + byte_length = strlen(text); + + /* + * Count length as number of characters (not bytes), for passing to + * substring + */ + while (*pstr) + { + char_length++; + pstr += PQmblen(pstr, pset.encoding); + } + + /* Free any prior result */ + PQclear(result); + result = NULL; + + /* Set up suitably-escaped copies of textual inputs */ + e_text = escape_string(text); + + if (completion_info_charp) + e_info_charp = escape_string(completion_info_charp); + else + e_info_charp = NULL; + + if (completion_info_charp2) + e_info_charp2 = escape_string(completion_info_charp2); + else + e_info_charp2 = NULL; + + initPQExpBuffer(&query_buffer); + + if (is_schema_query) + { + /* completion_squery gives us the pieces to assemble */ + const char *qualresult = completion_squery->qualresult; + + if (qualresult == NULL) + qualresult = completion_squery->result; + + /* Get unqualified names matching the input-so-far */ + appendPQExpBuffer(&query_buffer, "SELECT %s FROM %s WHERE ", + completion_squery->result, + completion_squery->catname); + if (completion_squery->selcondition) + appendPQExpBuffer(&query_buffer, "%s AND ", + completion_squery->selcondition); + appendPQExpBuffer(&query_buffer, "substring(%s,1,%d)='%s'", + completion_squery->result, + char_length, e_text); + appendPQExpBuffer(&query_buffer, " AND %s", + completion_squery->viscondition); + + /* + * When fetching relation names, suppress system catalogs unless + * the input-so-far begins with "pg_". This is a compromise + * between not offering system catalogs for completion at all, and + * having them swamp the result when the input is just "p". + */ + if (strcmp(completion_squery->catname, + "pg_catalog.pg_class c") == 0 && + strncmp(text, "pg_", 3) !=0) + { + appendPQExpBufferStr(&query_buffer, + " AND c.relnamespace <> (SELECT oid FROM" + " pg_catalog.pg_namespace WHERE nspname = 'pg_catalog')"); + } + + /* + * Add in matching schema names, but only if there is more than + * one potential match among schema names. + */ + appendPQExpBuffer(&query_buffer, "\nUNION\n" + "SELECT pg_catalog.quote_ident(n.nspname) || '.' " + "FROM pg_catalog.pg_namespace n " + "WHERE substring(pg_catalog.quote_ident(n.nspname) || '.',1,%d)='%s'", + char_length, e_text); + appendPQExpBuffer(&query_buffer, + " AND (SELECT pg_catalog.count(*)" + " FROM pg_catalog.pg_namespace" + " WHERE substring(pg_catalog.quote_ident(nspname) || '.',1,%d) =" + " substring('%s',1,pg_catalog.length(pg_catalog.quote_ident(nspname))+1)) > 1", + char_length, e_text); + + /* + * Add in matching qualified names, but only if there is exactly + * one schema matching the input-so-far. + */ + appendPQExpBuffer(&query_buffer, "\nUNION\n" + "SELECT pg_catalog.quote_ident(n.nspname) || '.' || %s " + "FROM %s, pg_catalog.pg_namespace n " + "WHERE %s = n.oid AND ", + qualresult, + completion_squery->catname, + completion_squery->namespace); + if (completion_squery->selcondition) + appendPQExpBuffer(&query_buffer, "%s AND ", + completion_squery->selcondition); + appendPQExpBuffer(&query_buffer, "substring(pg_catalog.quote_ident(n.nspname) || '.' || %s,1,%d)='%s'", + qualresult, + char_length, e_text); + + /* + * This condition exploits the single-matching-schema rule to + * speed up the query + */ + appendPQExpBuffer(&query_buffer, + " AND substring(pg_catalog.quote_ident(n.nspname) || '.',1,%d) =" + " substring('%s',1,pg_catalog.length(pg_catalog.quote_ident(n.nspname))+1)", + char_length, e_text); + appendPQExpBuffer(&query_buffer, + " AND (SELECT pg_catalog.count(*)" + " FROM pg_catalog.pg_namespace" + " WHERE substring(pg_catalog.quote_ident(nspname) || '.',1,%d) =" + " substring('%s',1,pg_catalog.length(pg_catalog.quote_ident(nspname))+1)) = 1", + char_length, e_text); + + /* If an addon query was provided, use it */ + if (completion_charp) + appendPQExpBuffer(&query_buffer, "\n%s", completion_charp); + } + else + { + /* completion_charp is an sprintf-style format string */ + appendPQExpBuffer(&query_buffer, completion_charp, + char_length, e_text, + e_info_charp, e_info_charp, + e_info_charp2, e_info_charp2); + } + + /* Limit the number of records in the result */ + appendPQExpBuffer(&query_buffer, "\nLIMIT %d", + completion_max_records); + + result = exec_query(query_buffer.data); + + termPQExpBuffer(&query_buffer); + free(e_text); + if (e_info_charp) + free(e_info_charp); + if (e_info_charp2) + free(e_info_charp2); + } + + /* Find something that matches */ + if (result && PQresultStatus(result) == PGRES_TUPLES_OK) + { + const char *item; + + while (list_index < PQntuples(result) && + (item = PQgetvalue(result, list_index++, 0))) + if (pg_strncasecmp(text, item, byte_length) == 0) + return pg_strdup(item); + } + + /* If nothing matches, free the db structure and return null */ + PQclear(result); + result = NULL; + return NULL; +} + + +/* + * This function returns in order one of a fixed, NULL pointer terminated list + * of strings (if matching). This can be used if there are only a fixed number + * SQL words that can appear at certain spot. + */ +static char * +complete_from_list(const char *text, int state) +{ + static int string_length, + list_index, + matches; + static bool casesensitive; + const char *item; + + /* need to have a list */ + Assert(completion_charpp != NULL); + + /* Initialization */ + if (state == 0) + { + list_index = 0; + string_length = strlen(text); + casesensitive = completion_case_sensitive; + matches = 0; + } + + while ((item = completion_charpp[list_index++])) + { + /* First pass is case sensitive */ + if (casesensitive && strncmp(text, item, string_length) == 0) + { + matches++; + return pg_strdup(item); + } + + /* Second pass is case insensitive, don't bother counting matches */ + if (!casesensitive && pg_strncasecmp(text, item, string_length) == 0) + { + if (completion_case_sensitive) + return pg_strdup(item); + else + + /* + * If case insensitive matching was requested initially, + * adjust the case according to setting. + */ + return pg_strdup_keyword_case(item, text); + } + } + + /* + * No matches found. If we're not case insensitive already, lets switch to + * being case insensitive and try again + */ + if (casesensitive && matches == 0) + { + casesensitive = false; + list_index = 0; + state++; + return complete_from_list(text, state); + } + + /* If no more matches, return null. */ + return NULL; +} + + +/* + * This function returns one fixed string the first time even if it doesn't + * match what's there, and nothing the second time. This should be used if + * there is only one possibility that can appear at a certain spot, so + * misspellings will be overwritten. The string to be passed must be in + * completion_charp. + */ +static char * +complete_from_const(const char *text, int state) +{ + Assert(completion_charp != NULL); + if (state == 0) + { + if (completion_case_sensitive) + return pg_strdup(completion_charp); + else + + /* + * If case insensitive matching was requested initially, adjust + * the case according to setting. + */ + return pg_strdup_keyword_case(completion_charp, text); + } + else + return NULL; +} + + +/* + * This function appends the variable name with prefix and suffix to + * the variable names array. + */ +static void +append_variable_names(char ***varnames, int *nvars, + int *maxvars, const char *varname, + const char *prefix, const char *suffix) +{ + if (*nvars >= *maxvars) + { + *maxvars *= 2; + *varnames = (char **) pg_realloc(*varnames, + ((*maxvars) + 1) * sizeof(char *)); + } + + (*varnames)[(*nvars)++] = psprintf("%s%s%s", prefix, varname, suffix); +} + + +/* + * This function supports completion with the name of a psql variable. + * The variable names can be prefixed and suffixed with additional text + * to support quoting usages. If need_value is true, only variables + * that are currently set are included; otherwise, special variables + * (those that have hooks) are included even if currently unset. + */ +static char ** +complete_from_variables(const char *text, const char *prefix, const char *suffix, + bool need_value) +{ + char **matches; + char **varnames; + int nvars = 0; + int maxvars = 100; + int i; + struct _variable *ptr; + + varnames = (char **) pg_malloc((maxvars + 1) * sizeof(char *)); + + for (ptr = pset.vars->next; ptr; ptr = ptr->next) + { + if (need_value && !(ptr->value)) + continue; + append_variable_names(&varnames, &nvars, &maxvars, ptr->name, + prefix, suffix); + } + + varnames[nvars] = NULL; + COMPLETE_WITH_LIST_CS((const char *const *) varnames); + + for (i = 0; i < nvars; i++) + free(varnames[i]); + free(varnames); + + return matches; +} + + +/* + * This function wraps rl_filename_completion_function() to strip quotes from + * the input before searching for matches and to quote any matches for which + * the consuming command will require it. + */ +static char * +complete_from_files(const char *text, int state) +{ + static const char *unquoted_text; + char *unquoted_match; + char *ret = NULL; + + if (state == 0) + { + /* Initialization: stash the unquoted input. */ + unquoted_text = strtokx(text, "", NULL, "'", *completion_charp, + false, true, pset.encoding); + /* expect a NULL return for the empty string only */ + if (!unquoted_text) + { + Assert(*text == '\0'); + unquoted_text = text; + } + } + + unquoted_match = filename_completion_function(unquoted_text, state); + if (unquoted_match) + { + /* + * Caller sets completion_charp to a zero- or one-character string + * containing the escape character. This is necessary since \copy has + * no escape character, but every other backslash command recognizes + * "\" as an escape character. Since we have only two callers, don't + * bother providing a macro to simplify this. + */ + ret = quote_if_needed(unquoted_match, " \t\r\n\"`", + '\'', *completion_charp, pset.encoding); + if (ret) + free(unquoted_match); + else + ret = unquoted_match; + } + + return ret; +} + + +/* HELPER FUNCTIONS */ + + +/* + * Make a pg_strdup copy of s and convert the case according to + * COMP_KEYWORD_CASE setting, using ref as the text that was already entered. + */ +static char * +pg_strdup_keyword_case(const char *s, const char *ref) +{ + char *ret, + *p; + unsigned char first = ref[0]; + + ret = pg_strdup(s); + + if (pset.comp_case == PSQL_COMP_CASE_LOWER || + ((pset.comp_case == PSQL_COMP_CASE_PRESERVE_LOWER || + pset.comp_case == PSQL_COMP_CASE_PRESERVE_UPPER) && islower(first)) || + (pset.comp_case == PSQL_COMP_CASE_PRESERVE_LOWER && !isalpha(first))) + { + for (p = ret; *p; p++) + *p = pg_tolower((unsigned char) *p); + } + else + { + for (p = ret; *p; p++) + *p = pg_toupper((unsigned char) *p); + } + + return ret; +} + + +/* + * escape_string - Escape argument for use as string literal. + * + * The returned value has to be freed. + */ +static char * +escape_string(const char *text) +{ + size_t text_length; + char *result; + + text_length = strlen(text); + + result = pg_malloc(text_length * 2 + 1); + PQescapeStringConn(pset.db, result, text, text_length, NULL); + + return result; +} + + +/* + * Execute a query and report any errors. This should be the preferred way of + * talking to the database in this file. + */ +static PGresult * +exec_query(const char *query) +{ + PGresult *result; + + if (query == NULL || !pset.db || PQstatus(pset.db) != CONNECTION_OK) + return NULL; + + result = PQexec(pset.db, query); + + if (PQresultStatus(result) != PGRES_TUPLES_OK) + { +#ifdef NOT_USED + psql_error("tab completion query failed: %s\nQuery was:\n%s\n", + PQerrorMessage(pset.db), query); +#endif + PQclear(result); + result = NULL; + } + + return result; +} + + +/* + * Parse all the word(s) before point. + * + * Returns a malloc'd array of character pointers that point into the malloc'd + * data array returned to *buffer; caller must free() both of these when done. + * *nwords receives the number of words found, ie, the valid length of the + * return array. + * + * Words are returned right to left, that is, previous_words[0] gets the last + * word before point, previous_words[1] the next-to-last, etc. + */ +static char ** +get_previous_words(int point, char **buffer, int *nwords) +{ + char **previous_words; + char *buf; + char *outptr; + int words_found = 0; + int i; + + /* + * If we have anything in tab_completion_query_buf, paste it together with + * rl_line_buffer to construct the full query. Otherwise we can just use + * rl_line_buffer as the input string. + */ + if (tab_completion_query_buf && tab_completion_query_buf->len > 0) + { + i = tab_completion_query_buf->len; + buf = pg_malloc(point + i + 2); + memcpy(buf, tab_completion_query_buf->data, i); + buf[i++] = '\n'; + memcpy(buf + i, rl_line_buffer, point); + i += point; + buf[i] = '\0'; + /* Readjust point to reference appropriate offset in buf */ + point = i; + } + else + buf = rl_line_buffer; + + /* + * Allocate an array of string pointers and a buffer to hold the strings + * themselves. The worst case is that the line contains only + * non-whitespace WORD_BREAKS characters, making each one a separate word. + * This is usually much more space than we need, but it's cheaper than + * doing a separate malloc() for each word. + */ + previous_words = (char **) pg_malloc(point * sizeof(char *)); + *buffer = outptr = (char *) pg_malloc(point * 2); + + /* + * First we look for a non-word char before the current point. (This is + * probably useless, if readline is on the same page as we are about what + * is a word, but if so it's cheap.) + */ + for (i = point - 1; i >= 0; i--) + { + if (strchr(WORD_BREAKS, buf[i])) + break; + } + point = i; + + /* + * Now parse words, working backwards, until we hit start of line. The + * backwards scan has some interesting but intentional properties + * concerning parenthesis handling. + */ + while (point >= 0) + { + int start, + end; + bool inquotes = false; + int parentheses = 0; + + /* now find the first non-space which then constitutes the end */ + end = -1; + for (i = point; i >= 0; i--) + { + if (!isspace((unsigned char) buf[i])) + { + end = i; + break; + } + } + /* if no end found, we're done */ + if (end < 0) + break; + + /* + * Otherwise we now look for the start. The start is either the last + * character before any word-break character going backwards from the + * end, or it's simply character 0. We also handle open quotes and + * parentheses. + */ + for (start = end; start > 0; start--) + { + if (buf[start] == '"') + inquotes = !inquotes; + if (!inquotes) + { + if (buf[start] == ')') + parentheses++; + else if (buf[start] == '(') + { + if (--parentheses <= 0) + break; + } + else if (parentheses == 0 && + strchr(WORD_BREAKS, buf[start - 1])) + break; + } + } + + /* Return the word located at start to end inclusive */ + previous_words[words_found++] = outptr; + i = end - start + 1; + memcpy(outptr, &buf[start], i); + outptr += i; + *outptr++ = '\0'; + + /* Continue searching */ + point = start - 1; + } + + /* Release parsing input workspace, if we made one above */ + if (buf != rl_line_buffer) + free(buf); + + *nwords = words_found; + return previous_words; +} + +/* + * Look up the type for the GUC variable with the passed name. + * + * Returns NULL if the variable is unknown. Otherwise the returned string, + * containing the type, has to be freed. + */ +static char * +get_guctype(const char *varname) +{ + PQExpBufferData query_buffer; + char *e_varname; + PGresult *result; + char *guctype = NULL; + + e_varname = escape_string(varname); + + initPQExpBuffer(&query_buffer); + appendPQExpBuffer(&query_buffer, + "SELECT vartype FROM pg_catalog.pg_settings " + "WHERE pg_catalog.lower(name) = pg_catalog.lower('%s')", + e_varname); + + result = exec_query(query_buffer.data); + termPQExpBuffer(&query_buffer); + free(e_varname); + + if (PQresultStatus(result) == PGRES_TUPLES_OK && PQntuples(result) > 0) + guctype = pg_strdup(PQgetvalue(result, 0, 0)); + + PQclear(result); + + return guctype; +} + +#ifdef NOT_USED + +/* + * Surround a string with single quotes. This works for both SQL and + * psql internal. Currently disabled because it is reported not to + * cooperate with certain versions of readline. + */ +static char * +quote_file_name(char *text, int match_type, char *quote_pointer) +{ + char *s; + size_t length; + + (void) quote_pointer; /* not used */ + + length = strlen(text) +(match_type == SINGLE_MATCH ? 3 : 2); + s = pg_malloc(length); + s[0] = '\''; + strcpy(s + 1, text); + if (match_type == SINGLE_MATCH) + s[length - 2] = '\''; + s[length - 1] = '\0'; + return s; +} + +static char * +dequote_file_name(char *text, char quote_char) +{ + char *s; + size_t length; + + if (!quote_char) + return pg_strdup(text); + + length = strlen(text); + s = pg_malloc(length - 2 + 1); + strlcpy(s, text +1, length - 2 + 1); + + return s; +} +#endif /* NOT_USED */ + +#endif /* USE_READLINE */ diff --git a/src/bin/psql/tab-complete-pipelinedb.h b/src/bin/psql/tab-complete-pipelinedb.h new file mode 100644 index 0000000000000..af2597892db6c --- /dev/null +++ b/src/bin/psql/tab-complete-pipelinedb.h @@ -0,0 +1,17 @@ +/* + * psql - the PostgreSQL interactive terminal + * + * Copyright (c) 2000-2017, PostgreSQL Global Development Group + * + * src/bin/psql/tab-complete-pipelinedb.h + */ +#ifndef TAB_COMPLETE_PIPELINEDB_H +#define TAB_COMPLETE_PIPELINEDB_H + +#include "pqexpbuffer.h" + +extern PQExpBuffer tab_completion_query_buf; + +extern void initialize_readline_pipelinedb(void); + +#endif /* TAB_COMPLETE_PIPELINEDB_H */ From 4a38fdb378cd4b431b11775f59c5808a3bf68f73 Mon Sep 17 00:00:00 2001 From: Robins Tharakan Date: Fri, 15 Sep 2017 22:12:16 +1000 Subject: [PATCH 100/149] Remove a lot of excess code from the files that are recently split. More to be removed soon --- src/bin/psql/tab-complete-pipelinedb.c | 93 -------- src/bin/psql/tab-complete-redshift.c | 5 - src/bin/psql/tab-complete.c | 288 +++---------------------- 3 files changed, 25 insertions(+), 361 deletions(-) diff --git a/src/bin/psql/tab-complete-pipelinedb.c b/src/bin/psql/tab-complete-pipelinedb.c index 21903f5f88632..3312f2230e5b3 100644 --- a/src/bin/psql/tab-complete-pipelinedb.c +++ b/src/bin/psql/tab-complete-pipelinedb.c @@ -1999,36 +1999,6 @@ psql_completion(const char *text, int start, int end) COMPLETE_WITH_LIST(list_ALTER2); } - /* ALTER TABLE xxx ADD */ - else if (IS_REDSHIFT && Matches4("ALTER", "TABLE", MatchAny, "ADD")) - COMPLETE_WITH_LIST2("COLUMN", "PARTITION"); - - else if (IS_REDSHIFT && HeadMatches6("ALTER", "TABLE", MatchAny, "ADD", MatchAny, MatchAny)) - { - if (TailMatches2("COLUMN", MatchAny)) - COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_datatypes, NULL); - else - { - if (TailMatches2("DEFAULT", MatchAny)) - COMPLETE_WITH_LIST3("ENCODE", "NOT NULL", "NULL"); - else if (TailMatches1("ENCODE")) - COMPLETE_WITH_QUERY(Query_for_list_of_column_compressions); - else if (TailMatches2("ENCODE", MatchAny)) - COMPLETE_WITH_LIST3("DEFAULT", "NOT NULL", "NULL"); - else if (TailMatches1("NULL")) - COMPLETE_WITH_LIST2("ENCODE", "DEFAULT"); - else if (TailMatches1("NOT")) - COMPLETE_WITH_CONST("NULL"); - else - if (!TailMatches1("DEFAULT")) - COMPLETE_WITH_LIST4("DEFAULT", "ENCODE", "NOT NULL", "NULL"); - } - } - - else if (IS_REDSHIFT && HeadMatches5("ALTER", "TABLE", MatchAny, "ADD", MatchAny) && - (!TailMatches1("COLUMN"))) - COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_datatypes, NULL); - /* ALTER TABLE xxx ENABLE */ else if (Matches4("ALTER", "TABLE", MatchAny, "ENABLE")) COMPLETE_WITH_LIST5("ALWAYS", "REPLICA", "ROW LEVEL SECURITY", "RULE", @@ -2325,9 +2295,6 @@ psql_completion(const char *text, int start, int end) completion_info_charp = prev2_wd; COMPLETE_WITH_QUERY(Query_for_index_of_table); } -/* CANCEL */ - else if (IS_REDSHIFT && Matches1("CANCEL")) - COMPLETE_WITH_QUERY(Query_for_list_of_pids); /* CLOSE */ else if (Matches1("CLOSE")) @@ -2413,11 +2380,6 @@ psql_completion(const char *text, int start, int end) COMPLETE_WITH_LIST5("HEADER", "QUOTE", "ESCAPE", "FORCE QUOTE", "FORCE NOT NULL"); - /* COPY FROM REGION */ - else if (IS_REDSHIFT && HeadMatches2("COPY", MatchAny) && TailMatches1("REGION")) - COMPLETE_WITH_QUERY(Query_for_list_of_aws_regions); - - /* CREATE ACCESS METHOD */ /* Complete "CREATE ACCESS METHOD " */ else if (Matches4("CREATE", "ACCESS", "METHOD", MatchAny)) @@ -2478,61 +2440,6 @@ psql_completion(const char *text, int start, int end) COMPLETE_WITH_QUERY(Query_for_list_of_available_extension_versions); } - else if (IS_REDSHIFT && (Matches2("CREATE", "EXTERNAL"))) - COMPLETE_WITH_LIST2("SCHEMA", "TABLE"); - - /* CREATE EXTERNAL SCHEMA */ - else if (IS_REDSHIFT && (Matches3("CREATE", "EXTERNAL", "SCHEMA"))) { - if (Matches2("CREATE", "EXTERNAL")) - COMPLETE_WITH_LIST2("SCHEMA", "TABLE"); - else if (Matches3("CREATE", "EXTERNAL", "SCHEMA")) - COMPLETE_WITH_QUERY(Query_for_list_of_external_schemas - " UNION ALL SELECT 'IF NOT EXISTS'"); - else if ( - (Matches7("CREATE", "EXTERNAL", "SCHEMA", "IF", "NOT", "EXISTS", MatchAnyExcept("FROM"))) || - (Matches4("CREATE", "EXTERNAL", "SCHEMA", MatchAnyExcept("FROM"))) - ) - COMPLETE_WITH_CONST("FROM"); - else if (HeadMatches3("CREATE", "EXTERNAL", "SCHEMA") && TailMatches1("FROM")) - COMPLETE_WITH_LIST2("DATA CATALOG", "HIVE METASTORE"); - else if (HeadMatches3("CREATE", "EXTERNAL", "SCHEMA") && - (TailMatches2("DATA", "CATALOG") || TailMatches2("HIVE", "METASTORE")) - ) - COMPLETE_WITH_CONST("DATABASE"); - else if (HeadMatches3("CREATE", "EXTERNAL", "SCHEMA") && TailMatches2("DATABASE", MatchAny)) - COMPLETE_WITH_LIST4("REGION", "URI", "IAM_ROLE", "CREATE EXTERNAL DATABASE IF NOT EXISTS"); - else if (HeadMatches3("CREATE", "EXTERNAL", "SCHEMA") && TailMatches1("REGION")) - COMPLETE_WITH_QUERY(Query_for_list_of_aws_regions); - else if (HeadMatches3("CREATE", "EXTERNAL", "SCHEMA") && TailMatches2("REGION", MatchAny)) - COMPLETE_WITH_LIST3("URI", "IAM_ROLE", "CREATE EXTERNAL DATABASE IF NOT EXISTS"); - else if (HeadMatches3("CREATE", "EXTERNAL", "SCHEMA") && TailMatches2("URI", MatchAny)) - COMPLETE_WITH_LIST3("PORT", "IAM_ROLE", "CREATE EXTERNAL DATABASE IF NOT EXISTS"); - else if (HeadMatches3("CREATE", "EXTERNAL", "SCHEMA") && TailMatches2("PORT", MatchAny)) - COMPLETE_WITH_LIST2("IAM_ROLE", "CREATE EXTERNAL DATABASE IF NOT EXISTS"); - else if (HeadMatches3("CREATE", "EXTERNAL", "SCHEMA") && TailMatches2("IAM_ROLE", MatchAny)) - COMPLETE_WITH_CONST("CREATE EXTERNAL DATABASE IF NOT EXISTS"); - } - - /* CREATE EXTERNAL TABLE */ - else if (IS_REDSHIFT && (Matches3("CREATE", "EXTERNAL", "TABLE"))) { - if (Matches3("CREATE", "EXTERNAL", "TABLE")) - COMPLETE_WITH_QUERY(Query_for_list_of_external_schemas); - else if ((HeadMatches3("CREATE", "EXTERNAL", "TABLE")) && TailMatches1("PARTITIONED")) - COMPLETE_WITH_CONST("BY ("); - else if ((HeadMatches3("CREATE", "EXTERNAL", "TABLE")) && TailMatches1("ROW")) - COMPLETE_WITH_LIST2("FORMAT", "DELIMITED"); - else if ((HeadMatches3("CREATE", "EXTERNAL", "TABLE")) && TailMatches3("ROW", "FORMAT", "DELIMITED")) - COMPLETE_WITH_LIST2("FIELDS TERMINATED BY", "LINES TERMINATED BY"); - else if ((HeadMatches3("CREATE", "EXTERNAL", "TABLE")) && TailMatches3("ROW", "FORMAT", "DELIMITED")) - COMPLETE_WITH_LIST2("FIELDS TERMINATED BY", "LINES TERMINATED BY"); - else if ((HeadMatches3("CREATE", "EXTERNAL", "TABLE")) && TailMatches2("STORED", "AS")) - COMPLETE_WITH_LIST5("PARQUET", "RCFILE", "SEQUENCEFILE", "TEXTFILE", "ORC"); - else if ((HeadMatches3("CREATE", "EXTERNAL", "TABLE")) && TailMatches1("PROPERTIES")) - COMPLETE_WITH_CONST("('numRows'='"); - else if (HeadMatches4("CREATE", "EXTERNAL", "TABLE", MatchAny)) - COMPLETE_WITH_LIST5("PARTITIONED BY (", "ROW FORMAT DELIMITED", "STORED AS", "LOCATION", "TABLE PROPERTIES ('numRows'='"); - } - /* CREATE FOREIGN */ else if (Matches2("CREATE", "FOREIGN")) COMPLETE_WITH_LIST2("DATA WRAPPER", "TABLE"); diff --git a/src/bin/psql/tab-complete-redshift.c b/src/bin/psql/tab-complete-redshift.c index 28461459d2c61..7ad99532475a3 100644 --- a/src/bin/psql/tab-complete-redshift.c +++ b/src/bin/psql/tab-complete-redshift.c @@ -1591,7 +1591,6 @@ psql_completion(const char *text, int start, int end) Any change here should probably be replicated elsewhere since #define for various SQLs (in this script) employ there own string-compare */ #define IS_REDSHIFT (strncmp(pset.sengine, "redshift", 8) == 0) - #define IS_PIPELINEDB (strncmp(pset.sengine, "pipelinedb", 10) == 0) /* Clear a few things. */ completion_charp = NULL; @@ -2967,10 +2966,6 @@ psql_completion(const char *text, int start, int end) else if (Matches5("CREATE", "EVENT", "TRIGGER", MatchAny, "ON")) COMPLETE_WITH_LIST3("ddl_command_start", "ddl_command_end", "sql_drop"); -/* CREATE CONTINUOUS TRANSFORM / VIEW */ - else if ((IS_PIPELINEDB) && (Matches2("CREATE|DROP", "CONTINUOUS"))) - COMPLETE_WITH_LIST2("TRANSFORM", "VIEW"); - /* DEALLOCATE */ else if (Matches1("DEALLOCATE")) COMPLETE_WITH_QUERY(Query_for_list_of_prepared_statements " UNION SELECT 'PREPARE'"); diff --git a/src/bin/psql/tab-complete.c b/src/bin/psql/tab-complete.c index 9e1b9f953f0a3..32860b90e7fb2 100644 --- a/src/bin/psql/tab-complete.c +++ b/src/bin/psql/tab-complete.c @@ -314,18 +314,6 @@ do { \ COMPLETE_WITH_LIST(list); \ } while (0) -#define COMPLETE_WITH_LIST11(s1, s2, s3, s4, s5, s6, s7, s8, s9, s10, s11) \ -do { \ - static const char *const list[] = { s1, s2, s3, s4, s5, s6, s7, s8, s9, s10, s11, NULL }; \ - COMPLETE_WITH_LIST(list); \ -} while (0) - -#define COMPLETE_WITH_LIST12(s1, s2, s3, s4, s5, s6, s7, s8, s9, s10, s11, s12) \ -do { \ - static const char *const list[] = { s1, s2, s3, s4, s5, s6, s7, s8, s9, s10, s11, s12, NULL }; \ - COMPLETE_WITH_LIST(list); \ -} while (0) - /* * Likewise for COMPLETE_WITH_LIST_CS. */ @@ -682,40 +670,8 @@ static const SchemaQuery Query_for_list_of_statistics = { " AND (pg_catalog.quote_ident(nspname)='%s' "\ " OR '\"' || nspname || '\"' ='%s') " -#define Query_for_list_of_aws_regions \ -"SELECT '''ap-northeast-1''' UNION ALL "\ -"SELECT '''ap-northeast-2''' UNION ALL "\ -"SELECT '''ap-south-1''' UNION ALL "\ -"SELECT '''ap-southeast-1''' UNION ALL "\ -"SELECT '''ap-southeast-2''' UNION ALL "\ -"SELECT '''ca-central-1''' UNION ALL "\ -"SELECT '''eu-central-1''' UNION ALL "\ -"SELECT '''eu-west-1''' UNION ALL "\ -"SELECT '''eu-west-2''' UNION ALL "\ -"SELECT '''sa-east-1''' UNION ALL "\ -"SELECT '''us-east-1''' UNION ALL "\ -"SELECT '''us-east-2''' UNION ALL "\ -"SELECT '''us-west-1''' UNION ALL "\ -"SELECT '''us-west-2''' " - -#define Query_for_list_of_column_compressions \ -" SELECT 'BYTEDICT' UNION ALL "\ -" SELECT 'DELTA' UNION ALL "\ -" SELECT 'DELTA32K' UNION ALL "\ -" SELECT 'LZO' UNION ALL "\ -" SELECT 'MOSTLY8' UNION ALL "\ -" SELECT 'MOSTLY16' UNION ALL "\ -" SELECT 'MOSTLY32' UNION ALL "\ -" SELECT 'RAW' UNION ALL "\ -" SELECT 'RUNLENGTH' UNION ALL "\ -" SELECT 'TEXT255' UNION ALL "\ -" SELECT 'TEXT32K' UNION ALL "\ -" SELECT 'ZST' " - #define Query_for_list_of_cursors \ -((strncmp(pset.sengine, "redshift", 8) == 0) ? \ - " SELECT trim(name) FROM stv_active_cursors ORDER BY 1" : \ - " SELECT trim(name) FROM pg_cursors ORDER BY 1" ) +" SELECT trim(name) FROM pg_cursors ORDER BY 1" #define Query_for_list_of_enum_values \ "SELECT pg_catalog.quote_literal(enumlabel) "\ @@ -766,14 +722,6 @@ static const SchemaQuery Query_for_list_of_statistics = { "SELECT pg_catalog.quote_ident(nspname) FROM pg_catalog.pg_namespace "\ " WHERE substring(pg_catalog.quote_ident(nspname),1,%d)='%s'" -#define Query_for_list_of_external_schemas \ -"SELECT pg_catalog.quote_ident(schemaname) FROM SVV_EXTERNAL_SCHEMAS "\ -" WHERE substring(pg_catalog.quote_ident(schemaname),1,%d)='%s'" - -#define Query_for_list_of_external_tables \ -"SELECT pg_catalog.quote_ident(tablename) FROM SVV_EXTERNAL_TABLES "\ -" WHERE substring(pg_catalog.quote_ident(tablename),1,%d)='%s'" - #define Query_for_list_of_alter_system_set_vars \ "SELECT name FROM "\ " (SELECT pg_catalog.lower(name) AS name FROM pg_catalog.pg_settings "\ @@ -782,9 +730,7 @@ static const SchemaQuery Query_for_list_of_statistics = { " WHERE substring(name,1,%d)='%s'" #define Query_for_list_of_pids \ -((strncmp(pset.sengine, "redshift", 8) == 0) ? \ - "SELECT procpid || ' (user->' || usename || '; database->' || datname || ')' FROM pg_stat_activity ORDER BY datname, usename" : \ - "SELECT process || ' (user->' || usename || '; database->' || datname || ')' FROM pg_stat_activity ORDER BY datname, usename") +"SELECT process || ' (user->' || usename || '; database->' || datname || ')' FROM pg_stat_activity ORDER BY datname, usename" #define Query_for_list_of_set_vars \ "SELECT name FROM "\ @@ -1082,8 +1028,6 @@ static const pgsql_thing_t words_after_create[] = { * to be used only by pg_dump. */ {"CONFIGURATION", Query_for_list_of_ts_configurations, NULL, THING_NO_SHOW}, - {"CONTINUOUS TRANSFORM", NULL, NULL, THING_NO_ALTER}, - {"CONTINUOUS VIEW", NULL, NULL, THING_NO_ALTER}, {"CONVERSION", "SELECT pg_catalog.quote_ident(conname) FROM pg_catalog.pg_conversion WHERE substring(pg_catalog.quote_ident(conname),1,%d)='%s'"}, {"DATABASE", Query_for_list_of_databases}, {"DEFAULT PRIVILEGES", NULL, NULL, THING_NO_CREATE | THING_NO_DROP}, @@ -1091,8 +1035,6 @@ static const pgsql_thing_t words_after_create[] = { {"DOMAIN", NULL, &Query_for_list_of_domains}, {"EVENT TRIGGER", NULL, NULL}, {"EXTENSION", Query_for_list_of_extensions}, - {"EXTERNAL SCHEMA", Query_for_list_of_external_schemas}, - {"EXTERNAL TABLE", Query_for_list_of_external_tables}, {"FOREIGN DATA WRAPPER", NULL, NULL}, {"FOREIGN TABLE", NULL, NULL}, {"FUNCTION", NULL, &Query_for_list_of_functions}, @@ -1118,7 +1060,6 @@ static const pgsql_thing_t words_after_create[] = { {"SEQUENCE", NULL, &Query_for_list_of_sequences}, {"SERVER", Query_for_list_of_servers}, {"STATISTICS", NULL, &Query_for_list_of_statistics}, - {"STREAM", NULL, NULL}, {"SUBSCRIPTION", Query_for_list_of_subscriptions}, {"SYSTEM", NULL, NULL, THING_NO_CREATE | THING_NO_DROP}, {"TABLE", NULL, &Query_for_list_of_tables}, @@ -1485,66 +1426,9 @@ psql_completion(const char *text, int start, int end) word_matches(p2, previous_words[previous_words_count - 2]) && \ word_matches(p3, previous_words[previous_words_count - 3])) -#define HeadMatches4(p1, p2, p3, p4) \ - (previous_words_count >= 4 && \ - word_matches(p1, previous_words[previous_words_count - 1]) && \ - word_matches(p2, previous_words[previous_words_count - 2]) && \ - word_matches(p3, previous_words[previous_words_count - 3]) && \ - word_matches(p4, previous_words[previous_words_count - 4])) - -#define HeadMatches5(p1, p2, p3, p4, p5) \ - (previous_words_count >= 5 && \ - word_matches(p1, previous_words[previous_words_count - 1]) && \ - word_matches(p2, previous_words[previous_words_count - 2]) && \ - word_matches(p3, previous_words[previous_words_count - 3]) && \ - word_matches(p4, previous_words[previous_words_count - 4]) && \ - word_matches(p5, previous_words[previous_words_count - 5])) - -#define HeadMatches6(p1, p2, p3, p4, p5, p6) \ - (previous_words_count >= 6 && \ - word_matches(p1, previous_words[previous_words_count - 1]) && \ - word_matches(p2, previous_words[previous_words_count - 2]) && \ - word_matches(p3, previous_words[previous_words_count - 3]) && \ - word_matches(p4, previous_words[previous_words_count - 4]) && \ - word_matches(p5, previous_words[previous_words_count - 5]) && \ - word_matches(p6, previous_words[previous_words_count - 6])) - -#define HeadMatches7(p1, p2, p3, p4, p5, p6, p7) \ - (previous_words_count >= 7 && \ - word_matches(p1, previous_words[previous_words_count - 1]) && \ - word_matches(p2, previous_words[previous_words_count - 2]) && \ - word_matches(p3, previous_words[previous_words_count - 3]) && \ - word_matches(p4, previous_words[previous_words_count - 4]) && \ - word_matches(p5, previous_words[previous_words_count - 5]) && \ - word_matches(p6, previous_words[previous_words_count - 6]) && \ - word_matches(p7, previous_words[previous_words_count - 7])) - -#define HeadMatches8(p1, p2, p3, p4, p5, p6, p7, p8) \ -(previous_words_count >= 8 && \ - word_matches(p1, previous_words[previous_words_count - 1]) && \ - word_matches(p2, previous_words[previous_words_count - 2]) && \ - word_matches(p3, previous_words[previous_words_count - 3]) && \ - word_matches(p4, previous_words[previous_words_count - 4]) && \ - word_matches(p5, previous_words[previous_words_count - 5]) && \ - word_matches(p6, previous_words[previous_words_count - 6]) && \ - word_matches(p7, previous_words[previous_words_count - 7]) && \ - word_matches(p8, previous_words[previous_words_count - 8])) - -#define HeadMatches9(p1, p2, p3, p4, p5, p6, p7, p8, p9) \ -(previous_words_count >= 9 && \ - word_matches(p1, previous_words[previous_words_count - 1]) && \ - word_matches(p2, previous_words[previous_words_count - 2]) && \ - word_matches(p3, previous_words[previous_words_count - 3]) && \ - word_matches(p4, previous_words[previous_words_count - 4]) && \ - word_matches(p5, previous_words[previous_words_count - 5]) && \ - word_matches(p6, previous_words[previous_words_count - 6]) && \ - word_matches(p7, previous_words[previous_words_count - 7]) && \ - word_matches(p8, previous_words[previous_words_count - 8]) && \ - word_matches(p9, previous_words[previous_words_count - 9])) - /* Known command-starting keywords. */ static const char *const sql_commands[] = { - "ABORT", "ALTER", "ANALYZE", "BEGIN", "CANCEL", "CHECKPOINT", "CLOSE", "CLUSTER", + "ABORT", "ALTER", "ANALYZE", "BEGIN", "CHECKPOINT", "CLOSE", "CLUSTER", "COMMENT", "COMMIT", "COPY", "CREATE", "DEALLOCATE", "DECLARE", "DELETE FROM", "DISCARD", "DO", "DROP", "END", "EXECUTE", "EXPLAIN", "FETCH", "GRANT", "IMPORT", "INSERT", "LISTEN", "LOAD", "LOCK", @@ -1552,7 +1436,7 @@ psql_completion(const char *text, int start, int end) "REASSIGN", "REFRESH MATERIALIZED VIEW", "REINDEX", "RELEASE", "RESET", "REVOKE", "ROLLBACK", "SAVEPOINT", "SECURITY LABEL", "SELECT", "SET", "SHOW", "START", - "TABLE", "TRUNCATE", "UNLISTEN", "UNLOAD", "UPDATE", "VACUUM", "VALUES", "WITH", + "TABLE", "TRUNCATE", "UNLISTEN", "UPDATE", "VACUUM", "VALUES", "WITH", NULL }; @@ -1594,12 +1478,6 @@ psql_completion(const char *text, int start, int end) rl_completion_append_character = ' '; #endif -/* Macro to easily identify which Engine (type) are we speaking to - Any change here should probably be replicated elsewhere since - #define for various SQLs (in this script) employ there own string-compare */ - #define IS_REDSHIFT (strncmp(pset.sengine, "redshift", 8) == 0) - #define IS_PIPELINEDB (strncmp(pset.sengine, "pipelinedb", 10) == 0) - /* Clear a few things. */ completion_charp = NULL; completion_charpp = NULL; @@ -1823,9 +1701,9 @@ psql_completion(const char *text, int start, int end) !TailMatches2("USER", "MAPPING")) { static const char *const list_ALTERUSER[] = - {"BYPASSRLS", "CONNECTION LIMIT", "CREATEDB", "CREATEROLE", "CREATEUSER", + {"BYPASSRLS", "CONNECTION LIMIT", "CREATEDB", "CREATEROLE", "ENCRYPTED PASSWORD", "INHERIT", "LOGIN", "NOBYPASSRLS", - "NOCREATEDB", "NOCREATEROLE", "NOCREATEUSER", "NOINHERIT", + "NOCREATEDB", "NOCREATEROLE", "NOINHERIT", "NOLOGIN", "NOREPLICATION", "NOSUPERUSER", "PASSWORD", "RENAME TO", "REPLICATION", "RESET", "SET", "SUPERUSER", "VALID UNTIL", "WITH", NULL}; @@ -2003,44 +1881,13 @@ psql_completion(const char *text, int start, int end) else if (Matches3("ALTER", "TABLE", MatchAny)) { static const char *const list_ALTER2[] = - {"ADD", "ALTER", "APPEND FROM", "CLUSTER ON", "DISABLE", "DROP", "ENABLE", "INHERIT", + {"ADD", "ALTER", "CLUSTER ON", "DISABLE", "DROP", "ENABLE", "INHERIT", "NO INHERIT", "RENAME", "RESET", "OWNER TO", "SET", "VALIDATE CONSTRAINT", "REPLICA IDENTITY", "ATTACH PARTITION", "DETACH PARTITION", NULL}; COMPLETE_WITH_LIST(list_ALTER2); } - - /* ALTER TABLE xxx ADD */ - else if (IS_REDSHIFT && Matches4("ALTER", "TABLE", MatchAny, "ADD")) - COMPLETE_WITH_LIST2("COLUMN", "PARTITION"); - - else if (IS_REDSHIFT && HeadMatches6("ALTER", "TABLE", MatchAny, "ADD", MatchAny, MatchAny)) - { - if (TailMatches2("COLUMN", MatchAny)) - COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_datatypes, NULL); - else - { - if (TailMatches2("DEFAULT", MatchAny)) - COMPLETE_WITH_LIST3("ENCODE", "NOT NULL", "NULL"); - else if (TailMatches1("ENCODE")) - COMPLETE_WITH_QUERY(Query_for_list_of_column_compressions); - else if (TailMatches2("ENCODE", MatchAny)) - COMPLETE_WITH_LIST3("DEFAULT", "NOT NULL", "NULL"); - else if (TailMatches1("NULL")) - COMPLETE_WITH_LIST2("ENCODE", "DEFAULT"); - else if (TailMatches1("NOT")) - COMPLETE_WITH_CONST("NULL"); - else - if (!TailMatches1("DEFAULT")) - COMPLETE_WITH_LIST4("DEFAULT", "ENCODE", "NOT NULL", "NULL"); - } - } - - else if (IS_REDSHIFT && HeadMatches5("ALTER", "TABLE", MatchAny, "ADD", MatchAny) && - (!TailMatches1("COLUMN"))) - COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_datatypes, NULL); - /* ALTER TABLE xxx ENABLE */ else if (Matches4("ALTER", "TABLE", MatchAny, "ENABLE")) COMPLETE_WITH_LIST5("ALWAYS", "REPLICA", "ROW LEVEL SECURITY", "RULE", @@ -2107,7 +1954,7 @@ psql_completion(const char *text, int start, int end) /* If we have ALTER TABLE DROP, provide COLUMN or CONSTRAINT */ else if (Matches4("ALTER", "TABLE", MatchAny, "DROP")) - COMPLETE_WITH_LIST3("COLUMN", "CONSTRAINT", "PARTITION"); + COMPLETE_WITH_LIST3("COLUMN", "CONSTRAINT"); /* If we have ALTER TABLE DROP COLUMN, provide list of columns */ else if (Matches5("ALTER", "TABLE", MatchAny, "DROP", "COLUMN")) COMPLETE_WITH_ATTR(prev3_wd, ""); @@ -2150,7 +1997,7 @@ psql_completion(const char *text, int start, int end) } /* If we have ALTER TABLE SET, provide list of attributes and '(' */ else if (Matches4("ALTER", "TABLE", MatchAny, "SET")) - COMPLETE_WITH_LIST10("(", "LOGGED", "LOCATION", "FILE FORMAT", "TABLE PROPERTIES", "SCHEMA", "TABLESPACE", "UNLOGGED", + COMPLETE_WITH_LIST10("(", "LOGGED", "SCHEMA", "TABLESPACE", "UNLOGGED", "WITH", "WITHOUT"); /* @@ -2337,9 +2184,6 @@ psql_completion(const char *text, int start, int end) completion_info_charp = prev2_wd; COMPLETE_WITH_QUERY(Query_for_index_of_table); } -/* CANCEL */ - else if (IS_REDSHIFT && Matches1("CANCEL")) - COMPLETE_WITH_QUERY(Query_for_list_of_pids); /* CLOSE */ else if (Matches1("CLOSE")) @@ -2425,11 +2269,6 @@ psql_completion(const char *text, int start, int end) COMPLETE_WITH_LIST5("HEADER", "QUOTE", "ESCAPE", "FORCE QUOTE", "FORCE NOT NULL"); - /* COPY FROM REGION */ - else if (IS_REDSHIFT && HeadMatches2("COPY", MatchAny) && TailMatches1("REGION")) - COMPLETE_WITH_QUERY(Query_for_list_of_aws_regions); - - /* CREATE ACCESS METHOD */ /* Complete "CREATE ACCESS METHOD " */ else if (Matches4("CREATE", "ACCESS", "METHOD", MatchAny)) @@ -2490,61 +2329,6 @@ psql_completion(const char *text, int start, int end) COMPLETE_WITH_QUERY(Query_for_list_of_available_extension_versions); } - else if (IS_REDSHIFT && (Matches2("CREATE", "EXTERNAL"))) - COMPLETE_WITH_LIST2("SCHEMA", "TABLE"); - - /* CREATE EXTERNAL SCHEMA */ - else if (IS_REDSHIFT && (Matches3("CREATE", "EXTERNAL", "SCHEMA"))) { - if (Matches2("CREATE", "EXTERNAL")) - COMPLETE_WITH_LIST2("SCHEMA", "TABLE"); - else if (Matches3("CREATE", "EXTERNAL", "SCHEMA")) - COMPLETE_WITH_QUERY(Query_for_list_of_external_schemas - " UNION ALL SELECT 'IF NOT EXISTS'"); - else if ( - (Matches7("CREATE", "EXTERNAL", "SCHEMA", "IF", "NOT", "EXISTS", MatchAnyExcept("FROM"))) || - (Matches4("CREATE", "EXTERNAL", "SCHEMA", MatchAnyExcept("FROM"))) - ) - COMPLETE_WITH_CONST("FROM"); - else if (HeadMatches3("CREATE", "EXTERNAL", "SCHEMA") && TailMatches1("FROM")) - COMPLETE_WITH_LIST2("DATA CATALOG", "HIVE METASTORE"); - else if (HeadMatches3("CREATE", "EXTERNAL", "SCHEMA") && - (TailMatches2("DATA", "CATALOG") || TailMatches2("HIVE", "METASTORE")) - ) - COMPLETE_WITH_CONST("DATABASE"); - else if (HeadMatches3("CREATE", "EXTERNAL", "SCHEMA") && TailMatches2("DATABASE", MatchAny)) - COMPLETE_WITH_LIST4("REGION", "URI", "IAM_ROLE", "CREATE EXTERNAL DATABASE IF NOT EXISTS"); - else if (HeadMatches3("CREATE", "EXTERNAL", "SCHEMA") && TailMatches1("REGION")) - COMPLETE_WITH_QUERY(Query_for_list_of_aws_regions); - else if (HeadMatches3("CREATE", "EXTERNAL", "SCHEMA") && TailMatches2("REGION", MatchAny)) - COMPLETE_WITH_LIST3("URI", "IAM_ROLE", "CREATE EXTERNAL DATABASE IF NOT EXISTS"); - else if (HeadMatches3("CREATE", "EXTERNAL", "SCHEMA") && TailMatches2("URI", MatchAny)) - COMPLETE_WITH_LIST3("PORT", "IAM_ROLE", "CREATE EXTERNAL DATABASE IF NOT EXISTS"); - else if (HeadMatches3("CREATE", "EXTERNAL", "SCHEMA") && TailMatches2("PORT", MatchAny)) - COMPLETE_WITH_LIST2("IAM_ROLE", "CREATE EXTERNAL DATABASE IF NOT EXISTS"); - else if (HeadMatches3("CREATE", "EXTERNAL", "SCHEMA") && TailMatches2("IAM_ROLE", MatchAny)) - COMPLETE_WITH_CONST("CREATE EXTERNAL DATABASE IF NOT EXISTS"); - } - - /* CREATE EXTERNAL TABLE */ - else if (IS_REDSHIFT && (Matches3("CREATE", "EXTERNAL", "TABLE"))) { - if (Matches3("CREATE", "EXTERNAL", "TABLE")) - COMPLETE_WITH_QUERY(Query_for_list_of_external_schemas); - else if ((HeadMatches3("CREATE", "EXTERNAL", "TABLE")) && TailMatches1("PARTITIONED")) - COMPLETE_WITH_CONST("BY ("); - else if ((HeadMatches3("CREATE", "EXTERNAL", "TABLE")) && TailMatches1("ROW")) - COMPLETE_WITH_LIST2("FORMAT", "DELIMITED"); - else if ((HeadMatches3("CREATE", "EXTERNAL", "TABLE")) && TailMatches3("ROW", "FORMAT", "DELIMITED")) - COMPLETE_WITH_LIST2("FIELDS TERMINATED BY", "LINES TERMINATED BY"); - else if ((HeadMatches3("CREATE", "EXTERNAL", "TABLE")) && TailMatches3("ROW", "FORMAT", "DELIMITED")) - COMPLETE_WITH_LIST2("FIELDS TERMINATED BY", "LINES TERMINATED BY"); - else if ((HeadMatches3("CREATE", "EXTERNAL", "TABLE")) && TailMatches2("STORED", "AS")) - COMPLETE_WITH_LIST5("PARQUET", "RCFILE", "SEQUENCEFILE", "TEXTFILE", "ORC"); - else if ((HeadMatches3("CREATE", "EXTERNAL", "TABLE")) && TailMatches1("PROPERTIES")) - COMPLETE_WITH_CONST("('numRows'='"); - else if (HeadMatches4("CREATE", "EXTERNAL", "TABLE", MatchAny)) - COMPLETE_WITH_LIST5("PARTITIONED BY (", "ROW FORMAT DELIMITED", "STORED AS", "LOCATION", "TABLE PROPERTIES ('numRows'='"); - } - /* CREATE FOREIGN */ else if (Matches2("CREATE", "FOREIGN")) COMPLETE_WITH_LIST2("DATA WRAPPER", "TABLE"); @@ -2606,22 +2390,6 @@ psql_completion(const char *text, int start, int end) !TailMatches4("FOR", MatchAny, MatchAny, MatchAny)) COMPLETE_WITH_CONST("("); -/* CREATE LIBRARY */ - else if (Matches2("CREATE", "LIBRARY")) - COMPLETE_WITH_QUERY(Query_for_list_of_libraries); - else if (Matches4("CREATE", "OR", "REPLACE", "LIBRARY")) - COMPLETE_WITH_QUERY(Query_for_list_of_libraries); - else if (Matches5("CREATE", "OR", "REPLACE", "LIBRARY", MatchAny)) - COMPLETE_WITH_CONST("LANGUAGE plpythonu FROM"); - else if (Matches1("CREATE") && TailMatches5("LIBRARY", "LANGUAGE", "plpythonu", "FROM", MatchAny)) - COMPLETE_WITH_CONST("AUTHORIZATION"); - else if (Matches1("CREATE") && TailMatches6("LIBRARY", "LANGUAGE", "plpythonu", "FROM", MatchAny, "AUTHORIZATION")) - COMPLETE_WITH_CONST("REGION"); - else if (Matches1("CREATE") && TailMatches7("LIBRARY", "LANGUAGE", "plpythonu", "FROM", MatchAny, "AUTHORIZATION", "REGION")) - COMPLETE_WITH_QUERY(Query_for_list_of_aws_regions); - else if (Matches1("CREATE") && TailMatches8("LIBRARY", "LANGUAGE", "plpythonu", "FROM", MatchAny, "AUTHORIZATION", "REGION", "AS")) - COMPLETE_WITH_QUERY(Query_for_list_of_aws_regions); - // XXX See if you can add Regions here!? /* CREATE POLICY */ @@ -2974,10 +2742,6 @@ psql_completion(const char *text, int start, int end) else if (Matches5("CREATE", "EVENT", "TRIGGER", MatchAny, "ON")) COMPLETE_WITH_LIST3("ddl_command_start", "ddl_command_end", "sql_drop"); -/* CREATE CONTINUOUS TRANSFORM / VIEW */ - else if ((IS_PIPELINEDB) && (Matches2("CREATE|DROP", "CONTINUOUS"))) - COMPLETE_WITH_LIST2("TRANSFORM", "VIEW"); - /* DEALLOCATE */ else if (Matches1("DEALLOCATE")) COMPLETE_WITH_QUERY(Query_for_list_of_prepared_statements " UNION SELECT 'PREPARE'"); @@ -3051,10 +2815,6 @@ psql_completion(const char *text, int start, int end) else if (Matches4("DROP", "INDEX", "CONCURRENTLY", MatchAny)) COMPLETE_WITH_LIST2("CASCADE", "RESTRICT"); - /* DROP LIBRARY */ - else if (Matches2("DROP", "LIBRARY")) - COMPLETE_WITH_QUERY(Query_for_list_of_libraries); - /* DROP MATERIALIZED VIEW */ else if (Matches2("DROP", "MATERIALIZED")) COMPLETE_WITH_CONST("VIEW"); @@ -3184,18 +2944,21 @@ psql_completion(const char *text, int start, int end) Matches3("EXPLAIN", "ANALYZE", "VERBOSE")) COMPLETE_WITH_LIST5("SELECT", "INSERT", "DELETE", "UPDATE", "DECLARE"); -/* FETCH */ - /* Experiment with removing Postgres compliant and keeping only Redshift */ - else if (Matches1("FETCH")) - COMPLETE_WITH_LIST4("ALL", "FORWARD", "FROM", "NEXT"); - else if (Matches2("FETCH", "FORWARD")) - COMPLETE_WITH_LIST2("ALL", "FROM"); - else if (Matches2("FETCH", "NEXT|ALL")) - COMPLETE_WITH_CONST("FROM"); - else if (Matches3("FETCH", "FORWARD", MatchAnyExcept("FROM"))) - COMPLETE_WITH_CONST("FROM"); - else if (HeadMatches1("FETCH") && TailMatches1("FROM")) - COMPLETE_WITH_QUERY(Query_for_list_of_cursors); + /* FETCH && MOVE */ + /* Complete FETCH with one of FORWARD, BACKWARD, RELATIVE */ + else if (Matches1("FETCH|MOVE")) + COMPLETE_WITH_LIST4("ABSOLUTE", "BACKWARD", "FORWARD", "RELATIVE"); + /* Complete FETCH with one of ALL, NEXT, PRIOR */ + else if (Matches2("FETCH|MOVE", MatchAny)) + COMPLETE_WITH_LIST3("ALL", "NEXT", "PRIOR"); + + /* + * Complete FETCH with "FROM" or "IN". These are equivalent, + * but we may as well tab-complete both: perhaps some users prefer one + * variant or the other. + */ + else if (Matches3("FETCH|MOVE", MatchAny, MatchAny)) + COMPLETE_WITH_LIST2("FROM", "IN"); /* FOREIGN DATA WRAPPER */ /* applies in ALTER/DROP FDW and in CREATE SERVER */ @@ -3245,8 +3008,7 @@ psql_completion(const char *text, int start, int end) " UNION SELECT 'TEMPORARY'" " UNION SELECT 'EXECUTE'" " UNION SELECT 'USAGE'" - " UNION SELECT 'ALL'" - " UNION SELECT 'GRANT OPTION FOR'"); + " UNION SELECT 'ALL'"); } /* From bc95234e0bb4f932a42175aaa3da4ced9b0ac258 Mon Sep 17 00:00:00 2001 From: Robins Tharakan Date: Sat, 16 Sep 2017 04:01:16 +1000 Subject: [PATCH 101/149] \des and \det now shows EXTERNAL schemas / tables in Redshift --- src/bin/psql/command.c | 15 +- src/bin/psql/describe.c | 293 ++++++++++++++++++--------- src/bin/psql/describe.h | 6 + src/bin/psql/tab-complete-redshift.c | 8 +- src/bin/psql/tab-complete.c | 73 ++++++- 5 files changed, 286 insertions(+), 109 deletions(-) diff --git a/src/bin/psql/command.c b/src/bin/psql/command.c index 1d21777ff253a..bcbd19d9ae499 100644 --- a/src/bin/psql/command.c +++ b/src/bin/psql/command.c @@ -43,6 +43,11 @@ #include "settings.h" #include "variables.h" +/* Macro to easily identify which Engine (type) are we speaking to + Any change here should probably be replicated elsewhere since + #define for various SQLs (in this script) employ there own string-compare */ + #define IS_REDSHIFT (strncmp(pset.sengine, "redshift", 8) == 0) + /* * Editable database object types. */ @@ -860,7 +865,10 @@ exec_command_d(PsqlScanState scan_state, bool active_branch, const char *cmd) switch (cmd[2]) { case 's': - success = listForeignServers(pattern, show_verbose); + if (IS_REDSHIFT) + success = listExternalSchemasInRedshift(pattern, show_verbose); + else + success = listForeignServers(pattern, show_verbose); break; case 'u': success = listUserMappings(pattern, show_verbose); @@ -869,7 +877,10 @@ exec_command_d(PsqlScanState scan_state, bool active_branch, const char *cmd) success = listForeignDataWrappers(pattern, show_verbose); break; case 't': - success = listForeignTables(pattern, show_verbose); + if (IS_REDSHIFT) + success = listExternalTablesInRedshift(pattern, show_verbose); + else + success = listForeignTables(pattern, show_verbose); break; default: status = PSQL_CMD_UNKNOWN; diff --git a/src/bin/psql/describe.c b/src/bin/psql/describe.c index 61b3a88934331..8958cdbb4a2a1 100644 --- a/src/bin/psql/describe.c +++ b/src/bin/psql/describe.c @@ -4816,53 +4816,53 @@ listForeignServers(const char *pattern, bool verbose) char sverbuf[32]; psql_error("The server (version %s) does not support foreign servers.\n", - formatPGVersionNumber(pset.sversion, false, - sverbuf, sizeof(sverbuf))); + formatPGVersionNumber(pset.sversion, false, + sverbuf, sizeof(sverbuf))); return true; } initPQExpBuffer(&buf); printfPQExpBuffer(&buf, - "SELECT s.srvname AS \"%s\",\n" - " pg_catalog.pg_get_userbyid(s.srvowner) AS \"%s\",\n" - " f.fdwname AS \"%s\"", - gettext_noop("Name"), - gettext_noop("Owner"), - gettext_noop("Foreign-data wrapper")); + "SELECT s.srvname AS \"%s\",\n" + " pg_catalog.pg_get_userbyid(s.srvowner) AS \"%s\",\n" + " f.fdwname AS \"%s\"", + gettext_noop("Name"), + gettext_noop("Owner"), + gettext_noop("Foreign-data wrapper")); if (verbose) { appendPQExpBufferStr(&buf, ",\n "); printACLColumn(&buf, "s.srvacl"); appendPQExpBuffer(&buf, - ",\n" - " s.srvtype AS \"%s\",\n" - " s.srvversion AS \"%s\",\n" - " CASE WHEN srvoptions IS NULL THEN '' ELSE " - " '(' || pg_catalog.array_to_string(ARRAY(SELECT " - " pg_catalog.quote_ident(option_name) || ' ' || " - " pg_catalog.quote_literal(option_value) FROM " - " pg_catalog.pg_options_to_table(srvoptions)), ', ') || ')' " - " END AS \"%s\",\n" - " d.description AS \"%s\"", - gettext_noop("Type"), - gettext_noop("Version"), - gettext_noop("FDW options"), - gettext_noop("Description")); + ",\n" + " s.srvtype AS \"%s\",\n" + " s.srvversion AS \"%s\",\n" + " CASE WHEN srvoptions IS NULL THEN '' ELSE " + " '(' || pg_catalog.array_to_string(ARRAY(SELECT " + " pg_catalog.quote_ident(option_name) || ' ' || " + " pg_catalog.quote_literal(option_value) FROM " + " pg_catalog.pg_options_to_table(srvoptions)), ', ') || ')' " + " END AS \"%s\",\n" + " d.description AS \"%s\"", + gettext_noop("Type"), + gettext_noop("Version"), + gettext_noop("FDW options"), + gettext_noop("Description")); } appendPQExpBufferStr(&buf, - "\nFROM pg_catalog.pg_foreign_server s\n" - " JOIN pg_catalog.pg_foreign_data_wrapper f ON f.oid=s.srvfdw\n"); + "\nFROM pg_catalog.pg_foreign_server s\n" + " JOIN pg_catalog.pg_foreign_data_wrapper f ON f.oid=s.srvfdw\n"); if (verbose) appendPQExpBufferStr(&buf, - "LEFT JOIN pg_catalog.pg_description d\n " - "ON d.classoid = s.tableoid AND d.objoid = s.oid " - "AND d.objsubid = 0\n"); + "LEFT JOIN pg_catalog.pg_description d\n " + "ON d.classoid = s.tableoid AND d.objoid = s.oid " + "AND d.objsubid = 0\n"); processSQLNamePattern(pset.db, &buf, pattern, false, false, - NULL, "s.srvname", NULL, NULL); + NULL, "s.srvname", NULL, NULL); appendPQExpBufferStr(&buf, "ORDER BY 1;"); @@ -4881,6 +4881,50 @@ listForeignServers(const char *pattern, bool verbose) return true; } +/* + * \des + * + * Describes external schemas (in Redshift). + */ + bool + listExternalSchemasInRedshift(const char *pattern, bool verbose) + { + PQExpBufferData buf; + PGresult *res; + printQueryOpt myopt = pset.popt; + + initPQExpBuffer(&buf); + printfPQExpBuffer(&buf, + "SELECT trim(schemaname) AS \"%s\",\n" + " trim(pg_catalog.pg_get_userbyid(esowner)) AS \"%s\",\n" + " trim(databasename) AS \"%s\"\n", + gettext_noop("Schema"), + gettext_noop("Owner"), + gettext_noop("External Database")); + + appendPQExpBufferStr(&buf, + "\nFROM SVV_EXTERNAL_SCHEMAS\n"); + + processSQLNamePattern(pset.db, &buf, pattern, false, false, + NULL, NULL, "tablename", NULL); + + appendPQExpBufferStr(&buf, "ORDER BY 2;"); + + res = PSQLexec(buf.data); + termPQExpBuffer(&buf); + if (!res) + return false; + + myopt.nullPrint = NULL; + myopt.title = _("List of external schemas"); + myopt.translate_header = true; + + printQuery(res, &myopt, pset.queryFout, false, pset.logfile); + + PQclear(res); + return true; + } + /* * \deu * @@ -4947,78 +4991,127 @@ listUserMappings(const char *pattern, bool verbose) * * Describes foreign tables. */ -bool -listForeignTables(const char *pattern, bool verbose) -{ - PQExpBufferData buf; - PGresult *res; - printQueryOpt myopt = pset.popt; - - if (pset.sversion < 90100) - { - char sverbuf[32]; - - psql_error("The server (version %s) does not support foreign tables.\n", - formatPGVersionNumber(pset.sversion, false, - sverbuf, sizeof(sverbuf))); - return true; - } - - initPQExpBuffer(&buf); - printfPQExpBuffer(&buf, - "SELECT n.nspname AS \"%s\",\n" - " c.relname AS \"%s\",\n" - " s.srvname AS \"%s\"", - gettext_noop("Schema"), - gettext_noop("Table"), - gettext_noop("Server")); - - if (verbose) - appendPQExpBuffer(&buf, - ",\n CASE WHEN ftoptions IS NULL THEN '' ELSE " - " '(' || pg_catalog.array_to_string(ARRAY(SELECT " - " pg_catalog.quote_ident(option_name) || ' ' || " - " pg_catalog.quote_literal(option_value) FROM " - " pg_catalog.pg_options_to_table(ftoptions)), ', ') || ')' " - " END AS \"%s\",\n" - " d.description AS \"%s\"", - gettext_noop("FDW options"), - gettext_noop("Description")); - - appendPQExpBufferStr(&buf, - "\nFROM pg_catalog.pg_foreign_table ft\n" - " INNER JOIN pg_catalog.pg_class c" - " ON c.oid = ft.ftrelid\n" - " INNER JOIN pg_catalog.pg_namespace n" - " ON n.oid = c.relnamespace\n" - " INNER JOIN pg_catalog.pg_foreign_server s" - " ON s.oid = ft.ftserver\n"); - if (verbose) - appendPQExpBufferStr(&buf, - " LEFT JOIN pg_catalog.pg_description d\n" - " ON d.classoid = c.tableoid AND " - "d.objoid = c.oid AND d.objsubid = 0\n"); - - processSQLNamePattern(pset.db, &buf, pattern, false, false, - "n.nspname", "c.relname", NULL, - "pg_catalog.pg_table_is_visible(c.oid)"); - - appendPQExpBufferStr(&buf, "ORDER BY 1, 2;"); - - res = PSQLexec(buf.data); - termPQExpBuffer(&buf); - if (!res) - return false; - - myopt.nullPrint = NULL; - myopt.title = _("List of foreign tables"); - myopt.translate_header = true; - - printQuery(res, &myopt, pset.queryFout, false, pset.logfile); - - PQclear(res); - return true; -} + bool + listForeignTables(const char *pattern, bool verbose) + { + PQExpBufferData buf; + PGresult *res; + printQueryOpt myopt = pset.popt; + + if (pset.sversion < 90100) + { + char sverbuf[32]; + + psql_error("The server (version %s) does not support foreign tables.\n", + formatPGVersionNumber(pset.sversion, false, + sverbuf, sizeof(sverbuf))); + return true; + } + + initPQExpBuffer(&buf); + printfPQExpBuffer(&buf, + "SELECT n.nspname AS \"%s\",\n" + " c.relname AS \"%s\",\n" + " s.srvname AS \"%s\"", + gettext_noop("Schema"), + gettext_noop("Table"), + gettext_noop("Server")); + + if (verbose) + appendPQExpBuffer(&buf, + ",\n CASE WHEN ftoptions IS NULL THEN '' ELSE " + " '(' || pg_catalog.array_to_string(ARRAY(SELECT " + " pg_catalog.quote_ident(option_name) || ' ' || " + " pg_catalog.quote_literal(option_value) FROM " + " pg_catalog.pg_options_to_table(ftoptions)), ', ') || ')' " + " END AS \"%s\",\n" + " d.description AS \"%s\"", + gettext_noop("FDW options"), + gettext_noop("Description")); + + appendPQExpBufferStr(&buf, + "\nFROM pg_catalog.pg_foreign_table ft\n" + " INNER JOIN pg_catalog.pg_class c" + " ON c.oid = ft.ftrelid\n" + " INNER JOIN pg_catalog.pg_namespace n" + " ON n.oid = c.relnamespace\n" + " INNER JOIN pg_catalog.pg_foreign_server s" + " ON s.oid = ft.ftserver\n"); + if (verbose) + appendPQExpBufferStr(&buf, + " LEFT JOIN pg_catalog.pg_description d\n" + " ON d.classoid = c.tableoid AND " + "d.objoid = c.oid AND d.objsubid = 0\n"); + + processSQLNamePattern(pset.db, &buf, pattern, false, false, + "n.nspname", "c.relname", NULL, + "pg_catalog.pg_table_is_visible(c.oid)"); + + appendPQExpBufferStr(&buf, "ORDER BY 1, 2;"); + + res = PSQLexec(buf.data); + termPQExpBuffer(&buf); + if (!res) + return false; + + myopt.nullPrint = NULL; + myopt.title = _("List of foreign tables"); + myopt.translate_header = true; + + printQuery(res, &myopt, pset.queryFout, false, pset.logfile); + + PQclear(res); + return true; + } + +/* + * \det + * + * Describes external tables (in Redshift). + */ + bool + listExternalTablesInRedshift(const char *pattern, bool verbose) + { + PQExpBufferData buf; + PGresult *res; + printQueryOpt myopt = pset.popt; + + initPQExpBuffer(&buf); + printfPQExpBuffer(&buf, + "SELECT trim(et.schemaname) AS \"%s\",\n" + " trim(et.tablename) AS \"%s\",\n" + " '%s' AS \"%s\"," + " (SELECT trim(usename) FROM pg_user where usesysid = es.esowner) AS \"%s\"", + gettext_noop("Schema"), + gettext_noop("Table"), + gettext_noop("External Table"), + gettext_noop("Type"), + gettext_noop("Owner")); + + appendPQExpBufferStr(&buf, + "\nFROM SVV_EXTERNAL_TABLES et\n" + " JOIN SVV_EXTERNAL_SCHEMAS es\n" + " ON es.schemaname = et.schemaname\n"); + + processSQLNamePattern(pset.db, &buf, pattern, false, false, + "schemaname", "tablename", NULL, NULL); + + appendPQExpBufferStr(&buf, " ORDER BY 1, 2;"); + + res = PSQLexec(buf.data); + termPQExpBuffer(&buf); + if (!res) + return false; + + myopt.nullPrint = NULL; + myopt.title = _("List of external tables"); + myopt.translate_header = true; + + printQuery(res, &myopt, pset.queryFout, false, pset.logfile); + + PQclear(res); + return true; + } /* * \dx diff --git a/src/bin/psql/describe.h b/src/bin/psql/describe.h index 14a5667f3e2c3..6339bc76ca304 100644 --- a/src/bin/psql/describe.h +++ b/src/bin/psql/describe.h @@ -84,12 +84,18 @@ extern bool listForeignDataWrappers(const char *pattern, bool verbose); /* \des */ extern bool listForeignServers(const char *pattern, bool verbose); +/* \des in Redshift*/ +extern bool listExternalSchemasInRedshift(const char *pattern, bool verbose); + /* \deu */ extern bool listUserMappings(const char *pattern, bool verbose); /* \det */ extern bool listForeignTables(const char *pattern, bool verbose); +/* \det in Redshift*/ +extern bool listExternalTablesInRedshift(const char *pattern, bool verbose); + /* \dL */ extern bool listLanguages(const char *pattern, bool verbose, bool showSystem); diff --git a/src/bin/psql/tab-complete-redshift.c b/src/bin/psql/tab-complete-redshift.c index 7ad99532475a3..409605f52319b 100644 --- a/src/bin/psql/tab-complete-redshift.c +++ b/src/bin/psql/tab-complete-redshift.c @@ -3708,10 +3708,6 @@ psql_completion(const char *text, int start, int end) else if (Matches1("START")) COMPLETE_WITH_CONST("TRANSACTION"); -/* TABLE, but not TABLE embedded in other commands */ - else if (Matches1("TABLE")) - COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_relations, NULL); - /* TABLESAMPLE */ else if (TailMatches1("TABLESAMPLE")) COMPLETE_WITH_QUERY(Query_for_list_of_tablesample_methods); @@ -3935,7 +3931,9 @@ psql_completion(const char *text, int start, int end) else if (TailMatchesCS1("\\dD*")) COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_domains, NULL); else if (TailMatchesCS1("\\des*")) - COMPLETE_WITH_QUERY(Query_for_list_of_servers); + COMPLETE_WITH_QUERY(Query_for_list_of_external_schemas); + else if (TailMatchesCS1("\\det*")) + COMPLETE_WITH_QUERY(Query_for_list_of_external_tables); else if (TailMatchesCS1("\\deu*")) COMPLETE_WITH_QUERY(Query_for_list_of_user_mappings); else if (TailMatchesCS1("\\dew*")) diff --git a/src/bin/psql/tab-complete.c b/src/bin/psql/tab-complete.c index 32860b90e7fb2..c58673e06a238 100644 --- a/src/bin/psql/tab-complete.c +++ b/src/bin/psql/tab-complete.c @@ -314,6 +314,18 @@ do { \ COMPLETE_WITH_LIST(list); \ } while (0) +#define COMPLETE_WITH_LIST11(s1, s2, s3, s4, s5, s6, s7, s8, s9, s10, s11) \ +do { \ + static const char *const list[] = { s1, s2, s3, s4, s5, s6, s7, s8, s9, s10, s11, NULL }; \ + COMPLETE_WITH_LIST(list); \ +} while (0) + +#define COMPLETE_WITH_LIST12(s1, s2, s3, s4, s5, s6, s7, s8, s9, s10, s11, s12) \ +do { \ + static const char *const list[] = { s1, s2, s3, s4, s5, s6, s7, s8, s9, s10, s11, s12, NULL }; \ + COMPLETE_WITH_LIST(list); \ +} while (0) + /* * Likewise for COMPLETE_WITH_LIST_CS. */ @@ -1426,6 +1438,63 @@ psql_completion(const char *text, int start, int end) word_matches(p2, previous_words[previous_words_count - 2]) && \ word_matches(p3, previous_words[previous_words_count - 3])) + #define HeadMatches4(p1, p2, p3, p4) \ + (previous_words_count >= 4 && \ + word_matches(p1, previous_words[previous_words_count - 1]) && \ + word_matches(p2, previous_words[previous_words_count - 2]) && \ + word_matches(p3, previous_words[previous_words_count - 3]) && \ + word_matches(p4, previous_words[previous_words_count - 4])) + + #define HeadMatches5(p1, p2, p3, p4, p5) \ + (previous_words_count >= 5 && \ + word_matches(p1, previous_words[previous_words_count - 1]) && \ + word_matches(p2, previous_words[previous_words_count - 2]) && \ + word_matches(p3, previous_words[previous_words_count - 3]) && \ + word_matches(p4, previous_words[previous_words_count - 4]) && \ + word_matches(p5, previous_words[previous_words_count - 5])) + + #define HeadMatches6(p1, p2, p3, p4, p5, p6) \ + (previous_words_count >= 6 && \ + word_matches(p1, previous_words[previous_words_count - 1]) && \ + word_matches(p2, previous_words[previous_words_count - 2]) && \ + word_matches(p3, previous_words[previous_words_count - 3]) && \ + word_matches(p4, previous_words[previous_words_count - 4]) && \ + word_matches(p5, previous_words[previous_words_count - 5]) && \ + word_matches(p6, previous_words[previous_words_count - 6])) + + #define HeadMatches7(p1, p2, p3, p4, p5, p6, p7) \ + (previous_words_count >= 7 && \ + word_matches(p1, previous_words[previous_words_count - 1]) && \ + word_matches(p2, previous_words[previous_words_count - 2]) && \ + word_matches(p3, previous_words[previous_words_count - 3]) && \ + word_matches(p4, previous_words[previous_words_count - 4]) && \ + word_matches(p5, previous_words[previous_words_count - 5]) && \ + word_matches(p6, previous_words[previous_words_count - 6]) && \ + word_matches(p7, previous_words[previous_words_count - 7])) + + #define HeadMatches8(p1, p2, p3, p4, p5, p6, p7, p8) \ + (previous_words_count >= 8 && \ + word_matches(p1, previous_words[previous_words_count - 1]) && \ + word_matches(p2, previous_words[previous_words_count - 2]) && \ + word_matches(p3, previous_words[previous_words_count - 3]) && \ + word_matches(p4, previous_words[previous_words_count - 4]) && \ + word_matches(p5, previous_words[previous_words_count - 5]) && \ + word_matches(p6, previous_words[previous_words_count - 6]) && \ + word_matches(p7, previous_words[previous_words_count - 7]) && \ + word_matches(p8, previous_words[previous_words_count - 8])) + + #define HeadMatches9(p1, p2, p3, p4, p5, p6, p7, p8, p9) \ + (previous_words_count >= 9 && \ + word_matches(p1, previous_words[previous_words_count - 1]) && \ + word_matches(p2, previous_words[previous_words_count - 2]) && \ + word_matches(p3, previous_words[previous_words_count - 3]) && \ + word_matches(p4, previous_words[previous_words_count - 4]) && \ + word_matches(p5, previous_words[previous_words_count - 5]) && \ + word_matches(p6, previous_words[previous_words_count - 6]) && \ + word_matches(p7, previous_words[previous_words_count - 7]) && \ + word_matches(p8, previous_words[previous_words_count - 8]) && \ + word_matches(p9, previous_words[previous_words_count - 9])) + /* Known command-starting keywords. */ static const char *const sql_commands[] = { "ABORT", "ALTER", "ANALYZE", "BEGIN", "CHECKPOINT", "CLOSE", "CLUSTER", @@ -1954,7 +2023,7 @@ psql_completion(const char *text, int start, int end) /* If we have ALTER TABLE DROP, provide COLUMN or CONSTRAINT */ else if (Matches4("ALTER", "TABLE", MatchAny, "DROP")) - COMPLETE_WITH_LIST3("COLUMN", "CONSTRAINT"); + COMPLETE_WITH_LIST2("COLUMN", "CONSTRAINT"); /* If we have ALTER TABLE DROP COLUMN, provide list of columns */ else if (Matches5("ALTER", "TABLE", MatchAny, "DROP", "COLUMN")) COMPLETE_WITH_ATTR(prev3_wd, ""); @@ -1997,7 +2066,7 @@ psql_completion(const char *text, int start, int end) } /* If we have ALTER TABLE SET, provide list of attributes and '(' */ else if (Matches4("ALTER", "TABLE", MatchAny, "SET")) - COMPLETE_WITH_LIST10("(", "LOGGED", "SCHEMA", "TABLESPACE", "UNLOGGED", + COMPLETE_WITH_LIST7("(", "LOGGED", "SCHEMA", "TABLESPACE", "UNLOGGED", "WITH", "WITHOUT"); /* From c31e0ac08a6588b6dd55da553875397ad5e2eda7 Mon Sep 17 00:00:00 2001 From: Robins Tharakan Date: Mon, 18 Sep 2017 01:14:34 +1000 Subject: [PATCH 102/149] Part way into requesting password from an external JSON input. Still WIP --- src/bin/psql/Makefile | 2 +- src/bin/psql/command.c | 2 +- src/bin/psql/common.c | 91 ++++++++++++ src/bin/psql/common.h | 2 + src/bin/psql/jsmn.c | 314 +++++++++++++++++++++++++++++++++++++++++ src/bin/psql/jsmn.h | 76 ++++++++++ src/bin/psql/nls.mk | 2 +- src/bin/psql/startup.c | 38 +---- 8 files changed, 488 insertions(+), 39 deletions(-) create mode 100755 src/bin/psql/jsmn.c create mode 100755 src/bin/psql/jsmn.h diff --git a/src/bin/psql/Makefile b/src/bin/psql/Makefile index 99ab5e46c8ee0..cce3ac1a6115a 100644 --- a/src/bin/psql/Makefile +++ b/src/bin/psql/Makefile @@ -22,7 +22,7 @@ override CPPFLAGS := -I. -I$(srcdir) -I$(libpq_srcdir) $(CPPFLAGS) override LDFLAGS := -L$(top_builddir)/src/fe_utils -lpgfeutils $(libpq_pgport) $(LDFLAGS) OBJS= command.o common.o conditional.o copy.o crosstabview.o \ - describe.o help.o input.o large_obj.o mainloop.o \ + describe.o help.o input.o jsmn.o large_obj.o mainloop.o \ prompt.o psqlscanslash.o sql_help.o startup.o stringutils.o \ tab-complete.o tab-complete-pipelinedb.o tab-complete-redshift.o variables.o \ $(WIN32RES) diff --git a/src/bin/psql/command.c b/src/bin/psql/command.c index 8a2526484dc81..e17af5776a3e4 100644 --- a/src/bin/psql/command.c +++ b/src/bin/psql/command.c @@ -2976,7 +2976,7 @@ do_connect(enum trivalue reuse_previous_specification, if (pset.getPassword == TRI_YES) { if (pset.credential_source == AWS_IAM_REDSHIFT) - request_password_from_external_source(); + request_password_from_external_source(password); else if (pset.credential_source == DEFAULT) password = prompt_for_password(user); else diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index 9b59ee840b91a..1649bc3d42ebf 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -28,6 +28,7 @@ #include "crosstabview.h" #include "fe_utils/mbprint.h" +#include "jsmn.h" static bool DescribeQuery(const char *query, double *elapsed_msec); static bool ExecQueryUsingCursor(const char *query, double *elapsed_msec); @@ -35,6 +36,19 @@ static bool command_no_begin(const char *query); static bool is_select_command(const char *query); +static const char *JSON_STRING = +"{\"DbUser\": \"IAM:tempdel\", \"DbPassword\": \"tempdel\", \"Expiration\": \"2017-09-16T11:12:37.608Z\"}"; + + +static int jsoneq(const char *json, jsmntok_t *tok, const char *s) { + if (tok->type == JSMN_STRING && (int) strlen(s) == tok->end - tok->start && + strncmp(json + tok->start, s, tok->end - tok->start) == 0) { + return 0; + } + return -1; +} + + /* * openQueryOutputFile --- attempt to open a query output file * @@ -111,6 +125,83 @@ setQFout(const char *fname) } + +/* + * request_password_from_external_source + * + * Generalized function to fetch usernames and passwords from external source. + * For now works only in Linux. + */ +char * +request_password_from_external_source(char *password) +{ + // FILE *fp; + // char path[1035]; + + /* Open the command for reading. * / + fp = popen("aws redshift get-cluster-credentials --db-user redshift2 --cluster-identifier redshift2", "r"); + if (fp == NULL) { + printf("Failed to run command\n" ); + exit(1); + } + */ + + printf("\n===Enter Function===\n"); + + int i; + int r; + jsmn_parser p; + jsmntok_t t[128]; /* We expect no more than 128 tokens */ + + jsmn_init(&p); + r = jsmn_parse(&p, JSON_STRING, strlen(JSON_STRING), t, sizeof(t)/sizeof(t[0])); + if (r < 0) { + printf("Failed to parse JSON: %d\n", r); + return; + } + + + /* Assume the top-level element is an object */ + if (r < 1 || t[0].type != JSMN_OBJECT) { + printf("Object expected\n"); + return; + } + + /* Loop over all keys of the root object */ + for (i = 1; i < r; i++) { + if (jsoneq(JSON_STRING, &t[i], "DbUser") == 0) { + /* We may use strndup() to fetch string value */ + printf("- Username: %.*s\n", t[i+1].end-t[i+1].start, + JSON_STRING + t[i+1].start); + i++; + } else if (jsoneq(JSON_STRING, &t[i], "DbPassword") == 0) { + /* We may additionally check if the value is either "true" or "false" */ + printf("- Password: %.*s\n", t[i+1].end-t[i+1].start, + JSON_STRING + t[i+1].start); + StrNCpy(password, JSON_STRING + t[i+1].start, t[i+1].end-t[i+1].start + 1); + i++; + } else if (jsoneq(JSON_STRING, &t[i], "Expiration") == 0) { + /* We may want to do strtol() here to get numeric value */ + printf("- Expiration: %.*s\n", t[i+1].end-t[i+1].start, + JSON_STRING + t[i+1].start); + i++; + } else { + printf("Unexpected key: %.*s\n", t[i].end-t[i].start, + JSON_STRING + t[i].start); + } + } + return; + + /* Read the output a line at a time - output it. * / + while (fgets(path, sizeof(path)-1, fp) != NULL) { + printf("%s", path); + } + + / * close * / + pclose(fp); */ +} + + /* * Variable-fetching callback for flex lexer * diff --git a/src/bin/psql/common.h b/src/bin/psql/common.h index f34868b54e4e6..8e7c06f072bd8 100644 --- a/src/bin/psql/common.h +++ b/src/bin/psql/common.h @@ -46,4 +46,6 @@ extern void expand_tilde(char **filename); extern bool recognized_connection_string(const char *connstr); +extern char *request_password_from_external_source(char *password); + #endif /* COMMON_H */ diff --git a/src/bin/psql/jsmn.c b/src/bin/psql/jsmn.c new file mode 100755 index 0000000000000..bcd6392a069ca --- /dev/null +++ b/src/bin/psql/jsmn.c @@ -0,0 +1,314 @@ +#include "jsmn.h" + +/** + * Allocates a fresh unused token from the token pull. + */ +static jsmntok_t *jsmn_alloc_token(jsmn_parser *parser, + jsmntok_t *tokens, size_t num_tokens) { + jsmntok_t *tok; + if (parser->toknext >= num_tokens) { + return NULL; + } + tok = &tokens[parser->toknext++]; + tok->start = tok->end = -1; + tok->size = 0; +#ifdef JSMN_PARENT_LINKS + tok->parent = -1; +#endif + return tok; +} + +/** + * Fills token type and boundaries. + */ +static void jsmn_fill_token(jsmntok_t *token, jsmntype_t type, + int start, int end) { + token->type = type; + token->start = start; + token->end = end; + token->size = 0; +} + +/** + * Fills next available token with JSON primitive. + */ +static int jsmn_parse_primitive(jsmn_parser *parser, const char *js, + size_t len, jsmntok_t *tokens, size_t num_tokens) { + jsmntok_t *token; + int start; + + start = parser->pos; + + for (; parser->pos < len && js[parser->pos] != '\0'; parser->pos++) { + switch (js[parser->pos]) { +#ifndef JSMN_STRICT + /* In strict mode primitive must be followed by "," or "}" or "]" */ + case ':': +#endif + case '\t' : case '\r' : case '\n' : case ' ' : + case ',' : case ']' : case '}' : + goto found; + } + if (js[parser->pos] < 32 || js[parser->pos] >= 127) { + parser->pos = start; + return JSMN_ERROR_INVAL; + } + } +#ifdef JSMN_STRICT + /* In strict mode primitive must be followed by a comma/object/array */ + parser->pos = start; + return JSMN_ERROR_PART; +#endif + +found: + if (tokens == NULL) { + parser->pos--; + return 0; + } + token = jsmn_alloc_token(parser, tokens, num_tokens); + if (token == NULL) { + parser->pos = start; + return JSMN_ERROR_NOMEM; + } + jsmn_fill_token(token, JSMN_PRIMITIVE, start, parser->pos); +#ifdef JSMN_PARENT_LINKS + token->parent = parser->toksuper; +#endif + parser->pos--; + return 0; +} + +/** + * Fills next token with JSON string. + */ +static int jsmn_parse_string(jsmn_parser *parser, const char *js, + size_t len, jsmntok_t *tokens, size_t num_tokens) { + jsmntok_t *token; + + int start = parser->pos; + + parser->pos++; + + /* Skip starting quote */ + for (; parser->pos < len && js[parser->pos] != '\0'; parser->pos++) { + char c = js[parser->pos]; + + /* Quote: end of string */ + if (c == '\"') { + if (tokens == NULL) { + return 0; + } + token = jsmn_alloc_token(parser, tokens, num_tokens); + if (token == NULL) { + parser->pos = start; + return JSMN_ERROR_NOMEM; + } + jsmn_fill_token(token, JSMN_STRING, start+1, parser->pos); +#ifdef JSMN_PARENT_LINKS + token->parent = parser->toksuper; +#endif + return 0; + } + + /* Backslash: Quoted symbol expected */ + if (c == '\\' && parser->pos + 1 < len) { + int i; + parser->pos++; + switch (js[parser->pos]) { + /* Allowed escaped symbols */ + case '\"': case '/' : case '\\' : case 'b' : + case 'f' : case 'r' : case 'n' : case 't' : + break; + /* Allows escaped symbol \uXXXX */ + case 'u': + parser->pos++; + for(i = 0; i < 4 && parser->pos < len && js[parser->pos] != '\0'; i++) { + /* If it isn't a hex character we have an error */ + if(!((js[parser->pos] >= 48 && js[parser->pos] <= 57) || /* 0-9 */ + (js[parser->pos] >= 65 && js[parser->pos] <= 70) || /* A-F */ + (js[parser->pos] >= 97 && js[parser->pos] <= 102))) { /* a-f */ + parser->pos = start; + return JSMN_ERROR_INVAL; + } + parser->pos++; + } + parser->pos--; + break; + /* Unexpected symbol */ + default: + parser->pos = start; + return JSMN_ERROR_INVAL; + } + } + } + parser->pos = start; + return JSMN_ERROR_PART; +} + +/** + * Parse JSON string and fill tokens. + */ +int jsmn_parse(jsmn_parser *parser, const char *js, size_t len, + jsmntok_t *tokens, unsigned int num_tokens) { + int r; + int i; + jsmntok_t *token; + int count = parser->toknext; + + for (; parser->pos < len && js[parser->pos] != '\0'; parser->pos++) { + char c; + jsmntype_t type; + + c = js[parser->pos]; + switch (c) { + case '{': case '[': + count++; + if (tokens == NULL) { + break; + } + token = jsmn_alloc_token(parser, tokens, num_tokens); + if (token == NULL) + return JSMN_ERROR_NOMEM; + if (parser->toksuper != -1) { + tokens[parser->toksuper].size++; +#ifdef JSMN_PARENT_LINKS + token->parent = parser->toksuper; +#endif + } + token->type = (c == '{' ? JSMN_OBJECT : JSMN_ARRAY); + token->start = parser->pos; + parser->toksuper = parser->toknext - 1; + break; + case '}': case ']': + if (tokens == NULL) + break; + type = (c == '}' ? JSMN_OBJECT : JSMN_ARRAY); +#ifdef JSMN_PARENT_LINKS + if (parser->toknext < 1) { + return JSMN_ERROR_INVAL; + } + token = &tokens[parser->toknext - 1]; + for (;;) { + if (token->start != -1 && token->end == -1) { + if (token->type != type) { + return JSMN_ERROR_INVAL; + } + token->end = parser->pos + 1; + parser->toksuper = token->parent; + break; + } + if (token->parent == -1) { + if(token->type != type || parser->toksuper == -1) { + return JSMN_ERROR_INVAL; + } + break; + } + token = &tokens[token->parent]; + } +#else + for (i = parser->toknext - 1; i >= 0; i--) { + token = &tokens[i]; + if (token->start != -1 && token->end == -1) { + if (token->type != type) { + return JSMN_ERROR_INVAL; + } + parser->toksuper = -1; + token->end = parser->pos + 1; + break; + } + } + /* Error if unmatched closing bracket */ + if (i == -1) return JSMN_ERROR_INVAL; + for (; i >= 0; i--) { + token = &tokens[i]; + if (token->start != -1 && token->end == -1) { + parser->toksuper = i; + break; + } + } +#endif + break; + case '\"': + r = jsmn_parse_string(parser, js, len, tokens, num_tokens); + if (r < 0) return r; + count++; + if (parser->toksuper != -1 && tokens != NULL) + tokens[parser->toksuper].size++; + break; + case '\t' : case '\r' : case '\n' : case ' ': + break; + case ':': + parser->toksuper = parser->toknext - 1; + break; + case ',': + if (tokens != NULL && parser->toksuper != -1 && + tokens[parser->toksuper].type != JSMN_ARRAY && + tokens[parser->toksuper].type != JSMN_OBJECT) { +#ifdef JSMN_PARENT_LINKS + parser->toksuper = tokens[parser->toksuper].parent; +#else + for (i = parser->toknext - 1; i >= 0; i--) { + if (tokens[i].type == JSMN_ARRAY || tokens[i].type == JSMN_OBJECT) { + if (tokens[i].start != -1 && tokens[i].end == -1) { + parser->toksuper = i; + break; + } + } + } +#endif + } + break; +#ifdef JSMN_STRICT + /* In strict mode primitives are: numbers and booleans */ + case '-': case '0': case '1' : case '2': case '3' : case '4': + case '5': case '6': case '7' : case '8': case '9': + case 't': case 'f': case 'n' : + /* And they must not be keys of the object */ + if (tokens != NULL && parser->toksuper != -1) { + jsmntok_t *t = &tokens[parser->toksuper]; + if (t->type == JSMN_OBJECT || + (t->type == JSMN_STRING && t->size != 0)) { + return JSMN_ERROR_INVAL; + } + } +#else + /* In non-strict mode every unquoted value is a primitive */ + default: +#endif + r = jsmn_parse_primitive(parser, js, len, tokens, num_tokens); + if (r < 0) return r; + count++; + if (parser->toksuper != -1 && tokens != NULL) + tokens[parser->toksuper].size++; + break; + +#ifdef JSMN_STRICT + /* Unexpected char in strict mode */ + default: + return JSMN_ERROR_INVAL; +#endif + } + } + + if (tokens != NULL) { + for (i = parser->toknext - 1; i >= 0; i--) { + /* Unmatched opened object or array */ + if (tokens[i].start != -1 && tokens[i].end == -1) { + return JSMN_ERROR_PART; + } + } + } + + return count; +} + +/** + * Creates a new parser based over a given buffer with an array of tokens + * available. + */ +void jsmn_init(jsmn_parser *parser) { + parser->pos = 0; + parser->toknext = 0; + parser->toksuper = -1; +} + diff --git a/src/bin/psql/jsmn.h b/src/bin/psql/jsmn.h new file mode 100755 index 0000000000000..5a5200ee2fb8a --- /dev/null +++ b/src/bin/psql/jsmn.h @@ -0,0 +1,76 @@ +#ifndef __JSMN_H_ +#define __JSMN_H_ + +#include + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * JSON type identifier. Basic types are: + * o Object + * o Array + * o String + * o Other primitive: number, boolean (true/false) or null + */ +typedef enum { + JSMN_UNDEFINED = 0, + JSMN_OBJECT = 1, + JSMN_ARRAY = 2, + JSMN_STRING = 3, + JSMN_PRIMITIVE = 4 +} jsmntype_t; + +enum jsmnerr { + /* Not enough tokens were provided */ + JSMN_ERROR_NOMEM = -1, + /* Invalid character inside JSON string */ + JSMN_ERROR_INVAL = -2, + /* The string is not a full JSON packet, more bytes expected */ + JSMN_ERROR_PART = -3 +}; + +/** + * JSON token description. + * type type (object, array, string etc.) + * start start position in JSON data string + * end end position in JSON data string + */ +typedef struct { + jsmntype_t type; + int start; + int end; + int size; +#ifdef JSMN_PARENT_LINKS + int parent; +#endif +} jsmntok_t; + +/** + * JSON parser. Contains an array of token blocks available. Also stores + * the string being parsed now and current position in that string + */ +typedef struct { + unsigned int pos; /* offset in the JSON string */ + unsigned int toknext; /* next token to allocate */ + int toksuper; /* superior token node, e.g parent object or array */ +} jsmn_parser; + +/** + * Create JSON parser over an array of tokens + */ +void jsmn_init(jsmn_parser *parser); + +/** + * Run JSON parser. It parses a JSON data string into and array of tokens, each describing + * a single JSON object. + */ +int jsmn_parse(jsmn_parser *parser, const char *js, size_t len, + jsmntok_t *tokens, unsigned int num_tokens); + +#ifdef __cplusplus +} +#endif + +#endif /* __JSMN_H_ */ diff --git a/src/bin/psql/nls.mk b/src/bin/psql/nls.mk index 00548e124439f..fdd825e031584 100644 --- a/src/bin/psql/nls.mk +++ b/src/bin/psql/nls.mk @@ -1,7 +1,7 @@ # src/bin/psql/nls.mk CATALOG_NAME = psql AVAIL_LANGUAGES = cs de es fr he it ja ko pl pt_BR ru sv zh_CN zh_TW -GETTEXT_FILES = command.c common.c copy.c crosstabview.c help.c input.c large_obj.c \ +GETTEXT_FILES = command.c common.c copy.c crosstabview.c help.c input.c jsmn.c large_obj.c \ mainloop.c psqlscanslash.c startup.c \ describe.c sql_help.h sql_help.c \ tab-complete.c tab-complete-pipelinedb.c tab-complete-redshift.c variables.c \ diff --git a/src/bin/psql/startup.c b/src/bin/psql/startup.c index 50dd229e742ad..dcacea498c81b 100644 --- a/src/bin/psql/startup.c +++ b/src/bin/psql/startup.c @@ -25,8 +25,6 @@ #include "fe_utils/print.h" #include "settings.h" - - /* * Global psql options */ @@ -89,37 +87,6 @@ static void EstablishVariableSpace(void); #define NOPAGER 0 - - -/* - * request_password_from_external_source - * - * Generalized function to fetch usernames and passwords from external source. - * For now works only in Linux. - */ - void - request_password_from_external_source() - { - FILE *fp; - char path[1035]; - - /* Open the command for reading. */ - fp = popen("aws redshift get-cluster-credentials --db-user redshift2 --cluster-identifier redshift2", "r"); - if (fp == NULL) { - printf("Failed to run command\n" ); - exit(1); - } - - /* Read the output a line at a time - output it. */ - while (fgets(path, sizeof(path)-1, fp) != NULL) { - printf("%s", path); - } - - /* close */ - pclose(fp); - } - - /* * * main @@ -247,7 +214,7 @@ main(int argc, char *argv[]) { if (pset.credential_source == AWS_IAM_REDSHIFT) { - request_password_from_external_source(); + request_password_from_external_source(password); have_password = true; } else if (pset.credential_source == DEFAULT) @@ -300,8 +267,7 @@ main(int argc, char *argv[]) if (pset.credential_source == AWS_IAM_REDSHIFT) { PQfinish(pset.db); - request_password_from_external_source(); -// simple_prompt(password_prompt, password, sizeof(password), false); + request_password_from_external_source(password); have_password = true; new_pass = true; } From eb1ce255c67c1235a97ea2424905df917eb27c00 Mon Sep 17 00:00:00 2001 From: Robins Tharakan Date: Mon, 18 Sep 2017 06:16:53 +1000 Subject: [PATCH 103/149] (Not working) Trying to fetch both username/password from external source. Still WIP --- src/bin/psql/command.c | 2 +- src/bin/psql/common.c | 51 +++++++++++++++++++++++++++++++----------- src/bin/psql/common.h | 2 +- src/bin/psql/startup.c | 22 +++++++++++++----- 4 files changed, 57 insertions(+), 20 deletions(-) diff --git a/src/bin/psql/command.c b/src/bin/psql/command.c index e17af5776a3e4..cbee91cd3fa59 100644 --- a/src/bin/psql/command.c +++ b/src/bin/psql/command.c @@ -2976,7 +2976,7 @@ do_connect(enum trivalue reuse_previous_specification, if (pset.getPassword == TRI_YES) { if (pset.credential_source == AWS_IAM_REDSHIFT) - request_password_from_external_source(password); +;//request_password_from_external_source(username, password); else if (pset.credential_source == DEFAULT) password = prompt_for_password(user); else diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index 1649bc3d42ebf..c891bb9370c2d 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -132,8 +132,8 @@ setQFout(const char *fname) * Generalized function to fetch usernames and passwords from external source. * For now works only in Linux. */ -char * -request_password_from_external_source(char *password) +bool +request_password_from_external_source(char *username, char *password) { // FILE *fp; // char path[1035]; @@ -150,6 +150,10 @@ request_password_from_external_source(char *password) int i; int r; + char *new_username; + char *new_password; + bool found_password = false; + bool found_username = false; jsmn_parser p; jsmntok_t t[128]; /* We expect no more than 128 tokens */ @@ -157,40 +161,61 @@ request_password_from_external_source(char *password) r = jsmn_parse(&p, JSON_STRING, strlen(JSON_STRING), t, sizeof(t)/sizeof(t[0])); if (r < 0) { printf("Failed to parse JSON: %d\n", r); - return; + return false; } /* Assume the top-level element is an object */ if (r < 1 || t[0].type != JSMN_OBJECT) { printf("Object expected\n"); - return; + return false; } + /* + buffer = (char *) pg_malloc(maxlength + 1); + while (fgets(buffer, maxlength + 1, infile) != NULL && n < nlines) + result[n++] = pg_strdup(buffer); + + */ /* Loop over all keys of the root object */ for (i = 1; i < r; i++) { + printf("\n0000000000 r=%d i=%d : ", r, i); if (jsoneq(JSON_STRING, &t[i], "DbUser") == 0) { - /* We may use strndup() to fetch string value */ - printf("- Username: %.*s\n", t[i+1].end-t[i+1].start, - JSON_STRING + t[i+1].start); + printf("\n1111111"); + new_username = pg_malloc(t[i+1].end-t[i+1].start + 2); + StrNCpy(new_username, JSON_STRING + t[i+1].start, t[i+1].end-t[i+1].start + 1); + printf("- Username: %s\n", new_username); + found_username = true; i++; } else if (jsoneq(JSON_STRING, &t[i], "DbPassword") == 0) { - /* We may additionally check if the value is either "true" or "false" */ - printf("- Password: %.*s\n", t[i+1].end-t[i+1].start, - JSON_STRING + t[i+1].start); - StrNCpy(password, JSON_STRING + t[i+1].start, t[i+1].end-t[i+1].start + 1); + printf("\n22222222"); + new_password = pg_malloc(t[i+1].end-t[i+1].start + 2); + StrNCpy(new_password, JSON_STRING + t[i+1].start, t[i+1].end-t[i+1].start + 1); + printf("- Password: %s", new_password); + found_password = true; i++; } else if (jsoneq(JSON_STRING, &t[i], "Expiration") == 0) { - /* We may want to do strtol() here to get numeric value */ + printf("\n333333"); printf("- Expiration: %.*s\n", t[i+1].end-t[i+1].start, JSON_STRING + t[i+1].start); + printf("\n333333-end1"); i++; + printf("\n333333-end2"); } else { + printf("\n44444444"); printf("Unexpected key: %.*s\n", t[i].end-t[i].start, JSON_STRING + t[i].start); + } + if (found_username && found_password) + { + free(username); + username = pg_strdup(new_username); + password = new_password; + return true; } } - return; + printf("fetched all params"); + return false; /* Read the output a line at a time - output it. * / while (fgets(path, sizeof(path)-1, fp) != NULL) { diff --git a/src/bin/psql/common.h b/src/bin/psql/common.h index 8e7c06f072bd8..62fbaae768c34 100644 --- a/src/bin/psql/common.h +++ b/src/bin/psql/common.h @@ -46,6 +46,6 @@ extern void expand_tilde(char **filename); extern bool recognized_connection_string(const char *connstr); -extern char *request_password_from_external_source(char *password); +extern bool request_password_from_external_source(char *username, char *password); #endif /* COMMON_H */ diff --git a/src/bin/psql/startup.c b/src/bin/psql/startup.c index dcacea498c81b..88071df2e9511 100644 --- a/src/bin/psql/startup.c +++ b/src/bin/psql/startup.c @@ -214,8 +214,14 @@ main(int argc, char *argv[]) { if (pset.credential_source == AWS_IAM_REDSHIFT) { - request_password_from_external_source(password); - have_password = true; + if (request_password_from_external_source(&options.username, password)) + { + have_password = true; + } + else + { + printf("Unable to fetch Username / Password from IAM"); + } } else if (pset.credential_source == DEFAULT) { @@ -267,9 +273,15 @@ main(int argc, char *argv[]) if (pset.credential_source == AWS_IAM_REDSHIFT) { PQfinish(pset.db); - request_password_from_external_source(password); - have_password = true; - new_pass = true; + if (request_password_from_external_source(&options.username, password)) + { + have_password = true; + new_pass = true; + } + else + { + printf("Unable to fetch new Username / Password from IAM"); + } } else { From 6cc0e1596e15d30bc7e96e4da8a20837d0d0c07e Mon Sep 17 00:00:00 2001 From: Robins Tharakan Date: Wed, 20 Sep 2017 00:14:20 +1000 Subject: [PATCH 104/149] Still WIP on fetching username / password from external source. almost there, but not quite --- src/bin/psql/command.c | 5 ++++- src/bin/psql/common.c | 34 +++++++++++++++++++--------------- src/bin/psql/startup.c | 11 +++++++++-- 3 files changed, 32 insertions(+), 18 deletions(-) diff --git a/src/bin/psql/command.c b/src/bin/psql/command.c index cbee91cd3fa59..3c532d4c8d7c1 100644 --- a/src/bin/psql/command.c +++ b/src/bin/psql/command.c @@ -2976,7 +2976,10 @@ do_connect(enum trivalue reuse_previous_specification, if (pset.getPassword == TRI_YES) { if (pset.credential_source == AWS_IAM_REDSHIFT) -;//request_password_from_external_source(username, password); + { + printf("Entry3"); + request_password_from_external_source(user, password); + } else if (pset.credential_source == DEFAULT) password = prompt_for_password(user); else diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index c891bb9370c2d..3a71953f2c09a 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -37,7 +37,7 @@ static bool is_select_command(const char *query); static const char *JSON_STRING = -"{\"DbUser\": \"IAM:tempdel\", \"DbPassword\": \"tempdel\", \"Expiration\": \"2017-09-16T11:12:37.608Z\"}"; +"{\"DbUser\": \"tempdel\", \"DbPassword\": \"tempdel\", \"Expiration\": \"2017-09-16T11:12:37.608Z\"}"; static int jsoneq(const char *json, jsmntok_t *tok, const char *s) { @@ -146,17 +146,17 @@ request_password_from_external_source(char *username, char *password) } */ - printf("\n===Enter Function===\n"); - int i; int r; - char *new_username; - char *new_password; + char *new_username = NULL; + char *new_password = NULL; bool found_password = false; bool found_username = false; jsmn_parser p; jsmntok_t t[128]; /* We expect no more than 128 tokens */ + printf("\n===Enter Function===\n"); + jsmn_init(&p); r = jsmn_parse(&p, JSON_STRING, strlen(JSON_STRING), t, sizeof(t)/sizeof(t[0])); if (r < 0) { @@ -195,26 +195,30 @@ request_password_from_external_source(char *username, char *password) found_password = true; i++; } else if (jsoneq(JSON_STRING, &t[i], "Expiration") == 0) { - printf("\n333333"); + printf("\n333333b"); printf("- Expiration: %.*s\n", t[i+1].end-t[i+1].start, JSON_STRING + t[i+1].start); - printf("\n333333-end1"); i++; - printf("\n333333-end2"); + printf("\n333333e"); } else { printf("\n44444444"); printf("Unexpected key: %.*s\n", t[i].end-t[i].start, JSON_STRING + t[i].start); } - if (found_username && found_password) - { - free(username); - username = pg_strdup(new_username); - password = new_password; - return true; - } } printf("fetched all params"); + if (found_username && found_password) + { + free(username); + username = pg_strdup(new_username); +// password = pg_strdup(new_password); + printf("Found username: %s, Password: %s\n", new_username, new_password); +// snprintf(password, sizeof(new_password), "%s", new_password); + password = pg_strdup(new_password); +//sprintf(password, "%s", new_password); + printf("Found username: %s, Password: %s\n", username, password); + return true; + } return false; /* Read the output a line at a time - output it. * / diff --git a/src/bin/psql/startup.c b/src/bin/psql/startup.c index 88071df2e9511..8e2986df6374f 100644 --- a/src/bin/psql/startup.c +++ b/src/bin/psql/startup.c @@ -214,14 +214,20 @@ main(int argc, char *argv[]) { if (pset.credential_source == AWS_IAM_REDSHIFT) { - if (request_password_from_external_source(&options.username, password)) + printf("Entry1"); + char *fetch_password = NULL; + if (request_password_from_external_source(options.username, fetch_password)) { + printf("Username: %s, Password: %s\n", options.username, fetch_password); + sprintf(password, "%s", fetch_password); have_password = true; } else { printf("Unable to fetch Username / Password from IAM"); } + if (fetch_password) + free(fetch_password); } else if (pset.credential_source == DEFAULT) { @@ -273,7 +279,8 @@ main(int argc, char *argv[]) if (pset.credential_source == AWS_IAM_REDSHIFT) { PQfinish(pset.db); - if (request_password_from_external_source(&options.username, password)) + printf("Entry2"); + if (request_password_from_external_source(options.username, password)) { have_password = true; new_pass = true; From 025bc0a172d5bfd0b6c9fabdaf4af36b6ddf10c6 Mon Sep 17 00:00:00 2001 From: Robins Tharakan Date: Sat, 23 Sep 2017 02:51:37 +1000 Subject: [PATCH 105/149] Missed out committing these changes. Can't now recollect the state of affairs in the previous run of coding. Comitting present state to start afresh. bummer. --- src/bin/psql/command.c | 13 ++++++++++++- src/bin/psql/common.c | 6 +++--- src/bin/psql/common.h | 2 +- src/bin/psql/startup.c | 17 ++++++++++------- 4 files changed, 26 insertions(+), 12 deletions(-) diff --git a/src/bin/psql/command.c b/src/bin/psql/command.c index 3c532d4c8d7c1..81182e3bd95f0 100644 --- a/src/bin/psql/command.c +++ b/src/bin/psql/command.c @@ -2888,6 +2888,7 @@ do_connect(enum trivalue reuse_previous_specification, PGconn *o_conn = pset.db, *n_conn; char *password = NULL; + char *new_password = NULL; bool keep_password; bool has_connection_string; bool reuse_previous; @@ -2978,7 +2979,17 @@ do_connect(enum trivalue reuse_previous_specification, if (pset.credential_source == AWS_IAM_REDSHIFT) { printf("Entry3"); - request_password_from_external_source(user, password); + if (request_password_from_external_source(user, &new_password)) + { + printf("Username: %s, Password: %s, Len: %d \n", user, new_password, strlen(new_password)); + sprintf(password, "%s", new_password); + password[strlen(new_password)]='\0'; + printf("Username: %s, Password: %s\n", user, password); + } + else + { + password[0] = '\0'; + } } else if (pset.credential_source == DEFAULT) password = prompt_for_password(user); diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index 3a71953f2c09a..0887f404ad3cb 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -133,7 +133,7 @@ setQFout(const char *fname) * For now works only in Linux. */ bool -request_password_from_external_source(char *username, char *password) +request_password_from_external_source(char *username, char **password) { // FILE *fp; // char path[1035]; @@ -214,9 +214,9 @@ request_password_from_external_source(char *username, char *password) // password = pg_strdup(new_password); printf("Found username: %s, Password: %s\n", new_username, new_password); // snprintf(password, sizeof(new_password), "%s", new_password); - password = pg_strdup(new_password); + *password = pg_strdup(new_password); //sprintf(password, "%s", new_password); - printf("Found username: %s, Password: %s\n", username, password); + printf("Found username: %s, Password: %s\n", username, *password); return true; } return false; diff --git a/src/bin/psql/common.h b/src/bin/psql/common.h index 62fbaae768c34..a455d45484adc 100644 --- a/src/bin/psql/common.h +++ b/src/bin/psql/common.h @@ -46,6 +46,6 @@ extern void expand_tilde(char **filename); extern bool recognized_connection_string(const char *connstr); -extern bool request_password_from_external_source(char *username, char *password); +extern bool request_password_from_external_source(char *username, char **password); #endif /* COMMON_H */ diff --git a/src/bin/psql/startup.c b/src/bin/psql/startup.c index 8e2986df6374f..fa3a7f7811644 100644 --- a/src/bin/psql/startup.c +++ b/src/bin/psql/startup.c @@ -99,6 +99,7 @@ main(int argc, char *argv[]) int successResult; bool have_password = false; char password[100]; + char *new_password = NULL; char *password_prompt = NULL; bool new_pass; @@ -215,19 +216,18 @@ main(int argc, char *argv[]) if (pset.credential_source == AWS_IAM_REDSHIFT) { printf("Entry1"); - char *fetch_password = NULL; - if (request_password_from_external_source(options.username, fetch_password)) + if (request_password_from_external_source(options.username, &new_password)) { - printf("Username: %s, Password: %s\n", options.username, fetch_password); - sprintf(password, "%s", fetch_password); + printf("Username: %s, Password: %s\n", options.username, new_password); + sprintf(password, "%s", new_password); have_password = true; } else { printf("Unable to fetch Username / Password from IAM"); } - if (fetch_password) - free(fetch_password); + if (new_password) + free(new_password); } else if (pset.credential_source == DEFAULT) { @@ -280,8 +280,9 @@ main(int argc, char *argv[]) { PQfinish(pset.db); printf("Entry2"); - if (request_password_from_external_source(options.username, password)) + if (request_password_from_external_source(options.username, &new_password)) { + sprintf(password, "%s", new_password); have_password = true; new_pass = true; } @@ -289,6 +290,8 @@ main(int argc, char *argv[]) { printf("Unable to fetch new Username / Password from IAM"); } + if (new_password) + free(new_password); } else { From 659502cbb6343138d57b99e19a9d98db292bad7a Mon Sep 17 00:00:00 2001 From: Robins Tharakan Date: Sat, 23 Sep 2017 04:17:32 +1000 Subject: [PATCH 106/149] Still WIP on understand C, more than understanding the issue. We're able to parse JSON and store username / password into an array, but unable to do that for all corner-cases yet --- src/bin/psql/command.c | 4 +++- src/bin/psql/common.c | 30 +++++++++++++++--------------- src/bin/psql/common.h | 2 +- src/bin/psql/startup.c | 10 ++++++---- 4 files changed, 25 insertions(+), 21 deletions(-) diff --git a/src/bin/psql/command.c b/src/bin/psql/command.c index 81182e3bd95f0..6d742d3424ae0 100644 --- a/src/bin/psql/command.c +++ b/src/bin/psql/command.c @@ -2979,7 +2979,7 @@ do_connect(enum trivalue reuse_previous_specification, if (pset.credential_source == AWS_IAM_REDSHIFT) { printf("Entry3"); - if (request_password_from_external_source(user, &new_password)) + if (request_password_from_external_source(&user, &new_password)) { printf("Username: %s, Password: %s, Len: %d \n", user, new_password, strlen(new_password)); sprintf(password, "%s", new_password); @@ -3045,6 +3045,8 @@ do_connect(enum trivalue reuse_previous_specification, keywords[++paramnum] = NULL; values[paramnum] = NULL; + printf("3048: Username: %s, Password: %s, PasswordLen: %d, Database: %s \n", user, password, strlen(password), dbname); + n_conn = PQconnectdbParams(keywords, values, true); pg_free(keywords); diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index 0887f404ad3cb..8a0071e2a9b22 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -37,7 +37,8 @@ static bool is_select_command(const char *query); static const char *JSON_STRING = -"{\"DbUser\": \"tempdel\", \"DbPassword\": \"tempdel\", \"Expiration\": \"2017-09-16T11:12:37.608Z\"}"; +//"{\"DbUser\": \"redshift2\", \"DbPassword\": \"Redshift2\", \"Expiration\": \"2017-09-16T11:12:37.608Z\"}"; +"{\"DbUser\": \"IAM:redshift2\", \"DbPassword\": \"BYYEjOoHP+CK08q05vZZHNaE1j0N4SCOkbaugGMQwlmBHSsVZkeZroJ+6EeeD8isYSphiZA==\", \"Expiration\": \"2017-09-16T11:12:37.608Z\"}"; static int jsoneq(const char *json, jsmntok_t *tok, const char *s) { @@ -133,7 +134,7 @@ setQFout(const char *fname) * For now works only in Linux. */ bool -request_password_from_external_source(char *username, char **password) +request_password_from_external_source(char **username, char **password) { // FILE *fp; // char path[1035]; @@ -179,44 +180,43 @@ request_password_from_external_source(char *username, char **password) */ /* Loop over all keys of the root object */ for (i = 1; i < r; i++) { - printf("\n0000000000 r=%d i=%d : ", r, i); + //printf("\n0000000000 r=%d i=%d : ", r, i); if (jsoneq(JSON_STRING, &t[i], "DbUser") == 0) { - printf("\n1111111"); + //printf("\n1111111"); new_username = pg_malloc(t[i+1].end-t[i+1].start + 2); StrNCpy(new_username, JSON_STRING + t[i+1].start, t[i+1].end-t[i+1].start + 1); - printf("- Username: %s\n", new_username); + //printf("- Username: %s\n", new_username); found_username = true; i++; } else if (jsoneq(JSON_STRING, &t[i], "DbPassword") == 0) { - printf("\n22222222"); + //printf("\n22222222"); new_password = pg_malloc(t[i+1].end-t[i+1].start + 2); StrNCpy(new_password, JSON_STRING + t[i+1].start, t[i+1].end-t[i+1].start + 1); - printf("- Password: %s", new_password); + //printf("- Password: %s", new_password); found_password = true; i++; } else if (jsoneq(JSON_STRING, &t[i], "Expiration") == 0) { - printf("\n333333b"); + //printf("\n333333b"); printf("- Expiration: %.*s\n", t[i+1].end-t[i+1].start, JSON_STRING + t[i+1].start); i++; - printf("\n333333e"); + //printf("\n333333e"); } else { - printf("\n44444444"); + //printf("\n44444444"); printf("Unexpected key: %.*s\n", t[i].end-t[i].start, JSON_STRING + t[i].start); } } - printf("fetched all params"); + //printf("fetched all params"); if (found_username && found_password) { - free(username); - username = pg_strdup(new_username); + *username = pg_strdup(new_username); // password = pg_strdup(new_password); - printf("Found username: %s, Password: %s\n", new_username, new_password); + printf("PRE: Found username: %s, Password: %s\n", new_username, new_password); // snprintf(password, sizeof(new_password), "%s", new_password); *password = pg_strdup(new_password); //sprintf(password, "%s", new_password); - printf("Found username: %s, Password: %s\n", username, *password); + printf("POST: Found username: %s, Password: %s\n", *username, *password); return true; } return false; diff --git a/src/bin/psql/common.h b/src/bin/psql/common.h index a455d45484adc..904e9ffe5cb88 100644 --- a/src/bin/psql/common.h +++ b/src/bin/psql/common.h @@ -46,6 +46,6 @@ extern void expand_tilde(char **filename); extern bool recognized_connection_string(const char *connstr); -extern bool request_password_from_external_source(char *username, char **password); +extern bool request_password_from_external_source(char **username, char **password); #endif /* COMMON_H */ diff --git a/src/bin/psql/startup.c b/src/bin/psql/startup.c index fa3a7f7811644..894e55e3ed28a 100644 --- a/src/bin/psql/startup.c +++ b/src/bin/psql/startup.c @@ -215,8 +215,8 @@ main(int argc, char *argv[]) { if (pset.credential_source == AWS_IAM_REDSHIFT) { - printf("Entry1"); - if (request_password_from_external_source(options.username, &new_password)) + printf("Entry1\n"); + if (request_password_from_external_source(&(options.username), &new_password)) { printf("Username: %s, Password: %s\n", options.username, new_password); sprintf(password, "%s", new_password); @@ -266,6 +266,8 @@ main(int argc, char *argv[]) keywords[7] = NULL; values[7] = NULL; + printf("269: Username: %s, Password: %s, PasswordLen: %d, Database: %s \n", values[2], values[3], strlen(values[3]), values[4]); + new_pass = false; pset.db = PQconnectdbParams(keywords, values, true); free(keywords); @@ -279,8 +281,8 @@ main(int argc, char *argv[]) if (pset.credential_source == AWS_IAM_REDSHIFT) { PQfinish(pset.db); - printf("Entry2"); - if (request_password_from_external_source(options.username, &new_password)) + printf("Entry2\n"); + if (request_password_from_external_source(&(options.username), &new_password)) { sprintf(password, "%s", new_password); have_password = true; From b85c320b3b35ab89db7bb28e6fae28a2690e28cc Mon Sep 17 00:00:00 2001 From: Robins Tharakan Date: Mon, 25 Sep 2017 05:26:16 +1000 Subject: [PATCH 107/149] Still testing. WIP --- src/bin/psql/common.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index 8a0071e2a9b22..3f1aa657440de 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -38,7 +38,7 @@ static bool is_select_command(const char *query); static const char *JSON_STRING = //"{\"DbUser\": \"redshift2\", \"DbPassword\": \"Redshift2\", \"Expiration\": \"2017-09-16T11:12:37.608Z\"}"; -"{\"DbUser\": \"IAM:redshift2\", \"DbPassword\": \"BYYEjOoHP+CK08q05vZZHNaE1j0N4SCOkbaugGMQwlmBHSsVZkeZroJ+6EeeD8isYSphiZA==\", \"Expiration\": \"2017-09-16T11:12:37.608Z\"}"; +"{\"DbUser\": \"IAM:redshift2\", \"DbPassword\": \"BIKXgrJ+KurOerMkFF/IZJyzqcUxjAcqWV2aPJBjkX6DdW8VZDOfI75/FEeeC3zGQQAO6oQ==\", \"Expiration\": \"2017-09-16T11:12:37.608Z\"}"; static int jsoneq(const char *json, jsmntok_t *tok, const char *s) { From 1e3a6e310b02fd056960e321e3555976be995a03 Mon Sep 17 00:00:00 2001 From: Robins Tharakan Date: Mon, 25 Sep 2017 06:17:44 +1000 Subject: [PATCH 108/149] Initial commit to support CockroachDB. Still WIP --- src/bin/psql/Makefile | 2 +- src/bin/psql/README.md | 1 + src/bin/psql/README_cockroachdb.md | 45 + src/bin/psql/command.c | 9 +- src/bin/psql/describe.c | 1 + src/bin/psql/input.c | 3 + src/bin/psql/nls.mk | 2 +- src/bin/psql/startup.c | 2 +- src/bin/psql/tab-complete-cockroachdb.c | 4781 +++++++++++++++++++++++ src/bin/psql/tab-complete-cockroachdb.h | 17 + 10 files changed, 4858 insertions(+), 5 deletions(-) create mode 100644 src/bin/psql/README_cockroachdb.md create mode 100644 src/bin/psql/tab-complete-cockroachdb.c create mode 100644 src/bin/psql/tab-complete-cockroachdb.h diff --git a/src/bin/psql/Makefile b/src/bin/psql/Makefile index 99ab5e46c8ee0..02054e0a121a7 100644 --- a/src/bin/psql/Makefile +++ b/src/bin/psql/Makefile @@ -24,7 +24,7 @@ override LDFLAGS := -L$(top_builddir)/src/fe_utils -lpgfeutils $(libpq_pgport) $ OBJS= command.o common.o conditional.o copy.o crosstabview.o \ describe.o help.o input.o large_obj.o mainloop.o \ prompt.o psqlscanslash.o sql_help.o startup.o stringutils.o \ - tab-complete.o tab-complete-pipelinedb.o tab-complete-redshift.o variables.o \ + tab-complete.o tabtab-complete-cockroachdb.o -complete-pipelinedb.o tab-complete-redshift.o variables.o \ $(WIN32RES) diff --git a/src/bin/psql/README.md b/src/bin/psql/README.md index 92103eb638dc9..a03fd0ce49d9f 100644 --- a/src/bin/psql/README.md +++ b/src/bin/psql/README.md @@ -5,4 +5,5 @@ This documents the current state of psql's auto-complete support for various Pos Postgres Forks | Support Level | Description :-------------:| :-----------: | :---------- Amazon Redshift | [Status](https://github.com/robins/postgres/blob/redshift/src/bin/psql/README_redshift.md) | Amazon Redshift was spin off of PostgreSQL (version 8.0.2) +CockroachDB | [Status](https://github.com/robins/postgres/blob/redshift/src/bin/psql/README_cockroachdb.md) | Compatible with PostgreSQL (version 9.5.0) PipelineDB | [Status](https://github.com/robins/postgres/blob/redshift/src/bin/psql/README_pipelinedb.md) | Compatible with PostgreSQL (version 9.5) diff --git a/src/bin/psql/README_cockroachdb.md b/src/bin/psql/README_cockroachdb.md new file mode 100644 index 0000000000000..a65c6255c47ab --- /dev/null +++ b/src/bin/psql/README_cockroachdb.md @@ -0,0 +1,45 @@ +This document shows the current state of psql's auto-complete support for CockroachDB. + +Reference: https://www.cockroachlabs.com/docs/stable/sql-statements.html + +See Support level of all Postgres forks: [Here](https://github.com/robins/postgres/blob/redshift/src/bin/psql/README.md) + + +SQL Commands | Supported State +:----------- |:--------------: +ADD COLUMN | Native +ADD CONSTRAINT | Native +ALTER COLUMN | Native +ALTER TABLE | Native +ALTER VIEW | Native +CREATE DATABASE | Native +CREATE INDEX | Native +CREATE TABLE | Native +CREATE TABLE AS | Native +CREATE VIEW | Native +DROP COLUMN | Native +DROP CONSTRAINT | Native +DROP DATABASE | Native +DROP INDEX | Native +DROP TABLE | Native +DROP VIEW | Native +RENAME COLUMN | Native +RENAME DATABASE | Native +RENAME INDEX | Native +RENAME TABLE | Native +SHOW COLUMNS | Native +SHOW CONSTRAINTS | Native +SHOW CREATE TABLE | Native +SHOW CREATE VIEW | Native +SHOW DATABASES | Native +SHOW INDEX | Native +SHOW TABLES | Native + + +Support Legend | Description +:-------------:| ----------- +Native | All combinations supported by Native psql +Unsupported | Not (yet)supported +Partial | Only some combinations supported +**Yes** | Supported by (only) this branch (not Native) +Complete | All combinations supported \ No newline at end of file diff --git a/src/bin/psql/command.c b/src/bin/psql/command.c index bcbd19d9ae499..4b29163e97c70 100644 --- a/src/bin/psql/command.c +++ b/src/bin/psql/command.c @@ -3242,11 +3242,16 @@ SyncVariables(void) else { guctype = get_guctype("wlm_query_slot_count"); - if (guctype != NULL) server_engine = "redshift"; else - server_engine = "postgres"; + { + guctype = get_guctype("max_index_keys"); + if (guctype != NULL) + server_engine = "cockroachdb"; + else + server_engine = "postgres"; + } } pset.sengine = server_engine; diff --git a/src/bin/psql/describe.c b/src/bin/psql/describe.c index 8958cdbb4a2a1..9a5c28b4c7e87 100644 --- a/src/bin/psql/describe.c +++ b/src/bin/psql/describe.c @@ -27,6 +27,7 @@ #include "variables.h" /* Flag to check if server is a Redshift Engine */ +#define IS_COCKROACHDB (strncmp(pset.sengine, "cockroachdb", 11) == 0) #define IS_REDSHIFT (strncmp(pset.sengine, "redshift", 8) == 0) #define IS_PIPELINEDB (strncmp(pset.sengine, "pipelinedb", 10) == 0) diff --git a/src/bin/psql/input.c b/src/bin/psql/input.c index 256f470aea2c9..5fd6d3d599076 100644 --- a/src/bin/psql/input.c +++ b/src/bin/psql/input.c @@ -18,6 +18,7 @@ #include "tab-complete.h" #include "tab-complete-redshift.h" #include "tab-complete-pipelinedb.h" +#include "tab-complete-cockroachdb.h" #include "common.h" #ifndef WIN32 @@ -360,6 +361,8 @@ initializeInput(int flags) initialize_readline_redshift(); else if (strncmp(pset.sengine, "pipelinedb", 10) == 0) initialize_readline_pipelinedb(); + else if (strncmp(pset.sengine, "cockroachdb", 11) == 0) + initialize_readline_cockroachdb(); else initialize_readline(); rl_initialize(); diff --git a/src/bin/psql/nls.mk b/src/bin/psql/nls.mk index 00548e124439f..164d90e7b8e69 100644 --- a/src/bin/psql/nls.mk +++ b/src/bin/psql/nls.mk @@ -4,7 +4,7 @@ AVAIL_LANGUAGES = cs de es fr he it ja ko pl pt_BR ru sv zh_CN zh_TW GETTEXT_FILES = command.c common.c copy.c crosstabview.c help.c input.c large_obj.c \ mainloop.c psqlscanslash.c startup.c \ describe.c sql_help.h sql_help.c \ - tab-complete.c tab-complete-pipelinedb.c tab-complete-redshift.c variables.c \ + tab-complete.c tab-complete-cockroachdb.c tab-complete-pipelinedb.c tab-complete-redshift.c variables.c \ ../../fe_utils/print.c ../../fe_utils/psqlscan.c \ ../../common/exec.c ../../common/fe_memutils.c ../../common/username.c \ ../../common/wait_error.c diff --git a/src/bin/psql/startup.c b/src/bin/psql/startup.c index 58099f02f0945..c80bbcc93cdb4 100644 --- a/src/bin/psql/startup.c +++ b/src/bin/psql/startup.c @@ -775,7 +775,7 @@ process_psqlrc_file(char *filename) static void showVersion(void) { - puts("psql (PostgreSQL / Amazon Redshift / PipelineDB) " PG_VERSION); + puts("psql (PostgreSQL / Amazon Redshift / CockroachDB / PipelineDB) " PG_VERSION); } diff --git a/src/bin/psql/tab-complete-cockroachdb.c b/src/bin/psql/tab-complete-cockroachdb.c new file mode 100644 index 0000000000000..ee7a9a84f2565 --- /dev/null +++ b/src/bin/psql/tab-complete-cockroachdb.c @@ -0,0 +1,4781 @@ +/* + * psql - the PostgreSQL interactive terminal + * + * Copyright (c) 2000-2017, PostgreSQL Global Development Group + * + * src/bin/psql/tab-complete-cockroachdb.c + */ + +/*---------------------------------------------------------------------- + * This file implements a somewhat more sophisticated readline "TAB + * completion" in psql. It is not intended to be AI, to replace + * learning SQL, or to relieve you from thinking about what you're + * doing. Also it does not always give you all the syntactically legal + * completions, only those that are the most common or the ones that + * the programmer felt most like implementing. + * + * CAVEAT: Tab completion causes queries to be sent to the backend. + * The number of tuples returned gets limited, in most default + * installations to 1000, but if you still don't like this prospect, + * you can turn off tab completion in your ~/.inputrc (or else + * ${INPUTRC}) file so: + * + * $if psql + * set disable-completion on + * $endif + * + * See `man 3 readline' or `info readline' for the full details. + * + * BUGS: + * - Quotes, parentheses, and other funny characters are not handled + * all that gracefully. + *---------------------------------------------------------------------- + */ + +#include "postgres_fe.h" +#include "tab-complete-cockroachdb.h" +#include "input.h" + +/* If we don't have this, we might as well forget about the whole thing: */ +#ifdef USE_READLINE + +#include + +#include "catalog/pg_class.h" + +#include "libpq-fe.h" +#include "pqexpbuffer.h" +#include "common.h" +#include "settings.h" +#include "stringutils.h" + +#ifdef HAVE_RL_FILENAME_COMPLETION_FUNCTION +#define filename_completion_function rl_filename_completion_function +#else +/* missing in some header files */ +extern char *filename_completion_function(); +#endif + +#ifdef HAVE_RL_COMPLETION_MATCHES +#define completion_matches rl_completion_matches +#endif + +/* word break characters */ +#define WORD_BREAKS "\t\n@$><=;|&{() " + +/* + * Since readline doesn't let us pass any state through to the tab completion + * callback, we have to use this global variable to let get_previous_words() + * get at the previous lines of the current command. Ick. + */ +PQExpBuffer tab_completion_query_buf = NULL; + +/* + * This struct is used to define "schema queries", which are custom-built + * to obtain possibly-schema-qualified names of database objects. There is + * enough similarity in the structure that we don't want to repeat it each + * time. So we put the components of each query into this struct and + * assemble them with the common boilerplate in _complete_from_query(). + */ +typedef struct SchemaQuery +{ + /* + * Name of catalog or catalogs to be queried, with alias, eg. + * "pg_catalog.pg_class c". Note that "pg_namespace n" will be added. + */ + const char *catname; + + /* + * Selection condition --- only rows meeting this condition are candidates + * to display. If catname mentions multiple tables, include the necessary + * join condition here. For example, this might look like "c.relkind = " + * CppAsString2(RELKIND_RELATION). Write NULL (not an empty string) if + * not needed. + */ + const char *selcondition; + + /* + * Visibility condition --- which rows are visible without schema + * qualification? For example, "pg_catalog.pg_table_is_visible(c.oid)". + */ + const char *viscondition; + + /* + * Namespace --- name of field to join to pg_namespace.oid. For example, + * "c.relnamespace". + */ + const char *namespace; + + /* + * Result --- the appropriately-quoted name to return, in the case of an + * unqualified name. For example, "pg_catalog.quote_ident(c.relname)". + */ + const char *result; + + /* + * In some cases a different result must be used for qualified names. + * Enter that here, or write NULL if result can be used. + */ + const char *qualresult; +} SchemaQuery; + + +/* Store maximum number of records we want from database queries + * (implemented via SELECT ... LIMIT xx). + */ +static int completion_max_records; + +/* + * Communication variables set by COMPLETE_WITH_FOO macros and then used by + * the completion callback functions. Ugly but there is no better way. + */ +static const char *completion_charp; /* to pass a string */ +static const char *const *completion_charpp; /* to pass a list of strings */ +static const char *completion_info_charp; /* to pass a second string */ +static const char *completion_info_charp2; /* to pass a third string */ +static const SchemaQuery *completion_squery; /* to pass a SchemaQuery */ +static bool completion_case_sensitive; /* completion is case sensitive */ + +/* + * A few macros to ease typing. You can use these to complete the given + * string with + * 1) The results from a query you pass it. (Perhaps one of those below?) + * 2) The results from a schema query you pass it. + * 3) The items from a null-pointer-terminated list (with or without + * case-sensitive comparison; see also COMPLETE_WITH_LISTn, below). + * 4) A string constant. + * 5) The list of attributes of the given table (possibly schema-qualified). + * 6/ The list of arguments to the given function (possibly schema-qualified). + */ +#define COMPLETE_WITH_QUERY(query) \ +do { \ + completion_charp = query; \ + matches = completion_matches(text, complete_from_query); \ +} while (0) + +#define COMPLETE_WITH_SCHEMA_QUERY(query, addon) \ +do { \ + completion_squery = &(query); \ + completion_charp = addon; \ + matches = completion_matches(text, complete_from_schema_query); \ +} while (0) + +#define COMPLETE_WITH_LIST_CS(list) \ +do { \ + completion_charpp = list; \ + completion_case_sensitive = true; \ + matches = completion_matches(text, complete_from_list); \ +} while (0) + +#define COMPLETE_WITH_LIST(list) \ +do { \ + completion_charpp = list; \ + completion_case_sensitive = false; \ + matches = completion_matches(text, complete_from_list); \ +} while (0) + +#define COMPLETE_WITH_CONST(string) \ +do { \ + completion_charp = string; \ + completion_case_sensitive = false; \ + matches = completion_matches(text, complete_from_const); \ +} while (0) + +#define COMPLETE_WITH_ATTR(relation, addon) \ +do { \ + char *_completion_schema; \ + char *_completion_table; \ +\ + _completion_schema = strtokx(relation, " \t\n\r", ".", "\"", 0, \ + false, false, pset.encoding); \ + (void) strtokx(NULL, " \t\n\r", ".", "\"", 0, \ + false, false, pset.encoding); \ + _completion_table = strtokx(NULL, " \t\n\r", ".", "\"", 0, \ + false, false, pset.encoding); \ + if (_completion_table == NULL) \ + { \ + completion_charp = Query_for_list_of_attributes addon; \ + completion_info_charp = relation; \ + } \ + else \ + { \ + completion_charp = Query_for_list_of_attributes_with_schema addon; \ + completion_info_charp = _completion_table; \ + completion_info_charp2 = _completion_schema; \ + } \ + matches = completion_matches(text, complete_from_query); \ +} while (0) + +#define COMPLETE_WITH_ENUM_VALUE(type) \ +do { \ + char *_completion_schema; \ + char *_completion_type; \ +\ + _completion_schema = strtokx(type, " \t\n\r", ".", "\"", 0, \ + false, false, pset.encoding); \ + (void) strtokx(NULL, " \t\n\r", ".", "\"", 0, \ + false, false, pset.encoding); \ + _completion_type = strtokx(NULL, " \t\n\r", ".", "\"", 0, \ + false, false, pset.encoding); \ + if (_completion_type == NULL)\ + { \ + completion_charp = Query_for_list_of_enum_values; \ + completion_info_charp = type; \ + } \ + else \ + { \ + completion_charp = Query_for_list_of_enum_values_with_schema; \ + completion_info_charp = _completion_type; \ + completion_info_charp2 = _completion_schema; \ + } \ + matches = completion_matches(text, complete_from_query); \ +} while (0) + +#define COMPLETE_WITH_FUNCTION_ARG(function) \ +do { \ + char *_completion_schema; \ + char *_completion_function; \ +\ + _completion_schema = strtokx(function, " \t\n\r", ".", "\"", 0, \ + false, false, pset.encoding); \ + (void) strtokx(NULL, " \t\n\r", ".", "\"", 0, \ + false, false, pset.encoding); \ + _completion_function = strtokx(NULL, " \t\n\r", ".", "\"", 0, \ + false, false, pset.encoding); \ + if (_completion_function == NULL) \ + { \ + completion_charp = Query_for_list_of_arguments; \ + completion_info_charp = function; \ + } \ + else \ + { \ + completion_charp = Query_for_list_of_arguments_with_schema; \ + completion_info_charp = _completion_function; \ + completion_info_charp2 = _completion_schema; \ + } \ + matches = completion_matches(text, complete_from_query); \ +} while (0) + +/* + * These macros simplify use of COMPLETE_WITH_LIST for short, fixed lists. + * There is no COMPLETE_WITH_LIST1; use COMPLETE_WITH_CONST for that case. + */ +#define COMPLETE_WITH_LIST2(s1, s2) \ +do { \ + static const char *const list[] = { s1, s2, NULL }; \ + COMPLETE_WITH_LIST(list); \ +} while (0) + +#define COMPLETE_WITH_LIST3(s1, s2, s3) \ +do { \ + static const char *const list[] = { s1, s2, s3, NULL }; \ + COMPLETE_WITH_LIST(list); \ +} while (0) + +#define COMPLETE_WITH_LIST4(s1, s2, s3, s4) \ +do { \ + static const char *const list[] = { s1, s2, s3, s4, NULL }; \ + COMPLETE_WITH_LIST(list); \ +} while (0) + +#define COMPLETE_WITH_LIST5(s1, s2, s3, s4, s5) \ +do { \ + static const char *const list[] = { s1, s2, s3, s4, s5, NULL }; \ + COMPLETE_WITH_LIST(list); \ +} while (0) + +#define COMPLETE_WITH_LIST6(s1, s2, s3, s4, s5, s6) \ +do { \ + static const char *const list[] = { s1, s2, s3, s4, s5, s6, NULL }; \ + COMPLETE_WITH_LIST(list); \ +} while (0) + +#define COMPLETE_WITH_LIST7(s1, s2, s3, s4, s5, s6, s7) \ +do { \ + static const char *const list[] = { s1, s2, s3, s4, s5, s6, s7, NULL }; \ + COMPLETE_WITH_LIST(list); \ +} while (0) + +#define COMPLETE_WITH_LIST8(s1, s2, s3, s4, s5, s6, s7, s8) \ +do { \ + static const char *const list[] = { s1, s2, s3, s4, s5, s6, s7, s8, NULL }; \ + COMPLETE_WITH_LIST(list); \ +} while (0) + +#define COMPLETE_WITH_LIST9(s1, s2, s3, s4, s5, s6, s7, s8, s9) \ +do { \ + static const char *const list[] = { s1, s2, s3, s4, s5, s6, s7, s8, s9, NULL }; \ + COMPLETE_WITH_LIST(list); \ +} while (0) + +#define COMPLETE_WITH_LIST10(s1, s2, s3, s4, s5, s6, s7, s8, s9, s10) \ +do { \ + static const char *const list[] = { s1, s2, s3, s4, s5, s6, s7, s8, s9, s10, NULL }; \ + COMPLETE_WITH_LIST(list); \ +} while (0) + +#define COMPLETE_WITH_LIST11(s1, s2, s3, s4, s5, s6, s7, s8, s9, s10, s11) \ +do { \ + static const char *const list[] = { s1, s2, s3, s4, s5, s6, s7, s8, s9, s10, s11, NULL }; \ + COMPLETE_WITH_LIST(list); \ +} while (0) + +#define COMPLETE_WITH_LIST12(s1, s2, s3, s4, s5, s6, s7, s8, s9, s10, s11, s12) \ +do { \ + static const char *const list[] = { s1, s2, s3, s4, s5, s6, s7, s8, s9, s10, s11, s12, NULL }; \ + COMPLETE_WITH_LIST(list); \ +} while (0) + +/* + * Likewise for COMPLETE_WITH_LIST_CS. + */ +#define COMPLETE_WITH_LIST_CS2(s1, s2) \ +do { \ + static const char *const list[] = { s1, s2, NULL }; \ + COMPLETE_WITH_LIST_CS(list); \ +} while (0) + +#define COMPLETE_WITH_LIST_CS3(s1, s2, s3) \ +do { \ + static const char *const list[] = { s1, s2, s3, NULL }; \ + COMPLETE_WITH_LIST_CS(list); \ +} while (0) + +#define COMPLETE_WITH_LIST_CS4(s1, s2, s3, s4) \ +do { \ + static const char *const list[] = { s1, s2, s3, s4, NULL }; \ + COMPLETE_WITH_LIST_CS(list); \ +} while (0) + +#define COMPLETE_WITH_LIST_CS5(s1, s2, s3, s4, s5) \ +do { \ + static const char *const list[] = { s1, s2, s3, s4, s5, NULL }; \ + COMPLETE_WITH_LIST_CS(list); \ +} while (0) + +/* + * Assembly instructions for schema queries + */ + +static const SchemaQuery Query_for_list_of_aggregates = { + /* catname */ + "pg_catalog.pg_proc p", + /* selcondition */ + "p.proisagg", + /* viscondition */ + "pg_catalog.pg_function_is_visible(p.oid)", + /* namespace */ + "p.pronamespace", + /* result */ + "pg_catalog.quote_ident(p.proname)", + /* qualresult */ + NULL +}; + +static const SchemaQuery Query_for_list_of_datatypes = { + /* catname */ + "pg_catalog.pg_type t", + /* selcondition --- ignore table rowtypes and array types */ + "(t.typrelid = 0 " + " OR (SELECT c.relkind = " CppAsString2(RELKIND_COMPOSITE_TYPE) + " FROM pg_catalog.pg_class c WHERE c.oid = t.typrelid)) " + "AND t.typname !~ '^_'", + /* viscondition */ + "pg_catalog.pg_type_is_visible(t.oid)", + /* namespace */ + "t.typnamespace", + /* result */ + "pg_catalog.format_type(t.oid, NULL)", + /* qualresult */ + "pg_catalog.quote_ident(t.typname)" +}; + +static const SchemaQuery Query_for_list_of_domains = { + /* catname */ + "pg_catalog.pg_type t", + /* selcondition */ + "t.typtype = 'd'", + /* viscondition */ + "pg_catalog.pg_type_is_visible(t.oid)", + /* namespace */ + "t.typnamespace", + /* result */ + "pg_catalog.quote_ident(t.typname)", + /* qualresult */ + NULL +}; + +static const SchemaQuery Query_for_list_of_functions = { + /* catname */ + "pg_catalog.pg_proc p", + /* selcondition */ + NULL, + /* viscondition */ + "pg_catalog.pg_function_is_visible(p.oid)", + /* namespace */ + "p.pronamespace", + /* result */ + "pg_catalog.quote_ident(p.proname)", + /* qualresult */ + NULL +}; + +static const SchemaQuery Query_for_list_of_indexes = { + /* catname */ + "pg_catalog.pg_class c", + /* selcondition */ + "c.relkind IN (" CppAsString2(RELKIND_INDEX) ")", + /* viscondition */ + "pg_catalog.pg_table_is_visible(c.oid)", + /* namespace */ + "c.relnamespace", + /* result */ + "pg_catalog.quote_ident(c.relname)", + /* qualresult */ + NULL +}; + +static const SchemaQuery Query_for_list_of_sequences = { + /* catname */ + "pg_catalog.pg_class c", + /* selcondition */ + "c.relkind IN (" CppAsString2(RELKIND_SEQUENCE) ")", + /* viscondition */ + "pg_catalog.pg_table_is_visible(c.oid)", + /* namespace */ + "c.relnamespace", + /* result */ + "pg_catalog.quote_ident(c.relname)", + /* qualresult */ + NULL +}; + +static const SchemaQuery Query_for_list_of_foreign_tables = { + /* catname */ + "pg_catalog.pg_class c", + /* selcondition */ + "c.relkind IN (" CppAsString2(RELKIND_FOREIGN_TABLE) ")", + /* viscondition */ + "pg_catalog.pg_table_is_visible(c.oid)", + /* namespace */ + "c.relnamespace", + /* result */ + "pg_catalog.quote_ident(c.relname)", + /* qualresult */ + NULL +}; + +static const SchemaQuery Query_for_list_of_tables = { + /* catname */ + "pg_catalog.pg_class c", + /* selcondition */ + "c.relkind IN (" CppAsString2(RELKIND_RELATION) ", " + CppAsString2(RELKIND_PARTITIONED_TABLE) ")", + /* viscondition */ + "pg_catalog.pg_table_is_visible(c.oid)", + /* namespace */ + "c.relnamespace", + /* result */ + "pg_catalog.quote_ident(c.relname)", + /* qualresult */ + NULL +}; + +static const SchemaQuery Query_for_list_of_partitioned_tables = { + /* catname */ + "pg_catalog.pg_class c", + /* selcondition */ + "c.relkind IN (" CppAsString2(RELKIND_PARTITIONED_TABLE) ")", + /* viscondition */ + "pg_catalog.pg_table_is_visible(c.oid)", + /* namespace */ + "c.relnamespace", + /* result */ + "pg_catalog.quote_ident(c.relname)", + /* qualresult */ + NULL +}; + +static const SchemaQuery Query_for_list_of_constraints_with_schema = { + /* catname */ + "pg_catalog.pg_constraint c", + /* selcondition */ + "c.conrelid <> 0", + /* viscondition */ + "true", /* there is no pg_constraint_is_visible */ + /* namespace */ + "c.connamespace", + /* result */ + "pg_catalog.quote_ident(c.conname)", + /* qualresult */ + NULL +}; + +/* Relations supporting INSERT, UPDATE or DELETE */ +static const SchemaQuery Query_for_list_of_updatables = { + /* catname */ + "pg_catalog.pg_class c", + /* selcondition */ + "c.relkind IN (" CppAsString2(RELKIND_RELATION) ", " + CppAsString2(RELKIND_FOREIGN_TABLE) ", " + CppAsString2(RELKIND_VIEW) ", " + CppAsString2(RELKIND_PARTITIONED_TABLE) ")", + /* viscondition */ + "pg_catalog.pg_table_is_visible(c.oid)", + /* namespace */ + "c.relnamespace", + /* result */ + "pg_catalog.quote_ident(c.relname)", + /* qualresult */ + NULL +}; + +static const SchemaQuery Query_for_list_of_relations = { + /* catname */ + "pg_catalog.pg_class c", + /* selcondition */ + NULL, + /* viscondition */ + "pg_catalog.pg_table_is_visible(c.oid)", + /* namespace */ + "c.relnamespace", + /* result */ + "pg_catalog.quote_ident(c.relname)", + /* qualresult */ + NULL +}; + +static const SchemaQuery Query_for_list_of_tsvmf = { + /* catname */ + "pg_catalog.pg_class c", + /* selcondition */ + "c.relkind IN (" CppAsString2(RELKIND_RELATION) ", " + CppAsString2(RELKIND_SEQUENCE) ", " + CppAsString2(RELKIND_VIEW) ", " + CppAsString2(RELKIND_MATVIEW) ", " + CppAsString2(RELKIND_FOREIGN_TABLE) ", " + CppAsString2(RELKIND_PARTITIONED_TABLE) ")", + /* viscondition */ + "pg_catalog.pg_table_is_visible(c.oid)", + /* namespace */ + "c.relnamespace", + /* result */ + "pg_catalog.quote_ident(c.relname)", + /* qualresult */ + NULL +}; + +static const SchemaQuery Query_for_list_of_tmf = { + /* catname */ + "pg_catalog.pg_class c", + /* selcondition */ + "c.relkind IN (" CppAsString2(RELKIND_RELATION) ", " + CppAsString2(RELKIND_MATVIEW) ", " + CppAsString2(RELKIND_FOREIGN_TABLE) ")", + /* viscondition */ + "pg_catalog.pg_table_is_visible(c.oid)", + /* namespace */ + "c.relnamespace", + /* result */ + "pg_catalog.quote_ident(c.relname)", + /* qualresult */ + NULL +}; + +static const SchemaQuery Query_for_list_of_tm = { + /* catname */ + "pg_catalog.pg_class c", + /* selcondition */ + "c.relkind IN (" CppAsString2(RELKIND_RELATION) ", " + CppAsString2(RELKIND_MATVIEW) ")", + /* viscondition */ + "pg_catalog.pg_table_is_visible(c.oid)", + /* namespace */ + "c.relnamespace", + /* result */ + "pg_catalog.quote_ident(c.relname)", + /* qualresult */ + NULL +}; + +static const SchemaQuery Query_for_list_of_views = { + /* catname */ + "pg_catalog.pg_class c", + /* selcondition */ + "c.relkind IN (" CppAsString2(RELKIND_VIEW) ")", + /* viscondition */ + "pg_catalog.pg_table_is_visible(c.oid)", + /* namespace */ + "c.relnamespace", + /* result */ + "pg_catalog.quote_ident(c.relname)", + /* qualresult */ + NULL +}; + +static const SchemaQuery Query_for_list_of_matviews = { + /* catname */ + "pg_catalog.pg_class c", + /* selcondition */ + "c.relkind IN (" CppAsString2(RELKIND_MATVIEW) ")", + /* viscondition */ + "pg_catalog.pg_table_is_visible(c.oid)", + /* namespace */ + "c.relnamespace", + /* result */ + "pg_catalog.quote_ident(c.relname)", + /* qualresult */ + NULL +}; + +static const SchemaQuery Query_for_list_of_statistics = { + /* catname */ + "pg_catalog.pg_statistic_ext s", + /* selcondition */ + NULL, + /* viscondition */ + "pg_catalog.pg_statistics_obj_is_visible(s.oid)", + /* namespace */ + "s.stxnamespace", + /* result */ + "pg_catalog.quote_ident(s.stxname)", + /* qualresult */ + NULL +}; + + +/* + * Queries to get lists of names of various kinds of things, possibly + * restricted to names matching a partially entered name. In these queries, + * the first %s will be replaced by the text entered so far (suitably escaped + * to become a SQL literal string). %d will be replaced by the length of the + * string (in unescaped form). A second and third %s, if present, will be + * replaced by a suitably-escaped version of the string provided in + * completion_info_charp. A fourth and fifth %s are similarly replaced by + * completion_info_charp2. + * + * Beware that the allowed sequences of %s and %d are determined by + * _complete_from_query(). + */ + +#define Query_for_list_of_attributes \ +"SELECT pg_catalog.quote_ident(attname) "\ +" FROM pg_catalog.pg_attribute a, pg_catalog.pg_class c "\ +" WHERE c.oid = a.attrelid "\ +" AND a.attnum > 0 "\ +" AND NOT a.attisdropped "\ +" AND substring(pg_catalog.quote_ident(attname),1,%d)='%s' "\ +" AND (pg_catalog.quote_ident(relname)='%s' "\ +" OR '\"' || relname || '\"'='%s') "\ +" AND pg_catalog.pg_table_is_visible(c.oid)" + +#define Query_for_list_of_attributes_with_schema \ +"SELECT pg_catalog.quote_ident(attname) "\ +" FROM pg_catalog.pg_attribute a, pg_catalog.pg_class c, pg_catalog.pg_namespace n "\ +" WHERE c.oid = a.attrelid "\ +" AND n.oid = c.relnamespace "\ +" AND a.attnum > 0 "\ +" AND NOT a.attisdropped "\ +" AND substring(pg_catalog.quote_ident(attname),1,%d)='%s' "\ +" AND (pg_catalog.quote_ident(relname)='%s' "\ +" OR '\"' || relname || '\"' ='%s') "\ +" AND (pg_catalog.quote_ident(nspname)='%s' "\ +" OR '\"' || nspname || '\"' ='%s') " + +#define Query_for_list_of_cursors \ +" SELECT trim(name) FROM pg_cursors ORDER BY 1" + +#define Query_for_list_of_enum_values \ +"SELECT pg_catalog.quote_literal(enumlabel) "\ +" FROM pg_catalog.pg_enum e, pg_catalog.pg_type t "\ +" WHERE t.oid = e.enumtypid "\ +" AND substring(pg_catalog.quote_literal(enumlabel),1,%d)='%s' "\ +" AND (pg_catalog.quote_ident(typname)='%s' "\ +" OR '\"' || typname || '\"'='%s') "\ +" AND pg_catalog.pg_type_is_visible(t.oid)" + +#define Query_for_list_of_enum_values_with_schema \ +"SELECT pg_catalog.quote_literal(enumlabel) "\ +" FROM pg_catalog.pg_enum e, pg_catalog.pg_type t, pg_catalog.pg_namespace n "\ +" WHERE t.oid = e.enumtypid "\ +" AND n.oid = t.typnamespace "\ +" AND substring(pg_catalog.quote_literal(enumlabel),1,%d)='%s' "\ +" AND (pg_catalog.quote_ident(typname)='%s' "\ +" OR '\"' || typname || '\"'='%s') "\ +" AND (pg_catalog.quote_ident(nspname)='%s' "\ +" OR '\"' || nspname || '\"' ='%s') " + +#define Query_for_list_of_template_databases \ +"SELECT pg_catalog.quote_ident(d.datname) "\ +" FROM pg_catalog.pg_database d "\ +" WHERE substring(pg_catalog.quote_ident(d.datname),1,%d)='%s' "\ +" AND (d.datistemplate OR pg_catalog.pg_has_role(d.datdba, 'USAGE'))" + +#define Query_for_list_of_databases \ +"SELECT pg_catalog.quote_ident(datname) FROM pg_catalog.pg_database "\ +" WHERE substring(pg_catalog.quote_ident(datname),1,%d)='%s'" + +#define Query_for_list_of_tablespaces \ +"SELECT pg_catalog.quote_ident(spcname) FROM pg_catalog.pg_tablespace "\ +" WHERE substring(pg_catalog.quote_ident(spcname),1,%d)='%s'" + +#define Query_for_list_of_encodings \ +" SELECT DISTINCT pg_catalog.pg_encoding_to_char(conforencoding) "\ +" FROM pg_catalog.pg_conversion "\ +" WHERE substring(pg_catalog.pg_encoding_to_char(conforencoding),1,%d)=UPPER('%s')" + +#define Query_for_list_of_languages \ +"SELECT pg_catalog.quote_ident(lanname) "\ +" FROM pg_catalog.pg_language "\ +" WHERE lanname != 'internal' "\ +" AND substring(pg_catalog.quote_ident(lanname),1,%d)='%s'" + +#define Query_for_list_of_schemas \ +"SELECT pg_catalog.quote_ident(nspname) FROM pg_catalog.pg_namespace "\ +" WHERE substring(pg_catalog.quote_ident(nspname),1,%d)='%s'" + +#define Query_for_list_of_alter_system_set_vars \ +"SELECT name FROM "\ +" (SELECT pg_catalog.lower(name) AS name FROM pg_catalog.pg_settings "\ +" WHERE context != 'internal' "\ +" UNION ALL SELECT 'all') ss "\ +" WHERE substring(name,1,%d)='%s'" + +#define Query_for_list_of_pids \ +"SELECT process || ' (user->' || usename || '; database->' || datname || ')' FROM pg_stat_activity ORDER BY datname, usename" + +#define Query_for_list_of_set_vars \ +"SELECT name FROM "\ +" (SELECT pg_catalog.lower(name) AS name FROM pg_catalog.pg_settings "\ +" WHERE context IN ('user', 'superuser') "\ +" UNION ALL SELECT 'constraints' "\ +" UNION ALL SELECT 'transaction' "\ +" UNION ALL SELECT 'session' "\ +" UNION ALL SELECT 'role' "\ +" UNION ALL SELECT 'tablespace' "\ +" UNION ALL SELECT 'all') ss "\ +" WHERE substring(name,1,%d)='%s'" + +#define Query_for_list_of_show_vars \ +"SELECT name FROM "\ +" (SELECT pg_catalog.lower(name) AS name FROM pg_catalog.pg_settings "\ +" UNION ALL SELECT 'session authorization' "\ +" UNION ALL SELECT 'all') ss "\ +" WHERE substring(name,1,%d)='%s'" + +#define Query_for_list_of_roles \ +" SELECT pg_catalog.quote_ident(rolname) "\ +" FROM pg_catalog.pg_roles "\ +" WHERE substring(pg_catalog.quote_ident(rolname),1,%d)='%s'" + +#define Query_for_list_of_users \ +" SELECT pg_catalog.quote_ident(usename) "\ +" FROM pg_catalog.pg_user "\ +" WHERE substring(pg_catalog.quote_ident(usename),1,%d)='%s'" + +#define Query_for_list_of_grant_roles \ +" SELECT pg_catalog.quote_ident(rolname) "\ +" FROM pg_catalog.pg_roles "\ +" WHERE substring(pg_catalog.quote_ident(rolname),1,%d)='%s'"\ +" UNION ALL SELECT 'PUBLIC'"\ +" UNION ALL SELECT 'CURRENT_USER'"\ +" UNION ALL SELECT 'SESSION_USER'" + +/* the silly-looking length condition is just to eat up the current word */ +#define Query_for_table_owning_index \ +"SELECT pg_catalog.quote_ident(c1.relname) "\ +" FROM pg_catalog.pg_class c1, pg_catalog.pg_class c2, pg_catalog.pg_index i"\ +" WHERE c1.oid=i.indrelid and i.indexrelid=c2.oid"\ +" and (%d = pg_catalog.length('%s'))"\ +" and pg_catalog.quote_ident(c2.relname)='%s'"\ +" and pg_catalog.pg_table_is_visible(c2.oid)" + +/* the silly-looking length condition is just to eat up the current word */ +#define Query_for_index_of_table \ +"SELECT pg_catalog.quote_ident(c2.relname) "\ +" FROM pg_catalog.pg_class c1, pg_catalog.pg_class c2, pg_catalog.pg_index i"\ +" WHERE c1.oid=i.indrelid and i.indexrelid=c2.oid"\ +" and (%d = pg_catalog.length('%s'))"\ +" and pg_catalog.quote_ident(c1.relname)='%s'"\ +" and pg_catalog.pg_table_is_visible(c2.oid)" + +/* the silly-looking length condition is just to eat up the current word */ +#define Query_for_constraint_of_table \ +"SELECT pg_catalog.quote_ident(conname) "\ +" FROM pg_catalog.pg_class c1, pg_catalog.pg_constraint con "\ +" WHERE c1.oid=conrelid and (%d = pg_catalog.length('%s'))"\ +" and pg_catalog.quote_ident(c1.relname)='%s'"\ +" and pg_catalog.pg_table_is_visible(c1.oid)" + +#define Query_for_all_table_constraints \ +"SELECT pg_catalog.quote_ident(conname) "\ +" FROM pg_catalog.pg_constraint c "\ +" WHERE c.conrelid <> 0 " + +/* the silly-looking length condition is just to eat up the current word */ +#define Query_for_constraint_of_type \ +"SELECT pg_catalog.quote_ident(conname) "\ +" FROM pg_catalog.pg_type t, pg_catalog.pg_constraint con "\ +" WHERE t.oid=contypid and (%d = pg_catalog.length('%s'))"\ +" and pg_catalog.quote_ident(t.typname)='%s'"\ +" and pg_catalog.pg_type_is_visible(t.oid)" + +/* the silly-looking length condition is just to eat up the current word */ +#define Query_for_list_of_tables_for_constraint \ +"SELECT pg_catalog.quote_ident(relname) "\ +" FROM pg_catalog.pg_class"\ +" WHERE (%d = pg_catalog.length('%s'))"\ +" AND oid IN "\ +" (SELECT conrelid FROM pg_catalog.pg_constraint "\ +" WHERE pg_catalog.quote_ident(conname)='%s')" + +/* the silly-looking length condition is just to eat up the current word */ +#define Query_for_rule_of_table \ +"SELECT pg_catalog.quote_ident(rulename) "\ +" FROM pg_catalog.pg_class c1, pg_catalog.pg_rewrite "\ +" WHERE c1.oid=ev_class and (%d = pg_catalog.length('%s'))"\ +" and pg_catalog.quote_ident(c1.relname)='%s'"\ +" and pg_catalog.pg_table_is_visible(c1.oid)" + +/* the silly-looking length condition is just to eat up the current word */ +#define Query_for_list_of_tables_for_rule \ +"SELECT pg_catalog.quote_ident(relname) "\ +" FROM pg_catalog.pg_class"\ +" WHERE (%d = pg_catalog.length('%s'))"\ +" AND oid IN "\ +" (SELECT ev_class FROM pg_catalog.pg_rewrite "\ +" WHERE pg_catalog.quote_ident(rulename)='%s')" + +/* the silly-looking length condition is just to eat up the current word */ +#define Query_for_trigger_of_table \ +"SELECT pg_catalog.quote_ident(tgname) "\ +" FROM pg_catalog.pg_class c1, pg_catalog.pg_trigger "\ +" WHERE c1.oid=tgrelid and (%d = pg_catalog.length('%s'))"\ +" and pg_catalog.quote_ident(c1.relname)='%s'"\ +" and pg_catalog.pg_table_is_visible(c1.oid)"\ +" and not tgisinternal" + +/* the silly-looking length condition is just to eat up the current word */ +#define Query_for_list_of_tables_for_trigger \ +"SELECT pg_catalog.quote_ident(relname) "\ +" FROM pg_catalog.pg_class"\ +" WHERE (%d = pg_catalog.length('%s'))"\ +" AND oid IN "\ +" (SELECT tgrelid FROM pg_catalog.pg_trigger "\ +" WHERE pg_catalog.quote_ident(tgname)='%s')" + +#define Query_for_list_of_ts_configurations \ +"SELECT pg_catalog.quote_ident(cfgname) FROM pg_catalog.pg_ts_config "\ +" WHERE substring(pg_catalog.quote_ident(cfgname),1,%d)='%s'" + +#define Query_for_list_of_ts_dictionaries \ +"SELECT pg_catalog.quote_ident(dictname) FROM pg_catalog.pg_ts_dict "\ +" WHERE substring(pg_catalog.quote_ident(dictname),1,%d)='%s'" + +#define Query_for_list_of_ts_parsers \ +"SELECT pg_catalog.quote_ident(prsname) FROM pg_catalog.pg_ts_parser "\ +" WHERE substring(pg_catalog.quote_ident(prsname),1,%d)='%s'" + +#define Query_for_list_of_ts_templates \ +"SELECT pg_catalog.quote_ident(tmplname) FROM pg_catalog.pg_ts_template "\ +" WHERE substring(pg_catalog.quote_ident(tmplname),1,%d)='%s'" + +#define Query_for_list_of_fdws \ +" SELECT pg_catalog.quote_ident(fdwname) "\ +" FROM pg_catalog.pg_foreign_data_wrapper "\ +" WHERE substring(pg_catalog.quote_ident(fdwname),1,%d)='%s'" + +#define Query_for_list_of_servers \ +" SELECT pg_catalog.quote_ident(srvname) "\ +" FROM pg_catalog.pg_foreign_server "\ +" WHERE substring(pg_catalog.quote_ident(srvname),1,%d)='%s'" + +#define Query_for_list_of_user_mappings \ +" SELECT pg_catalog.quote_ident(usename) "\ +" FROM pg_catalog.pg_user_mappings "\ +" WHERE substring(pg_catalog.quote_ident(usename),1,%d)='%s'" + +#define Query_for_list_of_access_methods \ +" SELECT pg_catalog.quote_ident(amname) "\ +" FROM pg_catalog.pg_am "\ +" WHERE substring(pg_catalog.quote_ident(amname),1,%d)='%s'" + +#define Query_for_list_of_libraries \ +" SELECT pg_catalog.quote_ident(name) "\ +" FROM pg_catalog.pg_library "\ +" WHERE substring(pg_catalog.quote_ident(name),1,%d)='%s'" + +#define Query_for_list_of_publications \ +" SELECT pg_catalog.quote_ident(pubname) "\ +" FROM pg_catalog.pg_publication "\ +" WHERE substring(pg_catalog.quote_ident(pubname),1,%d)='%s'" + +#define Query_for_list_of_subscriptions \ +" SELECT pg_catalog.quote_ident(s.subname) "\ +" FROM pg_catalog.pg_subscription s, pg_catalog.pg_database d "\ +" WHERE substring(pg_catalog.quote_ident(s.subname),1,%d)='%s' "\ +" AND d.datname = pg_catalog.current_database() "\ +" AND s.subdbid = d.oid" + +/* the silly-looking length condition is just to eat up the current word */ +#define Query_for_list_of_arguments \ +"SELECT pg_catalog.oidvectortypes(proargtypes)||')' "\ +" FROM pg_catalog.pg_proc "\ +" WHERE (%d = pg_catalog.length('%s'))"\ +" AND (pg_catalog.quote_ident(proname)='%s'"\ +" OR '\"' || proname || '\"'='%s') "\ +" AND (pg_catalog.pg_function_is_visible(pg_proc.oid))" + +/* the silly-looking length condition is just to eat up the current word */ +#define Query_for_list_of_arguments_with_schema \ +"SELECT pg_catalog.oidvectortypes(proargtypes)||')' "\ +" FROM pg_catalog.pg_proc p, pg_catalog.pg_namespace n "\ +" WHERE (%d = pg_catalog.length('%s'))"\ +" AND n.oid = p.pronamespace "\ +" AND (pg_catalog.quote_ident(proname)='%s' "\ +" OR '\"' || proname || '\"' ='%s') "\ +" AND (pg_catalog.quote_ident(nspname)='%s' "\ +" OR '\"' || nspname || '\"' ='%s') " + +#define Query_for_list_of_extensions \ +" SELECT pg_catalog.quote_ident(extname) "\ +" FROM pg_catalog.pg_extension "\ +" WHERE substring(pg_catalog.quote_ident(extname),1,%d)='%s'" + +#define Query_for_list_of_available_extensions \ +" SELECT pg_catalog.quote_ident(name) "\ +" FROM pg_catalog.pg_available_extensions "\ +" WHERE substring(pg_catalog.quote_ident(name),1,%d)='%s' AND installed_version IS NULL" + +/* the silly-looking length condition is just to eat up the current word */ +#define Query_for_list_of_available_extension_versions \ +" SELECT pg_catalog.quote_ident(version) "\ +" FROM pg_catalog.pg_available_extension_versions "\ +" WHERE (%d = pg_catalog.length('%s'))"\ +" AND pg_catalog.quote_ident(name)='%s'" + +/* the silly-looking length condition is just to eat up the current word */ +#define Query_for_list_of_available_extension_versions_with_TO \ +" SELECT 'TO ' || pg_catalog.quote_ident(version) "\ +" FROM pg_catalog.pg_available_extension_versions "\ +" WHERE (%d = pg_catalog.length('%s'))"\ +" AND pg_catalog.quote_ident(name)='%s'" + +#define Query_for_list_of_prepared_statements \ +" SELECT pg_catalog.quote_ident(name) "\ +" FROM pg_catalog.pg_prepared_statements "\ +" WHERE substring(pg_catalog.quote_ident(name),1,%d)='%s'" + +#define Query_for_list_of_event_triggers \ +" SELECT pg_catalog.quote_ident(evtname) "\ +" FROM pg_catalog.pg_event_trigger "\ +" WHERE substring(pg_catalog.quote_ident(evtname),1,%d)='%s'" + +#define Query_for_list_of_tablesample_methods \ +" SELECT pg_catalog.quote_ident(proname) "\ +" FROM pg_catalog.pg_proc "\ +" WHERE prorettype = 'pg_catalog.tsm_handler'::pg_catalog.regtype AND "\ +" proargtypes[0] = 'pg_catalog.internal'::pg_catalog.regtype AND "\ +" substring(pg_catalog.quote_ident(proname),1,%d)='%s'" + +#define Query_for_list_of_policies \ +" SELECT pg_catalog.quote_ident(polname) "\ +" FROM pg_catalog.pg_policy "\ +" WHERE substring(pg_catalog.quote_ident(polname),1,%d)='%s'" + +#define Query_for_list_of_tables_for_policy \ +"SELECT pg_catalog.quote_ident(relname) "\ +" FROM pg_catalog.pg_class"\ +" WHERE (%d = pg_catalog.length('%s'))"\ +" AND oid IN "\ +" (SELECT polrelid FROM pg_catalog.pg_policy "\ +" WHERE pg_catalog.quote_ident(polname)='%s')" + +#define Query_for_enum \ +" SELECT name FROM ( "\ +" SELECT pg_catalog.quote_ident(pg_catalog.unnest(enumvals)) AS name "\ +" FROM pg_catalog.pg_settings "\ +" WHERE pg_catalog.lower(name)=pg_catalog.lower('%s') "\ +" UNION ALL " \ +" SELECT 'DEFAULT' ) ss "\ +" WHERE pg_catalog.substring(name,1,%%d)='%%s'" + +/* the silly-looking length condition is just to eat up the current word */ +#define Query_for_partition_of_table \ +"SELECT pg_catalog.quote_ident(c2.relname) "\ +" FROM pg_catalog.pg_class c1, pg_catalog.pg_class c2, pg_catalog.pg_inherits i"\ +" WHERE c1.oid=i.inhparent and i.inhrelid=c2.oid"\ +" and (%d = pg_catalog.length('%s'))"\ +" and pg_catalog.quote_ident(c1.relname)='%s'"\ +" and pg_catalog.pg_table_is_visible(c2.oid)"\ +" and c2.relispartition = 'true'" + +/* + * This is a list of all "things" in Pgsql, which can show up after CREATE or + * DROP; and there is also a query to get a list of them. + */ + +typedef struct +{ + const char *name; + const char *query; /* simple query, or NULL */ + const SchemaQuery *squery; /* schema query, or NULL */ + const bits32 flags; /* visibility flags, see below */ +} pgsql_thing_t; + +#define THING_NO_CREATE (1 << 0) /* should not show up after CREATE */ +#define THING_NO_DROP (1 << 1) /* should not show up after DROP */ +#define THING_NO_ALTER (1 << 2) /* should not show up after ALTER */ +#define THING_NO_SHOW (THING_NO_CREATE | THING_NO_DROP | THING_NO_ALTER) + +static const pgsql_thing_t words_after_create[] = { + {"ACCESS METHOD", NULL, NULL, THING_NO_ALTER}, + {"AGGREGATE", NULL, &Query_for_list_of_aggregates}, + {"CAST", NULL, NULL}, /* Casts have complex structures for names, so + * skip it */ + {"COLLATION", "SELECT pg_catalog.quote_ident(collname) FROM pg_catalog.pg_collation WHERE collencoding IN (-1, pg_catalog.pg_char_to_encoding(pg_catalog.getdatabaseencoding())) AND substring(pg_catalog.quote_ident(collname),1,%d)='%s'"}, + + /* + * CREATE CONSTRAINT TRIGGER is not supported here because it is designed + * to be used only by pg_dump. + */ + {"CONFIGURATION", Query_for_list_of_ts_configurations, NULL, THING_NO_SHOW}, + {"CONVERSION", "SELECT pg_catalog.quote_ident(conname) FROM pg_catalog.pg_conversion WHERE substring(pg_catalog.quote_ident(conname),1,%d)='%s'"}, + {"DATABASE", Query_for_list_of_databases}, + {"DEFAULT PRIVILEGES", NULL, NULL, THING_NO_CREATE | THING_NO_DROP}, + {"DICTIONARY", Query_for_list_of_ts_dictionaries, NULL, THING_NO_SHOW}, + {"DOMAIN", NULL, &Query_for_list_of_domains}, + {"EVENT TRIGGER", NULL, NULL}, + {"EXTENSION", Query_for_list_of_extensions}, + {"FOREIGN DATA WRAPPER", NULL, NULL}, + {"FOREIGN TABLE", NULL, NULL}, + {"FUNCTION", NULL, &Query_for_list_of_functions}, + {"GROUP", Query_for_list_of_roles}, + {"INDEX", NULL, &Query_for_list_of_indexes}, + {"LANGUAGE", Query_for_list_of_languages}, + {"LARGE OBJECT", NULL, NULL, THING_NO_CREATE | THING_NO_DROP}, + {"LIBRARY", NULL, NULL, THING_NO_ALTER}, + {"LOCAL", NULL, NULL, THING_NO_DROP | THING_NO_ALTER}, /* CREATE LOCAL TABLE */ + {"MATERIALIZED VIEW", NULL, &Query_for_list_of_matviews}, + {"OPERATOR", NULL, NULL}, /* Querying for this is probably not such a + * good idea. */ + {"OR REPLACE FUNCTION", NULL, &Query_for_list_of_functions}, + {"OR REPLACE LIBRARY", Query_for_list_of_libraries}, + {"OR REPLACE VIEW", NULL, &Query_for_list_of_views}, + {"OWNED", NULL, NULL, THING_NO_CREATE | THING_NO_ALTER}, /* for DROP OWNED BY ... */ + {"PARSER", Query_for_list_of_ts_parsers, NULL, THING_NO_SHOW}, + {"POLICY", NULL, NULL}, + {"PUBLICATION", Query_for_list_of_publications}, + {"ROLE", Query_for_list_of_roles}, + {"RULE", "SELECT pg_catalog.quote_ident(rulename) FROM pg_catalog.pg_rules WHERE substring(pg_catalog.quote_ident(rulename),1,%d)='%s'"}, + {"SCHEMA", Query_for_list_of_schemas}, + {"SEQUENCE", NULL, &Query_for_list_of_sequences}, + {"SERVER", Query_for_list_of_servers}, + {"STATISTICS", NULL, &Query_for_list_of_statistics}, + {"SUBSCRIPTION", Query_for_list_of_subscriptions}, + {"SYSTEM", NULL, NULL, THING_NO_CREATE | THING_NO_DROP}, + {"TABLE", NULL, &Query_for_list_of_tables}, + {"TABLESPACE", Query_for_list_of_tablespaces}, + {"TEMP", NULL, NULL, THING_NO_DROP | THING_NO_ALTER}, /* for CREATE TEMP TABLE + * ... */ + {"TEMPLATE", Query_for_list_of_ts_templates, NULL, THING_NO_SHOW}, + {"TEMPORARY", NULL, NULL, THING_NO_DROP | THING_NO_ALTER}, /* for CREATE TEMPORARY + * TABLE ... */ + {"TEXT SEARCH", NULL, NULL}, + {"TRANSFORM", NULL, NULL}, + {"TRIGGER", "SELECT pg_catalog.quote_ident(tgname) FROM pg_catalog.pg_trigger WHERE substring(pg_catalog.quote_ident(tgname),1,%d)='%s' AND NOT tgisinternal"}, + {"TYPE", NULL, &Query_for_list_of_datatypes}, + {"UNIQUE", NULL, NULL, THING_NO_DROP | THING_NO_ALTER}, /* for CREATE UNIQUE + * INDEX ... */ + {"UNLOGGED", NULL, NULL, THING_NO_DROP | THING_NO_ALTER}, /* for CREATE UNLOGGED + * TABLE ... */ + {"USER", Query_for_list_of_roles " UNION SELECT 'MAPPING FOR'"}, + {"USER MAPPING FOR", NULL, NULL}, + {"VIEW", NULL, &Query_for_list_of_views}, + {NULL} /* end of list */ +}; + + +/* Forward declaration of functions */ +static char **psql_completion(const char *text, int start, int end); +static char *create_command_generator(const char *text, int state); +static char *drop_command_generator(const char *text, int state); +static char *alter_command_generator(const char *text, int state); +static char *complete_from_query(const char *text, int state); +static char *complete_from_schema_query(const char *text, int state); +static char *_complete_from_query(int is_schema_query, + const char *text, int state); +static char *complete_from_list(const char *text, int state); +static char *complete_from_const(const char *text, int state); +static void append_variable_names(char ***varnames, int *nvars, + int *maxvars, const char *varname, + const char *prefix, const char *suffix); +static char **complete_from_variables(const char *text, + const char *prefix, const char *suffix, bool need_value); +static char *complete_from_files(const char *text, int state); + +static char *pg_strdup_keyword_case(const char *s, const char *ref); +static char *escape_string(const char *text); +static PGresult *exec_query(const char *query); + +static char **get_previous_words(int point, char **buffer, int *nwords); + +static char *get_guctype(const char *varname); + +#ifdef NOT_USED +static char *quote_file_name(char *text, int match_type, char *quote_pointer); +static char *dequote_file_name(char *text, char quote_char); +#endif + + +/* + * Initialize the readline library for our purposes. + */ +void +initialize_readline_cockroachdb(void) +{ + rl_readline_name = (char *) pset.progname; + rl_attempted_completion_function = psql_completion; + + rl_basic_word_break_characters = WORD_BREAKS; + + completion_max_records = 1000; + + /* + * There is a variable rl_completion_query_items for this but apparently + * it's not defined everywhere. + */ +} + +/* + * Check if 'word' matches any of the '|'-separated strings in 'pattern', + * using case-insensitive or case-sensitive comparisons. + * + * If pattern is NULL, it's a wild card that matches any word. + * If pattern begins with '!', the result is negated, ie we check that 'word' + * does *not* match any alternative appearing in the rest of 'pattern'. + * Any alternative can end with '*' which is a wild card, i.e., it means + * match any word that matches the characters so far. (We do not currently + * support '*' elsewhere than the end of an alternative.) + * + * For readability, callers should use the macros MatchAny and MatchAnyExcept + * to invoke those two special cases for 'pattern'. (But '|' and '*' must + * just be written directly in patterns.) + */ +#define MatchAny NULL +#define MatchAnyExcept(pattern) ("!" pattern) + +static bool +word_matches_internal(const char *pattern, + const char *word, + bool case_sensitive) +{ + size_t wordlen, + patternlen; + + /* NULL pattern matches anything. */ + if (pattern == NULL) + return true; + + /* Handle negated patterns from the MatchAnyExcept macro. */ + if (*pattern == '!') + return !word_matches_internal(pattern + 1, word, case_sensitive); + + /* Else consider each alternative in the pattern. */ + wordlen = strlen(word); + for (;;) + { + const char *c; + + /* Find end of current alternative. */ + c = pattern; + while (*c != '\0' && *c != '|') + c++; + /* Was there a wild card? (Assumes first alternative is not empty) */ + if (c[-1] == '*') + { + /* Yes, wildcard match? */ + patternlen = c - pattern - 1; + if (wordlen >= patternlen && + (case_sensitive ? + strncmp(word, pattern, patternlen) == 0 : + pg_strncasecmp(word, pattern, patternlen) == 0)) + return true; + } + else + { + /* No, plain match? */ + patternlen = c - pattern; + if (wordlen == patternlen && + (case_sensitive ? + strncmp(word, pattern, wordlen) == 0 : + pg_strncasecmp(word, pattern, wordlen) == 0)) + return true; + } + /* Out of alternatives? */ + if (*c == '\0') + break; + /* Nope, try next alternative. */ + pattern = c + 1; + } + + return false; +} + +/* + * There are enough matching calls below that it seems worth having these two + * interface routines rather than including a third parameter in every call. + * + * word_matches --- match case-insensitively. + */ +static bool +word_matches(const char *pattern, const char *word) +{ + return word_matches_internal(pattern, word, false); +} + +/* + * word_matches_cs --- match case-sensitively. + */ +static bool +word_matches_cs(const char *pattern, const char *word) +{ + return word_matches_internal(pattern, word, true); +} + +/* + * Check if the final character of 's' is 'c'. + */ +static bool +ends_with(const char *s, char c) +{ + size_t length = strlen(s); + + return (length > 0 && s[length - 1] == c); +} + +/* + * The completion function. + * + * According to readline spec this gets passed the text entered so far and its + * start and end positions in the readline buffer. The return value is some + * partially obscure list format that can be generated by readline's + * completion_matches() function, so we don't have to worry about it. + */ +static char ** +psql_completion(const char *text, int start, int end) +{ + /* This is the variable we'll return. */ + char **matches = NULL; + + /* Workspace for parsed words. */ + char *words_buffer; + + /* This array will contain pointers to parsed words. */ + char **previous_words; + + /* The number of words found on the input line. */ + int previous_words_count; + + /* + * For compactness, we use these macros to reference previous_words[]. + * Caution: do not access a previous_words[] entry without having checked + * previous_words_count to be sure it's valid. In most cases below, that + * check is implicit in a TailMatches() or similar macro, but in some + * places we have to check it explicitly. + */ +#define prev_wd (previous_words[0]) +#define prev2_wd (previous_words[1]) +#define prev3_wd (previous_words[2]) +#define prev4_wd (previous_words[3]) +#define prev5_wd (previous_words[4]) +#define prev6_wd (previous_words[5]) +#define prev7_wd (previous_words[6]) +#define prev8_wd (previous_words[7]) +#define prev9_wd (previous_words[8]) + + /* Macros for matching the last N words before point, case-insensitively. */ +#define TailMatches1(p1) \ + (previous_words_count >= 1 && \ + word_matches(p1, prev_wd)) + +#define TailMatches2(p2, p1) \ + (previous_words_count >= 2 && \ + word_matches(p1, prev_wd) && \ + word_matches(p2, prev2_wd)) + +#define TailMatches3(p3, p2, p1) \ + (previous_words_count >= 3 && \ + word_matches(p1, prev_wd) && \ + word_matches(p2, prev2_wd) && \ + word_matches(p3, prev3_wd)) + +#define TailMatches4(p4, p3, p2, p1) \ + (previous_words_count >= 4 && \ + word_matches(p1, prev_wd) && \ + word_matches(p2, prev2_wd) && \ + word_matches(p3, prev3_wd) && \ + word_matches(p4, prev4_wd)) + +#define TailMatches5(p5, p4, p3, p2, p1) \ + (previous_words_count >= 5 && \ + word_matches(p1, prev_wd) && \ + word_matches(p2, prev2_wd) && \ + word_matches(p3, prev3_wd) && \ + word_matches(p4, prev4_wd) && \ + word_matches(p5, prev5_wd)) + +#define TailMatches6(p6, p5, p4, p3, p2, p1) \ + (previous_words_count >= 6 && \ + word_matches(p1, prev_wd) && \ + word_matches(p2, prev2_wd) && \ + word_matches(p3, prev3_wd) && \ + word_matches(p4, prev4_wd) && \ + word_matches(p5, prev5_wd) && \ + word_matches(p6, prev6_wd)) + +#define TailMatches7(p7, p6, p5, p4, p3, p2, p1) \ + (previous_words_count >= 7 && \ + word_matches(p1, prev_wd) && \ + word_matches(p2, prev2_wd) && \ + word_matches(p3, prev3_wd) && \ + word_matches(p4, prev4_wd) && \ + word_matches(p5, prev5_wd) && \ + word_matches(p6, prev6_wd) && \ + word_matches(p7, prev7_wd)) + +#define TailMatches8(p8, p7, p6, p5, p4, p3, p2, p1) \ + (previous_words_count >= 8 && \ + word_matches(p1, prev_wd) && \ + word_matches(p2, prev2_wd) && \ + word_matches(p3, prev3_wd) && \ + word_matches(p4, prev4_wd) && \ + word_matches(p5, prev5_wd) && \ + word_matches(p6, prev6_wd) && \ + word_matches(p7, prev7_wd) && \ + word_matches(p8, prev8_wd)) + +#define TailMatches9(p9, p8, p7, p6, p5, p4, p3, p2, p1) \ + (previous_words_count >= 9 && \ + word_matches(p1, prev_wd) && \ + word_matches(p2, prev2_wd) && \ + word_matches(p3, prev3_wd) && \ + word_matches(p4, prev4_wd) && \ + word_matches(p5, prev5_wd) && \ + word_matches(p6, prev6_wd) && \ + word_matches(p7, prev7_wd) && \ + word_matches(p8, prev8_wd) && \ + word_matches(p9, prev9_wd)) + + /* Macros for matching the last N words before point, case-sensitively. */ +#define TailMatchesCS1(p1) \ + (previous_words_count >= 1 && \ + word_matches_cs(p1, prev_wd)) +#define TailMatchesCS2(p2, p1) \ + (previous_words_count >= 2 && \ + word_matches_cs(p1, prev_wd) && \ + word_matches_cs(p2, prev2_wd)) +#define TailMatchesCS3(p3, p2, p1) \ + (previous_words_count >= 3 && \ + word_matches_cs(p1, prev_wd) && \ + word_matches_cs(p2, prev2_wd) && \ + word_matches_cs(p3, prev3_wd)) +#define TailMatchesCS4(p4, p3, p2, p1) \ + (previous_words_count >= 4 && \ + word_matches_cs(p1, prev_wd) && \ + word_matches_cs(p2, prev2_wd) && \ + word_matches_cs(p3, prev3_wd) && \ + word_matches_cs(p4, prev4_wd)) + + /* + * Macros for matching N words beginning at the start of the line, + * case-insensitively. + */ +#define Matches1(p1) \ + (previous_words_count == 1 && \ + TailMatches1(p1)) +#define Matches2(p1, p2) \ + (previous_words_count == 2 && \ + TailMatches2(p1, p2)) +#define Matches3(p1, p2, p3) \ + (previous_words_count == 3 && \ + TailMatches3(p1, p2, p3)) +#define Matches4(p1, p2, p3, p4) \ + (previous_words_count == 4 && \ + TailMatches4(p1, p2, p3, p4)) +#define Matches5(p1, p2, p3, p4, p5) \ + (previous_words_count == 5 && \ + TailMatches5(p1, p2, p3, p4, p5)) +#define Matches6(p1, p2, p3, p4, p5, p6) \ + (previous_words_count == 6 && \ + TailMatches6(p1, p2, p3, p4, p5, p6)) +#define Matches7(p1, p2, p3, p4, p5, p6, p7) \ + (previous_words_count == 7 && \ + TailMatches7(p1, p2, p3, p4, p5, p6, p7)) +#define Matches8(p1, p2, p3, p4, p5, p6, p7, p8) \ + (previous_words_count == 8 && \ + TailMatches8(p1, p2, p3, p4, p5, p6, p7, p8)) +#define Matches9(p1, p2, p3, p4, p5, p6, p7, p8, p9) \ + (previous_words_count == 9 && \ + TailMatches9(p1, p2, p3, p4, p5, p6, p7, p8, p9)) + + /* + * Macros for matching N words at the start of the line, regardless of + * what is after them, case-insensitively. + */ +#define HeadMatches1(p1) \ + (previous_words_count >= 1 && \ + word_matches(p1, previous_words[previous_words_count - 1])) + +#define HeadMatches2(p1, p2) \ + (previous_words_count >= 2 && \ + word_matches(p1, previous_words[previous_words_count - 1]) && \ + word_matches(p2, previous_words[previous_words_count - 2])) + +#define HeadMatches3(p1, p2, p3) \ + (previous_words_count >= 3 && \ + word_matches(p1, previous_words[previous_words_count - 1]) && \ + word_matches(p2, previous_words[previous_words_count - 2]) && \ + word_matches(p3, previous_words[previous_words_count - 3])) + + #define HeadMatches4(p1, p2, p3, p4) \ + (previous_words_count >= 4 && \ + word_matches(p1, previous_words[previous_words_count - 1]) && \ + word_matches(p2, previous_words[previous_words_count - 2]) && \ + word_matches(p3, previous_words[previous_words_count - 3]) && \ + word_matches(p4, previous_words[previous_words_count - 4])) + + #define HeadMatches5(p1, p2, p3, p4, p5) \ + (previous_words_count >= 5 && \ + word_matches(p1, previous_words[previous_words_count - 1]) && \ + word_matches(p2, previous_words[previous_words_count - 2]) && \ + word_matches(p3, previous_words[previous_words_count - 3]) && \ + word_matches(p4, previous_words[previous_words_count - 4]) && \ + word_matches(p5, previous_words[previous_words_count - 5])) + + #define HeadMatches6(p1, p2, p3, p4, p5, p6) \ + (previous_words_count >= 6 && \ + word_matches(p1, previous_words[previous_words_count - 1]) && \ + word_matches(p2, previous_words[previous_words_count - 2]) && \ + word_matches(p3, previous_words[previous_words_count - 3]) && \ + word_matches(p4, previous_words[previous_words_count - 4]) && \ + word_matches(p5, previous_words[previous_words_count - 5]) && \ + word_matches(p6, previous_words[previous_words_count - 6])) + + #define HeadMatches7(p1, p2, p3, p4, p5, p6, p7) \ + (previous_words_count >= 7 && \ + word_matches(p1, previous_words[previous_words_count - 1]) && \ + word_matches(p2, previous_words[previous_words_count - 2]) && \ + word_matches(p3, previous_words[previous_words_count - 3]) && \ + word_matches(p4, previous_words[previous_words_count - 4]) && \ + word_matches(p5, previous_words[previous_words_count - 5]) && \ + word_matches(p6, previous_words[previous_words_count - 6]) && \ + word_matches(p7, previous_words[previous_words_count - 7])) + + #define HeadMatches8(p1, p2, p3, p4, p5, p6, p7, p8) \ + (previous_words_count >= 8 && \ + word_matches(p1, previous_words[previous_words_count - 1]) && \ + word_matches(p2, previous_words[previous_words_count - 2]) && \ + word_matches(p3, previous_words[previous_words_count - 3]) && \ + word_matches(p4, previous_words[previous_words_count - 4]) && \ + word_matches(p5, previous_words[previous_words_count - 5]) && \ + word_matches(p6, previous_words[previous_words_count - 6]) && \ + word_matches(p7, previous_words[previous_words_count - 7]) && \ + word_matches(p8, previous_words[previous_words_count - 8])) + + #define HeadMatches9(p1, p2, p3, p4, p5, p6, p7, p8, p9) \ + (previous_words_count >= 9 && \ + word_matches(p1, previous_words[previous_words_count - 1]) && \ + word_matches(p2, previous_words[previous_words_count - 2]) && \ + word_matches(p3, previous_words[previous_words_count - 3]) && \ + word_matches(p4, previous_words[previous_words_count - 4]) && \ + word_matches(p5, previous_words[previous_words_count - 5]) && \ + word_matches(p6, previous_words[previous_words_count - 6]) && \ + word_matches(p7, previous_words[previous_words_count - 7]) && \ + word_matches(p8, previous_words[previous_words_count - 8]) && \ + word_matches(p9, previous_words[previous_words_count - 9])) + + /* Known command-starting keywords. */ + static const char *const sql_commands[] = { + "ABORT", "ALTER", "ANALYZE", "BEGIN", "CHECKPOINT", "CLOSE", "CLUSTER", + "COMMENT", "COMMIT", "COPY", "CREATE", "DEALLOCATE", "DECLARE", + "DELETE FROM", "DISCARD", "DO", "DROP", "END", "EXECUTE", "EXPLAIN", + "FETCH", "GRANT", "IMPORT", "INSERT", "LISTEN", "LOAD", "LOCK", + "MOVE", "NOTIFY", "PREPARE", + "REASSIGN", "REFRESH MATERIALIZED VIEW", "REINDEX", "RELEASE", + "RESET", "REVOKE", "ROLLBACK", + "SAVEPOINT", "SECURITY LABEL", "SELECT", "SET", "SHOW", "START", + "TABLE", "TRUNCATE", "UNLISTEN", "UPDATE", "VACUUM", "VALUES", "WITH", + NULL + }; + + /* psql's backslash commands. */ + static const char *const backslash_commands[] = { + "\\a", + "\\connect", "\\conninfo", "\\C", "\\cd", "\\copy", + "\\copyright", "\\crosstabview", + "\\d", "\\da", "\\dA", "\\db", "\\dc", "\\dC", "\\dd", "\\ddp", "\\dD", + "\\des", "\\det", "\\deu", "\\dew", "\\dE", "\\df", + "\\dF", "\\dFd", "\\dFp", "\\dFt", "\\dg", "\\di", "\\dl", "\\dL", + "\\dm", "\\dn", "\\do", "\\dO", "\\dp", + "\\drds", "\\dRs", "\\dRp", "\\ds", "\\dS", + "\\dt", "\\dT", "\\dv", "\\du", "\\dx", "\\dy", + "\\e", "\\echo", "\\ef", "\\elif", "\\else", "\\encoding", + "\\endif", "\\errverbose", "\\ev", + "\\f", + "\\g", "\\gdesc", "\\gexec", "\\gset", "\\gx", + "\\h", "\\help", "\\H", + "\\i", "\\if", "\\ir", + "\\l", "\\lo_import", "\\lo_export", "\\lo_list", "\\lo_unlink", + "\\o", + "\\p", "\\password", "\\prompt", "\\pset", + "\\q", "\\qecho", + "\\r", + "\\s", "\\set", "\\setenv", "\\sf", "\\sv", + "\\t", "\\T", "\\timing", + "\\unset", + "\\x", + "\\w", "\\watch", + "\\z", + "\\!", "\\?", + NULL + }; + + (void) end; /* "end" is not used */ + +#ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER + rl_completion_append_character = ' '; +#endif + + /* Clear a few things. */ + completion_charp = NULL; + completion_charpp = NULL; + completion_info_charp = NULL; + completion_info_charp2 = NULL; + + /* + * Scan the input line to extract the words before our current position. + * According to those we'll make some smart decisions on what the user is + * probably intending to type. + */ + previous_words = get_previous_words(start, + &words_buffer, + &previous_words_count); + + /* If current word is a backslash command, offer completions for that */ + if (text[0] == '\\') + COMPLETE_WITH_LIST_CS(backslash_commands); + + /* If current word is a variable interpolation, handle that case */ + else if (text[0] == ':' && text[1] != ':') + { + if (text[1] == '\'') + matches = complete_from_variables(text, ":'", "'", true); + else if (text[1] == '"') + matches = complete_from_variables(text, ":\"", "\"", true); + else + matches = complete_from_variables(text, ":", "", true); + } + + /* If no previous word, suggest one of the basic sql commands */ + else if (previous_words_count == 0) + COMPLETE_WITH_LIST(sql_commands); + +/* CREATE */ + /* complete with something you can create */ + else if (TailMatches1("CREATE")) + matches = completion_matches(text, create_command_generator); + +/* DROP, but not DROP embedded in other commands */ + /* complete with something you can drop */ + else if (Matches1("DROP")) + matches = completion_matches(text, drop_command_generator); + +/* ALTER */ + + /* ALTER TABLE */ + else if (Matches2("ALTER", "TABLE")) + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tables, + "UNION SELECT 'ALL IN TABLESPACE'"); + + /* ALTER something */ + else if (Matches1("ALTER")) + matches = completion_matches(text, alter_command_generator); + /* ALTER TABLE,INDEX,MATERIALIZED VIEW ALL IN TABLESPACE xxx */ + else if (TailMatches4("ALL", "IN", "TABLESPACE", MatchAny)) + COMPLETE_WITH_LIST2("SET TABLESPACE", "OWNED BY"); + /* ALTER TABLE,INDEX,MATERIALIZED VIEW ALL IN TABLESPACE xxx OWNED BY */ + else if (TailMatches6("ALL", "IN", "TABLESPACE", MatchAny, "OWNED", "BY")) + COMPLETE_WITH_QUERY(Query_for_list_of_roles); + /* ALTER TABLE,INDEX,MATERIALIZED VIEW ALL IN TABLESPACE xxx OWNED BY xxx */ + else if (TailMatches7("ALL", "IN", "TABLESPACE", MatchAny, "OWNED", "BY", MatchAny)) + COMPLETE_WITH_CONST("SET TABLESPACE"); + /* ALTER AGGREGATE,FUNCTION */ + else if (Matches3("ALTER", "AGGREGATE|FUNCTION", MatchAny)) + COMPLETE_WITH_CONST("("); + /* ALTER AGGREGATE,FUNCTION (...) */ + else if (Matches4("ALTER", "AGGREGATE|FUNCTION", MatchAny, MatchAny)) + { + if (ends_with(prev_wd, ')')) + COMPLETE_WITH_LIST3("OWNER TO", "RENAME TO", "SET SCHEMA"); + else + COMPLETE_WITH_FUNCTION_ARG(prev2_wd); + } + /* ALTER PUBLICATION */ + else if (Matches3("ALTER", "PUBLICATION", MatchAny)) + COMPLETE_WITH_LIST5("ADD TABLE", "DROP TABLE", "OWNER TO", "RENAME TO", "SET"); + /* ALTER PUBLICATION SET */ + else if (Matches4("ALTER", "PUBLICATION", MatchAny, "SET")) + COMPLETE_WITH_LIST2("(", "TABLE"); + /* ALTER PUBLICATION SET ( */ + else if (HeadMatches3("ALTER", "PUBLICATION", MatchAny) && TailMatches2("SET", "(")) + COMPLETE_WITH_CONST("publish"); + /* ALTER SUBSCRIPTION */ + else if (Matches3("ALTER", "SUBSCRIPTION", MatchAny)) + COMPLETE_WITH_LIST7("CONNECTION", "ENABLE", "DISABLE", "OWNER TO", + "RENAME TO", "REFRESH PUBLICATION", "SET"); + /* ALTER SUBSCRIPTION REFRESH PUBLICATION */ + else if (HeadMatches3("ALTER", "SUBSCRIPTION", MatchAny) && + TailMatches2("REFRESH", "PUBLICATION")) + COMPLETE_WITH_CONST("WITH ("); + /* ALTER SUBSCRIPTION REFRESH PUBLICATION WITH ( */ + else if (HeadMatches3("ALTER", "SUBSCRIPTION", MatchAny) && + TailMatches4("REFRESH", "PUBLICATION", "WITH", "(")) + COMPLETE_WITH_CONST("copy_data"); + /* ALTER SUBSCRIPTION SET */ + else if (Matches4("ALTER", "SUBSCRIPTION", MatchAny, "SET")) + COMPLETE_WITH_LIST2("(", "PUBLICATION"); + /* ALTER SUBSCRIPTION SET ( */ + else if (HeadMatches3("ALTER", "SUBSCRIPTION", MatchAny) && TailMatches2("SET", "(")) + COMPLETE_WITH_LIST2("slot_name", "synchronous_commit"); + /* ALTER SUBSCRIPTION SET PUBLICATION */ + else if (HeadMatches3("ALTER", "SUBSCRIPTION", MatchAny) && TailMatches2("SET", "PUBLICATION")) + { + /* complete with nothing here as this refers to remote publications */ + } + /* ALTER SUBSCRIPTION SET PUBLICATION */ + else if (HeadMatches3("ALTER", "SUBSCRIPTION", MatchAny) && + TailMatches3("SET", "PUBLICATION", MatchAny)) + COMPLETE_WITH_CONST("WITH ("); + /* ALTER SUBSCRIPTION SET PUBLICATION WITH ( */ + else if (HeadMatches3("ALTER", "SUBSCRIPTION", MatchAny) && + TailMatches5("SET", "PUBLICATION", MatchAny, "WITH", "(")) + COMPLETE_WITH_LIST2("copy_data", "refresh"); + /* ALTER SCHEMA */ + else if (Matches3("ALTER", "SCHEMA", MatchAny)) + COMPLETE_WITH_LIST2("OWNER TO", "RENAME TO"); + + /* ALTER COLLATION */ + else if (Matches3("ALTER", "COLLATION", MatchAny)) + COMPLETE_WITH_LIST3("OWNER TO", "RENAME TO", "SET SCHEMA"); + + /* ALTER CONVERSION */ + else if (Matches3("ALTER", "CONVERSION", MatchAny)) + COMPLETE_WITH_LIST3("OWNER TO", "RENAME TO", "SET SCHEMA"); + + /* ALTER DATABASE */ + else if (Matches3("ALTER", "DATABASE", MatchAny)) + COMPLETE_WITH_LIST7("RESET", "SET", "OWNER TO", "RENAME TO", + "IS_TEMPLATE", "ALLOW_CONNECTIONS", + "CONNECTION LIMIT"); + + /* ALTER EVENT TRIGGER */ + else if (Matches3("ALTER", "EVENT", "TRIGGER")) + COMPLETE_WITH_QUERY(Query_for_list_of_event_triggers); + + /* ALTER EVENT TRIGGER */ + else if (Matches4("ALTER", "EVENT", "TRIGGER", MatchAny)) + COMPLETE_WITH_LIST4("DISABLE", "ENABLE", "OWNER TO", "RENAME TO"); + + /* ALTER EVENT TRIGGER ENABLE */ + else if (Matches5("ALTER", "EVENT", "TRIGGER", MatchAny, "ENABLE")) + COMPLETE_WITH_LIST2("REPLICA", "ALWAYS"); + + /* ALTER EXTENSION */ + else if (Matches3("ALTER", "EXTENSION", MatchAny)) + COMPLETE_WITH_LIST4("ADD", "DROP", "UPDATE", "SET SCHEMA"); + + /* ALTER EXTENSION UPDATE */ + else if (Matches4("ALTER", "EXTENSION", MatchAny, "UPDATE")) + { + completion_info_charp = prev2_wd; + COMPLETE_WITH_QUERY(Query_for_list_of_available_extension_versions_with_TO); + } + + /* ALTER EXTENSION UPDATE TO */ + else if (Matches5("ALTER", "EXTENSION", MatchAny, "UPDATE", "TO")) + { + completion_info_charp = prev3_wd; + COMPLETE_WITH_QUERY(Query_for_list_of_available_extension_versions); + } + + /* ALTER FOREIGN */ + else if (Matches2("ALTER", "FOREIGN")) + COMPLETE_WITH_LIST2("DATA WRAPPER", "TABLE"); + + /* ALTER FOREIGN DATA WRAPPER */ + else if (Matches5("ALTER", "FOREIGN", "DATA", "WRAPPER", MatchAny)) + COMPLETE_WITH_LIST5("HANDLER", "VALIDATOR", "OPTIONS", "OWNER TO", "RENAME TO"); + + /* ALTER FOREIGN TABLE */ + else if (Matches4("ALTER", "FOREIGN", "TABLE", MatchAny)) + { + static const char *const list_ALTER_FOREIGN_TABLE[] = + {"ADD", "ALTER", "DISABLE TRIGGER", "DROP", "ENABLE", "INHERIT", + "NO INHERIT", "OPTIONS", "OWNER TO", "RENAME", "SET", + "VALIDATE CONSTRAINT", NULL}; + + COMPLETE_WITH_LIST(list_ALTER_FOREIGN_TABLE); + } + + /* ALTER INDEX */ + else if (Matches2("ALTER", "INDEX")) + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_indexes, + "UNION SELECT 'ALL IN TABLESPACE'"); + /* ALTER INDEX */ + else if (Matches3("ALTER", "INDEX", MatchAny)) + COMPLETE_WITH_LIST5("ALTER COLUMN", "OWNER TO", "RENAME TO", "SET", "RESET"); + /* ALTER INDEX ALTER COLUMN */ + else if (Matches6("ALTER", "INDEX", MatchAny, "ALTER", "COLUMN", MatchAny)) + COMPLETE_WITH_CONST("SET STATISTICS"); + /* ALTER INDEX SET */ + else if (Matches4("ALTER", "INDEX", MatchAny, "SET")) + COMPLETE_WITH_LIST2("(", "TABLESPACE"); + /* ALTER INDEX RESET */ + else if (Matches4("ALTER", "INDEX", MatchAny, "RESET")) + COMPLETE_WITH_CONST("("); + /* ALTER INDEX SET|RESET ( */ + else if (Matches5("ALTER", "INDEX", MatchAny, "RESET", "(")) + COMPLETE_WITH_LIST3("fillfactor", "fastupdate", + "gin_pending_list_limit"); + else if (Matches5("ALTER", "INDEX", MatchAny, "SET", "(")) + COMPLETE_WITH_LIST3("fillfactor =", "fastupdate =", + "gin_pending_list_limit ="); + + /* ALTER LANGUAGE */ + else if (Matches3("ALTER", "LANGUAGE", MatchAny)) + COMPLETE_WITH_LIST2("OWNER_TO", "RENAME TO"); + + /* ALTER LARGE OBJECT */ + else if (Matches4("ALTER", "LARGE", "OBJECT", MatchAny)) + COMPLETE_WITH_CONST("OWNER TO"); + + /* ALTER MATERIALIZED VIEW */ + else if (Matches3("ALTER", "MATERIALIZED", "VIEW")) + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_matviews, + "UNION SELECT 'ALL IN TABLESPACE'"); + + /* ALTER USER,ROLE */ + else if (Matches3("ALTER", "USER|ROLE", MatchAny) && + !TailMatches2("USER", "MAPPING")) + { + static const char *const list_ALTERUSER[] = + {"BYPASSRLS", "CONNECTION LIMIT", "CREATEDB", "CREATEROLE", + "ENCRYPTED PASSWORD", "INHERIT", "LOGIN", "NOBYPASSRLS", + "NOCREATEDB", "NOCREATEROLE", "NOINHERIT", + "NOLOGIN", "NOREPLICATION", "NOSUPERUSER", "PASSWORD", "RENAME TO", + "REPLICATION", "RESET", "SET", "SUPERUSER", + "VALID UNTIL", "WITH", NULL}; + + COMPLETE_WITH_LIST(list_ALTERUSER); + } + + /* ALTER USER,ROLE WITH */ + else if (Matches4("ALTER", "USER|ROLE", MatchAny, "WITH")) + { + /* Similar to the above, but don't complete "WITH" again. */ + static const char *const list_ALTERUSER_WITH[] = + {"BYPASSRLS", "CONNECTION LIMIT", "CREATEDB", "CREATEROLE", + "ENCRYPTED PASSWORD", "INHERIT", "LOGIN", "NOBYPASSRLS", + "NOCREATEDB", "NOCREATEROLE", "NOINHERIT", + "NOLOGIN", "NOREPLICATION", "NOSUPERUSER", "PASSWORD", "RENAME TO", + "REPLICATION", "RESET", "SET", "SUPERUSER", + "VALID UNTIL", NULL}; + + COMPLETE_WITH_LIST(list_ALTERUSER_WITH); + } + + /* ALTER DEFAULT PRIVILEGES */ + else if (Matches3("ALTER", "DEFAULT", "PRIVILEGES")) + COMPLETE_WITH_LIST2("FOR ROLE", "IN SCHEMA"); + /* ALTER DEFAULT PRIVILEGES FOR */ + else if (Matches4("ALTER", "DEFAULT", "PRIVILEGES", "FOR")) + COMPLETE_WITH_CONST("ROLE"); + /* ALTER DEFAULT PRIVILEGES IN */ + else if (Matches4("ALTER", "DEFAULT", "PRIVILEGES", "IN")) + COMPLETE_WITH_CONST("SCHEMA"); + /* ALTER DEFAULT PRIVILEGES FOR ROLE|USER ... */ + else if (Matches6("ALTER", "DEFAULT", "PRIVILEGES", "FOR", "ROLE|USER", + MatchAny)) + COMPLETE_WITH_LIST3("GRANT", "REVOKE", "IN SCHEMA"); + /* ALTER DEFAULT PRIVILEGES IN SCHEMA ... */ + else if (Matches6("ALTER", "DEFAULT", "PRIVILEGES", "IN", "SCHEMA", + MatchAny)) + COMPLETE_WITH_LIST3("GRANT", "REVOKE", "FOR ROLE"); + /* ALTER DEFAULT PRIVILEGES IN SCHEMA ... FOR */ + else if (Matches7("ALTER", "DEFAULT", "PRIVILEGES", "IN", "SCHEMA", + MatchAny, "FOR")) + COMPLETE_WITH_CONST("ROLE"); + /* ALTER DEFAULT PRIVILEGES FOR ROLE|USER ... IN SCHEMA ... */ + /* ALTER DEFAULT PRIVILEGES IN SCHEMA ... FOR ROLE|USER ... */ + else if (Matches9("ALTER", "DEFAULT", "PRIVILEGES", "FOR", "ROLE|USER", + MatchAny, "IN", "SCHEMA", MatchAny) || + Matches9("ALTER", "DEFAULT", "PRIVILEGES", "IN", "SCHEMA", + MatchAny, "FOR", "ROLE|USER", MatchAny)) + COMPLETE_WITH_LIST2("GRANT", "REVOKE"); + /* ALTER DOMAIN */ + else if (Matches3("ALTER", "DOMAIN", MatchAny)) + COMPLETE_WITH_LIST6("ADD", "DROP", "OWNER TO", "RENAME", "SET", + "VALIDATE CONSTRAINT"); + /* ALTER DOMAIN DROP */ + else if (Matches4("ALTER", "DOMAIN", MatchAny, "DROP")) + COMPLETE_WITH_LIST3("CONSTRAINT", "DEFAULT", "NOT NULL"); + /* ALTER DOMAIN DROP|RENAME|VALIDATE CONSTRAINT */ + else if (Matches5("ALTER", "DOMAIN", MatchAny, "DROP|RENAME|VALIDATE", "CONSTRAINT")) + { + completion_info_charp = prev3_wd; + COMPLETE_WITH_QUERY(Query_for_constraint_of_type); + } + /* ALTER DOMAIN RENAME */ + else if (Matches4("ALTER", "DOMAIN", MatchAny, "RENAME")) + COMPLETE_WITH_LIST2("CONSTRAINT", "TO"); + /* ALTER DOMAIN RENAME CONSTRAINT */ + else if (Matches6("ALTER", "DOMAIN", MatchAny, "RENAME", "CONSTRAINT", MatchAny)) + COMPLETE_WITH_CONST("TO"); + + /* ALTER DOMAIN SET */ + else if (Matches4("ALTER", "DOMAIN", MatchAny, "SET")) + COMPLETE_WITH_LIST3("DEFAULT", "NOT NULL", "SCHEMA"); + /* ALTER SEQUENCE */ + else if (Matches3("ALTER", "SEQUENCE", MatchAny)) + { + static const char *const list_ALTERSEQUENCE[] = + {"INCREMENT", "MINVALUE", "MAXVALUE", "RESTART", "NO", "CACHE", "CYCLE", + "SET SCHEMA", "OWNED BY", "OWNER TO", "RENAME TO", NULL}; + + COMPLETE_WITH_LIST(list_ALTERSEQUENCE); + } + /* ALTER SEQUENCE NO */ + else if (Matches4("ALTER", "SEQUENCE", MatchAny, "NO")) + COMPLETE_WITH_LIST3("MINVALUE", "MAXVALUE", "CYCLE"); + /* ALTER SERVER */ + else if (Matches3("ALTER", "SERVER", MatchAny)) + COMPLETE_WITH_LIST4("VERSION", "OPTIONS", "OWNER TO", "RENAME TO"); + /* ALTER SERVER VERSION */ + else if (Matches5("ALTER", "SERVER", MatchAny, "VERSION", MatchAny)) + COMPLETE_WITH_CONST("OPTIONS"); + /* ALTER SYSTEM SET, RESET, RESET ALL */ + else if (Matches2("ALTER", "SYSTEM")) + COMPLETE_WITH_LIST2("SET", "RESET"); + else if (Matches3("ALTER", "SYSTEM", "SET|RESET")) + COMPLETE_WITH_QUERY(Query_for_list_of_alter_system_set_vars); + else if (Matches4("ALTER", "SYSTEM", "SET", MatchAny)) + COMPLETE_WITH_CONST("TO"); + /* ALTER VIEW */ + else if (Matches3("ALTER", "VIEW", MatchAny)) + COMPLETE_WITH_LIST4("ALTER COLUMN", "OWNER TO", "RENAME TO", + "SET SCHEMA"); + /* ALTER MATERIALIZED VIEW */ + else if (Matches4("ALTER", "MATERIALIZED", "VIEW", MatchAny)) + COMPLETE_WITH_LIST4("ALTER COLUMN", "OWNER TO", "RENAME TO", + "SET SCHEMA"); + + /* ALTER POLICY */ + else if (Matches2("ALTER", "POLICY")) + COMPLETE_WITH_QUERY(Query_for_list_of_policies); + /* ALTER POLICY ON */ + else if (Matches3("ALTER", "POLICY", MatchAny)) + COMPLETE_WITH_CONST("ON"); + /* ALTER POLICY ON
*/ + else if (Matches4("ALTER", "POLICY", MatchAny, "ON")) + { + completion_info_charp = prev2_wd; + COMPLETE_WITH_QUERY(Query_for_list_of_tables_for_policy); + } + /* ALTER POLICY ON
- show options */ + else if (Matches5("ALTER", "POLICY", MatchAny, "ON", MatchAny)) + COMPLETE_WITH_LIST4("RENAME TO", "TO", "USING (", "WITH CHECK ("); + /* ALTER POLICY ON
TO */ + else if (Matches6("ALTER", "POLICY", MatchAny, "ON", MatchAny, "TO")) + COMPLETE_WITH_QUERY(Query_for_list_of_grant_roles); + /* ALTER POLICY ON
USING ( */ + else if (Matches6("ALTER", "POLICY", MatchAny, "ON", MatchAny, "USING")) + COMPLETE_WITH_CONST("("); + /* ALTER POLICY ON
WITH CHECK ( */ + else if (Matches7("ALTER", "POLICY", MatchAny, "ON", MatchAny, "WITH", "CHECK")) + COMPLETE_WITH_CONST("("); + + /* ALTER RULE , add ON */ + else if (Matches3("ALTER", "RULE", MatchAny)) + COMPLETE_WITH_CONST("ON"); + + /* If we have ALTER RULE ON, then add the correct tablename */ + else if (Matches4("ALTER", "RULE", MatchAny, "ON")) + { + completion_info_charp = prev2_wd; + COMPLETE_WITH_QUERY(Query_for_list_of_tables_for_rule); + } + + /* ALTER RULE ON */ + else if (Matches5("ALTER", "RULE", MatchAny, "ON", MatchAny)) + COMPLETE_WITH_CONST("RENAME TO"); + + /* ALTER STATISTICS */ + else if (Matches3("ALTER", "STATISTICS", MatchAny)) + COMPLETE_WITH_LIST3("OWNER TO", "RENAME TO", "SET SCHEMA"); + + /* ALTER TRIGGER , add ON */ + else if (Matches3("ALTER", "TRIGGER", MatchAny)) + COMPLETE_WITH_CONST("ON"); + + else if (Matches4("ALTER", "TRIGGER", MatchAny, MatchAny)) + { + completion_info_charp = prev2_wd; + COMPLETE_WITH_QUERY(Query_for_list_of_tables_for_trigger); + } + + /* + * If we have ALTER TRIGGER ON, then add the correct tablename + */ + else if (Matches4("ALTER", "TRIGGER", MatchAny, "ON")) + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tables, NULL); + + /* ALTER TRIGGER ON */ + else if (Matches5("ALTER", "TRIGGER", MatchAny, "ON", MatchAny)) + COMPLETE_WITH_CONST("RENAME TO"); + + /* + * If we detect ALTER TABLE , suggest sub commands + */ + else if (Matches3("ALTER", "TABLE", MatchAny)) + { + static const char *const list_ALTER2[] = + {"ADD", "ALTER", "CLUSTER ON", "DISABLE", "DROP", "ENABLE", "INHERIT", + "NO INHERIT", "RENAME", "RESET", "OWNER TO", "SET", + "VALIDATE CONSTRAINT", "REPLICA IDENTITY", "ATTACH PARTITION", + "DETACH PARTITION", NULL}; + + COMPLETE_WITH_LIST(list_ALTER2); + } + /* ALTER TABLE xxx ENABLE */ + else if (Matches4("ALTER", "TABLE", MatchAny, "ENABLE")) + COMPLETE_WITH_LIST5("ALWAYS", "REPLICA", "ROW LEVEL SECURITY", "RULE", + "TRIGGER"); + else if (Matches5("ALTER", "TABLE", MatchAny, "ENABLE", "REPLICA|ALWAYS")) + COMPLETE_WITH_LIST2("RULE", "TRIGGER"); + else if (Matches5("ALTER", "TABLE", MatchAny, "ENABLE", "RULE")) + { + completion_info_charp = prev3_wd; + COMPLETE_WITH_QUERY(Query_for_rule_of_table); + } + else if (Matches6("ALTER", "TABLE", MatchAny, "ENABLE", MatchAny, "RULE")) + { + completion_info_charp = prev4_wd; + COMPLETE_WITH_QUERY(Query_for_rule_of_table); + } + else if (Matches5("ALTER", "TABLE", MatchAny, "ENABLE", "TRIGGER")) + { + completion_info_charp = prev3_wd; + COMPLETE_WITH_QUERY(Query_for_trigger_of_table); + } + else if (Matches6("ALTER", "TABLE", MatchAny, "ENABLE", MatchAny, "TRIGGER")) + { + completion_info_charp = prev4_wd; + COMPLETE_WITH_QUERY(Query_for_trigger_of_table); + } + /* ALTER TABLE xxx INHERIT */ + else if (Matches4("ALTER", "TABLE", MatchAny, "INHERIT")) + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tables, ""); + /* ALTER TABLE xxx NO INHERIT */ + else if (Matches5("ALTER", "TABLE", MatchAny, "NO", "INHERIT")) + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tables, ""); + /* ALTER TABLE xxx DISABLE */ + else if (Matches4("ALTER", "TABLE", MatchAny, "DISABLE")) + COMPLETE_WITH_LIST3("ROW LEVEL SECURITY", "RULE", "TRIGGER"); + else if (Matches5("ALTER", "TABLE", MatchAny, "DISABLE", "RULE")) + { + completion_info_charp = prev3_wd; + COMPLETE_WITH_QUERY(Query_for_rule_of_table); + } + else if (Matches5("ALTER", "TABLE", MatchAny, "DISABLE", "TRIGGER")) + { + completion_info_charp = prev3_wd; + COMPLETE_WITH_QUERY(Query_for_trigger_of_table); + } + + /* ALTER TABLE xxx ALTER */ + else if (Matches4("ALTER", "TABLE", MatchAny, "ALTER")) + COMPLETE_WITH_ATTR(prev2_wd, " UNION SELECT 'COLUMN' UNION SELECT 'CONSTRAINT'"); + + /* ALTER TABLE xxx RENAME */ + else if (Matches4("ALTER", "TABLE", MatchAny, "RENAME")) + COMPLETE_WITH_ATTR(prev2_wd, " UNION SELECT 'COLUMN' UNION SELECT 'CONSTRAINT' UNION SELECT 'TO'"); + else if (Matches5("ALTER", "TABLE", MatchAny, "ALTER|RENAME", "COLUMN")) + COMPLETE_WITH_ATTR(prev3_wd, ""); + + /* ALTER TABLE xxx RENAME yyy */ + else if (Matches5("ALTER", "TABLE", MatchAny, "RENAME", MatchAnyExcept("CONSTRAINT|TO"))) + COMPLETE_WITH_CONST("TO"); + + /* ALTER TABLE xxx RENAME COLUMN/CONSTRAINT yyy */ + else if (Matches6("ALTER", "TABLE", MatchAny, "RENAME", "COLUMN|CONSTRAINT", MatchAnyExcept("TO"))) + COMPLETE_WITH_CONST("TO"); + + /* If we have ALTER TABLE DROP, provide COLUMN or CONSTRAINT */ + else if (Matches4("ALTER", "TABLE", MatchAny, "DROP")) + COMPLETE_WITH_LIST2("COLUMN", "CONSTRAINT"); + /* If we have ALTER TABLE DROP COLUMN, provide list of columns */ + else if (Matches5("ALTER", "TABLE", MatchAny, "DROP", "COLUMN")) + COMPLETE_WITH_ATTR(prev3_wd, ""); + + /* + * If we have ALTER TABLE ALTER|DROP|RENAME|VALIDATE CONSTRAINT, + * provide list of constraints + */ + else if (Matches5("ALTER", "TABLE", MatchAny, "ALTER|DROP|RENAME|VALIDATE", "CONSTRAINT")) + { + completion_info_charp = prev3_wd; + COMPLETE_WITH_QUERY(Query_for_constraint_of_table); + } + /* ALTER TABLE ALTER [COLUMN] */ + else if (Matches6("ALTER", "TABLE", MatchAny, "ALTER", "COLUMN", MatchAny) || + Matches5("ALTER", "TABLE", MatchAny, "ALTER", MatchAny)) + COMPLETE_WITH_LIST6("TYPE", "SET", "RESET", "RESTART", "ADD", "DROP"); + /* ALTER TABLE ALTER [COLUMN] SET */ + else if (Matches7("ALTER", "TABLE", MatchAny, "ALTER", "COLUMN", MatchAny, "SET") || + Matches6("ALTER", "TABLE", MatchAny, "ALTER", MatchAny, "SET")) + COMPLETE_WITH_LIST5("(", "DEFAULT", "NOT NULL", "STATISTICS", "STORAGE"); + /* ALTER TABLE ALTER [COLUMN] SET ( */ + else if (Matches8("ALTER", "TABLE", MatchAny, "ALTER", "COLUMN", MatchAny, "SET", "(") || + Matches7("ALTER", "TABLE", MatchAny, "ALTER", MatchAny, "SET", "(")) + COMPLETE_WITH_LIST2("n_distinct", "n_distinct_inherited"); + /* ALTER TABLE ALTER [COLUMN] SET STORAGE */ + else if (Matches8("ALTER", "TABLE", MatchAny, "ALTER", "COLUMN", MatchAny, "SET", "STORAGE") || + Matches7("ALTER", "TABLE", MatchAny, "ALTER", MatchAny, "SET", "STORAGE")) + COMPLETE_WITH_LIST4("PLAIN", "EXTERNAL", "EXTENDED", "MAIN"); + /* ALTER TABLE ALTER [COLUMN] DROP */ + else if (Matches7("ALTER", "TABLE", MatchAny, "ALTER", "COLUMN", MatchAny, "DROP") || + Matches6("ALTER", "TABLE", MatchAny, "ALTER", MatchAny, "DROP")) + COMPLETE_WITH_LIST3("DEFAULT", "IDENTITY", "NOT NULL"); + else if (Matches4("ALTER", "TABLE", MatchAny, "CLUSTER")) + COMPLETE_WITH_CONST("ON"); + else if (Matches5("ALTER", "TABLE", MatchAny, "CLUSTER", "ON")) + { + completion_info_charp = prev3_wd; + COMPLETE_WITH_QUERY(Query_for_index_of_table); + } + /* If we have ALTER TABLE SET, provide list of attributes and '(' */ + else if (Matches4("ALTER", "TABLE", MatchAny, "SET")) + COMPLETE_WITH_LIST7("(", "LOGGED", "SCHEMA", "TABLESPACE", "UNLOGGED", + "WITH", "WITHOUT"); + + /* + * If we have ALTER TABLE SET TABLESPACE provide a list of + * tablespaces + */ + else if (Matches5("ALTER", "TABLE", MatchAny, "SET", "TABLESPACE")) + COMPLETE_WITH_QUERY(Query_for_list_of_tablespaces); + /* If we have ALTER TABLE SET WITH provide OIDS */ + else if (Matches5("ALTER", "TABLE", MatchAny, "SET", "WITH")) + COMPLETE_WITH_CONST("OIDS"); + /* If we have ALTER TABLE SET WITHOUT provide CLUSTER or OIDS */ + else if (Matches5("ALTER", "TABLE", MatchAny, "SET", "WITHOUT")) + COMPLETE_WITH_LIST2("CLUSTER", "OIDS"); + /* ALTER TABLE RESET */ + else if (Matches4("ALTER", "TABLE", MatchAny, "RESET")) + COMPLETE_WITH_CONST("("); + /* ALTER TABLE SET|RESET ( */ + else if (Matches5("ALTER", "TABLE", MatchAny, "SET|RESET", "(")) + { + static const char *const list_TABLEOPTIONS[] = + { + "autovacuum_analyze_scale_factor", + "autovacuum_analyze_threshold", + "autovacuum_enabled", + "autovacuum_freeze_max_age", + "autovacuum_freeze_min_age", + "autovacuum_freeze_table_age", + "autovacuum_multixact_freeze_max_age", + "autovacuum_multixact_freeze_min_age", + "autovacuum_multixact_freeze_table_age", + "autovacuum_vacuum_cost_delay", + "autovacuum_vacuum_cost_limit", + "autovacuum_vacuum_scale_factor", + "autovacuum_vacuum_threshold", + "fillfactor", + "parallel_workers", + "log_autovacuum_min_duration", + "toast.autovacuum_enabled", + "toast.autovacuum_freeze_max_age", + "toast.autovacuum_freeze_min_age", + "toast.autovacuum_freeze_table_age", + "toast.autovacuum_multixact_freeze_max_age", + "toast.autovacuum_multixact_freeze_min_age", + "toast.autovacuum_multixact_freeze_table_age", + "toast.autovacuum_vacuum_cost_delay", + "toast.autovacuum_vacuum_cost_limit", + "toast.autovacuum_vacuum_scale_factor", + "toast.autovacuum_vacuum_threshold", + "toast.log_autovacuum_min_duration", + "user_catalog_table", + NULL + }; + + COMPLETE_WITH_LIST(list_TABLEOPTIONS); + } + else if (Matches7("ALTER", "TABLE", MatchAny, "REPLICA", "IDENTITY", "USING", "INDEX")) + { + completion_info_charp = prev5_wd; + COMPLETE_WITH_QUERY(Query_for_index_of_table); + } + else if (Matches6("ALTER", "TABLE", MatchAny, "REPLICA", "IDENTITY", "USING")) + COMPLETE_WITH_CONST("INDEX"); + else if (Matches5("ALTER", "TABLE", MatchAny, "REPLICA", "IDENTITY")) + COMPLETE_WITH_LIST4("FULL", "NOTHING", "DEFAULT", "USING"); + else if (Matches4("ALTER", "TABLE", MatchAny, "REPLICA")) + COMPLETE_WITH_CONST("IDENTITY"); + + /* + * If we have ALTER TABLE ATTACH PARTITION, provide a list of + * tables. + */ + else if (Matches5("ALTER", "TABLE", MatchAny, "ATTACH", "PARTITION")) + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tables, ""); + /* Limited completion support for partition bound specification */ + else if (TailMatches3("ATTACH", "PARTITION", MatchAny)) + COMPLETE_WITH_LIST2("FOR VALUES", "DEFAULT"); + else if (TailMatches2("FOR", "VALUES")) + COMPLETE_WITH_LIST2("FROM (", "IN ("); + + /* + * If we have ALTER TABLE DETACH PARTITION, provide a list of + * partitions of . + */ + else if (Matches5("ALTER", "TABLE", MatchAny, "DETACH", "PARTITION")) + { + completion_info_charp = prev3_wd; + COMPLETE_WITH_QUERY(Query_for_partition_of_table); + } + + /* ALTER TABLESPACE with RENAME TO, OWNER TO, SET, RESET */ + else if (Matches3("ALTER", "TABLESPACE", MatchAny)) + COMPLETE_WITH_LIST4("RENAME TO", "OWNER TO", "SET", "RESET"); + /* ALTER TABLESPACE SET|RESET */ + else if (Matches4("ALTER", "TABLESPACE", MatchAny, "SET|RESET")) + COMPLETE_WITH_CONST("("); + /* ALTER TABLESPACE SET|RESET ( */ + else if (Matches5("ALTER", "TABLESPACE", MatchAny, "SET|RESET", "(")) + COMPLETE_WITH_LIST3("seq_page_cost", "random_page_cost", + "effective_io_concurrency"); + + /* ALTER TEXT SEARCH */ + else if (Matches3("ALTER", "TEXT", "SEARCH")) + COMPLETE_WITH_LIST4("CONFIGURATION", "DICTIONARY", "PARSER", "TEMPLATE"); + else if (Matches5("ALTER", "TEXT", "SEARCH", "TEMPLATE|PARSER", MatchAny)) + COMPLETE_WITH_LIST2("RENAME TO", "SET SCHEMA"); + else if (Matches5("ALTER", "TEXT", "SEARCH", "DICTIONARY", MatchAny)) + COMPLETE_WITH_LIST3("OWNER TO", "RENAME TO", "SET SCHEMA"); + else if (Matches5("ALTER", "TEXT", "SEARCH", "CONFIGURATION", MatchAny)) + COMPLETE_WITH_LIST6("ADD MAPPING FOR", "ALTER MAPPING", + "DROP MAPPING FOR", + "OWNER TO", "RENAME TO", "SET SCHEMA"); + + /* complete ALTER TYPE with actions */ + else if (Matches3("ALTER", "TYPE", MatchAny)) + COMPLETE_WITH_LIST7("ADD ATTRIBUTE", "ADD VALUE", "ALTER ATTRIBUTE", + "DROP ATTRIBUTE", + "OWNER TO", "RENAME", "SET SCHEMA"); + /* complete ALTER TYPE ADD with actions */ + else if (Matches4("ALTER", "TYPE", MatchAny, "ADD")) + COMPLETE_WITH_LIST2("ATTRIBUTE", "VALUE"); + /* ALTER TYPE RENAME */ + else if (Matches4("ALTER", "TYPE", MatchAny, "RENAME")) + COMPLETE_WITH_LIST3("ATTRIBUTE", "TO", "VALUE"); + /* ALTER TYPE xxx RENAME (ATTRIBUTE|VALUE) yyy */ + else if (Matches6("ALTER", "TYPE", MatchAny, "RENAME", "ATTRIBUTE|VALUE", MatchAny)) + COMPLETE_WITH_CONST("TO"); + + /* + * If we have ALTER TYPE ALTER/DROP/RENAME ATTRIBUTE, provide list + * of attributes + */ + else if (Matches5("ALTER", "TYPE", MatchAny, "ALTER|DROP|RENAME", "ATTRIBUTE")) + COMPLETE_WITH_ATTR(prev3_wd, ""); + /* ALTER TYPE ALTER ATTRIBUTE */ + else if (Matches6("ALTER", "TYPE", MatchAny, "ALTER", "ATTRIBUTE", MatchAny)) + COMPLETE_WITH_CONST("TYPE"); + /* complete ALTER GROUP */ + else if (Matches3("ALTER", "GROUP", MatchAny)) + COMPLETE_WITH_LIST3("ADD USER", "DROP USER", "RENAME TO"); + /* complete ALTER GROUP ADD|DROP with USER */ + else if (Matches4("ALTER", "GROUP", MatchAny, "ADD|DROP")) + COMPLETE_WITH_CONST("USER"); + /* complete ALTER GROUP ADD|DROP USER with a user name */ + else if (Matches5("ALTER", "GROUP", MatchAny, "ADD|DROP", "USER")) + COMPLETE_WITH_QUERY(Query_for_list_of_roles); + + /* + * If we have ALTER TYPE RENAME VALUE, provide list of enum values + */ + else if (Matches5("ALTER", "TYPE", MatchAny, "RENAME", "VALUE")) + COMPLETE_WITH_ENUM_VALUE(prev3_wd); + +/* BEGIN */ + else if (Matches1("BEGIN")) + COMPLETE_WITH_LIST6("WORK", "TRANSACTION", "ISOLATION LEVEL", "READ", "DEFERRABLE", "NOT DEFERRABLE"); +/* END, ABORT */ + else if (Matches1("END|ABORT")) + COMPLETE_WITH_LIST2("WORK", "TRANSACTION"); +/* COMMIT */ + else if (Matches1("COMMIT")) + COMPLETE_WITH_LIST3("WORK", "TRANSACTION", "PREPARED"); +/* RELEASE SAVEPOINT */ + else if (Matches1("RELEASE")) + COMPLETE_WITH_CONST("SAVEPOINT"); +/* ROLLBACK */ + else if (Matches1("ROLLBACK")) + COMPLETE_WITH_LIST4("WORK", "TRANSACTION", "TO SAVEPOINT", "PREPARED"); +/* CLUSTER */ + else if (Matches1("CLUSTER")) + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tm, "UNION SELECT 'VERBOSE'"); + else if (Matches2("CLUSTER", "VERBOSE")) + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tm, NULL); + /* If we have CLUSTER , then add "USING" */ + else if (Matches2("CLUSTER", MatchAnyExcept("VERBOSE|ON"))) + COMPLETE_WITH_CONST("USING"); + /* If we have CLUSTER VERBOSE , then add "USING" */ + else if (Matches3("CLUSTER", "VERBOSE", MatchAny)) + COMPLETE_WITH_CONST("USING"); + /* If we have CLUSTER USING, then add the index as well */ + else if (Matches3("CLUSTER", MatchAny, "USING") || + Matches4("CLUSTER", "VERBOSE", MatchAny, "USING")) + { + completion_info_charp = prev2_wd; + COMPLETE_WITH_QUERY(Query_for_index_of_table); + } + +/* CLOSE */ + else if (Matches1("CLOSE")) + COMPLETE_WITH_QUERY(Query_for_list_of_cursors); + +/* COMMENT */ + else if (Matches1("COMMENT")) + COMPLETE_WITH_CONST("ON"); + else if (Matches2("COMMENT", "ON")) + { + static const char *const list_COMMENT[] = + {"ACCESS METHOD", "CAST", "COLLATION", "CONVERSION", "DATABASE", + "EVENT TRIGGER", "EXTENSION", + "FOREIGN DATA WRAPPER", "FOREIGN TABLE", + "SERVER", "INDEX", "LANGUAGE", "POLICY", "PUBLICATION", "RULE", + "SCHEMA", "SEQUENCE", "STATISTICS", "SUBSCRIPTION", + "TABLE", "TYPE", "VIEW", "MATERIALIZED VIEW", "COLUMN", "AGGREGATE", "FUNCTION", + "OPERATOR", "TRIGGER", "CONSTRAINT", "DOMAIN", "LARGE OBJECT", + "TABLESPACE", "TEXT SEARCH", "ROLE", NULL}; + + COMPLETE_WITH_LIST(list_COMMENT); + } + else if (Matches4("COMMENT", "ON", "ACCESS", "METHOD")) + COMPLETE_WITH_QUERY(Query_for_list_of_access_methods); + else if (Matches3("COMMENT", "ON", "FOREIGN")) + COMPLETE_WITH_LIST2("DATA WRAPPER", "TABLE"); + else if (Matches4("COMMENT", "ON", "TEXT", "SEARCH")) + COMPLETE_WITH_LIST4("CONFIGURATION", "DICTIONARY", "PARSER", "TEMPLATE"); + else if (Matches3("COMMENT", "ON", "CONSTRAINT")) + COMPLETE_WITH_QUERY(Query_for_all_table_constraints); + else if (Matches4("COMMENT", "ON", "CONSTRAINT", MatchAny)) + COMPLETE_WITH_CONST("ON"); + else if (Matches5("COMMENT", "ON", "CONSTRAINT", MatchAny, "ON")) + { + completion_info_charp = prev2_wd; + COMPLETE_WITH_QUERY(Query_for_list_of_tables_for_constraint); + } + else if (Matches4("COMMENT", "ON", "MATERIALIZED", "VIEW")) + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_matviews, NULL); + else if (Matches4("COMMENT", "ON", "EVENT", "TRIGGER")) + COMPLETE_WITH_QUERY(Query_for_list_of_event_triggers); + else if (Matches4("COMMENT", "ON", MatchAny, MatchAnyExcept("IS")) || + Matches5("COMMENT", "ON", MatchAny, MatchAny, MatchAnyExcept("IS")) || + Matches6("COMMENT", "ON", MatchAny, MatchAny, MatchAny, MatchAnyExcept("IS"))) + COMPLETE_WITH_CONST("IS"); + +/* COPY */ + + /* + * If we have COPY, offer list of tables or "(" (Also cover the analogous + * backslash command). + */ + else if (Matches1("COPY|\\copy")) + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tables, + " UNION ALL SELECT '('"); + /* If we have COPY BINARY, complete with list of tables */ + else if (Matches2("COPY", "BINARY")) + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tables, NULL); + /* If we have COPY (, complete it with legal commands */ + else if (Matches2("COPY|\\copy", "(")) + COMPLETE_WITH_LIST7("SELECT", "TABLE", "VALUES", "INSERT", "UPDATE", "DELETE", "WITH"); + /* If we have COPY [BINARY] , complete it with "TO" or "FROM" */ + else if (Matches2("COPY|\\copy", MatchAny) || + Matches3("COPY", "BINARY", MatchAny)) + COMPLETE_WITH_LIST2("FROM", "TO"); + /* If we have COPY [BINARY] FROM|TO, complete with filename */ + else if (Matches3("COPY|\\copy", MatchAny, "FROM|TO") || + Matches4("COPY", "BINARY", MatchAny, "FROM|TO")) + { + completion_charp = ""; + matches = completion_matches(text, complete_from_files); + } + + /* Handle COPY [BINARY] FROM|TO filename */ + else if (Matches4("COPY|\\copy", MatchAny, "FROM|TO", MatchAny) || + Matches5("COPY", "BINARY", MatchAny, "FROM|TO", MatchAny)) + COMPLETE_WITH_LIST6("BINARY", "OIDS", "DELIMITER", "NULL", "CSV", + "ENCODING"); + + /* Handle COPY [BINARY] FROM|TO filename CSV */ + else if (Matches5("COPY|\\copy", MatchAny, "FROM|TO", MatchAny, "CSV") || + Matches6("COPY", "BINARY", MatchAny, "FROM|TO", MatchAny, "CSV")) + COMPLETE_WITH_LIST5("HEADER", "QUOTE", "ESCAPE", "FORCE QUOTE", + "FORCE NOT NULL"); + + /* CREATE ACCESS METHOD */ + /* Complete "CREATE ACCESS METHOD " */ + else if (Matches4("CREATE", "ACCESS", "METHOD", MatchAny)) + COMPLETE_WITH_CONST("TYPE"); + /* Complete "CREATE ACCESS METHOD TYPE" */ + else if (Matches5("CREATE", "ACCESS", "METHOD", MatchAny, "TYPE")) + COMPLETE_WITH_CONST("INDEX"); + /* Complete "CREATE ACCESS METHOD TYPE " */ + else if (Matches6("CREATE", "ACCESS", "METHOD", MatchAny, "TYPE", MatchAny)) + COMPLETE_WITH_CONST("HANDLER"); + + /* CREATE DATABASE */ + else if (Matches3("CREATE", "DATABASE", MatchAny)) + COMPLETE_WITH_LIST10("WITH", "OWNER", "TEMPLATE", "ENCODING", "TABLESPACE", + "IS_TEMPLATE", + "ALLOW_CONNECTIONS", "CONNECTION LIMIT", + "LC_COLLATE", "LC_CTYPE"); + + /* CREATE DATABASE WITH */ + else if (Matches4("CREATE", "DATABASE", MatchAny, "WITH")) + COMPLETE_WITH_LIST9("OWNER", "TEMPLATE", "ENCODING", "TABLESPACE", + "IS_TEMPLATE", + "ALLOW_CONNECTIONS", "CONNECTION LIMIT", + "LC_COLLATE", "LC_CTYPE"); + + /* CREATE DATABASE WITH OWNER */ + else if (Matches5("CREATE", "DATABASE", MatchAny, "WITH", "OWNER")) + COMPLETE_WITH_LIST8("TEMPLATE", "ENCODING", "TABLESPACE", + "IS_TEMPLATE", + "ALLOW_CONNECTIONS", "CONNECTION LIMIT", + "LC_COLLATE", "LC_CTYPE"); + + /* CREATE DATABASE OWNER */ + else if (Matches4("CREATE", "DATABASE", MatchAny, "OWNER")) + COMPLETE_WITH_LIST8("TEMPLATE", "ENCODING", "TABLESPACE", + "IS_TEMPLATE", + "ALLOW_CONNECTIONS", "CONNECTION LIMIT", + "LC_COLLATE", "LC_CTYPE"); + + /* CONNECTION LIMIT -- is allowed inside CREATE DATABASE, so use TailMatches */ + else if (TailMatches2("CONNECTION", "LIMIT")) + COMPLETE_WITH_CONST("UNLIMITED"); + + else if (Matches4("CREATE", "DATABASE", MatchAny, "TEMPLATE")) + COMPLETE_WITH_QUERY(Query_for_list_of_template_databases); + + /* CREATE EXTENSION */ + /* Complete with available extensions rather than installed ones. */ + else if (Matches2("CREATE", "EXTENSION")) + COMPLETE_WITH_QUERY(Query_for_list_of_available_extensions); + /* CREATE EXTENSION */ + else if (Matches3("CREATE", "EXTENSION", MatchAny)) + COMPLETE_WITH_LIST3("WITH SCHEMA", "CASCADE", "VERSION"); + /* CREATE EXTENSION VERSION */ + else if (Matches4("CREATE", "EXTENSION", MatchAny, "VERSION")) + { + completion_info_charp = prev2_wd; + COMPLETE_WITH_QUERY(Query_for_list_of_available_extension_versions); + } + + /* CREATE FOREIGN */ + else if (Matches2("CREATE", "FOREIGN")) + COMPLETE_WITH_LIST2("DATA WRAPPER", "TABLE"); + + /* CREATE FOREIGN DATA WRAPPER */ + else if (Matches5("CREATE", "FOREIGN", "DATA", "WRAPPER", MatchAny)) + COMPLETE_WITH_LIST3("HANDLER", "VALIDATOR", "OPTIONS"); + + /* CREATE INDEX --- is allowed inside CREATE SCHEMA, so use TailMatches */ + /* First off we complete CREATE UNIQUE with "INDEX" */ + else if (TailMatches2("CREATE", "UNIQUE")) + COMPLETE_WITH_CONST("INDEX"); + + /* + * If we have CREATE|UNIQUE INDEX, then add "ON", "CONCURRENTLY", and + * existing indexes + */ + else if (TailMatches2("CREATE|UNIQUE", "INDEX")) + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_indexes, + " UNION SELECT 'ON'" + " UNION SELECT 'CONCURRENTLY'"); + /* Complete ... INDEX|CONCURRENTLY [] ON with a list of tables */ + else if (TailMatches3("INDEX|CONCURRENTLY", MatchAny, "ON") || + TailMatches2("INDEX|CONCURRENTLY", "ON")) + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tm, NULL); + + /* + * Complete CREATE|UNIQUE INDEX CONCURRENTLY with "ON" and existing + * indexes + */ + else if (TailMatches3("CREATE|UNIQUE", "INDEX", "CONCURRENTLY")) + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_indexes, + " UNION SELECT 'ON'"); + /* Complete CREATE|UNIQUE INDEX [CONCURRENTLY] with "ON" */ + else if (TailMatches3("CREATE|UNIQUE", "INDEX", MatchAny) || + TailMatches4("CREATE|UNIQUE", "INDEX", "CONCURRENTLY", MatchAny)) + COMPLETE_WITH_CONST("ON"); + + /* + * Complete INDEX ON
with a list of table columns (which + * should really be in parens) + */ + else if (TailMatches4("INDEX", MatchAny, "ON", MatchAny) || + TailMatches3("INDEX|CONCURRENTLY", "ON", MatchAny)) + COMPLETE_WITH_LIST2("(", "USING"); + else if (TailMatches5("INDEX", MatchAny, "ON", MatchAny, "(") || + TailMatches4("INDEX|CONCURRENTLY", "ON", MatchAny, "(")) + COMPLETE_WITH_ATTR(prev2_wd, ""); + /* same if you put in USING */ + else if (TailMatches5("ON", MatchAny, "USING", MatchAny, "(")) + COMPLETE_WITH_ATTR(prev4_wd, ""); + /* Complete USING with an index method */ + else if (TailMatches6("INDEX", MatchAny, MatchAny, "ON", MatchAny, "USING") || + TailMatches5("INDEX", MatchAny, "ON", MatchAny, "USING") || + TailMatches4("INDEX", "ON", MatchAny, "USING")) + COMPLETE_WITH_QUERY(Query_for_list_of_access_methods); + else if (TailMatches4("ON", MatchAny, "USING", MatchAny) && + !TailMatches6("POLICY", MatchAny, MatchAny, MatchAny, MatchAny, MatchAny) && + !TailMatches4("FOR", MatchAny, MatchAny, MatchAny)) + COMPLETE_WITH_CONST("("); + + // XXX See if you can add Regions here!? + + /* CREATE POLICY */ + /* Complete "CREATE POLICY ON" */ + else if (Matches3("CREATE", "POLICY", MatchAny)) + COMPLETE_WITH_CONST("ON"); + /* Complete "CREATE POLICY ON
" */ + else if (Matches4("CREATE", "POLICY", MatchAny, "ON")) + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tables, NULL); + /* Complete "CREATE POLICY ON
AS|FOR|TO|USING|WITH CHECK" */ + else if (Matches5("CREATE", "POLICY", MatchAny, "ON", MatchAny)) + COMPLETE_WITH_LIST5("AS", "FOR", "TO", "USING (", "WITH CHECK ("); + /* CREATE POLICY ON
AS PERMISSIVE|RESTRICTIVE */ + else if (Matches6("CREATE", "POLICY", MatchAny, "ON", MatchAny, "AS")) + COMPLETE_WITH_LIST2("PERMISSIVE", "RESTRICTIVE"); + + /* + * CREATE POLICY ON
AS PERMISSIVE|RESTRICTIVE + * FOR|TO|USING|WITH CHECK + */ + else if (Matches7("CREATE", "POLICY", MatchAny, "ON", MatchAny, "AS", MatchAny)) + COMPLETE_WITH_LIST4("FOR", "TO", "USING", "WITH CHECK"); + /* CREATE POLICY ON
FOR ALL|SELECT|INSERT|UPDATE|DELETE */ + else if (Matches6("CREATE", "POLICY", MatchAny, "ON", MatchAny, "FOR")) + COMPLETE_WITH_LIST5("ALL", "SELECT", "INSERT", "UPDATE", "DELETE"); + /* Complete "CREATE POLICY ON
FOR INSERT TO|WITH CHECK" */ + else if (Matches7("CREATE", "POLICY", MatchAny, "ON", MatchAny, "FOR", "INSERT")) + COMPLETE_WITH_LIST2("TO", "WITH CHECK ("); + /* Complete "CREATE POLICY ON
FOR SELECT|DELETE TO|USING" */ + else if (Matches7("CREATE", "POLICY", MatchAny, "ON", MatchAny, "FOR", "SELECT|DELETE")) + COMPLETE_WITH_LIST2("TO", "USING ("); + /* CREATE POLICY ON
FOR ALL|UPDATE TO|USING|WITH CHECK */ + else if (Matches7("CREATE", "POLICY", MatchAny, "ON", MatchAny, "FOR", "ALL|UPDATE")) + COMPLETE_WITH_LIST3("TO", "USING (", "WITH CHECK ("); + /* Complete "CREATE POLICY ON
TO " */ + else if (Matches6("CREATE", "POLICY", MatchAny, "ON", MatchAny, "TO")) + COMPLETE_WITH_QUERY(Query_for_list_of_grant_roles); + /* Complete "CREATE POLICY ON
USING (" */ + else if (Matches6("CREATE", "POLICY", MatchAny, "ON", MatchAny, "USING")) + COMPLETE_WITH_CONST("("); + + /* + * CREATE POLICY ON
AS PERMISSIVE|RESTRICTIVE FOR + * ALL|SELECT|INSERT|UPDATE|DELETE + */ + else if (Matches8("CREATE", "POLICY", MatchAny, "ON", MatchAny, "AS", MatchAny, "FOR")) + COMPLETE_WITH_LIST5("ALL", "SELECT", "INSERT", "UPDATE", "DELETE"); + + /* + * Complete "CREATE POLICY ON
AS PERMISSIVE|RESTRICTIVE FOR + * INSERT TO|WITH CHECK" + */ + else if (Matches9("CREATE", "POLICY", MatchAny, "ON", MatchAny, "AS", MatchAny, "FOR", "INSERT")) + COMPLETE_WITH_LIST2("TO", "WITH CHECK ("); + + /* + * Complete "CREATE POLICY ON
AS PERMISSIVE|RESTRICTIVE FOR + * SELECT|DELETE TO|USING" + */ + else if (Matches9("CREATE", "POLICY", MatchAny, "ON", MatchAny, "AS", MatchAny, "FOR", "SELECT|DELETE")) + COMPLETE_WITH_LIST2("TO", "USING ("); + + /* + * CREATE POLICY ON
AS PERMISSIVE|RESTRICTIVE FOR + * ALL|UPDATE TO|USING|WITH CHECK + */ + else if (Matches9("CREATE", "POLICY", MatchAny, "ON", MatchAny, "AS", MatchAny, "FOR", "ALL|UPDATE")) + COMPLETE_WITH_LIST3("TO", "USING (", "WITH CHECK ("); + + /* + * Complete "CREATE POLICY ON
AS PERMISSIVE|RESTRICTIVE TO + * " + */ + else if (Matches8("CREATE", "POLICY", MatchAny, "ON", MatchAny, "AS", MatchAny, "TO")) + COMPLETE_WITH_QUERY(Query_for_list_of_grant_roles); + + /* + * Complete "CREATE POLICY ON
AS PERMISSIVE|RESTRICTIVE + * USING (" + */ + else if (Matches8("CREATE", "POLICY", MatchAny, "ON", MatchAny, "AS", MatchAny, "USING")) + COMPLETE_WITH_CONST("("); + + +/* CREATE PUBLICATION */ + else if (Matches3("CREATE", "PUBLICATION", MatchAny)) + COMPLETE_WITH_LIST3("FOR TABLE", "FOR ALL TABLES", "WITH ("); + else if (Matches4("CREATE", "PUBLICATION", MatchAny, "FOR")) + COMPLETE_WITH_LIST2("TABLE", "ALL TABLES"); + /* Complete "CREATE PUBLICATION FOR TABLE
" */ + else if (Matches4("CREATE", "PUBLICATION", MatchAny, "FOR TABLE")) + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tables, NULL); + /* Complete "CREATE PUBLICATION [...] WITH" */ + else if (HeadMatches2("CREATE", "PUBLICATION") && TailMatches2("WITH", "(")) + COMPLETE_WITH_CONST("publish"); + +/* CREATE RULE */ + /* Complete "CREATE RULE " with "AS ON" */ + else if (Matches3("CREATE", "RULE", MatchAny)) + COMPLETE_WITH_CONST("AS ON"); + /* Complete "CREATE RULE AS" with "ON" */ + else if (Matches4("CREATE", "RULE", MatchAny, "AS")) + COMPLETE_WITH_CONST("ON"); + /* Complete "CREATE RULE AS ON" with SELECT|UPDATE|INSERT|DELETE */ + else if (Matches5("CREATE", "RULE", MatchAny, "AS", "ON")) + COMPLETE_WITH_LIST4("SELECT", "UPDATE", "INSERT", "DELETE"); + /* Complete "AS ON SELECT|UPDATE|INSERT|DELETE" with a "TO" */ + else if (TailMatches3("AS", "ON", "SELECT|UPDATE|INSERT|DELETE")) + COMPLETE_WITH_CONST("TO"); + /* Complete "AS ON TO" with a table name */ + else if (TailMatches4("AS", "ON", "SELECT|UPDATE|INSERT|DELETE", "TO")) + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tables, NULL); + +/* CREATE SCHEMA */ + else if (Matches2("CREATE", "SCHEMA")) + COMPLETE_WITH_LIST2("AUTHORIZATION", "IF NOT EXISTS"); + else if (Matches3("CREATE", "SCHEMA", MatchAny)) + COMPLETE_WITH_CONST("AUTHORIZATION"); + else if (Matches4("CREATE", "SCHEMA", "IF NOT EXISTS", MatchAny)) + COMPLETE_WITH_CONST("AUTHORIZATION"); + + +/* CREATE SEQUENCE --- is allowed inside CREATE SCHEMA, so use TailMatches */ + else if (TailMatches3("CREATE", "SEQUENCE", MatchAny) || + TailMatches4("CREATE", "TEMP|TEMPORARY", "SEQUENCE", MatchAny)) + COMPLETE_WITH_LIST8("INCREMENT BY", "MINVALUE", "MAXVALUE", "NO", "CACHE", + "CYCLE", "OWNED BY", "START WITH"); + else if (TailMatches4("CREATE", "SEQUENCE", MatchAny, "NO") || + TailMatches5("CREATE", "TEMP|TEMPORARY", "SEQUENCE", MatchAny, "NO")) + COMPLETE_WITH_LIST3("MINVALUE", "MAXVALUE", "CYCLE"); + +/* CREATE SERVER */ + else if (Matches3("CREATE", "SERVER", MatchAny)) + COMPLETE_WITH_LIST3("TYPE", "VERSION", "FOREIGN DATA WRAPPER"); + +/* CREATE STATISTICS */ + else if (Matches3("CREATE", "STATISTICS", MatchAny)) + COMPLETE_WITH_LIST2("(", "ON"); + else if (Matches4("CREATE", "STATISTICS", MatchAny, "(")) + COMPLETE_WITH_LIST2("ndistinct", "dependencies"); + else if (HeadMatches3("CREATE", "STATISTICS", MatchAny) && + previous_words[0][0] == '(' && + previous_words[0][strlen(previous_words[0]) - 1] == ')') + COMPLETE_WITH_CONST("ON"); + else if (HeadMatches3("CREATE", "STATISTICS", MatchAny) && + TailMatches1("FROM")) + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tables, NULL); + + /* Complete "CREATE TEMP/TEMPORARY" with the possible temp objects */ + else if (HeadMatches2("CREATE", "TEMP|TEMPORARY")) + COMPLETE_WITH_LIST3("SEQUENCE", "TABLE", "VIEW"); + + /* Complete "CREATE UNLOGGED" with TABLE or MATVIEW */ + else if (TailMatches2("CREATE", "UNLOGGED")) + COMPLETE_WITH_LIST2("TABLE", "MATERIALIZED VIEW"); + + /* CREATE TABLE is allowed inside CREATE SCHEMA in Native Postgres so although + we ought to use TailMatches, using HeadMatches since this allows supporting + far more options */ + else if (Matches2("CREATE", "TABLE") || + Matches3("CREATE", "TEMP|TEMPORARY", "TABLE") || + Matches4("CREATE", "LOCAL", "TEMP|TEMPORARY", "TABLE")) + COMPLETE_WITH_LIST2("IF NOT EXISTS", "AS"); + + else if (Matches3("CREATE", "TABLE", MatchAny)) + COMPLETE_WITH_CONST("("); + + else if (HeadMatches2("CREATE", "TABLE") /*|| + HeadMatches3("CREATE", "TEMP|TEMPORARY", "TABLE") || + HeadMatches4("CREATE", "LOCAL", "TEMP|TEMPORARY", "TABLE")*/) + { + //if (TailMatches3(MatchAnyExcept("("), MatchAny, ")")) + if (TailMatches1("BACKUP")) + COMPLETE_WITH_LIST2("YES", "NO"); + else if (TailMatches2("BACKUP", "YES|NO")) + COMPLETE_WITH_LIST5("DISTSTYLE", "DISTKEY (", "COMPOUND", "INTERLEAVED", "SORTKEY ("); + else if (TailMatches1("DISTSTYLE")) + COMPLETE_WITH_LIST3("EVEN", "KEY", "ALL"); + else if (TailMatches2("DISTSTYLE", "EVEN|KEY|ALL")) + COMPLETE_WITH_LIST3("COMPOUND", "INTERLEAVED", "SORTKEY ("); + else if (TailMatches2("DISTKEY", MatchAny)) + COMPLETE_WITH_LIST3("COMPOUND", "INTERLEAVED", "SORTKEY ("); + else if (TailMatches1("COMPOUND|INTERLEAVED")) + COMPLETE_WITH_CONST("SORTKEY ("); + else + COMPLETE_WITH_LIST6("BACKUP", "DISTSTYLE", "DISTKEY (", "COMPOUND", "INTERLEAVED", "SORTKEY ("); + } + + /* Complete PARTITION BY with RANGE ( or LIST ( or ... */ + else if (TailMatches2("PARTITION", "BY")) + COMPLETE_WITH_LIST2("RANGE (", "LIST ("); + /* If we have xxx PARTITION OF, provide a list of partitioned tables */ + else if (TailMatches2("PARTITION", "OF")) + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_partitioned_tables, ""); + /* Limited completion support for partition bound specification */ + else if (TailMatches3("PARTITION", "OF", MatchAny)) + COMPLETE_WITH_LIST2("FOR VALUES", "DEFAULT"); + +/* CREATE TABLESPACE */ + else if (Matches3("CREATE", "TABLESPACE", MatchAny)) + COMPLETE_WITH_LIST2("OWNER", "LOCATION"); + /* Complete CREATE TABLESPACE name OWNER name with "LOCATION" */ + else if (Matches5("CREATE", "TABLESPACE", MatchAny, "OWNER", MatchAny)) + COMPLETE_WITH_CONST("LOCATION"); + +/* CREATE TEXT SEARCH */ + else if (Matches3("CREATE", "TEXT", "SEARCH")) + COMPLETE_WITH_LIST4("CONFIGURATION", "DICTIONARY", "PARSER", "TEMPLATE"); + else if (Matches5("CREATE", "TEXT", "SEARCH", "CONFIGURATION", MatchAny)) + COMPLETE_WITH_CONST("("); + +/* CREATE SUBSCRIPTION */ + else if (Matches3("CREATE", "SUBSCRIPTION", MatchAny)) + COMPLETE_WITH_CONST("CONNECTION"); + else if (Matches5("CREATE", "SUBSCRIPTION", MatchAny, "CONNECTION", MatchAny)) + COMPLETE_WITH_CONST("PUBLICATION"); + else if (Matches6("CREATE", "SUBSCRIPTION", MatchAny, "CONNECTION", + MatchAny, "PUBLICATION")) + { + /* complete with nothing here as this refers to remote publications */ + } + else if (HeadMatches2("CREATE", "SUBSCRIPTION") && TailMatches2("PUBLICATION", MatchAny)) + COMPLETE_WITH_CONST("WITH ("); + /* Complete "CREATE SUBSCRIPTION ... WITH ( " */ + else if (HeadMatches2("CREATE", "SUBSCRIPTION") && TailMatches2("WITH", "(")) + COMPLETE_WITH_LIST6("copy_data", "connect", "create_slot", "enabled", + "slot_name", "synchronous_commit"); + +/* CREATE TRIGGER --- is allowed inside CREATE SCHEMA, so use TailMatches */ + /* complete CREATE TRIGGER with BEFORE,AFTER,INSTEAD OF */ + else if (TailMatches3("CREATE", "TRIGGER", MatchAny)) + COMPLETE_WITH_LIST3("BEFORE", "AFTER", "INSTEAD OF"); + /* complete CREATE TRIGGER BEFORE,AFTER with an event */ + else if (TailMatches4("CREATE", "TRIGGER", MatchAny, "BEFORE|AFTER")) + COMPLETE_WITH_LIST4("INSERT", "DELETE", "UPDATE", "TRUNCATE"); + /* complete CREATE TRIGGER INSTEAD OF with an event */ + else if (TailMatches5("CREATE", "TRIGGER", MatchAny, "INSTEAD", "OF")) + COMPLETE_WITH_LIST3("INSERT", "DELETE", "UPDATE"); + /* complete CREATE TRIGGER BEFORE,AFTER sth with OR,ON */ + else if (TailMatches5("CREATE", "TRIGGER", MatchAny, "BEFORE|AFTER", MatchAny) || + TailMatches6("CREATE", "TRIGGER", MatchAny, "INSTEAD", "OF", MatchAny)) + COMPLETE_WITH_LIST2("ON", "OR"); + + /* + * complete CREATE TRIGGER BEFORE,AFTER event ON with a list of + * tables + */ + else if (TailMatches6("CREATE", "TRIGGER", MatchAny, "BEFORE|AFTER", MatchAny, "ON")) + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tables, NULL); + /* complete CREATE TRIGGER ... INSTEAD OF event ON with a list of views */ + else if (TailMatches7("CREATE", "TRIGGER", MatchAny, "INSTEAD", "OF", MatchAny, "ON")) + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_views, NULL); + else if (HeadMatches2("CREATE", "TRIGGER") && TailMatches2("ON", MatchAny)) + COMPLETE_WITH_LIST7("NOT DEFERRABLE", "DEFERRABLE", "INITIALLY", + "REFERENCING", "FOR", "WHEN (", "EXECUTE PROCEDURE"); + else if (HeadMatches2("CREATE", "TRIGGER") && + (TailMatches1("DEFERRABLE") || TailMatches2("INITIALLY", "IMMEDIATE|DEFERRED"))) + COMPLETE_WITH_LIST4("REFERENCING", "FOR", "WHEN (", "EXECUTE PROCEDURE"); + else if (HeadMatches2("CREATE", "TRIGGER") && TailMatches1("REFERENCING")) + COMPLETE_WITH_LIST2("OLD TABLE", "NEW TABLE"); + else if (HeadMatches2("CREATE", "TRIGGER") && TailMatches2("OLD|NEW", "TABLE")) + COMPLETE_WITH_CONST("AS"); + else if (HeadMatches2("CREATE", "TRIGGER") && + (TailMatches5("REFERENCING", "OLD", "TABLE", "AS", MatchAny) || + TailMatches4("REFERENCING", "OLD", "TABLE", MatchAny))) + COMPLETE_WITH_LIST4("NEW TABLE", "FOR", "WHEN (", "EXECUTE PROCEDURE"); + else if (HeadMatches2("CREATE", "TRIGGER") && + (TailMatches5("REFERENCING", "NEW", "TABLE", "AS", MatchAny) || + TailMatches4("REFERENCING", "NEW", "TABLE", MatchAny))) + COMPLETE_WITH_LIST4("OLD TABLE", "FOR", "WHEN (", "EXECUTE PROCEDURE"); + else if (HeadMatches2("CREATE", "TRIGGER") && + (TailMatches9("REFERENCING", "OLD|NEW", "TABLE", "AS", MatchAny, "OLD|NEW", "TABLE", "AS", MatchAny) || + TailMatches8("REFERENCING", "OLD|NEW", "TABLE", MatchAny, "OLD|NEW", "TABLE", "AS", MatchAny) || + TailMatches8("REFERENCING", "OLD|NEW", "TABLE", "AS", MatchAny, "OLD|NEW", "TABLE", MatchAny) || + TailMatches7("REFERENCING", "OLD|NEW", "TABLE", MatchAny, "OLD|NEW", "TABLE", MatchAny))) + COMPLETE_WITH_LIST3("FOR", "WHEN (", "EXECUTE PROCEDURE"); + else if (HeadMatches2("CREATE", "TRIGGER") && TailMatches1("FOR")) + COMPLETE_WITH_LIST3("EACH", "ROW", "STATEMENT"); + else if (HeadMatches2("CREATE", "TRIGGER") && TailMatches2("FOR", "EACH")) + COMPLETE_WITH_LIST2("ROW", "STATEMENT"); + else if (HeadMatches2("CREATE", "TRIGGER") && + (TailMatches3("FOR", "EACH", "ROW|STATEMENT") || + TailMatches2("FOR", "ROW|STATEMENT"))) + COMPLETE_WITH_LIST2("WHEN (", "EXECUTE PROCEDURE"); + /* complete CREATE TRIGGER ... EXECUTE with PROCEDURE */ + else if (HeadMatches2("CREATE", "TRIGGER") && TailMatches1("EXECUTE")) + COMPLETE_WITH_CONST("PROCEDURE"); + else if (HeadMatches2("CREATE", "TRIGGER") && TailMatches2("EXECUTE", "PROCEDURE")) + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_functions, NULL); + +/* CREATE ROLE,USER,GROUP */ + else if (Matches3("CREATE", "ROLE|GROUP|USER", MatchAny) && + !TailMatches2("USER", "MAPPING")) + { + static const char *const list_CREATEROLE[] = + {"ADMIN", "BYPASSRLS", "CONNECTION LIMIT", "CREATEDB", "CREATEROLE", "CREATEUSER", + "ENCRYPTED PASSWORD", "IN", "INHERIT", "LOGIN", "NOBYPASSRLS", + "NOCREATEDB", "NOCREATEROLE", "NOCREATEUSER", "NOINHERIT", + "NOLOGIN", "NOREPLICATION", "NOSUPERUSER", "PASSWORD", + "REPLICATION", "ROLE", "SUPERUSER", "SYSID", + "VALID UNTIL", "WITH", NULL}; + + COMPLETE_WITH_LIST(list_CREATEROLE); + } + +/* CREATE ROLE,USER,GROUP WITH */ + else if (Matches4("CREATE", "ROLE|GROUP|USER", MatchAny, "WITH")) + { + /* Similar to the above, but don't complete "WITH" again. */ + static const char *const list_CREATEROLE_WITH[] = + {"ADMIN", "BYPASSRLS", "CONNECTION LIMIT", "CREATEDB", "CREATEROLE", + "ENCRYPTED PASSWORD", "IN", "INHERIT", "LOGIN", "NOBYPASSRLS", + "NOCREATEDB", "NOCREATEROLE", "NOINHERIT", + "NOLOGIN", "NOREPLICATION", "NOSUPERUSER", "PASSWORD", + "REPLICATION", "ROLE", "SUPERUSER", "SYSID", + "VALID UNTIL", NULL}; + + COMPLETE_WITH_LIST(list_CREATEROLE_WITH); + } + + /* complete CREATE ROLE,USER,GROUP IN with ROLE,GROUP */ + else if (Matches4("CREATE", "ROLE|USER|GROUP", MatchAny, "IN")) + COMPLETE_WITH_LIST2("GROUP", "ROLE"); + +/* CREATE VIEW --- is allowed inside CREATE SCHEMA, so use TailMatches */ + /* Complete CREATE VIEW with AS */ + else if (TailMatches3("CREATE", "VIEW", MatchAny)) + COMPLETE_WITH_CONST("AS"); + /* Complete "CREATE VIEW AS with "SELECT" */ + else if (TailMatches4("CREATE", "VIEW", MatchAny, "AS")) + COMPLETE_WITH_CONST("SELECT"); + +/* CREATE MATERIALIZED VIEW */ + else if (Matches2("CREATE", "MATERIALIZED")) + COMPLETE_WITH_CONST("VIEW"); + /* Complete CREATE MATERIALIZED VIEW with AS */ + else if (Matches4("CREATE", "MATERIALIZED", "VIEW", MatchAny)) + COMPLETE_WITH_CONST("AS"); + /* Complete "CREATE MATERIALIZED VIEW AS with "SELECT" */ + else if (Matches5("CREATE", "MATERIALIZED", "VIEW", MatchAny, "AS")) + COMPLETE_WITH_CONST("SELECT"); + +/* CREATE EVENT TRIGGER */ + else if (Matches2("CREATE", "EVENT")) + COMPLETE_WITH_CONST("TRIGGER"); + /* Complete CREATE EVENT TRIGGER with ON */ + else if (Matches4("CREATE", "EVENT", "TRIGGER", MatchAny)) + COMPLETE_WITH_CONST("ON"); + /* Complete CREATE EVENT TRIGGER ON with event_type */ + else if (Matches5("CREATE", "EVENT", "TRIGGER", MatchAny, "ON")) + COMPLETE_WITH_LIST3("ddl_command_start", "ddl_command_end", "sql_drop"); + +/* DEALLOCATE */ + else if (Matches1("DEALLOCATE")) + COMPLETE_WITH_QUERY(Query_for_list_of_prepared_statements " UNION SELECT 'PREPARE'"); + else if (Matches2("DEALLOCATE", "PREPARE")) + COMPLETE_WITH_QUERY(Query_for_list_of_prepared_statements); + +/* DECLARE */ + else if (Matches2("DECLARE", MatchAny)) + COMPLETE_WITH_LIST5("BINARY", "INSENSITIVE", "SCROLL", "NO SCROLL", + "CURSOR"); + else if (HeadMatches1("DECLARE") && TailMatches1("CURSOR")) + COMPLETE_WITH_LIST3("WITH HOLD", "WITHOUT HOLD", "FOR"); + +/* DELETE --- can be inside EXPLAIN, RULE, etc */ + /* ... despite which, only complete DELETE with FROM at start of line */ + else if (Matches1("DELETE")) + COMPLETE_WITH_CONST("FROM"); + /* Complete DELETE FROM with a list of tables */ + else if (TailMatches2("DELETE", "FROM")) + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_updatables, NULL); + /* Complete DELETE FROM
*/ + else if (TailMatches3("DELETE", "FROM", MatchAny)) + COMPLETE_WITH_LIST2("USING", "WHERE"); + else if (TailMatches4("DELETE", "FROM", MatchAny, "USING")) + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tables, NULL); + else if (TailMatches5("DELETE", "FROM", MatchAny, "USING", MatchAny)) + COMPLETE_WITH_CONST("WHERE"); + else if ((TailMatches5("DELETE", MatchAny, "USING", MatchAny, "WHERE")) || + (TailMatches6("DELETE", "FROM", MatchAny, "USING", MatchAny, "WHERE"))) + COMPLETE_WITH_ATTR(prev2_wd, ""); + /* XXX: implement tab completion for DELETE ... USING */ + +/* DISCARD */ + else if (Matches1("DISCARD")) + COMPLETE_WITH_LIST4("ALL", "PLANS", "SEQUENCES", "TEMP"); + +/* DO */ + else if (Matches1("DO")) + COMPLETE_WITH_CONST("LANGUAGE"); + +/* DROP */ + /* Complete DROP object with CASCADE / RESTRICT */ + else if (Matches3("DROP", + "COLLATION|CONVERSION|DOMAIN|EXTENSION|LANGUAGE|PUBLICATION|SCHEMA|SEQUENCE|SERVER|SUBSCRIPTION|STATISTICS|TABLE|TYPE|VIEW", + MatchAny) || + Matches4("DROP", "ACCESS", "METHOD", MatchAny) || + (Matches4("DROP", "AGGREGATE|FUNCTION", MatchAny, MatchAny) && + ends_with(prev_wd, ')')) || + Matches4("DROP", "EVENT", "TRIGGER", MatchAny) || + Matches5("DROP", "FOREIGN", "DATA", "WRAPPER", MatchAny) || + Matches4("DROP", "FOREIGN", "TABLE", MatchAny) || + Matches5("DROP", "TEXT", "SEARCH", "CONFIGURATION|DICTIONARY|PARSER|TEMPLATE", MatchAny)) + COMPLETE_WITH_LIST2("CASCADE", "RESTRICT"); + + /* help completing some of the variants */ + else if (Matches3("DROP", "AGGREGATE|FUNCTION", MatchAny)) + COMPLETE_WITH_CONST("("); + else if (Matches4("DROP", "AGGREGATE|FUNCTION", MatchAny, "(")) + COMPLETE_WITH_FUNCTION_ARG(prev2_wd); + else if (Matches2("DROP", "FOREIGN")) + COMPLETE_WITH_LIST2("DATA WRAPPER", "TABLE"); + + /* DROP INDEX */ + else if (Matches2("DROP", "INDEX")) + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_indexes, + " UNION SELECT 'CONCURRENTLY'"); + else if (Matches3("DROP", "INDEX", "CONCURRENTLY")) + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_indexes, NULL); + else if (Matches3("DROP", "INDEX", MatchAny)) + COMPLETE_WITH_LIST2("CASCADE", "RESTRICT"); + else if (Matches4("DROP", "INDEX", "CONCURRENTLY", MatchAny)) + COMPLETE_WITH_LIST2("CASCADE", "RESTRICT"); + + /* DROP MATERIALIZED VIEW */ + else if (Matches2("DROP", "MATERIALIZED")) + COMPLETE_WITH_CONST("VIEW"); + else if (Matches3("DROP", "MATERIALIZED", "VIEW")) + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_matviews, NULL); + + /* DROP OWNED BY */ + else if (Matches2("DROP", "OWNED")) + COMPLETE_WITH_CONST("BY"); + else if (Matches3("DROP", "OWNED", "BY")) + COMPLETE_WITH_QUERY(Query_for_list_of_roles); + + /* DROP SCHEMA */ + else if (Matches2("DROP", "SCHEMA")) + COMPLETE_WITH_QUERY(Query_for_list_of_schemas + " UNION ALL SELECT 'IF EXISTS'"); + else if ((Matches3("DROP", "SCHEMA", MatchAny))) + COMPLETE_WITH_LIST2("CASCADE", "RESTRICT"); + else if (Matches4("DROP", "SCHEMA", MatchAny, MatchAny) + && TailMatches1(MatchAnyExcept("CASCADE|RESTRICT"))) + COMPLETE_WITH_QUERY(Query_for_list_of_schemas); + else if ((HeadMatches5("DROP", "SCHEMA", MatchAny, MatchAny, MatchAny)) + && TailMatches1(MatchAnyExcept("EXISTS|CASCADE|RESTRICT"))) + COMPLETE_WITH_LIST2("CASCADE", "RESTRICT"); + + /* DROP TEXT SEARCH */ + else if (Matches3("DROP", "TEXT", "SEARCH")) + COMPLETE_WITH_LIST4("CONFIGURATION", "DICTIONARY", "PARSER", "TEMPLATE"); + + /* DROP TRIGGER */ + else if (Matches3("DROP", "TRIGGER", MatchAny)) + COMPLETE_WITH_CONST("ON"); + else if (Matches4("DROP", "TRIGGER", MatchAny, "ON")) + { + completion_info_charp = prev2_wd; + COMPLETE_WITH_QUERY(Query_for_list_of_tables_for_trigger); + } + else if (Matches5("DROP", "TRIGGER", MatchAny, "ON", MatchAny)) + COMPLETE_WITH_LIST2("CASCADE", "RESTRICT"); + + /* DROP ACCESS METHOD */ + else if (Matches2("DROP", "ACCESS")) + COMPLETE_WITH_CONST("METHOD"); + else if (Matches3("DROP", "ACCESS", "METHOD")) + COMPLETE_WITH_QUERY(Query_for_list_of_access_methods); + + /* DROP EVENT TRIGGER */ + else if (Matches2("DROP", "EVENT")) + COMPLETE_WITH_CONST("TRIGGER"); + else if (Matches3("DROP", "EVENT", "TRIGGER")) + COMPLETE_WITH_QUERY(Query_for_list_of_event_triggers); + + /* DROP POLICY */ + else if (Matches2("DROP", "POLICY")) + COMPLETE_WITH_QUERY(Query_for_list_of_policies); + /* DROP POLICY ON */ + else if (Matches3("DROP", "POLICY", MatchAny)) + COMPLETE_WITH_CONST("ON"); + /* DROP POLICY ON
*/ + else if (Matches4("DROP", "POLICY", MatchAny, "ON")) + { + completion_info_charp = prev2_wd; + COMPLETE_WITH_QUERY(Query_for_list_of_tables_for_policy); + } + + /* DROP RULE */ + else if (Matches3("DROP", "RULE", MatchAny)) + COMPLETE_WITH_CONST("ON"); + else if (Matches4("DROP", "RULE", MatchAny, "ON")) + { + completion_info_charp = prev2_wd; + COMPLETE_WITH_QUERY(Query_for_list_of_tables_for_rule); + } + else if (Matches5("DROP", "RULE", MatchAny, "ON", MatchAny)) + COMPLETE_WITH_LIST2("CASCADE", "RESTRICT"); + + /* DROP TABLE */ + else if (Matches2("DROP", "TABLE")) + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tables, + " UNION ALL SELECT 'IF EXISTS'"); + else if ((Matches3("DROP", "TABLE", MatchAny))) + COMPLETE_WITH_LIST2("CASCADE", "RESTRICT"); + else if (Matches4("DROP", "TABLE", MatchAny, MatchAny) + && TailMatches1(MatchAnyExcept("CASCADE|RESTRICT"))) + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tables, NULL); + else if ((HeadMatches5("DROP", "TABLE", MatchAny, MatchAny, MatchAny)) + && TailMatches1(MatchAnyExcept("EXISTS|CASCADE|RESTRICT"))) + COMPLETE_WITH_LIST2("CASCADE", "RESTRICT"); + + /* DROP USER */ + else if (HeadMatches2("DROP", "USER")) + COMPLETE_WITH_QUERY(Query_for_list_of_users + " UNION ALL SELECT 'IF EXISTS'"); + else if (HeadMatches4("DROP", "USER", "IF", "EXISTS")) + COMPLETE_WITH_QUERY(Query_for_list_of_users); + + /* DROP VIEW */ + else if (Matches2("DROP", "VIEW")) + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_views, + " UNION ALL SELECT 'IF EXISTS'"); + else if ((Matches3("DROP", "VIEW", MatchAny))) + COMPLETE_WITH_LIST2("CASCADE", "RESTRICT"); + else if (Matches4("DROP", "VIEW", MatchAny, MatchAny) + && TailMatches1(MatchAnyExcept("CASCADE|RESTRICT"))) + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_views, NULL); + else if ((HeadMatches5("DROP", "VIEW", MatchAny, MatchAny, MatchAny)) + && TailMatches1(MatchAnyExcept("EXISTS|CASCADE|RESTRICT"))) + COMPLETE_WITH_LIST2("CASCADE", "RESTRICT"); + + +/* EXECUTE */ + else if (Matches1("EXECUTE")) + COMPLETE_WITH_QUERY(Query_for_list_of_prepared_statements); + +/* EXPLAIN */ + + /* + * Complete EXPLAIN [ANALYZE] [VERBOSE] with list of EXPLAIN-able commands + */ + else if (Matches1("EXPLAIN")) + COMPLETE_WITH_LIST7("SELECT", "INSERT", "DELETE", "UPDATE", "DECLARE", + "ANALYZE", "VERBOSE"); + else if (Matches2("EXPLAIN", "ANALYZE")) + COMPLETE_WITH_LIST6("SELECT", "INSERT", "DELETE", "UPDATE", "DECLARE", + "VERBOSE"); + else if (Matches2("EXPLAIN", "VERBOSE") || + Matches3("EXPLAIN", "ANALYZE", "VERBOSE")) + COMPLETE_WITH_LIST5("SELECT", "INSERT", "DELETE", "UPDATE", "DECLARE"); + + /* FETCH && MOVE */ + /* Complete FETCH with one of FORWARD, BACKWARD, RELATIVE */ + else if (Matches1("FETCH|MOVE")) + COMPLETE_WITH_LIST4("ABSOLUTE", "BACKWARD", "FORWARD", "RELATIVE"); + /* Complete FETCH with one of ALL, NEXT, PRIOR */ + else if (Matches2("FETCH|MOVE", MatchAny)) + COMPLETE_WITH_LIST3("ALL", "NEXT", "PRIOR"); + + /* + * Complete FETCH with "FROM" or "IN". These are equivalent, + * but we may as well tab-complete both: perhaps some users prefer one + * variant or the other. + */ + else if (Matches3("FETCH|MOVE", MatchAny, MatchAny)) + COMPLETE_WITH_LIST2("FROM", "IN"); + +/* FOREIGN DATA WRAPPER */ + /* applies in ALTER/DROP FDW and in CREATE SERVER */ + else if (TailMatches3("FOREIGN", "DATA", "WRAPPER") && + !TailMatches4("CREATE", MatchAny, MatchAny, MatchAny)) + COMPLETE_WITH_QUERY(Query_for_list_of_fdws); + /* applies in CREATE SERVER */ + else if (TailMatches4("FOREIGN", "DATA", "WRAPPER", MatchAny) && + HeadMatches2("CREATE", "SERVER")) + COMPLETE_WITH_CONST("OPTIONS"); + +/* FOREIGN TABLE */ + else if (TailMatches2("FOREIGN", "TABLE") && + !TailMatches3("CREATE", MatchAny, MatchAny)) + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_foreign_tables, NULL); + +/* FOREIGN SERVER */ + else if (TailMatches2("FOREIGN", "SERVER")) + COMPLETE_WITH_QUERY(Query_for_list_of_servers); + +/* + * GRANT and REVOKE are allowed inside CREATE SCHEMA and + * ALTER DEFAULT PRIVILEGES, so use TailMatches + */ + /* Complete GRANT/REVOKE with a list of roles and privileges */ + else if (TailMatches1("GRANT|REVOKE")) + { + /* + * With ALTER DEFAULT PRIVILEGES, restrict completion to grantable + * privileges (can't grant roles) + */ + if (HeadMatches3("ALTER", "DEFAULT", "PRIVILEGES")) + COMPLETE_WITH_LIST10("SELECT", "INSERT", "UPDATE", + "DELETE", "TRUNCATE", "REFERENCES", "TRIGGER", + "EXECUTE", "USAGE", "ALL"); + else + COMPLETE_WITH_QUERY(Query_for_list_of_roles + " UNION SELECT 'SELECT'" + " UNION SELECT 'INSERT'" + " UNION SELECT 'UPDATE'" + " UNION SELECT 'DELETE'" + " UNION SELECT 'TRUNCATE'" + " UNION SELECT 'REFERENCES'" + " UNION SELECT 'TRIGGER'" + " UNION SELECT 'CREATE'" + " UNION SELECT 'CONNECT'" + " UNION SELECT 'TEMPORARY'" + " UNION SELECT 'EXECUTE'" + " UNION SELECT 'USAGE'" + " UNION SELECT 'ALL'"); + } + + /* + * Complete GRANT/REVOKE with "ON", GRANT/REVOKE with + * TO/FROM + */ + else if (TailMatches2("GRANT|REVOKE", MatchAny)) + { + if (TailMatches1("SELECT|INSERT|UPDATE|DELETE|TRUNCATE|REFERENCES|TRIGGER|CREATE|CONNECT|TEMPORARY|TEMP|EXECUTE|USAGE|ALL")) + COMPLETE_WITH_CONST("ON"); + else if (TailMatches2("GRANT", MatchAny)) + COMPLETE_WITH_CONST("TO"); + else + COMPLETE_WITH_CONST("FROM"); + } + + /* + * Complete GRANT/REVOKE ON with a list of tables, views, and + * sequences. + * + * Keywords like DATABASE, FUNCTION, LANGUAGE and SCHEMA added to query + * result via UNION; seems to work intuitively. + * + * Note: GRANT/REVOKE can get quite complex; tab-completion as implemented + * here will only work if the privilege list contains exactly one + * privilege. + */ + else if (TailMatches3("GRANT|REVOKE", MatchAny, "ON")) + { + /* + * With ALTER DEFAULT PRIVILEGES, restrict completion to the kinds of + * objects supported. + */ + if (HeadMatches3("ALTER", "DEFAULT", "PRIVILEGES")) + COMPLETE_WITH_LIST5("TABLES", "SEQUENCES", "FUNCTIONS", "TYPES", "SCHEMAS"); + else + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tsvmf, + " UNION SELECT 'ALL FUNCTIONS IN SCHEMA'" + " UNION SELECT 'ALL SEQUENCES IN SCHEMA'" + " UNION SELECT 'ALL TABLES IN SCHEMA'" + " UNION SELECT 'DATABASE'" + " UNION SELECT 'DOMAIN'" + " UNION SELECT 'FOREIGN DATA WRAPPER'" + " UNION SELECT 'FOREIGN SERVER'" + " UNION SELECT 'FUNCTION'" + " UNION SELECT 'LANGUAGE'" + " UNION SELECT 'LARGE OBJECT'" + " UNION SELECT 'SCHEMA'" + " UNION SELECT 'SEQUENCE'" + " UNION SELECT 'TABLE'" + " UNION SELECT 'TABLESPACE'" + " UNION SELECT 'TYPE'"); + } + else if (TailMatches4("GRANT|REVOKE", MatchAny, "ON", "ALL")) + COMPLETE_WITH_LIST3("FUNCTIONS IN SCHEMA", "SEQUENCES IN SCHEMA", + "TABLES IN SCHEMA"); + else if (TailMatches4("GRANT|REVOKE", MatchAny, "ON", "FOREIGN")) + COMPLETE_WITH_LIST2("DATA WRAPPER", "SERVER"); + + /* + * Complete "GRANT/REVOKE * ON DATABASE/DOMAIN/..." with a list of + * appropriate objects. + * + * Complete "GRANT/REVOKE * ON *" with "TO/FROM". + */ + else if (TailMatches4("GRANT|REVOKE", MatchAny, "ON", MatchAny)) + { + if (TailMatches1("DATABASE")) + COMPLETE_WITH_QUERY(Query_for_list_of_databases); + else if (TailMatches1("DOMAIN")) + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_domains, NULL); + else if (TailMatches1("FUNCTION")) + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_functions, NULL); + else if (TailMatches1("LANGUAGE")) + COMPLETE_WITH_QUERY(Query_for_list_of_languages); + else if (TailMatches1("SCHEMA")) + COMPLETE_WITH_QUERY(Query_for_list_of_schemas); + else if (TailMatches1("SEQUENCE")) + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_sequences, NULL); + else if (TailMatches1("TABLE")) + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tsvmf, NULL); + else if (TailMatches1("TABLESPACE")) + COMPLETE_WITH_QUERY(Query_for_list_of_tablespaces); + else if (TailMatches1("TYPE")) + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_datatypes, NULL); + else if (TailMatches4("GRANT", MatchAny, MatchAny, MatchAny)) + COMPLETE_WITH_CONST("TO"); + else + COMPLETE_WITH_CONST("FROM"); + } + + /* + * Complete "GRANT/REVOKE ... TO/FROM" with username, PUBLIC, + * CURRENT_USER, or SESSION_USER. + */ + else if ((HeadMatches1("GRANT") && TailMatches1("TO")) || + (HeadMatches1("REVOKE") && TailMatches1("FROM"))) + COMPLETE_WITH_QUERY(Query_for_list_of_grant_roles); + /* Complete "ALTER DEFAULT PRIVILEGES ... GRANT/REVOKE ... TO/FROM */ + else if (HeadMatches3("ALTER", "DEFAULT", "PRIVILEGES") && TailMatches1("TO|FROM")) + COMPLETE_WITH_QUERY(Query_for_list_of_grant_roles); + /* Complete "GRANT/REVOKE ... ON * *" with TO/FROM */ + else if (HeadMatches1("GRANT") && TailMatches3("ON", MatchAny, MatchAny)) + COMPLETE_WITH_CONST("TO"); + else if (HeadMatches1("REVOKE") && TailMatches3("ON", MatchAny, MatchAny)) + COMPLETE_WITH_CONST("FROM"); + + /* Complete "GRANT/REVOKE * ON ALL * IN SCHEMA *" with TO/FROM */ + else if (TailMatches8("GRANT|REVOKE", MatchAny, "ON", "ALL", MatchAny, "IN", "SCHEMA", MatchAny)) + { + if (TailMatches8("GRANT", MatchAny, MatchAny, MatchAny, MatchAny, MatchAny, MatchAny, MatchAny)) + COMPLETE_WITH_CONST("TO"); + else + COMPLETE_WITH_CONST("FROM"); + } + + /* Complete "GRANT/REVOKE * ON FOREIGN DATA WRAPPER *" with TO/FROM */ + else if (TailMatches7("GRANT|REVOKE", MatchAny, "ON", "FOREIGN", "DATA", "WRAPPER", MatchAny)) + { + if (TailMatches7("GRANT", MatchAny, MatchAny, MatchAny, MatchAny, MatchAny, MatchAny)) + COMPLETE_WITH_CONST("TO"); + else + COMPLETE_WITH_CONST("FROM"); + } + + /* Complete "GRANT/REVOKE * ON FOREIGN SERVER *" with TO/FROM */ + else if (TailMatches6("GRANT|REVOKE", MatchAny, "ON", "FOREIGN", "SERVER", MatchAny)) + { + if (TailMatches6("GRANT", MatchAny, MatchAny, MatchAny, MatchAny, MatchAny)) + COMPLETE_WITH_CONST("TO"); + else + COMPLETE_WITH_CONST("FROM"); + } + +/* GROUP BY */ + else if (TailMatches3("FROM", MatchAny, "GROUP")) + COMPLETE_WITH_CONST("BY"); + +/* IMPORT FOREIGN SCHEMA */ + else if (Matches1("IMPORT")) + COMPLETE_WITH_CONST("FOREIGN SCHEMA"); + else if (Matches2("IMPORT", "FOREIGN")) + COMPLETE_WITH_CONST("SCHEMA"); + +/* INSERT --- can be inside EXPLAIN, RULE, etc */ + /* Complete INSERT with "INTO" */ + else if (TailMatches1("INSERT")) + COMPLETE_WITH_CONST("INTO"); + /* Complete INSERT INTO with table names */ + else if (TailMatches2("INSERT", "INTO")) + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_updatables, NULL); + /* Complete "INSERT INTO
(" with attribute names */ + else if (TailMatches4("INSERT", "INTO", MatchAny, "(")) + COMPLETE_WITH_ATTR(prev2_wd, ""); + + /* + * Complete INSERT INTO
with "(" or "VALUES" or "SELECT" or + * "TABLE" or "DEFAULT VALUES" or "OVERRIDING" + */ + else if (TailMatches3("INSERT", "INTO", MatchAny)) + COMPLETE_WITH_LIST6("(", "DEFAULT VALUES", "SELECT", "TABLE", "VALUES", "OVERRIDING"); + + /* + * Complete INSERT INTO
(attribs) with "VALUES" or "SELECT" or + * "TABLE" or "OVERRIDING" + */ + else if (TailMatches4("INSERT", "INTO", MatchAny, MatchAny) && + ends_with(prev_wd, ')')) + COMPLETE_WITH_LIST4("SELECT", "TABLE", "VALUES", "OVERRIDING"); + + /* Complete OVERRIDING */ + else if (TailMatches1("OVERRIDING")) + COMPLETE_WITH_LIST2("SYSTEM VALUE", "USER VALUE"); + + /* Complete after OVERRIDING clause */ + else if (TailMatches3("OVERRIDING", MatchAny, "VALUE")) + COMPLETE_WITH_LIST3("SELECT", "TABLE", "VALUES"); + + /* Insert an open parenthesis after "VALUES" */ + else if (TailMatches1("VALUES") && !TailMatches2("DEFAULT", "VALUES")) + COMPLETE_WITH_CONST("("); + +/* LOCK */ + /* Complete LOCK [TABLE] with a list of tables */ + else if (Matches1("LOCK")) + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tables, + " UNION SELECT 'TABLE'"); + else if (Matches2("LOCK", "TABLE")) + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tables, ""); + + /* For the following, handle the case of a single table only for now */ + + /* Complete LOCK [TABLE]
with "IN" */ + else if (Matches2("LOCK", MatchAnyExcept("TABLE")) || + Matches3("LOCK", "TABLE", MatchAny)) + COMPLETE_WITH_CONST("IN"); + + /* Complete LOCK [TABLE]
IN with a lock mode */ + else if (Matches3("LOCK", MatchAny, "IN") || + Matches4("LOCK", "TABLE", MatchAny, "IN")) + COMPLETE_WITH_LIST8("ACCESS SHARE MODE", + "ROW SHARE MODE", "ROW EXCLUSIVE MODE", + "SHARE UPDATE EXCLUSIVE MODE", "SHARE MODE", + "SHARE ROW EXCLUSIVE MODE", + "EXCLUSIVE MODE", "ACCESS EXCLUSIVE MODE"); + + /* Complete LOCK [TABLE]
IN ACCESS|ROW with rest of lock mode */ + else if (Matches4("LOCK", MatchAny, "IN", "ACCESS|ROW") || + Matches5("LOCK", "TABLE", MatchAny, "IN", "ACCESS|ROW")) + COMPLETE_WITH_LIST2("EXCLUSIVE MODE", "SHARE MODE"); + + /* Complete LOCK [TABLE]
IN SHARE with rest of lock mode */ + else if (Matches4("LOCK", MatchAny, "IN", "SHARE") || + Matches5("LOCK", "TABLE", MatchAny, "IN", "SHARE")) + COMPLETE_WITH_LIST3("MODE", "ROW EXCLUSIVE MODE", + "UPDATE EXCLUSIVE MODE"); + +/* NOTIFY --- can be inside EXPLAIN, RULE, etc */ + else if (TailMatches1("NOTIFY")) + COMPLETE_WITH_QUERY("SELECT pg_catalog.quote_ident(channel) FROM pg_catalog.pg_listening_channels() AS channel WHERE substring(pg_catalog.quote_ident(channel),1,%d)='%s'"); + +/* OPTIONS */ + else if (TailMatches1("OPTIONS")) + COMPLETE_WITH_CONST("("); + +/* OWNER TO - complete with available roles */ + else if (TailMatches2("OWNER", "TO")) + COMPLETE_WITH_QUERY(Query_for_list_of_roles); + +/* ORDER BY */ + else if (TailMatches3("FROM", MatchAny, "ORDER")) + COMPLETE_WITH_CONST("BY"); + else if (TailMatches4("FROM", MatchAny, "ORDER", "BY")) + COMPLETE_WITH_ATTR(prev3_wd, ""); + +/* PREPARE xx AS */ + else if (Matches2("PREPARE", MatchAny)) + COMPLETE_WITH_CONST("AS"); + else if (Matches3("PREPARE", MatchAny, "AS")) + COMPLETE_WITH_LIST4("SELECT", "UPDATE", "INSERT", "DELETE FROM"); + +/* + * PREPARE TRANSACTION is missing on purpose. It's intended for transaction + * managers, not for manual use in interactive sessions. + */ + +/* REASSIGN OWNED BY xxx TO yyy */ + else if (Matches1("REASSIGN")) + COMPLETE_WITH_CONST("OWNED BY"); + else if (Matches2("REASSIGN", "OWNED")) + COMPLETE_WITH_CONST("BY"); + else if (Matches3("REASSIGN", "OWNED", "BY")) + COMPLETE_WITH_QUERY(Query_for_list_of_roles); + else if (Matches4("REASSIGN", "OWNED", "BY", MatchAny)) + COMPLETE_WITH_CONST("TO"); + else if (Matches5("REASSIGN", "OWNED", "BY", MatchAny, "TO")) + COMPLETE_WITH_QUERY(Query_for_list_of_roles); + +/* REFRESH MATERIALIZED VIEW */ + else if (Matches1("REFRESH")) + COMPLETE_WITH_CONST("MATERIALIZED VIEW"); + else if (Matches2("REFRESH", "MATERIALIZED")) + COMPLETE_WITH_CONST("VIEW"); + else if (Matches3("REFRESH", "MATERIALIZED", "VIEW")) + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_matviews, + " UNION SELECT 'CONCURRENTLY'"); + else if (Matches4("REFRESH", "MATERIALIZED", "VIEW", "CONCURRENTLY")) + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_matviews, NULL); + else if (Matches4("REFRESH", "MATERIALIZED", "VIEW", MatchAny)) + COMPLETE_WITH_CONST("WITH"); + else if (Matches5("REFRESH", "MATERIALIZED", "VIEW", "CONCURRENTLY", MatchAny)) + COMPLETE_WITH_CONST("WITH"); + else if (Matches5("REFRESH", "MATERIALIZED", "VIEW", MatchAny, "WITH")) + COMPLETE_WITH_LIST2("NO DATA", "DATA"); + else if (Matches6("REFRESH", "MATERIALIZED", "VIEW", "CONCURRENTLY", MatchAny, "WITH")) + COMPLETE_WITH_LIST2("NO DATA", "DATA"); + else if (Matches6("REFRESH", "MATERIALIZED", "VIEW", MatchAny, "WITH", "NO")) + COMPLETE_WITH_CONST("DATA"); + else if (Matches7("REFRESH", "MATERIALIZED", "VIEW", "CONCURRENTLY", MatchAny, "WITH", "NO")) + COMPLETE_WITH_CONST("DATA"); + +/* REINDEX */ + else if (Matches1("REINDEX")) + COMPLETE_WITH_LIST5("TABLE", "INDEX", "SYSTEM", "SCHEMA", "DATABASE"); + else if (Matches2("REINDEX", "TABLE")) + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tm, NULL); + else if (Matches2("REINDEX", "INDEX")) + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_indexes, NULL); + else if (Matches2("REINDEX", "SCHEMA")) + COMPLETE_WITH_QUERY(Query_for_list_of_schemas); + else if (Matches2("REINDEX", "SYSTEM|DATABASE")) + COMPLETE_WITH_QUERY(Query_for_list_of_databases); + +/* SECURITY LABEL */ + else if (Matches1("SECURITY")) + COMPLETE_WITH_CONST("LABEL"); + else if (Matches2("SECURITY", "LABEL")) + COMPLETE_WITH_LIST2("ON", "FOR"); + else if (Matches4("SECURITY", "LABEL", "FOR", MatchAny)) + COMPLETE_WITH_CONST("ON"); + else if (Matches3("SECURITY", "LABEL", "ON") || + Matches5("SECURITY", "LABEL", "FOR", MatchAny, "ON")) + { + static const char *const list_SECURITY_LABEL[] = + {"TABLE", "COLUMN", "AGGREGATE", "DATABASE", "DOMAIN", + "EVENT TRIGGER", "FOREIGN TABLE", "FUNCTION", "LARGE OBJECT", + "MATERIALIZED VIEW", "LANGUAGE", "PUBLICATION", "ROLE", "SCHEMA", + "SEQUENCE", "SUBSCRIPTION", "TABLESPACE", "TYPE", "VIEW", NULL}; + + COMPLETE_WITH_LIST(list_SECURITY_LABEL); + } + else if (Matches5("SECURITY", "LABEL", "ON", MatchAny, MatchAny)) + COMPLETE_WITH_CONST("IS"); + +/* SELECT */ + else if (Matches1("SELECT")) + COMPLETE_WITH_LIST4("TOP", "ALL", "DISTINCT", "*"); +/* remove if MatchAny below works for * + else if (Matches2("SELECT", "*")) + COMPLETE_WITH_LIST7("FROM", "WHERE", "GROUP BY", "HAVING", "ORDER BY", "LIMIT", "OFFSET"); +*/ + else if (Matches2("SELECT", MatchAny)) + COMPLETE_WITH_LIST8("FROM", "INTO", "WHERE", "GROUP BY", "HAVING", "ORDER BY", "LIMIT", "OFFSET"); + else if (HeadMatches1("SELECT") && TailMatches1("INTO")) + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tables, + "UNION SELECT 'TEMP' UNION SELECT 'TEMPORARY' UNION SELECT 'TABLE'"); + else if (HeadMatches1("SELECT") && TailMatches2("INTO", MatchAny)) + COMPLETE_WITH_LIST7("FROM", "WHERE", "GROUP BY", "HAVING", "ORDER BY", "LIMIT", "OFFSET"); + /* SELECT FROM */ + else if (HeadMatches1("SELECT") && TailMatches1("FROM")) + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tables, NULL); + else if (HeadMatches1("SELECT") && TailMatches2("FROM", MatchAny)) + COMPLETE_WITH_LIST7("WHERE", "GROUP BY", "HAVING", "UNION", "ORDER BY", "LIMIT", "OFFSET"); + /* SELECT WHERE */ + else if (HeadMatches1("SELECT") && TailMatches3("FROM", MatchAny, "WHERE")) + COMPLETE_WITH_ATTR(prev2_wd, ""); + else if ( + (HeadMatches1("SELECT") && TailMatches3("WHERE", MatchAny, MatchAnyExcept("HAVING|BY|LIMIT|OFFSET"))) || + (HeadMatches1("SELECT") && TailMatches4("WHERE", MatchAny, MatchAnyExcept("HAVING|BY|LIMIT|OFFSET"), MatchAnyExcept("HAVING|BY|LIMIT|OFFSET"))) + ) + COMPLETE_WITH_LIST5("GROUP BY", "HAVING", "ORDER BY", "LIMIT", "OFFSET"); + /* SELECT GROUP BY */ + else if (HeadMatches1("SELECT") && TailMatches3("GROUP", "BY", MatchAny)) + COMPLETE_WITH_LIST5("HAVING", "UNION", "ORDER BY", "LIMIT", "OFFSET"); + /* SELECT ORDER BY */ + else if (HeadMatches1("SELECT") && TailMatches3("ORDER", "BY", MatchAny)) + COMPLETE_WITH_LIST2("ASC", "DESC"); + else if ((HeadMatches1("SELECT") && TailMatches3("ORDER", "BY", MatchAny)) || + (HeadMatches1("SELECT") && TailMatches4("ORDER", "BY", MatchAny, "ASC|DESC"))) + COMPLETE_WITH_LIST2("LIMIT", "OFFSET"); + /* SELECT LIMIT */ + else if (HeadMatches1("SELECT") && TailMatches1("LIMIT")) + COMPLETE_WITH_CONST("ALL"); + /* SELECT OFFSET */ + else if (HeadMatches1("SELECT") && TailMatches2("LIMIT", MatchAny)) + COMPLETE_WITH_CONST("OFFSET"); + + +/* SET, RESET, SHOW */ + /* Complete with a variable name */ + else if (TailMatches1("SET|RESET") && !TailMatches3("UPDATE", MatchAny, "SET")) + COMPLETE_WITH_QUERY(Query_for_list_of_set_vars + " UNION SELECT 'SESSION'" + " UNION SELECT 'LOCAL'" + " UNION SELECT 'SEED'"); + else if (Matches1("SHOW")) + COMPLETE_WITH_QUERY(Query_for_list_of_show_vars); + /* Complete "SET TRANSACTION" */ + else if (Matches2("SET", "TRANSACTION")) + COMPLETE_WITH_LIST5("SNAPSHOT", "ISOLATION LEVEL", "READ", "DEFERRABLE", "NOT DEFERRABLE"); + else if (Matches2("BEGIN|START", "TRANSACTION") || + Matches2("BEGIN", "WORK") || + Matches1("BEGIN") || + Matches5("SET", "SESSION", "CHARACTERISTICS", "AS", "TRANSACTION")) + COMPLETE_WITH_LIST4("ISOLATION LEVEL", "READ", "DEFERRABLE", "NOT DEFERRABLE"); + else if (Matches3("SET|BEGIN|START", "TRANSACTION|WORK", "NOT") || + Matches2("BEGIN", "NOT") || + Matches6("SET", "SESSION", "CHARACTERISTICS", "AS", "TRANSACTION", "NOT")) + COMPLETE_WITH_CONST("DEFERRABLE"); + else if (Matches3("SET|BEGIN|START", "TRANSACTION|WORK", "ISOLATION") || + Matches2("BEGIN", "ISOLATION") || + Matches6("SET", "SESSION", "CHARACTERISTICS", "AS", "TRANSACTION", "ISOLATION")) + COMPLETE_WITH_CONST("LEVEL"); + else if (Matches4("SET|BEGIN|START", "TRANSACTION|WORK", "ISOLATION", "LEVEL") || + Matches3("BEGIN", "ISOLATION", "LEVEL") || + Matches7("SET", "SESSION", "CHARACTERISTICS", "AS", "TRANSACTION", "ISOLATION", "LEVEL")) + COMPLETE_WITH_LIST3("READ", "REPEATABLE READ", "SERIALIZABLE"); + else if (Matches5("SET|BEGIN|START", "TRANSACTION|WORK", "ISOLATION", "LEVEL", "READ") || + Matches4("BEGIN", "ISOLATION", "LEVEL", "READ") || + Matches8("SET", "SESSION", "CHARACTERISTICS", "AS", "TRANSACTION", "ISOLATION", "LEVEL", "READ")) + COMPLETE_WITH_LIST2("UNCOMMITTED", "COMMITTED"); + else if (Matches5("SET|BEGIN|START", "TRANSACTION|WORK", "ISOLATION", "LEVEL", "REPEATABLE") || + Matches4("BEGIN", "ISOLATION", "LEVEL", "REPEATABLE") || + Matches8("SET", "SESSION", "CHARACTERISTICS", "AS", "TRANSACTION", "ISOLATION", "LEVEL", "REPEATABLE")) + COMPLETE_WITH_CONST("READ"); + else if (Matches3("SET|BEGIN|START", "TRANSACTION|WORK", "READ") || + Matches2("BEGIN", "READ") || + Matches6("SET", "SESSION", "CHARACTERISTICS", "AS", "TRANSACTION", "READ")) + COMPLETE_WITH_LIST2("ONLY", "WRITE"); + /* SET CONSTRAINTS */ + else if (Matches2("SET", "CONSTRAINTS")) + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_constraints_with_schema, "UNION SELECT 'ALL'"); + /* Complete SET CONSTRAINTS with DEFERRED|IMMEDIATE */ + else if (Matches3("SET", "CONSTRAINTS", MatchAny)) + COMPLETE_WITH_LIST2("DEFERRED", "IMMEDIATE"); + /* Complete SET ROLE */ + else if (Matches2("SET", "ROLE")) + COMPLETE_WITH_QUERY(Query_for_list_of_roles); + /* Complete SET SESSION with AUTHORIZATION or CHARACTERISTICS... */ + else if (Matches2("SET", "SESSION")) + COMPLETE_WITH_LIST2("AUTHORIZATION", "CHARACTERISTICS AS TRANSACTION"); + /* Complete SET SESSION AUTHORIZATION with username */ + else if (Matches3("SET", "SESSION", "AUTHORIZATION")) + COMPLETE_WITH_QUERY(Query_for_list_of_roles " UNION SELECT 'DEFAULT'"); + /* Complete RESET SESSION with AUTHORIZATION */ + else if (Matches2("RESET", "SESSION")) + COMPLETE_WITH_CONST("AUTHORIZATION"); + /* RESET parameter / ALL */ + else if (Matches1("RESET")) + COMPLETE_WITH_QUERY(Query_for_list_of_show_vars); +/* Complete SET with "TO" */ + else if (Matches2("SET", MatchAny)) + COMPLETE_WITH_CONST("TO"); + /* Complete ALTER DATABASE|FUNCTION|ROLE|USER ... SET */ + else if (HeadMatches2("ALTER", "DATABASE|FUNCTION|ROLE|USER") && + TailMatches2("SET", MatchAny)) + COMPLETE_WITH_LIST2("FROM CURRENT", "TO"); + /* Suggest possible variable values */ + else if (TailMatches3("SET", MatchAny, "TO|=")) + { + /* special cased code for individual GUCs */ + if (TailMatches2("DateStyle", "TO|=")) + { + static const char *const my_list[] = + {"ISO", "SQL", "Postgres", "German", + "YMD", "DMY", "MDY", + "US", "European", "NonEuropean", + "DEFAULT", NULL}; + + COMPLETE_WITH_LIST(my_list); + } + else if (TailMatches2("search_path", "TO|=")) + COMPLETE_WITH_QUERY(Query_for_list_of_schemas + " AND nspname not like 'pg\\_toast%%' " + " AND nspname not like 'pg\\_temp%%' " + " UNION SELECT 'DEFAULT' "); + else + { + /* generic, type based, GUC support */ + char *guctype = get_guctype(prev2_wd); + + if (guctype && strcmp(guctype, "enum") == 0) + { + char querybuf[1024]; + + snprintf(querybuf, sizeof(querybuf), Query_for_enum, prev2_wd); + COMPLETE_WITH_QUERY(querybuf); + } + else if (guctype && strcmp(guctype, "bool") == 0) + COMPLETE_WITH_LIST9("on", "off", "true", "false", "yes", "no", + "1", "0", "DEFAULT"); + else + COMPLETE_WITH_CONST("DEFAULT"); + + if (guctype) + free(guctype); + } + } + +/* START TRANSACTION */ + else if (Matches1("START")) + COMPLETE_WITH_CONST("TRANSACTION"); + +/* TABLE, but not TABLE embedded in other commands */ + else if (Matches1("TABLE")) + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_relations, NULL); + +/* TABLESAMPLE */ + else if (TailMatches1("TABLESAMPLE")) + COMPLETE_WITH_QUERY(Query_for_list_of_tablesample_methods); + else if (TailMatches2("TABLESAMPLE", MatchAny)) + COMPLETE_WITH_CONST("("); + +/* TRUNCATE */ + else if (Matches1("TRUNCATE")) + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tables, " UNION SELECT 'TABLE'"); + else if (Matches2("TRUNCATE", "TABLE")) + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tables, NULL); + +/* UNLISTEN */ + else if (Matches1("UNLISTEN")) + COMPLETE_WITH_QUERY("SELECT pg_catalog.quote_ident(channel) FROM pg_catalog.pg_listening_channels() AS channel WHERE substring(pg_catalog.quote_ident(channel),1,%d)='%s' UNION SELECT '*'"); + +/* UNLISTEN */ + else if (Matches1("UNLOAD")) + COMPLETE_WITH_CONST("("); + else if (Matches2("UNLOAD", MatchAny)) + COMPLETE_WITH_CONST("TO"); + else if (HeadMatches1("UNLOAD") && TailMatches2("TO", MatchAnyExcept("TO"))) + COMPLETE_WITH_LIST6("IAM_ROLE", "ACCESS_KEY_ID", "SECRET_ACCESS_KEY", "SESSION_TOKEN", "WITH", "CREDENTIALS"); + else if (HeadMatches1("UNLOAD") && TailMatches2("ACCESS_KEY_ID", MatchAny)) + COMPLETE_WITH_CONST("SECRET_ACCESS_KEY"); + else if (HeadMatches1("UNLOAD") && TailMatches2("SECRET_ACCESS_KEY", MatchAny)) + COMPLETE_WITH_CONST("SESSION_TOKEN"); + else if (HeadMatches1("UNLOAD") && TailMatches3("TO", MatchAny, "WITH")) + COMPLETE_WITH_CONST("CREDENTIALS"); + else if (HeadMatches1("UNLOAD") && TailMatches1("CREDENTIALS")) + COMPLETE_WITH_CONST("AS"); + else if (HeadMatches1("UNLOAD") && ( + (TailMatches2("IAM_ROLE", MatchAny)) || + (TailMatches2("SECRET_ACCESS_KEY", MatchAny)) || + (TailMatches2("SESSION_TOKEN", MatchAny)) || + (TailMatches2("CREDENTIALS|AS", MatchAny)) + )) + COMPLETE_WITH_LIST12("MANIFEST", "DELIMITER", "FIXEDWIDTH", "ENCRYPTED", "BZIP2", "GZIP", "ADDQUOTES", "NULL", "ESCAPE", "ALLOWOVERWRITE", "PARALLEL", "MAXFILESIZE"); + else if (HeadMatches1("UNLOAD") && TailMatches1("MANIFEST")) + COMPLETE_WITH_LIST11("DELIMITER", "FIXEDWIDTH", "ENCRYPTED", "BZIP2", "GZIP", "ADDQUOTES", "NULL", "ESCAPE", "ALLOWOVERWRITE", "PARALLEL", "MAXFILESIZE"); + else if (HeadMatches1("UNLOAD") && TailMatches1("ENCRYPTED")) + COMPLETE_WITH_LIST11("MANIFEST", "DELIMITER", "FIXEDWIDTH", "BZIP2", "GZIP", "ADDQUOTES", "NULL", "ESCAPE", "ALLOWOVERWRITE", "PARALLEL", "MAXFILESIZE"); + else if (HeadMatches1("UNLOAD") && TailMatches1("BZIP2")) + COMPLETE_WITH_LIST11("MANIFEST", "DELIMITER", "FIXEDWIDTH", "ENCRYPTED", "GZIP", "ADDQUOTES", "NULL", "ESCAPE", "ALLOWOVERWRITE", "PARALLEL", "MAXFILESIZE"); + else if (HeadMatches1("UNLOAD") && TailMatches1("GZIP")) + COMPLETE_WITH_LIST11("MANIFEST", "DELIMITER", "FIXEDWIDTH", "ENCRYPTED", "BZIP2", "ADDQUOTES", "NULL", "ESCAPE", "ALLOWOVERWRITE", "PARALLEL", "MAXFILESIZE"); + else if (HeadMatches1("UNLOAD") && TailMatches1("ADDQUOTES")) + COMPLETE_WITH_LIST11("MANIFEST", "DELIMITER", "FIXEDWIDTH", "ENCRYPTED", "BZIP2", "GZIP", "NULL", "ESCAPE", "ALLOWOVERWRITE", "PARALLEL", "MAXFILESIZE"); + else if (HeadMatches1("UNLOAD") && TailMatches1("ESCAPE")) + COMPLETE_WITH_LIST11("MANIFEST", "DELIMITER", "FIXEDWIDTH", "ENCRYPTED", "BZIP2", "GZIP", "ADDQUOTES", "NULL", "ALLOWOVERWRITE", "PARALLEL", "MAXFILESIZE"); + else if (HeadMatches1("UNLOAD") && TailMatches1("ALLOWOVERWRITE")) + COMPLETE_WITH_LIST11("MANIFEST", "DELIMITER", "FIXEDWIDTH", "ENCRYPTED", "BZIP2", "GZIP", "ADDQUOTES", "NULL", "ESCAPE", "ALLOWOVERWRITE", "PARALLEL"); + else if (HeadMatches1("UNLOAD") && TailMatches1("DELIMITER|FIXEDWIDTH|NULL")) + COMPLETE_WITH_CONST("AS '"); + else if (HeadMatches1("UNLOAD") && TailMatches1("MAXFILESIZE")) + COMPLETE_WITH_CONST("AS"); + else if (HeadMatches1("UNLOAD") && TailMatches1("PARALLEL")) + COMPLETE_WITH_LIST4("ON", "OFF", "TRUE", "FALSE"); + else if (HeadMatches1("UNLOAD") && ( + TailMatches2("MAXFILESIZE", MatchAnyExcept("AS")) || + TailMatches3("MAXFILESIZE", "AS", MatchAny) + )) + COMPLETE_WITH_LIST2("MB", "GB"); + else if (HeadMatches1("UNLOAD") && ( + TailMatches2("DELIMITER", MatchAny) || + TailMatches3("DELIMITER", "AS", MatchAny) + )) + COMPLETE_WITH_LIST11("MANIFEST", "FIXEDWIDTH", "ENCRYPTED", "BZIP2", "GZIP", "ADDQUOTES", "NULL", "ESCAPE", "ALLOWOVERWRITE", "PARALLEL", "MAXFILESIZE"); + else if (HeadMatches1("UNLOAD") && ( + TailMatches2("FIXEDWIDTH", MatchAny) || + TailMatches3("FIXEDWIDTH", "AS", MatchAny) + )) + COMPLETE_WITH_LIST11("MANIFEST", "DELIMITER", "ENCRYPTED", "BZIP2", "GZIP", "ADDQUOTES", "NULL", "ESCAPE", "ALLOWOVERWRITE", "PARALLEL", "MAXFILESIZE"); + else if (HeadMatches1("UNLOAD") && ( + TailMatches2("NULL", MatchAny) || + TailMatches3("NULL", "AS", MatchAny) + )) + COMPLETE_WITH_LIST11("MANIFEST", "DELIMITER", "FIXEDWIDTH", "ENCRYPTED", "BZIP2", "GZIP", "ADDQUOTES", "ESCAPE", "ALLOWOVERWRITE", "PARALLEL", "MAXFILESIZE"); + else if (HeadMatches1("UNLOAD") && ( + TailMatches2("PARALLEL", MatchAny) || + TailMatches3("PARALLEL", "AS", MatchAny) + )) + COMPLETE_WITH_LIST11("MANIFEST", "DELIMITER", "FIXEDWIDTH", "ENCRYPTED", "BZIP2", "GZIP", "ADDQUOTES", "NULL", "ESCAPE", "ALLOWOVERWRITE", "MAXFILESIZE"); + else if (HeadMatches1("UNLOAD") && ( + TailMatches3("MAXFILESIZE", MatchAnyExcept("AS"), "MB|GB") || + TailMatches4("MAXFILESIZE", "AS", MatchAnyExcept("AS"), "MB|GB") + )) + COMPLETE_WITH_LIST12("MANIFEST", "DELIMITER", "FIXEDWIDTH", "ENCRYPTED", "BZIP2", "GZIP", "ADDQUOTES", "NULL", "ESCAPE", "ALLOWOVERWRITE", "PARALLEL", "MAXFILESIZE"); + + +/* UPDATE --- can be inside EXPLAIN, RULE, etc */ + /* If prev. word is UPDATE suggest a list of tables */ + else if (TailMatches1("UPDATE")) + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_updatables, NULL); + /* Complete UPDATE
with "SET" */ + else if (TailMatches2("UPDATE", MatchAny)) + COMPLETE_WITH_CONST("SET"); + /* Complete UPDATE
SET with list of attributes */ + else if (TailMatches3("UPDATE", MatchAny, "SET")) + COMPLETE_WITH_ATTR(prev2_wd, ""); + /* UPDATE
SET = */ + else if (TailMatches4("UPDATE", MatchAny, "SET", MatchAny)) + COMPLETE_WITH_CONST("="); + /* UPDATE
SET = FROM / WHERE */ + else if (TailMatches6("UPDATE", MatchAny, "SET", MatchAny, "=", MatchAny)) + COMPLETE_WITH_LIST2("FROM", "WHERE"); + /* UPDATE
SET = FROM */ + else if (TailMatches7("UPDATE", MatchAny, "SET", MatchAny, "=", MatchAny, "FROM")) + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tables, NULL); + /* UPDATE
SET = FROM WHERE */ + else if (TailMatches8("UPDATE", MatchAny, "SET", MatchAny, "=", MatchAny, "FROM", MatchAny)) + COMPLETE_WITH_CONST("WHERE"); + /* UPDATE
SET = FROM WHERE */ + else if (TailMatches9("UPDATE", MatchAny, "SET", MatchAny, "=", MatchAny, "FROM", MatchAny, "WHERE")) + COMPLETE_WITH_ATTR(prev2_wd, ""); + +/* USER MAPPING */ + else if (Matches3("ALTER|CREATE|DROP", "USER", "MAPPING")) + COMPLETE_WITH_CONST("FOR"); + else if (Matches4("CREATE", "USER", "MAPPING", "FOR")) + COMPLETE_WITH_QUERY(Query_for_list_of_roles + " UNION SELECT 'CURRENT_USER'" + " UNION SELECT 'PUBLIC'" + " UNION SELECT 'USER'"); + else if (Matches4("ALTER|DROP", "USER", "MAPPING", "FOR")) + COMPLETE_WITH_QUERY(Query_for_list_of_user_mappings); + else if (Matches5("CREATE|ALTER|DROP", "USER", "MAPPING", "FOR", MatchAny)) + COMPLETE_WITH_CONST("SERVER"); + else if (Matches7("CREATE|ALTER", "USER", "MAPPING", "FOR", MatchAny, "SERVER", MatchAny)) + COMPLETE_WITH_CONST("OPTIONS"); + +/* + * VACUUM [ FULL | FREEZE ] [ VERBOSE ] [ table ] + * VACUUM [ FULL | FREEZE ] [ VERBOSE ] ANALYZE [ table [ (column [, ...] ) ] ] + */ + else if (Matches1("VACUUM")) + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tm, + " UNION SELECT 'FULL'" + " UNION SELECT 'SORT ONLY'" + " UNION SELECT 'DELETE ONLY'" + " UNION SELECT 'REINDEX'" + " UNION SELECT 'FREEZE'" + " UNION SELECT 'ANALYZE'" + " UNION SELECT 'VERBOSE'"); + else if (Matches2("VACUUM", "FULL|FREEZE")) + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tm, + " UNION SELECT 'ANALYZE'" + " UNION SELECT 'VERBOSE'"); + else if (Matches3("VACUUM", "FULL|FREEZE", "ANALYZE")) + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tm, + " UNION SELECT 'VERBOSE'"); + else if (Matches3("VACUUM", "FULL|FREEZE", "VERBOSE")) + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tm, + " UNION SELECT 'ANALYZE'"); + else if (Matches2("VACUUM", "VERBOSE")) + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tm, + " UNION SELECT 'ANALYZE'"); + else if (Matches2("VACUUM", "ANALYZE")) + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tm, + " UNION SELECT 'VERBOSE'"); + else if (Matches3("VACUUM", "FULL|REINDEX", MatchAny) || + Matches4("VACUUM", "SORT|DELETE", "ONLY", MatchAny)) + COMPLETE_WITH_CONST("TO"); + else if (Matches5("VACUUM", "FULL|REINDEX", MatchAny, "TO", MatchAny) || + Matches6("VACUUM", "SORT|DELETE", "ONLY", MatchAny, "TO", MatchAny)) + COMPLETE_WITH_CONST("PERCENT"); + +/* WITH [RECURSIVE] */ + + /* + * Only match when WITH is the first word, as WITH may appear in many + * other contexts. + */ + else if (Matches1("WITH")) + COMPLETE_WITH_CONST("RECURSIVE"); + +/* ANALYZE */ + /* Complete with list of tables */ + else if (Matches1("ANALYZE|ANALYSE")) + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tmf, + " UNION SELECT 'VERBOSE'" + " UNION SELECT 'PREDICATE COLUMNS'" + " UNION SELECT 'ALL COLUMNS'" + " UNION SELECT 'COMPRESSION'" + ); + +/* WHERE */ + /* Simple case of the word before the where being the table name */ + else if (TailMatches2(MatchAny, "WHERE")) + COMPLETE_WITH_ATTR(prev2_wd, ""); + +/* ... FROM ... */ +/* TODO: also include SRF ? */ + else if (TailMatches1("FROM") && !Matches3("COPY|\\copy", MatchAny, "FROM")) + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tsvmf, NULL); + +/* ... JOIN ... */ + else if (TailMatches1("JOIN")) + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tsvmf, NULL); + +/* Backslash commands */ +/* TODO: \dc \dd \dl */ + else if (TailMatchesCS1("\\?")) + COMPLETE_WITH_LIST_CS3("commands", "options", "variables"); + else if (TailMatchesCS1("\\connect|\\c")) + { + if (!recognized_connection_string(text)) + COMPLETE_WITH_QUERY(Query_for_list_of_databases); + } + else if (TailMatchesCS2("\\connect|\\c", MatchAny)) + { + if (!recognized_connection_string(prev_wd)) + COMPLETE_WITH_QUERY(Query_for_list_of_roles); + } + else if (TailMatchesCS1("\\da*")) + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_aggregates, NULL); + else if (TailMatchesCS1("\\dA*")) + COMPLETE_WITH_QUERY(Query_for_list_of_access_methods); + else if (TailMatchesCS1("\\db*")) + COMPLETE_WITH_QUERY(Query_for_list_of_tablespaces); + else if (TailMatchesCS1("\\dD*")) + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_domains, NULL); + else if (TailMatchesCS1("\\des*")) + COMPLETE_WITH_QUERY(Query_for_list_of_servers); + else if (TailMatchesCS1("\\deu*")) + COMPLETE_WITH_QUERY(Query_for_list_of_user_mappings); + else if (TailMatchesCS1("\\dew*")) + COMPLETE_WITH_QUERY(Query_for_list_of_fdws); + else if (TailMatchesCS1("\\df*")) + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_functions, NULL); + + else if (TailMatchesCS1("\\dFd*")) + COMPLETE_WITH_QUERY(Query_for_list_of_ts_dictionaries); + else if (TailMatchesCS1("\\dFp*")) + COMPLETE_WITH_QUERY(Query_for_list_of_ts_parsers); + else if (TailMatchesCS1("\\dFt*")) + COMPLETE_WITH_QUERY(Query_for_list_of_ts_templates); + /* must be at end of \dF alternatives: */ + else if (TailMatchesCS1("\\dF*")) + COMPLETE_WITH_QUERY(Query_for_list_of_ts_configurations); + + else if (TailMatchesCS1("\\di*")) + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_indexes, NULL); + else if (TailMatchesCS1("\\dL*")) + COMPLETE_WITH_QUERY(Query_for_list_of_languages); + else if (TailMatchesCS1("\\dn*")) + COMPLETE_WITH_QUERY(Query_for_list_of_schemas); + else if (TailMatchesCS1("\\dp") || TailMatchesCS1("\\z")) + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tsvmf, NULL); + else if (TailMatchesCS1("\\ds*")) + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_sequences, NULL); + else if (TailMatchesCS1("\\dt*")) + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tables, NULL); + else if (TailMatchesCS1("\\dT*")) + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_datatypes, NULL); + else if (TailMatchesCS1("\\du*") || TailMatchesCS1("\\dg*")) + COMPLETE_WITH_QUERY(Query_for_list_of_roles); + else if (TailMatchesCS1("\\dv*")) + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_views, NULL); + else if (TailMatchesCS1("\\dx*")) + COMPLETE_WITH_QUERY(Query_for_list_of_extensions); + else if (TailMatchesCS1("\\dm*")) + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_matviews, NULL); + else if (TailMatchesCS1("\\dE*")) + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_foreign_tables, NULL); + else if (TailMatchesCS1("\\dy*")) + COMPLETE_WITH_QUERY(Query_for_list_of_event_triggers); + + /* must be at end of \d alternatives: */ + else if (TailMatchesCS1("\\d*")) + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_relations, NULL); + + else if (TailMatchesCS1("\\ef")) + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_functions, NULL); + else if (TailMatchesCS1("\\ev")) + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_views, NULL); + + else if (TailMatchesCS1("\\encoding")) + COMPLETE_WITH_QUERY(Query_for_list_of_encodings); + else if (TailMatchesCS1("\\h|\\help")) + COMPLETE_WITH_LIST(sql_commands); + else if (TailMatchesCS2("\\h|\\help", MatchAny)) + { + if (TailMatches1("DROP")) + matches = completion_matches(text, drop_command_generator); + else if (TailMatches1("ALTER")) + matches = completion_matches(text, alter_command_generator); + + /* + * CREATE is recognized by tail match elsewhere, so doesn't need to be + * repeated here + */ + } + else if (TailMatchesCS3("\\h|\\help", MatchAny, MatchAny)) + { + if (TailMatches2("CREATE|DROP", "ACCESS")) + COMPLETE_WITH_CONST("METHOD"); + else if (TailMatches2("ALTER", "DEFAULT")) + COMPLETE_WITH_CONST("PRIVILEGES"); + else if (TailMatches2("CREATE|ALTER|DROP", "EVENT")) + COMPLETE_WITH_CONST("TRIGGER"); + else if (TailMatches2("CREATE|ALTER|DROP", "FOREIGN")) + COMPLETE_WITH_LIST2("DATA WRAPPER", "TABLE"); + else if (TailMatches2("ALTER", "LARGE")) + COMPLETE_WITH_CONST("OBJECT"); + else if (TailMatches2("CREATE|ALTER|DROP", "MATERIALIZED")) + COMPLETE_WITH_CONST("VIEW"); + else if (TailMatches2("CREATE|ALTER|DROP", "TEXT")) + COMPLETE_WITH_CONST("SEARCH"); + else if (TailMatches2("CREATE|ALTER|DROP", "USER")) + COMPLETE_WITH_CONST("MAPPING FOR"); + } + else if (TailMatchesCS4("\\h|\\help", MatchAny, MatchAny, MatchAny)) + { + if (TailMatches3("CREATE|ALTER|DROP", "FOREIGN", "DATA")) + COMPLETE_WITH_CONST("WRAPPER"); + else if (TailMatches3("CREATE|ALTER|DROP", "TEXT", "SEARCH")) + COMPLETE_WITH_LIST4("CONFIGURATION", "DICTIONARY", "PARSER", "TEMPLATE"); + else if (TailMatches3("CREATE|ALTER|DROP", "USER", "MAPPING")) + COMPLETE_WITH_CONST("FOR"); + } + else if (TailMatchesCS1("\\l*") && !TailMatchesCS1("\\lo*")) + COMPLETE_WITH_QUERY(Query_for_list_of_databases); + else if (TailMatchesCS1("\\password")) + COMPLETE_WITH_QUERY(Query_for_list_of_roles); + else if (TailMatchesCS1("\\pset")) + { + static const char *const my_list[] = + {"border", "columns", "expanded", "fieldsep", "fieldsep_zero", + "footer", "format", "linestyle", "null", "numericlocale", + "pager", "pager_min_lines", "recordsep", "recordsep_zero", + "tableattr", "title", "tuples_only", "unicode_border_linestyle", + "unicode_column_linestyle", "unicode_header_linestyle", NULL}; + + COMPLETE_WITH_LIST_CS(my_list); + } + else if (TailMatchesCS2("\\pset", MatchAny)) + { + if (TailMatchesCS1("format")) + { + static const char *const my_list[] = + {"unaligned", "aligned", "wrapped", "html", "asciidoc", + "latex", "latex-longtable", "troff-ms", NULL}; + + COMPLETE_WITH_LIST_CS(my_list); + } + else if (TailMatchesCS1("linestyle")) + COMPLETE_WITH_LIST_CS3("ascii", "old-ascii", "unicode"); + else if (TailMatchesCS1("pager")) + COMPLETE_WITH_LIST_CS3("on", "off", "always"); + else if (TailMatchesCS1("unicode_border_linestyle|" + "unicode_column_linestyle|" + "unicode_header_linestyle")) + COMPLETE_WITH_LIST_CS2("single", "double"); + } + else if (TailMatchesCS1("\\unset")) + matches = complete_from_variables(text, "", "", true); + else if (TailMatchesCS1("\\set")) + matches = complete_from_variables(text, "", "", false); + else if (TailMatchesCS2("\\set", MatchAny)) + { + if (TailMatchesCS1("AUTOCOMMIT|ON_ERROR_STOP|QUIET|" + "SINGLELINE|SINGLESTEP")) + COMPLETE_WITH_LIST_CS2("on", "off"); + else if (TailMatchesCS1("COMP_KEYWORD_CASE")) + COMPLETE_WITH_LIST_CS4("lower", "upper", + "preserve-lower", "preserve-upper"); + else if (TailMatchesCS1("ECHO")) + COMPLETE_WITH_LIST_CS4("errors", "queries", "all", "none"); + else if (TailMatchesCS1("ECHO_HIDDEN")) + COMPLETE_WITH_LIST_CS3("noexec", "off", "on"); + else if (TailMatchesCS1("HISTCONTROL")) + COMPLETE_WITH_LIST_CS4("ignorespace", "ignoredups", + "ignoreboth", "none"); + else if (TailMatchesCS1("ON_ERROR_ROLLBACK")) + COMPLETE_WITH_LIST_CS3("on", "off", "interactive"); + else if (TailMatchesCS1("SHOW_CONTEXT")) + COMPLETE_WITH_LIST_CS3("never", "errors", "always"); + else if (TailMatchesCS1("VERBOSITY")) + COMPLETE_WITH_LIST_CS3("default", "verbose", "terse"); + } + else if (TailMatchesCS1("\\sf*")) + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_functions, NULL); + else if (TailMatchesCS1("\\sv*")) + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_views, NULL); + else if (TailMatchesCS1("\\cd|\\e|\\edit|\\g|\\i|\\include|" + "\\ir|\\include_relative|\\o|\\out|" + "\\s|\\w|\\write|\\lo_import")) + { + completion_charp = "\\"; + matches = completion_matches(text, complete_from_files); + } + + /* + * Finally, we look through the list of "things", such as TABLE, INDEX and + * check if that was the previous word. If so, execute the query to get a + * list of them. + */ + else + { + int i; + + for (i = 0; words_after_create[i].name; i++) + { + if (pg_strcasecmp(prev_wd, words_after_create[i].name) == 0) + { + if (words_after_create[i].query) + COMPLETE_WITH_QUERY(words_after_create[i].query); + else if (words_after_create[i].squery) + COMPLETE_WITH_SCHEMA_QUERY(*words_after_create[i].squery, + NULL); + break; + } + } + } + + /* + * If we still don't have anything to match we have to fabricate some sort + * of default list. If we were to just return NULL, readline automatically + * attempts filename completion, and that's usually no good. + */ + if (matches == NULL) + { + COMPLETE_WITH_CONST(""); +#ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER + rl_completion_append_character = '\0'; +#endif + } + +/* Macro to easily identify which Engine (type) are we speaking to + Any change here should probably be replicated elsewhere since + #define for various SQLs (in this script) employ there own string-compare */ + #define IS_COCKROACHDB (strncmp(pset.sengine, "cockroachdb", 10) == 0) + +/* free storage */ + free(previous_words); + free(words_buffer); + + /* Return our Grand List O' Matches */ + return matches; +} + + +/* + * GENERATOR FUNCTIONS + * + * These functions do all the actual work of completing the input. They get + * passed the text so far and the count how many times they have been called + * so far with the same text. + * If you read the above carefully, you'll see that these don't get called + * directly but through the readline interface. + * The return value is expected to be the full completion of the text, going + * through a list each time, or NULL if there are no more matches. The string + * will be free()'d by readline, so you must run it through strdup() or + * something of that sort. + */ + +/* + * Common routine for create_command_generator and drop_command_generator. + * Entries that have 'excluded' flags are not returned. + */ +static char * +create_or_drop_command_generator(const char *text, int state, bits32 excluded) +{ + static int list_index, + string_length; + const char *name; + + /* If this is the first time for this completion, init some values */ + if (state == 0) + { + list_index = 0; + string_length = strlen(text); + } + + /* find something that matches */ + while ((name = words_after_create[list_index++].name)) + { + if ((pg_strncasecmp(name, text, string_length) == 0) && + !(words_after_create[list_index - 1].flags & excluded)) + return pg_strdup_keyword_case(name, text); + } + /* if nothing matches, return NULL */ + return NULL; +} + +/* + * This one gives you one from a list of things you can put after CREATE + * as defined above. + */ +static char * +create_command_generator(const char *text, int state) +{ + return create_or_drop_command_generator(text, state, THING_NO_CREATE); +} + +/* + * This function gives you a list of things you can put after a DROP command. + */ +static char * +drop_command_generator(const char *text, int state) +{ + return create_or_drop_command_generator(text, state, THING_NO_DROP); +} + +/* + * This function gives you a list of things you can put after an ALTER command. + */ +static char * +alter_command_generator(const char *text, int state) +{ + return create_or_drop_command_generator(text, state, THING_NO_ALTER); +} + +/* The following two functions are wrappers for _complete_from_query */ + +static char * +complete_from_query(const char *text, int state) +{ + return _complete_from_query(0, text, state); +} + +static char * +complete_from_schema_query(const char *text, int state) +{ + return _complete_from_query(1, text, state); +} + + +/* + * This creates a list of matching things, according to a query pointed to + * by completion_charp. + * The query can be one of two kinds: + * + * 1. A simple query which must contain a %d and a %s, which will be replaced + * by the string length of the text and the text itself. The query may also + * have up to four more %s in it; the first two such will be replaced by the + * value of completion_info_charp, the next two by the value of + * completion_info_charp2. + * + * 2. A schema query used for completion of both schema and relation names. + * These are more complex and must contain in the following order: + * %d %s %d %s %d %s %s %d %s + * where %d is the string length of the text and %s the text itself. + * + * It is assumed that strings should be escaped to become SQL literals + * (that is, what is in the query is actually ... '%s' ...) + * + * See top of file for examples of both kinds of query. + */ +static char * +_complete_from_query(int is_schema_query, const char *text, int state) +{ + static int list_index, + byte_length; + static PGresult *result = NULL; + + /* + * If this is the first time for this completion, we fetch a list of our + * "things" from the backend. + */ + if (state == 0) + { + PQExpBufferData query_buffer; + char *e_text; + char *e_info_charp; + char *e_info_charp2; + const char *pstr = text; + int char_length = 0; + + list_index = 0; + byte_length = strlen(text); + + /* + * Count length as number of characters (not bytes), for passing to + * substring + */ + while (*pstr) + { + char_length++; + pstr += PQmblen(pstr, pset.encoding); + } + + /* Free any prior result */ + PQclear(result); + result = NULL; + + /* Set up suitably-escaped copies of textual inputs */ + e_text = escape_string(text); + + if (completion_info_charp) + e_info_charp = escape_string(completion_info_charp); + else + e_info_charp = NULL; + + if (completion_info_charp2) + e_info_charp2 = escape_string(completion_info_charp2); + else + e_info_charp2 = NULL; + + initPQExpBuffer(&query_buffer); + + if (is_schema_query) + { + /* completion_squery gives us the pieces to assemble */ + const char *qualresult = completion_squery->qualresult; + + if (qualresult == NULL) + qualresult = completion_squery->result; + + /* Get unqualified names matching the input-so-far */ + appendPQExpBuffer(&query_buffer, "SELECT %s FROM %s WHERE ", + completion_squery->result, + completion_squery->catname); + if (completion_squery->selcondition) + appendPQExpBuffer(&query_buffer, "%s AND ", + completion_squery->selcondition); + appendPQExpBuffer(&query_buffer, "substring(%s,1,%d)='%s'", + completion_squery->result, + char_length, e_text); + appendPQExpBuffer(&query_buffer, " AND %s", + completion_squery->viscondition); + + /* + * When fetching relation names, suppress system catalogs unless + * the input-so-far begins with "pg_". This is a compromise + * between not offering system catalogs for completion at all, and + * having them swamp the result when the input is just "p". + */ + if (strcmp(completion_squery->catname, + "pg_catalog.pg_class c") == 0 && + strncmp(text, "pg_", 3) !=0) + { + appendPQExpBufferStr(&query_buffer, + " AND c.relnamespace <> (SELECT oid FROM" + " pg_catalog.pg_namespace WHERE nspname = 'pg_catalog')"); + } + + /* + * Add in matching schema names, but only if there is more than + * one potential match among schema names. + */ + appendPQExpBuffer(&query_buffer, "\nUNION\n" + "SELECT pg_catalog.quote_ident(n.nspname) || '.' " + "FROM pg_catalog.pg_namespace n " + "WHERE substring(pg_catalog.quote_ident(n.nspname) || '.',1,%d)='%s'", + char_length, e_text); + appendPQExpBuffer(&query_buffer, + " AND (SELECT pg_catalog.count(*)" + " FROM pg_catalog.pg_namespace" + " WHERE substring(pg_catalog.quote_ident(nspname) || '.',1,%d) =" + " substring('%s',1,pg_catalog.length(pg_catalog.quote_ident(nspname))+1)) > 1", + char_length, e_text); + + /* + * Add in matching qualified names, but only if there is exactly + * one schema matching the input-so-far. + */ + appendPQExpBuffer(&query_buffer, "\nUNION\n" + "SELECT pg_catalog.quote_ident(n.nspname) || '.' || %s " + "FROM %s, pg_catalog.pg_namespace n " + "WHERE %s = n.oid AND ", + qualresult, + completion_squery->catname, + completion_squery->namespace); + if (completion_squery->selcondition) + appendPQExpBuffer(&query_buffer, "%s AND ", + completion_squery->selcondition); + appendPQExpBuffer(&query_buffer, "substring(pg_catalog.quote_ident(n.nspname) || '.' || %s,1,%d)='%s'", + qualresult, + char_length, e_text); + + /* + * This condition exploits the single-matching-schema rule to + * speed up the query + */ + appendPQExpBuffer(&query_buffer, + " AND substring(pg_catalog.quote_ident(n.nspname) || '.',1,%d) =" + " substring('%s',1,pg_catalog.length(pg_catalog.quote_ident(n.nspname))+1)", + char_length, e_text); + appendPQExpBuffer(&query_buffer, + " AND (SELECT pg_catalog.count(*)" + " FROM pg_catalog.pg_namespace" + " WHERE substring(pg_catalog.quote_ident(nspname) || '.',1,%d) =" + " substring('%s',1,pg_catalog.length(pg_catalog.quote_ident(nspname))+1)) = 1", + char_length, e_text); + + /* If an addon query was provided, use it */ + if (completion_charp) + appendPQExpBuffer(&query_buffer, "\n%s", completion_charp); + } + else + { + /* completion_charp is an sprintf-style format string */ + appendPQExpBuffer(&query_buffer, completion_charp, + char_length, e_text, + e_info_charp, e_info_charp, + e_info_charp2, e_info_charp2); + } + + /* Limit the number of records in the result */ + appendPQExpBuffer(&query_buffer, "\nLIMIT %d", + completion_max_records); + + result = exec_query(query_buffer.data); + + termPQExpBuffer(&query_buffer); + free(e_text); + if (e_info_charp) + free(e_info_charp); + if (e_info_charp2) + free(e_info_charp2); + } + + /* Find something that matches */ + if (result && PQresultStatus(result) == PGRES_TUPLES_OK) + { + const char *item; + + while (list_index < PQntuples(result) && + (item = PQgetvalue(result, list_index++, 0))) + if (pg_strncasecmp(text, item, byte_length) == 0) + return pg_strdup(item); + } + + /* If nothing matches, free the db structure and return null */ + PQclear(result); + result = NULL; + return NULL; +} + + +/* + * This function returns in order one of a fixed, NULL pointer terminated list + * of strings (if matching). This can be used if there are only a fixed number + * SQL words that can appear at certain spot. + */ +static char * +complete_from_list(const char *text, int state) +{ + static int string_length, + list_index, + matches; + static bool casesensitive; + const char *item; + + /* need to have a list */ + Assert(completion_charpp != NULL); + + /* Initialization */ + if (state == 0) + { + list_index = 0; + string_length = strlen(text); + casesensitive = completion_case_sensitive; + matches = 0; + } + + while ((item = completion_charpp[list_index++])) + { + /* First pass is case sensitive */ + if (casesensitive && strncmp(text, item, string_length) == 0) + { + matches++; + return pg_strdup(item); + } + + /* Second pass is case insensitive, don't bother counting matches */ + if (!casesensitive && pg_strncasecmp(text, item, string_length) == 0) + { + if (completion_case_sensitive) + return pg_strdup(item); + else + + /* + * If case insensitive matching was requested initially, + * adjust the case according to setting. + */ + return pg_strdup_keyword_case(item, text); + } + } + + /* + * No matches found. If we're not case insensitive already, lets switch to + * being case insensitive and try again + */ + if (casesensitive && matches == 0) + { + casesensitive = false; + list_index = 0; + state++; + return complete_from_list(text, state); + } + + /* If no more matches, return null. */ + return NULL; +} + + +/* + * This function returns one fixed string the first time even if it doesn't + * match what's there, and nothing the second time. This should be used if + * there is only one possibility that can appear at a certain spot, so + * misspellings will be overwritten. The string to be passed must be in + * completion_charp. + */ +static char * +complete_from_const(const char *text, int state) +{ + Assert(completion_charp != NULL); + if (state == 0) + { + if (completion_case_sensitive) + return pg_strdup(completion_charp); + else + + /* + * If case insensitive matching was requested initially, adjust + * the case according to setting. + */ + return pg_strdup_keyword_case(completion_charp, text); + } + else + return NULL; +} + + +/* + * This function appends the variable name with prefix and suffix to + * the variable names array. + */ +static void +append_variable_names(char ***varnames, int *nvars, + int *maxvars, const char *varname, + const char *prefix, const char *suffix) +{ + if (*nvars >= *maxvars) + { + *maxvars *= 2; + *varnames = (char **) pg_realloc(*varnames, + ((*maxvars) + 1) * sizeof(char *)); + } + + (*varnames)[(*nvars)++] = psprintf("%s%s%s", prefix, varname, suffix); +} + + +/* + * This function supports completion with the name of a psql variable. + * The variable names can be prefixed and suffixed with additional text + * to support quoting usages. If need_value is true, only variables + * that are currently set are included; otherwise, special variables + * (those that have hooks) are included even if currently unset. + */ +static char ** +complete_from_variables(const char *text, const char *prefix, const char *suffix, + bool need_value) +{ + char **matches; + char **varnames; + int nvars = 0; + int maxvars = 100; + int i; + struct _variable *ptr; + + varnames = (char **) pg_malloc((maxvars + 1) * sizeof(char *)); + + for (ptr = pset.vars->next; ptr; ptr = ptr->next) + { + if (need_value && !(ptr->value)) + continue; + append_variable_names(&varnames, &nvars, &maxvars, ptr->name, + prefix, suffix); + } + + varnames[nvars] = NULL; + COMPLETE_WITH_LIST_CS((const char *const *) varnames); + + for (i = 0; i < nvars; i++) + free(varnames[i]); + free(varnames); + + return matches; +} + + +/* + * This function wraps rl_filename_completion_function() to strip quotes from + * the input before searching for matches and to quote any matches for which + * the consuming command will require it. + */ +static char * +complete_from_files(const char *text, int state) +{ + static const char *unquoted_text; + char *unquoted_match; + char *ret = NULL; + + if (state == 0) + { + /* Initialization: stash the unquoted input. */ + unquoted_text = strtokx(text, "", NULL, "'", *completion_charp, + false, true, pset.encoding); + /* expect a NULL return for the empty string only */ + if (!unquoted_text) + { + Assert(*text == '\0'); + unquoted_text = text; + } + } + + unquoted_match = filename_completion_function(unquoted_text, state); + if (unquoted_match) + { + /* + * Caller sets completion_charp to a zero- or one-character string + * containing the escape character. This is necessary since \copy has + * no escape character, but every other backslash command recognizes + * "\" as an escape character. Since we have only two callers, don't + * bother providing a macro to simplify this. + */ + ret = quote_if_needed(unquoted_match, " \t\r\n\"`", + '\'', *completion_charp, pset.encoding); + if (ret) + free(unquoted_match); + else + ret = unquoted_match; + } + + return ret; +} + + +/* HELPER FUNCTIONS */ + + +/* + * Make a pg_strdup copy of s and convert the case according to + * COMP_KEYWORD_CASE setting, using ref as the text that was already entered. + */ +static char * +pg_strdup_keyword_case(const char *s, const char *ref) +{ + char *ret, + *p; + unsigned char first = ref[0]; + + ret = pg_strdup(s); + + if (pset.comp_case == PSQL_COMP_CASE_LOWER || + ((pset.comp_case == PSQL_COMP_CASE_PRESERVE_LOWER || + pset.comp_case == PSQL_COMP_CASE_PRESERVE_UPPER) && islower(first)) || + (pset.comp_case == PSQL_COMP_CASE_PRESERVE_LOWER && !isalpha(first))) + { + for (p = ret; *p; p++) + *p = pg_tolower((unsigned char) *p); + } + else + { + for (p = ret; *p; p++) + *p = pg_toupper((unsigned char) *p); + } + + return ret; +} + + +/* + * escape_string - Escape argument for use as string literal. + * + * The returned value has to be freed. + */ +static char * +escape_string(const char *text) +{ + size_t text_length; + char *result; + + text_length = strlen(text); + + result = pg_malloc(text_length * 2 + 1); + PQescapeStringConn(pset.db, result, text, text_length, NULL); + + return result; +} + + +/* + * Execute a query and report any errors. This should be the preferred way of + * talking to the database in this file. + */ +static PGresult * +exec_query(const char *query) +{ + PGresult *result; + + if (query == NULL || !pset.db || PQstatus(pset.db) != CONNECTION_OK) + return NULL; + + result = PQexec(pset.db, query); + + if (PQresultStatus(result) != PGRES_TUPLES_OK) + { +#ifdef NOT_USED + psql_error("tab completion query failed: %s\nQuery was:\n%s\n", + PQerrorMessage(pset.db), query); +#endif + PQclear(result); + result = NULL; + } + + return result; +} + + +/* + * Parse all the word(s) before point. + * + * Returns a malloc'd array of character pointers that point into the malloc'd + * data array returned to *buffer; caller must free() both of these when done. + * *nwords receives the number of words found, ie, the valid length of the + * return array. + * + * Words are returned right to left, that is, previous_words[0] gets the last + * word before point, previous_words[1] the next-to-last, etc. + */ +static char ** +get_previous_words(int point, char **buffer, int *nwords) +{ + char **previous_words; + char *buf; + char *outptr; + int words_found = 0; + int i; + + /* + * If we have anything in tab_completion_query_buf, paste it together with + * rl_line_buffer to construct the full query. Otherwise we can just use + * rl_line_buffer as the input string. + */ + if (tab_completion_query_buf && tab_completion_query_buf->len > 0) + { + i = tab_completion_query_buf->len; + buf = pg_malloc(point + i + 2); + memcpy(buf, tab_completion_query_buf->data, i); + buf[i++] = '\n'; + memcpy(buf + i, rl_line_buffer, point); + i += point; + buf[i] = '\0'; + /* Readjust point to reference appropriate offset in buf */ + point = i; + } + else + buf = rl_line_buffer; + + /* + * Allocate an array of string pointers and a buffer to hold the strings + * themselves. The worst case is that the line contains only + * non-whitespace WORD_BREAKS characters, making each one a separate word. + * This is usually much more space than we need, but it's cheaper than + * doing a separate malloc() for each word. + */ + previous_words = (char **) pg_malloc(point * sizeof(char *)); + *buffer = outptr = (char *) pg_malloc(point * 2); + + /* + * First we look for a non-word char before the current point. (This is + * probably useless, if readline is on the same page as we are about what + * is a word, but if so it's cheap.) + */ + for (i = point - 1; i >= 0; i--) + { + if (strchr(WORD_BREAKS, buf[i])) + break; + } + point = i; + + /* + * Now parse words, working backwards, until we hit start of line. The + * backwards scan has some interesting but intentional properties + * concerning parenthesis handling. + */ + while (point >= 0) + { + int start, + end; + bool inquotes = false; + int parentheses = 0; + + /* now find the first non-space which then constitutes the end */ + end = -1; + for (i = point; i >= 0; i--) + { + if (!isspace((unsigned char) buf[i])) + { + end = i; + break; + } + } + /* if no end found, we're done */ + if (end < 0) + break; + + /* + * Otherwise we now look for the start. The start is either the last + * character before any word-break character going backwards from the + * end, or it's simply character 0. We also handle open quotes and + * parentheses. + */ + for (start = end; start > 0; start--) + { + if (buf[start] == '"') + inquotes = !inquotes; + if (!inquotes) + { + if (buf[start] == ')') + parentheses++; + else if (buf[start] == '(') + { + if (--parentheses <= 0) + break; + } + else if (parentheses == 0 && + strchr(WORD_BREAKS, buf[start - 1])) + break; + } + } + + /* Return the word located at start to end inclusive */ + previous_words[words_found++] = outptr; + i = end - start + 1; + memcpy(outptr, &buf[start], i); + outptr += i; + *outptr++ = '\0'; + + /* Continue searching */ + point = start - 1; + } + + /* Release parsing input workspace, if we made one above */ + if (buf != rl_line_buffer) + free(buf); + + *nwords = words_found; + return previous_words; +} + +/* + * Look up the type for the GUC variable with the passed name. + * + * Returns NULL if the variable is unknown. Otherwise the returned string, + * containing the type, has to be freed. + */ +static char * +get_guctype(const char *varname) +{ + PQExpBufferData query_buffer; + char *e_varname; + PGresult *result; + char *guctype = NULL; + + e_varname = escape_string(varname); + + initPQExpBuffer(&query_buffer); + appendPQExpBuffer(&query_buffer, + "SELECT vartype FROM pg_catalog.pg_settings " + "WHERE pg_catalog.lower(name) = pg_catalog.lower('%s')", + e_varname); + + result = exec_query(query_buffer.data); + termPQExpBuffer(&query_buffer); + free(e_varname); + + if (PQresultStatus(result) == PGRES_TUPLES_OK && PQntuples(result) > 0) + guctype = pg_strdup(PQgetvalue(result, 0, 0)); + + PQclear(result); + + return guctype; +} + +#ifdef NOT_USED + +/* + * Surround a string with single quotes. This works for both SQL and + * psql internal. Currently disabled because it is reported not to + * cooperate with certain versions of readline. + */ +static char * +quote_file_name(char *text, int match_type, char *quote_pointer) +{ + char *s; + size_t length; + + (void) quote_pointer; /* not used */ + + length = strlen(text) +(match_type == SINGLE_MATCH ? 3 : 2); + s = pg_malloc(length); + s[0] = '\''; + strcpy(s + 1, text); + if (match_type == SINGLE_MATCH) + s[length - 2] = '\''; + s[length - 1] = '\0'; + return s; +} + +static char * +dequote_file_name(char *text, char quote_char) +{ + char *s; + size_t length; + + if (!quote_char) + return pg_strdup(text); + + length = strlen(text); + s = pg_malloc(length - 2 + 1); + strlcpy(s, text +1, length - 2 + 1); + + return s; +} +#endif /* NOT_USED */ + +#endif /* USE_READLINE */ diff --git a/src/bin/psql/tab-complete-cockroachdb.h b/src/bin/psql/tab-complete-cockroachdb.h new file mode 100644 index 0000000000000..5fdcc0f847b87 --- /dev/null +++ b/src/bin/psql/tab-complete-cockroachdb.h @@ -0,0 +1,17 @@ +/* + * psql - the PostgreSQL interactive terminal + * + * Copyright (c) 2000-2017, PostgreSQL Global Development Group + * + * src/bin/psql/tab-complete.h + */ +#ifndef TAB_COMPLETE_COCKROACHDB_H +#define TAB_COMPLETE_COCKROACHDB_H + +#include "pqexpbuffer.h" + +extern PQExpBuffer tab_completion_query_buf; + +extern void initialize_readline_cockroachdb(void); + +#endif /* TAB_COMPLETE_COCKROACHDB_H */ From 99f673ba6a1dc6332df53f3a04a4093c71a992b8 Mon Sep 17 00:00:00 2001 From: Robins Tharakan Date: Mon, 25 Sep 2017 06:41:55 +1000 Subject: [PATCH 109/149] Correct a typo in Makefile. Compiling / Logsin / Recognizes CockroachDB correctly --- src/bin/psql/Makefile | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/bin/psql/Makefile b/src/bin/psql/Makefile index 02054e0a121a7..9955f38bbb2b4 100644 --- a/src/bin/psql/Makefile +++ b/src/bin/psql/Makefile @@ -24,7 +24,8 @@ override LDFLAGS := -L$(top_builddir)/src/fe_utils -lpgfeutils $(libpq_pgport) $ OBJS= command.o common.o conditional.o copy.o crosstabview.o \ describe.o help.o input.o large_obj.o mainloop.o \ prompt.o psqlscanslash.o sql_help.o startup.o stringutils.o \ - tab-complete.o tabtab-complete-cockroachdb.o -complete-pipelinedb.o tab-complete-redshift.o variables.o \ + tab-complete.o tab-complete-cockroachdb.o tab-complete-pipelinedb.o \ + tab-complete-redshift.o variables.o \ $(WIN32RES) From cc93be638582d08bb6d2a9c6f53a20704274a4fb Mon Sep 17 00:00:00 2001 From: Robins Tharakan Date: Mon, 25 Sep 2017 06:44:33 +1000 Subject: [PATCH 110/149] Begin using forks branch in psql/README documentations --- src/bin/psql/README.md | 6 +++--- src/bin/psql/README_cockroachdb.md | 2 +- src/bin/psql/README_pipelinedb.md | 2 +- src/bin/psql/README_redshift.md | 2 +- 4 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/bin/psql/README.md b/src/bin/psql/README.md index a03fd0ce49d9f..34e71d4c79e12 100644 --- a/src/bin/psql/README.md +++ b/src/bin/psql/README.md @@ -4,6 +4,6 @@ This documents the current state of psql's auto-complete support for various Pos Postgres Forks | Support Level | Description :-------------:| :-----------: | :---------- -Amazon Redshift | [Status](https://github.com/robins/postgres/blob/redshift/src/bin/psql/README_redshift.md) | Amazon Redshift was spin off of PostgreSQL (version 8.0.2) -CockroachDB | [Status](https://github.com/robins/postgres/blob/redshift/src/bin/psql/README_cockroachdb.md) | Compatible with PostgreSQL (version 9.5.0) -PipelineDB | [Status](https://github.com/robins/postgres/blob/redshift/src/bin/psql/README_pipelinedb.md) | Compatible with PostgreSQL (version 9.5) +Amazon Redshift | [Status](https://github.com/robins/postgres/blob/forks/src/bin/psql/README_redshift.md) | Amazon Redshift was spin off of PostgreSQL (version 8.0.2) +CockroachDB | [Status](https://github.com/robins/postgres/blob/forks/src/bin/psql/README_cockroachdb.md) | Compatible with PostgreSQL (version 9.5.0) +PipelineDB | [Status](https://github.com/robins/postgres/blob/forks/src/bin/psql/README_pipelinedb.md) | Compatible with PostgreSQL (version 9.5) diff --git a/src/bin/psql/README_cockroachdb.md b/src/bin/psql/README_cockroachdb.md index a65c6255c47ab..a71338fd18749 100644 --- a/src/bin/psql/README_cockroachdb.md +++ b/src/bin/psql/README_cockroachdb.md @@ -2,7 +2,7 @@ This document shows the current state of psql's auto-complete support for Cockro Reference: https://www.cockroachlabs.com/docs/stable/sql-statements.html -See Support level of all Postgres forks: [Here](https://github.com/robins/postgres/blob/redshift/src/bin/psql/README.md) +See Support level of all Postgres forks: [Here](https://github.com/robins/postgres/blob/forks/src/bin/psql/README.md) SQL Commands | Supported State diff --git a/src/bin/psql/README_pipelinedb.md b/src/bin/psql/README_pipelinedb.md index ea29e352ab062..2f494649f06bb 100644 --- a/src/bin/psql/README_pipelinedb.md +++ b/src/bin/psql/README_pipelinedb.md @@ -2,7 +2,7 @@ This document shows the current state of psql's auto-complete support for Pipeli Reference: http://docs.pipelinedb.com/ -See Support level of all Postgres forks: [Here](https://github.com/robins/postgres/blob/redshift/src/bin/psql/README.md) +See Support level of all Postgres forks: [Here](https://github.com/robins/postgres/blob/forks/src/bin/psql/README.md) SQL Commands | Supported State :----------- |:--------------: diff --git a/src/bin/psql/README_redshift.md b/src/bin/psql/README_redshift.md index 69808160025a5..3b292e2716b28 100644 --- a/src/bin/psql/README_redshift.md +++ b/src/bin/psql/README_redshift.md @@ -2,7 +2,7 @@ This document shows the current state of psql's auto-complete support for Amazon NOTE: This branch is maintained as a personal effort and is not sponsored / supported by my Employer or any other third-party. This is not officially supported / endorsed by Amazon in any way. -See Support level of all Postgres forks: [Here](https://github.com/robins/postgres/blob/redshift/src/bin/psql/README.md) +See Support level of all Postgres forks: [Here](https://github.com/robins/postgres/blob/forks/src/bin/psql/README.md) Parameters | Supported State :---------:|:--------------: From 9e76fca20dfe340c1dcc73694249e00c0307b90c Mon Sep 17 00:00:00 2001 From: Robins Tharakan Date: Mon, 25 Sep 2017 06:46:07 +1000 Subject: [PATCH 111/149] Mark all SQL statements as Untested (instead of Native) --- src/bin/psql/README_cockroachdb.md | 54 +++++++++++++++--------------- 1 file changed, 27 insertions(+), 27 deletions(-) diff --git a/src/bin/psql/README_cockroachdb.md b/src/bin/psql/README_cockroachdb.md index a71338fd18749..e198b8e18bb49 100644 --- a/src/bin/psql/README_cockroachdb.md +++ b/src/bin/psql/README_cockroachdb.md @@ -7,33 +7,33 @@ See Support level of all Postgres forks: [Here](https://github.com/robins/postgr SQL Commands | Supported State :----------- |:--------------: -ADD COLUMN | Native -ADD CONSTRAINT | Native -ALTER COLUMN | Native -ALTER TABLE | Native -ALTER VIEW | Native -CREATE DATABASE | Native -CREATE INDEX | Native -CREATE TABLE | Native -CREATE TABLE AS | Native -CREATE VIEW | Native -DROP COLUMN | Native -DROP CONSTRAINT | Native -DROP DATABASE | Native -DROP INDEX | Native -DROP TABLE | Native -DROP VIEW | Native -RENAME COLUMN | Native -RENAME DATABASE | Native -RENAME INDEX | Native -RENAME TABLE | Native -SHOW COLUMNS | Native -SHOW CONSTRAINTS | Native -SHOW CREATE TABLE | Native -SHOW CREATE VIEW | Native -SHOW DATABASES | Native -SHOW INDEX | Native -SHOW TABLES | Native +ADD COLUMN | Untested +ADD CONSTRAINT | Untested +ALTER COLUMN | Untested +ALTER TABLE | Untested +ALTER VIEW | Untested +CREATE DATABASE | Untested +CREATE INDEX | Untested +CREATE TABLE | Untested +CREATE TABLE AS | Untested +CREATE VIEW | Untested +DROP COLUMN | Untested +DROP CONSTRAINT | Untested +DROP DATABASE | Untested +DROP INDEX | Untested +DROP TABLE | Untested +DROP VIEW | Untested +RENAME COLUMN | Untested +RENAME DATABASE | Untested +RENAME INDEX | Untested +RENAME TABLE | Untested +SHOW COLUMNS | Untested +SHOW CONSTRAINTS | Untested +SHOW CREATE TABLE | Untested +SHOW CREATE VIEW | Untested +SHOW DATABASES | Untested +SHOW INDEX | Untested +SHOW TABLES | Untested Support Legend | Description From b1b5dbf074bd6a581ff4e053a3509d8c066505c4 Mon Sep 17 00:00:00 2001 From: Robins Tharakan Date: Mon, 25 Sep 2017 09:10:43 +1000 Subject: [PATCH 112/149] \d etc. now begin to work. Still some other basics need more work. Removed xxx_is_visible() from all over in Cockroachdb --- src/bin/psql/describe.c | 59 ++++++++++++---------- src/bin/psql/tab-complete-cockroachdb.c | 66 +++++++++++-------------- 2 files changed, 61 insertions(+), 64 deletions(-) diff --git a/src/bin/psql/describe.c b/src/bin/psql/describe.c index 9a5c28b4c7e87..465163e377f51 100644 --- a/src/bin/psql/describe.c +++ b/src/bin/psql/describe.c @@ -116,8 +116,8 @@ describeAggregates(const char *pattern, bool verbose, bool showSystem) " AND n.nspname <> 'information_schema'\n"); processSQLNamePattern(pset.db, &buf, pattern, true, false, - "n.nspname", "p.proname", NULL, - "pg_catalog.pg_function_is_visible(p.oid)"); + "n.nspname", "p.proname", NULL, + (!IS_COCKROACHDB?"pg_catalog.pg_function_is_visible(p.oid)":NULL)); appendPQExpBufferStr(&buf, "ORDER BY 1, 2, 4;"); @@ -553,7 +553,7 @@ describeFunctions(const char *functypes, const char *pattern, bool verbose, bool processSQLNamePattern(pset.db, &buf, pattern, have_where, false, "n.nspname", "p.proname", NULL, - "pg_catalog.pg_function_is_visible(p.oid)"); + (!IS_COCKROACHDB?"pg_catalog.pg_function_is_visible(p.oid)":NULL)); if (!showSystem && !pattern) appendPQExpBufferStr(&buf, " AND n.nspname <> 'pg_catalog'\n" @@ -684,7 +684,7 @@ describeTypes(const char *pattern, bool verbose, bool showSystem) processSQLNamePattern(pset.db, &buf, pattern, true, false, "n.nspname", "t.typname", "pg_catalog.format_type(t.oid, NULL)", - "pg_catalog.pg_type_is_visible(t.oid)"); + (!IS_COCKROACHDB?"pg_catalog.pg_type_is_visible(t.oid)":NULL)); appendPQExpBufferStr(&buf, "ORDER BY 1, 2;"); @@ -760,7 +760,7 @@ describeOperators(const char *pattern, bool verbose, bool showSystem) processSQLNamePattern(pset.db, &buf, pattern, !showSystem && !pattern, true, "n.nspname", "o.oprname", NULL, - "pg_catalog.pg_operator_is_visible(o.oid)"); + (!IS_COCKROACHDB?"pg_catalog.pg_operator_is_visible(o.oid)":NULL)); appendPQExpBufferStr(&buf, "ORDER BY 1, 2, 3, 4;"); @@ -982,9 +982,14 @@ permissionsList(const char *pattern) * point of view. You can see 'em by explicit request though, eg with \z * pg_catalog.* */ - processSQLNamePattern(pset.db, &buf, pattern, true, false, - "n.nspname", "c.relname", NULL, - "n.nspname !~ '^pg_' AND pg_catalog.pg_table_is_visible(c.oid)"); + if (IS_COCKROACHDB) + processSQLNamePattern(pset.db, &buf, pattern, true, false, + "n.nspname", "c.relname", NULL, + "n.nspname !~ '^pg_' AND"); + else + processSQLNamePattern(pset.db, &buf, pattern, true, false, + "n.nspname", "c.relname", NULL, + "n.nspname !~ '^pg_' AND pg_catalog.pg_table_is_visible(c.oid)"); appendPQExpBufferStr(&buf, "ORDER BY 1, 2;"); @@ -1137,7 +1142,7 @@ objectDescription(const char *pattern, bool showSystem) processSQLNamePattern(pset.db, &buf, pattern, !showSystem && !pattern, false, "n.nspname", "pgc.conname", NULL, - "pg_catalog.pg_table_is_visible(c.oid)"); + (!IS_COCKROACHDB?"pg_catalog.pg_table_is_visible(c.oid)":NULL)); /* Domain constraint descriptions */ appendPQExpBuffer(&buf, @@ -1159,7 +1164,7 @@ objectDescription(const char *pattern, bool showSystem) processSQLNamePattern(pset.db, &buf, pattern, !showSystem && !pattern, false, "n.nspname", "pgc.conname", NULL, - "pg_catalog.pg_type_is_visible(t.oid)"); + (!IS_COCKROACHDB?"pg_catalog.pg_type_is_visible(t.oid)":NULL)); /* @@ -1187,7 +1192,7 @@ objectDescription(const char *pattern, bool showSystem) processSQLNamePattern(pset.db, &buf, pattern, true, false, "n.nspname", "o.opcname", NULL, - "pg_catalog.pg_opclass_is_visible(o.oid)"); + (!IS_COCKROACHDB?"pg_catalog.pg_opclass_is_visible(o.oid)":NULL)); } /* @@ -1216,7 +1221,7 @@ objectDescription(const char *pattern, bool showSystem) processSQLNamePattern(pset.db, &buf, pattern, true, false, "n.nspname", "opf.opfname", NULL, - "pg_catalog.pg_opfamily_is_visible(opf.oid)"); + (!IS_COCKROACHDB?"pg_catalog.pg_opfamily_is_visible(opf.oid)":NULL)); } /* Rule descriptions (ignore rules for views) */ @@ -1258,7 +1263,7 @@ objectDescription(const char *pattern, bool showSystem) processSQLNamePattern(pset.db, &buf, pattern, !showSystem && !pattern, false, "n.nspname", "t.tgname", NULL, - "pg_catalog.pg_table_is_visible(c.oid)"); + (!IS_COCKROACHDB?"pg_catalog.pg_table_is_visible(c.oid)":NULL)); appendPQExpBufferStr(&buf, ") AS tt\n" @@ -1314,7 +1319,7 @@ describeTableDetails(const char *pattern, bool verbose, bool showSystem) processSQLNamePattern(pset.db, &buf, pattern, !showSystem && !pattern, false, "n.nspname", "c.relname", NULL, - "pg_catalog.pg_table_is_visible(c.oid)"); + (!IS_COCKROACHDB?"pg_catalog.pg_table_is_visible(c.oid)":NULL)); appendPQExpBufferStr(&buf, "ORDER BY 2, 3;"); @@ -3541,7 +3546,7 @@ listTables(const char *tabtypes, const char *pattern, bool verbose, bool showSys processSQLNamePattern(pset.db, &buf, pattern, true, false, "n.nspname", "c.relname", NULL, - "pg_catalog.pg_table_is_visible(c.oid)"); + (!IS_COCKROACHDB?"pg_catalog.pg_table_is_visible(c.oid)":NULL)); appendPQExpBufferStr(&buf, "ORDER BY 1,2;"); @@ -3721,7 +3726,7 @@ listDomains(const char *pattern, bool verbose, bool showSystem) processSQLNamePattern(pset.db, &buf, pattern, true, false, "n.nspname", "t.typname", NULL, - "pg_catalog.pg_type_is_visible(t.oid)"); + (!IS_COCKROACHDB?"pg_catalog.pg_type_is_visible(t.oid)":NULL)); appendPQExpBufferStr(&buf, "ORDER BY 1, 2;"); @@ -3795,7 +3800,7 @@ listConversions(const char *pattern, bool verbose, bool showSystem) processSQLNamePattern(pset.db, &buf, pattern, true, false, "n.nspname", "c.conname", NULL, - "pg_catalog.pg_conversion_is_visible(c.oid)"); + (!IS_COCKROACHDB?"pg_catalog.pg_conversion_is_visible(c.oid)":NULL)); appendPQExpBufferStr(&buf, "ORDER BY 1, 2;"); @@ -3953,14 +3958,14 @@ listCasts(const char *pattern, bool verbose) processSQLNamePattern(pset.db, &buf, pattern, true, false, "ns.nspname", "ts.typname", "pg_catalog.format_type(ts.oid, NULL)", - "pg_catalog.pg_type_is_visible(ts.oid)"); + (!IS_COCKROACHDB?"pg_catalog.pg_type_is_visible(ts.oid)":NULL)); appendPQExpBufferStr(&buf, ") OR (true"); processSQLNamePattern(pset.db, &buf, pattern, true, false, "nt.nspname", "tt.typname", "pg_catalog.format_type(tt.oid, NULL)", - "pg_catalog.pg_type_is_visible(tt.oid)"); + (!IS_COCKROACHDB?"pg_catalog.pg_type_is_visible(tt.oid)":NULL)); appendPQExpBufferStr(&buf, ") )\nORDER BY 1, 2;"); @@ -4044,7 +4049,7 @@ listCollations(const char *pattern, bool verbose, bool showSystem) processSQLNamePattern(pset.db, &buf, pattern, true, false, "n.nspname", "c.collname", NULL, - "pg_catalog.pg_collation_is_visible(c.oid)"); + (!IS_COCKROACHDB?"pg_catalog.pg_collation_is_visible(c.oid)":NULL)); appendPQExpBufferStr(&buf, "ORDER BY 1, 2;"); @@ -4163,7 +4168,7 @@ listTSParsers(const char *pattern, bool verbose) processSQLNamePattern(pset.db, &buf, pattern, false, false, "n.nspname", "p.prsname", NULL, - "pg_catalog.pg_ts_parser_is_visible(p.oid)"); + (!IS_COCKROACHDB?"pg_catalog.pg_ts_parser_is_visible(p.oid)":NULL)); appendPQExpBufferStr(&buf, "ORDER BY 1, 2;"); @@ -4204,7 +4209,7 @@ listTSParsersVerbose(const char *pattern) processSQLNamePattern(pset.db, &buf, pattern, false, false, "n.nspname", "p.prsname", NULL, - "pg_catalog.pg_ts_parser_is_visible(p.oid)"); + (!IS_COCKROACHDB?"pg_catalog.pg_ts_parser_is_visible(p.oid)":NULL)); appendPQExpBufferStr(&buf, "ORDER BY 1, 2;"); @@ -4421,7 +4426,7 @@ listTSDictionaries(const char *pattern, bool verbose) processSQLNamePattern(pset.db, &buf, pattern, false, false, "n.nspname", "d.dictname", NULL, - "pg_catalog.pg_ts_dict_is_visible(d.oid)"); + (!IS_COCKROACHDB?"pg_catalog.pg_ts_dict_is_visible(d.oid)":NULL)); appendPQExpBufferStr(&buf, "ORDER BY 1, 2;"); @@ -4492,7 +4497,7 @@ listTSTemplates(const char *pattern, bool verbose) processSQLNamePattern(pset.db, &buf, pattern, false, false, "n.nspname", "t.tmplname", NULL, - "pg_catalog.pg_ts_template_is_visible(t.oid)"); + (!IS_COCKROACHDB?"pg_catalog.pg_ts_template_is_visible(t.oid)":NULL)); appendPQExpBufferStr(&buf, "ORDER BY 1, 2;"); @@ -4552,7 +4557,7 @@ listTSConfigs(const char *pattern, bool verbose) processSQLNamePattern(pset.db, &buf, pattern, false, false, "n.nspname", "c.cfgname", NULL, - "pg_catalog.pg_ts_config_is_visible(c.oid)"); + (!IS_COCKROACHDB?"pg_catalog.pg_ts_config_is_visible(c.oid)":NULL)); appendPQExpBufferStr(&buf, "ORDER BY 1, 2;"); @@ -4594,7 +4599,7 @@ listTSConfigsVerbose(const char *pattern) processSQLNamePattern(pset.db, &buf, pattern, true, false, "n.nspname", "c.cfgname", NULL, - "pg_catalog.pg_ts_config_is_visible(c.oid)"); + (!IS_COCKROACHDB?"pg_catalog.pg_ts_config_is_visible(c.oid)":NULL)); appendPQExpBufferStr(&buf, "ORDER BY 3, 2;"); @@ -5046,7 +5051,7 @@ listUserMappings(const char *pattern, bool verbose) processSQLNamePattern(pset.db, &buf, pattern, false, false, "n.nspname", "c.relname", NULL, - "pg_catalog.pg_table_is_visible(c.oid)"); + (!IS_COCKROACHDB?"pg_catalog.pg_table_is_visible(c.oid)":NULL)); appendPQExpBufferStr(&buf, "ORDER BY 1, 2;"); diff --git a/src/bin/psql/tab-complete-cockroachdb.c b/src/bin/psql/tab-complete-cockroachdb.c index ee7a9a84f2565..03b6bdc3e23ac 100644 --- a/src/bin/psql/tab-complete-cockroachdb.c +++ b/src/bin/psql/tab-complete-cockroachdb.c @@ -96,7 +96,9 @@ typedef struct SchemaQuery /* * Visibility condition --- which rows are visible without schema - * qualification? For example, "pg_catalog.pg_table_is_visible(c.oid)". + * qualification? For example, "pg_catalog.pg_table_is_visible(c.oid)" + * However, in CockroachDB there are *no* xxx_is_visible() functions, + * so setting all viscondition values (in Macros) to true by default */ const char *viscondition; @@ -363,7 +365,7 @@ static const SchemaQuery Query_for_list_of_aggregates = { /* selcondition */ "p.proisagg", /* viscondition */ - "pg_catalog.pg_function_is_visible(p.oid)", + "true", /* namespace */ "p.pronamespace", /* result */ @@ -381,7 +383,7 @@ static const SchemaQuery Query_for_list_of_datatypes = { " FROM pg_catalog.pg_class c WHERE c.oid = t.typrelid)) " "AND t.typname !~ '^_'", /* viscondition */ - "pg_catalog.pg_type_is_visible(t.oid)", + "true", /* namespace */ "t.typnamespace", /* result */ @@ -396,7 +398,7 @@ static const SchemaQuery Query_for_list_of_domains = { /* selcondition */ "t.typtype = 'd'", /* viscondition */ - "pg_catalog.pg_type_is_visible(t.oid)", + "true", /* namespace */ "t.typnamespace", /* result */ @@ -411,7 +413,7 @@ static const SchemaQuery Query_for_list_of_functions = { /* selcondition */ NULL, /* viscondition */ - "pg_catalog.pg_function_is_visible(p.oid)", + "true", /* namespace */ "p.pronamespace", /* result */ @@ -426,7 +428,7 @@ static const SchemaQuery Query_for_list_of_indexes = { /* selcondition */ "c.relkind IN (" CppAsString2(RELKIND_INDEX) ")", /* viscondition */ - "pg_catalog.pg_table_is_visible(c.oid)", + "true", /* namespace */ "c.relnamespace", /* result */ @@ -441,7 +443,7 @@ static const SchemaQuery Query_for_list_of_sequences = { /* selcondition */ "c.relkind IN (" CppAsString2(RELKIND_SEQUENCE) ")", /* viscondition */ - "pg_catalog.pg_table_is_visible(c.oid)", + "true", /* namespace */ "c.relnamespace", /* result */ @@ -456,7 +458,7 @@ static const SchemaQuery Query_for_list_of_foreign_tables = { /* selcondition */ "c.relkind IN (" CppAsString2(RELKIND_FOREIGN_TABLE) ")", /* viscondition */ - "pg_catalog.pg_table_is_visible(c.oid)", + "true", /* namespace */ "c.relnamespace", /* result */ @@ -472,7 +474,7 @@ static const SchemaQuery Query_for_list_of_tables = { "c.relkind IN (" CppAsString2(RELKIND_RELATION) ", " CppAsString2(RELKIND_PARTITIONED_TABLE) ")", /* viscondition */ - "pg_catalog.pg_table_is_visible(c.oid)", + "true", /* namespace */ "c.relnamespace", /* result */ @@ -487,7 +489,7 @@ static const SchemaQuery Query_for_list_of_partitioned_tables = { /* selcondition */ "c.relkind IN (" CppAsString2(RELKIND_PARTITIONED_TABLE) ")", /* viscondition */ - "pg_catalog.pg_table_is_visible(c.oid)", + "true", /* namespace */ "c.relnamespace", /* result */ @@ -502,7 +504,7 @@ static const SchemaQuery Query_for_list_of_constraints_with_schema = { /* selcondition */ "c.conrelid <> 0", /* viscondition */ - "true", /* there is no pg_constraint_is_visible */ + "true", /* namespace */ "c.connamespace", /* result */ @@ -521,7 +523,7 @@ static const SchemaQuery Query_for_list_of_updatables = { CppAsString2(RELKIND_VIEW) ", " CppAsString2(RELKIND_PARTITIONED_TABLE) ")", /* viscondition */ - "pg_catalog.pg_table_is_visible(c.oid)", + "true", /* namespace */ "c.relnamespace", /* result */ @@ -536,7 +538,7 @@ static const SchemaQuery Query_for_list_of_relations = { /* selcondition */ NULL, /* viscondition */ - "pg_catalog.pg_table_is_visible(c.oid)", + "true", /* namespace */ "c.relnamespace", /* result */ @@ -556,7 +558,7 @@ static const SchemaQuery Query_for_list_of_tsvmf = { CppAsString2(RELKIND_FOREIGN_TABLE) ", " CppAsString2(RELKIND_PARTITIONED_TABLE) ")", /* viscondition */ - "pg_catalog.pg_table_is_visible(c.oid)", + "true", /* namespace */ "c.relnamespace", /* result */ @@ -573,7 +575,7 @@ static const SchemaQuery Query_for_list_of_tmf = { CppAsString2(RELKIND_MATVIEW) ", " CppAsString2(RELKIND_FOREIGN_TABLE) ")", /* viscondition */ - "pg_catalog.pg_table_is_visible(c.oid)", + "true", /* namespace */ "c.relnamespace", /* result */ @@ -589,7 +591,7 @@ static const SchemaQuery Query_for_list_of_tm = { "c.relkind IN (" CppAsString2(RELKIND_RELATION) ", " CppAsString2(RELKIND_MATVIEW) ")", /* viscondition */ - "pg_catalog.pg_table_is_visible(c.oid)", + "true", /* namespace */ "c.relnamespace", /* result */ @@ -604,7 +606,7 @@ static const SchemaQuery Query_for_list_of_views = { /* selcondition */ "c.relkind IN (" CppAsString2(RELKIND_VIEW) ")", /* viscondition */ - "pg_catalog.pg_table_is_visible(c.oid)", + "true", /* namespace */ "c.relnamespace", /* result */ @@ -619,7 +621,7 @@ static const SchemaQuery Query_for_list_of_matviews = { /* selcondition */ "c.relkind IN (" CppAsString2(RELKIND_MATVIEW) ")", /* viscondition */ - "pg_catalog.pg_table_is_visible(c.oid)", + "true", /* namespace */ "c.relnamespace", /* result */ @@ -634,7 +636,7 @@ static const SchemaQuery Query_for_list_of_statistics = { /* selcondition */ NULL, /* viscondition */ - "pg_catalog.pg_statistics_obj_is_visible(s.oid)", + "true", /* namespace */ "s.stxnamespace", /* result */ @@ -666,8 +668,7 @@ static const SchemaQuery Query_for_list_of_statistics = { " AND NOT a.attisdropped "\ " AND substring(pg_catalog.quote_ident(attname),1,%d)='%s' "\ " AND (pg_catalog.quote_ident(relname)='%s' "\ -" OR '\"' || relname || '\"'='%s') "\ -" AND pg_catalog.pg_table_is_visible(c.oid)" +" OR '\"' || relname || '\"'='%s') " #define Query_for_list_of_attributes_with_schema \ "SELECT pg_catalog.quote_ident(attname) "\ @@ -691,8 +692,7 @@ static const SchemaQuery Query_for_list_of_statistics = { " WHERE t.oid = e.enumtypid "\ " AND substring(pg_catalog.quote_literal(enumlabel),1,%d)='%s' "\ " AND (pg_catalog.quote_ident(typname)='%s' "\ -" OR '\"' || typname || '\"'='%s') "\ -" AND pg_catalog.pg_type_is_visible(t.oid)" +" OR '\"' || typname || '\"'='%s') " #define Query_for_list_of_enum_values_with_schema \ "SELECT pg_catalog.quote_literal(enumlabel) "\ @@ -787,8 +787,7 @@ static const SchemaQuery Query_for_list_of_statistics = { " FROM pg_catalog.pg_class c1, pg_catalog.pg_class c2, pg_catalog.pg_index i"\ " WHERE c1.oid=i.indrelid and i.indexrelid=c2.oid"\ " and (%d = pg_catalog.length('%s'))"\ -" and pg_catalog.quote_ident(c2.relname)='%s'"\ -" and pg_catalog.pg_table_is_visible(c2.oid)" +" and pg_catalog.quote_ident(c2.relname)='%s'" /* the silly-looking length condition is just to eat up the current word */ #define Query_for_index_of_table \ @@ -796,16 +795,14 @@ static const SchemaQuery Query_for_list_of_statistics = { " FROM pg_catalog.pg_class c1, pg_catalog.pg_class c2, pg_catalog.pg_index i"\ " WHERE c1.oid=i.indrelid and i.indexrelid=c2.oid"\ " and (%d = pg_catalog.length('%s'))"\ -" and pg_catalog.quote_ident(c1.relname)='%s'"\ -" and pg_catalog.pg_table_is_visible(c2.oid)" +" and pg_catalog.quote_ident(c1.relname)='%s'" /* the silly-looking length condition is just to eat up the current word */ #define Query_for_constraint_of_table \ "SELECT pg_catalog.quote_ident(conname) "\ " FROM pg_catalog.pg_class c1, pg_catalog.pg_constraint con "\ " WHERE c1.oid=conrelid and (%d = pg_catalog.length('%s'))"\ -" and pg_catalog.quote_ident(c1.relname)='%s'"\ -" and pg_catalog.pg_table_is_visible(c1.oid)" +" and pg_catalog.quote_ident(c1.relname)='%s'" #define Query_for_all_table_constraints \ "SELECT pg_catalog.quote_ident(conname) "\ @@ -817,8 +814,7 @@ static const SchemaQuery Query_for_list_of_statistics = { "SELECT pg_catalog.quote_ident(conname) "\ " FROM pg_catalog.pg_type t, pg_catalog.pg_constraint con "\ " WHERE t.oid=contypid and (%d = pg_catalog.length('%s'))"\ -" and pg_catalog.quote_ident(t.typname)='%s'"\ -" and pg_catalog.pg_type_is_visible(t.oid)" +" and pg_catalog.quote_ident(t.typname)='%s'" /* the silly-looking length condition is just to eat up the current word */ #define Query_for_list_of_tables_for_constraint \ @@ -834,8 +830,7 @@ static const SchemaQuery Query_for_list_of_statistics = { "SELECT pg_catalog.quote_ident(rulename) "\ " FROM pg_catalog.pg_class c1, pg_catalog.pg_rewrite "\ " WHERE c1.oid=ev_class and (%d = pg_catalog.length('%s'))"\ -" and pg_catalog.quote_ident(c1.relname)='%s'"\ -" and pg_catalog.pg_table_is_visible(c1.oid)" +" and pg_catalog.quote_ident(c1.relname)='%s'" /* the silly-looking length condition is just to eat up the current word */ #define Query_for_list_of_tables_for_rule \ @@ -852,7 +847,6 @@ static const SchemaQuery Query_for_list_of_statistics = { " FROM pg_catalog.pg_class c1, pg_catalog.pg_trigger "\ " WHERE c1.oid=tgrelid and (%d = pg_catalog.length('%s'))"\ " and pg_catalog.quote_ident(c1.relname)='%s'"\ -" and pg_catalog.pg_table_is_visible(c1.oid)"\ " and not tgisinternal" /* the silly-looking length condition is just to eat up the current word */ @@ -923,8 +917,7 @@ static const SchemaQuery Query_for_list_of_statistics = { " FROM pg_catalog.pg_proc "\ " WHERE (%d = pg_catalog.length('%s'))"\ " AND (pg_catalog.quote_ident(proname)='%s'"\ -" OR '\"' || proname || '\"'='%s') "\ -" AND (pg_catalog.pg_function_is_visible(pg_proc.oid))" +" OR '\"' || proname || '\"'='%s') " /* the silly-looking length condition is just to eat up the current word */ #define Query_for_list_of_arguments_with_schema \ @@ -1007,7 +1000,6 @@ static const SchemaQuery Query_for_list_of_statistics = { " WHERE c1.oid=i.inhparent and i.inhrelid=c2.oid"\ " and (%d = pg_catalog.length('%s'))"\ " and pg_catalog.quote_ident(c1.relname)='%s'"\ -" and pg_catalog.pg_table_is_visible(c2.oid)"\ " and c2.relispartition = 'true'" /* From 45ad349a3d7c1db555b6c6ed32db44b46b086658 Mon Sep 17 00:00:00 2001 From: Robins Tharakan Date: Mon, 25 Sep 2017 23:30:29 +1000 Subject: [PATCH 113/149] \du starts working --- src/bin/psql/describe.c | 28 +++++++++++++++++++++++----- 1 file changed, 23 insertions(+), 5 deletions(-) diff --git a/src/bin/psql/describe.c b/src/bin/psql/describe.c index 465163e377f51..11ab210279915 100644 --- a/src/bin/psql/describe.c +++ b/src/bin/psql/describe.c @@ -3189,16 +3189,34 @@ describeRoles(const char *pattern, bool verbose, bool showSystem) initPQExpBuffer(&buf); - if (pset.sversion >= 80100) + if (IS_COCKROACHDB) { printfPQExpBuffer(&buf, "SELECT r.rolname, r.rolsuper, r.rolinherit,\n" " r.rolcreaterole, r.rolcreatedb, r.rolcanlogin,\n" " r.rolconnlimit, r.rolvaliduntil,\n" - " ARRAY(SELECT b.rolname\n" - " FROM pg_catalog.pg_auth_members m\n" - " JOIN pg_catalog.pg_roles b ON (m.roleid = b.oid)\n" - " WHERE m.member = r.oid) as memberof"); + " 'NA' as memberof ,\n" + " pg_catalog.shobj_description(r.oid, 'pg_authid') AS description,\n" + " 'f' AS rolreplication, \n" + " 'f' AS rolbypassrls\n" + "FROM pg_catalog.pg_roles r\n"); + + if (!showSystem && !pattern) + appendPQExpBufferStr(&buf, "WHERE r.rolname !~ '^pg_'\n"); + + processSQLNamePattern(pset.db, &buf, pattern, false, false, + NULL, "r.rolname", NULL, NULL); + } + else if (pset.sversion >= 80100) + { + printfPQExpBuffer(&buf, + "SELECT r.rolname, r.rolsuper, r.rolinherit,\n" + " r.rolcreaterole, r.rolcreatedb, r.rolcanlogin,\n" + " r.rolconnlimit, r.rolvaliduntil,\n" + " ARRAY(SELECT b.rolname\n" + " FROM pg_catalog.pg_auth_members m\n" + " JOIN pg_catalog.pg_roles b ON (m.roleid = b.oid)\n" + " WHERE m.member = r.oid) as memberof"); if (verbose && pset.sversion >= 80200) { From af1f9506a0ae4908328f88bf5334a1d37bdaa693 Mon Sep 17 00:00:00 2001 From: Robins Tharakan Date: Mon, 25 Sep 2017 23:47:16 +1000 Subject: [PATCH 114/149] Basic psql shortcuts working (relevant to the engine) such as \di \dv \l \du \d+... --- src/bin/psql/describe.c | 38 +++++++++++++++++++++++++++----------- 1 file changed, 27 insertions(+), 11 deletions(-) diff --git a/src/bin/psql/describe.c b/src/bin/psql/describe.c index 11ab210279915..9c4b1dc06e097 100644 --- a/src/bin/psql/describe.c +++ b/src/bin/psql/describe.c @@ -794,13 +794,22 @@ listAllDbs(const char *pattern, bool verbose) initPQExpBuffer(&buf); - printfPQExpBuffer(&buf, - "SELECT d.datname as \"%s\",\n" - " pg_catalog.pg_get_userbyid(d.datdba) as \"%s\",\n" - " pg_catalog.pg_encoding_to_char(d.encoding) as \"%s\",\n", - gettext_noop("Name"), - gettext_noop("Owner"), - gettext_noop("Encoding")); + if (IS_COCKROACHDB) + printfPQExpBuffer(&buf, + "SELECT d.datname as \"%s\",\n" + " pg_catalog.pg_get_userbyid(d.datdba) as \"%s\",\n" + " 'Not Supported Yet' as \"%s\",\n", + gettext_noop("Name"), + gettext_noop("Owner"), + gettext_noop("Encoding")); + else + printfPQExpBuffer(&buf, + "SELECT d.datname as \"%s\",\n" + " pg_catalog.pg_get_userbyid(d.datdba) as \"%s\",\n" + " pg_catalog.pg_encoding_to_char(d.encoding) as \"%s\",\n", + gettext_noop("Name"), + gettext_noop("Owner"), + gettext_noop("Encoding")); if (pset.sversion >= 80400) appendPQExpBuffer(&buf, " d.datcollate as \"%s\",\n" @@ -3507,7 +3516,11 @@ listTables(const char *tabtypes, const char *pattern, bool verbose, bool showSys * As of PostgreSQL 9.0, use pg_table_size() to show a more accurate * size of a table, including FSM, VM and TOAST tables. */ - if (pset.sversion >= 90000) + if (IS_COCKROACHDB) + appendPQExpBuffer(&buf, + ",\n 'Not Supported Yet' as \"%s\"", + gettext_noop("Size")); + else if (pset.sversion >= 90000) appendPQExpBuffer(&buf, ",\n pg_catalog.pg_size_pretty(pg_catalog.pg_table_size(c.oid)) as \"%s\"", gettext_noop("Size")); @@ -5600,10 +5613,13 @@ describeSubscriptions(const char *pattern, bool verbose) static void printACLColumn(PQExpBuffer buf, const char *colname) { - if (pset.sversion >= 80100) + if (IS_COCKROACHDB) appendPQExpBuffer(buf, - "pg_catalog.array_to_string(%s, E'\\n') AS \"%s\"", - colname, gettext_noop("Access privileges")); + "'Not Supported Yet' AS \"%s\"", gettext_noop("Access privileges")); + else if (pset.sversion >= 80100) + appendPQExpBuffer(buf, + "pg_catalog.array_to_string(%s, E'\\n') AS \"%s\"", + colname, gettext_noop("Access privileges")); else appendPQExpBuffer(buf, "pg_catalog.array_to_string(%s, '\\n') AS \"%s\"", From 6a9dc265042b2cc16889705b593891ec536bb264 Mon Sep 17 00:00:00 2001 From: Robins Tharakan Date: Tue, 26 Sep 2017 05:17:54 +1000 Subject: [PATCH 115/149] \d tbl was not working in CockroachDB. Took a lot of mangling. Working now --- src/bin/psql/describe.c | 79 +++++++++++++++++++++++++++++++---------- 1 file changed, 61 insertions(+), 18 deletions(-) diff --git a/src/bin/psql/describe.c b/src/bin/psql/describe.c index 9c4b1dc06e097..0060ca9abb1a9 100644 --- a/src/bin/psql/describe.c +++ b/src/bin/psql/describe.c @@ -1435,7 +1435,22 @@ describeOneTableDetails(const char *schemaname, initPQExpBuffer(&tmpbuf); /* Get general table info */ - if (pset.sversion >= 90500) + if (IS_COCKROACHDB) + { + printfPQExpBuffer(&buf, + "SELECT c.relchecks, c.relkind, c.relhasindex, c.relhasrules, " + "c.relhastriggers, 'Not Supported Yet' AS relrowsecurity, 'Not Supported Yet' AS relforcerowsecurity, " + "c.relhasoids, %s, c.reltablespace, " + "'', 'Not Supported Yet' AS relpersistence, 'Not Supported Yet' AS relreplident\n" + "FROM pg_catalog.pg_class c\n " + "LEFT JOIN pg_catalog.pg_class tc ON (c.reltoastrelid = tc.oid)\n" + "WHERE c.oid = '%s'::OID;", + (verbose ? + "'Not Supported Yet'\n" + : "''"), + oid); + } + else if (pset.sversion >= 90500) { printfPQExpBuffer(&buf, "SELECT c.relchecks, c.relkind, c.relhasindex, c.relhasrules, " @@ -1628,25 +1643,34 @@ describeOneTableDetails(const char *schemaname, * you are adding column(s) preceding to verbose-only columns. */ printfPQExpBuffer(&buf, "SELECT a.attname,"); - appendPQExpBufferStr(&buf, "\n pg_catalog.format_type(a.atttypid, a.atttypmod)," - "\n (SELECT substring(pg_catalog.pg_get_expr(d.adbin, d.adrelid) for 128)" - "\n FROM pg_catalog.pg_attrdef d" - "\n WHERE d.adrelid = a.attrelid AND d.adnum = a.attnum AND a.atthasdef)," + if (IS_COCKROACHDB) + appendPQExpBufferStr(&buf, "\n pg_catalog.format_type(a.atttypid, a.atttypmod)," + "\n 'Not Supported Yet'," "\n a.attnotnull, a.attnum,"); - if (pset.sversion >= 90100) + else + appendPQExpBufferStr(&buf, "\n pg_catalog.format_type(a.atttypid, a.atttypmod)," + "\n (SELECT substring(pg_catalog.pg_get_expr(d.adbin, d.adrelid) for 128)" + "\n FROM pg_catalog.pg_attrdef d" + "\n WHERE d.adrelid = a.attrelid AND d.adnum = a.attnum AND a.atthasdef)," + "\n a.attnotnull, a.attnum,"); + if (IS_COCKROACHDB) + appendPQExpBufferStr(&buf, "\n 'Not Supported Yet' AS attcollation"); + else if (pset.sversion >= 90100) appendPQExpBufferStr(&buf, "\n (SELECT c.collname FROM pg_catalog.pg_collation c, pg_catalog.pg_type t\n" " WHERE c.oid = a.attcollation AND t.oid = a.atttypid AND a.attcollation <> t.typcollation) AS attcollation"); else appendPQExpBufferStr(&buf, "\n NULL AS attcollation"); - if (pset.sversion >= 100000) + if (IS_COCKROACHDB) + appendPQExpBufferStr(&buf, ",\n 'Not Supported Yet' AS attidentity"); + else if (pset.sversion >= 100000) appendPQExpBufferStr(&buf, ",\n a.attidentity"); else appendPQExpBufferStr(&buf, ",\n ''::pg_catalog.char AS attidentity"); - if (tableinfo.relkind == RELKIND_INDEX) + if ((tableinfo.relkind == RELKIND_INDEX && !IS_COCKROACHDB)) appendPQExpBufferStr(&buf, ",\n pg_catalog.pg_get_indexdef(a.attrelid, a.attnum, TRUE) AS indexdef"); else appendPQExpBufferStr(&buf, ",\n NULL AS indexdef"); - if (tableinfo.relkind == RELKIND_FOREIGN_TABLE && pset.sversion >= 90200) + if ((tableinfo.relkind == RELKIND_FOREIGN_TABLE && pset.sversion >= 90200 && IS_COCKROACHDB)) appendPQExpBufferStr(&buf, ",\n CASE WHEN attfdwoptions IS NULL THEN '' ELSE " " '(' || pg_catalog.array_to_string(ARRAY(SELECT pg_catalog.quote_ident(option_name) || ' ' || pg_catalog.quote_literal(option_value) FROM " " pg_catalog.pg_options_to_table(attfdwoptions)), ', ') || ')' END AS attfdwoptions"); @@ -1690,7 +1714,10 @@ describeOneTableDetails(const char *schemaname, } appendPQExpBufferStr(&buf, "\nFROM pg_catalog.pg_attribute a"); - appendPQExpBuffer(&buf, "\nWHERE a.attrelid = '%s' AND a.attnum > 0 AND NOT a.attisdropped", oid); + if (IS_COCKROACHDB) + appendPQExpBuffer(&buf, "\nWHERE a.attrelid = '%s'::OID AND a.attnum > 0 AND NOT a.attisdropped", oid); + else + appendPQExpBuffer(&buf, "\nWHERE a.attrelid = '%s' AND a.attnum > 0 AND NOT a.attisdropped", oid); appendPQExpBufferStr(&buf, "\nORDER BY a.attnum;"); res = PSQLexec(buf.data); @@ -2169,8 +2196,11 @@ describeOneTableDetails(const char *schemaname, appendPQExpBufferStr(&buf, "i.indisvalid, "); else appendPQExpBufferStr(&buf, "true as indisvalid, "); - appendPQExpBufferStr(&buf, "pg_catalog.pg_get_indexdef(i.indexrelid, 0, true),\n "); - if (pset.sversion >= 90000) + if (IS_COCKROACHDB) + appendPQExpBufferStr(&buf, "'Not Supported Yet' AS pg_get_indexdef,\n "); + else + appendPQExpBufferStr(&buf, "pg_catalog.pg_get_indexdef(i.indexrelid, 0, true),\n "); + if (pset.sversion >= 90000 && !IS_COCKROACHDB) appendPQExpBufferStr(&buf, "pg_catalog.pg_get_constraintdef(con.oid, true), " "contype, condeferrable, condeferred"); @@ -2194,15 +2224,19 @@ describeOneTableDetails(const char *schemaname, if (pset.sversion >= 90000) appendPQExpBufferStr(&buf, " LEFT JOIN pg_catalog.pg_constraint con ON (conrelid = i.indrelid AND conindid = i.indexrelid AND contype IN ('p','u','x'))\n"); - appendPQExpBuffer(&buf, - "WHERE c.oid = '%s' AND c.oid = i.indrelid AND i.indexrelid = c2.oid\n", oid); + if (IS_COCKROACHDB) + appendPQExpBuffer(&buf, + "WHERE c.oid = '%s'::OID AND c.oid = i.indrelid AND i.indexrelid = c2.oid\n", oid); + else + appendPQExpBuffer(&buf, + "WHERE c.oid = '%s' AND c.oid = i.indrelid AND i.indexrelid = c2.oid\n", oid); } - if (tableinfo.hasindex && tableinfo.hassortkey) { + if (tableinfo.hasindex && tableinfo.hassortkey && IS_REDSHIFT) { appendPQExpBufferStr(&buf, " UNION ALL "); } - if (tableinfo.hassortkey) { + if (tableinfo.hassortkey && IS_REDSHIFT) { appendPQExpBufferStr(&buf, " SELECT " @@ -2422,7 +2456,7 @@ describeOneTableDetails(const char *schemaname, } /* print any row-level policies */ - if (pset.sversion >= 90500) + if (pset.sversion >= 90500 && !IS_COCKROACHDB) { printfPQExpBuffer(&buf, "SELECT pol.polname,"); if (pset.sversion >= 100000) @@ -2926,6 +2960,8 @@ describeOneTableDetails(const char *schemaname, } /* print inherited tables (exclude, if parent is a partitioned table) */ + if (!IS_COCKROACHDB) + { printfPQExpBuffer(&buf, "SELECT c.oid::pg_catalog.regclass" " FROM pg_catalog.pg_class c, pg_catalog.pg_inherits i" @@ -2959,9 +2995,16 @@ describeOneTableDetails(const char *schemaname, PQclear(result); } + } /* print child tables (with additional info if partitions) */ - if (pset.sversion >= 100000) + if (IS_COCKROACHDB) + printfPQExpBuffer(&buf, + "SELECT c.oid::regclass, 'Not Supported Yet' AS relpartbound" + " FROM pg_catalog.pg_class c, pg_catalog.pg_inherits i" + " WHERE c.oid=i.inhrelid AND i.inhparent = '%s'::OID" + " ORDER BY c.oid::regclass::text;", oid); + else if (pset.sversion >= 100000) printfPQExpBuffer(&buf, "SELECT c.oid::pg_catalog.regclass, pg_catalog.pg_get_expr(c.relpartbound, c.oid)" " FROM pg_catalog.pg_class c, pg_catalog.pg_inherits i" From f16b3ae7802a9d9ab54f67ba5b4a785b150be7e5 Mon Sep 17 00:00:00 2001 From: Robins Tharakan Date: Wed, 27 Sep 2017 05:53:39 +1000 Subject: [PATCH 116/149] In CockroachDB, some corner cases were crashing or still ???. Make things Robust and clearly say 'Not Supported Yet' where requird --- src/bin/psql/describe.c | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/src/bin/psql/describe.c b/src/bin/psql/describe.c index 0060ca9abb1a9..5d4354c9eab96 100644 --- a/src/bin/psql/describe.c +++ b/src/bin/psql/describe.c @@ -1853,8 +1853,8 @@ describeOneTableDetails(const char *schemaname, printTableAddHeader(&cont, headers[i], true, 'l'); /* Get view_def if table is a view or materialized view */ - if ((tableinfo.relkind == RELKIND_VIEW || - tableinfo.relkind == RELKIND_MATVIEW) && verbose) + if (!IS_COCKROACHDB && ((tableinfo.relkind == RELKIND_VIEW || + tableinfo.relkind == RELKIND_MATVIEW) && verbose)) { PGresult *result; @@ -1935,7 +1935,7 @@ describeOneTableDetails(const char *schemaname, (storage[0] == 'm' ? "main" : (storage[0] == 'x' ? "extended" : (storage[0] == 'e' ? "external" : - "???")))), + "Not Supported Yet")))), false, false); /* Statistics target, if the relkind supports this feature */ @@ -2197,7 +2197,7 @@ describeOneTableDetails(const char *schemaname, else appendPQExpBufferStr(&buf, "true as indisvalid, "); if (IS_COCKROACHDB) - appendPQExpBufferStr(&buf, "'Not Supported Yet' AS pg_get_indexdef,\n "); + appendPQExpBufferStr(&buf, "' USING (Not Supported Yet)' AS pg_get_indexdef,\n "); else appendPQExpBufferStr(&buf, "pg_catalog.pg_get_indexdef(i.indexrelid, 0, true),\n "); if (pset.sversion >= 90000 && !IS_COCKROACHDB) @@ -2736,8 +2736,13 @@ describeOneTableDetails(const char *schemaname, PGresult *result = NULL; /* Footer information about a view */ - printTableAddFooter(&cont, _("View definition:")); - printTableAddFooter(&cont, view_def); + if (IS_COCKROACHDB) + printTableAddFooter(&cont, _("View definition: Not Supported Yet")); + else + { + printTableAddFooter(&cont, _("View definition:")); + printTableAddFooter(&cont, view_def); + } /* print rules */ if (tableinfo.hasrules) @@ -3101,7 +3106,7 @@ describeOneTableDetails(const char *schemaname, s, tableinfo.relreplident == 'f' ? "FULL" : tableinfo.relreplident == 'n' ? "NOTHING" : - "???"); + "Not Supported Yet"); printTableAddFooter(&cont, buf.data); } From 145f823723c13aa7375b0583b613ee9e39b2c614 Mon Sep 17 00:00:00 2001 From: Robins Tharakan Date: Wed, 27 Sep 2017 06:53:39 +1000 Subject: [PATCH 117/149] \l+ was failing for want of pg_tablespace, which is not supported. Working now --- src/bin/psql/describe.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/bin/psql/describe.c b/src/bin/psql/describe.c index 5d4354c9eab96..6f3610b61132a 100644 --- a/src/bin/psql/describe.c +++ b/src/bin/psql/describe.c @@ -818,14 +818,14 @@ listAllDbs(const char *pattern, bool verbose) gettext_noop("Ctype")); appendPQExpBufferStr(&buf, " "); printACLColumn(&buf, "d.datacl"); - if (verbose && pset.sversion >= 80200) + if (verbose && pset.sversion >= 80200 && !IS_COCKROACHDB) appendPQExpBuffer(&buf, ",\n CASE WHEN pg_catalog.has_database_privilege(d.datname, 'CONNECT')\n" " THEN pg_catalog.pg_size_pretty(pg_catalog.pg_database_size(d.datname))\n" " ELSE 'No Access'\n" " END as \"%s\"", gettext_noop("Size")); - if (verbose && pset.sversion >= 80000) + if (verbose && pset.sversion >= 80000 && !IS_COCKROACHDB) appendPQExpBuffer(&buf, ",\n t.spcname as \"%s\"", gettext_noop("Tablespace")); @@ -835,7 +835,7 @@ listAllDbs(const char *pattern, bool verbose) gettext_noop("Description")); appendPQExpBufferStr(&buf, "\nFROM pg_catalog.pg_database d\n"); - if (verbose && pset.sversion >= 80000) + if (verbose && pset.sversion >= 80000 && !IS_COCKROACHDB) appendPQExpBufferStr(&buf, " JOIN pg_catalog.pg_tablespace t on d.dattablespace = t.oid\n"); From b66d77fa599dff0c90c5e88335d58fd1f6e22587 Mon Sep 17 00:00:00 2001 From: Robins Tharakan Date: Wed, 27 Sep 2017 17:05:11 +1000 Subject: [PATCH 118/149] Check some SQLs and update psql/README_cockroachdb accordingly. Update one limitation with reasoning in comments --- src/bin/psql/README_cockroachdb.md | 20 ++++++++++---------- src/bin/psql/describe.c | 2 +- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/src/bin/psql/README_cockroachdb.md b/src/bin/psql/README_cockroachdb.md index e198b8e18bb49..51b961961ba7a 100644 --- a/src/bin/psql/README_cockroachdb.md +++ b/src/bin/psql/README_cockroachdb.md @@ -7,26 +7,26 @@ See Support level of all Postgres forks: [Here](https://github.com/robins/postgr SQL Commands | Supported State :----------- |:--------------: -ADD COLUMN | Untested +ADD COLUMN | Native ADD CONSTRAINT | Untested -ALTER COLUMN | Untested -ALTER TABLE | Untested -ALTER VIEW | Untested -CREATE DATABASE | Untested +ALTER COLUMN | Native +ALTER TABLE | Native +ALTER VIEW | Native +CREATE DATABASE | Native CREATE INDEX | Untested CREATE TABLE | Untested CREATE TABLE AS | Untested CREATE VIEW | Untested -DROP COLUMN | Untested +DROP COLUMN | Native DROP CONSTRAINT | Untested DROP DATABASE | Untested DROP INDEX | Untested -DROP TABLE | Untested -DROP VIEW | Untested -RENAME COLUMN | Untested +DROP TABLE | Native +DROP VIEW | Native +RENAME COLUMN | Native RENAME DATABASE | Untested RENAME INDEX | Untested -RENAME TABLE | Untested +RENAME TABLE | Native SHOW COLUMNS | Untested SHOW CONSTRAINTS | Untested SHOW CREATE TABLE | Untested diff --git a/src/bin/psql/describe.c b/src/bin/psql/describe.c index 6f3610b61132a..e977a1c979f1b 100644 --- a/src/bin/psql/describe.c +++ b/src/bin/psql/describe.c @@ -1645,7 +1645,7 @@ describeOneTableDetails(const char *schemaname, printfPQExpBuffer(&buf, "SELECT a.attname,"); if (IS_COCKROACHDB) appendPQExpBufferStr(&buf, "\n pg_catalog.format_type(a.atttypid, a.atttypmod)," - "\n 'Not Supported Yet'," + "\n 'Not Supported Yet'," // (Subquery not fully supported in CockroachDB) "\n a.attnotnull, a.attnum,"); else appendPQExpBufferStr(&buf, "\n pg_catalog.format_type(a.atttypid, a.atttypmod)," From 0fbdeb5ba0c961473d2b6eaffa8d94eb41de3565 Mon Sep 17 00:00:00 2001 From: Robins Tharakan Date: Wed, 27 Sep 2017 19:13:25 +1000 Subject: [PATCH 119/149] Update psql/README_cockroachdb with critical dependency not supported in CockroachDB --- src/bin/psql/README_cockroachdb.md | 65 +++++++++++++++++------------- 1 file changed, 37 insertions(+), 28 deletions(-) diff --git a/src/bin/psql/README_cockroachdb.md b/src/bin/psql/README_cockroachdb.md index 51b961961ba7a..b50ba37c97ef2 100644 --- a/src/bin/psql/README_cockroachdb.md +++ b/src/bin/psql/README_cockroachdb.md @@ -1,3 +1,11 @@ +**Note**: Tab Completion requires quote_ident() that is NOT yet supported by the CockroachDB engine. There is a pending request towards this ( cockroachdb/cockroach#15006 ) however, without this tab-completion is not going to work in its current state. + +We could remove quote_ident() requirement from psql, but this would be a pretty big security issue, considering that this is effectively SQL injection in the making. + +So by design, tab-completion is going to silently fail (no errors), and intentionally so, until CockroachDB starts support quote_ident(). + +============================================= + This document shows the current state of psql's auto-complete support for CockroachDB. Reference: https://www.cockroachlabs.com/docs/stable/sql-statements.html @@ -7,39 +15,40 @@ See Support level of all Postgres forks: [Here](https://github.com/robins/postgr SQL Commands | Supported State :----------- |:--------------: -ADD COLUMN | Native -ADD CONSTRAINT | Untested -ALTER COLUMN | Native -ALTER TABLE | Native -ALTER VIEW | Native -CREATE DATABASE | Native -CREATE INDEX | Untested -CREATE TABLE | Untested -CREATE TABLE AS | Untested -CREATE VIEW | Untested -DROP COLUMN | Native -DROP CONSTRAINT | Untested -DROP DATABASE | Untested -DROP INDEX | Untested -DROP TABLE | Native -DROP VIEW | Native -RENAME COLUMN | Native -RENAME DATABASE | Untested -RENAME INDEX | Untested -RENAME TABLE | Native -SHOW COLUMNS | Untested -SHOW CONSTRAINTS | Untested -SHOW CREATE TABLE | Untested -SHOW CREATE VIEW | Untested -SHOW DATABASES | Untested -SHOW INDEX | Untested -SHOW TABLES | Untested +ADD COLUMN | Dependency Unsupported +ADD CONSTRAINT | Dependency Unsupported +ALTER COLUMN | Dependency Unsupported +ALTER TABLE | Dependency Unsupported +ALTER VIEW | Dependency Unsupported +CREATE DATABASE | Dependency Unsupported +CREATE INDEX | Dependency Unsupported +CREATE TABLE | Dependency Unsupported +CREATE TABLE AS | Dependency Unsupported +CREATE VIEW | Dependency Unsupported +DROP COLUMN | Dependency Unsupported +DROP CONSTRAINT | Dependency Unsupported +DROP DATABASE | Dependency Unsupported +DROP INDEX | Dependency Unsupported +DROP TABLE | Dependency Unsupported +DROP VIEW | Dependency Unsupported +RENAME COLUMN | Dependency Unsupported +RENAME DATABASE | Dependency Unsupported +RENAME INDEX | Dependency Unsupported +RENAME TABLE | Dependency Unsupported +SHOW COLUMNS | Dependency Unsupported +SHOW CONSTRAINTS | Dependency Unsupported +SHOW CREATE TABLE | Dependency Unsupported +SHOW CREATE VIEW | Dependency Unsupported +SHOW DATABASES | Dependency Unsupported +SHOW INDEX | Dependency Unsupported +SHOW TABLES | Dependency Unsupported Support Legend | Description :-------------:| ----------- Native | All combinations supported by Native psql -Unsupported | Not (yet)supported +Unsupported | Not (yet) supported by PsqlForks +Dependency Unsupported | Engine does not support a dependency Partial | Only some combinations supported **Yes** | Supported by (only) this branch (not Native) Complete | All combinations supported \ No newline at end of file From 677c17e9732b571f8eec5b11f93f82f71e274190 Mon Sep 17 00:00:00 2001 From: Robins Tharakan Date: Wed, 27 Sep 2017 19:15:31 +1000 Subject: [PATCH 120/149] Update psql/README_cockroachdb with the URL (instead of brief) of the issue in cockroachDB rgithub repo --- src/bin/psql/README_cockroachdb.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/bin/psql/README_cockroachdb.md b/src/bin/psql/README_cockroachdb.md index b50ba37c97ef2..ec80d959ca18d 100644 --- a/src/bin/psql/README_cockroachdb.md +++ b/src/bin/psql/README_cockroachdb.md @@ -1,4 +1,4 @@ -**Note**: Tab Completion requires quote_ident() that is NOT yet supported by the CockroachDB engine. There is a pending request towards this ( cockroachdb/cockroach#15006 ) however, without this tab-completion is not going to work in its current state. +**Note**: Tab Completion requires quote_ident() that is NOT yet supported by the CockroachDB engine. There is a pending request towards this ( https://github.com/cockroachdb/cockroach/issues/15006 ) however, without this tab-completion is not going to work in its current state. We could remove quote_ident() requirement from psql, but this would be a pretty big security issue, considering that this is effectively SQL injection in the making. From d036f50ac49d518227cfbdd39ce5b9f962d982ba Mon Sep 17 00:00:00 2001 From: Robins Tharakan Date: Wed, 27 Sep 2017 21:00:21 +1000 Subject: [PATCH 121/149] Compile / add the psql's metacommands into psql/README_cockroachdb's list --- src/bin/psql/README_cockroachdb.md | 98 ++++++++++++++++++++++++++++++ 1 file changed, 98 insertions(+) diff --git a/src/bin/psql/README_cockroachdb.md b/src/bin/psql/README_cockroachdb.md index ec80d959ca18d..380d1c8644331 100644 --- a/src/bin/psql/README_cockroachdb.md +++ b/src/bin/psql/README_cockroachdb.md @@ -13,6 +13,104 @@ Reference: https://www.cockroachlabs.com/docs/stable/sql-statements.html See Support level of all Postgres forks: [Here](https://github.com/robins/postgres/blob/forks/src/bin/psql/README.md) +psql Commands | Supported State +:------------ |:--------------: +\a | Untested +\C | Untested +\cd | Untested +\conninfo | Untested +\copy | Untested +\copyright | Untested +\crosstabview | Untested +\dS[+] | Untested +\da[S] | Untested +\dA[+] | Untested +\db[+] | Untested +\dc[S+] | Untested +\dC[+] | Untested +\dd[S] | Untested +\dD[S+] | Untested +\ddp | Untested +\dE[S+] | Untested +\di[S+] | Untested +\dm[S+] | Untested +\ds[S+] | Untested +\dt[S+] | Untested +\dv[S+] | Untested +\des[+] | Untested +\det[+] | Untested +\deu[+] | Untested +\dew[+] | Untested +\df[antwS+] | Untested +\dF[+] | Untested +\dFd[+] | Untested +\dFp[+] | Untested +\dFt[+] | Untested +\dg[S+] | Untested +\dl | Untested +\dL[S+] | Untested +\dn[S+] | Untested +\do[S+] | Untested +\dO[S+] | Untested +\dp | Untested +\drds | Untested +\dRp[+] | Untested +\dRs[+] | Untested +\dT[S+] | Untested +\du[S+] | Untested +\dx[+] | Untested +\dy[+] | Untested +\e | Untested +\echo | Untested +\ef | Untested +\encoding | Untested +\errverbose | Untested +\ev | Untested +\f | Untested +\g | Untested +\gdesc | Untested +\gexec | Untested +\gset | Untested +\gx | Untested +\h | Untested +\H | Untested +\i | Untested +\if | Untested +\elif | Untested +\else | Untested +\endif | Untested +\ir | Untested +\l[+] | Untested +\lo_export | Untested +\lo_import | Untested +\lo_list | Untested +\lo_unlink | Untested +\o | Untested +\p | Untested +\password | Untested +\prompt | Untested +\pset | Untested +\q | Untested +\qecho | Untested +\r | Untested +\s | Untested +\set | Untested +\setenv | Untested +\sf[+] | Untested +\sv[+] | Untested +\t | Untested +\T | Untested +\timing | Untested +\unset | Untested +\w | Untested +\watch | Untested +\x | Untested +\z | Untested +\! | Untested +\? | Untested +\; | Untested + + SQL Commands | Supported State :----------- |:--------------: ADD COLUMN | Dependency Unsupported From 0296db4b95608090a993f6dfdf31cffb614e8ddd Mon Sep 17 00:00:00 2001 From: Robins Tharakan Date: Thu, 28 Sep 2017 02:08:48 +1000 Subject: [PATCH 122/149] Update psql/README_cockcroach with psql's metacommands and their compatibility with psqlForks --- src/bin/psql/README_cockroachdb.md | 60 +++++++++++++++--------------- 1 file changed, 30 insertions(+), 30 deletions(-) diff --git a/src/bin/psql/README_cockroachdb.md b/src/bin/psql/README_cockroachdb.md index 380d1c8644331..965f505c73b23 100644 --- a/src/bin/psql/README_cockroachdb.md +++ b/src/bin/psql/README_cockroachdb.md @@ -13,18 +13,18 @@ Reference: https://www.cockroachlabs.com/docs/stable/sql-statements.html See Support level of all Postgres forks: [Here](https://github.com/robins/postgres/blob/forks/src/bin/psql/README.md) -psql Commands | Supported State -:------------ |:--------------: -\a | Untested -\C | Untested +psql Commands | Supported State | Description +:------------ |:--------------:|:------------ +\a | Native +\C | Native \cd | Untested -\conninfo | Untested +\conninfo | Native \copy | Untested -\copyright | Untested +\copyright | Native \crosstabview | Untested -\dS[+] | Untested +\dS[+] | **Yes** \da[S] | Untested -\dA[+] | Untested +\dA[+] | Dependency Unsupported|The server (version 9.5) does not support access methods. \db[+] | Untested \dc[S+] | Untested \dC[+] | Untested @@ -32,24 +32,24 @@ psql Commands | Supported State \dD[S+] | Untested \ddp | Untested \dE[S+] | Untested -\di[S+] | Untested +\di[S+] | **Yes** \dm[S+] | Untested \ds[S+] | Untested -\dt[S+] | Untested -\dv[S+] | Untested +\dt[S+] | **Yes** +\dv[S+] | **Yes** \des[+] | Untested \det[+] | Untested \deu[+] | Untested \dew[+] | Untested -\df[antwS+] | Untested -\dF[+] | Untested +\df[antwS+] | Unsupported +\dF[+] | Unsupported \dFd[+] | Untested \dFp[+] | Untested \dFt[+] | Untested -\dg[S+] | Untested -\dl | Untested +\dg[S+] | Unsupported +\dl | Unsupported \dL[S+] | Untested -\dn[S+] | Untested +\dn[S+] | **Yes** \do[S+] | Untested \dO[S+] | Untested \dp | Untested @@ -57,41 +57,41 @@ psql Commands | Supported State \dRp[+] | Untested \dRs[+] | Untested \dT[S+] | Untested -\du[S+] | Untested -\dx[+] | Untested -\dy[+] | Untested -\e | Untested -\echo | Untested -\ef | Untested +\du[S+] | Partial +\dx[+] | Unsupported +\dy[+] | Unsupported +\e | Native +\echo | Native +\ef | Unsupported \encoding | Untested \errverbose | Untested -\ev | Untested -\f | Untested +\ev | Unsupported +\f | Native \g | Untested \gdesc | Untested \gexec | Untested \gset | Untested \gx | Untested -\h | Untested -\H | Untested +\h | Native +\H | Native \i | Untested \if | Untested \elif | Untested \else | Untested \endif | Untested \ir | Untested -\l[+] | Untested +\l[+] | **Yes** \lo_export | Untested \lo_import | Untested \lo_list | Untested \lo_unlink | Untested -\o | Untested +\o | Native \p | Untested \password | Untested \prompt | Untested \pset | Untested -\q | Untested -\qecho | Untested +\q | Native +\qecho | Native \r | Untested \s | Untested \set | Untested From 05c0488c49a7c175c2ed59846afbf08edd78df14 Mon Sep 17 00:00:00 2001 From: Robins Tharakan Date: Thu, 28 Sep 2017 09:41:20 +1000 Subject: [PATCH 123/149] Update psql/README_cockcroach with psql's metacommands and their compatibility with psqlForks --- src/bin/psql/README_cockroachdb.md | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/bin/psql/README_cockroachdb.md b/src/bin/psql/README_cockroachdb.md index 965f505c73b23..aa8f3480c6d41 100644 --- a/src/bin/psql/README_cockroachdb.md +++ b/src/bin/psql/README_cockroachdb.md @@ -96,19 +96,19 @@ psql Commands | Supported State | Description \s | Untested \set | Untested \setenv | Untested -\sf[+] | Untested -\sv[+] | Untested -\t | Untested -\T | Untested +\sf[+] | Dependency Unsupported +\sv[+] | Unsupported +\t | Native +\T | Native \timing | Untested \unset | Untested \w | Untested -\watch | Untested -\x | Untested +\watch | Native +\x | Native \z | Untested \! | Untested \? | Untested -\; | Untested +\; | Native SQL Commands | Supported State From a450276cfc55ffd47ae65b14c6d12d167eeb0e5e Mon Sep 17 00:00:00 2001 From: Robins Tharakan Date: Thu, 28 Sep 2017 16:29:40 +1000 Subject: [PATCH 124/149] Update psql/README_cockroachdb with current status. Remove all 'Unsupported' and move over either to 'Dependency Unsupported' with reasons, or ensure that they work --- src/bin/psql/README_cockroachdb.md | 20 ++++---- src/bin/psql/command.c | 7 ++- src/bin/psql/describe.c | 73 ++++++++++++++++-------------- 3 files changed, 54 insertions(+), 46 deletions(-) diff --git a/src/bin/psql/README_cockroachdb.md b/src/bin/psql/README_cockroachdb.md index aa8f3480c6d41..adc4e1f20b2b5 100644 --- a/src/bin/psql/README_cockroachdb.md +++ b/src/bin/psql/README_cockroachdb.md @@ -2,7 +2,7 @@ We could remove quote_ident() requirement from psql, but this would be a pretty big security issue, considering that this is effectively SQL injection in the making. -So by design, tab-completion is going to silently fail (no errors), and intentionally so, until CockroachDB starts support quote_ident(). +So by design, tab-completion is going to silently fail (no errors), and intentionally so, until CockroachDB starts support quote_ident(). This issue is logged in #21 and would be addressed if the Dependency is resolved. ============================================= @@ -41,13 +41,13 @@ psql Commands | Supported State | Description \det[+] | Untested \deu[+] | Untested \dew[+] | Untested -\df[antwS+] | Unsupported -\dF[+] | Unsupported +\df[antwS+] | **Yes** +\dF[+] | Dependency Unsupported \dFd[+] | Untested \dFp[+] | Untested \dFt[+] | Untested -\dg[S+] | Unsupported -\dl | Unsupported +\dg[S+] | **Yes** +\dl | Dependency Unsupported | pg_largeobject_metadata not supported in Engine \dL[S+] | Untested \dn[S+] | **Yes** \do[S+] | Untested @@ -58,14 +58,14 @@ psql Commands | Supported State | Description \dRs[+] | Untested \dT[S+] | Untested \du[S+] | Partial -\dx[+] | Unsupported -\dy[+] | Unsupported +\dx[+] | Dependency Unsupported | Issue cockroachdb/cockroach#13436 didn't add supported for OID column +\dy[+] | Dependency Unsupported | Functions not supported in Engine \e | Native \echo | Native -\ef | Unsupported +\ef | Dependency Unsupported | Functions not supported in Engine \encoding | Untested \errverbose | Untested -\ev | Unsupported +\ev | Dependency Unsupported | Issue #24 is logged towards this \f | Native \g | Untested \gdesc | Untested @@ -97,7 +97,7 @@ psql Commands | Supported State | Description \set | Untested \setenv | Untested \sf[+] | Dependency Unsupported -\sv[+] | Unsupported +\sv[+] | Dependency Unsupported | Issue #24 is logged towards this \t | Native \T | Native \timing | Untested diff --git a/src/bin/psql/command.c b/src/bin/psql/command.c index 4b29163e97c70..a3dc77c717890 100644 --- a/src/bin/psql/command.c +++ b/src/bin/psql/command.c @@ -46,7 +46,9 @@ /* Macro to easily identify which Engine (type) are we speaking to Any change here should probably be replicated elsewhere since #define for various SQLs (in this script) employ there own string-compare */ + #define IS_COCKROACHDB (strncmp(pset.sengine, "cockroachdb", 11) == 0) #define IS_REDSHIFT (strncmp(pset.sengine, "redshift", 8) == 0) + #define IS_PIPELINEDB (strncmp(pset.sengine, "pipelinedb", 10) == 0) /* * Editable database object types. @@ -4532,7 +4534,10 @@ lookup_object_oid(EditableObjectType obj_type, const char *desc, */ appendPQExpBufferStr(query, "SELECT "); appendStringLiteralConn(query, desc, pset.db); - appendPQExpBuffer(query, "::pg_catalog.regclass::pg_catalog.oid"); + if (IS_COCKROACHDB) + appendPQExpBuffer(query, "::regclass::OID"); + else + appendPQExpBuffer(query, "::pg_catalog.regclass::pg_catalog.oid"); break; } diff --git a/src/bin/psql/describe.c b/src/bin/psql/describe.c index e977a1c979f1b..b07f46b0a733d 100644 --- a/src/bin/psql/describe.c +++ b/src/bin/psql/describe.c @@ -349,8 +349,23 @@ describeFunctions(const char *functypes, const char *pattern, bool verbose, bool " p.proname as \"%s\",\n", gettext_noop("Schema"), gettext_noop("Name")); - - if (pset.sversion >= 80400) + if (IS_COCKROACHDB) + appendPQExpBuffer(&buf, + " 'Not Supported Yet' as \"%s\",\n" + " 'Not Supported Yet' as \"%s\",\n" + " CASE\n" + " WHEN p.proisagg THEN '%s'\n" + " WHEN p.proiswindow THEN '%s'\n" + " ELSE '%s'\n" + " END as \"%s\"", + gettext_noop("Result data type"), + gettext_noop("Argument data types"), + /* translator: "agg" is short for "aggregate" */ + gettext_noop("agg"), + gettext_noop("window"), + gettext_noop("normal"), + gettext_noop("Type")); + else if (pset.sversion >= 80400) appendPQExpBuffer(&buf, " pg_catalog.pg_get_function_result(p.oid) as \"%s\",\n" " pg_catalog.pg_get_function_arguments(p.oid) as \"%s\",\n" @@ -463,11 +478,11 @@ describeFunctions(const char *functypes, const char *pattern, bool verbose, bool gettext_noop("Security")); appendPQExpBufferStr(&buf, ",\n "); printACLColumn(&buf, "p.proacl"); + if (!IS_COCKROACHDB) + appendPQExpBuffer(&buf, ",\n l.lanname as \"%s\"", gettext_noop("Language")); appendPQExpBuffer(&buf, - ",\n l.lanname as \"%s\"" - ",\n p.prosrc as \"%s\"" + ",\n p.prosrc as \"%s\"" ",\n pg_catalog.obj_description(p.oid, 'pg_proc') as \"%s\"", - gettext_noop("Language"), gettext_noop("Source code"), gettext_noop("Description")); } @@ -476,7 +491,7 @@ describeFunctions(const char *functypes, const char *pattern, bool verbose, bool "\nFROM pg_catalog.pg_proc p" "\n LEFT JOIN pg_catalog.pg_namespace n ON n.oid = p.pronamespace\n"); - if (verbose) + if (verbose && !IS_COCKROACHDB) appendPQExpBufferStr(&buf, " LEFT JOIN pg_catalog.pg_language l ON l.oid = p.prolang\n"); @@ -498,7 +513,7 @@ describeFunctions(const char *functypes, const char *pattern, bool verbose, bool } appendPQExpBufferStr(&buf, "NOT p.proisagg\n"); } - if (!showTrigger) + if (!showTrigger && !IS_COCKROACHDB) { if (have_where) appendPQExpBufferStr(&buf, " AND "); @@ -533,7 +548,7 @@ describeFunctions(const char *functypes, const char *pattern, bool verbose, bool appendPQExpBufferStr(&buf, "p.proisagg\n"); needs_or = true; } - if (showTrigger) + if (showTrigger && !IS_COCKROACHDB) { if (needs_or) appendPQExpBufferStr(&buf, " OR "); @@ -548,6 +563,8 @@ describeFunctions(const char *functypes, const char *pattern, bool verbose, bool appendPQExpBufferStr(&buf, "p.proiswindow\n"); needs_or = true; } + if (!needs_or) + appendPQExpBufferStr(&buf, " TRUE "); appendPQExpBufferStr(&buf, " )\n"); } @@ -3246,31 +3263,16 @@ describeRoles(const char *pattern, bool verbose, bool showSystem) initPQExpBuffer(&buf); - if (IS_COCKROACHDB) - { - printfPQExpBuffer(&buf, - "SELECT r.rolname, r.rolsuper, r.rolinherit,\n" - " r.rolcreaterole, r.rolcreatedb, r.rolcanlogin,\n" - " r.rolconnlimit, r.rolvaliduntil,\n" - " 'NA' as memberof ,\n" - " pg_catalog.shobj_description(r.oid, 'pg_authid') AS description,\n" - " 'f' AS rolreplication, \n" - " 'f' AS rolbypassrls\n" - "FROM pg_catalog.pg_roles r\n"); - - if (!showSystem && !pattern) - appendPQExpBufferStr(&buf, "WHERE r.rolname !~ '^pg_'\n"); - - processSQLNamePattern(pset.db, &buf, pattern, false, false, - NULL, "r.rolname", NULL, NULL); - } - else if (pset.sversion >= 80100) + if (pset.sversion >= 80100) { printfPQExpBuffer(&buf, "SELECT r.rolname, r.rolsuper, r.rolinherit,\n" " r.rolcreaterole, r.rolcreatedb, r.rolcanlogin,\n" - " r.rolconnlimit, r.rolvaliduntil,\n" - " ARRAY(SELECT b.rolname\n" + " r.rolconnlimit, r.rolvaliduntil\n"); + if (IS_COCKROACHDB) + appendPQExpBufferStr(&buf, " ,'NA' as memberof \n"); + else + appendPQExpBufferStr(&buf, " ,ARRAY(SELECT b.rolname\n" " FROM pg_catalog.pg_auth_members m\n" " JOIN pg_catalog.pg_roles b ON (m.roleid = b.oid)\n" " WHERE m.member = r.oid) as memberof"); @@ -3280,12 +3282,12 @@ describeRoles(const char *pattern, bool verbose, bool showSystem) appendPQExpBufferStr(&buf, "\n, pg_catalog.shobj_description(r.oid, 'pg_authid') AS description"); ncols++; } - if (pset.sversion >= 90100) + if (pset.sversion >= 90100 && !IS_COCKROACHDB) { appendPQExpBufferStr(&buf, "\n, r.rolreplication"); } - if (pset.sversion >= 90500) + if (pset.sversion >= 90500 && !IS_COCKROACHDB) { appendPQExpBufferStr(&buf, "\n, r.rolbypassrls"); } @@ -3352,11 +3354,11 @@ describeRoles(const char *pattern, bool verbose, bool showSystem) if (strcmp(PQgetvalue(res, i, 5), "t") != 0) add_role_attribute(&buf, _("Cannot login")); - if (pset.sversion >= 90100) + if (pset.sversion >= 90100 && !IS_COCKROACHDB) if (strcmp(PQgetvalue(res, i, (verbose ? 10 : 9)), "t") == 0) add_role_attribute(&buf, _("Replication")); - if (pset.sversion >= 90500) + if (pset.sversion >= 90500 && !IS_COCKROACHDB) if (strcmp(PQgetvalue(res, i, (verbose ? 11 : 10)), "t") == 0) add_role_attribute(&buf, _("Bypass RLS")); @@ -5227,11 +5229,12 @@ listExtensions(const char *pattern) "FROM pg_catalog.pg_extension e " "LEFT JOIN pg_catalog.pg_namespace n ON n.oid = e.extnamespace " "LEFT JOIN pg_catalog.pg_description c ON c.objoid = e.oid " - "AND c.classoid = 'pg_catalog.pg_extension'::pg_catalog.regclass\n", + "AND c.classoid = 'pg_catalog.pg_extension'::%s\n", gettext_noop("Name"), gettext_noop("Version"), gettext_noop("Schema"), - gettext_noop("Description")); + gettext_noop("Description"), + gettext_noop((IS_COCKROACHDB?"regclass":"pg_catalog.regclass"))); processSQLNamePattern(pset.db, &buf, pattern, false, false, From 2ecdc7c481753bae23f0061e97c1e4dcdb2131a4 Mon Sep 17 00:00:00 2001 From: Robins Tharakan Date: Thu, 28 Sep 2017 16:51:08 +1000 Subject: [PATCH 125/149] Change License to GPLv3 --- COPYRIGHT | 23 -- LICENSE.txt | 674 ++++++++++++++++++++++++++++++++++++++++++++++++++++ README | 28 ++- 3 files changed, 689 insertions(+), 36 deletions(-) delete mode 100644 COPYRIGHT create mode 100644 LICENSE.txt diff --git a/COPYRIGHT b/COPYRIGHT deleted file mode 100644 index c320eccac08f7..0000000000000 --- a/COPYRIGHT +++ /dev/null @@ -1,23 +0,0 @@ -PostgreSQL Database Management System -(formerly known as Postgres, then as Postgres95) - -Portions Copyright (c) 1996-2017, PostgreSQL Global Development Group - -Portions Copyright (c) 1994, The Regents of the University of California - -Permission to use, copy, modify, and distribute this software and its -documentation for any purpose, without fee, and without a written agreement -is hereby granted, provided that the above copyright notice and this -paragraph and the following two paragraphs appear in all copies. - -IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR -DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING -LOST PROFITS, ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS -DOCUMENTATION, EVEN IF THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE -POSSIBILITY OF SUCH DAMAGE. - -THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES, -INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY -AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS -ON AN "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATIONS TO -PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. diff --git a/LICENSE.txt b/LICENSE.txt new file mode 100644 index 0000000000000..9cecc1d4669ee --- /dev/null +++ b/LICENSE.txt @@ -0,0 +1,674 @@ + GNU GENERAL PUBLIC LICENSE + Version 3, 29 June 2007 + + Copyright (C) 2007 Free Software Foundation, Inc. + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + + Preamble + + The GNU General Public License is a free, copyleft license for +software and other kinds of works. + + The licenses for most software and other practical works are designed +to take away your freedom to share and change the works. By contrast, +the GNU General Public License is intended to guarantee your freedom to +share and change all versions of a program--to make sure it remains free +software for all its users. We, the Free Software Foundation, use the +GNU General Public License for most of our software; it applies also to +any other work released this way by its authors. You can apply it to +your programs, too. + + When we speak of free software, we are referring to freedom, not +price. Our General Public Licenses are designed to make sure that you +have the freedom to distribute copies of free software (and charge for +them if you wish), that you receive source code or can get it if you +want it, that you can change the software or use pieces of it in new +free programs, and that you know you can do these things. + + To protect your rights, we need to prevent others from denying you +these rights or asking you to surrender the rights. Therefore, you have +certain responsibilities if you distribute copies of the software, or if +you modify it: responsibilities to respect the freedom of others. + + For example, if you distribute copies of such a program, whether +gratis or for a fee, you must pass on to the recipients the same +freedoms that you received. You must make sure that they, too, receive +or can get the source code. And you must show them these terms so they +know their rights. + + Developers that use the GNU GPL protect your rights with two steps: +(1) assert copyright on the software, and (2) offer you this License +giving you legal permission to copy, distribute and/or modify it. + + For the developers' and authors' protection, the GPL clearly explains +that there is no warranty for this free software. For both users' and +authors' sake, the GPL requires that modified versions be marked as +changed, so that their problems will not be attributed erroneously to +authors of previous versions. + + Some devices are designed to deny users access to install or run +modified versions of the software inside them, although the manufacturer +can do so. This is fundamentally incompatible with the aim of +protecting users' freedom to change the software. The systematic +pattern of such abuse occurs in the area of products for individuals to +use, which is precisely where it is most unacceptable. Therefore, we +have designed this version of the GPL to prohibit the practice for those +products. If such problems arise substantially in other domains, we +stand ready to extend this provision to those domains in future versions +of the GPL, as needed to protect the freedom of users. + + Finally, every program is threatened constantly by software patents. +States should not allow patents to restrict development and use of +software on general-purpose computers, but in those that do, we wish to +avoid the special danger that patents applied to a free program could +make it effectively proprietary. To prevent this, the GPL assures that +patents cannot be used to render the program non-free. + + The precise terms and conditions for copying, distribution and +modification follow. + + TERMS AND CONDITIONS + + 0. Definitions. + + "This License" refers to version 3 of the GNU General Public License. + + "Copyright" also means copyright-like laws that apply to other kinds of +works, such as semiconductor masks. + + "The Program" refers to any copyrightable work licensed under this +License. Each licensee is addressed as "you". "Licensees" and +"recipients" may be individuals or organizations. + + To "modify" a work means to copy from or adapt all or part of the work +in a fashion requiring copyright permission, other than the making of an +exact copy. The resulting work is called a "modified version" of the +earlier work or a work "based on" the earlier work. + + A "covered work" means either the unmodified Program or a work based +on the Program. + + To "propagate" a work means to do anything with it that, without +permission, would make you directly or secondarily liable for +infringement under applicable copyright law, except executing it on a +computer or modifying a private copy. Propagation includes copying, +distribution (with or without modification), making available to the +public, and in some countries other activities as well. + + To "convey" a work means any kind of propagation that enables other +parties to make or receive copies. Mere interaction with a user through +a computer network, with no transfer of a copy, is not conveying. + + An interactive user interface displays "Appropriate Legal Notices" +to the extent that it includes a convenient and prominently visible +feature that (1) displays an appropriate copyright notice, and (2) +tells the user that there is no warranty for the work (except to the +extent that warranties are provided), that licensees may convey the +work under this License, and how to view a copy of this License. If +the interface presents a list of user commands or options, such as a +menu, a prominent item in the list meets this criterion. + + 1. Source Code. + + The "source code" for a work means the preferred form of the work +for making modifications to it. "Object code" means any non-source +form of a work. + + A "Standard Interface" means an interface that either is an official +standard defined by a recognized standards body, or, in the case of +interfaces specified for a particular programming language, one that +is widely used among developers working in that language. + + The "System Libraries" of an executable work include anything, other +than the work as a whole, that (a) is included in the normal form of +packaging a Major Component, but which is not part of that Major +Component, and (b) serves only to enable use of the work with that +Major Component, or to implement a Standard Interface for which an +implementation is available to the public in source code form. A +"Major Component", in this context, means a major essential component +(kernel, window system, and so on) of the specific operating system +(if any) on which the executable work runs, or a compiler used to +produce the work, or an object code interpreter used to run it. + + The "Corresponding Source" for a work in object code form means all +the source code needed to generate, install, and (for an executable +work) run the object code and to modify the work, including scripts to +control those activities. However, it does not include the work's +System Libraries, or general-purpose tools or generally available free +programs which are used unmodified in performing those activities but +which are not part of the work. For example, Corresponding Source +includes interface definition files associated with source files for +the work, and the source code for shared libraries and dynamically +linked subprograms that the work is specifically designed to require, +such as by intimate data communication or control flow between those +subprograms and other parts of the work. + + The Corresponding Source need not include anything that users +can regenerate automatically from other parts of the Corresponding +Source. + + The Corresponding Source for a work in source code form is that +same work. + + 2. Basic Permissions. + + All rights granted under this License are granted for the term of +copyright on the Program, and are irrevocable provided the stated +conditions are met. This License explicitly affirms your unlimited +permission to run the unmodified Program. The output from running a +covered work is covered by this License only if the output, given its +content, constitutes a covered work. This License acknowledges your +rights of fair use or other equivalent, as provided by copyright law. + + You may make, run and propagate covered works that you do not +convey, without conditions so long as your license otherwise remains +in force. You may convey covered works to others for the sole purpose +of having them make modifications exclusively for you, or provide you +with facilities for running those works, provided that you comply with +the terms of this License in conveying all material for which you do +not control copyright. Those thus making or running the covered works +for you must do so exclusively on your behalf, under your direction +and control, on terms that prohibit them from making any copies of +your copyrighted material outside their relationship with you. + + Conveying under any other circumstances is permitted solely under +the conditions stated below. Sublicensing is not allowed; section 10 +makes it unnecessary. + + 3. Protecting Users' Legal Rights From Anti-Circumvention Law. + + No covered work shall be deemed part of an effective technological +measure under any applicable law fulfilling obligations under article +11 of the WIPO copyright treaty adopted on 20 December 1996, or +similar laws prohibiting or restricting circumvention of such +measures. + + When you convey a covered work, you waive any legal power to forbid +circumvention of technological measures to the extent such circumvention +is effected by exercising rights under this License with respect to +the covered work, and you disclaim any intention to limit operation or +modification of the work as a means of enforcing, against the work's +users, your or third parties' legal rights to forbid circumvention of +technological measures. + + 4. Conveying Verbatim Copies. + + You may convey verbatim copies of the Program's source code as you +receive it, in any medium, provided that you conspicuously and +appropriately publish on each copy an appropriate copyright notice; +keep intact all notices stating that this License and any +non-permissive terms added in accord with section 7 apply to the code; +keep intact all notices of the absence of any warranty; and give all +recipients a copy of this License along with the Program. + + You may charge any price or no price for each copy that you convey, +and you may offer support or warranty protection for a fee. + + 5. Conveying Modified Source Versions. + + You may convey a work based on the Program, or the modifications to +produce it from the Program, in the form of source code under the +terms of section 4, provided that you also meet all of these conditions: + + a) The work must carry prominent notices stating that you modified + it, and giving a relevant date. + + b) The work must carry prominent notices stating that it is + released under this License and any conditions added under section + 7. This requirement modifies the requirement in section 4 to + "keep intact all notices". + + c) You must license the entire work, as a whole, under this + License to anyone who comes into possession of a copy. This + License will therefore apply, along with any applicable section 7 + additional terms, to the whole of the work, and all its parts, + regardless of how they are packaged. This License gives no + permission to license the work in any other way, but it does not + invalidate such permission if you have separately received it. + + d) If the work has interactive user interfaces, each must display + Appropriate Legal Notices; however, if the Program has interactive + interfaces that do not display Appropriate Legal Notices, your + work need not make them do so. + + A compilation of a covered work with other separate and independent +works, which are not by their nature extensions of the covered work, +and which are not combined with it such as to form a larger program, +in or on a volume of a storage or distribution medium, is called an +"aggregate" if the compilation and its resulting copyright are not +used to limit the access or legal rights of the compilation's users +beyond what the individual works permit. Inclusion of a covered work +in an aggregate does not cause this License to apply to the other +parts of the aggregate. + + 6. Conveying Non-Source Forms. + + You may convey a covered work in object code form under the terms +of sections 4 and 5, provided that you also convey the +machine-readable Corresponding Source under the terms of this License, +in one of these ways: + + a) Convey the object code in, or embodied in, a physical product + (including a physical distribution medium), accompanied by the + Corresponding Source fixed on a durable physical medium + customarily used for software interchange. + + b) Convey the object code in, or embodied in, a physical product + (including a physical distribution medium), accompanied by a + written offer, valid for at least three years and valid for as + long as you offer spare parts or customer support for that product + model, to give anyone who possesses the object code either (1) a + copy of the Corresponding Source for all the software in the + product that is covered by this License, on a durable physical + medium customarily used for software interchange, for a price no + more than your reasonable cost of physically performing this + conveying of source, or (2) access to copy the + Corresponding Source from a network server at no charge. + + c) Convey individual copies of the object code with a copy of the + written offer to provide the Corresponding Source. This + alternative is allowed only occasionally and noncommercially, and + only if you received the object code with such an offer, in accord + with subsection 6b. + + d) Convey the object code by offering access from a designated + place (gratis or for a charge), and offer equivalent access to the + Corresponding Source in the same way through the same place at no + further charge. You need not require recipients to copy the + Corresponding Source along with the object code. If the place to + copy the object code is a network server, the Corresponding Source + may be on a different server (operated by you or a third party) + that supports equivalent copying facilities, provided you maintain + clear directions next to the object code saying where to find the + Corresponding Source. Regardless of what server hosts the + Corresponding Source, you remain obligated to ensure that it is + available for as long as needed to satisfy these requirements. + + e) Convey the object code using peer-to-peer transmission, provided + you inform other peers where the object code and Corresponding + Source of the work are being offered to the general public at no + charge under subsection 6d. + + A separable portion of the object code, whose source code is excluded +from the Corresponding Source as a System Library, need not be +included in conveying the object code work. + + A "User Product" is either (1) a "consumer product", which means any +tangible personal property which is normally used for personal, family, +or household purposes, or (2) anything designed or sold for incorporation +into a dwelling. In determining whether a product is a consumer product, +doubtful cases shall be resolved in favor of coverage. For a particular +product received by a particular user, "normally used" refers to a +typical or common use of that class of product, regardless of the status +of the particular user or of the way in which the particular user +actually uses, or expects or is expected to use, the product. A product +is a consumer product regardless of whether the product has substantial +commercial, industrial or non-consumer uses, unless such uses represent +the only significant mode of use of the product. + + "Installation Information" for a User Product means any methods, +procedures, authorization keys, or other information required to install +and execute modified versions of a covered work in that User Product from +a modified version of its Corresponding Source. The information must +suffice to ensure that the continued functioning of the modified object +code is in no case prevented or interfered with solely because +modification has been made. + + If you convey an object code work under this section in, or with, or +specifically for use in, a User Product, and the conveying occurs as +part of a transaction in which the right of possession and use of the +User Product is transferred to the recipient in perpetuity or for a +fixed term (regardless of how the transaction is characterized), the +Corresponding Source conveyed under this section must be accompanied +by the Installation Information. But this requirement does not apply +if neither you nor any third party retains the ability to install +modified object code on the User Product (for example, the work has +been installed in ROM). + + The requirement to provide Installation Information does not include a +requirement to continue to provide support service, warranty, or updates +for a work that has been modified or installed by the recipient, or for +the User Product in which it has been modified or installed. Access to a +network may be denied when the modification itself materially and +adversely affects the operation of the network or violates the rules and +protocols for communication across the network. + + Corresponding Source conveyed, and Installation Information provided, +in accord with this section must be in a format that is publicly +documented (and with an implementation available to the public in +source code form), and must require no special password or key for +unpacking, reading or copying. + + 7. Additional Terms. + + "Additional permissions" are terms that supplement the terms of this +License by making exceptions from one or more of its conditions. +Additional permissions that are applicable to the entire Program shall +be treated as though they were included in this License, to the extent +that they are valid under applicable law. If additional permissions +apply only to part of the Program, that part may be used separately +under those permissions, but the entire Program remains governed by +this License without regard to the additional permissions. + + When you convey a copy of a covered work, you may at your option +remove any additional permissions from that copy, or from any part of +it. (Additional permissions may be written to require their own +removal in certain cases when you modify the work.) You may place +additional permissions on material, added by you to a covered work, +for which you have or can give appropriate copyright permission. + + Notwithstanding any other provision of this License, for material you +add to a covered work, you may (if authorized by the copyright holders of +that material) supplement the terms of this License with terms: + + a) Disclaiming warranty or limiting liability differently from the + terms of sections 15 and 16 of this License; or + + b) Requiring preservation of specified reasonable legal notices or + author attributions in that material or in the Appropriate Legal + Notices displayed by works containing it; or + + c) Prohibiting misrepresentation of the origin of that material, or + requiring that modified versions of such material be marked in + reasonable ways as different from the original version; or + + d) Limiting the use for publicity purposes of names of licensors or + authors of the material; or + + e) Declining to grant rights under trademark law for use of some + trade names, trademarks, or service marks; or + + f) Requiring indemnification of licensors and authors of that + material by anyone who conveys the material (or modified versions of + it) with contractual assumptions of liability to the recipient, for + any liability that these contractual assumptions directly impose on + those licensors and authors. + + All other non-permissive additional terms are considered "further +restrictions" within the meaning of section 10. If the Program as you +received it, or any part of it, contains a notice stating that it is +governed by this License along with a term that is a further +restriction, you may remove that term. If a license document contains +a further restriction but permits relicensing or conveying under this +License, you may add to a covered work material governed by the terms +of that license document, provided that the further restriction does +not survive such relicensing or conveying. + + If you add terms to a covered work in accord with this section, you +must place, in the relevant source files, a statement of the +additional terms that apply to those files, or a notice indicating +where to find the applicable terms. + + Additional terms, permissive or non-permissive, may be stated in the +form of a separately written license, or stated as exceptions; +the above requirements apply either way. + + 8. Termination. + + You may not propagate or modify a covered work except as expressly +provided under this License. Any attempt otherwise to propagate or +modify it is void, and will automatically terminate your rights under +this License (including any patent licenses granted under the third +paragraph of section 11). + + However, if you cease all violation of this License, then your +license from a particular copyright holder is reinstated (a) +provisionally, unless and until the copyright holder explicitly and +finally terminates your license, and (b) permanently, if the copyright +holder fails to notify you of the violation by some reasonable means +prior to 60 days after the cessation. + + Moreover, your license from a particular copyright holder is +reinstated permanently if the copyright holder notifies you of the +violation by some reasonable means, this is the first time you have +received notice of violation of this License (for any work) from that +copyright holder, and you cure the violation prior to 30 days after +your receipt of the notice. + + Termination of your rights under this section does not terminate the +licenses of parties who have received copies or rights from you under +this License. If your rights have been terminated and not permanently +reinstated, you do not qualify to receive new licenses for the same +material under section 10. + + 9. Acceptance Not Required for Having Copies. + + You are not required to accept this License in order to receive or +run a copy of the Program. Ancillary propagation of a covered work +occurring solely as a consequence of using peer-to-peer transmission +to receive a copy likewise does not require acceptance. However, +nothing other than this License grants you permission to propagate or +modify any covered work. These actions infringe copyright if you do +not accept this License. Therefore, by modifying or propagating a +covered work, you indicate your acceptance of this License to do so. + + 10. Automatic Licensing of Downstream Recipients. + + Each time you convey a covered work, the recipient automatically +receives a license from the original licensors, to run, modify and +propagate that work, subject to this License. You are not responsible +for enforcing compliance by third parties with this License. + + An "entity transaction" is a transaction transferring control of an +organization, or substantially all assets of one, or subdividing an +organization, or merging organizations. If propagation of a covered +work results from an entity transaction, each party to that +transaction who receives a copy of the work also receives whatever +licenses to the work the party's predecessor in interest had or could +give under the previous paragraph, plus a right to possession of the +Corresponding Source of the work from the predecessor in interest, if +the predecessor has it or can get it with reasonable efforts. + + You may not impose any further restrictions on the exercise of the +rights granted or affirmed under this License. For example, you may +not impose a license fee, royalty, or other charge for exercise of +rights granted under this License, and you may not initiate litigation +(including a cross-claim or counterclaim in a lawsuit) alleging that +any patent claim is infringed by making, using, selling, offering for +sale, or importing the Program or any portion of it. + + 11. Patents. + + A "contributor" is a copyright holder who authorizes use under this +License of the Program or a work on which the Program is based. The +work thus licensed is called the contributor's "contributor version". + + A contributor's "essential patent claims" are all patent claims +owned or controlled by the contributor, whether already acquired or +hereafter acquired, that would be infringed by some manner, permitted +by this License, of making, using, or selling its contributor version, +but do not include claims that would be infringed only as a +consequence of further modification of the contributor version. For +purposes of this definition, "control" includes the right to grant +patent sublicenses in a manner consistent with the requirements of +this License. + + Each contributor grants you a non-exclusive, worldwide, royalty-free +patent license under the contributor's essential patent claims, to +make, use, sell, offer for sale, import and otherwise run, modify and +propagate the contents of its contributor version. + + In the following three paragraphs, a "patent license" is any express +agreement or commitment, however denominated, not to enforce a patent +(such as an express permission to practice a patent or covenant not to +sue for patent infringement). To "grant" such a patent license to a +party means to make such an agreement or commitment not to enforce a +patent against the party. + + If you convey a covered work, knowingly relying on a patent license, +and the Corresponding Source of the work is not available for anyone +to copy, free of charge and under the terms of this License, through a +publicly available network server or other readily accessible means, +then you must either (1) cause the Corresponding Source to be so +available, or (2) arrange to deprive yourself of the benefit of the +patent license for this particular work, or (3) arrange, in a manner +consistent with the requirements of this License, to extend the patent +license to downstream recipients. "Knowingly relying" means you have +actual knowledge that, but for the patent license, your conveying the +covered work in a country, or your recipient's use of the covered work +in a country, would infringe one or more identifiable patents in that +country that you have reason to believe are valid. + + If, pursuant to or in connection with a single transaction or +arrangement, you convey, or propagate by procuring conveyance of, a +covered work, and grant a patent license to some of the parties +receiving the covered work authorizing them to use, propagate, modify +or convey a specific copy of the covered work, then the patent license +you grant is automatically extended to all recipients of the covered +work and works based on it. + + A patent license is "discriminatory" if it does not include within +the scope of its coverage, prohibits the exercise of, or is +conditioned on the non-exercise of one or more of the rights that are +specifically granted under this License. You may not convey a covered +work if you are a party to an arrangement with a third party that is +in the business of distributing software, under which you make payment +to the third party based on the extent of your activity of conveying +the work, and under which the third party grants, to any of the +parties who would receive the covered work from you, a discriminatory +patent license (a) in connection with copies of the covered work +conveyed by you (or copies made from those copies), or (b) primarily +for and in connection with specific products or compilations that +contain the covered work, unless you entered into that arrangement, +or that patent license was granted, prior to 28 March 2007. + + Nothing in this License shall be construed as excluding or limiting +any implied license or other defenses to infringement that may +otherwise be available to you under applicable patent law. + + 12. No Surrender of Others' Freedom. + + If conditions are imposed on you (whether by court order, agreement or +otherwise) that contradict the conditions of this License, they do not +excuse you from the conditions of this License. If you cannot convey a +covered work so as to satisfy simultaneously your obligations under this +License and any other pertinent obligations, then as a consequence you may +not convey it at all. For example, if you agree to terms that obligate you +to collect a royalty for further conveying from those to whom you convey +the Program, the only way you could satisfy both those terms and this +License would be to refrain entirely from conveying the Program. + + 13. Use with the GNU Affero General Public License. + + Notwithstanding any other provision of this License, you have +permission to link or combine any covered work with a work licensed +under version 3 of the GNU Affero General Public License into a single +combined work, and to convey the resulting work. The terms of this +License will continue to apply to the part which is the covered work, +but the special requirements of the GNU Affero General Public License, +section 13, concerning interaction through a network will apply to the +combination as such. + + 14. Revised Versions of this License. + + The Free Software Foundation may publish revised and/or new versions of +the GNU General Public License from time to time. Such new versions will +be similar in spirit to the present version, but may differ in detail to +address new problems or concerns. + + Each version is given a distinguishing version number. If the +Program specifies that a certain numbered version of the GNU General +Public License "or any later version" applies to it, you have the +option of following the terms and conditions either of that numbered +version or of any later version published by the Free Software +Foundation. If the Program does not specify a version number of the +GNU General Public License, you may choose any version ever published +by the Free Software Foundation. + + If the Program specifies that a proxy can decide which future +versions of the GNU General Public License can be used, that proxy's +public statement of acceptance of a version permanently authorizes you +to choose that version for the Program. + + Later license versions may give you additional or different +permissions. However, no additional obligations are imposed on any +author or copyright holder as a result of your choosing to follow a +later version. + + 15. Disclaimer of Warranty. + + THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY +APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT +HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY +OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, +THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR +PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM +IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF +ALL NECESSARY SERVICING, REPAIR OR CORRECTION. + + 16. Limitation of Liability. + + IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING +WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS +THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY +GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE +USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF +DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD +PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), +EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF +SUCH DAMAGES. + + 17. Interpretation of Sections 15 and 16. + + If the disclaimer of warranty and limitation of liability provided +above cannot be given local legal effect according to their terms, +reviewing courts shall apply local law that most closely approximates +an absolute waiver of all civil liability in connection with the +Program, unless a warranty or assumption of liability accompanies a +copy of the Program in return for a fee. + + END OF TERMS AND CONDITIONS + + How to Apply These Terms to Your New Programs + + If you develop a new program, and you want it to be of the greatest +possible use to the public, the best way to achieve this is to make it +free software which everyone can redistribute and change under these terms. + + To do so, attach the following notices to the program. It is safest +to attach them to the start of each source file to most effectively +state the exclusion of warranty; and each file should have at least +the "copyright" line and a pointer to where the full notice is found. + + {one line to give the program's name and a brief idea of what it does.} + Copyright (C) {year} {name of author} + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . + +Also add information on how to contact you by electronic and paper mail. + + If the program does terminal interaction, make it output a short +notice like this when it starts in an interactive mode: + + {project} Copyright (C) {year} {fullname} + This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. + This is free software, and you are welcome to redistribute it + under certain conditions; type `show c' for details. + +The hypothetical commands `show w' and `show c' should show the appropriate +parts of the General Public License. Of course, your program's commands +might be different; for a GUI interface, you would use an "about box". + + You should also get your employer (if you work as a programmer) or school, +if any, to sign a "copyright disclaimer" for the program, if necessary. +For more information on this, and how to apply and follow the GNU GPL, see +. + + The GNU General Public License does not permit incorporating your program +into proprietary programs. If your program is a subroutine library, you +may consider it more useful to permit linking proprietary applications with +the library. If this is what you want to do, use the GNU Lesser General +Public License instead of this License. But first, please read +. diff --git a/README b/README index a564b7decd415..84d368fa4ff64 100644 --- a/README +++ b/README @@ -1,29 +1,31 @@ -NOTE: This branch is maintained as a personal effort and is not sponsored / supported by my Employer or any other third-party. This is not officially supported / endorsed by Amazon in any way. +**NOTE**: This branch is maintained as a personal effort and is not +sponsored / supported by my Employer or any other third-party. This +is not officially supported / endorsed by Amazon in any way. -PostgreSQL Database Management System -===================================== +PsqlForks +========= This directory contains the source code distribution of the PostgreSQL -database management system. +database management system, along with modifications required for +PsqlForks. -PostgreSQL is an advanced object-relational database management system -that supports an extended subset of the SQL standard, including -transactions, foreign keys, subqueries, triggers, user-defined types -and functions. This distribution also contains C language bindings. +PsqlForks is a psql variant that tries to accommodate as many PostgrSQL +variants (or software that speak the PostgreSQL protocol to an extent). -PostgreSQL has many language interfaces, many of which are listed here: +The current status of support for different PostgreSQL derived databases +can be seen here: - https://www.postgresql.org/download +https://github.com/robins/postgres/blob/forks/src/bin/psql/README.md See the file INSTALL for instructions on how to build and install PostgreSQL. That file also lists supported operating systems and hardware platforms and contains information regarding any other software packages that are required to build or run the PostgreSQL system. Copyright and license information can be found in the -file COPYRIGHT. A comprehensive documentation set is included in this +file LICENSE.txt. A comprehensive documentation set is included in this distribution; it can be read as described in the installation instructions. The latest version of this software may be obtained at -https://www.postgresql.org/download/. For more information look at our -web site located at https://www.postgresql.org/. +https://github.com/robins/postgres/tree/forks . For more information look +at the web site located at http://www.thatguyfromdelhi.com/search/label/psql . From d8d9f1f8be6241c71f8239a92189bc870c64901c Mon Sep 17 00:00:00 2001 From: Robins Tharakan Date: Thu, 28 Sep 2017 19:49:33 +1000 Subject: [PATCH 126/149] Reorder the Engine Support list. Also correct the GUC that cockroachdb supports. was recognizing engine wrongly earlier --- src/bin/psql/command.c | 2 +- src/bin/psql/startup.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/bin/psql/command.c b/src/bin/psql/command.c index a3dc77c717890..c8ec10950987f 100644 --- a/src/bin/psql/command.c +++ b/src/bin/psql/command.c @@ -3248,7 +3248,7 @@ SyncVariables(void) server_engine = "redshift"; else { - guctype = get_guctype("max_index_keys"); + guctype = get_guctype("node_id"); if (guctype != NULL) server_engine = "cockroachdb"; else diff --git a/src/bin/psql/startup.c b/src/bin/psql/startup.c index c80bbcc93cdb4..34cdede3e89b5 100644 --- a/src/bin/psql/startup.c +++ b/src/bin/psql/startup.c @@ -775,7 +775,7 @@ process_psqlrc_file(char *filename) static void showVersion(void) { - puts("psql (PostgreSQL / Amazon Redshift / CockroachDB / PipelineDB) " PG_VERSION); + puts("psql (CockroachDB / PipelineDB / PostgreSQL / Amazon Redshift) " PG_VERSION); } From 29f94b62aabd6c0e8a446a34b0af1e41209c5121 Mon Sep 17 00:00:00 2001 From: Robins Tharakan Date: Fri, 29 Sep 2017 03:48:23 +1000 Subject: [PATCH 127/149] Minor jugglery in command.c to recognize PgBouncer as the server. This should in principle be the first step to do anything customized in psql, once we know we are talking to PgBouncer (and not Postgres). First step in Issua #18 --- src/bin/psql/command.c | 30 ++++++++++++++++++------------ 1 file changed, 18 insertions(+), 12 deletions(-) diff --git a/src/bin/psql/command.c b/src/bin/psql/command.c index c8ec10950987f..4a8eb29879aa8 100644 --- a/src/bin/psql/command.c +++ b/src/bin/psql/command.c @@ -3237,6 +3237,16 @@ SyncVariables(void) pset.popt.topt.encoding = pset.encoding; pset.sversion = PQserverVersion(pset.db); + /* this bit should match connection_warnings(): */ + /* Try to get full text form of version, might include "devel" etc */ + server_version = PQparameterStatus(pset.db, "server_version"); + /* Otherwise fall back on pset.sversion */ + if (!server_version) + { + formatPGVersionNumber(pset.sversion, true, vbuf, sizeof(vbuf)); + server_version = vbuf; + } + /* Check whether a given GUC exists */ guctype = get_guctype("continuous_queries_enabled"); if (guctype != NULL) @@ -3252,30 +3262,26 @@ SyncVariables(void) if (guctype != NULL) server_engine = "cockroachdb"; else - server_engine = "postgres"; + { + if (strstr(server_version, "bouncer") != NULL) + server_engine = "pgbouncer"; + else + server_engine = "postgres"; + } } } - pset.sengine = server_engine; - if (guctype) free(guctype); + pset.sengine = server_engine; + SetVariable(pset.vars, "DBNAME", PQdb(pset.db)); SetVariable(pset.vars, "USER", PQuser(pset.db)); SetVariable(pset.vars, "HOST", PQhost(pset.db)); SetVariable(pset.vars, "PORT", PQport(pset.db)); SetVariable(pset.vars, "ENCODING", pg_encoding_to_char(pset.encoding)); - /* this bit should match connection_warnings(): */ - /* Try to get full text form of version, might include "devel" etc */ - server_version = PQparameterStatus(pset.db, "server_version"); - /* Otherwise fall back on pset.sversion */ - if (!server_version) - { - formatPGVersionNumber(pset.sversion, true, vbuf, sizeof(vbuf)); - server_version = vbuf; - } SetVariable(pset.vars, "SERVER_VERSION_NAME", server_version); SetVariable(pset.vars, "SERVER_ENGINE", server_engine); From a1c88143a1d7d663072d196a7e25f3e81e02d627 Mon Sep 17 00:00:00 2001 From: Robins Tharakan Date: Fri, 29 Sep 2017 03:56:55 +1000 Subject: [PATCH 128/149] Update psql/README with recently added basic support for PgBouncer --- README | 3 ++- src/bin/psql/README.md | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/README b/README index 84d368fa4ff64..9907b69bdb3f7 100644 --- a/README +++ b/README @@ -1,6 +1,7 @@ **NOTE**: This branch is maintained as a personal effort and is not sponsored / supported by my Employer or any other third-party. This -is not officially supported / endorsed by Amazon in any way. +is not officially supported / endorsed by any Commercial interest in +any way. PsqlForks ========= diff --git a/src/bin/psql/README.md b/src/bin/psql/README.md index 34e71d4c79e12..8c6eac0c9f872 100644 --- a/src/bin/psql/README.md +++ b/src/bin/psql/README.md @@ -1,4 +1,4 @@ -This documents the current state of psql's auto-complete support for various Postgres Variants. +This documents the current state of PsqlFork's support for various server side software that speaks (to an extent) Postgres. **NOTE**: This branch is maintained as a personal effort and is not sponsored / supported by my Employer or any other third-party. This is not officially supported / endorsed by any Commercial interest in any way. @@ -7,3 +7,4 @@ Postgres Forks | Support Level | Description Amazon Redshift | [Status](https://github.com/robins/postgres/blob/forks/src/bin/psql/README_redshift.md) | Amazon Redshift was spin off of PostgreSQL (version 8.0.2) CockroachDB | [Status](https://github.com/robins/postgres/blob/forks/src/bin/psql/README_cockroachdb.md) | Compatible with PostgreSQL (version 9.5.0) PipelineDB | [Status](https://github.com/robins/postgres/blob/forks/src/bin/psql/README_pipelinedb.md) | Compatible with PostgreSQL (version 9.5) +PgBouncer | [Basic Engine Comprehension] \ No newline at end of file From 138ef75c5a8f8ef3f6ed14573dbf92a003ce602f Mon Sep 17 00:00:00 2001 From: Robins Tharakan Date: Fri, 29 Sep 2017 18:28:46 +1000 Subject: [PATCH 129/149] Add IS_PGBOUNCER macro for easier identification to be used down the line --- src/bin/psql/command.c | 5 +++-- src/bin/psql/describe.c | 5 +++-- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/src/bin/psql/command.c b/src/bin/psql/command.c index 4a8eb29879aa8..e260441bd92a9 100644 --- a/src/bin/psql/command.c +++ b/src/bin/psql/command.c @@ -43,12 +43,13 @@ #include "settings.h" #include "variables.h" -/* Macro to easily identify which Engine (type) are we speaking to +/* Macro to easily identify which Engine (type) are we speaking with Any change here should probably be replicated elsewhere since #define for various SQLs (in this script) employ there own string-compare */ #define IS_COCKROACHDB (strncmp(pset.sengine, "cockroachdb", 11) == 0) - #define IS_REDSHIFT (strncmp(pset.sengine, "redshift", 8) == 0) + #define IS_PGBOUNCER (strncmp(pset.sengine, "pgbouncer", 9) == 0) #define IS_PIPELINEDB (strncmp(pset.sengine, "pipelinedb", 10) == 0) + #define IS_REDSHIFT (strncmp(pset.sengine, "redshift", 8) == 0) /* * Editable database object types. diff --git a/src/bin/psql/describe.c b/src/bin/psql/describe.c index b07f46b0a733d..26ef1f2e94c03 100644 --- a/src/bin/psql/describe.c +++ b/src/bin/psql/describe.c @@ -26,10 +26,11 @@ #include "settings.h" #include "variables.h" -/* Flag to check if server is a Redshift Engine */ +/* Macro to easily identify which Engine (type) are we speaking with */ #define IS_COCKROACHDB (strncmp(pset.sengine, "cockroachdb", 11) == 0) -#define IS_REDSHIFT (strncmp(pset.sengine, "redshift", 8) == 0) +#define IS_PGBOUNCER (strncmp(pset.sengine, "pgbouncer", 9) == 0) #define IS_PIPELINEDB (strncmp(pset.sengine, "pipelinedb", 10) == 0) +#define IS_REDSHIFT (strncmp(pset.sengine, "redshift", 8) == 0) static bool describeOneTableDetails(const char *schemaname, const char *relationname, From 723973e19c739be6176528ccc869cf7bfd0b7021 Mon Sep 17 00:00:00 2001 From: Robins Tharakan Date: Fri, 29 Sep 2017 18:38:59 +1000 Subject: [PATCH 130/149] Add IS_PGBOUNCER macro. Do not process \* commands in PgBouncer. Update Startup message with PgBouncer support --- src/bin/psql/command.c | 8 ++++++++ src/bin/psql/startup.c | 2 +- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/src/bin/psql/command.c b/src/bin/psql/command.c index e260441bd92a9..b83e470a201ca 100644 --- a/src/bin/psql/command.c +++ b/src/bin/psql/command.c @@ -291,6 +291,14 @@ exec_command(const char *cmd, backslashResult status; bool active_branch = conditional_active(cstack); + /* PgBouncer supports no \* commands and so we skip if we're speaking with PgBouncer */ + if (IS_PGBOUNCER) + { + psql_error("\\%s command ignored - PgBouncer doesn't support ( \\* ) backslash commands.\n", + cmd); + return PSQL_CMD_ERROR; + } + /* * In interactive mode, warn when we're ignoring a command within a false * \if-branch. But we continue on, so as to parse and discard the right diff --git a/src/bin/psql/startup.c b/src/bin/psql/startup.c index 34cdede3e89b5..d6a413d4ea0a6 100644 --- a/src/bin/psql/startup.c +++ b/src/bin/psql/startup.c @@ -775,7 +775,7 @@ process_psqlrc_file(char *filename) static void showVersion(void) { - puts("psql (CockroachDB / PipelineDB / PostgreSQL / Amazon Redshift) " PG_VERSION); + puts("psql (CockroachDB / PgBouncer / PipelineDB / PostgreSQL / Amazon Redshift) " PG_VERSION); } From 5f040c5accd626a83136272fb23a5aa7a435bad4 Mon Sep 17 00:00:00 2001 From: Robins Tharakan Date: Fri, 29 Sep 2017 20:17:40 +1000 Subject: [PATCH 131/149] Minor PgBouncer related update in psql/README --- src/bin/psql/README.md | 2 +- src/bin/psql/command.c | 6 +++--- src/bin/psql/describe.c | 6 +++--- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/bin/psql/README.md b/src/bin/psql/README.md index 8c6eac0c9f872..c53a127c025f5 100644 --- a/src/bin/psql/README.md +++ b/src/bin/psql/README.md @@ -7,4 +7,4 @@ Postgres Forks | Support Level | Description Amazon Redshift | [Status](https://github.com/robins/postgres/blob/forks/src/bin/psql/README_redshift.md) | Amazon Redshift was spin off of PostgreSQL (version 8.0.2) CockroachDB | [Status](https://github.com/robins/postgres/blob/forks/src/bin/psql/README_cockroachdb.md) | Compatible with PostgreSQL (version 9.5.0) PipelineDB | [Status](https://github.com/robins/postgres/blob/forks/src/bin/psql/README_pipelinedb.md) | Compatible with PostgreSQL (version 9.5) -PgBouncer | [Basic Engine Comprehension] \ No newline at end of file +PgBouncer | Supported | \* (Backslash) is not supported, and gives appropriate error message \ No newline at end of file diff --git a/src/bin/psql/command.c b/src/bin/psql/command.c index b83e470a201ca..5ecaf1d8b4e91 100644 --- a/src/bin/psql/command.c +++ b/src/bin/psql/command.c @@ -47,9 +47,9 @@ Any change here should probably be replicated elsewhere since #define for various SQLs (in this script) employ there own string-compare */ #define IS_COCKROACHDB (strncmp(pset.sengine, "cockroachdb", 11) == 0) - #define IS_PGBOUNCER (strncmp(pset.sengine, "pgbouncer", 9) == 0) - #define IS_PIPELINEDB (strncmp(pset.sengine, "pipelinedb", 10) == 0) - #define IS_REDSHIFT (strncmp(pset.sengine, "redshift", 8) == 0) + #define IS_PGBOUNCER (strncmp(pset.sengine, "pgbouncer", 9) == 0) + #define IS_PIPELINEDB (strncmp(pset.sengine, "pipelinedb", 10) == 0) + #define IS_REDSHIFT (strncmp(pset.sengine, "redshift", 8) == 0) /* * Editable database object types. diff --git a/src/bin/psql/describe.c b/src/bin/psql/describe.c index 26ef1f2e94c03..3a8cee1efa944 100644 --- a/src/bin/psql/describe.c +++ b/src/bin/psql/describe.c @@ -28,9 +28,9 @@ /* Macro to easily identify which Engine (type) are we speaking with */ #define IS_COCKROACHDB (strncmp(pset.sengine, "cockroachdb", 11) == 0) -#define IS_PGBOUNCER (strncmp(pset.sengine, "pgbouncer", 9) == 0) -#define IS_PIPELINEDB (strncmp(pset.sengine, "pipelinedb", 10) == 0) -#define IS_REDSHIFT (strncmp(pset.sengine, "redshift", 8) == 0) +#define IS_PGBOUNCER (strncmp(pset.sengine, "pgbouncer", 9) == 0) +#define IS_PIPELINEDB (strncmp(pset.sengine, "pipelinedb", 10) == 0) +#define IS_REDSHIFT (strncmp(pset.sengine, "redshift", 8) == 0) static bool describeOneTableDetails(const char *schemaname, const char *relationname, From 7e0dab5d52744abcbe25a34cce60fb64710d3ccc Mon Sep 17 00:00:00 2001 From: Robins Tharakan Date: Sat, 30 Sep 2017 03:12:35 +1000 Subject: [PATCH 132/149] Phew. So finally we're able to execute a command and get the result to be used in the username / password combination. Still WIP though --- src/bin/psql/common.c | 81 ++++++++++++++++++++++++++----------------- 1 file changed, 50 insertions(+), 31 deletions(-) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index 3f1aa657440de..4e34b7e2442ad 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -125,8 +125,6 @@ setQFout(const char *fname) return true; } - - /* * request_password_from_external_source * @@ -136,16 +134,45 @@ setQFout(const char *fname) bool request_password_from_external_source(char **username, char **password) { - // FILE *fp; - // char path[1035]; - - /* Open the command for reading. * / - fp = popen("aws redshift get-cluster-credentials --db-user redshift2 --cluster-identifier redshift2", "r"); - if (fp == NULL) { - printf("Failed to run command\n" ); - exit(1); + FILE *fp; + long length; + char linebuffer[1035]; + char filebuffer[2000]; + + strcpy(filebuffer, " "); + /* Open the command for reading. */ + //fp = popen("aws redshift get-cluster-credentials --db-user redshift2 --cluster-identifier redshift2", "r"); + fp = popen("/usr/bin/cat /home/tharar/proj/postgres/src/bin/psql/cluster.txt", "r"); + if (fp == NULL) + { + printf("Failed to run command\n" ); + exit(1); + } + else + { + printf("Able to read file fine\n"); + + /* Read the output a line at a time - output it. */ + while (fgets(linebuffer, sizeof(linebuffer)-1, fp) != NULL) { + strncat(filebuffer, linebuffer, sizeof(linebuffer)); + printf("Filebuffer: %s\n", filebuffer); } - */ + + // fseek (fp, 0, SEEK_END); + // length = ftell (fp); + // fseek (fp, 0, SEEK_SET); + // filebuffer = malloc (length); + // if (filebuffer) + // { + // fread (filebuffer, 1, length, fp); + // } + pclose (fp); + } + + // /* Read the output a line at a time - output it. */ + // while (fgets(path, sizeof(path)-1, fp) != NULL) { + // printf("%s", path); + // } int i; int r; @@ -159,7 +186,7 @@ request_password_from_external_source(char **username, char **password) printf("\n===Enter Function===\n"); jsmn_init(&p); - r = jsmn_parse(&p, JSON_STRING, strlen(JSON_STRING), t, sizeof(t)/sizeof(t[0])); + r = jsmn_parse(&p, filebuffer, strlen(filebuffer), t, sizeof(t)/sizeof(t[0])); if (r < 0) { printf("Failed to parse JSON: %d\n", r); return false; @@ -173,39 +200,39 @@ request_password_from_external_source(char **username, char **password) } /* - buffer = (char *) pg_malloc(maxlength + 1); - while (fgets(buffer, maxlength + 1, infile) != NULL && n < nlines) - result[n++] = pg_strdup(buffer); + filebuffer = (char *) pg_malloc(maxlength + 1); + while (fgets(filebuffer, maxlength + 1, infile) != NULL && n < nlines) + result[n++] = pg_strdup(filebuffer); */ /* Loop over all keys of the root object */ for (i = 1; i < r; i++) { //printf("\n0000000000 r=%d i=%d : ", r, i); - if (jsoneq(JSON_STRING, &t[i], "DbUser") == 0) { + if (jsoneq(filebuffer, &t[i], "DbUser") == 0) { //printf("\n1111111"); new_username = pg_malloc(t[i+1].end-t[i+1].start + 2); - StrNCpy(new_username, JSON_STRING + t[i+1].start, t[i+1].end-t[i+1].start + 1); + StrNCpy(new_username, filebuffer + t[i+1].start, t[i+1].end-t[i+1].start + 1); //printf("- Username: %s\n", new_username); found_username = true; i++; - } else if (jsoneq(JSON_STRING, &t[i], "DbPassword") == 0) { + } else if (jsoneq(filebuffer, &t[i], "DbPassword") == 0) { //printf("\n22222222"); new_password = pg_malloc(t[i+1].end-t[i+1].start + 2); - StrNCpy(new_password, JSON_STRING + t[i+1].start, t[i+1].end-t[i+1].start + 1); + StrNCpy(new_password, filebuffer + t[i+1].start, t[i+1].end-t[i+1].start + 1); //printf("- Password: %s", new_password); found_password = true; i++; - } else if (jsoneq(JSON_STRING, &t[i], "Expiration") == 0) { + } else if (jsoneq(filebuffer, &t[i], "Expiration") == 0) { //printf("\n333333b"); printf("- Expiration: %.*s\n", t[i+1].end-t[i+1].start, - JSON_STRING + t[i+1].start); + filebuffer + t[i+1].start); i++; //printf("\n333333e"); } else { //printf("\n44444444"); printf("Unexpected key: %.*s\n", t[i].end-t[i].start, - JSON_STRING + t[i].start); - } + filebuffer + t[i].start); + } } //printf("fetched all params"); if (found_username && found_password) @@ -220,14 +247,6 @@ request_password_from_external_source(char **username, char **password) return true; } return false; - - /* Read the output a line at a time - output it. * / - while (fgets(path, sizeof(path)-1, fp) != NULL) { - printf("%s", path); - } - - / * close * / - pclose(fp); */ } From c0e2797e1e4e8e40914f57ed0835c89499c94fa8 Mon Sep 17 00:00:00 2001 From: Robins Tharakan Date: Tue, 3 Oct 2017 04:39:41 +1100 Subject: [PATCH 133/149] Lots of debug information, half-way through another day's working, just committing in, probably need to revert some once debugging is done. --- src/bin/psql/startup.c | 20 +++++++++++---- src/interfaces/libpq/fe-connect.c | 41 ++++++++++++++++++++++--------- 2 files changed, 44 insertions(+), 17 deletions(-) diff --git a/src/bin/psql/startup.c b/src/bin/psql/startup.c index 894e55e3ed28a..792b877c57b06 100644 --- a/src/bin/psql/startup.c +++ b/src/bin/psql/startup.c @@ -218,8 +218,8 @@ main(int argc, char *argv[]) printf("Entry1\n"); if (request_password_from_external_source(&(options.username), &new_password)) { - printf("Username: %s, Password: %s\n", options.username, new_password); sprintf(password, "%s", new_password); + printf("Username: '%s', Password: '%s'\n", options.username, password); have_password = true; } else @@ -266,18 +266,20 @@ main(int argc, char *argv[]) keywords[7] = NULL; values[7] = NULL; - printf("269: Username: %s, Password: %s, PasswordLen: %d, Database: %s \n", values[2], values[3], strlen(values[3]), values[4]); + printf("Username: '%s', Password: '%s', Host: '%s', Port: '%s', PasswordLen: %d, Database: '%s' \n", values[2], values[3], values[0], values[1], strlen(values[3]), values[4]); new_pass = false; pset.db = PQconnectdbParams(keywords, values, true); free(keywords); free(values); - + printf("Exit1\n"); + if (PQstatus(pset.db) == CONNECTION_BAD && PQconnectionNeedsPassword(pset.db) && !have_password && pset.getPassword != TRI_NO) { + printf("n1"); if (pset.credential_source == AWS_IAM_REDSHIFT) { PQfinish(pset.db); @@ -297,23 +299,31 @@ main(int argc, char *argv[]) } else { + printf("n2"); + PQfinish(pset.db); simple_prompt(password_prompt, password, sizeof(password), false); have_password = true; new_pass = true; } } + printf("n3"); + if (new_pass) + printf("new_pass"); } while (new_pass); - + printf("n4"); + free(password_prompt); if (PQstatus(pset.db) == CONNECTION_BAD) { + printf("n5"); fprintf(stderr, "%s: %s", pset.progname, PQerrorMessage(pset.db)); PQfinish(pset.db); exit(EXIT_BADCONN); } - + printf("n6"); + setup_cancel_handler(); PQsetNoticeProcessor(pset.db, NoticeProcessor, NULL); diff --git a/src/interfaces/libpq/fe-connect.c b/src/interfaces/libpq/fe-connect.c index c580d91135aad..3763d61b54884 100644 --- a/src/interfaces/libpq/fe-connect.c +++ b/src/interfaces/libpq/fe-connect.c @@ -531,7 +531,7 @@ PQconnectdbParams(const char *const *keywords, int expand_dbname) { PGconn *conn = PQconnectStartParams(keywords, values, expand_dbname); - +printf("n10\n"); if (conn && conn->status != CONNECTION_BAD) (void) connectDBComplete(conn); @@ -1837,15 +1837,18 @@ connectDBComplete(PGconn *conn) PostgresPollingStatusType flag = PGRES_POLLING_WRITING; time_t finish_time = ((time_t) -1); int timeout = 0; - +printf("n1--1\n"); if (conn == NULL || conn->status == CONNECTION_BAD) - return 0; - + {return 0;printf("n110\n"); + } + printf("n1101\n"); + /* * Set up a time limit, if connect_timeout isn't zero. */ if (conn->connect_timeout != NULL) { + printf("n1102\n"); timeout = atoi(conn->connect_timeout); if (timeout > 0) { @@ -1853,12 +1856,14 @@ connectDBComplete(PGconn *conn) * Rounding could cause connection to fail; need at least 2 secs */ if (timeout < 2) - timeout = 2; + timeout = 2; printf("n1103\n"); + /* calculate the finish time based on start + timeout */ finish_time = time(NULL) + timeout; } } - + printf("n111\n"); + for (;;) { int ret = 0; @@ -1870,7 +1875,8 @@ connectDBComplete(PGconn *conn) switch (flag) { case PGRES_POLLING_OK: - + printf("n112\n"); + /* * Reset stored error messages since we now have a working * connection @@ -1899,9 +1905,11 @@ connectDBComplete(PGconn *conn) default: /* Just in case we failed to set it in PQconnectPoll */ conn->status = CONNECTION_BAD; + printf("n113\n"); return 0; } - + printf("n114\n"); + if (ret == 1) /* connect_timeout elapsed */ { /* @@ -1914,7 +1922,8 @@ connectDBComplete(PGconn *conn) conn->status = CONNECTION_BAD; return 0; } - + printf("n115\n"); + /* * Attempt connection to the next host, starting the * connect_timeout timer @@ -1925,12 +1934,14 @@ connectDBComplete(PGconn *conn) if (conn->connect_timeout != NULL) finish_time = time(NULL) + timeout; } - + printf("n116\n"); + /* * Now try to advance the state machine. */ flag = PQconnectPoll(conn); } + printf("n11a\n"); } /* @@ -1951,6 +1962,7 @@ saveErrorMessage(PGconn *conn, PQExpBuffer savedMessage) appendPQExpBufferStr(savedMessage, conn->errorMessage.data); resetPQExpBuffer(&conn->errorMessage); + printf("n11b\n"); return true; } @@ -1964,6 +1976,7 @@ restoreErrorMessage(PGconn *conn, PQExpBuffer savedMessage) resetPQExpBuffer(&conn->errorMessage); appendPQExpBufferStr(&conn->errorMessage, savedMessage->data); termPQExpBuffer(savedMessage); + printf("n11c\n"); } /* ---------------- @@ -2056,7 +2069,8 @@ PQconnectPoll(PGconn *conn) goto error_return; } - + printf("n11d\n"); + keep_going: /* We will come back to here until there is * nothing left to do. */ switch (conn->status) @@ -2147,6 +2161,7 @@ PQconnectPoll(PGconn *conn) continue; } #endif /* F_SETFD */ +printf("n11e\n"); if (!IS_AF_UNIX(addr_cur->ai_family)) { @@ -2419,6 +2434,7 @@ PQconnectPoll(PGconn *conn) } } #endif /* HAVE_UNIX_SOCKETS */ +printf("n11g\n"); #ifdef USE_SSL @@ -3386,7 +3402,8 @@ makeEmptyPGconn(void) freePGconn(conn); conn = NULL; } - + printf("n11h\n"); + return conn; } From d04a1ee75e7f4b1c0148a5dd3bd0a0eb3a3c22e0 Mon Sep 17 00:00:00 2001 From: Robins Tharakan Date: Tue, 3 Oct 2017 04:52:38 +1100 Subject: [PATCH 134/149] Add a --no-comments option to pg_dump, which is required for RDS PostgreSQL wherein, restoring from an RDS Postgres database (which already had a pre-installed extension) generates 'COMMENT ON EXTENSION' lines, which can't be used to create another RDS Postgres database, since EXTENSIONS on RDS Postgres are owned by the Superuser, commenting on which is disallowed for a regular user. This patch has been awaiting committing in the Postgres mainline for a while, and thought it to be a good commit to allow for supporting the RDS Postgres branch here. You can check the Commitfest linke here ( https://commitfest.postgresql.org/15/1155/ ) and the mail thread discussion here ( https://www.postgresql.org/message-id/flat/CAEP4nAx22Z4ch74oJGzr5RyyjcyUSbpiFLyeYXX8pehfou92ug@mail.gmail.com#CAEP4nAx22Z4ch74oJGzr5RyyjcyUSbpiFLyeYXX8pehfou92ug@mail.gmail.com ). This patch includes all documentation / tests / code related changes. --- doc/src/sgml/ref/pg_dump.sgml | 9 ++ doc/src/sgml/ref/pg_dumpall.sgml | 9 ++ src/bin/pg_dump/pg_backup.h | 1 + src/bin/pg_dump/pg_dump.c | 19 +++- src/bin/pg_dump/pg_dumpall.c | 9 +- src/bin/pg_dump/t/002_pg_dump.pl | 159 +++++++++++++++++++++++++++++-- 6 files changed, 193 insertions(+), 13 deletions(-) diff --git a/doc/src/sgml/ref/pg_dump.sgml b/doc/src/sgml/ref/pg_dump.sgml index 7ccbee4855c05..ce49bf2507f12 100644 --- a/doc/src/sgml/ref/pg_dump.sgml +++ b/doc/src/sgml/ref/pg_dump.sgml @@ -791,6 +791,15 @@ PostgreSQL documentation + + + + + Do not dump comments. + + + + diff --git a/doc/src/sgml/ref/pg_dumpall.sgml b/doc/src/sgml/ref/pg_dumpall.sgml index 1dba702ad95b2..f9de4b24e73dd 100644 --- a/doc/src/sgml/ref/pg_dumpall.sgml +++ b/doc/src/sgml/ref/pg_dumpall.sgml @@ -345,6 +345,15 @@ PostgreSQL documentation + + + + + Do not dump comments. + + + + diff --git a/src/bin/pg_dump/pg_backup.h b/src/bin/pg_dump/pg_backup.h index ce3100f09d65a..9f08be68dbf5f 100644 --- a/src/bin/pg_dump/pg_backup.h +++ b/src/bin/pg_dump/pg_backup.h @@ -146,6 +146,7 @@ typedef struct _dumpOptions int dump_inserts; int column_inserts; int if_exists; + int no_comments; int no_security_labels; int no_publications; int no_subscriptions; diff --git a/src/bin/pg_dump/pg_dump.c b/src/bin/pg_dump/pg_dump.c index 75f08cd7924a4..9bbb3815df7fb 100644 --- a/src/bin/pg_dump/pg_dump.c +++ b/src/bin/pg_dump/pg_dump.c @@ -353,6 +353,7 @@ main(int argc, char **argv) {"snapshot", required_argument, NULL, 6}, {"strict-names", no_argument, &strict_names, 1}, {"use-set-session-authorization", no_argument, &dopt.use_setsessauth, 1}, + {"no-comments", no_argument, &dopt.no_comments, 1}, {"no-publications", no_argument, &dopt.no_publications, 1}, {"no-security-labels", no_argument, &dopt.no_security_labels, 1}, {"no-synchronized-snapshots", no_argument, &dopt.no_synchronized_snapshots, 1}, @@ -954,6 +955,7 @@ help(const char *progname) printf(_(" --exclude-table-data=TABLE do NOT dump data for the named table(s)\n")); printf(_(" --if-exists use IF EXISTS when dropping objects\n")); printf(_(" --inserts dump data as INSERT commands, rather than COPY\n")); + printf(_(" --no-comments do not dump comments\n")); printf(_(" --no-publications do not dump publications\n")); printf(_(" --no-security-labels do not dump security label assignments\n")); printf(_(" --no-subscriptions do not dump subscriptions\n")); @@ -2783,8 +2785,9 @@ dumpDatabase(Archive *fout) destroyPQExpBuffer(loOutQry); } - /* Dump DB comment if any */ - if (fout->remoteVersion >= 80200) + + /* Dump DB comment if any, and if --no-comments is not supplied */ + if (fout->remoteVersion >= 80200 && !dopt->no_comments) { /* * 8.2 keeps comments on shared objects in a shared table, so we @@ -9145,6 +9148,10 @@ dumpComment(Archive *fout, const char *target, return; } + /* do nothing, if --no-comments is supplied */ + if (dopt->no_comments) + return; + /* Search for comments associated with catalogId, using table */ ncomments = findComments(fout, catalogId.tableoid, catalogId.oid, &comments); @@ -9203,6 +9210,10 @@ dumpTableComment(Archive *fout, TableInfo *tbinfo, if (dopt->dataOnly) return; + /* do nothing, if --no-comments is supplied */ + if (dopt->no_comments) + return; + /* Search for comments associated with relation, using table */ ncomments = findComments(fout, tbinfo->dobj.catId.tableoid, @@ -10850,6 +10861,10 @@ dumpCompositeTypeColComments(Archive *fout, TypeInfo *tyinfo) int i_attname; int i_attnum; + /* do nothing, if --no-comments is supplied */ + if (fout->dopt->no_comments) + return; + query = createPQExpBuffer(); appendPQExpBuffer(query, diff --git a/src/bin/pg_dump/pg_dumpall.c b/src/bin/pg_dump/pg_dumpall.c index 41c5ff89b7587..9ac43295ea7e6 100644 --- a/src/bin/pg_dump/pg_dumpall.c +++ b/src/bin/pg_dump/pg_dumpall.c @@ -74,6 +74,7 @@ static int if_exists = 0; static int inserts = 0; static int no_tablespaces = 0; static int use_setsessauth = 0; +static int no_comments = 0; static int no_publications = 0; static int no_security_labels = 0; static int no_subscriptions = 0; @@ -133,6 +134,7 @@ main(int argc, char *argv[]) {"load-via-partition-root", no_argument, &load_via_partition_root, 1}, {"role", required_argument, NULL, 3}, {"use-set-session-authorization", no_argument, &use_setsessauth, 1}, + {"no-comments", no_argument, &no_comments, 1}, {"no-publications", no_argument, &no_publications, 1}, {"no-role-passwords", no_argument, &no_role_passwords, 1}, {"no-security-labels", no_argument, &no_security_labels, 1}, @@ -399,6 +401,8 @@ main(int argc, char *argv[]) appendPQExpBufferStr(pgdumpopts, " --load-via-partition-root"); if (use_setsessauth) appendPQExpBufferStr(pgdumpopts, " --use-set-session-authorization"); + if (no_comments) + appendPQExpBufferStr(pgdumpopts, " --no-comments"); if (no_publications) appendPQExpBufferStr(pgdumpopts, " --no-publications"); if (no_security_labels) @@ -624,6 +628,7 @@ help(void) printf(_(" --disable-triggers disable triggers during data-only restore\n")); printf(_(" --if-exists use IF EXISTS when dropping objects\n")); printf(_(" --inserts dump data as INSERT commands, rather than COPY\n")); + printf(_(" --no-comments do not dump comments\n")); printf(_(" --no-publications do not dump publications\n")); printf(_(" --no-role-passwords do not dump passwords for roles\n")); printf(_(" --no-security-labels do not dump security label assignments\n")); @@ -932,7 +937,7 @@ dumpRoles(PGconn *conn) appendPQExpBufferStr(buf, ";\n"); - if (!PQgetisnull(res, i, i_rolcomment)) + if (!no_comments && !PQgetisnull(res, i, i_rolcomment)) { appendPQExpBuffer(buf, "COMMENT ON ROLE %s IS ", fmtId(rolename)); appendStringLiteralConn(buf, PQgetvalue(res, i, i_rolcomment), conn); @@ -1238,7 +1243,7 @@ dumpTablespaces(PGconn *conn) exit_nicely(1); } - if (spccomment && strlen(spccomment)) + if (!no_comments && spccomment && strlen(spccomment)) { appendPQExpBuffer(buf, "COMMENT ON TABLESPACE %s IS ", fspcname); appendStringLiteralConn(buf, spccomment, conn); diff --git a/src/bin/pg_dump/t/002_pg_dump.pl b/src/bin/pg_dump/t/002_pg_dump.pl index c492fbdc24d62..fca0baefb8714 100644 --- a/src/bin/pg_dump/t/002_pg_dump.pl +++ b/src/bin/pg_dump/t/002_pg_dump.pl @@ -183,11 +183,21 @@ dump_cmd => [ 'pg_dumpall', '--no-sync', "--file=$tempdir/pg_dumpall_dbprivs.sql", ], }, + pg_dumpall_no_comments => { + dump_cmd => [ + 'pg_dumpall', '--no-sync', + "--file=$tempdir/pg_dumpall_no_comments.sql", + '--no-comments', ], }, no_blobs => { dump_cmd => [ 'pg_dump', '--no-sync', "--file=$tempdir/no_blobs.sql", '-B', 'postgres', ], }, + no_comments => { + dump_cmd => [ + 'pg_dump', '--no-sync', + "--file=$tempdir/no_comments.sql", + '--no-comments', 'postgres', ], }, no_privs => { dump_cmd => [ 'pg_dump', '--no-sync', @@ -321,9 +331,11 @@ exclude_test_table => 1, exclude_test_table_data => 1, no_blobs => 1, + no_comments => 1, no_owner => 1, only_dump_test_schema => 1, pg_dumpall_dbprivs => 1, + pg_dumpall_no_comments => 1, schema_only => 1, section_post_data => 1, test_schema_plus_blobs => 1, @@ -361,8 +373,10 @@ exclude_test_table => 1, exclude_test_table_data => 1, no_blobs => 1, + no_comments => 1, no_owner => 1, pg_dumpall_dbprivs => 1, + pg_dumpall_no_comments => 1, schema_only => 1, section_post_data => 1, with_oids => 1, }, @@ -404,8 +418,10 @@ exclude_test_table => 1, exclude_test_table_data => 1, no_blobs => 1, + no_comments => 1, no_owner => 1, pg_dumpall_dbprivs => 1, + pg_dumpall_no_comments => 1, schema_only => 1, section_post_data => 1, with_oids => 1, }, @@ -432,7 +448,8 @@ like => { pg_dumpall_dbprivs => 1, pg_dumpall_globals => 1, - pg_dumpall_globals_clean => 1, }, + pg_dumpall_globals_clean => 1, + pg_dumpall_no_comments => 1, }, unlike => { binary_upgrade => 1, clean => 1, @@ -445,6 +462,7 @@ exclude_test_table => 1, exclude_test_table_data => 1, no_blobs => 1, + no_comments => 1, no_privs => 1, no_owner => 1, only_dump_test_schema => 1, @@ -623,9 +641,11 @@ exclude_test_table_data => 1, no_blobs => 1, no_owner => 1, + no_comments => 1, no_privs => 1, only_dump_test_schema => 1, pg_dumpall_dbprivs => 1, + pg_dumpall_no_comments => 1, schema_only => 1, section_pre_data => 1, test_schema_plus_blobs => 1, @@ -681,9 +701,11 @@ exclude_dump_test_schema => 1, exclude_test_table => 1, exclude_test_table_data => 1, + no_comments => 1, no_privs => 1, no_blobs => 1, pg_dumpall_dbprivs => 1, + pg_dumpall_no_comments => 1, schema_only => 1, section_post_data => 1, with_oids => 1, }, @@ -714,8 +736,10 @@ exclude_dump_test_schema => 1, exclude_test_table => 1, exclude_test_table_data => 1, + no_comments => 1, no_privs => 1, pg_dumpall_dbprivs => 1, + pg_dumpall_no_comments => 1, section_pre_data => 1, test_schema_plus_blobs => 1, with_oids => 1, }, @@ -819,11 +843,13 @@ defaults => 1, exclude_test_table_data => 1, no_blobs => 1, + no_comments => 1, no_privs => 1, no_owner => 1, only_dump_test_schema => 1, only_dump_test_table => 1, pg_dumpall_dbprivs => 1, + pg_dumpall_no_comments => 1, schema_only => 1, section_pre_data => 1, test_schema_plus_blobs => 1, @@ -854,9 +880,11 @@ exclude_test_table => 1, exclude_test_table_data => 1, no_blobs => 1, + no_comments => 1, no_privs => 1, no_owner => 1, pg_dumpall_dbprivs => 1, + pg_dumpall_no_comments => 1, role => 1, schema_only => 1, section_pre_data => 1, @@ -1057,6 +1085,7 @@ exclude_test_table => 1, exclude_test_table_data => 1, no_blobs => 1, + no_comments => 1, no_privs => 1, no_owner => 1, pg_dumpall_dbprivs => 1, @@ -1068,6 +1097,7 @@ only_dump_test_table => 1, pg_dumpall_globals => 1, pg_dumpall_globals_clean => 1, + pg_dumpall_no_comments => 1, section_post_data => 1, test_schema_plus_blobs => 1, column_inserts => 1, @@ -1125,10 +1155,12 @@ defaults => 1, exclude_test_table_data => 1, no_blobs => 1, + no_comments => 1, no_privs => 1, only_dump_test_schema => 1, only_dump_test_table => 1, pg_dumpall_dbprivs => 1, + pg_dumpall_no_comments => 1, schema_only => 1, section_pre_data => 1, test_schema_plus_blobs => 1, @@ -1421,7 +1453,9 @@ all_runs => 0, # catch-all regexp => qr/^ALTER (?!EVENT TRIGGER|LARGE OBJECT|PUBLICATION|SUBSCRIPTION)(.*) OWNER TO .*;/m, - like => {}, # use more-specific options above + like => { # use more-specific options above + no_comments => 1, + pg_dumpall_no_comments => 1, }, unlike => { column_inserts => 1, data_only => 1, @@ -1435,7 +1469,9 @@ 'ALTER TABLE ... commands' => { all_runs => 0, # catch-all regexp => qr/^ALTER TABLE .* (?!OWNER TO)(.*);/m, - like => {}, # use more-specific options above + like => { # use more-specific options above + no_comments => 1, + pg_dumpall_no_comments => 1, }, unlike => { column_inserts => 1, pg_dumpall_globals => 1, @@ -1459,9 +1495,11 @@ exclude_dump_test_schema => 1, exclude_test_table => 1, exclude_test_table_data => 1, + no_comments => 1, no_privs => 1, no_owner => 1, pg_dumpall_dbprivs => 1, + pg_dumpall_no_comments => 1, section_pre_data => 1, test_schema_plus_blobs => 1, with_oids => 1, }, @@ -1495,8 +1533,10 @@ exclude_test_table => 1, exclude_test_table_data => 1, no_owner => 1, + no_comments => 1, no_privs => 1, pg_dumpall_dbprivs => 1, + pg_dumpall_no_comments => 1, section_data => 1, test_schema_plus_blobs => 1, with_oids => 1, }, @@ -1837,10 +1877,12 @@ with_oids => 1, }, unlike => { no_blobs => 1, + no_comments => 1, only_dump_test_schema => 1, only_dump_test_table => 1, pg_dumpall_globals => 1, pg_dumpall_globals_clean => 1, + pg_dumpall_no_comments => 1, role => 1, schema_only => 1, section_data => 1, @@ -1872,10 +1914,12 @@ unlike => { column_inserts => 1, data_only => 1, + no_comments => 1, only_dump_test_table => 1, only_dump_test_schema => 1, pg_dumpall_globals => 1, pg_dumpall_globals_clean => 1, + pg_dumpall_no_comments => 1, role => 1, section_data => 1, section_pre_data => 1, @@ -1907,10 +1951,12 @@ unlike => { column_inserts => 1, data_only => 1, + no_comments => 1, only_dump_test_table => 1, only_dump_test_schema => 1, pg_dumpall_globals => 1, pg_dumpall_globals_clean => 1, + pg_dumpall_no_comments => 1, role => 1, section_data => 1, section_pre_data => 1, @@ -2185,8 +2231,10 @@ regexp => qr/^COMMENT ON /m, like => {}, # use more-specific options above unlike => { + no_comments => 1, pg_dumpall_globals => 1, pg_dumpall_globals_clean => 1, + pg_dumpall_no_comments => 1, section_data => 1, }, }, 'COPY test_table' => { @@ -2453,7 +2501,9 @@ 'COPY ... commands' => { # catch-all for COPY all_runs => 0, # catch-all regexp => qr/^COPY /m, - like => {}, # use more-specific options above + like => { # use more-specific options above + no_comments => 1, + pg_dumpall_no_comments => 1, }, unlike => { binary_upgrade => 1, column_inserts => 1, @@ -2529,11 +2579,13 @@ exclude_test_table => 1, exclude_test_table_data => 1, no_blobs => 1, + no_comments => 1, no_privs => 1, no_owner => 1, pg_dumpall_dbprivs => 1, pg_dumpall_globals => 1, pg_dumpall_globals_clean => 1, + pg_dumpall_no_comments => 1, section_data => 1, only_dump_test_schema => 1, only_dump_test_table => 1, @@ -2553,7 +2605,8 @@ like => { pg_dumpall_dbprivs => 1, pg_dumpall_globals => 1, - pg_dumpall_globals_clean => 1, }, + pg_dumpall_globals_clean => 1, + pg_dumpall_no_comments => 1, }, unlike => { binary_upgrade => 1, clean => 1, @@ -2566,6 +2619,7 @@ exclude_test_table => 1, exclude_test_table_data => 1, no_blobs => 1, + no_comments => 1, no_privs => 1, no_owner => 1, only_dump_test_schema => 1, @@ -2694,6 +2748,7 @@ exclude_test_table => 1, exclude_test_table_data => 1, no_blobs => 1, + no_comments => 1, no_privs => 1, no_owner => 1, only_dump_test_schema => 1, @@ -2701,6 +2756,7 @@ pg_dumpall_dbprivs => 1, pg_dumpall_globals => 1, pg_dumpall_globals_clean => 1, + pg_dumpall_no_comments => 1, role => 1, schema_only => 1, section_pre_data => 1, @@ -2716,7 +2772,9 @@ regexp => qr/^ \QCREATE DATABASE dump_test WITH TEMPLATE = template0 \E .*;/xm, - like => { pg_dumpall_dbprivs => 1, }, + like => { + pg_dumpall_dbprivs => 1, + pg_dumpall_no_comments => 1, }, unlike => { binary_upgrade => 1, clean => 1, @@ -2729,6 +2787,7 @@ exclude_test_table => 1, exclude_test_table_data => 1, no_blobs => 1, + no_comments => 1, no_privs => 1, no_owner => 1, only_dump_test_schema => 1, @@ -2757,9 +2816,11 @@ exclude_test_table => 1, exclude_test_table_data => 1, no_blobs => 1, + no_comments => 1, no_privs => 1, no_owner => 1, pg_dumpall_dbprivs => 1, + pg_dumpall_no_comments => 1, schema_only => 1, section_pre_data => 1, with_oids => 1, }, @@ -2802,10 +2863,12 @@ exclude_test_table => 1, exclude_test_table_data => 1, no_blobs => 1, + no_comments => 1, no_privs => 1, no_owner => 1, only_dump_test_schema => 1, pg_dumpall_dbprivs => 1, + pg_dumpall_no_comments => 1, schema_only => 1, section_pre_data => 1, test_schema_plus_blobs => 1, @@ -2837,10 +2900,12 @@ exclude_test_table => 1, exclude_test_table_data => 1, no_blobs => 1, + no_comments => 1, no_privs => 1, no_owner => 1, only_dump_test_schema => 1, pg_dumpall_dbprivs => 1, + pg_dumpall_no_comments => 1, schema_only => 1, section_pre_data => 1, test_schema_plus_blobs => 1, @@ -2880,10 +2945,12 @@ exclude_test_table => 1, exclude_test_table_data => 1, no_blobs => 1, + no_comments => 1, no_privs => 1, no_owner => 1, only_dump_test_schema => 1, pg_dumpall_dbprivs => 1, + pg_dumpall_no_comments => 1, schema_only => 1, section_pre_data => 1, test_schema_plus_blobs => 1, @@ -2921,10 +2988,12 @@ exclude_test_table => 1, exclude_test_table_data => 1, no_blobs => 1, + no_comments => 1, no_privs => 1, no_owner => 1, only_dump_test_schema => 1, pg_dumpall_dbprivs => 1, + pg_dumpall_no_comments => 1, schema_only => 1, section_pre_data => 1, test_schema_plus_blobs => 1, @@ -2961,10 +3030,12 @@ exclude_test_table => 1, exclude_test_table_data => 1, no_blobs => 1, + no_comments => 1, no_privs => 1, no_owner => 1, only_dump_test_schema => 1, pg_dumpall_dbprivs => 1, + pg_dumpall_no_comments => 1, schema_only => 1, section_pre_data => 1, test_schema_plus_blobs => 1, @@ -3001,10 +3072,12 @@ exclude_test_table => 1, exclude_test_table_data => 1, no_blobs => 1, + no_comments => 1, no_privs => 1, no_owner => 1, only_dump_test_schema => 1, pg_dumpall_dbprivs => 1, + pg_dumpall_no_comments => 1, schema_only => 1, section_pre_data => 1, test_schema_plus_blobs => 1, @@ -3037,10 +3110,12 @@ exclude_test_table => 1, exclude_test_table_data => 1, no_blobs => 1, + no_comments => 1, no_privs => 1, no_owner => 1, only_dump_test_schema => 1, pg_dumpall_dbprivs => 1, + pg_dumpall_no_comments => 1, schema_only => 1, section_pre_data => 1, test_schema_plus_blobs => 1, @@ -3089,10 +3164,12 @@ exclude_test_table => 1, exclude_test_table_data => 1, no_blobs => 1, + no_comments => 1, no_privs => 1, no_owner => 1, only_dump_test_schema => 1, pg_dumpall_dbprivs => 1, + pg_dumpall_no_comments => 1, schema_only => 1, section_pre_data => 1, test_schema_plus_blobs => 1, @@ -3129,9 +3206,11 @@ exclude_test_table => 1, exclude_test_table_data => 1, no_blobs => 1, + no_comments => 1, no_privs => 1, no_owner => 1, pg_dumpall_dbprivs => 1, + pg_dumpall_no_comments => 1, schema_only => 1, section_post_data => 1, with_oids => 1, }, @@ -3167,11 +3246,13 @@ defaults => 1, exclude_test_table_data => 1, no_blobs => 1, + no_comments => 1, no_privs => 1, no_owner => 1, only_dump_test_schema => 1, only_dump_test_table => 1, pg_dumpall_dbprivs => 1, + pg_dumpall_no_comments => 1, schema_only => 1, section_post_data => 1, test_schema_plus_blobs => 1, @@ -3206,10 +3287,12 @@ exclude_test_table => 1, exclude_test_table_data => 1, no_blobs => 1, + no_comments => 1, no_privs => 1, no_owner => 1, only_dump_test_schema => 1, pg_dumpall_dbprivs => 1, + pg_dumpall_no_comments => 1, schema_only => 1, section_pre_data => 1, test_schema_plus_blobs => 1, @@ -3248,10 +3331,12 @@ exclude_test_table => 1, exclude_test_table_data => 1, no_blobs => 1, + no_comments => 1, no_privs => 1, no_owner => 1, only_dump_test_schema => 1, pg_dumpall_dbprivs => 1, + pg_dumpall_no_comments => 1, schema_only => 1, section_pre_data => 1, test_schema_plus_blobs => 1, @@ -3284,10 +3369,12 @@ exclude_test_table => 1, exclude_test_table_data => 1, no_blobs => 1, + no_comments => 1, no_privs => 1, no_owner => 1, only_dump_test_schema => 1, pg_dumpall_dbprivs => 1, + pg_dumpall_no_comments => 1, schema_only => 1, section_pre_data => 1, test_schema_plus_blobs => 1, @@ -3317,10 +3404,12 @@ exclude_test_table => 1, exclude_test_table_data => 1, no_blobs => 1, + no_comments => 1, no_privs => 1, no_owner => 1, only_dump_test_schema => 1, pg_dumpall_dbprivs => 1, + pg_dumpall_no_comments => 1, schema_only => 1, section_pre_data => 1, test_schema_plus_blobs => 1, @@ -3353,10 +3442,12 @@ exclude_test_table => 1, exclude_test_table_data => 1, no_blobs => 1, + no_comments => 1, no_privs => 1, no_owner => 1, only_dump_test_schema => 1, pg_dumpall_dbprivs => 1, + pg_dumpall_no_comments => 1, schema_only => 1, section_pre_data => 1, test_schema_plus_blobs => 1, @@ -3442,10 +3533,12 @@ exclude_test_table => 1, exclude_test_table_data => 1, no_blobs => 1, + no_comments => 1, no_privs => 1, no_owner => 1, only_dump_test_schema => 1, pg_dumpall_dbprivs => 1, + pg_dumpall_no_comments => 1, schema_only => 1, section_pre_data => 1, test_schema_plus_blobs => 1, @@ -3478,10 +3571,12 @@ exclude_test_table => 1, exclude_test_table_data => 1, no_blobs => 1, + no_comments => 1, no_privs => 1, no_owner => 1, only_dump_test_schema => 1, pg_dumpall_dbprivs => 1, + pg_dumpall_no_comments => 1, schema_only => 1, section_pre_data => 1, test_schema_plus_blobs => 1, @@ -3518,10 +3613,12 @@ exclude_test_table => 1, exclude_test_table_data => 1, no_blobs => 1, + no_comments => 1, no_privs => 1, no_owner => 1, only_dump_test_schema => 1, pg_dumpall_dbprivs => 1, + pg_dumpall_no_comments => 1, schema_only => 1, section_pre_data => 1, test_schema_plus_blobs => 1, @@ -3555,10 +3652,12 @@ exclude_test_table => 1, exclude_test_table_data => 1, no_blobs => 1, + no_comments => 1, no_privs => 1, no_owner => 1, only_dump_test_schema => 1, pg_dumpall_dbprivs => 1, + pg_dumpall_no_comments => 1, schema_only => 1, section_pre_data => 1, test_schema_plus_blobs => 1, @@ -3594,10 +3693,12 @@ exclude_test_table => 1, exclude_test_table_data => 1, no_blobs => 1, + no_comments => 1, no_privs => 1, no_owner => 1, only_dump_test_schema => 1, pg_dumpall_dbprivs => 1, + pg_dumpall_no_comments => 1, schema_only => 1, section_pre_data => 1, test_schema_plus_blobs => 1, @@ -3633,10 +3734,12 @@ exclude_test_table => 1, exclude_test_table_data => 1, no_blobs => 1, + no_comments => 1, no_privs => 1, no_owner => 1, only_dump_test_schema => 1, pg_dumpall_dbprivs => 1, + pg_dumpall_no_comments => 1, schema_only => 1, section_pre_data => 1, test_schema_plus_blobs => 1, @@ -3681,10 +3784,12 @@ exclude_test_table => 1, exclude_test_table_data => 1, no_blobs => 1, + no_comments => 1, no_privs => 1, no_owner => 1, only_dump_test_schema => 1, pg_dumpall_dbprivs => 1, + pg_dumpall_no_comments => 1, schema_only => 1, section_pre_data => 1, test_schema_plus_blobs => 1, @@ -3722,10 +3827,12 @@ exclude_test_table => 1, exclude_test_table_data => 1, no_blobs => 1, + no_comments => 1, no_privs => 1, no_owner => 1, only_dump_test_schema => 1, pg_dumpall_dbprivs => 1, + pg_dumpall_no_comments => 1, schema_only => 1, section_pre_data => 1, test_schema_plus_blobs => 1, @@ -3755,10 +3862,12 @@ exclude_test_table => 1, exclude_test_table_data => 1, no_blobs => 1, + no_comments => 1, no_privs => 1, no_owner => 1, only_dump_test_schema => 1, pg_dumpall_dbprivs => 1, + pg_dumpall_no_comments => 1, schema_only => 1, section_pre_data => 1, test_schema_plus_blobs => 1, @@ -3789,9 +3898,11 @@ exclude_test_table => 1, exclude_test_table_data => 1, no_blobs => 1, + no_comments => 1, no_privs => 1, no_owner => 1, pg_dumpall_dbprivs => 1, + pg_dumpall_no_comments => 1, schema_only => 1, section_pre_data => 1, with_oids => 1, }, @@ -3822,9 +3933,11 @@ exclude_test_table => 1, exclude_test_table_data => 1, no_blobs => 1, + no_comments => 1, no_privs => 1, no_owner => 1, pg_dumpall_dbprivs => 1, + pg_dumpall_no_comments => 1, schema_only => 1, section_pre_data => 1, with_oids => 1, }, @@ -3864,10 +3977,12 @@ exclude_test_table => 1, exclude_test_table_data => 1, no_blobs => 1, + no_comments => 1, no_privs => 1, no_owner => 1, only_dump_test_schema => 1, pg_dumpall_dbprivs => 1, + pg_dumpall_no_comments => 1, schema_only => 1, section_pre_data => 1, test_schema_plus_blobs => 1, @@ -3900,9 +4015,11 @@ exclude_test_table => 1, exclude_test_table_data => 1, no_blobs => 1, + no_comments => 1, no_privs => 1, no_owner => 1, pg_dumpall_dbprivs => 1, + pg_dumpall_no_comments => 1, schema_only => 1, section_pre_data => 1, with_oids => 1, }, @@ -3935,9 +4052,11 @@ exclude_test_table => 1, exclude_test_table_data => 1, no_blobs => 1, + no_comments => 1, no_privs => 1, no_owner => 1, pg_dumpall_dbprivs => 1, + pg_dumpall_no_comments => 1, schema_only => 1, section_pre_data => 1, with_oids => 1, }, @@ -4470,9 +4589,11 @@ defaults => 1, exclude_test_table_data => 1, no_blobs => 1, + no_comments => 1, no_privs => 1, no_owner => 1, pg_dumpall_dbprivs => 1, + pg_dumpall_no_comments => 1, schema_only => 1, section_post_data => 1, with_oids => 1, }, @@ -4504,9 +4625,11 @@ defaults => 1, exclude_test_table => 1, exclude_test_table_data => 1, + no_comments => 1, no_privs => 1, no_owner => 1, pg_dumpall_dbprivs => 1, + pg_dumpall_no_comments => 1, schema_only => 1, section_post_data => 1, }, unlike => { @@ -5236,7 +5359,9 @@ 'CREATE ... commands' => { # catch-all for CREATE all_runs => 0, # catch-all regexp => qr/^CREATE /m, - like => {}, # use more-specific options above + like => { # use more-specific options above + no_comments => 1, + pg_dumpall_no_comments => 1, }, unlike => { column_inserts => 1, data_only => 1, @@ -5465,12 +5590,14 @@ exclude_test_table => 1, exclude_test_table_data => 1, no_blobs => 1, + no_comments => 1, no_privs => 1, no_owner => 1, only_dump_test_schema => 1, only_dump_test_table => 1, pg_dumpall_dbprivs => 1, pg_dumpall_globals => 1, + pg_dumpall_no_comments => 1, role => 1, schema_only => 1, section_data => 1, @@ -6137,6 +6264,7 @@ exclude_test_table => 1, exclude_test_table_data => 1, no_blobs => 1, + no_comments => 1, no_owner => 1, pg_dumpall_dbprivs => 1, schema_only => 1, @@ -6144,6 +6272,7 @@ only_dump_test_schema => 1, only_dump_test_table => 1, pg_dumpall_globals_clean => 1, + pg_dumpall_no_comments => 1, role => 1, section_data => 1, section_post_data => 1, @@ -6152,7 +6281,9 @@ 'GRANT commands' => { # catch-all for GRANT commands all_runs => 0, # catch-all regexp => qr/^GRANT /m, - like => {}, # use more-specific options above + like => { # use more-specific options above + no_comments => 1, + pg_dumpall_no_comments => 1, }, unlike => { no_privs => 1, pg_dumpall_globals_clean => 1, @@ -6170,10 +6301,12 @@ exclude_test_table => 1, exclude_test_table_data => 1, no_blobs => 1, + no_comments => 1, no_privs => 1, no_owner => 1, only_dump_test_schema => 1, pg_dumpall_dbprivs => 1, + pg_dumpall_no_comments => 1, test_schema_plus_blobs => 1, section_post_data => 1, with_oids => 1, }, @@ -6205,10 +6338,12 @@ exclude_test_table => 1, exclude_test_table_data => 1, no_blobs => 1, + no_comments => 1, no_privs => 1, no_owner => 1, only_dump_test_schema => 1, pg_dumpall_dbprivs => 1, + pg_dumpall_no_comments => 1, test_schema_plus_blobs => 1, section_post_data => 1, with_oids => 1, }, @@ -6243,6 +6378,7 @@ exclude_test_table => 1, exclude_test_table_data => 1, no_blobs => 1, + no_comments => 1, no_privs => 1, no_owner => 1, only_dump_test_schema => 1, @@ -6250,6 +6386,7 @@ pg_dumpall_dbprivs => 1, pg_dumpall_globals => 1, pg_dumpall_globals_clean => 1, + pg_dumpall_no_comments => 1, role => 1, schema_only => 1, section_data => 1, @@ -6276,6 +6413,7 @@ exclude_test_table => 1, exclude_test_table_data => 1, no_blobs => 1, + no_comments => 1, no_privs => 1, no_owner => 1, only_dump_test_schema => 1, @@ -6283,6 +6421,7 @@ pg_dumpall_dbprivs => 1, pg_dumpall_globals => 1, pg_dumpall_globals_clean => 1, + pg_dumpall_no_comments => 1, role => 1, schema_only => 1, section_data => 1, @@ -6454,7 +6593,9 @@ 'REVOKE commands' => { # catch-all for REVOKE commands all_runs => 0, # catch-all regexp => qr/^REVOKE /m, - like => {}, # use more-specific options above + like => { # use more-specific options above + no_comments => 1, + pg_dumpall_no_comments => 1, }, unlike => { column_inserts => 1, data_only => 1, From 9785c0c702a7aad7b4de6db65170675f2bf3b56e Mon Sep 17 00:00:00 2001 From: Robins Tharakan Date: Tue, 3 Oct 2017 22:55:03 +1100 Subject: [PATCH 135/149] Merging recent changes with the master --- .gitignore | 2 +- config/c-compiler.m4 | 17 + configure | 24 + configure.in | 1 + contrib/btree_gist/btree_uuid.c | 4 +- contrib/pg_prewarm/autoprewarm.c | 6 +- .../pg_stat_statements/pg_stat_statements.c | 1 + contrib/pgcrypto/crypt-des.c | 17 +- contrib/uuid-ossp/uuid-ossp.c | 17 +- doc/src/sgml/array.sgml | 5 +- doc/src/sgml/bgworker.sgml | 11 +- doc/src/sgml/charset.sgml | 135 ++++-- doc/src/sgml/client-auth.sgml | 2 +- doc/src/sgml/config.sgml | 39 -- doc/src/sgml/ref/create_collation.sgml | 28 +- doc/src/sgml/release-10.sgml | 362 ++++++++++++++- doc/src/sgml/release-9.6.sgml | 2 +- src/Makefile.global.in | 28 +- src/backend/access/transam/parallel.c | 1 + src/backend/catalog/dependency.c | 9 +- src/backend/commands/analyze.c | 7 +- src/backend/commands/copy.c | 11 +- src/backend/commands/typecmds.c | 50 ++- src/backend/commands/vacuum.c | 60 ++- src/backend/executor/execExpr.c | 58 +-- src/backend/executor/execExprInterp.c | 145 ++++-- src/backend/libpq/auth.c | 18 +- src/backend/libpq/ifaddr.c | 6 +- src/backend/libpq/pqcomm.c | 6 +- src/backend/libpq/pqformat.c | 40 +- src/backend/nodes/copyfuncs.c | 3 +- src/backend/nodes/equalfuncs.c | 3 +- src/backend/nodes/nodeFuncs.c | 20 +- src/backend/nodes/outfuncs.c | 3 +- src/backend/nodes/readfuncs.c | 3 +- src/backend/optimizer/path/costsize.c | 13 +- src/backend/optimizer/plan/setrefs.c | 6 - src/backend/optimizer/prep/preptlist.c | 2 +- src/backend/optimizer/util/clauses.c | 46 +- src/backend/parser/parse_coerce.c | 155 ++++--- src/backend/postmaster/bgworker.c | 51 ++- src/backend/postmaster/postmaster.c | 18 +- src/backend/replication/logical/launcher.c | 3 + src/backend/rewrite/rewriteHandler.c | 4 +- src/backend/rewrite/rewriteManip.c | 2 +- src/backend/tcop/fastpath.c | 8 +- src/backend/utils/adt/arrayfuncs.c | 85 ++-- src/backend/utils/adt/pgstatfuncs.c | 16 +- src/backend/utils/adt/selfuncs.c | 15 +- src/backend/utils/fmgr/fmgr.c | 12 +- src/backend/utils/init/globals.c | 1 - src/backend/utils/misc/guc.c | 10 - src/backend/utils/misc/postgresql.conf.sample | 1 - src/backend/utils/sort/tuplesort.c | 415 +++--------------- src/bin/pg_basebackup/streamutil.c | 34 +- src/bin/pg_ctl/pg_ctl.c | 5 +- src/bin/pg_dump/parallel.c | 6 +- src/bin/pg_dump/pg_dump.c | 79 +++- src/bin/pg_rewind/libpq_fetch.c | 29 +- src/bin/pgbench/pgbench.c | 34 +- src/bin/psql/describe.c | 206 +++++---- src/bin/scripts/t/080_pg_isready.pl | 3 +- src/common/scram-common.c | 7 +- src/include/catalog/catversion.h | 2 +- src/include/executor/execExpr.h | 7 +- src/include/miscadmin.h | 1 - src/include/nodes/primnodes.h | 14 +- src/include/parser/parse_coerce.h | 5 +- src/include/pg_config.h.in | 3 + src/include/pg_config.h.win32 | 3 + src/include/port/pg_bswap.h | 132 +++++- src/include/port/pg_crc32c.h | 2 +- src/include/postmaster/bgworker.h | 2 + src/include/utils/array.h | 9 +- src/include/utils/hashutils.h | 2 +- src/interfaces/libpq/fe-connect.c | 12 +- src/interfaces/libpq/fe-lobj.c | 11 +- src/interfaces/libpq/fe-misc.c | 14 +- src/interfaces/libpq/fe-protocol2.c | 5 +- src/interfaces/libpq/fe-protocol3.c | 5 +- src/pl/plpython/plpy_cursorobject.c | 3 +- src/pl/plpython/plpy_plpymodule.c | 3 +- src/pl/plpython/plpy_subxactobject.c | 3 +- src/port/getaddrinfo.c | 11 +- src/port/inet_aton.c | 4 +- src/test/modules/test_shm_mq/setup.c | 2 +- src/test/modules/worker_spi/worker_spi.c | 8 +- src/test/recovery/t/013_crash_restart.pl | 28 +- src/test/regress/expected/cluster.out | 17 +- src/test/regress/expected/domain.out | 95 ++++ src/test/regress/expected/identity.out | 7 + src/test/regress/expected/sequence.out | 13 + src/test/regress/sql/cluster.sql | 14 +- src/test/regress/sql/domain.sql | 43 ++ src/test/regress/sql/identity.sql | 2 + src/test/regress/sql/sequence.sql | 4 + 96 files changed, 1714 insertions(+), 1182 deletions(-) diff --git a/.gitignore b/.gitignore index 94e2c582f5133..a59e3da3bef68 100644 --- a/.gitignore +++ b/.gitignore @@ -21,7 +21,7 @@ objfiles.txt *.gcda *.gcov *.gcov.out -lcov.info +lcov*.info coverage/ coverage-html-stamp *.vcproj diff --git a/config/c-compiler.m4 b/config/c-compiler.m4 index 7275ea69fef7c..6dcc79064916a 100644 --- a/config/c-compiler.m4 +++ b/config/c-compiler.m4 @@ -224,6 +224,23 @@ AC_DEFINE(HAVE__BUILTIN_TYPES_COMPATIBLE_P, 1, fi])# PGAC_C_TYPES_COMPATIBLE +# PGAC_C_BUILTIN_BSWAP16 +# ------------------------- +# Check if the C compiler understands __builtin_bswap16(), +# and define HAVE__BUILTIN_BSWAP16 if so. +AC_DEFUN([PGAC_C_BUILTIN_BSWAP16], +[AC_CACHE_CHECK(for __builtin_bswap16, pgac_cv__builtin_bswap16, +[AC_COMPILE_IFELSE([AC_LANG_SOURCE( +[static unsigned long int x = __builtin_bswap16(0xaabb);] +)], +[pgac_cv__builtin_bswap16=yes], +[pgac_cv__builtin_bswap16=no])]) +if test x"$pgac_cv__builtin_bswap16" = xyes ; then +AC_DEFINE(HAVE__BUILTIN_BSWAP16, 1, + [Define to 1 if your compiler understands __builtin_bswap16.]) +fi])# PGAC_C_BUILTIN_BSWAP16 + + # PGAC_C_BUILTIN_BSWAP32 # ------------------------- diff --git a/configure b/configure index 4f3b97c7cf795..216447e73903d 100755 --- a/configure +++ b/configure @@ -11816,6 +11816,30 @@ if test x"$pgac_cv__types_compatible" = xyes ; then $as_echo "#define HAVE__BUILTIN_TYPES_COMPATIBLE_P 1" >>confdefs.h +fi +{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for __builtin_bswap16" >&5 +$as_echo_n "checking for __builtin_bswap16... " >&6; } +if ${pgac_cv__builtin_bswap16+:} false; then : + $as_echo_n "(cached) " >&6 +else + cat confdefs.h - <<_ACEOF >conftest.$ac_ext +/* end confdefs.h. */ +static unsigned long int x = __builtin_bswap16(0xaabb); + +_ACEOF +if ac_fn_c_try_compile "$LINENO"; then : + pgac_cv__builtin_bswap16=yes +else + pgac_cv__builtin_bswap16=no +fi +rm -f core conftest.err conftest.$ac_objext conftest.$ac_ext +fi +{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $pgac_cv__builtin_bswap16" >&5 +$as_echo "$pgac_cv__builtin_bswap16" >&6; } +if test x"$pgac_cv__builtin_bswap16" = xyes ; then + +$as_echo "#define HAVE__BUILTIN_BSWAP16 1" >>confdefs.h + fi { $as_echo "$as_me:${as_lineno-$LINENO}: checking for __builtin_bswap32" >&5 $as_echo_n "checking for __builtin_bswap32... " >&6; } diff --git a/configure.in b/configure.in index fa48369630784..a2e3d8331ad3f 100644 --- a/configure.in +++ b/configure.in @@ -1306,6 +1306,7 @@ PGAC_C_FUNCNAME_SUPPORT PGAC_C_STATIC_ASSERT PGAC_C_TYPEOF PGAC_C_TYPES_COMPATIBLE +PGAC_C_BUILTIN_BSWAP16 PGAC_C_BUILTIN_BSWAP32 PGAC_C_BUILTIN_BSWAP64 PGAC_C_BUILTIN_CONSTANT_P diff --git a/contrib/btree_gist/btree_uuid.c b/contrib/btree_gist/btree_uuid.c index ecf357d662ce9..9ff421ea5575e 100644 --- a/contrib/btree_gist/btree_uuid.c +++ b/contrib/btree_gist/btree_uuid.c @@ -182,8 +182,8 @@ uuid_2_double(const pg_uuid_t *u) * machine, byte-swap each half so we can use native uint64 arithmetic. */ #ifndef WORDS_BIGENDIAN - uu[0] = BSWAP64(uu[0]); - uu[1] = BSWAP64(uu[1]); + uu[0] = pg_bswap64(uu[0]); + uu[1] = pg_bswap64(uu[1]); #endif /* diff --git a/contrib/pg_prewarm/autoprewarm.c b/contrib/pg_prewarm/autoprewarm.c index cc0350e6d684a..006c3153db0ec 100644 --- a/contrib/pg_prewarm/autoprewarm.c +++ b/contrib/pg_prewarm/autoprewarm.c @@ -800,7 +800,8 @@ apw_start_master_worker(void) worker.bgw_start_time = BgWorkerStart_ConsistentState; strcpy(worker.bgw_library_name, "pg_prewarm"); strcpy(worker.bgw_function_name, "autoprewarm_main"); - strcpy(worker.bgw_name, "autoprewarm"); + strcpy(worker.bgw_name, "autoprewarm master"); + strcpy(worker.bgw_type, "autoprewarm master"); if (process_shared_preload_libraries_in_progress) { @@ -840,7 +841,8 @@ apw_start_database_worker(void) worker.bgw_start_time = BgWorkerStart_ConsistentState; strcpy(worker.bgw_library_name, "pg_prewarm"); strcpy(worker.bgw_function_name, "autoprewarm_database_main"); - strcpy(worker.bgw_name, "autoprewarm"); + strcpy(worker.bgw_name, "autoprewarm worker"); + strcpy(worker.bgw_type, "autoprewarm worker"); /* must set notify PID to wait for shutdown */ worker.bgw_notify_pid = MyProcPid; diff --git a/contrib/pg_stat_statements/pg_stat_statements.c b/contrib/pg_stat_statements/pg_stat_statements.c index 3ab1fd2db43ac..a0e7a46871dcf 100644 --- a/contrib/pg_stat_statements/pg_stat_statements.c +++ b/contrib/pg_stat_statements/pg_stat_statements.c @@ -2630,6 +2630,7 @@ JumbleExpr(pgssJumbleState *jstate, Node *node) APP_JUMB(acexpr->resulttype); JumbleExpr(jstate, (Node *) acexpr->arg); + JumbleExpr(jstate, (Node *) acexpr->elemexpr); } break; case T_ConvertRowtypeExpr: diff --git a/contrib/pgcrypto/crypt-des.c b/contrib/pgcrypto/crypt-des.c index ee3a0f216965b..ed07fc4606402 100644 --- a/contrib/pgcrypto/crypt-des.c +++ b/contrib/pgcrypto/crypt-des.c @@ -62,13 +62,10 @@ #include "postgres.h" #include "miscadmin.h" +#include "port/pg_bswap.h" #include "px-crypt.h" -/* for ntohl/htonl */ -#include -#include - #define _PASSWORD_EFMT1 '_' static const char _crypt_a64[] = @@ -408,8 +405,8 @@ des_setkey(const char *key) if (!des_initialised) des_init(); - rawkey0 = ntohl(*(const uint32 *) key); - rawkey1 = ntohl(*(const uint32 *) (key + 4)); + rawkey0 = pg_ntoh32(*(const uint32 *) key); + rawkey1 = pg_ntoh32(*(const uint32 *) (key + 4)); if ((rawkey0 | rawkey1) && rawkey0 == old_rawkey0 @@ -634,15 +631,15 @@ des_cipher(const char *in, char *out, long salt, int count) /* copy data to avoid assuming input is word-aligned */ memcpy(buffer, in, sizeof(buffer)); - rawl = ntohl(buffer[0]); - rawr = ntohl(buffer[1]); + rawl = pg_ntoh32(buffer[0]); + rawr = pg_ntoh32(buffer[1]); retval = do_des(rawl, rawr, &l_out, &r_out, count); if (retval) return retval; - buffer[0] = htonl(l_out); - buffer[1] = htonl(r_out); + buffer[0] = pg_hton32(l_out); + buffer[1] = pg_hton32(r_out); /* copy data to avoid assuming output is word-aligned */ memcpy(out, buffer, sizeof(buffer)); diff --git a/contrib/uuid-ossp/uuid-ossp.c b/contrib/uuid-ossp/uuid-ossp.c index 55bc60941588f..fce4bc9140bdb 100644 --- a/contrib/uuid-ossp/uuid-ossp.c +++ b/contrib/uuid-ossp/uuid-ossp.c @@ -14,13 +14,10 @@ #include "postgres.h" #include "fmgr.h" +#include "port/pg_bswap.h" #include "utils/builtins.h" #include "utils/uuid.h" -/* for ntohl/htonl */ -#include -#include - /* * It's possible that there's more than one uuid.h header file present. * We expect configure to set the HAVE_ symbol for only the one we want. @@ -90,16 +87,16 @@ typedef struct #define UUID_TO_NETWORK(uu) \ do { \ - uu.time_low = htonl(uu.time_low); \ - uu.time_mid = htons(uu.time_mid); \ - uu.time_hi_and_version = htons(uu.time_hi_and_version); \ + uu.time_low = pg_hton32(uu.time_low); \ + uu.time_mid = pg_hton16(uu.time_mid); \ + uu.time_hi_and_version = pg_hton16(uu.time_hi_and_version); \ } while (0) #define UUID_TO_LOCAL(uu) \ do { \ - uu.time_low = ntohl(uu.time_low); \ - uu.time_mid = ntohs(uu.time_mid); \ - uu.time_hi_and_version = ntohs(uu.time_hi_and_version); \ + uu.time_low = pg_ntoh32(uu.time_low); \ + uu.time_mid = pg_ntoh16(uu.time_mid); \ + uu.time_hi_and_version = pg_ntoh16(uu.time_hi_and_version); \ } while (0) #define UUID_V3_OR_V5(uu, v) \ diff --git a/doc/src/sgml/array.sgml b/doc/src/sgml/array.sgml index dd0d20e541f89..88eb4be04d001 100644 --- a/doc/src/sgml/array.sgml +++ b/doc/src/sgml/array.sgml @@ -10,9 +10,8 @@ PostgreSQL allows columns of a table to be defined as variable-length multidimensional arrays. Arrays of any - built-in or user-defined base type, enum type, or composite type - can be created. - Arrays of domains are not yet supported. + built-in or user-defined base type, enum type, composite type, range type, + or domain can be created. diff --git a/doc/src/sgml/bgworker.sgml b/doc/src/sgml/bgworker.sgml index b42232308199f..ea1b5c0c8e393 100644 --- a/doc/src/sgml/bgworker.sgml +++ b/doc/src/sgml/bgworker.sgml @@ -51,6 +51,7 @@ typedef void (*bgworker_main_type)(Datum main_arg); typedef struct BackgroundWorker { char bgw_name[BGW_MAXLEN]; + char bgw_type[BGW_MAXLEN]; int bgw_flags; BgWorkerStartTime bgw_start_time; int bgw_restart_time; /* in seconds, or BGW_NEVER_RESTART */ @@ -64,8 +65,14 @@ typedef struct BackgroundWorker - bgw_name is a string to be used in log messages, process - listings and similar contexts. + bgw_name and bgw_type are + strings to be used in log messages, process listings and similar contexts. + bgw_type should be the same for all background + workers of the same type, so that it is possible to group such workers in a + process listing, for example. bgw_name on the + other hand can contain additional information about the specific process. + (Typically, the string for bgw_name will contain + the type somehow, but that is not strictly required.) diff --git a/doc/src/sgml/charset.sgml b/doc/src/sgml/charset.sgml index 44e43503a6197..63f7de5b43800 100644 --- a/doc/src/sgml/charset.sgml +++ b/doc/src/sgml/charset.sgml @@ -515,7 +515,7 @@ SELECT * FROM test1 ORDER BY a || b COLLATE "fr_FR"; A collation object provided by libc maps to a combination of LC_COLLATE and LC_CTYPE - settings. (As + settings, as accepted by the setlocale() system library call. (As the name would suggest, the main purpose of a collation is to set LC_COLLATE, which controls the sort order. But it is rarely necessary in practice to have an @@ -640,21 +640,19 @@ SELECT a COLLATE "C" < b COLLATE "POSIX" FROM test1; Codestin Search App - Collations provided by ICU are created with names in BCP 47 language tag + With ICU, it is not sensible to enumerate all possible locale names. ICU + uses a particular naming system for locales, but there are many more ways + to name a locale than there are actually distinct locales. + initdb uses the ICU APIs to extract a set of distinct + locales to populate the initial set of collations. Collations provided by + ICU are created in the SQL environment with names in BCP 47 language tag format, with a private use extension -x-icu appended, to distinguish them from - libc locales. So de-x-icu would be an example name. + libc locales. - With ICU, it is not sensible to enumerate all possible locale names. ICU - uses a particular naming system for locales, but there are many more ways - to name a locale than there are actually distinct locales. (In fact, any - string will be accepted as a locale name.) - See for - information on ICU locale naming. initdb uses the ICU - APIs to extract a set of distinct locales to populate the initial set of - collations. Here are some example collations that might be created: + Here are some example collations that might be created: @@ -695,32 +693,104 @@ SELECT a COLLATE "C" < b COLLATE "POSIX" FROM test1; will draw an error along the lines of collation "de-x-icu" for encoding "WIN874" does not exist. + + + + + Codestin Search App + + + If the standard and predefined collations are not sufficient, users can + create their own collation objects using the SQL + command . + + + + The standard and predefined collations are in the + schema pg_catalog, like all predefined objects. + User-defined collations should be created in user schemas. This also + ensures that they are saved by pg_dump. + + + + Codestin Search App + + + New libc collations can be created like this: + +CREATE COLLATION german (provider = libc, locale = 'de_DE'); + + The exact values that are acceptable for the locale + clause in this command depend on the operating system. On Unix-like + systems, the command locale -a will show a list. + + + + Since the predefined libc collations already include all collations + defined in the operating system when the database instance is + initialized, it is not often necessary to manually create new ones. + Reasons might be if a different naming system is desired (in which case + see also ) or if the operating system has + been upgraded to provide new locale definitions (in which case see + also pg_import_system_collations()). + + + + + Codestin Search App ICU allows collations to be customized beyond the basic language+country set that is preloaded by initdb. Users are encouraged to define their own collation objects that make use of these facilities to - suit the sorting behavior to their requirements. Here are some examples: + suit the sorting behavior to their requirements. + See + and for + information on ICU locale naming. The set of acceptable names and + attributes depends on the particular ICU version. + + + + Here are some examples: - CREATE COLLATION "de-u-co-phonebk-x-icu" (provider = icu, locale = 'de-u-co-phonebk') + CREATE COLLATION "de-u-co-phonebk-x-icu" (provider = icu, locale = 'de-u-co-phonebk'); + CREATE COLLATION "de-u-co-phonebk-x-icu" (provider = icu, locale = 'de@collation=phonebook'); German collation with phone book collation type + + The first example selects the ICU locale using a language + tag per BCP 47. The second example uses the traditional + ICU-specific locale syntax. The first style is preferred going + forward, but it is not supported by older ICU versions. + + + Note that you can name the collation objects in the SQL environment + anything you want. In this example, we follow the naming style that + the predefined collations use, which in turn also follow BCP 47, but + that is not required for user-defined collations. + - CREATE COLLATION "und-u-co-emoji-x-icu" (provider = icu, locale = 'und-u-co-emoji') + CREATE COLLATION "und-u-co-emoji-x-icu" (provider = icu, locale = 'und-u-co-emoji'); + CREATE COLLATION "und-u-co-emoji-x-icu" (provider = icu, locale = '@collation=emoji'); Root collation with Emoji collation type, per Unicode Technical Standard #51 + + Observe how in the traditional ICU locale naming system, the root + locale is selected by an empty string. + - CREATE COLLATION digitslast (provider = icu, locale = 'en-u-kr-latn-digit') + CREATE COLLATION digitslast (provider = icu, locale = 'en-u-kr-latn-digit'); + CREATE COLLATION digitslast (provider = icu, locale = 'en@colReorder=latn-digit'); Sort digits after Latin letters. (The default is digits before letters.) @@ -729,7 +799,8 @@ SELECT a COLLATE "C" < b COLLATE "POSIX" FROM test1; - CREATE COLLATION upperfirst (provider = icu, locale = 'en-u-kf-upper') + CREATE COLLATION upperfirst (provider = icu, locale = 'en-u-kf-upper'); + CREATE COLLATION upperfirst (provider = icu, locale = 'en@colCaseFirst=upper'); Sort upper-case letters before lower-case letters. (The default is @@ -739,7 +810,8 @@ SELECT a COLLATE "C" < b COLLATE "POSIX" FROM test1; - CREATE COLLATION special (provider = icu, locale = 'en-u-kf-upper-kr-latn-digit') + CREATE COLLATION special (provider = icu, locale = 'en-u-kf-upper-kr-latn-digit'); + CREATE COLLATION special (provider = icu, locale = 'en@colCaseFirst=upper;colReorder=latn-digit'); Combines both of the above options. @@ -748,7 +820,8 @@ SELECT a COLLATE "C" < b COLLATE "POSIX" FROM test1; - CREATE COLLATION numeric (provider = icu, locale = 'en-u-kn-true') + CREATE COLLATION numeric (provider = icu, locale = 'en-u-kn-true'); + CREATE COLLATION numeric (provider = icu, locale = 'en@colNumeric=yes'); Numeric ordering, sorts sequences of digits by their numeric value, @@ -768,7 +841,8 @@ SELECT a COLLATE "C" < b COLLATE "POSIX" FROM test1; repository. The ICU Locale Explorer can be used to check the details of a particular locale - definition. + definition. The examples using the k* subtags require + at least ICU version 54. @@ -779,10 +853,21 @@ SELECT a COLLATE "C" < b COLLATE "POSIX" FROM test1; strings that compare equal according to the collation but are not byte-wise equal will be sorted according to their byte values. + + + + By design, ICU will accept almost any string as a locale name and match + it to the closet locale it can provide, using the fallback procedure + described in its documentation. Thus, there will be no direct feedback + if a collation specification is composed using features that the given + ICU installation does not actually support. It is therefore recommended + to create application-level test cases to check that the collation + definitions satisfy one's requirements. + + - - + Codestin Search App @@ -796,13 +881,7 @@ CREATE COLLATION german FROM "de_DE"; CREATE COLLATION french FROM "fr-x-icu"; - - - The standard and predefined collations are in the - schema pg_catalog, like all predefined objects. - User-defined collations should be created in user schemas. This also - ensures that they are saved by pg_dump. - + diff --git a/doc/src/sgml/client-auth.sgml b/doc/src/sgml/client-auth.sgml index c76d5faf445ad..78c594bbbaa28 100644 --- a/doc/src/sgml/client-auth.sgml +++ b/doc/src/sgml/client-auth.sgml @@ -959,7 +959,7 @@ omicron bryanh guest1 mechanism. It prevents password sniffing and avoids storing passwords on the server in plain text but provides no protection if an attacker manages to steal the password hash from the server. Also, the MD5 hash - algorithm is nowadays no longer consider secure against determined + algorithm is nowadays no longer considered secure against determined attacks. diff --git a/doc/src/sgml/config.sgml b/doc/src/sgml/config.sgml index 4b265d9e40c50..c13f60230f9a8 100644 --- a/doc/src/sgml/config.sgml +++ b/doc/src/sgml/config.sgml @@ -1505,45 +1505,6 @@ include_dir 'conf.d' - - replacement_sort_tuples (integer) - - replacement_sort_tuples configuration parameter - - - - - When the number of tuples to be sorted is smaller than this number, - a sort will produce its first output run using replacement selection - rather than quicksort. This may be useful in memory-constrained - environments where tuples that are input into larger sort operations - have a strong physical-to-logical correlation. Note that this does - not include input tuples with an inverse - correlation. It is possible for the replacement selection algorithm - to generate one long run that requires no merging, where use of the - default strategy would result in many runs that must be merged - to produce a final sorted output. This may allow sort - operations to complete sooner. - - - The default is 150,000 tuples. Note that higher values are typically - not much more effective, and may be counter-productive, since the - priority queue is sensitive to the size of available CPU cache, whereas - the default strategy sorts runs using a cache - oblivious algorithm. This property allows the default sort - strategy to automatically and transparently make effective use - of available CPU cache. - - - Setting maintenance_work_mem to its default - value usually prevents utility command external sorts (e.g., - sorts used by CREATE INDEX to build B-Tree - indexes) from ever using replacement selection sort, unless the - input tuples are quite wide. - - - - autovacuum_work_mem (integer) diff --git a/doc/src/sgml/ref/create_collation.sgml b/doc/src/sgml/ref/create_collation.sgml index 2d3e050545c7e..f88758095f266 100644 --- a/doc/src/sgml/ref/create_collation.sgml +++ b/doc/src/sgml/ref/create_collation.sgml @@ -93,10 +93,7 @@ CREATE COLLATION [ IF NOT EXISTS ] name FROM Use the specified operating system locale for - the LC_COLLATE locale category. The locale - must be applicable to the current database encoding. - (See for the precise - rules.) + the LC_COLLATE locale category. @@ -107,10 +104,7 @@ CREATE COLLATION [ IF NOT EXISTS ] name FROM Use the specified operating system locale for - the LC_CTYPE locale category. The locale - must be applicable to the current database encoding. - (See for the precise - rules.) + the LC_CTYPE locale category. @@ -173,8 +167,13 @@ CREATE COLLATION [ IF NOT EXISTS ] name FROM - See for more information about collation - support in PostgreSQL. + See for more information on how to create collations. + + + + When using the libc collation provider, the locale must + be applicable to the current database encoding. + See for the precise rules. @@ -186,7 +185,14 @@ CREATE COLLATION [ IF NOT EXISTS ] name FROM fr_FR.utf8 (assuming the current database encoding is UTF8): -CREATE COLLATION french (LOCALE = 'fr_FR.utf8'); +CREATE COLLATION french (locale = 'fr_FR.utf8'); + + + + + To create a collation using the ICU provider using German phone book sort order: + +CREATE COLLATION german_phonebook (provider = icu, locale = 'de-u-co-phonebk'); diff --git a/doc/src/sgml/release-10.sgml b/doc/src/sgml/release-10.sgml index 9fd3b2c8ac60c..fed67e1b231ef 100644 --- a/doc/src/sgml/release-10.sgml +++ b/doc/src/sgml/release-10.sgml @@ -6,7 +6,7 @@ Codestin Search App - 2017-??-?? (current as of 2017-09-17, commit 244b4a37e) + 2017-10-05 @@ -193,6 +193,7 @@ 2016-11-18 [67dc4ccbb] Add pg_sequences view 2017-05-15 [f8dc1985f] Fix ALTER SEQUENCE locking 2017-06-01 [3d79013b9] Make ALTER SEQUENCE, including RESTART, fully transactio +2017-09-29 [5cc5987ce] psql: Update \d sequence display --> Move sequences' metadata fields into a new + + + The output of psql's \d command for a + sequence has been redesigned, too. + @@ -764,23 +770,6 @@ - - Reduce locking required for adding values to enum types (Andrew - Dunstan, Tom Lane) - - - - Previously it was impossible to run ALTER TYPE ... ADD - VALUE in a transaction block unless the enum type was created - in the same block. Now, only references to uncommitted enum - values from other transactions are prohibited. - - - - - @@ -3169,4 +3158,341 @@ + + Codestin Search App + + + The following individuals (in alphabetical order) have contributed to this + release as patch authors, committers, reviewers, testers, or reporters of + issues. + + + + Adam Brightwell + Adam Brusselback + Adam Gomaa + Adam Sah + Adrian Klaver + Aidan Van Dyk + Aleksander Alekseev + Alexander Korotkov + Alexander Lakhin + Alexander Sosna + Alexey Bashtanov + Alexey Grishchenko + Alexey Isayko + Álvaro Hernández Tortosa + Álvaro Herrera + Amit Kapila + Amit Khandekar + Amit Langote + Amul Sul + Anastasia Lubennikova + Andreas Joseph Krogh + Andreas Karlsson + Andreas Scherbaum + Andreas Seltenreich + Andres Freund + Andrew Dunstan + Andrew Gierth + Andrew Wheelwright + Andrey Borodin + Andrey Lizenko + Andy Abelisto + Antonin Houska + Ants Aasma + Arjen Nienhuis + Arseny Sher + Artur Zakirov + Ashutosh Bapat + Ashutosh Sharma + Ashwin Agrawal + Atsushi Torikoshi + Ayumi Ishii + Basil Bourque + Beena Emerson + Ben de Graaff + Benedikt Grundmann + Bernd Helmle + Brad DeJong + Brandur Leach + Breen Hagan + Bruce Momjian + Bruno Wolff III + Catalin Iacob + Chapman Flack + Chen Huajun + Choi Doo-Won + Chris Bandy + Chris Richards + Chris Ruprecht + Christian Ullrich + Christoph Berg + Chuanting Wang + Claudio Freire + Clinton Adams + Const Zhang + Constantin Pan + Corey Huinker + Craig Ringer + Cynthia Shang + Dagfinn Ilmari Mannsåker + Daisuke Higuchi + Damian Quiroga + Dan Wood + Daniel Gustafsson + Daniel Vérité + Daniel Westermann + Daniele Varrazzo + Danylo Hlynskyi + Darko Prelec + Dave Cramer + Dave Page + David Christensen + David Fetter + David Johnston + David Rader + David Rowley + David Steele + Dean Rasheed + Denis Smirnov + Denish Patel + Dennis Björklund + Devrim Gündüz + Dilip Kumar + Dilyan Palauzov + Dima Pavlov + Dimitry Ivanov + Dmitriy Sarafannikov + Dmitry Dolgov + Dmitry Fedin + Don Morrison + Egor Rogov + Eiji Seki + Emil Iggland + Emre Hasegeli + Enrique Meneses + Erik Nordström + Erik Rijkers + Erwin Brandstetter + Etsuro Fujita + Eugen Konkov + Eugene Kazakov + Euler Taveira + Fabien Coelho + Fabrízio de Royes Mello + Fakhroutdinov Evgenievich + Feike Steenbergen + Felix Gerzaguet + Filip Jirsák + Fujii Masao + Gabriele Bartolini + Gabrielle Roth + Gao Zengqi + Gerdan Santos + Gianni Ciolli + Gilles Darold + Giuseppe Broccolo + Graham Dutton + Greg Atkins + Greg Burek + Grigory Smolkin + Guillaume Lelarge + Hans Buschmann + Haribabu Kommi + Heikki Linnakangas + Henry Boehlert + Huan Ruan + Huong Dangminh + Ian Barwick + Igor Korot + Ildus Kurbangaliev + Ivan Kartyshov + Jaime Casanova + Jakob Egger + James Parks + Jarred Ward + Jason Li + Jason O'Donnell + Jason Petersen + Jeevan Chalke + Jeevan Ladhe + Jeff Dafoe + Jeff Davis + Jeff Janes + Jelte Fennema + Jeremy Finzel + Jeremy Schneider + Jeroen van der Ham + Jesper Pedersen + Jim Mlodgenski + Jim Nasby + Jinyu Zhang + Joe Conway + Joel Jacobson + John Harvey + Jon Nelson + Jordan Gigov + Josh Berkus + Josh Soref + Julian Markwort + Julien Rouhaud + Junseok Yang + Justin Muise + Justin Pryzby + Kacper Zuk + KaiGai Kohei + Karen Huddleston + Karl Lehenbauer + Karl O. Pinc + Keith Fiske + Kevin Grittner + Kim Rose Carlsen + Konstantin Evteev + Konstantin Knizhnik + Kuntal Ghosh + Kurt Kartaltepe + Kyle Conroy + Kyotaro Horiguchi + Laurenz Albe + Leonardo Cecchi + Ludovic Vaugeois-Pepin + Lukas Fittl + Magnus Hagander + Maksim Milyutin + Maksym Sobolyev + Marc Rassbach + Marc-Olaf Jaschke + Marcos Castedo + Marek Cvoren + Mark Dilger + Mark Kirkwood + Mark Pether + Marko Tiikkaja + Markus Winand + Marllius Ribeiro + Marti Raudsepp + Martín Marqués + Masahiko Sawada + Matheus Oliveira + Mathieu Fenniak + Merlin Moncure + Michael Banck + Michael Day + Michael Meskes + Michael Overmeyer + Michael Paquier + Mike Palmiotto + Milos Urbanek + Mithun Cy + Moshe Jacobson + Murtuza Zabuawala + Naoki Okano + Nathan Bossart + Nathan Wagner + Neha Khatri + Neha Sharma + Neil Anderson + Nicolas Baccelli + Nicolas Guini + Nicolas Thauvin + Nikhil Sontakke + Nikita Glukhov + Nikolaus Thiel + Nikolay Nikitin + Nikolay Shaplov + Noah Misch + Noriyoshi Shinoda + Olaf Gawenda + Oleg Bartunov + Oskari Saarenmaa + Otar Shavadze + Paresh More + Paul Jungwirth + Paul Ramsey + Pavan Deolasee + Pavel Golub + Pavel Hanák + Pavel Raiskup + Pavel Stehule + Peng Sun + Peter Eisentraut + Peter Geoghegan + Petr Jelínek + Philippe Beaudoin + Pierre-Emmanuel André + Piotr Stefaniak + Prabhat Sahu + QL Zhuo + Radek Slupik + Rafa de la Torre + Rafia Sabih + Ragnar Ouchterlony + Rahila Syed + Rajkumar Raghuwanshi + Regina Obe + Richard Pistole + Robert Haas + Robins Tharakan + Rod Taylor + Roman Shaposhnik + Rushabh Lathia + Ryan Murphy + Sandeep Thakkar + Scott Milliken + Sean Farrell + Sebastian Luque + Sehrope Sarkuni + Sergey Burladyan + Sergey Koposov + Shay Rojansky + Shinichi Matsuda + Sho Kato + Simon Riggs + Simone Gotti + Spencer Thomason + Stas Kelvich + Stepan Pesternikov + Stephen Frost + Steve Randall + Steve Singer + Steven Fackler + Steven Winfield + Suraj Kharage + Sveinn Sveinsson + Sven R. Kunze + Taiki Kondo + Takayuki Tsunakawa + Takeshi Ideriha + Tatsuo Ishii + Tatsuro Yamada + Teodor Sigaev + Thom Brown + Thomas Kellerer + Thomas Munro + Tim Goodaire + Tobias Bussmann + Tom Dunstan + Tom Lane + Tom van Tilburg + Tomas Vondra + Tomonari Katsumata + Tushar Ahuja + Vaishnavi Prabakaran + Venkata Balaji Nagothi + Vicky Vergara + Victor Wagner + Vik Fearing + Vinayak Pokale + Viren Negi + Vitaly Burovoy + Vladimir Kunshchikov + Vladimir Rusinov + Yi Wen Wong + Yugo Nagata + Zhen Ming Yang + Zhou Digoal + + + diff --git a/doc/src/sgml/release-9.6.sgml b/doc/src/sgml/release-9.6.sgml index dc811c4ca5d9b..09b6b902547c5 100644 --- a/doc/src/sgml/release-9.6.sgml +++ b/doc/src/sgml/release-9.6.sgml @@ -5140,7 +5140,7 @@ and many others in the same vein The new approach makes better use of the CPU cache for typical cache sizes and data volumes. Where necessary, the behavior can be adjusted via the new configuration parameter - . + replacement_sort_tuples. diff --git a/src/Makefile.global.in b/src/Makefile.global.in index 2b22f0de2997f..1a0faf9023df9 100644 --- a/src/Makefile.global.in +++ b/src/Makefile.global.in @@ -869,8 +869,13 @@ endif # enable_nls # gcov from foo.gcda (by "make coverage") # foo.c.gcov.out stdout captured when foo.c.gcov is created, mildly # interesting -# lcov.info lcov tracefile, built from gcda files in one directory, +# lcov_test.info +# lcov tracefile, built from gcda files in one directory, # later collected by "make coverage-html" +# lcov_base.info +# tracefile for zero counters for every file, so that +# even files that are not touched by tests are counted +# for the overall coverage rate ifeq ($(enable_coverage), yes) @@ -888,15 +893,26 @@ coverage: $(local_gcda_files:.gcda=.c.gcov) .PHONY: coverage-html coverage-html: coverage-html-stamp -coverage-html-stamp: lcov.info +GENHTML_FLAGS = --show-details --legend +GENHTML_TITLE = PostgreSQL $(VERSION) + +coverage-html-stamp: lcov_base.info lcov_test.info rm -rf coverage - $(GENHTML) --show-details --legend --output-directory=coverage --title=PostgreSQL --num-spaces=4 --prefix=$(abs_top_srcdir) $< + $(GENHTML) $(GENHTML_FLAGS) -o coverage --title='$(GENHTML_TITLE)' --num-spaces=4 --prefix='$(abs_top_srcdir)' $^ touch $@ +LCOV += --gcov-tool $(GCOV) +LCOVFLAGS = --no-external + +all_gcno_files = $(shell find . -name '*.gcno' -print) + +lcov_base.info: $(all_gcno_files) + $(LCOV) $(LCOVFLAGS) -c -i -d . -o $@ + all_gcda_files = $(shell find . -name '*.gcda' -print) -lcov.info: $(all_gcda_files) - $(LCOV) -d . -c -o $@ $(LCOVFLAGS) --gcov-tool $(GCOV) --no-external +lcov_test.info: $(all_gcda_files) + $(LCOV) $(LCOVFLAGS) -c -d . -o $@ # hook for clean-up @@ -905,7 +921,7 @@ clean distclean maintainer-clean: clean-coverage .PHONY: clean-coverage clean-coverage: rm -rf coverage coverage-html-stamp - rm -f *.gcda *.gcno lcov.info *.gcov .*.gcov *.gcov.out + rm -f *.gcda *.gcno lcov*.info *.gcov .*.gcov *.gcov.out # User-callable target to reset counts between test runs diff --git a/src/backend/access/transam/parallel.c b/src/backend/access/transam/parallel.c index 13c8ba3b19684..c6f7b7af0e1fd 100644 --- a/src/backend/access/transam/parallel.c +++ b/src/backend/access/transam/parallel.c @@ -467,6 +467,7 @@ LaunchParallelWorkers(ParallelContext *pcxt) memset(&worker, 0, sizeof(worker)); snprintf(worker.bgw_name, BGW_MAXLEN, "parallel worker for PID %d", MyProcPid); + snprintf(worker.bgw_type, BGW_MAXLEN, "parallel worker"); worker.bgw_flags = BGWORKER_SHMEM_ACCESS | BGWORKER_BACKEND_DATABASE_CONNECTION | BGWORKER_CLASS_PARALLEL; diff --git a/src/backend/catalog/dependency.c b/src/backend/catalog/dependency.c index 6fffc290fad8a..2668650f27238 100644 --- a/src/backend/catalog/dependency.c +++ b/src/backend/catalog/dependency.c @@ -1738,11 +1738,14 @@ find_expr_references_walker(Node *node, { ArrayCoerceExpr *acoerce = (ArrayCoerceExpr *) node; - if (OidIsValid(acoerce->elemfuncid)) - add_object_address(OCLASS_PROC, acoerce->elemfuncid, 0, - context->addrs); + /* as above, depend on type */ add_object_address(OCLASS_TYPE, acoerce->resulttype, 0, context->addrs); + /* the collation might not be referenced anywhere else, either */ + if (OidIsValid(acoerce->resultcollid) && + acoerce->resultcollid != DEFAULT_COLLATION_OID) + add_object_address(OCLASS_COLLATION, acoerce->resultcollid, 0, + context->addrs); /* fall through to examine arguments */ } else if (IsA(node, ConvertRowtypeExpr)) diff --git a/src/backend/commands/analyze.c b/src/backend/commands/analyze.c index 283845cf2a6ee..d432f8208dde6 100644 --- a/src/backend/commands/analyze.c +++ b/src/backend/commands/analyze.c @@ -106,6 +106,10 @@ static Datum ind_fetch_func(VacAttrStatsP stats, int rownum, bool *isNull); /* * analyze_rel() -- analyze one relation + * + * relid identifies the relation to analyze. If relation is supplied, use + * the name therein for reporting any failure to open/lock the rel; do not + * use it once we've successfully opened the rel, since it might be stale. */ void analyze_rel(Oid relid, RangeVar *relation, int options, @@ -145,7 +149,8 @@ analyze_rel(Oid relid, RangeVar *relation, int options, else { onerel = NULL; - if (IsAutoVacuumWorkerProcess() && params->log_min_duration >= 0) + if (relation && + IsAutoVacuumWorkerProcess() && params->log_min_duration >= 0) ereport(LOG, (errcode(ERRCODE_LOCK_NOT_AVAILABLE), errmsg("skipping analyze of \"%s\" --- lock not available", diff --git a/src/backend/commands/copy.c b/src/backend/commands/copy.c index 7c004ffad8a6b..e87588040fafb 100644 --- a/src/backend/commands/copy.c +++ b/src/backend/commands/copy.c @@ -17,8 +17,6 @@ #include #include #include -#include -#include #include "access/heapam.h" #include "access/htup_details.h" @@ -38,6 +36,7 @@ #include "optimizer/planner.h" #include "nodes/makefuncs.h" #include "parser/parse_relation.h" +#include "port/pg_bswap.h" #include "rewrite/rewriteHandler.h" #include "storage/fd.h" #include "tcop/tcopprot.h" @@ -671,7 +670,7 @@ CopySendInt32(CopyState cstate, int32 val) { uint32 buf; - buf = htonl((uint32) val); + buf = pg_hton32((uint32) val); CopySendData(cstate, &buf, sizeof(buf)); } @@ -690,7 +689,7 @@ CopyGetInt32(CopyState cstate, int32 *val) *val = 0; /* suppress compiler warning */ return false; } - *val = (int32) ntohl(buf); + *val = (int32) pg_ntoh32(buf); return true; } @@ -702,7 +701,7 @@ CopySendInt16(CopyState cstate, int16 val) { uint16 buf; - buf = htons((uint16) val); + buf = pg_hton16((uint16) val); CopySendData(cstate, &buf, sizeof(buf)); } @@ -719,7 +718,7 @@ CopyGetInt16(CopyState cstate, int16 *val) *val = 0; /* suppress compiler warning */ return false; } - *val = (int16) ntohs(buf); + *val = (int16) pg_ntoh16(buf); return true; } diff --git a/src/backend/commands/typecmds.c b/src/backend/commands/typecmds.c index 4c490ed5c1bba..c1b87e09e7473 100644 --- a/src/backend/commands/typecmds.c +++ b/src/backend/commands/typecmds.c @@ -729,6 +729,7 @@ ObjectAddress DefineDomain(CreateDomainStmt *stmt) { char *domainName; + char *domainArrayName; Oid domainNamespace; AclResult aclresult; int16 internalLength; @@ -757,6 +758,7 @@ DefineDomain(CreateDomainStmt *stmt) Oid basetypeoid; Oid old_type_oid; Oid domaincoll; + Oid domainArrayOid; Form_pg_type baseType; int32 basetypeMod; Oid baseColl; @@ -1027,6 +1029,9 @@ DefineDomain(CreateDomainStmt *stmt) } } + /* Allocate OID for array type */ + domainArrayOid = AssignTypeArrayOid(); + /* * Have TypeCreate do all the real work. */ @@ -1051,7 +1056,7 @@ DefineDomain(CreateDomainStmt *stmt) analyzeProcedure, /* analyze procedure */ InvalidOid, /* no array element type */ false, /* this isn't an array */ - InvalidOid, /* no arrays for domains (yet) */ + domainArrayOid, /* array type we are about to create */ basetypeoid, /* base type ID */ defaultValue, /* default type value (text) */ defaultValueBin, /* default type value (binary) */ @@ -1063,6 +1068,48 @@ DefineDomain(CreateDomainStmt *stmt) typNotNull, /* Type NOT NULL */ domaincoll); /* type's collation */ + /* + * Create the array type that goes with it. + */ + domainArrayName = makeArrayTypeName(domainName, domainNamespace); + + /* alignment must be 'i' or 'd' for arrays */ + alignment = (alignment == 'd') ? 'd' : 'i'; + + TypeCreate(domainArrayOid, /* force assignment of this type OID */ + domainArrayName, /* type name */ + domainNamespace, /* namespace */ + InvalidOid, /* relation oid (n/a here) */ + 0, /* relation kind (ditto) */ + GetUserId(), /* owner's ID */ + -1, /* internal size (always varlena) */ + TYPTYPE_BASE, /* type-type (base type) */ + TYPCATEGORY_ARRAY, /* type-category (array) */ + false, /* array types are never preferred */ + delimiter, /* array element delimiter */ + F_ARRAY_IN, /* input procedure */ + F_ARRAY_OUT, /* output procedure */ + F_ARRAY_RECV, /* receive procedure */ + F_ARRAY_SEND, /* send procedure */ + InvalidOid, /* typmodin procedure - none */ + InvalidOid, /* typmodout procedure - none */ + F_ARRAY_TYPANALYZE, /* analyze procedure */ + address.objectId, /* element type ID */ + true, /* yes this is an array type */ + InvalidOid, /* no further array type */ + InvalidOid, /* base type ID */ + NULL, /* never a default type value */ + NULL, /* binary default isn't sent either */ + false, /* never passed by value */ + alignment, /* see above */ + 'x', /* ARRAY is always toastable */ + -1, /* typMod (Domains only) */ + 0, /* Array dimensions of typbasetype */ + false, /* Type NOT NULL */ + domaincoll); /* type's collation */ + + pfree(domainArrayName); + /* * Process constraints which refer to the domain ID returned by TypeCreate */ @@ -1139,6 +1186,7 @@ DefineEnum(CreateEnumStmt *stmt) errmsg("type \"%s\" already exists", enumName))); } + /* Allocate OID for array type */ enumArrayOid = AssignTypeArrayOid(); /* Create the pg_type entry */ diff --git a/src/backend/commands/vacuum.c b/src/backend/commands/vacuum.c index faa181207a85d..d533cef6a6c19 100644 --- a/src/backend/commands/vacuum.c +++ b/src/backend/commands/vacuum.c @@ -128,9 +128,11 @@ ExecVacuum(VacuumStmt *vacstmt, bool isTopLevel) * * options is a bitmask of VacuumOption flags, indicating what to do. * - * relid, if not InvalidOid, indicate the relation to process; otherwise, - * the RangeVar is used. (The latter must always be passed, because it's - * used for error messages.) + * relid, if not InvalidOid, indicates the relation to process; otherwise, + * if a RangeVar is supplied, that's what to process; otherwise, we process + * all relevant tables in the database. (If both relid and a RangeVar are + * supplied, the relid is what is processed, but we use the RangeVar's name + * to report any open/lock failure.) * * params contains a set of parameters that can be used to customize the * behavior. @@ -226,8 +228,8 @@ vacuum(int options, RangeVar *relation, Oid relid, VacuumParams *params, vac_strategy = bstrategy; /* - * Build list of relations to process, unless caller gave us one. (If we - * build one, we put it in vac_context for safekeeping.) + * Build list of relation OID(s) to process, putting it in vac_context for + * safekeeping. */ relations = get_rel_oids(relid, relation); @@ -393,22 +395,18 @@ get_rel_oids(Oid relid, const RangeVar *vacrel) } else if (vacrel) { - /* Process a specific relation */ + /* Process a specific relation, and possibly partitions thereof */ Oid relid; HeapTuple tuple; Form_pg_class classForm; bool include_parts; /* - * Since we don't take a lock here, the relation might be gone, or the - * RangeVar might no longer refer to the OID we look up here. In the - * former case, VACUUM will do nothing; in the latter case, it will - * process the OID we looked up here, rather than the new one. Neither - * is ideal, but there's little practical alternative, since we're - * going to commit this transaction and begin a new one between now - * and then. + * We transiently take AccessShareLock to protect the syscache lookup + * below, as well as find_all_inheritors's expectation that the caller + * holds some lock on the starting relation. */ - relid = RangeVarGetRelid(vacrel, NoLock, false); + relid = RangeVarGetRelid(vacrel, AccessShareLock, false); /* * To check whether the relation is a partitioned table, fetch its @@ -422,10 +420,11 @@ get_rel_oids(Oid relid, const RangeVar *vacrel) ReleaseSysCache(tuple); /* - * Make relation list entries for this guy and its partitions, if any. - * Note that the list returned by find_all_inheritors() include the - * passed-in OID at its head. Also note that we did not request a - * lock to be taken to match what would be done otherwise. + * Make relation list entries for this rel and its partitions, if any. + * Note that the list returned by find_all_inheritors() includes the + * passed-in OID at its head. There's no point in taking locks on the + * individual partitions yet, and doing so would just add unnecessary + * deadlock risk. */ oldcontext = MemoryContextSwitchTo(vac_context); if (include_parts) @@ -434,6 +433,19 @@ get_rel_oids(Oid relid, const RangeVar *vacrel) else oid_list = lappend_oid(oid_list, relid); MemoryContextSwitchTo(oldcontext); + + /* + * Release lock again. This means that by the time we actually try to + * process the table, it might be gone or renamed. In the former case + * we'll silently ignore it; in the latter case we'll process it + * anyway, but we must beware that the RangeVar doesn't necessarily + * identify it anymore. This isn't ideal, perhaps, but there's little + * practical alternative, since we're typically going to commit this + * transaction and begin a new one between now and then. Moreover, + * holding locks on multiple relations would create significant risk + * of deadlock. + */ + UnlockRelationOid(relid, AccessShareLock); } else { @@ -463,7 +475,7 @@ get_rel_oids(Oid relid, const RangeVar *vacrel) classForm->relkind != RELKIND_PARTITIONED_TABLE) continue; - /* Make a relation list entry for this guy */ + /* Make a relation list entry for this rel */ oldcontext = MemoryContextSwitchTo(vac_context); oid_list = lappend_oid(oid_list, HeapTupleGetOid(tuple)); MemoryContextSwitchTo(oldcontext); @@ -1212,6 +1224,11 @@ vac_truncate_clog(TransactionId frozenXID, /* * vacuum_rel() -- vacuum one heap relation * + * relid identifies the relation to vacuum. If relation is supplied, + * use the name therein for reporting any failure to open/lock the rel; + * do not use it once we've successfully opened the rel, since it might + * be stale. + * * Doing one heap at a time incurs extra overhead, since we need to * check that the heap exists again just before we vacuum it. The * reason that we do this is so that vacuuming can be spread across @@ -1300,7 +1317,8 @@ vacuum_rel(Oid relid, RangeVar *relation, int options, VacuumParams *params) else { onerel = NULL; - if (IsAutoVacuumWorkerProcess() && params->log_min_duration >= 0) + if (relation && + IsAutoVacuumWorkerProcess() && params->log_min_duration >= 0) ereport(LOG, (errcode(ERRCODE_LOCK_NOT_AVAILABLE), errmsg("skipping vacuum of \"%s\" --- lock not available", @@ -1468,7 +1486,7 @@ vacuum_rel(Oid relid, RangeVar *relation, int options, VacuumParams *params) * totally unimportant for toast relations. */ if (toast_relid != InvalidOid) - vacuum_rel(toast_relid, relation, options, params); + vacuum_rel(toast_relid, NULL, options, params); /* * Now release the session-level lock on the master table. diff --git a/src/backend/executor/execExpr.c b/src/backend/executor/execExpr.c index be9d23bc323ba..e0839616e1966 100644 --- a/src/backend/executor/execExpr.c +++ b/src/backend/executor/execExpr.c @@ -1225,6 +1225,7 @@ ExecInitExprRec(Expr *node, PlanState *parent, ExprState *state, { ArrayCoerceExpr *acoerce = (ArrayCoerceExpr *) node; Oid resultelemtype; + ExprState *elemstate; /* evaluate argument into step's result area */ ExecInitExprRec(acoerce->arg, parent, state, resv, resnull); @@ -1234,42 +1235,49 @@ ExecInitExprRec(Expr *node, PlanState *parent, ExprState *state, ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE), errmsg("target type is not an array"))); - /* Arrays over domains aren't supported yet */ - Assert(getBaseType(resultelemtype) == resultelemtype); - scratch.opcode = EEOP_ARRAYCOERCE; - scratch.d.arraycoerce.coerceexpr = acoerce; - scratch.d.arraycoerce.resultelemtype = resultelemtype; + /* + * Construct a sub-expression for the per-element expression; + * but don't ready it until after we check it for triviality. + * We assume it hasn't any Var references, but does have a + * CaseTestExpr representing the source array element values. + */ + elemstate = makeNode(ExprState); + elemstate->expr = acoerce->elemexpr; + elemstate->innermost_caseval = (Datum *) palloc(sizeof(Datum)); + elemstate->innermost_casenull = (bool *) palloc(sizeof(bool)); - if (OidIsValid(acoerce->elemfuncid)) - { - AclResult aclresult; + ExecInitExprRec(acoerce->elemexpr, parent, elemstate, + &elemstate->resvalue, &elemstate->resnull); - /* Check permission to call function */ - aclresult = pg_proc_aclcheck(acoerce->elemfuncid, - GetUserId(), - ACL_EXECUTE); - if (aclresult != ACLCHECK_OK) - aclcheck_error(aclresult, ACL_KIND_PROC, - get_func_name(acoerce->elemfuncid)); - InvokeFunctionExecuteHook(acoerce->elemfuncid); + if (elemstate->steps_len == 1 && + elemstate->steps[0].opcode == EEOP_CASE_TESTVAL) + { + /* Trivial, so we need no per-element work at runtime */ + elemstate = NULL; + } + else + { + /* Not trivial, so append a DONE step */ + scratch.opcode = EEOP_DONE; + ExprEvalPushStep(elemstate, &scratch); + /* and ready the subexpression */ + ExecReadyExpr(elemstate); + } - /* Set up the primary fmgr lookup information */ - scratch.d.arraycoerce.elemfunc = - (FmgrInfo *) palloc0(sizeof(FmgrInfo)); - fmgr_info(acoerce->elemfuncid, - scratch.d.arraycoerce.elemfunc); - fmgr_info_set_expr((Node *) acoerce, - scratch.d.arraycoerce.elemfunc); + scratch.opcode = EEOP_ARRAYCOERCE; + scratch.d.arraycoerce.elemexprstate = elemstate; + scratch.d.arraycoerce.resultelemtype = resultelemtype; + if (elemstate) + { /* Set up workspace for array_map */ scratch.d.arraycoerce.amstate = (ArrayMapState *) palloc0(sizeof(ArrayMapState)); } else { - /* Don't need workspace if there's no conversion func */ - scratch.d.arraycoerce.elemfunc = NULL; + /* Don't need workspace if there's no subexpression */ scratch.d.arraycoerce.amstate = NULL; } diff --git a/src/backend/executor/execExprInterp.c b/src/backend/executor/execExprInterp.c index 09abd46dda87f..c5e97ef9e2d93 100644 --- a/src/backend/executor/execExprInterp.c +++ b/src/backend/executor/execExprInterp.c @@ -34,10 +34,8 @@ * * For very simple instructions the overhead of the full interpreter * "startup", as minimal as it is, is noticeable. Therefore - * ExecReadyInterpretedExpr will choose to implement simple scalar Var - * and Const expressions using special fast-path routines (ExecJust*). - * Benchmarking shows anything more complex than those may as well use the - * "full interpreter". + * ExecReadyInterpretedExpr will choose to implement certain simple + * opcode patterns using special fast-path routines (ExecJust*). * * Complex or uncommon instructions are not implemented in-line in * ExecInterpExpr(), rather we call out to a helper function appearing later @@ -149,6 +147,7 @@ static Datum ExecJustConst(ExprState *state, ExprContext *econtext, bool *isnull static Datum ExecJustAssignInnerVar(ExprState *state, ExprContext *econtext, bool *isnull); static Datum ExecJustAssignOuterVar(ExprState *state, ExprContext *econtext, bool *isnull); static Datum ExecJustAssignScanVar(ExprState *state, ExprContext *econtext, bool *isnull); +static Datum ExecJustApplyFuncToCase(ExprState *state, ExprContext *econtext, bool *isnull); /* @@ -184,10 +183,8 @@ ExecReadyInterpretedExpr(ExprState *state) /* * Select fast-path evalfuncs for very simple expressions. "Starting up" - * the full interpreter is a measurable overhead for these. Plain Vars - * and Const seem to be the only ones where the intrinsic cost is small - * enough that the overhead of ExecInterpExpr matters. For more complex - * expressions it's cheaper to use ExecInterpExpr always. + * the full interpreter is a measurable overhead for these, and these + * patterns occur often enough to be worth optimizing. */ if (state->steps_len == 3) { @@ -230,6 +227,13 @@ ExecReadyInterpretedExpr(ExprState *state) state->evalfunc = ExecJustAssignScanVar; return; } + else if (step0 == EEOP_CASE_TESTVAL && + step1 == EEOP_FUNCEXPR_STRICT && + state->steps[0].d.casetest.value) + { + state->evalfunc = ExecJustApplyFuncToCase; + return; + } } else if (state->steps_len == 2 && state->steps[0].opcode == EEOP_CONST) @@ -501,15 +505,17 @@ ExecInterpExpr(ExprState *state, ExprContext *econtext, bool *isnull) EEO_CASE(EEOP_INNER_SYSVAR) { int attnum = op->d.var.attnum; + Datum d; /* these asserts must match defenses in slot_getattr */ Assert(innerslot->tts_tuple != NULL); Assert(innerslot->tts_tuple != &(innerslot->tts_minhdr)); - /* heap_getsysattr has sufficient defenses against bad attnums */ - *op->resvalue = heap_getsysattr(innerslot->tts_tuple, attnum, - innerslot->tts_tupleDescriptor, - op->resnull); + /* heap_getsysattr has sufficient defenses against bad attnums */ + d = heap_getsysattr(innerslot->tts_tuple, attnum, + innerslot->tts_tupleDescriptor, + op->resnull); + *op->resvalue = d; EEO_NEXT(); } @@ -517,15 +523,17 @@ ExecInterpExpr(ExprState *state, ExprContext *econtext, bool *isnull) EEO_CASE(EEOP_OUTER_SYSVAR) { int attnum = op->d.var.attnum; + Datum d; /* these asserts must match defenses in slot_getattr */ Assert(outerslot->tts_tuple != NULL); Assert(outerslot->tts_tuple != &(outerslot->tts_minhdr)); /* heap_getsysattr has sufficient defenses against bad attnums */ - *op->resvalue = heap_getsysattr(outerslot->tts_tuple, attnum, - outerslot->tts_tupleDescriptor, - op->resnull); + d = heap_getsysattr(outerslot->tts_tuple, attnum, + outerslot->tts_tupleDescriptor, + op->resnull); + *op->resvalue = d; EEO_NEXT(); } @@ -533,15 +541,17 @@ ExecInterpExpr(ExprState *state, ExprContext *econtext, bool *isnull) EEO_CASE(EEOP_SCAN_SYSVAR) { int attnum = op->d.var.attnum; + Datum d; /* these asserts must match defenses in slot_getattr */ Assert(scanslot->tts_tuple != NULL); Assert(scanslot->tts_tuple != &(scanslot->tts_minhdr)); - /* heap_getsysattr has sufficient defenses against bad attnums */ - *op->resvalue = heap_getsysattr(scanslot->tts_tuple, attnum, - scanslot->tts_tupleDescriptor, - op->resnull); + /* heap_getsysattr has sufficient defenses against bad attnums */ + d = heap_getsysattr(scanslot->tts_tuple, attnum, + scanslot->tts_tupleDescriptor, + op->resnull); + *op->resvalue = d; EEO_NEXT(); } @@ -641,13 +651,22 @@ ExecInterpExpr(ExprState *state, ExprContext *econtext, bool *isnull) * As both STRICT checks and function-usage are noticeable performance * wise, and function calls are a very hot-path (they also back * operators!), it's worth having so many separate opcodes. + * + * Note: the reason for using a temporary variable "d", here and in + * other places, is that some compilers think "*op->resvalue = f();" + * requires them to evaluate op->resvalue into a register before + * calling f(), just in case f() is able to modify op->resvalue + * somehow. The extra line of code can save a useless register spill + * and reload across the function call. */ EEO_CASE(EEOP_FUNCEXPR) { FunctionCallInfo fcinfo = op->d.func.fcinfo_data; + Datum d; fcinfo->isnull = false; - *op->resvalue = op->d.func.fn_addr(fcinfo); + d = op->d.func.fn_addr(fcinfo); + *op->resvalue = d; *op->resnull = fcinfo->isnull; EEO_NEXT(); @@ -658,6 +677,7 @@ ExecInterpExpr(ExprState *state, ExprContext *econtext, bool *isnull) FunctionCallInfo fcinfo = op->d.func.fcinfo_data; bool *argnull = fcinfo->argnull; int argno; + Datum d; /* strict function, so check for NULL args */ for (argno = 0; argno < op->d.func.nargs; argno++) @@ -669,7 +689,8 @@ ExecInterpExpr(ExprState *state, ExprContext *econtext, bool *isnull) } } fcinfo->isnull = false; - *op->resvalue = op->d.func.fn_addr(fcinfo); + d = op->d.func.fn_addr(fcinfo); + *op->resvalue = d; *op->resnull = fcinfo->isnull; strictfail: @@ -680,11 +701,13 @@ ExecInterpExpr(ExprState *state, ExprContext *econtext, bool *isnull) { FunctionCallInfo fcinfo = op->d.func.fcinfo_data; PgStat_FunctionCallUsage fcusage; + Datum d; pgstat_init_function_usage(fcinfo, &fcusage); fcinfo->isnull = false; - *op->resvalue = op->d.func.fn_addr(fcinfo); + d = op->d.func.fn_addr(fcinfo); + *op->resvalue = d; *op->resnull = fcinfo->isnull; pgstat_end_function_usage(&fcusage, true); @@ -698,6 +721,7 @@ ExecInterpExpr(ExprState *state, ExprContext *econtext, bool *isnull) PgStat_FunctionCallUsage fcusage; bool *argnull = fcinfo->argnull; int argno; + Datum d; /* strict function, so check for NULL args */ for (argno = 0; argno < op->d.func.nargs; argno++) @@ -712,7 +736,8 @@ ExecInterpExpr(ExprState *state, ExprContext *econtext, bool *isnull) pgstat_init_function_usage(fcinfo, &fcusage); fcinfo->isnull = false; - *op->resvalue = op->d.func.fn_addr(fcinfo); + d = op->d.func.fn_addr(fcinfo); + *op->resvalue = d; *op->resnull = fcinfo->isnull; pgstat_end_function_usage(&fcusage, true); @@ -1113,6 +1138,7 @@ ExecInterpExpr(ExprState *state, ExprContext *econtext, bool *isnull) if (!op->d.iocoerce.finfo_in->fn_strict || str != NULL) { FunctionCallInfo fcinfo_in; + Datum d; fcinfo_in = op->d.iocoerce.fcinfo_data_in; fcinfo_in->arg[0] = PointerGetDatum(str); @@ -1120,7 +1146,8 @@ ExecInterpExpr(ExprState *state, ExprContext *econtext, bool *isnull) /* second and third arguments are already set up */ fcinfo_in->isnull = false; - *op->resvalue = FunctionCallInvoke(fcinfo_in); + d = FunctionCallInvoke(fcinfo_in); + *op->resvalue = d; /* Should get null result if and only if str is NULL */ if (str == NULL) @@ -1252,7 +1279,7 @@ ExecInterpExpr(ExprState *state, ExprContext *econtext, bool *isnull) EEO_CASE(EEOP_ARRAYCOERCE) { /* too complex for an inline implementation */ - ExecEvalArrayCoerce(state, op); + ExecEvalArrayCoerce(state, op, econtext); EEO_NEXT(); } @@ -1268,6 +1295,7 @@ ExecInterpExpr(ExprState *state, ExprContext *econtext, bool *isnull) EEO_CASE(EEOP_ROWCOMPARE_STEP) { FunctionCallInfo fcinfo = op->d.rowcompare_step.fcinfo_data; + Datum d; /* force NULL result if strict fn and NULL input */ if (op->d.rowcompare_step.finfo->fn_strict && @@ -1279,7 +1307,8 @@ ExecInterpExpr(ExprState *state, ExprContext *econtext, bool *isnull) /* Apply comparison function */ fcinfo->isnull = false; - *op->resvalue = op->d.rowcompare_step.fn_addr(fcinfo); + d = op->d.rowcompare_step.fn_addr(fcinfo); + *op->resvalue = d; /* force NULL result if NULL function result */ if (fcinfo->isnull) @@ -1786,6 +1815,43 @@ ExecJustAssignScanVar(ExprState *state, ExprContext *econtext, bool *isnull) return 0; } +/* Evaluate CASE_TESTVAL and apply a strict function to it */ +static Datum +ExecJustApplyFuncToCase(ExprState *state, ExprContext *econtext, bool *isnull) +{ + ExprEvalStep *op = &state->steps[0]; + FunctionCallInfo fcinfo; + bool *argnull; + int argno; + Datum d; + + /* + * XXX with some redesign of the CaseTestExpr mechanism, maybe we could + * get rid of this data shuffling? + */ + *op->resvalue = *op->d.casetest.value; + *op->resnull = *op->d.casetest.isnull; + + op++; + + fcinfo = op->d.func.fcinfo_data; + argnull = fcinfo->argnull; + + /* strict function, so check for NULL args */ + for (argno = 0; argno < op->d.func.nargs; argno++) + { + if (argnull[argno]) + { + *isnull = true; + return (Datum) 0; + } + } + fcinfo->isnull = false; + d = op->d.func.fn_addr(fcinfo); + *isnull = fcinfo->isnull; + return d; +} + /* * Do one-time initialization of interpretation machinery. @@ -2320,11 +2386,9 @@ ExecEvalArrayExpr(ExprState *state, ExprEvalStep *op) * Source array is in step's result variable. */ void -ExecEvalArrayCoerce(ExprState *state, ExprEvalStep *op) +ExecEvalArrayCoerce(ExprState *state, ExprEvalStep *op, ExprContext *econtext) { - ArrayCoerceExpr *acoerce = op->d.arraycoerce.coerceexpr; Datum arraydatum; - FunctionCallInfoData locfcinfo; /* NULL array -> NULL result */ if (*op->resnull) @@ -2336,7 +2400,7 @@ ExecEvalArrayCoerce(ExprState *state, ExprEvalStep *op) * If it's binary-compatible, modify the element type in the array header, * but otherwise leave the array as we received it. */ - if (!OidIsValid(acoerce->elemfuncid)) + if (op->d.arraycoerce.elemexprstate == NULL) { /* Detoast input array if necessary, and copy in any case */ ArrayType *array = DatumGetArrayTypePCopy(arraydatum); @@ -2347,23 +2411,12 @@ ExecEvalArrayCoerce(ExprState *state, ExprEvalStep *op) } /* - * Use array_map to apply the function to each array element. - * - * We pass on the desttypmod and isExplicit flags whether or not the - * function wants them. - * - * Note: coercion functions are assumed to not use collation. + * Use array_map to apply the sub-expression to each array element. */ - InitFunctionCallInfoData(locfcinfo, op->d.arraycoerce.elemfunc, 3, - InvalidOid, NULL, NULL); - locfcinfo.arg[0] = arraydatum; - locfcinfo.arg[1] = Int32GetDatum(acoerce->resulttypmod); - locfcinfo.arg[2] = BoolGetDatum(acoerce->isExplicit); - locfcinfo.argnull[0] = false; - locfcinfo.argnull[1] = false; - locfcinfo.argnull[2] = false; - - *op->resvalue = array_map(&locfcinfo, op->d.arraycoerce.resultelemtype, + *op->resvalue = array_map(arraydatum, + op->d.arraycoerce.elemexprstate, + econtext, + op->d.arraycoerce.resultelemtype, op->d.arraycoerce.amstate); } diff --git a/src/backend/libpq/auth.c b/src/backend/libpq/auth.c index 39a57d48353b2..480e344eb3a45 100644 --- a/src/backend/libpq/auth.c +++ b/src/backend/libpq/auth.c @@ -18,7 +18,6 @@ #include #include #include -#include #include #ifdef HAVE_SYS_SELECT_H #include @@ -33,6 +32,7 @@ #include "libpq/pqformat.h" #include "libpq/scram.h" #include "miscadmin.h" +#include "port/pg_bswap.h" #include "replication/walsender.h" #include "storage/ipc.h" #include "utils/backend_random.h" @@ -2840,7 +2840,7 @@ PerformRadiusTransaction(char *server, char *secret, char *portstr, char *identi radius_packet *receivepacket = &radius_recv_pack; char *radius_buffer = (char *) &radius_send_pack; char *receive_buffer = (char *) &radius_recv_pack; - int32 service = htonl(RADIUS_AUTHENTICATE_ONLY); + int32 service = pg_hton32(RADIUS_AUTHENTICATE_ONLY); uint8 *cryptvector; int encryptedpasswordlen; uint8 encryptedpassword[RADIUS_MAX_PASSWORD_LENGTH]; @@ -2948,7 +2948,7 @@ PerformRadiusTransaction(char *server, char *secret, char *portstr, char *identi /* Length needs to be in network order on the wire */ packetlength = packet->length; - packet->length = htons(packet->length); + packet->length = pg_hton16(packet->length); sock = socket(serveraddrs[0].ai_family, SOCK_DGRAM, 0); if (sock == PGINVALID_SOCKET) @@ -3074,19 +3074,19 @@ PerformRadiusTransaction(char *server, char *secret, char *portstr, char *identi } #ifdef HAVE_IPV6 - if (remoteaddr.sin6_port != htons(port)) + if (remoteaddr.sin6_port != pg_hton16(port)) #else - if (remoteaddr.sin_port != htons(port)) + if (remoteaddr.sin_port != pg_hton16(port)) #endif { #ifdef HAVE_IPV6 ereport(LOG, (errmsg("RADIUS response from %s was sent from incorrect port: %d", - server, ntohs(remoteaddr.sin6_port)))); + server, pg_ntoh16(remoteaddr.sin6_port)))); #else ereport(LOG, (errmsg("RADIUS response from %s was sent from incorrect port: %d", - server, ntohs(remoteaddr.sin_port)))); + server, pg_ntoh16(remoteaddr.sin_port)))); #endif continue; } @@ -3098,11 +3098,11 @@ PerformRadiusTransaction(char *server, char *secret, char *portstr, char *identi continue; } - if (packetlength != ntohs(receivepacket->length)) + if (packetlength != pg_ntoh16(receivepacket->length)) { ereport(LOG, (errmsg("RADIUS response from %s has corrupt length: %d (actual length %d)", - server, ntohs(receivepacket->length), packetlength))); + server, pg_ntoh16(receivepacket->length), packetlength))); continue; } diff --git a/src/backend/libpq/ifaddr.c b/src/backend/libpq/ifaddr.c index 53bf6bcd80ffb..b8c463b101337 100644 --- a/src/backend/libpq/ifaddr.c +++ b/src/backend/libpq/ifaddr.c @@ -27,10 +27,10 @@ #ifdef HAVE_NETINET_TCP_H #include #endif -#include #include #include "libpq/ifaddr.h" +#include "port/pg_bswap.h" static int range_sockaddr_AF_INET(const struct sockaddr_in *addr, const struct sockaddr_in *netaddr, @@ -144,7 +144,7 @@ pg_sockaddr_cidr_mask(struct sockaddr_storage *mask, char *numbits, int family) & 0xffffffffUL; else maskl = 0; - mask4.sin_addr.s_addr = htonl(maskl); + mask4.sin_addr.s_addr = pg_hton32(maskl); memcpy(mask, &mask4, sizeof(mask4)); break; } @@ -568,7 +568,7 @@ pg_foreach_ifaddr(PgIfAddrCallback callback, void *cb_data) /* addr 127.0.0.1/8 */ memset(&addr, 0, sizeof(addr)); addr.sin_family = AF_INET; - addr.sin_addr.s_addr = ntohl(0x7f000001); + addr.sin_addr.s_addr = pg_ntoh32(0x7f000001); memset(&mask, 0, sizeof(mask)); pg_sockaddr_cidr_mask(&mask, "8", AF_INET); run_ifaddr_callback(callback, cb_data, diff --git a/src/backend/libpq/pqcomm.c b/src/backend/libpq/pqcomm.c index 4452ea4228cb8..754154b83bb53 100644 --- a/src/backend/libpq/pqcomm.c +++ b/src/backend/libpq/pqcomm.c @@ -81,7 +81,6 @@ #ifdef HAVE_NETINET_TCP_H #include #endif -#include #ifdef HAVE_UTIME_H #include #endif @@ -92,6 +91,7 @@ #include "common/ip.h" #include "libpq/libpq.h" #include "miscadmin.h" +#include "port/pg_bswap.h" #include "storage/ipc.h" #include "utils/guc.h" #include "utils/memutils.h" @@ -1286,7 +1286,7 @@ pq_getmessage(StringInfo s, int maxlen) return EOF; } - len = ntohl(len); + len = pg_ntoh32(len); if (len < 4 || (maxlen > 0 && len > maxlen)) @@ -1569,7 +1569,7 @@ socket_putmessage(char msgtype, const char *s, size_t len) { uint32 n32; - n32 = htonl((uint32) (len + 4)); + n32 = pg_hton32((uint32) (len + 4)); if (internal_putbytes((char *) &n32, 4)) goto fail; } diff --git a/src/backend/libpq/pqformat.c b/src/backend/libpq/pqformat.c index c8cf67c041333..f27a04f8344e0 100644 --- a/src/backend/libpq/pqformat.c +++ b/src/backend/libpq/pqformat.c @@ -72,12 +72,11 @@ #include "postgres.h" #include -#include -#include #include "libpq/libpq.h" #include "libpq/pqformat.h" #include "mb/pg_wchar.h" +#include "port/pg_bswap.h" /* -------------------------------- @@ -246,11 +245,11 @@ pq_sendint(StringInfo buf, int i, int b) appendBinaryStringInfo(buf, (char *) &n8, 1); break; case 2: - n16 = htons((uint16) i); + n16 = pg_hton16((uint16) i); appendBinaryStringInfo(buf, (char *) &n16, 2); break; case 4: - n32 = htonl((uint32) i); + n32 = pg_hton32((uint32) i); appendBinaryStringInfo(buf, (char *) &n32, 4); break; default: @@ -270,17 +269,9 @@ pq_sendint(StringInfo buf, int i, int b) void pq_sendint64(StringInfo buf, int64 i) { - uint32 n32; - - /* High order half first, since we're doing MSB-first */ - n32 = (uint32) (i >> 32); - n32 = htonl(n32); - appendBinaryStringInfo(buf, (char *) &n32, 4); + uint64 n64 = pg_hton64(i); - /* Now the low order half */ - n32 = (uint32) i; - n32 = htonl(n32); - appendBinaryStringInfo(buf, (char *) &n32, 4); + appendBinaryStringInfo(buf, (char *) &n64, sizeof(n64)); } /* -------------------------------- @@ -304,7 +295,7 @@ pq_sendfloat4(StringInfo buf, float4 f) } swap; swap.f = f; - swap.i = htonl(swap.i); + swap.i = pg_hton32(swap.i); appendBinaryStringInfo(buf, (char *) &swap.i, 4); } @@ -460,11 +451,11 @@ pq_getmsgint(StringInfo msg, int b) break; case 2: pq_copymsgbytes(msg, (char *) &n16, 2); - result = ntohs(n16); + result = pg_ntoh16(n16); break; case 4: pq_copymsgbytes(msg, (char *) &n32, 4); - result = ntohl(n32); + result = pg_ntoh32(n32); break; default: elog(ERROR, "unsupported integer size %d", b); @@ -485,20 +476,11 @@ pq_getmsgint(StringInfo msg, int b) int64 pq_getmsgint64(StringInfo msg) { - int64 result; - uint32 h32; - uint32 l32; + uint64 n64; - pq_copymsgbytes(msg, (char *) &h32, 4); - pq_copymsgbytes(msg, (char *) &l32, 4); - h32 = ntohl(h32); - l32 = ntohl(l32); + pq_copymsgbytes(msg, (char *) &n64, sizeof(n64)); - result = h32; - result <<= 32; - result |= l32; - - return result; + return pg_ntoh64(n64); } /* -------------------------------- diff --git a/src/backend/nodes/copyfuncs.c b/src/backend/nodes/copyfuncs.c index f1bed14e2bbc4..b274af26a4278 100644 --- a/src/backend/nodes/copyfuncs.c +++ b/src/backend/nodes/copyfuncs.c @@ -1698,11 +1698,10 @@ _copyArrayCoerceExpr(const ArrayCoerceExpr *from) ArrayCoerceExpr *newnode = makeNode(ArrayCoerceExpr); COPY_NODE_FIELD(arg); - COPY_SCALAR_FIELD(elemfuncid); + COPY_NODE_FIELD(elemexpr); COPY_SCALAR_FIELD(resulttype); COPY_SCALAR_FIELD(resulttypmod); COPY_SCALAR_FIELD(resultcollid); - COPY_SCALAR_FIELD(isExplicit); COPY_SCALAR_FIELD(coerceformat); COPY_LOCATION_FIELD(location); diff --git a/src/backend/nodes/equalfuncs.c b/src/backend/nodes/equalfuncs.c index 8b56b9146a1e7..5c839f4c31a6a 100644 --- a/src/backend/nodes/equalfuncs.c +++ b/src/backend/nodes/equalfuncs.c @@ -513,11 +513,10 @@ static bool _equalArrayCoerceExpr(const ArrayCoerceExpr *a, const ArrayCoerceExpr *b) { COMPARE_NODE_FIELD(arg); - COMPARE_SCALAR_FIELD(elemfuncid); + COMPARE_NODE_FIELD(elemexpr); COMPARE_SCALAR_FIELD(resulttype); COMPARE_SCALAR_FIELD(resulttypmod); COMPARE_SCALAR_FIELD(resultcollid); - COMPARE_SCALAR_FIELD(isExplicit); COMPARE_COERCIONFORM_FIELD(coerceformat); COMPARE_LOCATION_FIELD(location); diff --git a/src/backend/nodes/nodeFuncs.c b/src/backend/nodes/nodeFuncs.c index e3eb0c578877a..8e6f27e153686 100644 --- a/src/backend/nodes/nodeFuncs.c +++ b/src/backend/nodes/nodeFuncs.c @@ -1717,15 +1717,6 @@ check_functions_in_node(Node *node, check_function_callback checker, return true; } break; - case T_ArrayCoerceExpr: - { - ArrayCoerceExpr *expr = (ArrayCoerceExpr *) node; - - if (OidIsValid(expr->elemfuncid) && - checker(expr->elemfuncid, context)) - return true; - } - break; case T_RowCompareExpr: { RowCompareExpr *rcexpr = (RowCompareExpr *) node; @@ -2023,7 +2014,15 @@ expression_tree_walker(Node *node, case T_CoerceViaIO: return walker(((CoerceViaIO *) node)->arg, context); case T_ArrayCoerceExpr: - return walker(((ArrayCoerceExpr *) node)->arg, context); + { + ArrayCoerceExpr *acoerce = (ArrayCoerceExpr *) node; + + if (walker(acoerce->arg, context)) + return true; + if (walker(acoerce->elemexpr, context)) + return true; + } + break; case T_ConvertRowtypeExpr: return walker(((ConvertRowtypeExpr *) node)->arg, context); case T_CollateExpr: @@ -2705,6 +2704,7 @@ expression_tree_mutator(Node *node, FLATCOPY(newnode, acoerce, ArrayCoerceExpr); MUTATE(newnode->arg, acoerce->arg, Expr *); + MUTATE(newnode->elemexpr, acoerce->elemexpr, Expr *); return (Node *) newnode; } break; diff --git a/src/backend/nodes/outfuncs.c b/src/backend/nodes/outfuncs.c index b83d919e408ca..2532edc94a2b2 100644 --- a/src/backend/nodes/outfuncs.c +++ b/src/backend/nodes/outfuncs.c @@ -1394,11 +1394,10 @@ _outArrayCoerceExpr(StringInfo str, const ArrayCoerceExpr *node) WRITE_NODE_TYPE("ARRAYCOERCEEXPR"); WRITE_NODE_FIELD(arg); - WRITE_OID_FIELD(elemfuncid); + WRITE_NODE_FIELD(elemexpr); WRITE_OID_FIELD(resulttype); WRITE_INT_FIELD(resulttypmod); WRITE_OID_FIELD(resultcollid); - WRITE_BOOL_FIELD(isExplicit); WRITE_ENUM_FIELD(coerceformat, CoercionForm); WRITE_LOCATION_FIELD(location); } diff --git a/src/backend/nodes/readfuncs.c b/src/backend/nodes/readfuncs.c index fbf8330735835..07ba69178c84d 100644 --- a/src/backend/nodes/readfuncs.c +++ b/src/backend/nodes/readfuncs.c @@ -892,11 +892,10 @@ _readArrayCoerceExpr(void) READ_LOCALS(ArrayCoerceExpr); READ_NODE_FIELD(arg); - READ_OID_FIELD(elemfuncid); + READ_NODE_FIELD(elemexpr); READ_OID_FIELD(resulttype); READ_INT_FIELD(resulttypmod); READ_OID_FIELD(resultcollid); - READ_BOOL_FIELD(isExplicit); READ_ENUM_FIELD(coerceformat, CoercionForm); READ_LOCATION_FIELD(location); diff --git a/src/backend/optimizer/path/costsize.c b/src/backend/optimizer/path/costsize.c index 0baf9785c98e1..f76da49044704 100644 --- a/src/backend/optimizer/path/costsize.c +++ b/src/backend/optimizer/path/costsize.c @@ -3632,11 +3632,14 @@ cost_qual_eval_walker(Node *node, cost_qual_eval_context *context) else if (IsA(node, ArrayCoerceExpr)) { ArrayCoerceExpr *acoerce = (ArrayCoerceExpr *) node; - Node *arraynode = (Node *) acoerce->arg; - - if (OidIsValid(acoerce->elemfuncid)) - context->total.per_tuple += get_func_cost(acoerce->elemfuncid) * - cpu_operator_cost * estimate_array_length(arraynode); + QualCost perelemcost; + + cost_qual_eval_node(&perelemcost, (Node *) acoerce->elemexpr, + context->root); + context->total.startup += perelemcost.startup; + if (perelemcost.per_tuple > 0) + context->total.per_tuple += perelemcost.per_tuple * + estimate_array_length((Node *) acoerce->arg); } else if (IsA(node, RowCompareExpr)) { diff --git a/src/backend/optimizer/plan/setrefs.c b/src/backend/optimizer/plan/setrefs.c index b0c9e94459780..dee4414cec228 100644 --- a/src/backend/optimizer/plan/setrefs.c +++ b/src/backend/optimizer/plan/setrefs.c @@ -1395,12 +1395,6 @@ fix_expr_common(PlannerInfo *root, Node *node) record_plan_function_dependency(root, ((ScalarArrayOpExpr *) node)->opfuncid); } - else if (IsA(node, ArrayCoerceExpr)) - { - if (OidIsValid(((ArrayCoerceExpr *) node)->elemfuncid)) - record_plan_function_dependency(root, - ((ArrayCoerceExpr *) node)->elemfuncid); - } else if (IsA(node, Const)) { Const *con = (Const *) node; diff --git a/src/backend/optimizer/prep/preptlist.c b/src/backend/optimizer/prep/preptlist.c index 9d75e8612ae73..d7db32ebf5e46 100644 --- a/src/backend/optimizer/prep/preptlist.c +++ b/src/backend/optimizer/prep/preptlist.c @@ -306,9 +306,9 @@ expand_targetlist(List *tlist, int command_type, new_expr = coerce_to_domain(new_expr, InvalidOid, -1, atttype, + COERCION_IMPLICIT, COERCE_IMPLICIT_CAST, -1, - false, false); } else diff --git a/src/backend/optimizer/util/clauses.c b/src/backend/optimizer/util/clauses.c index 93add27dbe2f0..79613622805c2 100644 --- a/src/backend/optimizer/util/clauses.c +++ b/src/backend/optimizer/util/clauses.c @@ -1361,6 +1361,17 @@ contain_nonstrict_functions_walker(Node *node, void *context) return true; if (IsA(node, FieldStore)) return true; + if (IsA(node, ArrayCoerceExpr)) + { + /* + * ArrayCoerceExpr is strict at the array level, regardless of what + * the per-element expression is; so we should ignore elemexpr and + * recurse only into the arg. + */ + return expression_tree_walker((Node *) ((ArrayCoerceExpr *) node)->arg, + contain_nonstrict_functions_walker, + context); + } if (IsA(node, CaseExpr)) return true; if (IsA(node, ArrayExpr)) @@ -1380,14 +1391,11 @@ contain_nonstrict_functions_walker(Node *node, void *context) if (IsA(node, BooleanTest)) return true; - /* - * Check other function-containing nodes; but ArrayCoerceExpr is strict at - * the array level, regardless of elemfunc. - */ - if (!IsA(node, ArrayCoerceExpr) && - check_functions_in_node(node, contain_nonstrict_functions_checker, + /* Check other function-containing nodes */ + if (check_functions_in_node(node, contain_nonstrict_functions_checker, context)) return true; + return expression_tree_walker(node, contain_nonstrict_functions_walker, context); } @@ -1757,7 +1765,7 @@ find_nonnullable_rels_walker(Node *node, bool top_level) } else if (IsA(node, ArrayCoerceExpr)) { - /* ArrayCoerceExpr is strict at the array level */ + /* ArrayCoerceExpr is strict at the array level; ignore elemexpr */ ArrayCoerceExpr *expr = (ArrayCoerceExpr *) node; result = find_nonnullable_rels_walker((Node *) expr->arg, top_level); @@ -1965,7 +1973,7 @@ find_nonnullable_vars_walker(Node *node, bool top_level) } else if (IsA(node, ArrayCoerceExpr)) { - /* ArrayCoerceExpr is strict at the array level */ + /* ArrayCoerceExpr is strict at the array level; ignore elemexpr */ ArrayCoerceExpr *expr = (ArrayCoerceExpr *) node; result = find_nonnullable_vars_walker((Node *) expr->arg, top_level); @@ -3005,32 +3013,38 @@ eval_const_expressions_mutator(Node *node, { ArrayCoerceExpr *expr = (ArrayCoerceExpr *) node; Expr *arg; + Expr *elemexpr; ArrayCoerceExpr *newexpr; /* - * Reduce constants in the ArrayCoerceExpr's argument, then - * build a new ArrayCoerceExpr. + * Reduce constants in the ArrayCoerceExpr's argument and + * per-element expressions, then build a new ArrayCoerceExpr. */ arg = (Expr *) eval_const_expressions_mutator((Node *) expr->arg, context); + elemexpr = (Expr *) eval_const_expressions_mutator((Node *) expr->elemexpr, + context); newexpr = makeNode(ArrayCoerceExpr); newexpr->arg = arg; - newexpr->elemfuncid = expr->elemfuncid; + newexpr->elemexpr = elemexpr; newexpr->resulttype = expr->resulttype; newexpr->resulttypmod = expr->resulttypmod; newexpr->resultcollid = expr->resultcollid; - newexpr->isExplicit = expr->isExplicit; newexpr->coerceformat = expr->coerceformat; newexpr->location = expr->location; /* - * If constant argument and it's a binary-coercible or - * immutable conversion, we can simplify it to a constant. + * If constant argument and per-element expression is + * immutable, we can simplify the whole thing to a constant. + * Exception: although contain_mutable_functions considers + * CoerceToDomain immutable for historical reasons, let's not + * do so here; this ensures coercion to an array-over-domain + * does not apply the domain's constraints until runtime. */ if (arg && IsA(arg, Const) && - (!OidIsValid(newexpr->elemfuncid) || - func_volatile(newexpr->elemfuncid) == PROVOLATILE_IMMUTABLE)) + elemexpr && !IsA(elemexpr, CoerceToDomain) && + !contain_mutable_functions((Node *) elemexpr)) return (Node *) evaluate_expr((Expr *) newexpr, newexpr->resulttype, newexpr->resulttypmod, diff --git a/src/backend/parser/parse_coerce.c b/src/backend/parser/parse_coerce.c index e79ad26e71658..53457dc2c8df3 100644 --- a/src/backend/parser/parse_coerce.c +++ b/src/backend/parser/parse_coerce.c @@ -34,15 +34,16 @@ static Node *coerce_type_typmod(Node *node, Oid targetTypeId, int32 targetTypMod, - CoercionForm cformat, int location, - bool isExplicit, bool hideInputCoercion); + CoercionContext ccontext, CoercionForm cformat, + int location, + bool hideInputCoercion); static void hide_coercion_node(Node *node); static Node *build_coercion_expression(Node *node, CoercionPathType pathtype, Oid funcId, Oid targetTypeId, int32 targetTypMod, - CoercionForm cformat, int location, - bool isExplicit); + CoercionContext ccontext, CoercionForm cformat, + int location); static Node *coerce_record_to_complex(ParseState *pstate, Node *node, Oid targetTypeId, CoercionContext ccontext, @@ -110,8 +111,7 @@ coerce_to_target_type(ParseState *pstate, Node *expr, Oid exprtype, */ result = coerce_type_typmod(result, targettype, targettypmod, - cformat, location, - (cformat != COERCE_IMPLICIT_CAST), + ccontext, cformat, location, (result != expr && !IsA(result, Const))); if (expr != origexpr) @@ -355,7 +355,8 @@ coerce_type(ParseState *pstate, Node *node, result = coerce_to_domain(result, baseTypeId, baseTypeMod, targetTypeId, - cformat, location, false, false); + ccontext, cformat, location, + false); ReleaseSysCache(baseType); @@ -370,10 +371,10 @@ coerce_type(ParseState *pstate, Node *node, * NULL to indicate we should proceed with normal coercion. */ result = pstate->p_coerce_param_hook(pstate, - (Param *) node, - targetTypeId, - targetTypeMod, - location); + (Param *) node, + targetTypeId, + targetTypeMod, + location); if (result) return result; } @@ -417,20 +418,17 @@ coerce_type(ParseState *pstate, Node *node, result = build_coercion_expression(node, pathtype, funcId, baseTypeId, baseTypeMod, - cformat, location, - (cformat != COERCE_IMPLICIT_CAST)); + ccontext, cformat, location); /* * If domain, coerce to the domain type and relabel with domain - * type ID. We can skip the internal length-coercion step if the - * selected coercion function was a type-and-length coercion. + * type ID, hiding the previous coercion node. */ if (targetTypeId != baseTypeId) result = coerce_to_domain(result, baseTypeId, baseTypeMod, targetTypeId, - cformat, location, true, - exprIsLengthCoercion(result, - NULL)); + ccontext, cformat, location, + true); } else { @@ -444,7 +442,8 @@ coerce_type(ParseState *pstate, Node *node, * then we won't need a RelabelType node. */ result = coerce_to_domain(node, InvalidOid, -1, targetTypeId, - cformat, location, false, false); + ccontext, cformat, location, + false); if (result == node) { /* @@ -636,19 +635,17 @@ can_coerce_type(int nargs, Oid *input_typeids, Oid *target_typeids, * 'baseTypeMod': base type typmod of domain, if known (pass -1 if caller * has not bothered to look this up) * 'typeId': target type to coerce to - * 'cformat': coercion format + * 'ccontext': context indicator to control coercions + * 'cformat': coercion display format * 'location': coercion request location * 'hideInputCoercion': if true, hide the input coercion under this one. - * 'lengthCoercionDone': if true, caller already accounted for length, - * ie the input is already of baseTypMod as well as baseTypeId. * * If the target type isn't a domain, the given 'arg' is returned as-is. */ Node * coerce_to_domain(Node *arg, Oid baseTypeId, int32 baseTypeMod, Oid typeId, - CoercionForm cformat, int location, - bool hideInputCoercion, - bool lengthCoercionDone) + CoercionContext ccontext, CoercionForm cformat, int location, + bool hideInputCoercion) { CoerceToDomain *result; @@ -677,14 +674,9 @@ coerce_to_domain(Node *arg, Oid baseTypeId, int32 baseTypeMod, Oid typeId, * would be safe to do anyway, without lots of knowledge about what the * base type thinks the typmod means. */ - if (!lengthCoercionDone) - { - if (baseTypeMod >= 0) - arg = coerce_type_typmod(arg, baseTypeId, baseTypeMod, - COERCE_IMPLICIT_CAST, location, - (cformat != COERCE_IMPLICIT_CAST), - false); - } + arg = coerce_type_typmod(arg, baseTypeId, baseTypeMod, + ccontext, COERCE_IMPLICIT_CAST, location, + false); /* * Now build the domain coercion node. This represents run-time checking @@ -714,11 +706,14 @@ coerce_to_domain(Node *arg, Oid baseTypeId, int32 baseTypeMod, Oid typeId, * The caller must have already ensured that the value is of the correct * type, typically by applying coerce_type. * - * cformat determines the display properties of the generated node (if any), - * while isExplicit may affect semantics. If hideInputCoercion is true - * *and* we generate a node, the input node is forced to IMPLICIT display - * form, so that only the typmod coercion node will be visible when - * displaying the expression. + * ccontext may affect semantics, depending on whether the length coercion + * function pays attention to the isExplicit flag it's passed. + * + * cformat determines the display properties of the generated node (if any). + * + * If hideInputCoercion is true *and* we generate a node, the input node is + * forced to IMPLICIT display form, so that only the typmod coercion node will + * be visible when displaying the expression. * * NOTE: this does not need to work on domain types, because any typmod * coercion for a domain is considered to be part of the type coercion @@ -726,8 +721,9 @@ coerce_to_domain(Node *arg, Oid baseTypeId, int32 baseTypeMod, Oid typeId, */ static Node * coerce_type_typmod(Node *node, Oid targetTypeId, int32 targetTypMod, - CoercionForm cformat, int location, - bool isExplicit, bool hideInputCoercion) + CoercionContext ccontext, CoercionForm cformat, + int location, + bool hideInputCoercion) { CoercionPathType pathtype; Oid funcId; @@ -749,8 +745,7 @@ coerce_type_typmod(Node *node, Oid targetTypeId, int32 targetTypMod, node = build_coercion_expression(node, pathtype, funcId, targetTypeId, targetTypMod, - cformat, location, - isExplicit); + ccontext, cformat, location); } return node; @@ -799,8 +794,8 @@ build_coercion_expression(Node *node, CoercionPathType pathtype, Oid funcId, Oid targetTypeId, int32 targetTypMod, - CoercionForm cformat, int location, - bool isExplicit) + CoercionContext ccontext, CoercionForm cformat, + int location) { int nargs = 0; @@ -865,7 +860,7 @@ build_coercion_expression(Node *node, -1, InvalidOid, sizeof(bool), - BoolGetDatum(isExplicit), + BoolGetDatum(ccontext == COERCION_EXPLICIT), false, true); @@ -881,19 +876,52 @@ build_coercion_expression(Node *node, { /* We need to build an ArrayCoerceExpr */ ArrayCoerceExpr *acoerce = makeNode(ArrayCoerceExpr); + CaseTestExpr *ctest = makeNode(CaseTestExpr); + Oid sourceBaseTypeId; + int32 sourceBaseTypeMod; + Oid targetElementType; + Node *elemexpr; + + /* + * Look through any domain over the source array type. Note we don't + * expect that the target type is a domain; it must be a plain array. + * (To get to a domain target type, we'll do coerce_to_domain later.) + */ + sourceBaseTypeMod = exprTypmod(node); + sourceBaseTypeId = getBaseTypeAndTypmod(exprType(node), + &sourceBaseTypeMod); + + /* Set up CaseTestExpr representing one element of source array */ + ctest->typeId = get_element_type(sourceBaseTypeId); + Assert(OidIsValid(ctest->typeId)); + ctest->typeMod = sourceBaseTypeMod; + ctest->collation = InvalidOid; /* Assume coercions don't care */ + + /* And coerce it to the target element type */ + targetElementType = get_element_type(targetTypeId); + Assert(OidIsValid(targetElementType)); + + elemexpr = coerce_to_target_type(NULL, + (Node *) ctest, + ctest->typeId, + targetElementType, + targetTypMod, + ccontext, + cformat, + location); + if (elemexpr == NULL) /* shouldn't happen */ + elog(ERROR, "failed to coerce array element type as expected"); acoerce->arg = (Expr *) node; - acoerce->elemfuncid = funcId; + acoerce->elemexpr = (Expr *) elemexpr; acoerce->resulttype = targetTypeId; /* - * Label the output as having a particular typmod only if we are - * really invoking a length-coercion function, ie one with more than - * one argument. + * Label the output as having a particular element typmod only if we + * ended up with a per-element expression that is labeled that way. */ - acoerce->resulttypmod = (nargs >= 2) ? targetTypMod : -1; + acoerce->resulttypmod = exprTypmod(elemexpr); /* resultcollid will be set by parse_collate.c */ - acoerce->isExplicit = isExplicit; acoerce->coerceformat = cformat; acoerce->location = location; @@ -2148,8 +2176,7 @@ IsBinaryCoercible(Oid srctype, Oid targettype) * COERCION_PATH_RELABELTYPE: binary-compatible cast, no function needed * *funcid is set to InvalidOid * COERCION_PATH_ARRAYCOERCE: need an ArrayCoerceExpr node - * *funcid is set to the element cast function, or InvalidOid - * if the array elements are binary-compatible + * *funcid is set to InvalidOid * COERCION_PATH_COERCEVIAIO: need a CoerceViaIO node * *funcid is set to InvalidOid * @@ -2235,11 +2262,8 @@ find_coercion_pathway(Oid targetTypeId, Oid sourceTypeId, { /* * If there's no pg_cast entry, perhaps we are dealing with a pair of - * array types. If so, and if the element types have a suitable cast, - * report that we can coerce with an ArrayCoerceExpr. - * - * Note that the source type can be a domain over array, but not the - * target, because ArrayCoerceExpr won't check domain constraints. + * array types. If so, and if their element types have a conversion + * pathway, report that we can coerce with an ArrayCoerceExpr. * * Hack: disallow coercions to oidvector and int2vector, which * otherwise tend to capture coercions that should go to "real" array @@ -2254,7 +2278,7 @@ find_coercion_pathway(Oid targetTypeId, Oid sourceTypeId, Oid sourceElem; if ((targetElem = get_element_type(targetTypeId)) != InvalidOid && - (sourceElem = get_base_element_type(sourceTypeId)) != InvalidOid) + (sourceElem = get_element_type(sourceTypeId)) != InvalidOid) { CoercionPathType elempathtype; Oid elemfuncid; @@ -2263,14 +2287,9 @@ find_coercion_pathway(Oid targetTypeId, Oid sourceTypeId, sourceElem, ccontext, &elemfuncid); - if (elempathtype != COERCION_PATH_NONE && - elempathtype != COERCION_PATH_ARRAYCOERCE) + if (elempathtype != COERCION_PATH_NONE) { - *funcid = elemfuncid; - if (elempathtype == COERCION_PATH_COERCEVIAIO) - result = COERCION_PATH_COERCEVIAIO; - else - result = COERCION_PATH_ARRAYCOERCE; + result = COERCION_PATH_ARRAYCOERCE; } } } @@ -2311,7 +2330,9 @@ find_coercion_pathway(Oid targetTypeId, Oid sourceTypeId, * If the given type is a varlena array type, we do not look for a coercion * function associated directly with the array type, but instead look for * one associated with the element type. An ArrayCoerceExpr node must be - * used to apply such a function. + * used to apply such a function. (Note: currently, it's pointless to + * return the funcid in this case, because it'll just get looked up again + * in the recursive construction of the ArrayCoerceExpr's elemexpr.) * * We use the same result enum as find_coercion_pathway, but the only possible * result codes are: diff --git a/src/backend/postmaster/bgworker.c b/src/backend/postmaster/bgworker.c index 28af6f0f07deb..4a3c4b4cc9c02 100644 --- a/src/backend/postmaster/bgworker.c +++ b/src/backend/postmaster/bgworker.c @@ -344,6 +344,8 @@ BackgroundWorkerStateChange(void) */ ascii_safe_strlcpy(rw->rw_worker.bgw_name, slot->worker.bgw_name, BGW_MAXLEN); + ascii_safe_strlcpy(rw->rw_worker.bgw_type, + slot->worker.bgw_type, BGW_MAXLEN); ascii_safe_strlcpy(rw->rw_worker.bgw_library_name, slot->worker.bgw_library_name, BGW_MAXLEN); ascii_safe_strlcpy(rw->rw_worker.bgw_function_name, @@ -630,6 +632,12 @@ SanityCheckBackgroundWorker(BackgroundWorker *worker, int elevel) return false; } + /* + * If bgw_type is not filled in, use bgw_name. + */ + if (strcmp(worker->bgw_type, "") == 0) + strcpy(worker->bgw_type, worker->bgw_name); + return true; } @@ -671,7 +679,7 @@ bgworker_die(SIGNAL_ARGS) ereport(FATAL, (errcode(ERRCODE_ADMIN_SHUTDOWN), errmsg("terminating background worker \"%s\" due to administrator command", - MyBgworkerEntry->bgw_name))); + MyBgworkerEntry->bgw_type))); } /* @@ -700,7 +708,6 @@ void StartBackgroundWorker(void) { sigjmp_buf local_sigjmp_buf; - char buf[MAXPGPATH]; BackgroundWorker *worker = MyBgworkerEntry; bgworker_main_type entrypt; @@ -710,8 +717,7 @@ StartBackgroundWorker(void) IsBackgroundWorker = true; /* Identify myself via ps */ - snprintf(buf, MAXPGPATH, "bgworker: %s", worker->bgw_name); - init_ps_display(buf, "", "", ""); + init_ps_display(worker->bgw_name, "", "", ""); /* * If we're not supposed to have shared memory access, then detach from @@ -1233,3 +1239,40 @@ LookupBackgroundWorkerFunction(const char *libraryname, const char *funcname) return (bgworker_main_type) load_external_function(libraryname, funcname, true, NULL); } + +/* + * Given a PID, get the bgw_type of the background worker. Returns NULL if + * not a valid background worker. + * + * The return value is in static memory belonging to this function, so it has + * to be used before calling this function again. This is so that the caller + * doesn't have to worry about the background worker locking protocol. + */ +const char * +GetBackgroundWorkerTypeByPid(pid_t pid) +{ + int slotno; + bool found = false; + static char result[BGW_MAXLEN]; + + LWLockAcquire(BackgroundWorkerLock, LW_SHARED); + + for (slotno = 0; slotno < BackgroundWorkerData->total_slots; slotno++) + { + BackgroundWorkerSlot *slot = &BackgroundWorkerData->slot[slotno]; + + if (slot->pid > 0 && slot->pid == pid) + { + strcpy(result, slot->worker.bgw_type); + found = true; + break; + } + } + + LWLockRelease(BackgroundWorkerLock); + + if (!found) + return NULL; + + return result; +} diff --git a/src/backend/postmaster/postmaster.c b/src/backend/postmaster/postmaster.c index 1bcbce537a45e..2b2b993e2cfc8 100644 --- a/src/backend/postmaster/postmaster.c +++ b/src/backend/postmaster/postmaster.c @@ -74,8 +74,6 @@ #include #include #include -#include -#include #include #include @@ -107,6 +105,7 @@ #include "miscadmin.h" #include "pg_getopt.h" #include "pgstat.h" +#include "port/pg_bswap.h" #include "postmaster/autovacuum.h" #include "postmaster/bgworker_internals.h" #include "postmaster/fork_process.h" @@ -1072,7 +1071,7 @@ PostmasterMain(int argc, char *argv[]) "_postgresql._tcp.", NULL, NULL, - htons(PostPortNumber), + pg_hton16(PostPortNumber), 0, NULL, NULL, @@ -1966,7 +1965,7 @@ ProcessStartupPacket(Port *port, bool SSLdone) return STATUS_ERROR; } - len = ntohl(len); + len = pg_ntoh32(len); len -= 4; if (len < (int32) sizeof(ProtocolVersion) || @@ -2002,7 +2001,7 @@ ProcessStartupPacket(Port *port, bool SSLdone) * The first field is either a protocol version number or a special * request code. */ - port->proto = proto = ntohl(*((ProtocolVersion *) buf)); + port->proto = proto = pg_ntoh32(*((ProtocolVersion *) buf)); if (proto == CANCEL_REQUEST_CODE) { @@ -2281,8 +2280,8 @@ processCancelRequest(Port *port, void *pkt) int i; #endif - backendPID = (int) ntohl(canc->backendPID); - cancelAuthCode = (int32) ntohl(canc->cancelAuthCode); + backendPID = (int) pg_ntoh32(canc->backendPID); + cancelAuthCode = (int32) pg_ntoh32(canc->cancelAuthCode); /* * See if we have a matching backend. In the EXEC_BACKEND case, we can no @@ -3117,8 +3116,9 @@ CleanupBackgroundWorker(int pid, exitstatus = 0; #endif - snprintf(namebuf, MAXPGPATH, "%s: %s", _("worker process"), - rw->rw_worker.bgw_name); + snprintf(namebuf, MAXPGPATH, _("background worker \"%s\""), + rw->rw_worker.bgw_type); + if (!EXIT_STATUS_0(exitstatus)) { diff --git a/src/backend/replication/logical/launcher.c b/src/backend/replication/logical/launcher.c index 44bdcab3b9796..a613ef4757bec 100644 --- a/src/backend/replication/logical/launcher.c +++ b/src/backend/replication/logical/launcher.c @@ -422,6 +422,7 @@ logicalrep_worker_launch(Oid dbid, Oid subid, const char *subname, Oid userid, else snprintf(bgw.bgw_name, BGW_MAXLEN, "logical replication worker for subscription %u", subid); + snprintf(bgw.bgw_type, BGW_MAXLEN, "logical replication worker"); bgw.bgw_restart_time = BGW_NEVER_RESTART; bgw.bgw_notify_pid = MyProcPid; @@ -775,6 +776,8 @@ ApplyLauncherRegister(void) snprintf(bgw.bgw_function_name, BGW_MAXLEN, "ApplyLauncherMain"); snprintf(bgw.bgw_name, BGW_MAXLEN, "logical replication launcher"); + snprintf(bgw.bgw_type, BGW_MAXLEN, + "logical replication launcher"); bgw.bgw_restart_time = 5; bgw.bgw_notify_pid = 0; bgw.bgw_main_arg = (Datum) 0; diff --git a/src/backend/rewrite/rewriteHandler.c b/src/backend/rewrite/rewriteHandler.c index ef52dd5b955ea..7054d4f77d994 100644 --- a/src/backend/rewrite/rewriteHandler.c +++ b/src/backend/rewrite/rewriteHandler.c @@ -875,9 +875,9 @@ rewriteTargetListIU(List *targetList, new_expr = coerce_to_domain(new_expr, InvalidOid, -1, att_tup->atttypid, + COERCION_IMPLICIT, COERCE_IMPLICIT_CAST, -1, - false, false); } } @@ -1271,9 +1271,9 @@ rewriteValuesRTE(RangeTblEntry *rte, Relation target_relation, List *attrnos) new_expr = coerce_to_domain(new_expr, InvalidOid, -1, att_tup->atttypid, + COERCION_IMPLICIT, COERCE_IMPLICIT_CAST, -1, - false, false); } newList = lappend(newList, new_expr); diff --git a/src/backend/rewrite/rewriteManip.c b/src/backend/rewrite/rewriteManip.c index 5c17213720731..c5773efd19296 100644 --- a/src/backend/rewrite/rewriteManip.c +++ b/src/backend/rewrite/rewriteManip.c @@ -1429,9 +1429,9 @@ ReplaceVarsFromTargetList_callback(Var *var, var->varcollid), InvalidOid, -1, var->vartype, + COERCION_IMPLICIT, COERCE_IMPLICIT_CAST, -1, - false, false); } elog(ERROR, "could not find replacement targetlist entry for attno %d", diff --git a/src/backend/tcop/fastpath.c b/src/backend/tcop/fastpath.c index 9207d76981acb..8101ae74e0bfd 100644 --- a/src/backend/tcop/fastpath.c +++ b/src/backend/tcop/fastpath.c @@ -17,9 +17,6 @@ */ #include "postgres.h" -#include -#include - #include "access/htup_details.h" #include "access/xact.h" #include "catalog/objectaccess.h" @@ -28,6 +25,7 @@ #include "libpq/pqformat.h" #include "mb/pg_wchar.h" #include "miscadmin.h" +#include "port/pg_bswap.h" #include "tcop/fastpath.h" #include "tcop/tcopprot.h" #include "utils/acl.h" @@ -92,7 +90,7 @@ GetOldFunctionMessage(StringInfo buf) if (pq_getbytes((char *) &ibuf, 4)) return EOF; appendBinaryStringInfo(buf, (char *) &ibuf, 4); - nargs = ntohl(ibuf); + nargs = pg_ntoh32(ibuf); /* For each argument ... */ while (nargs-- > 0) { @@ -102,7 +100,7 @@ GetOldFunctionMessage(StringInfo buf) if (pq_getbytes((char *) &ibuf, 4)) return EOF; appendBinaryStringInfo(buf, (char *) &ibuf, 4); - argsize = ntohl(ibuf); + argsize = pg_ntoh32(ibuf); if (argsize < -1) { /* FATAL here since no hope of regaining message sync */ diff --git a/src/backend/utils/adt/arrayfuncs.c b/src/backend/utils/adt/arrayfuncs.c index d1f2fe7d95854..ca04b13e825ab 100644 --- a/src/backend/utils/adt/arrayfuncs.c +++ b/src/backend/utils/adt/arrayfuncs.c @@ -3092,21 +3092,18 @@ array_set(ArrayType *array, int nSubscripts, int *indx, /* * array_map() * - * Map an array through an arbitrary function. Return a new array with - * same dimensions and each source element transformed by fn(). Each - * source element is passed as the first argument to fn(); additional - * arguments to be passed to fn() can be specified by the caller. - * The output array can have a different element type than the input. + * Map an array through an arbitrary expression. Return a new array with + * the same dimensions and each source element transformed by the given, + * already-compiled expression. Each source element is placed in the + * innermost_caseval/innermost_casenull fields of the ExprState. * * Parameters are: - * * fcinfo: a function-call data structure pre-constructed by the caller - * to be ready to call the desired function, with everything except the - * first argument position filled in. In particular, flinfo identifies - * the function fn(), and if nargs > 1 then argument positions after the - * first must be preset to the additional values to be passed. The - * first argument position initially holds the input array value. + * * arrayd: Datum representing array argument. + * * exprstate: ExprState representing the per-element transformation. + * * econtext: context for expression evaluation. * * retType: OID of element type of output array. This must be the same as, - * or binary-compatible with, the result type of fn(). + * or binary-compatible with, the result type of the expression. It might + * be different from the input array's element type. * * amstate: workspace for array_map. Must be zeroed by caller before * first call, and not touched after that. * @@ -3116,11 +3113,14 @@ array_set(ArrayType *array, int nSubscripts, int *indx, * * NB: caller must assure that input array is not NULL. NULL elements in * the array are OK however. + * NB: caller should be running in econtext's per-tuple memory context. */ Datum -array_map(FunctionCallInfo fcinfo, Oid retType, ArrayMapState *amstate) +array_map(Datum arrayd, + ExprState *exprstate, ExprContext *econtext, + Oid retType, ArrayMapState *amstate) { - AnyArrayType *v; + AnyArrayType *v = DatumGetAnyArrayP(arrayd); ArrayType *result; Datum *values; bool *nulls; @@ -3141,13 +3141,8 @@ array_map(FunctionCallInfo fcinfo, Oid retType, ArrayMapState *amstate) array_iter iter; ArrayMetaState *inp_extra; ArrayMetaState *ret_extra; - - /* Get input array */ - if (fcinfo->nargs < 1) - elog(ERROR, "invalid nargs: %d", fcinfo->nargs); - if (PG_ARGISNULL(0)) - elog(ERROR, "null input array"); - v = PG_GETARG_ANY_ARRAY_P(0); + Datum *transform_source = exprstate->innermost_caseval; + bool *transform_source_isnull = exprstate->innermost_casenull; inpType = AARR_ELEMTYPE(v); ndim = AARR_NDIM(v); @@ -3158,7 +3153,7 @@ array_map(FunctionCallInfo fcinfo, Oid retType, ArrayMapState *amstate) if (nitems <= 0) { /* Return empty array */ - PG_RETURN_ARRAYTYPE_P(construct_empty_array(retType)); + return PointerGetDatum(construct_empty_array(retType)); } /* @@ -3203,39 +3198,15 @@ array_map(FunctionCallInfo fcinfo, Oid retType, ArrayMapState *amstate) for (i = 0; i < nitems; i++) { - bool callit = true; - /* Get source element, checking for NULL */ - fcinfo->arg[0] = array_iter_next(&iter, &fcinfo->argnull[0], i, - inp_typlen, inp_typbyval, inp_typalign); - - /* - * Apply the given function to source elt and extra args. - */ - if (fcinfo->flinfo->fn_strict) - { - int j; + *transform_source = + array_iter_next(&iter, transform_source_isnull, i, + inp_typlen, inp_typbyval, inp_typalign); - for (j = 0; j < fcinfo->nargs; j++) - { - if (fcinfo->argnull[j]) - { - callit = false; - break; - } - } - } + /* Apply the given expression to source element */ + values[i] = ExecEvalExpr(exprstate, econtext, &nulls[i]); - if (callit) - { - fcinfo->isnull = false; - values[i] = FunctionCallInvoke(fcinfo); - } - else - fcinfo->isnull = true; - - nulls[i] = fcinfo->isnull; - if (fcinfo->isnull) + if (nulls[i]) hasnulls = true; else { @@ -3254,7 +3225,7 @@ array_map(FunctionCallInfo fcinfo, Oid retType, ArrayMapState *amstate) } } - /* Allocate and initialize the result array */ + /* Allocate and fill the result array */ if (hasnulls) { dataoffset = ARR_OVERHEAD_WITHNULLS(ndim, nitems); @@ -3273,18 +3244,18 @@ array_map(FunctionCallInfo fcinfo, Oid retType, ArrayMapState *amstate) memcpy(ARR_DIMS(result), AARR_DIMS(v), ndim * sizeof(int)); memcpy(ARR_LBOUND(result), AARR_LBOUND(v), ndim * sizeof(int)); - /* - * Note: do not risk trying to pfree the results of the called function - */ CopyArrayEls(result, values, nulls, nitems, typlen, typbyval, typalign, false); + /* + * Note: do not risk trying to pfree the results of the called expression + */ pfree(values); pfree(nulls); - PG_RETURN_ARRAYTYPE_P(result); + return PointerGetDatum(result); } /* diff --git a/src/backend/utils/adt/pgstatfuncs.c b/src/backend/utils/adt/pgstatfuncs.c index 5a968e3758f6d..8d9e7c10ae758 100644 --- a/src/backend/utils/adt/pgstatfuncs.c +++ b/src/backend/utils/adt/pgstatfuncs.c @@ -21,6 +21,7 @@ #include "funcapi.h" #include "miscadmin.h" #include "pgstat.h" +#include "postmaster/bgworker_internals.h" #include "postmaster/postmaster.h" #include "storage/proc.h" #include "storage/procarray.h" @@ -823,8 +824,19 @@ pg_stat_get_activity(PG_FUNCTION_ARGS) } } /* Add backend type */ - values[17] = - CStringGetTextDatum(pgstat_get_backend_desc(beentry->st_backendType)); + if (beentry->st_backendType == B_BG_WORKER) + { + const char *bgw_type; + + bgw_type = GetBackgroundWorkerTypeByPid(beentry->st_procpid); + if (bgw_type) + values[17] = CStringGetTextDatum(bgw_type); + else + nulls[17] = true; + } + else + values[17] = + CStringGetTextDatum(pgstat_get_backend_desc(beentry->st_backendType)); } else { diff --git a/src/backend/utils/adt/selfuncs.c b/src/backend/utils/adt/selfuncs.c index db1792bf8d409..7361e9d43caa5 100644 --- a/src/backend/utils/adt/selfuncs.c +++ b/src/backend/utils/adt/selfuncs.c @@ -1816,10 +1816,19 @@ strip_array_coercion(Node *node) { for (;;) { - if (node && IsA(node, ArrayCoerceExpr) && - ((ArrayCoerceExpr *) node)->elemfuncid == InvalidOid) + if (node && IsA(node, ArrayCoerceExpr)) { - node = (Node *) ((ArrayCoerceExpr *) node)->arg; + ArrayCoerceExpr *acoerce = (ArrayCoerceExpr *) node; + + /* + * If the per-element expression is just a RelabelType on top of + * CaseTestExpr, then we know it's a binary-compatible relabeling. + */ + if (IsA(acoerce->elemexpr, RelabelType) && + IsA(((RelabelType *) acoerce->elemexpr)->arg, CaseTestExpr)) + node = (Node *) acoerce->arg; + else + break; } else if (node && IsA(node, RelabelType)) { diff --git a/src/backend/utils/fmgr/fmgr.c b/src/backend/utils/fmgr/fmgr.c index a7b07827e01c9..919733517bda3 100644 --- a/src/backend/utils/fmgr/fmgr.c +++ b/src/backend/utils/fmgr/fmgr.c @@ -1941,8 +1941,6 @@ get_call_expr_argtype(Node *expr, int argnum) args = ((DistinctExpr *) expr)->args; else if (IsA(expr, ScalarArrayOpExpr)) args = ((ScalarArrayOpExpr *) expr)->args; - else if (IsA(expr, ArrayCoerceExpr)) - args = list_make1(((ArrayCoerceExpr *) expr)->arg); else if (IsA(expr, NullIfExpr)) args = ((NullIfExpr *) expr)->args; else if (IsA(expr, WindowFunc)) @@ -1956,16 +1954,12 @@ get_call_expr_argtype(Node *expr, int argnum) argtype = exprType((Node *) list_nth(args, argnum)); /* - * special hack for ScalarArrayOpExpr and ArrayCoerceExpr: what the - * underlying function will actually get passed is the element type of the - * array. + * special hack for ScalarArrayOpExpr: what the underlying function will + * actually get passed is the element type of the array. */ if (IsA(expr, ScalarArrayOpExpr) && argnum == 1) argtype = get_base_element_type(argtype); - else if (IsA(expr, ArrayCoerceExpr) && - argnum == 0) - argtype = get_base_element_type(argtype); return argtype; } @@ -2012,8 +2006,6 @@ get_call_expr_arg_stable(Node *expr, int argnum) args = ((DistinctExpr *) expr)->args; else if (IsA(expr, ScalarArrayOpExpr)) args = ((ScalarArrayOpExpr *) expr)->args; - else if (IsA(expr, ArrayCoerceExpr)) - args = list_make1(((ArrayCoerceExpr *) expr)->arg); else if (IsA(expr, NullIfExpr)) args = ((NullIfExpr *) expr)->args; else if (IsA(expr, WindowFunc)) diff --git a/src/backend/utils/init/globals.c b/src/backend/utils/init/globals.c index 7c09498dc047e..9680a4b0f7a9e 100644 --- a/src/backend/utils/init/globals.c +++ b/src/backend/utils/init/globals.c @@ -112,7 +112,6 @@ bool enableFsync = true; bool allowSystemTableMods = false; int work_mem = 1024; int maintenance_work_mem = 16384; -int replacement_sort_tuples = 150000; /* * Primary determinants of sizes of shared-memory structures. diff --git a/src/backend/utils/misc/guc.c b/src/backend/utils/misc/guc.c index 47a5f257071ca..8292df00bbfac 100644 --- a/src/backend/utils/misc/guc.c +++ b/src/backend/utils/misc/guc.c @@ -1933,16 +1933,6 @@ static struct config_int ConfigureNamesInt[] = NULL, NULL, NULL }, - { - {"replacement_sort_tuples", PGC_USERSET, RESOURCES_MEM, - gettext_noop("Sets the maximum number of tuples to be sorted using replacement selection."), - gettext_noop("When more tuples than this are present, quicksort will be used.") - }, - &replacement_sort_tuples, - 150000, 0, INT_MAX, - NULL, NULL, NULL - }, - /* * We use the hopefully-safely-small value of 100kB as the compiled-in * default for max_stack_depth. InitializeGUCOptions will increase it if diff --git a/src/backend/utils/misc/postgresql.conf.sample b/src/backend/utils/misc/postgresql.conf.sample index 8ba6b1d08a519..cf4ddcd94a7c0 100644 --- a/src/backend/utils/misc/postgresql.conf.sample +++ b/src/backend/utils/misc/postgresql.conf.sample @@ -121,7 +121,6 @@ # you actively intend to use prepared transactions. #work_mem = 4MB # min 64kB #maintenance_work_mem = 64MB # min 1MB -#replacement_sort_tuples = 150000 # limits use of replacement selection sort #autovacuum_work_mem = -1 # min 1MB, or -1 to use maintenance_work_mem #max_stack_depth = 2MB # min 100kB #dynamic_shared_memory_type = posix # the default is the first option diff --git a/src/backend/utils/sort/tuplesort.c b/src/backend/utils/sort/tuplesort.c index 17e1b6860ba06..60522cb442474 100644 --- a/src/backend/utils/sort/tuplesort.c +++ b/src/backend/utils/sort/tuplesort.c @@ -13,47 +13,11 @@ * See Knuth, volume 3, for more than you want to know about the external * sorting algorithm. Historically, we divided the input into sorted runs * using replacement selection, in the form of a priority tree implemented - * as a heap (essentially his Algorithm 5.2.3H), but now we only do that - * for the first run, and only if the run would otherwise end up being very - * short. We merge the runs using polyphase merge, Knuth's Algorithm - * 5.4.2D. The logical "tapes" used by Algorithm D are implemented by - * logtape.c, which avoids space wastage by recycling disk space as soon - * as each block is read from its "tape". - * - * We do not use Knuth's recommended data structure (Algorithm 5.4.1R) for - * the replacement selection, because it uses a fixed number of records - * in memory at all times. Since we are dealing with tuples that may vary - * considerably in size, we want to be able to vary the number of records - * kept in memory to ensure full utilization of the allowed sort memory - * space. So, we keep the tuples in a variable-size heap, with the next - * record to go out at the top of the heap. Like Algorithm 5.4.1R, each - * record is stored with the run number that it must go into, and we use - * (run number, key) as the ordering key for the heap. When the run number - * at the top of the heap changes, we know that no more records of the prior - * run are left in the heap. Note that there are in practice only ever two - * distinct run numbers, because since PostgreSQL 9.6, we only use - * replacement selection to form the first run. - * - * In PostgreSQL 9.6, a heap (based on Knuth's Algorithm H, with some small - * customizations) is only used with the aim of producing just one run, - * thereby avoiding all merging. Only the first run can use replacement - * selection, which is why there are now only two possible valid run - * numbers, and why heapification is customized to not distinguish between - * tuples in the second run (those will be quicksorted). We generally - * prefer a simple hybrid sort-merge strategy, where runs are sorted in much - * the same way as the entire input of an internal sort is sorted (using - * qsort()). The replacement_sort_tuples GUC controls the limited remaining - * use of replacement selection for the first run. - * - * There are several reasons to favor a hybrid sort-merge strategy. - * Maintaining a priority tree/heap has poor CPU cache characteristics. - * Furthermore, the growth in main memory sizes has greatly diminished the - * value of having runs that are larger than available memory, even in the - * case where there is partially sorted input and runs can be made far - * larger by using a heap. In most cases, a single-pass merge step is all - * that is required even when runs are no larger than available memory. - * Avoiding multiple merge passes was traditionally considered to be the - * major advantage of using replacement selection. + * as a heap (essentially his Algorithm 5.2.3H), but now we always use + * quicksort for run generation. We merge the runs using polyphase merge, + * Knuth's Algorithm 5.4.2D. The logical "tapes" used by Algorithm D are + * implemented by logtape.c, which avoids space wastage by recycling disk + * space as soon as each block is read from its "tape". * * The approximate amount of memory allowed for any one sort operation * is specified in kilobytes by the caller (most pass work_mem). Initially, @@ -64,9 +28,8 @@ * workMem, we begin to emit tuples into sorted runs in temporary tapes. * When tuples are dumped in batch after quicksorting, we begin a new run * with a new output tape (selected per Algorithm D). After the end of the - * input is reached, we dump out remaining tuples in memory into a final run - * (or two, when replacement selection is still used), then merge the runs - * using Algorithm D. + * input is reached, we dump out remaining tuples in memory into a final run, + * then merge the runs using Algorithm D. * * When merging runs, we use a heap containing just the frontmost tuple from * each source run; we repeatedly output the smallest tuple and replace it @@ -188,13 +151,8 @@ bool optimize_bounded_sort = true; * described above. Accordingly, "tuple" is always used in preference to * datum1 as the authoritative value for pass-by-reference cases. * - * While building initial runs, tupindex holds the tuple's run number. - * Historically, the run number could meaningfully distinguish many runs, but - * it now only distinguishes RUN_FIRST and HEAP_RUN_NEXT, since replacement - * selection is always abandoned after the first run; no other run number - * should be represented here. During merge passes, we re-use it to hold the - * input tape number that each tuple in the heap was read from. tupindex goes - * unused if the sort occurs entirely in memory. + * tupindex holds the input tape number that each tuple in the heap was read + * from during merge passes. */ typedef struct { @@ -253,15 +211,6 @@ typedef enum #define TAPE_BUFFER_OVERHEAD BLCKSZ #define MERGE_BUFFER_SIZE (BLCKSZ * 32) - /* - * Run numbers, used during external sort operations. - * - * HEAP_RUN_NEXT is only used for SortTuple.tupindex, never state.currentRun. - */ -#define RUN_FIRST 0 -#define HEAP_RUN_NEXT INT_MAX -#define RUN_SECOND 1 - typedef int (*SortTupleComparator) (const SortTuple *a, const SortTuple *b, Tuplesortstate *state); @@ -381,16 +330,8 @@ struct Tuplesortstate void *lastReturnedTuple; /* - * While building initial runs, this indicates if the replacement - * selection strategy is in use. When it isn't, then a simple hybrid - * sort-merge strategy is in use instead (runs are quicksorted). - */ - bool replaceActive; - - /* - * While building initial runs, this is the current output run number - * (starting at RUN_FIRST). Afterwards, it is the number of initial runs - * we made. + * While building initial runs, this is the current output run number. + * Afterwards, it is the number of initial runs we made. */ int currentRun; @@ -583,7 +524,6 @@ struct Tuplesortstate static Tuplesortstate *tuplesort_begin_common(int workMem, bool randomAccess); static void puttuple_common(Tuplesortstate *state, SortTuple *tuple); static bool consider_abort_common(Tuplesortstate *state); -static bool useselection(Tuplesortstate *state); static void inittapes(Tuplesortstate *state); static void selectnewtape(Tuplesortstate *state); static void init_slab_allocator(Tuplesortstate *state, int numSlots); @@ -592,15 +532,12 @@ static void mergeonerun(Tuplesortstate *state); static void beginmerge(Tuplesortstate *state); static bool mergereadnext(Tuplesortstate *state, int srcTape, SortTuple *stup); static void dumptuples(Tuplesortstate *state, bool alltuples); -static void dumpbatch(Tuplesortstate *state, bool alltuples); static void make_bounded_heap(Tuplesortstate *state); static void sort_bounded_heap(Tuplesortstate *state); static void tuplesort_sort_memtuples(Tuplesortstate *state); -static void tuplesort_heap_insert(Tuplesortstate *state, SortTuple *tuple, - bool checkIndex); -static void tuplesort_heap_replace_top(Tuplesortstate *state, SortTuple *tuple, - bool checkIndex); -static void tuplesort_heap_delete_top(Tuplesortstate *state, bool checkIndex); +static void tuplesort_heap_insert(Tuplesortstate *state, SortTuple *tuple); +static void tuplesort_heap_replace_top(Tuplesortstate *state, SortTuple *tuple); +static void tuplesort_heap_delete_top(Tuplesortstate *state); static void reversedirection(Tuplesortstate *state); static unsigned int getlen(Tuplesortstate *state, int tapenum, bool eofOK); static void markrunend(Tuplesortstate *state, int tapenum); @@ -738,7 +675,7 @@ tuplesort_begin_common(int workMem, bool randomAccess) if (LACKMEM(state)) elog(ERROR, "insufficient memory allowed for sort"); - state->currentRun = RUN_FIRST; + state->currentRun = 0; /* * maxTapes, tapeRange, and Algorithm D variables will be initialized by @@ -1622,7 +1559,7 @@ puttuple_common(Tuplesortstate *state, SortTuple *tuple) inittapes(state); /* - * Dump tuples until we are back under the limit. + * Dump all tuples. */ dumptuples(state, false); break; @@ -1647,74 +1584,20 @@ puttuple_common(Tuplesortstate *state, SortTuple *tuple) { /* discard top of heap, replacing it with the new tuple */ free_sort_tuple(state, &state->memtuples[0]); - tuple->tupindex = 0; /* not used */ - tuplesort_heap_replace_top(state, tuple, false); + tuplesort_heap_replace_top(state, tuple); } break; case TSS_BUILDRUNS: /* - * Insert the tuple into the heap, with run number currentRun if - * it can go into the current run, else HEAP_RUN_NEXT. The tuple - * can go into the current run if it is >= the first - * not-yet-output tuple. (Actually, it could go into the current - * run if it is >= the most recently output tuple ... but that - * would require keeping around the tuple we last output, and it's - * simplest to let writetup free each tuple as soon as it's - * written.) - * - * Note that this only applies when: - * - * - currentRun is RUN_FIRST - * - * - Replacement selection is in use (typically it is never used). - * - * When these two conditions are not both true, all tuples are - * appended indifferently, much like the TSS_INITIAL case. - * - * There should always be room to store the incoming tuple. + * Save the tuple into the unsorted array (there must be + * space) */ - Assert(!state->replaceActive || state->memtupcount > 0); - if (state->replaceActive && - COMPARETUP(state, tuple, &state->memtuples[0]) >= 0) - { - Assert(state->currentRun == RUN_FIRST); - - /* - * Insert tuple into first, fully heapified run. - * - * Unlike classic replacement selection, which this module was - * previously based on, only RUN_FIRST tuples are fully - * heapified. Any second/next run tuples are appended - * indifferently. While HEAP_RUN_NEXT tuples may be sifted - * out of the way of first run tuples, COMPARETUP() will never - * be called for the run's tuples during sifting (only our - * initial COMPARETUP() call is required for the tuple, to - * determine that the tuple does not belong in RUN_FIRST). - */ - tuple->tupindex = state->currentRun; - tuplesort_heap_insert(state, tuple, true); - } - else - { - /* - * Tuple was determined to not belong to heapified RUN_FIRST, - * or replacement selection not in play. Append the tuple to - * memtuples indifferently. - * - * dumptuples() does not trust that the next run's tuples are - * heapified. Anything past the first run will always be - * quicksorted even when replacement selection is initially - * used. (When it's never used, every tuple still takes this - * path.) - */ - tuple->tupindex = HEAP_RUN_NEXT; - state->memtuples[state->memtupcount++] = *tuple; - } + state->memtuples[state->memtupcount++] = *tuple; /* - * If we are over the memory limit, dump tuples till we're under. + * If we are over the memory limit, dump all tuples. */ dumptuples(state, false); break; @@ -2068,7 +1951,7 @@ tuplesort_gettuple_common(Tuplesortstate *state, bool forward, * If no more data, we've reached end of run on this tape. * Remove the top node from the heap. */ - tuplesort_heap_delete_top(state, false); + tuplesort_heap_delete_top(state); /* * Rewind to free the read buffer. It'd go away at the @@ -2079,7 +1962,7 @@ tuplesort_gettuple_common(Tuplesortstate *state, bool forward, return true; } newtup.tupindex = srcTape; - tuplesort_heap_replace_top(state, &newtup, false); + tuplesort_heap_replace_top(state, &newtup); return true; } return false; @@ -2336,28 +2219,6 @@ tuplesort_merge_order(int64 allowedMem) return mOrder; } -/* - * useselection - determine algorithm to use to sort first run. - * - * It can sometimes be useful to use the replacement selection algorithm if it - * results in one large run, and there is little available workMem. See - * remarks on RUN_SECOND optimization within dumptuples(). - */ -static bool -useselection(Tuplesortstate *state) -{ - /* - * memtupsize might be noticeably higher than memtupcount here in atypical - * cases. It seems slightly preferable to not allow recent outliers to - * impact this determination. Note that caller's trace_sort output - * reports memtupcount instead. - */ - if (state->memtupsize <= replacement_sort_tuples) - return true; - - return false; -} - /* * inittapes - initialize for tape sorting. * @@ -2413,44 +2274,7 @@ inittapes(Tuplesortstate *state) state->tp_dummy = (int *) palloc0(maxTapes * sizeof(int)); state->tp_tapenum = (int *) palloc0(maxTapes * sizeof(int)); - /* - * Give replacement selection a try based on user setting. There will be - * a switch to a simple hybrid sort-merge strategy after the first run - * (iff we could not output one long run). - */ - state->replaceActive = useselection(state); - - if (state->replaceActive) - { - /* - * Convert the unsorted contents of memtuples[] into a heap. Each - * tuple is marked as belonging to run number zero. - * - * NOTE: we pass false for checkIndex since there's no point in - * comparing indexes in this step, even though we do intend the - * indexes to be part of the sort key... - */ - int ntuples = state->memtupcount; - -#ifdef TRACE_SORT - if (trace_sort) - elog(LOG, "replacement selection will sort %d first run tuples", - state->memtupcount); -#endif - state->memtupcount = 0; /* make the heap empty */ - - for (j = 0; j < ntuples; j++) - { - /* Must copy source tuple to avoid possible overwrite */ - SortTuple stup = state->memtuples[j]; - - stup.tupindex = RUN_FIRST; - tuplesort_heap_insert(state, &stup, false); - } - Assert(state->memtupcount == ntuples); - } - - state->currentRun = RUN_FIRST; + state->currentRun = 0; /* * Initialize variables of Algorithm D (step D1). @@ -2624,22 +2448,6 @@ mergeruns(Tuplesortstate *state) else init_slab_allocator(state, 0); - /* - * If we produced only one initial run (quite likely if the total data - * volume is between 1X and 2X workMem when replacement selection is used, - * but something we particularly count on when input is presorted), we can - * just use that tape as the finished output, rather than doing a useless - * merge. (This obvious optimization is not in Knuth's algorithm.) - */ - if (state->currentRun == RUN_SECOND) - { - state->result_tape = state->tp_tapenum[state->destTape]; - /* must freeze and rewind the finished output tape */ - LogicalTapeFreeze(state->tapeset, state->result_tape); - state->status = TSS_SORTEDONTAPE; - return; - } - /* * Allocate a new 'memtuples' array, for the heap. It will hold one tuple * from each input tape. @@ -2826,11 +2634,11 @@ mergeonerun(Tuplesortstate *state) if (mergereadnext(state, srcTape, &stup)) { stup.tupindex = srcTape; - tuplesort_heap_replace_top(state, &stup, false); + tuplesort_heap_replace_top(state, &stup); } else - tuplesort_heap_delete_top(state, false); + tuplesort_heap_delete_top(state); } /* @@ -2892,7 +2700,7 @@ beginmerge(Tuplesortstate *state) if (mergereadnext(state, srcTape, &tup)) { tup.tupindex = srcTape; - tuplesort_heap_insert(state, &tup, false); + tuplesort_heap_insert(state, &tup); } } } @@ -2922,124 +2730,25 @@ mergereadnext(Tuplesortstate *state, int srcTape, SortTuple *stup) } /* - * dumptuples - remove tuples from memtuples and write to tape - * - * This is used during initial-run building, but not during merging. + * dumptuples - remove tuples from memtuples and write initial run to tape * - * When alltuples = false and replacement selection is still active, dump - * only enough tuples to get under the availMem limit (and leave at least - * one tuple in memtuples, since puttuple will then assume it is a heap that - * has a tuple to compare to). We always insist there be at least one free - * slot in the memtuples[] array. - * - * When alltuples = true, dump everything currently in memory. (This - * case is only used at end of input data, although in practice only the - * first run could fail to dump all tuples when we LACKMEM(), and only - * when replacement selection is active.) - * - * If, when replacement selection is active, we see that the tuple run - * number at the top of the heap has changed, start a new run. This must be - * the first run, because replacement selection is always abandoned for all - * further runs. + * When alltuples = true, dump everything currently in memory. (This case is + * only used at end of input data.) */ static void dumptuples(Tuplesortstate *state, bool alltuples) -{ - while (alltuples || - (LACKMEM(state) && state->memtupcount > 1) || - state->memtupcount >= state->memtupsize) - { - if (state->replaceActive) - { - /* - * Still holding out for a case favorable to replacement - * selection. Still incrementally spilling using heap. - * - * Dump the heap's frontmost entry, and remove it from the heap. - */ - Assert(state->memtupcount > 0); - WRITETUP(state, state->tp_tapenum[state->destTape], - &state->memtuples[0]); - tuplesort_heap_delete_top(state, true); - } - else - { - /* - * Once committed to quicksorting runs, never incrementally spill - */ - dumpbatch(state, alltuples); - break; - } - - /* - * If top run number has changed, we've finished the current run (this - * can only be the first run), and will no longer spill incrementally. - */ - if (state->memtupcount == 0 || - state->memtuples[0].tupindex == HEAP_RUN_NEXT) - { - markrunend(state, state->tp_tapenum[state->destTape]); - Assert(state->currentRun == RUN_FIRST); - state->currentRun++; - state->tp_runs[state->destTape]++; - state->tp_dummy[state->destTape]--; /* per Alg D step D2 */ - -#ifdef TRACE_SORT - if (trace_sort) - elog(LOG, "finished incrementally writing %s run %d to tape %d: %s", - (state->memtupcount == 0) ? "only" : "first", - state->currentRun, state->destTape, - pg_rusage_show(&state->ru_start)); -#endif - - /* - * Done if heap is empty, which is possible when there is only one - * long run. - */ - Assert(state->currentRun == RUN_SECOND); - if (state->memtupcount == 0) - { - /* - * Replacement selection best case; no final merge required, - * because there was only one initial run (second run has no - * tuples). See RUN_SECOND case in mergeruns(). - */ - break; - } - - /* - * Abandon replacement selection for second run (as well as any - * subsequent runs). - */ - state->replaceActive = false; - - /* - * First tuple of next run should not be heapified, and so will - * bear placeholder run number. In practice this must actually be - * the second run, which just became the currentRun, so we're - * clear to quicksort and dump the tuples in batch next time - * memtuples becomes full. - */ - Assert(state->memtuples[0].tupindex == HEAP_RUN_NEXT); - selectnewtape(state); - } - } -} - -/* - * dumpbatch - sort and dump all memtuples, forming one run on tape - * - * Second or subsequent runs are never heapified by this module (although - * heapification still respects run number differences between the first and - * second runs), and a heap (replacement selection priority queue) is often - * avoided in the first place. - */ -static void -dumpbatch(Tuplesortstate *state, bool alltuples) { int memtupwrite; int i; + /* + * Nothing to do if we still fit in available memory and have array + * slots, unless this is the final call during initial run generation. + */ + if (state->memtupcount < state->memtupsize && !LACKMEM(state) && + !alltuples) + return; + /* * Final call might require no sorting, in rare cases where we just so * happen to have previously LACKMEM()'d at the point where exactly all @@ -3308,21 +3017,8 @@ tuplesort_space_type_name(TuplesortSpaceType t) /* * Heap manipulation routines, per Knuth's Algorithm 5.2.3H. - * - * Compare two SortTuples. If checkIndex is true, use the tuple index - * as the front of the sort key; otherwise, no. - * - * Note that for checkIndex callers, the heap invariant is never - * maintained beyond the first run, and so there are no COMPARETUP() - * calls needed to distinguish tuples in HEAP_RUN_NEXT. */ -#define HEAPCOMPARE(tup1,tup2) \ - (checkIndex && ((tup1)->tupindex != (tup2)->tupindex || \ - (tup1)->tupindex == HEAP_RUN_NEXT) ? \ - ((tup1)->tupindex) - ((tup2)->tupindex) : \ - COMPARETUP(state, tup1, tup2)) - /* * Convert the existing unordered array of SortTuples to a bounded heap, * discarding all but the smallest "state->bound" tuples. @@ -3331,10 +3027,6 @@ tuplesort_space_type_name(TuplesortSpaceType t) * at the root (array entry zero), instead of the smallest as in the normal * sort case. This allows us to discard the largest entry cheaply. * Therefore, we temporarily reverse the sort direction. - * - * We assume that all entries in a bounded heap will always have tupindex - * zero; it therefore doesn't matter that HEAPCOMPARE() doesn't reverse - * the direction of comparison for tupindexes. */ static void make_bounded_heap(Tuplesortstate *state) @@ -3358,8 +3050,7 @@ make_bounded_heap(Tuplesortstate *state) /* Must copy source tuple to avoid possible overwrite */ SortTuple stup = state->memtuples[i]; - stup.tupindex = 0; /* not used */ - tuplesort_heap_insert(state, &stup, false); + tuplesort_heap_insert(state, &stup); } else { @@ -3374,7 +3065,7 @@ make_bounded_heap(Tuplesortstate *state) CHECK_FOR_INTERRUPTS(); } else - tuplesort_heap_replace_top(state, &state->memtuples[i], false); + tuplesort_heap_replace_top(state, &state->memtuples[i]); } } @@ -3404,7 +3095,7 @@ sort_bounded_heap(Tuplesortstate *state) SortTuple stup = state->memtuples[0]; /* this sifts-up the next-largest entry and decreases memtupcount */ - tuplesort_heap_delete_top(state, false); + tuplesort_heap_delete_top(state); state->memtuples[state->memtupcount] = stup; } state->memtupcount = tupcount; @@ -3422,10 +3113,7 @@ sort_bounded_heap(Tuplesortstate *state) /* * Sort all memtuples using specialized qsort() routines. * - * Quicksort is used for small in-memory sorts. Quicksort is also generally - * preferred to replacement selection for generating runs during external sort - * operations, although replacement selection is sometimes used for the first - * run. + * Quicksort is used for small in-memory sorts, and external sort runs. */ static void tuplesort_sort_memtuples(Tuplesortstate *state) @@ -3454,15 +3142,13 @@ tuplesort_sort_memtuples(Tuplesortstate *state) * is, it might get overwritten before being moved into the heap! */ static void -tuplesort_heap_insert(Tuplesortstate *state, SortTuple *tuple, - bool checkIndex) +tuplesort_heap_insert(Tuplesortstate *state, SortTuple *tuple) { SortTuple *memtuples; int j; memtuples = state->memtuples; Assert(state->memtupcount < state->memtupsize); - Assert(!checkIndex || tuple->tupindex == RUN_FIRST); CHECK_FOR_INTERRUPTS(); @@ -3475,7 +3161,7 @@ tuplesort_heap_insert(Tuplesortstate *state, SortTuple *tuple, { int i = (j - 1) >> 1; - if (HEAPCOMPARE(tuple, &memtuples[i]) >= 0) + if (COMPARETUP(state, tuple, &memtuples[i]) >= 0) break; memtuples[j] = memtuples[i]; j = i; @@ -3491,12 +3177,11 @@ tuplesort_heap_insert(Tuplesortstate *state, SortTuple *tuple, * if necessary. */ static void -tuplesort_heap_delete_top(Tuplesortstate *state, bool checkIndex) +tuplesort_heap_delete_top(Tuplesortstate *state) { SortTuple *memtuples = state->memtuples; SortTuple *tuple; - Assert(!checkIndex || state->currentRun == RUN_FIRST); if (--state->memtupcount <= 0) return; @@ -3505,7 +3190,7 @@ tuplesort_heap_delete_top(Tuplesortstate *state, bool checkIndex) * current top node with it. */ tuple = &memtuples[state->memtupcount]; - tuplesort_heap_replace_top(state, tuple, checkIndex); + tuplesort_heap_replace_top(state, tuple); } /* @@ -3516,14 +3201,12 @@ tuplesort_heap_delete_top(Tuplesortstate *state, bool checkIndex) * Heapsort, steps H3-H8). */ static void -tuplesort_heap_replace_top(Tuplesortstate *state, SortTuple *tuple, - bool checkIndex) +tuplesort_heap_replace_top(Tuplesortstate *state, SortTuple *tuple) { SortTuple *memtuples = state->memtuples; unsigned int i, n; - Assert(!checkIndex || state->currentRun == RUN_FIRST); Assert(state->memtupcount >= 1); CHECK_FOR_INTERRUPTS(); @@ -3542,9 +3225,9 @@ tuplesort_heap_replace_top(Tuplesortstate *state, SortTuple *tuple, if (j >= n) break; if (j + 1 < n && - HEAPCOMPARE(&memtuples[j], &memtuples[j + 1]) > 0) + COMPARETUP(state, &memtuples[j], &memtuples[j + 1]) > 0) j++; - if (HEAPCOMPARE(tuple, &memtuples[j]) <= 0) + if (COMPARETUP(state, tuple, &memtuples[j]) <= 0) break; memtuples[i] = memtuples[j]; i = j; diff --git a/src/bin/pg_basebackup/streamutil.c b/src/bin/pg_basebackup/streamutil.c index 81fef8cd516c6..a57ff8f2c4719 100644 --- a/src/bin/pg_basebackup/streamutil.c +++ b/src/bin/pg_basebackup/streamutil.c @@ -17,18 +17,15 @@ #include #include -/* for ntohl/htonl */ -#include -#include - /* local includes */ #include "receivelog.h" #include "streamutil.h" #include "access/xlog_internal.h" -#include "pqexpbuffer.h" #include "common/fe_memutils.h" #include "datatype/timestamp.h" +#include "port/pg_bswap.h" +#include "pqexpbuffer.h" #define ERRCODE_DUPLICATE_OBJECT "42710" @@ -576,17 +573,9 @@ feTimestampDifferenceExceeds(TimestampTz start_time, void fe_sendint64(int64 i, char *buf) { - uint32 n32; + uint64 n64 = pg_hton64(i); - /* High order half first, since we're doing MSB-first */ - n32 = (uint32) (i >> 32); - n32 = htonl(n32); - memcpy(&buf[0], &n32, 4); - - /* Now the low order half */ - n32 = (uint32) i; - n32 = htonl(n32); - memcpy(&buf[4], &n32, 4); + memcpy(buf, &n64, sizeof(n64)); } /* @@ -595,18 +584,9 @@ fe_sendint64(int64 i, char *buf) int64 fe_recvint64(char *buf) { - int64 result; - uint32 h32; - uint32 l32; - - memcpy(&h32, buf, 4); - memcpy(&l32, buf + 4, 4); - h32 = ntohl(h32); - l32 = ntohl(l32); + uint64 n64; - result = h32; - result <<= 32; - result |= l32; + memcpy(&n64, buf, sizeof(n64)); - return result; + return pg_ntoh64(n64); } diff --git a/src/bin/pg_ctl/pg_ctl.c b/src/bin/pg_ctl/pg_ctl.c index 4e02c4cea1a51..0893d0d26f174 100644 --- a/src/bin/pg_ctl/pg_ctl.c +++ b/src/bin/pg_ctl/pg_ctl.c @@ -1903,7 +1903,7 @@ do_help(void) printf(_(" immediate quit without complete shutdown; will lead to recovery on restart\n")); printf(_("\nAllowed signal names for kill:\n")); - printf(" ABRT HUP INT QUIT TERM USR1 USR2\n"); + printf(" ABRT HUP INT KILL QUIT TERM USR1 USR2\n"); #ifdef WIN32 printf(_("\nOptions for register and unregister:\n")); @@ -1961,11 +1961,8 @@ set_sig(char *signame) sig = SIGQUIT; else if (strcmp(signame, "ABRT") == 0) sig = SIGABRT; -#if 0 - /* probably should NOT provide SIGKILL */ else if (strcmp(signame, "KILL") == 0) sig = SIGKILL; -#endif else if (strcmp(signame, "TERM") == 0) sig = SIGTERM; else if (strcmp(signame, "USR1") == 0) diff --git a/src/bin/pg_dump/parallel.c b/src/bin/pg_dump/parallel.c index 8ad51942ff27f..8b996f46990b4 100644 --- a/src/bin/pg_dump/parallel.c +++ b/src/bin/pg_dump/parallel.c @@ -63,7 +63,9 @@ #include "parallel.h" #include "pg_backup_utils.h" + #include "fe_utils/string_utils.h" +#include "port/pg_bswap.h" /* Mnemonic macros for indexing the fd array returned by pipe(2) */ #define PIPE_READ 0 @@ -1764,8 +1766,8 @@ pgpipe(int handles[2]) memset((void *) &serv_addr, 0, sizeof(serv_addr)); serv_addr.sin_family = AF_INET; - serv_addr.sin_port = htons(0); - serv_addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK); + serv_addr.sin_port = pg_hton16(0); + serv_addr.sin_addr.s_addr = pg_hton32(INADDR_LOOPBACK); if (bind(s, (SOCKADDR *) &serv_addr, len) == SOCKET_ERROR) { write_msg(modulename, "pgpipe: could not bind: error code %d\n", diff --git a/src/bin/pg_dump/pg_dump.c b/src/bin/pg_dump/pg_dump.c index 75f08cd7924a4..e34c83acd2a6c 100644 --- a/src/bin/pg_dump/pg_dump.c +++ b/src/bin/pg_dump/pg_dump.c @@ -253,7 +253,9 @@ static void dumpDatabase(Archive *AH); static void dumpEncoding(Archive *AH); static void dumpStdStrings(Archive *AH); static void binary_upgrade_set_type_oids_by_type_oid(Archive *fout, - PQExpBuffer upgrade_buffer, Oid pg_type_oid); + PQExpBuffer upgrade_buffer, + Oid pg_type_oid, + bool force_array_type); static bool binary_upgrade_set_type_oids_by_rel_oid(Archive *fout, PQExpBuffer upgrade_buffer, Oid pg_rel_oid); static void binary_upgrade_set_pg_class_oids(Archive *fout, @@ -3949,10 +3951,11 @@ dumpSubscription(Archive *fout, SubscriptionInfo *subinfo) static void binary_upgrade_set_type_oids_by_type_oid(Archive *fout, PQExpBuffer upgrade_buffer, - Oid pg_type_oid) + Oid pg_type_oid, + bool force_array_type) { PQExpBuffer upgrade_query = createPQExpBuffer(); - PGresult *upgrade_res; + PGresult *res; Oid pg_type_array_oid; appendPQExpBufferStr(upgrade_buffer, "\n-- For binary upgrade, must preserve pg_type oid\n"); @@ -3964,12 +3967,43 @@ binary_upgrade_set_type_oids_by_type_oid(Archive *fout, appendPQExpBuffer(upgrade_query, "SELECT typarray " "FROM pg_catalog.pg_type " - "WHERE pg_type.oid = '%u'::pg_catalog.oid;", + "WHERE oid = '%u'::pg_catalog.oid;", pg_type_oid); - upgrade_res = ExecuteSqlQueryForSingleRow(fout, upgrade_query->data); + res = ExecuteSqlQueryForSingleRow(fout, upgrade_query->data); + + pg_type_array_oid = atooid(PQgetvalue(res, 0, PQfnumber(res, "typarray"))); - pg_type_array_oid = atooid(PQgetvalue(upgrade_res, 0, PQfnumber(upgrade_res, "typarray"))); + PQclear(res); + + if (!OidIsValid(pg_type_array_oid) && force_array_type) + { + /* + * If the old version didn't assign an array type, but the new version + * does, we must select an unused type OID to assign. This currently + * only happens for domains, when upgrading pre-v11 to v11 and up. + * + * Note: local state here is kind of ugly, but we must have some, + * since we mustn't choose the same unused OID more than once. + */ + static Oid next_possible_free_oid = FirstNormalObjectId; + bool is_dup; + + do + { + ++next_possible_free_oid; + printfPQExpBuffer(upgrade_query, + "SELECT EXISTS(SELECT 1 " + "FROM pg_catalog.pg_type " + "WHERE oid = '%u'::pg_catalog.oid);", + next_possible_free_oid); + res = ExecuteSqlQueryForSingleRow(fout, upgrade_query->data); + is_dup = (PQgetvalue(res, 0, 0)[0] == 't'); + PQclear(res); + } while (is_dup); + + pg_type_array_oid = next_possible_free_oid; + } if (OidIsValid(pg_type_array_oid)) { @@ -3980,7 +4014,6 @@ binary_upgrade_set_type_oids_by_type_oid(Archive *fout, pg_type_array_oid); } - PQclear(upgrade_res); destroyPQExpBuffer(upgrade_query); } @@ -4008,7 +4041,7 @@ binary_upgrade_set_type_oids_by_rel_oid(Archive *fout, pg_type_oid = atooid(PQgetvalue(upgrade_res, 0, PQfnumber(upgrade_res, "crel"))); binary_upgrade_set_type_oids_by_type_oid(fout, upgrade_buffer, - pg_type_oid); + pg_type_oid, false); if (!PQgetisnull(upgrade_res, 0, PQfnumber(upgrade_res, "trel"))) { @@ -9840,7 +9873,8 @@ dumpEnumType(Archive *fout, TypeInfo *tyinfo) if (dopt->binary_upgrade) binary_upgrade_set_type_oids_by_type_oid(fout, q, - tyinfo->dobj.catId.oid); + tyinfo->dobj.catId.oid, + false); appendPQExpBuffer(q, "CREATE TYPE %s AS ENUM (", qtypname); @@ -9976,8 +10010,9 @@ dumpRangeType(Archive *fout, TypeInfo *tyinfo) qtypname); if (dopt->binary_upgrade) - binary_upgrade_set_type_oids_by_type_oid(fout, - q, tyinfo->dobj.catId.oid); + binary_upgrade_set_type_oids_by_type_oid(fout, q, + tyinfo->dobj.catId.oid, + false); appendPQExpBuffer(q, "CREATE TYPE %s AS RANGE (", qtypname); @@ -10091,8 +10126,9 @@ dumpUndefinedType(Archive *fout, TypeInfo *tyinfo) qtypname); if (dopt->binary_upgrade) - binary_upgrade_set_type_oids_by_type_oid(fout, - q, tyinfo->dobj.catId.oid); + binary_upgrade_set_type_oids_by_type_oid(fout, q, + tyinfo->dobj.catId.oid, + false); appendPQExpBuffer(q, "CREATE TYPE %s;\n", qtypname); @@ -10296,10 +10332,14 @@ dumpBaseType(Archive *fout, TypeInfo *tyinfo) appendPQExpBuffer(delq, "%s CASCADE;\n", qtypname); - /* We might already have a shell type, but setting pg_type_oid is harmless */ + /* + * We might already have a shell type, but setting pg_type_oid is + * harmless, and in any case we'd better set the array type OID. + */ if (dopt->binary_upgrade) binary_upgrade_set_type_oids_by_type_oid(fout, q, - tyinfo->dobj.catId.oid); + tyinfo->dobj.catId.oid, + false); appendPQExpBuffer(q, "CREATE TYPE %s (\n" @@ -10490,7 +10530,8 @@ dumpDomain(Archive *fout, TypeInfo *tyinfo) if (dopt->binary_upgrade) binary_upgrade_set_type_oids_by_type_oid(fout, q, - tyinfo->dobj.catId.oid); + tyinfo->dobj.catId.oid, + true); /* force array type */ qtypname = pg_strdup(fmtId(tyinfo->dobj.name)); @@ -10692,7 +10733,8 @@ dumpCompositeType(Archive *fout, TypeInfo *tyinfo) if (dopt->binary_upgrade) { binary_upgrade_set_type_oids_by_type_oid(fout, q, - tyinfo->dobj.catId.oid); + tyinfo->dobj.catId.oid, + false); binary_upgrade_set_pg_class_oids(fout, q, tyinfo->typrelid, false); } @@ -10967,7 +11009,8 @@ dumpShellType(Archive *fout, ShellTypeInfo *stinfo) if (dopt->binary_upgrade) binary_upgrade_set_type_oids_by_type_oid(fout, q, - stinfo->baseType->dobj.catId.oid); + stinfo->baseType->dobj.catId.oid, + false); appendPQExpBuffer(q, "CREATE TYPE %s;\n", fmtId(stinfo->dobj.name)); diff --git a/src/bin/pg_rewind/libpq_fetch.c b/src/bin/pg_rewind/libpq_fetch.c index 0cdff55cabea4..79bec40b02fac 100644 --- a/src/bin/pg_rewind/libpq_fetch.c +++ b/src/bin/pg_rewind/libpq_fetch.c @@ -14,10 +14,6 @@ #include #include -/* for ntohl/htonl */ -#include -#include - #include "pg_rewind.h" #include "datapagemap.h" #include "fetch.h" @@ -28,6 +24,7 @@ #include "libpq-fe.h" #include "catalog/catalog.h" #include "catalog/pg_type.h" +#include "port/pg_bswap.h" static PGconn *conn = NULL; @@ -220,28 +217,6 @@ libpqProcessFileList(void) PQclear(res); } -/* - * Converts an int64 from network byte order to native format. - */ -static int64 -pg_recvint64(int64 value) -{ - union - { - int64 i64; - uint32 i32[2]; - } swap; - int64 result; - - swap.i64 = value; - - result = (uint32) ntohl(swap.i32[0]); - result <<= 32; - result |= (uint32) ntohl(swap.i32[1]); - - return result; -} - /*---- * Runs a query, which returns pieces of files from the remote source data * directory, and overwrites the corresponding parts of target files with @@ -318,7 +293,7 @@ receiveFileChunks(const char *sql) /* Read result set to local variables */ memcpy(&chunkoff, PQgetvalue(res, 0, 1), sizeof(int64)); - chunkoff = pg_recvint64(chunkoff); + chunkoff = pg_ntoh64(chunkoff); chunksize = PQgetlength(res, 0, 2); filenamelen = PQgetlength(res, 0, 0); diff --git a/src/bin/pgbench/pgbench.c b/src/bin/pgbench/pgbench.c index e37496c971459..5d8a01c72cf63 100644 --- a/src/bin/pgbench/pgbench.c +++ b/src/bin/pgbench/pgbench.c @@ -2194,12 +2194,8 @@ doCustom(TState *thread, CState *st, StatsData *agg) { if (!sendCommand(st, command)) { - /* - * Failed. Stay in CSTATE_START_COMMAND state, to - * retry. ??? What the point or retrying? Should - * rather abort? - */ - return; + commandFailed(st, "SQL command send failed"); + st->state = CSTATE_ABORTED; } else st->state = CSTATE_WAIT_RESULT; @@ -4582,20 +4578,30 @@ threadRun(void *arg) * or it's time to print a progress report. Update input_mask to show * which client(s) received data. */ - if (min_usec > 0 && maxsock != -1) + if (min_usec > 0) { - int nsocks; /* return from select(2) */ + int nsocks = 0; /* return from select(2) if called */ if (min_usec != PG_INT64_MAX) { - struct timeval timeout; + if (maxsock != -1) + { + struct timeval timeout; - timeout.tv_sec = min_usec / 1000000; - timeout.tv_usec = min_usec % 1000000; - nsocks = select(maxsock + 1, &input_mask, NULL, NULL, &timeout); + timeout.tv_sec = min_usec / 1000000; + timeout.tv_usec = min_usec % 1000000; + nsocks = select(maxsock + 1, &input_mask, NULL, NULL, &timeout); + } + else /* nothing active, simple sleep */ + { + pg_usleep(min_usec); + } } - else + else /* no explicit delay, select without timeout */ + { nsocks = select(maxsock + 1, &input_mask, NULL, NULL, NULL); + } + if (nsocks < 0) { if (errno == EINTR) @@ -4608,7 +4614,7 @@ threadRun(void *arg) goto done; } } - else + else /* min_usec == 0, i.e. something needs to be executed */ { /* If we didn't call select(), don't try to read any data */ FD_ZERO(&input_mask); diff --git a/src/bin/psql/describe.c b/src/bin/psql/describe.c index 3a8cee1efa944..d99934c3bdddc 100644 --- a/src/bin/psql/describe.c +++ b/src/bin/psql/describe.c @@ -1415,7 +1415,7 @@ describeOneTableDetails(const char *schemaname, bool printTableInitialized = false; int i; char *view_def = NULL; - char *headers[16]; + char *headers[18]; char **seq_values = NULL; char **ptr; PQExpBufferData title; @@ -1631,27 +1631,125 @@ describeOneTableDetails(const char *schemaname, res = NULL; /* - * If it's a sequence, fetch its values and store into an array that will - * be used later. + * If it's a sequence, deal with it here separately. */ if (tableinfo.relkind == RELKIND_SEQUENCE) { - printfPQExpBuffer(&buf, "SELECT * FROM %s", fmtId(schemaname)); - /* must be separate because fmtId isn't reentrant */ - appendPQExpBuffer(&buf, ".%s;", fmtId(relationname)); + PGresult *result = NULL; + printQueryOpt myopt = pset.popt; + char *footers[2] = {NULL, NULL}; + + if (pset.sversion >= 100000) + { + printfPQExpBuffer(&buf, + "SELECT pg_catalog.format_type(seqtypid, NULL) AS \"%s\",\n" + " seqstart AS \"%s\",\n" + " seqmin AS \"%s\",\n" + " seqmax AS \"%s\",\n" + " seqincrement AS \"%s\",\n" + " CASE WHEN seqcycle THEN '%s' ELSE '%s' END AS \"%s\",\n" + " seqcache AS \"%s\"\n", + gettext_noop("Type"), + gettext_noop("Start"), + gettext_noop("Minimum"), + gettext_noop("Maximum"), + gettext_noop("Increment"), + gettext_noop("yes"), + gettext_noop("no"), + gettext_noop("Cycles?"), + gettext_noop("Cache")); + appendPQExpBuffer(&buf, + "FROM pg_catalog.pg_sequence\n" + "WHERE seqrelid = '%s';", + oid); + } + else + { + printfPQExpBuffer(&buf, + "SELECT pg_catalog.format_type('bigint'::regtype, NULL) AS \"%s\",\n" + " start_value AS \"%s\",\n" + " min_value AS \"%s\",\n" + " max_value AS \"%s\",\n" + " increment_by AS \"%s\",\n" + " CASE WHEN is_cycled THEN '%s' ELSE '%s' END AS \"%s\",\n" + " cache_value AS \"%s\"\n", + gettext_noop("Type"), + gettext_noop("Start"), + gettext_noop("Minimum"), + gettext_noop("Maximum"), + gettext_noop("Increment"), + gettext_noop("yes"), + gettext_noop("no"), + gettext_noop("Cycles?"), + gettext_noop("Cache")); + appendPQExpBuffer(&buf, "FROM %s", fmtId(schemaname)); + /* must be separate because fmtId isn't reentrant */ + appendPQExpBuffer(&buf, ".%s;", fmtId(relationname)); + } res = PSQLexec(buf.data); if (!res) goto error_return; - seq_values = pg_malloc((PQnfields(res) + 1) * sizeof(*seq_values)); + /* Footer information about a sequence */ + + /* Get the column that owns this sequence */ + printfPQExpBuffer(&buf, "SELECT pg_catalog.quote_ident(nspname) || '.' ||" + "\n pg_catalog.quote_ident(relname) || '.' ||" + "\n pg_catalog.quote_ident(attname)," + "\n d.deptype" + "\nFROM pg_catalog.pg_class c" + "\nINNER JOIN pg_catalog.pg_depend d ON c.oid=d.refobjid" + "\nINNER JOIN pg_catalog.pg_namespace n ON n.oid=c.relnamespace" + "\nINNER JOIN pg_catalog.pg_attribute a ON (" + "\n a.attrelid=c.oid AND" + "\n a.attnum=d.refobjsubid)" + "\nWHERE d.classid='pg_catalog.pg_class'::pg_catalog.regclass" + "\n AND d.refclassid='pg_catalog.pg_class'::pg_catalog.regclass" + "\n AND d.objid='%s'" + "\n AND d.deptype IN ('a', 'i')", + oid); + + result = PSQLexec(buf.data); + + /* + * If we get no rows back, don't show anything (obviously). We should + * never get more than one row back, but if we do, just ignore it and + * don't print anything. + */ + if (!result) + goto error_return; + else if (PQntuples(result) == 1) + { + switch (PQgetvalue(result, 0, 1)[0]) + { + case 'a': + footers[0] = psprintf(_("Owned by: %s"), + PQgetvalue(result, 0, 0)); + break; + case 'i': + footers[0] = psprintf(_("Sequence for identity column: %s"), + PQgetvalue(result, 0, 0)); + break; + } + } + PQclear(result); + + printfPQExpBuffer(&title, _("Sequence \"%s.%s\""), + schemaname, relationname); - for (i = 0; i < PQnfields(res); i++) - seq_values[i] = pg_strdup(PQgetvalue(res, 0, i)); - seq_values[i] = NULL; + myopt.footers = footers; + myopt.topt.default_footer = false; + myopt.title = title.data; + myopt.translate_header = true; - PQclear(res); - res = NULL; + printQuery(res, &myopt, pset.queryFout, false, pset.logfile); + + if (footers[0]) + free(footers[0]); + + retval = true; + goto error_return; /* not an error, just return early */ } /* @@ -1766,10 +1864,6 @@ describeOneTableDetails(const char *schemaname, printfPQExpBuffer(&title, _("Materialized view \"%s.%s\""), schemaname, relationname); break; - case RELKIND_SEQUENCE: - printfPQExpBuffer(&title, _("Sequence \"%s.%s\""), - schemaname, relationname); - break; case RELKIND_INDEX: if (tableinfo.relpersistence == 'u') printfPQExpBuffer(&title, _("Unlogged index \"%s.%s\""), @@ -1836,9 +1930,6 @@ describeOneTableDetails(const char *schemaname, show_column_details = true; } - if (tableinfo.relkind == RELKIND_SEQUENCE) - headers[cols++] = gettext_noop("Value"); - if (tableinfo.relkind == RELKIND_INDEX) headers[cols++] = gettext_noop("Definition"); @@ -1930,10 +2021,6 @@ describeOneTableDetails(const char *schemaname, printTableAddCell(&cont, default_str, false, false); } - /* Value: for sequences only */ - if (tableinfo.relkind == RELKIND_SEQUENCE) - printTableAddCell(&cont, seq_values[i], false, false); - /* Expression for index column */ if (tableinfo.relkind == RELKIND_INDEX) printTableAddCell(&cont, PQgetvalue(res, i, 7), false, false); @@ -2016,13 +2103,16 @@ describeOneTableDetails(const char *schemaname, partdef); printTableAddFooter(&cont, tmpbuf.data); - /* If there isn't any constraint, show that explicitly */ - if (partconstraintdef == NULL || partconstraintdef[0] == '\0') - printfPQExpBuffer(&tmpbuf, _("No partition constraint")); - else - printfPQExpBuffer(&tmpbuf, _("Partition constraint: %s"), - partconstraintdef); - printTableAddFooter(&cont, tmpbuf.data); + if (verbose) + { + /* If there isn't any constraint, show that explicitly */ + if (partconstraintdef == NULL || partconstraintdef[0] == '\0') + printfPQExpBuffer(&tmpbuf, _("No partition constraint")); + else + printfPQExpBuffer(&tmpbuf, _("Partition constraint: %s"), + partconstraintdef); + printTableAddFooter(&cont, tmpbuf.data); + } PQclear(result); } @@ -2146,55 +2236,6 @@ describeOneTableDetails(const char *schemaname, PQclear(result); } - else if (tableinfo.relkind == RELKIND_SEQUENCE) - { - /* Footer information about a sequence */ - PGresult *result = NULL; - - /* Get the column that owns this sequence */ - printfPQExpBuffer(&buf, "SELECT pg_catalog.quote_ident(nspname) || '.' ||" - "\n pg_catalog.quote_ident(relname) || '.' ||" - "\n pg_catalog.quote_ident(attname)," - "\n d.deptype" - "\nFROM pg_catalog.pg_class c" - "\nINNER JOIN pg_catalog.pg_depend d ON c.oid=d.refobjid" - "\nINNER JOIN pg_catalog.pg_namespace n ON n.oid=c.relnamespace" - "\nINNER JOIN pg_catalog.pg_attribute a ON (" - "\n a.attrelid=c.oid AND" - "\n a.attnum=d.refobjsubid)" - "\nWHERE d.classid='pg_catalog.pg_class'::pg_catalog.regclass" - "\n AND d.refclassid='pg_catalog.pg_class'::pg_catalog.regclass" - "\n AND d.objid='%s'" - "\n AND d.deptype IN ('a', 'i')", - oid); - - result = PSQLexec(buf.data); - if (!result) - goto error_return; - else if (PQntuples(result) == 1) - { - switch (PQgetvalue(result, 0, 1)[0]) - { - case 'a': - printfPQExpBuffer(&buf, _("Owned by: %s"), - PQgetvalue(result, 0, 0)); - printTableAddFooter(&cont, buf.data); - break; - case 'i': - printfPQExpBuffer(&buf, _("Sequence for identity column: %s"), - PQgetvalue(result, 0, 0)); - printTableAddFooter(&cont, buf.data); - break; - } - } - - /* - * If we get no rows back, don't show anything (obviously). We should - * never get more than one row back, but if we do, just ignore it and - * don't print anything. - */ - PQclear(result); - } else if (tableinfo.relkind == RELKIND_RELATION || tableinfo.relkind == RELKIND_MATVIEW || tableinfo.relkind == RELKIND_FOREIGN_TABLE || @@ -3161,13 +3202,6 @@ describeOneTableDetails(const char *schemaname, termPQExpBuffer(&title); termPQExpBuffer(&tmpbuf); - if (seq_values) - { - for (ptr = seq_values; *ptr; ptr++) - free(*ptr); - free(seq_values); - } - if (view_def) free(view_def); diff --git a/src/bin/scripts/t/080_pg_isready.pl b/src/bin/scripts/t/080_pg_isready.pl index d9830b5b3adcf..d01804da37165 100644 --- a/src/bin/scripts/t/080_pg_isready.pl +++ b/src/bin/scripts/t/080_pg_isready.pl @@ -15,4 +15,5 @@ $node->init; $node->start; -$node->command_ok(['pg_isready'], 'succeeds with server running'); +# use a long timeout for the benefit of very slow buildfarm machines +$node->command_ok([qw(pg_isready --timeout=60)], 'succeeds with server running'); diff --git a/src/common/scram-common.c b/src/common/scram-common.c index e43d035d4da82..e54fe1a7c985d 100644 --- a/src/common/scram-common.c +++ b/src/common/scram-common.c @@ -19,12 +19,9 @@ #include "postgres_fe.h" #endif -/* for htonl */ -#include -#include - #include "common/base64.h" #include "common/scram-common.h" +#include "port/pg_bswap.h" #define HMAC_IPAD 0x36 #define HMAC_OPAD 0x5C @@ -109,7 +106,7 @@ scram_SaltedPassword(const char *password, uint8 *result) { int password_len = strlen(password); - uint32 one = htonl(1); + uint32 one = pg_hton32(1); int i, j; uint8 Ui[SCRAM_KEY_LEN]; diff --git a/src/include/catalog/catversion.h b/src/include/catalog/catversion.h index 5d57a95d8bb22..2c382a73cfbb2 100644 --- a/src/include/catalog/catversion.h +++ b/src/include/catalog/catversion.h @@ -53,6 +53,6 @@ */ /* yyyymmddN */ -#define CATALOG_VERSION_NO 201709191 +#define CATALOG_VERSION_NO 201709301 #endif diff --git a/src/include/executor/execExpr.h b/src/include/executor/execExpr.h index 8ee0496e0106b..78d22478166fa 100644 --- a/src/include/executor/execExpr.h +++ b/src/include/executor/execExpr.h @@ -385,10 +385,8 @@ typedef struct ExprEvalStep /* for EEOP_ARRAYCOERCE */ struct { - ArrayCoerceExpr *coerceexpr; + ExprState *elemexprstate; /* null if no per-element work */ Oid resultelemtype; /* element type of result array */ - FmgrInfo *elemfunc; /* lookup info for element coercion - * function */ struct ArrayMapState *amstate; /* workspace for array_map */ } arraycoerce; @@ -621,7 +619,8 @@ extern void ExecEvalRowNull(ExprState *state, ExprEvalStep *op, extern void ExecEvalRowNotNull(ExprState *state, ExprEvalStep *op, ExprContext *econtext); extern void ExecEvalArrayExpr(ExprState *state, ExprEvalStep *op); -extern void ExecEvalArrayCoerce(ExprState *state, ExprEvalStep *op); +extern void ExecEvalArrayCoerce(ExprState *state, ExprEvalStep *op, + ExprContext *econtext); extern void ExecEvalRow(ExprState *state, ExprEvalStep *op); extern void ExecEvalMinMax(ExprState *state, ExprEvalStep *op); extern void ExecEvalFieldSelect(ExprState *state, ExprEvalStep *op, diff --git a/src/include/miscadmin.h b/src/include/miscadmin.h index dad98de98d7c3..3950054368b4a 100644 --- a/src/include/miscadmin.h +++ b/src/include/miscadmin.h @@ -241,7 +241,6 @@ extern bool enableFsync; extern bool allowSystemTableMods; extern PGDLLIMPORT int work_mem; extern PGDLLIMPORT int maintenance_work_mem; -extern PGDLLIMPORT int replacement_sort_tuples; extern int VacuumCostPageHit; extern int VacuumCostPageMiss; diff --git a/src/include/nodes/primnodes.h b/src/include/nodes/primnodes.h index 8c536a8d38d8c..ccb5123e2ec64 100644 --- a/src/include/nodes/primnodes.h +++ b/src/include/nodes/primnodes.h @@ -820,11 +820,12 @@ typedef struct CoerceViaIO * ArrayCoerceExpr * * ArrayCoerceExpr represents a type coercion from one array type to another, - * which is implemented by applying the indicated element-type coercion - * function to each element of the source array. If elemfuncid is InvalidOid - * then the element types are binary-compatible, but the coercion still - * requires some effort (we have to fix the element type ID stored in the - * array header). + * which is implemented by applying the per-element coercion expression + * "elemexpr" to each element of the source array. Within elemexpr, the + * source element is represented by a CaseTestExpr node. Note that even if + * elemexpr is a no-op (that is, just CaseTestExpr + RelabelType), the + * coercion still requires some effort: we have to fix the element type OID + * stored in the array header. * ---------------- */ @@ -832,11 +833,10 @@ typedef struct ArrayCoerceExpr { Expr xpr; Expr *arg; /* input expression (yields an array) */ - Oid elemfuncid; /* OID of element coercion function, or 0 */ + Expr *elemexpr; /* expression representing per-element work */ Oid resulttype; /* output type of coercion (an array type) */ int32 resulttypmod; /* output typmod (also element typmod) */ Oid resultcollid; /* OID of collation, or InvalidOid if none */ - bool isExplicit; /* conversion semantics flag to pass to func */ CoercionForm coerceformat; /* how to display this node */ int location; /* token location, or -1 if unknown */ } ArrayCoerceExpr; diff --git a/src/include/parser/parse_coerce.h b/src/include/parser/parse_coerce.h index 06f65293cb39d..e560f0c96e443 100644 --- a/src/include/parser/parse_coerce.h +++ b/src/include/parser/parse_coerce.h @@ -48,9 +48,8 @@ extern Node *coerce_type(ParseState *pstate, Node *node, CoercionContext ccontext, CoercionForm cformat, int location); extern Node *coerce_to_domain(Node *arg, Oid baseTypeId, int32 baseTypeMod, Oid typeId, - CoercionForm cformat, int location, - bool hideInputCoercion, - bool lengthCoercionDone); + CoercionContext ccontext, CoercionForm cformat, int location, + bool hideInputCoercion); extern Node *coerce_to_boolean(ParseState *pstate, Node *node, const char *constructName); diff --git a/src/include/pg_config.h.in b/src/include/pg_config.h.in index 39286836dc0fd..368a297e6df48 100644 --- a/src/include/pg_config.h.in +++ b/src/include/pg_config.h.in @@ -668,6 +668,9 @@ /* Define to 1 if you have the header file. */ #undef HAVE_WINLDAP_H +/* Define to 1 if your compiler understands __builtin_bswap16. */ +#undef HAVE__BUILTIN_BSWAP16 + /* Define to 1 if your compiler understands __builtin_bswap32. */ #undef HAVE__BUILTIN_BSWAP32 diff --git a/src/include/pg_config.h.win32 b/src/include/pg_config.h.win32 index d90f5a6b5b756..3537b6f7045ca 100644 --- a/src/include/pg_config.h.win32 +++ b/src/include/pg_config.h.win32 @@ -493,6 +493,9 @@ /* Define to 1 if you have the header file. */ /* #undef HAVE_WINLDAP_H */ +/* Define to 1 if your compiler understands __builtin_bswap16. */ +/* #undef HAVE__BUILTIN_BSWAP16 */ + /* Define to 1 if your compiler understands __builtin_bswap32. */ /* #undef HAVE__BUILTIN_BSWAP32 */ diff --git a/src/include/port/pg_bswap.h b/src/include/port/pg_bswap.h index 50a6bd106b0bf..0b2dcf52cb581 100644 --- a/src/include/port/pg_bswap.h +++ b/src/include/port/pg_bswap.h @@ -3,15 +3,13 @@ * pg_bswap.h * Byte swapping. * - * Macros for reversing the byte order of 32-bit and 64-bit unsigned integers. + * Macros for reversing the byte order of 16, 32 and 64-bit unsigned integers. * For example, 0xAABBCCDD becomes 0xDDCCBBAA. These are just wrappers for * built-in functions provided by the compiler where support exists. - * Elsewhere, beware of multiple evaluations of the arguments! * - * Note that the GCC built-in functions __builtin_bswap32() and - * __builtin_bswap64() are documented as accepting single arguments of type - * uint32_t and uint64_t respectively (these are also the respective return - * types). Use caution when using these wrapper macros with signed integers. + * Note that all of these functions accept unsigned integers as arguments and + * return the same. Use caution when using these wrapper macros with signed + * integers. * * Copyright (c) 2015-2017, PostgreSQL Global Development Group * @@ -22,28 +20,114 @@ #ifndef PG_BSWAP_H #define PG_BSWAP_H -#ifdef HAVE__BUILTIN_BSWAP32 -#define BSWAP32(x) __builtin_bswap32(x) + +/* + * In all supported versions msvc provides _byteswap_* functions in stdlib.h, + * already included by c.h. + */ + + +/* implementation of uint16 pg_bswap16(uint16) */ +#if defined(HAVE__BUILTIN_BSWAP16) + +#define pg_bswap16(x) __builtin_bswap16(x) + +#elif defined(_MSC_VER) + +#define pg_bswap16(x) _byteswap_ushort(x) + #else -#define BSWAP32(x) ((((x) << 24) & 0xff000000) | \ - (((x) << 8) & 0x00ff0000) | \ - (((x) >> 8) & 0x0000ff00) | \ - (((x) >> 24) & 0x000000ff)) + +static inline uint16 +pg_bswap16(uint16 x) +{ + return + ((x << 8) & 0xff00) | + ((x >> 8) & 0x00ff); +} + +#endif /* HAVE__BUILTIN_BSWAP16 */ + + +/* implementation of uint32 pg_bswap32(uint32) */ +#if defined(HAVE__BUILTIN_BSWAP32) + +#define pg_bswap32(x) __builtin_bswap32(x) + +#elif defined(_MSC_VER) + +#define pg_bswap32(x) _byteswap_ulong(x) + +#else + +static inline uint32 +pg_bswap32(uint32 x) +{ + return + ((x << 24) & 0xff000000) | + ((x << 8) & 0x00ff0000) | + ((x >> 8) & 0x0000ff00) | + ((x >> 24) & 0x000000ff); +} + #endif /* HAVE__BUILTIN_BSWAP32 */ -#ifdef HAVE__BUILTIN_BSWAP64 -#define BSWAP64(x) __builtin_bswap64(x) + +/* implementation of uint64 pg_bswap64(uint64) */ +#if defined(HAVE__BUILTIN_BSWAP64) + +#define pg_bswap64(x) __builtin_bswap64(x) + + +#elif defined(_MSC_VER) + +#define pg_bswap64(x) _byteswap_uint64(x) + #else -#define BSWAP64(x) ((((x) << 56) & UINT64CONST(0xff00000000000000)) | \ - (((x) << 40) & UINT64CONST(0x00ff000000000000)) | \ - (((x) << 24) & UINT64CONST(0x0000ff0000000000)) | \ - (((x) << 8) & UINT64CONST(0x000000ff00000000)) | \ - (((x) >> 8) & UINT64CONST(0x00000000ff000000)) | \ - (((x) >> 24) & UINT64CONST(0x0000000000ff0000)) | \ - (((x) >> 40) & UINT64CONST(0x000000000000ff00)) | \ - (((x) >> 56) & UINT64CONST(0x00000000000000ff))) + +static inline uint64 +pg_bswap64(uint64 x) +{ + return + ((x << 56) & UINT64CONST(0xff00000000000000)) | + ((x << 40) & UINT64CONST(0x00ff000000000000)) | + ((x << 24) & UINT64CONST(0x0000ff0000000000)) | + ((x << 8) & UINT64CONST(0x000000ff00000000)) | + ((x >> 8) & UINT64CONST(0x00000000ff000000)) | + ((x >> 24) & UINT64CONST(0x0000000000ff0000)) | + ((x >> 40) & UINT64CONST(0x000000000000ff00)) | + ((x >> 56) & UINT64CONST(0x00000000000000ff)); +} #endif /* HAVE__BUILTIN_BSWAP64 */ + +/* + * Portable and fast equivalents for for ntohs, ntohl, htons, htonl, + * additionally extended to 64 bits. + */ +#ifdef WORDS_BIGENDIAN + +#define pg_hton16(x) (x) +#define pg_hton32(x) (x) +#define pg_hton64(x) (x) + +#define pg_ntoh16(x) (x) +#define pg_ntoh32(x) (x) +#define pg_ntoh64(x) (x) + +#else + +#define pg_hton16(x) pg_bswap16(x) +#define pg_hton32(x) pg_bswap32(x) +#define pg_hton64(x) pg_bswap64(x) + +#define pg_ntoh16(x) pg_bswap16(x) +#define pg_ntoh32(x) pg_bswap32(x) +#define pg_ntoh64(x) pg_bswap64(x) + +#endif /* WORDS_BIGENDIAN */ + + /* * Rearrange the bytes of a Datum from big-endian order into the native byte * order. On big-endian machines, this does nothing at all. Note that the C @@ -60,9 +144,9 @@ #define DatumBigEndianToNative(x) (x) #else /* !WORDS_BIGENDIAN */ #if SIZEOF_DATUM == 8 -#define DatumBigEndianToNative(x) BSWAP64(x) +#define DatumBigEndianToNative(x) pg_bswap64(x) #else /* SIZEOF_DATUM != 8 */ -#define DatumBigEndianToNative(x) BSWAP32(x) +#define DatumBigEndianToNative(x) pg_bswap32(x) #endif /* SIZEOF_DATUM == 8 */ #endif /* WORDS_BIGENDIAN */ diff --git a/src/include/port/pg_crc32c.h b/src/include/port/pg_crc32c.h index cd58ecc988adf..32d71762731c3 100644 --- a/src/include/port/pg_crc32c.h +++ b/src/include/port/pg_crc32c.h @@ -73,7 +73,7 @@ extern pg_crc32c (*pg_comp_crc32c) (pg_crc32c crc, const void *data, size_t len) #define COMP_CRC32C(crc, data, len) \ ((crc) = pg_comp_crc32c_sb8((crc), (data), (len))) #ifdef WORDS_BIGENDIAN -#define FIN_CRC32C(crc) ((crc) = BSWAP32(crc) ^ 0xFFFFFFFF) +#define FIN_CRC32C(crc) ((crc) = pg_bswap32(crc) ^ 0xFFFFFFFF) #else #define FIN_CRC32C(crc) ((crc) ^= 0xFFFFFFFF) #endif diff --git a/src/include/postmaster/bgworker.h b/src/include/postmaster/bgworker.h index e2ecd3c9eb256..6b4e631880d15 100644 --- a/src/include/postmaster/bgworker.h +++ b/src/include/postmaster/bgworker.h @@ -88,6 +88,7 @@ typedef enum typedef struct BackgroundWorker { char bgw_name[BGW_MAXLEN]; + char bgw_type[BGW_MAXLEN]; int bgw_flags; BgWorkerStartTime bgw_start_time; int bgw_restart_time; /* in seconds, or BGW_NEVER_RESTART */ @@ -122,6 +123,7 @@ extern BgwHandleStatus GetBackgroundWorkerPid(BackgroundWorkerHandle *handle, extern BgwHandleStatus WaitForBackgroundWorkerStartup(BackgroundWorkerHandle *handle, pid_t *pid); extern BgwHandleStatus WaitForBackgroundWorkerShutdown(BackgroundWorkerHandle *); +extern const char *GetBackgroundWorkerTypeByPid(pid_t pid); /* Terminate a bgworker */ extern void TerminateBackgroundWorker(BackgroundWorkerHandle *handle); diff --git a/src/include/utils/array.h b/src/include/utils/array.h index d6d3c582b6a86..cc19879a9a733 100644 --- a/src/include/utils/array.h +++ b/src/include/utils/array.h @@ -64,6 +64,10 @@ #include "fmgr.h" #include "utils/expandeddatum.h" +/* avoid including execnodes.h here */ +struct ExprState; +struct ExprContext; + /* * Arrays are varlena objects, so must meet the varlena convention that @@ -360,8 +364,9 @@ extern ArrayType *array_set(ArrayType *array, int nSubscripts, int *indx, Datum dataValue, bool isNull, int arraytyplen, int elmlen, bool elmbyval, char elmalign); -extern Datum array_map(FunctionCallInfo fcinfo, Oid retType, - ArrayMapState *amstate); +extern Datum array_map(Datum arrayd, + struct ExprState *exprstate, struct ExprContext *econtext, + Oid retType, ArrayMapState *amstate); extern void array_bitmap_copy(bits8 *destbitmap, int destoffset, const bits8 *srcbitmap, int srcoffset, diff --git a/src/include/utils/hashutils.h b/src/include/utils/hashutils.h index 35281689e8244..366bd0e78bd95 100644 --- a/src/include/utils/hashutils.h +++ b/src/include/utils/hashutils.h @@ -22,7 +22,7 @@ hash_combine(uint32 a, uint32 b) /* - * Simple inline murmur hash implementation hashing a 32 bit ingeger, for + * Simple inline murmur hash implementation hashing a 32 bit integer, for * performance. */ static inline uint32 diff --git a/src/interfaces/libpq/fe-connect.c b/src/interfaces/libpq/fe-connect.c index c580d91135aad..5f79803607156 100644 --- a/src/interfaces/libpq/fe-connect.c +++ b/src/interfaces/libpq/fe-connect.c @@ -47,7 +47,6 @@ #ifdef HAVE_NETINET_TCP_H #include #endif -#include #endif #ifdef ENABLE_THREAD_SAFETY @@ -73,6 +72,7 @@ static int ldapServiceLookup(const char *purl, PQconninfoOption *options, #include "common/ip.h" #include "mb/pg_wchar.h" +#include "port/pg_bswap.h" #ifndef WIN32 @@ -2443,7 +2443,7 @@ PQconnectPoll(PGconn *conn) * shouldn't since we only got here if the socket is * write-ready. */ - pv = htonl(NEGOTIATE_SSL_CODE); + pv = pg_hton32(NEGOTIATE_SSL_CODE); if (pqPacketSend(conn, 0, &pv, sizeof(pv)) != STATUS_OK) { appendPQExpBuffer(&conn->errorMessage, @@ -3838,10 +3838,10 @@ internal_cancel(SockAddr *raddr, int be_pid, int be_key, /* Create and send the cancel request packet. */ - crp.packetlen = htonl((uint32) sizeof(crp)); - crp.cp.cancelRequestCode = (MsgType) htonl(CANCEL_REQUEST_CODE); - crp.cp.backendPID = htonl(be_pid); - crp.cp.cancelAuthCode = htonl(be_key); + crp.packetlen = pg_hton32((uint32) sizeof(crp)); + crp.cp.cancelRequestCode = (MsgType) pg_hton32(CANCEL_REQUEST_CODE); + crp.cp.backendPID = pg_hton32(be_pid); + crp.cp.cancelAuthCode = pg_hton32(be_key); retry4: if (send(tmpsock, (char *) &crp, sizeof(crp), 0) != (int) sizeof(crp)) diff --git a/src/interfaces/libpq/fe-lobj.c b/src/interfaces/libpq/fe-lobj.c index 343e5303d9980..2ff5559233a00 100644 --- a/src/interfaces/libpq/fe-lobj.c +++ b/src/interfaces/libpq/fe-lobj.c @@ -33,12 +33,11 @@ #include #include #include -#include /* for ntohl/htonl */ -#include #include "libpq-fe.h" #include "libpq-int.h" #include "libpq/libpq-fs.h" /* must come after sys/stat.h */ +#include "port/pg_bswap.h" #define LO_BUFSIZE 8192 @@ -1070,11 +1069,11 @@ lo_hton64(pg_int64 host64) /* High order half first, since we're doing MSB-first */ t = (uint32) (host64 >> 32); - swap.i32[0] = htonl(t); + swap.i32[0] = pg_hton32(t); /* Now the low order half */ t = (uint32) host64; - swap.i32[1] = htonl(t); + swap.i32[1] = pg_hton32(t); return swap.i64; } @@ -1095,9 +1094,9 @@ lo_ntoh64(pg_int64 net64) swap.i64 = net64; - result = (uint32) ntohl(swap.i32[0]); + result = (uint32) pg_ntoh32(swap.i32[0]); result <<= 32; - result |= (uint32) ntohl(swap.i32[1]); + result |= (uint32) pg_ntoh32(swap.i32[1]); return result; } diff --git a/src/interfaces/libpq/fe-misc.c b/src/interfaces/libpq/fe-misc.c index cac6359585ee3..41b1749d07456 100644 --- a/src/interfaces/libpq/fe-misc.c +++ b/src/interfaces/libpq/fe-misc.c @@ -33,9 +33,6 @@ #include #include -#include -#include - #ifdef WIN32 #include "win32.h" #else @@ -53,6 +50,7 @@ #include "libpq-fe.h" #include "libpq-int.h" #include "mb/pg_wchar.h" +#include "port/pg_bswap.h" #include "pg_config_paths.h" @@ -278,14 +276,14 @@ pqGetInt(int *result, size_t bytes, PGconn *conn) return EOF; memcpy(&tmp2, conn->inBuffer + conn->inCursor, 2); conn->inCursor += 2; - *result = (int) ntohs(tmp2); + *result = (int) pg_ntoh16(tmp2); break; case 4: if (conn->inCursor + 4 > conn->inEnd) return EOF; memcpy(&tmp4, conn->inBuffer + conn->inCursor, 4); conn->inCursor += 4; - *result = (int) ntohl(tmp4); + *result = (int) pg_ntoh32(tmp4); break; default: pqInternalNotice(&conn->noticeHooks, @@ -314,12 +312,12 @@ pqPutInt(int value, size_t bytes, PGconn *conn) switch (bytes) { case 2: - tmp2 = htons((uint16) value); + tmp2 = pg_hton16((uint16) value); if (pqPutMsgBytes((const char *) &tmp2, 2, conn)) return EOF; break; case 4: - tmp4 = htonl((uint32) value); + tmp4 = pg_hton32((uint32) value); if (pqPutMsgBytes((const char *) &tmp4, 4, conn)) return EOF; break; @@ -597,7 +595,7 @@ pqPutMsgEnd(PGconn *conn) { uint32 msgLen = conn->outMsgEnd - conn->outMsgStart; - msgLen = htonl(msgLen); + msgLen = pg_hton32(msgLen); memcpy(conn->outBuffer + conn->outMsgStart, &msgLen, 4); } diff --git a/src/interfaces/libpq/fe-protocol2.c b/src/interfaces/libpq/fe-protocol2.c index 83f74f3985244..1320d18a9967c 100644 --- a/src/interfaces/libpq/fe-protocol2.c +++ b/src/interfaces/libpq/fe-protocol2.c @@ -19,17 +19,16 @@ #include "libpq-fe.h" #include "libpq-int.h" +#include "port/pg_bswap.h" #ifdef WIN32 #include "win32.h" #else #include -#include #ifdef HAVE_NETINET_TCP_H #include #endif -#include #endif @@ -1609,7 +1608,7 @@ pqBuildStartupPacket2(PGconn *conn, int *packetlen, MemSet(startpacket, 0, sizeof(StartupPacket)); - startpacket->protoVersion = htonl(conn->pversion); + startpacket->protoVersion = pg_hton32(conn->pversion); /* strncpy is safe here: postmaster will handle full fields correctly */ strncpy(startpacket->user, conn->pguser, SM_USER); diff --git a/src/interfaces/libpq/fe-protocol3.c b/src/interfaces/libpq/fe-protocol3.c index 7da5fb28fb2f0..21fb8f2f213c5 100644 --- a/src/interfaces/libpq/fe-protocol3.c +++ b/src/interfaces/libpq/fe-protocol3.c @@ -21,16 +21,15 @@ #include "libpq-int.h" #include "mb/pg_wchar.h" +#include "port/pg_bswap.h" #ifdef WIN32 #include "win32.h" #else #include -#include #ifdef HAVE_NETINET_TCP_H #include #endif -#include #endif @@ -2148,7 +2147,7 @@ build_startup_packet(const PGconn *conn, char *packet, /* Protocol version comes first. */ if (packet) { - ProtocolVersion pv = htonl(conn->pversion); + ProtocolVersion pv = pg_hton32(conn->pversion); memcpy(packet + packet_len, &pv, sizeof(ProtocolVersion)); } diff --git a/src/pl/plpython/plpy_cursorobject.c b/src/pl/plpython/plpy_cursorobject.c index 2ad663cf6618c..0108471bfe218 100644 --- a/src/pl/plpython/plpy_cursorobject.c +++ b/src/pl/plpython/plpy_cursorobject.c @@ -509,6 +509,5 @@ PLy_cursor_close(PyObject *self, PyObject *unused) cursor->closed = true; } - Py_INCREF(Py_None); - return Py_None; + Py_RETURN_NONE; } diff --git a/src/pl/plpython/plpy_plpymodule.c b/src/pl/plpython/plpy_plpymodule.c index feaf203256086..759ad449323ca 100644 --- a/src/pl/plpython/plpy_plpymodule.c +++ b/src/pl/plpython/plpy_plpymodule.c @@ -575,6 +575,5 @@ PLy_output(volatile int level, PyObject *self, PyObject *args, PyObject *kw) /* * return a legal object so the interpreter will continue on its merry way */ - Py_INCREF(Py_None); - return Py_None; + Py_RETURN_NONE; } diff --git a/src/pl/plpython/plpy_subxactobject.c b/src/pl/plpython/plpy_subxactobject.c index 9f1caa87d9411..331d2b859c0ee 100644 --- a/src/pl/plpython/plpy_subxactobject.c +++ b/src/pl/plpython/plpy_subxactobject.c @@ -212,6 +212,5 @@ PLy_subtransaction_exit(PyObject *self, PyObject *args) CurrentResourceOwner = subxactdata->oldowner; pfree(subxactdata); - Py_INCREF(Py_None); - return Py_None; + Py_RETURN_NONE; } diff --git a/src/port/getaddrinfo.c b/src/port/getaddrinfo.c index e5b5702c79975..dbad0293e80bb 100644 --- a/src/port/getaddrinfo.c +++ b/src/port/getaddrinfo.c @@ -31,6 +31,7 @@ #include "getaddrinfo.h" #include "libpq/pqcomm.h" /* needed for struct sockaddr_storage */ +#include "port/pg_bswap.h" #ifdef WIN32 @@ -178,7 +179,7 @@ getaddrinfo(const char *node, const char *service, if (node) { if (node[0] == '\0') - sin.sin_addr.s_addr = htonl(INADDR_ANY); + sin.sin_addr.s_addr = pg_hton32(INADDR_ANY); else if (hints.ai_flags & AI_NUMERICHOST) { if (!inet_aton(node, &sin.sin_addr)) @@ -221,13 +222,13 @@ getaddrinfo(const char *node, const char *service, else { if (hints.ai_flags & AI_PASSIVE) - sin.sin_addr.s_addr = htonl(INADDR_ANY); + sin.sin_addr.s_addr = pg_hton32(INADDR_ANY); else - sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK); + sin.sin_addr.s_addr = pg_hton32(INADDR_LOOPBACK); } if (service) - sin.sin_port = htons((unsigned short) atoi(service)); + sin.sin_port = pg_hton16((unsigned short) atoi(service)); #ifdef HAVE_STRUCT_SOCKADDR_STORAGE_SS_LEN sin.sin_len = sizeof(sin); @@ -402,7 +403,7 @@ getnameinfo(const struct sockaddr *sa, int salen, if (sa->sa_family == AF_INET) { ret = snprintf(service, servicelen, "%d", - ntohs(((struct sockaddr_in *) sa)->sin_port)); + pg_ntoh16(((struct sockaddr_in *) sa)->sin_port)); } if (ret == -1 || ret >= servicelen) return EAI_MEMORY; diff --git a/src/port/inet_aton.c b/src/port/inet_aton.c index 68efd4723ec25..adaf18adb3929 100644 --- a/src/port/inet_aton.c +++ b/src/port/inet_aton.c @@ -43,6 +43,8 @@ #include #include +#include "port/pg_bswap.h" + /* * Check whether "cp" is a valid ascii representation * of an Internet address and convert to a binary address. @@ -142,6 +144,6 @@ inet_aton(const char *cp, struct in_addr *addr) break; } if (addr) - addr->s_addr = htonl(val); + addr->s_addr = pg_hton32(val); return 1; } diff --git a/src/test/modules/test_shm_mq/setup.c b/src/test/modules/test_shm_mq/setup.c index 3ae901836053f..561f6f9bac3d0 100644 --- a/src/test/modules/test_shm_mq/setup.c +++ b/src/test/modules/test_shm_mq/setup.c @@ -219,7 +219,7 @@ setup_background_workers(int nworkers, dsm_segment *seg) worker.bgw_restart_time = BGW_NEVER_RESTART; sprintf(worker.bgw_library_name, "test_shm_mq"); sprintf(worker.bgw_function_name, "test_shm_mq_main"); - snprintf(worker.bgw_name, BGW_MAXLEN, "test_shm_mq"); + snprintf(worker.bgw_type, BGW_MAXLEN, "test_shm_mq"); worker.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(seg)); /* set bgw_notify_pid, so we can detect if the worker stops */ worker.bgw_notify_pid = MyProcPid; diff --git a/src/test/modules/worker_spi/worker_spi.c b/src/test/modules/worker_spi/worker_spi.c index 12c8cd5774c75..4c6ab6d575d07 100644 --- a/src/test/modules/worker_spi/worker_spi.c +++ b/src/test/modules/worker_spi/worker_spi.c @@ -111,7 +111,7 @@ initialize_worker_spi(worktable *table) StartTransactionCommand(); SPI_connect(); PushActiveSnapshot(GetTransactionSnapshot()); - pgstat_report_activity(STATE_RUNNING, "initializing spi_worker schema"); + pgstat_report_activity(STATE_RUNNING, "initializing worker_spi schema"); /* XXX could we use CREATE SCHEMA IF NOT EXISTS? */ initStringInfo(&buf); @@ -359,7 +359,8 @@ _PG_init(void) */ for (i = 1; i <= worker_spi_total_workers; i++) { - snprintf(worker.bgw_name, BGW_MAXLEN, "worker %d", i); + snprintf(worker.bgw_name, BGW_MAXLEN, "worker_spi worker %d", i); + snprintf(worker.bgw_type, BGW_MAXLEN, "worker_spi"); worker.bgw_main_arg = Int32GetDatum(i); RegisterBackgroundWorker(&worker); @@ -385,7 +386,8 @@ worker_spi_launch(PG_FUNCTION_ARGS) worker.bgw_restart_time = BGW_NEVER_RESTART; sprintf(worker.bgw_library_name, "worker_spi"); sprintf(worker.bgw_function_name, "worker_spi_main"); - snprintf(worker.bgw_name, BGW_MAXLEN, "worker %d", i); + snprintf(worker.bgw_name, BGW_MAXLEN, "worker_spi worker %d", i); + snprintf(worker.bgw_type, BGW_MAXLEN, "worker_spi"); worker.bgw_main_arg = Int32GetDatum(i); /* set bgw_notify_pid so that we can use WaitForBackgroundWorkerStartup */ worker.bgw_notify_pid = MyProcPid; diff --git a/src/test/recovery/t/013_crash_restart.pl b/src/test/recovery/t/013_crash_restart.pl index ca02054ff0c79..91a8ef90c164f 100644 --- a/src/test/recovery/t/013_crash_restart.pl +++ b/src/test/recovery/t/013_crash_restart.pl @@ -16,16 +16,8 @@ use Config; use Time::HiRes qw(usleep); -if ($Config{osname} eq 'MSWin32') -{ - # some Windows Perls at least don't like IPC::Run's - # start/kill_kill regime. - plan skip_all => "Test fails on Windows perl"; -} -else -{ - plan tests => 18; -} +plan tests => 18; + # To avoid hanging while expecting some specific input from a psql # instance being driven by us, add a timeout high enough that it @@ -106,10 +98,10 @@ $monitor_stderr = ''; # kill once with QUIT - we expect psql to exit, while emitting error message first -my $cnt = kill 'QUIT', $pid; +my $ret = TestLib::system_log('pg_ctl', 'kill', 'QUIT', $pid); # Exactly process should have been alive to be killed -is($cnt, 1, "exactly one process killed with SIGQUIT"); +is($ret, 0, "killed process with SIGQUIT"); # Check that psql sees the killed backend as having been terminated $killme_stdin .= q[ @@ -119,14 +111,14 @@ "psql query died successfully after SIGQUIT"); $killme_stderr = ''; $killme_stdout = ''; -$killme->kill_kill; +$killme->finish; # Wait till server restarts - we should get the WARNING here, but # sometimes the server is unable to send that, if interrupted while # sending. ok(pump_until($monitor, \$monitor_stderr, qr/WARNING: terminating connection because of crash of another server process|server closed the connection unexpectedly/m), "psql monitor died successfully after SIGQUIT"); -$monitor->kill_kill; +$monitor->finish; # Wait till server restarts is($node->poll_query_until('postgres', 'SELECT $$restarted after sigquit$$;', 'restarted after sigquit'), @@ -179,8 +171,8 @@ # kill with SIGKILL this time - we expect the backend to exit, without # being able to emit an error error message -$cnt = kill 'KILL', $pid; -is($cnt, 1, "exactly one process killed with KILL"); +$ret = TestLib::system_log('pg_ctl', 'kill', 'KILL', $pid); +is($ret, 0, "killed process with KILL"); # Check that psql sees the server as being terminated. No WARNING, # because signal handlers aren't being run on SIGKILL. @@ -189,14 +181,14 @@ ]; ok(pump_until($killme, \$killme_stderr, qr/server closed the connection unexpectedly/m), "psql query died successfully after SIGKILL"); -$killme->kill_kill; +$killme->finish; # Wait till server restarts - we should get the WARNING here, but # sometimes the server is unable to send that, if interrupted while # sending. ok(pump_until($monitor, \$monitor_stderr, qr/WARNING: terminating connection because of crash of another server process|server closed the connection unexpectedly/m), "psql monitor died successfully after SIGKILL"); -$monitor->kill_kill; +$monitor->finish; # Wait till server restarts is($node->poll_query_until('postgres', 'SELECT 1', '1'), "1", "reconnected after SIGKILL"); diff --git a/src/test/regress/expected/cluster.out b/src/test/regress/expected/cluster.out index 097ac2b006068..82713bfa2c706 100644 --- a/src/test/regress/expected/cluster.out +++ b/src/test/regress/expected/cluster.out @@ -444,22 +444,8 @@ create table clstr_4 as select * from tenk1; create index cluster_sort on clstr_4 (hundred, thousand, tenthous); -- ensure we don't use the index in CLUSTER nor the checking SELECTs set enable_indexscan = off; --- Use external sort that only ever uses quicksort to sort runs: +-- Use external sort: set maintenance_work_mem = '1MB'; -set replacement_sort_tuples = 0; -cluster clstr_4 using cluster_sort; -select * from -(select hundred, lag(hundred) over () as lhundred, - thousand, lag(thousand) over () as lthousand, - tenthous, lag(tenthous) over () as ltenthous from clstr_4) ss -where row(hundred, thousand, tenthous) <= row(lhundred, lthousand, ltenthous); - hundred | lhundred | thousand | lthousand | tenthous | ltenthous ----------+----------+----------+-----------+----------+----------- -(0 rows) - --- Replacement selection will now be forced. It should only produce a single --- run, due to the fact that input is found to be presorted: -set replacement_sort_tuples = 150000; cluster clstr_4 using cluster_sort; select * from (select hundred, lag(hundred) over () as lhundred, @@ -472,7 +458,6 @@ where row(hundred, thousand, tenthous) <= row(lhundred, lthousand, ltenthous); reset enable_indexscan; reset maintenance_work_mem; -reset replacement_sort_tuples; -- clean up DROP TABLE clustertest; DROP TABLE clstr_1; diff --git a/src/test/regress/expected/domain.out b/src/test/regress/expected/domain.out index 3acc696863d17..1e62c57a68863 100644 --- a/src/test/regress/expected/domain.out +++ b/src/test/regress/expected/domain.out @@ -310,6 +310,101 @@ Rules: drop table dcomptable; drop type comptype cascade; NOTICE: drop cascades to type dcomptypea +-- Test arrays over domains +create domain posint as int check (value > 0); +create table pitable (f1 posint[]); +insert into pitable values(array[42]); +insert into pitable values(array[-1]); -- fail +ERROR: value for domain posint violates check constraint "posint_check" +insert into pitable values('{0}'); -- fail +ERROR: value for domain posint violates check constraint "posint_check" +LINE 1: insert into pitable values('{0}'); + ^ +update pitable set f1[1] = f1[1] + 1; +update pitable set f1[1] = 0; -- fail +ERROR: value for domain posint violates check constraint "posint_check" +select * from pitable; + f1 +------ + {43} +(1 row) + +drop table pitable; +create domain vc4 as varchar(4); +create table vc4table (f1 vc4[]); +insert into vc4table values(array['too long']); -- fail +ERROR: value too long for type character varying(4) +insert into vc4table values(array['too long']::vc4[]); -- cast truncates +select * from vc4table; + f1 +---------- + {"too "} +(1 row) + +drop table vc4table; +drop type vc4; +-- You can sort of fake arrays-of-arrays by putting a domain in between +create domain dposinta as posint[]; +create table dposintatable (f1 dposinta[]); +insert into dposintatable values(array[array[42]]); -- fail +ERROR: column "f1" is of type dposinta[] but expression is of type integer[] +LINE 1: insert into dposintatable values(array[array[42]]); + ^ +HINT: You will need to rewrite or cast the expression. +insert into dposintatable values(array[array[42]::posint[]]); -- still fail +ERROR: column "f1" is of type dposinta[] but expression is of type posint[] +LINE 1: insert into dposintatable values(array[array[42]::posint[]])... + ^ +HINT: You will need to rewrite or cast the expression. +insert into dposintatable values(array[array[42]::dposinta]); -- but this works +select f1, f1[1], (f1[1])[1] from dposintatable; + f1 | f1 | f1 +----------+------+---- + {"{42}"} | {42} | 42 +(1 row) + +select pg_typeof(f1) from dposintatable; + pg_typeof +------------ + dposinta[] +(1 row) + +select pg_typeof(f1[1]) from dposintatable; + pg_typeof +----------- + dposinta +(1 row) + +select pg_typeof(f1[1][1]) from dposintatable; + pg_typeof +----------- + dposinta +(1 row) + +select pg_typeof((f1[1])[1]) from dposintatable; + pg_typeof +----------- + posint +(1 row) + +update dposintatable set f1[2] = array[99]; +select f1, f1[1], (f1[2])[1] from dposintatable; + f1 | f1 | f1 +-----------------+------+---- + {"{42}","{99}"} | {42} | 99 +(1 row) + +-- it'd be nice if you could do something like this, but for now you can't: +update dposintatable set f1[2][1] = array[97]; +ERROR: wrong number of array subscripts +-- maybe someday we can make this syntax work: +update dposintatable set (f1[2])[1] = array[98]; +ERROR: syntax error at or near "[" +LINE 1: update dposintatable set (f1[2])[1] = array[98]; + ^ +drop table dposintatable; +drop domain posint cascade; +NOTICE: drop cascades to type dposinta -- Test not-null restrictions create domain dnotnull varchar(15) NOT NULL; create domain dnull varchar(15); diff --git a/src/test/regress/expected/identity.out b/src/test/regress/expected/identity.out index 2800ed7caabc3..5fa585d6cc55b 100644 --- a/src/test/regress/expected/identity.out +++ b/src/test/regress/expected/identity.out @@ -32,6 +32,13 @@ SELECT pg_get_serial_sequence('itest1', 'a'); public.itest1_a_seq (1 row) +\d itest1_a_seq + Sequence "public.itest1_a_seq" + Type | Start | Minimum | Maximum | Increment | Cycles? | Cache +---------+-------+---------+------------+-----------+---------+------- + integer | 1 | 1 | 2147483647 | 1 | no | 1 +Sequence for identity column: public.itest1.a + CREATE TABLE itest4 (a int, b text); ALTER TABLE itest4 ALTER COLUMN a ADD GENERATED ALWAYS AS IDENTITY; -- error, requires NOT NULL ERROR: column "a" of relation "itest4" must be declared NOT NULL before identity can be added diff --git a/src/test/regress/expected/sequence.out b/src/test/regress/expected/sequence.out index ea05a3382b876..2384b7dd81594 100644 --- a/src/test/regress/expected/sequence.out +++ b/src/test/regress/expected/sequence.out @@ -535,6 +535,19 @@ SELECT * FROM pg_sequence_parameters('sequence_test4'::regclass); -1 | -9223372036854775808 | -1 | -1 | f | 1 | 20 (1 row) +\d sequence_test4 + Sequence "public.sequence_test4" + Type | Start | Minimum | Maximum | Increment | Cycles? | Cache +--------+-------+----------------------+---------+-----------+---------+------- + bigint | -1 | -9223372036854775808 | -1 | -1 | no | 1 + +\d serialtest2_f2_seq + Sequence "public.serialtest2_f2_seq" + Type | Start | Minimum | Maximum | Increment | Cycles? | Cache +---------+-------+---------+------------+-----------+---------+------- + integer | 1 | 1 | 2147483647 | 1 | no | 1 +Owned by: public.serialtest2.f2 + -- Test comments COMMENT ON SEQUENCE asdf IS 'won''t work'; ERROR: relation "asdf" does not exist diff --git a/src/test/regress/sql/cluster.sql b/src/test/regress/sql/cluster.sql index 8dd9459bda00c..a6c2757efaaac 100644 --- a/src/test/regress/sql/cluster.sql +++ b/src/test/regress/sql/cluster.sql @@ -203,19 +203,8 @@ create index cluster_sort on clstr_4 (hundred, thousand, tenthous); -- ensure we don't use the index in CLUSTER nor the checking SELECTs set enable_indexscan = off; --- Use external sort that only ever uses quicksort to sort runs: +-- Use external sort: set maintenance_work_mem = '1MB'; -set replacement_sort_tuples = 0; -cluster clstr_4 using cluster_sort; -select * from -(select hundred, lag(hundred) over () as lhundred, - thousand, lag(thousand) over () as lthousand, - tenthous, lag(tenthous) over () as ltenthous from clstr_4) ss -where row(hundred, thousand, tenthous) <= row(lhundred, lthousand, ltenthous); - --- Replacement selection will now be forced. It should only produce a single --- run, due to the fact that input is found to be presorted: -set replacement_sort_tuples = 150000; cluster clstr_4 using cluster_sort; select * from (select hundred, lag(hundred) over () as lhundred, @@ -225,7 +214,6 @@ where row(hundred, thousand, tenthous) <= row(lhundred, lthousand, ltenthous); reset enable_indexscan; reset maintenance_work_mem; -reset replacement_sort_tuples; -- clean up DROP TABLE clustertest; diff --git a/src/test/regress/sql/domain.sql b/src/test/regress/sql/domain.sql index 0fd383e2721a6..8fb3e2086a12d 100644 --- a/src/test/regress/sql/domain.sql +++ b/src/test/regress/sql/domain.sql @@ -166,6 +166,49 @@ drop table dcomptable; drop type comptype cascade; +-- Test arrays over domains + +create domain posint as int check (value > 0); + +create table pitable (f1 posint[]); +insert into pitable values(array[42]); +insert into pitable values(array[-1]); -- fail +insert into pitable values('{0}'); -- fail +update pitable set f1[1] = f1[1] + 1; +update pitable set f1[1] = 0; -- fail +select * from pitable; +drop table pitable; + +create domain vc4 as varchar(4); +create table vc4table (f1 vc4[]); +insert into vc4table values(array['too long']); -- fail +insert into vc4table values(array['too long']::vc4[]); -- cast truncates +select * from vc4table; +drop table vc4table; +drop type vc4; + +-- You can sort of fake arrays-of-arrays by putting a domain in between +create domain dposinta as posint[]; +create table dposintatable (f1 dposinta[]); +insert into dposintatable values(array[array[42]]); -- fail +insert into dposintatable values(array[array[42]::posint[]]); -- still fail +insert into dposintatable values(array[array[42]::dposinta]); -- but this works +select f1, f1[1], (f1[1])[1] from dposintatable; +select pg_typeof(f1) from dposintatable; +select pg_typeof(f1[1]) from dposintatable; +select pg_typeof(f1[1][1]) from dposintatable; +select pg_typeof((f1[1])[1]) from dposintatable; +update dposintatable set f1[2] = array[99]; +select f1, f1[1], (f1[2])[1] from dposintatable; +-- it'd be nice if you could do something like this, but for now you can't: +update dposintatable set f1[2][1] = array[97]; +-- maybe someday we can make this syntax work: +update dposintatable set (f1[2])[1] = array[98]; + +drop table dposintatable; +drop domain posint cascade; + + -- Test not-null restrictions create domain dnotnull varchar(15) NOT NULL; diff --git a/src/test/regress/sql/identity.sql b/src/test/regress/sql/identity.sql index 7886456a56974..e1b5a074c9610 100644 --- a/src/test/regress/sql/identity.sql +++ b/src/test/regress/sql/identity.sql @@ -14,6 +14,8 @@ SELECT sequence_name FROM information_schema.sequences WHERE sequence_name LIKE SELECT pg_get_serial_sequence('itest1', 'a'); +\d itest1_a_seq + CREATE TABLE itest4 (a int, b text); ALTER TABLE itest4 ALTER COLUMN a ADD GENERATED ALWAYS AS IDENTITY; -- error, requires NOT NULL ALTER TABLE itest4 ALTER COLUMN a SET NOT NULL; diff --git a/src/test/regress/sql/sequence.sql b/src/test/regress/sql/sequence.sql index c50834a5b97e6..a7b9e633728bd 100644 --- a/src/test/regress/sql/sequence.sql +++ b/src/test/regress/sql/sequence.sql @@ -246,6 +246,10 @@ WHERE sequencename ~ ANY(ARRAY['sequence_test', 'serialtest']) SELECT * FROM pg_sequence_parameters('sequence_test4'::regclass); +\d sequence_test4 +\d serialtest2_f2_seq + + -- Test comments COMMENT ON SEQUENCE asdf IS 'won''t work'; COMMENT ON SEQUENCE sequence_test2 IS 'will work'; From 50ab808e8147463dd5e836c9d62b01fa3833a24f Mon Sep 17 00:00:00 2001 From: Robins Date: Thu, 5 Oct 2017 06:11:02 +1100 Subject: [PATCH 136/149] Updating README with psqlforks.com website --- README | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/README b/README index 9907b69bdb3f7..e12b7c3a27ed5 100644 --- a/README +++ b/README @@ -10,8 +10,9 @@ This directory contains the source code distribution of the PostgreSQL database management system, along with modifications required for PsqlForks. -PsqlForks is a psql variant that tries to accommodate as many PostgrSQL -variants (or software that speak the PostgreSQL protocol to an extent). +PsqlForks ( http://psqlforks.com ) is a psql variant that tries to +accommodate as many PostgrSQL variants (or software that speak the +PostgreSQL protocol to an extent). The current status of support for different PostgreSQL derived databases can be seen here: From 791e7cb775f3f5cd0190a04fe86b53a619bc96b1 Mon Sep 17 00:00:00 2001 From: Robins Tharakan Date: Fri, 6 Oct 2017 07:19:42 +1100 Subject: [PATCH 137/149] We now recognize Amazon RDS PostgreSQL servers separately. This infrastructure is going to be helpful for future commits which would decide whether to show / hide some features --- src/bin/psql/command.c | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/bin/psql/command.c b/src/bin/psql/command.c index 5ecaf1d8b4e91..0d23b1447da0a 100644 --- a/src/bin/psql/command.c +++ b/src/bin/psql/command.c @@ -3272,10 +3272,16 @@ SyncVariables(void) server_engine = "cockroachdb"; else { - if (strstr(server_version, "bouncer") != NULL) - server_engine = "pgbouncer"; + guctype = get_guctype("rds.extensions"); + if (guctype != NULL) + server_engine = "rdspostgres"; else - server_engine = "postgres"; + { + if (strstr(server_version, "bouncer") != NULL) + server_engine = "pgbouncer"; + else + server_engine = "postgres"; + } } } } From bebad5ece77c36ecc2703152872c910d1c22d693 Mon Sep 17 00:00:00 2001 From: Robins Tharakan Date: Sun, 8 Oct 2017 07:44:31 +0000 Subject: [PATCH 138/149] For some reason tab_completion_query_buf was referenced in more places than required --- src/bin/psql/common.c | 4 ++-- src/bin/psql/tab-complete-cockroachdb.c | 8 +------- src/bin/psql/tab-complete-cockroachdb.h | 2 -- src/bin/psql/tab-complete-pipelinedb.c | 1 + src/bin/psql/tab-complete-pipelinedb.h | 2 -- src/bin/psql/tab-complete-redshift.c | 1 + src/bin/psql/tab-complete-redshift.h | 2 -- 7 files changed, 5 insertions(+), 15 deletions(-) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index 4e34b7e2442ad..ba58b28bd47f2 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -141,8 +141,8 @@ request_password_from_external_source(char **username, char **password) strcpy(filebuffer, " "); /* Open the command for reading. */ - //fp = popen("aws redshift get-cluster-credentials --db-user redshift2 --cluster-identifier redshift2", "r"); - fp = popen("/usr/bin/cat /home/tharar/proj/postgres/src/bin/psql/cluster.txt", "r"); + fp = popen("aws redshift get-cluster-credentials --db-user redshift2 --cluster-identifier redshift2", "r"); + //fp = popen("/usr/bin/cat /home/tharar/proj/postgres/src/bin/psql/cluster.txt", "r"); if (fp == NULL) { printf("Failed to run command\n" ); diff --git a/src/bin/psql/tab-complete-cockroachdb.c b/src/bin/psql/tab-complete-cockroachdb.c index 03b6bdc3e23ac..146684238f93a 100644 --- a/src/bin/psql/tab-complete-cockroachdb.c +++ b/src/bin/psql/tab-complete-cockroachdb.c @@ -33,6 +33,7 @@ */ #include "postgres_fe.h" +#include "tab-complete.h" #include "tab-complete-cockroachdb.h" #include "input.h" @@ -63,13 +64,6 @@ extern char *filename_completion_function(); /* word break characters */ #define WORD_BREAKS "\t\n@$><=;|&{() " -/* - * Since readline doesn't let us pass any state through to the tab completion - * callback, we have to use this global variable to let get_previous_words() - * get at the previous lines of the current command. Ick. - */ -PQExpBuffer tab_completion_query_buf = NULL; - /* * This struct is used to define "schema queries", which are custom-built * to obtain possibly-schema-qualified names of database objects. There is diff --git a/src/bin/psql/tab-complete-cockroachdb.h b/src/bin/psql/tab-complete-cockroachdb.h index 5fdcc0f847b87..da86ffcf16282 100644 --- a/src/bin/psql/tab-complete-cockroachdb.h +++ b/src/bin/psql/tab-complete-cockroachdb.h @@ -10,8 +10,6 @@ #include "pqexpbuffer.h" -extern PQExpBuffer tab_completion_query_buf; - extern void initialize_readline_cockroachdb(void); #endif /* TAB_COMPLETE_COCKROACHDB_H */ diff --git a/src/bin/psql/tab-complete-pipelinedb.c b/src/bin/psql/tab-complete-pipelinedb.c index 3312f2230e5b3..a4b0a00a45766 100644 --- a/src/bin/psql/tab-complete-pipelinedb.c +++ b/src/bin/psql/tab-complete-pipelinedb.c @@ -33,6 +33,7 @@ */ #include "postgres_fe.h" +#include "tab-complete.h" #include "tab-complete-pipelinedb.h" #include "input.h" diff --git a/src/bin/psql/tab-complete-pipelinedb.h b/src/bin/psql/tab-complete-pipelinedb.h index af2597892db6c..d8672fb2557d0 100644 --- a/src/bin/psql/tab-complete-pipelinedb.h +++ b/src/bin/psql/tab-complete-pipelinedb.h @@ -10,8 +10,6 @@ #include "pqexpbuffer.h" -extern PQExpBuffer tab_completion_query_buf; - extern void initialize_readline_pipelinedb(void); #endif /* TAB_COMPLETE_PIPELINEDB_H */ diff --git a/src/bin/psql/tab-complete-redshift.c b/src/bin/psql/tab-complete-redshift.c index 409605f52319b..0c2d76b77f204 100644 --- a/src/bin/psql/tab-complete-redshift.c +++ b/src/bin/psql/tab-complete-redshift.c @@ -33,6 +33,7 @@ */ #include "postgres_fe.h" +#include "tab-complete.h" #include "tab-complete-redshift.h" #include "input.h" diff --git a/src/bin/psql/tab-complete-redshift.h b/src/bin/psql/tab-complete-redshift.h index 3d2c7ce2279ca..c1e368ccb0b8b 100644 --- a/src/bin/psql/tab-complete-redshift.h +++ b/src/bin/psql/tab-complete-redshift.h @@ -10,8 +10,6 @@ #include "pqexpbuffer.h" -extern PQExpBuffer tab_completion_query_buf; - extern void initialize_readline_redshift(void); #endif /* TAB_COMPLETE_REDSHIFT_H */ From 1fdd13ff41047192defbdaf23e7507a9cc0f8e63 Mon Sep 17 00:00:00 2001 From: Robins Tharakan Date: Sun, 8 Oct 2017 22:12:32 +1100 Subject: [PATCH 139/149] Some more testing. nowhere close --- src/bin/psql/common.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index ba58b28bd47f2..209c4f38d2a3f 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -141,8 +141,8 @@ request_password_from_external_source(char **username, char **password) strcpy(filebuffer, " "); /* Open the command for reading. */ - fp = popen("aws redshift get-cluster-credentials --db-user redshift2 --cluster-identifier redshift2", "r"); - //fp = popen("/usr/bin/cat /home/tharar/proj/postgres/src/bin/psql/cluster.txt", "r"); + //fp = popen("aws redshift get-cluster-credentials --db-user redshift2 --cluster-identifier redshift2", "r"); + fp = popen("cat /home/tharar/proj/postgres/src/bin/psql/cluster.txt", "r"); if (fp == NULL) { printf("Failed to run command\n" ); From 6a3ea0518ff061e8c9d76651e1cf9b34cfff6c9a Mon Sep 17 00:00:00 2001 From: Robins Tharakan Date: Mon, 9 Oct 2017 07:43:09 +1100 Subject: [PATCH 140/149] working with and without -I for localhost host. Still WIP --- src/bin/psql/startup.c | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/src/bin/psql/startup.c b/src/bin/psql/startup.c index fa5ed46d92a16..8125ea7408b00 100644 --- a/src/bin/psql/startup.c +++ b/src/bin/psql/startup.c @@ -266,20 +266,25 @@ main(int argc, char *argv[]) keywords[7] = NULL; values[7] = NULL; - printf("Username: '%s', Password: '%s', Host: '%s', Port: '%s', PasswordLen: %d, Database: '%s' \n", values[2], values[3], values[0], values[1], strlen(values[3]), values[4]); + // printf("Username: '%s', Password: '%s', Host: '%s', Port: '%s', PasswordLen: %d, Database: '%s' \n", values[2], values[3], values[0], values[1], strlen(values[3]), values[4]); + printf("Username: '%s', Password: '%s', Host: '%s', Port: '%s', Database: '%s' \n", values[2], values[3], values[0], values[1], values[4]); new_pass = false; pset.db = PQconnectdbParams(keywords, values, true); free(keywords); free(values); printf("Exit1\n"); - + if (PQstatus(pset.db) == CONNECTION_BAD) + printf("connection bad\n"); + else + printf("connection not bad\n"); + if (PQstatus(pset.db) == CONNECTION_BAD && PQconnectionNeedsPassword(pset.db) && !have_password && pset.getPassword != TRI_NO) { - printf("n1"); + printf("n1\n"); if (pset.credential_source == AWS_IAM_REDSHIFT) { PQfinish(pset.db); @@ -299,7 +304,7 @@ main(int argc, char *argv[]) } else { - printf("n2"); + printf("n2\n"); PQfinish(pset.db); simple_prompt(password_prompt, password, sizeof(password), false); @@ -307,22 +312,22 @@ main(int argc, char *argv[]) new_pass = true; } } - printf("n3"); + printf("n3\n"); if (new_pass) printf("new_pass"); } while (new_pass); - printf("n4"); + printf("n4\n"); free(password_prompt); if (PQstatus(pset.db) == CONNECTION_BAD) { - printf("n5"); + printf("n5\n"); fprintf(stderr, "%s: %s", pset.progname, PQerrorMessage(pset.db)); PQfinish(pset.db); exit(EXIT_BADCONN); } - printf("n6"); + printf("n6\n"); setup_cancel_handler(); From 453137c05e7a6e01d2eee992a9002f829d28ff71 Mon Sep 17 00:00:00 2001 From: Robins Tharakan Date: Wed, 11 Oct 2017 05:44:36 +1100 Subject: [PATCH 141/149] \ddp support in Redshift required using defacluser column and not the then renamed defaclrole column, which doesn't exist in Redshift. This should resolve Issue #27. --- src/bin/psql/describe.c | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/src/bin/psql/describe.c b/src/bin/psql/describe.c index d99934c3bdddc..a6df5fe701bd6 100644 --- a/src/bin/psql/describe.c +++ b/src/bin/psql/describe.c @@ -1055,7 +1055,7 @@ listDefaultACLs(const char *pattern) printQueryOpt myopt = pset.popt; static const bool translate_columns[] = {false, false, true, false}; - if (pset.sversion < 90000) + if (pset.sversion < 90000 && !IS_REDSHIFT) { char sverbuf[32]; @@ -1067,12 +1067,17 @@ listDefaultACLs(const char *pattern) initPQExpBuffer(&buf); - printfPQExpBuffer(&buf, - "SELECT pg_catalog.pg_get_userbyid(d.defaclrole) AS \"%s\",\n" - " n.nspname AS \"%s\",\n" + if (IS_REDSHIFT) + printfPQExpBuffer(&buf, + "SELECT pg_catalog.pg_get_userbyid(d.defacluser) AS \"%s\",\n", gettext_noop("Owner")); + else + printfPQExpBuffer(&buf, + "SELECT pg_catalog.pg_get_userbyid(d.defaclrole) AS \"%s\",\n", gettext_noop("Owner")); + + appendPQExpBuffer(&buf, + " n.nspname AS \"%s\",\n" " CASE d.defaclobjtype WHEN '%c' THEN '%s' WHEN '%c' THEN '%s' WHEN '%c' THEN '%s' WHEN '%c' THEN '%s' WHEN '%c' THEN '%s' END AS \"%s\",\n" " ", - gettext_noop("Owner"), gettext_noop("Schema"), DEFACLOBJ_RELATION, gettext_noop("table"), From 708264336e4dcb39e656e162d80ff628cc90fc75 Mon Sep 17 00:00:00 2001 From: Robins Tharakan Date: Tue, 10 Oct 2017 20:10:25 +0000 Subject: [PATCH 142/149] the fetch from text file works and logs in successfully. Fetch from the external CLI call also fetches details, but doesn't work yet. Need to investigate why. Mostly there, but not quite :) --- src/bin/psql/common.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index 209c4f38d2a3f..3f2b50aa5faab 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -142,7 +142,7 @@ request_password_from_external_source(char **username, char **password) strcpy(filebuffer, " "); /* Open the command for reading. */ //fp = popen("aws redshift get-cluster-credentials --db-user redshift2 --cluster-identifier redshift2", "r"); - fp = popen("cat /home/tharar/proj/postgres/src/bin/psql/cluster.txt", "r"); + fp = popen("cat /home/pi/projects/postgres/src/bin/psql/cluster.txt", "r"); if (fp == NULL) { printf("Failed to run command\n" ); From d6999381b74c2d9cda7ef167bccffe6147366e4f Mon Sep 17 00:00:00 2001 From: Robins Tharakan Date: Thu, 12 Oct 2017 16:36:27 +1100 Subject: [PATCH 143/149] Removed some logging. IAM authentication is mostly working. Barring Issue #28 and Issue #29 --- src/bin/psql/command.c | 5 -- src/bin/psql/common.c | 60 ++++-------------------- src/bin/psql/leauth.sh | 76 ------------------------------- src/bin/psql/startup.c | 32 +++---------- src/interfaces/libpq/fe-connect.c | 41 +++++------------ 5 files changed, 27 insertions(+), 187 deletions(-) delete mode 100755 src/bin/psql/leauth.sh diff --git a/src/bin/psql/command.c b/src/bin/psql/command.c index d5cf915ff42fa..2f0a8a03f45c8 100644 --- a/src/bin/psql/command.c +++ b/src/bin/psql/command.c @@ -2989,13 +2989,10 @@ do_connect(enum trivalue reuse_previous_specification, { if (pset.credential_source == AWS_IAM_REDSHIFT) { - printf("Entry3"); if (request_password_from_external_source(&user, &new_password)) { - printf("Username: %s, Password: %s, Len: %d \n", user, new_password, strlen(new_password)); sprintf(password, "%s", new_password); password[strlen(new_password)]='\0'; - printf("Username: %s, Password: %s\n", user, password); } else { @@ -3056,8 +3053,6 @@ do_connect(enum trivalue reuse_previous_specification, keywords[++paramnum] = NULL; values[paramnum] = NULL; - printf("3048: Username: %s, Password: %s, PasswordLen: %d, Database: %s \n", user, password, strlen(password), dbname); - n_conn = PQconnectdbParams(keywords, values, true); pg_free(keywords); diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index 3f2b50aa5faab..d5a8eb8e77096 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -35,12 +35,6 @@ static bool ExecQueryUsingCursor(const char *query, double *elapsed_msec); static bool command_no_begin(const char *query); static bool is_select_command(const char *query); - -static const char *JSON_STRING = -//"{\"DbUser\": \"redshift2\", \"DbPassword\": \"Redshift2\", \"Expiration\": \"2017-09-16T11:12:37.608Z\"}"; -"{\"DbUser\": \"IAM:redshift2\", \"DbPassword\": \"BIKXgrJ+KurOerMkFF/IZJyzqcUxjAcqWV2aPJBjkX6DdW8VZDOfI75/FEeeC3zGQQAO6oQ==\", \"Expiration\": \"2017-09-16T11:12:37.608Z\"}"; - - static int jsoneq(const char *json, jsmntok_t *tok, const char *s) { if (tok->type == JSMN_STRING && (int) strlen(s) == tok->end - tok->start && strncmp(json + tok->start, s, tok->end - tok->start) == 0) { @@ -49,7 +43,6 @@ static int jsoneq(const char *json, jsmntok_t *tok, const char *s) { return -1; } - /* * openQueryOutputFile --- attempt to open a query output file * @@ -129,9 +122,8 @@ setQFout(const char *fname) * request_password_from_external_source * * Generalized function to fetch usernames and passwords from external source. - * For now works only in Linux. */ -bool +bool request_password_from_external_source(char **username, char **password) { FILE *fp; @@ -145,35 +137,20 @@ request_password_from_external_source(char **username, char **password) fp = popen("cat /home/pi/projects/postgres/src/bin/psql/cluster.txt", "r"); if (fp == NULL) { - printf("Failed to run command\n" ); + psql_error("Failed to run AWS CLI to fetch IAM authentication: \n"); exit(1); } - else + else { - printf("Able to read file fine\n"); - /* Read the output a line at a time - output it. */ while (fgets(linebuffer, sizeof(linebuffer)-1, fp) != NULL) { strncat(filebuffer, linebuffer, sizeof(linebuffer)); - printf("Filebuffer: %s\n", filebuffer); + //printf("Filebuffer: %s\n", filebuffer); } - // fseek (fp, 0, SEEK_END); - // length = ftell (fp); - // fseek (fp, 0, SEEK_SET); - // filebuffer = malloc (length); - // if (filebuffer) - // { - // fread (filebuffer, 1, length, fp); - // } pclose (fp); } - // /* Read the output a line at a time - output it. */ - // while (fgets(path, sizeof(path)-1, fp) != NULL) { - // printf("%s", path); - // } - int i; int r; char *new_username = NULL; @@ -192,58 +169,37 @@ request_password_from_external_source(char **username, char **password) return false; } - /* Assume the top-level element is an object */ if (r < 1 || t[0].type != JSMN_OBJECT) { printf("Object expected\n"); return false; } - /* - filebuffer = (char *) pg_malloc(maxlength + 1); - while (fgets(filebuffer, maxlength + 1, infile) != NULL && n < nlines) - result[n++] = pg_strdup(filebuffer); - - */ /* Loop over all keys of the root object */ for (i = 1; i < r; i++) { - //printf("\n0000000000 r=%d i=%d : ", r, i); if (jsoneq(filebuffer, &t[i], "DbUser") == 0) { - //printf("\n1111111"); new_username = pg_malloc(t[i+1].end-t[i+1].start + 2); StrNCpy(new_username, filebuffer + t[i+1].start, t[i+1].end-t[i+1].start + 1); - //printf("- Username: %s\n", new_username); found_username = true; i++; } else if (jsoneq(filebuffer, &t[i], "DbPassword") == 0) { - //printf("\n22222222"); new_password = pg_malloc(t[i+1].end-t[i+1].start + 2); StrNCpy(new_password, filebuffer + t[i+1].start, t[i+1].end-t[i+1].start + 1); - //printf("- Password: %s", new_password); found_password = true; i++; } else if (jsoneq(filebuffer, &t[i], "Expiration") == 0) { - //printf("\n333333b"); - printf("- Expiration: %.*s\n", t[i+1].end-t[i+1].start, - filebuffer + t[i+1].start); + // XXX: Need to cross check current time vs Expiration. i++; - //printf("\n333333e"); } else { - //printf("\n44444444"); - printf("Unexpected key: %.*s\n", t[i].end-t[i].start, - filebuffer + t[i].start); + psql_error("Unexpected key: %.*s\n", t[i].end-t[i].start, + filebuffer + t[i].start); } } - //printf("fetched all params"); + if (found_username && found_password) { *username = pg_strdup(new_username); -// password = pg_strdup(new_password); - printf("PRE: Found username: %s, Password: %s\n", new_username, new_password); -// snprintf(password, sizeof(new_password), "%s", new_password); *password = pg_strdup(new_password); -//sprintf(password, "%s", new_password); - printf("POST: Found username: %s, Password: %s\n", *username, *password); return true; } return false; diff --git a/src/bin/psql/leauth.sh b/src/bin/psql/leauth.sh deleted file mode 100755 index 7aca7b79849a7..0000000000000 --- a/src/bin/psql/leauth.sh +++ /dev/null @@ -1,76 +0,0 @@ -#!/bin/bash - -# AWS CLI requires Python 2.6.5, this script also requires Python 2.6+ or newer -if which aws > /dev/null 2>&1; -then - AWSCLI_VERSION=`python -c "import awscli; print(awscli.__version__)"` - if [[ "$AWSCLI_VERSION" < "1.11.132" ]] ; - then - echo "Please update your version of the AWS CLI." - exit 0 - fi -else - echo "Please install and configure the AWS CLI." - echo "http://docs.aws.amazon.com/cli/latest/userguide/installing.html" - exit 0 -fi - -ARGS="" - -while [[ $# -gt 1 ]] -do -key="$1" -case "$key" in - -U|--username) - USERNAME="$2" - shift - ;; - -i|--clusterid) - CLUSTER_ID="$2" - shift - ;; - -r|--region) - REGION="$2" - shift - ;; - -h|--host) - HOST="$2" - shift - ;; - *) - ARGS="$ARGS $1" - ;; -esac -shift -done - -ARGS="$ARGS $@" - - -if [ "$HOST" != "" ] && [ "$CLUSTER_ID" != "" ] ; -then - echo "Specify either Host or Cluster Identifier, not both." - exit 0 -fi - -if [ "$CLUSTER_ID" != "" ] ; -then - if [ "$REGION" == "" ] ; - then - CLUSTER_INFO=`aws redshift describe-clusters --cluster-identifier "$CLUSTER_ID"` || exit 0 - CREDS=`aws redshift get-cluster-credentials --db-user "$USERNAME" --cluster-identifier "$CLUSTER_ID"` - else - CLUSTER_INFO=`aws redshift describe-clusters --cluster-identifier "$CLUSTER_ID" --region="$REGION"` || exit 0 - CREDS=`aws redshift get-cluster-credentials --db-user "$USERNAME" --cluster-identifier "$CLUSTER_ID" --region="$REGION"` - fi - - CLUSTER_ENDPOINT=`echo "$CLUSTER_INFO" | python -c "import sys, json;print(json.load(sys.stdin)['Clusters'][0]['Endpoint']['Address'])"` - DBPWD=`echo "$CREDS" | python -c "import sys, json; print(json.load(sys.stdin)['DbPassword'])"` - DBUSER=`echo "$CREDS" | python -c "import sys, json; print(json.load(sys.stdin)['DbUser'])"` - - ARGS="$ARGS -U $DBUSER -h $CLUSTER_ENDPOINT" -else - ARGS="$ARGS -U $USERNAME -h $HOST" -fi - -PGPASSWORD="$DBPWD" psql ${ARGS} diff --git a/src/bin/psql/startup.c b/src/bin/psql/startup.c index 8125ea7408b00..f36e7b109bc10 100644 --- a/src/bin/psql/startup.c +++ b/src/bin/psql/startup.c @@ -25,6 +25,8 @@ #include "fe_utils/print.h" #include "settings.h" + + /* * Global psql options */ @@ -157,7 +159,7 @@ main(int argc, char *argv[]) pset.getPassword = TRI_DEFAULT; pset.credential_source = DEFAULT; - + EstablishVariableSpace(); /* Create variables showing psql version number */ @@ -215,16 +217,14 @@ main(int argc, char *argv[]) { if (pset.credential_source == AWS_IAM_REDSHIFT) { - printf("Entry1\n"); if (request_password_from_external_source(&(options.username), &new_password)) { sprintf(password, "%s", new_password); - printf("Username: '%s', Password: '%s'\n", options.username, password); have_password = true; } else { - printf("Unable to fetch Username / Password from IAM"); + psql_error("Unable to fetch Username / Password from IAM"); } if (new_password) free(new_password); @@ -266,29 +266,19 @@ main(int argc, char *argv[]) keywords[7] = NULL; values[7] = NULL; - // printf("Username: '%s', Password: '%s', Host: '%s', Port: '%s', PasswordLen: %d, Database: '%s' \n", values[2], values[3], values[0], values[1], strlen(values[3]), values[4]); - printf("Username: '%s', Password: '%s', Host: '%s', Port: '%s', Database: '%s' \n", values[2], values[3], values[0], values[1], values[4]); - new_pass = false; pset.db = PQconnectdbParams(keywords, values, true); free(keywords); free(values); - printf("Exit1\n"); - if (PQstatus(pset.db) == CONNECTION_BAD) - printf("connection bad\n"); - else - printf("connection not bad\n"); if (PQstatus(pset.db) == CONNECTION_BAD && PQconnectionNeedsPassword(pset.db) && !have_password && pset.getPassword != TRI_NO) { - printf("n1\n"); if (pset.credential_source == AWS_IAM_REDSHIFT) { PQfinish(pset.db); - printf("Entry2\n"); if (request_password_from_external_source(&(options.username), &new_password)) { sprintf(password, "%s", new_password); @@ -297,38 +287,30 @@ main(int argc, char *argv[]) } else { - printf("Unable to fetch new Username / Password from IAM"); + psql_error("Unable to fetch new Username / Password from IAM"); } if (new_password) free(new_password); } else { - printf("n2\n"); - PQfinish(pset.db); simple_prompt(password_prompt, password, sizeof(password), false); have_password = true; new_pass = true; } } - printf("n3\n"); - if (new_pass) - printf("new_pass"); } while (new_pass); - printf("n4\n"); - + free(password_prompt); if (PQstatus(pset.db) == CONNECTION_BAD) { - printf("n5\n"); fprintf(stderr, "%s: %s", pset.progname, PQerrorMessage(pset.db)); PQfinish(pset.db); exit(EXIT_BADCONN); } - printf("n6\n"); - + setup_cancel_handler(); PQsetNoticeProcessor(pset.db, NoticeProcessor, NULL); diff --git a/src/interfaces/libpq/fe-connect.c b/src/interfaces/libpq/fe-connect.c index ae11c61ae95e3..5f79803607156 100644 --- a/src/interfaces/libpq/fe-connect.c +++ b/src/interfaces/libpq/fe-connect.c @@ -531,7 +531,7 @@ PQconnectdbParams(const char *const *keywords, int expand_dbname) { PGconn *conn = PQconnectStartParams(keywords, values, expand_dbname); -printf("n10\n"); + if (conn && conn->status != CONNECTION_BAD) (void) connectDBComplete(conn); @@ -1837,18 +1837,15 @@ connectDBComplete(PGconn *conn) PostgresPollingStatusType flag = PGRES_POLLING_WRITING; time_t finish_time = ((time_t) -1); int timeout = 0; -printf("n1--1\n"); + if (conn == NULL || conn->status == CONNECTION_BAD) - {return 0;printf("n110\n"); - } - printf("n1101\n"); - + return 0; + /* * Set up a time limit, if connect_timeout isn't zero. */ if (conn->connect_timeout != NULL) { - printf("n1102\n"); timeout = atoi(conn->connect_timeout); if (timeout > 0) { @@ -1856,14 +1853,12 @@ printf("n1--1\n"); * Rounding could cause connection to fail; need at least 2 secs */ if (timeout < 2) - timeout = 2; printf("n1103\n"); - + timeout = 2; /* calculate the finish time based on start + timeout */ finish_time = time(NULL) + timeout; } } - printf("n111\n"); - + for (;;) { int ret = 0; @@ -1875,8 +1870,7 @@ printf("n1--1\n"); switch (flag) { case PGRES_POLLING_OK: - printf("n112\n"); - + /* * Reset stored error messages since we now have a working * connection @@ -1905,11 +1899,9 @@ printf("n1--1\n"); default: /* Just in case we failed to set it in PQconnectPoll */ conn->status = CONNECTION_BAD; - printf("n113\n"); return 0; } - printf("n114\n"); - + if (ret == 1) /* connect_timeout elapsed */ { /* @@ -1922,8 +1914,7 @@ printf("n1--1\n"); conn->status = CONNECTION_BAD; return 0; } - printf("n115\n"); - + /* * Attempt connection to the next host, starting the * connect_timeout timer @@ -1934,14 +1925,12 @@ printf("n1--1\n"); if (conn->connect_timeout != NULL) finish_time = time(NULL) + timeout; } - printf("n116\n"); - + /* * Now try to advance the state machine. */ flag = PQconnectPoll(conn); } - printf("n11a\n"); } /* @@ -1962,7 +1951,6 @@ saveErrorMessage(PGconn *conn, PQExpBuffer savedMessage) appendPQExpBufferStr(savedMessage, conn->errorMessage.data); resetPQExpBuffer(&conn->errorMessage); - printf("n11b\n"); return true; } @@ -1976,7 +1964,6 @@ restoreErrorMessage(PGconn *conn, PQExpBuffer savedMessage) resetPQExpBuffer(&conn->errorMessage); appendPQExpBufferStr(&conn->errorMessage, savedMessage->data); termPQExpBuffer(savedMessage); - printf("n11c\n"); } /* ---------------- @@ -2069,8 +2056,7 @@ PQconnectPoll(PGconn *conn) goto error_return; } - printf("n11d\n"); - + keep_going: /* We will come back to here until there is * nothing left to do. */ switch (conn->status) @@ -2161,7 +2147,6 @@ PQconnectPoll(PGconn *conn) continue; } #endif /* F_SETFD */ -printf("n11e\n"); if (!IS_AF_UNIX(addr_cur->ai_family)) { @@ -2434,7 +2419,6 @@ printf("n11e\n"); } } #endif /* HAVE_UNIX_SOCKETS */ -printf("n11g\n"); #ifdef USE_SSL @@ -3402,8 +3386,7 @@ makeEmptyPGconn(void) freePGconn(conn); conn = NULL; } - printf("n11h\n"); - + return conn; } From d6d8228b11be75f1f4b5ec67c0f0109fbcc90730 Mon Sep 17 00:00:00 2001 From: Robins Tharakan Date: Fri, 13 Oct 2017 05:57:34 +1100 Subject: [PATCH 144/149] Finally. psql now fetches password directly from a local working AWS CLI and uses that to login to redshift! Woohoo! --- src/bin/psql/common.c | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index d5a8eb8e77096..5412846296a26 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -133,8 +133,8 @@ request_password_from_external_source(char **username, char **password) strcpy(filebuffer, " "); /* Open the command for reading. */ - //fp = popen("aws redshift get-cluster-credentials --db-user redshift2 --cluster-identifier redshift2", "r"); - fp = popen("cat /home/pi/projects/postgres/src/bin/psql/cluster.txt", "r"); + fp = popen("aws redshift get-cluster-credentials --db-user redshift2 --cluster-identifier redshift2", "r"); + //fp = popen("cat /home/pi/projects/postgres/src/bin/psql/cluster.txt", "r"); if (fp == NULL) { psql_error("Failed to run AWS CLI to fetch IAM authentication: \n"); @@ -160,8 +160,6 @@ request_password_from_external_source(char **username, char **password) jsmn_parser p; jsmntok_t t[128]; /* We expect no more than 128 tokens */ - printf("\n===Enter Function===\n"); - jsmn_init(&p); r = jsmn_parse(&p, filebuffer, strlen(filebuffer), t, sizeof(t)/sizeof(t[0])); if (r < 0) { From 61bacb4ecf0b70725828a656629d64c430d38c09 Mon Sep 17 00:00:00 2001 From: Robins Tharakan Date: Fri, 13 Oct 2017 06:30:54 +1100 Subject: [PATCH 145/149] Pass username argument from commandline to AWS CLI command --- src/bin/psql/common.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index 5412846296a26..71f09719d666c 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -130,10 +130,16 @@ request_password_from_external_source(char **username, char **password) long length; char linebuffer[1035]; char filebuffer[2000]; - + char *aws_command; + strcpy(filebuffer, " "); /* Open the command for reading. */ - fp = popen("aws redshift get-cluster-credentials --db-user redshift2 --cluster-identifier redshift2", "r"); + + aws_command = psprintf("aws redshift get-cluster-credentials --auto-create --db-user %s --cluster-identifier redshift2", *username); + printf("\nCLI: %s\n", aws_command); + fp = popen(aws_command, "r"); + free(aws_command); + //fp = popen("cat /home/pi/projects/postgres/src/bin/psql/cluster.txt", "r"); if (fp == NULL) { From f6c00b58f5377d1a2362330d1b24f1fecaca267c Mon Sep 17 00:00:00 2001 From: Robins Tharakan Date: Fri, 13 Oct 2017 16:39:32 +1100 Subject: [PATCH 146/149] Ensure that re-connections within psql work with IAM. Create stripIAMFromUsernameIfExists() function strip Usernames of IAMA / IAM prefixes --- src/bin/psql/command.c | 9 ++------- src/bin/psql/common.c | 31 +++++++++++++++++++++++++++---- src/bin/psql/common.h | 1 + 3 files changed, 30 insertions(+), 11 deletions(-) diff --git a/src/bin/psql/command.c b/src/bin/psql/command.c index 2f0a8a03f45c8..1ed6fd551bcc7 100644 --- a/src/bin/psql/command.c +++ b/src/bin/psql/command.c @@ -2990,14 +2990,9 @@ do_connect(enum trivalue reuse_previous_specification, if (pset.credential_source == AWS_IAM_REDSHIFT) { if (request_password_from_external_source(&user, &new_password)) - { - sprintf(password, "%s", new_password); - password[strlen(new_password)]='\0'; - } + password = pg_strdup(new_password); else - { - password[0] = '\0'; - } + password = NULL; } else if (pset.credential_source == DEFAULT) password = prompt_for_password(user); diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index 71f09719d666c..716bcd13794b7 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -118,6 +118,27 @@ setQFout(const char *fname) return true; } +char * +stripIAMFromUsernameIfExists(const char *username) +{ + char *new_username = NULL; + + if (strncmp(username, "IAMA", 4) == 0) + { + new_username = pg_malloc(strnlen(username, 60) - 4); + strlcpy(new_username, username+5, strnlen(username, 60) - 4); + } + else if (strncmp(username, "IAM", 3) == 0) + { + new_username = pg_malloc(strnlen(username, 60) - 3); + strlcpy(new_username, username+4, strnlen(username, 60) - 3); + } + else + new_username = pg_strdup(username); + + return new_username; +} + /* * request_password_from_external_source * @@ -131,12 +152,14 @@ request_password_from_external_source(char **username, char **password) char linebuffer[1035]; char filebuffer[2000]; char *aws_command; + char *new_username = NULL; strcpy(filebuffer, " "); /* Open the command for reading. */ - aws_command = psprintf("aws redshift get-cluster-credentials --auto-create --db-user %s --cluster-identifier redshift2", *username); - printf("\nCLI: %s\n", aws_command); + new_username = stripIAMFromUsernameIfExists(*username); + aws_command = psprintf("aws redshift get-cluster-credentials --auto-create --db-user %s --cluster-identifier redshift2", new_username); + printf("\nCLI (for FYI): %s\n", aws_command); fp = popen(aws_command, "r"); free(aws_command); @@ -149,7 +172,8 @@ request_password_from_external_source(char **username, char **password) else { /* Read the output a line at a time - output it. */ - while (fgets(linebuffer, sizeof(linebuffer)-1, fp) != NULL) { + while (fgets(linebuffer, sizeof(linebuffer)-1, fp) != NULL) + { strncat(filebuffer, linebuffer, sizeof(linebuffer)); //printf("Filebuffer: %s\n", filebuffer); } @@ -159,7 +183,6 @@ request_password_from_external_source(char **username, char **password) int i; int r; - char *new_username = NULL; char *new_password = NULL; bool found_password = false; bool found_username = false; diff --git a/src/bin/psql/common.h b/src/bin/psql/common.h index 904e9ffe5cb88..dc4afd1209043 100644 --- a/src/bin/psql/common.h +++ b/src/bin/psql/common.h @@ -47,5 +47,6 @@ extern void expand_tilde(char **filename); extern bool recognized_connection_string(const char *connstr); extern bool request_password_from_external_source(char **username, char **password); +extern char *stripIAMFromUsernameIfExists(const char *username); #endif /* COMMON_H */ From f45734f6f5732eb625d902355d95b9a50512fbfb Mon Sep 17 00:00:00 2001 From: Robins Tharakan Date: Sat, 14 Oct 2017 03:37:29 +1100 Subject: [PATCH 147/149] IAM support now fetches hostname from -h argument of psql, which resolves Issue #35 . Currently we still don't support host provided within the dbname string, which is another issue ( #36 ) --- src/bin/psql/command.c | 2 +- src/bin/psql/common.c | 43 +++++++++++++++++++++++++++++++----------- src/bin/psql/common.h | 3 ++- src/bin/psql/startup.c | 4 ++-- 4 files changed, 37 insertions(+), 15 deletions(-) diff --git a/src/bin/psql/command.c b/src/bin/psql/command.c index 1ed6fd551bcc7..ffcb824622963 100644 --- a/src/bin/psql/command.c +++ b/src/bin/psql/command.c @@ -2989,7 +2989,7 @@ do_connect(enum trivalue reuse_previous_specification, { if (pset.credential_source == AWS_IAM_REDSHIFT) { - if (request_password_from_external_source(&user, &new_password)) + if (request_password_from_external_source(&user, &new_password, host)) password = pg_strdup(new_password); else password = NULL; diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index 716bcd13794b7..04b1557c33936 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -139,26 +139,55 @@ stripIAMFromUsernameIfExists(const char *username) return new_username; } +char * +stripSubDomainFromHost(const char *host) +{ + int len; + char *stripped_host = strstr(host, "."); + + if (stripped_host) + { + len = stripped_host - host; + stripped_host = pg_malloc(len+1); + + if (stripped_host != NULL) + strlcpy(stripped_host, host, len+1); + } + else + stripped_host = pg_strdup(host); + + return stripped_host; +} + /* * request_password_from_external_source * * Generalized function to fetch usernames and passwords from external source. */ bool -request_password_from_external_source(char **username, char **password) +request_password_from_external_source(char **username, char **password, const char *host) { FILE *fp; - long length; char linebuffer[1035]; char filebuffer[2000]; char *aws_command; char *new_username = NULL; + char *stripped_host = NULL; + int i; + int r; + char *new_password = NULL; + bool found_password = false; + bool found_username = false; + jsmn_parser p; + jsmntok_t t[128]; /* We expect no more than 128 tokens */ strcpy(filebuffer, " "); /* Open the command for reading. */ new_username = stripIAMFromUsernameIfExists(*username); - aws_command = psprintf("aws redshift get-cluster-credentials --auto-create --db-user %s --cluster-identifier redshift2", new_username); + stripped_host = stripSubDomainFromHost(host); + + aws_command = psprintf("aws redshift get-cluster-credentials --auto-create --db-user %s --cluster-identifier %s", new_username, stripped_host); printf("\nCLI (for FYI): %s\n", aws_command); fp = popen(aws_command, "r"); free(aws_command); @@ -181,14 +210,6 @@ request_password_from_external_source(char **username, char **password) pclose (fp); } - int i; - int r; - char *new_password = NULL; - bool found_password = false; - bool found_username = false; - jsmn_parser p; - jsmntok_t t[128]; /* We expect no more than 128 tokens */ - jsmn_init(&p); r = jsmn_parse(&p, filebuffer, strlen(filebuffer), t, sizeof(t)/sizeof(t[0])); if (r < 0) { diff --git a/src/bin/psql/common.h b/src/bin/psql/common.h index dc4afd1209043..0f965cc2dce62 100644 --- a/src/bin/psql/common.h +++ b/src/bin/psql/common.h @@ -46,7 +46,8 @@ extern void expand_tilde(char **filename); extern bool recognized_connection_string(const char *connstr); -extern bool request_password_from_external_source(char **username, char **password); +extern bool request_password_from_external_source(char **username, char **password, const char *host); extern char *stripIAMFromUsernameIfExists(const char *username); +extern char *stripSubDomainFromHost(const char *host); #endif /* COMMON_H */ diff --git a/src/bin/psql/startup.c b/src/bin/psql/startup.c index f36e7b109bc10..c5ad00ce13d31 100644 --- a/src/bin/psql/startup.c +++ b/src/bin/psql/startup.c @@ -217,7 +217,7 @@ main(int argc, char *argv[]) { if (pset.credential_source == AWS_IAM_REDSHIFT) { - if (request_password_from_external_source(&(options.username), &new_password)) + if (request_password_from_external_source(&(options.username), &new_password, options.host)) { sprintf(password, "%s", new_password); have_password = true; @@ -279,7 +279,7 @@ main(int argc, char *argv[]) if (pset.credential_source == AWS_IAM_REDSHIFT) { PQfinish(pset.db); - if (request_password_from_external_source(&(options.username), &new_password)) + if (request_password_from_external_source(&(options.username), &new_password, options.host)) { sprintf(password, "%s", new_password); have_password = true; From 2efc1a7912bda2d6a9061f04406f63c295d43040 Mon Sep 17 00:00:00 2001 From: Robins Tharakan Date: Sat, 14 Oct 2017 03:52:56 +1100 Subject: [PATCH 148/149] Minor sanity check of error messages / display messages etc. before merging to mainline --- src/bin/psql/command.c | 5 ++++- src/bin/psql/common.c | 8 ++++---- src/bin/psql/startup.c | 4 ++-- 3 files changed, 10 insertions(+), 7 deletions(-) diff --git a/src/bin/psql/command.c b/src/bin/psql/command.c index ffcb824622963..0bc60023457ab 100644 --- a/src/bin/psql/command.c +++ b/src/bin/psql/command.c @@ -2992,7 +2992,10 @@ do_connect(enum trivalue reuse_previous_specification, if (request_password_from_external_source(&user, &new_password, host)) password = pg_strdup(new_password); else - password = NULL; + { + psql_error("Unable to fetch new Username / Password from IAM\n"); + password = NULL; + } } else if (pset.credential_source == DEFAULT) password = prompt_for_password(user); diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index 04b1557c33936..c0056299be0d7 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -188,14 +188,14 @@ request_password_from_external_source(char **username, char **password, const ch stripped_host = stripSubDomainFromHost(host); aws_command = psprintf("aws redshift get-cluster-credentials --auto-create --db-user %s --cluster-identifier %s", new_username, stripped_host); - printf("\nCLI (for FYI): %s\n", aws_command); + printf("\nCLI Command (Just FYI): %s\n", aws_command); fp = popen(aws_command, "r"); free(aws_command); //fp = popen("cat /home/pi/projects/postgres/src/bin/psql/cluster.txt", "r"); if (fp == NULL) { - psql_error("Failed to run AWS CLI to fetch IAM authentication: \n"); + psql_error("Unable to run AWS CLI to fetch IAM authentication.\n"); exit(1); } else @@ -213,13 +213,13 @@ request_password_from_external_source(char **username, char **password, const ch jsmn_init(&p); r = jsmn_parse(&p, filebuffer, strlen(filebuffer), t, sizeof(t)/sizeof(t[0])); if (r < 0) { - printf("Failed to parse JSON: %d\n", r); + psql_error("Unable to parse CLI JSON Response: %d\n", r); return false; } /* Assume the top-level element is an object */ if (r < 1 || t[0].type != JSMN_OBJECT) { - printf("Object expected\n"); + psql_error("CLI Response not a JSON object\n"); return false; } diff --git a/src/bin/psql/startup.c b/src/bin/psql/startup.c index c5ad00ce13d31..2626e61663a95 100644 --- a/src/bin/psql/startup.c +++ b/src/bin/psql/startup.c @@ -224,7 +224,7 @@ main(int argc, char *argv[]) } else { - psql_error("Unable to fetch Username / Password from IAM"); + psql_error("Unable to fetch Username / Password from IAM\n"); } if (new_password) free(new_password); @@ -287,7 +287,7 @@ main(int argc, char *argv[]) } else { - psql_error("Unable to fetch new Username / Password from IAM"); + psql_error("Unable to fetch new Username / Password from IAM\n"); } if (new_password) free(new_password); From 01c3bce06dde737db2177e852c75efdb8526868b Mon Sep 17 00:00:00 2001 From: Robins Tharakan Date: Sat, 14 Oct 2017 04:06:10 +1100 Subject: [PATCH 149/149] Last minute Whitespace / Debug output cleansing before merge to mainline --- src/bin/psql/common.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index c0056299be0d7..3a69fe6424cd1 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -180,10 +180,10 @@ request_password_from_external_source(char **username, char **password, const ch bool found_username = false; jsmn_parser p; jsmntok_t t[128]; /* We expect no more than 128 tokens */ - + strcpy(filebuffer, " "); /* Open the command for reading. */ - + new_username = stripIAMFromUsernameIfExists(*username); stripped_host = stripSubDomainFromHost(host); @@ -204,7 +204,6 @@ request_password_from_external_source(char **username, char **password, const ch while (fgets(linebuffer, sizeof(linebuffer)-1, fp) != NULL) { strncat(filebuffer, linebuffer, sizeof(linebuffer)); - //printf("Filebuffer: %s\n", filebuffer); } pclose (fp);