Skip to content

Comments

feat: created a walkthrough of the schema and table from the postgres DB#1

Merged
Abiji-2020 merged 18 commits intomainfrom
feat/dev
Nov 16, 2025
Merged

feat: created a walkthrough of the schema and table from the postgres DB#1
Abiji-2020 merged 18 commits intomainfrom
feat/dev

Conversation

@Abiji-2020
Copy link
Owner

No description provided.

Signed-off-by: Abinand P <abinand0911@gmail.com>
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR implements database schema exploration functionality for the PostgreSQL extension by introducing new components to walk through and format database schema information including schemas, tables, and columns.

Key Changes:

  • Introduced explorer.cpp/h to implement database schema scanning using PostgreSQL system catalogs
  • Added ai_engine.cpp/h to create an AI engine wrapper with OpenAI client integration
  • Refactored pg_ask.cpp to use the new explorer functions instead of the previous implementation
  • Removed legacy components (factory, prompt_builder, schema_loader, sql_executor, ai_client)
  • Updated code formatting rules and documentation

Reviewed Changes

Copilot reviewed 12 out of 12 changed files in this pull request and generated 7 comments.

Show a summary per file
File Description
src/pg_ask.cpp Updated to use new explorer functions for database schema retrieval and formatting
src/explorer.cpp Implements database schema scanning by querying PostgreSQL system catalogs and formats output
include/explorer.h Header declaring buildDatabaseMap and formatSchema functions
src/ai_engine.cpp Creates AI engine with OpenAI client, handles API key configuration and model selection
include/ai_engine.h Declares Engine struct and related functions for AI integration
include/factory.hpp Removed legacy factory pattern for AI client creation
include/prompt_builder.hpp Removed legacy prompt builder interface
include/schema_loader.hpp Removed legacy schema loader with TableInfo struct
include/sql_executor.hpp Removed legacy SQL executor interface
include/ai_client.hpp Removed legacy abstract AI client interface
.clang-format Updated formatting rules to use LLVM style with custom settings
.github/instructions/copilot-instructions.md Added project structure documentation for Copilot

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Signed-off-by: Abinand P <abinand0911@gmail.com>
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

Copilot reviewed 13 out of 13 changed files in this pull request and generated 8 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Signed-off-by: Abinand P <abinand0911@gmail.com>
Signed-off-by: Abinand P <abinand0911@gmail.com>
Signed-off-by: Abinand P <abinand0911@gmail.com>
Signed-off-by: Abinand P <abinand0911@gmail.com>
Signed-off-by: Abinand P <abinand0911@gmail.com>
Signed-off-by: Abinand P <abinand0911@gmail.com>
Signed-off-by: Abinand P <abinand0911@gmail.com>
Signed-off-by: Abinand P <abinand0911@gmail.com>
Signed-off-by: Abinand P <abinand0911@gmail.com>
Signed-off-by: Abinand P <abinand0911@gmail.com>
Signed-off-by: Abinand P <abinand0911@gmail.com>
Signed-off-by: Abinand P <abinand0911@gmail.com>
Signed-off-by: Abinand P <abinand0911@gmail.com>
Signed-off-by: Abinand P <abinand0911@gmail.com>
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

Copilot reviewed 23 out of 23 changed files in this pull request and generated 11 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.


