Subject: Finding error in long input file

From: Rich Shepard <[email-masked]>
Date: Tue, 9 Jul 2024 15:14:13 -0700 (PDT)

I've a file with 488 rows to be input into a table. I run the script using
psql with the `\i' option.

After psql found a few missing commas I thought the script was clean. But,
psql returned this error:
bustrac=# \i scripts/insert-addrs.sql
psql:scripts/insert-addrs.sql:488: ERROR:  syntax error at or near ";"
LINE 488: ...2832,1,default,'85250 Red House Rd','Paisley','OR','97636');
                                                                         ^ Am
I correct that some rows were inserted into the table before this error was
generated somewhere prior to the end of the file? If so, how do I determine
which rows were inserted and what is the recommended procedure to locate
just where that error is?

I've learned from experience that using the old coding approach of dividing
the file in half, then half again and again until the row with the error is
found produces multiple rows with different PKs. Not what I want to clean
up.

A clue stick will be much appreciated.

TIA,

Rich

---

From: "Ray O'Donnell" <[email-masked]>
Date: Tue, 09 Jul 2024 23:26:33 +0100

> just where that error is?

Did you run the entire thing inside a transaction? If so, then it will have 
been rolled back after the error, and no rows will have been inserted.

Ray.

---

From: Adrian Klaver <[email-masked]>
Date: Tue, 9 Jul 2024 15:29:41 -0700

> A clue stick will be much appreciated.

bustrac=#\e scripts/insert-addrs.sql 488

If that raises this error:

environment variable PSQL_EDITOR_LINENUMBER_ARG must be set to specify a 
line number

then:

bustrac=#\e scripts/insert-addrs.sql

If the editor is vi then:

:488

-- 
Adrian Klaver
[email-masked]

---

From: Rich Shepard <[email-masked]>
Date: Tue, 9 Jul 2024 15:33:03 -0700 (PDT)

> been rolled back after the error, and no rows will have been inserted.

Ray,

When I tried using transactions they froze the application. Probably because
I don't know to properly invoke them.

Thanks

Rich

---

From: Rich Shepard <[email-masked]>
Date: Tue, 9 Jul 2024 15:40:33 -0700 (PDT)

On Tue, 9 Jul 2024, Adrian Klaver wrote:

> environment variable PSQL_EDITOR_LINENUMBER_ARG must be set to specify a line 
> number

Adrian,

It doesn't; it prints the contents of the file to the virtual terminal.
Using my default small editor, joe, I scrolled back through the file
displayed without seeing any error.

Should I run BEGIN: at the psql prompt prior to entering the insert command?
Would that tell me if any rows were entered and, if so, where that stopped?

TIA,

Rich

---

From: Rich Shepard <[email-masked]>
Date: Tue, 9 Jul 2024 15:46:54 -0700 (PDT)

On Tue, 9 Jul 2024, Rich Shepard wrote:

> Should I run BEGIN: at the psql prompt prior to entering the insert
> command? Would that tell me if any rows were entered and, if so, where
> that stopped?

Began a transction, ran the script, checked the locations table for a couple
of rows to be inserted. They weren't. Issued the rollback; command.

How best should I proceed to isolate the source of the error at the end of
the file?

Rich

---

From: Adrian Klaver <[email-masked]>
Date: Tue, 9 Jul 2024 15:52:54 -0700

On 7/9/24 15:40, Rich Shepard wrote:
>> environment variable PSQL_EDITOR_LINENUMBER_ARG must be set to specify 
>> a line number
> 
> Adrian,
> 

The error:

LINE 488: ...2832,1,default,'85250 Red House Rd','Paisley','OR','97636')

is giving you the line number and the data:
a) Navigate to that line number using whatever method Joe has for that.
b) Search for '85250 Red House Rd'.

> command?

---

From: Craig McIlwee <[email-masked]>
Date: Tue, 9 Jul 2024 20:46:45 -0400

Full error message from earlier in the thread:

>                                                                          ^

> b) Search for '85250 Red House Rd'.

The input file is 488 lines (presumably, since Rich said the file should
insert 488 rows).  It seems like too much of a coincidence that the last
character of the last line is really the error.  My guess is that there is
an unmatched character, perhaps a parenthesis, that is throwing off the
parser because it doesn't expect the statement to terminate yet.  Maybe
that unmatched char really is on the last line, but '85250 Red House Rd'
doesn't seem like the issue.  I don't know anything about the joe editor,
but I'd hope that any decent editor with syntax highlighting would make it
apparent where things went awry.

Craig

---

From: Adrian Klaver <[email-masked]>
Date: Tue, 9 Jul 2024 21:58:07 -0700

On 7/9/24 17:46, Craig McIlwee wrote:
> character of the last line is really the error.  My guess is that there 

This assumes that there where only INSERT lines and that each INSERT was 
only one line. I have bit by those assumptions before, hence my 
suggestion to actually find line 488.

> is an unmatched character, perhaps a parenthesis, that is throwing off 
> the parser because it doesn't expect the statement to terminate yet.  
> Maybe that unmatched char really is on the last line, but '85250 Red 
> House Rd' doesn't seem like the issue.  I don't know anything about the 
> joe editor, but I'd hope that any decent editor with syntax highlighting 
> would make it apparent where things went awry.
> 
> Craig

---

From: Alban Hertroys <[email-masked]>
Date: Wed, 10 Jul 2024 08:57:52 +0200

> On 10 Jul 2024, at 06:58, Adrian Klaver <[email-masked]> wrote:
> 
>> The input file is 488 lines (presumably, since Rich said the file should insert 488 rows).  It seems like too much of a coincidence that the last character of the last line is really the error.  My guess is that there 
> 
> This assumes that there where only INSERT lines and that each INSERT was only one line. I have bit by those assumptions before, hence my suggestion to actually find line 488.
> 
>> is an unmatched character, perhaps a parenthesis, that is throwing off the parser because it doesn't expect the statement to terminate yet.  Maybe that unmatched char really is on the last line, but '85250 Red House Rd' doesn't seem like the issue.  I don't know anything about the joe editor, but I'd hope that any decent editor with syntax highlighting would make it apparent where things went awry.
>> Craig
> 

Is this a single INSERT statement with multiple tuples after VALUES? Then perhaps an earlier line (my bet would be on line 487) accidentally ends with a semi-colon instead of a comma?

Something like this:

INSERT INTO table (col1, col2, ..., coln) VALUES
(..., ..., ),
(..., ..., ),
(..., ..., ); -- <-- This terminates the INSERT
(..., ..., ); -- <-- Now this line make no sense

Alban Hertroys
--
If you can't see the forest for the trees,
cut the trees and you'll find there is no forest.

---

From: Rich Shepard <[email-masked]>
Date: Wed, 10 Jul 2024 05:30:59 -0700 (PDT)

Adrian,

With the semicolon at the end of the line it's the last line in the file.
That's why I asked for help in finding where, in all the lines above it, the
actual error is located.

FWIW, I use emacs, not vim.

I'll try inserting the data a few lines at a time, each in a transaction.

Thanks,

Rich

---

From: Adrian Klaver <[email-masked]>
Date: Wed, 10 Jul 2024 08:05:59 -0700

> With the semicolon at the end of the line it's the last line in the file.

True, I was fixated on the line number.

> I'll try inserting the data a few lines at a time, each in a transaction.

If it where me I would make the data a CSV file and use \copy. That is 
an all or none operation and you would get a error message showing you 
where in the file the operation failed.