I have two tables. Table logfile contains whole text file in one attribute; table rawtext should contain one line of text from the text file in each row:
create table LOGFILE ( LOGF_PK serial primary key, TEXT_CONTENT text ) ;
create table LOGTEXT ( LT_PK serial primary key,
LOGF_PK integer not null references LOGFILE( LOGF_PK ),
LINENUM integer,
LINETEXT varchar(255)
);
I can split TEXT_CONTENT into a set of rows, but I am unable (don't know how) to generate line numbers in table LOGTEXT, e.g. the first line from LOGFILE.TEXT_CONTENT would be in a row <LT_PK>, <LOGF_PK>, 1, 'the first line of text', etc.
This query returns what I expect to obtain, but without line numbers:
select LOGF_PK, regexp_split_to_table( TEXT_CONTENT, '\r?\n' ) as LINETEXT ;
However, I cannot obtain any line numbers. I tried variant 1:
select LOGF_PK
, row_number() over (partition by LOGFILE.LOGF_PK) as LINENUM
, regexp_split_to_table( TEXT_CONTENT, '\r?\n' )) as LINETEXT
from LOGFILE
In the above example, LINENUM is always 1...
And when I try another possibility, variant 2:
select LOGF_PK
, regexp_split_to_table( TEXT_CONTENT, '\r?\n' )) with ORDINALITY
from LOGFILE
I get error
ERROR: syntax error at or near "with"
LINE 3: , regexp_split_to_array( content, '\r?\n' ) with ordinalit...
^
Unfortunately, there are very few examples of WITH ORDINALITY in PostgreSQL documentation and the few that are there are very general and unexplaining. And I suspect that some examples from documentation have never worked on a real system.
Database is PostgreSQL 12.8
SELECTfrom table with array-type columns. The key is usingFROM x, LATERAL <function returning set>and explaining how use this. There is no such example in the current docs (ie. up to v14), or it is hidden so well that I was unable to find one comprehensive example after several hours of intensive, focused search. IMHO, this feature should be documented better, with a clear example. – Jindrich Vavruska Oct 11 '21 at 19:41LATERALdoes not show the main power of the clause because it refers to a different table. How much explanatory isSELECT * FROM t, LATERAL (SELECT * from b ... etc )? It does not explain anything, it just kind of shows thatJOINandLATERALare more or less the same and does not show any specific benefit ofLATERALcompared toJOIN. Such benefit is, however, available when you need to expand array-type column into a set of rows. That's why this example does not help understanding the feature. – Jindrich Vavruska Oct 11 '21 at 19:58LATERALis not an alternative toJOIN, but a modifier. There is noLATERALwithoutJOIN. Don't get confused by the syntax shorthand, LATERAL(or even just,before table-functions in theFROMlist, both are just short forCROSS JOIN LATERAL. There is a link in my answer to detailed explanation. – Erwin Brandstetter Oct 11 '21 at 21:22SELECT logf_pk, x.* FROM logfile l, regexp_split_to_table(l.text_content, '\r?\n') WITH ORDINALITY x(linetext, linenum);. There is no "different" table. jkavalik and a_horse posted queries to that effect in their answers - one which should be the accepted answer. – Erwin Brandstetter Oct 11 '21 at 21:23