From fb84551a808aa273e8be3aa2ba6e9a3ac02c0598 Mon Sep 17 00:00:00 2001 From: Fahim Ahammed Firoz Date: Wed, 1 Oct 2025 21:40:36 +0600 Subject: [PATCH 1/3] docs: add PostgreSQL functions & procedures guide --- README.md | 1 + psql/functions.md | 82 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 83 insertions(+) create mode 100644 psql/functions.md diff --git a/README.md b/README.md index 60d6e84..01a4025 100644 --- a/README.md +++ b/README.md @@ -14,6 +14,7 @@ Welcome to the **Learn PostgreSQL** repository! This guide provides comprehensiv - [Querying Data](psql/query.md) - [SQL Clauses](psql/clauses.md) - [Advanced Concepts](psql/advance.md) +- [Functions & Procedures](psql/functions.md) - [Contributing](#contributing) - [License](#license) - [Acknowledgements](#acknowledgements) diff --git a/psql/functions.md b/psql/functions.md new file mode 100644 index 0000000..7f9c09c --- /dev/null +++ b/psql/functions.md @@ -0,0 +1,82 @@ +# PostgreSQL Functions & Stored Procedures + +PostgreSQL provides powerful support for creating **functions** and **stored procedures** to encapsulate SQL logic, improve code reusability, and optimize performance. Functions can return values, while stored procedures can perform actions without necessarily returning data. + +--- + +## Types of Functions + +- **SQL Functions**: Written directly in SQL. +- **PL/pgSQL Functions**: Use PostgreSQL’s procedural language for more complex logic. +- **Trigger Functions**: Special functions invoked automatically by triggers. + +--- + +## Creating a Function + +```sql +CREATE FUNCTION add_numbers(a INT, b INT) +RETURNS INT AS $$ +BEGIN + RETURN a + b; +END; +$$ LANGUAGE plpgsql; +```` + +**Usage:** + +```sql +SELECT add_numbers(5, 10); +``` + +--- + +## Stored Procedures + +Stored procedures are similar to functions but are called using the `CALL` statement and can perform transactions. + +```sql +CREATE PROCEDURE log_message(msg TEXT) +LANGUAGE plpgsql +AS $$ +BEGIN + INSERT INTO logs(message, created_at) VALUES (msg, NOW()); +END; +$$; +``` + +**Usage:** + +```sql +CALL log_message('Database operation completed'); +``` + +--- + +## Triggers with Functions + +You can use functions as **triggers** to automate actions: + +```sql +CREATE OR REPLACE FUNCTION update_timestamp() +RETURNS TRIGGER AS $$ +BEGIN + NEW.updated_at = NOW(); + RETURN NEW; +END; +$$ LANGUAGE plpgsql; + +CREATE TRIGGER update_user_timestamp +BEFORE UPDATE ON users +FOR EACH ROW +EXECUTE FUNCTION update_timestamp(); +``` + +--- + +## Best Practices + +* Use `plpgsql` for procedural logic. +* Keep functions modular and reusable. +* Add exception handling for reliability. +* Use triggers carefully to avoid hidden performance issues. \ No newline at end of file From cdb87dd5172fa85ae74b7d6ceae920536688665d Mon Sep 17 00:00:00 2001 From: Fahim Ahammed Firoz Date: Wed, 1 Oct 2025 22:53:16 +0600 Subject: [PATCH 2/3] update contributing for hacktoberfest --- CONTRIBUTING.md | 52 ++++++++++++++++++++++--------------------------- 1 file changed, 23 insertions(+), 29 deletions(-) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 5a1964e..355df38 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -1,64 +1,58 @@ -# Contributing to Learn PostgreSQL - -Thank you for considering contributing to the **Learn PostgreSQL** project! Your contributions help improve the quality of this resource and are greatly appreciated. -## Hacktoberfest +# Contributing to Learn PostgreSQL -This repository is open to contributions as part of **Hacktoberfest**. If you are participating in Hacktoberfest, follow the guidelines below to ensure that your pull request (PR) is accepted and counts toward your Hacktoberfest contributions. +Thank you for your interest in contributing to the **Learn PostgreSQL** project! Your help makes this resource better for everyone—whether by fixing typos, improving explanations, or adding new PostgreSQL tutorials. -### What is Hacktoberfest? +## Hacktoberfest 2025 -Hacktoberfest is a month-long event (October) that encourages people to contribute to open-source projects. Contributors must submit **four quality pull requests** to participating repositories to earn a Hacktoberfest T-shirt or plant a tree. +This repository is open for **Hacktoberfest 2025** contributions. Hacktoberfest is a month-long event in October that encourages participants to contribute to open-source projects. Completing **four quality pull requests** to participating repositories earns you a Hacktoberfest T-shirt or a tree planted in your name. -You can read more about Hacktoberfest [here](https://hacktoberfest.com). +Learn more about Hacktoberfest [here](https://hacktoberfest.com). ## How to Contribute -1. **Fork the repository**: Start by forking this repository to your own GitHub account. Click the "Fork" button at the top right of this page. - -2. **Clone the repository**: Clone your fork locally. +1. **Fork the repository** – Click the "Fork" button at the top right of this page. +2. **Clone your fork** – Clone it to your local machine: + ```bash git clone https://github.com/fahimahammed/learn-postgresql.git ``` +3. **Create a branch** – Make a new branch for your work: -3. **Create a branch**: Create a new branch to work on your changes. ```bash git checkout -b your-branch-name ``` +4. **Make your changes** – Fix typos, improve guides, or add new PostgreSQL content. +5. **Commit your changes** – Write a clear and descriptive commit message: -4. **Make your changes**: Implement your changes. You can improve documentation, fix issues, or add new PostgreSQL learning content. - -5. **Commit your changes**: After making changes, commit them with a descriptive message. ```bash git add . - git commit -m "Your descriptive commit message" + git commit -m "Add guide for PostgreSQL JSON functions" ``` +6. **Push your branch** – Push your changes to your fork: -6. **Push your changes**: Push the changes to your fork. ```bash git push origin your-branch-name ``` - -7. **Create a pull request**: Go to the original repository and create a pull request from your fork. Ensure you provide a detailed description of the changes you made. +7. **Open a Pull Request** – Go to the original repository and submit a PR with a detailed description of your changes. ## Contribution Guidelines -- **New to open source?** No problem! Feel free to start by reviewing the [issues](https://github.com/fahimahammed/learn-postgresql/issues) in this repository and addressing bugs, typos, or other enhancements. -- **Documentation**: If you're improving the documentation (e.g., README, guides), make sure to use clear and concise language. -- **Code Contributions**: If you're contributing code or examples, make sure your code is clean, follows best practices, and is well-documented. -- **Respect Community Standards**: Please be kind and respectful to others in your interactions. We strive to maintain a welcoming and supportive community. +* **New to open source?** Start by reviewing [issues](https://github.com/fahimahammed/learn-postgresql/issues) and fixing small bugs, typos, or formatting errors. +* **Documentation** – Keep language clear and concise. Improve readability wherever possible. +* **Code & Examples** – Ensure your code is clean, follows best practices, and includes comments when necessary. +* **Community Standards** – Be respectful, kind, and supportive in all interactions. ## Issues -Feel free to browse open issues or create new ones if you spot any problems or have suggestions. When creating an issue, try to provide as much information as possible. +If you find a bug, have suggestions, or want to propose new content, feel free to [create an issue](https://github.com/fahimahammed/learn-postgresql/issues) with detailed information. ## Review Process -Once you submit a pull request: -- Your contribution will be reviewed by the maintainers. -- Feedback may be provided, and updates may be requested to ensure the quality of the content. -- Once everything looks good, your pull request will be merged! +* Submitted PRs will be reviewed by maintainers. +* Feedback may be provided, and updates requested to ensure quality. +* Once approved, your PR will be merged into the main repository. ## License -By contributing, you agree that your contributions will be licensed under the MIT License, as detailed in the [LICENSE](LICENSE) file. +By contributing, you agree that your contributions will be licensed under the **MIT License** as detailed in the [LICENSE](LICENSE) file. From 0f31866f83790fea390e30c50eaa240ac9ef1d18 Mon Sep 17 00:00:00 2001 From: Fahim Ahammed Firoz Date: Wed, 1 Oct 2025 22:56:13 +0600 Subject: [PATCH 3/3] add github action for pr labeler --- .github/workflows/pr-labeler.yml | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 .github/workflows/pr-labeler.yml diff --git a/.github/workflows/pr-labeler.yml b/.github/workflows/pr-labeler.yml new file mode 100644 index 0000000..00324fe --- /dev/null +++ b/.github/workflows/pr-labeler.yml @@ -0,0 +1,22 @@ +name: "PR Labeler for Hacktoberfest" + +on: + pull_request: + types: [opened, reopened, synchronize] + +jobs: + add-labels: + runs-on: ubuntu-latest + steps: + - name: Checkout repository + uses: actions/checkout@v3 + + - name: Label PR for Hacktoberfest + uses: actions-ecosystem/action-add-labels@v1 + with: + github_token: ${{ secrets.GITHUB_TOKEN }} + labels: | + good first issue + hacktoberfest + hacktoberfest_2025 + hacktoberfest-accepted