file(GLOB CORE_SOURCES src/*.cpp)
file(GLOB CORE_HEADERS include/*.hpp)
file(GLOB CORE_HEADERS include/*.h)
Copy link

Copilot AI Nov 16, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] The file glob pattern has been changed from include/*.hpp to include/*.h. Ensure that all header files in the include/ directory now use the .h extension and that no .hpp files remain that should be included in the build.

Copilot uses AI. Check for mistakes.
Comment on lines +7 to +10
CREATE OR REPLACE FUNCTION pg_execute_query(query text)
RETURNS text
AS 'pg_ask', 'pg_execute_query'
LANGUAGE C STRICT PARALLEL SAFE;
Copy link

Copilot AI Nov 16, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The SQL file declares a function pg_execute_query that is not implemented in the C++ code. This will cause runtime errors when users try to call this function. Either implement the function or remove its declaration from the SQL file.

Suggested change
CREATE OR REPLACE FUNCTION pg_execute_query(query text)
RETURNS text
AS 'pg_ask', 'pg_execute_query'
LANGUAGE C STRICT PARALLEL SAFE;

Copilot uses AI. Check for mistakes.

PG_RETURN_TEXT_P(cstring_to_text(userQuery.c_str()));
try {
auto eng = pg_ask::ai_engine::make_engine("", "openai/gpt-oss-20b", "https://api.groq.com/openai");
Copy link

Copilot AI Nov 16, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The model name "openai/gpt-oss-20b" appears to be a hardcoded test or placeholder value. This should be configurable (e.g., via an environment variable or function parameter) to allow users to specify different models. Additionally, the "gpt-oss-20b" model name does not appear to be a standard OpenAI or Groq model identifier.

Copilot uses AI. Check for mistakes.
src/pg_ask.cpp Outdated
PG_RETURN_NULL();
} catch (...) {
ereport(ERROR, (errmsg("Unknown error during catalog inspection/formatting")));
PG_RETURN_NULL();
Copy link

Copilot AI Nov 16, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After calling ereport(ERROR, ...), execution does not continue, so PG_RETURN_NULL() on this line is unreachable. The ereport(ERROR, ...) function will longjmp and never return. This line should be removed.

Suggested change
PG_RETURN_NULL();

Copilot uses AI. Check for mistakes.
Comment on lines +8 to +184
curl -fsSL https://$GITHUB_PAGES_URL/install.sh | bash
```

### Windows
For Windows, use Docker or WSL2 with the Linux installation method.

### Docker
```bash
docker pull ghcr.io/$GITHUB_REPOSITORY:latest
docker run -it \
-e POSTGRES_PASSWORD=postgres \
-e PG_ASK_AI_KEY="your-api-key" \
-p 5432:5432 \
ghcr.io/$GITHUB_REPOSITORY:latest
```

### With docker-compose
```bash
cat > docker-compose.yaml << 'EOF'
version: "3.9"

services:
postgres:
image: ghcr.io/$GITHUB_REPOSITORY:latest
environment:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
POSTGRES_DB: askdb
PG_ASK_AI_KEY: ${API_KEY}
ports:
- "5432:5432"
volumes:
- pgdata:/var/lib/postgresql/data

pgadmin:
image: dpage/pgadmin4:latest
environment:
PGADMIN_DEFAULT_EMAIL: admin@example.com
PGADMIN_DEFAULT_PASSWORD: admin
depends_on:
- postgres
ports:
- "8080:80"

volumes:
pgdata:
EOF

export API_KEY="your-api-key"
docker-compose up
```

## Direct Downloads

All pre-built binaries are available at:
**https://$GITHUB_PAGES_URL/releases/**

Or browse: **https://$GITHUB_PAGES_URL/**

## Manual Installation

### Prerequisites
- PostgreSQL 14+ with development headers installed
- CMake 3.10+
- C++ compiler (GCC, Clang, or MSVC)
- ai-sdk-cpp library (included in repository)

### Build from Source
```bash
git clone https://github.com/$GITHUB_REPOSITORY.git
cd pg_ask
mkdir build && cd build
cmake -DUSE_AI_SDK=ON ..
make
sudo make install
```

### Create Extension
```sql
CREATE EXTENSION pg_ask;
```

### Verify Installation
```sql
SELECT pg_gen_query('show me all tables');
```

## Docker Advanced Usage

### With docker-compose
```bash
cat > docker-compose.yaml << 'EOF'
version: "3.9"

services:
postgres:
image: ghcr.io/Abiji-2020/pg_ask:latest
environment:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
POSTGRES_DB: askdb
PG_ASK_AI_KEY: ${API_KEY}
ports:
- "5432:5432"
volumes:
- pgdata:/var/lib/postgresql/data

pgadmin:
image: dpage/pgadmin4:latest
environment:
PGADMIN_DEFAULT_EMAIL: admin@example.com
PGADMIN_DEFAULT_PASSWORD: admin
depends_on:
- postgres
ports:
- "8080:80"

volumes:
pgdata:
EOF

export API_KEY="your-api-key"
docker-compose up
```

## Environment Variables

### Docker/Runtime
- `PG_ASK_AI_KEY`: OPENAI API key (required for SQL generation)
- `POSTGRES_USER`: PostgreSQL user (default: postgres)
- `POSTGRES_PASSWORD`: PostgreSQL password (required)
- `POSTGRES_DB`: Initial database name (default: postgres)

### Installation Scripts
- `--version VERSION`: Specify release version (default: latest)
- `--pg-version PG_VERSION`: PostgreSQL major version (auto-detected if not specified)

## Troubleshooting

### Extension not found after installation
```sql
-- Check extension is installed
\dx pg_ask

-- Check extension files exist
SELECT pg_catalog.pg_ls_dir(setting || '/extension')
FROM pg_settings WHERE name = 'sharedir';
```

### Permission denied errors on Linux
- Ensure you have write permissions or use `sudo`
- Try: `sudo -E bash -c 'curl ... | bash'` to preserve env vars

### Build errors
Ensure PostgreSQL development headers:
```bash
# Ubuntu/Debian
sudo apt-get install postgresql-server-dev-16

# CentOS/RHEL
sudo yum install postgresql-devel
```

### Docker connection issues
```bash
# Test connection
docker exec pg_ask_postgres psql -U postgres -d postgres -c "SELECT version();"

# View logs
docker logs pg_ask_postgres
```

## Support

- Issues: https://github.com/$GITHUB_REPOSITORY/issues
- Releases: https://github.com/$GITHUB_REPOSITORY/releases
- Pages: https://$GITHUB_PAGES_URL/pg_ask/
Copy link

Copilot AI Nov 16, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The placeholders $GITHUB_REPOSITORY and $GITHUB_PAGES_URL are used throughout this markdown file but will not be expanded. These should be replaced with actual values (e.g., Abiji-2020/pg_ask as seen in line 104) or clearly documented as placeholders that users need to replace manually.

Copilot uses AI. Check for mistakes.
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
@Abiji-2020 Abiji-2020 merged commit 3227f19 into main Nov 16, 2025
1 of 2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant