teleren/devto huggingface dataset:
Finding Which Columns Value Has Actually Changed in PostgreSQL Trigger Functions
Tags: sql, tutorial, programming
Published: 2024-04-07 15:49:15

---
published: true
title: Finding Which Columns Value Has Actually Changed in PostgreSQL Trigger Functions
description: 'In PostgreSQL, triggers are powerful tools used to execute custom actions when certain events occur on a specified table.'
tags:
  - sql
  - tutorial
  - programming
cover_image: 'https://i.imgur.com/JK2j6mT.png'
canonical_url: null
id: 1814001
date: '2024-04-07T15:49:15Z'
---

### Finding Which Columns Value Has Actually Changed in PostgreSQL Trigger Functions

In PostgreSQL, triggers are powerful tools used to execute custom actions when certain events occur on a specified table. One common use case for triggers is auditing changes to data. When implementing an audit trail, it's essential to identify which columns have been modified in an update operation. This article explores how to efficiently determine which columns have had their values changed within a trigger function.

### The Challenge

When an update operation occurs on a table, it's necessary to identify which columns have been modified and record these changes. However, PostgreSQL does not provide built-in functionality to directly identify the changed columns within a trigger function. Instead, developers need to devise a method to achieve this.

### Solution With Example

To identify the changed columns within a PostgreSQL trigger function, we can compare the old and new values of each column using dynamic SQL and JSON functions. Let's illustrate this with an example trigger function:

```sql
CREATE OR REPLACE FUNCTION audit_changes()
RETURNS TRIGGER AS
$$
DECLARE
    _changed_columns TEXT[];
BEGIN
    SELECT array_agg(pre.key)
    INTO _changed_columns
    FROM jsonb_each(to_jsonb(old)) AS pre
    CROSS JOIN jsonb_each(to_jsonb(new)) AS post
    WHERE pre.key = post.key
      AND pre.value IS DISTINCT FROM post.value
    ;

    -- Perform actions based on the changed columns
    -- For example, log the changes to an audit table
    INSERT INTO audit_table (table_name, changed_columns)
    VALUES (TG_TABLE_NAME, _changed_columns);
    
    RETURN NEW;
END;
$$
LANGUAGE plpgsql;
```

Once the trigger function `audit_changes()` is defined, it can be associated with a specific table and event (e.g., `BEFORE UPDATE`):

```sql
CREATE TRIGGER audit_table_changes
BEFORE UPDATE ON your_table
FOR EACH ROW
EXECUTE FUNCTION audit_changes();
```

This trigger will execute the `audit_changes()` function before each `UPDATE` operation on `your_table`. It will identify the changed columns, log them into an audit table, and then allow the original update operation to proceed.

### Conclusion

In PostgreSQL trigger functions, identifying changed columns is a common requirement for various purposes such as auditing and conditional logic. By leveraging PostgreSQL's JSONB functions, we can efficiently compare old and new row states to determine which columns have been modified. Implementing this logic within trigger functions allows for automated tracking of changes, enhancing data integrity and auditability in database systems.
A typical Machine learning workflow
Tags: machinelearning, ai, datascience
Published: 2024-04-07 15:33:06

Typical workflow for building a machine learning model 📝, Very Important for Data Science, Artificial intelligence AI, Data Analysis, Machine learning ML


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hixnn3xox2y2tca693fe.jpg)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dkgd00j73odv4dbziufx.jpg)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mbk91h5wxl2jcq0vvwnf.jpg)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ukf5d2o7h7eq0ge0rdji.jpg)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/139nt3qpqal2l086n5en.jpg)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4xm4npom52vld35gnybg.jpg)
LoReFT and pyreft for surgical fine-tuning
Tags: llm, finetuning, lora, loreft
Published: 2024-04-07 15:23:13

Here’s trying to understand and summarise the paper "ReFT: Representation Finetuning for Language Models" 

https://arxiv.org/abs/2404.03592
https://github.com/stanfordnlp/pyreft

The paper proposes Representation Finetuning (ReFT) as a more parameter-efficient alternative to PEFTs like adapters and LoRA for adapting large language models to downstream tasks. Key ideas:

- ReFT methods train task-specific interventions that modify hidden representations of a frozen base model, leveraging the insight from interpretability work that representations encode rich semantics. 

- The Low-rank Linear Subspace ReFT (LoReFT) variant uses 10-50x fewer parameters than leading PEFTs by intervening on representations in a learned low-dimensional subspace.

- LoReFT provides state-of-the-art efficiency-performance tradeoffs on commonsense reasoning, arithmetic, instruction following, and language understanding tasks. On instruction tuning Llama-2 7B, it nearly matches GPT-3.5 using only 0.004% extra parameters.

- Editing representations may be more powerful than modifying weights as done in PEFTs. LoReFT did struggle more on arithmetic reasoning, possibly due to long output lengths.

To make ReFT easy to use, the authors also introduce pyreft, a library that enables:

- Fine-tuning any HuggingFace pretrained LM with ReFT 
- Configuring ReFT hyperparameters via config files
- Easily sharing fine-tuned models on HuggingFace

Here is some pseudo-code showing a basic usage of pyreft to fine-tune a Llama-7B model:

```python
from pyreft import get_reft_model, ReftConfig, ReftTrainerForCausalLM

# Load pretrained LM
model = AutoModelForCausalLM.from_pretrained("llama-7b-hf") 

# Configure LoReFT intervention
reft_config = ReftConfig(
  representations={
    "layer": 15, 
    "component": "block_output",
    "intervention": LoReftIntervention(embed_dim=model.hidden_size, rank=1)
  }  
)

# Wrap model with ReFT  
reft_model = get_reft_model(model, reft_config)

# Load training data
data_module = make_supervised_data_module(...)

# Configure training
trainer = ReftTrainerForCausalLM(
  model=reft_model,
  args=TrainingArguments(...),  
  **data_module
)

# Train model
trainer.train()
```

This fine-tunes a Llama-7B model by training a rank-1 LoReFT intervention at layer 15, modifying only 0.00006% of the model parameters. Once trained, the reft_model can be used for inference on downstream tasks.

By establishing ReFT as a promising new paradigm for LM adaptation, this work points to representation editing as a powerful lever for both model efficiency and interpretability. The pyreft library enables researchers and practitioners to easily experiment with and build on these ideas.
Add Scroll to Top functionality inside shadcn's <ScrollArea/> component
Published: 2024-04-07 15:17:44

The `<ScrollArea/>` component from shadcn's UI library provides an elegant scrolling experience, but it doesn't include built-in functionality for scrolling to the top of the content area. In this post, we'll walk through how to add a "Scroll to Top" button that smoothly scrolls the `<ScrollArea/>` to its beginning.

#Step 1: Import `useRef` from React
```
import { useRef } from "react";
```

#Step 2: Create a ref for the `<ScrollArea/>`
```
const scrollAreaRef = useRef<HTMLDivElement | null>(null);
```
This line creates a mutable ref object that will hold a reference to the `<ScrollArea/>` div element.

#Step 3: Attach the `ref` to the top `<div>`
```
<div ref={scrollAreaRef}>
  {/* ScrollArea content goes here */}
</div>
```

![Scroll to top ShadCN](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7kq0deuyc5copi6dis55.png)
Just like this

#Step 4: Define the `triggerScrollToTop` function
```
const triggerScrollToTop = () => {
  if (scrollAreaRef.current) {
    scrollAreaRef.current.scrollIntoView({
      behavior: 'smooth',
      block: 'start'
    });
  }
};
```
This function checks if `scrollAreaRef.current` has a value (i.e., if the `<ScrollArea/>` has mounted). If so, it calls the `scrollIntoView` method on the element, passing options to ensure smooth scrolling to the top.



#Step 5: Add a "Scroll to Top" button
```
<button
  onClick={triggerScrollToTop}
>
  Scroll to Top
</button>
```

Finally, render a button that calls `triggerScrollToTop` when clicked. Adjust the styling and placement as desired.

And that's it! With these steps, you've added smooth scroll-to-top functionality to shadcn's `<ScrollArea/>` component using just a few lines of React code.
basic structure for an online compiler that allows users to write, compile, and execute C++ code
Tags: programming, devops, coding, javascript
Published: 2024-04-07 15:05:10

#include <iostream>
#include <string>
#include <sstream>

// Function to simulate the compilation process
std::string compileAndRun(const std::string& code) {
    // In a real scenario, this function would invoke the C++ compiler
    // For demonstration, we'll just return a success message
    return "Compilation successful. Program executed.";
}

int main() {
    std::string userCode;
    std::cout << "Enter your C++ code below:" << std::endl;
    std::getline(std::cin, userCode); // Read the user's code

    // Simulate saving the code (in reality, save to a file or database)
    std::cout << "Saving your code..." << std::endl;

    // Simulate sharing the code (in reality, generate a shareable link)
    std::cout << "Generating a link to share your code..." << std::endl;

    // Compile and run the user's code
    std::string result = compileAndRun(userCode);
    std::cout << result << std::endl;

    return 0;
}
Terraform Test and AWS Lambda
Tags: aws, lambda, terraform
Published: 2024-04-07 14:59:57

You may find my base repository here.

{% embed https://github.com/bervProject/lambda-sharp %}

Please see my previous post for the step-by-step creation of the lambda itself.

## Designing the testing

I may extract some "constant" to variables, such as image_uri, lambda timeout, function_name, etc. After some extraction, we will write "basic" (unit) testing without end-to-end testing.

```terraform
variables {
  lambda_image = "test"
}

run "valid_image" {

  command = plan

  assert {
    condition     = aws_lambda_function.lambda_container_demo.image_uri == "test"
    error_message = "AWS Lambda Function Image URI did not match"
  }

}

run "valid_function_name" {

  command = plan

  assert {
    condition     = aws_lambda_function.lambda_container_demo.function_name == "lambda_container_demo"
    error_message = "AWS Lambda Function Name did not match"
  }

}

run "valid_lambda_timeout" {

  command = plan

  assert {
    condition     = aws_lambda_function.lambda_container_demo.timeout == 60
    error_message = "AWS Lambda timeout did not match"
  }

}

run "valid_dynamo_db_capacity" {

  command = plan

  assert {
    condition     = aws_dynamodb_table.lambda_container_demo_dev.read_capacity == 20
    error_message = "Dynamo DB read capacity did not match"
  }

  assert {
    condition     = aws_dynamodb_table.lambda_container_demo_dev.write_capacity == 20
    error_message = "Dynamo DB write capacity did not match"
  }

}
```

However, it's not challenging, right? I'll share more challenging tests (maybe end-to-end ones) after this post. Stay tuned!

![Stay Tuned GIF](https://media.giphy.com/media/v1.Y2lkPTc5MGI3NjExcng3dmcybDc4YnUwbmwzNGdid3BubTJ4MmUyMDg2Mm10N2J5dm01YiZlcD12MV9pbnRlcm5hbF9naWZfYnlfaWQmY3Q9Zw/MTbya3k32GED2EyPEX/giphy.gif)

You may check this pull request for the changes.

{% embed https://github.com/bervProject/lambda-sharp/pull/64 %}
To make Python 3 the default version on Ubuntu, replacing Python 2
Tags: javascript, tutorial, python, ai
Published: 2024-04-07 14:54:56

Here's a step-by-step guide:

1. Open a terminal.
2. Install the `python-is-python3` package which will set Python 3 as the default when you use the `python` command:
  bash
   sudo apt install python-is-python3
3. If you have multiple versions of Python 3 and want to set a specific one (e.g., Python 3.8) as the default for the `python3` command, use `update-alternatives`:
   ```bash
   sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.8 1
   ```
4. You can then configure the default version by running:
   ```bash
   sudo update-alternatives --config python3
   ```
   Follow the on-screen instructions to select the default Python 3 version.

5. Verify the change by checking the Python version:
   ```bash
   python3 --version
   ```
How To Create A Virtual Machine
Tags: tutorial, programming, cloud, github
Published: 2024-04-07 14:51:23

## What is a Virtual Machine
A virtual machine is a software-based emulation of a physical computer. It allows you to run multiple operating systems on a single physical machine, enabling you to isolate and consolidate resources, run different software environments, and enhance flexibility and scalability in computing environments.

## How to create a Virtual Machine
Step 1
sign in to Azure account
![azure portal](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zp5sunqvbmzs7f56wbx6.png)
Step 2
on the Azure portal search bar, search for Virtual Machine and click.
![azure search bar](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/n0pjirey3gvezmefdrsr.png)
Step 3
click the + create to create a Virtual Machine
![+ create ](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/eyii62egi4yudytoz7j5.png)
step 4
click Azure Virtual machine to continue
![Virtual machine create](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nrc2ni6ii42sm0cm581g.png)
Step 5
enter Subscription type and click create new Resource Group 
![subscription and create](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ycn3ohtcut04bvm6vc0a.png)
Step 6
input the Virtual Machine name, choose the region and availability zone
![VM instances](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/x2kjvyjw4ya0ueevsr4c.png)
Step 7
set prefered image windows/linux (ubuntu)
![Image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fhfg6k6y5tjlluokhh0j.png)
Step 8
set prefered size
![size](https://dev-to-
uploads.s3.amazonaws.com/uploads/articles/p76sppw6u7sleag648zk.png
)
Step 9
create user name amd password
![user name and password](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kghp5g9go7zjt2xe0l0n.png)
Step 10
select the prefered inbound port rules
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8bl2q843bgqwu02jevb2.png)
Step 11
confirm license, then review and create
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wnr21676wiol23wsu9y7.png)
[Sparta Coding Club] Backend Week 4 - Today I learned
Tags: webdev, python, fullstack, beginners
Published: 2024-04-07 14:30:46

Hey everyone! Today, I'm excited to share my journey of building the Expense Tracker web application for my final project at the Sparta Coding Club. Let's dive into the key highlights and learnings from this experience!

**1. Getting Started:**
The Expense Tracker project aimed to develop a user-friendly platform for managing finances effectively. From tracking income and expenses to setting monthly budgets, the goal was to create a comprehensive tool for financial management.

**2. Project Layout:**
The application was structured into different pages, including the homepage, dashboard, income/expense logging page, and expense tracking page. Each page served a specific purpose, contributing to the overall functionality of the app.

**3. Must-Have Features:**
Implementing user authentication was crucial to ensure data security. Income and expense logging features were developed with proper validation to maintain data accuracy. The financial overview dashboard provided users with insights into their financial status, while category-wise expense tracking enabled them to monitor spending patterns effectively.

**4. Required Data Sources:**
MongoDB was chosen as the database solution for storing user account information, income/expense data, and budget settings. Additionally, external APIs were considered for implementing multiple currency support, although this feature was optional and not implemented in the current version.

**5. To-Do Lists:**
While the core features were successfully implemented, there were some optional features on the to-do list for future enhancements. These included supporting multiple currencies, integrating goal-setting features, implementing data visualization tools, setting up notification systems, and developing a transaction history feature.

**Reflections:**
Building the Expense Tracker was a challenging yet rewarding experience. It allowed me to apply various concepts and techniques learned throughout the coding club sessions. From frontend development using HTML, CSS, and JavaScript to backend implementation with MongoDB, the project provided valuable hands-on experience in full-stack web development.

**Screenshots:**


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9dg0fl2wzcgvewier3bu.png)



![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/upcmzsi7okhpxhexmn4o.png)




**Closing Thoughts:**
Overall, creating the Expense Tracker was an enriching journey that taught me the importance of effective financial management and the power of technology in addressing real-world challenges. I look forward to further refining and expanding the application in the future, leveraging new skills and insights gained along the way.

That's all for now! Stay tuned for more updates on my coding adventures. Until next time, happy coding! 🚀✨
Service management in Linux
Tags: linux
Published: 2024-04-07 14:17:33

People who use Linux do know how to start/stop services with the front-end command line utilities like _systemctl_ but they don’t really appreciate the stuff that happens behind the scenes just to start a service that you want to be already started.  

In this blogpost, let me tell you all how service management happens in Linux operating systems, so you might appreciate this wonderful aspect of Linux.

Operating systems manage applications and services for you that run in foreground and background. Windows does in its own way. But Linux, the rock solid operating system for servers does this in a way that’s so awesome.

If you didn’t know how a system gets booted up, let me tell you in a nutshell.
First off, when you press that little plastic button on your machine, it starts with the whooshing sound of cooling fans. Then the system goes through something known as POST_(_Power-On-Self-Test) process which is actually a set of diagnostic tests that a computer runs every time it's powered on to make sure all of its components are working properly. Next, the BIOS _(if it’s an older system)_ or UEFI _(newer ones use this)_ starts up, which then look for the boot files especially the kernel. The kernel kicks in and starts the services.

But how does the kernel know the services it has to start, the ones it has to start on-boot , the ones which you wanna start on-demand?

That’s where a service management system comes into play!

Service management system is the one that actually starts up the services on-boot. I’m saying services but that doesn’t just include applications like MySQL, Apache web server engines. But the word services encompasses the networking stack, Storage I/O service, LVM ,NFS, anything you could think of. 

Think of it. You just have an on-demand application like Firefox installed but without the TCP/IP networking stack started, can you even get anywhere? Or you have a hard-drive installed. If you can’t perform any I/O operations on it, what’s the purpose then?
Kernel deals with the hardware interaction. These service management systems deal with the running of software on top of the kernel. These are so important that if you’d close this process system, the entire system would crash.

With Linux, there are two service management systems which are **Sysinit** and **systemd**, namely.


**Sysint** is the good, old guy who’s been around for a long time. His birth dates back to the good, old 80’s when pure Unix, Slackware systems were present. Don’t get me wrong. Slackware is still there but the last release was actually in 2016. The last version is the **SysVinit** _(you gotta pronounce it like System-Five-init)_. 
**Systemd** is the new kid in the block. Actually ,he’s prevalent from the year 2010. systemd does things way better, faster than sysinit but there are some gotchas to systemd as well.

I hope you understood how services and applications are managed in Linux. In the next blogpost, let’s take a look at how these two systems manage things in their own way.

Till then, be happy and keep learning. See ya!
Cancel Sync / Import from Other Mail to Gmail Account
Tags: gmail, email, imports, synchronization
Published: 2024-04-07 14:10:52

Have you ever linked your another mail (i.e. Yahoo Mail, Outlook, etc.) to your Gmail account so that all mails in that another mail _imported_ to your Gmail? Now, are you wondering about how to cancel it? This article would answer that kind of question!

Let's do it step by step:

1. Open Gmail in browser (Desktop mode is preferred)

2. After the Gmail page loaded, click on the **Settings** icon.

    ![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/c8vfgnzvugc1681jodky.png)

3. Next, click on the **See All Settings**

    ![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ac53be9290puy8wc35ht.png)

4. After that, go to the **Accounts and Import** tab, then finally click on **unlink** button at the field **Check mail from other accounts** for the respective email you would like to unlink. 

    ![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/o609ayw6a05umyb4gxbm.png)


Congratulations, now your gmail account will not import any message from the other mail that you don't want to sync. Let me know if you have any difficulties on doing this. Also, please kindly react this post if you feel this is helpful.
Scalability and resilience
Published: 2024-04-07 14:02:04

- **Elasticsearch Scalability and Resilience**:
  - Elasticsearch is distributed by nature, ensuring high availability and scalability.
  - Add servers (nodes) to a cluster to increase capacity; Elasticsearch distributes data and queries across nodes.
  - No application overhaul needed; Elasticsearch balances multi-node clusters automatically.
  - Under the hood, an index consists of one or more physical shards, each being a self-contained index.
  - Shards distribute documents across nodes for redundancy and query capacity.
  - Two types of shards: primaries (store documents) and replicas (copies of primaries).
  - Replicas serve read requests (searching or retrieving documents).

- **Shard Considerations**:
  - Shard size and number impact performance.
  - Aim for average shard size between a few GB and a few tens of GB.
  - Avoid excessive shards per node (proportional to heap space).
  - Test with your own data to find optimal configurations.

- **Cross-Cluster Replication (CCR)**:
  - Synchronize indices from primary to secondary remote clusters.
  - Secondary clusters serve as hot backups or read-only followers.
  - Active-passive replication; primary handles write requests, secondary is read-only.
  - Use CCR for disaster recovery and geo-proximity read requests.